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