-
Notifications
You must be signed in to change notification settings - Fork 2
/
isl_coalesce.c
3944 lines (3502 loc) · 119 KB
/
isl_coalesce.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
* Copyright 2010 INRIA Saclay
* Copyright 2012-2013 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
* Copyright 2016 INRIA Paris
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
* and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
* ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
* and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
* and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
* CS 42112, 75589 Paris Cedex 12, France
*/
#include <isl_ctx_private.h>
#include "isl_map_private.h"
#include <isl_seq.h>
#include <isl/options.h>
#include "isl_tab.h"
#include <isl_mat_private.h>
#include <isl_local_space_private.h>
#include <isl_val_private.h>
#include <isl_vec_private.h>
#include <isl_aff_private.h>
#include <isl_equalities.h>
#include <isl_constraint_private.h>
#include <set_to_map.c>
#include <set_from_map.c>
#define STATUS_ERROR -1
#define STATUS_REDUNDANT 1
#define STATUS_VALID 2
#define STATUS_SEPARATE 3
#define STATUS_CUT 4
#define STATUS_ADJ_EQ 5
#define STATUS_ADJ_INEQ 6
static int status_in(isl_int *ineq, struct isl_tab *tab)
{
enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
switch (type) {
default:
case isl_ineq_error: return STATUS_ERROR;
case isl_ineq_redundant: return STATUS_VALID;
case isl_ineq_separate: return STATUS_SEPARATE;
case isl_ineq_cut: return STATUS_CUT;
case isl_ineq_adj_eq: return STATUS_ADJ_EQ;
case isl_ineq_adj_ineq: return STATUS_ADJ_INEQ;
}
}
/* Compute the position of the equalities of basic map "bmap_i"
* with respect to the basic map represented by "tab_j".
* The resulting array has twice as many entries as the number
* of equalities corresponding to the two inequalities to which
* each equality corresponds.
*/
static int *eq_status_in(__isl_keep isl_basic_map *bmap_i,
struct isl_tab *tab_j)
{
int k, l;
int *eq = isl_calloc_array(bmap_i->ctx, int, 2 * bmap_i->n_eq);
unsigned dim;
if (!eq)
return NULL;
dim = isl_basic_map_total_dim(bmap_i);
for (k = 0; k < bmap_i->n_eq; ++k) {
for (l = 0; l < 2; ++l) {
isl_seq_neg(bmap_i->eq[k], bmap_i->eq[k], 1+dim);
eq[2 * k + l] = status_in(bmap_i->eq[k], tab_j);
if (eq[2 * k + l] == STATUS_ERROR)
goto error;
}
}
return eq;
error:
free(eq);
return NULL;
}
/* Compute the position of the inequalities of basic map "bmap_i"
* (also represented by "tab_i", if not NULL) with respect to the basic map
* represented by "tab_j".
*/
static int *ineq_status_in(__isl_keep isl_basic_map *bmap_i,
struct isl_tab *tab_i, struct isl_tab *tab_j)
{
int k;
unsigned n_eq = bmap_i->n_eq;
int *ineq = isl_calloc_array(bmap_i->ctx, int, bmap_i->n_ineq);
if (!ineq)
return NULL;
for (k = 0; k < bmap_i->n_ineq; ++k) {
if (tab_i && isl_tab_is_redundant(tab_i, n_eq + k)) {
ineq[k] = STATUS_REDUNDANT;
continue;
}
ineq[k] = status_in(bmap_i->ineq[k], tab_j);
if (ineq[k] == STATUS_ERROR)
goto error;
if (ineq[k] == STATUS_SEPARATE)
break;
}
return ineq;
error:
free(ineq);
return NULL;
}
static int any(int *con, unsigned len, int status)
{
int i;
for (i = 0; i < len ; ++i)
if (con[i] == status)
return 1;
return 0;
}
/* Return the first position of "status" in the list "con" of length "len".
* Return -1 if there is no such entry.
*/
static int find(int *con, unsigned len, int status)
{
int i;
for (i = 0; i < len ; ++i)
if (con[i] == status)
return i;
return -1;
}
static int count(int *con, unsigned len, int status)
{
int i;
int c = 0;
for (i = 0; i < len ; ++i)
if (con[i] == status)
c++;
return c;
}
static int all(int *con, unsigned len, int status)
{
int i;
for (i = 0; i < len ; ++i) {
if (con[i] == STATUS_REDUNDANT)
continue;
if (con[i] != status)
return 0;
}
return 1;
}
/* Internal information associated to a basic map in a map
* that is to be coalesced by isl_map_coalesce.
*
* "bmap" is the basic map itself (or NULL if "removed" is set)
* "tab" is the corresponding tableau (or NULL if "removed" is set)
* "hull_hash" identifies the affine space in which "bmap" lives.
* "removed" is set if this basic map has been removed from the map
* "simplify" is set if this basic map may have some unknown integer
* divisions that were not present in the input basic maps. The basic
* map should then be simplified such that we may be able to find
* a definition among the constraints.
*
* "eq" and "ineq" are only set if we are currently trying to coalesce
* this basic map with another basic map, in which case they represent
* the position of the inequalities of this basic map with respect to
* the other basic map. The number of elements in the "eq" array
* is twice the number of equalities in the "bmap", corresponding
* to the two inequalities that make up each equality.
*/
struct isl_coalesce_info {
isl_basic_map *bmap;
struct isl_tab *tab;
uint32_t hull_hash;
int removed;
int simplify;
int *eq;
int *ineq;
};
/* Is there any (half of an) equality constraint in the description
* of the basic map represented by "info" that
* has position "status" with respect to the other basic map?
*/
static int any_eq(struct isl_coalesce_info *info, int status)
{
unsigned n_eq;
n_eq = isl_basic_map_n_equality(info->bmap);
return any(info->eq, 2 * n_eq, status);
}
/* Is there any inequality constraint in the description
* of the basic map represented by "info" that
* has position "status" with respect to the other basic map?
*/
static int any_ineq(struct isl_coalesce_info *info, int status)
{
unsigned n_ineq;
n_ineq = isl_basic_map_n_inequality(info->bmap);
return any(info->ineq, n_ineq, status);
}
/* Return the position of the first half on an equality constraint
* in the description of the basic map represented by "info" that
* has position "status" with respect to the other basic map.
* The returned value is twice the position of the equality constraint
* plus zero for the negative half and plus one for the positive half.
* Return -1 if there is no such entry.
*/
static int find_eq(struct isl_coalesce_info *info, int status)
{
unsigned n_eq;
n_eq = isl_basic_map_n_equality(info->bmap);
return find(info->eq, 2 * n_eq, status);
}
/* Return the position of the first inequality constraint in the description
* of the basic map represented by "info" that
* has position "status" with respect to the other basic map.
* Return -1 if there is no such entry.
*/
static int find_ineq(struct isl_coalesce_info *info, int status)
{
unsigned n_ineq;
n_ineq = isl_basic_map_n_inequality(info->bmap);
return find(info->ineq, n_ineq, status);
}
/* Return the number of (halves of) equality constraints in the description
* of the basic map represented by "info" that
* have position "status" with respect to the other basic map.
*/
static int count_eq(struct isl_coalesce_info *info, int status)
{
unsigned n_eq;
n_eq = isl_basic_map_n_equality(info->bmap);
return count(info->eq, 2 * n_eq, status);
}
/* Return the number of inequality constraints in the description
* of the basic map represented by "info" that
* have position "status" with respect to the other basic map.
*/
static int count_ineq(struct isl_coalesce_info *info, int status)
{
unsigned n_ineq;
n_ineq = isl_basic_map_n_inequality(info->bmap);
return count(info->ineq, n_ineq, status);
}
/* Are all non-redundant constraints of the basic map represented by "info"
* either valid or cut constraints with respect to the other basic map?
*/
static int all_valid_or_cut(struct isl_coalesce_info *info)
{
int i;
for (i = 0; i < 2 * info->bmap->n_eq; ++i) {
if (info->eq[i] == STATUS_REDUNDANT)
continue;
if (info->eq[i] == STATUS_VALID)
continue;
if (info->eq[i] == STATUS_CUT)
continue;
return 0;
}
for (i = 0; i < info->bmap->n_ineq; ++i) {
if (info->ineq[i] == STATUS_REDUNDANT)
continue;
if (info->ineq[i] == STATUS_VALID)
continue;
if (info->ineq[i] == STATUS_CUT)
continue;
return 0;
}
return 1;
}
/* Compute the hash of the (apparent) affine hull of info->bmap (with
* the existentially quantified variables removed) and store it
* in info->hash.
*/
static int coalesce_info_set_hull_hash(struct isl_coalesce_info *info)
{
isl_basic_map *hull;
unsigned n_div;
hull = isl_basic_map_copy(info->bmap);
hull = isl_basic_map_plain_affine_hull(hull);
n_div = isl_basic_map_dim(hull, isl_dim_div);
hull = isl_basic_map_drop_constraints_involving_dims(hull,
isl_dim_div, 0, n_div);
info->hull_hash = isl_basic_map_get_hash(hull);
isl_basic_map_free(hull);
return hull ? 0 : -1;
}
/* Free all the allocated memory in an array
* of "n" isl_coalesce_info elements.
*/
static void clear_coalesce_info(int n, struct isl_coalesce_info *info)
{
int i;
if (!info)
return;
for (i = 0; i < n; ++i) {
isl_basic_map_free(info[i].bmap);
isl_tab_free(info[i].tab);
}
free(info);
}
/* Drop the basic map represented by "info".
* That is, clear the memory associated to the entry and
* mark it as having been removed.
*/
static void drop(struct isl_coalesce_info *info)
{
info->bmap = isl_basic_map_free(info->bmap);
isl_tab_free(info->tab);
info->tab = NULL;
info->removed = 1;
}
/* Exchange the information in "info1" with that in "info2".
*/
static void exchange(struct isl_coalesce_info *info1,
struct isl_coalesce_info *info2)
{
struct isl_coalesce_info info;
info = *info1;
*info1 = *info2;
*info2 = info;
}
/* This type represents the kind of change that has been performed
* while trying to coalesce two basic maps.
*
* isl_change_none: nothing was changed
* isl_change_drop_first: the first basic map was removed
* isl_change_drop_second: the second basic map was removed
* isl_change_fuse: the two basic maps were replaced by a new basic map.
*/
enum isl_change {
isl_change_error = -1,
isl_change_none = 0,
isl_change_drop_first,
isl_change_drop_second,
isl_change_fuse,
};
/* Update "change" based on an interchange of the first and the second
* basic map. That is, interchange isl_change_drop_first and
* isl_change_drop_second.
*/
static enum isl_change invert_change(enum isl_change change)
{
switch (change) {
case isl_change_error:
return isl_change_error;
case isl_change_none:
return isl_change_none;
case isl_change_drop_first:
return isl_change_drop_second;
case isl_change_drop_second:
return isl_change_drop_first;
case isl_change_fuse:
return isl_change_fuse;
}
return isl_change_error;
}
/* Add the valid constraints of the basic map represented by "info"
* to "bmap". "len" is the size of the constraints.
* If only one of the pair of inequalities that make up an equality
* is valid, then add that inequality.
*/
static __isl_give isl_basic_map *add_valid_constraints(
__isl_take isl_basic_map *bmap, struct isl_coalesce_info *info,
unsigned len)
{
int k, l;
if (!bmap)
return NULL;
for (k = 0; k < info->bmap->n_eq; ++k) {
if (info->eq[2 * k] == STATUS_VALID &&
info->eq[2 * k + 1] == STATUS_VALID) {
l = isl_basic_map_alloc_equality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->eq[l], info->bmap->eq[k], len);
} else if (info->eq[2 * k] == STATUS_VALID) {
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_neg(bmap->ineq[l], info->bmap->eq[k], len);
} else if (info->eq[2 * k + 1] == STATUS_VALID) {
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->ineq[l], info->bmap->eq[k], len);
}
}
for (k = 0; k < info->bmap->n_ineq; ++k) {
if (info->ineq[k] != STATUS_VALID)
continue;
l = isl_basic_map_alloc_inequality(bmap);
if (l < 0)
return isl_basic_map_free(bmap);
isl_seq_cpy(bmap->ineq[l], info->bmap->ineq[k], len);
}
return bmap;
}
/* Is "bmap" defined by a number of (non-redundant) constraints that
* is greater than the number of constraints of basic maps i and j combined?
* Equalities are counted as two inequalities.
*/
static int number_of_constraints_increases(int i, int j,
struct isl_coalesce_info *info,
__isl_keep isl_basic_map *bmap, struct isl_tab *tab)
{
int k, n_old, n_new;
n_old = 2 * info[i].bmap->n_eq + info[i].bmap->n_ineq;
n_old += 2 * info[j].bmap->n_eq + info[j].bmap->n_ineq;
n_new = 2 * bmap->n_eq;
for (k = 0; k < bmap->n_ineq; ++k)
if (!isl_tab_is_redundant(tab, bmap->n_eq + k))
++n_new;
return n_new > n_old;
}
/* Replace the pair of basic maps i and j by the basic map bounded
* by the valid constraints in both basic maps and the constraints
* in extra (if not NULL).
* Place the fused basic map in the position that is the smallest of i and j.
*
* If "detect_equalities" is set, then look for equalities encoded
* as pairs of inequalities.
* If "check_number" is set, then the original basic maps are only
* replaced if the total number of constraints does not increase.
* While the number of integer divisions in the two basic maps
* is assumed to be the same, the actual definitions may be different.
* We only copy the definition from one of the basic map if it is
* the same as that of the other basic map. Otherwise, we mark
* the integer division as unknown and simplify the basic map
* in an attempt to recover the integer division definition.
*/
static enum isl_change fuse(int i, int j, struct isl_coalesce_info *info,
__isl_keep isl_mat *extra, int detect_equalities, int check_number)
{
int k, l;
struct isl_basic_map *fused = NULL;
struct isl_tab *fused_tab = NULL;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
unsigned extra_rows = extra ? extra->n_row : 0;
unsigned n_eq, n_ineq;
int simplify = 0;
if (j < i)
return fuse(j, i, info, extra, detect_equalities, check_number);
n_eq = info[i].bmap->n_eq + info[j].bmap->n_eq;
n_ineq = info[i].bmap->n_ineq + info[j].bmap->n_ineq;
fused = isl_basic_map_alloc_space(isl_space_copy(info[i].bmap->dim),
info[i].bmap->n_div, n_eq, n_eq + n_ineq + extra_rows);
fused = add_valid_constraints(fused, &info[i], 1 + total);
fused = add_valid_constraints(fused, &info[j], 1 + total);
if (!fused)
goto error;
if (ISL_F_ISSET(info[i].bmap, ISL_BASIC_MAP_RATIONAL) &&
ISL_F_ISSET(info[j].bmap, ISL_BASIC_MAP_RATIONAL))
ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
for (k = 0; k < info[i].bmap->n_div; ++k) {
int l = isl_basic_map_alloc_div(fused);
if (l < 0)
goto error;
if (isl_seq_eq(info[i].bmap->div[k], info[j].bmap->div[k],
1 + 1 + total)) {
isl_seq_cpy(fused->div[l], info[i].bmap->div[k],
1 + 1 + total);
} else {
isl_int_set_si(fused->div[l][0], 0);
simplify = 1;
}
}
for (k = 0; k < extra_rows; ++k) {
l = isl_basic_map_alloc_inequality(fused);
if (l < 0)
goto error;
isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
}
if (detect_equalities)
fused = isl_basic_map_detect_inequality_pairs(fused, NULL);
fused = isl_basic_map_gauss(fused, NULL);
if (simplify || info[j].simplify) {
fused = isl_basic_map_simplify(fused);
info[i].simplify = 0;
}
fused = isl_basic_map_finalize(fused);
fused_tab = isl_tab_from_basic_map(fused, 0);
if (isl_tab_detect_redundant(fused_tab) < 0)
goto error;
if (check_number &&
number_of_constraints_increases(i, j, info, fused, fused_tab)) {
isl_tab_free(fused_tab);
isl_basic_map_free(fused);
return isl_change_none;
}
isl_basic_map_free(info[i].bmap);
info[i].bmap = fused;
isl_tab_free(info[i].tab);
info[i].tab = fused_tab;
drop(&info[j]);
return isl_change_fuse;
error:
isl_tab_free(fused_tab);
isl_basic_map_free(fused);
return isl_change_error;
}
/* Given a pair of basic maps i and j such that all constraints are either
* "valid" or "cut", check if the facets corresponding to the "cut"
* constraints of i lie entirely within basic map j.
* If so, replace the pair by the basic map consisting of the valid
* constraints in both basic maps.
* Checking whether the facet lies entirely within basic map j
* is performed by checking whether the constraints of basic map j
* are valid for the facet. These tests are performed on a rational
* tableau to avoid the theoretical possibility that a constraint
* that was considered to be a cut constraint for the entire basic map i
* happens to be considered to be a valid constraint for the facet,
* even though it cuts off the same rational points.
*
* To see that we are not introducing any extra points, call the
* two basic maps A and B and the resulting map U and let x
* be an element of U \setminus ( A \cup B ).
* A line connecting x with an element of A \cup B meets a facet F
* of either A or B. Assume it is a facet of B and let c_1 be
* the corresponding facet constraint. We have c_1(x) < 0 and
* so c_1 is a cut constraint. This implies that there is some
* (possibly rational) point x' satisfying the constraints of A
* and the opposite of c_1 as otherwise c_1 would have been marked
* valid for A. The line connecting x and x' meets a facet of A
* in a (possibly rational) point that also violates c_1, but this
* is impossible since all cut constraints of B are valid for all
* cut facets of A.
* In case F is a facet of A rather than B, then we can apply the
* above reasoning to find a facet of B separating x from A \cup B first.
*/
static enum isl_change check_facets(int i, int j,
struct isl_coalesce_info *info)
{
int k, l;
struct isl_tab_undo *snap, *snap2;
unsigned n_eq = info[i].bmap->n_eq;
snap = isl_tab_snap(info[i].tab);
if (isl_tab_mark_rational(info[i].tab) < 0)
return isl_change_error;
snap2 = isl_tab_snap(info[i].tab);
for (k = 0; k < info[i].bmap->n_ineq; ++k) {
if (info[i].ineq[k] != STATUS_CUT)
continue;
if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
return isl_change_error;
for (l = 0; l < info[j].bmap->n_ineq; ++l) {
int stat;
if (info[j].ineq[l] != STATUS_CUT)
continue;
stat = status_in(info[j].bmap->ineq[l], info[i].tab);
if (stat < 0)
return isl_change_error;
if (stat != STATUS_VALID)
break;
}
if (isl_tab_rollback(info[i].tab, snap2) < 0)
return isl_change_error;
if (l < info[j].bmap->n_ineq)
break;
}
if (k < info[i].bmap->n_ineq) {
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return isl_change_none;
}
return fuse(i, j, info, NULL, 0, 0);
}
/* Check if info->bmap contains the basic map represented
* by the tableau "tab".
* For each equality, we check both the constraint itself
* (as an inequality) and its negation. Make sure the
* equality is returned to its original state before returning.
*/
static isl_bool contains(struct isl_coalesce_info *info, struct isl_tab *tab)
{
int k;
unsigned dim;
isl_basic_map *bmap = info->bmap;
dim = isl_basic_map_total_dim(bmap);
for (k = 0; k < bmap->n_eq; ++k) {
int stat;
isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
stat = status_in(bmap->eq[k], tab);
isl_seq_neg(bmap->eq[k], bmap->eq[k], 1 + dim);
if (stat < 0)
return isl_bool_error;
if (stat != STATUS_VALID)
return isl_bool_false;
stat = status_in(bmap->eq[k], tab);
if (stat < 0)
return isl_bool_error;
if (stat != STATUS_VALID)
return isl_bool_false;
}
for (k = 0; k < bmap->n_ineq; ++k) {
int stat;
if (info->ineq[k] == STATUS_REDUNDANT)
continue;
stat = status_in(bmap->ineq[k], tab);
if (stat < 0)
return isl_bool_error;
if (stat != STATUS_VALID)
return isl_bool_false;
}
return isl_bool_true;
}
/* Basic map "i" has an inequality (say "k") that is adjacent
* to some inequality of basic map "j". All the other inequalities
* are valid for "j".
* Check if basic map "j" forms an extension of basic map "i".
*
* Note that this function is only called if some of the equalities or
* inequalities of basic map "j" do cut basic map "i". The function is
* correct even if there are no such cut constraints, but in that case
* the additional checks performed by this function are overkill.
*
* In particular, we replace constraint k, say f >= 0, by constraint
* f <= -1, add the inequalities of "j" that are valid for "i"
* and check if the result is a subset of basic map "j".
* To improve the chances of the subset relation being detected,
* any variable that only attains a single integer value
* in the tableau of "i" is first fixed to that value.
* If the result is a subset, then we know that this result is exactly equal
* to basic map "j" since all its constraints are valid for basic map "j".
* By combining the valid constraints of "i" (all equalities and all
* inequalities except "k") and the valid constraints of "j" we therefore
* obtain a basic map that is equal to their union.
* In this case, there is no need to perform a rollback of the tableau
* since it is going to be destroyed in fuse().
*
*
* |\__ |\__
* | \__ | \__
* | \_ => | \__
* |_______| _ |_________\
*
*
* |\ |\
* | \ | \
* | \ | \
* | | | \
* | ||\ => | \
* | || \ | \
* | || | | |
* |__||_/ |_____/
*/
static enum isl_change is_adj_ineq_extension(int i, int j,
struct isl_coalesce_info *info)
{
int k;
struct isl_tab_undo *snap;
unsigned n_eq = info[i].bmap->n_eq;
unsigned total = isl_basic_map_total_dim(info[i].bmap);
isl_stat r;
isl_bool super;
if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
return isl_change_error;
k = find_ineq(&info[i], STATUS_ADJ_INEQ);
if (k < 0)
isl_die(isl_basic_map_get_ctx(info[i].bmap), isl_error_internal,
"info[i].ineq should have exactly one STATUS_ADJ_INEQ",
return isl_change_error);
snap = isl_tab_snap(info[i].tab);
if (isl_tab_unrestrict(info[i].tab, n_eq + k) < 0)
return isl_change_error;
isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
r = isl_tab_add_ineq(info[i].tab, info[i].bmap->ineq[k]);
isl_seq_neg(info[i].bmap->ineq[k], info[i].bmap->ineq[k], 1 + total);
isl_int_sub_ui(info[i].bmap->ineq[k][0], info[i].bmap->ineq[k][0], 1);
if (r < 0)
return isl_change_error;
for (k = 0; k < info[j].bmap->n_ineq; ++k) {
if (info[j].ineq[k] != STATUS_VALID)
continue;
if (isl_tab_add_ineq(info[i].tab, info[j].bmap->ineq[k]) < 0)
return isl_change_error;
}
if (isl_tab_detect_constants(info[i].tab) < 0)
return isl_change_error;
super = contains(&info[j], info[i].tab);
if (super < 0)
return isl_change_error;
if (super)
return fuse(i, j, info, NULL, 0, 0);
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return isl_change_none;
}
/* Both basic maps have at least one inequality with and adjacent
* (but opposite) inequality in the other basic map.
* Check that there are no cut constraints and that there is only
* a single pair of adjacent inequalities.
* If so, we can replace the pair by a single basic map described
* by all but the pair of adjacent inequalities.
* Any additional points introduced lie strictly between the two
* adjacent hyperplanes and can therefore be integral.
*
* ____ _____
* / ||\ / \
* / || \ / \
* \ || \ => \ \
* \ || / \ /
* \___||_/ \_____/
*
* The test for a single pair of adjancent inequalities is important
* for avoiding the combination of two basic maps like the following
*
* /|
* / |
* /__|
* _____
* | |
* | |
* |___|
*
* If there are some cut constraints on one side, then we may
* still be able to fuse the two basic maps, but we need to perform
* some additional checks in is_adj_ineq_extension.
*/
static enum isl_change check_adj_ineq(int i, int j,
struct isl_coalesce_info *info)
{
int count_i, count_j;
int cut_i, cut_j;
count_i = count_ineq(&info[i], STATUS_ADJ_INEQ);
count_j = count_ineq(&info[j], STATUS_ADJ_INEQ);
if (count_i != 1 && count_j != 1)
return isl_change_none;
cut_i = any_eq(&info[i], STATUS_CUT) || any_ineq(&info[i], STATUS_CUT);
cut_j = any_eq(&info[j], STATUS_CUT) || any_ineq(&info[j], STATUS_CUT);
if (!cut_i && !cut_j && count_i == 1 && count_j == 1)
return fuse(i, j, info, NULL, 0, 0);
if (count_i == 1 && !cut_i)
return is_adj_ineq_extension(i, j, info);
if (count_j == 1 && !cut_j)
return is_adj_ineq_extension(j, i, info);
return isl_change_none;
}
/* Given an affine transformation matrix "T", does row "row" represent
* anything other than a unit vector (possibly shifted by a constant)
* that is not involved in any of the other rows?
*
* That is, if a constraint involves the variable corresponding to
* the row, then could its preimage by "T" have any coefficients
* that are different from those in the original constraint?
*/
static int not_unique_unit_row(__isl_keep isl_mat *T, int row)
{
int i, j;
int len = T->n_col - 1;
i = isl_seq_first_non_zero(T->row[row] + 1, len);
if (i < 0)
return 1;
if (!isl_int_is_one(T->row[row][1 + i]) &&
!isl_int_is_negone(T->row[row][1 + i]))
return 1;
j = isl_seq_first_non_zero(T->row[row] + 1 + i + 1, len - (i + 1));
if (j >= 0)
return 1;
for (j = 1; j < T->n_row; ++j) {
if (j == row)
continue;
if (!isl_int_is_zero(T->row[j][1 + i]))
return 1;
}
return 0;
}
/* Does inequality constraint "ineq" of "bmap" involve any of
* the variables marked in "affected"?
* "total" is the total number of variables, i.e., the number
* of entries in "affected".
*/
static isl_bool is_affected(__isl_keep isl_basic_map *bmap, int ineq,
int *affected, int total)
{
int i;
for (i = 0; i < total; ++i) {
if (!affected[i])
continue;
if (!isl_int_is_zero(bmap->ineq[ineq][1 + i]))
return isl_bool_true;
}
return isl_bool_false;
}
/* Given the compressed version of inequality constraint "ineq"
* of info->bmap in "v", check if the constraint can be tightened,
* where the compression is based on an equality constraint valid
* for info->tab.
* If so, add the tightened version of the inequality constraint
* to info->tab. "v" may be modified by this function.
*
* That is, if the compressed constraint is of the form
*
* m f() + c >= 0
*
* with 0 < c < m, then it is equivalent to
*
* f() >= 0
*
* This means that c can also be subtracted from the original,
* uncompressed constraint without affecting the integer points
* in info->tab. Add this tightened constraint as an extra row
* to info->tab to make this information explicitly available.
*/
static __isl_give isl_vec *try_tightening(struct isl_coalesce_info *info,
int ineq, __isl_take isl_vec *v)
{
isl_ctx *ctx;
isl_stat r;
if (!v)
return NULL;
ctx = isl_vec_get_ctx(v);
isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
if (isl_int_is_zero(ctx->normalize_gcd) ||
isl_int_is_one(ctx->normalize_gcd)) {
return v;
}
v = isl_vec_cow(v);
if (!v)
return NULL;
isl_int_fdiv_r(v->el[0], v->el[0], ctx->normalize_gcd);
if (isl_int_is_zero(v->el[0]))
return v;
if (isl_tab_extend_cons(info->tab, 1) < 0)
return isl_vec_free(v);
isl_int_sub(info->bmap->ineq[ineq][0],
info->bmap->ineq[ineq][0], v->el[0]);
r = isl_tab_add_ineq(info->tab, info->bmap->ineq[ineq]);
isl_int_add(info->bmap->ineq[ineq][0],
info->bmap->ineq[ineq][0], v->el[0]);
if (r < 0)
return isl_vec_free(v);
return v;
}
/* Tighten the (non-redundant) constraints on the facet represented
* by info->tab.
* In particular, on input, info->tab represents the result
* of relaxing the "n" inequality constraints of info->bmap in "relaxed"
* by one, i.e., replacing f_i >= 0 by f_i + 1 >= 0, and then
* replacing the one at index "l" by the corresponding equality,
* i.e., f_k + 1 = 0, with k = relaxed[l].
*
* Compute a variable compression from the equality constraint f_k + 1 = 0
* and use it to tighten the other constraints of info->bmap
* (that is, all constraints that have not been relaxed),
* updating info->tab (and leaving info->bmap untouched).
* The compression handles essentially two cases, one where a variable
* is assigned a fixed value and can therefore be eliminated, and one
* where one variable is a shifted multiple of some other variable and
* can therefore be replaced by that multiple.
* Gaussian elimination would also work for the first case, but for
* the second case, the effectiveness would depend on the order
* of the variables.
* After compression, some of the constraints may have coefficients
* with a common divisor. If this divisor does not divide the constant
* term, then the constraint can be tightened.
* The tightening is performed on the tableau info->tab by introducing
* extra (temporary) constraints.
*
* Only constraints that are possibly affected by the compression are
* considered. In particular, if the constraint only involves variables
* that are directly mapped to a distinct set of other variables, then
* no common divisor can be introduced and no tightening can occur.
*
* It is important to only consider the non-redundant constraints
* since the facet constraint has been relaxed prior to the call
* to this function, meaning that the constraints that were redundant
* prior to the relaxation may no longer be redundant.
* These constraints will be ignored in the fused result, so
* the fusion detection should not exploit them.
*/
static isl_stat tighten_on_relaxed_facet(struct isl_coalesce_info *info,
int n, int *relaxed, int l)
{
unsigned total;
isl_ctx *ctx;
isl_vec *v = NULL;
isl_mat *T;
int i;
int k;
int *affected;
k = relaxed[l];
ctx = isl_basic_map_get_ctx(info->bmap);
total = isl_basic_map_total_dim(info->bmap);
isl_int_add_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
T = isl_mat_sub_alloc6(ctx, info->bmap->ineq, k, 1, 0, 1 + total);
T = isl_mat_variable_compression(T, NULL);
isl_int_sub_ui(info->bmap->ineq[k][0], info->bmap->ineq[k][0], 1);
if (!T)