d8c9dab2737a9720f274255c36dce1ba8e0227bc
[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
399         total = isl_basic_map_total_dim(bmap);
400         for (k = 0; k < bmap->n_eq; ++k) {
401                 if (bmap->eq[k] == eq)
402                         continue;
403                 if (isl_int_is_zero(bmap->eq[k][1+pos]))
404                         continue;
405                 if (progress)
406                         *progress = 1;
407                 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
408         }
409
410         for (k = 0; k < bmap->n_ineq; ++k) {
411                 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
412                         continue;
413                 if (progress)
414                         *progress = 1;
415                 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
416                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
417         }
418
419         for (k = 0; k < bmap->n_div; ++k) {
420                 if (isl_int_is_zero(bmap->div[k][0]))
421                         continue;
422                 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
423                         continue;
424                 if (progress)
425                         *progress = 1;
426                 isl_seq_elim(bmap->div[k]+1, eq,
427                                 1+pos, 1+total, &bmap->div[k][0]);
428                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
429         }
430 }
431
432 struct isl_basic_map *isl_basic_map_gauss(
433         struct isl_basic_map *bmap, int *progress)
434 {
435         int k;
436         int done;
437         int last_var;
438         unsigned total_var;
439         unsigned total;
440
441         if (!bmap)
442                 return NULL;
443
444         total = isl_basic_map_total_dim(bmap);
445         total_var = total - bmap->n_div;
446
447         last_var = total - 1;
448         for (done = 0; done < bmap->n_eq; ++done) {
449                 for (; last_var >= 0; --last_var) {
450                         for (k = done; k < bmap->n_eq; ++k)
451                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
452                                         break;
453                         if (k < bmap->n_eq)
454                                 break;
455                 }
456                 if (last_var < 0)
457                         break;
458                 if (k != done)
459                         swap_equality(bmap, k, done);
460                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
461                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
462
463                 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
464                                                 progress);
465
466                 if (last_var >= total_var &&
467                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
468                         unsigned div = last_var - total_var;
469                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
470                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
471                         isl_int_set(bmap->div[div][0],
472                                     bmap->eq[done][1+last_var]);
473                         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
474                 }
475         }
476         if (done == bmap->n_eq)
477                 return bmap;
478         for (k = done; k < bmap->n_eq; ++k) {
479                 if (isl_int_is_zero(bmap->eq[k][0]))
480                         continue;
481                 return isl_basic_map_set_to_empty(bmap);
482         }
483         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
484         return bmap;
485 }
486
487 struct isl_basic_set *isl_basic_set_gauss(
488         struct isl_basic_set *bset, int *progress)
489 {
490         return (struct isl_basic_set*)isl_basic_map_gauss(
491                         (struct isl_basic_map *)bset, progress);
492 }
493
494
495 static unsigned int round_up(unsigned int v)
496 {
497         int old_v = v;
498
499         while (v) {
500                 old_v = v;
501                 v ^= v & -v;
502         }
503         return old_v << 1;
504 }
505
506 static int hash_index(isl_int ***index, unsigned int size, int bits,
507                         struct isl_basic_map *bmap, int k)
508 {
509         int h;
510         unsigned total = isl_basic_map_total_dim(bmap);
511         uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
512         for (h = hash; index[h]; h = (h+1) % size)
513                 if (&bmap->ineq[k] != index[h] &&
514                     isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
515                         break;
516         return h;
517 }
518
519 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
520                           struct isl_basic_set *bset, int k)
521 {
522         return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
523 }
524
525 static struct isl_basic_map *remove_duplicate_divs(
526         struct isl_basic_map *bmap, int *progress)
527 {
528         unsigned int size;
529         int *index;
530         int k, l, h;
531         int bits;
532         struct isl_blk eq;
533         unsigned total_var = isl_dim_total(bmap->dim);
534         unsigned total = total_var + bmap->n_div;
535         struct isl_ctx *ctx;
536
537         if (bmap->n_div <= 1)
538                 return bmap;
539
540         ctx = bmap->ctx;
541         for (k = bmap->n_div - 1; k >= 0; --k)
542                 if (!isl_int_is_zero(bmap->div[k][0]))
543                         break;
544         if (k <= 0)
545                 return bmap;
546
547         size = round_up(4 * bmap->n_div / 3 - 1);
548         bits = ffs(size) - 1;
549         index = isl_calloc_array(ctx, int, size);
550         if (!index)
551                 return bmap;
552         eq = isl_blk_alloc(ctx, 1+total);
553         if (isl_blk_is_error(eq))
554                 goto out;
555
556         isl_seq_clr(eq.data, 1+total);
557         index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
558         for (--k; k >= 0; --k) {
559                 uint32_t hash;
560
561                 if (isl_int_is_zero(bmap->div[k][0]))
562                         continue;
563
564                 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
565                 for (h = hash; index[h]; h = (h+1) % size)
566                         if (isl_seq_eq(bmap->div[k],
567                                        bmap->div[index[h]-1], 2+total))
568                                 break;
569                 if (index[h]) {
570                         *progress = 1;
571                         l = index[h] - 1;
572                         isl_int_set_si(eq.data[1+total_var+k], -1);
573                         isl_int_set_si(eq.data[1+total_var+l], 1);
574                         eliminate_div(bmap, eq.data, l);
575                         isl_int_set_si(eq.data[1+total_var+k], 0);
576                         isl_int_set_si(eq.data[1+total_var+l], 0);
577                 }
578                 index[h] = k+1;
579         }
580
581         isl_blk_free(ctx, eq);
582 out:
583         free(index);
584         return bmap;
585 }
586
587 /* Normalize divs that appear in equalities.
588  *
589  * In particular, we assume that bmap contains some equalities
590  * of the form
591  *
592  *      a x = m * e_i
593  *
594  * and we want to replace the set of e_i by a minimal set and
595  * such that the new e_i have a canonical representation in terms
596  * of the vector x.
597  * If any of the equalities involves more than one divs, then
598  * we currently simply bail out.
599  *
600  * Let us first additionally assume that all equalities involve
601  * a div.  The equalities then express modulo constraints on the
602  * remaining variables and we can use "parameter compression"
603  * to find a minimal set of constraints.  The result is a transformation
604  *
605  *      x = T(x') = x_0 + G x'
606  *
607  * with G a lower-triangular matrix with all elements below the diagonal
608  * non-negative and smaller than the diagonal element on the same row.
609  * We first normalize x_0 by making the same property hold in the affine
610  * T matrix.
611  * The rows i of G with a 1 on the diagonal do not impose any modulo
612  * constraint and simply express x_i = x'_i.
613  * For each of the remaining rows i, we introduce a div and a corresponding
614  * equality.  In particular
615  *
616  *      g_ii e_j = x_i - g_i(x')
617  *
618  * where each x'_k is replaced either by x_k (if g_kk = 1) or the
619  * corresponding div (if g_kk != 1).
620  *
621  * If there are any equalities not involving any div, then we
622  * first apply a variable compression on the variables x:
623  *
624  *      x = C x''       x'' = C_2 x
625  *
626  * and perform the above parameter compression on A C instead of on A.
627  * The resulting compression is then of the form
628  *
629  *      x'' = T(x') = x_0 + G x'
630  *
631  * and in constructing the new divs and the corresponding equalities,
632  * we have to replace each x'' by the corresponding row from C_2.
633  */
634 static struct isl_basic_map *normalize_divs(
635         struct isl_basic_map *bmap, int *progress)
636 {
637         int i, j, k;
638         int total;
639         int div_eq;
640         struct isl_mat *B;
641         struct isl_vec *d;
642         struct isl_mat *T = NULL;
643         struct isl_mat *C = NULL;
644         struct isl_mat *C2 = NULL;
645         isl_int v;
646         int *pos;
647
648         if (!bmap)
649                 return NULL;
650
651         if (bmap->n_div == 0)
652                 return bmap;
653
654         if (bmap->n_eq == 0)
655                 return bmap;
656
657         if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
658                 return bmap;
659
660         total = isl_dim_total(bmap->dim);
661         for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
662                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
663                         --j;
664                 if (j < 0)
665                         break;
666                 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
667                         goto done;
668         }
669         div_eq = i;
670         if (div_eq == 0)
671                 return bmap;
672
673         if (div_eq < bmap->n_eq) {
674                 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
675                                         bmap->n_eq - div_eq, 0, 1 + total);
676                 C = isl_mat_variable_compression(bmap->ctx, B, &C2);
677                 if (!C || !C2)
678                         goto error;
679                 if (C->n_col == 0) {
680                         bmap = isl_basic_map_set_to_empty(bmap);
681                         isl_mat_free(bmap->ctx, C);
682                         isl_mat_free(bmap->ctx, C2);
683                         goto done;
684                 }
685         }
686
687         d = isl_vec_alloc(bmap->ctx, div_eq);
688         if (!d)
689                 goto error;
690         for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
691                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
692                         --j;
693                 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
694         }
695         B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
696
697         if (C) {
698                 B = isl_mat_product(bmap->ctx, B, C);
699                 C = NULL;
700         }
701
702         T = isl_mat_parameter_compression(bmap->ctx, B, d);
703         if (!T)
704                 goto error;
705         if (T->n_col == 0) {
706                 bmap = isl_basic_map_set_to_empty(bmap);
707                 isl_mat_free(bmap->ctx, C2);
708                 isl_mat_free(bmap->ctx, T);
709                 goto done;
710         }
711         isl_int_init(v);
712         for (i = 0; i < T->n_row - 1; ++i) {
713                 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
714                 if (isl_int_is_zero(v))
715                         continue;
716                 isl_mat_col_submul(T, 0, v, 1 + i);
717         }
718         isl_int_clear(v);
719         pos = isl_alloc_array(bmap->ctx, int, T->n_row);
720         /* We have to be careful because dropping equalities may reorder them */
721         for (j = bmap->n_div - 1; j >= 0; --j) {
722                 for (i = 0; i < bmap->n_eq; ++i)
723                         if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
724                                 break;
725                 if (i < bmap->n_eq) {
726                         bmap = isl_basic_map_drop_div(bmap, j);
727                         isl_basic_map_drop_equality(bmap, i);
728                 }
729         }
730         pos[0] = 0;
731         for (i = 1; i < T->n_row; ++i) {
732                 if (isl_int_is_one(T->row[i][i])) {
733                         pos[i] = i;
734                         continue;
735                 }
736                 k = isl_basic_map_alloc_div(bmap);
737                 pos[i] = 1 + total + k;
738                 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
739                 isl_int_set(bmap->div[k][0], T->row[i][i]);
740                 if (C2)
741                         isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
742                 else
743                         isl_int_set_si(bmap->div[k][1 + i], 1);
744                 for (j = 0; j < i; ++j) {
745                         if (isl_int_is_zero(T->row[i][j]))
746                                 continue;
747                         if (C2)
748                                 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
749                                                 C2->row[pos[j]], 1 + total);
750                         else
751                                 isl_int_neg(bmap->div[k][1 + pos[j]],
752                                                                 T->row[i][j]);
753                 }
754                 j = isl_basic_map_alloc_equality(bmap);
755                 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
756                 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
757         }
758         free(pos);
759         isl_mat_free(bmap->ctx, C2);
760         isl_mat_free(bmap->ctx, T);
761
762         *progress = 1;
763 done:
764         F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
765
766         return bmap;
767 error:
768         isl_mat_free(bmap->ctx, C);
769         isl_mat_free(bmap->ctx, C2);
770         isl_mat_free(bmap->ctx, T);
771         return bmap;
772 }
773
774 static struct isl_basic_map *remove_duplicate_constraints(
775         struct isl_basic_map *bmap, int *progress)
776 {
777         unsigned int size;
778         isl_int ***index;
779         int k, l, h;
780         int bits;
781         unsigned total = isl_basic_map_total_dim(bmap);
782         isl_int sum;
783
784         if (bmap->n_ineq <= 1)
785                 return bmap;
786
787         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
788         bits = ffs(size) - 1;
789         index = isl_calloc_array(ctx, isl_int **, size);
790         if (!index)
791                 return bmap;
792
793         index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
794         for (k = 1; k < bmap->n_ineq; ++k) {
795                 h = hash_index(index, size, bits, bmap, k);
796                 if (!index[h]) {
797                         index[h] = &bmap->ineq[k];
798                         continue;
799                 }
800                 if (progress)
801                         *progress = 1;
802                 l = index[h] - &bmap->ineq[0];
803                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
804                         swap_inequality(bmap, k, l);
805                 isl_basic_map_drop_inequality(bmap, k);
806                 --k;
807         }
808         isl_int_init(sum);
809         for (k = 0; k < bmap->n_ineq-1; ++k) {
810                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
811                 h = hash_index(index, size, bits, bmap, k);
812                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
813                 if (!index[h])
814                         continue;
815                 l = index[h] - &bmap->ineq[0];
816                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
817                 if (isl_int_is_pos(sum))
818                         continue;
819                 if (isl_int_is_zero(sum)) {
820                         /* We need to break out of the loop after these
821                          * changes since the contents of the hash
822                          * will no longer be valid.
823                          * Plus, we probably we want to regauss first.
824                          */
825                         isl_basic_map_drop_inequality(bmap, l);
826                         isl_basic_map_inequality_to_equality(bmap, k);
827                 } else
828                         bmap = isl_basic_map_set_to_empty(bmap);
829                 break;
830         }
831         isl_int_clear(sum);
832
833         free(index);
834         return bmap;
835 }
836
837
838 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
839 {
840         int progress = 1;
841         if (!bmap)
842                 return NULL;
843         while (progress) {
844                 progress = 0;
845                 bmap = normalize_constraints(bmap);
846                 bmap = eliminate_divs_eq(bmap, &progress);
847                 bmap = eliminate_divs_ineq(bmap, &progress);
848                 bmap = isl_basic_map_gauss(bmap, &progress);
849                 /* requires equalities in normal form */
850                 bmap = normalize_divs(bmap, &progress);
851                 bmap = remove_duplicate_divs(bmap, &progress);
852                 bmap = remove_duplicate_constraints(bmap, &progress);
853         }
854         return bmap;
855 }
856
857 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
858 {
859         return (struct isl_basic_set *)
860                 isl_basic_map_simplify((struct isl_basic_map *)bset);
861 }
862
863
864 /* If the only constraints a div d=floor(f/m)
865  * appears in are its two defining constraints
866  *
867  *      f - m d >=0
868  *      -(f - (m - 1)) + m d >= 0
869  *
870  * then it can safely be removed.
871  */
872 static int div_is_redundant(struct isl_basic_map *bmap, int div)
873 {
874         int i;
875         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
876
877         for (i = 0; i < bmap->n_eq; ++i)
878                 if (!isl_int_is_zero(bmap->eq[i][pos]))
879                         return 0;
880
881         for (i = 0; i < bmap->n_ineq; ++i) {
882                 if (isl_int_is_zero(bmap->ineq[i][pos]))
883                         continue;
884                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
885                         int neg;
886                         isl_int_sub(bmap->div[div][1],
887                                         bmap->div[div][1], bmap->div[div][0]);
888                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
889                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
890                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
891                         isl_int_add(bmap->div[div][1],
892                                         bmap->div[div][1], bmap->div[div][0]);
893                         if (!neg)
894                                 return 0;
895                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
896                                                     bmap->n_div-div-1) != -1)
897                                 return 0;
898                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
899                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
900                                 return 0;
901                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
902                                                     bmap->n_div-div-1) != -1)
903                                 return 0;
904                 } else
905                         return 0;
906         }
907
908         for (i = 0; i < bmap->n_div; ++i)
909                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
910                         return 0;
911
912         return 1;
913 }
914
915 /*
916  * Remove divs that don't occur in any of the constraints or other divs.
917  * These can arise when dropping some of the variables in a quast
918  * returned by piplib.
919  */
920 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
921 {
922         int i;
923
924         if (!bmap)
925                 return NULL;
926
927         for (i = bmap->n_div-1; i >= 0; --i) {
928                 if (!div_is_redundant(bmap, i))
929                         continue;
930                 bmap = isl_basic_map_drop_div(bmap, i);
931         }
932         return bmap;
933 }
934
935 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
936 {
937         bmap = remove_redundant_divs(bmap);
938         if (!bmap)
939                 return NULL;
940         F_SET(bmap, ISL_BASIC_SET_FINAL);
941         return bmap;
942 }
943
944 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
945 {
946         return (struct isl_basic_set *)
947                 isl_basic_map_finalize((struct isl_basic_map *)bset);
948 }
949
950 struct isl_set *isl_set_finalize(struct isl_set *set)
951 {
952         int i;
953
954         if (!set)
955                 return NULL;
956         for (i = 0; i < set->n; ++i) {
957                 set->p[i] = isl_basic_set_finalize(set->p[i]);
958                 if (!set->p[i])
959                         goto error;
960         }
961         return set;
962 error:
963         isl_set_free(set);
964         return NULL;
965 }
966
967 struct isl_map *isl_map_finalize(struct isl_map *map)
968 {
969         int i;
970
971         if (!map)
972                 return NULL;
973         for (i = 0; i < map->n; ++i) {
974                 map->p[i] = isl_basic_map_finalize(map->p[i]);
975                 if (!map->p[i])
976                         goto error;
977         }
978         F_CLR(map, ISL_MAP_NORMALIZED);
979         return map;
980 error:
981         isl_map_free(map);
982         return NULL;
983 }
984
985
986 /* Remove any div that is defined in terms of the given variable.
987  */
988 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
989                                                                         int pos)
990 {
991         int i;
992         unsigned dim = isl_dim_total(bmap->dim);
993
994         for (i = 0; i < bmap->n_div; ++i) {
995                 if (isl_int_is_zero(bmap->div[i][0]))
996                         continue;
997                 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
998                         continue;
999                 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
1000                 if (!bmap)
1001                         return NULL;
1002         }
1003         return bmap;
1004 }
1005
1006 /* Eliminate the specified variables from the constraints using
1007  * Fourier-Motzkin.  The variables themselves are not removed.
1008  */
1009 struct isl_basic_map *isl_basic_map_eliminate_vars(
1010         struct isl_basic_map *bmap, unsigned pos, unsigned n)
1011 {
1012         int d;
1013         int i, j, k;
1014         unsigned total;
1015
1016         if (n == 0)
1017                 return bmap;
1018         if (!bmap)
1019                 return NULL;
1020         total = isl_basic_map_total_dim(bmap);
1021
1022         bmap = isl_basic_map_cow(bmap);
1023         for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1024                 int n_lower, n_upper;
1025                 bmap = remove_dependent_vars(bmap, d);
1026                 if (!bmap)
1027                         return NULL;
1028                 if (d >= total - bmap->n_div)
1029                         isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1030                 for (i = 0; i < bmap->n_eq; ++i) {
1031                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1032                                 continue;
1033                         eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1034                         isl_basic_map_drop_equality(bmap, i);
1035                         break;
1036                 }
1037                 if (i < bmap->n_eq)
1038                         continue;
1039                 n_lower = 0;
1040                 n_upper = 0;
1041                 for (i = 0; i < bmap->n_ineq; ++i) {
1042                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1043                                 n_lower++;
1044                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1045                                 n_upper++;
1046                 }
1047                 bmap = isl_basic_map_extend_constraints(bmap,
1048                                 0, n_lower * n_upper);
1049                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1050                         int last;
1051                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1052                                 continue;
1053                         last = -1;
1054                         for (j = 0; j < i; ++j) {
1055                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1056                                         continue;
1057                                 last = j;
1058                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1059                                     isl_int_sgn(bmap->ineq[j][1+d]))
1060                                         continue;
1061                                 k = isl_basic_map_alloc_inequality(bmap);
1062                                 if (k < 0)
1063                                         goto error;
1064                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1065                                                 1+total);
1066                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1067                                                 1+d, 1+total, NULL);
1068                         }
1069                         isl_basic_map_drop_inequality(bmap, i);
1070                         i = last + 1;
1071                 }
1072                 if (n_lower > 0 && n_upper > 0) {
1073                         bmap = normalize_constraints(bmap);
1074                         bmap = remove_duplicate_constraints(bmap, NULL);
1075                         bmap = isl_basic_map_gauss(bmap, NULL);
1076                         bmap = isl_basic_map_convex_hull(bmap);
1077                         if (!bmap)
1078                                 goto error;
1079                         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1080                                 break;
1081                 }
1082         }
1083         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1084         return bmap;
1085 error:
1086         isl_basic_map_free(bmap);
1087         return NULL;
1088 }
1089
1090 struct isl_basic_set *isl_basic_set_eliminate_vars(
1091         struct isl_basic_set *bset, unsigned pos, unsigned n)
1092 {
1093         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1094                         (struct isl_basic_map *)bset, pos, n);
1095 }
1096
1097 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1098 {
1099         int d, i;
1100         unsigned total;
1101
1102         total = isl_dim_total(bmap->dim);
1103         for (d = 0; d < total; ++d)
1104                 elim[d] = -1;
1105         for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
1106                 for (; d >= 0; --d) {
1107                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1108                                 continue;
1109                         elim[d] = i;
1110                         break;
1111                 }
1112         }
1113 }
1114
1115 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1116 {
1117         return compute_elimination_index((struct isl_basic_map *)bset, elim);
1118 }
1119
1120 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1121         struct isl_basic_map *bmap, int *elim)
1122 {
1123         int d, i;
1124         int copied = 0;
1125         unsigned total;
1126
1127         total = isl_dim_total(bmap->dim);
1128         for (d = total - 1; d >= 0; --d) {
1129                 if (isl_int_is_zero(src[1+d]))
1130                         continue;
1131                 if (elim[d] == -1)
1132                         continue;
1133                 if (!copied) {
1134                         isl_seq_cpy(dst, src, 1 + total);
1135                         copied = 1;
1136                 }
1137                 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1138         }
1139         return copied;
1140 }
1141
1142 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1143         struct isl_basic_set *bset, int *elim)
1144 {
1145         return reduced_using_equalities(dst, src,
1146                                         (struct isl_basic_map *)bset, elim);
1147 }
1148
1149 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1150         struct isl_basic_set *bset, struct isl_basic_set *context)
1151 {
1152         int i;
1153         int *elim;
1154
1155         if (!bset || !context)
1156                 goto error;
1157
1158         bset = isl_basic_set_cow(bset);
1159         if (!bset)
1160                 goto error;
1161
1162         elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1163         if (!elim)
1164                 goto error;
1165         set_compute_elimination_index(context, elim);
1166         for (i = 0; i < bset->n_eq; ++i)
1167                 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1168                                                         context, elim);
1169         for (i = 0; i < bset->n_ineq; ++i)
1170                 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1171                                                         context, elim);
1172         isl_basic_set_free(context);
1173         free(elim);
1174         bset = isl_basic_set_simplify(bset);
1175         bset = isl_basic_set_finalize(bset);
1176         return bset;
1177 error:
1178         isl_basic_set_free(bset);
1179         isl_basic_set_free(context);
1180         return NULL;
1181 }
1182
1183 static struct isl_basic_set *remove_shifted_constraints(
1184         struct isl_basic_set *bset, struct isl_basic_set *context)
1185 {
1186         unsigned int size;
1187         isl_int ***index;
1188         int bits;
1189         int k, h, l;
1190
1191         if (!bset)
1192                 return NULL;
1193
1194         size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1195         bits = ffs(size) - 1;
1196         index = isl_calloc_array(ctx, isl_int **, size);
1197         if (!index)
1198                 return bset;
1199
1200         for (k = 0; k < context->n_ineq; ++k) {
1201                 h = set_hash_index(index, size, bits, context, k);
1202                 index[h] = &context->ineq[k];
1203         }
1204         for (k = 0; k < bset->n_ineq; ++k) {
1205                 h = set_hash_index(index, size, bits, bset, k);
1206                 if (!index[h])
1207                         continue;
1208                 l = index[h] - &context->ineq[0];
1209                 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1210                         continue;
1211                 bset = isl_basic_set_cow(bset);
1212                 if (!bset)
1213                         goto error;
1214                 isl_basic_set_drop_inequality(bset, k);
1215                 --k;
1216         }
1217         free(index);
1218         return bset;
1219 error:
1220         free(index);
1221         return bset;
1222 }
1223
1224 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1225         struct isl_basic_set *context);
1226
1227 static struct isl_basic_set *uset_gist_context_eq(struct isl_basic_set *bset,
1228         struct isl_basic_set *context)
1229 {
1230         struct isl_mat *T;
1231         struct isl_mat *T2;
1232         struct isl_ctx *ctx = context->ctx;
1233         struct isl_basic_set *reduced_context;
1234         reduced_context = isl_basic_set_remove_equalities(
1235                                 isl_basic_set_copy(context), &T, &T2);
1236         if (!reduced_context)
1237                 goto error;
1238         bset = isl_basic_set_preimage(ctx, bset, T);
1239         bset = uset_gist(bset, reduced_context);
1240         bset = isl_basic_set_preimage(ctx, bset, T2);
1241         bset = isl_basic_set_reduce_using_equalities(bset, context);
1242         return bset;
1243 error:
1244         isl_basic_set_free(context);
1245         isl_basic_set_free(bset);
1246         return NULL;
1247 }
1248
1249 static struct isl_basic_set *uset_gist_set_eq(struct isl_basic_set *bset,
1250         struct isl_basic_set *context)
1251 {
1252         struct isl_mat *T;
1253         struct isl_mat *T2;
1254         struct isl_ctx *ctx = context->ctx;
1255         struct isl_basic_set *affine_hull = NULL;
1256
1257         affine_hull = isl_basic_set_copy(bset);
1258         affine_hull = isl_basic_set_cow(affine_hull);
1259         if (!affine_hull)
1260                 goto error;
1261         isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1262
1263         bset = isl_basic_set_remove_equalities(bset, &T, &T2);
1264         if (!bset)
1265                 goto error;
1266         context = isl_basic_set_preimage(ctx, context, T);
1267         bset = uset_gist(bset, context);
1268         bset = isl_basic_set_preimage(ctx, bset, T2);
1269         bset = isl_basic_set_intersect(bset, affine_hull);
1270         return bset;
1271 error:
1272         isl_basic_set_free(affine_hull);
1273         isl_basic_set_free(context);
1274         isl_basic_set_free(bset);
1275         return NULL;
1276 }
1277
1278 /* Remove all information from bset that is redundant in the context
1279  * of context.  In particular, equalities that are linear combinations
1280  * of those in context are removed.  Then the inequalities that are
1281  * redundant in the context of the equalities and inequalities of
1282  * context are removed.
1283  */
1284 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1285         struct isl_basic_set *context)
1286 {
1287         int i;
1288         isl_int opt;
1289         struct isl_basic_set *combined;
1290         struct isl_ctx *ctx;
1291
1292         if (!bset || !context)
1293                 goto error;
1294
1295         if (context->n_eq > 0)
1296                 return uset_gist_context_eq(bset, context);
1297         if (!context->n_ineq)
1298                 goto done;
1299         if (bset->n_eq > 0)
1300                 return uset_gist_set_eq(bset, context);
1301         bset = remove_shifted_constraints(bset, context);
1302         combined = isl_basic_set_extend_constraints(isl_basic_set_copy(bset),
1303                         context->n_eq, context->n_ineq);
1304         context = isl_basic_set_add_constraints(combined, context, 0);
1305         if (!context)
1306                 goto error;
1307         ctx = context->ctx;
1308         isl_int_init(opt);
1309         for (i = bset->n_ineq-1; i >= 0; --i) {
1310                 int redundant;
1311                 set_swap_inequality(context, i, context->n_ineq-1);
1312                 context->n_ineq--;
1313                 redundant = isl_basic_set_constraint_is_redundant(&context,
1314                                 context->ineq[context->n_ineq], &opt, NULL);
1315                 if (redundant == -1) {
1316                         isl_int_clear(opt);
1317                         goto error;
1318                 }
1319                 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
1320                         bset = isl_basic_set_set_to_empty(bset);
1321                         break;
1322                 }
1323                 context->n_ineq++;
1324                 set_swap_inequality(context, i, context->n_ineq-1);
1325                 if (redundant) {
1326                         isl_basic_set_drop_inequality(context, i);
1327                         isl_basic_set_drop_inequality(bset, i);
1328                 }
1329         }
1330         isl_int_clear(opt);
1331 done:
1332         isl_basic_set_free(context);
1333         return bset;
1334 error:
1335         isl_basic_set_free(context);
1336         isl_basic_set_free(bset);
1337         return NULL;
1338 }
1339
1340 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1341         struct isl_basic_map *context)
1342 {
1343         struct isl_basic_set *bset;
1344
1345         if (!bmap || !context)
1346                 goto error;
1347
1348         context = isl_basic_map_align_divs(context, bmap);
1349         bmap = isl_basic_map_align_divs(bmap, context);
1350
1351         bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1352                          isl_basic_map_underlying_set(context));
1353
1354         return isl_basic_map_overlying_set(bset, bmap);
1355 error:
1356         isl_basic_map_free(bmap);
1357         isl_basic_map_free(context);
1358         return NULL;
1359 }
1360
1361 /*
1362  * Assumes context has no implicit divs.
1363  */
1364 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1365 {
1366         int i;
1367
1368         map = isl_map_cow(map);
1369         if (!map || !context)
1370                 return NULL;
1371         isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1372         map = isl_map_compute_divs(map);
1373         for (i = 0; i < map->n; ++i)
1374                 context = isl_basic_map_align_divs(context, map->p[i]);
1375         for (i = 0; i < map->n; ++i) {
1376                 map->p[i] = isl_basic_map_gist(map->p[i],
1377                                                 isl_basic_map_copy(context));
1378                 if (!map->p[i])
1379                         goto error;
1380         }
1381         isl_basic_map_free(context);
1382         F_CLR(map, ISL_MAP_NORMALIZED);
1383         return map;
1384 error:
1385         isl_map_free(map);
1386         isl_basic_map_free(context);
1387         return NULL;
1388 }
1389
1390 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1391                                                 struct isl_basic_set *context)
1392 {
1393         return (struct isl_basic_set *)isl_basic_map_gist(
1394                 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1395 }
1396
1397 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1398 {
1399         return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1400                                         (struct isl_basic_map *)context);
1401 }
1402
1403 /* Quick check to see if two basic maps are disjoint.
1404  * In particular, we reduce the equalities and inequalities of
1405  * one basic map in the context of the equalities of the other
1406  * basic map and check if we get a contradiction.
1407  */
1408 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1409         struct isl_basic_map *bmap2)
1410 {
1411         struct isl_vec *v = NULL;
1412         int *elim = NULL;
1413         unsigned total;
1414         int d, i;
1415
1416         if (!bmap1 || !bmap2)
1417                 return -1;
1418         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1419                         return -1);
1420         if (bmap1->n_div || bmap2->n_div)
1421                 return 0;
1422         if (!bmap1->n_eq && !bmap2->n_eq)
1423                 return 0;
1424
1425         total = isl_dim_total(bmap1->dim);
1426         if (total == 0)
1427                 return 0;
1428         v = isl_vec_alloc(bmap1->ctx, 1 + total);
1429         if (!v)
1430                 goto error;
1431         elim = isl_alloc_array(bmap1->ctx, int, total);
1432         if (!elim)
1433                 goto error;
1434         compute_elimination_index(bmap1, elim);
1435         for (i = 0; i < bmap2->n_eq; ++i) {
1436                 int reduced;
1437                 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1438                                                         bmap1, elim);
1439                 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1440                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1441                         goto disjoint;
1442         }
1443         for (i = 0; i < bmap2->n_ineq; ++i) {
1444                 int reduced;
1445                 reduced = reduced_using_equalities(v->block.data,
1446                                                 bmap2->ineq[i], bmap1, elim);
1447                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1448                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1449                         goto disjoint;
1450         }
1451         compute_elimination_index(bmap2, elim);
1452         for (i = 0; i < bmap1->n_ineq; ++i) {
1453                 int reduced;
1454                 reduced = reduced_using_equalities(v->block.data,
1455                                                 bmap1->ineq[i], bmap2, elim);
1456                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1457                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1458                         goto disjoint;
1459         }
1460         isl_vec_free(bmap1->ctx, v);
1461         free(elim);
1462         return 0;
1463 disjoint:
1464         isl_vec_free(bmap1->ctx, v);
1465         free(elim);
1466         return 1;
1467 error:
1468         isl_vec_free(bmap1->ctx, v);
1469         free(elim);
1470         return -1;
1471 }
1472
1473 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1474         struct isl_basic_set *bset2)
1475 {
1476         return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1477                                               (struct isl_basic_map *)bset2);
1478 }
1479
1480 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1481 {
1482         int i, j;
1483
1484         if (!map1 || !map2)
1485                 return -1;
1486
1487         if (isl_map_fast_is_equal(map1, map2))
1488                 return 0;
1489
1490         for (i = 0; i < map1->n; ++i) {
1491                 for (j = 0; j < map2->n; ++j) {
1492                         int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1493                                                                map2->p[j]);
1494                         if (d != 1)
1495                                 return d;
1496                 }
1497         }
1498         return 1;
1499 }
1500
1501 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1502 {
1503         return isl_map_fast_is_disjoint((struct isl_map *)set1,
1504                                         (struct isl_map *)set2);
1505 }