isl_basic_map_drop: drop error label
[isl.git] / isl_map_simplify.c
blob2412266e80354371cb9a9d6b59dcebcf49b1627f
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
50 int i;
51 isl_int gcd;
52 unsigned total = isl_basic_map_total_dim(bmap);
54 if (!bmap)
55 return NULL;
57 isl_int_init(gcd);
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
65 isl_basic_map_drop_equality(bmap, i);
66 continue;
68 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
69 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70 if (isl_int_is_one(gcd))
71 continue;
72 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
73 bmap = isl_basic_map_set_to_empty(bmap);
74 break;
76 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
79 for (i = bmap->n_ineq - 1; i >= 0; --i) {
80 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81 if (isl_int_is_zero(gcd)) {
82 if (isl_int_is_neg(bmap->ineq[i][0])) {
83 bmap = isl_basic_map_set_to_empty(bmap);
84 break;
86 isl_basic_map_drop_inequality(bmap, i);
87 continue;
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
90 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91 if (isl_int_is_one(gcd))
92 continue;
93 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
94 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
96 isl_int_clear(gcd);
98 return bmap;
101 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set *bset)
104 isl_basic_map *bmap = bset_to_bmap(bset);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
119 * Replace it by
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map *reduce_coefficient_in_div(
129 __isl_take isl_basic_map *bmap, int div, int pos)
131 isl_int shift;
132 int add_one;
134 isl_int_init(shift);
135 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136 isl_int_mul_ui(shift, shift, 2);
137 add_one = isl_int_gt(shift, bmap->div[div][0]);
138 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139 if (add_one)
140 isl_int_add_ui(shift, shift, 1);
141 isl_int_neg(shift, shift);
142 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143 isl_int_clear(shift);
145 return bmap;
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
152 * 2 * c != d.
154 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
155 int pos)
157 isl_bool r;
159 if (isl_int_is_zero(bmap->div[div][1 + pos]))
160 return isl_bool_false;
162 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
163 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
165 isl_int_divexact_ui(bmap->div[div][1 + pos],
166 bmap->div[div][1 + pos], 2);
168 return r;
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map *bmap, int div)
179 int i;
180 unsigned total = 1 + isl_basic_map_total_dim(bmap);
182 for (i = 0; i < total; ++i) {
183 isl_bool reduce;
185 reduce = needs_reduction(bmap, div, i);
186 if (reduce < 0)
187 return isl_basic_map_free(bmap);
188 if (!reduce)
189 continue;
190 bmap = reduce_coefficient_in_div(bmap, div, i);
191 if (!bmap)
192 break;
195 return bmap;
198 /* Reduce the coefficients (including the constant term) of
199 * the known integer divisions, if needed
200 * In particular, make sure all coefficients lie in
201 * the half-open interval (1/2,1/2].
203 static __isl_give isl_basic_map *reduce_div_coefficients(
204 __isl_take isl_basic_map *bmap)
206 int i;
208 if (!bmap)
209 return NULL;
210 if (bmap->n_div == 0)
211 return bmap;
213 for (i = 0; i < bmap->n_div; ++i) {
214 if (isl_int_is_zero(bmap->div[i][0]))
215 continue;
216 bmap = reduce_div_coefficients_of_div(bmap, i);
217 if (!bmap)
218 break;
221 return bmap;
224 /* Remove any common factor in numerator and denominator of the div expression,
225 * not taking into account the constant term.
226 * That is, if the div is of the form
228 * floor((a + m f(x))/(m d))
230 * then replace it by
232 * floor((floor(a/m) + f(x))/d)
234 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
235 * and can therefore not influence the result of the floor.
237 static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
239 unsigned total = isl_basic_map_total_dim(bmap);
240 isl_ctx *ctx = bmap->ctx;
242 if (isl_int_is_zero(bmap->div[div][0]))
243 return;
244 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
245 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
246 if (isl_int_is_one(ctx->normalize_gcd))
247 return;
248 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
249 ctx->normalize_gcd);
250 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
251 ctx->normalize_gcd);
252 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
253 ctx->normalize_gcd, total);
256 /* Remove any common factor in numerator and denominator of a div expression,
257 * not taking into account the constant term.
258 * That is, look for any div of the form
260 * floor((a + m f(x))/(m d))
262 * and replace it by
264 * floor((floor(a/m) + f(x))/d)
266 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
267 * and can therefore not influence the result of the floor.
269 static __isl_give isl_basic_map *normalize_div_expressions(
270 __isl_take isl_basic_map *bmap)
272 int i;
274 if (!bmap)
275 return NULL;
276 if (bmap->n_div == 0)
277 return bmap;
279 for (i = 0; i < bmap->n_div; ++i)
280 normalize_div_expression(bmap, i);
282 return bmap;
285 /* Assumes divs have been ordered if keep_divs is set.
287 static void eliminate_var_using_equality(struct isl_basic_map *bmap,
288 unsigned pos, isl_int *eq, int keep_divs, int *progress)
290 unsigned total;
291 unsigned space_total;
292 int k;
293 int last_div;
295 total = isl_basic_map_total_dim(bmap);
296 space_total = isl_space_dim(bmap->dim, isl_dim_all);
297 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
298 for (k = 0; k < bmap->n_eq; ++k) {
299 if (bmap->eq[k] == eq)
300 continue;
301 if (isl_int_is_zero(bmap->eq[k][1+pos]))
302 continue;
303 if (progress)
304 *progress = 1;
305 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
306 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
309 for (k = 0; k < bmap->n_ineq; ++k) {
310 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
311 continue;
312 if (progress)
313 *progress = 1;
314 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
315 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
316 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
317 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
320 for (k = 0; k < bmap->n_div; ++k) {
321 if (isl_int_is_zero(bmap->div[k][0]))
322 continue;
323 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
324 continue;
325 if (progress)
326 *progress = 1;
327 /* We need to be careful about circular definitions,
328 * so for now we just remove the definition of div k
329 * if the equality contains any divs.
330 * If keep_divs is set, then the divs have been ordered
331 * and we can keep the definition as long as the result
332 * is still ordered.
334 if (last_div == -1 || (keep_divs && last_div < k)) {
335 isl_seq_elim(bmap->div[k]+1, eq,
336 1+pos, 1+total, &bmap->div[k][0]);
337 normalize_div_expression(bmap, k);
338 } else
339 isl_seq_clr(bmap->div[k], 1 + total);
343 /* Assumes divs have been ordered if keep_divs is set.
345 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
346 isl_int *eq, unsigned div, int keep_divs)
348 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
350 eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
352 bmap = isl_basic_map_drop_div(bmap, div);
354 return bmap;
357 /* Check if elimination of div "div" using equality "eq" would not
358 * result in a div depending on a later div.
360 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
361 unsigned div)
363 int k;
364 int last_div;
365 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
366 unsigned pos = space_total + div;
368 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
369 if (last_div < 0 || last_div <= div)
370 return isl_bool_true;
372 for (k = 0; k <= last_div; ++k) {
373 if (isl_int_is_zero(bmap->div[k][0]))
374 continue;
375 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
376 return isl_bool_false;
379 return isl_bool_true;
382 /* Eliminate divs based on equalities
384 static __isl_give isl_basic_map *eliminate_divs_eq(
385 __isl_take isl_basic_map *bmap, int *progress)
387 int d;
388 int i;
389 int modified = 0;
390 unsigned off;
392 bmap = isl_basic_map_order_divs(bmap);
394 if (!bmap)
395 return NULL;
397 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
399 for (d = bmap->n_div - 1; d >= 0 ; --d) {
400 for (i = 0; i < bmap->n_eq; ++i) {
401 isl_bool ok;
403 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
404 !isl_int_is_negone(bmap->eq[i][off + d]))
405 continue;
406 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
407 if (ok < 0)
408 return isl_basic_map_free(bmap);
409 if (!ok)
410 continue;
411 modified = 1;
412 *progress = 1;
413 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
414 if (isl_basic_map_drop_equality(bmap, i) < 0)
415 return isl_basic_map_free(bmap);
416 break;
419 if (modified)
420 return eliminate_divs_eq(bmap, progress);
421 return bmap;
424 /* Eliminate divs based on inequalities
426 static __isl_give isl_basic_map *eliminate_divs_ineq(
427 __isl_take isl_basic_map *bmap, int *progress)
429 int d;
430 int i;
431 unsigned off;
432 struct isl_ctx *ctx;
434 if (!bmap)
435 return NULL;
437 ctx = bmap->ctx;
438 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
440 for (d = bmap->n_div - 1; d >= 0 ; --d) {
441 for (i = 0; i < bmap->n_eq; ++i)
442 if (!isl_int_is_zero(bmap->eq[i][off + d]))
443 break;
444 if (i < bmap->n_eq)
445 continue;
446 for (i = 0; i < bmap->n_ineq; ++i)
447 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
448 break;
449 if (i < bmap->n_ineq)
450 continue;
451 *progress = 1;
452 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
453 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
454 break;
455 bmap = isl_basic_map_drop_div(bmap, d);
456 if (!bmap)
457 break;
459 return bmap;
462 /* Does the equality constraint at position "eq" in "bmap" involve
463 * any local variables in the range [first, first + n)
464 * that are not marked as having an explicit representation?
466 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
467 int eq, unsigned first, unsigned n)
469 unsigned o_div;
470 int i;
472 if (!bmap)
473 return isl_bool_error;
475 o_div = isl_basic_map_offset(bmap, isl_dim_div);
476 for (i = 0; i < n; ++i) {
477 isl_bool unknown;
479 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
480 continue;
481 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
482 if (unknown < 0)
483 return isl_bool_error;
484 if (unknown)
485 return isl_bool_true;
488 return isl_bool_false;
491 /* The last local variable involved in the equality constraint
492 * at position "eq" in "bmap" is the local variable at position "div".
493 * It can therefore be used to extract an explicit representation
494 * for that variable.
495 * Do so unless the local variable already has an explicit representation or
496 * the explicit representation would involve any other local variables
497 * that in turn do not have an explicit representation.
498 * An equality constraint involving local variables without an explicit
499 * representation can be used in isl_basic_map_drop_redundant_divs
500 * to separate out an independent local variable. Introducing
501 * an explicit representation here would block this transformation,
502 * while the partial explicit representation in itself is not very useful.
503 * Set *progress if anything is changed.
505 * The equality constraint is of the form
507 * f(x) + n e >= 0
509 * with n a positive number. The explicit representation derived from
510 * this constraint is
512 * floor((-f(x))/n)
514 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
515 int div, int eq, int *progress)
517 unsigned total, o_div;
518 isl_bool involves;
520 if (!bmap)
521 return NULL;
523 if (!isl_int_is_zero(bmap->div[div][0]))
524 return bmap;
526 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
527 if (involves < 0)
528 return isl_basic_map_free(bmap);
529 if (involves)
530 return bmap;
532 total = isl_basic_map_dim(bmap, isl_dim_all);
533 o_div = isl_basic_map_offset(bmap, isl_dim_div);
534 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
535 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
536 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
537 if (progress)
538 *progress = 1;
540 return bmap;
543 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
544 int *progress)
546 int k;
547 int done;
548 int last_var;
549 unsigned total_var;
550 unsigned total;
552 bmap = isl_basic_map_order_divs(bmap);
554 if (!bmap)
555 return NULL;
557 total = isl_basic_map_total_dim(bmap);
558 total_var = total - bmap->n_div;
560 last_var = total - 1;
561 for (done = 0; done < bmap->n_eq; ++done) {
562 for (; last_var >= 0; --last_var) {
563 for (k = done; k < bmap->n_eq; ++k)
564 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
565 break;
566 if (k < bmap->n_eq)
567 break;
569 if (last_var < 0)
570 break;
571 if (k != done)
572 swap_equality(bmap, k, done);
573 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
574 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
576 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
577 progress);
579 if (last_var >= total_var)
580 bmap = set_div_from_eq(bmap, last_var - total_var,
581 done, progress);
582 if (!bmap)
583 return NULL;
585 if (done == bmap->n_eq)
586 return bmap;
587 for (k = done; k < bmap->n_eq; ++k) {
588 if (isl_int_is_zero(bmap->eq[k][0]))
589 continue;
590 return isl_basic_map_set_to_empty(bmap);
592 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
593 return bmap;
596 __isl_give isl_basic_set *isl_basic_set_gauss(
597 __isl_take isl_basic_set *bset, int *progress)
599 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
600 progress));
604 static unsigned int round_up(unsigned int v)
606 int old_v = v;
608 while (v) {
609 old_v = v;
610 v ^= v & -v;
612 return old_v << 1;
615 /* Hash table of inequalities in a basic map.
616 * "index" is an array of addresses of inequalities in the basic map, some
617 * of which are NULL. The inequalities are hashed on the coefficients
618 * except the constant term.
619 * "size" is the number of elements in the array and is always a power of two
620 * "bits" is the number of bits need to represent an index into the array.
621 * "total" is the total dimension of the basic map.
623 struct isl_constraint_index {
624 unsigned int size;
625 int bits;
626 isl_int ***index;
627 unsigned total;
630 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
632 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
633 __isl_keep isl_basic_map *bmap)
635 isl_ctx *ctx;
637 ci->index = NULL;
638 if (!bmap)
639 return isl_stat_error;
640 ci->total = isl_basic_set_total_dim(bmap);
641 if (bmap->n_ineq == 0)
642 return isl_stat_ok;
643 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
644 ci->bits = ffs(ci->size) - 1;
645 ctx = isl_basic_map_get_ctx(bmap);
646 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
647 if (!ci->index)
648 return isl_stat_error;
650 return isl_stat_ok;
653 /* Free the memory allocated by create_constraint_index.
655 static void constraint_index_free(struct isl_constraint_index *ci)
657 free(ci->index);
660 /* Return the position in ci->index that contains the address of
661 * an inequality that is equal to *ineq up to the constant term,
662 * provided this address is not identical to "ineq".
663 * If there is no such inequality, then return the position where
664 * such an inequality should be inserted.
666 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
668 int h;
669 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
670 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
671 if (ineq != ci->index[h] &&
672 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
673 break;
674 return h;
677 /* Return the position in ci->index that contains the address of
678 * an inequality that is equal to the k'th inequality of "bmap"
679 * up to the constant term, provided it does not point to the very
680 * same inequality.
681 * If there is no such inequality, then return the position where
682 * such an inequality should be inserted.
684 static int hash_index(struct isl_constraint_index *ci,
685 __isl_keep isl_basic_map *bmap, int k)
687 return hash_index_ineq(ci, &bmap->ineq[k]);
690 static int set_hash_index(struct isl_constraint_index *ci,
691 __isl_keep isl_basic_set *bset, int k)
693 return hash_index(ci, bset, k);
696 /* Fill in the "ci" data structure with the inequalities of "bset".
698 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
699 __isl_keep isl_basic_set *bset)
701 int k, h;
703 if (create_constraint_index(ci, bset) < 0)
704 return isl_stat_error;
706 for (k = 0; k < bset->n_ineq; ++k) {
707 h = set_hash_index(ci, bset, k);
708 ci->index[h] = &bset->ineq[k];
711 return isl_stat_ok;
714 /* Is the inequality ineq (obviously) redundant with respect
715 * to the constraints in "ci"?
717 * Look for an inequality in "ci" with the same coefficients and then
718 * check if the contant term of "ineq" is greater than or equal
719 * to the constant term of that inequality. If so, "ineq" is clearly
720 * redundant.
722 * Note that hash_index_ineq ignores a stored constraint if it has
723 * the same address as the passed inequality. It is ok to pass
724 * the address of a local variable here since it will never be
725 * the same as the address of a constraint in "ci".
727 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
728 isl_int *ineq)
730 int h;
732 h = hash_index_ineq(ci, &ineq);
733 if (!ci->index[h])
734 return isl_bool_false;
735 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
738 /* If we can eliminate more than one div, then we need to make
739 * sure we do it from last div to first div, in order not to
740 * change the position of the other divs that still need to
741 * be removed.
743 static __isl_give isl_basic_map *remove_duplicate_divs(
744 __isl_take isl_basic_map *bmap, int *progress)
746 unsigned int size;
747 int *index;
748 int *elim_for;
749 int k, l, h;
750 int bits;
751 struct isl_blk eq;
752 unsigned total_var;
753 unsigned total;
754 struct isl_ctx *ctx;
756 bmap = isl_basic_map_order_divs(bmap);
757 if (!bmap || bmap->n_div <= 1)
758 return bmap;
760 total_var = isl_space_dim(bmap->dim, isl_dim_all);
761 total = total_var + bmap->n_div;
763 ctx = bmap->ctx;
764 for (k = bmap->n_div - 1; k >= 0; --k)
765 if (!isl_int_is_zero(bmap->div[k][0]))
766 break;
767 if (k <= 0)
768 return bmap;
770 size = round_up(4 * bmap->n_div / 3 - 1);
771 if (size == 0)
772 return bmap;
773 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
774 bits = ffs(size) - 1;
775 index = isl_calloc_array(ctx, int, size);
776 if (!elim_for || !index)
777 goto out;
778 eq = isl_blk_alloc(ctx, 1+total);
779 if (isl_blk_is_error(eq))
780 goto out;
782 isl_seq_clr(eq.data, 1+total);
783 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
784 for (--k; k >= 0; --k) {
785 uint32_t hash;
787 if (isl_int_is_zero(bmap->div[k][0]))
788 continue;
790 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
791 for (h = hash; index[h]; h = (h+1) % size)
792 if (isl_seq_eq(bmap->div[k],
793 bmap->div[index[h]-1], 2+total))
794 break;
795 if (index[h]) {
796 *progress = 1;
797 l = index[h] - 1;
798 elim_for[l] = k + 1;
800 index[h] = k+1;
802 for (l = bmap->n_div - 1; l >= 0; --l) {
803 if (!elim_for[l])
804 continue;
805 k = elim_for[l] - 1;
806 isl_int_set_si(eq.data[1+total_var+k], -1);
807 isl_int_set_si(eq.data[1+total_var+l], 1);
808 bmap = eliminate_div(bmap, eq.data, l, 1);
809 if (!bmap)
810 break;
811 isl_int_set_si(eq.data[1+total_var+k], 0);
812 isl_int_set_si(eq.data[1+total_var+l], 0);
815 isl_blk_free(ctx, eq);
816 out:
817 free(index);
818 free(elim_for);
819 return bmap;
822 static int n_pure_div_eq(struct isl_basic_map *bmap)
824 int i, j;
825 unsigned total;
827 total = isl_space_dim(bmap->dim, isl_dim_all);
828 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
829 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
830 --j;
831 if (j < 0)
832 break;
833 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
834 return 0;
836 return i;
839 /* Normalize divs that appear in equalities.
841 * In particular, we assume that bmap contains some equalities
842 * of the form
844 * a x = m * e_i
846 * and we want to replace the set of e_i by a minimal set and
847 * such that the new e_i have a canonical representation in terms
848 * of the vector x.
849 * If any of the equalities involves more than one divs, then
850 * we currently simply bail out.
852 * Let us first additionally assume that all equalities involve
853 * a div. The equalities then express modulo constraints on the
854 * remaining variables and we can use "parameter compression"
855 * to find a minimal set of constraints. The result is a transformation
857 * x = T(x') = x_0 + G x'
859 * with G a lower-triangular matrix with all elements below the diagonal
860 * non-negative and smaller than the diagonal element on the same row.
861 * We first normalize x_0 by making the same property hold in the affine
862 * T matrix.
863 * The rows i of G with a 1 on the diagonal do not impose any modulo
864 * constraint and simply express x_i = x'_i.
865 * For each of the remaining rows i, we introduce a div and a corresponding
866 * equality. In particular
868 * g_ii e_j = x_i - g_i(x')
870 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
871 * corresponding div (if g_kk != 1).
873 * If there are any equalities not involving any div, then we
874 * first apply a variable compression on the variables x:
876 * x = C x'' x'' = C_2 x
878 * and perform the above parameter compression on A C instead of on A.
879 * The resulting compression is then of the form
881 * x'' = T(x') = x_0 + G x'
883 * and in constructing the new divs and the corresponding equalities,
884 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
885 * by the corresponding row from C_2.
887 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
888 int *progress)
890 int i, j, k;
891 int total;
892 int div_eq;
893 struct isl_mat *B;
894 struct isl_vec *d;
895 struct isl_mat *T = NULL;
896 struct isl_mat *C = NULL;
897 struct isl_mat *C2 = NULL;
898 isl_int v;
899 int *pos = NULL;
900 int dropped, needed;
902 if (!bmap)
903 return NULL;
905 if (bmap->n_div == 0)
906 return bmap;
908 if (bmap->n_eq == 0)
909 return bmap;
911 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
912 return bmap;
914 total = isl_space_dim(bmap->dim, isl_dim_all);
915 div_eq = n_pure_div_eq(bmap);
916 if (div_eq == 0)
917 return bmap;
919 if (div_eq < bmap->n_eq) {
920 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
921 bmap->n_eq - div_eq, 0, 1 + total);
922 C = isl_mat_variable_compression(B, &C2);
923 if (!C || !C2)
924 goto error;
925 if (C->n_col == 0) {
926 bmap = isl_basic_map_set_to_empty(bmap);
927 isl_mat_free(C);
928 isl_mat_free(C2);
929 goto done;
933 d = isl_vec_alloc(bmap->ctx, div_eq);
934 if (!d)
935 goto error;
936 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
937 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
938 --j;
939 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
941 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
943 if (C) {
944 B = isl_mat_product(B, C);
945 C = NULL;
948 T = isl_mat_parameter_compression(B, d);
949 if (!T)
950 goto error;
951 if (T->n_col == 0) {
952 bmap = isl_basic_map_set_to_empty(bmap);
953 isl_mat_free(C2);
954 isl_mat_free(T);
955 goto done;
957 isl_int_init(v);
958 for (i = 0; i < T->n_row - 1; ++i) {
959 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
960 if (isl_int_is_zero(v))
961 continue;
962 isl_mat_col_submul(T, 0, v, 1 + i);
964 isl_int_clear(v);
965 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
966 if (!pos)
967 goto error;
968 /* We have to be careful because dropping equalities may reorder them */
969 dropped = 0;
970 for (j = bmap->n_div - 1; j >= 0; --j) {
971 for (i = 0; i < bmap->n_eq; ++i)
972 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
973 break;
974 if (i < bmap->n_eq) {
975 bmap = isl_basic_map_drop_div(bmap, j);
976 isl_basic_map_drop_equality(bmap, i);
977 ++dropped;
980 pos[0] = 0;
981 needed = 0;
982 for (i = 1; i < T->n_row; ++i) {
983 if (isl_int_is_one(T->row[i][i]))
984 pos[i] = i;
985 else
986 needed++;
988 if (needed > dropped) {
989 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
990 needed, needed, 0);
991 if (!bmap)
992 goto error;
994 for (i = 1; i < T->n_row; ++i) {
995 if (isl_int_is_one(T->row[i][i]))
996 continue;
997 k = isl_basic_map_alloc_div(bmap);
998 pos[i] = 1 + total + k;
999 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1000 isl_int_set(bmap->div[k][0], T->row[i][i]);
1001 if (C2)
1002 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1003 else
1004 isl_int_set_si(bmap->div[k][1 + i], 1);
1005 for (j = 0; j < i; ++j) {
1006 if (isl_int_is_zero(T->row[i][j]))
1007 continue;
1008 if (pos[j] < T->n_row && C2)
1009 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1010 C2->row[pos[j]], 1 + total);
1011 else
1012 isl_int_neg(bmap->div[k][1 + pos[j]],
1013 T->row[i][j]);
1015 j = isl_basic_map_alloc_equality(bmap);
1016 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1017 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1019 free(pos);
1020 isl_mat_free(C2);
1021 isl_mat_free(T);
1023 if (progress)
1024 *progress = 1;
1025 done:
1026 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1028 return bmap;
1029 error:
1030 free(pos);
1031 isl_mat_free(C);
1032 isl_mat_free(C2);
1033 isl_mat_free(T);
1034 return bmap;
1037 static __isl_give isl_basic_map *set_div_from_lower_bound(
1038 __isl_take isl_basic_map *bmap, int div, int ineq)
1040 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1042 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1043 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1044 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1045 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1046 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1048 return bmap;
1051 /* Check whether it is ok to define a div based on an inequality.
1052 * To avoid the introduction of circular definitions of divs, we
1053 * do not allow such a definition if the resulting expression would refer to
1054 * any other undefined divs or if any known div is defined in
1055 * terms of the unknown div.
1057 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1058 int div, int ineq)
1060 int j;
1061 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1063 /* Not defined in terms of unknown divs */
1064 for (j = 0; j < bmap->n_div; ++j) {
1065 if (div == j)
1066 continue;
1067 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1068 continue;
1069 if (isl_int_is_zero(bmap->div[j][0]))
1070 return isl_bool_false;
1073 /* No other div defined in terms of this one => avoid loops */
1074 for (j = 0; j < bmap->n_div; ++j) {
1075 if (div == j)
1076 continue;
1077 if (isl_int_is_zero(bmap->div[j][0]))
1078 continue;
1079 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1080 return isl_bool_false;
1083 return isl_bool_true;
1086 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1087 * be a better expression than the current one?
1089 * If we do not have any expression yet, then any expression would be better.
1090 * Otherwise we check if the last variable involved in the inequality
1091 * (disregarding the div that it would define) is in an earlier position
1092 * than the last variable involved in the current div expression.
1094 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1095 int div, int ineq)
1097 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1098 int last_div;
1099 int last_ineq;
1101 if (isl_int_is_zero(bmap->div[div][0]))
1102 return isl_bool_true;
1104 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1105 bmap->n_div - (div + 1)) >= 0)
1106 return isl_bool_false;
1108 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1109 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1110 total + bmap->n_div);
1112 return last_ineq < last_div;
1115 /* Given two constraints "k" and "l" that are opposite to each other,
1116 * except for the constant term, check if we can use them
1117 * to obtain an expression for one of the hitherto unknown divs or
1118 * a "better" expression for a div for which we already have an expression.
1119 * "sum" is the sum of the constant terms of the constraints.
1120 * If this sum is strictly smaller than the coefficient of one
1121 * of the divs, then this pair can be used define the div.
1122 * To avoid the introduction of circular definitions of divs, we
1123 * do not use the pair if the resulting expression would refer to
1124 * any other undefined divs or if any known div is defined in
1125 * terms of the unknown div.
1127 static __isl_give isl_basic_map *check_for_div_constraints(
1128 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1129 int *progress)
1131 int i;
1132 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1134 for (i = 0; i < bmap->n_div; ++i) {
1135 isl_bool set_div;
1137 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1138 continue;
1139 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1140 continue;
1141 set_div = better_div_constraint(bmap, i, k);
1142 if (set_div >= 0 && set_div)
1143 set_div = ok_to_set_div_from_bound(bmap, i, k);
1144 if (set_div < 0)
1145 return isl_basic_map_free(bmap);
1146 if (!set_div)
1147 break;
1148 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1149 bmap = set_div_from_lower_bound(bmap, i, k);
1150 else
1151 bmap = set_div_from_lower_bound(bmap, i, l);
1152 if (progress)
1153 *progress = 1;
1154 break;
1156 return bmap;
1159 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1160 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1162 struct isl_constraint_index ci;
1163 int k, l, h;
1164 unsigned total = isl_basic_map_total_dim(bmap);
1165 isl_int sum;
1167 if (!bmap || bmap->n_ineq <= 1)
1168 return bmap;
1170 if (create_constraint_index(&ci, bmap) < 0)
1171 return bmap;
1173 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1174 ci.index[h] = &bmap->ineq[0];
1175 for (k = 1; k < bmap->n_ineq; ++k) {
1176 h = hash_index(&ci, bmap, k);
1177 if (!ci.index[h]) {
1178 ci.index[h] = &bmap->ineq[k];
1179 continue;
1181 if (progress)
1182 *progress = 1;
1183 l = ci.index[h] - &bmap->ineq[0];
1184 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1185 swap_inequality(bmap, k, l);
1186 isl_basic_map_drop_inequality(bmap, k);
1187 --k;
1189 isl_int_init(sum);
1190 for (k = 0; k < bmap->n_ineq-1; ++k) {
1191 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1192 h = hash_index(&ci, bmap, k);
1193 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1194 if (!ci.index[h])
1195 continue;
1196 l = ci.index[h] - &bmap->ineq[0];
1197 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1198 if (isl_int_is_pos(sum)) {
1199 if (detect_divs)
1200 bmap = check_for_div_constraints(bmap, k, l,
1201 sum, progress);
1202 continue;
1204 if (isl_int_is_zero(sum)) {
1205 /* We need to break out of the loop after these
1206 * changes since the contents of the hash
1207 * will no longer be valid.
1208 * Plus, we probably we want to regauss first.
1210 if (progress)
1211 *progress = 1;
1212 isl_basic_map_drop_inequality(bmap, l);
1213 isl_basic_map_inequality_to_equality(bmap, k);
1214 } else
1215 bmap = isl_basic_map_set_to_empty(bmap);
1216 break;
1218 isl_int_clear(sum);
1220 constraint_index_free(&ci);
1221 return bmap;
1224 /* Detect all pairs of inequalities that form an equality.
1226 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1227 * Call it repeatedly while it is making progress.
1229 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1230 __isl_take isl_basic_map *bmap, int *progress)
1232 int duplicate;
1234 do {
1235 duplicate = 0;
1236 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1237 &duplicate, 0);
1238 if (progress && duplicate)
1239 *progress = 1;
1240 } while (duplicate);
1242 return bmap;
1245 /* Eliminate knowns divs from constraints where they appear with
1246 * a (positive or negative) unit coefficient.
1248 * That is, replace
1250 * floor(e/m) + f >= 0
1252 * by
1254 * e + m f >= 0
1256 * and
1258 * -floor(e/m) + f >= 0
1260 * by
1262 * -e + m f + m - 1 >= 0
1264 * The first conversion is valid because floor(e/m) >= -f is equivalent
1265 * to e/m >= -f because -f is an integral expression.
1266 * The second conversion follows from the fact that
1268 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1271 * Note that one of the div constraints may have been eliminated
1272 * due to being redundant with respect to the constraint that is
1273 * being modified by this function. The modified constraint may
1274 * no longer imply this div constraint, so we add it back to make
1275 * sure we do not lose any information.
1277 * We skip integral divs, i.e., those with denominator 1, as we would
1278 * risk eliminating the div from the div constraints. We do not need
1279 * to handle those divs here anyway since the div constraints will turn
1280 * out to form an equality and this equality can then be used to eliminate
1281 * the div from all constraints.
1283 static __isl_give isl_basic_map *eliminate_unit_divs(
1284 __isl_take isl_basic_map *bmap, int *progress)
1286 int i, j;
1287 isl_ctx *ctx;
1288 unsigned total;
1290 if (!bmap)
1291 return NULL;
1293 ctx = isl_basic_map_get_ctx(bmap);
1294 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1296 for (i = 0; i < bmap->n_div; ++i) {
1297 if (isl_int_is_zero(bmap->div[i][0]))
1298 continue;
1299 if (isl_int_is_one(bmap->div[i][0]))
1300 continue;
1301 for (j = 0; j < bmap->n_ineq; ++j) {
1302 int s;
1304 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1305 !isl_int_is_negone(bmap->ineq[j][total + i]))
1306 continue;
1308 *progress = 1;
1310 s = isl_int_sgn(bmap->ineq[j][total + i]);
1311 isl_int_set_si(bmap->ineq[j][total + i], 0);
1312 if (s < 0)
1313 isl_seq_combine(bmap->ineq[j],
1314 ctx->negone, bmap->div[i] + 1,
1315 bmap->div[i][0], bmap->ineq[j],
1316 total + bmap->n_div);
1317 else
1318 isl_seq_combine(bmap->ineq[j],
1319 ctx->one, bmap->div[i] + 1,
1320 bmap->div[i][0], bmap->ineq[j],
1321 total + bmap->n_div);
1322 if (s < 0) {
1323 isl_int_add(bmap->ineq[j][0],
1324 bmap->ineq[j][0], bmap->div[i][0]);
1325 isl_int_sub_ui(bmap->ineq[j][0],
1326 bmap->ineq[j][0], 1);
1329 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1330 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1331 return isl_basic_map_free(bmap);
1335 return bmap;
1338 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1340 int progress = 1;
1341 if (!bmap)
1342 return NULL;
1343 while (progress) {
1344 isl_bool empty;
1346 progress = 0;
1347 empty = isl_basic_map_plain_is_empty(bmap);
1348 if (empty < 0)
1349 return isl_basic_map_free(bmap);
1350 if (empty)
1351 break;
1352 bmap = isl_basic_map_normalize_constraints(bmap);
1353 bmap = reduce_div_coefficients(bmap);
1354 bmap = normalize_div_expressions(bmap);
1355 bmap = remove_duplicate_divs(bmap, &progress);
1356 bmap = eliminate_unit_divs(bmap, &progress);
1357 bmap = eliminate_divs_eq(bmap, &progress);
1358 bmap = eliminate_divs_ineq(bmap, &progress);
1359 bmap = isl_basic_map_gauss(bmap, &progress);
1360 /* requires equalities in normal form */
1361 bmap = normalize_divs(bmap, &progress);
1362 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1363 &progress, 1);
1364 if (bmap && progress)
1365 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1367 return bmap;
1370 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1372 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1376 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1377 isl_int *constraint, unsigned div)
1379 unsigned pos;
1381 if (!bmap)
1382 return isl_bool_error;
1384 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1386 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1387 int neg;
1388 isl_int_sub(bmap->div[div][1],
1389 bmap->div[div][1], bmap->div[div][0]);
1390 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1391 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1392 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1393 isl_int_add(bmap->div[div][1],
1394 bmap->div[div][1], bmap->div[div][0]);
1395 if (!neg)
1396 return isl_bool_false;
1397 if (isl_seq_first_non_zero(constraint+pos+1,
1398 bmap->n_div-div-1) != -1)
1399 return isl_bool_false;
1400 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1401 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1402 return isl_bool_false;
1403 if (isl_seq_first_non_zero(constraint+pos+1,
1404 bmap->n_div-div-1) != -1)
1405 return isl_bool_false;
1406 } else
1407 return isl_bool_false;
1409 return isl_bool_true;
1412 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1413 isl_int *constraint, unsigned div)
1415 return isl_basic_map_is_div_constraint(bset, constraint, div);
1419 /* If the only constraints a div d=floor(f/m)
1420 * appears in are its two defining constraints
1422 * f - m d >=0
1423 * -(f - (m - 1)) + m d >= 0
1425 * then it can safely be removed.
1427 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1429 int i;
1430 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1432 for (i = 0; i < bmap->n_eq; ++i)
1433 if (!isl_int_is_zero(bmap->eq[i][pos]))
1434 return isl_bool_false;
1436 for (i = 0; i < bmap->n_ineq; ++i) {
1437 isl_bool red;
1439 if (isl_int_is_zero(bmap->ineq[i][pos]))
1440 continue;
1441 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1442 if (red < 0 || !red)
1443 return red;
1446 for (i = 0; i < bmap->n_div; ++i) {
1447 if (isl_int_is_zero(bmap->div[i][0]))
1448 continue;
1449 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1450 return isl_bool_false;
1453 return isl_bool_true;
1457 * Remove divs that don't occur in any of the constraints or other divs.
1458 * These can arise when dropping constraints from a basic map or
1459 * when the divs of a basic map have been temporarily aligned
1460 * with the divs of another basic map.
1462 static __isl_give isl_basic_map *remove_redundant_divs(
1463 __isl_take isl_basic_map *bmap)
1465 int i;
1466 int v_div;
1468 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1469 if (v_div < 0)
1470 return isl_basic_map_free(bmap);
1472 for (i = bmap->n_div-1; i >= 0; --i) {
1473 isl_bool redundant;
1475 redundant = div_is_redundant(bmap, i);
1476 if (redundant < 0)
1477 return isl_basic_map_free(bmap);
1478 if (!redundant)
1479 continue;
1480 bmap = isl_basic_map_drop_constraints_involving(bmap,
1481 v_div + i, 1);
1482 bmap = isl_basic_map_drop_div(bmap, i);
1484 return bmap;
1487 /* Mark "bmap" as final, without checking for obviously redundant
1488 * integer divisions. This function should be used when "bmap"
1489 * is known not to involve any such integer divisions.
1491 __isl_give isl_basic_map *isl_basic_map_mark_final(
1492 __isl_take isl_basic_map *bmap)
1494 if (!bmap)
1495 return NULL;
1496 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1497 return bmap;
1500 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1502 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1504 bmap = remove_redundant_divs(bmap);
1505 bmap = isl_basic_map_mark_final(bmap);
1506 return bmap;
1509 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1511 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1514 /* Remove definition of any div that is defined in terms of the given variable.
1515 * The div itself is not removed. Functions such as
1516 * eliminate_divs_ineq depend on the other divs remaining in place.
1518 static __isl_give isl_basic_map *remove_dependent_vars(
1519 __isl_take isl_basic_map *bmap, int pos)
1521 int i;
1523 if (!bmap)
1524 return NULL;
1526 for (i = 0; i < bmap->n_div; ++i) {
1527 if (isl_int_is_zero(bmap->div[i][0]))
1528 continue;
1529 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1530 continue;
1531 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1532 if (!bmap)
1533 return NULL;
1535 return bmap;
1538 /* Eliminate the specified variables from the constraints using
1539 * Fourier-Motzkin. The variables themselves are not removed.
1541 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1542 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1544 int d;
1545 int i, j, k;
1546 unsigned total;
1547 int need_gauss = 0;
1549 if (n == 0)
1550 return bmap;
1551 if (!bmap)
1552 return NULL;
1553 total = isl_basic_map_total_dim(bmap);
1555 bmap = isl_basic_map_cow(bmap);
1556 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1557 bmap = remove_dependent_vars(bmap, d);
1558 if (!bmap)
1559 return NULL;
1561 for (d = pos + n - 1;
1562 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1563 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1564 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1565 int n_lower, n_upper;
1566 if (!bmap)
1567 return NULL;
1568 for (i = 0; i < bmap->n_eq; ++i) {
1569 if (isl_int_is_zero(bmap->eq[i][1+d]))
1570 continue;
1571 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1572 isl_basic_map_drop_equality(bmap, i);
1573 need_gauss = 1;
1574 break;
1576 if (i < bmap->n_eq)
1577 continue;
1578 n_lower = 0;
1579 n_upper = 0;
1580 for (i = 0; i < bmap->n_ineq; ++i) {
1581 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1582 n_lower++;
1583 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1584 n_upper++;
1586 bmap = isl_basic_map_extend_constraints(bmap,
1587 0, n_lower * n_upper);
1588 if (!bmap)
1589 goto error;
1590 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1591 int last;
1592 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1593 continue;
1594 last = -1;
1595 for (j = 0; j < i; ++j) {
1596 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1597 continue;
1598 last = j;
1599 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1600 isl_int_sgn(bmap->ineq[j][1+d]))
1601 continue;
1602 k = isl_basic_map_alloc_inequality(bmap);
1603 if (k < 0)
1604 goto error;
1605 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1606 1+total);
1607 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1608 1+d, 1+total, NULL);
1610 isl_basic_map_drop_inequality(bmap, i);
1611 i = last + 1;
1613 if (n_lower > 0 && n_upper > 0) {
1614 bmap = isl_basic_map_normalize_constraints(bmap);
1615 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1616 NULL, 0);
1617 bmap = isl_basic_map_gauss(bmap, NULL);
1618 bmap = isl_basic_map_remove_redundancies(bmap);
1619 need_gauss = 0;
1620 if (!bmap)
1621 goto error;
1622 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1623 break;
1626 if (need_gauss)
1627 bmap = isl_basic_map_gauss(bmap, NULL);
1628 return bmap;
1629 error:
1630 isl_basic_map_free(bmap);
1631 return NULL;
1634 struct isl_basic_set *isl_basic_set_eliminate_vars(
1635 struct isl_basic_set *bset, unsigned pos, unsigned n)
1637 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1638 pos, n));
1641 /* Eliminate the specified n dimensions starting at first from the
1642 * constraints, without removing the dimensions from the space.
1643 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1644 * Otherwise, they are projected out and the original space is restored.
1646 __isl_give isl_basic_map *isl_basic_map_eliminate(
1647 __isl_take isl_basic_map *bmap,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1650 isl_space *space;
1652 if (!bmap)
1653 return NULL;
1654 if (n == 0)
1655 return bmap;
1657 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1658 isl_die(bmap->ctx, isl_error_invalid,
1659 "index out of bounds", goto error);
1661 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1662 first += isl_basic_map_offset(bmap, type) - 1;
1663 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1664 return isl_basic_map_finalize(bmap);
1667 space = isl_basic_map_get_space(bmap);
1668 bmap = isl_basic_map_project_out(bmap, type, first, n);
1669 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1670 bmap = isl_basic_map_reset_space(bmap, space);
1671 return bmap;
1672 error:
1673 isl_basic_map_free(bmap);
1674 return NULL;
1677 __isl_give isl_basic_set *isl_basic_set_eliminate(
1678 __isl_take isl_basic_set *bset,
1679 enum isl_dim_type type, unsigned first, unsigned n)
1681 return isl_basic_map_eliminate(bset, type, first, n);
1684 /* Remove all constraints from "bmap" that reference any unknown local
1685 * variables (directly or indirectly).
1687 * Dropping all constraints on a local variable will make it redundant,
1688 * so it will get removed implicitly by
1689 * isl_basic_map_drop_constraints_involving_dims. Some other local
1690 * variables may also end up becoming redundant if they only appear
1691 * in constraints together with the unknown local variable.
1692 * Therefore, start over after calling
1693 * isl_basic_map_drop_constraints_involving_dims.
1695 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1696 __isl_take isl_basic_map *bmap)
1698 isl_bool known;
1699 int i, n_div, o_div;
1701 known = isl_basic_map_divs_known(bmap);
1702 if (known < 0)
1703 return isl_basic_map_free(bmap);
1704 if (known)
1705 return bmap;
1707 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1708 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1710 for (i = 0; i < n_div; ++i) {
1711 known = isl_basic_map_div_is_known(bmap, i);
1712 if (known < 0)
1713 return isl_basic_map_free(bmap);
1714 if (known)
1715 continue;
1716 bmap = remove_dependent_vars(bmap, o_div + i);
1717 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1718 isl_dim_div, i, 1);
1719 if (!bmap)
1720 return NULL;
1721 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1722 i = -1;
1725 return bmap;
1728 /* Remove all constraints from "map" that reference any unknown local
1729 * variables (directly or indirectly).
1731 * Since constraints may get dropped from the basic maps,
1732 * they may no longer be disjoint from each other.
1734 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1735 __isl_take isl_map *map)
1737 int i;
1738 isl_bool known;
1740 known = isl_map_divs_known(map);
1741 if (known < 0)
1742 return isl_map_free(map);
1743 if (known)
1744 return map;
1746 map = isl_map_cow(map);
1747 if (!map)
1748 return NULL;
1750 for (i = 0; i < map->n; ++i) {
1751 map->p[i] =
1752 isl_basic_map_drop_constraint_involving_unknown_divs(
1753 map->p[i]);
1754 if (!map->p[i])
1755 return isl_map_free(map);
1758 if (map->n > 1)
1759 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1761 return map;
1764 /* Don't assume equalities are in order, because align_divs
1765 * may have changed the order of the divs.
1767 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1769 int d, i;
1770 unsigned total;
1772 total = isl_space_dim(bmap->dim, isl_dim_all);
1773 for (d = 0; d < total; ++d)
1774 elim[d] = -1;
1775 for (i = 0; i < bmap->n_eq; ++i) {
1776 for (d = total - 1; d >= 0; --d) {
1777 if (isl_int_is_zero(bmap->eq[i][1+d]))
1778 continue;
1779 elim[d] = i;
1780 break;
1785 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1786 int *elim)
1788 compute_elimination_index(bset_to_bmap(bset), elim);
1791 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1792 __isl_keep isl_basic_map *bmap, int *elim)
1794 int d;
1795 int copied = 0;
1796 unsigned total;
1798 total = isl_space_dim(bmap->dim, isl_dim_all);
1799 for (d = total - 1; d >= 0; --d) {
1800 if (isl_int_is_zero(src[1+d]))
1801 continue;
1802 if (elim[d] == -1)
1803 continue;
1804 if (!copied) {
1805 isl_seq_cpy(dst, src, 1 + total);
1806 copied = 1;
1808 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1810 return copied;
1813 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1814 __isl_keep isl_basic_set *bset, int *elim)
1816 return reduced_using_equalities(dst, src,
1817 bset_to_bmap(bset), elim);
1820 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1821 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1823 int i;
1824 int *elim;
1826 if (!bset || !context)
1827 goto error;
1829 if (context->n_eq == 0) {
1830 isl_basic_set_free(context);
1831 return bset;
1834 bset = isl_basic_set_cow(bset);
1835 if (!bset)
1836 goto error;
1838 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1839 if (!elim)
1840 goto error;
1841 set_compute_elimination_index(context, elim);
1842 for (i = 0; i < bset->n_eq; ++i)
1843 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1844 context, elim);
1845 for (i = 0; i < bset->n_ineq; ++i)
1846 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1847 context, elim);
1848 isl_basic_set_free(context);
1849 free(elim);
1850 bset = isl_basic_set_simplify(bset);
1851 bset = isl_basic_set_finalize(bset);
1852 return bset;
1853 error:
1854 isl_basic_set_free(bset);
1855 isl_basic_set_free(context);
1856 return NULL;
1859 /* For each inequality in "ineq" that is a shifted (more relaxed)
1860 * copy of an inequality in "context", mark the corresponding entry
1861 * in "row" with -1.
1862 * If an inequality only has a non-negative constant term, then
1863 * mark it as well.
1865 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1866 __isl_keep isl_basic_set *context, int *row)
1868 struct isl_constraint_index ci;
1869 int n_ineq;
1870 unsigned total;
1871 int k;
1873 if (!ineq || !context)
1874 return isl_stat_error;
1875 if (context->n_ineq == 0)
1876 return isl_stat_ok;
1877 if (setup_constraint_index(&ci, context) < 0)
1878 return isl_stat_error;
1880 n_ineq = isl_mat_rows(ineq);
1881 total = isl_mat_cols(ineq) - 1;
1882 for (k = 0; k < n_ineq; ++k) {
1883 int l;
1884 isl_bool redundant;
1886 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1887 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1888 row[k] = -1;
1889 continue;
1891 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1892 if (redundant < 0)
1893 goto error;
1894 if (!redundant)
1895 continue;
1896 row[k] = -1;
1898 constraint_index_free(&ci);
1899 return isl_stat_ok;
1900 error:
1901 constraint_index_free(&ci);
1902 return isl_stat_error;
1905 static __isl_give isl_basic_set *remove_shifted_constraints(
1906 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1908 struct isl_constraint_index ci;
1909 int k;
1911 if (!bset || !context)
1912 return bset;
1914 if (context->n_ineq == 0)
1915 return bset;
1916 if (setup_constraint_index(&ci, context) < 0)
1917 return bset;
1919 for (k = 0; k < bset->n_ineq; ++k) {
1920 isl_bool redundant;
1922 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1923 if (redundant < 0)
1924 goto error;
1925 if (!redundant)
1926 continue;
1927 bset = isl_basic_set_cow(bset);
1928 if (!bset)
1929 goto error;
1930 isl_basic_set_drop_inequality(bset, k);
1931 --k;
1933 constraint_index_free(&ci);
1934 return bset;
1935 error:
1936 constraint_index_free(&ci);
1937 return bset;
1940 /* Remove constraints from "bmap" that are identical to constraints
1941 * in "context" or that are more relaxed (greater constant term).
1943 * We perform the test for shifted copies on the pure constraints
1944 * in remove_shifted_constraints.
1946 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1947 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1949 isl_basic_set *bset, *bset_context;
1951 if (!bmap || !context)
1952 goto error;
1954 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1955 isl_basic_map_free(context);
1956 return bmap;
1959 context = isl_basic_map_align_divs(context, bmap);
1960 bmap = isl_basic_map_align_divs(bmap, context);
1962 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1963 bset_context = isl_basic_map_underlying_set(context);
1964 bset = remove_shifted_constraints(bset, bset_context);
1965 isl_basic_set_free(bset_context);
1967 bmap = isl_basic_map_overlying_set(bset, bmap);
1969 return bmap;
1970 error:
1971 isl_basic_map_free(bmap);
1972 isl_basic_map_free(context);
1973 return NULL;
1976 /* Does the (linear part of a) constraint "c" involve any of the "len"
1977 * "relevant" dimensions?
1979 static int is_related(isl_int *c, int len, int *relevant)
1981 int i;
1983 for (i = 0; i < len; ++i) {
1984 if (!relevant[i])
1985 continue;
1986 if (!isl_int_is_zero(c[i]))
1987 return 1;
1990 return 0;
1993 /* Drop constraints from "bmap" that do not involve any of
1994 * the dimensions marked "relevant".
1996 static __isl_give isl_basic_map *drop_unrelated_constraints(
1997 __isl_take isl_basic_map *bmap, int *relevant)
1999 int i, dim;
2001 dim = isl_basic_map_dim(bmap, isl_dim_all);
2002 for (i = 0; i < dim; ++i)
2003 if (!relevant[i])
2004 break;
2005 if (i >= dim)
2006 return bmap;
2008 for (i = bmap->n_eq - 1; i >= 0; --i)
2009 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2010 bmap = isl_basic_map_cow(bmap);
2011 if (isl_basic_map_drop_equality(bmap, i) < 0)
2012 return isl_basic_map_free(bmap);
2015 for (i = bmap->n_ineq - 1; i >= 0; --i)
2016 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2017 bmap = isl_basic_map_cow(bmap);
2018 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2019 return isl_basic_map_free(bmap);
2022 return bmap;
2025 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2027 * In particular, for any variable involved in the constraint,
2028 * find the actual group id from before and replace the group
2029 * of the corresponding variable by the minimal group of all
2030 * the variables involved in the constraint considered so far
2031 * (if this minimum is smaller) or replace the minimum by this group
2032 * (if the minimum is larger).
2034 * At the end, all the variables in "c" will (indirectly) point
2035 * to the minimal of the groups that they referred to originally.
2037 static void update_groups(int dim, int *group, isl_int *c)
2039 int j;
2040 int min = dim;
2042 for (j = 0; j < dim; ++j) {
2043 if (isl_int_is_zero(c[j]))
2044 continue;
2045 while (group[j] >= 0 && group[group[j]] != group[j])
2046 group[j] = group[group[j]];
2047 if (group[j] == min)
2048 continue;
2049 if (group[j] < min) {
2050 if (min >= 0 && min < dim)
2051 group[min] = group[j];
2052 min = group[j];
2053 } else
2054 group[group[j]] = min;
2058 /* Allocate an array of groups of variables, one for each variable
2059 * in "context", initialized to zero.
2061 static int *alloc_groups(__isl_keep isl_basic_set *context)
2063 isl_ctx *ctx;
2064 int dim;
2066 dim = isl_basic_set_dim(context, isl_dim_set);
2067 ctx = isl_basic_set_get_ctx(context);
2068 return isl_calloc_array(ctx, int, dim);
2071 /* Drop constraints from "bmap" that only involve variables that are
2072 * not related to any of the variables marked with a "-1" in "group".
2074 * We construct groups of variables that collect variables that
2075 * (indirectly) appear in some common constraint of "bmap".
2076 * Each group is identified by the first variable in the group,
2077 * except for the special group of variables that was already identified
2078 * in the input as -1 (or are related to those variables).
2079 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2080 * otherwise the group of i is the group of group[i].
2082 * We first initialize groups for the remaining variables.
2083 * Then we iterate over the constraints of "bmap" and update the
2084 * group of the variables in the constraint by the smallest group.
2085 * Finally, we resolve indirect references to groups by running over
2086 * the variables.
2088 * After computing the groups, we drop constraints that do not involve
2089 * any variables in the -1 group.
2091 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2092 __isl_take isl_basic_map *bmap, __isl_take int *group)
2094 int dim;
2095 int i;
2096 int last;
2098 if (!bmap)
2099 return NULL;
2101 dim = isl_basic_map_dim(bmap, isl_dim_all);
2103 last = -1;
2104 for (i = 0; i < dim; ++i)
2105 if (group[i] >= 0)
2106 last = group[i] = i;
2107 if (last < 0) {
2108 free(group);
2109 return bmap;
2112 for (i = 0; i < bmap->n_eq; ++i)
2113 update_groups(dim, group, bmap->eq[i] + 1);
2114 for (i = 0; i < bmap->n_ineq; ++i)
2115 update_groups(dim, group, bmap->ineq[i] + 1);
2117 for (i = 0; i < dim; ++i)
2118 if (group[i] >= 0)
2119 group[i] = group[group[i]];
2121 for (i = 0; i < dim; ++i)
2122 group[i] = group[i] == -1;
2124 bmap = drop_unrelated_constraints(bmap, group);
2126 free(group);
2127 return bmap;
2130 /* Drop constraints from "context" that are irrelevant for computing
2131 * the gist of "bset".
2133 * In particular, drop constraints in variables that are not related
2134 * to any of the variables involved in the constraints of "bset"
2135 * in the sense that there is no sequence of constraints that connects them.
2137 * We first mark all variables that appear in "bset" as belonging
2138 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2140 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2141 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2143 int *group;
2144 int dim;
2145 int i, j;
2147 if (!context || !bset)
2148 return isl_basic_set_free(context);
2150 group = alloc_groups(context);
2152 if (!group)
2153 return isl_basic_set_free(context);
2155 dim = isl_basic_set_dim(bset, isl_dim_set);
2156 for (i = 0; i < dim; ++i) {
2157 for (j = 0; j < bset->n_eq; ++j)
2158 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2159 break;
2160 if (j < bset->n_eq) {
2161 group[i] = -1;
2162 continue;
2164 for (j = 0; j < bset->n_ineq; ++j)
2165 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2166 break;
2167 if (j < bset->n_ineq)
2168 group[i] = -1;
2171 return isl_basic_map_drop_unrelated_constraints(context, group);
2174 /* Drop constraints from "context" that are irrelevant for computing
2175 * the gist of the inequalities "ineq".
2176 * Inequalities in "ineq" for which the corresponding element of row
2177 * is set to -1 have already been marked for removal and should be ignored.
2179 * In particular, drop constraints in variables that are not related
2180 * to any of the variables involved in "ineq"
2181 * in the sense that there is no sequence of constraints that connects them.
2183 * We first mark all variables that appear in "bset" as belonging
2184 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2186 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2187 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2189 int *group;
2190 int dim;
2191 int i, j, n;
2193 if (!context || !ineq)
2194 return isl_basic_set_free(context);
2196 group = alloc_groups(context);
2198 if (!group)
2199 return isl_basic_set_free(context);
2201 dim = isl_basic_set_dim(context, isl_dim_set);
2202 n = isl_mat_rows(ineq);
2203 for (i = 0; i < dim; ++i) {
2204 for (j = 0; j < n; ++j) {
2205 if (row[j] < 0)
2206 continue;
2207 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2208 break;
2210 if (j < n)
2211 group[i] = -1;
2214 return isl_basic_map_drop_unrelated_constraints(context, group);
2217 /* Do all "n" entries of "row" contain a negative value?
2219 static int all_neg(int *row, int n)
2221 int i;
2223 for (i = 0; i < n; ++i)
2224 if (row[i] >= 0)
2225 return 0;
2227 return 1;
2230 /* Update the inequalities in "bset" based on the information in "row"
2231 * and "tab".
2233 * In particular, the array "row" contains either -1, meaning that
2234 * the corresponding inequality of "bset" is redundant, or the index
2235 * of an inequality in "tab".
2237 * If the row entry is -1, then drop the inequality.
2238 * Otherwise, if the constraint is marked redundant in the tableau,
2239 * then drop the inequality. Similarly, if it is marked as an equality
2240 * in the tableau, then turn the inequality into an equality and
2241 * perform Gaussian elimination.
2243 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2244 __isl_keep int *row, struct isl_tab *tab)
2246 int i;
2247 unsigned n_ineq;
2248 unsigned n_eq;
2249 int found_equality = 0;
2251 if (!bset)
2252 return NULL;
2253 if (tab && tab->empty)
2254 return isl_basic_set_set_to_empty(bset);
2256 n_ineq = bset->n_ineq;
2257 for (i = n_ineq - 1; i >= 0; --i) {
2258 if (row[i] < 0) {
2259 if (isl_basic_set_drop_inequality(bset, i) < 0)
2260 return isl_basic_set_free(bset);
2261 continue;
2263 if (!tab)
2264 continue;
2265 n_eq = tab->n_eq;
2266 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2267 isl_basic_map_inequality_to_equality(bset, i);
2268 found_equality = 1;
2269 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2270 if (isl_basic_set_drop_inequality(bset, i) < 0)
2271 return isl_basic_set_free(bset);
2275 if (found_equality)
2276 bset = isl_basic_set_gauss(bset, NULL);
2277 bset = isl_basic_set_finalize(bset);
2278 return bset;
2281 /* Update the inequalities in "bset" based on the information in "row"
2282 * and "tab" and free all arguments (other than "bset").
2284 static __isl_give isl_basic_set *update_ineq_free(
2285 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2286 __isl_take isl_basic_set *context, __isl_take int *row,
2287 struct isl_tab *tab)
2289 isl_mat_free(ineq);
2290 isl_basic_set_free(context);
2292 bset = update_ineq(bset, row, tab);
2294 free(row);
2295 isl_tab_free(tab);
2296 return bset;
2299 /* Remove all information from bset that is redundant in the context
2300 * of context.
2301 * "ineq" contains the (possibly transformed) inequalities of "bset",
2302 * in the same order.
2303 * The (explicit) equalities of "bset" are assumed to have been taken
2304 * into account by the transformation such that only the inequalities
2305 * are relevant.
2306 * "context" is assumed not to be empty.
2308 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2309 * A value of -1 means that the inequality is obviously redundant and may
2310 * not even appear in "tab".
2312 * We first mark the inequalities of "bset"
2313 * that are obviously redundant with respect to some inequality in "context".
2314 * Then we remove those constraints from "context" that have become
2315 * irrelevant for computing the gist of "bset".
2316 * Note that this removal of constraints cannot be replaced by
2317 * a factorization because factors in "bset" may still be connected
2318 * to each other through constraints in "context".
2320 * If there are any inequalities left, we construct a tableau for
2321 * the context and then add the inequalities of "bset".
2322 * Before adding these inequalities, we freeze all constraints such that
2323 * they won't be considered redundant in terms of the constraints of "bset".
2324 * Then we detect all redundant constraints (among the
2325 * constraints that weren't frozen), first by checking for redundancy in the
2326 * the tableau and then by checking if replacing a constraint by its negation
2327 * would lead to an empty set. This last step is fairly expensive
2328 * and could be optimized by more reuse of the tableau.
2329 * Finally, we update bset according to the results.
2331 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2332 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2334 int i, r;
2335 int *row = NULL;
2336 isl_ctx *ctx;
2337 isl_basic_set *combined = NULL;
2338 struct isl_tab *tab = NULL;
2339 unsigned n_eq, context_ineq;
2341 if (!bset || !ineq || !context)
2342 goto error;
2344 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2345 isl_basic_set_free(context);
2346 isl_mat_free(ineq);
2347 return bset;
2350 ctx = isl_basic_set_get_ctx(context);
2351 row = isl_calloc_array(ctx, int, bset->n_ineq);
2352 if (!row)
2353 goto error;
2355 if (mark_shifted_constraints(ineq, context, row) < 0)
2356 goto error;
2357 if (all_neg(row, bset->n_ineq))
2358 return update_ineq_free(bset, ineq, context, row, NULL);
2360 context = drop_irrelevant_constraints_marked(context, ineq, row);
2361 if (!context)
2362 goto error;
2363 if (isl_basic_set_plain_is_universe(context))
2364 return update_ineq_free(bset, ineq, context, row, NULL);
2366 n_eq = context->n_eq;
2367 context_ineq = context->n_ineq;
2368 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2369 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2370 tab = isl_tab_from_basic_set(combined, 0);
2371 for (i = 0; i < context_ineq; ++i)
2372 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2373 goto error;
2374 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2375 goto error;
2376 r = context_ineq;
2377 for (i = 0; i < bset->n_ineq; ++i) {
2378 if (row[i] < 0)
2379 continue;
2380 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2381 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2382 goto error;
2383 row[i] = r++;
2385 if (isl_tab_detect_implicit_equalities(tab) < 0)
2386 goto error;
2387 if (isl_tab_detect_redundant(tab) < 0)
2388 goto error;
2389 for (i = bset->n_ineq - 1; i >= 0; --i) {
2390 isl_basic_set *test;
2391 int is_empty;
2393 if (row[i] < 0)
2394 continue;
2395 r = row[i];
2396 if (tab->con[n_eq + r].is_redundant)
2397 continue;
2398 test = isl_basic_set_dup(combined);
2399 if (isl_inequality_negate(test, r) < 0)
2400 test = isl_basic_set_free(test);
2401 test = isl_basic_set_update_from_tab(test, tab);
2402 is_empty = isl_basic_set_is_empty(test);
2403 isl_basic_set_free(test);
2404 if (is_empty < 0)
2405 goto error;
2406 if (is_empty)
2407 tab->con[n_eq + r].is_redundant = 1;
2409 bset = update_ineq_free(bset, ineq, context, row, tab);
2410 if (bset) {
2411 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2412 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2415 isl_basic_set_free(combined);
2416 return bset;
2417 error:
2418 free(row);
2419 isl_mat_free(ineq);
2420 isl_tab_free(tab);
2421 isl_basic_set_free(combined);
2422 isl_basic_set_free(context);
2423 isl_basic_set_free(bset);
2424 return NULL;
2427 /* Extract the inequalities of "bset" as an isl_mat.
2429 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2431 unsigned total;
2432 isl_ctx *ctx;
2433 isl_mat *ineq;
2435 if (!bset)
2436 return NULL;
2438 ctx = isl_basic_set_get_ctx(bset);
2439 total = isl_basic_set_total_dim(bset);
2440 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2441 0, 1 + total);
2443 return ineq;
2446 /* Remove all information from "bset" that is redundant in the context
2447 * of "context", for the case where both "bset" and "context" are
2448 * full-dimensional.
2450 static __isl_give isl_basic_set *uset_gist_uncompressed(
2451 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2453 isl_mat *ineq;
2455 ineq = extract_ineq(bset);
2456 return uset_gist_full(bset, ineq, context);
2459 /* Remove all information from "bset" that is redundant in the context
2460 * of "context", for the case where the combined equalities of
2461 * "bset" and "context" allow for a compression that can be obtained
2462 * by preapplication of "T".
2464 * "bset" itself is not transformed by "T". Instead, the inequalities
2465 * are extracted from "bset" and those are transformed by "T".
2466 * uset_gist_full then determines which of the transformed inequalities
2467 * are redundant with respect to the transformed "context" and removes
2468 * the corresponding inequalities from "bset".
2470 * After preapplying "T" to the inequalities, any common factor is
2471 * removed from the coefficients. If this results in a tightening
2472 * of the constant term, then the same tightening is applied to
2473 * the corresponding untransformed inequality in "bset".
2474 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2476 * g f'(x) + r >= 0
2478 * with 0 <= r < g, then it is equivalent to
2480 * f'(x) >= 0
2482 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2483 * subspace compressed by T since the latter would be transformed to
2485 * g f'(x) >= 0
2487 static __isl_give isl_basic_set *uset_gist_compressed(
2488 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2489 __isl_take isl_mat *T)
2491 isl_ctx *ctx;
2492 isl_mat *ineq;
2493 int i, n_row, n_col;
2494 isl_int rem;
2496 ineq = extract_ineq(bset);
2497 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2498 context = isl_basic_set_preimage(context, T);
2500 if (!ineq || !context)
2501 goto error;
2502 if (isl_basic_set_plain_is_empty(context)) {
2503 isl_mat_free(ineq);
2504 isl_basic_set_free(context);
2505 return isl_basic_set_set_to_empty(bset);
2508 ctx = isl_mat_get_ctx(ineq);
2509 n_row = isl_mat_rows(ineq);
2510 n_col = isl_mat_cols(ineq);
2511 isl_int_init(rem);
2512 for (i = 0; i < n_row; ++i) {
2513 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2514 if (isl_int_is_zero(ctx->normalize_gcd))
2515 continue;
2516 if (isl_int_is_one(ctx->normalize_gcd))
2517 continue;
2518 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2519 ctx->normalize_gcd, n_col - 1);
2520 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2521 isl_int_fdiv_q(ineq->row[i][0],
2522 ineq->row[i][0], ctx->normalize_gcd);
2523 if (isl_int_is_zero(rem))
2524 continue;
2525 bset = isl_basic_set_cow(bset);
2526 if (!bset)
2527 break;
2528 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2530 isl_int_clear(rem);
2532 return uset_gist_full(bset, ineq, context);
2533 error:
2534 isl_mat_free(ineq);
2535 isl_basic_set_free(context);
2536 isl_basic_set_free(bset);
2537 return NULL;
2540 /* Project "bset" onto the variables that are involved in "template".
2542 static __isl_give isl_basic_set *project_onto_involved(
2543 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2545 int i, n;
2547 if (!bset || !template)
2548 return isl_basic_set_free(bset);
2550 n = isl_basic_set_dim(template, isl_dim_set);
2552 for (i = 0; i < n; ++i) {
2553 isl_bool involved;
2555 involved = isl_basic_set_involves_dims(template,
2556 isl_dim_set, i, 1);
2557 if (involved < 0)
2558 return isl_basic_set_free(bset);
2559 if (involved)
2560 continue;
2561 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2564 return bset;
2567 /* Remove all information from bset that is redundant in the context
2568 * of context. In particular, equalities that are linear combinations
2569 * of those in context are removed. Then the inequalities that are
2570 * redundant in the context of the equalities and inequalities of
2571 * context are removed.
2573 * First of all, we drop those constraints from "context"
2574 * that are irrelevant for computing the gist of "bset".
2575 * Alternatively, we could factorize the intersection of "context" and "bset".
2577 * We first compute the intersection of the integer affine hulls
2578 * of "bset" and "context",
2579 * compute the gist inside this intersection and then reduce
2580 * the constraints with respect to the equalities of the context
2581 * that only involve variables already involved in the input.
2583 * If two constraints are mutually redundant, then uset_gist_full
2584 * will remove the second of those constraints. We therefore first
2585 * sort the constraints so that constraints not involving existentially
2586 * quantified variables are given precedence over those that do.
2587 * We have to perform this sorting before the variable compression,
2588 * because that may effect the order of the variables.
2590 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2591 __isl_take isl_basic_set *context)
2593 isl_mat *eq;
2594 isl_mat *T;
2595 isl_basic_set *aff;
2596 isl_basic_set *aff_context;
2597 unsigned total;
2599 if (!bset || !context)
2600 goto error;
2602 context = drop_irrelevant_constraints(context, bset);
2604 bset = isl_basic_set_detect_equalities(bset);
2605 aff = isl_basic_set_copy(bset);
2606 aff = isl_basic_set_plain_affine_hull(aff);
2607 context = isl_basic_set_detect_equalities(context);
2608 aff_context = isl_basic_set_copy(context);
2609 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2610 aff = isl_basic_set_intersect(aff, aff_context);
2611 if (!aff)
2612 goto error;
2613 if (isl_basic_set_plain_is_empty(aff)) {
2614 isl_basic_set_free(bset);
2615 isl_basic_set_free(context);
2616 return aff;
2618 bset = isl_basic_set_sort_constraints(bset);
2619 if (aff->n_eq == 0) {
2620 isl_basic_set_free(aff);
2621 return uset_gist_uncompressed(bset, context);
2623 total = isl_basic_set_total_dim(bset);
2624 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2625 eq = isl_mat_cow(eq);
2626 T = isl_mat_variable_compression(eq, NULL);
2627 isl_basic_set_free(aff);
2628 if (T && T->n_col == 0) {
2629 isl_mat_free(T);
2630 isl_basic_set_free(context);
2631 return isl_basic_set_set_to_empty(bset);
2634 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2635 aff_context = project_onto_involved(aff_context, bset);
2637 bset = uset_gist_compressed(bset, context, T);
2638 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2640 if (bset) {
2641 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2642 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2645 return bset;
2646 error:
2647 isl_basic_set_free(bset);
2648 isl_basic_set_free(context);
2649 return NULL;
2652 /* Return the number of equality constraints in "bmap" that involve
2653 * local variables. This function assumes that Gaussian elimination
2654 * has been applied to the equality constraints.
2656 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2658 int i;
2659 int total, n_div;
2661 if (!bmap)
2662 return -1;
2664 if (bmap->n_eq == 0)
2665 return 0;
2667 total = isl_basic_map_dim(bmap, isl_dim_all);
2668 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2669 total -= n_div;
2671 for (i = 0; i < bmap->n_eq; ++i)
2672 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2673 n_div) == -1)
2674 return i;
2676 return bmap->n_eq;
2679 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2680 * The constraints are assumed not to involve any local variables.
2682 static __isl_give isl_basic_map *basic_map_from_equalities(
2683 __isl_take isl_space *space, __isl_take isl_mat *eq)
2685 int i, k;
2686 isl_basic_map *bmap = NULL;
2688 if (!space || !eq)
2689 goto error;
2691 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2692 isl_die(isl_space_get_ctx(space), isl_error_internal,
2693 "unexpected number of columns", goto error);
2695 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2696 0, eq->n_row, 0);
2697 for (i = 0; i < eq->n_row; ++i) {
2698 k = isl_basic_map_alloc_equality(bmap);
2699 if (k < 0)
2700 goto error;
2701 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2704 isl_space_free(space);
2705 isl_mat_free(eq);
2706 return bmap;
2707 error:
2708 isl_space_free(space);
2709 isl_mat_free(eq);
2710 isl_basic_map_free(bmap);
2711 return NULL;
2714 /* Construct and return a variable compression based on the equality
2715 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2716 * "n1" is the number of (initial) equality constraints in "bmap1"
2717 * that do involve local variables.
2718 * "n2" is the number of (initial) equality constraints in "bmap2"
2719 * that do involve local variables.
2720 * "total" is the total number of other variables.
2721 * This function assumes that Gaussian elimination
2722 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2723 * such that the equality constraints not involving local variables
2724 * are those that start at "n1" or "n2".
2726 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2727 * then simply compute the compression based on the equality constraints
2728 * in the other basic map.
2729 * Otherwise, combine the equality constraints from both into a new
2730 * basic map such that Gaussian elimination can be applied to this combination
2731 * and then construct a variable compression from the resulting
2732 * equality constraints.
2734 static __isl_give isl_mat *combined_variable_compression(
2735 __isl_keep isl_basic_map *bmap1, int n1,
2736 __isl_keep isl_basic_map *bmap2, int n2, int total)
2738 isl_ctx *ctx;
2739 isl_mat *E1, *E2, *V;
2740 isl_basic_map *bmap;
2742 ctx = isl_basic_map_get_ctx(bmap1);
2743 if (bmap1->n_eq == n1) {
2744 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2745 n2, bmap2->n_eq - n2, 0, 1 + total);
2746 return isl_mat_variable_compression(E2, NULL);
2748 if (bmap2->n_eq == n2) {
2749 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2750 n1, bmap1->n_eq - n1, 0, 1 + total);
2751 return isl_mat_variable_compression(E1, NULL);
2753 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2754 n1, bmap1->n_eq - n1, 0, 1 + total);
2755 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2756 n2, bmap2->n_eq - n2, 0, 1 + total);
2757 E1 = isl_mat_concat(E1, E2);
2758 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2759 bmap = isl_basic_map_gauss(bmap, NULL);
2760 if (!bmap)
2761 return NULL;
2762 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2763 V = isl_mat_variable_compression(E1, NULL);
2764 isl_basic_map_free(bmap);
2766 return V;
2769 /* Extract the stride constraints from "bmap", compressed
2770 * with respect to both the stride constraints in "context" and
2771 * the remaining equality constraints in both "bmap" and "context".
2772 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2773 * "context_n_eq" is the number of (initial) stride constraints in "context".
2775 * Let x be all variables in "bmap" (and "context") other than the local
2776 * variables. First compute a variable compression
2778 * x = V x'
2780 * based on the non-stride equality constraints in "bmap" and "context".
2781 * Consider the stride constraints of "context",
2783 * A(x) + B(y) = 0
2785 * with y the local variables and plug in the variable compression,
2786 * resulting in
2788 * A(V x') + B(y) = 0
2790 * Use these constraints to compute a parameter compression on x'
2792 * x' = T x''
2794 * Now consider the stride constraints of "bmap"
2796 * C(x) + D(y) = 0
2798 * and plug in x = V*T x''.
2799 * That is, return A = [C*V*T D].
2801 static __isl_give isl_mat *extract_compressed_stride_constraints(
2802 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2803 __isl_keep isl_basic_map *context, int context_n_eq)
2805 int total, n_div;
2806 isl_ctx *ctx;
2807 isl_mat *A, *B, *T, *V;
2809 total = isl_basic_map_dim(context, isl_dim_all);
2810 n_div = isl_basic_map_dim(context, isl_dim_div);
2811 total -= n_div;
2813 ctx = isl_basic_map_get_ctx(bmap);
2815 V = combined_variable_compression(bmap, bmap_n_eq,
2816 context, context_n_eq, total);
2818 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2819 B = isl_mat_sub_alloc6(ctx, context->eq,
2820 0, context_n_eq, 1 + total, n_div);
2821 A = isl_mat_product(A, isl_mat_copy(V));
2822 T = isl_mat_parameter_compression_ext(A, B);
2823 T = isl_mat_product(V, T);
2825 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2826 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2828 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2829 0, bmap_n_eq, 0, 1 + total + n_div);
2830 A = isl_mat_product(A, T);
2832 return A;
2835 /* Remove the prime factors from *g that have an exponent that
2836 * is strictly smaller than the exponent in "c".
2837 * All exponents in *g are known to be smaller than or equal
2838 * to those in "c".
2840 * That is, if *g is equal to
2842 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2844 * and "c" is equal to
2846 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2848 * then update *g to
2850 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2851 * p_n^{e_n * (e_n = f_n)}
2853 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2854 * neither does the gcd of *g and c / *g.
2855 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2856 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2857 * Dividing *g by this gcd therefore strictly reduces the exponent
2858 * of the prime factors that need to be removed, while leaving the
2859 * other prime factors untouched.
2860 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2861 * removes all undesired factors, without removing any others.
2863 static void remove_incomplete_powers(isl_int *g, isl_int c)
2865 isl_int t;
2867 isl_int_init(t);
2868 for (;;) {
2869 isl_int_divexact(t, c, *g);
2870 isl_int_gcd(t, t, *g);
2871 if (isl_int_is_one(t))
2872 break;
2873 isl_int_divexact(*g, *g, t);
2875 isl_int_clear(t);
2878 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2879 * of the same stride constraints in a compressed space that exploits
2880 * all equalities in the context and the other equalities in "bmap".
2882 * If the stride constraints of "bmap" are of the form
2884 * C(x) + D(y) = 0
2886 * then A is of the form
2888 * B(x') + D(y) = 0
2890 * If any of these constraints involves only a single local variable y,
2891 * then the constraint appears as
2893 * f(x) + m y_i = 0
2895 * in "bmap" and as
2897 * h(x') + m y_i = 0
2899 * in "A".
2901 * Let g be the gcd of m and the coefficients of h.
2902 * Then, in particular, g is a divisor of the coefficients of h and
2904 * f(x) = h(x')
2906 * is known to be a multiple of g.
2907 * If some prime factor in m appears with the same exponent in g,
2908 * then it can be removed from m because f(x) is already known
2909 * to be a multiple of g and therefore in particular of this power
2910 * of the prime factors.
2911 * Prime factors that appear with a smaller exponent in g cannot
2912 * be removed from m.
2913 * Let g' be the divisor of g containing all prime factors that
2914 * appear with the same exponent in m and g, then
2916 * f(x) + m y_i = 0
2918 * can be replaced by
2920 * f(x) + m/g' y_i' = 0
2922 * Note that (if g' != 1) this changes the explicit representation
2923 * of y_i to that of y_i', so the integer division at position i
2924 * is marked unknown and later recomputed by a call to
2925 * isl_basic_map_gauss.
2927 static __isl_give isl_basic_map *reduce_stride_constraints(
2928 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2930 int i;
2931 int total, n_div;
2932 int any = 0;
2933 isl_int gcd;
2935 if (!bmap || !A)
2936 return isl_basic_map_free(bmap);
2938 total = isl_basic_map_dim(bmap, isl_dim_all);
2939 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2940 total -= n_div;
2942 isl_int_init(gcd);
2943 for (i = 0; i < n; ++i) {
2944 int div;
2946 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2947 if (div < 0)
2948 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2949 "equality constraints modified unexpectedly",
2950 goto error);
2951 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
2952 n_div - div - 1) != -1)
2953 continue;
2954 if (isl_mat_row_gcd(A, i, &gcd) < 0)
2955 goto error;
2956 if (isl_int_is_one(gcd))
2957 continue;
2958 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2959 if (isl_int_is_one(gcd))
2960 continue;
2961 isl_int_divexact(bmap->eq[i][1 + total + div],
2962 bmap->eq[i][1 + total + div], gcd);
2963 bmap = isl_basic_map_mark_div_unknown(bmap, div);
2964 if (!bmap)
2965 goto error;
2966 any = 1;
2968 isl_int_clear(gcd);
2970 if (any)
2971 bmap = isl_basic_map_gauss(bmap, NULL);
2973 return bmap;
2974 error:
2975 isl_int_clear(gcd);
2976 isl_basic_map_free(bmap);
2977 return NULL;
2980 /* Simplify the stride constraints in "bmap" based on
2981 * the remaining equality constraints in "bmap" and all equality
2982 * constraints in "context".
2983 * Only do this if both "bmap" and "context" have stride constraints.
2985 * First extract a copy of the stride constraints in "bmap" in a compressed
2986 * space exploiting all the other equality constraints and then
2987 * use this compressed copy to simplify the original stride constraints.
2989 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
2990 __isl_keep isl_basic_map *context)
2992 int bmap_n_eq, context_n_eq;
2993 isl_mat *A;
2995 if (!bmap || !context)
2996 return isl_basic_map_free(bmap);
2998 bmap_n_eq = n_div_eq(bmap);
2999 context_n_eq = n_div_eq(context);
3001 if (bmap_n_eq < 0 || context_n_eq < 0)
3002 return isl_basic_map_free(bmap);
3003 if (bmap_n_eq == 0 || context_n_eq == 0)
3004 return bmap;
3006 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3007 context, context_n_eq);
3008 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3010 isl_mat_free(A);
3012 return bmap;
3015 /* Return a basic map that has the same intersection with "context" as "bmap"
3016 * and that is as "simple" as possible.
3018 * The core computation is performed on the pure constraints.
3019 * When we add back the meaning of the integer divisions, we need
3020 * to (re)introduce the div constraints. If we happen to have
3021 * discovered that some of these integer divisions are equal to
3022 * some affine combination of other variables, then these div
3023 * constraints may end up getting simplified in terms of the equalities,
3024 * resulting in extra inequalities on the other variables that
3025 * may have been removed already or that may not even have been
3026 * part of the input. We try and remove those constraints of
3027 * this form that are most obviously redundant with respect to
3028 * the context. We also remove those div constraints that are
3029 * redundant with respect to the other constraints in the result.
3031 * The stride constraints among the equality constraints in "bmap" are
3032 * also simplified with respecting to the other equality constraints
3033 * in "bmap" and with respect to all equality constraints in "context".
3035 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3036 __isl_take isl_basic_map *context)
3038 isl_basic_set *bset, *eq;
3039 isl_basic_map *eq_bmap;
3040 unsigned total, n_div, extra, n_eq, n_ineq;
3042 if (!bmap || !context)
3043 goto error;
3045 if (isl_basic_map_plain_is_universe(bmap)) {
3046 isl_basic_map_free(context);
3047 return bmap;
3049 if (isl_basic_map_plain_is_empty(context)) {
3050 isl_space *space = isl_basic_map_get_space(bmap);
3051 isl_basic_map_free(bmap);
3052 isl_basic_map_free(context);
3053 return isl_basic_map_universe(space);
3055 if (isl_basic_map_plain_is_empty(bmap)) {
3056 isl_basic_map_free(context);
3057 return bmap;
3060 bmap = isl_basic_map_remove_redundancies(bmap);
3061 context = isl_basic_map_remove_redundancies(context);
3062 context = isl_basic_map_align_divs(context, bmap);
3063 if (!context)
3064 goto error;
3066 n_div = isl_basic_map_dim(context, isl_dim_div);
3067 total = isl_basic_map_dim(bmap, isl_dim_all);
3068 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3070 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3071 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3072 bset = uset_gist(bset,
3073 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3074 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3076 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3077 isl_basic_set_plain_is_empty(bset)) {
3078 isl_basic_map_free(context);
3079 return isl_basic_map_overlying_set(bset, bmap);
3082 n_eq = bset->n_eq;
3083 n_ineq = bset->n_ineq;
3084 eq = isl_basic_set_copy(bset);
3085 eq = isl_basic_set_cow(eq);
3086 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3087 eq = isl_basic_set_free(eq);
3088 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3089 bset = isl_basic_set_free(bset);
3091 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3092 eq_bmap = gist_strides(eq_bmap, context);
3093 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3094 bmap = isl_basic_map_overlying_set(bset, bmap);
3095 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3096 bmap = isl_basic_map_remove_redundancies(bmap);
3098 return bmap;
3099 error:
3100 isl_basic_map_free(bmap);
3101 isl_basic_map_free(context);
3102 return NULL;
3106 * Assumes context has no implicit divs.
3108 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3109 __isl_take isl_basic_map *context)
3111 int i;
3113 if (!map || !context)
3114 goto error;
3116 if (isl_basic_map_plain_is_empty(context)) {
3117 isl_space *space = isl_map_get_space(map);
3118 isl_map_free(map);
3119 isl_basic_map_free(context);
3120 return isl_map_universe(space);
3123 context = isl_basic_map_remove_redundancies(context);
3124 map = isl_map_cow(map);
3125 if (!map || !context)
3126 goto error;
3127 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3128 map = isl_map_compute_divs(map);
3129 if (!map)
3130 goto error;
3131 for (i = map->n - 1; i >= 0; --i) {
3132 map->p[i] = isl_basic_map_gist(map->p[i],
3133 isl_basic_map_copy(context));
3134 if (!map->p[i])
3135 goto error;
3136 if (isl_basic_map_plain_is_empty(map->p[i])) {
3137 isl_basic_map_free(map->p[i]);
3138 if (i != map->n - 1)
3139 map->p[i] = map->p[map->n - 1];
3140 map->n--;
3143 isl_basic_map_free(context);
3144 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3145 return map;
3146 error:
3147 isl_map_free(map);
3148 isl_basic_map_free(context);
3149 return NULL;
3152 /* Drop all inequalities from "bmap" that also appear in "context".
3153 * "context" is assumed to have only known local variables and
3154 * the initial local variables of "bmap" are assumed to be the same
3155 * as those of "context".
3156 * The constraints of both "bmap" and "context" are assumed
3157 * to have been sorted using isl_basic_map_sort_constraints.
3159 * Run through the inequality constraints of "bmap" and "context"
3160 * in sorted order.
3161 * If a constraint of "bmap" involves variables not in "context",
3162 * then it cannot appear in "context".
3163 * If a matching constraint is found, it is removed from "bmap".
3165 static __isl_give isl_basic_map *drop_inequalities(
3166 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3168 int i1, i2;
3169 unsigned total, extra;
3171 if (!bmap || !context)
3172 return isl_basic_map_free(bmap);
3174 total = isl_basic_map_total_dim(context);
3175 extra = isl_basic_map_total_dim(bmap) - total;
3177 i1 = bmap->n_ineq - 1;
3178 i2 = context->n_ineq - 1;
3179 while (bmap && i1 >= 0 && i2 >= 0) {
3180 int cmp;
3182 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3183 extra) != -1) {
3184 --i1;
3185 continue;
3187 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3188 context->ineq[i2]);
3189 if (cmp < 0) {
3190 --i2;
3191 continue;
3193 if (cmp > 0) {
3194 --i1;
3195 continue;
3197 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3198 bmap = isl_basic_map_cow(bmap);
3199 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3200 bmap = isl_basic_map_free(bmap);
3202 --i1;
3203 --i2;
3206 return bmap;
3209 /* Drop all equalities from "bmap" that also appear in "context".
3210 * "context" is assumed to have only known local variables and
3211 * the initial local variables of "bmap" are assumed to be the same
3212 * as those of "context".
3214 * Run through the equality constraints of "bmap" and "context"
3215 * in sorted order.
3216 * If a constraint of "bmap" involves variables not in "context",
3217 * then it cannot appear in "context".
3218 * If a matching constraint is found, it is removed from "bmap".
3220 static __isl_give isl_basic_map *drop_equalities(
3221 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3223 int i1, i2;
3224 unsigned total, extra;
3226 if (!bmap || !context)
3227 return isl_basic_map_free(bmap);
3229 total = isl_basic_map_total_dim(context);
3230 extra = isl_basic_map_total_dim(bmap) - total;
3232 i1 = bmap->n_eq - 1;
3233 i2 = context->n_eq - 1;
3235 while (bmap && i1 >= 0 && i2 >= 0) {
3236 int last1, last2;
3238 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3239 extra) != -1)
3240 break;
3241 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3242 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3243 if (last1 > last2) {
3244 --i2;
3245 continue;
3247 if (last1 < last2) {
3248 --i1;
3249 continue;
3251 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3252 bmap = isl_basic_map_cow(bmap);
3253 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3254 bmap = isl_basic_map_free(bmap);
3256 --i1;
3257 --i2;
3260 return bmap;
3263 /* Remove the constraints in "context" from "bmap".
3264 * "context" is assumed to have explicit representations
3265 * for all local variables.
3267 * First align the divs of "bmap" to those of "context" and
3268 * sort the constraints. Then drop all constraints from "bmap"
3269 * that appear in "context".
3271 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3272 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3274 isl_bool done, known;
3276 done = isl_basic_map_plain_is_universe(context);
3277 if (done == isl_bool_false)
3278 done = isl_basic_map_plain_is_universe(bmap);
3279 if (done == isl_bool_false)
3280 done = isl_basic_map_plain_is_empty(context);
3281 if (done == isl_bool_false)
3282 done = isl_basic_map_plain_is_empty(bmap);
3283 if (done < 0)
3284 goto error;
3285 if (done) {
3286 isl_basic_map_free(context);
3287 return bmap;
3289 known = isl_basic_map_divs_known(context);
3290 if (known < 0)
3291 goto error;
3292 if (!known)
3293 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3294 "context has unknown divs", goto error);
3296 bmap = isl_basic_map_align_divs(bmap, context);
3297 bmap = isl_basic_map_gauss(bmap, NULL);
3298 bmap = isl_basic_map_sort_constraints(bmap);
3299 context = isl_basic_map_sort_constraints(context);
3301 bmap = drop_inequalities(bmap, context);
3302 bmap = drop_equalities(bmap, context);
3304 isl_basic_map_free(context);
3305 bmap = isl_basic_map_finalize(bmap);
3306 return bmap;
3307 error:
3308 isl_basic_map_free(bmap);
3309 isl_basic_map_free(context);
3310 return NULL;
3313 /* Replace "map" by the disjunct at position "pos" and free "context".
3315 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3316 int pos, __isl_take isl_basic_map *context)
3318 isl_basic_map *bmap;
3320 bmap = isl_basic_map_copy(map->p[pos]);
3321 isl_map_free(map);
3322 isl_basic_map_free(context);
3323 return isl_map_from_basic_map(bmap);
3326 /* Remove the constraints in "context" from "map".
3327 * If any of the disjuncts in the result turns out to be the universe,
3328 * then return this universe.
3329 * "context" is assumed to have explicit representations
3330 * for all local variables.
3332 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3333 __isl_take isl_basic_map *context)
3335 int i;
3336 isl_bool univ, known;
3338 univ = isl_basic_map_plain_is_universe(context);
3339 if (univ < 0)
3340 goto error;
3341 if (univ) {
3342 isl_basic_map_free(context);
3343 return map;
3345 known = isl_basic_map_divs_known(context);
3346 if (known < 0)
3347 goto error;
3348 if (!known)
3349 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3350 "context has unknown divs", goto error);
3352 map = isl_map_cow(map);
3353 if (!map)
3354 goto error;
3355 for (i = 0; i < map->n; ++i) {
3356 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3357 isl_basic_map_copy(context));
3358 univ = isl_basic_map_plain_is_universe(map->p[i]);
3359 if (univ < 0)
3360 goto error;
3361 if (univ && map->n > 1)
3362 return replace_by_disjunct(map, i, context);
3365 isl_basic_map_free(context);
3366 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3367 if (map->n > 1)
3368 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3369 return map;
3370 error:
3371 isl_map_free(map);
3372 isl_basic_map_free(context);
3373 return NULL;
3376 /* Remove the constraints in "context" from "set".
3377 * If any of the disjuncts in the result turns out to be the universe,
3378 * then return this universe.
3379 * "context" is assumed to have explicit representations
3380 * for all local variables.
3382 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3383 __isl_take isl_basic_set *context)
3385 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3386 bset_to_bmap(context)));
3389 /* Remove the constraints in "context" from "map".
3390 * If any of the disjuncts in the result turns out to be the universe,
3391 * then return this universe.
3392 * "context" is assumed to consist of a single disjunct and
3393 * to have explicit representations for all local variables.
3395 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3396 __isl_take isl_map *context)
3398 isl_basic_map *hull;
3400 hull = isl_map_unshifted_simple_hull(context);
3401 return isl_map_plain_gist_basic_map(map, hull);
3404 /* Replace "map" by a universe map in the same space and free "drop".
3406 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3407 __isl_take isl_map *drop)
3409 isl_map *res;
3411 res = isl_map_universe(isl_map_get_space(map));
3412 isl_map_free(map);
3413 isl_map_free(drop);
3414 return res;
3417 /* Return a map that has the same intersection with "context" as "map"
3418 * and that is as "simple" as possible.
3420 * If "map" is already the universe, then we cannot make it any simpler.
3421 * Similarly, if "context" is the universe, then we cannot exploit it
3422 * to simplify "map"
3423 * If "map" and "context" are identical to each other, then we can
3424 * return the corresponding universe.
3426 * If either "map" or "context" consists of multiple disjuncts,
3427 * then check if "context" happens to be a subset of "map",
3428 * in which case all constraints can be removed.
3429 * In case of multiple disjuncts, the standard procedure
3430 * may not be able to detect that all constraints can be removed.
3432 * If none of these cases apply, we have to work a bit harder.
3433 * During this computation, we make use of a single disjunct context,
3434 * so if the original context consists of more than one disjunct
3435 * then we need to approximate the context by a single disjunct set.
3436 * Simply taking the simple hull may drop constraints that are
3437 * only implicitly available in each disjunct. We therefore also
3438 * look for constraints among those defining "map" that are valid
3439 * for the context. These can then be used to simplify away
3440 * the corresponding constraints in "map".
3442 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3443 __isl_take isl_map *context)
3445 int equal;
3446 int is_universe;
3447 int single_disjunct_map, single_disjunct_context;
3448 isl_bool subset;
3449 isl_basic_map *hull;
3451 is_universe = isl_map_plain_is_universe(map);
3452 if (is_universe >= 0 && !is_universe)
3453 is_universe = isl_map_plain_is_universe(context);
3454 if (is_universe < 0)
3455 goto error;
3456 if (is_universe) {
3457 isl_map_free(context);
3458 return map;
3461 equal = isl_map_plain_is_equal(map, context);
3462 if (equal < 0)
3463 goto error;
3464 if (equal)
3465 return replace_by_universe(map, context);
3467 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3468 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3469 if (!single_disjunct_map || !single_disjunct_context) {
3470 subset = isl_map_is_subset(context, map);
3471 if (subset < 0)
3472 goto error;
3473 if (subset)
3474 return replace_by_universe(map, context);
3477 context = isl_map_compute_divs(context);
3478 if (!context)
3479 goto error;
3480 if (single_disjunct_context) {
3481 hull = isl_map_simple_hull(context);
3482 } else {
3483 isl_ctx *ctx;
3484 isl_map_list *list;
3486 ctx = isl_map_get_ctx(map);
3487 list = isl_map_list_alloc(ctx, 2);
3488 list = isl_map_list_add(list, isl_map_copy(context));
3489 list = isl_map_list_add(list, isl_map_copy(map));
3490 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3491 list);
3493 return isl_map_gist_basic_map(map, hull);
3494 error:
3495 isl_map_free(map);
3496 isl_map_free(context);
3497 return NULL;
3500 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3501 __isl_take isl_map *context)
3503 return isl_map_align_params_map_map_and(map, context, &map_gist);
3506 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3507 struct isl_basic_set *context)
3509 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3510 bset_to_bmap(context)));
3513 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3514 __isl_take isl_basic_set *context)
3516 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3517 bset_to_bmap(context)));
3520 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3521 __isl_take isl_basic_set *context)
3523 isl_space *space = isl_set_get_space(set);
3524 isl_basic_set *dom_context = isl_basic_set_universe(space);
3525 dom_context = isl_basic_set_intersect_params(dom_context, context);
3526 return isl_set_gist_basic_set(set, dom_context);
3529 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3530 __isl_take isl_set *context)
3532 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3535 /* Compute the gist of "bmap" with respect to the constraints "context"
3536 * on the domain.
3538 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3539 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3541 isl_space *space = isl_basic_map_get_space(bmap);
3542 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3544 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3545 return isl_basic_map_gist(bmap, bmap_context);
3548 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3549 __isl_take isl_set *context)
3551 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3552 map_context = isl_map_intersect_domain(map_context, context);
3553 return isl_map_gist(map, map_context);
3556 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3557 __isl_take isl_set *context)
3559 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3560 map_context = isl_map_intersect_range(map_context, context);
3561 return isl_map_gist(map, map_context);
3564 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3565 __isl_take isl_set *context)
3567 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3568 map_context = isl_map_intersect_params(map_context, context);
3569 return isl_map_gist(map, map_context);
3572 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3573 __isl_take isl_set *context)
3575 return isl_map_gist_params(set, context);
3578 /* Quick check to see if two basic maps are disjoint.
3579 * In particular, we reduce the equalities and inequalities of
3580 * one basic map in the context of the equalities of the other
3581 * basic map and check if we get a contradiction.
3583 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3584 __isl_keep isl_basic_map *bmap2)
3586 struct isl_vec *v = NULL;
3587 int *elim = NULL;
3588 unsigned total;
3589 int i;
3591 if (!bmap1 || !bmap2)
3592 return isl_bool_error;
3593 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3594 return isl_bool_error);
3595 if (bmap1->n_div || bmap2->n_div)
3596 return isl_bool_false;
3597 if (!bmap1->n_eq && !bmap2->n_eq)
3598 return isl_bool_false;
3600 total = isl_space_dim(bmap1->dim, isl_dim_all);
3601 if (total == 0)
3602 return isl_bool_false;
3603 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3604 if (!v)
3605 goto error;
3606 elim = isl_alloc_array(bmap1->ctx, int, total);
3607 if (!elim)
3608 goto error;
3609 compute_elimination_index(bmap1, elim);
3610 for (i = 0; i < bmap2->n_eq; ++i) {
3611 int reduced;
3612 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3613 bmap1, elim);
3614 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3615 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3616 goto disjoint;
3618 for (i = 0; i < bmap2->n_ineq; ++i) {
3619 int reduced;
3620 reduced = reduced_using_equalities(v->block.data,
3621 bmap2->ineq[i], bmap1, elim);
3622 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3623 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3624 goto disjoint;
3626 compute_elimination_index(bmap2, elim);
3627 for (i = 0; i < bmap1->n_ineq; ++i) {
3628 int reduced;
3629 reduced = reduced_using_equalities(v->block.data,
3630 bmap1->ineq[i], bmap2, elim);
3631 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3632 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3633 goto disjoint;
3635 isl_vec_free(v);
3636 free(elim);
3637 return isl_bool_false;
3638 disjoint:
3639 isl_vec_free(v);
3640 free(elim);
3641 return isl_bool_true;
3642 error:
3643 isl_vec_free(v);
3644 free(elim);
3645 return isl_bool_error;
3648 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3649 __isl_keep isl_basic_set *bset2)
3651 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3652 bset_to_bmap(bset2));
3655 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3657 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3658 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3659 __isl_keep isl_basic_map *bmap2))
3661 int i, j;
3663 if (!map1 || !map2)
3664 return isl_bool_error;
3666 for (i = 0; i < map1->n; ++i) {
3667 for (j = 0; j < map2->n; ++j) {
3668 isl_bool d = test(map1->p[i], map2->p[j]);
3669 if (d != isl_bool_true)
3670 return d;
3674 return isl_bool_true;
3677 /* Are "map1" and "map2" obviously disjoint, based on information
3678 * that can be derived without looking at the individual basic maps?
3680 * In particular, if one of them is empty or if they live in different spaces
3681 * (ignoring parameters), then they are clearly disjoint.
3683 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3684 __isl_keep isl_map *map2)
3686 isl_bool disjoint;
3687 isl_bool match;
3689 if (!map1 || !map2)
3690 return isl_bool_error;
3692 disjoint = isl_map_plain_is_empty(map1);
3693 if (disjoint < 0 || disjoint)
3694 return disjoint;
3696 disjoint = isl_map_plain_is_empty(map2);
3697 if (disjoint < 0 || disjoint)
3698 return disjoint;
3700 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3701 map2->dim, isl_dim_in);
3702 if (match < 0 || !match)
3703 return match < 0 ? isl_bool_error : isl_bool_true;
3705 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3706 map2->dim, isl_dim_out);
3707 if (match < 0 || !match)
3708 return match < 0 ? isl_bool_error : isl_bool_true;
3710 return isl_bool_false;
3713 /* Are "map1" and "map2" obviously disjoint?
3715 * If one of them is empty or if they live in different spaces (ignoring
3716 * parameters), then they are clearly disjoint.
3717 * This is checked by isl_map_plain_is_disjoint_global.
3719 * If they have different parameters, then we skip any further tests.
3721 * If they are obviously equal, but not obviously empty, then we will
3722 * not be able to detect if they are disjoint.
3724 * Otherwise we check if each basic map in "map1" is obviously disjoint
3725 * from each basic map in "map2".
3727 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3728 __isl_keep isl_map *map2)
3730 isl_bool disjoint;
3731 isl_bool intersect;
3732 isl_bool match;
3734 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3735 if (disjoint < 0 || disjoint)
3736 return disjoint;
3738 match = isl_map_has_equal_params(map1, map2);
3739 if (match < 0 || !match)
3740 return match < 0 ? isl_bool_error : isl_bool_false;
3742 intersect = isl_map_plain_is_equal(map1, map2);
3743 if (intersect < 0 || intersect)
3744 return intersect < 0 ? isl_bool_error : isl_bool_false;
3746 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3749 /* Are "map1" and "map2" disjoint?
3750 * The parameters are assumed to have been aligned.
3752 * In particular, check whether all pairs of basic maps are disjoint.
3754 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3755 __isl_keep isl_map *map2)
3757 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3760 /* Are "map1" and "map2" disjoint?
3762 * They are disjoint if they are "obviously disjoint" or if one of them
3763 * is empty. Otherwise, they are not disjoint if one of them is universal.
3764 * If the two inputs are (obviously) equal and not empty, then they are
3765 * not disjoint.
3766 * If none of these cases apply, then check if all pairs of basic maps
3767 * are disjoint after aligning the parameters.
3769 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3771 isl_bool disjoint;
3772 isl_bool intersect;
3774 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3775 if (disjoint < 0 || disjoint)
3776 return disjoint;
3778 disjoint = isl_map_is_empty(map1);
3779 if (disjoint < 0 || disjoint)
3780 return disjoint;
3782 disjoint = isl_map_is_empty(map2);
3783 if (disjoint < 0 || disjoint)
3784 return disjoint;
3786 intersect = isl_map_plain_is_universe(map1);
3787 if (intersect < 0 || intersect)
3788 return intersect < 0 ? isl_bool_error : isl_bool_false;
3790 intersect = isl_map_plain_is_universe(map2);
3791 if (intersect < 0 || intersect)
3792 return intersect < 0 ? isl_bool_error : isl_bool_false;
3794 intersect = isl_map_plain_is_equal(map1, map2);
3795 if (intersect < 0 || intersect)
3796 return isl_bool_not(intersect);
3798 return isl_map_align_params_map_map_and_test(map1, map2,
3799 &isl_map_is_disjoint_aligned);
3802 /* Are "bmap1" and "bmap2" disjoint?
3804 * They are disjoint if they are "obviously disjoint" or if one of them
3805 * is empty. Otherwise, they are not disjoint if one of them is universal.
3806 * If none of these cases apply, we compute the intersection and see if
3807 * the result is empty.
3809 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3810 __isl_keep isl_basic_map *bmap2)
3812 isl_bool disjoint;
3813 isl_bool intersect;
3814 isl_basic_map *test;
3816 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3817 if (disjoint < 0 || disjoint)
3818 return disjoint;
3820 disjoint = isl_basic_map_is_empty(bmap1);
3821 if (disjoint < 0 || disjoint)
3822 return disjoint;
3824 disjoint = isl_basic_map_is_empty(bmap2);
3825 if (disjoint < 0 || disjoint)
3826 return disjoint;
3828 intersect = isl_basic_map_plain_is_universe(bmap1);
3829 if (intersect < 0 || intersect)
3830 return intersect < 0 ? isl_bool_error : isl_bool_false;
3832 intersect = isl_basic_map_plain_is_universe(bmap2);
3833 if (intersect < 0 || intersect)
3834 return intersect < 0 ? isl_bool_error : isl_bool_false;
3836 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3837 isl_basic_map_copy(bmap2));
3838 disjoint = isl_basic_map_is_empty(test);
3839 isl_basic_map_free(test);
3841 return disjoint;
3844 /* Are "bset1" and "bset2" disjoint?
3846 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3847 __isl_keep isl_basic_set *bset2)
3849 return isl_basic_map_is_disjoint(bset1, bset2);
3852 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3853 __isl_keep isl_set *set2)
3855 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3858 /* Are "set1" and "set2" disjoint?
3860 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3862 return isl_map_is_disjoint(set1, set2);
3865 /* Is "v" equal to 0, 1 or -1?
3867 static int is_zero_or_one(isl_int v)
3869 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3872 /* Check if we can combine a given div with lower bound l and upper
3873 * bound u with some other div and if so return that other div.
3874 * Otherwise return -1.
3876 * We first check that
3877 * - the bounds are opposites of each other (except for the constant
3878 * term)
3879 * - the bounds do not reference any other div
3880 * - no div is defined in terms of this div
3882 * Let m be the size of the range allowed on the div by the bounds.
3883 * That is, the bounds are of the form
3885 * e <= a <= e + m - 1
3887 * with e some expression in the other variables.
3888 * We look for another div b such that no third div is defined in terms
3889 * of this second div b and such that in any constraint that contains
3890 * a (except for the given lower and upper bound), also contains b
3891 * with a coefficient that is m times that of b.
3892 * That is, all constraints (except for the lower and upper bound)
3893 * are of the form
3895 * e + f (a + m b) >= 0
3897 * Furthermore, in the constraints that only contain b, the coefficient
3898 * of b should be equal to 1 or -1.
3899 * If so, we return b so that "a + m b" can be replaced by
3900 * a single div "c = a + m b".
3902 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
3903 unsigned div, unsigned l, unsigned u)
3905 int i, j;
3906 unsigned dim;
3907 int coalesce = -1;
3909 if (bmap->n_div <= 1)
3910 return -1;
3911 dim = isl_space_dim(bmap->dim, isl_dim_all);
3912 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3913 return -1;
3914 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3915 bmap->n_div - div - 1) != -1)
3916 return -1;
3917 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3918 dim + bmap->n_div))
3919 return -1;
3921 for (i = 0; i < bmap->n_div; ++i) {
3922 if (isl_int_is_zero(bmap->div[i][0]))
3923 continue;
3924 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3925 return -1;
3928 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3929 if (isl_int_is_neg(bmap->ineq[l][0])) {
3930 isl_int_sub(bmap->ineq[l][0],
3931 bmap->ineq[l][0], bmap->ineq[u][0]);
3932 bmap = isl_basic_map_copy(bmap);
3933 bmap = isl_basic_map_set_to_empty(bmap);
3934 isl_basic_map_free(bmap);
3935 return -1;
3937 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3938 for (i = 0; i < bmap->n_div; ++i) {
3939 if (i == div)
3940 continue;
3941 if (!pairs[i])
3942 continue;
3943 for (j = 0; j < bmap->n_div; ++j) {
3944 if (isl_int_is_zero(bmap->div[j][0]))
3945 continue;
3946 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3947 break;
3949 if (j < bmap->n_div)
3950 continue;
3951 for (j = 0; j < bmap->n_ineq; ++j) {
3952 int valid;
3953 if (j == l || j == u)
3954 continue;
3955 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3956 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3957 continue;
3958 break;
3960 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3961 break;
3962 isl_int_mul(bmap->ineq[j][1 + dim + div],
3963 bmap->ineq[j][1 + dim + div],
3964 bmap->ineq[l][0]);
3965 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3966 bmap->ineq[j][1 + dim + i]);
3967 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3968 bmap->ineq[j][1 + dim + div],
3969 bmap->ineq[l][0]);
3970 if (!valid)
3971 break;
3973 if (j < bmap->n_ineq)
3974 continue;
3975 coalesce = i;
3976 break;
3978 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3979 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3980 return coalesce;
3983 /* Internal data structure used during the construction and/or evaluation of
3984 * an inequality that ensures that a pair of bounds always allows
3985 * for an integer value.
3987 * "tab" is the tableau in which the inequality is evaluated. It may
3988 * be NULL until it is actually needed.
3989 * "v" contains the inequality coefficients.
3990 * "g", "fl" and "fu" are temporary scalars used during the construction and
3991 * evaluation.
3993 struct test_ineq_data {
3994 struct isl_tab *tab;
3995 isl_vec *v;
3996 isl_int g;
3997 isl_int fl;
3998 isl_int fu;
4001 /* Free all the memory allocated by the fields of "data".
4003 static void test_ineq_data_clear(struct test_ineq_data *data)
4005 isl_tab_free(data->tab);
4006 isl_vec_free(data->v);
4007 isl_int_clear(data->g);
4008 isl_int_clear(data->fl);
4009 isl_int_clear(data->fu);
4012 /* Is the inequality stored in data->v satisfied by "bmap"?
4013 * That is, does it only attain non-negative values?
4014 * data->tab is a tableau corresponding to "bmap".
4016 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4017 struct test_ineq_data *data)
4019 isl_ctx *ctx;
4020 enum isl_lp_result res;
4022 ctx = isl_basic_map_get_ctx(bmap);
4023 if (!data->tab)
4024 data->tab = isl_tab_from_basic_map(bmap, 0);
4025 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4026 if (res == isl_lp_error)
4027 return isl_bool_error;
4028 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4031 /* Given a lower and an upper bound on div i, do they always allow
4032 * for an integer value of the given div?
4033 * Determine this property by constructing an inequality
4034 * such that the property is guaranteed when the inequality is nonnegative.
4035 * The lower bound is inequality l, while the upper bound is inequality u.
4036 * The constructed inequality is stored in data->v.
4038 * Let the upper bound be
4040 * -n_u a + e_u >= 0
4042 * and the lower bound
4044 * n_l a + e_l >= 0
4046 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4047 * We have
4049 * - f_u e_l <= f_u f_l g a <= f_l e_u
4051 * Since all variables are integer valued, this is equivalent to
4053 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4055 * If this interval is at least f_u f_l g, then it contains at least
4056 * one integer value for a.
4057 * That is, the test constraint is
4059 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4061 * or
4063 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4065 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4066 * then the constraint can be scaled down by a factor g',
4067 * with the constant term replaced by
4068 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4069 * Note that the result of applying Fourier-Motzkin to this pair
4070 * of constraints is
4072 * f_l e_u + f_u e_l >= 0
4074 * If the constant term of the scaled down version of this constraint,
4075 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4076 * term of the scaled down test constraint, then the test constraint
4077 * is known to hold and no explicit evaluation is required.
4078 * This is essentially the Omega test.
4080 * If the test constraint consists of only a constant term, then
4081 * it is sufficient to look at the sign of this constant term.
4083 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4084 int l, int u, struct test_ineq_data *data)
4086 unsigned offset, n_div;
4087 offset = isl_basic_map_offset(bmap, isl_dim_div);
4088 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4090 isl_int_gcd(data->g,
4091 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4092 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4093 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4094 isl_int_neg(data->fu, data->fu);
4095 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4096 data->fu, bmap->ineq[l], offset + n_div);
4097 isl_int_mul(data->g, data->g, data->fl);
4098 isl_int_mul(data->g, data->g, data->fu);
4099 isl_int_sub(data->g, data->g, data->fl);
4100 isl_int_sub(data->g, data->g, data->fu);
4101 isl_int_add_ui(data->g, data->g, 1);
4102 isl_int_sub(data->fl, data->v->el[0], data->g);
4104 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4105 if (isl_int_is_zero(data->g))
4106 return isl_int_is_nonneg(data->fl);
4107 if (isl_int_is_one(data->g)) {
4108 isl_int_set(data->v->el[0], data->fl);
4109 return test_ineq_is_satisfied(bmap, data);
4111 isl_int_fdiv_q(data->fl, data->fl, data->g);
4112 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4113 if (isl_int_eq(data->fl, data->v->el[0]))
4114 return isl_bool_true;
4115 isl_int_set(data->v->el[0], data->fl);
4116 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4117 offset - 1 + n_div);
4119 return test_ineq_is_satisfied(bmap, data);
4122 /* Remove more kinds of divs that are not strictly needed.
4123 * In particular, if all pairs of lower and upper bounds on a div
4124 * are such that they allow at least one integer value of the div,
4125 * then we can eliminate the div using Fourier-Motzkin without
4126 * introducing any spurious solutions.
4128 * If at least one of the two constraints has a unit coefficient for the div,
4129 * then the presence of such a value is guaranteed so there is no need to check.
4130 * In particular, the value attained by the bound with unit coefficient
4131 * can serve as this intermediate value.
4133 static __isl_give isl_basic_map *drop_more_redundant_divs(
4134 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4136 isl_ctx *ctx;
4137 struct test_ineq_data data = { NULL, NULL };
4138 unsigned off, n_div;
4139 int remove = -1;
4141 isl_int_init(data.g);
4142 isl_int_init(data.fl);
4143 isl_int_init(data.fu);
4145 if (!bmap)
4146 goto error;
4148 ctx = isl_basic_map_get_ctx(bmap);
4149 off = isl_basic_map_offset(bmap, isl_dim_div);
4150 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4151 data.v = isl_vec_alloc(ctx, off + n_div);
4152 if (!data.v)
4153 goto error;
4155 while (n > 0) {
4156 int i, l, u;
4157 int best = -1;
4158 isl_bool has_int;
4160 for (i = 0; i < n_div; ++i) {
4161 if (!pairs[i])
4162 continue;
4163 if (best >= 0 && pairs[best] <= pairs[i])
4164 continue;
4165 best = i;
4168 i = best;
4169 for (l = 0; l < bmap->n_ineq; ++l) {
4170 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4171 continue;
4172 if (isl_int_is_one(bmap->ineq[l][off + i]))
4173 continue;
4174 for (u = 0; u < bmap->n_ineq; ++u) {
4175 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4176 continue;
4177 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4178 continue;
4179 has_int = int_between_bounds(bmap, i, l, u,
4180 &data);
4181 if (has_int < 0)
4182 goto error;
4183 if (data.tab && data.tab->empty)
4184 break;
4185 if (!has_int)
4186 break;
4188 if (u < bmap->n_ineq)
4189 break;
4191 if (data.tab && data.tab->empty) {
4192 bmap = isl_basic_map_set_to_empty(bmap);
4193 break;
4195 if (l == bmap->n_ineq) {
4196 remove = i;
4197 break;
4199 pairs[i] = 0;
4200 --n;
4203 test_ineq_data_clear(&data);
4205 free(pairs);
4207 if (remove < 0)
4208 return bmap;
4210 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4211 return isl_basic_map_drop_redundant_divs(bmap);
4212 error:
4213 free(pairs);
4214 isl_basic_map_free(bmap);
4215 test_ineq_data_clear(&data);
4216 return NULL;
4219 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4220 * and the upper bound u, div1 always occurs together with div2 in the form
4221 * (div1 + m div2), where m is the constant range on the variable div1
4222 * allowed by l and u, replace the pair div1 and div2 by a single
4223 * div that is equal to div1 + m div2.
4225 * The new div will appear in the location that contains div2.
4226 * We need to modify all constraints that contain
4227 * div2 = (div - div1) / m
4228 * The coefficient of div2 is known to be equal to 1 or -1.
4229 * (If a constraint does not contain div2, it will also not contain div1.)
4230 * If the constraint also contains div1, then we know they appear
4231 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4232 * i.e., the coefficient of div is f.
4234 * Otherwise, we first need to introduce div1 into the constraint.
4235 * Let l be
4237 * div1 + f >=0
4239 * and u
4241 * -div1 + f' >= 0
4243 * A lower bound on div2
4245 * div2 + t >= 0
4247 * can be replaced by
4249 * m div2 + div1 + m t + f >= 0
4251 * An upper bound
4253 * -div2 + t >= 0
4255 * can be replaced by
4257 * -(m div2 + div1) + m t + f' >= 0
4259 * These constraint are those that we would obtain from eliminating
4260 * div1 using Fourier-Motzkin.
4262 * After all constraints have been modified, we drop the lower and upper
4263 * bound and then drop div1.
4264 * Since the new div is only placed in the same location that used
4265 * to store div2, but otherwise has a different meaning, any possible
4266 * explicit representation of the original div2 is removed.
4268 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4269 unsigned div1, unsigned div2, unsigned l, unsigned u)
4271 isl_ctx *ctx;
4272 isl_int m;
4273 unsigned dim, total;
4274 int i;
4276 ctx = isl_basic_map_get_ctx(bmap);
4278 dim = isl_space_dim(bmap->dim, isl_dim_all);
4279 total = 1 + dim + bmap->n_div;
4281 isl_int_init(m);
4282 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4283 isl_int_add_ui(m, m, 1);
4285 for (i = 0; i < bmap->n_ineq; ++i) {
4286 if (i == l || i == u)
4287 continue;
4288 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4289 continue;
4290 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4291 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4292 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4293 ctx->one, bmap->ineq[l], total);
4294 else
4295 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4296 ctx->one, bmap->ineq[u], total);
4298 isl_int_set(bmap->ineq[i][1 + dim + div2],
4299 bmap->ineq[i][1 + dim + div1]);
4300 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4303 isl_int_clear(m);
4304 if (l > u) {
4305 isl_basic_map_drop_inequality(bmap, l);
4306 isl_basic_map_drop_inequality(bmap, u);
4307 } else {
4308 isl_basic_map_drop_inequality(bmap, u);
4309 isl_basic_map_drop_inequality(bmap, l);
4311 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4312 bmap = isl_basic_map_drop_div(bmap, div1);
4313 return bmap;
4316 /* First check if we can coalesce any pair of divs and
4317 * then continue with dropping more redundant divs.
4319 * We loop over all pairs of lower and upper bounds on a div
4320 * with coefficient 1 and -1, respectively, check if there
4321 * is any other div "c" with which we can coalesce the div
4322 * and if so, perform the coalescing.
4324 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4325 __isl_take isl_basic_map *bmap, int *pairs, int n)
4327 int i, l, u;
4328 unsigned dim;
4330 dim = isl_space_dim(bmap->dim, isl_dim_all);
4332 for (i = 0; i < bmap->n_div; ++i) {
4333 if (!pairs[i])
4334 continue;
4335 for (l = 0; l < bmap->n_ineq; ++l) {
4336 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4337 continue;
4338 for (u = 0; u < bmap->n_ineq; ++u) {
4339 int c;
4341 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4342 continue;
4343 c = div_find_coalesce(bmap, pairs, i, l, u);
4344 if (c < 0)
4345 continue;
4346 free(pairs);
4347 bmap = coalesce_divs(bmap, i, c, l, u);
4348 return isl_basic_map_drop_redundant_divs(bmap);
4353 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4354 free(pairs);
4355 return bmap;
4358 return drop_more_redundant_divs(bmap, pairs, n);
4361 /* Are the "n" coefficients starting at "first" of inequality constraints
4362 * "i" and "j" of "bmap" equal to each other?
4364 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4365 int first, int n)
4367 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4370 /* Are the "n" coefficients starting at "first" of inequality constraints
4371 * "i" and "j" of "bmap" opposite to each other?
4373 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4374 int first, int n)
4376 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4379 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4380 * apart from the constant term?
4382 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4384 unsigned total;
4386 total = isl_basic_map_dim(bmap, isl_dim_all);
4387 return is_opposite_part(bmap, i, j, 1, total);
4390 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4391 * apart from the constant term and the coefficient at position "pos"?
4393 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4394 int pos)
4396 unsigned total;
4398 total = isl_basic_map_dim(bmap, isl_dim_all);
4399 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4400 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4403 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4404 * apart from the constant term and the coefficient at position "pos"?
4406 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4407 int pos)
4409 unsigned total;
4411 total = isl_basic_map_dim(bmap, isl_dim_all);
4412 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4413 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4416 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4417 * been modified, simplying it if "simplify" is set.
4418 * Free the temporary data structure "pairs" that was associated
4419 * to the old version of "bmap".
4421 static __isl_give isl_basic_map *drop_redundant_divs_again(
4422 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4424 if (simplify)
4425 bmap = isl_basic_map_simplify(bmap);
4426 free(pairs);
4427 return isl_basic_map_drop_redundant_divs(bmap);
4430 /* Is "div" the single unknown existentially quantified variable
4431 * in inequality constraint "ineq" of "bmap"?
4432 * "div" is known to have a non-zero coefficient in "ineq".
4434 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4435 int div)
4437 int i;
4438 unsigned n_div, o_div;
4439 isl_bool known;
4441 known = isl_basic_map_div_is_known(bmap, div);
4442 if (known < 0 || known)
4443 return isl_bool_not(known);
4444 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4445 if (n_div == 1)
4446 return isl_bool_true;
4447 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4448 for (i = 0; i < n_div; ++i) {
4449 isl_bool known;
4451 if (i == div)
4452 continue;
4453 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4454 continue;
4455 known = isl_basic_map_div_is_known(bmap, i);
4456 if (known < 0 || !known)
4457 return known;
4460 return isl_bool_true;
4463 /* Does integer division "div" have coefficient 1 in inequality constraint
4464 * "ineq" of "map"?
4466 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4468 unsigned o_div;
4470 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4471 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4472 return isl_bool_true;
4474 return isl_bool_false;
4477 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4478 * then try and drop redundant divs again,
4479 * freeing the temporary data structure "pairs" that was associated
4480 * to the old version of "bmap".
4482 static __isl_give isl_basic_map *set_eq_and_try_again(
4483 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4485 bmap = isl_basic_map_cow(bmap);
4486 isl_basic_map_inequality_to_equality(bmap, ineq);
4487 return drop_redundant_divs_again(bmap, pairs, 1);
4490 /* Drop the integer division at position "div", along with the two
4491 * inequality constraints "ineq1" and "ineq2" in which it appears
4492 * from "bmap" and then try and drop redundant divs again,
4493 * freeing the temporary data structure "pairs" that was associated
4494 * to the old version of "bmap".
4496 static __isl_give isl_basic_map *drop_div_and_try_again(
4497 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4498 __isl_take int *pairs)
4500 if (ineq1 > ineq2) {
4501 isl_basic_map_drop_inequality(bmap, ineq1);
4502 isl_basic_map_drop_inequality(bmap, ineq2);
4503 } else {
4504 isl_basic_map_drop_inequality(bmap, ineq2);
4505 isl_basic_map_drop_inequality(bmap, ineq1);
4507 bmap = isl_basic_map_drop_div(bmap, div);
4508 return drop_redundant_divs_again(bmap, pairs, 0);
4511 /* Given two inequality constraints
4513 * f(x) + n d + c >= 0, (ineq)
4515 * with d the variable at position "pos", and
4517 * f(x) + c0 >= 0, (lower)
4519 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4520 * determined by the first constraint.
4521 * That is, store
4523 * ceil((c0 - c)/n)
4525 * in *l.
4527 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4528 int ineq, int lower, int pos, isl_int *l)
4530 isl_int_neg(*l, bmap->ineq[ineq][0]);
4531 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4532 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4535 /* Given two inequality constraints
4537 * f(x) + n d + c >= 0, (ineq)
4539 * with d the variable at position "pos", and
4541 * -f(x) - c0 >= 0, (upper)
4543 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4544 * determined by the first constraint.
4545 * That is, store
4547 * ceil((-c1 - c)/n)
4549 * in *u.
4551 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4552 int ineq, int upper, int pos, isl_int *u)
4554 isl_int_neg(*u, bmap->ineq[ineq][0]);
4555 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4556 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4559 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4560 * does the corresponding lower bound have a fixed value in "bmap"?
4562 * In particular, "ineq" is of the form
4564 * f(x) + n d + c >= 0
4566 * with n > 0, c the constant term and
4567 * d the existentially quantified variable "div".
4568 * That is, the lower bound is
4570 * ceil((-f(x) - c)/n)
4572 * Look for a pair of constraints
4574 * f(x) + c0 >= 0
4575 * -f(x) + c1 >= 0
4577 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4578 * That is, check that
4580 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4582 * If so, return the index of inequality f(x) + c0 >= 0.
4583 * Otherwise, return -1.
4585 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4587 int i;
4588 int lower = -1, upper = -1;
4589 unsigned o_div;
4590 isl_int l, u;
4591 int equal;
4593 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4594 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4595 if (i == ineq)
4596 continue;
4597 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4598 continue;
4599 if (lower < 0 &&
4600 is_parallel_except(bmap, ineq, i, o_div + div)) {
4601 lower = i;
4602 continue;
4604 if (upper < 0 &&
4605 is_opposite_except(bmap, ineq, i, o_div + div)) {
4606 upper = i;
4610 if (lower < 0 || upper < 0)
4611 return -1;
4613 isl_int_init(l);
4614 isl_int_init(u);
4616 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4617 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4619 equal = isl_int_eq(l, u);
4621 isl_int_clear(l);
4622 isl_int_clear(u);
4624 return equal ? lower : -1;
4627 /* Given a lower bound constraint "ineq" on the existentially quantified
4628 * variable "div", such that the corresponding lower bound has
4629 * a fixed value in "bmap", assign this fixed value to the variable and
4630 * then try and drop redundant divs again,
4631 * freeing the temporary data structure "pairs" that was associated
4632 * to the old version of "bmap".
4633 * "lower" determines the constant value for the lower bound.
4635 * In particular, "ineq" is of the form
4637 * f(x) + n d + c >= 0,
4639 * while "lower" is of the form
4641 * f(x) + c0 >= 0
4643 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4644 * is ceil((c0 - c)/n).
4646 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4647 int div, int ineq, int lower, int *pairs)
4649 isl_int c;
4650 unsigned o_div;
4652 isl_int_init(c);
4654 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4655 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4656 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4657 free(pairs);
4659 isl_int_clear(c);
4661 return isl_basic_map_drop_redundant_divs(bmap);
4664 /* Remove divs that are not strictly needed based on the inequality
4665 * constraints.
4666 * In particular, if a div only occurs positively (or negatively)
4667 * in constraints, then it can simply be dropped.
4668 * Also, if a div occurs in only two constraints and if moreover
4669 * those two constraints are opposite to each other, except for the constant
4670 * term and if the sum of the constant terms is such that for any value
4671 * of the other values, there is always at least one integer value of the
4672 * div, i.e., if one plus this sum is greater than or equal to
4673 * the (absolute value) of the coefficient of the div in the constraints,
4674 * then we can also simply drop the div.
4676 * If an existentially quantified variable does not have an explicit
4677 * representation, appears in only a single lower bound that does not
4678 * involve any other such existentially quantified variables and appears
4679 * in this lower bound with coefficient 1,
4680 * then fix the variable to the value of the lower bound. That is,
4681 * turn the inequality into an equality.
4682 * If for any value of the other variables, there is any value
4683 * for the existentially quantified variable satisfying the constraints,
4684 * then this lower bound also satisfies the constraints.
4685 * It is therefore safe to pick this lower bound.
4687 * The same reasoning holds even if the coefficient is not one.
4688 * However, fixing the variable to the value of the lower bound may
4689 * in general introduce an extra integer division, in which case
4690 * it may be better to pick another value.
4691 * If this integer division has a known constant value, then plugging
4692 * in this constant value removes the existentially quantified variable
4693 * completely. In particular, if the lower bound is of the form
4694 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4695 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4696 * then the existentially quantified variable can be assigned this
4697 * shared value.
4699 * We skip divs that appear in equalities or in the definition of other divs.
4700 * Divs that appear in the definition of other divs usually occur in at least
4701 * 4 constraints, but the constraints may have been simplified.
4703 * If any divs are left after these simple checks then we move on
4704 * to more complicated cases in drop_more_redundant_divs.
4706 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4707 __isl_take isl_basic_map *bmap)
4709 int i, j;
4710 unsigned off;
4711 int *pairs = NULL;
4712 int n = 0;
4714 if (!bmap)
4715 goto error;
4716 if (bmap->n_div == 0)
4717 return bmap;
4719 off = isl_space_dim(bmap->dim, isl_dim_all);
4720 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4721 if (!pairs)
4722 goto error;
4724 for (i = 0; i < bmap->n_div; ++i) {
4725 int pos, neg;
4726 int last_pos, last_neg;
4727 int redundant;
4728 int defined;
4729 isl_bool opp, set_div;
4731 defined = !isl_int_is_zero(bmap->div[i][0]);
4732 for (j = i; j < bmap->n_div; ++j)
4733 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4734 break;
4735 if (j < bmap->n_div)
4736 continue;
4737 for (j = 0; j < bmap->n_eq; ++j)
4738 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4739 break;
4740 if (j < bmap->n_eq)
4741 continue;
4742 ++n;
4743 pos = neg = 0;
4744 for (j = 0; j < bmap->n_ineq; ++j) {
4745 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4746 last_pos = j;
4747 ++pos;
4749 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4750 last_neg = j;
4751 ++neg;
4754 pairs[i] = pos * neg;
4755 if (pairs[i] == 0) {
4756 for (j = bmap->n_ineq - 1; j >= 0; --j)
4757 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4758 isl_basic_map_drop_inequality(bmap, j);
4759 bmap = isl_basic_map_drop_div(bmap, i);
4760 return drop_redundant_divs_again(bmap, pairs, 0);
4762 if (pairs[i] != 1)
4763 opp = isl_bool_false;
4764 else
4765 opp = is_opposite(bmap, last_pos, last_neg);
4766 if (opp < 0)
4767 goto error;
4768 if (!opp) {
4769 int lower;
4770 isl_bool single, one;
4772 if (pos != 1)
4773 continue;
4774 single = single_unknown(bmap, last_pos, i);
4775 if (single < 0)
4776 goto error;
4777 if (!single)
4778 continue;
4779 one = has_coef_one(bmap, i, last_pos);
4780 if (one < 0)
4781 goto error;
4782 if (one)
4783 return set_eq_and_try_again(bmap, last_pos,
4784 pairs);
4785 lower = lower_bound_is_cst(bmap, i, last_pos);
4786 if (lower >= 0)
4787 return fix_cst_lower(bmap, i, last_pos, lower,
4788 pairs);
4789 continue;
4792 isl_int_add(bmap->ineq[last_pos][0],
4793 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4794 isl_int_add_ui(bmap->ineq[last_pos][0],
4795 bmap->ineq[last_pos][0], 1);
4796 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4797 bmap->ineq[last_pos][1+off+i]);
4798 isl_int_sub_ui(bmap->ineq[last_pos][0],
4799 bmap->ineq[last_pos][0], 1);
4800 isl_int_sub(bmap->ineq[last_pos][0],
4801 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4802 if (redundant)
4803 return drop_div_and_try_again(bmap, i,
4804 last_pos, last_neg, pairs);
4805 if (defined)
4806 set_div = isl_bool_false;
4807 else
4808 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4809 if (set_div < 0)
4810 return isl_basic_map_free(bmap);
4811 if (set_div) {
4812 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4813 return drop_redundant_divs_again(bmap, pairs, 1);
4815 pairs[i] = 0;
4816 --n;
4819 if (n > 0)
4820 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4822 free(pairs);
4823 return bmap;
4824 error:
4825 free(pairs);
4826 isl_basic_map_free(bmap);
4827 return NULL;
4830 /* Consider the coefficients at "c" as a row vector and replace
4831 * them with their product with "T". "T" is assumed to be a square matrix.
4833 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4835 int n;
4836 isl_ctx *ctx;
4837 isl_vec *v;
4839 if (!T)
4840 return isl_stat_error;
4841 n = isl_mat_rows(T);
4842 if (isl_seq_first_non_zero(c, n) == -1)
4843 return isl_stat_ok;
4844 ctx = isl_mat_get_ctx(T);
4845 v = isl_vec_alloc(ctx, n);
4846 if (!v)
4847 return isl_stat_error;
4848 isl_seq_swp_or_cpy(v->el, c, n);
4849 v = isl_vec_mat_product(v, isl_mat_copy(T));
4850 if (!v)
4851 return isl_stat_error;
4852 isl_seq_swp_or_cpy(c, v->el, n);
4853 isl_vec_free(v);
4855 return isl_stat_ok;
4858 /* Plug in T for the variables in "bmap" starting at "pos".
4859 * T is a linear unimodular matrix, i.e., without constant term.
4861 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4862 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4864 int i;
4865 unsigned n, total;
4867 bmap = isl_basic_map_cow(bmap);
4868 if (!bmap || !T)
4869 goto error;
4871 n = isl_mat_cols(T);
4872 if (n != isl_mat_rows(T))
4873 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4874 "expecting square matrix", goto error);
4876 total = isl_basic_map_dim(bmap, isl_dim_all);
4877 if (pos + n > total || pos + n < pos)
4878 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4879 "invalid range", goto error);
4881 for (i = 0; i < bmap->n_eq; ++i)
4882 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4883 goto error;
4884 for (i = 0; i < bmap->n_ineq; ++i)
4885 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4886 goto error;
4887 for (i = 0; i < bmap->n_div; ++i) {
4888 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4889 continue;
4890 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4891 goto error;
4894 isl_mat_free(T);
4895 return bmap;
4896 error:
4897 isl_basic_map_free(bmap);
4898 isl_mat_free(T);
4899 return NULL;
4902 /* Remove divs that are not strictly needed.
4904 * First look for an equality constraint involving two or more
4905 * existentially quantified variables without an explicit
4906 * representation. Replace the combination that appears
4907 * in the equality constraint by a single existentially quantified
4908 * variable such that the equality can be used to derive
4909 * an explicit representation for the variable.
4910 * If there are no more such equality constraints, then continue
4911 * with isl_basic_map_drop_redundant_divs_ineq.
4913 * In particular, if the equality constraint is of the form
4915 * f(x) + \sum_i c_i a_i = 0
4917 * with a_i existentially quantified variable without explicit
4918 * representation, then apply a transformation on the existentially
4919 * quantified variables to turn the constraint into
4921 * f(x) + g a_1' = 0
4923 * with g the gcd of the c_i.
4924 * In order to easily identify which existentially quantified variables
4925 * have a complete explicit representation, i.e., without being defined
4926 * in terms of other existentially quantified variables without
4927 * an explicit representation, the existentially quantified variables
4928 * are first sorted.
4930 * The variable transformation is computed by extending the row
4931 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4933 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4934 * [a_2'] [ a_2 ]
4935 * ... = U ....
4936 * [a_n'] [ a_n ]
4938 * with [c_1/g ... c_n/g] representing the first row of U.
4939 * The inverse of U is then plugged into the original constraints.
4940 * The call to isl_basic_map_simplify makes sure the explicit
4941 * representation for a_1' is extracted from the equality constraint.
4943 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4944 __isl_take isl_basic_map *bmap)
4946 int first;
4947 int i;
4948 unsigned o_div, n_div;
4949 int l;
4950 isl_ctx *ctx;
4951 isl_mat *T;
4953 if (!bmap)
4954 return NULL;
4955 if (isl_basic_map_divs_known(bmap))
4956 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4957 if (bmap->n_eq == 0)
4958 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4959 bmap = isl_basic_map_sort_divs(bmap);
4960 if (!bmap)
4961 return NULL;
4963 first = isl_basic_map_first_unknown_div(bmap);
4964 if (first < 0)
4965 return isl_basic_map_free(bmap);
4967 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4968 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4970 for (i = 0; i < bmap->n_eq; ++i) {
4971 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4972 n_div - (first));
4973 if (l < 0)
4974 continue;
4975 l += first;
4976 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4977 n_div - (l + 1)) == -1)
4978 continue;
4979 break;
4981 if (i >= bmap->n_eq)
4982 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4984 ctx = isl_basic_map_get_ctx(bmap);
4985 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
4986 if (!T)
4987 return isl_basic_map_free(bmap);
4988 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
4989 T = isl_mat_normalize_row(T, 0);
4990 T = isl_mat_unimodular_complete(T, 1);
4991 T = isl_mat_right_inverse(T);
4993 for (i = l; i < n_div; ++i)
4994 bmap = isl_basic_map_mark_div_unknown(bmap, i);
4995 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
4996 bmap = isl_basic_map_simplify(bmap);
4998 return isl_basic_map_drop_redundant_divs(bmap);
5001 /* Does "bmap" satisfy any equality that involves more than 2 variables
5002 * and/or has coefficients different from -1 and 1?
5004 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5006 int i;
5007 unsigned total;
5009 total = isl_basic_map_dim(bmap, isl_dim_all);
5011 for (i = 0; i < bmap->n_eq; ++i) {
5012 int j, k;
5014 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5015 if (j < 0)
5016 continue;
5017 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5018 !isl_int_is_negone(bmap->eq[i][1 + j]))
5019 return 1;
5021 j += 1;
5022 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5023 if (k < 0)
5024 continue;
5025 j += k;
5026 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5027 !isl_int_is_negone(bmap->eq[i][1 + j]))
5028 return 1;
5030 j += 1;
5031 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5032 if (k >= 0)
5033 return 1;
5036 return 0;
5039 /* Remove any common factor g from the constraint coefficients in "v".
5040 * The constant term is stored in the first position and is replaced
5041 * by floor(c/g). If any common factor is removed and if this results
5042 * in a tightening of the constraint, then set *tightened.
5044 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5045 int *tightened)
5047 isl_ctx *ctx;
5049 if (!v)
5050 return NULL;
5051 ctx = isl_vec_get_ctx(v);
5052 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5053 if (isl_int_is_zero(ctx->normalize_gcd))
5054 return v;
5055 if (isl_int_is_one(ctx->normalize_gcd))
5056 return v;
5057 v = isl_vec_cow(v);
5058 if (!v)
5059 return NULL;
5060 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5061 *tightened = 1;
5062 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5063 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5064 v->size - 1);
5065 return v;
5068 /* If "bmap" is an integer set that satisfies any equality involving
5069 * more than 2 variables and/or has coefficients different from -1 and 1,
5070 * then use variable compression to reduce the coefficients by removing
5071 * any (hidden) common factor.
5072 * In particular, apply the variable compression to each constraint,
5073 * factor out any common factor in the non-constant coefficients and
5074 * then apply the inverse of the compression.
5075 * At the end, we mark the basic map as having reduced constants.
5076 * If this flag is still set on the next invocation of this function,
5077 * then we skip the computation.
5079 * Removing a common factor may result in a tightening of some of
5080 * the constraints. If this happens, then we may end up with two
5081 * opposite inequalities that can be replaced by an equality.
5082 * We therefore call isl_basic_map_detect_inequality_pairs,
5083 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5084 * and isl_basic_map_gauss if such a pair was found.
5086 * Note that this function may leave the result in an inconsistent state.
5087 * In particular, the constraints may not be gaussed.
5088 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5089 * for some of the test cases to pass successfully.
5090 * Any potential modification of the representation is therefore only
5091 * performed on a single copy of the basic map.
5093 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5094 __isl_take isl_basic_map *bmap)
5096 unsigned total;
5097 isl_ctx *ctx;
5098 isl_vec *v;
5099 isl_mat *eq, *T, *T2;
5100 int i;
5101 int tightened;
5103 if (!bmap)
5104 return NULL;
5105 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5106 return bmap;
5107 if (isl_basic_map_is_rational(bmap))
5108 return bmap;
5109 if (bmap->n_eq == 0)
5110 return bmap;
5111 if (!has_multiple_var_equality(bmap))
5112 return bmap;
5114 total = isl_basic_map_dim(bmap, isl_dim_all);
5115 ctx = isl_basic_map_get_ctx(bmap);
5116 v = isl_vec_alloc(ctx, 1 + total);
5117 if (!v)
5118 return isl_basic_map_free(bmap);
5120 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5121 T = isl_mat_variable_compression(eq, &T2);
5122 if (!T || !T2)
5123 goto error;
5124 if (T->n_col == 0) {
5125 isl_mat_free(T);
5126 isl_mat_free(T2);
5127 isl_vec_free(v);
5128 return isl_basic_map_set_to_empty(bmap);
5131 bmap = isl_basic_map_cow(bmap);
5132 if (!bmap)
5133 goto error;
5135 tightened = 0;
5136 for (i = 0; i < bmap->n_ineq; ++i) {
5137 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5138 v = isl_vec_mat_product(v, isl_mat_copy(T));
5139 v = normalize_constraint(v, &tightened);
5140 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5141 if (!v)
5142 goto error;
5143 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5146 isl_mat_free(T);
5147 isl_mat_free(T2);
5148 isl_vec_free(v);
5150 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5152 if (tightened) {
5153 int progress = 0;
5155 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5156 if (progress) {
5157 bmap = eliminate_divs_eq(bmap, &progress);
5158 bmap = isl_basic_map_gauss(bmap, NULL);
5162 return bmap;
5163 error:
5164 isl_mat_free(T);
5165 isl_mat_free(T2);
5166 isl_vec_free(v);
5167 return isl_basic_map_free(bmap);
5170 /* Shift the integer division at position "div" of "bmap"
5171 * by "shift" times the variable at position "pos".
5172 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5173 * corresponds to the constant term.
5175 * That is, if the integer division has the form
5177 * floor(f(x)/d)
5179 * then replace it by
5181 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5183 __isl_give isl_basic_map *isl_basic_map_shift_div(
5184 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5186 int i;
5187 unsigned total;
5189 if (isl_int_is_zero(shift))
5190 return bmap;
5191 if (!bmap)
5192 return NULL;
5194 total = isl_basic_map_dim(bmap, isl_dim_all);
5195 total -= isl_basic_map_dim(bmap, isl_dim_div);
5197 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5199 for (i = 0; i < bmap->n_eq; ++i) {
5200 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5201 continue;
5202 isl_int_submul(bmap->eq[i][pos],
5203 shift, bmap->eq[i][1 + total + div]);
5205 for (i = 0; i < bmap->n_ineq; ++i) {
5206 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5207 continue;
5208 isl_int_submul(bmap->ineq[i][pos],
5209 shift, bmap->ineq[i][1 + total + div]);
5211 for (i = 0; i < bmap->n_div; ++i) {
5212 if (isl_int_is_zero(bmap->div[i][0]))
5213 continue;
5214 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5215 continue;
5216 isl_int_submul(bmap->div[i][1 + pos],
5217 shift, bmap->div[i][1 + 1 + total + div]);
5220 return bmap;