isl_basic_map_drop: properly handle dimensions of type isl_dim_div
[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_tab.h"
5
6 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
7 {
8         isl_int *t = bmap->eq[a];
9         bmap->eq[a] = bmap->eq[b];
10         bmap->eq[b] = t;
11 }
12
13 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
14 {
15         if (a != b) {
16                 isl_int *t = bmap->ineq[a];
17                 bmap->ineq[a] = bmap->ineq[b];
18                 bmap->ineq[b] = t;
19         }
20 }
21
22 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
23 {
24         swap_inequality((struct isl_basic_map *)bset, a, b);
25 }
26
27 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
28 {
29         isl_seq_cpy(c, c + n, rem);
30         isl_seq_clr(c + rem, n);
31 }
32
33 /* Drop n dimensions starting at first.
34  *
35  * In principle, this frees up some extra variables as the number
36  * of columns remains constant, but we would have to extend
37  * the div array too as the number of rows in this array is assumed
38  * to be equal to extra.
39  */
40 struct isl_basic_set *isl_basic_set_drop_dims(
41                 struct isl_basic_set *bset, unsigned first, unsigned n)
42 {
43         int i;
44
45         if (!bset)
46                 goto error;
47
48         isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
49
50         if (n == 0)
51                 return bset;
52
53         bset = isl_basic_set_cow(bset);
54         if (!bset)
55                 return NULL;
56
57         for (i = 0; i < bset->n_eq; ++i)
58                 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
59                                      (bset->dim->n_out-first-n)+bset->extra);
60
61         for (i = 0; i < bset->n_ineq; ++i)
62                 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
63                                      (bset->dim->n_out-first-n)+bset->extra);
64
65         for (i = 0; i < bset->n_div; ++i)
66                 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
67                                      (bset->dim->n_out-first-n)+bset->extra);
68
69         bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
70         if (!bset->dim)
71                 goto error;
72
73         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
74         bset = isl_basic_set_simplify(bset);
75         return isl_basic_set_finalize(bset);
76 error:
77         isl_basic_set_free(bset);
78         return NULL;
79 }
80
81 struct isl_set *isl_set_drop_dims(
82                 struct isl_set *set, unsigned first, unsigned n)
83 {
84         int i;
85
86         if (!set)
87                 goto error;
88
89         isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
90
91         if (n == 0)
92                 return set;
93         set = isl_set_cow(set);
94         if (!set)
95                 goto error;
96         set->dim = isl_dim_drop_outputs(set->dim, first, n);
97         if (!set->dim)
98                 goto error;
99
100         for (i = 0; i < set->n; ++i) {
101                 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
102                 if (!set->p[i])
103                         goto error;
104         }
105
106         ISL_F_CLR(set, ISL_SET_NORMALIZED);
107         return set;
108 error:
109         isl_set_free(set);
110         return NULL;
111 }
112
113 /* Move "n" divs starting at "first" to the end of the list of divs.
114  */
115 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
116         unsigned first, unsigned n)
117 {
118         isl_int **div;
119         int i;
120
121         if (first + n == bmap->n_div)
122                 return bmap;
123
124         div = isl_alloc_array(bmap->ctx, isl_int *, n);
125         if (!div)
126                 goto error;
127         for (i = 0; i < n; ++i)
128                 div[i] = bmap->div[first + i];
129         for (i = 0; i < bmap->n_div - first - n; ++i)
130                 bmap->div[first + i] = bmap->div[first + n + i];
131         for (i = 0; i < n; ++i)
132                 bmap->div[bmap->n_div - n + i] = div[i];
133         free(div);
134         return bmap;
135 error:
136         isl_basic_map_free(bmap);
137         return NULL;
138 }
139
140 /* Drop "n" dimensions of type "type" starting at "first".
141  *
142  * In principle, this frees up some extra variables as the number
143  * of columns remains constant, but we would have to extend
144  * the div array too as the number of rows in this array is assumed
145  * to be equal to extra.
146  */
147 struct isl_basic_map *isl_basic_map_drop(struct isl_basic_map *bmap,
148         enum isl_dim_type type, unsigned first, unsigned n)
149 {
150         int i;
151         unsigned dim;
152         unsigned offset;
153         unsigned left;
154
155         if (!bmap)
156                 goto error;
157
158         dim = isl_basic_map_dim(bmap, type);
159         isl_assert(bmap->ctx, first + n <= dim, goto error);
160
161         if (n == 0)
162                 return bmap;
163
164         bmap = isl_basic_map_cow(bmap);
165         if (!bmap)
166                 return NULL;
167
168         offset = isl_basic_map_offset(bmap, type) + first;
169         left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
170         for (i = 0; i < bmap->n_eq; ++i)
171                 constraint_drop_vars(bmap->eq[i]+offset, n, left);
172
173         for (i = 0; i < bmap->n_ineq; ++i)
174                 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
175
176         for (i = 0; i < bmap->n_div; ++i)
177                 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
178
179         if (type == isl_dim_div) {
180                 bmap = move_divs_last(bmap, first, n);
181                 if (!bmap)
182                         goto error;
183                 isl_basic_map_free_div(bmap, n);
184         } else
185                 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
186         if (!bmap->dim)
187                 goto error;
188
189         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
190         bmap = isl_basic_map_simplify(bmap);
191         return isl_basic_map_finalize(bmap);
192 error:
193         isl_basic_map_free(bmap);
194         return NULL;
195 }
196
197 struct isl_basic_map *isl_basic_map_drop_inputs(
198                 struct isl_basic_map *bmap, unsigned first, unsigned n)
199 {
200         return isl_basic_map_drop(bmap, isl_dim_in, first, n);
201 }
202
203 struct isl_map *isl_map_drop(struct isl_map *map,
204         enum isl_dim_type type, unsigned first, unsigned n)
205 {
206         int i;
207
208         if (!map)
209                 goto error;
210
211         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
212
213         if (n == 0)
214                 return map;
215         map = isl_map_cow(map);
216         if (!map)
217                 goto error;
218         map->dim = isl_dim_drop(map->dim, type, first, n);
219         if (!map->dim)
220                 goto error;
221
222         for (i = 0; i < map->n; ++i) {
223                 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
224                 if (!map->p[i])
225                         goto error;
226         }
227         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
228
229         return map;
230 error:
231         isl_map_free(map);
232         return NULL;
233 }
234
235 struct isl_map *isl_map_drop_inputs(
236                 struct isl_map *map, unsigned first, unsigned n)
237 {
238         return isl_map_drop(map, isl_dim_in, first, n);
239 }
240
241 /*
242  * We don't cow, as the div is assumed to be redundant.
243  */
244 static struct isl_basic_map *isl_basic_map_drop_div(
245                 struct isl_basic_map *bmap, unsigned div)
246 {
247         int i;
248         unsigned pos;
249
250         if (!bmap)
251                 goto error;
252
253         pos = 1 + isl_dim_total(bmap->dim) + div;
254
255         isl_assert(bmap->ctx, div < bmap->n_div, goto error);
256
257         for (i = 0; i < bmap->n_eq; ++i)
258                 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
259
260         for (i = 0; i < bmap->n_ineq; ++i) {
261                 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
262                         isl_basic_map_drop_inequality(bmap, i);
263                         --i;
264                         continue;
265                 }
266                 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
267         }
268
269         for (i = 0; i < bmap->n_div; ++i)
270                 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
271
272         if (div != bmap->n_div - 1) {
273                 int j;
274                 isl_int *t = bmap->div[div];
275
276                 for (j = div; j < bmap->n_div - 1; ++j)
277                         bmap->div[j] = bmap->div[j+1];
278
279                 bmap->div[bmap->n_div - 1] = t;
280         }
281         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
282         isl_basic_map_free_div(bmap, 1);
283
284         return bmap;
285 error:
286         isl_basic_map_free(bmap);
287         return NULL;
288 }
289
290 struct isl_basic_map *isl_basic_map_normalize_constraints(
291         struct isl_basic_map *bmap)
292 {
293         int i;
294         isl_int gcd;
295         unsigned total = isl_basic_map_total_dim(bmap);
296
297         isl_int_init(gcd);
298         for (i = bmap->n_eq - 1; i >= 0; --i) {
299                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
300                 if (isl_int_is_zero(gcd)) {
301                         if (!isl_int_is_zero(bmap->eq[i][0])) {
302                                 bmap = isl_basic_map_set_to_empty(bmap);
303                                 break;
304                         }
305                         isl_basic_map_drop_equality(bmap, i);
306                         continue;
307                 }
308                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
309                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
310                 if (isl_int_is_one(gcd))
311                         continue;
312                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
313                         bmap = isl_basic_map_set_to_empty(bmap);
314                         break;
315                 }
316                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
317         }
318
319         for (i = bmap->n_ineq - 1; i >= 0; --i) {
320                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
321                 if (isl_int_is_zero(gcd)) {
322                         if (isl_int_is_neg(bmap->ineq[i][0])) {
323                                 bmap = isl_basic_map_set_to_empty(bmap);
324                                 break;
325                         }
326                         isl_basic_map_drop_inequality(bmap, i);
327                         continue;
328                 }
329                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
330                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
331                 if (isl_int_is_one(gcd))
332                         continue;
333                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
334                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
335         }
336         isl_int_clear(gcd);
337
338         return bmap;
339 }
340
341 struct isl_basic_set *isl_basic_set_normalize_constraints(
342         struct isl_basic_set *bset)
343 {
344         (struct isl_basic_set *)isl_basic_map_normalize_constraints(
345                 (struct isl_basic_map *)bset);
346 }
347
348 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
349 {
350         int i;
351         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
352         unsigned len;
353         len = 1 + isl_basic_map_total_dim(bmap);
354
355         for (i = 0; i < bmap->n_eq; ++i)
356                 if (bmap->eq[i] != eq)
357                         isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
358
359         for (i = 0; i < bmap->n_ineq; ++i)
360                 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
361
362         /* We need to be careful about circular definitions,
363          * so for now we just remove the definitions of other divs that
364          * depend on this div and (possibly) recompute them later.
365          */
366         for (i = 0; i < bmap->n_div; ++i)
367                 if (!isl_int_is_zero(bmap->div[i][0]) &&
368                     !isl_int_is_zero(bmap->div[i][1 + pos]))
369                         isl_seq_clr(bmap->div[i], 1 + len);
370
371         isl_basic_map_drop_div(bmap, div);
372 }
373
374 /* Elimininate divs based on equalities
375  */
376 static struct isl_basic_map *eliminate_divs_eq(
377                 struct isl_basic_map *bmap, int *progress)
378 {
379         int d;
380         int i;
381         int modified = 0;
382         unsigned off;
383
384         if (!bmap)
385                 return NULL;
386
387         off = 1 + isl_dim_total(bmap->dim);
388
389         for (d = bmap->n_div - 1; d >= 0 ; --d) {
390                 for (i = 0; i < bmap->n_eq; ++i) {
391                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
392                             !isl_int_is_negone(bmap->eq[i][off + d]))
393                                 continue;
394                         modified = 1;
395                         *progress = 1;
396                         eliminate_div(bmap, bmap->eq[i], d);
397                         isl_basic_map_drop_equality(bmap, i);
398                         break;
399                 }
400         }
401         if (modified)
402                 return eliminate_divs_eq(bmap, progress);
403         return bmap;
404 }
405
406 /* Elimininate divs based on inequalities
407  */
408 static struct isl_basic_map *eliminate_divs_ineq(
409                 struct isl_basic_map *bmap, int *progress)
410 {
411         int d;
412         int i;
413         unsigned off;
414         struct isl_ctx *ctx;
415
416         if (!bmap)
417                 return NULL;
418
419         ctx = bmap->ctx;
420         off = 1 + isl_dim_total(bmap->dim);
421
422         for (d = bmap->n_div - 1; d >= 0 ; --d) {
423                 for (i = 0; i < bmap->n_eq; ++i)
424                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
425                                 break;
426                 if (i < bmap->n_eq)
427                         continue;
428                 for (i = 0; i < bmap->n_ineq; ++i)
429                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
430                                 break;
431                 if (i < bmap->n_ineq)
432                         continue;
433                 *progress = 1;
434                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
435                 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
436                         break;
437                 bmap = isl_basic_map_drop_div(bmap, d);
438                 if (!bmap)
439                         break;
440         }
441         return bmap;
442 }
443
444 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
445         unsigned pos, isl_int *eq, int *progress)
446 {
447         unsigned total;
448         int k;
449         int contains_divs;
450
451         total = isl_basic_map_total_dim(bmap);
452         contains_divs =
453                 isl_seq_first_non_zero(eq + 1 + isl_dim_total(bmap->dim),
454                                                 bmap->n_div) != -1;
455         for (k = 0; k < bmap->n_eq; ++k) {
456                 if (bmap->eq[k] == eq)
457                         continue;
458                 if (isl_int_is_zero(bmap->eq[k][1+pos]))
459                         continue;
460                 if (progress)
461                         *progress = 1;
462                 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
463         }
464
465         for (k = 0; k < bmap->n_ineq; ++k) {
466                 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
467                         continue;
468                 if (progress)
469                         *progress = 1;
470                 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
471                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
472         }
473
474         for (k = 0; k < bmap->n_div; ++k) {
475                 if (isl_int_is_zero(bmap->div[k][0]))
476                         continue;
477                 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
478                         continue;
479                 if (progress)
480                         *progress = 1;
481                 /* We need to be careful about circular definitions,
482                  * so for now we just remove the definition of div k
483                  * if the equality contains any divs.
484                  */
485                 if (contains_divs)
486                         isl_seq_clr(bmap->div[k], 1 + total);
487                 else
488                         isl_seq_elim(bmap->div[k]+1, eq,
489                                         1+pos, 1+total, &bmap->div[k][0]);
490                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
491         }
492 }
493
494 struct isl_basic_map *isl_basic_map_gauss(
495         struct isl_basic_map *bmap, int *progress)
496 {
497         int k;
498         int done;
499         int last_var;
500         unsigned total_var;
501         unsigned total;
502
503         if (!bmap)
504                 return NULL;
505
506         total = isl_basic_map_total_dim(bmap);
507         total_var = total - bmap->n_div;
508
509         last_var = total - 1;
510         for (done = 0; done < bmap->n_eq; ++done) {
511                 for (; last_var >= 0; --last_var) {
512                         for (k = done; k < bmap->n_eq; ++k)
513                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
514                                         break;
515                         if (k < bmap->n_eq)
516                                 break;
517                 }
518                 if (last_var < 0)
519                         break;
520                 if (k != done)
521                         swap_equality(bmap, k, done);
522                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
523                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
524
525                 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
526                                                 progress);
527
528                 if (last_var >= total_var &&
529                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
530                         unsigned div = last_var - total_var;
531                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
532                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
533                         isl_int_set(bmap->div[div][0],
534                                     bmap->eq[done][1+last_var]);
535                         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
536                 }
537         }
538         if (done == bmap->n_eq)
539                 return bmap;
540         for (k = done; k < bmap->n_eq; ++k) {
541                 if (isl_int_is_zero(bmap->eq[k][0]))
542                         continue;
543                 return isl_basic_map_set_to_empty(bmap);
544         }
545         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
546         return bmap;
547 }
548
549 struct isl_basic_set *isl_basic_set_gauss(
550         struct isl_basic_set *bset, int *progress)
551 {
552         return (struct isl_basic_set*)isl_basic_map_gauss(
553                         (struct isl_basic_map *)bset, progress);
554 }
555
556
557 static unsigned int round_up(unsigned int v)
558 {
559         int old_v = v;
560
561         while (v) {
562                 old_v = v;
563                 v ^= v & -v;
564         }
565         return old_v << 1;
566 }
567
568 static int hash_index(isl_int ***index, unsigned int size, int bits,
569                         struct isl_basic_map *bmap, int k)
570 {
571         int h;
572         unsigned total = isl_basic_map_total_dim(bmap);
573         uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
574         for (h = hash; index[h]; h = (h+1) % size)
575                 if (&bmap->ineq[k] != index[h] &&
576                     isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
577                         break;
578         return h;
579 }
580
581 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
582                           struct isl_basic_set *bset, int k)
583 {
584         return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
585 }
586
587 /* If we can eliminate more than one div, then we need to make
588  * sure we do it from last div to first div, in order not to
589  * change the position of the other divs that still need to
590  * be removed.
591  */
592 static struct isl_basic_map *remove_duplicate_divs(
593         struct isl_basic_map *bmap, int *progress)
594 {
595         unsigned int size;
596         int *index;
597         int *elim_for;
598         int k, l, h;
599         int bits;
600         struct isl_blk eq;
601         unsigned total_var = isl_dim_total(bmap->dim);
602         unsigned total = total_var + bmap->n_div;
603         struct isl_ctx *ctx;
604
605         if (bmap->n_div <= 1)
606                 return bmap;
607
608         ctx = bmap->ctx;
609         for (k = bmap->n_div - 1; k >= 0; --k)
610                 if (!isl_int_is_zero(bmap->div[k][0]))
611                         break;
612         if (k <= 0)
613                 return bmap;
614
615         elim_for = isl_calloc_array(ctx, int, bmap->n_div);
616         size = round_up(4 * bmap->n_div / 3 - 1);
617         bits = ffs(size) - 1;
618         index = isl_calloc_array(ctx, int, size);
619         if (!index)
620                 return bmap;
621         eq = isl_blk_alloc(ctx, 1+total);
622         if (isl_blk_is_error(eq))
623                 goto out;
624
625         isl_seq_clr(eq.data, 1+total);
626         index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
627         for (--k; k >= 0; --k) {
628                 uint32_t hash;
629
630                 if (isl_int_is_zero(bmap->div[k][0]))
631                         continue;
632
633                 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
634                 for (h = hash; index[h]; h = (h+1) % size)
635                         if (isl_seq_eq(bmap->div[k],
636                                        bmap->div[index[h]-1], 2+total))
637                                 break;
638                 if (index[h]) {
639                         *progress = 1;
640                         l = index[h] - 1;
641                         elim_for[l] = k + 1;
642                 }
643                 index[h] = k+1;
644         }
645         for (l = bmap->n_div - 1; l >= 0; --l) {
646                 if (!elim_for[l])
647                         continue;
648                 k = elim_for[l] - 1;
649                 isl_int_set_si(eq.data[1+total_var+k], -1);
650                 isl_int_set_si(eq.data[1+total_var+l], 1);
651                 eliminate_div(bmap, eq.data, l);
652                 isl_int_set_si(eq.data[1+total_var+k], 0);
653                 isl_int_set_si(eq.data[1+total_var+l], 0);
654         }
655
656         isl_blk_free(ctx, eq);
657 out:
658         free(index);
659         free(elim_for);
660         return bmap;
661 }
662
663 static int n_pure_div_eq(struct isl_basic_map *bmap)
664 {
665         int i, j;
666         unsigned total;
667
668         total = isl_dim_total(bmap->dim);
669         for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
670                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
671                         --j;
672                 if (j < 0)
673                         break;
674                 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
675                         return 0;
676         }
677         return i;
678 }
679
680 /* Normalize divs that appear in equalities.
681  *
682  * In particular, we assume that bmap contains some equalities
683  * of the form
684  *
685  *      a x = m * e_i
686  *
687  * and we want to replace the set of e_i by a minimal set and
688  * such that the new e_i have a canonical representation in terms
689  * of the vector x.
690  * If any of the equalities involves more than one divs, then
691  * we currently simply bail out.
692  *
693  * Let us first additionally assume that all equalities involve
694  * a div.  The equalities then express modulo constraints on the
695  * remaining variables and we can use "parameter compression"
696  * to find a minimal set of constraints.  The result is a transformation
697  *
698  *      x = T(x') = x_0 + G x'
699  *
700  * with G a lower-triangular matrix with all elements below the diagonal
701  * non-negative and smaller than the diagonal element on the same row.
702  * We first normalize x_0 by making the same property hold in the affine
703  * T matrix.
704  * The rows i of G with a 1 on the diagonal do not impose any modulo
705  * constraint and simply express x_i = x'_i.
706  * For each of the remaining rows i, we introduce a div and a corresponding
707  * equality.  In particular
708  *
709  *      g_ii e_j = x_i - g_i(x')
710  *
711  * where each x'_k is replaced either by x_k (if g_kk = 1) or the
712  * corresponding div (if g_kk != 1).
713  *
714  * If there are any equalities not involving any div, then we
715  * first apply a variable compression on the variables x:
716  *
717  *      x = C x''       x'' = C_2 x
718  *
719  * and perform the above parameter compression on A C instead of on A.
720  * The resulting compression is then of the form
721  *
722  *      x'' = T(x') = x_0 + G x'
723  *
724  * and in constructing the new divs and the corresponding equalities,
725  * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
726  * by the corresponding row from C_2.
727  */
728 static struct isl_basic_map *normalize_divs(
729         struct isl_basic_map *bmap, int *progress)
730 {
731         int i, j, k;
732         int total;
733         int div_eq;
734         struct isl_mat *B;
735         struct isl_vec *d;
736         struct isl_mat *T = NULL;
737         struct isl_mat *C = NULL;
738         struct isl_mat *C2 = NULL;
739         isl_int v;
740         int *pos;
741         int dropped, needed;
742
743         if (!bmap)
744                 return NULL;
745
746         if (bmap->n_div == 0)
747                 return bmap;
748
749         if (bmap->n_eq == 0)
750                 return bmap;
751
752         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
753                 return bmap;
754
755         total = isl_dim_total(bmap->dim);
756         div_eq = n_pure_div_eq(bmap);
757         if (div_eq == 0)
758                 return bmap;
759
760         if (div_eq < bmap->n_eq) {
761                 B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, div_eq,
762                                         bmap->n_eq - div_eq, 0, 1 + total);
763                 C = isl_mat_variable_compression(bmap->ctx, B, &C2);
764                 if (!C || !C2)
765                         goto error;
766                 if (C->n_col == 0) {
767                         bmap = isl_basic_map_set_to_empty(bmap);
768                         isl_mat_free(bmap->ctx, C);
769                         isl_mat_free(bmap->ctx, C2);
770                         goto done;
771                 }
772         }
773
774         d = isl_vec_alloc(bmap->ctx, div_eq);
775         if (!d)
776                 goto error;
777         for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
778                 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
779                         --j;
780                 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
781         }
782         B = isl_mat_sub_alloc(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
783
784         if (C) {
785                 B = isl_mat_product(bmap->ctx, B, C);
786                 C = NULL;
787         }
788
789         T = isl_mat_parameter_compression(bmap->ctx, B, d);
790         if (!T)
791                 goto error;
792         if (T->n_col == 0) {
793                 bmap = isl_basic_map_set_to_empty(bmap);
794                 isl_mat_free(bmap->ctx, C2);
795                 isl_mat_free(bmap->ctx, T);
796                 goto done;
797         }
798         isl_int_init(v);
799         for (i = 0; i < T->n_row - 1; ++i) {
800                 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
801                 if (isl_int_is_zero(v))
802                         continue;
803                 isl_mat_col_submul(T, 0, v, 1 + i);
804         }
805         isl_int_clear(v);
806         pos = isl_alloc_array(bmap->ctx, int, T->n_row);
807         /* We have to be careful because dropping equalities may reorder them */
808         dropped = 0;
809         for (j = bmap->n_div - 1; j >= 0; --j) {
810                 for (i = 0; i < bmap->n_eq; ++i)
811                         if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
812                                 break;
813                 if (i < bmap->n_eq) {
814                         bmap = isl_basic_map_drop_div(bmap, j);
815                         isl_basic_map_drop_equality(bmap, i);
816                         ++dropped;
817                 }
818         }
819         pos[0] = 0;
820         needed = 0;
821         for (i = 1; i < T->n_row; ++i) {
822                 if (isl_int_is_one(T->row[i][i]))
823                         pos[i] = i;
824                 else
825                         needed++;
826         }
827         if (needed > dropped) {
828                 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
829                                 needed, needed, 0);
830                 if (!bmap)
831                         goto error;
832         }
833         for (i = 1; i < T->n_row; ++i) {
834                 if (isl_int_is_one(T->row[i][i]))
835                         continue;
836                 k = isl_basic_map_alloc_div(bmap);
837                 pos[i] = 1 + total + k;
838                 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
839                 isl_int_set(bmap->div[k][0], T->row[i][i]);
840                 if (C2)
841                         isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
842                 else
843                         isl_int_set_si(bmap->div[k][1 + i], 1);
844                 for (j = 0; j < i; ++j) {
845                         if (isl_int_is_zero(T->row[i][j]))
846                                 continue;
847                         if (pos[j] < T->n_row && C2)
848                                 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
849                                                 C2->row[pos[j]], 1 + total);
850                         else
851                                 isl_int_neg(bmap->div[k][1 + pos[j]],
852                                                                 T->row[i][j]);
853                 }
854                 j = isl_basic_map_alloc_equality(bmap);
855                 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
856                 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
857         }
858         free(pos);
859         isl_mat_free(bmap->ctx, C2);
860         isl_mat_free(bmap->ctx, T);
861
862         if (progress)
863                 *progress = 1;
864 done:
865         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
866
867         return bmap;
868 error:
869         isl_mat_free(bmap->ctx, C);
870         isl_mat_free(bmap->ctx, C2);
871         isl_mat_free(bmap->ctx, T);
872         return bmap;
873 }
874
875 static struct isl_basic_map *remove_duplicate_constraints(
876         struct isl_basic_map *bmap, int *progress)
877 {
878         unsigned int size;
879         isl_int ***index;
880         int k, l, h;
881         int bits;
882         unsigned total = isl_basic_map_total_dim(bmap);
883         isl_int sum;
884
885         if (bmap->n_ineq <= 1)
886                 return bmap;
887
888         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
889         bits = ffs(size) - 1;
890         index = isl_calloc_array(ctx, isl_int **, size);
891         if (!index)
892                 return bmap;
893
894         index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
895         for (k = 1; k < bmap->n_ineq; ++k) {
896                 h = hash_index(index, size, bits, bmap, k);
897                 if (!index[h]) {
898                         index[h] = &bmap->ineq[k];
899                         continue;
900                 }
901                 if (progress)
902                         *progress = 1;
903                 l = index[h] - &bmap->ineq[0];
904                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
905                         swap_inequality(bmap, k, l);
906                 isl_basic_map_drop_inequality(bmap, k);
907                 --k;
908         }
909         isl_int_init(sum);
910         for (k = 0; k < bmap->n_ineq-1; ++k) {
911                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
912                 h = hash_index(index, size, bits, bmap, k);
913                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
914                 if (!index[h])
915                         continue;
916                 l = index[h] - &bmap->ineq[0];
917                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
918                 if (isl_int_is_pos(sum))
919                         continue;
920                 if (isl_int_is_zero(sum)) {
921                         /* We need to break out of the loop after these
922                          * changes since the contents of the hash
923                          * will no longer be valid.
924                          * Plus, we probably we want to regauss first.
925                          */
926                         isl_basic_map_drop_inequality(bmap, l);
927                         isl_basic_map_inequality_to_equality(bmap, k);
928                 } else
929                         bmap = isl_basic_map_set_to_empty(bmap);
930                 break;
931         }
932         isl_int_clear(sum);
933
934         free(index);
935         return bmap;
936 }
937
938
939 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
940 {
941         int progress = 1;
942         if (!bmap)
943                 return NULL;
944         while (progress) {
945                 progress = 0;
946                 bmap = isl_basic_map_normalize_constraints(bmap);
947                 bmap = remove_duplicate_divs(bmap, &progress);
948                 bmap = eliminate_divs_eq(bmap, &progress);
949                 bmap = eliminate_divs_ineq(bmap, &progress);
950                 bmap = isl_basic_map_gauss(bmap, &progress);
951                 /* requires equalities in normal form */
952                 bmap = normalize_divs(bmap, &progress);
953                 bmap = remove_duplicate_constraints(bmap, &progress);
954         }
955         return bmap;
956 }
957
958 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
959 {
960         return (struct isl_basic_set *)
961                 isl_basic_map_simplify((struct isl_basic_map *)bset);
962 }
963
964
965 /* If the only constraints a div d=floor(f/m)
966  * appears in are its two defining constraints
967  *
968  *      f - m d >=0
969  *      -(f - (m - 1)) + m d >= 0
970  *
971  * then it can safely be removed.
972  */
973 static int div_is_redundant(struct isl_basic_map *bmap, int div)
974 {
975         int i;
976         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
977
978         for (i = 0; i < bmap->n_eq; ++i)
979                 if (!isl_int_is_zero(bmap->eq[i][pos]))
980                         return 0;
981
982         for (i = 0; i < bmap->n_ineq; ++i) {
983                 if (isl_int_is_zero(bmap->ineq[i][pos]))
984                         continue;
985                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
986                         int neg;
987                         isl_int_sub(bmap->div[div][1],
988                                         bmap->div[div][1], bmap->div[div][0]);
989                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
990                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
991                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
992                         isl_int_add(bmap->div[div][1],
993                                         bmap->div[div][1], bmap->div[div][0]);
994                         if (!neg)
995                                 return 0;
996                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
997                                                     bmap->n_div-div-1) != -1)
998                                 return 0;
999                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
1000                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
1001                                 return 0;
1002                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
1003                                                     bmap->n_div-div-1) != -1)
1004                                 return 0;
1005                 } else
1006                         return 0;
1007         }
1008
1009         for (i = 0; i < bmap->n_div; ++i)
1010                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1011                         return 0;
1012
1013         return 1;
1014 }
1015
1016 /*
1017  * Remove divs that don't occur in any of the constraints or other divs.
1018  * These can arise when dropping some of the variables in a quast
1019  * returned by piplib.
1020  */
1021 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1022 {
1023         int i;
1024
1025         if (!bmap)
1026                 return NULL;
1027
1028         for (i = bmap->n_div-1; i >= 0; --i) {
1029                 if (!div_is_redundant(bmap, i))
1030                         continue;
1031                 bmap = isl_basic_map_drop_div(bmap, i);
1032         }
1033         return bmap;
1034 }
1035
1036 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1037 {
1038         bmap = remove_redundant_divs(bmap);
1039         if (!bmap)
1040                 return NULL;
1041         ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1042         return bmap;
1043 }
1044
1045 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1046 {
1047         return (struct isl_basic_set *)
1048                 isl_basic_map_finalize((struct isl_basic_map *)bset);
1049 }
1050
1051 struct isl_set *isl_set_finalize(struct isl_set *set)
1052 {
1053         int i;
1054
1055         if (!set)
1056                 return NULL;
1057         for (i = 0; i < set->n; ++i) {
1058                 set->p[i] = isl_basic_set_finalize(set->p[i]);
1059                 if (!set->p[i])
1060                         goto error;
1061         }
1062         return set;
1063 error:
1064         isl_set_free(set);
1065         return NULL;
1066 }
1067
1068 struct isl_map *isl_map_finalize(struct isl_map *map)
1069 {
1070         int i;
1071
1072         if (!map)
1073                 return NULL;
1074         for (i = 0; i < map->n; ++i) {
1075                 map->p[i] = isl_basic_map_finalize(map->p[i]);
1076                 if (!map->p[i])
1077                         goto error;
1078         }
1079         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1080         return map;
1081 error:
1082         isl_map_free(map);
1083         return NULL;
1084 }
1085
1086
1087 /* Remove any div that is defined in terms of the given variable.
1088  */
1089 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1090                                                                         int pos)
1091 {
1092         int i;
1093         unsigned dim = isl_dim_total(bmap->dim);
1094
1095         for (i = 0; i < bmap->n_div; ++i) {
1096                 if (isl_int_is_zero(bmap->div[i][0]))
1097                         continue;
1098                 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1099                         continue;
1100                 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
1101                 if (!bmap)
1102                         return NULL;
1103         }
1104         return bmap;
1105 }
1106
1107 /* Eliminate the specified variables from the constraints using
1108  * Fourier-Motzkin.  The variables themselves are not removed.
1109  */
1110 struct isl_basic_map *isl_basic_map_eliminate_vars(
1111         struct isl_basic_map *bmap, unsigned pos, unsigned n)
1112 {
1113         int d;
1114         int i, j, k;
1115         unsigned total;
1116
1117         if (n == 0)
1118                 return bmap;
1119         if (!bmap)
1120                 return NULL;
1121         total = isl_basic_map_total_dim(bmap);
1122
1123         bmap = isl_basic_map_cow(bmap);
1124         for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1125                 bmap = remove_dependent_vars(bmap, d);
1126
1127         for (d = pos + n - 1;
1128              d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1129                 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1130         for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1131                 int n_lower, n_upper;
1132                 if (!bmap)
1133                         return NULL;
1134                 for (i = 0; i < bmap->n_eq; ++i) {
1135                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1136                                 continue;
1137                         eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
1138                         isl_basic_map_drop_equality(bmap, i);
1139                         break;
1140                 }
1141                 if (i < bmap->n_eq)
1142                         continue;
1143                 n_lower = 0;
1144                 n_upper = 0;
1145                 for (i = 0; i < bmap->n_ineq; ++i) {
1146                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1147                                 n_lower++;
1148                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1149                                 n_upper++;
1150                 }
1151                 bmap = isl_basic_map_extend_constraints(bmap,
1152                                 0, n_lower * n_upper);
1153                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1154                         int last;
1155                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1156                                 continue;
1157                         last = -1;
1158                         for (j = 0; j < i; ++j) {
1159                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1160                                         continue;
1161                                 last = j;
1162                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1163                                     isl_int_sgn(bmap->ineq[j][1+d]))
1164                                         continue;
1165                                 k = isl_basic_map_alloc_inequality(bmap);
1166                                 if (k < 0)
1167                                         goto error;
1168                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1169                                                 1+total);
1170                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1171                                                 1+d, 1+total, NULL);
1172                         }
1173                         isl_basic_map_drop_inequality(bmap, i);
1174                         i = last + 1;
1175                 }
1176                 if (n_lower > 0 && n_upper > 0) {
1177                         bmap = isl_basic_map_normalize_constraints(bmap);
1178                         bmap = remove_duplicate_constraints(bmap, NULL);
1179                         bmap = isl_basic_map_gauss(bmap, NULL);
1180                         bmap = isl_basic_map_convex_hull(bmap);
1181                         if (!bmap)
1182                                 goto error;
1183                         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1184                                 break;
1185                 }
1186         }
1187         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1188         return bmap;
1189 error:
1190         isl_basic_map_free(bmap);
1191         return NULL;
1192 }
1193
1194 struct isl_basic_set *isl_basic_set_eliminate_vars(
1195         struct isl_basic_set *bset, unsigned pos, unsigned n)
1196 {
1197         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1198                         (struct isl_basic_map *)bset, pos, n);
1199 }
1200
1201 /* Don't assume equalities are in order, because align_divs
1202  * may have changed the order of the divs.
1203  */
1204 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
1205 {
1206         int d, i;
1207         unsigned total;
1208
1209         total = isl_dim_total(bmap->dim);
1210         for (d = 0; d < total; ++d)
1211                 elim[d] = -1;
1212         for (i = 0; i < bmap->n_eq; ++i) {
1213                 for (d = total - 1; d >= 0; --d) {
1214                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1215                                 continue;
1216                         elim[d] = i;
1217                         break;
1218                 }
1219         }
1220 }
1221
1222 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
1223 {
1224         return compute_elimination_index((struct isl_basic_map *)bset, elim);
1225 }
1226
1227 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1228         struct isl_basic_map *bmap, int *elim)
1229 {
1230         int d, i;
1231         int copied = 0;
1232         unsigned total;
1233
1234         total = isl_dim_total(bmap->dim);
1235         for (d = total - 1; d >= 0; --d) {
1236                 if (isl_int_is_zero(src[1+d]))
1237                         continue;
1238                 if (elim[d] == -1)
1239                         continue;
1240                 if (!copied) {
1241                         isl_seq_cpy(dst, src, 1 + total);
1242                         copied = 1;
1243                 }
1244                 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1245         }
1246         return copied;
1247 }
1248
1249 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1250         struct isl_basic_set *bset, int *elim)
1251 {
1252         return reduced_using_equalities(dst, src,
1253                                         (struct isl_basic_map *)bset, elim);
1254 }
1255
1256 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
1257         struct isl_basic_set *bset, struct isl_basic_set *context)
1258 {
1259         int i;
1260         int *elim;
1261
1262         if (!bset || !context)
1263                 goto error;
1264
1265         bset = isl_basic_set_cow(bset);
1266         if (!bset)
1267                 goto error;
1268
1269         elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
1270         if (!elim)
1271                 goto error;
1272         set_compute_elimination_index(context, elim);
1273         for (i = 0; i < bset->n_eq; ++i)
1274                 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1275                                                         context, elim);
1276         for (i = 0; i < bset->n_ineq; ++i)
1277                 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1278                                                         context, elim);
1279         isl_basic_set_free(context);
1280         free(elim);
1281         bset = isl_basic_set_simplify(bset);
1282         bset = isl_basic_set_finalize(bset);
1283         return bset;
1284 error:
1285         isl_basic_set_free(bset);
1286         isl_basic_set_free(context);
1287         return NULL;
1288 }
1289
1290 static struct isl_basic_set *remove_shifted_constraints(
1291         struct isl_basic_set *bset, struct isl_basic_set *context)
1292 {
1293         unsigned int size;
1294         isl_int ***index;
1295         int bits;
1296         int k, h, l;
1297
1298         if (!bset)
1299                 return NULL;
1300
1301         size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1302         bits = ffs(size) - 1;
1303         index = isl_calloc_array(ctx, isl_int **, size);
1304         if (!index)
1305                 return bset;
1306
1307         for (k = 0; k < context->n_ineq; ++k) {
1308                 h = set_hash_index(index, size, bits, context, k);
1309                 index[h] = &context->ineq[k];
1310         }
1311         for (k = 0; k < bset->n_ineq; ++k) {
1312                 h = set_hash_index(index, size, bits, bset, k);
1313                 if (!index[h])
1314                         continue;
1315                 l = index[h] - &context->ineq[0];
1316                 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1317                         continue;
1318                 bset = isl_basic_set_cow(bset);
1319                 if (!bset)
1320                         goto error;
1321                 isl_basic_set_drop_inequality(bset, k);
1322                 --k;
1323         }
1324         free(index);
1325         return bset;
1326 error:
1327         free(index);
1328         return bset;
1329 }
1330
1331 /* Tighten (decrease) the constant terms of the inequalities based
1332  * on the equalities, without removing any integer points.
1333  * For example, if there is an equality
1334  *
1335  *              i = 3 * j
1336  *
1337  * and an inequality
1338  *
1339  *              i >= 1
1340  *
1341  * then we want to replace the inequality by
1342  *
1343  *              i >= 3
1344  *
1345  * We do this by computing a variable compression and translating
1346  * the constraints to the compressed space.
1347  * If any constraint has coefficients (except the contant term)
1348  * with a common factor "f", then we can replace the constant term "c"
1349  * by
1350  *
1351  *              f * floor(c/f)
1352  *
1353  * That is, we add
1354  *
1355  *              f * floor(c/f) - c = -fract(c/f)
1356  *
1357  * and we can add the same value to the original constraint.
1358  *
1359  * In the example, the compressed space only contains "j",
1360  * and the inequality translates to
1361  *
1362  *              3 * j - 1 >= 0
1363  *
1364  * We add -fract(-1/3) = -2 to the original constraint to obtain
1365  *
1366  *              i - 3 >= 0
1367  */
1368 static struct isl_basic_set *normalize_constraints_in_compressed_space(
1369         struct isl_basic_set *bset)
1370 {
1371         int i;
1372         unsigned total;
1373         struct isl_mat *B, *C;
1374         isl_int gcd;
1375
1376         if (!bset)
1377                 return NULL;
1378
1379         if (ISL_F_ISSET(bset, ISL_BASIC_SET_RATIONAL))
1380                 return bset;
1381
1382         if (!bset->n_ineq)
1383                 return bset;
1384
1385         bset = isl_basic_set_cow(bset);
1386         if (!bset)
1387                 return NULL;
1388
1389         total = isl_basic_set_total_dim(bset);
1390         B = isl_mat_sub_alloc(bset->ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
1391         C = isl_mat_variable_compression(bset->ctx, B, NULL);
1392         if (!C)
1393                 return bset;
1394         if (C->n_col == 0) {
1395                 isl_mat_free(bset->ctx, C);
1396                 return isl_basic_set_set_to_empty(bset);
1397         }
1398         B = isl_mat_sub_alloc(bset->ctx, bset->ineq,
1399                                                 0, bset->n_ineq, 0, 1 + total);
1400         C = isl_mat_product(bset->ctx, B, C);
1401         if (!C)
1402                 return bset;
1403
1404         isl_int_init(gcd);
1405         for (i = 0; i < bset->n_ineq; ++i) {
1406                 isl_seq_gcd(C->row[i] + 1, C->n_col - 1, &gcd);
1407                 if (isl_int_is_one(gcd))
1408                         continue;
1409                 isl_int_fdiv_r(C->row[i][0], C->row[i][0], gcd);
1410                 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], C->row[i][0]);
1411         }
1412         isl_int_clear(gcd);
1413
1414         isl_mat_free(bset->ctx, C);
1415
1416         return bset;
1417 }
1418
1419 /* Remove all information from bset that is redundant in the context
1420  * of context.  In particular, equalities that are linear combinations
1421  * of those in context are removed.  Then the inequalities that are
1422  * redundant in the context of the equalities and inequalities of
1423  * context are removed.
1424  *
1425  * We first simplify the constraints of "bset" in the context of the
1426  * equalities of "context".
1427  * Then we simplify the inequalities of the context in the context
1428  * of the equalities of bset and remove the inequalities from "bset"
1429  * that are obviously redundant with respect to some inequality in "context".
1430  *
1431  * If there are any inequalities left, we construct a tableau for
1432  * the context and then add the inequalities of "bset".
1433  * Before adding these equalities, we freeze all constraints such that
1434  * they won't be considered redundant in terms of the constraints of "bset".
1435  * Then we detect all equalities and redundant constraints (among the
1436  * constraints that weren't frozen) and update bset according to the results.
1437  * We have to be careful here because we don't want any of the context
1438  * constraints to remain and because we haven't added the equalities of "bset"
1439  * to the tableau so we temporarily have to pretend that there were no
1440  * equalities.
1441  */
1442 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1443         struct isl_basic_set *context)
1444 {
1445         int i;
1446         struct isl_tab *tab;
1447         unsigned context_ineq;
1448         struct isl_basic_set *combined = NULL;
1449
1450         if (!context || !bset)
1451                 goto error;
1452
1453         if (context->n_eq > 0)
1454                 bset = isl_basic_set_reduce_using_equalities(bset,
1455                                         isl_basic_set_copy(context));
1456         if (!bset)
1457                 goto error;
1458         if (isl_basic_set_fast_is_empty(bset))
1459                 goto done;
1460         if (!bset->n_ineq)
1461                 goto done;
1462
1463         if (bset->n_eq > 0) {
1464                 struct isl_basic_set *affine_hull;
1465                 affine_hull = isl_basic_set_copy(bset);
1466                 affine_hull = isl_basic_set_cow(affine_hull);
1467                 if (!affine_hull)
1468                         goto error;
1469                 isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1470                 context = isl_basic_set_intersect(context, affine_hull);
1471                 context = isl_basic_set_gauss(context, NULL);
1472                 context = normalize_constraints_in_compressed_space(context);
1473         }
1474         if (!context)
1475                 goto error;
1476         if (ISL_F_ISSET(context, ISL_BASIC_SET_EMPTY)) {
1477                 isl_basic_set_free(bset);
1478                 return context;
1479         }
1480         if (!context->n_ineq)
1481                 goto done;
1482         bset = remove_shifted_constraints(bset, context);
1483         if (!bset->n_ineq)
1484                 goto done;
1485         isl_basic_set_free_equality(context, context->n_eq);
1486         context_ineq = context->n_ineq;
1487         combined = isl_basic_set_cow(isl_basic_set_copy(context));
1488         combined = isl_basic_set_extend_constraints(combined,
1489                                                     bset->n_eq, bset->n_ineq);
1490         tab = isl_tab_from_basic_set(combined);
1491         if (!tab)
1492                 goto error;
1493         for (i = 0; i < context_ineq; ++i)
1494                 tab->con[i].frozen = 1;
1495         tab = isl_tab_extend(bset->ctx, tab, bset->n_ineq);
1496         if (!tab)
1497                 goto error;
1498         for (i = 0; i < bset->n_ineq; ++i)
1499                 tab = isl_tab_add_ineq(bset->ctx, tab, bset->ineq[i]);
1500         bset = isl_basic_set_add_constraints(combined, bset, 0);
1501         tab = isl_tab_detect_equalities(bset->ctx, tab);
1502         tab = isl_tab_detect_redundant(bset->ctx, tab);
1503         if (!tab)
1504                 goto error2;
1505         for (i = 0; i < context_ineq; ++i) {
1506                 tab->con[i].is_zero = 0;
1507                 tab->con[i].is_redundant = 1;
1508         }
1509         bset = isl_basic_set_update_from_tab(bset, tab);
1510         isl_tab_free(bset->ctx, tab);
1511         ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
1512         ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
1513 done:
1514         bset = isl_basic_set_simplify(bset);
1515         bset = isl_basic_set_finalize(bset);
1516         isl_basic_set_free(context);
1517         return bset;
1518 error:
1519         isl_basic_set_free(combined);
1520 error2:
1521         isl_basic_set_free(bset);
1522         isl_basic_set_free(context);
1523         return NULL;
1524 }
1525
1526 /* Normalize the divs in "bmap" in the context of the equalities in "context".
1527  * We simply add the equalities in context to bmap and then do a regular
1528  * div normalizations.  Better results can be obtained by normalizing
1529  * only the divs in bmap than do not also appear in context.
1530  * We need to be careful to reduce the divs using the equalities
1531  * so that later calls to isl_basic_map_overlying_set wouldn't introduce
1532  * spurious constraints.
1533  */
1534 static struct isl_basic_map *normalize_divs_in_context(
1535         struct isl_basic_map *bmap, struct isl_basic_map *context)
1536 {
1537         int i;
1538         unsigned total_context;
1539         int div_eq;
1540
1541         div_eq = n_pure_div_eq(bmap);
1542         if (div_eq == 0)
1543                 return bmap;
1544
1545         if (context->n_div > 0)
1546                 bmap = isl_basic_map_align_divs(bmap, context);
1547
1548         total_context = isl_basic_map_total_dim(context);
1549         bmap = isl_basic_map_extend_constraints(bmap, context->n_eq, 0);
1550         for (i = 0; i < context->n_eq; ++i) {
1551                 int k;
1552                 k = isl_basic_map_alloc_equality(bmap);
1553                 isl_seq_cpy(bmap->eq[k], context->eq[i], 1 + total_context);
1554                 isl_seq_clr(bmap->eq[k] + 1 + total_context,
1555                                 isl_basic_map_total_dim(bmap) - total_context);
1556         }
1557         bmap = isl_basic_map_gauss(bmap, NULL);
1558         bmap = normalize_divs(bmap, NULL);
1559         bmap = isl_basic_map_gauss(bmap, NULL);
1560         return bmap;
1561 }
1562
1563 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1564         struct isl_basic_map *context)
1565 {
1566         struct isl_basic_set *bset;
1567
1568         if (!bmap || !context)
1569                 goto error;
1570
1571         if (isl_basic_map_is_universe(context)) {
1572                 isl_basic_map_free(context);
1573                 return bmap;
1574         }
1575         if (isl_basic_map_is_universe(bmap)) {
1576                 isl_basic_map_free(context);
1577                 return bmap;
1578         }
1579         if (isl_basic_map_fast_is_empty(context)) {
1580                 struct isl_dim *dim = isl_dim_copy(bmap->dim);
1581                 isl_basic_map_free(context);
1582                 isl_basic_map_free(bmap);
1583                 return isl_basic_map_universe(dim);
1584         }
1585         if (isl_basic_map_fast_is_empty(bmap)) {
1586                 isl_basic_map_free(context);
1587                 return bmap;
1588         }
1589
1590         bmap = isl_basic_map_convex_hull(bmap);
1591         context = isl_basic_map_convex_hull(context);
1592
1593         if (context->n_eq)
1594                 bmap = normalize_divs_in_context(bmap, context);
1595
1596         context = isl_basic_map_align_divs(context, bmap);
1597         bmap = isl_basic_map_align_divs(bmap, context);
1598
1599         bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1600                          isl_basic_map_underlying_set(context));
1601
1602         return isl_basic_map_overlying_set(bset, bmap);
1603 error:
1604         isl_basic_map_free(bmap);
1605         isl_basic_map_free(context);
1606         return NULL;
1607 }
1608
1609 /*
1610  * Assumes context has no implicit divs.
1611  */
1612 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1613 {
1614         int i;
1615
1616         if (!map || !context)
1617                 goto error;;
1618
1619         if (isl_basic_map_is_universe(context)) {
1620                 isl_basic_map_free(context);
1621                 return map;
1622         }
1623         if (isl_basic_map_fast_is_empty(context)) {
1624                 struct isl_dim *dim = isl_dim_copy(map->dim);
1625                 isl_basic_map_free(context);
1626                 isl_map_free(map);
1627                 return isl_map_universe(dim);
1628         }
1629
1630         context = isl_basic_map_convex_hull(context);
1631         map = isl_map_cow(map);
1632         if (!map || !context)
1633                 goto error;;
1634         isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1635         map = isl_map_compute_divs(map);
1636         for (i = 0; i < map->n; ++i)
1637                 context = isl_basic_map_align_divs(context, map->p[i]);
1638         for (i = 0; i < map->n; ++i) {
1639                 map->p[i] = isl_basic_map_gist(map->p[i],
1640                                                 isl_basic_map_copy(context));
1641                 if (!map->p[i])
1642                         goto error;
1643         }
1644         isl_basic_map_free(context);
1645         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
1646         return map;
1647 error:
1648         isl_map_free(map);
1649         isl_basic_map_free(context);
1650         return NULL;
1651 }
1652
1653 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1654                                                 struct isl_basic_set *context)
1655 {
1656         return (struct isl_basic_set *)isl_basic_map_gist(
1657                 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1658 }
1659
1660 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1661 {
1662         return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1663                                         (struct isl_basic_map *)context);
1664 }
1665
1666 /* Quick check to see if two basic maps are disjoint.
1667  * In particular, we reduce the equalities and inequalities of
1668  * one basic map in the context of the equalities of the other
1669  * basic map and check if we get a contradiction.
1670  */
1671 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1672         struct isl_basic_map *bmap2)
1673 {
1674         struct isl_vec *v = NULL;
1675         int *elim = NULL;
1676         unsigned total;
1677         int d, i;
1678
1679         if (!bmap1 || !bmap2)
1680                 return -1;
1681         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1682                         return -1);
1683         if (bmap1->n_div || bmap2->n_div)
1684                 return 0;
1685         if (!bmap1->n_eq && !bmap2->n_eq)
1686                 return 0;
1687
1688         total = isl_dim_total(bmap1->dim);
1689         if (total == 0)
1690                 return 0;
1691         v = isl_vec_alloc(bmap1->ctx, 1 + total);
1692         if (!v)
1693                 goto error;
1694         elim = isl_alloc_array(bmap1->ctx, int, total);
1695         if (!elim)
1696                 goto error;
1697         compute_elimination_index(bmap1, elim);
1698         for (i = 0; i < bmap2->n_eq; ++i) {
1699                 int reduced;
1700                 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1701                                                         bmap1, elim);
1702                 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1703                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1704                         goto disjoint;
1705         }
1706         for (i = 0; i < bmap2->n_ineq; ++i) {
1707                 int reduced;
1708                 reduced = reduced_using_equalities(v->block.data,
1709                                                 bmap2->ineq[i], bmap1, elim);
1710                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1711                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1712                         goto disjoint;
1713         }
1714         compute_elimination_index(bmap2, elim);
1715         for (i = 0; i < bmap1->n_ineq; ++i) {
1716                 int reduced;
1717                 reduced = reduced_using_equalities(v->block.data,
1718                                                 bmap1->ineq[i], bmap2, elim);
1719                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1720                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1721                         goto disjoint;
1722         }
1723         isl_vec_free(bmap1->ctx, v);
1724         free(elim);
1725         return 0;
1726 disjoint:
1727         isl_vec_free(bmap1->ctx, v);
1728         free(elim);
1729         return 1;
1730 error:
1731         isl_vec_free(bmap1->ctx, v);
1732         free(elim);
1733         return -1;
1734 }
1735
1736 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1737         struct isl_basic_set *bset2)
1738 {
1739         return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1740                                               (struct isl_basic_map *)bset2);
1741 }
1742
1743 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1744 {
1745         int i, j;
1746
1747         if (!map1 || !map2)
1748                 return -1;
1749
1750         if (isl_map_fast_is_equal(map1, map2))
1751                 return 0;
1752
1753         for (i = 0; i < map1->n; ++i) {
1754                 for (j = 0; j < map2->n; ++j) {
1755                         int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1756                                                                map2->p[j]);
1757                         if (d != 1)
1758                                 return d;
1759                 }
1760         }
1761         return 1;
1762 }
1763
1764 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1765 {
1766         return isl_map_fast_is_disjoint((struct isl_map *)set1,
1767                                         (struct isl_map *)set2);
1768 }