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