-
Notifications
You must be signed in to change notification settings - Fork 1
/
isl_coalesce.c
2684 lines (2365 loc) · 79.9 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
*
* 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
*/
#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_vec_private.h>
#include <isl_aff_private.h>
#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 inequalties 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;
}
if (eq[2 * k] == STATUS_SEPARATE ||
eq[2 * k + 1] == STATUS_SEPARATE)
break;
}
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;
}
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;
};
/* 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 schedule for the basic map
* to be simplified 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;
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;
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);
info[i].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);
ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
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);
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;
}
info[i].simplify |= info[j].simplify;
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 int 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 -1;
if (stat != STATUS_VALID)
return 0;
stat = status_in(bmap->eq[k], tab);
if (stat < 0)
return -1;
if (stat != STATUS_VALID)
return 0;
}
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 -1;
if (stat != STATUS_VALID)
return 0;
}
return 1;
}
/* 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".
* If so, 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);
int r;
int super;
if (isl_tab_extend_cons(info[i].tab, 1 + info[j].bmap->n_ineq) < 0)
return isl_change_error;
for (k = 0; k < info[i].bmap->n_ineq; ++k)
if (info[i].ineq[k] == STATUS_ADJ_INEQ)
break;
if (k >= info[i].bmap->n_ineq)
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;
}
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(info[i].ineq, info[i].bmap->n_ineq, STATUS_ADJ_INEQ);
count_j = count(info[j].ineq, info[j].bmap->n_ineq, STATUS_ADJ_INEQ);
if (count_i != 1 && count_j != 1)
return isl_change_none;
cut_i = any(info[i].eq, 2 * info[i].bmap->n_eq, STATUS_CUT) ||
any(info[i].ineq, info[i].bmap->n_ineq, STATUS_CUT);
cut_j = any(info[j].eq, 2 * info[j].bmap->n_eq, STATUS_CUT) ||
any(info[j].ineq, info[j].bmap->n_ineq, 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;
}
/* Basic map "i" has an inequality "k" that is adjacent to some equality
* of basic map "j". All the other inequalities are valid for "j".
* Check if basic map "j" forms an extension of basic map "i".
*
* In particular, we relax constraint "k", compute the corresponding
* facet and check whether it is included in the other basic map.
* If so, we know that relaxing the constraint extends the basic
* map with exactly the other basic map (we already know that this
* other basic map is included in the extension, because there
* were no "cut" inequalities in "i") and we can replace the
* two basic maps by this extension.
* Each integer division that does not have exactly the same
* definition in "i" and "j" is marked unknown and the basic map
* is scheduled to be simplified in an attempt to recover
* the integer division definition.
* Place this extension in the position that is the smallest of i and j.
* ____ _____
* / || / |
* / || / |
* \ || => \ |
* \ || \ |
* \___|| \____|
*/
static enum isl_change is_adj_eq_extension(int i, int j, int k,
struct isl_coalesce_info *info)
{
int change = isl_change_none;
int super;
struct isl_tab_undo *snap, *snap2;
unsigned n_eq = info[i].bmap->n_eq;
if (isl_tab_is_equality(info[i].tab, n_eq + k))
return isl_change_none;
snap = isl_tab_snap(info[i].tab);
if (isl_tab_relax(info[i].tab, n_eq + k) < 0)
return isl_change_error;
snap2 = isl_tab_snap(info[i].tab);
if (isl_tab_select_facet(info[i].tab, n_eq + k) < 0)
return isl_change_error;
super = contains(&info[j], info[i].tab);
if (super < 0)
return isl_change_error;
if (super) {
int l;
unsigned total;
if (isl_tab_rollback(info[i].tab, snap2) < 0)
return isl_change_error;
info[i].bmap = isl_basic_map_cow(info[i].bmap);
if (!info[i].bmap)
return isl_change_error;
total = isl_basic_map_total_dim(info[i].bmap);
for (l = 0; l < info[i].bmap->n_div; ++l)
if (!isl_seq_eq(info[i].bmap->div[l],
info[j].bmap->div[l], 1 + 1 + total)) {
isl_int_set_si(info[i].bmap->div[l][0], 0);
info[i].simplify = 1;
}
isl_int_add_ui(info[i].bmap->ineq[k][0],
info[i].bmap->ineq[k][0], 1);
ISL_F_SET(info[i].bmap, ISL_BASIC_MAP_FINAL);
drop(&info[j]);
if (j < i)
exchange(&info[i], &info[j]);
change = isl_change_fuse;
} else
if (isl_tab_rollback(info[i].tab, snap) < 0)
return isl_change_error;
return change;
}
/* Data structure that keeps track of the wrapping constraints
* and of information to bound the coefficients of those constraints.
*
* bound is set if we want to apply a bound on the coefficients
* mat contains the wrapping constraints
* max is the bound on the coefficients (if bound is set)
*/
struct isl_wraps {
int bound;
isl_mat *mat;
isl_int max;
};
/* Update wraps->max to be greater than or equal to the coefficients
* in the equalities and inequalities of info->bmap that can be removed
* if we end up applying wrapping.
*/
static void wraps_update_max(struct isl_wraps *wraps,
struct isl_coalesce_info *info)
{
int k;
isl_int max_k;
unsigned total = isl_basic_map_total_dim(info->bmap);
isl_int_init(max_k);
for (k = 0; k < info->bmap->n_eq; ++k) {
if (info->eq[2 * k] == STATUS_VALID &&
info->eq[2 * k + 1] == STATUS_VALID)
continue;
isl_seq_abs_max(info->bmap->eq[k] + 1, total, &max_k);
if (isl_int_abs_gt(max_k, wraps->max))
isl_int_set(wraps->max, max_k);
}
for (k = 0; k < info->bmap->n_ineq; ++k) {
if (info->ineq[k] == STATUS_VALID ||
info->ineq[k] == STATUS_REDUNDANT)
continue;
isl_seq_abs_max(info->bmap->ineq[k] + 1, total, &max_k);
if (isl_int_abs_gt(max_k, wraps->max))
isl_int_set(wraps->max, max_k);
}
isl_int_clear(max_k);
}
/* Initialize the isl_wraps data structure.
* If we want to bound the coefficients of the wrapping constraints,
* we set wraps->max to the largest coefficient
* in the equalities and inequalities that can be removed if we end up
* applying wrapping.
*/
static void wraps_init(struct isl_wraps *wraps, __isl_take isl_mat *mat,
struct isl_coalesce_info *info, int i, int j)
{
isl_ctx *ctx;
wraps->bound = 0;
wraps->mat = mat;
if (!mat)
return;
ctx = isl_mat_get_ctx(mat);
wraps->bound = isl_options_get_coalesce_bounded_wrapping(ctx);
if (!wraps->bound)
return;
isl_int_init(wraps->max);
isl_int_set_si(wraps->max, 0);
wraps_update_max(wraps, &info[i]);
wraps_update_max(wraps, &info[j]);
}
/* Free the contents of the isl_wraps data structure.
*/
static void wraps_free(struct isl_wraps *wraps)
{
isl_mat_free(wraps->mat);
if (wraps->bound)
isl_int_clear(wraps->max);
}
/* Is the wrapping constraint in row "row" allowed?
*
* If wraps->bound is set, we check that none of the coefficients
* is greater than wraps->max.
*/
static int allow_wrap(struct isl_wraps *wraps, int row)
{
int i;
if (!wraps->bound)
return 1;
for (i = 1; i < wraps->mat->n_col; ++i)
if (isl_int_abs_gt(wraps->mat->row[row][i], wraps->max))
return 0;
return 1;
}
/* Wrap "ineq" (or its opposite if "negate" is set) around "bound"
* to include "set" and add the result in position "w" of "wraps".
* "len" is the total number of coefficients in "bound" and "ineq".
* Return 1 on success, 0 on failure and -1 on error.
* Wrapping can fail if the result of wrapping is equal to "bound"
* or if we want to bound the sizes of the coefficients and
* the wrapped constraint does not satisfy this bound.
*/
static int add_wrap(struct isl_wraps *wraps, int w, isl_int *bound,
isl_int *ineq, unsigned len, __isl_keep isl_set *set, int negate)
{
isl_seq_cpy(wraps->mat->row[w], bound, len);
if (negate) {
isl_seq_neg(wraps->mat->row[w + 1], ineq, len);
ineq = wraps->mat->row[w + 1];
}
if (!isl_set_wrap_facet(set, wraps->mat->row[w], ineq))
return -1;
if (isl_seq_eq(wraps->mat->row[w], bound, len))
return 0;
if (!allow_wrap(wraps, w))
return 0;
return 1;
}
/* For each constraint in info->bmap that is not redundant (as determined
* by info->tab) and that is not a valid constraint for the other basic map,
* wrap the constraint around "bound" such that it includes the whole
* set "set" and append the resulting constraint to "wraps".
* Note that the constraints that are valid for the other basic map
* will be added to the combined basic map by default, so there is
* no need to wrap them.
* The caller wrap_in_facets even relies on this function not wrapping
* any constraints that are already valid.
* "wraps" is assumed to have been pre-allocated to the appropriate size.
* wraps->n_row is the number of actual wrapped constraints that have
* been added.
* If any of the wrapping problems results in a constraint that is
* identical to "bound", then this means that "set" is unbounded in such
* way that no wrapping is possible. If this happens then wraps->n_row
* is reset to zero.
* Similarly, if we want to bound the coefficients of the wrapping
* constraints and a newly added wrapping constraint does not
* satisfy the bound, then wraps->n_row is also reset to zero.
*/
static int add_wraps(struct isl_wraps *wraps, struct isl_coalesce_info *info,
isl_int *bound, __isl_keep isl_set *set)
{
int l, m;
int w;
int added;
isl_basic_map *bmap = info->bmap;
unsigned len = 1 + isl_basic_map_total_dim(bmap);
w = wraps->mat->n_row;
for (l = 0; l < bmap->n_ineq; ++l) {
if (info->ineq[l] == STATUS_VALID ||
info->ineq[l] == STATUS_REDUNDANT)
continue;
if (isl_seq_is_neg(bound, bmap->ineq[l], len))
continue;
if (isl_seq_eq(bound, bmap->ineq[l], len))
continue;
if (isl_tab_is_redundant(info->tab, bmap->n_eq + l))
continue;
added = add_wrap(wraps, w, bound, bmap->ineq[l], len, set, 0);
if (added < 0)
return -1;
if (!added)
goto unbounded;
++w;
}
for (l = 0; l < bmap->n_eq; ++l) {
if (isl_seq_is_neg(bound, bmap->eq[l], len))
continue;
if (isl_seq_eq(bound, bmap->eq[l], len))
continue;
for (m = 0; m < 2; ++m) {
if (info->eq[2 * l + m] == STATUS_VALID)
continue;
added = add_wrap(wraps, w, bound, bmap->eq[l], len,
set, !m);
if (added < 0)
return -1;
if (!added)
goto unbounded;
++w;
}
}
wraps->mat->n_row = w;
return 0;
unbounded:
wraps->mat->n_row = 0;
return 0;
}
/* Check if the constraints in "wraps" from "first" until the last
* are all valid for the basic set represented by "tab".
* If not, wraps->n_row is set to zero.
*/
static int check_wraps(__isl_keep isl_mat *wraps, int first,
struct isl_tab *tab)
{
int i;
for (i = first; i < wraps->n_row; ++i) {
enum isl_ineq_type type;
type = isl_tab_ineq_type(tab, wraps->row[i]);
if (type == isl_ineq_error)
return -1;
if (type == isl_ineq_redundant)
continue;
wraps->n_row = 0;
return 0;
}
return 0;
}
/* Return a set that corresponds to the non-redundant constraints
* (as recorded in tab) of bmap.
*
* It's important to remove the redundant constraints as some