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