fix some icc warnings
[platform/upstream/isl.git] / isl_coalesce.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include "isl_map_private.h"
14 #include "isl_seq.h"
15 #include "isl_tab.h"
16
17 #define STATUS_ERROR            -1
18 #define STATUS_REDUNDANT         1
19 #define STATUS_VALID             2
20 #define STATUS_SEPARATE          3
21 #define STATUS_CUT               4
22 #define STATUS_ADJ_EQ            5
23 #define STATUS_ADJ_INEQ          6
24
25 static int status_in(isl_int *ineq, struct isl_tab *tab)
26 {
27         enum isl_ineq_type type = isl_tab_ineq_type(tab, ineq);
28         switch (type) {
29         default:
30         case isl_ineq_error:            return STATUS_ERROR;
31         case isl_ineq_redundant:        return STATUS_VALID;
32         case isl_ineq_separate:         return STATUS_SEPARATE;
33         case isl_ineq_cut:              return STATUS_CUT;
34         case isl_ineq_adj_eq:           return STATUS_ADJ_EQ;
35         case isl_ineq_adj_ineq:         return STATUS_ADJ_INEQ;
36         }
37 }
38
39 /* Compute the position of the equalities of basic map "i"
40  * with respect to basic map "j".
41  * The resulting array has twice as many entries as the number
42  * of equalities corresponding to the two inequalties to which
43  * each equality corresponds.
44  */
45 static int *eq_status_in(struct isl_map *map, int i, int j,
46         struct isl_tab **tabs)
47 {
48         int k, l;
49         int *eq = isl_calloc_array(map->ctx, int, 2 * map->p[i]->n_eq);
50         unsigned dim;
51
52         dim = isl_basic_map_total_dim(map->p[i]);
53         for (k = 0; k < map->p[i]->n_eq; ++k) {
54                 for (l = 0; l < 2; ++l) {
55                         isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
56                         eq[2 * k + l] = status_in(map->p[i]->eq[k], tabs[j]);
57                         if (eq[2 * k + l] == STATUS_ERROR)
58                                 goto error;
59                 }
60                 if (eq[2 * k] == STATUS_SEPARATE ||
61                     eq[2 * k + 1] == STATUS_SEPARATE)
62                         break;
63         }
64
65         return eq;
66 error:
67         free(eq);
68         return NULL;
69 }
70
71 /* Compute the position of the inequalities of basic map "i"
72  * with respect to basic map "j".
73  */
74 static int *ineq_status_in(struct isl_map *map, int i, int j,
75         struct isl_tab **tabs)
76 {
77         int k;
78         unsigned n_eq = map->p[i]->n_eq;
79         int *ineq = isl_calloc_array(map->ctx, int, map->p[i]->n_ineq);
80
81         for (k = 0; k < map->p[i]->n_ineq; ++k) {
82                 if (isl_tab_is_redundant(tabs[i], n_eq + k)) {
83                         ineq[k] = STATUS_REDUNDANT;
84                         continue;
85                 }
86                 ineq[k] = status_in(map->p[i]->ineq[k], tabs[j]);
87                 if (ineq[k] == STATUS_ERROR)
88                         goto error;
89                 if (ineq[k] == STATUS_SEPARATE)
90                         break;
91         }
92
93         return ineq;
94 error:
95         free(ineq);
96         return NULL;
97 }
98
99 static int any(int *con, unsigned len, int status)
100 {
101         int i;
102
103         for (i = 0; i < len ; ++i)
104                 if (con[i] == status)
105                         return 1;
106         return 0;
107 }
108
109 static int count(int *con, unsigned len, int status)
110 {
111         int i;
112         int c = 0;
113
114         for (i = 0; i < len ; ++i)
115                 if (con[i] == status)
116                         c++;
117         return c;
118 }
119
120 static int all(int *con, unsigned len, int status)
121 {
122         int i;
123
124         for (i = 0; i < len ; ++i) {
125                 if (con[i] == STATUS_REDUNDANT)
126                         continue;
127                 if (con[i] != status)
128                         return 0;
129         }
130         return 1;
131 }
132
133 static void drop(struct isl_map *map, int i, struct isl_tab **tabs)
134 {
135         isl_basic_map_free(map->p[i]);
136         isl_tab_free(tabs[i]);
137
138         if (i != map->n - 1) {
139                 map->p[i] = map->p[map->n - 1];
140                 tabs[i] = tabs[map->n - 1];
141         }
142         tabs[map->n - 1] = NULL;
143         map->n--;
144 }
145
146 /* Replace the pair of basic maps i and j by the basic map bounded
147  * by the valid constraints in both basic maps and the constraint
148  * in extra (if not NULL).
149  */
150 static int fuse(struct isl_map *map, int i, int j,
151         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j,
152         __isl_keep isl_mat *extra)
153 {
154         int k, l;
155         struct isl_basic_map *fused = NULL;
156         struct isl_tab *fused_tab = NULL;
157         unsigned total = isl_basic_map_total_dim(map->p[i]);
158         unsigned extra_rows = extra ? extra->n_row : 0;
159
160         fused = isl_basic_map_alloc_dim(isl_dim_copy(map->p[i]->dim),
161                         map->p[i]->n_div,
162                         map->p[i]->n_eq + map->p[j]->n_eq,
163                         map->p[i]->n_ineq + map->p[j]->n_ineq + extra_rows);
164         if (!fused)
165                 goto error;
166
167         for (k = 0; k < map->p[i]->n_eq; ++k) {
168                 if (eq_i && (eq_i[2 * k] != STATUS_VALID ||
169                              eq_i[2 * k + 1] != STATUS_VALID))
170                         continue;
171                 l = isl_basic_map_alloc_equality(fused);
172                 if (l < 0)
173                         goto error;
174                 isl_seq_cpy(fused->eq[l], map->p[i]->eq[k], 1 + total);
175         }
176
177         for (k = 0; k < map->p[j]->n_eq; ++k) {
178                 if (eq_j && (eq_j[2 * k] != STATUS_VALID ||
179                              eq_j[2 * k + 1] != STATUS_VALID))
180                         continue;
181                 l = isl_basic_map_alloc_equality(fused);
182                 if (l < 0)
183                         goto error;
184                 isl_seq_cpy(fused->eq[l], map->p[j]->eq[k], 1 + total);
185         }
186
187         for (k = 0; k < map->p[i]->n_ineq; ++k) {
188                 if (ineq_i[k] != STATUS_VALID)
189                         continue;
190                 l = isl_basic_map_alloc_inequality(fused);
191                 if (l < 0)
192                         goto error;
193                 isl_seq_cpy(fused->ineq[l], map->p[i]->ineq[k], 1 + total);
194         }
195
196         for (k = 0; k < map->p[j]->n_ineq; ++k) {
197                 if (ineq_j[k] != STATUS_VALID)
198                         continue;
199                 l = isl_basic_map_alloc_inequality(fused);
200                 if (l < 0)
201                         goto error;
202                 isl_seq_cpy(fused->ineq[l], map->p[j]->ineq[k], 1 + total);
203         }
204
205         for (k = 0; k < map->p[i]->n_div; ++k) {
206                 int l = isl_basic_map_alloc_div(fused);
207                 if (l < 0)
208                         goto error;
209                 isl_seq_cpy(fused->div[l], map->p[i]->div[k], 1 + 1 + total);
210         }
211
212         for (k = 0; k < extra_rows; ++k) {
213                 l = isl_basic_map_alloc_inequality(fused);
214                 if (l < 0)
215                         goto error;
216                 isl_seq_cpy(fused->ineq[l], extra->row[k], 1 + total);
217         }
218
219         fused = isl_basic_map_gauss(fused, NULL);
220         ISL_F_SET(fused, ISL_BASIC_MAP_FINAL);
221         if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) &&
222             ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
223                 ISL_F_SET(fused, ISL_BASIC_MAP_RATIONAL);
224
225         fused_tab = isl_tab_from_basic_map(fused);
226         if (isl_tab_detect_redundant(fused_tab) < 0)
227                 goto error;
228
229         isl_basic_map_free(map->p[i]);
230         map->p[i] = fused;
231         isl_tab_free(tabs[i]);
232         tabs[i] = fused_tab;
233         drop(map, j, tabs);
234
235         return 1;
236 error:
237         isl_tab_free(fused_tab);
238         isl_basic_map_free(fused);
239         return -1;
240 }
241
242 /* Given a pair of basic maps i and j such that all constraints are either
243  * "valid" or "cut", check if the facets corresponding to the "cut"
244  * constraints of i lie entirely within basic map j.
245  * If so, replace the pair by the basic map consisting of the valid
246  * constraints in both basic maps.
247  *
248  * To see that we are not introducing any extra points, call the
249  * two basic maps A and B and the resulting map U and let x
250  * be an element of U \setminus ( A \cup B ).
251  * Then there is a pair of cut constraints c_1 and c_2 in A and B such that x
252  * violates them.  Let X be the intersection of U with the opposites
253  * of these constraints.  Then x \in X.
254  * The facet corresponding to c_1 contains the corresponding facet of A.
255  * This facet is entirely contained in B, so c_2 is valid on the facet.
256  * However, since it is also (part of) a facet of X, -c_2 is also valid
257  * on the facet.  This means c_2 is saturated on the facet, so c_1 and
258  * c_2 must be opposites of each other, but then x could not violate
259  * both of them.
260  */
261 static int check_facets(struct isl_map *map, int i, int j,
262         struct isl_tab **tabs, int *ineq_i, int *ineq_j)
263 {
264         int k, l;
265         struct isl_tab_undo *snap;
266         unsigned n_eq = map->p[i]->n_eq;
267
268         snap = isl_tab_snap(tabs[i]);
269
270         for (k = 0; k < map->p[i]->n_ineq; ++k) {
271                 if (ineq_i[k] != STATUS_CUT)
272                         continue;
273                 tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
274                 for (l = 0; l < map->p[j]->n_ineq; ++l) {
275                         int stat;
276                         if (ineq_j[l] != STATUS_CUT)
277                                 continue;
278                         stat = status_in(map->p[j]->ineq[l], tabs[i]);
279                         if (stat != STATUS_VALID)
280                                 break;
281                 }
282                 if (isl_tab_rollback(tabs[i], snap) < 0)
283                         return -1;
284                 if (l < map->p[j]->n_ineq)
285                         break;
286         }
287
288         if (k < map->p[i]->n_ineq)
289                 /* BAD CUT PAIR */
290                 return 0;
291         return fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
292 }
293
294 /* Both basic maps have at least one inequality with and adjacent
295  * (but opposite) inequality in the other basic map.
296  * Check that there are no cut constraints and that there is only
297  * a single pair of adjacent inequalities.
298  * If so, we can replace the pair by a single basic map described
299  * by all but the pair of adjacent inequalities.
300  * Any additional points introduced lie strictly between the two
301  * adjacent hyperplanes and can therefore be integral.
302  *
303  *        ____                    _____
304  *       /    ||\                /     \
305  *      /     || \              /       \
306  *      \     ||  \     =>      \        \
307  *       \    ||  /              \       /
308  *        \___||_/                \_____/
309  *
310  * The test for a single pair of adjancent inequalities is important
311  * for avoiding the combination of two basic maps like the following
312  *
313  *       /|
314  *      / |
315  *     /__|
316  *         _____
317  *         |   |
318  *         |   |
319  *         |___|
320  */
321 static int check_adj_ineq(struct isl_map *map, int i, int j,
322         struct isl_tab **tabs, int *ineq_i, int *ineq_j)
323 {
324         int changed = 0;
325
326         if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT) ||
327             any(ineq_j, map->p[j]->n_ineq, STATUS_CUT))
328                 /* ADJ INEQ CUT */
329                 ;
330         else if (count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) == 1 &&
331                  count(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ) == 1)
332                 changed = fuse(map, i, j, tabs, NULL, ineq_i, NULL, ineq_j, NULL);
333         /* else ADJ INEQ TOO MANY */
334
335         return changed;
336 }
337
338 /* Check if basic map "i" contains the basic map represented
339  * by the tableau "tab".
340  */
341 static int contains(struct isl_map *map, int i, int *ineq_i,
342         struct isl_tab *tab)
343 {
344         int k, l;
345         unsigned dim;
346
347         dim = isl_basic_map_total_dim(map->p[i]);
348         for (k = 0; k < map->p[i]->n_eq; ++k) {
349                 for (l = 0; l < 2; ++l) {
350                         int stat;
351                         isl_seq_neg(map->p[i]->eq[k], map->p[i]->eq[k], 1+dim);
352                         stat = status_in(map->p[i]->eq[k], tab);
353                         if (stat != STATUS_VALID)
354                                 return 0;
355                 }
356         }
357
358         for (k = 0; k < map->p[i]->n_ineq; ++k) {
359                 int stat;
360                 if (ineq_i[k] == STATUS_REDUNDANT)
361                         continue;
362                 stat = status_in(map->p[i]->ineq[k], tab);
363                 if (stat != STATUS_VALID)
364                         return 0;
365         }
366         return 1;
367 }
368
369 /* Basic map "i" has an inequality "k" that is adjacent to some equality
370  * of basic map "j".  All the other inequalities are valid for "j".
371  * Check if basic map "j" forms an extension of basic map "i".
372  *
373  * In particular, we relax constraint "k", compute the corresponding
374  * facet and check whether it is included in the other basic map.
375  * If so, we know that relaxing the constraint extends the basic
376  * map with exactly the other basic map (we already know that this
377  * other basic map is included in the extension, because there
378  * were no "cut" inequalities in "i") and we can replace the
379  * two basic maps by thie extension.
380  *        ____                    _____
381  *       /    ||                 /     |
382  *      /     ||                /      |
383  *      \     ||        =>      \      |
384  *       \    ||                 \     |
385  *        \___||                  \____|
386  */
387 static int is_extension(struct isl_map *map, int i, int j, int k,
388         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
389 {
390         int changed = 0;
391         int super;
392         struct isl_tab_undo *snap, *snap2;
393         unsigned n_eq = map->p[i]->n_eq;
394
395         snap = isl_tab_snap(tabs[i]);
396         tabs[i] = isl_tab_relax(tabs[i], n_eq + k);
397         snap2 = isl_tab_snap(tabs[i]);
398         tabs[i] = isl_tab_select_facet(tabs[i], n_eq + k);
399         super = contains(map, j, ineq_j, tabs[i]);
400         if (super) {
401                 if (isl_tab_rollback(tabs[i], snap2) < 0)
402                         return -1;
403                 map->p[i] = isl_basic_map_cow(map->p[i]);
404                 if (!map->p[i])
405                         return -1;
406                 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
407                 ISL_F_SET(map->p[i], ISL_BASIC_MAP_FINAL);
408                 drop(map, j, tabs);
409                 changed = 1;
410         } else
411                 if (isl_tab_rollback(tabs[i], snap) < 0)
412                         return -1;
413
414         return changed;
415 }
416
417 /* For each non-redundant constraint in "bmap" (as determined by "tab"),
418  * wrap the constraint around "bound" such that it includes the whole
419  * set "set" and append the resulting constraint to "wraps".
420  * "wraps" is assumed to have been pre-allocated to the appropriate size.
421  * wraps->n_row is the number of actual wrapped constraints that have
422  * been added.
423  * If any of the wrapping problems results in a constraint that is
424  * identical to "bound", then this means that "set" is unbounded in such
425  * way that no wrapping is possible.  If this happens then wraps->n_row
426  * is reset to zero.
427  */
428 static int add_wraps(__isl_keep isl_mat *wraps, __isl_keep isl_basic_map *bmap,
429         struct isl_tab *tab, isl_int *bound, __isl_keep isl_set *set)
430 {
431         int l;
432         int w;
433         unsigned total = isl_basic_map_total_dim(bmap);
434
435         w = wraps->n_row;
436
437         for (l = 0; l < bmap->n_ineq; ++l) {
438                 if (isl_seq_is_neg(bound, bmap->ineq[l], 1 + total))
439                         continue;
440                 if (isl_seq_eq(bound, bmap->ineq[l], 1 + total))
441                         continue;
442                 if (isl_tab_is_redundant(tab, bmap->n_eq + l))
443                         continue;
444
445                 isl_seq_cpy(wraps->row[w], bound, 1 + total);
446                 if (!isl_set_wrap_facet(set, wraps->row[w], bmap->ineq[l]))
447                         return -1;
448                 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
449                         goto unbounded;
450                 ++w;
451         }
452         for (l = 0; l < bmap->n_eq; ++l) {
453                 if (isl_seq_is_neg(bound, bmap->eq[l], 1 + total))
454                         continue;
455                 if (isl_seq_eq(bound, bmap->eq[l], 1 + total))
456                         continue;
457
458                 isl_seq_cpy(wraps->row[w], bound, 1 + total);
459                 isl_seq_neg(wraps->row[w + 1], bmap->eq[l], 1 + total);
460                 if (!isl_set_wrap_facet(set, wraps->row[w], wraps->row[w + 1]))
461                         return -1;
462                 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
463                         goto unbounded;
464                 ++w;
465
466                 isl_seq_cpy(wraps->row[w], bound, 1 + total);
467                 if (!isl_set_wrap_facet(set, wraps->row[w], bmap->eq[l]))
468                         return -1;
469                 if (isl_seq_eq(wraps->row[w], bound, 1 + total))
470                         goto unbounded;
471                 ++w;
472         }
473
474         wraps->n_row = w;
475         return 0;
476 unbounded:
477         wraps->n_row = 0;
478         return 0;
479 }
480
481 /* Check if the constraints in "wraps" from "first" until the last
482  * are all valid for the basic set represented by "tab".
483  * If not, wraps->n_row is set to zero.
484  */
485 static int check_wraps(__isl_keep isl_mat *wraps, int first,
486         struct isl_tab *tab)
487 {
488         int i;
489
490         for (i = first; i < wraps->n_row; ++i) {
491                 enum isl_ineq_type type;
492                 type = isl_tab_ineq_type(tab, wraps->row[i]);
493                 if (type == isl_ineq_error)
494                         return -1;
495                 if (type == isl_ineq_redundant)
496                         continue;
497                 wraps->n_row = 0;
498                 return 0;
499         }
500
501         return 0;
502 }
503
504 /* Return a set that corresponds to the non-redudant constraints
505  * (as recorded in tab) of bmap.
506  *
507  * It's important to remove the redundant constraints as some
508  * of the other constraints may have been modified after the
509  * constraints were marked redundant.
510  * In particular, a constraint may have been relaxed.
511  * Redundant constraints are ignored when a constraint is relaxed
512  * and should therefore continue to be ignored ever after.
513  * Otherwise, the relaxation might be thwarted by some of
514  * these constraints.
515  */
516 static __isl_give isl_set *set_from_updated_bmap(__isl_keep isl_basic_map *bmap,
517         struct isl_tab *tab)
518 {
519         bmap = isl_basic_map_copy(bmap);
520         bmap = isl_basic_map_cow(bmap);
521         bmap = isl_basic_map_update_from_tab(bmap, tab);
522         return isl_set_from_basic_set(isl_basic_map_underlying_set(bmap));
523 }
524
525 /* Given a basic set i with a constraint k that is adjacent to either the
526  * whole of basic set j or a facet of basic set j, check if we can wrap
527  * both the facet corresponding to k and the facet of j (or the whole of j)
528  * around their ridges to include the other set.
529  * If so, replace the pair of basic sets by their union.
530  *
531  * All constraints of i (except k) are assumed to be valid for j.
532  *
533  * However, the constraints of j may not be valid for i and so
534  * we have to check that the wrapping constraints for j are valid for i.
535  *
536  * In the case where j has a facet adjacent to i, tab[j] is assumed
537  * to have been restricted to this facet, so that the non-redundant
538  * constraints in tab[j] are the ridges of the facet.
539  * Note that for the purpose of wrapping, it does not matter whether
540  * we wrap the ridges of i around the whole of j or just around
541  * the facet since all the other constraints are assumed to be valid for j.
542  * In practice, we wrap to include the whole of j.
543  *        ____                    _____
544  *       /    |                  /     \
545  *      /     ||                /      |
546  *      \     ||        =>      \      |
547  *       \    ||                 \     |
548  *        \___||                  \____|
549  *
550  */
551 static int can_wrap_in_facet(struct isl_map *map, int i, int j, int k,
552         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
553 {
554         int changed = 0;
555         struct isl_mat *wraps = NULL;
556         struct isl_set *set_i = NULL;
557         struct isl_set *set_j = NULL;
558         struct isl_vec *bound = NULL;
559         unsigned total = isl_basic_map_total_dim(map->p[i]);
560         struct isl_tab_undo *snap;
561         int n;
562
563         set_i = set_from_updated_bmap(map->p[i], tabs[i]);
564         set_j = set_from_updated_bmap(map->p[j], tabs[j]);
565         wraps = isl_mat_alloc(map->ctx, 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
566                                         map->p[i]->n_ineq + map->p[j]->n_ineq,
567                                         1 + total);
568         bound = isl_vec_alloc(map->ctx, 1 + total);
569         if (!set_i || !set_j || !wraps || !bound)
570                 goto error;
571
572         isl_seq_cpy(bound->el, map->p[i]->ineq[k], 1 + total);
573         isl_int_add_ui(bound->el[0], bound->el[0], 1);
574
575         isl_seq_cpy(wraps->row[0], bound->el, 1 + total);
576         wraps->n_row = 1;
577
578         if (add_wraps(wraps, map->p[j], tabs[j], bound->el, set_i) < 0)
579                 goto error;
580         if (!wraps->n_row)
581                 goto unbounded;
582
583         snap = isl_tab_snap(tabs[i]);
584
585         tabs[i] = isl_tab_select_facet(tabs[i], map->p[i]->n_eq + k);
586         if (isl_tab_detect_redundant(tabs[i]) < 0)
587                 goto error;
588
589         isl_seq_neg(bound->el, map->p[i]->ineq[k], 1 + total);
590
591         n = wraps->n_row;
592         if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set_j) < 0)
593                 goto error;
594
595         if (isl_tab_rollback(tabs[i], snap) < 0)
596                 goto error;
597         if (check_wraps(wraps, n, tabs[i]) < 0)
598                 goto error;
599         if (!wraps->n_row)
600                 goto unbounded;
601
602         changed = fuse(map, i, j, tabs, eq_i, ineq_i, eq_j, ineq_j, wraps);
603
604 unbounded:
605         isl_mat_free(wraps);
606
607         isl_set_free(set_i);
608         isl_set_free(set_j);
609
610         isl_vec_free(bound);
611
612         return changed;
613 error:
614         isl_vec_free(bound);
615         isl_mat_free(wraps);
616         isl_set_free(set_i);
617         isl_set_free(set_j);
618         return -1;
619 }
620
621 /* Set the is_redundant property of the "n" constraints in "cuts",
622  * except "k" to "v".
623  * This is a fairly tricky operation as it bypasses isl_tab.c.
624  * The reason we want to temporarily mark some constraints redundant
625  * is that we want to ignore them in add_wraps.
626  *
627  * Initially all cut constraints are non-redundant, but the
628  * selection of a facet right before the call to this function
629  * may have made some of them redundant.
630  * Likewise, the same constraints are marked non-redundant
631  * in the second call to this function, before they are officially
632  * made non-redundant again in the subsequent rollback.
633  */
634 static void set_is_redundant(struct isl_tab *tab, unsigned n_eq,
635         int *cuts, int n, int k, int v)
636 {
637         int l;
638
639         for (l = 0; l < n; ++l) {
640                 if (l == k)
641                         continue;
642                 tab->con[n_eq + cuts[l]].is_redundant = v;
643         }
644 }
645
646 /* Given a pair of basic maps i and j such that j stick out
647  * of i at n cut constraints, each time by at most one,
648  * try to compute wrapping constraints and replace the two
649  * basic maps by a single basic map.
650  * The other constraints of i are assumed to be valid for j.
651  *
652  * The facets of i corresponding to the cut constraints are
653  * wrapped around their ridges, except those ridges determined
654  * by any of the other cut constraints.
655  * The intersections of cut constraints need to be ignored
656  * as the result of wrapping on cur constraint around another
657  * would result in a constraint cutting the union.
658  * In each case, the facets are wrapped to include the union
659  * of the two basic maps.
660  *
661  * The pieces of j that lie at an offset of exactly one from
662  * one of the cut constraints of i are wrapped around their edges.
663  * Here, there is no need to ignore intersections because we
664  * are wrapping around the union of the two basic maps.
665  *
666  * If any wrapping fails, i.e., if we cannot wrap to touch
667  * the union, then we give up.
668  * Otherwise, the pair of basic maps is replaced by their union.
669  */
670 static int wrap_in_facets(struct isl_map *map, int i, int j,
671         int *cuts, int n, struct isl_tab **tabs,
672         int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
673 {
674         int changed = 0;
675         isl_mat *wraps = NULL;
676         isl_set *set = NULL;
677         isl_vec *bound = NULL;
678         unsigned total = isl_basic_map_total_dim(map->p[i]);
679         int max_wrap;
680         int k;
681         struct isl_tab_undo *snap_i, *snap_j;
682
683         if (isl_tab_extend_cons(tabs[j], 1) < 0)
684                 goto error;
685
686         max_wrap = 2 * (map->p[i]->n_eq + map->p[j]->n_eq) +
687                     map->p[i]->n_ineq + map->p[j]->n_ineq;
688         max_wrap *= n;
689
690         set = isl_set_union(set_from_updated_bmap(map->p[i], tabs[i]),
691                             set_from_updated_bmap(map->p[j], tabs[j]));
692         wraps = isl_mat_alloc(map->ctx, max_wrap, 1 + total);
693         bound = isl_vec_alloc(map->ctx, 1 + total);
694         if (!set || !wraps || !bound)
695                 goto error;
696
697         snap_i = isl_tab_snap(tabs[i]);
698         snap_j = isl_tab_snap(tabs[j]);
699
700         wraps->n_row = 0;
701
702         for (k = 0; k < n; ++k) {
703                 tabs[i] = isl_tab_select_facet(tabs[i],
704                                                 map->p[i]->n_eq + cuts[k]);
705                 if (isl_tab_detect_redundant(tabs[i]) < 0)
706                         goto error;
707                 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 1);
708
709                 isl_seq_neg(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
710                 if (add_wraps(wraps, map->p[i], tabs[i], bound->el, set) < 0)
711                         goto error;
712
713                 set_is_redundant(tabs[i], map->p[i]->n_eq, cuts, n, k, 0);
714                 if (isl_tab_rollback(tabs[i], snap_i) < 0)
715                         goto error;
716
717                 if (!wraps->n_row)
718                         break;
719
720                 isl_seq_cpy(bound->el, map->p[i]->ineq[cuts[k]], 1 + total);
721                 isl_int_add_ui(bound->el[0], bound->el[0], 1);
722                 tabs[j] = isl_tab_add_eq(tabs[j], bound->el);
723                 if (isl_tab_detect_redundant(tabs[j]) < 0)
724                         goto error;
725
726                 if (!tabs[j]->empty &&
727                     add_wraps(wraps, map->p[j], tabs[j], bound->el, set) < 0)
728                         goto error;
729
730                 if (isl_tab_rollback(tabs[j], snap_j) < 0)
731                         goto error;
732
733                 if (!wraps->n_row)
734                         break;
735         }
736
737         if (k == n)
738                 changed = fuse(map, i, j, tabs,
739                                 eq_i, ineq_i, eq_j, ineq_j, wraps);
740
741         isl_vec_free(bound);
742         isl_mat_free(wraps);
743         isl_set_free(set);
744
745         return changed;
746 error:
747         isl_vec_free(bound);
748         isl_mat_free(wraps);
749         isl_set_free(set);
750         return -1;
751 }
752
753 /* Given two basic sets i and j such that i has not cut equalities,
754  * check if relaxing all the cut inequalities of i by one turns
755  * them into valid constraint for j and check if we can wrap in
756  * the bits that are sticking out.
757  * If so, replace the pair by their union.
758  *
759  * We first check if all relaxed cut inequalities of i are valid for j
760  * and then try to wrap in the intersections of the relaxed cut inequalities
761  * with j.
762  *
763  * During this wrapping, we consider the points of j that lie at a distance
764  * of exactly 1 from i.  In particular, we ignore the points that lie in
765  * between this lower-dimensional space and the basic map i.
766  * We can therefore only apply this to integer maps.
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  * Wrapping can fail if the result of wrapping one of the facets
793  * around its edges does not produce any new facet constraint.
794  * In particular, this happens when we try to wrap in unbounded sets.
795  *
796  *       _______________________________________________________________________
797  *      |
798  *      |  ___
799  *      | |   |
800  *      |_|   |_________________________________________________________________
801  *        |___|
802  *
803  * The following is not an acceptable result of coalescing the above two
804  * sets as it includes extra integer points.
805  *       _______________________________________________________________________
806  *      |
807  *      |     
808  *      |      
809  *      |
810  *       \______________________________________________________________________
811  */
812 static int can_wrap_in_set(struct isl_map *map, int i, int j,
813         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
814 {
815         int changed = 0;
816         int k, l, m;
817         unsigned total = isl_basic_map_total_dim(map->p[i]);
818         struct isl_tab_undo *snap;
819         int n;
820         int *cuts = NULL;
821
822         if (ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_RATIONAL) ||
823             ISL_F_ISSET(map->p[j], ISL_BASIC_MAP_RATIONAL))
824                 return 0;
825
826         n = count(ineq_i, map->p[i]->n_ineq, STATUS_CUT);
827         if (n == 0)
828                 return 0;
829
830         cuts = isl_alloc_array(map->ctx, int, n);
831         if (!cuts)
832                 return -1;
833
834         for (k = 0, m = 0; m < n; ++k) {
835                 enum isl_ineq_type type;
836
837                 if (ineq_i[k] != STATUS_CUT)
838                         continue;
839
840                 isl_int_add_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
841                 type = isl_tab_ineq_type(tabs[j], map->p[i]->ineq[k]);
842                 isl_int_sub_ui(map->p[i]->ineq[k][0], map->p[i]->ineq[k][0], 1);
843                 if (type == isl_ineq_error)
844                         goto error;
845                 if (type != isl_ineq_redundant)
846                         break;
847                 cuts[m] = k;
848                 ++m;
849         }
850
851         if (m == n)
852                 changed = wrap_in_facets(map, i, j, cuts, n, tabs,
853                                          eq_i, ineq_i, eq_j, ineq_j);
854
855         free(cuts);
856
857         return changed;
858 error:
859         free(cuts);
860         return -1;
861 }
862
863 /* Check if either i or j has a single cut constraint that can
864  * be used to wrap in (a facet of) the other basic set.
865  * if so, replace the pair by their union.
866  */
867 static int check_wrap(struct isl_map *map, int i, int j,
868         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
869 {
870         int changed = 0;
871
872         if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
873                 changed = can_wrap_in_set(map, i, j, tabs,
874                                             eq_i, ineq_i, eq_j, ineq_j);
875         if (changed)
876                 return changed;
877
878         if (!any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
879                 changed = can_wrap_in_set(map, j, i, tabs,
880                                             eq_j, ineq_j, eq_i, ineq_i);
881         return changed;
882 }
883
884 /* At least one of the basic maps has an equality that is adjacent
885  * to inequality.  Make sure that only one of the basic maps has
886  * such an equality and that the other basic map has exactly one
887  * inequality adjacent to an equality.
888  * We call the basic map that has the inequality "i" and the basic
889  * map that has the equality "j".
890  * If "i" has any "cut" (in)equality, then relaxing the inequality
891  * by one would not result in a basic map that contains the other
892  * basic map.
893  */
894 static int check_adj_eq(struct isl_map *map, int i, int j,
895         struct isl_tab **tabs, int *eq_i, int *ineq_i, int *eq_j, int *ineq_j)
896 {
897         int changed = 0;
898         int k;
899
900         if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) &&
901             any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ))
902                 /* ADJ EQ TOO MANY */
903                 return 0;
904
905         if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ))
906                 return check_adj_eq(map, j, i, tabs,
907                                         eq_j, ineq_j, eq_i, ineq_i);
908
909         /* j has an equality adjacent to an inequality in i */
910
911         if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT))
912                 return 0;
913         if (any(ineq_i, map->p[i]->n_ineq, STATUS_CUT))
914                 /* ADJ EQ CUT */
915                 return 0;
916         if (count(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ) != 1 ||
917             count(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) != 1 ||
918             any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ) ||
919             any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
920             any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ))
921                 /* ADJ EQ TOO MANY */
922                 return 0;
923
924         for (k = 0; k < map->p[i]->n_ineq ; ++k)
925                 if (ineq_i[k] == STATUS_ADJ_EQ)
926                         break;
927
928         changed = is_extension(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
929         if (changed)
930                 return changed;
931
932         changed = can_wrap_in_facet(map, i, j, k, tabs, eq_i, ineq_i, eq_j, ineq_j);
933
934         return changed;
935 }
936
937 /* Check if the union of the given pair of basic maps
938  * can be represented by a single basic map.
939  * If so, replace the pair by the single basic map and return 1.
940  * Otherwise, return 0;
941  *
942  * We first check the effect of each constraint of one basic map
943  * on the other basic map.
944  * The constraint may be
945  *      redundant       the constraint is redundant in its own
946  *                      basic map and should be ignore and removed
947  *                      in the end
948  *      valid           all (integer) points of the other basic map
949  *                      satisfy the constraint
950  *      separate        no (integer) point of the other basic map
951  *                      satisfies the constraint
952  *      cut             some but not all points of the other basic map
953  *                      satisfy the constraint
954  *      adj_eq          the given constraint is adjacent (on the outside)
955  *                      to an equality of the other basic map
956  *      adj_ineq        the given constraint is adjacent (on the outside)
957  *                      to an inequality of the other basic map
958  *
959  * We consider six cases in which we can replace the pair by a single
960  * basic map.  We ignore all "redundant" constraints.
961  *
962  *      1. all constraints of one basic map are valid
963  *              => the other basic map is a subset and can be removed
964  *
965  *      2. all constraints of both basic maps are either "valid" or "cut"
966  *         and the facets corresponding to the "cut" constraints
967  *         of one of the basic maps lies entirely inside the other basic map
968  *              => the pair can be replaced by a basic map consisting
969  *                 of the valid constraints in both basic maps
970  *
971  *      3. there is a single pair of adjacent inequalities
972  *         (all other constraints are "valid")
973  *              => the pair can be replaced by a basic map consisting
974  *                 of the valid constraints in both basic maps
975  *
976  *      4. there is a single adjacent pair of an inequality and an equality,
977  *         the other constraints of the basic map containing the inequality are
978  *         "valid".  Moreover, if the inequality the basic map is relaxed
979  *         and then turned into an equality, then resulting facet lies
980  *         entirely inside the other basic map
981  *              => the pair can be replaced by the basic map containing
982  *                 the inequality, with the inequality relaxed.
983  *
984  *      5. there is a single adjacent pair of an inequality and an equality,
985  *         the other constraints of the basic map containing the inequality are
986  *         "valid".  Moreover, the facets corresponding to both
987  *         the inequality and the equality can be wrapped around their
988  *         ridges to include the other basic map
989  *              => the pair can be replaced by a basic map consisting
990  *                 of the valid constraints in both basic maps together
991  *                 with all wrapping constraints
992  *
993  *      6. one of the basic maps extends beyond the other by at most one.
994  *         Moreover, the facets corresponding to the cut constraints and
995  *         the pieces of the other basic map at offset one from these cut
996  *         constraints can be wrapped around their ridges to include
997  *         the unione of the two basic maps
998  *              => the pair can be replaced by a basic map consisting
999  *                 of the valid constraints in both basic maps together
1000  *                 with all wrapping constraints
1001  *
1002  * Throughout the computation, we maintain a collection of tableaus
1003  * corresponding to the basic maps.  When the basic maps are dropped
1004  * or combined, the tableaus are modified accordingly.
1005  */
1006 static int coalesce_pair(struct isl_map *map, int i, int j,
1007         struct isl_tab **tabs)
1008 {
1009         int changed = 0;
1010         int *eq_i = NULL;
1011         int *eq_j = NULL;
1012         int *ineq_i = NULL;
1013         int *ineq_j = NULL;
1014
1015         eq_i = eq_status_in(map, i, j, tabs);
1016         if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ERROR))
1017                 goto error;
1018         if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_SEPARATE))
1019                 goto done;
1020
1021         eq_j = eq_status_in(map, j, i, tabs);
1022         if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_ERROR))
1023                 goto error;
1024         if (any(eq_j, 2 * map->p[j]->n_eq, STATUS_SEPARATE))
1025                 goto done;
1026
1027         ineq_i = ineq_status_in(map, i, j, tabs);
1028         if (any(ineq_i, map->p[i]->n_ineq, STATUS_ERROR))
1029                 goto error;
1030         if (any(ineq_i, map->p[i]->n_ineq, STATUS_SEPARATE))
1031                 goto done;
1032
1033         ineq_j = ineq_status_in(map, j, i, tabs);
1034         if (any(ineq_j, map->p[j]->n_ineq, STATUS_ERROR))
1035                 goto error;
1036         if (any(ineq_j, map->p[j]->n_ineq, STATUS_SEPARATE))
1037                 goto done;
1038
1039         if (all(eq_i, 2 * map->p[i]->n_eq, STATUS_VALID) &&
1040             all(ineq_i, map->p[i]->n_ineq, STATUS_VALID)) {
1041                 drop(map, j, tabs);
1042                 changed = 1;
1043         } else if (all(eq_j, 2 * map->p[j]->n_eq, STATUS_VALID) &&
1044                    all(ineq_j, map->p[j]->n_ineq, STATUS_VALID)) {
1045                 drop(map, i, tabs);
1046                 changed = 1;
1047         } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_EQ) ||
1048                    any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_EQ)) {
1049                 /* ADJ EQ PAIR */
1050         } else if (any(eq_i, 2 * map->p[i]->n_eq, STATUS_ADJ_INEQ) ||
1051                    any(eq_j, 2 * map->p[j]->n_eq, STATUS_ADJ_INEQ)) {
1052                 changed = check_adj_eq(map, i, j, tabs,
1053                                         eq_i, ineq_i, eq_j, ineq_j);
1054         } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_EQ) ||
1055                    any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_EQ)) {
1056                 /* Can't happen */
1057                 /* BAD ADJ INEQ */
1058         } else if (any(ineq_i, map->p[i]->n_ineq, STATUS_ADJ_INEQ) ||
1059                    any(ineq_j, map->p[j]->n_ineq, STATUS_ADJ_INEQ)) {
1060                 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1061                     !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1062                         changed = check_adj_ineq(map, i, j, tabs,
1063                                                  ineq_i, ineq_j);
1064         } else {
1065                 if (!any(eq_i, 2 * map->p[i]->n_eq, STATUS_CUT) &&
1066                     !any(eq_j, 2 * map->p[j]->n_eq, STATUS_CUT))
1067                         changed = check_facets(map, i, j, tabs, ineq_i, ineq_j);
1068                 if (!changed)
1069                         changed = check_wrap(map, i, j, tabs,
1070                                                 eq_i, ineq_i, eq_j, ineq_j);
1071         }
1072
1073 done:
1074         free(eq_i);
1075         free(eq_j);
1076         free(ineq_i);
1077         free(ineq_j);
1078         return changed;
1079 error:
1080         free(eq_i);
1081         free(eq_j);
1082         free(ineq_i);
1083         free(ineq_j);
1084         return -1;
1085 }
1086
1087 static struct isl_map *coalesce(struct isl_map *map, struct isl_tab **tabs)
1088 {
1089         int i, j;
1090
1091         for (i = map->n - 2; i >= 0; --i)
1092 restart:
1093                 for (j = i + 1; j < map->n; ++j) {
1094                         int changed;
1095                         changed = coalesce_pair(map, i, j, tabs);
1096                         if (changed < 0)
1097                                 goto error;
1098                         if (changed)
1099                                 goto restart;
1100                 }
1101         return map;
1102 error:
1103         isl_map_free(map);
1104         return NULL;
1105 }
1106
1107 /* For each pair of basic maps in the map, check if the union of the two
1108  * can be represented by a single basic map.
1109  * If so, replace the pair by the single basic map and start over.
1110  */
1111 struct isl_map *isl_map_coalesce(struct isl_map *map)
1112 {
1113         int i;
1114         unsigned n;
1115         struct isl_tab **tabs = NULL;
1116
1117         if (!map)
1118                 return NULL;
1119
1120         if (map->n <= 1)
1121                 return map;
1122
1123         map = isl_map_align_divs(map);
1124
1125         tabs = isl_calloc_array(map->ctx, struct isl_tab *, map->n);
1126         if (!tabs)
1127                 goto error;
1128
1129         n = map->n;
1130         for (i = 0; i < map->n; ++i) {
1131                 tabs[i] = isl_tab_from_basic_map(map->p[i]);
1132                 if (!tabs[i])
1133                         goto error;
1134                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT))
1135                         tabs[i] = isl_tab_detect_implicit_equalities(tabs[i]);
1136                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT))
1137                         if (isl_tab_detect_redundant(tabs[i]) < 0)
1138                                 goto error;
1139         }
1140         for (i = map->n - 1; i >= 0; --i)
1141                 if (tabs[i]->empty)
1142                         drop(map, i, tabs);
1143
1144         map = coalesce(map, tabs);
1145
1146         if (map)
1147                 for (i = 0; i < map->n; ++i) {
1148                         map->p[i] = isl_basic_map_update_from_tab(map->p[i],
1149                                                                     tabs[i]);
1150                         map->p[i] = isl_basic_map_finalize(map->p[i]);
1151                         if (!map->p[i])
1152                                 goto error;
1153                         ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_IMPLICIT);
1154                         ISL_F_SET(map->p[i], ISL_BASIC_MAP_NO_REDUNDANT);
1155                 }
1156
1157         for (i = 0; i < n; ++i)
1158                 isl_tab_free(tabs[i]);
1159
1160         free(tabs);
1161
1162         return map;
1163 error:
1164         if (tabs)
1165                 for (i = 0; i < n; ++i)
1166                         isl_tab_free(tabs[i]);
1167         free(tabs);
1168         return NULL;
1169 }
1170
1171 /* For each pair of basic sets in the set, check if the union of the two
1172  * can be represented by a single basic set.
1173  * If so, replace the pair by the single basic set and start over.
1174  */
1175 struct isl_set *isl_set_coalesce(struct isl_set *set)
1176 {
1177         return (struct isl_set *)isl_map_coalesce((struct isl_map *)set);
1178 }