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