isl_basic_map_gauss: be careful not to introduce circular div definitions
[platform/upstream/isl.git] / isl_map_simplify.c
1 #include "isl_equalities.h"
2 #include "isl_map.h"
3 #include "isl_map_private.h"
4
5 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
6 {
7         isl_int *t = bmap->eq[a];
8         bmap->eq[a] = bmap->eq[b];
9         bmap->eq[b] = t;
10 }
11
12 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
13 {
14         if (a != b) {
15                 isl_int *t = bmap->ineq[a];
16                 bmap->ineq[a] = bmap->ineq[b];
17                 bmap->ineq[b] = t;
18         }
19 }
20
21 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
22 {
23         swap_inequality((struct isl_basic_map *)bset, a, b);
24 }
25
26 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
27 {
28         isl_seq_cpy(c, c + n, rem);
29         isl_seq_clr(c + rem, n);
30 }
31
32 /* Drop n dimensions starting at first.
33  *
34  * In principle, this frees up some extra variables as the number
35  * of columns remains constant, but we would have to extend
36  * the div array too as the number of rows in this array is assumed
37  * to be equal to extra.
38  */
39 struct isl_basic_set *isl_basic_set_drop_dims(
40                 struct isl_basic_set *bset, unsigned first, unsigned n)
41 {
42         int i;
43
44         if (!bset)
45                 goto error;
46
47         isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
48
49         if (n == 0)
50                 return bset;
51
52         bset = isl_basic_set_cow(bset);
53         if (!bset)
54                 return NULL;
55
56         for (i = 0; i < bset->n_eq; ++i)
57                 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
58                                      (bset->dim->n_out-first-n)+bset->extra);
59
60         for (i = 0; i < bset->n_ineq; ++i)
61                 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
62                                      (bset->dim->n_out-first-n)+bset->extra);
63
64         for (i = 0; i < bset->n_div; ++i)
65                 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
66                                      (bset->dim->n_out-first-n)+bset->extra);
67
68         bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
69         if (!bset->dim)
70                 goto error;
71
72         F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
73         bset = isl_basic_set_simplify(bset);
74         return isl_basic_set_finalize(bset);
75 error:
76         isl_basic_set_free(bset);
77         return NULL;
78 }
79
80 struct isl_set *isl_set_drop_dims(
81                 struct isl_set *set, unsigned first, unsigned n)
82 {
83         int i;
84
85         if (!set)
86                 goto error;
87
88         isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
89
90         if (n == 0)
91                 return set;
92         set = isl_set_cow(set);
93         if (!set)
94                 goto error;
95         set->dim = isl_dim_drop_outputs(set->dim, first, n);
96         if (!set->dim)
97                 goto error;
98
99         for (i = 0; i < set->n; ++i) {
100                 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
101                 if (!set->p[i])
102                         goto error;
103         }
104
105         F_CLR(set, ISL_SET_NORMALIZED);
106         return set;
107 error:
108         isl_set_free(set);
109         return NULL;
110 }
111
112 /* Drop n input dimensions starting at first.
113  *
114  * In principle, this frees up some extra variables as the number
115  * of columns remains constant, but we would have to extend
116  * the div array too as the number of rows in this array is assumed
117  * to be equal to extra.
118  */
119 struct isl_basic_map *isl_basic_map_drop_inputs(
120                 struct isl_basic_map *bmap, unsigned first, unsigned n)
121 {
122         int i;
123         unsigned nparam;
124         unsigned n_in;
125         unsigned n_out;
126
127         if (!bmap)
128                 goto error;
129
130         nparam = isl_basic_map_n_param(bmap);
131         n_in = isl_basic_map_n_in(bmap);
132         n_out = isl_basic_map_n_out(bmap);
133         isl_assert(bmap->ctx, first + n <= n_in, goto error);
134
135         if (n == 0)
136                 return bmap;
137
138         bmap = isl_basic_map_cow(bmap);
139         if (!bmap)
140                 return NULL;
141
142         for (i = 0; i < bmap->n_eq; ++i)
143                 constraint_drop_vars(bmap->eq[i]+1+nparam+first, n,
144                                  (n_in-first-n)+n_out+bmap->extra);
145
146         for (i = 0; i < bmap->n_ineq; ++i)
147                 constraint_drop_vars(bmap->ineq[i]+1+nparam+first, n,
148                                  (n_in-first-n)+n_out+bmap->extra);
149
150         for (i = 0; i < bmap->n_div; ++i)
151                 constraint_drop_vars(bmap->div[i]+1+1+nparam+first, n,
152                                  (n_in-first-n)+n_out+bmap->extra);
153
154         bmap->dim = isl_dim_drop_inputs(bmap->dim, first, n);
155         if (!bmap->dim)
156                 goto error;
157
158         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
159         bmap = isl_basic_map_simplify(bmap);
160         return isl_basic_map_finalize(bmap);
161 error:
162         isl_basic_map_free(bmap);
163         return NULL;
164 }
165
166 struct isl_map *isl_map_drop_inputs(
167                 struct isl_map *map, unsigned first, unsigned n)
168 {
169         int i;
170
171         if (!map)
172                 goto error;
173
174         isl_assert(map->ctx, first + n <= map->dim->n_in, goto error);
175
176         if (n == 0)
177                 return map;
178         map = isl_map_cow(map);
179         if (!map)
180                 goto error;
181         map->dim = isl_dim_drop_inputs(map->dim, first, n);
182         if (!map->dim)
183                 goto error;
184
185         for (i = 0; i < map->n; ++i) {
186                 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
187                 if (!map->p[i])
188                         goto error;
189         }
190         F_CLR(map, ISL_MAP_NORMALIZED);
191
192         return map;
193 error:
194         isl_map_free(map);
195         return NULL;
196 }
197
198 /*
199  * We don't cow, as the div is assumed to be redundant.
200  */
201 static struct isl_basic_map *isl_basic_map_drop_div(
202                 struct isl_basic_map *bmap, unsigned div)
203 {
204         int i;
205         unsigned pos;
206
207         if (!bmap)
208                 goto error;
209
210         pos = 1 + isl_dim_total(bmap->dim) + div;
211
212         isl_assert(bmap->ctx, div < bmap->n_div, goto error);
213
214         for (i = 0; i < bmap->n_eq; ++i)
215                 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
216
217         for (i = 0; i < bmap->n_ineq; ++i) {
218                 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
219                         isl_basic_map_drop_inequality(bmap, i);
220                         --i;
221                         continue;
222                 }
223                 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
224         }
225
226         for (i = 0; i < bmap->n_div; ++i)
227                 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
228
229         if (div != bmap->n_div - 1) {
230                 int j;
231                 isl_int *t = bmap->div[div];
232
233                 for (j = div; j < bmap->n_div - 1; ++j)
234                         bmap->div[j] = bmap->div[j+1];
235
236                 bmap->div[bmap->n_div - 1] = t;
237         }
238         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
239         isl_basic_map_free_div(bmap, 1);
240
241         return bmap;
242 error:
243         isl_basic_map_free(bmap);
244         return NULL;
245 }
246
247 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
248 {
249         int i;
250         isl_int gcd;
251         unsigned total = isl_basic_map_total_dim(bmap);
252
253         isl_int_init(gcd);
254         for (i = bmap->n_eq - 1; i >= 0; --i) {
255                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
256                 if (isl_int_is_zero(gcd)) {
257                         if (!isl_int_is_zero(bmap->eq[i][0])) {
258                                 bmap = isl_basic_map_set_to_empty(bmap);
259                                 break;
260                         }
261                         isl_basic_map_drop_equality(bmap, i);
262                         continue;
263                 }
264                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
265                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
266                 if (isl_int_is_one(gcd))
267                         continue;
268                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
269                         bmap = isl_basic_map_set_to_empty(bmap);
270                         break;
271                 }
272                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
273         }
274
275         for (i = bmap->n_ineq - 1; i >= 0; --i) {
276                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
277                 if (isl_int_is_zero(gcd)) {
278                         if (isl_int_is_neg(bmap->ineq[i][0])) {
279                                 bmap = isl_basic_map_set_to_empty(bmap);
280                                 break;
281                         }
282                         isl_basic_map_drop_inequality(bmap, i);
283                         continue;
284                 }
285                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
286                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
287                 if (isl_int_is_one(gcd))
288                         continue;
289                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
290                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
291         }
292         isl_int_clear(gcd);
293
294         return bmap;
295 }
296
297 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
298 {
299         int i;
300         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
301         unsigned len;
302         len = 1 + isl_basic_map_total_dim(bmap);
303
304         for (i = 0; i < bmap->n_eq; ++i)
305                 if (bmap->eq[i] != eq)
306                         isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
307
308         for (i = 0; i < bmap->n_ineq; ++i)
309                 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
310
311         /* We need to be careful about circular definitions,
312          * so for now we just remove the definitions of other divs that
313          * depend on this div and (possibly) recompute them later.
314          */
315         for (i = 0; i < bmap->n_div; ++i)
316                 if (!isl_int_is_zero(bmap->div[i][0]) &&
317                     !isl_int_is_zero(bmap->div[i][1 + pos]))
318                         isl_seq_clr(bmap->div[i], 1 + len);
319
320         isl_basic_map_drop_div(bmap, div);
321 }
322
323 /* Elimininate divs based on equalities
324  */
325 static struct isl_basic_map *eliminate_divs_eq(
326                 struct isl_basic_map *bmap, int *progress)
327 {
328         int d;
329         int i;
330         int modified = 0;
331         unsigned off;
332
333         if (!bmap)
334                 return NULL;
335
336         off = 1 + isl_dim_total(bmap->dim);
337
338         for (d = bmap->n_div - 1; d >= 0 ; --d) {
339                 for (i = 0; i < bmap->n_eq; ++i) {
340                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
341                             !isl_int_is_negone(bmap->eq[i][off + d]))
342                                 continue;
343                         modified = 1;
344                         *progress = 1;
345                         eliminate_div(bmap, bmap->eq[i], d);
346                         isl_basic_map_drop_equality(bmap, i);
347                         break;
348                 }
349         }
350         if (modified)
351                 return eliminate_divs_eq(bmap, progress);
352         return bmap;
353 }
354
355 /* Elimininate divs based on inequalities
356  */
357 static struct isl_basic_map *eliminate_divs_ineq(
358                 struct isl_basic_map *bmap, int *progress)
359 {
360         int d;
361         int i;
362         unsigned off;
363         struct isl_ctx *ctx;
364
365         if (!bmap)
366                 return NULL;
367
368         ctx = bmap->ctx;
369         off = 1 + isl_dim_total(bmap->dim);
370
371         for (d = bmap->n_div - 1; d >= 0 ; --d) {
372                 for (i = 0; i < bmap->n_eq; ++i)
373                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
374                                 break;
375                 if (i < bmap->n_eq)
376                         continue;
377                 for (i = 0; i < bmap->n_ineq; ++i)
378                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
379                                 break;
380                 if (i < bmap->n_ineq)
381                         continue;
382                 *progress = 1;
383                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
384                 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
385                         break;
386                 bmap = isl_basic_map_drop_div(bmap, d);
387                 if (!bmap)
388                         break;
389         }
390         return bmap;
391 }
392
393 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
394         unsigned pos, isl_int *eq, int *progress)
395 {
396         unsigned total;
397         int k;
398         int contains_divs;
399
400         total = isl_basic_map_total_dim(bmap);
401         contains_divs =
402                 isl_seq_first_non_zero(eq + 1 + isl_dim_total(bmap->dim),
403                                                 bmap->n_div) != -1;
404         for (k = 0; k < bmap->n_eq; ++k) {
405                 if (bmap->eq[k] == eq)
406                         continue;
407                 if (isl_int_is_zero(bmap->eq[k][1+pos]))
408                         continue;
409                 if (progress)
410                         *progress = 1;
411                 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
412         }
413
414         for (k = 0; k < bmap->n_ineq; ++k) {
415                 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
416                         continue;
417                 if (progress)
418                         *progress = 1;
419                 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
420                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
421         }
422
423         for (k = 0; k < bmap->n_div; ++k) {
424                 if (isl_int_is_zero(bmap->div[k][0]))
425                         continue;
426                 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
427                         continue;
428                 if (progress)
429                         *progress = 1;
430                 /* We need to be careful about circular definitions,
431                  * so for now we just remove the definition of div k
432                  * if the equality contains any divs.
433                  */
434                 if (contains_divs)
435                         isl_seq_clr(bmap->div[k], 1 + total);
436                 else
437                         isl_seq_elim(bmap->div[k]+1, eq,
438                                         1+pos, 1+total, &bmap->div[k][0]);
439                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
440         }
441 }
442
443 struct isl_basic_map *isl_basic_map_gauss(
444         struct isl_basic_map *bmap, int *progress)
445 {
446         int k;
447         int done;
448         int last_var;
449         unsigned total_var;
450         unsigned total;
451
452         if (!bmap)
453                 return NULL;
454
455         total = isl_basic_map_total_dim(bmap);
456         total_var = total - bmap->n_div;
457
458         last_var = total - 1;
459         for (done = 0; done < bmap->n_eq; ++done) {
460                 for (; last_var >= 0; --last_var) {
461                         for (k = done; k < bmap->n_eq; ++k)
462                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
463                                         break;
464                         if (k < bmap->n_eq)
465                                 break;
466                 }
467                 if (last_var < 0)
468                         break;
469                 if (k != done)
470                         swap_equality(bmap, k, done);
471                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
472                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
473
474                 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
475                                                 progress);
476
477                 if (last_var >= total_var &&
478                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
479                         unsigned div = last_var - total_var;
480                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
481                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
482                         isl_int_set(bmap->div[div][0],
483                                     bmap->eq[done][1+last_var]);
484                         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
485                 }
486         }
487         if (done == bmap->n_eq)
488                 return bmap;
489         for (k = done; k < bmap->n_eq; ++k) {
490                 if (isl_int_is_zero(bmap->eq[k][0]))
491                         continue;
492                 return isl_basic_map_set_to_empty(bmap);
493         }
494         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
495         return bmap;
496 }
497
498 struct isl_basic_set *isl_basic_set_gauss(
499         struct isl_basic_set *bset, int *progress)
500 {
501         return (struct isl_basic_set*)isl_basic_map_gauss(
502                         (struct isl_basic_map *)bset, progress);
503 }
504
505
506 static unsigned int round_up(unsigned int v)
507 {
508         int old_v = v;
509
510         while (v) {
511                 old_v = v;
512                 v ^= v & -v;
513         }
514         return old_v << 1;
515 }
516
517 static int hash_index(isl_int ***index, unsigned int size, int bits,
518                         struct isl_basic_map *bmap, int k)
519 {
520         int h;
521         unsigned total = isl_basic_map_total_dim(bmap);
522         uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
523         for (h = hash; index[h]; h = (h+1) % size)
524                 if (&bmap->ineq[k] != index[h] &&
525                     isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
526                         break;
527         return h;
528 }
529
530 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
531                           struct isl_basic_set *bset, int k)
532 {
533         return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
534 }
535
536 static struct isl_basic_map *remove_duplicate_divs(
537         struct isl_basic_map *bmap, int *progress)
538 {
539         unsigned int size;
540         int *index;
541         int k, l, h;
542         int bits;
543         struct isl_blk eq;
544         unsigned total_var = isl_dim_total(bmap->dim);
545         unsigned total = total_var + bmap->n_div;
546         struct isl_ctx *ctx;
547
548         if (bmap->n_div <= 1)
549                 return bmap;
550
551         ctx = bmap->ctx;
552         for (k = bmap->n_div - 1; k >= 0; --k)
553                 if (!isl_int_is_zero(bmap->div[k][0]))
554                         break;
555         if (k <= 0)
556                 return bmap;
557
558         size = round_up(4 * bmap->n_div / 3 - 1);
559         bits = ffs(size) - 1;
560         index = isl_calloc_array(ctx, int, size);
561         if (!index)
562                 return bmap;
563         eq = isl_blk_alloc(ctx, 1+total);
564         if (isl_blk_is_error(eq))
565                 goto out;
566
567         isl_seq_clr(eq.data, 1+total);
568         index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
569         for (--k; k >= 0; --k) {
570                 uint32_t hash;
571
572                 if (isl_int_is_zero(bmap->div[k][0]))
573                         continue;
574
575                 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
576                 for (h = hash; index[h]; h = (h+1) % size)
577                         if (isl_seq_eq(bmap->div[k],
578                                        bmap->div[index[h]-1], 2+total))
579                                 break;
580                 if (index[h]) {
581                         *progress = 1;
582                         l = index[h] - 1;
583                         isl_int_set_si(eq.data[1+total_var+k], -1);
584                         isl_int_set_si(eq.data[1+total_var+l], 1);
585                         eliminate_div(bmap, eq.data, l);
586                         isl_int_set_si(eq.data[1+total_var+k], 0);
587                         isl_int_set_si(eq.data[1+total_var+l], 0);
588                 }
589                 index[h] = k+1;
590         }
591
592         isl_blk_free(ctx, eq);
593 out:
594         free(index);
595         return bmap;
596 }
597
598 /* Normalize divs that appear in equalities.
599  *
600  * In particular, we assume that bmap contains some equalities
601  * of the form
602  *
603  *      a x = m * e_i
604  *
605  * and we want to replace the set of e_i by a minimal set and
606  * such that the new e_i have a canonical representation in terms
607  * of the vector x.
608  * If any of the equalities involves more than one divs, then
609  * we currently simply bail out.
610  *
611  * Let us first additionally assume that all equalities involve
612  * a div.  The equalities then express modulo constraints on the
613  * remaining variables and we can use "parameter compression"
614  * to find a minimal set of constraints.  The result is a transformation
615  *
616  *      x = T(x') = x_0 + G x'
617  *
618  * with G a lower-triangular matrix with all elements below the diagonal
619  * non-negative and smaller than the diagonal element on the same row.
620  * We first normalize x_0 by making the same property hold in the affine
621  * T matrix.
622  * The rows i of G with a 1 on the diagonal do not impose any modulo
623  * constraint and simply express x_i = x'_i.
624  * For each of the remaining rows i, we introduce a div and a corresponding
625  * equality.  In particular
626  *
627  *      g_ii e_j = x_i - g_i(x')
628  *
629  * where each x'_k is replaced either by x_k (if g_kk = 1) or the
630  * corresponding div (if g_kk != 1).
631  *
632  * If there are any equalities not involving any div, then we
633  * first apply a variable compression on the variables x:
634  *
635  *      x = C x''       x'' = C_2 x
636  *
637  * and perform the above parameter compression on A C instead of on A.
638  * The resulting compression is then of the form
639  *
640  *      x'' = T(x') = x_0 + G x'
641  *
642  * and in constructing the new divs and the corresponding equalities,
643  * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
644  * by the corresponding row from C_2.
645  */
646 static struct isl_basic_map *normalize_divs(
647         struct isl_basic_map *bmap, int *progress)
648 {
649         int i, j, k;
650         int total;
651         int div_eq;
652         struct isl_mat *B;
653         struct isl_vec *d;
654         struct isl_mat *T = NULL;
655         struct isl_mat *C = NULL;
656         struct isl_mat *C2 = NULL;
657         isl_int v;
658         int *pos;
659         int dropped, needed;
660
661         if (!bmap)
662                 return NULL;
663
664         if (bmap->n_div == 0)
665                 return bmap;
666
667         if (bmap->n_eq == 0)
668                 return bmap;
669
670         if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
671                 return bmap;
672
673         total = isl_dim_total(bmap->dim);
674         for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
675                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
676                         --j;
677                 if (j < 0)
678                         break;
679                 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
680                         goto done;
681         }
682         div_eq = i;
683         if (div_eq == 0)
684                 return bmap;
685
686         if (div_eq < bmap->n_eq) {
687                 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
688                                         bmap->n_eq - div_eq, 0, 1 + total);
689                 C = isl_mat_variable_compression(bmap->ctx, B, &C2);
690                 if (!C || !C2)
691                         goto error;
692                 if (C->n_col == 0) {
693                         bmap = isl_basic_map_set_to_empty(bmap);
694                         isl_mat_free(bmap->ctx, C);
695                         isl_mat_free(bmap->ctx, C2);
696                         goto done;
697                 }
698         }
699
700         d = isl_vec_alloc(bmap->ctx, div_eq);
701         if (!d)
702                 goto error;
703         for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
704                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
705                         --j;
706                 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
707         }
708         B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
709
710         if (C) {
711                 B = isl_mat_product(bmap->ctx, B, C);
712                 C = NULL;
713         }
714
715         T = isl_mat_parameter_compression(bmap->ctx, B, d);
716         if (!T)
717                 goto error;
718         if (T->n_col == 0) {
719                 bmap = isl_basic_map_set_to_empty(bmap);
720                 isl_mat_free(bmap->ctx, C2);
721                 isl_mat_free(bmap->ctx, T);
722                 goto done;
723         }
724         isl_int_init(v);
725         for (i = 0; i < T->n_row - 1; ++i) {
726                 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
727                 if (isl_int_is_zero(v))
728                         continue;
729                 isl_mat_col_submul(T, 0, v, 1 + i);
730         }
731         isl_int_clear(v);
732         pos = isl_alloc_array(bmap->ctx, int, T->n_row);
733         /* We have to be careful because dropping equalities may reorder them */
734         dropped = 0;
735         for (j = bmap->n_div - 1; j >= 0; --j) {
736                 for (i = 0; i < bmap->n_eq; ++i)
737                         if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
738                                 break;
739                 if (i < bmap->n_eq) {
740                         bmap = isl_basic_map_drop_div(bmap, j);
741                         isl_basic_map_drop_equality(bmap, i);
742                         ++dropped;
743                 }
744         }
745         pos[0] = 0;
746         needed = 0;
747         for (i = 1; i < T->n_row; ++i) {
748                 if (isl_int_is_one(T->row[i][i]))
749                         pos[i] = i;
750                 else
751                         needed++;
752         }
753         if (needed > dropped) {
754                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
755                                 needed, needed, 0);
756                 if (!bmap)
757                         goto error;
758         }
759         for (i = 1; i < T->n_row; ++i) {
760                 if (isl_int_is_one(T->row[i][i]))
761                         continue;
762                 k = isl_basic_map_alloc_div(bmap);
763                 pos[i] = 1 + total + k;
764                 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
765                 isl_int_set(bmap->div[k][0], T->row[i][i]);
766                 if (C2)
767                         isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
768                 else
769                         isl_int_set_si(bmap->div[k][1 + i], 1);
770                 for (j = 0; j < i; ++j) {
771                         if (isl_int_is_zero(T->row[i][j]))
772                                 continue;
773                         if (pos[j] < T->n_row && C2)
774                                 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
775                                                 C2->row[pos[j]], 1 + total);
776                         else
777                                 isl_int_neg(bmap->div[k][1 + pos[j]],
778                                                                 T->row[i][j]);
779                 }
780                 j = isl_basic_map_alloc_equality(bmap);
781                 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
782                 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
783         }
784         free(pos);
785         isl_mat_free(bmap->ctx, C2);
786         isl_mat_free(bmap->ctx, T);
787
788         *progress = 1;
789 done:
790         F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
791
792         return bmap;
793 error:
794         isl_mat_free(bmap->ctx, C);
795         isl_mat_free(bmap->ctx, C2);
796         isl_mat_free(bmap->ctx, T);
797         return bmap;
798 }
799
800 static struct isl_basic_map *remove_duplicate_constraints(
801         struct isl_basic_map *bmap, int *progress)
802 {
803         unsigned int size;
804         isl_int ***index;
805         int k, l, h;
806         int bits;
807         unsigned total = isl_basic_map_total_dim(bmap);
808         isl_int sum;
809
810         if (bmap->n_ineq <= 1)
811                 return bmap;
812
813         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
814         bits = ffs(size) - 1;
815         index = isl_calloc_array(ctx, isl_int **, size);
816         if (!index)
817                 return bmap;
818
819         index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
820         for (k = 1; k < bmap->n_ineq; ++k) {
821                 h = hash_index(index, size, bits, bmap, k);
822                 if (!index[h]) {
823                         index[h] = &bmap->ineq[k];
824                         continue;
825                 }
826                 if (progress)
827                         *progress = 1;
828                 l = index[h] - &bmap->ineq[0];
829                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
830                         swap_inequality(bmap, k, l);
831                 isl_basic_map_drop_inequality(bmap, k);
832                 --k;
833         }
834         isl_int_init(sum);
835         for (k = 0; k < bmap->n_ineq-1; ++k) {
836                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
837                 h = hash_index(index, size, bits, bmap, k);
838                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
839                 if (!index[h])
840                         continue;
841                 l = index[h] - &bmap->ineq[0];
842                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
843                 if (isl_int_is_pos(sum))
844                         continue;
845                 if (isl_int_is_zero(sum)) {
846                         /* We need to break out of the loop after these
847                          * changes since the contents of the hash
848                          * will no longer be valid.
849                          * Plus, we probably we want to regauss first.
850                          */
851                         isl_basic_map_drop_inequality(bmap, l);
852                         isl_basic_map_inequality_to_equality(bmap, k);
853                 } else
854                         bmap = isl_basic_map_set_to_empty(bmap);
855                 break;
856         }
857         isl_int_clear(sum);
858
859         free(index);
860         return bmap;
861 }
862
863
864 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
865 {
866         int progress = 1;
867         if (!bmap)
868                 return NULL;
869         while (progress) {
870                 progress = 0;
871                 bmap = normalize_constraints(bmap);
872                 bmap = eliminate_divs_eq(bmap, &progress);
873                 bmap = eliminate_divs_ineq(bmap, &progress);
874                 bmap = isl_basic_map_gauss(bmap, &progress);
875                 /* requires equalities in normal form */
876                 bmap = normalize_divs(bmap, &progress);
877                 bmap = remove_duplicate_divs(bmap, &progress);
878                 bmap = remove_duplicate_constraints(bmap, &progress);
879         }
880         return bmap;
881 }
882
883 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
884 {
885         return (struct isl_basic_set *)
886                 isl_basic_map_simplify((struct isl_basic_map *)bset);
887 }
888
889
890 /* If the only constraints a div d=floor(f/m)
891  * appears in are its two defining constraints
892  *
893  *      f - m d >=0
894  *      -(f - (m - 1)) + m d >= 0
895  *
896  * then it can safely be removed.
897  */
898 static int div_is_redundant(struct isl_basic_map *bmap, int div)
899 {
900         int i;
901         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
902
903         for (i = 0; i < bmap->n_eq; ++i)
904                 if (!isl_int_is_zero(bmap->eq[i][pos]))
905                         return 0;
906
907         for (i = 0; i < bmap->n_ineq; ++i) {
908                 if (isl_int_is_zero(bmap->ineq[i][pos]))
909                         continue;
910                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
911                         int neg;
912                         isl_int_sub(bmap->div[div][1],
913                                         bmap->div[div][1], bmap->div[div][0]);
914                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
915                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
916                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
917                         isl_int_add(bmap->div[div][1],
918                                         bmap->div[div][1], bmap->div[div][0]);
919                         if (!neg)
920                                 return 0;
921                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
922                                                     bmap->n_div-div-1) != -1)
923                                 return 0;
924                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
925                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
926                                 return 0;
927                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
928                                                     bmap->n_div-div-1) != -1)
929                                 return 0;
930                 } else
931                         return 0;
932         }
933
934         for (i = 0; i < bmap->n_div; ++i)
935                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
936                         return 0;
937
938         return 1;
939 }
940
941 /*
942  * Remove divs that don't occur in any of the constraints or other divs.
943  * These can arise when dropping some of the variables in a quast
944  * returned by piplib.
945  */
946 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
947 {
948         int i;
949
950         if (!bmap)
951                 return NULL;
952
953         for (i = bmap->n_div-1; i >= 0; --i) {
954                 if (!div_is_redundant(bmap, i))
955                         continue;
956                 bmap = isl_basic_map_drop_div(bmap, i);
957         }
958         return bmap;
959 }
960
961 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
962 {
963         bmap = remove_redundant_divs(bmap);
964         if (!bmap)
965                 return NULL;
966         F_SET(bmap, ISL_BASIC_SET_FINAL);
967         return bmap;
968 }
969
970 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
971 {
972         return (struct isl_basic_set *)
973                 isl_basic_map_finalize((struct isl_basic_map *)bset);
974 }
975
976 struct isl_set *isl_set_finalize(struct isl_set *set)
977 {
978         int i;
979
980         if (!set)
981                 return NULL;
982         for (i = 0; i < set->n; ++i) {
983                 set->p[i] = isl_basic_set_finalize(set->p[i]);
984                 if (!set->p[i])
985                         goto error;
986         }
987         return set;
988 error:
989         isl_set_free(set);
990         return NULL;
991 }
992
993 struct isl_map *isl_map_finalize(struct isl_map *map)
994 {
995         int i;
996
997         if (!map)
998                 return NULL;
999         for (i = 0; i < map->n; ++i) {
1000                 map->p[i] = isl_basic_map_finalize(map->p[i]);
1001                 if (!map->p[i])
1002                         goto error;
1003         }
1004         F_CLR(map, ISL_MAP_NORMALIZED);
1005         return map;
1006 error:
1007         isl_map_free(map);
1008         return NULL;
1009 }
1010
1011
1012 /* Remove any div that is defined in terms of the given variable.
1013  */
1014 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1015                                                                         int pos)
1016 {
1017         int i;
1018         unsigned dim = isl_dim_total(bmap->dim);
1019
1020         for (i = 0; i < bmap->n_div; ++i) {
1021                 if (isl_int_is_zero(bmap->div[i][0]))
1022                         continue;
1023                 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1024                         continue;
1025                 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
1026                 if (!bmap)
1027                         return NULL;
1028         }
1029         return bmap;
1030 }
1031
1032 /* Eliminate the specified variables from the constraints using
1033  * Fourier-Motzkin.  The variables themselves are not removed.
1034  */
1035 struct isl_basic_map *isl_basic_map_eliminate_vars(
1036         struct isl_basic_map *bmap, unsigned pos, unsigned n)
1037 {
1038         int d;
1039         int i, j, k;
1040         unsigned total;
1041
1042         if (n == 0)
1043                 return bmap;
1044         if (!bmap)
1045                 return NULL;
1046         total = isl_basic_map_total_dim(bmap);
1047
1048         bmap = isl_basic_map_cow(bmap);
1049         for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1050                 int n_lower, n_upper;
1051                 bmap = remove_dependent_vars(bmap, d);
1052                 if (!bmap)
1053                         return NULL;
1054                 if (d >= total - bmap->n_div)
1055                         isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1056                 for (i = 0; i < bmap->n_eq; ++i) {
1057                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1058                                 continue;
1059                         eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1060                         isl_basic_map_drop_equality(bmap, i);
1061                         break;
1062                 }
1063                 if (i < bmap->n_eq)
1064                         continue;
1065                 n_lower = 0;
1066                 n_upper = 0;
1067                 for (i = 0; i < bmap->n_ineq; ++i) {
1068                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1069                                 n_lower++;
1070                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1071                                 n_upper++;
1072                 }
1073                 bmap = isl_basic_map_extend_constraints(bmap,
1074                                 0, n_lower * n_upper);
1075                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1076                         int last;
1077                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1078                                 continue;
1079                         last = -1;
1080                         for (j = 0; j < i; ++j) {
1081                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1082                                         continue;
1083                                 last = j;
1084                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1085                                     isl_int_sgn(bmap->ineq[j][1+d]))
1086                                         continue;
1087                                 k = isl_basic_map_alloc_inequality(bmap);
1088                                 if (k < 0)
1089                                         goto error;
1090                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1091                                                 1+total);
1092                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1093                                                 1+d, 1+total, NULL);
1094                         }
1095                         isl_basic_map_drop_inequality(bmap, i);
1096                         i = last + 1;
1097                 }
1098                 if (n_lower > 0 && n_upper > 0) {
1099                         bmap = normalize_constraints(bmap);
1100                         bmap = remove_duplicate_constraints(bmap, NULL);
1101                         bmap = isl_basic_map_gauss(bmap, NULL);
1102                         bmap = isl_basic_map_convex_hull(bmap);
1103                         if (!bmap)
1104                                 goto error;
1105                         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1106                                 break;
1107                 }
1108         }
1109         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1110         return bmap;
1111 error:
1112         isl_basic_map_free(bmap);
1113         return NULL;
1114 }
1115
1116 struct isl_basic_set *isl_basic_set_eliminate_vars(
1117         struct isl_basic_set *bset, unsigned pos, unsigned n)
1118 {
1119         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1120                         (struct isl_basic_map *)bset, pos, n);
1121 }
1122
1123 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1124 {
1125         int d, i;
1126         unsigned total;
1127
1128         total = isl_dim_total(bmap->dim);
1129         for (d = 0; d < total; ++d)
1130                 elim[d] = -1;
1131         for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1132                 for (; d >= 0; --d) {
1133                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1134                                 continue;
1135                         elim[d] = i;
1136                         break;
1137                 }
1138         }
1139 }
1140
1141 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1142 {
1143         return compute_elimination_index((struct isl_basic_map *)bset, elim);
1144 }
1145
1146 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1147         struct isl_basic_map *bmap, int *elim)
1148 {
1149         int d, i;
1150         int copied = 0;
1151         unsigned total;
1152
1153         total = isl_dim_total(bmap->dim);
1154         for (d = total - 1; d >= 0; --d) {
1155                 if (isl_int_is_zero(src[1+d]))
1156                         continue;
1157                 if (elim[d] == -1)
1158                         continue;
1159                 if (!copied) {
1160                         isl_seq_cpy(dst, src, 1 + total);
1161                         copied = 1;
1162                 }
1163                 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1164         }
1165         return copied;
1166 }
1167
1168 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1169         struct isl_basic_set *bset, int *elim)
1170 {
1171         return reduced_using_equalities(dst, src,
1172                                         (struct isl_basic_map *)bset, elim);
1173 }
1174
1175 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1176         struct isl_basic_set *bset, struct isl_basic_set *context)
1177 {
1178         int i;
1179         int *elim;
1180
1181         if (!bset || !context)
1182                 goto error;
1183
1184         bset = isl_basic_set_cow(bset);
1185         if (!bset)
1186                 goto error;
1187
1188         elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1189         if (!elim)
1190                 goto error;
1191         set_compute_elimination_index(context, elim);
1192         for (i = 0; i < bset->n_eq; ++i)
1193                 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1194                                                         context, elim);
1195         for (i = 0; i < bset->n_ineq; ++i)
1196                 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1197                                                         context, elim);
1198         isl_basic_set_free(context);
1199         free(elim);
1200         bset = isl_basic_set_simplify(bset);
1201         bset = isl_basic_set_finalize(bset);
1202         return bset;
1203 error:
1204         isl_basic_set_free(bset);
1205         isl_basic_set_free(context);
1206         return NULL;
1207 }
1208
1209 static struct isl_basic_set *remove_shifted_constraints(
1210         struct isl_basic_set *bset, struct isl_basic_set *context)
1211 {
1212         unsigned int size;
1213         isl_int ***index;
1214         int bits;
1215         int k, h, l;
1216
1217         if (!bset)
1218                 return NULL;
1219
1220         size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1221         bits = ffs(size) - 1;
1222         index = isl_calloc_array(ctx, isl_int **, size);
1223         if (!index)
1224                 return bset;
1225
1226         for (k = 0; k < context->n_ineq; ++k) {
1227                 h = set_hash_index(index, size, bits, context, k);
1228                 index[h] = &context->ineq[k];
1229         }
1230         for (k = 0; k < bset->n_ineq; ++k) {
1231                 h = set_hash_index(index, size, bits, bset, k);
1232                 if (!index[h])
1233                         continue;
1234                 l = index[h] - &context->ineq[0];
1235                 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1236                         continue;
1237                 bset = isl_basic_set_cow(bset);
1238                 if (!bset)
1239                         goto error;
1240                 isl_basic_set_drop_inequality(bset, k);
1241                 --k;
1242         }
1243         free(index);
1244         return bset;
1245 error:
1246         free(index);
1247         return bset;
1248 }
1249
1250 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1251         struct isl_basic_set *context);
1252
1253 static struct isl_basic_set *uset_gist_context_eq(struct isl_basic_set *bset,
1254         struct isl_basic_set *context)
1255 {
1256         struct isl_mat *T;
1257         struct isl_mat *T2;
1258         struct isl_ctx *ctx = context->ctx;
1259         struct isl_basic_set *reduced_context;
1260         reduced_context = isl_basic_set_remove_equalities(
1261                                 isl_basic_set_copy(context), &T, &T2);
1262         if (!reduced_context)
1263                 goto error;
1264         bset = isl_basic_set_preimage(ctx, bset, T);
1265         bset = uset_gist(bset, reduced_context);
1266         bset = isl_basic_set_preimage(ctx, bset, T2);
1267         bset = isl_basic_set_reduce_using_equalities(bset, context);
1268         return bset;
1269 error:
1270         isl_basic_set_free(context);
1271         isl_basic_set_free(bset);
1272         return NULL;
1273 }
1274
1275 static struct isl_basic_set *uset_gist_set_eq(struct isl_basic_set *bset,
1276         struct isl_basic_set *context)
1277 {
1278         struct isl_mat *T;
1279         struct isl_mat *T2;
1280         struct isl_ctx *ctx = context->ctx;
1281         struct isl_basic_set *affine_hull = NULL;
1282
1283         affine_hull = isl_basic_set_copy(bset);
1284         affine_hull = isl_basic_set_cow(affine_hull);
1285         if (!affine_hull)
1286                 goto error;
1287         isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1288
1289         bset = isl_basic_set_remove_equalities(bset, &T, &T2);
1290         if (!bset)
1291                 goto error;
1292         context = isl_basic_set_preimage(ctx, context, T);
1293         bset = uset_gist(bset, context);
1294         bset = isl_basic_set_preimage(ctx, bset, T2);
1295         bset = isl_basic_set_intersect(bset, affine_hull);
1296         return bset;
1297 error:
1298         isl_basic_set_free(affine_hull);
1299         isl_basic_set_free(context);
1300         isl_basic_set_free(bset);
1301         return NULL;
1302 }
1303
1304 /* Remove all information from bset that is redundant in the context
1305  * of context.  In particular, equalities that are linear combinations
1306  * of those in context are removed.  Then the inequalities that are
1307  * redundant in the context of the equalities and inequalities of
1308  * context are removed.
1309  */
1310 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1311         struct isl_basic_set *context)
1312 {
1313         int i;
1314         isl_int opt;
1315         struct isl_basic_set *combined;
1316         struct isl_ctx *ctx;
1317
1318         if (!bset || !context)
1319                 goto error;
1320
1321         if (context->n_eq > 0)
1322                 return uset_gist_context_eq(bset, context);
1323         if (!context->n_ineq)
1324                 goto done;
1325         if (bset->n_eq > 0)
1326                 return uset_gist_set_eq(bset, context);
1327         bset = remove_shifted_constraints(bset, context);
1328         combined = isl_basic_set_extend_constraints(isl_basic_set_copy(bset),
1329                         context->n_eq, context->n_ineq);
1330         context = isl_basic_set_add_constraints(combined, context, 0);
1331         if (!context)
1332                 goto error;
1333         ctx = context->ctx;
1334         isl_int_init(opt);
1335         for (i = bset->n_ineq-1; i >= 0; --i) {
1336                 int redundant;
1337                 set_swap_inequality(context, i, context->n_ineq-1);
1338                 context->n_ineq--;
1339                 redundant = isl_basic_set_constraint_is_redundant(&context,
1340                                 context->ineq[context->n_ineq], &opt, NULL);
1341                 if (redundant == -1) {
1342                         isl_int_clear(opt);
1343                         goto error;
1344                 }
1345                 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
1346                         bset = isl_basic_set_set_to_empty(bset);
1347                         break;
1348                 }
1349                 context->n_ineq++;
1350                 set_swap_inequality(context, i, context->n_ineq-1);
1351                 if (redundant) {
1352                         isl_basic_set_drop_inequality(context, i);
1353                         isl_basic_set_drop_inequality(bset, i);
1354                 }
1355         }
1356         isl_int_clear(opt);
1357 done:
1358         isl_basic_set_free(context);
1359         return bset;
1360 error:
1361         isl_basic_set_free(context);
1362         isl_basic_set_free(bset);
1363         return NULL;
1364 }
1365
1366 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1367         struct isl_basic_map *context)
1368 {
1369         struct isl_basic_set *bset;
1370
1371         if (!bmap || !context)
1372                 goto error;
1373
1374         context = isl_basic_map_align_divs(context, bmap);
1375         bmap = isl_basic_map_align_divs(bmap, context);
1376
1377         bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1378                          isl_basic_map_underlying_set(context));
1379
1380         return isl_basic_map_overlying_set(bset, bmap);
1381 error:
1382         isl_basic_map_free(bmap);
1383         isl_basic_map_free(context);
1384         return NULL;
1385 }
1386
1387 /*
1388  * Assumes context has no implicit divs.
1389  */
1390 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1391 {
1392         int i;
1393
1394         map = isl_map_cow(map);
1395         if (!map || !context)
1396                 return NULL;
1397         isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1398         map = isl_map_compute_divs(map);
1399         for (i = 0; i < map->n; ++i)
1400                 context = isl_basic_map_align_divs(context, map->p[i]);
1401         for (i = 0; i < map->n; ++i) {
1402                 map->p[i] = isl_basic_map_gist(map->p[i],
1403                                                 isl_basic_map_copy(context));
1404                 if (!map->p[i])
1405                         goto error;
1406         }
1407         isl_basic_map_free(context);
1408         F_CLR(map, ISL_MAP_NORMALIZED);
1409         return map;
1410 error:
1411         isl_map_free(map);
1412         isl_basic_map_free(context);
1413         return NULL;
1414 }
1415
1416 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1417                                                 struct isl_basic_set *context)
1418 {
1419         return (struct isl_basic_set *)isl_basic_map_gist(
1420                 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1421 }
1422
1423 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1424 {
1425         return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1426                                         (struct isl_basic_map *)context);
1427 }
1428
1429 /* Quick check to see if two basic maps are disjoint.
1430  * In particular, we reduce the equalities and inequalities of
1431  * one basic map in the context of the equalities of the other
1432  * basic map and check if we get a contradiction.
1433  */
1434 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1435         struct isl_basic_map *bmap2)
1436 {
1437         struct isl_vec *v = NULL;
1438         int *elim = NULL;
1439         unsigned total;
1440         int d, i;
1441
1442         if (!bmap1 || !bmap2)
1443                 return -1;
1444         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1445                         return -1);
1446         if (bmap1->n_div || bmap2->n_div)
1447                 return 0;
1448         if (!bmap1->n_eq && !bmap2->n_eq)
1449                 return 0;
1450
1451         total = isl_dim_total(bmap1->dim);
1452         if (total == 0)
1453                 return 0;
1454         v = isl_vec_alloc(bmap1->ctx, 1 + total);
1455         if (!v)
1456                 goto error;
1457         elim = isl_alloc_array(bmap1->ctx, int, total);
1458         if (!elim)
1459                 goto error;
1460         compute_elimination_index(bmap1, elim);
1461         for (i = 0; i < bmap2->n_eq; ++i) {
1462                 int reduced;
1463                 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1464                                                         bmap1, elim);
1465                 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1466                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1467                         goto disjoint;
1468         }
1469         for (i = 0; i < bmap2->n_ineq; ++i) {
1470                 int reduced;
1471                 reduced = reduced_using_equalities(v->block.data,
1472                                                 bmap2->ineq[i], bmap1, elim);
1473                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1474                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1475                         goto disjoint;
1476         }
1477         compute_elimination_index(bmap2, elim);
1478         for (i = 0; i < bmap1->n_ineq; ++i) {
1479                 int reduced;
1480                 reduced = reduced_using_equalities(v->block.data,
1481                                                 bmap1->ineq[i], bmap2, elim);
1482                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1483                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1484                         goto disjoint;
1485         }
1486         isl_vec_free(bmap1->ctx, v);
1487         free(elim);
1488         return 0;
1489 disjoint:
1490         isl_vec_free(bmap1->ctx, v);
1491         free(elim);
1492         return 1;
1493 error:
1494         isl_vec_free(bmap1->ctx, v);
1495         free(elim);
1496         return -1;
1497 }
1498
1499 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1500         struct isl_basic_set *bset2)
1501 {
1502         return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1503                                               (struct isl_basic_map *)bset2);
1504 }
1505
1506 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1507 {
1508         int i, j;
1509
1510         if (!map1 || !map2)
1511                 return -1;
1512
1513         if (isl_map_fast_is_equal(map1, map2))
1514                 return 0;
1515
1516         for (i = 0; i < map1->n; ++i) {
1517                 for (j = 0; j < map2->n; ++j) {
1518                         int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1519                                                                map2->p[j]);
1520                         if (d != 1)
1521                                 return d;
1522                 }
1523         }
1524         return 1;
1525 }
1526
1527 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1528 {
1529         return isl_map_fast_is_disjoint((struct isl_map *)set1,
1530                                         (struct isl_map *)set2);
1531 }