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