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