isl_basic_map_gist: be more aggressive in removing constraints
[platform/upstream/isl.git] / isl_map_simplify.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include "isl_equalities.h"
11 #include "isl_map.h"
12 #include "isl_map_private.h"
13 #include "isl_seq.h"
14 #include "isl_tab.h"
15
16 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
17 {
18         isl_int *t = bmap->eq[a];
19         bmap->eq[a] = bmap->eq[b];
20         bmap->eq[b] = t;
21 }
22
23 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
24 {
25         if (a != b) {
26                 isl_int *t = bmap->ineq[a];
27                 bmap->ineq[a] = bmap->ineq[b];
28                 bmap->ineq[b] = t;
29         }
30 }
31
32 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
33 {
34         swap_inequality((struct isl_basic_map *)bset, a, b);
35 }
36
37 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
38 {
39         isl_seq_cpy(c, c + n, rem);
40         isl_seq_clr(c + rem, n);
41 }
42
43 /* Drop n dimensions starting at first.
44  *
45  * In principle, this frees up some extra variables as the number
46  * of columns remains constant, but we would have to extend
47  * the div array too as the number of rows in this array is assumed
48  * to be equal to extra.
49  */
50 struct isl_basic_set *isl_basic_set_drop_dims(
51                 struct isl_basic_set *bset, unsigned first, unsigned n)
52 {
53         int i;
54
55         if (!bset)
56                 goto error;
57
58         isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
59
60         if (n == 0)
61                 return bset;
62
63         bset = isl_basic_set_cow(bset);
64         if (!bset)
65                 return NULL;
66
67         for (i = 0; i < bset->n_eq; ++i)
68                 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
69                                      (bset->dim->n_out-first-n)+bset->extra);
70
71         for (i = 0; i < bset->n_ineq; ++i)
72                 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
73                                      (bset->dim->n_out-first-n)+bset->extra);
74
75         for (i = 0; i < bset->n_div; ++i)
76                 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
77                                      (bset->dim->n_out-first-n)+bset->extra);
78
79         bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
80         if (!bset->dim)
81                 goto error;
82
83         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
84         bset = isl_basic_set_simplify(bset);
85         return isl_basic_set_finalize(bset);
86 error:
87         isl_basic_set_free(bset);
88         return NULL;
89 }
90
91 struct isl_set *isl_set_drop_dims(
92                 struct isl_set *set, unsigned first, unsigned n)
93 {
94         int i;
95
96         if (!set)
97                 goto error;
98
99         isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
100
101         if (n == 0)
102                 return set;
103         set = isl_set_cow(set);
104         if (!set)
105                 goto error;
106         set->dim = isl_dim_drop_outputs(set->dim, first, n);
107         if (!set->dim)
108                 goto error;
109
110         for (i = 0; i < set->n; ++i) {
111                 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
112                 if (!set->p[i])
113                         goto error;
114         }
115
116         ISL_F_CLR(set, ISL_SET_NORMALIZED);
117         return set;
118 error:
119         isl_set_free(set);
120         return NULL;
121 }
122
123 /* Move "n" divs starting at "first" to the end of the list of divs.
124  */
125 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
126         unsigned first, unsigned n)
127 {
128         isl_int **div;
129         int i;
130
131         if (first + n == bmap->n_div)
132                 return bmap;
133
134         div = isl_alloc_array(bmap->ctx, isl_int *, n);
135         if (!div)
136                 goto error;
137         for (i = 0; i < n; ++i)
138                 div[i] = bmap->div[first + i];
139         for (i = 0; i < bmap->n_div - first - n; ++i)
140                 bmap->div[first + i] = bmap->div[first + n + i];
141         for (i = 0; i < n; ++i)
142                 bmap->div[bmap->n_div - n + i] = div[i];
143         free(div);
144         return bmap;
145 error:
146         isl_basic_map_free(bmap);
147         return NULL;
148 }
149
150 /* Drop "n" dimensions of type "type" starting at "first".
151  *
152  * In principle, this frees up some extra variables as the number
153  * of columns remains constant, but we would have to extend
154  * the div array too as the number of rows in this array is assumed
155  * to be equal to extra.
156  */
157 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
158         enum isl_dim_type type, unsigned first, unsigned n)
159 {
160         int i;
161         unsigned dim;
162         unsigned offset;
163         unsigned left;
164
165         if (!bmap)
166                 goto error;
167
168         dim = isl_basic_map_dim(bmap, type);
169         isl_assert(bmap->ctx, first + n <= dim, goto error);
170
171         if (n == 0)
172                 return bmap;
173
174         bmap = isl_basic_map_cow(bmap);
175         if (!bmap)
176                 return NULL;
177
178         offset = isl_basic_map_offset(bmap, type) + first;
179         left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
180         for (i = 0; i < bmap->n_eq; ++i)
181                 constraint_drop_vars(bmap->eq[i]+offset, n, left);
182
183         for (i = 0; i < bmap->n_ineq; ++i)
184                 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
185
186         for (i = 0; i < bmap->n_div; ++i)
187                 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
188
189         if (type == isl_dim_div) {
190                 bmap = move_divs_last(bmap, first, n);
191                 if (!bmap)
192                         goto error;
193                 isl_basic_map_free_div(bmap, n);
194         } else
195                 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
196         if (!bmap->dim)
197                 goto error;
198
199         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
200         bmap = isl_basic_map_simplify(bmap);
201         return isl_basic_map_finalize(bmap);
202 error:
203         isl_basic_map_free(bmap);
204         return NULL;
205 }
206
207 struct isl_basic_map *isl_basic_map_drop_inputs(
208                 struct isl_basic_map *bmap, unsigned first, unsigned n)
209 {
210         return isl_basic_map_drop(bmap, isl_dim_in, first, n);
211 }
212
213 struct isl_map *isl_map_drop(struct isl_map *map,
214         enum isl_dim_type type, unsigned first, unsigned n)
215 {
216         int i;
217
218         if (!map)
219                 goto error;
220
221         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
222
223         if (n == 0)
224                 return map;
225         map = isl_map_cow(map);
226         if (!map)
227                 goto error;
228         map->dim = isl_dim_drop(map->dim, type, first, n);
229         if (!map->dim)
230                 goto error;
231
232         for (i = 0; i < map->n; ++i) {
233                 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
234                 if (!map->p[i])
235                         goto error;
236         }
237         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
238
239         return map;
240 error:
241         isl_map_free(map);
242         return NULL;
243 }
244
245 struct isl_set *isl_set_drop(struct isl_set *set,
246         enum isl_dim_type type, unsigned first, unsigned n)
247 {
248         return (isl_set *)isl_map_drop((isl_map *)set, type, first, n);
249 }
250
251 struct isl_map *isl_map_drop_inputs(
252                 struct isl_map *map, unsigned first, unsigned n)
253 {
254         return isl_map_drop(map, isl_dim_in, first, n);
255 }
256
257 /*
258  * We don't cow, as the div is assumed to be redundant.
259  */
260 static struct isl_basic_map *isl_basic_map_drop_div(
261                 struct isl_basic_map *bmap, unsigned div)
262 {
263         int i;
264         unsigned pos;
265
266         if (!bmap)
267                 goto error;
268
269         pos = 1 + isl_dim_total(bmap->dim) + div;
270
271         isl_assert(bmap->ctx, div < bmap->n_div, goto error);
272
273         for (i = 0; i < bmap->n_eq; ++i)
274                 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
275
276         for (i = 0; i < bmap->n_ineq; ++i) {
277                 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
278                         isl_basic_map_drop_inequality(bmap, i);
279                         --i;
280                         continue;
281                 }
282                 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
283         }
284
285         for (i = 0; i < bmap->n_div; ++i)
286                 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
287
288         if (div != bmap->n_div - 1) {
289                 int j;
290                 isl_int *t = bmap->div[div];
291
292                 for (j = div; j < bmap->n_div - 1; ++j)
293                         bmap->div[j] = bmap->div[j+1];
294
295                 bmap->div[bmap->n_div - 1] = t;
296         }
297         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
298         isl_basic_map_free_div(bmap, 1);
299
300         return bmap;
301 error:
302         isl_basic_map_free(bmap);
303         return NULL;
304 }
305
306 struct isl_basic_map *isl_basic_map_normalize_constraints(
307         struct isl_basic_map *bmap)
308 {
309         int i;
310         isl_int gcd;
311         unsigned total = isl_basic_map_total_dim(bmap);
312
313         isl_int_init(gcd);
314         for (i = bmap->n_eq - 1; i >= 0; --i) {
315                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
316                 if (isl_int_is_zero(gcd)) {
317                         if (!isl_int_is_zero(bmap->eq[i][0])) {
318                                 bmap = isl_basic_map_set_to_empty(bmap);
319                                 break;
320                         }
321                         isl_basic_map_drop_equality(bmap, i);
322                         continue;
323                 }
324                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
325                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
326                 if (isl_int_is_one(gcd))
327                         continue;
328                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
329                         bmap = isl_basic_map_set_to_empty(bmap);
330                         break;
331                 }
332                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
333         }
334
335         for (i = bmap->n_ineq - 1; i >= 0; --i) {
336                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
337                 if (isl_int_is_zero(gcd)) {
338                         if (isl_int_is_neg(bmap->ineq[i][0])) {
339                                 bmap = isl_basic_map_set_to_empty(bmap);
340                                 break;
341                         }
342                         isl_basic_map_drop_inequality(bmap, i);
343                         continue;
344                 }
345                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
346                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
347                 if (isl_int_is_one(gcd))
348                         continue;
349                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
350                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
351         }
352         isl_int_clear(gcd);
353
354         return bmap;
355 }
356
357 struct isl_basic_set *isl_basic_set_normalize_constraints(
358         struct isl_basic_set *bset)
359 {
360         return (struct isl_basic_set *)isl_basic_map_normalize_constraints(
361                 (struct isl_basic_map *)bset);
362 }
363
364 /* Assumes divs have been ordered if keep_divs is set.
365  */
366 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
367         unsigned pos, isl_int *eq, int keep_divs, int *progress)
368 {
369         unsigned total;
370         int k;
371         int last_div;
372
373         total = isl_basic_map_total_dim(bmap);
374         last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
375                                                 bmap->n_div);
376         for (k = 0; k < bmap->n_eq; ++k) {
377                 if (bmap->eq[k] == eq)
378                         continue;
379                 if (isl_int_is_zero(bmap->eq[k][1+pos]))
380                         continue;
381                 if (progress)
382                         *progress = 1;
383                 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
384         }
385
386         for (k = 0; k < bmap->n_ineq; ++k) {
387                 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
388                         continue;
389                 if (progress)
390                         *progress = 1;
391                 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
392                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
393         }
394
395         for (k = 0; k < bmap->n_div; ++k) {
396                 if (isl_int_is_zero(bmap->div[k][0]))
397                         continue;
398                 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
399                         continue;
400                 if (progress)
401                         *progress = 1;
402                 /* We need to be careful about circular definitions,
403                  * so for now we just remove the definition of div k
404                  * if the equality contains any divs.
405                  * If keep_divs is set, then the divs have been ordered
406                  * and we can keep the definition as long as the result
407                  * is still ordered.
408                  */
409                 if (last_div == -1 || (keep_divs && last_div < k))
410                         isl_seq_elim(bmap->div[k]+1, eq,
411                                         1+pos, 1+total, &bmap->div[k][0]);
412                 else
413                         isl_seq_clr(bmap->div[k], 1 + total);
414                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
415         }
416 }
417
418 /* Assumes divs have been ordered if keep_divs is set.
419  */
420 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
421         unsigned div, int keep_divs)
422 {
423         unsigned pos = isl_dim_total(bmap->dim) + div;
424
425         eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
426
427         isl_basic_map_drop_div(bmap, div);
428 }
429
430 /* Check if elimination of div "div" using equality "eq" would not
431  * result in a div depending on a later div.
432  */
433 static int ok_to_eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
434         unsigned div)
435 {
436         int k;
437         int last_div;
438         unsigned pos = isl_dim_total(bmap->dim) + div;
439
440         last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
441                                                 bmap->n_div);
442         if (last_div < 0 || last_div <= div)
443                 return 1;
444
445         for (k = 0; k <= last_div; ++k) {
446                 if (isl_int_is_zero(bmap->div[k][0]))
447                         return 1;
448                 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
449                         return 0;
450         }
451
452         return 1;
453 }
454
455 /* Elimininate divs based on equalities
456  */
457 static struct isl_basic_map *eliminate_divs_eq(
458                 struct isl_basic_map *bmap, int *progress)
459 {
460         int d;
461         int i;
462         int modified = 0;
463         unsigned off;
464
465         bmap = isl_basic_map_order_divs(bmap);
466
467         if (!bmap)
468                 return NULL;
469
470         off = 1 + isl_dim_total(bmap->dim);
471
472         for (d = bmap->n_div - 1; d >= 0 ; --d) {
473                 for (i = 0; i < bmap->n_eq; ++i) {
474                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
475                             !isl_int_is_negone(bmap->eq[i][off + d]))
476                                 continue;
477                         if (!ok_to_eliminate_div(bmap, bmap->eq[i], d))
478                                 continue;
479                         modified = 1;
480                         *progress = 1;
481                         eliminate_div(bmap, bmap->eq[i], d, 1);
482                         isl_basic_map_drop_equality(bmap, i);
483                         break;
484                 }
485         }
486         if (modified)
487                 return eliminate_divs_eq(bmap, progress);
488         return bmap;
489 }
490
491 /* Elimininate divs based on inequalities
492  */
493 static struct isl_basic_map *eliminate_divs_ineq(
494                 struct isl_basic_map *bmap, int *progress)
495 {
496         int d;
497         int i;
498         unsigned off;
499         struct isl_ctx *ctx;
500
501         if (!bmap)
502                 return NULL;
503
504         ctx = bmap->ctx;
505         off = 1 + isl_dim_total(bmap->dim);
506
507         for (d = bmap->n_div - 1; d >= 0 ; --d) {
508                 for (i = 0; i < bmap->n_eq; ++i)
509                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
510                                 break;
511                 if (i < bmap->n_eq)
512                         continue;
513                 for (i = 0; i < bmap->n_ineq; ++i)
514                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
515                                 break;
516                 if (i < bmap->n_ineq)
517                         continue;
518                 *progress = 1;
519                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
520                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
521                         break;
522                 bmap = isl_basic_map_drop_div(bmap, d);
523                 if (!bmap)
524                         break;
525         }
526         return bmap;
527 }
528
529 struct isl_basic_map *isl_basic_map_gauss(
530         struct isl_basic_map *bmap, int *progress)
531 {
532         int k;
533         int done;
534         int last_var;
535         unsigned total_var;
536         unsigned total;
537
538         bmap = isl_basic_map_order_divs(bmap);
539
540         if (!bmap)
541                 return NULL;
542
543         total = isl_basic_map_total_dim(bmap);
544         total_var = total - bmap->n_div;
545
546         last_var = total - 1;
547         for (done = 0; done < bmap->n_eq; ++done) {
548                 for (; last_var >= 0; --last_var) {
549                         for (k = done; k < bmap->n_eq; ++k)
550                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
551                                         break;
552                         if (k < bmap->n_eq)
553                                 break;
554                 }
555                 if (last_var < 0)
556                         break;
557                 if (k != done)
558                         swap_equality(bmap, k, done);
559                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
560                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
561
562                 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
563                                                 progress);
564
565                 if (last_var >= total_var &&
566                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
567                         unsigned div = last_var - total_var;
568                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
569                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
570                         isl_int_set(bmap->div[div][0],
571                                     bmap->eq[done][1+last_var]);
572                         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
573                 }
574         }
575         if (done == bmap->n_eq)
576                 return bmap;
577         for (k = done; k < bmap->n_eq; ++k) {
578                 if (isl_int_is_zero(bmap->eq[k][0]))
579                         continue;
580                 return isl_basic_map_set_to_empty(bmap);
581         }
582         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
583         return bmap;
584 }
585
586 struct isl_basic_set *isl_basic_set_gauss(
587         struct isl_basic_set *bset, int *progress)
588 {
589         return (struct isl_basic_set*)isl_basic_map_gauss(
590                         (struct isl_basic_map *)bset, progress);
591 }
592
593
594 static unsigned int round_up(unsigned int v)
595 {
596         int old_v = v;
597
598         while (v) {
599                 old_v = v;
600                 v ^= v & -v;
601         }
602         return old_v << 1;
603 }
604
605 static int hash_index(isl_int ***index, unsigned int size, int bits,
606                         struct isl_basic_map *bmap, int k)
607 {
608         int h;
609         unsigned total = isl_basic_map_total_dim(bmap);
610         uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
611         for (h = hash; index[h]; h = (h+1) % size)
612                 if (&bmap->ineq[k] != index[h] &&
613                     isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
614                         break;
615         return h;
616 }
617
618 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
619                           struct isl_basic_set *bset, int k)
620 {
621         return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
622 }
623
624 /* If we can eliminate more than one div, then we need to make
625  * sure we do it from last div to first div, in order not to
626  * change the position of the other divs that still need to
627  * be removed.
628  */
629 static struct isl_basic_map *remove_duplicate_divs(
630         struct isl_basic_map *bmap, int *progress)
631 {
632         unsigned int size;
633         int *index;
634         int *elim_for;
635         int k, l, h;
636         int bits;
637         struct isl_blk eq;
638         unsigned total_var = isl_dim_total(bmap->dim);
639         unsigned total = total_var + bmap->n_div;
640         struct isl_ctx *ctx;
641
642         if (bmap->n_div <= 1)
643                 return bmap;
644
645         ctx = bmap->ctx;
646         for (k = bmap->n_div - 1; k >= 0; --k)
647                 if (!isl_int_is_zero(bmap->div[k][0]))
648                         break;
649         if (k <= 0)
650                 return bmap;
651
652         elim_for = isl_calloc_array(ctx, int, bmap->n_div);
653         size = round_up(4 * bmap->n_div / 3 - 1);
654         bits = ffs(size) - 1;
655         index = isl_calloc_array(ctx, int, size);
656         if (!index)
657                 return bmap;
658         eq = isl_blk_alloc(ctx, 1+total);
659         if (isl_blk_is_error(eq))
660                 goto out;
661
662         isl_seq_clr(eq.data, 1+total);
663         index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
664         for (--k; k >= 0; --k) {
665                 uint32_t hash;
666
667                 if (isl_int_is_zero(bmap->div[k][0]))
668                         continue;
669
670                 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
671                 for (h = hash; index[h]; h = (h+1) % size)
672                         if (isl_seq_eq(bmap->div[k],
673                                        bmap->div[index[h]-1], 2+total))
674                                 break;
675                 if (index[h]) {
676                         *progress = 1;
677                         l = index[h] - 1;
678                         elim_for[l] = k + 1;
679                 }
680                 index[h] = k+1;
681         }
682         for (l = bmap->n_div - 1; l >= 0; --l) {
683                 if (!elim_for[l])
684                         continue;
685                 k = elim_for[l] - 1;
686                 isl_int_set_si(eq.data[1+total_var+k], -1);
687                 isl_int_set_si(eq.data[1+total_var+l], 1);
688                 eliminate_div(bmap, eq.data, l, 0);
689                 isl_int_set_si(eq.data[1+total_var+k], 0);
690                 isl_int_set_si(eq.data[1+total_var+l], 0);
691         }
692
693         isl_blk_free(ctx, eq);
694 out:
695         free(index);
696         free(elim_for);
697         return bmap;
698 }
699
700 static int n_pure_div_eq(struct isl_basic_map *bmap)
701 {
702         int i, j;
703         unsigned total;
704
705         total = isl_dim_total(bmap->dim);
706         for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
707                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
708                         --j;
709                 if (j < 0)
710                         break;
711                 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
712                         return 0;
713         }
714         return i;
715 }
716
717 /* Normalize divs that appear in equalities.
718  *
719  * In particular, we assume that bmap contains some equalities
720  * of the form
721  *
722  *      a x = m * e_i
723  *
724  * and we want to replace the set of e_i by a minimal set and
725  * such that the new e_i have a canonical representation in terms
726  * of the vector x.
727  * If any of the equalities involves more than one divs, then
728  * we currently simply bail out.
729  *
730  * Let us first additionally assume that all equalities involve
731  * a div.  The equalities then express modulo constraints on the
732  * remaining variables and we can use "parameter compression"
733  * to find a minimal set of constraints.  The result is a transformation
734  *
735  *      x = T(x') = x_0 + G x'
736  *
737  * with G a lower-triangular matrix with all elements below the diagonal
738  * non-negative and smaller than the diagonal element on the same row.
739  * We first normalize x_0 by making the same property hold in the affine
740  * T matrix.
741  * The rows i of G with a 1 on the diagonal do not impose any modulo
742  * constraint and simply express x_i = x'_i.
743  * For each of the remaining rows i, we introduce a div and a corresponding
744  * equality.  In particular
745  *
746  *      g_ii e_j = x_i - g_i(x')
747  *
748  * where each x'_k is replaced either by x_k (if g_kk = 1) or the
749  * corresponding div (if g_kk != 1).
750  *
751  * If there are any equalities not involving any div, then we
752  * first apply a variable compression on the variables x:
753  *
754  *      x = C x''       x'' = C_2 x
755  *
756  * and perform the above parameter compression on A C instead of on A.
757  * The resulting compression is then of the form
758  *
759  *      x'' = T(x') = x_0 + G x'
760  *
761  * and in constructing the new divs and the corresponding equalities,
762  * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
763  * by the corresponding row from C_2.
764  */
765 static struct isl_basic_map *normalize_divs(
766         struct isl_basic_map *bmap, int *progress)
767 {
768         int i, j, k;
769         int total;
770         int div_eq;
771         struct isl_mat *B;
772         struct isl_vec *d;
773         struct isl_mat *T = NULL;
774         struct isl_mat *C = NULL;
775         struct isl_mat *C2 = NULL;
776         isl_int v;
777         int *pos;
778         int dropped, needed;
779
780         if (!bmap)
781                 return NULL;
782
783         if (bmap->n_div == 0)
784                 return bmap;
785
786         if (bmap->n_eq == 0)
787                 return bmap;
788
789         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
790                 return bmap;
791
792         total = isl_dim_total(bmap->dim);
793         div_eq = n_pure_div_eq(bmap);
794         if (div_eq == 0)
795                 return bmap;
796
797         if (div_eq < bmap->n_eq) {
798                 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
799                                         bmap->n_eq - div_eq, 0, 1 + total);
800                 C = isl_mat_variable_compression(B, &C2);
801                 if (!C || !C2)
802                         goto error;
803                 if (C->n_col == 0) {
804                         bmap = isl_basic_map_set_to_empty(bmap);
805                         isl_mat_free(C);
806                         isl_mat_free(C2);
807                         goto done;
808                 }
809         }
810
811         d = isl_vec_alloc(bmap->ctx, div_eq);
812         if (!d)
813                 goto error;
814         for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
815                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
816                         --j;
817                 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
818         }
819         B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
820
821         if (C) {
822                 B = isl_mat_product(B, C);
823                 C = NULL;
824         }
825
826         T = isl_mat_parameter_compression(B, d);
827         if (!T)
828                 goto error;
829         if (T->n_col == 0) {
830                 bmap = isl_basic_map_set_to_empty(bmap);
831                 isl_mat_free(C2);
832                 isl_mat_free(T);
833                 goto done;
834         }
835         isl_int_init(v);
836         for (i = 0; i < T->n_row - 1; ++i) {
837                 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
838                 if (isl_int_is_zero(v))
839                         continue;
840                 isl_mat_col_submul(T, 0, v, 1 + i);
841         }
842         isl_int_clear(v);
843         pos = isl_alloc_array(bmap->ctx, int, T->n_row);
844         /* We have to be careful because dropping equalities may reorder them */
845         dropped = 0;
846         for (j = bmap->n_div - 1; j >= 0; --j) {
847                 for (i = 0; i < bmap->n_eq; ++i)
848                         if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
849                                 break;
850                 if (i < bmap->n_eq) {
851                         bmap = isl_basic_map_drop_div(bmap, j);
852                         isl_basic_map_drop_equality(bmap, i);
853                         ++dropped;
854                 }
855         }
856         pos[0] = 0;
857         needed = 0;
858         for (i = 1; i < T->n_row; ++i) {
859                 if (isl_int_is_one(T->row[i][i]))
860                         pos[i] = i;
861                 else
862                         needed++;
863         }
864         if (needed > dropped) {
865                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
866                                 needed, needed, 0);
867                 if (!bmap)
868                         goto error;
869         }
870         for (i = 1; i < T->n_row; ++i) {
871                 if (isl_int_is_one(T->row[i][i]))
872                         continue;
873                 k = isl_basic_map_alloc_div(bmap);
874                 pos[i] = 1 + total + k;
875                 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
876                 isl_int_set(bmap->div[k][0], T->row[i][i]);
877                 if (C2)
878                         isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
879                 else
880                         isl_int_set_si(bmap->div[k][1 + i], 1);
881                 for (j = 0; j < i; ++j) {
882                         if (isl_int_is_zero(T->row[i][j]))
883                                 continue;
884                         if (pos[j] < T->n_row && C2)
885                                 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
886                                                 C2->row[pos[j]], 1 + total);
887                         else
888                                 isl_int_neg(bmap->div[k][1 + pos[j]],
889                                                                 T->row[i][j]);
890                 }
891                 j = isl_basic_map_alloc_equality(bmap);
892                 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
893                 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
894         }
895         free(pos);
896         isl_mat_free(C2);
897         isl_mat_free(T);
898
899         if (progress)
900                 *progress = 1;
901 done:
902         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
903
904         return bmap;
905 error:
906         isl_mat_free(C);
907         isl_mat_free(C2);
908         isl_mat_free(T);
909         return bmap;
910 }
911
912 static struct isl_basic_map *set_div_from_lower_bound(
913         struct isl_basic_map *bmap, int div, int ineq)
914 {
915         unsigned total = 1 + isl_dim_total(bmap->dim);
916
917         isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
918         isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
919         isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
920         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
921         isl_int_set_si(bmap->div[div][1 + total + div], 0);
922
923         return bmap;
924 }
925
926 /* Check whether it is ok to define a div based on an inequality.
927  * To avoid the introduction of circular definitions of divs, we
928  * do not allow such a definition if the resulting expression would refer to
929  * any other undefined divs or if any known div is defined in
930  * terms of the unknown div.
931  */
932 static int ok_to_set_div_from_bound(struct isl_basic_map *bmap,
933         int div, int ineq)
934 {
935         int j;
936         unsigned total = 1 + isl_dim_total(bmap->dim);
937
938         /* Not defined in terms of unknown divs */
939         for (j = 0; j < bmap->n_div; ++j) {
940                 if (div == j)
941                         continue;
942                 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
943                         continue;
944                 if (isl_int_is_zero(bmap->div[j][0]))
945                         return 0;
946         }
947
948         /* No other div defined in terms of this one => avoid loops */
949         for (j = 0; j < bmap->n_div; ++j) {
950                 if (div == j)
951                         continue;
952                 if (isl_int_is_zero(bmap->div[j][0]))
953                         continue;
954                 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
955                         return 0;
956         }
957
958         return 1;
959 }
960
961 /* Given two constraints "k" and "l" that are opposite to each other,
962  * except for the constant term, check if we can use them
963  * to obtain an expression for one of the hitherto unknown divs.
964  * "sum" is the sum of the constant terms of the constraints.
965  * If this sum is strictly smaller than the coefficient of one
966  * of the divs, then this pair can be used define the div.
967  * To avoid the introduction of circular definitions of divs, we
968  * do not use the pair if the resulting expression would refer to
969  * any other undefined divs or if any known div is defined in
970  * terms of the unknown div.
971  */
972 static struct isl_basic_map *check_for_div_constraints(
973         struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
974 {
975         int i;
976         unsigned total = 1 + isl_dim_total(bmap->dim);
977
978         for (i = 0; i < bmap->n_div; ++i) {
979                 if (!isl_int_is_zero(bmap->div[i][0]))
980                         continue;
981                 if (isl_int_is_zero(bmap->ineq[k][total + i]))
982                         continue;
983                 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
984                         continue;
985                 if (!ok_to_set_div_from_bound(bmap, i, k))
986                         break;
987                 if (isl_int_is_pos(bmap->ineq[k][total + i]))
988                         bmap = set_div_from_lower_bound(bmap, i, k);
989                 else
990                         bmap = set_div_from_lower_bound(bmap, i, l);
991                 if (progress)
992                         *progress = 1;
993                 break;
994         }
995         return bmap;
996 }
997
998 static struct isl_basic_map *remove_duplicate_constraints(
999         struct isl_basic_map *bmap, int *progress)
1000 {
1001         unsigned int size;
1002         isl_int ***index;
1003         int k, l, h;
1004         int bits;
1005         unsigned total = isl_basic_map_total_dim(bmap);
1006         isl_int sum;
1007
1008         if (bmap->n_ineq <= 1)
1009                 return bmap;
1010
1011         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
1012         bits = ffs(size) - 1;
1013         index = isl_calloc_array(ctx, isl_int **, size);
1014         if (!index)
1015                 return bmap;
1016
1017         index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
1018         for (k = 1; k < bmap->n_ineq; ++k) {
1019                 h = hash_index(index, size, bits, bmap, k);
1020                 if (!index[h]) {
1021                         index[h] = &bmap->ineq[k];
1022                         continue;
1023                 }
1024                 if (progress)
1025                         *progress = 1;
1026                 l = index[h] - &bmap->ineq[0];
1027                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1028                         swap_inequality(bmap, k, l);
1029                 isl_basic_map_drop_inequality(bmap, k);
1030                 --k;
1031         }
1032         isl_int_init(sum);
1033         for (k = 0; k < bmap->n_ineq-1; ++k) {
1034                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1035                 h = hash_index(index, size, bits, bmap, k);
1036                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1037                 if (!index[h])
1038                         continue;
1039                 l = index[h] - &bmap->ineq[0];
1040                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1041                 if (isl_int_is_pos(sum)) {
1042                         bmap = check_for_div_constraints(bmap, k, l, sum,
1043                                                          progress);
1044                         continue;
1045                 }
1046                 if (isl_int_is_zero(sum)) {
1047                         /* We need to break out of the loop after these
1048                          * changes since the contents of the hash
1049                          * will no longer be valid.
1050                          * Plus, we probably we want to regauss first.
1051                          */
1052                         if (progress)
1053                                 *progress = 1;
1054                         isl_basic_map_drop_inequality(bmap, l);
1055                         isl_basic_map_inequality_to_equality(bmap, k);
1056                 } else
1057                         bmap = isl_basic_map_set_to_empty(bmap);
1058                 break;
1059         }
1060         isl_int_clear(sum);
1061
1062         free(index);
1063         return bmap;
1064 }
1065
1066
1067 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1068 {
1069         int progress = 1;
1070         if (!bmap)
1071                 return NULL;
1072         while (progress) {
1073                 progress = 0;
1074                 bmap = isl_basic_map_normalize_constraints(bmap);
1075                 bmap = remove_duplicate_divs(bmap, &progress);
1076                 bmap = eliminate_divs_eq(bmap, &progress);
1077                 bmap = eliminate_divs_ineq(bmap, &progress);
1078                 bmap = isl_basic_map_gauss(bmap, &progress);
1079                 /* requires equalities in normal form */
1080                 bmap = normalize_divs(bmap, &progress);
1081                 bmap = remove_duplicate_constraints(bmap, &progress);
1082         }
1083         return bmap;
1084 }
1085
1086 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1087 {
1088         return (struct isl_basic_set *)
1089                 isl_basic_map_simplify((struct isl_basic_map *)bset);
1090 }
1091
1092
1093 /* If the only constraints a div d=floor(f/m)
1094  * appears in are its two defining constraints
1095  *
1096  *      f - m d >=0
1097  *      -(f - (m - 1)) + m d >= 0
1098  *
1099  * then it can safely be removed.
1100  */
1101 static int div_is_redundant(struct isl_basic_map *bmap, int div)
1102 {
1103         int i;
1104         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
1105
1106         for (i = 0; i < bmap->n_eq; ++i)
1107                 if (!isl_int_is_zero(bmap->eq[i][pos]))
1108                         return 0;
1109
1110         for (i = 0; i < bmap->n_ineq; ++i) {
1111                 if (isl_int_is_zero(bmap->ineq[i][pos]))
1112                         continue;
1113                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1114                         int neg;
1115                         isl_int_sub(bmap->div[div][1],
1116                                         bmap->div[div][1], bmap->div[div][0]);
1117                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1118                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
1119                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1120                         isl_int_add(bmap->div[div][1],
1121                                         bmap->div[div][1], bmap->div[div][0]);
1122                         if (!neg)
1123                                 return 0;
1124                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1125                                                     bmap->n_div-div-1) != -1)
1126                                 return 0;
1127                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1128                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
1129                                 return 0;
1130                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1131                                                     bmap->n_div-div-1) != -1)
1132                                 return 0;
1133                 } else
1134                         return 0;
1135         }
1136
1137         for (i = 0; i < bmap->n_div; ++i)
1138                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1139                         return 0;
1140
1141         return 1;
1142 }
1143
1144 /*
1145  * Remove divs that don't occur in any of the constraints or other divs.
1146  * These can arise when dropping some of the variables in a quast
1147  * returned by piplib.
1148  */
1149 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1150 {
1151         int i;
1152
1153         if (!bmap)
1154                 return NULL;
1155
1156         for (i = bmap->n_div-1; i >= 0; --i) {
1157                 if (!div_is_redundant(bmap, i))
1158                         continue;
1159                 bmap = isl_basic_map_drop_div(bmap, i);
1160         }
1161         return bmap;
1162 }
1163
1164 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1165 {
1166         bmap = remove_redundant_divs(bmap);
1167         if (!bmap)
1168                 return NULL;
1169         ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1170         return bmap;
1171 }
1172
1173 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1174 {
1175         return (struct isl_basic_set *)
1176                 isl_basic_map_finalize((struct isl_basic_map *)bset);
1177 }
1178
1179 struct isl_set *isl_set_finalize(struct isl_set *set)
1180 {
1181         int i;
1182
1183         if (!set)
1184                 return NULL;
1185         for (i = 0; i < set->n; ++i) {
1186                 set->p[i] = isl_basic_set_finalize(set->p[i]);
1187                 if (!set->p[i])
1188                         goto error;
1189         }
1190         return set;
1191 error:
1192         isl_set_free(set);
1193         return NULL;
1194 }
1195
1196 struct isl_map *isl_map_finalize(struct isl_map *map)
1197 {
1198         int i;
1199
1200         if (!map)
1201                 return NULL;
1202         for (i = 0; i < map->n; ++i) {
1203                 map->p[i] = isl_basic_map_finalize(map->p[i]);
1204                 if (!map->p[i])
1205                         goto error;
1206         }
1207         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1208         return map;
1209 error:
1210         isl_map_free(map);
1211         return NULL;
1212 }
1213
1214
1215 /* Remove definition of any div that is defined in terms of the given variable.
1216  * The div itself is not removed.  Functions such as
1217  * eliminate_divs_ineq depend on the other divs remaining in place.
1218  */
1219 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1220                                                                         int pos)
1221 {
1222         int i;
1223
1224         for (i = 0; i < bmap->n_div; ++i) {
1225                 if (isl_int_is_zero(bmap->div[i][0]))
1226                         continue;
1227                 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1228                         continue;
1229                 isl_int_set_si(bmap->div[i][0], 0);
1230         }
1231         return bmap;
1232 }
1233
1234 /* Eliminate the specified variables from the constraints using
1235  * Fourier-Motzkin.  The variables themselves are not removed.
1236  */
1237 struct isl_basic_map *isl_basic_map_eliminate_vars(
1238         struct isl_basic_map *bmap, unsigned pos, unsigned n)
1239 {
1240         int d;
1241         int i, j, k;
1242         unsigned total;
1243
1244         if (n == 0)
1245                 return bmap;
1246         if (!bmap)
1247                 return NULL;
1248         total = isl_basic_map_total_dim(bmap);
1249
1250         bmap = isl_basic_map_cow(bmap);
1251         for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1252                 bmap = remove_dependent_vars(bmap, d);
1253
1254         for (d = pos + n - 1;
1255              d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1256                 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1257         for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1258                 int n_lower, n_upper;
1259                 if (!bmap)
1260                         return NULL;
1261                 for (i = 0; i < bmap->n_eq; ++i) {
1262                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1263                                 continue;
1264                         eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1265                         isl_basic_map_drop_equality(bmap, i);
1266                         break;
1267                 }
1268                 if (i < bmap->n_eq)
1269                         continue;
1270                 n_lower = 0;
1271                 n_upper = 0;
1272                 for (i = 0; i < bmap->n_ineq; ++i) {
1273                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1274                                 n_lower++;
1275                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1276                                 n_upper++;
1277                 }
1278                 bmap = isl_basic_map_extend_constraints(bmap,
1279                                 0, n_lower * n_upper);
1280                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1281                         int last;
1282                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1283                                 continue;
1284                         last = -1;
1285                         for (j = 0; j < i; ++j) {
1286                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1287                                         continue;
1288                                 last = j;
1289                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1290                                     isl_int_sgn(bmap->ineq[j][1+d]))
1291                                         continue;
1292                                 k = isl_basic_map_alloc_inequality(bmap);
1293                                 if (k < 0)
1294                                         goto error;
1295                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1296                                                 1+total);
1297                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1298                                                 1+d, 1+total, NULL);
1299                         }
1300                         isl_basic_map_drop_inequality(bmap, i);
1301                         i = last + 1;
1302                 }
1303                 if (n_lower > 0 && n_upper > 0) {
1304                         bmap = isl_basic_map_normalize_constraints(bmap);
1305                         bmap = remove_duplicate_constraints(bmap, NULL);
1306                         bmap = isl_basic_map_gauss(bmap, NULL);
1307                         bmap = isl_basic_map_convex_hull(bmap);
1308                         if (!bmap)
1309                                 goto error;
1310                         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1311                                 break;
1312                 }
1313         }
1314         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1315         return bmap;
1316 error:
1317         isl_basic_map_free(bmap);
1318         return NULL;
1319 }
1320
1321 struct isl_basic_set *isl_basic_set_eliminate_vars(
1322         struct isl_basic_set *bset, unsigned pos, unsigned n)
1323 {
1324         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1325                         (struct isl_basic_map *)bset, pos, n);
1326 }
1327
1328 /* Don't assume equalities are in order, because align_divs
1329  * may have changed the order of the divs.
1330  */
1331 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1332 {
1333         int d, i;
1334         unsigned total;
1335
1336         total = isl_dim_total(bmap->dim);
1337         for (d = 0; d < total; ++d)
1338                 elim[d] = -1;
1339         for (i = 0; i < bmap->n_eq; ++i) {
1340                 for (d = total - 1; d >= 0; --d) {
1341                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1342                                 continue;
1343                         elim[d] = i;
1344                         break;
1345                 }
1346         }
1347 }
1348
1349 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1350 {
1351         compute_elimination_index((struct isl_basic_map *)bset, elim);
1352 }
1353
1354 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1355         struct isl_basic_map *bmap, int *elim)
1356 {
1357         int d;
1358         int copied = 0;
1359         unsigned total;
1360
1361         total = isl_dim_total(bmap->dim);
1362         for (d = total - 1; d >= 0; --d) {
1363                 if (isl_int_is_zero(src[1+d]))
1364                         continue;
1365                 if (elim[d] == -1)
1366                         continue;
1367                 if (!copied) {
1368                         isl_seq_cpy(dst, src, 1 + total);
1369                         copied = 1;
1370                 }
1371                 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1372         }
1373         return copied;
1374 }
1375
1376 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1377         struct isl_basic_set *bset, int *elim)
1378 {
1379         return reduced_using_equalities(dst, src,
1380                                         (struct isl_basic_map *)bset, elim);
1381 }
1382
1383 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1384         struct isl_basic_set *bset, struct isl_basic_set *context)
1385 {
1386         int i;
1387         int *elim;
1388
1389         if (!bset || !context)
1390                 goto error;
1391
1392         if (context->n_eq == 0) {
1393                 isl_basic_set_free(context);
1394                 return bset;
1395         }
1396
1397         bset = isl_basic_set_cow(bset);
1398         if (!bset)
1399                 goto error;
1400
1401         elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1402         if (!elim)
1403                 goto error;
1404         set_compute_elimination_index(context, elim);
1405         for (i = 0; i < bset->n_eq; ++i)
1406                 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1407                                                         context, elim);
1408         for (i = 0; i < bset->n_ineq; ++i)
1409                 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1410                                                         context, elim);
1411         isl_basic_set_free(context);
1412         free(elim);
1413         bset = isl_basic_set_simplify(bset);
1414         bset = isl_basic_set_finalize(bset);
1415         return bset;
1416 error:
1417         isl_basic_set_free(bset);
1418         isl_basic_set_free(context);
1419         return NULL;
1420 }
1421
1422 static struct isl_basic_set *remove_shifted_constraints(
1423         struct isl_basic_set *bset, struct isl_basic_set *context)
1424 {
1425         unsigned int size;
1426         isl_int ***index;
1427         int bits;
1428         int k, h, l;
1429
1430         if (!bset)
1431                 return NULL;
1432
1433         size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1434         bits = ffs(size) - 1;
1435         index = isl_calloc_array(ctx, isl_int **, size);
1436         if (!index)
1437                 return bset;
1438
1439         for (k = 0; k < context->n_ineq; ++k) {
1440                 h = set_hash_index(index, size, bits, context, k);
1441                 index[h] = &context->ineq[k];
1442         }
1443         for (k = 0; k < bset->n_ineq; ++k) {
1444                 h = set_hash_index(index, size, bits, bset, k);
1445                 if (!index[h])
1446                         continue;
1447                 l = index[h] - &context->ineq[0];
1448                 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1449                         continue;
1450                 bset = isl_basic_set_cow(bset);
1451                 if (!bset)
1452                         goto error;
1453                 isl_basic_set_drop_inequality(bset, k);
1454                 --k;
1455         }
1456         free(index);
1457         return bset;
1458 error:
1459         free(index);
1460         return bset;
1461 }
1462
1463 /* Tighten (decrease) the constant terms of the inequalities based
1464  * on the equalities, without removing any integer points.
1465  * For example, if there is an equality
1466  *
1467  *              i = 3 * j
1468  *
1469  * and an inequality
1470  *
1471  *              i >= 1
1472  *
1473  * then we want to replace the inequality by
1474  *
1475  *              i >= 3
1476  *
1477  * We do this by computing a variable compression and translating
1478  * the constraints to the compressed space.
1479  * If any constraint has coefficients (except the contant term)
1480  * with a common factor "f", then we can replace the constant term "c"
1481  * by
1482  *
1483  *              f * floor(c/f)
1484  *
1485  * That is, we add
1486  *
1487  *              f * floor(c/f) - c = -fract(c/f)
1488  *
1489  * and we can add the same value to the original constraint.
1490  *
1491  * In the example, the compressed space only contains "j",
1492  * and the inequality translates to
1493  *
1494  *              3 * j - 1 >= 0
1495  *
1496  * We add -fract(-1/3) = -2 to the original constraint to obtain
1497  *
1498  *              i - 3 >= 0
1499  */
1500 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1501         struct isl_basic_set *bset)
1502 {
1503         int i;
1504         unsigned total;
1505         struct isl_mat *B, *C;
1506         isl_int gcd;
1507
1508         if (!bset)
1509                 return NULL;
1510
1511         if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1512                 return bset;
1513
1514         if (!bset->n_ineq)
1515                 return bset;
1516
1517         bset = isl_basic_set_cow(bset);
1518         if (!bset)
1519                 return NULL;
1520
1521         total = isl_basic_set_total_dim(bset);
1522         B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1523         C = isl_mat_variable_compression(B, NULL);
1524         if (!C)
1525                 return bset;
1526         if (C->n_col == 0) {
1527                 isl_mat_free(C);
1528                 return isl_basic_set_set_to_empty(bset);
1529         }
1530         B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1531                                                 0, bset->n_ineq, 0, 1 + total);
1532         C = isl_mat_product(B, C);
1533         if (!C)
1534                 return bset;
1535
1536         isl_int_init(gcd);
1537         for (i = 0; i < bset->n_ineq; ++i) {
1538                 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1539                 if (isl_int_is_one(gcd))
1540                         continue;
1541                 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1542                 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1543         }
1544         isl_int_clear(gcd);
1545
1546         isl_mat_free(C);
1547
1548         return bset;
1549 }
1550
1551 /* Remove all information from bset that is redundant in the context
1552  * of context.  Both bset and context are assumed to be full-dimensional.
1553  *
1554  * We first * remove the inequalities from "bset"
1555  * that are obviously redundant with respect to some inequality in "context".
1556  *
1557  * If there are any inequalities left, we construct a tableau for
1558  * the context and then add the inequalities of "bset".
1559  * Before adding these inequalities, we freeze all constraints such that
1560  * they won't be considered redundant in terms of the constraints of "bset".
1561  * Then we detect all redundant constraints (among the
1562  * constraints that weren't frozen), first by checking for redundancy in the
1563  * the tableau and then by checking if replacing a constraint by its negation
1564  * would lead to an empty set.  This last step is fairly expensive
1565  * and could be optimized by more reuse of the tableau.
1566  * Finally, we update bset according to the results.
1567  */
1568 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
1569         __isl_take isl_basic_set *context)
1570 {
1571         int i, k;
1572         isl_basic_set *combined = NULL;
1573         struct isl_tab *tab = NULL;
1574         unsigned context_ineq;
1575         unsigned total;
1576
1577         if (!bset || !context)
1578                 goto error;
1579
1580         if (isl_basic_set_is_universe(bset)) {
1581                 isl_basic_set_free(context);
1582                 return bset;
1583         }
1584
1585         if (isl_basic_set_is_universe(context)) {
1586                 isl_basic_set_free(context);
1587                 return bset;
1588         }
1589
1590         bset = remove_shifted_constraints(bset, context);
1591         if (!bset)
1592                 goto error;
1593         if (bset->n_ineq == 0)
1594                 goto done;
1595
1596         context_ineq = context->n_ineq;
1597         combined = isl_basic_set_cow(isl_basic_set_copy(context));
1598         combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
1599         tab = isl_tab_from_basic_set(combined);
1600         for (i = 0; i < context_ineq; ++i)
1601                 if (isl_tab_freeze_constraint(tab, i) < 0)
1602                         goto error;
1603         tab = isl_tab_extend(tab, bset->n_ineq);
1604         for (i = 0; i < bset->n_ineq; ++i)
1605                 if (isl_tab_add_ineq(tab, bset->ineq[i]) < 0)
1606                         goto error;
1607         bset = isl_basic_set_add_constraints(combined, bset, 0);
1608         combined = NULL;
1609         if (!bset)
1610                 goto error;
1611         if (isl_tab_detect_redundant(tab) < 0)
1612                 goto error;
1613         total = isl_basic_set_total_dim(bset);
1614         for (i = context_ineq; i < bset->n_ineq; ++i) {
1615                 int is_empty;
1616                 if (tab->con[i].is_redundant)
1617                         continue;
1618                 tab->con[i].is_redundant = 1;
1619                 combined = isl_basic_set_dup(bset);
1620                 combined = isl_basic_set_update_from_tab(combined, tab);
1621                 combined = isl_basic_set_extend_constraints(combined, 0, 1);
1622                 k = isl_basic_set_alloc_inequality(combined);
1623                 if (k < 0)
1624                         goto error;
1625                 isl_seq_neg(combined->ineq[k], bset->ineq[i], 1 + total);
1626                 isl_int_sub_ui(combined->ineq[k][0], combined->ineq[k][0], 1);
1627                 is_empty = isl_basic_set_is_empty(combined);
1628                 if (is_empty < 0)
1629                         goto error;
1630                 isl_basic_set_free(combined);
1631                 combined = NULL;
1632                 if (!is_empty)
1633                         tab->con[i].is_redundant = 0;
1634         }
1635         for (i = 0; i < context_ineq; ++i)
1636                 tab->con[i].is_redundant = 1;
1637         bset = isl_basic_set_update_from_tab(bset, tab);
1638         if (bset) {
1639                 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1640                 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1641         }
1642
1643         isl_tab_free(tab);
1644 done:
1645         bset = isl_basic_set_simplify(bset);
1646         bset = isl_basic_set_finalize(bset);
1647         isl_basic_set_free(context);
1648         return bset;
1649 error:
1650         isl_tab_free(tab);
1651         isl_basic_set_free(combined);
1652         isl_basic_set_free(context);
1653         isl_basic_set_free(bset);
1654         return NULL;
1655 }
1656
1657 /* Remove all information from bset that is redundant in the context
1658  * of context.  In particular, equalities that are linear combinations
1659  * of those in context are removed.  Then the inequalities that are
1660  * redundant in the context of the equalities and inequalities of
1661  * context are removed.
1662  *
1663  * We first compute the integer affine hull of the intersection,
1664  * compute the gist inside this affine hull and then add back
1665  * those equalities that are not implied by the context.
1666  */
1667 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
1668         __isl_take isl_basic_set *context)
1669 {
1670         isl_mat *eq;
1671         isl_mat *T, *T2;
1672         isl_basic_set *aff;
1673         isl_basic_set *aff_context;
1674         unsigned total;
1675
1676         if (!bset || !context)
1677                 goto error;
1678
1679         bset = isl_basic_set_intersect(bset, isl_basic_set_copy(context));
1680         if (isl_basic_set_fast_is_empty(bset)) {
1681                 isl_basic_set_free(context);
1682                 return bset;
1683         }
1684         aff = isl_basic_set_affine_hull(isl_basic_set_copy(bset));
1685         if (!aff)
1686                 goto error;
1687         if (isl_basic_set_fast_is_empty(aff)) {
1688                 isl_basic_set_free(aff);
1689                 isl_basic_set_free(context);
1690                 return bset;
1691         }
1692         if (aff->n_eq == 0) {
1693                 isl_basic_set_free(aff);
1694                 return uset_gist_full(bset, context);
1695         }
1696         total = isl_basic_set_total_dim(bset);
1697         eq = isl_mat_sub_alloc(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
1698         eq = isl_mat_cow(eq);
1699         T = isl_mat_variable_compression(eq, &T2);
1700         if (T && T->n_col == 0) {
1701                 isl_mat_free(T);
1702                 isl_mat_free(T2);
1703                 isl_basic_set_free(context);
1704                 isl_basic_set_free(aff);
1705                 return isl_basic_set_set_to_empty(bset);
1706         }
1707
1708         aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
1709
1710         bset = isl_basic_set_preimage(bset, isl_mat_copy(T));
1711         context = isl_basic_set_preimage(context, T);
1712
1713         bset = uset_gist_full(bset, context);
1714         bset = isl_basic_set_preimage(bset, T2);
1715         bset = isl_basic_set_intersect(bset, aff);
1716         bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
1717
1718         if (bset) {
1719                 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1720                 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1721         }
1722
1723         return bset;
1724 error:
1725         isl_basic_set_free(bset);
1726         isl_basic_set_free(context);
1727         return NULL;
1728 }
1729
1730 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1731  * We simply add the equalities in context to bmap and then do a regular
1732  * div normalizations.  Better results can be obtained by normalizing
1733  * only the divs in bmap than do not also appear in context.
1734  * We need to be careful to reduce the divs using the equalities
1735  * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1736  * spurious constraints.
1737  */
1738 static struct isl_basic_map *normalize_divs_in_context(
1739         struct isl_basic_map *bmap, struct isl_basic_map *context)
1740 {
1741         int i;
1742         unsigned total_context;
1743         int div_eq;
1744
1745         div_eq = n_pure_div_eq(bmap);
1746         if (div_eq == 0)
1747                 return bmap;
1748
1749         if (context->n_div > 0)
1750                 bmap = isl_basic_map_align_divs(bmap, context);
1751
1752         total_context = isl_basic_map_total_dim(context);
1753         bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
1754         for (i = 0; i < context->n_eq; ++i) {
1755                 int k;
1756                 k = isl_basic_map_alloc_equality(bmap);
1757                 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
1758                 isl_seq_clr(bmap->eq[k] + 1 + total_context,
1759                                 isl_basic_map_total_dim(bmap) - total_context);
1760         }
1761         bmap = isl_basic_map_gauss(bmap, NULL);
1762         bmap = normalize_divs(bmap, NULL);
1763         bmap = isl_basic_map_gauss(bmap, NULL);
1764         return bmap;
1765 }
1766
1767 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1768         struct isl_basic_map *context)
1769 {
1770         struct isl_basic_set *bset;
1771
1772         if (!bmap || !context)
1773                 goto error;
1774
1775         if (isl_basic_map_is_universe(context)) {
1776                 isl_basic_map_free(context);
1777                 return bmap;
1778         }
1779         if (isl_basic_map_is_universe(bmap)) {
1780                 isl_basic_map_free(context);
1781                 return bmap;
1782         }
1783         if (isl_basic_map_fast_is_empty(context)) {
1784                 struct isl_dim *dim = isl_dim_copy(bmap->dim);
1785                 isl_basic_map_free(context);
1786                 isl_basic_map_free(bmap);
1787                 return isl_basic_map_universe(dim);
1788         }
1789         if (isl_basic_map_fast_is_empty(bmap)) {
1790                 isl_basic_map_free(context);
1791                 return bmap;
1792         }
1793
1794         bmap = isl_basic_map_convex_hull(bmap);
1795         context = isl_basic_map_convex_hull(context);
1796
1797         if (context->n_eq)
1798                 bmap = normalize_divs_in_context(bmap, context);
1799
1800         context = isl_basic_map_align_divs(context, bmap);
1801         bmap = isl_basic_map_align_divs(bmap, context);
1802
1803         bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1804                          isl_basic_map_underlying_set(context));
1805
1806         return isl_basic_map_overlying_set(bset, bmap);
1807 error:
1808         isl_basic_map_free(bmap);
1809         isl_basic_map_free(context);
1810         return NULL;
1811 }
1812
1813 /*
1814  * Assumes context has no implicit divs.
1815  */
1816 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
1817         __isl_take isl_basic_map *context)
1818 {
1819         int i;
1820
1821         if (!map || !context)
1822                 goto error;;
1823
1824         if (isl_basic_map_is_universe(context)) {
1825                 isl_basic_map_free(context);
1826                 return map;
1827         }
1828         if (isl_basic_map_fast_is_empty(context)) {
1829                 struct isl_dim *dim = isl_dim_copy(map->dim);
1830                 isl_basic_map_free(context);
1831                 isl_map_free(map);
1832                 return isl_map_universe(dim);
1833         }
1834
1835         context = isl_basic_map_convex_hull(context);
1836         map = isl_map_cow(map);
1837         if (!map || !context)
1838                 goto error;;
1839         isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1840         map = isl_map_compute_divs(map);
1841         for (i = 0; i < map->n; ++i)
1842                 context = isl_basic_map_align_divs(context, map->p[i]);
1843         for (i = 0; i < map->n; ++i) {
1844                 map->p[i] = isl_basic_map_gist(map->p[i],
1845                                                 isl_basic_map_copy(context));
1846                 if (!map->p[i])
1847                         goto error;
1848         }
1849         isl_basic_map_free(context);
1850         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1851         return map;
1852 error:
1853         isl_map_free(map);
1854         isl_basic_map_free(context);
1855         return NULL;
1856 }
1857
1858 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
1859         __isl_take isl_map *context)
1860 {
1861         return isl_map_gist_basic_map(map, isl_map_convex_hull(context));
1862 }
1863
1864 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1865                                                 struct isl_basic_set *context)
1866 {
1867         return (struct isl_basic_set *)isl_basic_map_gist(
1868                 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1869 }
1870
1871 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
1872         __isl_take isl_basic_set *context)
1873 {
1874         return (struct isl_set *)isl_map_gist_basic_map((struct isl_map *)set,
1875                                         (struct isl_basic_map *)context);
1876 }
1877
1878 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
1879         __isl_take isl_set *context)
1880 {
1881         return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1882                                         (struct isl_map *)context);
1883 }
1884
1885 /* Quick check to see if two basic maps are disjoint.
1886  * In particular, we reduce the equalities and inequalities of
1887  * one basic map in the context of the equalities of the other
1888  * basic map and check if we get a contradiction.
1889  */
1890 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1891         struct isl_basic_map *bmap2)
1892 {
1893         struct isl_vec *v = NULL;
1894         int *elim = NULL;
1895         unsigned total;
1896         int i;
1897
1898         if (!bmap1 || !bmap2)
1899                 return -1;
1900         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1901                         return -1);
1902         if (bmap1->n_div || bmap2->n_div)
1903                 return 0;
1904         if (!bmap1->n_eq && !bmap2->n_eq)
1905                 return 0;
1906
1907         total = isl_dim_total(bmap1->dim);
1908         if (total == 0)
1909                 return 0;
1910         v = isl_vec_alloc(bmap1->ctx, 1 + total);
1911         if (!v)
1912                 goto error;
1913         elim = isl_alloc_array(bmap1->ctx, int, total);
1914         if (!elim)
1915                 goto error;
1916         compute_elimination_index(bmap1, elim);
1917         for (i = 0; i < bmap2->n_eq; ++i) {
1918                 int reduced;
1919                 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1920                                                         bmap1, elim);
1921                 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1922                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1923                         goto disjoint;
1924         }
1925         for (i = 0; i < bmap2->n_ineq; ++i) {
1926                 int reduced;
1927                 reduced = reduced_using_equalities(v->block.data,
1928                                                 bmap2->ineq[i], bmap1, elim);
1929                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1930                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1931                         goto disjoint;
1932         }
1933         compute_elimination_index(bmap2, elim);
1934         for (i = 0; i < bmap1->n_ineq; ++i) {
1935                 int reduced;
1936                 reduced = reduced_using_equalities(v->block.data,
1937                                                 bmap1->ineq[i], bmap2, elim);
1938                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1939                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1940                         goto disjoint;
1941         }
1942         isl_vec_free(v);
1943         free(elim);
1944         return 0;
1945 disjoint:
1946         isl_vec_free(v);
1947         free(elim);
1948         return 1;
1949 error:
1950         isl_vec_free(v);
1951         free(elim);
1952         return -1;
1953 }
1954
1955 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1956         struct isl_basic_set *bset2)
1957 {
1958         return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1959                                               (struct isl_basic_map *)bset2);
1960 }
1961
1962 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1963 {
1964         int i, j;
1965
1966         if (!map1 || !map2)
1967                 return -1;
1968
1969         if (isl_map_fast_is_equal(map1, map2))
1970                 return 0;
1971
1972         for (i = 0; i < map1->n; ++i) {
1973                 for (j = 0; j < map2->n; ++j) {
1974                         int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1975                                                                map2->p[j]);
1976                         if (d != 1)
1977                                 return d;
1978                 }
1979         }
1980         return 1;
1981 }
1982
1983 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1984 {
1985         return isl_map_fast_is_disjoint((struct isl_map *)set1,
1986                                         (struct isl_map *)set2);
1987 }
1988
1989 /* Check if we can combine a given div with lower bound l and upper
1990  * bound u with some other div and if so return that other div.
1991  * Otherwise return -1.
1992  *
1993  * We first check that
1994  *      - the bounds are opposites of each other (except for the constant
1995  *        term)
1996  *      - the bounds do not reference any other div
1997  *      - no div is defined in terms of this div
1998  *
1999  * Let m be the size of the range allowed on the div by the bounds.
2000  * That is, the bounds are of the form
2001  *
2002  *      e <= a <= e + m - 1
2003  *
2004  * with e some expression in the other variables.
2005  * We look for another div b such that no third div is defined in terms
2006  * of this second div b and such that in any constraint that contains
2007  * a (except for the given lower and upper bound), also contains b
2008  * with a coefficient that is m times that of b.
2009  * That is, all constraints (execpt for the lower and upper bound)
2010  * are of the form
2011  *
2012  *      e + f (a + m b) >= 0
2013  *
2014  * If so, we return b so that "a + m b" can be replaced by
2015  * a single div "c = a + m b".
2016  */
2017 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
2018         unsigned div, unsigned l, unsigned u)
2019 {
2020         int i, j;
2021         unsigned dim;
2022         int coalesce = -1;
2023
2024         if (bmap->n_div <= 1)
2025                 return -1;
2026         dim = isl_dim_total(bmap->dim);
2027         if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
2028                 return -1;
2029         if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
2030                                    bmap->n_div - div - 1) != -1)
2031                 return -1;
2032         if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
2033                             dim + bmap->n_div))
2034                 return -1;
2035
2036         for (i = 0; i < bmap->n_div; ++i) {
2037                 if (isl_int_is_zero(bmap->div[i][0]))
2038                         continue;
2039                 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
2040                         return -1;
2041         }
2042
2043         isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2044         if (isl_int_is_neg(bmap->ineq[l][0])) {
2045                 isl_int_sub(bmap->ineq[l][0],
2046                             bmap->ineq[l][0], bmap->ineq[u][0]);
2047                 bmap = isl_basic_map_copy(bmap);
2048                 bmap = isl_basic_map_set_to_empty(bmap);
2049                 isl_basic_map_free(bmap);
2050                 return -1;
2051         }
2052         isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2053         for (i = 0; i < bmap->n_div; ++i) {
2054                 if (i == div)
2055                         continue;
2056                 if (!pairs[i])
2057                         continue;
2058                 for (j = 0; j < bmap->n_div; ++j) {
2059                         if (isl_int_is_zero(bmap->div[j][0]))
2060                                 continue;
2061                         if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
2062                                 break;
2063                 }
2064                 if (j < bmap->n_div)
2065                         continue;
2066                 for (j = 0; j < bmap->n_ineq; ++j) {
2067                         int valid;
2068                         if (j == l || j == u)
2069                                 continue;
2070                         if (isl_int_is_zero(bmap->ineq[j][1 + dim + div]))
2071                                 continue;
2072                         if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
2073                                 break;
2074                         isl_int_mul(bmap->ineq[j][1 + dim + div],
2075                                     bmap->ineq[j][1 + dim + div],
2076                                     bmap->ineq[l][0]);
2077                         valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
2078                                            bmap->ineq[j][1 + dim + i]);
2079                         isl_int_divexact(bmap->ineq[j][1 + dim + div],
2080                                          bmap->ineq[j][1 + dim + div],
2081                                          bmap->ineq[l][0]);
2082                         if (!valid)
2083                                 break;
2084                 }
2085                 if (j < bmap->n_ineq)
2086                         continue;
2087                 coalesce = i;
2088                 break;
2089         }
2090         isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
2091         isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
2092         return coalesce;
2093 }
2094
2095 /* Given a lower and an upper bound on div i, construct an inequality
2096  * that when nonnegative ensures that this pair of bounds always allows
2097  * for an integer value of the given div.
2098  * The lower bound is inequality l, while the upper bound is inequality u.
2099  * The constructed inequality is stored in ineq.
2100  * g, fl, fu are temporary scalars.
2101  *
2102  * Let the upper bound be
2103  *
2104  *      -n_u a + e_u >= 0
2105  *
2106  * and the lower bound
2107  *
2108  *      n_l a + e_l >= 0
2109  *
2110  * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
2111  * We have
2112  *
2113  *      - f_u e_l <= f_u f_l g a <= f_l e_u
2114  *
2115  * Since all variables are integer valued, this is equivalent to
2116  *
2117  *      - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
2118  *
2119  * If this interval is at least f_u f_l g, then it contains at least
2120  * one integer value for a.
2121  * That is, the test constraint is
2122  *
2123  *      f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
2124  */
2125 static void construct_test_ineq(struct isl_basic_map *bmap, int i,
2126         int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
2127 {
2128         unsigned dim;
2129         dim = isl_dim_total(bmap->dim);
2130
2131         isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
2132         isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
2133         isl_int_divexact(fu, bmap->ineq[u][1 + dim + i], g);
2134         isl_int_neg(fu, fu);
2135         isl_seq_combine(ineq, fl, bmap->ineq[u], fu, bmap->ineq[l],
2136                         1 + dim + bmap->n_div);
2137         isl_int_add(ineq[0], ineq[0], fl);
2138         isl_int_add(ineq[0], ineq[0], fu);
2139         isl_int_sub_ui(ineq[0], ineq[0], 1);
2140         isl_int_mul(g, g, fl);
2141         isl_int_mul(g, g, fu);
2142         isl_int_sub(ineq[0], ineq[0], g);
2143 }
2144
2145 /* Remove more kinds of divs that are not strictly needed.
2146  * In particular, if all pairs of lower and upper bounds on a div
2147  * are such that they allow at least one integer value of the div,
2148  * the we can eliminate the div using Fourier-Motzkin without
2149  * introducing any spurious solutions.
2150  */
2151 static struct isl_basic_map *drop_more_redundant_divs(
2152         struct isl_basic_map *bmap, int *pairs, int n)
2153 {
2154         struct isl_tab *tab = NULL;
2155         struct isl_vec *vec = NULL;
2156         unsigned dim;
2157         int remove = -1;
2158         isl_int g, fl, fu;
2159
2160         isl_int_init(g);
2161         isl_int_init(fl);
2162         isl_int_init(fu);
2163
2164         if (!bmap)
2165                 goto error;
2166
2167         dim = isl_dim_total(bmap->dim);
2168         vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
2169         if (!vec)
2170                 goto error;
2171
2172         tab = isl_tab_from_basic_map(bmap);
2173
2174         while (n > 0) {
2175                 int i, l, u;
2176                 int best = -1;
2177                 enum isl_lp_result res;
2178
2179                 for (i = 0; i < bmap->n_div; ++i) {
2180                         if (!pairs[i])
2181                                 continue;
2182                         if (best >= 0 && pairs[best] <= pairs[i])
2183                                 continue;
2184                         best = i;
2185                 }
2186
2187                 i = best;
2188                 for (l = 0; l < bmap->n_ineq; ++l) {
2189                         if (!isl_int_is_pos(bmap->ineq[l][1 + dim + i]))
2190                                 continue;
2191                         for (u = 0; u < bmap->n_ineq; ++u) {
2192                                 if (!isl_int_is_neg(bmap->ineq[u][1 + dim + i]))
2193                                         continue;
2194                                 construct_test_ineq(bmap, i, l, u,
2195                                                     vec->el, g, fl, fu);
2196                                 res = isl_tab_min(tab, vec->el,
2197                                                   bmap->ctx->one, &g, NULL, 0);
2198                                 if (res == isl_lp_error)
2199                                         goto error;
2200                                 if (res == isl_lp_empty) {
2201                                         bmap = isl_basic_map_set_to_empty(bmap);
2202                                         break;
2203                                 }
2204                                 if (res != isl_lp_ok || isl_int_is_neg(g))
2205                                         break;
2206                         }
2207                         if (u < bmap->n_ineq)
2208                                 break;
2209                 }
2210                 if (l == bmap->n_ineq) {
2211                         remove = i;
2212                         break;
2213                 }
2214                 pairs[i] = 0;
2215                 --n;
2216         }
2217
2218         isl_tab_free(tab);
2219         isl_vec_free(vec);
2220
2221         isl_int_clear(g);
2222         isl_int_clear(fl);
2223         isl_int_clear(fu);
2224
2225         free(pairs);
2226
2227         if (remove < 0)
2228                 return bmap;
2229
2230         bmap = isl_basic_map_remove(bmap, isl_dim_div, remove, 1);
2231         return isl_basic_map_drop_redundant_divs(bmap);
2232 error:
2233         free(pairs);
2234         isl_basic_map_free(bmap);
2235         isl_tab_free(tab);
2236         isl_vec_free(vec);
2237         isl_int_clear(g);
2238         isl_int_clear(fl);
2239         isl_int_clear(fu);
2240         return NULL;
2241 }
2242
2243 /* Given a pair of divs div1 and div2 such that, expect for the lower bound l
2244  * and the upper bound u, div1 always occurs together with div2 in the form 
2245  * (div1 + m div2), where m is the constant range on the variable div1
2246  * allowed by l and u, replace the pair div1 and div2 by a single
2247  * div that is equal to div1 + m div2.
2248  *
2249  * The new div will appear in the location that contains div2.
2250  * We need to modify all constraints that contain
2251  * div2 = (div - div1) / m
2252  * (If a constraint does not contain div2, it will also not contain div1.)
2253  * If the constraint also contains div1, then we know they appear
2254  * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
2255  * i.e., the coefficient of div is f.
2256  *
2257  * Otherwise, we first need to introduce div1 into the constraint.
2258  * Let the l be
2259  *
2260  *      div1 + f >=0
2261  *
2262  * and u
2263  *
2264  *      -div1 + f' >= 0
2265  *
2266  * A lower bound on div2
2267  *
2268  *      n div2 + t >= 0
2269  *
2270  * can be replaced by
2271  *
2272  *      (n * (m div 2 + div1) + m t + n f)/g >= 0
2273  *
2274  * with g = gcd(m,n).
2275  * An upper bound
2276  *
2277  *      -n div2 + t >= 0
2278  *
2279  * can be replaced by
2280  *
2281  *      (-n * (m div2 + div1) + m t + n f')/g >= 0
2282  *
2283  * These constraint are those that we would obtain from eliminating
2284  * div1 using Fourier-Motzkin.
2285  *
2286  * After all constraints have been modified, we drop the lower and upper
2287  * bound and then drop div1.
2288  */
2289 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
2290         unsigned div1, unsigned div2, unsigned l, unsigned u)
2291 {
2292         isl_int a;
2293         isl_int b;
2294         isl_int m;
2295         unsigned dim, total;
2296         int i;
2297
2298         dim = isl_dim_total(bmap->dim);
2299         total = 1 + dim + bmap->n_div;
2300
2301         isl_int_init(a);
2302         isl_int_init(b);
2303         isl_int_init(m);
2304         isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
2305         isl_int_add_ui(m, m, 1);
2306
2307         for (i = 0; i < bmap->n_ineq; ++i) {
2308                 if (i == l || i == u)
2309                         continue;
2310                 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
2311                         continue;
2312                 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
2313                         isl_int_gcd(b, m, bmap->ineq[i][1 + dim + div2]);
2314                         isl_int_divexact(a, m, b);
2315                         isl_int_divexact(b, bmap->ineq[i][1 + dim + div2], b);
2316                         if (isl_int_is_pos(b)) {
2317                                 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2318                                                 b, bmap->ineq[l], total);
2319                         } else {
2320                                 isl_int_neg(b, b);
2321                                 isl_seq_combine(bmap->ineq[i], a, bmap->ineq[i],
2322                                                 b, bmap->ineq[u], total);
2323                         }
2324                 }
2325                 isl_int_set(bmap->ineq[i][1 + dim + div2],
2326                             bmap->ineq[i][1 + dim + div1]);
2327                 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
2328         }
2329
2330         isl_int_clear(a);
2331         isl_int_clear(b);
2332         isl_int_clear(m);
2333         if (l > u) {
2334                 isl_basic_map_drop_inequality(bmap, l);
2335                 isl_basic_map_drop_inequality(bmap, u);
2336         } else {
2337                 isl_basic_map_drop_inequality(bmap, u);
2338                 isl_basic_map_drop_inequality(bmap, l);
2339         }
2340         bmap = isl_basic_map_drop_div(bmap, div1);
2341         return bmap;
2342 }
2343
2344 /* First check if we can coalesce any pair of divs and
2345  * then continue with dropping more redundant divs.
2346  *
2347  * We loop over all pairs of lower and upper bounds on a div
2348  * with coefficient 1 and -1, respectively, check if there
2349  * is any other div "c" with which we can coalesce the div
2350  * and if so, perform the coalescing.
2351  */
2352 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
2353         struct isl_basic_map *bmap, int *pairs, int n)
2354 {
2355         int i, l, u;
2356         unsigned dim;
2357
2358         dim = isl_dim_total(bmap->dim);
2359
2360         for (i = 0; i < bmap->n_div; ++i) {
2361                 if (!pairs[i])
2362                         continue;
2363                 for (l = 0; l < bmap->n_ineq; ++l) {
2364                         if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
2365                                 continue;
2366                         for (u = 0; u < bmap->n_ineq; ++u) {
2367                                 int c;
2368
2369                                 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
2370                                         continue;
2371                                 c = div_find_coalesce(bmap, pairs, i, l, u);
2372                                 if (c < 0)
2373                                         continue;
2374                                 free(pairs);
2375                                 bmap = coalesce_divs(bmap, i, c, l, u);
2376                                 return isl_basic_map_drop_redundant_divs(bmap);
2377                         }
2378                 }
2379         }
2380
2381         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
2382                 return bmap;
2383
2384         return drop_more_redundant_divs(bmap, pairs, n);
2385 }
2386
2387 /* Remove divs that are not strictly needed.
2388  * In particular, if a div only occurs positively (or negatively)
2389  * in constraints, then it can simply be dropped.
2390  * Also, if a div occurs only occurs in two constraints and if moreover
2391  * those two constraints are opposite to each other, except for the constant
2392  * term and if the sum of the constant terms is such that for any value
2393  * of the other values, there is always at least one integer value of the
2394  * div, i.e., if one plus this sum is greater than or equal to
2395  * the (absolute value) of the coefficent of the div in the constraints,
2396  * then we can also simply drop the div.
2397  *
2398  * If any divs are left after these simple checks then we move on
2399  * to more complicated cases in drop_more_redundant_divs.
2400  */
2401 struct isl_basic_map *isl_basic_map_drop_redundant_divs(
2402         struct isl_basic_map *bmap)
2403 {
2404         int i, j;
2405         unsigned off;
2406         int *pairs = NULL;
2407         int n = 0;
2408
2409         if (!bmap)
2410                 goto error;
2411
2412         off = isl_dim_total(bmap->dim);
2413         pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
2414         if (!pairs)
2415                 goto error;
2416
2417         for (i = 0; i < bmap->n_div; ++i) {
2418                 int pos, neg;
2419                 int last_pos, last_neg;
2420                 int redundant;
2421                 int defined;
2422
2423                 defined = !isl_int_is_zero(bmap->div[i][0]);
2424                 for (j = 0; j < bmap->n_eq; ++j)
2425                         if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
2426                                 break;
2427                 if (j < bmap->n_eq)
2428                         continue;
2429                 ++n;
2430                 pos = neg = 0;
2431                 for (j = 0; j < bmap->n_ineq; ++j) {
2432                         if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
2433                                 last_pos = j;
2434                                 ++pos;
2435                         }
2436                         if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
2437                                 last_neg = j;
2438                                 ++neg;
2439                         }
2440                 }
2441                 pairs[i] = pos * neg;
2442                 if (pairs[i] == 0) {
2443                         for (j = bmap->n_ineq - 1; j >= 0; --j)
2444                                 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
2445                                         isl_basic_map_drop_inequality(bmap, j);
2446                         bmap = isl_basic_map_drop_div(bmap, i);
2447                         free(pairs);
2448                         return isl_basic_map_drop_redundant_divs(bmap);
2449                 }
2450                 if (pairs[i] != 1)
2451                         continue;
2452                 if (!isl_seq_is_neg(bmap->ineq[last_pos] + 1,
2453                                     bmap->ineq[last_neg] + 1,
2454                                     off + bmap->n_div))
2455                         continue;
2456
2457                 isl_int_add(bmap->ineq[last_pos][0],
2458                             bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2459                 isl_int_add_ui(bmap->ineq[last_pos][0],
2460                                bmap->ineq[last_pos][0], 1);
2461                 redundant = isl_int_ge(bmap->ineq[last_pos][0],
2462                                 bmap->ineq[last_pos][1+off+i]);
2463                 isl_int_sub_ui(bmap->ineq[last_pos][0],
2464                                bmap->ineq[last_pos][0], 1);
2465                 isl_int_sub(bmap->ineq[last_pos][0],
2466                             bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
2467                 if (!redundant) {
2468                         if (defined ||
2469                             !ok_to_set_div_from_bound(bmap, i, last_pos)) {
2470                                 pairs[i] = 0;
2471                                 --n;
2472                                 continue;
2473                         }
2474                         bmap = set_div_from_lower_bound(bmap, i, last_pos);
2475                         bmap = isl_basic_map_simplify(bmap);
2476                         free(pairs);
2477                         return isl_basic_map_drop_redundant_divs(bmap);
2478                 }
2479                 if (last_pos > last_neg) {
2480                         isl_basic_map_drop_inequality(bmap, last_pos);
2481                         isl_basic_map_drop_inequality(bmap, last_neg);
2482                 } else {
2483                         isl_basic_map_drop_inequality(bmap, last_neg);
2484                         isl_basic_map_drop_inequality(bmap, last_pos);
2485                 }
2486                 bmap = isl_basic_map_drop_div(bmap, i);
2487                 free(pairs);
2488                 return isl_basic_map_drop_redundant_divs(bmap);
2489         }
2490
2491         if (n > 0)
2492                 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
2493
2494         free(pairs);
2495         return bmap;
2496 error:
2497         free(pairs);
2498         isl_basic_map_free(bmap);
2499         return NULL;
2500 }
2501
2502 struct isl_basic_set *isl_basic_set_drop_redundant_divs(
2503         struct isl_basic_set *bset)
2504 {
2505         return (struct isl_basic_set *)
2506             isl_basic_map_drop_redundant_divs((struct isl_basic_map *)bset);
2507 }
2508
2509 struct isl_map *isl_map_drop_redundant_divs(struct isl_map *map)
2510 {
2511         int i;
2512
2513         if (!map)
2514                 return NULL;
2515         for (i = 0; i < map->n; ++i) {
2516                 map->p[i] = isl_basic_map_drop_redundant_divs(map->p[i]);
2517                 if (!map->p[i])
2518                         goto error;
2519         }
2520         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2521         return map;
2522 error:
2523         isl_map_free(map);
2524         return NULL;
2525 }
2526
2527 struct isl_set *isl_set_drop_redundant_divs(struct isl_set *set)
2528 {
2529         return (struct isl_set *)
2530             isl_map_drop_redundant_divs((struct isl_map *)set);
2531 }