isl_map_simplify.c: extract out simplify and related code from isl_map.c
[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
5 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
6 {
7         isl_int *t = bmap->eq[a];
8         bmap->eq[a] = bmap->eq[b];
9         bmap->eq[b] = t;
10 }
11
12 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
13 {
14         if (a != b) {
15                 isl_int *t = bmap->ineq[a];
16                 bmap->ineq[a] = bmap->ineq[b];
17                 bmap->ineq[b] = t;
18         }
19 }
20
21 static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
22 {
23         swap_inequality((struct isl_basic_map *)bset, a, b);
24 }
25
26 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
27 {
28         isl_seq_cpy(c, c + n, rem);
29         isl_seq_clr(c + rem, n);
30 }
31
32 /* Drop n dimensions starting at first.
33  *
34  * In principle, this frees up some extra variables as the number
35  * of columns remains constant, but we would have to extend
36  * the div array too as the number of rows in this array is assumed
37  * to be equal to extra.
38  */
39 struct isl_basic_set *isl_basic_set_drop_dims(
40                 struct isl_basic_set *bset, unsigned first, unsigned n)
41 {
42         int i;
43
44         if (!bset)
45                 goto error;
46
47         isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
48
49         if (n == 0)
50                 return bset;
51
52         bset = isl_basic_set_cow(bset);
53         if (!bset)
54                 return NULL;
55
56         for (i = 0; i < bset->n_eq; ++i)
57                 constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
58                                      (bset->dim->n_out-first-n)+bset->extra);
59
60         for (i = 0; i < bset->n_ineq; ++i)
61                 constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
62                                      (bset->dim->n_out-first-n)+bset->extra);
63
64         for (i = 0; i < bset->n_div; ++i)
65                 constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
66                                      (bset->dim->n_out-first-n)+bset->extra);
67
68         bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
69         if (!bset->dim)
70                 goto error;
71
72         F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
73         bset = isl_basic_set_simplify(bset);
74         return isl_basic_set_finalize(bset);
75 error:
76         isl_basic_set_free(bset);
77         return NULL;
78 }
79
80 struct isl_set *isl_set_drop_dims(
81                 struct isl_set *set, unsigned first, unsigned n)
82 {
83         int i;
84
85         if (!set)
86                 goto error;
87
88         isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
89
90         if (n == 0)
91                 return set;
92         set = isl_set_cow(set);
93         if (!set)
94                 goto error;
95         set->dim = isl_dim_drop_outputs(set->dim, first, n);
96         if (!set->dim)
97                 goto error;
98
99         for (i = 0; i < set->n; ++i) {
100                 set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
101                 if (!set->p[i])
102                         goto error;
103         }
104
105         F_CLR(set, ISL_SET_NORMALIZED);
106         return set;
107 error:
108         isl_set_free(set);
109         return NULL;
110 }
111
112 /* Drop n input dimensions starting at first.
113  *
114  * In principle, this frees up some extra variables as the number
115  * of columns remains constant, but we would have to extend
116  * the div array too as the number of rows in this array is assumed
117  * to be equal to extra.
118  */
119 struct isl_basic_map *isl_basic_map_drop_inputs(
120                 struct isl_basic_map *bmap, unsigned first, unsigned n)
121 {
122         int i;
123         unsigned nparam;
124         unsigned n_in;
125         unsigned n_out;
126
127         if (!bmap)
128                 goto error;
129
130         nparam = isl_basic_map_n_param(bmap);
131         n_in = isl_basic_map_n_in(bmap);
132         n_out = isl_basic_map_n_out(bmap);
133         isl_assert(bmap->ctx, first + n <= n_in, goto error);
134
135         if (n == 0)
136                 return bmap;
137
138         bmap = isl_basic_map_cow(bmap);
139         if (!bmap)
140                 return NULL;
141
142         for (i = 0; i < bmap->n_eq; ++i)
143                 constraint_drop_vars(bmap->eq[i]+1+nparam+first, n,
144                                  (n_in-first-n)+n_out+bmap->extra);
145
146         for (i = 0; i < bmap->n_ineq; ++i)
147                 constraint_drop_vars(bmap->ineq[i]+1+nparam+first, n,
148                                  (n_in-first-n)+n_out+bmap->extra);
149
150         for (i = 0; i < bmap->n_div; ++i)
151                 constraint_drop_vars(bmap->div[i]+1+1+nparam+first, n,
152                                  (n_in-first-n)+n_out+bmap->extra);
153
154         bmap->dim = isl_dim_drop_inputs(bmap->dim, first, n);
155         if (!bmap->dim)
156                 goto error;
157
158         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
159         bmap = isl_basic_map_simplify(bmap);
160         return isl_basic_map_finalize(bmap);
161 error:
162         isl_basic_map_free(bmap);
163         return NULL;
164 }
165
166 struct isl_map *isl_map_drop_inputs(
167                 struct isl_map *map, unsigned first, unsigned n)
168 {
169         int i;
170
171         if (!map)
172                 goto error;
173
174         isl_assert(map->ctx, first + n <= map->dim->n_in, goto error);
175
176         if (n == 0)
177                 return map;
178         map = isl_map_cow(map);
179         if (!map)
180                 goto error;
181         map->dim = isl_dim_drop_inputs(map->dim, first, n);
182         if (!map->dim)
183                 goto error;
184
185         for (i = 0; i < map->n; ++i) {
186                 map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
187                 if (!map->p[i])
188                         goto error;
189         }
190         F_CLR(map, ISL_MAP_NORMALIZED);
191
192         return map;
193 error:
194         isl_map_free(map);
195         return NULL;
196 }
197
198 /*
199  * We don't cow, as the div is assumed to be redundant.
200  */
201 static struct isl_basic_map *isl_basic_map_drop_div(
202                 struct isl_basic_map *bmap, unsigned div)
203 {
204         int i;
205         unsigned pos;
206
207         if (!bmap)
208                 goto error;
209
210         pos = 1 + isl_dim_total(bmap->dim) + div;
211
212         isl_assert(bmap->ctx, div < bmap->n_div, goto error);
213
214         for (i = 0; i < bmap->n_eq; ++i)
215                 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
216
217         for (i = 0; i < bmap->n_ineq; ++i) {
218                 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
219                         isl_basic_map_drop_inequality(bmap, i);
220                         --i;
221                         continue;
222                 }
223                 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
224         }
225
226         for (i = 0; i < bmap->n_div; ++i)
227                 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
228
229         if (div != bmap->n_div - 1) {
230                 int j;
231                 isl_int *t = bmap->div[div];
232
233                 for (j = div; j < bmap->n_div - 1; ++j)
234                         bmap->div[j] = bmap->div[j+1];
235
236                 bmap->div[bmap->n_div - 1] = t;
237         }
238         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
239         isl_basic_map_free_div(bmap, 1);
240
241         return bmap;
242 error:
243         isl_basic_map_free(bmap);
244         return NULL;
245 }
246
247 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
248 {
249         int i;
250         isl_int gcd;
251         unsigned total = isl_basic_map_total_dim(bmap);
252
253         isl_int_init(gcd);
254         for (i = bmap->n_eq - 1; i >= 0; --i) {
255                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
256                 if (isl_int_is_zero(gcd)) {
257                         if (!isl_int_is_zero(bmap->eq[i][0])) {
258                                 bmap = isl_basic_map_set_to_empty(bmap);
259                                 break;
260                         }
261                         isl_basic_map_drop_equality(bmap, i);
262                         continue;
263                 }
264                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
265                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
266                 if (isl_int_is_one(gcd))
267                         continue;
268                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
269                         bmap = isl_basic_map_set_to_empty(bmap);
270                         break;
271                 }
272                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
273         }
274
275         for (i = bmap->n_ineq - 1; i >= 0; --i) {
276                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
277                 if (isl_int_is_zero(gcd)) {
278                         if (isl_int_is_neg(bmap->ineq[i][0])) {
279                                 bmap = isl_basic_map_set_to_empty(bmap);
280                                 break;
281                         }
282                         isl_basic_map_drop_inequality(bmap, i);
283                         continue;
284                 }
285                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
286                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
287                 if (isl_int_is_one(gcd))
288                         continue;
289                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
290                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
291         }
292         isl_int_clear(gcd);
293
294         return bmap;
295 }
296
297 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
298 {
299         int i;
300         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
301         unsigned len;
302         len = 1 + isl_basic_map_total_dim(bmap);
303
304         for (i = 0; i < bmap->n_eq; ++i)
305                 if (bmap->eq[i] != eq)
306                         isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
307
308         for (i = 0; i < bmap->n_ineq; ++i)
309                 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
310
311         /* We need to be careful about circular definitions,
312          * so for now we just remove the definitions of other divs that
313          * depend on this div and (possibly) recompute them later.
314          */
315         for (i = 0; i < bmap->n_div; ++i)
316                 if (!isl_int_is_zero(bmap->div[i][0]) &&
317                     !isl_int_is_zero(bmap->div[i][1 + pos]))
318                         isl_seq_clr(bmap->div[i], 1 + len);
319
320         isl_basic_map_drop_div(bmap, div);
321 }
322
323 /* Elimininate divs based on equalities
324  */
325 static struct isl_basic_map *eliminate_divs_eq(
326                 struct isl_basic_map *bmap, int *progress)
327 {
328         int d;
329         int i;
330         int modified = 0;
331         unsigned off;
332
333         if (!bmap)
334                 return NULL;
335
336         off = 1 + isl_dim_total(bmap->dim);
337
338         for (d = bmap->n_div - 1; d >= 0 ; --d) {
339                 for (i = 0; i < bmap->n_eq; ++i) {
340                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
341                             !isl_int_is_negone(bmap->eq[i][off + d]))
342                                 continue;
343                         modified = 1;
344                         *progress = 1;
345                         eliminate_div(bmap, bmap->eq[i], d);
346                         isl_basic_map_drop_equality(bmap, i);
347                         break;
348                 }
349         }
350         if (modified)
351                 return eliminate_divs_eq(bmap, progress);
352         return bmap;
353 }
354
355 /* Elimininate divs based on inequalities
356  */
357 static struct isl_basic_map *eliminate_divs_ineq(
358                 struct isl_basic_map *bmap, int *progress)
359 {
360         int d;
361         int i;
362         unsigned off;
363         struct isl_ctx *ctx;
364
365         if (!bmap)
366                 return NULL;
367
368         ctx = bmap->ctx;
369         off = 1 + isl_dim_total(bmap->dim);
370
371         for (d = bmap->n_div - 1; d >= 0 ; --d) {
372                 for (i = 0; i < bmap->n_eq; ++i)
373                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
374                                 break;
375                 if (i < bmap->n_eq)
376                         continue;
377                 for (i = 0; i < bmap->n_ineq; ++i)
378                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
379                                 break;
380                 if (i < bmap->n_ineq)
381                         continue;
382                 *progress = 1;
383                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
384                 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
385                         break;
386                 bmap = isl_basic_map_drop_div(bmap, d);
387                 if (!bmap)
388                         break;
389         }
390         return bmap;
391 }
392
393 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
394         unsigned pos, isl_int *eq, int *progress)
395 {
396         unsigned total;
397         int k;
398
399         total = isl_basic_map_total_dim(bmap);
400         for (k = 0; k < bmap->n_eq; ++k) {
401                 if (bmap->eq[k] == eq)
402                         continue;
403                 if (isl_int_is_zero(bmap->eq[k][1+pos]))
404                         continue;
405                 if (progress)
406                         *progress = 1;
407                 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
408         }
409
410         for (k = 0; k < bmap->n_ineq; ++k) {
411                 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
412                         continue;
413                 if (progress)
414                         *progress = 1;
415                 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
416                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
417         }
418
419         for (k = 0; k < bmap->n_div; ++k) {
420                 if (isl_int_is_zero(bmap->div[k][0]))
421                         continue;
422                 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
423                         continue;
424                 if (progress)
425                         *progress = 1;
426                 isl_seq_elim(bmap->div[k]+1, eq,
427                                 1+pos, 1+total, &bmap->div[k][0]);
428                 F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
429         }
430 }
431
432 struct isl_basic_map *isl_basic_map_gauss(
433         struct isl_basic_map *bmap, int *progress)
434 {
435         int k;
436         int done;
437         int last_var;
438         unsigned total_var;
439         unsigned total;
440
441         if (!bmap)
442                 return NULL;
443
444         total = isl_basic_map_total_dim(bmap);
445         total_var = total - bmap->n_div;
446
447         last_var = total - 1;
448         for (done = 0; done < bmap->n_eq; ++done) {
449                 for (; last_var >= 0; --last_var) {
450                         for (k = done; k < bmap->n_eq; ++k)
451                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
452                                         break;
453                         if (k < bmap->n_eq)
454                                 break;
455                 }
456                 if (last_var < 0)
457                         break;
458                 if (k != done)
459                         swap_equality(bmap, k, done);
460                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
461                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
462
463                 eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
464                                                 progress);
465
466                 if (last_var >= total_var &&
467                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
468                         unsigned div = last_var - total_var;
469                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
470                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
471                         isl_int_set(bmap->div[div][0],
472                                     bmap->eq[done][1+last_var]);
473                         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
474                 }
475         }
476         if (done == bmap->n_eq)
477                 return bmap;
478         for (k = done; k < bmap->n_eq; ++k) {
479                 if (isl_int_is_zero(bmap->eq[k][0]))
480                         continue;
481                 return isl_basic_map_set_to_empty(bmap);
482         }
483         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
484         return bmap;
485 }
486
487 struct isl_basic_set *isl_basic_set_gauss(
488         struct isl_basic_set *bset, int *progress)
489 {
490         return (struct isl_basic_set*)isl_basic_map_gauss(
491                         (struct isl_basic_map *)bset, progress);
492 }
493
494
495 static unsigned int round_up(unsigned int v)
496 {
497         int old_v = v;
498
499         while (v) {
500                 old_v = v;
501                 v ^= v & -v;
502         }
503         return old_v << 1;
504 }
505
506 static int hash_index(isl_int ***index, unsigned int size, int bits,
507                         struct isl_basic_map *bmap, int k)
508 {
509         int h;
510         unsigned total = isl_basic_map_total_dim(bmap);
511         uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
512         for (h = hash; index[h]; h = (h+1) % size)
513                 if (&bmap->ineq[k] != index[h] &&
514                     isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
515                         break;
516         return h;
517 }
518
519 static int set_hash_index(isl_int ***index, unsigned int size, int bits,
520                           struct isl_basic_set *bset, int k)
521 {
522         return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
523 }
524
525 static struct isl_basic_map *remove_duplicate_divs(
526         struct isl_basic_map *bmap, int *progress)
527 {
528         unsigned int size;
529         int *index;
530         int k, l, h;
531         int bits;
532         struct isl_blk eq;
533         unsigned total_var = isl_dim_total(bmap->dim);
534         unsigned total = total_var + bmap->n_div;
535         struct isl_ctx *ctx;
536
537         if (bmap->n_div <= 1)
538                 return bmap;
539
540         ctx = bmap->ctx;
541         for (k = bmap->n_div - 1; k >= 0; --k)
542                 if (!isl_int_is_zero(bmap->div[k][0]))
543                         break;
544         if (k <= 0)
545                 return bmap;
546
547         size = round_up(4 * bmap->n_div / 3 - 1);
548         bits = ffs(size) - 1;
549         index = isl_calloc_array(ctx, int, size);
550         if (!index)
551                 return bmap;
552         eq = isl_blk_alloc(ctx, 1+total);
553         if (isl_blk_is_error(eq))
554                 goto out;
555
556         isl_seq_clr(eq.data, 1+total);
557         index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
558         for (--k; k >= 0; --k) {
559                 uint32_t hash;
560
561                 if (isl_int_is_zero(bmap->div[k][0]))
562                         continue;
563
564                 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
565                 for (h = hash; index[h]; h = (h+1) % size)
566                         if (isl_seq_eq(bmap->div[k],
567                                        bmap->div[index[h]-1], 2+total))
568                                 break;
569                 if (index[h]) {
570                         *progress = 1;
571                         l = index[h] - 1;
572                         isl_int_set_si(eq.data[1+total_var+k], -1);
573                         isl_int_set_si(eq.data[1+total_var+l], 1);
574                         eliminate_div(bmap, eq.data, l);
575                         isl_int_set_si(eq.data[1+total_var+k], 0);
576                         isl_int_set_si(eq.data[1+total_var+l], 0);
577                 }
578                 index[h] = k+1;
579         }
580
581         isl_blk_free(ctx, eq);
582 out:
583         free(index);
584         return bmap;
585 }
586
587 static struct isl_basic_map *remove_duplicate_constraints(
588         struct isl_basic_map *bmap, int *progress)
589 {
590         unsigned int size;
591         isl_int ***index;
592         int k, l, h;
593         int bits;
594         unsigned total = isl_basic_map_total_dim(bmap);
595         isl_int sum;
596
597         if (bmap->n_ineq <= 1)
598                 return bmap;
599
600         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
601         bits = ffs(size) - 1;
602         index = isl_calloc_array(ctx, isl_int **, size);
603         if (!index)
604                 return bmap;
605
606         index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
607         for (k = 1; k < bmap->n_ineq; ++k) {
608                 h = hash_index(index, size, bits, bmap, k);
609                 if (!index[h]) {
610                         index[h] = &bmap->ineq[k];
611                         continue;
612                 }
613                 if (progress)
614                         *progress = 1;
615                 l = index[h] - &bmap->ineq[0];
616                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
617                         swap_inequality(bmap, k, l);
618                 isl_basic_map_drop_inequality(bmap, k);
619                 --k;
620         }
621         isl_int_init(sum);
622         for (k = 0; k < bmap->n_ineq-1; ++k) {
623                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
624                 h = hash_index(index, size, bits, bmap, k);
625                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
626                 if (!index[h])
627                         continue;
628                 l = index[h] - &bmap->ineq[0];
629                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
630                 if (isl_int_is_pos(sum))
631                         continue;
632                 if (isl_int_is_zero(sum)) {
633                         /* We need to break out of the loop after these
634                          * changes since the contents of the hash
635                          * will no longer be valid.
636                          * Plus, we probably we want to regauss first.
637                          */
638                         isl_basic_map_drop_inequality(bmap, l);
639                         isl_basic_map_inequality_to_equality(bmap, k);
640                 } else
641                         bmap = isl_basic_map_set_to_empty(bmap);
642                 break;
643         }
644         isl_int_clear(sum);
645
646         free(index);
647         return bmap;
648 }
649
650
651 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
652 {
653         int progress = 1;
654         if (!bmap)
655                 return NULL;
656         while (progress) {
657                 progress = 0;
658                 bmap = normalize_constraints(bmap);
659                 bmap = eliminate_divs_eq(bmap, &progress);
660                 bmap = eliminate_divs_ineq(bmap, &progress);
661                 bmap = isl_basic_map_gauss(bmap, &progress);
662                 bmap = remove_duplicate_divs(bmap, &progress);
663                 bmap = remove_duplicate_constraints(bmap, &progress);
664         }
665         return bmap;
666 }
667
668 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
669 {
670         return (struct isl_basic_set *)
671                 isl_basic_map_simplify((struct isl_basic_map *)bset);
672 }
673
674
675 /* If the only constraints a div d=floor(f/m)
676  * appears in are its two defining constraints
677  *
678  *      f - m d >=0
679  *      -(f - (m - 1)) + m d >= 0
680  *
681  * then it can safely be removed.
682  */
683 static int div_is_redundant(struct isl_basic_map *bmap, int div)
684 {
685         int i;
686         unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
687
688         for (i = 0; i < bmap->n_eq; ++i)
689                 if (!isl_int_is_zero(bmap->eq[i][pos]))
690                         return 0;
691
692         for (i = 0; i < bmap->n_ineq; ++i) {
693                 if (isl_int_is_zero(bmap->ineq[i][pos]))
694                         continue;
695                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
696                         int neg;
697                         isl_int_sub(bmap->div[div][1],
698                                         bmap->div[div][1], bmap->div[div][0]);
699                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
700                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
701                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
702                         isl_int_add(bmap->div[div][1],
703                                         bmap->div[div][1], bmap->div[div][0]);
704                         if (!neg)
705                                 return 0;
706                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
707                                                     bmap->n_div-div-1) != -1)
708                                 return 0;
709                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
710                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
711                                 return 0;
712                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
713                                                     bmap->n_div-div-1) != -1)
714                                 return 0;
715                 } else
716                         return 0;
717         }
718
719         for (i = 0; i < bmap->n_div; ++i)
720                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
721                         return 0;
722
723         return 1;
724 }
725
726 /*
727  * Remove divs that don't occur in any of the constraints or other divs.
728  * These can arise when dropping some of the variables in a quast
729  * returned by piplib.
730  */
731 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
732 {
733         int i;
734
735         if (!bmap)
736                 return NULL;
737
738         for (i = bmap->n_div-1; i >= 0; --i) {
739                 if (!div_is_redundant(bmap, i))
740                         continue;
741                 bmap = isl_basic_map_drop_div(bmap, i);
742         }
743         return bmap;
744 }
745
746 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
747 {
748         bmap = remove_redundant_divs(bmap);
749         if (!bmap)
750                 return NULL;
751         F_SET(bmap, ISL_BASIC_SET_FINAL);
752         return bmap;
753 }
754
755 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
756 {
757         return (struct isl_basic_set *)
758                 isl_basic_map_finalize((struct isl_basic_map *)bset);
759 }
760
761 struct isl_set *isl_set_finalize(struct isl_set *set)
762 {
763         int i;
764
765         if (!set)
766                 return NULL;
767         for (i = 0; i < set->n; ++i) {
768                 set->p[i] = isl_basic_set_finalize(set->p[i]);
769                 if (!set->p[i])
770                         goto error;
771         }
772         return set;
773 error:
774         isl_set_free(set);
775         return NULL;
776 }
777
778 struct isl_map *isl_map_finalize(struct isl_map *map)
779 {
780         int i;
781
782         if (!map)
783                 return NULL;
784         for (i = 0; i < map->n; ++i) {
785                 map->p[i] = isl_basic_map_finalize(map->p[i]);
786                 if (!map->p[i])
787                         goto error;
788         }
789         F_CLR(map, ISL_MAP_NORMALIZED);
790         return map;
791 error:
792         isl_map_free(map);
793         return NULL;
794 }
795
796
797 /* Remove any div that is defined in terms of the given variable.
798  */
799 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
800                                                                         int pos)
801 {
802         int i;
803         unsigned dim = isl_dim_total(bmap->dim);
804
805         for (i = 0; i < bmap->n_div; ++i) {
806                 if (isl_int_is_zero(bmap->div[i][0]))
807                         continue;
808                 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
809                         continue;
810                 bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
811                 if (!bmap)
812                         return NULL;
813         }
814         return bmap;
815 }
816
817 /* Eliminate the specified variables from the constraints using
818  * Fourier-Motzkin.  The variables themselves are not removed.
819  */
820 struct isl_basic_map *isl_basic_map_eliminate_vars(
821         struct isl_basic_map *bmap, unsigned pos, unsigned n)
822 {
823         int d;
824         int i, j, k;
825         unsigned total;
826
827         if (n == 0)
828                 return bmap;
829         if (!bmap)
830                 return NULL;
831         total = isl_basic_map_total_dim(bmap);
832
833         bmap = isl_basic_map_cow(bmap);
834         for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
835                 int n_lower, n_upper;
836                 bmap = remove_dependent_vars(bmap, d);
837                 if (!bmap)
838                         return NULL;
839                 if (d >= total - bmap->n_div)
840                         isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
841                 for (i = 0; i < bmap->n_eq; ++i) {
842                         if (isl_int_is_zero(bmap->eq[i][1+d]))
843                                 continue;
844                         eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
845                         isl_basic_map_drop_equality(bmap, i);
846                         break;
847                 }
848                 if (i < bmap->n_eq)
849                         continue;
850                 n_lower = 0;
851                 n_upper = 0;
852                 for (i = 0; i < bmap->n_ineq; ++i) {
853                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
854                                 n_lower++;
855                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
856                                 n_upper++;
857                 }
858                 bmap = isl_basic_map_extend_constraints(bmap,
859                                 0, n_lower * n_upper);
860                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
861                         int last;
862                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
863                                 continue;
864                         last = -1;
865                         for (j = 0; j < i; ++j) {
866                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
867                                         continue;
868                                 last = j;
869                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
870                                     isl_int_sgn(bmap->ineq[j][1+d]))
871                                         continue;
872                                 k = isl_basic_map_alloc_inequality(bmap);
873                                 if (k < 0)
874                                         goto error;
875                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
876                                                 1+total);
877                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
878                                                 1+d, 1+total, NULL);
879                         }
880                         isl_basic_map_drop_inequality(bmap, i);
881                         i = last + 1;
882                 }
883                 if (n_lower > 0 && n_upper > 0) {
884                         bmap = normalize_constraints(bmap);
885                         bmap = remove_duplicate_constraints(bmap, NULL);
886                         bmap = isl_basic_map_gauss(bmap, NULL);
887                         bmap = isl_basic_map_convex_hull(bmap);
888                         if (!bmap)
889                                 goto error;
890                         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
891                                 break;
892                 }
893         }
894         F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
895         return bmap;
896 error:
897         isl_basic_map_free(bmap);
898         return NULL;
899 }
900
901 struct isl_basic_set *isl_basic_set_eliminate_vars(
902         struct isl_basic_set *bset, unsigned pos, unsigned n)
903 {
904         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
905                         (struct isl_basic_map *)bset, pos, n);
906 }
907
908 static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
909 {
910         int d, i;
911         unsigned total;
912
913         total = isl_dim_total(bmap->dim);
914         for (d = 0; d < total; ++d)
915                 elim[d] = -1;
916         for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
917                 for (; d >= 0; --d) {
918                         if (isl_int_is_zero(bmap->eq[i][1+d]))
919                                 continue;
920                         elim[d] = i;
921                         break;
922                 }
923         }
924 }
925
926 static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
927 {
928         return compute_elimination_index((struct isl_basic_map *)bset, elim);
929 }
930
931 static int reduced_using_equalities(isl_int *dst, isl_int *src,
932         struct isl_basic_map *bmap, int *elim)
933 {
934         int d, i;
935         int copied = 0;
936         unsigned total;
937
938         total = isl_dim_total(bmap->dim);
939         for (d = total - 1; d >= 0; --d) {
940                 if (isl_int_is_zero(src[1+d]))
941                         continue;
942                 if (elim[d] == -1)
943                         continue;
944                 if (!copied) {
945                         isl_seq_cpy(dst, src, 1 + total);
946                         copied = 1;
947                 }
948                 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
949         }
950         return copied;
951 }
952
953 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
954         struct isl_basic_set *bset, int *elim)
955 {
956         return reduced_using_equalities(dst, src,
957                                         (struct isl_basic_map *)bset, elim);
958 }
959
960 static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
961         struct isl_basic_set *bset, struct isl_basic_set *context)
962 {
963         int i;
964         int *elim;
965
966         if (!bset || !context)
967                 goto error;
968
969         bset = isl_basic_set_cow(bset);
970         if (!bset)
971                 goto error;
972
973         elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
974         if (!elim)
975                 goto error;
976         set_compute_elimination_index(context, elim);
977         for (i = 0; i < bset->n_eq; ++i)
978                 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
979                                                         context, elim);
980         for (i = 0; i < bset->n_ineq; ++i)
981                 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
982                                                         context, elim);
983         isl_basic_set_free(context);
984         free(elim);
985         bset = isl_basic_set_simplify(bset);
986         bset = isl_basic_set_finalize(bset);
987         return bset;
988 error:
989         isl_basic_set_free(bset);
990         isl_basic_set_free(context);
991         return NULL;
992 }
993
994 static struct isl_basic_set *remove_shifted_constraints(
995         struct isl_basic_set *bset, struct isl_basic_set *context)
996 {
997         unsigned int size;
998         isl_int ***index;
999         int bits;
1000         int k, h, l;
1001
1002         if (!bset)
1003                 return NULL;
1004
1005         size = round_up(4 * (context->n_ineq+1) / 3 - 1);
1006         bits = ffs(size) - 1;
1007         index = isl_calloc_array(ctx, isl_int **, size);
1008         if (!index)
1009                 return bset;
1010
1011         for (k = 0; k < context->n_ineq; ++k) {
1012                 h = set_hash_index(index, size, bits, context, k);
1013                 index[h] = &context->ineq[k];
1014         }
1015         for (k = 0; k < bset->n_ineq; ++k) {
1016                 h = set_hash_index(index, size, bits, bset, k);
1017                 if (!index[h])
1018                         continue;
1019                 l = index[h] - &context->ineq[0];
1020                 if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
1021                         continue;
1022                 bset = isl_basic_set_cow(bset);
1023                 if (!bset)
1024                         goto error;
1025                 isl_basic_set_drop_inequality(bset, k);
1026                 --k;
1027         }
1028         free(index);
1029         return bset;
1030 error:
1031         free(index);
1032         return bset;
1033 }
1034
1035 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1036         struct isl_basic_set *context);
1037
1038 static struct isl_basic_set *uset_gist_context_eq(struct isl_basic_set *bset,
1039         struct isl_basic_set *context)
1040 {
1041         struct isl_mat *T;
1042         struct isl_mat *T2;
1043         struct isl_ctx *ctx = context->ctx;
1044         struct isl_basic_set *reduced_context;
1045         reduced_context = isl_basic_set_remove_equalities(
1046                                 isl_basic_set_copy(context), &T, &T2);
1047         if (!reduced_context)
1048                 goto error;
1049         bset = isl_basic_set_preimage(ctx, bset, T);
1050         bset = uset_gist(bset, reduced_context);
1051         bset = isl_basic_set_preimage(ctx, bset, T2);
1052         bset = isl_basic_set_reduce_using_equalities(bset, context);
1053         return bset;
1054 error:
1055         isl_basic_set_free(context);
1056         isl_basic_set_free(bset);
1057         return NULL;
1058 }
1059
1060 static struct isl_basic_set *uset_gist_set_eq(struct isl_basic_set *bset,
1061         struct isl_basic_set *context)
1062 {
1063         struct isl_mat *T;
1064         struct isl_mat *T2;
1065         struct isl_ctx *ctx = context->ctx;
1066         struct isl_basic_set *affine_hull = NULL;
1067
1068         affine_hull = isl_basic_set_copy(bset);
1069         affine_hull = isl_basic_set_cow(affine_hull);
1070         if (!affine_hull)
1071                 goto error;
1072         isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
1073
1074         bset = isl_basic_set_remove_equalities(bset, &T, &T2);
1075         if (!bset)
1076                 goto error;
1077         context = isl_basic_set_preimage(ctx, context, T);
1078         bset = uset_gist(bset, context);
1079         bset = isl_basic_set_preimage(ctx, bset, T2);
1080         bset = isl_basic_set_intersect(bset, affine_hull);
1081         return bset;
1082 error:
1083         isl_basic_set_free(affine_hull);
1084         isl_basic_set_free(context);
1085         isl_basic_set_free(bset);
1086         return NULL;
1087 }
1088
1089 /* Remove all information from bset that is redundant in the context
1090  * of context.  In particular, equalities that are linear combinations
1091  * of those in context are removed.  Then the inequalities that are
1092  * redundant in the context of the equalities and inequalities of
1093  * context are removed.
1094  */
1095 static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
1096         struct isl_basic_set *context)
1097 {
1098         int i;
1099         isl_int opt;
1100         struct isl_basic_set *combined;
1101         struct isl_ctx *ctx;
1102
1103         if (!bset || !context)
1104                 goto error;
1105
1106         if (context->n_eq > 0)
1107                 return uset_gist_context_eq(bset, context);
1108         if (!context->n_ineq)
1109                 goto done;
1110         if (bset->n_eq > 0)
1111                 return uset_gist_set_eq(bset, context);
1112         bset = remove_shifted_constraints(bset, context);
1113         combined = isl_basic_set_extend_constraints(isl_basic_set_copy(bset),
1114                         context->n_eq, context->n_ineq);
1115         context = isl_basic_set_add_constraints(combined, context, 0);
1116         if (!context)
1117                 goto error;
1118         ctx = context->ctx;
1119         isl_int_init(opt);
1120         for (i = bset->n_ineq-1; i >= 0; --i) {
1121                 int redundant;
1122                 set_swap_inequality(context, i, context->n_ineq-1);
1123                 context->n_ineq--;
1124                 redundant = isl_basic_set_constraint_is_redundant(&context,
1125                                 context->ineq[context->n_ineq], &opt, NULL);
1126                 if (redundant == -1) {
1127                         isl_int_clear(opt);
1128                         goto error;
1129                 }
1130                 if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
1131                         bset = isl_basic_set_set_to_empty(bset);
1132                         break;
1133                 }
1134                 context->n_ineq++;
1135                 set_swap_inequality(context, i, context->n_ineq-1);
1136                 if (redundant) {
1137                         isl_basic_set_drop_inequality(context, i);
1138                         isl_basic_set_drop_inequality(bset, i);
1139                 }
1140         }
1141         isl_int_clear(opt);
1142 done:
1143         isl_basic_set_free(context);
1144         return bset;
1145 error:
1146         isl_basic_set_free(context);
1147         isl_basic_set_free(bset);
1148         return NULL;
1149 }
1150
1151 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
1152         struct isl_basic_map *context)
1153 {
1154         struct isl_basic_set *bset;
1155
1156         if (!bmap || !context)
1157                 goto error;
1158
1159         context = isl_basic_map_align_divs(context, bmap);
1160         bmap = isl_basic_map_align_divs(bmap, context);
1161
1162         bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
1163                          isl_basic_map_underlying_set(context));
1164
1165         return isl_basic_map_overlying_set(bset, bmap);
1166 error:
1167         isl_basic_map_free(bmap);
1168         isl_basic_map_free(context);
1169         return NULL;
1170 }
1171
1172 /*
1173  * Assumes context has no implicit divs.
1174  */
1175 struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
1176 {
1177         int i;
1178
1179         map = isl_map_cow(map);
1180         if (!map || !context)
1181                 return NULL;
1182         isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
1183         map = isl_map_compute_divs(map);
1184         for (i = 0; i < map->n; ++i)
1185                 context = isl_basic_map_align_divs(context, map->p[i]);
1186         for (i = 0; i < map->n; ++i) {
1187                 map->p[i] = isl_basic_map_gist(map->p[i],
1188                                                 isl_basic_map_copy(context));
1189                 if (!map->p[i])
1190                         goto error;
1191         }
1192         isl_basic_map_free(context);
1193         F_CLR(map, ISL_MAP_NORMALIZED);
1194         return map;
1195 error:
1196         isl_map_free(map);
1197         isl_basic_map_free(context);
1198         return NULL;
1199 }
1200
1201 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
1202                                                 struct isl_basic_set *context)
1203 {
1204         return (struct isl_basic_set *)isl_basic_map_gist(
1205                 (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
1206 }
1207
1208 struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
1209 {
1210         return (struct isl_set *)isl_map_gist((struct isl_map *)set,
1211                                         (struct isl_basic_map *)context);
1212 }
1213
1214 /* Quick check to see if two basic maps are disjoint.
1215  * In particular, we reduce the equalities and inequalities of
1216  * one basic map in the context of the equalities of the other
1217  * basic map and check if we get a contradiction.
1218  */
1219 int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
1220         struct isl_basic_map *bmap2)
1221 {
1222         struct isl_vec *v = NULL;
1223         int *elim = NULL;
1224         unsigned total;
1225         int d, i;
1226
1227         if (!bmap1 || !bmap2)
1228                 return -1;
1229         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1230                         return -1);
1231         if (bmap1->n_div || bmap2->n_div)
1232                 return 0;
1233         if (!bmap1->n_eq && !bmap2->n_eq)
1234                 return 0;
1235
1236         total = isl_dim_total(bmap1->dim);
1237         if (total == 0)
1238                 return 0;
1239         v = isl_vec_alloc(bmap1->ctx, 1 + total);
1240         if (!v)
1241                 goto error;
1242         elim = isl_alloc_array(bmap1->ctx, int, total);
1243         if (!elim)
1244                 goto error;
1245         compute_elimination_index(bmap1, elim);
1246         for (i = 0; i < bmap2->n_eq; ++i) {
1247                 int reduced;
1248                 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
1249                                                         bmap1, elim);
1250                 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
1251                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1252                         goto disjoint;
1253         }
1254         for (i = 0; i < bmap2->n_ineq; ++i) {
1255                 int reduced;
1256                 reduced = reduced_using_equalities(v->block.data,
1257                                                 bmap2->ineq[i], bmap1, elim);
1258                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1259                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1260                         goto disjoint;
1261         }
1262         compute_elimination_index(bmap2, elim);
1263         for (i = 0; i < bmap1->n_ineq; ++i) {
1264                 int reduced;
1265                 reduced = reduced_using_equalities(v->block.data,
1266                                                 bmap1->ineq[i], bmap2, elim);
1267                 if (reduced && isl_int_is_neg(v->block.data[0]) &&
1268                     isl_seq_first_non_zero(v->block.data + 1, total) == -1)
1269                         goto disjoint;
1270         }
1271         isl_vec_free(bmap1->ctx, v);
1272         free(elim);
1273         return 0;
1274 disjoint:
1275         isl_vec_free(bmap1->ctx, v);
1276         free(elim);
1277         return 1;
1278 error:
1279         isl_vec_free(bmap1->ctx, v);
1280         free(elim);
1281         return -1;
1282 }
1283
1284 int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
1285         struct isl_basic_set *bset2)
1286 {
1287         return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
1288                                               (struct isl_basic_map *)bset2);
1289 }
1290
1291 int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
1292 {
1293         int i, j;
1294
1295         if (!map1 || !map2)
1296                 return -1;
1297
1298         if (isl_map_fast_is_equal(map1, map2))
1299                 return 0;
1300
1301         for (i = 0; i < map1->n; ++i) {
1302                 for (j = 0; j < map2->n; ++j) {
1303                         int d = isl_basic_map_fast_is_disjoint(map1->p[i],
1304                                                                map2->p[j]);
1305                         if (d != 1)
1306                                 return d;
1307                 }
1308         }
1309         return 1;
1310 }
1311
1312 int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
1313 {
1314         return isl_map_fast_is_disjoint((struct isl_map *)set1,
1315                                         (struct isl_map *)set2);
1316 }