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