isl_map_simplify.c: set_compute_elimination_index: add annotation
[isl.git] / isl_map_simplify.c
blobed37a4e5b969b318fbf4e54b02ea8eb201d37cf2
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 struct isl_basic_map *isl_basic_map_normalize_constraints(
48 struct 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 struct isl_basic_set *isl_basic_set_normalize_constraints(
102 struct 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_NORMALIZED);
319 for (k = 0; k < bmap->n_div; ++k) {
320 if (isl_int_is_zero(bmap->div[k][0]))
321 continue;
322 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
323 continue;
324 if (progress)
325 *progress = 1;
326 /* We need to be careful about circular definitions,
327 * so for now we just remove the definition of div k
328 * if the equality contains any divs.
329 * If keep_divs is set, then the divs have been ordered
330 * and we can keep the definition as long as the result
331 * is still ordered.
333 if (last_div == -1 || (keep_divs && last_div < k)) {
334 isl_seq_elim(bmap->div[k]+1, eq,
335 1+pos, 1+total, &bmap->div[k][0]);
336 normalize_div_expression(bmap, k);
337 } else
338 isl_seq_clr(bmap->div[k], 1 + total);
339 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
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(struct 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 struct isl_basic_map *eliminate_divs_eq(
385 struct 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 struct isl_basic_map *eliminate_divs_ineq(
427 struct 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;
539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
541 return bmap;
544 struct isl_basic_map *isl_basic_map_gauss(
545 struct isl_basic_map *bmap, int *progress)
547 int k;
548 int done;
549 int last_var;
550 unsigned total_var;
551 unsigned total;
553 bmap = isl_basic_map_order_divs(bmap);
555 if (!bmap)
556 return NULL;
558 total = isl_basic_map_total_dim(bmap);
559 total_var = total - bmap->n_div;
561 last_var = total - 1;
562 for (done = 0; done < bmap->n_eq; ++done) {
563 for (; last_var >= 0; --last_var) {
564 for (k = done; k < bmap->n_eq; ++k)
565 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
566 break;
567 if (k < bmap->n_eq)
568 break;
570 if (last_var < 0)
571 break;
572 if (k != done)
573 swap_equality(bmap, k, done);
574 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
575 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
577 eliminate_var_using_equality(bmap, last_var, bmap->eq[done], 1,
578 progress);
580 if (last_var >= total_var)
581 bmap = set_div_from_eq(bmap, last_var - total_var,
582 done, progress);
583 if (!bmap)
584 return NULL;
586 if (done == bmap->n_eq)
587 return bmap;
588 for (k = done; k < bmap->n_eq; ++k) {
589 if (isl_int_is_zero(bmap->eq[k][0]))
590 continue;
591 return isl_basic_map_set_to_empty(bmap);
593 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
594 return bmap;
597 struct isl_basic_set *isl_basic_set_gauss(
598 struct isl_basic_set *bset, int *progress)
600 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
601 progress));
605 static unsigned int round_up(unsigned int v)
607 int old_v = v;
609 while (v) {
610 old_v = v;
611 v ^= v & -v;
613 return old_v << 1;
616 /* Hash table of inequalities in a basic map.
617 * "index" is an array of addresses of inequalities in the basic map, some
618 * of which are NULL. The inequalities are hashed on the coefficients
619 * except the constant term.
620 * "size" is the number of elements in the array and is always a power of two
621 * "bits" is the number of bits need to represent an index into the array.
622 * "total" is the total dimension of the basic map.
624 struct isl_constraint_index {
625 unsigned int size;
626 int bits;
627 isl_int ***index;
628 unsigned total;
631 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
633 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
634 __isl_keep isl_basic_map *bmap)
636 isl_ctx *ctx;
638 ci->index = NULL;
639 if (!bmap)
640 return isl_stat_error;
641 ci->total = isl_basic_set_total_dim(bmap);
642 if (bmap->n_ineq == 0)
643 return isl_stat_ok;
644 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
645 ci->bits = ffs(ci->size) - 1;
646 ctx = isl_basic_map_get_ctx(bmap);
647 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
648 if (!ci->index)
649 return isl_stat_error;
651 return isl_stat_ok;
654 /* Free the memory allocated by create_constraint_index.
656 static void constraint_index_free(struct isl_constraint_index *ci)
658 free(ci->index);
661 /* Return the position in ci->index that contains the address of
662 * an inequality that is equal to *ineq up to the constant term,
663 * provided this address is not identical to "ineq".
664 * If there is no such inequality, then return the position where
665 * such an inequality should be inserted.
667 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
669 int h;
670 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
671 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
672 if (ineq != ci->index[h] &&
673 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
674 break;
675 return h;
678 /* Return the position in ci->index that contains the address of
679 * an inequality that is equal to the k'th inequality of "bmap"
680 * up to the constant term, provided it does not point to the very
681 * same inequality.
682 * If there is no such inequality, then return the position where
683 * such an inequality should be inserted.
685 static int hash_index(struct isl_constraint_index *ci,
686 __isl_keep isl_basic_map *bmap, int k)
688 return hash_index_ineq(ci, &bmap->ineq[k]);
691 static int set_hash_index(struct isl_constraint_index *ci,
692 __isl_keep isl_basic_set *bset, int k)
694 return hash_index(ci, bset, k);
697 /* Fill in the "ci" data structure with the inequalities of "bset".
699 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
700 __isl_keep isl_basic_set *bset)
702 int k, h;
704 if (create_constraint_index(ci, bset) < 0)
705 return isl_stat_error;
707 for (k = 0; k < bset->n_ineq; ++k) {
708 h = set_hash_index(ci, bset, k);
709 ci->index[h] = &bset->ineq[k];
712 return isl_stat_ok;
715 /* Is the inequality ineq (obviously) redundant with respect
716 * to the constraints in "ci"?
718 * Look for an inequality in "ci" with the same coefficients and then
719 * check if the contant term of "ineq" is greater than or equal
720 * to the constant term of that inequality. If so, "ineq" is clearly
721 * redundant.
723 * Note that hash_index_ineq ignores a stored constraint if it has
724 * the same address as the passed inequality. It is ok to pass
725 * the address of a local variable here since it will never be
726 * the same as the address of a constraint in "ci".
728 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
729 isl_int *ineq)
731 int h;
733 h = hash_index_ineq(ci, &ineq);
734 if (!ci->index[h])
735 return isl_bool_false;
736 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
739 /* If we can eliminate more than one div, then we need to make
740 * sure we do it from last div to first div, in order not to
741 * change the position of the other divs that still need to
742 * be removed.
744 static struct isl_basic_map *remove_duplicate_divs(
745 struct isl_basic_map *bmap, int *progress)
747 unsigned int size;
748 int *index;
749 int *elim_for;
750 int k, l, h;
751 int bits;
752 struct isl_blk eq;
753 unsigned total_var;
754 unsigned total;
755 struct isl_ctx *ctx;
757 bmap = isl_basic_map_order_divs(bmap);
758 if (!bmap || bmap->n_div <= 1)
759 return bmap;
761 total_var = isl_space_dim(bmap->dim, isl_dim_all);
762 total = total_var + bmap->n_div;
764 ctx = bmap->ctx;
765 for (k = bmap->n_div - 1; k >= 0; --k)
766 if (!isl_int_is_zero(bmap->div[k][0]))
767 break;
768 if (k <= 0)
769 return bmap;
771 size = round_up(4 * bmap->n_div / 3 - 1);
772 if (size == 0)
773 return bmap;
774 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
775 bits = ffs(size) - 1;
776 index = isl_calloc_array(ctx, int, size);
777 if (!elim_for || !index)
778 goto out;
779 eq = isl_blk_alloc(ctx, 1+total);
780 if (isl_blk_is_error(eq))
781 goto out;
783 isl_seq_clr(eq.data, 1+total);
784 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
785 for (--k; k >= 0; --k) {
786 uint32_t hash;
788 if (isl_int_is_zero(bmap->div[k][0]))
789 continue;
791 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
792 for (h = hash; index[h]; h = (h+1) % size)
793 if (isl_seq_eq(bmap->div[k],
794 bmap->div[index[h]-1], 2+total))
795 break;
796 if (index[h]) {
797 *progress = 1;
798 l = index[h] - 1;
799 elim_for[l] = k + 1;
801 index[h] = k+1;
803 for (l = bmap->n_div - 1; l >= 0; --l) {
804 if (!elim_for[l])
805 continue;
806 k = elim_for[l] - 1;
807 isl_int_set_si(eq.data[1+total_var+k], -1);
808 isl_int_set_si(eq.data[1+total_var+l], 1);
809 bmap = eliminate_div(bmap, eq.data, l, 1);
810 if (!bmap)
811 break;
812 isl_int_set_si(eq.data[1+total_var+k], 0);
813 isl_int_set_si(eq.data[1+total_var+l], 0);
816 isl_blk_free(ctx, eq);
817 out:
818 free(index);
819 free(elim_for);
820 return bmap;
823 static int n_pure_div_eq(struct isl_basic_map *bmap)
825 int i, j;
826 unsigned total;
828 total = isl_space_dim(bmap->dim, isl_dim_all);
829 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
830 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
831 --j;
832 if (j < 0)
833 break;
834 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
835 return 0;
837 return i;
840 /* Normalize divs that appear in equalities.
842 * In particular, we assume that bmap contains some equalities
843 * of the form
845 * a x = m * e_i
847 * and we want to replace the set of e_i by a minimal set and
848 * such that the new e_i have a canonical representation in terms
849 * of the vector x.
850 * If any of the equalities involves more than one divs, then
851 * we currently simply bail out.
853 * Let us first additionally assume that all equalities involve
854 * a div. The equalities then express modulo constraints on the
855 * remaining variables and we can use "parameter compression"
856 * to find a minimal set of constraints. The result is a transformation
858 * x = T(x') = x_0 + G x'
860 * with G a lower-triangular matrix with all elements below the diagonal
861 * non-negative and smaller than the diagonal element on the same row.
862 * We first normalize x_0 by making the same property hold in the affine
863 * T matrix.
864 * The rows i of G with a 1 on the diagonal do not impose any modulo
865 * constraint and simply express x_i = x'_i.
866 * For each of the remaining rows i, we introduce a div and a corresponding
867 * equality. In particular
869 * g_ii e_j = x_i - g_i(x')
871 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
872 * corresponding div (if g_kk != 1).
874 * If there are any equalities not involving any div, then we
875 * first apply a variable compression on the variables x:
877 * x = C x'' x'' = C_2 x
879 * and perform the above parameter compression on A C instead of on A.
880 * The resulting compression is then of the form
882 * x'' = T(x') = x_0 + G x'
884 * and in constructing the new divs and the corresponding equalities,
885 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
886 * by the corresponding row from C_2.
888 static struct isl_basic_map *normalize_divs(
889 struct isl_basic_map *bmap, int *progress)
891 int i, j, k;
892 int total;
893 int div_eq;
894 struct isl_mat *B;
895 struct isl_vec *d;
896 struct isl_mat *T = NULL;
897 struct isl_mat *C = NULL;
898 struct isl_mat *C2 = NULL;
899 isl_int v;
900 int *pos = NULL;
901 int dropped, needed;
903 if (!bmap)
904 return NULL;
906 if (bmap->n_div == 0)
907 return bmap;
909 if (bmap->n_eq == 0)
910 return bmap;
912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
913 return bmap;
915 total = isl_space_dim(bmap->dim, isl_dim_all);
916 div_eq = n_pure_div_eq(bmap);
917 if (div_eq == 0)
918 return bmap;
920 if (div_eq < bmap->n_eq) {
921 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
922 bmap->n_eq - div_eq, 0, 1 + total);
923 C = isl_mat_variable_compression(B, &C2);
924 if (!C || !C2)
925 goto error;
926 if (C->n_col == 0) {
927 bmap = isl_basic_map_set_to_empty(bmap);
928 isl_mat_free(C);
929 isl_mat_free(C2);
930 goto done;
934 d = isl_vec_alloc(bmap->ctx, div_eq);
935 if (!d)
936 goto error;
937 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
938 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
939 --j;
940 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
942 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
944 if (C) {
945 B = isl_mat_product(B, C);
946 C = NULL;
949 T = isl_mat_parameter_compression(B, d);
950 if (!T)
951 goto error;
952 if (T->n_col == 0) {
953 bmap = isl_basic_map_set_to_empty(bmap);
954 isl_mat_free(C2);
955 isl_mat_free(T);
956 goto done;
958 isl_int_init(v);
959 for (i = 0; i < T->n_row - 1; ++i) {
960 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
961 if (isl_int_is_zero(v))
962 continue;
963 isl_mat_col_submul(T, 0, v, 1 + i);
965 isl_int_clear(v);
966 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
967 if (!pos)
968 goto error;
969 /* We have to be careful because dropping equalities may reorder them */
970 dropped = 0;
971 for (j = bmap->n_div - 1; j >= 0; --j) {
972 for (i = 0; i < bmap->n_eq; ++i)
973 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
974 break;
975 if (i < bmap->n_eq) {
976 bmap = isl_basic_map_drop_div(bmap, j);
977 isl_basic_map_drop_equality(bmap, i);
978 ++dropped;
981 pos[0] = 0;
982 needed = 0;
983 for (i = 1; i < T->n_row; ++i) {
984 if (isl_int_is_one(T->row[i][i]))
985 pos[i] = i;
986 else
987 needed++;
989 if (needed > dropped) {
990 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
991 needed, needed, 0);
992 if (!bmap)
993 goto error;
995 for (i = 1; i < T->n_row; ++i) {
996 if (isl_int_is_one(T->row[i][i]))
997 continue;
998 k = isl_basic_map_alloc_div(bmap);
999 pos[i] = 1 + total + k;
1000 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1001 isl_int_set(bmap->div[k][0], T->row[i][i]);
1002 if (C2)
1003 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1004 else
1005 isl_int_set_si(bmap->div[k][1 + i], 1);
1006 for (j = 0; j < i; ++j) {
1007 if (isl_int_is_zero(T->row[i][j]))
1008 continue;
1009 if (pos[j] < T->n_row && C2)
1010 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1011 C2->row[pos[j]], 1 + total);
1012 else
1013 isl_int_neg(bmap->div[k][1 + pos[j]],
1014 T->row[i][j]);
1016 j = isl_basic_map_alloc_equality(bmap);
1017 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1018 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1020 free(pos);
1021 isl_mat_free(C2);
1022 isl_mat_free(T);
1024 if (progress)
1025 *progress = 1;
1026 done:
1027 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1029 return bmap;
1030 error:
1031 free(pos);
1032 isl_mat_free(C);
1033 isl_mat_free(C2);
1034 isl_mat_free(T);
1035 return bmap;
1038 static __isl_give isl_basic_map *set_div_from_lower_bound(
1039 __isl_take isl_basic_map *bmap, int div, int ineq)
1041 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1043 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1044 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1045 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1046 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1047 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1049 return bmap;
1052 /* Check whether it is ok to define a div based on an inequality.
1053 * To avoid the introduction of circular definitions of divs, we
1054 * do not allow such a definition if the resulting expression would refer to
1055 * any other undefined divs or if any known div is defined in
1056 * terms of the unknown div.
1058 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1059 int div, int ineq)
1061 int j;
1062 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1064 /* Not defined in terms of unknown divs */
1065 for (j = 0; j < bmap->n_div; ++j) {
1066 if (div == j)
1067 continue;
1068 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1069 continue;
1070 if (isl_int_is_zero(bmap->div[j][0]))
1071 return isl_bool_false;
1074 /* No other div defined in terms of this one => avoid loops */
1075 for (j = 0; j < bmap->n_div; ++j) {
1076 if (div == j)
1077 continue;
1078 if (isl_int_is_zero(bmap->div[j][0]))
1079 continue;
1080 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1081 return isl_bool_false;
1084 return isl_bool_true;
1087 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1088 * be a better expression than the current one?
1090 * If we do not have any expression yet, then any expression would be better.
1091 * Otherwise we check if the last variable involved in the inequality
1092 * (disregarding the div that it would define) is in an earlier position
1093 * than the last variable involved in the current div expression.
1095 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1096 int div, int ineq)
1098 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1099 int last_div;
1100 int last_ineq;
1102 if (isl_int_is_zero(bmap->div[div][0]))
1103 return isl_bool_true;
1105 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1106 bmap->n_div - (div + 1)) >= 0)
1107 return isl_bool_false;
1109 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1110 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1111 total + bmap->n_div);
1113 return last_ineq < last_div;
1116 /* Given two constraints "k" and "l" that are opposite to each other,
1117 * except for the constant term, check if we can use them
1118 * to obtain an expression for one of the hitherto unknown divs or
1119 * a "better" expression for a div for which we already have an expression.
1120 * "sum" is the sum of the constant terms of the constraints.
1121 * If this sum is strictly smaller than the coefficient of one
1122 * of the divs, then this pair can be used define the div.
1123 * To avoid the introduction of circular definitions of divs, we
1124 * do not use the pair if the resulting expression would refer to
1125 * any other undefined divs or if any known div is defined in
1126 * terms of the unknown div.
1128 static __isl_give isl_basic_map *check_for_div_constraints(
1129 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1130 int *progress)
1132 int i;
1133 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1135 for (i = 0; i < bmap->n_div; ++i) {
1136 isl_bool set_div;
1138 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1139 continue;
1140 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1141 continue;
1142 set_div = better_div_constraint(bmap, i, k);
1143 if (set_div >= 0 && set_div)
1144 set_div = ok_to_set_div_from_bound(bmap, i, k);
1145 if (set_div < 0)
1146 return isl_basic_map_free(bmap);
1147 if (!set_div)
1148 break;
1149 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1150 bmap = set_div_from_lower_bound(bmap, i, k);
1151 else
1152 bmap = set_div_from_lower_bound(bmap, i, l);
1153 if (progress)
1154 *progress = 1;
1155 break;
1157 return bmap;
1160 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1161 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1163 struct isl_constraint_index ci;
1164 int k, l, h;
1165 unsigned total = isl_basic_map_total_dim(bmap);
1166 isl_int sum;
1168 if (!bmap || bmap->n_ineq <= 1)
1169 return bmap;
1171 if (create_constraint_index(&ci, bmap) < 0)
1172 return bmap;
1174 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1175 ci.index[h] = &bmap->ineq[0];
1176 for (k = 1; k < bmap->n_ineq; ++k) {
1177 h = hash_index(&ci, bmap, k);
1178 if (!ci.index[h]) {
1179 ci.index[h] = &bmap->ineq[k];
1180 continue;
1182 if (progress)
1183 *progress = 1;
1184 l = ci.index[h] - &bmap->ineq[0];
1185 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1186 swap_inequality(bmap, k, l);
1187 isl_basic_map_drop_inequality(bmap, k);
1188 --k;
1190 isl_int_init(sum);
1191 for (k = 0; k < bmap->n_ineq-1; ++k) {
1192 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1193 h = hash_index(&ci, bmap, k);
1194 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1195 if (!ci.index[h])
1196 continue;
1197 l = ci.index[h] - &bmap->ineq[0];
1198 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1199 if (isl_int_is_pos(sum)) {
1200 if (detect_divs)
1201 bmap = check_for_div_constraints(bmap, k, l,
1202 sum, progress);
1203 continue;
1205 if (isl_int_is_zero(sum)) {
1206 /* We need to break out of the loop after these
1207 * changes since the contents of the hash
1208 * will no longer be valid.
1209 * Plus, we probably we want to regauss first.
1211 if (progress)
1212 *progress = 1;
1213 isl_basic_map_drop_inequality(bmap, l);
1214 isl_basic_map_inequality_to_equality(bmap, k);
1215 } else
1216 bmap = isl_basic_map_set_to_empty(bmap);
1217 break;
1219 isl_int_clear(sum);
1221 constraint_index_free(&ci);
1222 return bmap;
1225 /* Detect all pairs of inequalities that form an equality.
1227 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1228 * Call it repeatedly while it is making progress.
1230 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1231 __isl_take isl_basic_map *bmap, int *progress)
1233 int duplicate;
1235 do {
1236 duplicate = 0;
1237 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1238 &duplicate, 0);
1239 if (progress && duplicate)
1240 *progress = 1;
1241 } while (duplicate);
1243 return bmap;
1246 /* Eliminate knowns divs from constraints where they appear with
1247 * a (positive or negative) unit coefficient.
1249 * That is, replace
1251 * floor(e/m) + f >= 0
1253 * by
1255 * e + m f >= 0
1257 * and
1259 * -floor(e/m) + f >= 0
1261 * by
1263 * -e + m f + m - 1 >= 0
1265 * The first conversion is valid because floor(e/m) >= -f is equivalent
1266 * to e/m >= -f because -f is an integral expression.
1267 * The second conversion follows from the fact that
1269 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1272 * Note that one of the div constraints may have been eliminated
1273 * due to being redundant with respect to the constraint that is
1274 * being modified by this function. The modified constraint may
1275 * no longer imply this div constraint, so we add it back to make
1276 * sure we do not lose any information.
1278 * We skip integral divs, i.e., those with denominator 1, as we would
1279 * risk eliminating the div from the div constraints. We do not need
1280 * to handle those divs here anyway since the div constraints will turn
1281 * out to form an equality and this equality can then be used to eliminate
1282 * the div from all constraints.
1284 static __isl_give isl_basic_map *eliminate_unit_divs(
1285 __isl_take isl_basic_map *bmap, int *progress)
1287 int i, j;
1288 isl_ctx *ctx;
1289 unsigned total;
1291 if (!bmap)
1292 return NULL;
1294 ctx = isl_basic_map_get_ctx(bmap);
1295 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1297 for (i = 0; i < bmap->n_div; ++i) {
1298 if (isl_int_is_zero(bmap->div[i][0]))
1299 continue;
1300 if (isl_int_is_one(bmap->div[i][0]))
1301 continue;
1302 for (j = 0; j < bmap->n_ineq; ++j) {
1303 int s;
1305 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1306 !isl_int_is_negone(bmap->ineq[j][total + i]))
1307 continue;
1309 *progress = 1;
1311 s = isl_int_sgn(bmap->ineq[j][total + i]);
1312 isl_int_set_si(bmap->ineq[j][total + i], 0);
1313 if (s < 0)
1314 isl_seq_combine(bmap->ineq[j],
1315 ctx->negone, bmap->div[i] + 1,
1316 bmap->div[i][0], bmap->ineq[j],
1317 total + bmap->n_div);
1318 else
1319 isl_seq_combine(bmap->ineq[j],
1320 ctx->one, bmap->div[i] + 1,
1321 bmap->div[i][0], bmap->ineq[j],
1322 total + bmap->n_div);
1323 if (s < 0) {
1324 isl_int_add(bmap->ineq[j][0],
1325 bmap->ineq[j][0], bmap->div[i][0]);
1326 isl_int_sub_ui(bmap->ineq[j][0],
1327 bmap->ineq[j][0], 1);
1330 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1331 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1332 return isl_basic_map_free(bmap);
1336 return bmap;
1339 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1341 int progress = 1;
1342 if (!bmap)
1343 return NULL;
1344 while (progress) {
1345 isl_bool empty;
1347 progress = 0;
1348 empty = isl_basic_map_plain_is_empty(bmap);
1349 if (empty < 0)
1350 return isl_basic_map_free(bmap);
1351 if (empty)
1352 break;
1353 bmap = isl_basic_map_normalize_constraints(bmap);
1354 bmap = reduce_div_coefficients(bmap);
1355 bmap = normalize_div_expressions(bmap);
1356 bmap = remove_duplicate_divs(bmap, &progress);
1357 bmap = eliminate_unit_divs(bmap, &progress);
1358 bmap = eliminate_divs_eq(bmap, &progress);
1359 bmap = eliminate_divs_ineq(bmap, &progress);
1360 bmap = isl_basic_map_gauss(bmap, &progress);
1361 /* requires equalities in normal form */
1362 bmap = normalize_divs(bmap, &progress);
1363 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1364 &progress, 1);
1365 if (bmap && progress)
1366 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1368 return bmap;
1371 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1373 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1377 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1378 isl_int *constraint, unsigned div)
1380 unsigned pos;
1382 if (!bmap)
1383 return isl_bool_error;
1385 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1387 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1388 int neg;
1389 isl_int_sub(bmap->div[div][1],
1390 bmap->div[div][1], bmap->div[div][0]);
1391 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1392 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1393 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1394 isl_int_add(bmap->div[div][1],
1395 bmap->div[div][1], bmap->div[div][0]);
1396 if (!neg)
1397 return isl_bool_false;
1398 if (isl_seq_first_non_zero(constraint+pos+1,
1399 bmap->n_div-div-1) != -1)
1400 return isl_bool_false;
1401 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1402 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1403 return isl_bool_false;
1404 if (isl_seq_first_non_zero(constraint+pos+1,
1405 bmap->n_div-div-1) != -1)
1406 return isl_bool_false;
1407 } else
1408 return isl_bool_false;
1410 return isl_bool_true;
1413 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1414 isl_int *constraint, unsigned div)
1416 return isl_basic_map_is_div_constraint(bset, constraint, div);
1420 /* If the only constraints a div d=floor(f/m)
1421 * appears in are its two defining constraints
1423 * f - m d >=0
1424 * -(f - (m - 1)) + m d >= 0
1426 * then it can safely be removed.
1428 static isl_bool div_is_redundant(struct isl_basic_map *bmap, int div)
1430 int i;
1431 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1433 for (i = 0; i < bmap->n_eq; ++i)
1434 if (!isl_int_is_zero(bmap->eq[i][pos]))
1435 return isl_bool_false;
1437 for (i = 0; i < bmap->n_ineq; ++i) {
1438 isl_bool red;
1440 if (isl_int_is_zero(bmap->ineq[i][pos]))
1441 continue;
1442 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1443 if (red < 0 || !red)
1444 return red;
1447 for (i = 0; i < bmap->n_div; ++i) {
1448 if (isl_int_is_zero(bmap->div[i][0]))
1449 continue;
1450 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1451 return isl_bool_false;
1454 return isl_bool_true;
1458 * Remove divs that don't occur in any of the constraints or other divs.
1459 * These can arise when dropping constraints from a basic map or
1460 * when the divs of a basic map have been temporarily aligned
1461 * with the divs of another basic map.
1463 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
1465 int i;
1467 if (!bmap)
1468 return NULL;
1470 for (i = bmap->n_div-1; i >= 0; --i) {
1471 isl_bool redundant;
1473 redundant = div_is_redundant(bmap, i);
1474 if (redundant < 0)
1475 return isl_basic_map_free(bmap);
1476 if (!redundant)
1477 continue;
1478 bmap = isl_basic_map_drop_div(bmap, i);
1480 return bmap;
1483 /* Mark "bmap" as final, without checking for obviously redundant
1484 * integer divisions. This function should be used when "bmap"
1485 * is known not to involve any such integer divisions.
1487 __isl_give isl_basic_map *isl_basic_map_mark_final(
1488 __isl_take isl_basic_map *bmap)
1490 if (!bmap)
1491 return NULL;
1492 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1493 return bmap;
1496 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1498 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
1500 bmap = remove_redundant_divs(bmap);
1501 bmap = isl_basic_map_mark_final(bmap);
1502 return bmap;
1505 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1507 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1510 /* Remove definition of any div that is defined in terms of the given variable.
1511 * The div itself is not removed. Functions such as
1512 * eliminate_divs_ineq depend on the other divs remaining in place.
1514 static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
1515 int pos)
1517 int i;
1519 if (!bmap)
1520 return NULL;
1522 for (i = 0; i < bmap->n_div; ++i) {
1523 if (isl_int_is_zero(bmap->div[i][0]))
1524 continue;
1525 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1526 continue;
1527 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1528 if (!bmap)
1529 return NULL;
1531 return bmap;
1534 /* Eliminate the specified variables from the constraints using
1535 * Fourier-Motzkin. The variables themselves are not removed.
1537 struct isl_basic_map *isl_basic_map_eliminate_vars(
1538 struct isl_basic_map *bmap, unsigned pos, unsigned n)
1540 int d;
1541 int i, j, k;
1542 unsigned total;
1543 int need_gauss = 0;
1545 if (n == 0)
1546 return bmap;
1547 if (!bmap)
1548 return NULL;
1549 total = isl_basic_map_total_dim(bmap);
1551 bmap = isl_basic_map_cow(bmap);
1552 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1553 bmap = remove_dependent_vars(bmap, d);
1554 if (!bmap)
1555 return NULL;
1557 for (d = pos + n - 1;
1558 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1559 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1560 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1561 int n_lower, n_upper;
1562 if (!bmap)
1563 return NULL;
1564 for (i = 0; i < bmap->n_eq; ++i) {
1565 if (isl_int_is_zero(bmap->eq[i][1+d]))
1566 continue;
1567 eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
1568 isl_basic_map_drop_equality(bmap, i);
1569 need_gauss = 1;
1570 break;
1572 if (i < bmap->n_eq)
1573 continue;
1574 n_lower = 0;
1575 n_upper = 0;
1576 for (i = 0; i < bmap->n_ineq; ++i) {
1577 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1578 n_lower++;
1579 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1580 n_upper++;
1582 bmap = isl_basic_map_extend_constraints(bmap,
1583 0, n_lower * n_upper);
1584 if (!bmap)
1585 goto error;
1586 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1587 int last;
1588 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1589 continue;
1590 last = -1;
1591 for (j = 0; j < i; ++j) {
1592 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1593 continue;
1594 last = j;
1595 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1596 isl_int_sgn(bmap->ineq[j][1+d]))
1597 continue;
1598 k = isl_basic_map_alloc_inequality(bmap);
1599 if (k < 0)
1600 goto error;
1601 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1602 1+total);
1603 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1604 1+d, 1+total, NULL);
1606 isl_basic_map_drop_inequality(bmap, i);
1607 i = last + 1;
1609 if (n_lower > 0 && n_upper > 0) {
1610 bmap = isl_basic_map_normalize_constraints(bmap);
1611 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1612 NULL, 0);
1613 bmap = isl_basic_map_gauss(bmap, NULL);
1614 bmap = isl_basic_map_remove_redundancies(bmap);
1615 need_gauss = 0;
1616 if (!bmap)
1617 goto error;
1618 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1619 break;
1622 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1623 if (need_gauss)
1624 bmap = isl_basic_map_gauss(bmap, NULL);
1625 return bmap;
1626 error:
1627 isl_basic_map_free(bmap);
1628 return NULL;
1631 struct isl_basic_set *isl_basic_set_eliminate_vars(
1632 struct isl_basic_set *bset, unsigned pos, unsigned n)
1634 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1635 pos, n));
1638 /* Eliminate the specified n dimensions starting at first from the
1639 * constraints, without removing the dimensions from the space.
1640 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1641 * Otherwise, they are projected out and the original space is restored.
1643 __isl_give isl_basic_map *isl_basic_map_eliminate(
1644 __isl_take isl_basic_map *bmap,
1645 enum isl_dim_type type, unsigned first, unsigned n)
1647 isl_space *space;
1649 if (!bmap)
1650 return NULL;
1651 if (n == 0)
1652 return bmap;
1654 if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
1655 isl_die(bmap->ctx, isl_error_invalid,
1656 "index out of bounds", goto error);
1658 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1659 first += isl_basic_map_offset(bmap, type) - 1;
1660 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1661 return isl_basic_map_finalize(bmap);
1664 space = isl_basic_map_get_space(bmap);
1665 bmap = isl_basic_map_project_out(bmap, type, first, n);
1666 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1667 bmap = isl_basic_map_reset_space(bmap, space);
1668 return bmap;
1669 error:
1670 isl_basic_map_free(bmap);
1671 return NULL;
1674 __isl_give isl_basic_set *isl_basic_set_eliminate(
1675 __isl_take isl_basic_set *bset,
1676 enum isl_dim_type type, unsigned first, unsigned n)
1678 return isl_basic_map_eliminate(bset, type, first, n);
1681 /* Remove all constraints from "bmap" that reference any unknown local
1682 * variables (directly or indirectly).
1684 * Dropping all constraints on a local variable will make it redundant,
1685 * so it will get removed implicitly by
1686 * isl_basic_map_drop_constraints_involving_dims. Some other local
1687 * variables may also end up becoming redundant if they only appear
1688 * in constraints together with the unknown local variable.
1689 * Therefore, start over after calling
1690 * isl_basic_map_drop_constraints_involving_dims.
1692 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1693 __isl_take isl_basic_map *bmap)
1695 isl_bool known;
1696 int i, n_div, o_div;
1698 known = isl_basic_map_divs_known(bmap);
1699 if (known < 0)
1700 return isl_basic_map_free(bmap);
1701 if (known)
1702 return bmap;
1704 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1705 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1707 for (i = 0; i < n_div; ++i) {
1708 known = isl_basic_map_div_is_known(bmap, i);
1709 if (known < 0)
1710 return isl_basic_map_free(bmap);
1711 if (known)
1712 continue;
1713 bmap = remove_dependent_vars(bmap, o_div + i);
1714 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1715 isl_dim_div, i, 1);
1716 if (!bmap)
1717 return NULL;
1718 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1719 i = -1;
1722 return bmap;
1725 /* Remove all constraints from "map" that reference any unknown local
1726 * variables (directly or indirectly).
1728 * Since constraints may get dropped from the basic maps,
1729 * they may no longer be disjoint from each other.
1731 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1732 __isl_take isl_map *map)
1734 int i;
1735 isl_bool known;
1737 known = isl_map_divs_known(map);
1738 if (known < 0)
1739 return isl_map_free(map);
1740 if (known)
1741 return map;
1743 map = isl_map_cow(map);
1744 if (!map)
1745 return NULL;
1747 for (i = 0; i < map->n; ++i) {
1748 map->p[i] =
1749 isl_basic_map_drop_constraint_involving_unknown_divs(
1750 map->p[i]);
1751 if (!map->p[i])
1752 return isl_map_free(map);
1755 if (map->n > 1)
1756 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1758 return map;
1761 /* Don't assume equalities are in order, because align_divs
1762 * may have changed the order of the divs.
1764 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1766 int d, i;
1767 unsigned total;
1769 total = isl_space_dim(bmap->dim, isl_dim_all);
1770 for (d = 0; d < total; ++d)
1771 elim[d] = -1;
1772 for (i = 0; i < bmap->n_eq; ++i) {
1773 for (d = total - 1; d >= 0; --d) {
1774 if (isl_int_is_zero(bmap->eq[i][1+d]))
1775 continue;
1776 elim[d] = i;
1777 break;
1782 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1783 int *elim)
1785 compute_elimination_index(bset_to_bmap(bset), elim);
1788 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1789 __isl_keep isl_basic_map *bmap, int *elim)
1791 int d;
1792 int copied = 0;
1793 unsigned total;
1795 total = isl_space_dim(bmap->dim, isl_dim_all);
1796 for (d = total - 1; d >= 0; --d) {
1797 if (isl_int_is_zero(src[1+d]))
1798 continue;
1799 if (elim[d] == -1)
1800 continue;
1801 if (!copied) {
1802 isl_seq_cpy(dst, src, 1 + total);
1803 copied = 1;
1805 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1807 return copied;
1810 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1811 __isl_keep isl_basic_set *bset, int *elim)
1813 return reduced_using_equalities(dst, src,
1814 bset_to_bmap(bset), elim);
1817 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1818 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1820 int i;
1821 int *elim;
1823 if (!bset || !context)
1824 goto error;
1826 if (context->n_eq == 0) {
1827 isl_basic_set_free(context);
1828 return bset;
1831 bset = isl_basic_set_cow(bset);
1832 if (!bset)
1833 goto error;
1835 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1836 if (!elim)
1837 goto error;
1838 set_compute_elimination_index(context, elim);
1839 for (i = 0; i < bset->n_eq; ++i)
1840 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1841 context, elim);
1842 for (i = 0; i < bset->n_ineq; ++i)
1843 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1844 context, elim);
1845 isl_basic_set_free(context);
1846 free(elim);
1847 bset = isl_basic_set_simplify(bset);
1848 bset = isl_basic_set_finalize(bset);
1849 return bset;
1850 error:
1851 isl_basic_set_free(bset);
1852 isl_basic_set_free(context);
1853 return NULL;
1856 /* For each inequality in "ineq" that is a shifted (more relaxed)
1857 * copy of an inequality in "context", mark the corresponding entry
1858 * in "row" with -1.
1859 * If an inequality only has a non-negative constant term, then
1860 * mark it as well.
1862 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1863 __isl_keep isl_basic_set *context, int *row)
1865 struct isl_constraint_index ci;
1866 int n_ineq;
1867 unsigned total;
1868 int k;
1870 if (!ineq || !context)
1871 return isl_stat_error;
1872 if (context->n_ineq == 0)
1873 return isl_stat_ok;
1874 if (setup_constraint_index(&ci, context) < 0)
1875 return isl_stat_error;
1877 n_ineq = isl_mat_rows(ineq);
1878 total = isl_mat_cols(ineq) - 1;
1879 for (k = 0; k < n_ineq; ++k) {
1880 int l;
1881 isl_bool redundant;
1883 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1884 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1885 row[k] = -1;
1886 continue;
1888 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1889 if (redundant < 0)
1890 goto error;
1891 if (!redundant)
1892 continue;
1893 row[k] = -1;
1895 constraint_index_free(&ci);
1896 return isl_stat_ok;
1897 error:
1898 constraint_index_free(&ci);
1899 return isl_stat_error;
1902 static __isl_give isl_basic_set *remove_shifted_constraints(
1903 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1905 struct isl_constraint_index ci;
1906 int k;
1908 if (!bset || !context)
1909 return bset;
1911 if (context->n_ineq == 0)
1912 return bset;
1913 if (setup_constraint_index(&ci, context) < 0)
1914 return bset;
1916 for (k = 0; k < bset->n_ineq; ++k) {
1917 isl_bool redundant;
1919 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1920 if (redundant < 0)
1921 goto error;
1922 if (!redundant)
1923 continue;
1924 bset = isl_basic_set_cow(bset);
1925 if (!bset)
1926 goto error;
1927 isl_basic_set_drop_inequality(bset, k);
1928 --k;
1930 constraint_index_free(&ci);
1931 return bset;
1932 error:
1933 constraint_index_free(&ci);
1934 return bset;
1937 /* Remove constraints from "bmap" that are identical to constraints
1938 * in "context" or that are more relaxed (greater constant term).
1940 * We perform the test for shifted copies on the pure constraints
1941 * in remove_shifted_constraints.
1943 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1944 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1946 isl_basic_set *bset, *bset_context;
1948 if (!bmap || !context)
1949 goto error;
1951 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1952 isl_basic_map_free(context);
1953 return bmap;
1956 context = isl_basic_map_align_divs(context, bmap);
1957 bmap = isl_basic_map_align_divs(bmap, context);
1959 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1960 bset_context = isl_basic_map_underlying_set(context);
1961 bset = remove_shifted_constraints(bset, bset_context);
1962 isl_basic_set_free(bset_context);
1964 bmap = isl_basic_map_overlying_set(bset, bmap);
1966 return bmap;
1967 error:
1968 isl_basic_map_free(bmap);
1969 isl_basic_map_free(context);
1970 return NULL;
1973 /* Does the (linear part of a) constraint "c" involve any of the "len"
1974 * "relevant" dimensions?
1976 static int is_related(isl_int *c, int len, int *relevant)
1978 int i;
1980 for (i = 0; i < len; ++i) {
1981 if (!relevant[i])
1982 continue;
1983 if (!isl_int_is_zero(c[i]))
1984 return 1;
1987 return 0;
1990 /* Drop constraints from "bmap" that do not involve any of
1991 * the dimensions marked "relevant".
1993 static __isl_give isl_basic_map *drop_unrelated_constraints(
1994 __isl_take isl_basic_map *bmap, int *relevant)
1996 int i, dim;
1998 dim = isl_basic_map_dim(bmap, isl_dim_all);
1999 for (i = 0; i < dim; ++i)
2000 if (!relevant[i])
2001 break;
2002 if (i >= dim)
2003 return bmap;
2005 for (i = bmap->n_eq - 1; i >= 0; --i)
2006 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2007 bmap = isl_basic_map_cow(bmap);
2008 if (isl_basic_map_drop_equality(bmap, i) < 0)
2009 return isl_basic_map_free(bmap);
2012 for (i = bmap->n_ineq - 1; i >= 0; --i)
2013 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2014 bmap = isl_basic_map_cow(bmap);
2015 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2016 return isl_basic_map_free(bmap);
2019 return bmap;
2022 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2024 * In particular, for any variable involved in the constraint,
2025 * find the actual group id from before and replace the group
2026 * of the corresponding variable by the minimal group of all
2027 * the variables involved in the constraint considered so far
2028 * (if this minimum is smaller) or replace the minimum by this group
2029 * (if the minimum is larger).
2031 * At the end, all the variables in "c" will (indirectly) point
2032 * to the minimal of the groups that they referred to originally.
2034 static void update_groups(int dim, int *group, isl_int *c)
2036 int j;
2037 int min = dim;
2039 for (j = 0; j < dim; ++j) {
2040 if (isl_int_is_zero(c[j]))
2041 continue;
2042 while (group[j] >= 0 && group[group[j]] != group[j])
2043 group[j] = group[group[j]];
2044 if (group[j] == min)
2045 continue;
2046 if (group[j] < min) {
2047 if (min >= 0 && min < dim)
2048 group[min] = group[j];
2049 min = group[j];
2050 } else
2051 group[group[j]] = min;
2055 /* Allocate an array of groups of variables, one for each variable
2056 * in "context", initialized to zero.
2058 static int *alloc_groups(__isl_keep isl_basic_set *context)
2060 isl_ctx *ctx;
2061 int dim;
2063 dim = isl_basic_set_dim(context, isl_dim_set);
2064 ctx = isl_basic_set_get_ctx(context);
2065 return isl_calloc_array(ctx, int, dim);
2068 /* Drop constraints from "bmap" that only involve variables that are
2069 * not related to any of the variables marked with a "-1" in "group".
2071 * We construct groups of variables that collect variables that
2072 * (indirectly) appear in some common constraint of "bmap".
2073 * Each group is identified by the first variable in the group,
2074 * except for the special group of variables that was already identified
2075 * in the input as -1 (or are related to those variables).
2076 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2077 * otherwise the group of i is the group of group[i].
2079 * We first initialize groups for the remaining variables.
2080 * Then we iterate over the constraints of "bmap" and update the
2081 * group of the variables in the constraint by the smallest group.
2082 * Finally, we resolve indirect references to groups by running over
2083 * the variables.
2085 * After computing the groups, we drop constraints that do not involve
2086 * any variables in the -1 group.
2088 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2089 __isl_take isl_basic_map *bmap, __isl_take int *group)
2091 int dim;
2092 int i;
2093 int last;
2095 if (!bmap)
2096 return NULL;
2098 dim = isl_basic_map_dim(bmap, isl_dim_all);
2100 last = -1;
2101 for (i = 0; i < dim; ++i)
2102 if (group[i] >= 0)
2103 last = group[i] = i;
2104 if (last < 0) {
2105 free(group);
2106 return bmap;
2109 for (i = 0; i < bmap->n_eq; ++i)
2110 update_groups(dim, group, bmap->eq[i] + 1);
2111 for (i = 0; i < bmap->n_ineq; ++i)
2112 update_groups(dim, group, bmap->ineq[i] + 1);
2114 for (i = 0; i < dim; ++i)
2115 if (group[i] >= 0)
2116 group[i] = group[group[i]];
2118 for (i = 0; i < dim; ++i)
2119 group[i] = group[i] == -1;
2121 bmap = drop_unrelated_constraints(bmap, group);
2123 free(group);
2124 return bmap;
2127 /* Drop constraints from "context" that are irrelevant for computing
2128 * the gist of "bset".
2130 * In particular, drop constraints in variables that are not related
2131 * to any of the variables involved in the constraints of "bset"
2132 * in the sense that there is no sequence of constraints that connects them.
2134 * We first mark all variables that appear in "bset" as belonging
2135 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2137 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2138 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2140 int *group;
2141 int dim;
2142 int i, j;
2144 if (!context || !bset)
2145 return isl_basic_set_free(context);
2147 group = alloc_groups(context);
2149 if (!group)
2150 return isl_basic_set_free(context);
2152 dim = isl_basic_set_dim(bset, isl_dim_set);
2153 for (i = 0; i < dim; ++i) {
2154 for (j = 0; j < bset->n_eq; ++j)
2155 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2156 break;
2157 if (j < bset->n_eq) {
2158 group[i] = -1;
2159 continue;
2161 for (j = 0; j < bset->n_ineq; ++j)
2162 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2163 break;
2164 if (j < bset->n_ineq)
2165 group[i] = -1;
2168 return isl_basic_map_drop_unrelated_constraints(context, group);
2171 /* Drop constraints from "context" that are irrelevant for computing
2172 * the gist of the inequalities "ineq".
2173 * Inequalities in "ineq" for which the corresponding element of row
2174 * is set to -1 have already been marked for removal and should be ignored.
2176 * In particular, drop constraints in variables that are not related
2177 * to any of the variables involved in "ineq"
2178 * in the sense that there is no sequence of constraints that connects them.
2180 * We first mark all variables that appear in "bset" as belonging
2181 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2183 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2184 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2186 int *group;
2187 int dim;
2188 int i, j, n;
2190 if (!context || !ineq)
2191 return isl_basic_set_free(context);
2193 group = alloc_groups(context);
2195 if (!group)
2196 return isl_basic_set_free(context);
2198 dim = isl_basic_set_dim(context, isl_dim_set);
2199 n = isl_mat_rows(ineq);
2200 for (i = 0; i < dim; ++i) {
2201 for (j = 0; j < n; ++j) {
2202 if (row[j] < 0)
2203 continue;
2204 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2205 break;
2207 if (j < n)
2208 group[i] = -1;
2211 return isl_basic_map_drop_unrelated_constraints(context, group);
2214 /* Do all "n" entries of "row" contain a negative value?
2216 static int all_neg(int *row, int n)
2218 int i;
2220 for (i = 0; i < n; ++i)
2221 if (row[i] >= 0)
2222 return 0;
2224 return 1;
2227 /* Update the inequalities in "bset" based on the information in "row"
2228 * and "tab".
2230 * In particular, the array "row" contains either -1, meaning that
2231 * the corresponding inequality of "bset" is redundant, or the index
2232 * of an inequality in "tab".
2234 * If the row entry is -1, then drop the inequality.
2235 * Otherwise, if the constraint is marked redundant in the tableau,
2236 * then drop the inequality. Similarly, if it is marked as an equality
2237 * in the tableau, then turn the inequality into an equality and
2238 * perform Gaussian elimination.
2240 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2241 __isl_keep int *row, struct isl_tab *tab)
2243 int i;
2244 unsigned n_ineq;
2245 unsigned n_eq;
2246 int found_equality = 0;
2248 if (!bset)
2249 return NULL;
2250 if (tab && tab->empty)
2251 return isl_basic_set_set_to_empty(bset);
2253 n_ineq = bset->n_ineq;
2254 for (i = n_ineq - 1; i >= 0; --i) {
2255 if (row[i] < 0) {
2256 if (isl_basic_set_drop_inequality(bset, i) < 0)
2257 return isl_basic_set_free(bset);
2258 continue;
2260 if (!tab)
2261 continue;
2262 n_eq = tab->n_eq;
2263 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2264 isl_basic_map_inequality_to_equality(bset, i);
2265 found_equality = 1;
2266 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2267 if (isl_basic_set_drop_inequality(bset, i) < 0)
2268 return isl_basic_set_free(bset);
2272 if (found_equality)
2273 bset = isl_basic_set_gauss(bset, NULL);
2274 bset = isl_basic_set_finalize(bset);
2275 return bset;
2278 /* Update the inequalities in "bset" based on the information in "row"
2279 * and "tab" and free all arguments (other than "bset").
2281 static __isl_give isl_basic_set *update_ineq_free(
2282 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2283 __isl_take isl_basic_set *context, __isl_take int *row,
2284 struct isl_tab *tab)
2286 isl_mat_free(ineq);
2287 isl_basic_set_free(context);
2289 bset = update_ineq(bset, row, tab);
2291 free(row);
2292 isl_tab_free(tab);
2293 return bset;
2296 /* Remove all information from bset that is redundant in the context
2297 * of context.
2298 * "ineq" contains the (possibly transformed) inequalities of "bset",
2299 * in the same order.
2300 * The (explicit) equalities of "bset" are assumed to have been taken
2301 * into account by the transformation such that only the inequalities
2302 * are relevant.
2303 * "context" is assumed not to be empty.
2305 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2306 * A value of -1 means that the inequality is obviously redundant and may
2307 * not even appear in "tab".
2309 * We first mark the inequalities of "bset"
2310 * that are obviously redundant with respect to some inequality in "context".
2311 * Then we remove those constraints from "context" that have become
2312 * irrelevant for computing the gist of "bset".
2313 * Note that this removal of constraints cannot be replaced by
2314 * a factorization because factors in "bset" may still be connected
2315 * to each other through constraints in "context".
2317 * If there are any inequalities left, we construct a tableau for
2318 * the context and then add the inequalities of "bset".
2319 * Before adding these inequalities, we freeze all constraints such that
2320 * they won't be considered redundant in terms of the constraints of "bset".
2321 * Then we detect all redundant constraints (among the
2322 * constraints that weren't frozen), first by checking for redundancy in the
2323 * the tableau and then by checking if replacing a constraint by its negation
2324 * would lead to an empty set. This last step is fairly expensive
2325 * and could be optimized by more reuse of the tableau.
2326 * Finally, we update bset according to the results.
2328 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2329 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2331 int i, r;
2332 int *row = NULL;
2333 isl_ctx *ctx;
2334 isl_basic_set *combined = NULL;
2335 struct isl_tab *tab = NULL;
2336 unsigned n_eq, context_ineq;
2338 if (!bset || !ineq || !context)
2339 goto error;
2341 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2342 isl_basic_set_free(context);
2343 isl_mat_free(ineq);
2344 return bset;
2347 ctx = isl_basic_set_get_ctx(context);
2348 row = isl_calloc_array(ctx, int, bset->n_ineq);
2349 if (!row)
2350 goto error;
2352 if (mark_shifted_constraints(ineq, context, row) < 0)
2353 goto error;
2354 if (all_neg(row, bset->n_ineq))
2355 return update_ineq_free(bset, ineq, context, row, NULL);
2357 context = drop_irrelevant_constraints_marked(context, ineq, row);
2358 if (!context)
2359 goto error;
2360 if (isl_basic_set_plain_is_universe(context))
2361 return update_ineq_free(bset, ineq, context, row, NULL);
2363 n_eq = context->n_eq;
2364 context_ineq = context->n_ineq;
2365 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2366 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2367 tab = isl_tab_from_basic_set(combined, 0);
2368 for (i = 0; i < context_ineq; ++i)
2369 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2370 goto error;
2371 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2372 goto error;
2373 r = context_ineq;
2374 for (i = 0; i < bset->n_ineq; ++i) {
2375 if (row[i] < 0)
2376 continue;
2377 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2378 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2379 goto error;
2380 row[i] = r++;
2382 if (isl_tab_detect_implicit_equalities(tab) < 0)
2383 goto error;
2384 if (isl_tab_detect_redundant(tab) < 0)
2385 goto error;
2386 for (i = bset->n_ineq - 1; i >= 0; --i) {
2387 isl_basic_set *test;
2388 int is_empty;
2390 if (row[i] < 0)
2391 continue;
2392 r = row[i];
2393 if (tab->con[n_eq + r].is_redundant)
2394 continue;
2395 test = isl_basic_set_dup(combined);
2396 if (isl_inequality_negate(test, r) < 0)
2397 test = isl_basic_set_free(test);
2398 test = isl_basic_set_update_from_tab(test, tab);
2399 is_empty = isl_basic_set_is_empty(test);
2400 isl_basic_set_free(test);
2401 if (is_empty < 0)
2402 goto error;
2403 if (is_empty)
2404 tab->con[n_eq + r].is_redundant = 1;
2406 bset = update_ineq_free(bset, ineq, context, row, tab);
2407 if (bset) {
2408 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2409 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2412 isl_basic_set_free(combined);
2413 return bset;
2414 error:
2415 free(row);
2416 isl_mat_free(ineq);
2417 isl_tab_free(tab);
2418 isl_basic_set_free(combined);
2419 isl_basic_set_free(context);
2420 isl_basic_set_free(bset);
2421 return NULL;
2424 /* Extract the inequalities of "bset" as an isl_mat.
2426 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2428 unsigned total;
2429 isl_ctx *ctx;
2430 isl_mat *ineq;
2432 if (!bset)
2433 return NULL;
2435 ctx = isl_basic_set_get_ctx(bset);
2436 total = isl_basic_set_total_dim(bset);
2437 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2438 0, 1 + total);
2440 return ineq;
2443 /* Remove all information from "bset" that is redundant in the context
2444 * of "context", for the case where both "bset" and "context" are
2445 * full-dimensional.
2447 static __isl_give isl_basic_set *uset_gist_uncompressed(
2448 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2450 isl_mat *ineq;
2452 ineq = extract_ineq(bset);
2453 return uset_gist_full(bset, ineq, context);
2456 /* Remove all information from "bset" that is redundant in the context
2457 * of "context", for the case where the combined equalities of
2458 * "bset" and "context" allow for a compression that can be obtained
2459 * by preapplication of "T".
2461 * "bset" itself is not transformed by "T". Instead, the inequalities
2462 * are extracted from "bset" and those are transformed by "T".
2463 * uset_gist_full then determines which of the transformed inequalities
2464 * are redundant with respect to the transformed "context" and removes
2465 * the corresponding inequalities from "bset".
2467 * After preapplying "T" to the inequalities, any common factor is
2468 * removed from the coefficients. If this results in a tightening
2469 * of the constant term, then the same tightening is applied to
2470 * the corresponding untransformed inequality in "bset".
2471 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2473 * g f'(x) + r >= 0
2475 * with 0 <= r < g, then it is equivalent to
2477 * f'(x) >= 0
2479 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2480 * subspace compressed by T since the latter would be transformed to
2482 * g f'(x) >= 0
2484 static __isl_give isl_basic_set *uset_gist_compressed(
2485 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2486 __isl_take isl_mat *T)
2488 isl_ctx *ctx;
2489 isl_mat *ineq;
2490 int i, n_row, n_col;
2491 isl_int rem;
2493 ineq = extract_ineq(bset);
2494 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2495 context = isl_basic_set_preimage(context, T);
2497 if (!ineq || !context)
2498 goto error;
2499 if (isl_basic_set_plain_is_empty(context)) {
2500 isl_mat_free(ineq);
2501 isl_basic_set_free(context);
2502 return isl_basic_set_set_to_empty(bset);
2505 ctx = isl_mat_get_ctx(ineq);
2506 n_row = isl_mat_rows(ineq);
2507 n_col = isl_mat_cols(ineq);
2508 isl_int_init(rem);
2509 for (i = 0; i < n_row; ++i) {
2510 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2511 if (isl_int_is_zero(ctx->normalize_gcd))
2512 continue;
2513 if (isl_int_is_one(ctx->normalize_gcd))
2514 continue;
2515 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2516 ctx->normalize_gcd, n_col - 1);
2517 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2518 isl_int_fdiv_q(ineq->row[i][0],
2519 ineq->row[i][0], ctx->normalize_gcd);
2520 if (isl_int_is_zero(rem))
2521 continue;
2522 bset = isl_basic_set_cow(bset);
2523 if (!bset)
2524 break;
2525 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2527 isl_int_clear(rem);
2529 return uset_gist_full(bset, ineq, context);
2530 error:
2531 isl_mat_free(ineq);
2532 isl_basic_set_free(context);
2533 isl_basic_set_free(bset);
2534 return NULL;
2537 /* Project "bset" onto the variables that are involved in "template".
2539 static __isl_give isl_basic_set *project_onto_involved(
2540 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2542 int i, n;
2544 if (!bset || !template)
2545 return isl_basic_set_free(bset);
2547 n = isl_basic_set_dim(template, isl_dim_set);
2549 for (i = 0; i < n; ++i) {
2550 isl_bool involved;
2552 involved = isl_basic_set_involves_dims(template,
2553 isl_dim_set, i, 1);
2554 if (involved < 0)
2555 return isl_basic_set_free(bset);
2556 if (involved)
2557 continue;
2558 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2561 return bset;
2564 /* Remove all information from bset that is redundant in the context
2565 * of context. In particular, equalities that are linear combinations
2566 * of those in context are removed. Then the inequalities that are
2567 * redundant in the context of the equalities and inequalities of
2568 * context are removed.
2570 * First of all, we drop those constraints from "context"
2571 * that are irrelevant for computing the gist of "bset".
2572 * Alternatively, we could factorize the intersection of "context" and "bset".
2574 * We first compute the intersection of the integer affine hulls
2575 * of "bset" and "context",
2576 * compute the gist inside this intersection and then reduce
2577 * the constraints with respect to the equalities of the context
2578 * that only involve variables already involved in the input.
2580 * If two constraints are mutually redundant, then uset_gist_full
2581 * will remove the second of those constraints. We therefore first
2582 * sort the constraints so that constraints not involving existentially
2583 * quantified variables are given precedence over those that do.
2584 * We have to perform this sorting before the variable compression,
2585 * because that may effect the order of the variables.
2587 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2588 __isl_take isl_basic_set *context)
2590 isl_mat *eq;
2591 isl_mat *T;
2592 isl_basic_set *aff;
2593 isl_basic_set *aff_context;
2594 unsigned total;
2596 if (!bset || !context)
2597 goto error;
2599 context = drop_irrelevant_constraints(context, bset);
2601 bset = isl_basic_set_detect_equalities(bset);
2602 aff = isl_basic_set_copy(bset);
2603 aff = isl_basic_set_plain_affine_hull(aff);
2604 context = isl_basic_set_detect_equalities(context);
2605 aff_context = isl_basic_set_copy(context);
2606 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2607 aff = isl_basic_set_intersect(aff, aff_context);
2608 if (!aff)
2609 goto error;
2610 if (isl_basic_set_plain_is_empty(aff)) {
2611 isl_basic_set_free(bset);
2612 isl_basic_set_free(context);
2613 return aff;
2615 bset = isl_basic_set_sort_constraints(bset);
2616 if (aff->n_eq == 0) {
2617 isl_basic_set_free(aff);
2618 return uset_gist_uncompressed(bset, context);
2620 total = isl_basic_set_total_dim(bset);
2621 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2622 eq = isl_mat_cow(eq);
2623 T = isl_mat_variable_compression(eq, NULL);
2624 isl_basic_set_free(aff);
2625 if (T && T->n_col == 0) {
2626 isl_mat_free(T);
2627 isl_basic_set_free(context);
2628 return isl_basic_set_set_to_empty(bset);
2631 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2632 aff_context = project_onto_involved(aff_context, bset);
2634 bset = uset_gist_compressed(bset, context, T);
2635 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2637 if (bset) {
2638 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2639 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2642 return bset;
2643 error:
2644 isl_basic_set_free(bset);
2645 isl_basic_set_free(context);
2646 return NULL;
2649 /* Return the number of equality constraints in "bmap" that involve
2650 * local variables. This function assumes that Gaussian elimination
2651 * has been applied to the equality constraints.
2653 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2655 int i;
2656 int total, n_div;
2658 if (!bmap)
2659 return -1;
2661 if (bmap->n_eq == 0)
2662 return 0;
2664 total = isl_basic_map_dim(bmap, isl_dim_all);
2665 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2666 total -= n_div;
2668 for (i = 0; i < bmap->n_eq; ++i)
2669 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2670 n_div) == -1)
2671 return i;
2673 return bmap->n_eq;
2676 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2677 * The constraints are assumed not to involve any local variables.
2679 static __isl_give isl_basic_map *basic_map_from_equalities(
2680 __isl_take isl_space *space, __isl_take isl_mat *eq)
2682 int i, k;
2683 isl_basic_map *bmap = NULL;
2685 if (!space || !eq)
2686 goto error;
2688 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2689 isl_die(isl_space_get_ctx(space), isl_error_internal,
2690 "unexpected number of columns", goto error);
2692 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2693 0, eq->n_row, 0);
2694 for (i = 0; i < eq->n_row; ++i) {
2695 k = isl_basic_map_alloc_equality(bmap);
2696 if (k < 0)
2697 goto error;
2698 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2701 isl_space_free(space);
2702 isl_mat_free(eq);
2703 return bmap;
2704 error:
2705 isl_space_free(space);
2706 isl_mat_free(eq);
2707 isl_basic_map_free(bmap);
2708 return NULL;
2711 /* Construct and return a variable compression based on the equality
2712 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2713 * "n1" is the number of (initial) equality constraints in "bmap1"
2714 * that do involve local variables.
2715 * "n2" is the number of (initial) equality constraints in "bmap2"
2716 * that do involve local variables.
2717 * "total" is the total number of other variables.
2718 * This function assumes that Gaussian elimination
2719 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2720 * such that the equality constraints not involving local variables
2721 * are those that start at "n1" or "n2".
2723 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2724 * then simply compute the compression based on the equality constraints
2725 * in the other basic map.
2726 * Otherwise, combine the equality constraints from both into a new
2727 * basic map such that Gaussian elimination can be applied to this combination
2728 * and then construct a variable compression from the resulting
2729 * equality constraints.
2731 static __isl_give isl_mat *combined_variable_compression(
2732 __isl_keep isl_basic_map *bmap1, int n1,
2733 __isl_keep isl_basic_map *bmap2, int n2, int total)
2735 isl_ctx *ctx;
2736 isl_mat *E1, *E2, *V;
2737 isl_basic_map *bmap;
2739 ctx = isl_basic_map_get_ctx(bmap1);
2740 if (bmap1->n_eq == n1) {
2741 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2742 n2, bmap2->n_eq - n2, 0, 1 + total);
2743 return isl_mat_variable_compression(E2, NULL);
2745 if (bmap2->n_eq == n2) {
2746 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2747 n1, bmap1->n_eq - n1, 0, 1 + total);
2748 return isl_mat_variable_compression(E1, NULL);
2750 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2751 n1, bmap1->n_eq - n1, 0, 1 + total);
2752 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2753 n2, bmap2->n_eq - n2, 0, 1 + total);
2754 E1 = isl_mat_concat(E1, E2);
2755 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2756 bmap = isl_basic_map_gauss(bmap, NULL);
2757 if (!bmap)
2758 return NULL;
2759 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2760 V = isl_mat_variable_compression(E1, NULL);
2761 isl_basic_map_free(bmap);
2763 return V;
2766 /* Extract the stride constraints from "bmap", compressed
2767 * with respect to both the stride constraints in "context" and
2768 * the remaining equality constraints in both "bmap" and "context".
2769 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2770 * "context_n_eq" is the number of (initial) stride constraints in "context".
2772 * Let x be all variables in "bmap" (and "context") other than the local
2773 * variables. First compute a variable compression
2775 * x = V x'
2777 * based on the non-stride equality constraints in "bmap" and "context".
2778 * Consider the stride constraints of "context",
2780 * A(x) + B(y) = 0
2782 * with y the local variables and plug in the variable compression,
2783 * resulting in
2785 * A(V x') + B(y) = 0
2787 * Use these constraints to compute a parameter compression on x'
2789 * x' = T x''
2791 * Now consider the stride constraints of "bmap"
2793 * C(x) + D(y) = 0
2795 * and plug in x = V*T x''.
2796 * That is, return A = [C*V*T D].
2798 static __isl_give isl_mat *extract_compressed_stride_constraints(
2799 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2800 __isl_keep isl_basic_map *context, int context_n_eq)
2802 int total, n_div;
2803 isl_ctx *ctx;
2804 isl_mat *A, *B, *T, *V;
2806 total = isl_basic_map_dim(context, isl_dim_all);
2807 n_div = isl_basic_map_dim(context, isl_dim_div);
2808 total -= n_div;
2810 ctx = isl_basic_map_get_ctx(bmap);
2812 V = combined_variable_compression(bmap, bmap_n_eq,
2813 context, context_n_eq, total);
2815 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2816 B = isl_mat_sub_alloc6(ctx, context->eq,
2817 0, context_n_eq, 1 + total, n_div);
2818 A = isl_mat_product(A, isl_mat_copy(V));
2819 T = isl_mat_parameter_compression_ext(A, B);
2820 T = isl_mat_product(V, T);
2822 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2823 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2825 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2826 0, bmap_n_eq, 0, 1 + total + n_div);
2827 A = isl_mat_product(A, T);
2829 return A;
2832 /* Remove the prime factors from *g that have an exponent that
2833 * is strictly smaller than the exponent in "c".
2834 * All exponents in *g are known to be smaller than or equal
2835 * to those in "c".
2837 * That is, if *g is equal to
2839 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2841 * and "c" is equal to
2843 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2845 * then update *g to
2847 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2848 * p_n^{e_n * (e_n = f_n)}
2850 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2851 * neither does the gcd of *g and c / *g.
2852 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2853 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2854 * Dividing *g by this gcd therefore strictly reduces the exponent
2855 * of the prime factors that need to be removed, while leaving the
2856 * other prime factors untouched.
2857 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2858 * removes all undesired factors, without removing any others.
2860 static void remove_incomplete_powers(isl_int *g, isl_int c)
2862 isl_int t;
2864 isl_int_init(t);
2865 for (;;) {
2866 isl_int_divexact(t, c, *g);
2867 isl_int_gcd(t, t, *g);
2868 if (isl_int_is_one(t))
2869 break;
2870 isl_int_divexact(*g, *g, t);
2872 isl_int_clear(t);
2875 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2876 * of the same stride constraints in a compressed space that exploits
2877 * all equalities in the context and the other equalities in "bmap".
2879 * If the stride constraints of "bmap" are of the form
2881 * C(x) + D(y) = 0
2883 * then A is of the form
2885 * B(x') + D(y) = 0
2887 * If any of these constraints involves only a single local variable y,
2888 * then the constraint appears as
2890 * f(x) + m y_i = 0
2892 * in "bmap" and as
2894 * h(x') + m y_i = 0
2896 * in "A".
2898 * Let g be the gcd of m and the coefficients of h.
2899 * Then, in particular, g is a divisor of the coefficients of h and
2901 * f(x) = h(x')
2903 * is known to be a multiple of g.
2904 * If some prime factor in m appears with the same exponent in g,
2905 * then it can be removed from m because f(x) is already known
2906 * to be a multiple of g and therefore in particular of this power
2907 * of the prime factors.
2908 * Prime factors that appear with a smaller exponent in g cannot
2909 * be removed from m.
2910 * Let g' be the divisor of g containing all prime factors that
2911 * appear with the same exponent in m and g, then
2913 * f(x) + m y_i = 0
2915 * can be replaced by
2917 * f(x) + m/g' y_i' = 0
2919 * Note that (if g' != 1) this changes the explicit representation
2920 * of y_i to that of y_i', so the integer division at position i
2921 * is marked unknown and later recomputed by a call to
2922 * isl_basic_map_gauss.
2924 static __isl_give isl_basic_map *reduce_stride_constraints(
2925 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2927 int i;
2928 int total, n_div;
2929 int any = 0;
2930 isl_int gcd;
2932 if (!bmap || !A)
2933 return isl_basic_map_free(bmap);
2935 total = isl_basic_map_dim(bmap, isl_dim_all);
2936 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2937 total -= n_div;
2939 isl_int_init(gcd);
2940 for (i = 0; i < n; ++i) {
2941 int div;
2943 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2944 if (div < 0)
2945 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2946 "equality constraints modified unexpectedly",
2947 goto error);
2948 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
2949 n_div - div - 1) != -1)
2950 continue;
2951 if (isl_mat_row_gcd(A, i, &gcd) < 0)
2952 goto error;
2953 if (isl_int_is_one(gcd))
2954 continue;
2955 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2956 if (isl_int_is_one(gcd))
2957 continue;
2958 isl_int_divexact(bmap->eq[i][1 + total + div],
2959 bmap->eq[i][1 + total + div], gcd);
2960 bmap = isl_basic_map_mark_div_unknown(bmap, div);
2961 if (!bmap)
2962 goto error;
2963 any = 1;
2965 isl_int_clear(gcd);
2967 if (any)
2968 bmap = isl_basic_map_gauss(bmap, NULL);
2970 return bmap;
2971 error:
2972 isl_int_clear(gcd);
2973 isl_basic_map_free(bmap);
2974 return NULL;
2977 /* Simplify the stride constraints in "bmap" based on
2978 * the remaining equality constraints in "bmap" and all equality
2979 * constraints in "context".
2980 * Only do this if both "bmap" and "context" have stride constraints.
2982 * First extract a copy of the stride constraints in "bmap" in a compressed
2983 * space exploiting all the other equality constraints and then
2984 * use this compressed copy to simplify the original stride constraints.
2986 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
2987 __isl_keep isl_basic_map *context)
2989 int bmap_n_eq, context_n_eq;
2990 isl_mat *A;
2992 if (!bmap || !context)
2993 return isl_basic_map_free(bmap);
2995 bmap_n_eq = n_div_eq(bmap);
2996 context_n_eq = n_div_eq(context);
2998 if (bmap_n_eq < 0 || context_n_eq < 0)
2999 return isl_basic_map_free(bmap);
3000 if (bmap_n_eq == 0 || context_n_eq == 0)
3001 return bmap;
3003 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3004 context, context_n_eq);
3005 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3007 isl_mat_free(A);
3009 return bmap;
3012 /* Return a basic map that has the same intersection with "context" as "bmap"
3013 * and that is as "simple" as possible.
3015 * The core computation is performed on the pure constraints.
3016 * When we add back the meaning of the integer divisions, we need
3017 * to (re)introduce the div constraints. If we happen to have
3018 * discovered that some of these integer divisions are equal to
3019 * some affine combination of other variables, then these div
3020 * constraints may end up getting simplified in terms of the equalities,
3021 * resulting in extra inequalities on the other variables that
3022 * may have been removed already or that may not even have been
3023 * part of the input. We try and remove those constraints of
3024 * this form that are most obviously redundant with respect to
3025 * the context. We also remove those div constraints that are
3026 * redundant with respect to the other constraints in the result.
3028 * The stride constraints among the equality constraints in "bmap" are
3029 * also simplified with respecting to the other equality constraints
3030 * in "bmap" and with respect to all equality constraints in "context".
3032 struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
3033 struct isl_basic_map *context)
3035 isl_basic_set *bset, *eq;
3036 isl_basic_map *eq_bmap;
3037 unsigned total, n_div, extra, n_eq, n_ineq;
3039 if (!bmap || !context)
3040 goto error;
3042 if (isl_basic_map_plain_is_universe(bmap)) {
3043 isl_basic_map_free(context);
3044 return bmap;
3046 if (isl_basic_map_plain_is_empty(context)) {
3047 isl_space *space = isl_basic_map_get_space(bmap);
3048 isl_basic_map_free(bmap);
3049 isl_basic_map_free(context);
3050 return isl_basic_map_universe(space);
3052 if (isl_basic_map_plain_is_empty(bmap)) {
3053 isl_basic_map_free(context);
3054 return bmap;
3057 bmap = isl_basic_map_remove_redundancies(bmap);
3058 context = isl_basic_map_remove_redundancies(context);
3059 if (!context)
3060 goto error;
3062 context = isl_basic_map_align_divs(context, bmap);
3063 n_div = isl_basic_map_dim(context, isl_dim_div);
3064 total = isl_basic_map_dim(bmap, isl_dim_all);
3065 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3067 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3068 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3069 bset = uset_gist(bset,
3070 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3071 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3073 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3074 isl_basic_set_plain_is_empty(bset)) {
3075 isl_basic_map_free(context);
3076 return isl_basic_map_overlying_set(bset, bmap);
3079 n_eq = bset->n_eq;
3080 n_ineq = bset->n_ineq;
3081 eq = isl_basic_set_copy(bset);
3082 eq = isl_basic_set_cow(eq);
3083 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3084 eq = isl_basic_set_free(eq);
3085 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3086 bset = isl_basic_set_free(bset);
3088 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3089 eq_bmap = gist_strides(eq_bmap, context);
3090 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3091 bmap = isl_basic_map_overlying_set(bset, bmap);
3092 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3093 bmap = isl_basic_map_remove_redundancies(bmap);
3095 return bmap;
3096 error:
3097 isl_basic_map_free(bmap);
3098 isl_basic_map_free(context);
3099 return NULL;
3103 * Assumes context has no implicit divs.
3105 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3106 __isl_take isl_basic_map *context)
3108 int i;
3110 if (!map || !context)
3111 goto error;
3113 if (isl_basic_map_plain_is_empty(context)) {
3114 isl_space *space = isl_map_get_space(map);
3115 isl_map_free(map);
3116 isl_basic_map_free(context);
3117 return isl_map_universe(space);
3120 context = isl_basic_map_remove_redundancies(context);
3121 map = isl_map_cow(map);
3122 if (!map || !context)
3123 goto error;
3124 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3125 map = isl_map_compute_divs(map);
3126 if (!map)
3127 goto error;
3128 for (i = map->n - 1; i >= 0; --i) {
3129 map->p[i] = isl_basic_map_gist(map->p[i],
3130 isl_basic_map_copy(context));
3131 if (!map->p[i])
3132 goto error;
3133 if (isl_basic_map_plain_is_empty(map->p[i])) {
3134 isl_basic_map_free(map->p[i]);
3135 if (i != map->n - 1)
3136 map->p[i] = map->p[map->n - 1];
3137 map->n--;
3140 isl_basic_map_free(context);
3141 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3142 return map;
3143 error:
3144 isl_map_free(map);
3145 isl_basic_map_free(context);
3146 return NULL;
3149 /* Drop all inequalities from "bmap" that also appear in "context".
3150 * "context" is assumed to have only known local variables and
3151 * the initial local variables of "bmap" are assumed to be the same
3152 * as those of "context".
3153 * The constraints of both "bmap" and "context" are assumed
3154 * to have been sorted using isl_basic_map_sort_constraints.
3156 * Run through the inequality constraints of "bmap" and "context"
3157 * in sorted order.
3158 * If a constraint of "bmap" involves variables not in "context",
3159 * then it cannot appear in "context".
3160 * If a matching constraint is found, it is removed from "bmap".
3162 static __isl_give isl_basic_map *drop_inequalities(
3163 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3165 int i1, i2;
3166 unsigned total, extra;
3168 if (!bmap || !context)
3169 return isl_basic_map_free(bmap);
3171 total = isl_basic_map_total_dim(context);
3172 extra = isl_basic_map_total_dim(bmap) - total;
3174 i1 = bmap->n_ineq - 1;
3175 i2 = context->n_ineq - 1;
3176 while (bmap && i1 >= 0 && i2 >= 0) {
3177 int cmp;
3179 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3180 extra) != -1) {
3181 --i1;
3182 continue;
3184 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3185 context->ineq[i2]);
3186 if (cmp < 0) {
3187 --i2;
3188 continue;
3190 if (cmp > 0) {
3191 --i1;
3192 continue;
3194 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3195 bmap = isl_basic_map_cow(bmap);
3196 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3197 bmap = isl_basic_map_free(bmap);
3199 --i1;
3200 --i2;
3203 return bmap;
3206 /* Drop all equalities from "bmap" that also appear in "context".
3207 * "context" is assumed to have only known local variables and
3208 * the initial local variables of "bmap" are assumed to be the same
3209 * as those of "context".
3211 * Run through the equality constraints of "bmap" and "context"
3212 * in sorted order.
3213 * If a constraint of "bmap" involves variables not in "context",
3214 * then it cannot appear in "context".
3215 * If a matching constraint is found, it is removed from "bmap".
3217 static __isl_give isl_basic_map *drop_equalities(
3218 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3220 int i1, i2;
3221 unsigned total, extra;
3223 if (!bmap || !context)
3224 return isl_basic_map_free(bmap);
3226 total = isl_basic_map_total_dim(context);
3227 extra = isl_basic_map_total_dim(bmap) - total;
3229 i1 = bmap->n_eq - 1;
3230 i2 = context->n_eq - 1;
3232 while (bmap && i1 >= 0 && i2 >= 0) {
3233 int last1, last2;
3235 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3236 extra) != -1)
3237 break;
3238 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3239 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3240 if (last1 > last2) {
3241 --i2;
3242 continue;
3244 if (last1 < last2) {
3245 --i1;
3246 continue;
3248 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3249 bmap = isl_basic_map_cow(bmap);
3250 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3251 bmap = isl_basic_map_free(bmap);
3253 --i1;
3254 --i2;
3257 return bmap;
3260 /* Remove the constraints in "context" from "bmap".
3261 * "context" is assumed to have explicit representations
3262 * for all local variables.
3264 * First align the divs of "bmap" to those of "context" and
3265 * sort the constraints. Then drop all constraints from "bmap"
3266 * that appear in "context".
3268 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3269 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3271 isl_bool done, known;
3273 done = isl_basic_map_plain_is_universe(context);
3274 if (done == isl_bool_false)
3275 done = isl_basic_map_plain_is_universe(bmap);
3276 if (done == isl_bool_false)
3277 done = isl_basic_map_plain_is_empty(context);
3278 if (done == isl_bool_false)
3279 done = isl_basic_map_plain_is_empty(bmap);
3280 if (done < 0)
3281 goto error;
3282 if (done) {
3283 isl_basic_map_free(context);
3284 return bmap;
3286 known = isl_basic_map_divs_known(context);
3287 if (known < 0)
3288 goto error;
3289 if (!known)
3290 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3291 "context has unknown divs", goto error);
3293 bmap = isl_basic_map_align_divs(bmap, context);
3294 bmap = isl_basic_map_gauss(bmap, NULL);
3295 bmap = isl_basic_map_sort_constraints(bmap);
3296 context = isl_basic_map_sort_constraints(context);
3298 bmap = drop_inequalities(bmap, context);
3299 bmap = drop_equalities(bmap, context);
3301 isl_basic_map_free(context);
3302 bmap = isl_basic_map_finalize(bmap);
3303 return bmap;
3304 error:
3305 isl_basic_map_free(bmap);
3306 isl_basic_map_free(context);
3307 return NULL;
3310 /* Replace "map" by the disjunct at position "pos" and free "context".
3312 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3313 int pos, __isl_take isl_basic_map *context)
3315 isl_basic_map *bmap;
3317 bmap = isl_basic_map_copy(map->p[pos]);
3318 isl_map_free(map);
3319 isl_basic_map_free(context);
3320 return isl_map_from_basic_map(bmap);
3323 /* Remove the constraints in "context" from "map".
3324 * If any of the disjuncts in the result turns out to be the universe,
3325 * then return this universe.
3326 * "context" is assumed to have explicit representations
3327 * for all local variables.
3329 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3330 __isl_take isl_basic_map *context)
3332 int i;
3333 isl_bool univ, known;
3335 univ = isl_basic_map_plain_is_universe(context);
3336 if (univ < 0)
3337 goto error;
3338 if (univ) {
3339 isl_basic_map_free(context);
3340 return map;
3342 known = isl_basic_map_divs_known(context);
3343 if (known < 0)
3344 goto error;
3345 if (!known)
3346 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3347 "context has unknown divs", goto error);
3349 map = isl_map_cow(map);
3350 if (!map)
3351 goto error;
3352 for (i = 0; i < map->n; ++i) {
3353 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3354 isl_basic_map_copy(context));
3355 univ = isl_basic_map_plain_is_universe(map->p[i]);
3356 if (univ < 0)
3357 goto error;
3358 if (univ && map->n > 1)
3359 return replace_by_disjunct(map, i, context);
3362 isl_basic_map_free(context);
3363 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3364 if (map->n > 1)
3365 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3366 return map;
3367 error:
3368 isl_map_free(map);
3369 isl_basic_map_free(context);
3370 return NULL;
3373 /* Replace "map" by a universe map in the same space and free "drop".
3375 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3376 __isl_take isl_map *drop)
3378 isl_map *res;
3380 res = isl_map_universe(isl_map_get_space(map));
3381 isl_map_free(map);
3382 isl_map_free(drop);
3383 return res;
3386 /* Return a map that has the same intersection with "context" as "map"
3387 * and that is as "simple" as possible.
3389 * If "map" is already the universe, then we cannot make it any simpler.
3390 * Similarly, if "context" is the universe, then we cannot exploit it
3391 * to simplify "map"
3392 * If "map" and "context" are identical to each other, then we can
3393 * return the corresponding universe.
3395 * If either "map" or "context" consists of multiple disjuncts,
3396 * then check if "context" happens to be a subset of "map",
3397 * in which case all constraints can be removed.
3398 * In case of multiple disjuncts, the standard procedure
3399 * may not be able to detect that all constraints can be removed.
3401 * If none of these cases apply, we have to work a bit harder.
3402 * During this computation, we make use of a single disjunct context,
3403 * so if the original context consists of more than one disjunct
3404 * then we need to approximate the context by a single disjunct set.
3405 * Simply taking the simple hull may drop constraints that are
3406 * only implicitly available in each disjunct. We therefore also
3407 * look for constraints among those defining "map" that are valid
3408 * for the context. These can then be used to simplify away
3409 * the corresponding constraints in "map".
3411 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3412 __isl_take isl_map *context)
3414 int equal;
3415 int is_universe;
3416 int single_disjunct_map, single_disjunct_context;
3417 isl_bool subset;
3418 isl_basic_map *hull;
3420 is_universe = isl_map_plain_is_universe(map);
3421 if (is_universe >= 0 && !is_universe)
3422 is_universe = isl_map_plain_is_universe(context);
3423 if (is_universe < 0)
3424 goto error;
3425 if (is_universe) {
3426 isl_map_free(context);
3427 return map;
3430 equal = isl_map_plain_is_equal(map, context);
3431 if (equal < 0)
3432 goto error;
3433 if (equal)
3434 return replace_by_universe(map, context);
3436 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3437 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3438 if (!single_disjunct_map || !single_disjunct_context) {
3439 subset = isl_map_is_subset(context, map);
3440 if (subset < 0)
3441 goto error;
3442 if (subset)
3443 return replace_by_universe(map, context);
3446 context = isl_map_compute_divs(context);
3447 if (!context)
3448 goto error;
3449 if (single_disjunct_context) {
3450 hull = isl_map_simple_hull(context);
3451 } else {
3452 isl_ctx *ctx;
3453 isl_map_list *list;
3455 ctx = isl_map_get_ctx(map);
3456 list = isl_map_list_alloc(ctx, 2);
3457 list = isl_map_list_add(list, isl_map_copy(context));
3458 list = isl_map_list_add(list, isl_map_copy(map));
3459 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3460 list);
3462 return isl_map_gist_basic_map(map, hull);
3463 error:
3464 isl_map_free(map);
3465 isl_map_free(context);
3466 return NULL;
3469 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3470 __isl_take isl_map *context)
3472 return isl_map_align_params_map_map_and(map, context, &map_gist);
3475 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3476 struct isl_basic_set *context)
3478 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3479 bset_to_bmap(context)));
3482 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3483 __isl_take isl_basic_set *context)
3485 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3486 bset_to_bmap(context)));
3489 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3490 __isl_take isl_basic_set *context)
3492 isl_space *space = isl_set_get_space(set);
3493 isl_basic_set *dom_context = isl_basic_set_universe(space);
3494 dom_context = isl_basic_set_intersect_params(dom_context, context);
3495 return isl_set_gist_basic_set(set, dom_context);
3498 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3499 __isl_take isl_set *context)
3501 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3504 /* Compute the gist of "bmap" with respect to the constraints "context"
3505 * on the domain.
3507 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3508 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3510 isl_space *space = isl_basic_map_get_space(bmap);
3511 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3513 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3514 return isl_basic_map_gist(bmap, bmap_context);
3517 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3518 __isl_take isl_set *context)
3520 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3521 map_context = isl_map_intersect_domain(map_context, context);
3522 return isl_map_gist(map, map_context);
3525 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3526 __isl_take isl_set *context)
3528 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3529 map_context = isl_map_intersect_range(map_context, context);
3530 return isl_map_gist(map, map_context);
3533 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3534 __isl_take isl_set *context)
3536 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3537 map_context = isl_map_intersect_params(map_context, context);
3538 return isl_map_gist(map, map_context);
3541 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3542 __isl_take isl_set *context)
3544 return isl_map_gist_params(set, context);
3547 /* Quick check to see if two basic maps are disjoint.
3548 * In particular, we reduce the equalities and inequalities of
3549 * one basic map in the context of the equalities of the other
3550 * basic map and check if we get a contradiction.
3552 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3553 __isl_keep isl_basic_map *bmap2)
3555 struct isl_vec *v = NULL;
3556 int *elim = NULL;
3557 unsigned total;
3558 int i;
3560 if (!bmap1 || !bmap2)
3561 return isl_bool_error;
3562 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3563 return isl_bool_error);
3564 if (bmap1->n_div || bmap2->n_div)
3565 return isl_bool_false;
3566 if (!bmap1->n_eq && !bmap2->n_eq)
3567 return isl_bool_false;
3569 total = isl_space_dim(bmap1->dim, isl_dim_all);
3570 if (total == 0)
3571 return isl_bool_false;
3572 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3573 if (!v)
3574 goto error;
3575 elim = isl_alloc_array(bmap1->ctx, int, total);
3576 if (!elim)
3577 goto error;
3578 compute_elimination_index(bmap1, elim);
3579 for (i = 0; i < bmap2->n_eq; ++i) {
3580 int reduced;
3581 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3582 bmap1, elim);
3583 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3584 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3585 goto disjoint;
3587 for (i = 0; i < bmap2->n_ineq; ++i) {
3588 int reduced;
3589 reduced = reduced_using_equalities(v->block.data,
3590 bmap2->ineq[i], bmap1, elim);
3591 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3592 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3593 goto disjoint;
3595 compute_elimination_index(bmap2, elim);
3596 for (i = 0; i < bmap1->n_ineq; ++i) {
3597 int reduced;
3598 reduced = reduced_using_equalities(v->block.data,
3599 bmap1->ineq[i], bmap2, elim);
3600 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3601 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3602 goto disjoint;
3604 isl_vec_free(v);
3605 free(elim);
3606 return isl_bool_false;
3607 disjoint:
3608 isl_vec_free(v);
3609 free(elim);
3610 return isl_bool_true;
3611 error:
3612 isl_vec_free(v);
3613 free(elim);
3614 return isl_bool_error;
3617 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3618 __isl_keep isl_basic_set *bset2)
3620 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3621 bset_to_bmap(bset2));
3624 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3626 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3627 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3628 __isl_keep isl_basic_map *bmap2))
3630 int i, j;
3632 if (!map1 || !map2)
3633 return isl_bool_error;
3635 for (i = 0; i < map1->n; ++i) {
3636 for (j = 0; j < map2->n; ++j) {
3637 isl_bool d = test(map1->p[i], map2->p[j]);
3638 if (d != isl_bool_true)
3639 return d;
3643 return isl_bool_true;
3646 /* Are "map1" and "map2" obviously disjoint, based on information
3647 * that can be derived without looking at the individual basic maps?
3649 * In particular, if one of them is empty or if they live in different spaces
3650 * (ignoring parameters), then they are clearly disjoint.
3652 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3653 __isl_keep isl_map *map2)
3655 isl_bool disjoint;
3656 isl_bool match;
3658 if (!map1 || !map2)
3659 return isl_bool_error;
3661 disjoint = isl_map_plain_is_empty(map1);
3662 if (disjoint < 0 || disjoint)
3663 return disjoint;
3665 disjoint = isl_map_plain_is_empty(map2);
3666 if (disjoint < 0 || disjoint)
3667 return disjoint;
3669 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3670 map2->dim, isl_dim_in);
3671 if (match < 0 || !match)
3672 return match < 0 ? isl_bool_error : isl_bool_true;
3674 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3675 map2->dim, isl_dim_out);
3676 if (match < 0 || !match)
3677 return match < 0 ? isl_bool_error : isl_bool_true;
3679 return isl_bool_false;
3682 /* Are "map1" and "map2" obviously disjoint?
3684 * If one of them is empty or if they live in different spaces (ignoring
3685 * parameters), then they are clearly disjoint.
3686 * This is checked by isl_map_plain_is_disjoint_global.
3688 * If they have different parameters, then we skip any further tests.
3690 * If they are obviously equal, but not obviously empty, then we will
3691 * not be able to detect if they are disjoint.
3693 * Otherwise we check if each basic map in "map1" is obviously disjoint
3694 * from each basic map in "map2".
3696 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3697 __isl_keep isl_map *map2)
3699 isl_bool disjoint;
3700 isl_bool intersect;
3701 isl_bool match;
3703 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3704 if (disjoint < 0 || disjoint)
3705 return disjoint;
3707 match = isl_map_has_equal_params(map1, map2);
3708 if (match < 0 || !match)
3709 return match < 0 ? isl_bool_error : isl_bool_false;
3711 intersect = isl_map_plain_is_equal(map1, map2);
3712 if (intersect < 0 || intersect)
3713 return intersect < 0 ? isl_bool_error : isl_bool_false;
3715 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3718 /* Are "map1" and "map2" disjoint?
3720 * They are disjoint if they are "obviously disjoint" or if one of them
3721 * is empty. Otherwise, they are not disjoint if one of them is universal.
3722 * If the two inputs are (obviously) equal and not empty, then they are
3723 * not disjoint.
3724 * If none of these cases apply, then check if all pairs of basic maps
3725 * are disjoint.
3727 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3729 isl_bool disjoint;
3730 isl_bool intersect;
3732 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3733 if (disjoint < 0 || disjoint)
3734 return disjoint;
3736 disjoint = isl_map_is_empty(map1);
3737 if (disjoint < 0 || disjoint)
3738 return disjoint;
3740 disjoint = isl_map_is_empty(map2);
3741 if (disjoint < 0 || disjoint)
3742 return disjoint;
3744 intersect = isl_map_plain_is_universe(map1);
3745 if (intersect < 0 || intersect)
3746 return intersect < 0 ? isl_bool_error : isl_bool_false;
3748 intersect = isl_map_plain_is_universe(map2);
3749 if (intersect < 0 || intersect)
3750 return intersect < 0 ? isl_bool_error : isl_bool_false;
3752 intersect = isl_map_plain_is_equal(map1, map2);
3753 if (intersect < 0 || intersect)
3754 return isl_bool_not(intersect);
3756 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3759 /* Are "bmap1" and "bmap2" disjoint?
3761 * They are disjoint if they are "obviously disjoint" or if one of them
3762 * is empty. Otherwise, they are not disjoint if one of them is universal.
3763 * If none of these cases apply, we compute the intersection and see if
3764 * the result is empty.
3766 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3767 __isl_keep isl_basic_map *bmap2)
3769 isl_bool disjoint;
3770 isl_bool intersect;
3771 isl_basic_map *test;
3773 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3774 if (disjoint < 0 || disjoint)
3775 return disjoint;
3777 disjoint = isl_basic_map_is_empty(bmap1);
3778 if (disjoint < 0 || disjoint)
3779 return disjoint;
3781 disjoint = isl_basic_map_is_empty(bmap2);
3782 if (disjoint < 0 || disjoint)
3783 return disjoint;
3785 intersect = isl_basic_map_plain_is_universe(bmap1);
3786 if (intersect < 0 || intersect)
3787 return intersect < 0 ? isl_bool_error : isl_bool_false;
3789 intersect = isl_basic_map_plain_is_universe(bmap2);
3790 if (intersect < 0 || intersect)
3791 return intersect < 0 ? isl_bool_error : isl_bool_false;
3793 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3794 isl_basic_map_copy(bmap2));
3795 disjoint = isl_basic_map_is_empty(test);
3796 isl_basic_map_free(test);
3798 return disjoint;
3801 /* Are "bset1" and "bset2" disjoint?
3803 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3804 __isl_keep isl_basic_set *bset2)
3806 return isl_basic_map_is_disjoint(bset1, bset2);
3809 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3810 __isl_keep isl_set *set2)
3812 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3815 /* Are "set1" and "set2" disjoint?
3817 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3819 return isl_map_is_disjoint(set1, set2);
3822 /* Is "v" equal to 0, 1 or -1?
3824 static int is_zero_or_one(isl_int v)
3826 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3829 /* Check if we can combine a given div with lower bound l and upper
3830 * bound u with some other div and if so return that other div.
3831 * Otherwise return -1.
3833 * We first check that
3834 * - the bounds are opposites of each other (except for the constant
3835 * term)
3836 * - the bounds do not reference any other div
3837 * - no div is defined in terms of this div
3839 * Let m be the size of the range allowed on the div by the bounds.
3840 * That is, the bounds are of the form
3842 * e <= a <= e + m - 1
3844 * with e some expression in the other variables.
3845 * We look for another div b such that no third div is defined in terms
3846 * of this second div b and such that in any constraint that contains
3847 * a (except for the given lower and upper bound), also contains b
3848 * with a coefficient that is m times that of b.
3849 * That is, all constraints (execpt for the lower and upper bound)
3850 * are of the form
3852 * e + f (a + m b) >= 0
3854 * Furthermore, in the constraints that only contain b, the coefficient
3855 * of b should be equal to 1 or -1.
3856 * If so, we return b so that "a + m b" can be replaced by
3857 * a single div "c = a + m b".
3859 static int div_find_coalesce(struct isl_basic_map *bmap, int *pairs,
3860 unsigned div, unsigned l, unsigned u)
3862 int i, j;
3863 unsigned dim;
3864 int coalesce = -1;
3866 if (bmap->n_div <= 1)
3867 return -1;
3868 dim = isl_space_dim(bmap->dim, isl_dim_all);
3869 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3870 return -1;
3871 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3872 bmap->n_div - div - 1) != -1)
3873 return -1;
3874 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3875 dim + bmap->n_div))
3876 return -1;
3878 for (i = 0; i < bmap->n_div; ++i) {
3879 if (isl_int_is_zero(bmap->div[i][0]))
3880 continue;
3881 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3882 return -1;
3885 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3886 if (isl_int_is_neg(bmap->ineq[l][0])) {
3887 isl_int_sub(bmap->ineq[l][0],
3888 bmap->ineq[l][0], bmap->ineq[u][0]);
3889 bmap = isl_basic_map_copy(bmap);
3890 bmap = isl_basic_map_set_to_empty(bmap);
3891 isl_basic_map_free(bmap);
3892 return -1;
3894 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3895 for (i = 0; i < bmap->n_div; ++i) {
3896 if (i == div)
3897 continue;
3898 if (!pairs[i])
3899 continue;
3900 for (j = 0; j < bmap->n_div; ++j) {
3901 if (isl_int_is_zero(bmap->div[j][0]))
3902 continue;
3903 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3904 break;
3906 if (j < bmap->n_div)
3907 continue;
3908 for (j = 0; j < bmap->n_ineq; ++j) {
3909 int valid;
3910 if (j == l || j == u)
3911 continue;
3912 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3913 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3914 continue;
3915 break;
3917 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3918 break;
3919 isl_int_mul(bmap->ineq[j][1 + dim + div],
3920 bmap->ineq[j][1 + dim + div],
3921 bmap->ineq[l][0]);
3922 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3923 bmap->ineq[j][1 + dim + i]);
3924 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3925 bmap->ineq[j][1 + dim + div],
3926 bmap->ineq[l][0]);
3927 if (!valid)
3928 break;
3930 if (j < bmap->n_ineq)
3931 continue;
3932 coalesce = i;
3933 break;
3935 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3936 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3937 return coalesce;
3940 /* Internal data structure used during the construction and/or evaluation of
3941 * an inequality that ensures that a pair of bounds always allows
3942 * for an integer value.
3944 * "tab" is the tableau in which the inequality is evaluated. It may
3945 * be NULL until it is actually needed.
3946 * "v" contains the inequality coefficients.
3947 * "g", "fl" and "fu" are temporary scalars used during the construction and
3948 * evaluation.
3950 struct test_ineq_data {
3951 struct isl_tab *tab;
3952 isl_vec *v;
3953 isl_int g;
3954 isl_int fl;
3955 isl_int fu;
3958 /* Free all the memory allocated by the fields of "data".
3960 static void test_ineq_data_clear(struct test_ineq_data *data)
3962 isl_tab_free(data->tab);
3963 isl_vec_free(data->v);
3964 isl_int_clear(data->g);
3965 isl_int_clear(data->fl);
3966 isl_int_clear(data->fu);
3969 /* Is the inequality stored in data->v satisfied by "bmap"?
3970 * That is, does it only attain non-negative values?
3971 * data->tab is a tableau corresponding to "bmap".
3973 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
3974 struct test_ineq_data *data)
3976 isl_ctx *ctx;
3977 enum isl_lp_result res;
3979 ctx = isl_basic_map_get_ctx(bmap);
3980 if (!data->tab)
3981 data->tab = isl_tab_from_basic_map(bmap, 0);
3982 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
3983 if (res == isl_lp_error)
3984 return isl_bool_error;
3985 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
3988 /* Given a lower and an upper bound on div i, do they always allow
3989 * for an integer value of the given div?
3990 * Determine this property by constructing an inequality
3991 * such that the property is guaranteed when the inequality is nonnegative.
3992 * The lower bound is inequality l, while the upper bound is inequality u.
3993 * The constructed inequality is stored in data->v.
3995 * Let the upper bound be
3997 * -n_u a + e_u >= 0
3999 * and the lower bound
4001 * n_l a + e_l >= 0
4003 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4004 * We have
4006 * - f_u e_l <= f_u f_l g a <= f_l e_u
4008 * Since all variables are integer valued, this is equivalent to
4010 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4012 * If this interval is at least f_u f_l g, then it contains at least
4013 * one integer value for a.
4014 * That is, the test constraint is
4016 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4018 * or
4020 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4022 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4023 * then the constraint can be scaled down by a factor g',
4024 * with the constant term replaced by
4025 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4026 * Note that the result of applying Fourier-Motzkin to this pair
4027 * of constraints is
4029 * f_l e_u + f_u e_l >= 0
4031 * If the constant term of the scaled down version of this constraint,
4032 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4033 * term of the scaled down test constraint, then the test constraint
4034 * is known to hold and no explicit evaluation is required.
4035 * This is essentially the Omega test.
4037 * If the test constraint consists of only a constant term, then
4038 * it is sufficient to look at the sign of this constant term.
4040 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4041 int l, int u, struct test_ineq_data *data)
4043 unsigned offset, n_div;
4044 offset = isl_basic_map_offset(bmap, isl_dim_div);
4045 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4047 isl_int_gcd(data->g,
4048 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4049 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4050 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4051 isl_int_neg(data->fu, data->fu);
4052 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4053 data->fu, bmap->ineq[l], offset + n_div);
4054 isl_int_mul(data->g, data->g, data->fl);
4055 isl_int_mul(data->g, data->g, data->fu);
4056 isl_int_sub(data->g, data->g, data->fl);
4057 isl_int_sub(data->g, data->g, data->fu);
4058 isl_int_add_ui(data->g, data->g, 1);
4059 isl_int_sub(data->fl, data->v->el[0], data->g);
4061 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4062 if (isl_int_is_zero(data->g))
4063 return isl_int_is_nonneg(data->fl);
4064 if (isl_int_is_one(data->g)) {
4065 isl_int_set(data->v->el[0], data->fl);
4066 return test_ineq_is_satisfied(bmap, data);
4068 isl_int_fdiv_q(data->fl, data->fl, data->g);
4069 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4070 if (isl_int_eq(data->fl, data->v->el[0]))
4071 return isl_bool_true;
4072 isl_int_set(data->v->el[0], data->fl);
4073 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4074 offset - 1 + n_div);
4076 return test_ineq_is_satisfied(bmap, data);
4079 /* Remove more kinds of divs that are not strictly needed.
4080 * In particular, if all pairs of lower and upper bounds on a div
4081 * are such that they allow at least one integer value of the div,
4082 * then we can eliminate the div using Fourier-Motzkin without
4083 * introducing any spurious solutions.
4085 * If at least one of the two constraints has a unit coefficient for the div,
4086 * then the presence of such a value is guaranteed so there is no need to check.
4087 * In particular, the value attained by the bound with unit coefficient
4088 * can serve as this intermediate value.
4090 static struct isl_basic_map *drop_more_redundant_divs(
4091 struct isl_basic_map *bmap, int *pairs, int n)
4093 isl_ctx *ctx;
4094 struct test_ineq_data data = { NULL, NULL };
4095 unsigned off, n_div;
4096 int remove = -1;
4098 isl_int_init(data.g);
4099 isl_int_init(data.fl);
4100 isl_int_init(data.fu);
4102 if (!bmap)
4103 goto error;
4105 ctx = isl_basic_map_get_ctx(bmap);
4106 off = isl_basic_map_offset(bmap, isl_dim_div);
4107 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4108 data.v = isl_vec_alloc(ctx, off + n_div);
4109 if (!data.v)
4110 goto error;
4112 while (n > 0) {
4113 int i, l, u;
4114 int best = -1;
4115 isl_bool has_int;
4117 for (i = 0; i < n_div; ++i) {
4118 if (!pairs[i])
4119 continue;
4120 if (best >= 0 && pairs[best] <= pairs[i])
4121 continue;
4122 best = i;
4125 i = best;
4126 for (l = 0; l < bmap->n_ineq; ++l) {
4127 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4128 continue;
4129 if (isl_int_is_one(bmap->ineq[l][off + i]))
4130 continue;
4131 for (u = 0; u < bmap->n_ineq; ++u) {
4132 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4133 continue;
4134 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4135 continue;
4136 has_int = int_between_bounds(bmap, i, l, u,
4137 &data);
4138 if (has_int < 0)
4139 goto error;
4140 if (data.tab && data.tab->empty)
4141 break;
4142 if (!has_int)
4143 break;
4145 if (u < bmap->n_ineq)
4146 break;
4148 if (data.tab && data.tab->empty) {
4149 bmap = isl_basic_map_set_to_empty(bmap);
4150 break;
4152 if (l == bmap->n_ineq) {
4153 remove = i;
4154 break;
4156 pairs[i] = 0;
4157 --n;
4160 test_ineq_data_clear(&data);
4162 free(pairs);
4164 if (remove < 0)
4165 return bmap;
4167 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4168 return isl_basic_map_drop_redundant_divs(bmap);
4169 error:
4170 free(pairs);
4171 isl_basic_map_free(bmap);
4172 test_ineq_data_clear(&data);
4173 return NULL;
4176 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4177 * and the upper bound u, div1 always occurs together with div2 in the form
4178 * (div1 + m div2), where m is the constant range on the variable div1
4179 * allowed by l and u, replace the pair div1 and div2 by a single
4180 * div that is equal to div1 + m div2.
4182 * The new div will appear in the location that contains div2.
4183 * We need to modify all constraints that contain
4184 * div2 = (div - div1) / m
4185 * The coefficient of div2 is known to be equal to 1 or -1.
4186 * (If a constraint does not contain div2, it will also not contain div1.)
4187 * If the constraint also contains div1, then we know they appear
4188 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4189 * i.e., the coefficient of div is f.
4191 * Otherwise, we first need to introduce div1 into the constraint.
4192 * Let the l be
4194 * div1 + f >=0
4196 * and u
4198 * -div1 + f' >= 0
4200 * A lower bound on div2
4202 * div2 + t >= 0
4204 * can be replaced by
4206 * m div2 + div1 + m t + f >= 0
4208 * An upper bound
4210 * -div2 + t >= 0
4212 * can be replaced by
4214 * -(m div2 + div1) + m t + f' >= 0
4216 * These constraint are those that we would obtain from eliminating
4217 * div1 using Fourier-Motzkin.
4219 * After all constraints have been modified, we drop the lower and upper
4220 * bound and then drop div1.
4221 * Since the new div is only placed in the same location that used
4222 * to store div2, but otherwise has a different meaning, any possible
4223 * explicit representation of the original div2 is removed.
4225 static struct isl_basic_map *coalesce_divs(struct isl_basic_map *bmap,
4226 unsigned div1, unsigned div2, unsigned l, unsigned u)
4228 isl_ctx *ctx;
4229 isl_int m;
4230 unsigned dim, total;
4231 int i;
4233 ctx = isl_basic_map_get_ctx(bmap);
4235 dim = isl_space_dim(bmap->dim, isl_dim_all);
4236 total = 1 + dim + bmap->n_div;
4238 isl_int_init(m);
4239 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4240 isl_int_add_ui(m, m, 1);
4242 for (i = 0; i < bmap->n_ineq; ++i) {
4243 if (i == l || i == u)
4244 continue;
4245 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4246 continue;
4247 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4248 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4249 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4250 ctx->one, bmap->ineq[l], total);
4251 else
4252 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4253 ctx->one, bmap->ineq[u], total);
4255 isl_int_set(bmap->ineq[i][1 + dim + div2],
4256 bmap->ineq[i][1 + dim + div1]);
4257 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4260 isl_int_clear(m);
4261 if (l > u) {
4262 isl_basic_map_drop_inequality(bmap, l);
4263 isl_basic_map_drop_inequality(bmap, u);
4264 } else {
4265 isl_basic_map_drop_inequality(bmap, u);
4266 isl_basic_map_drop_inequality(bmap, l);
4268 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4269 bmap = isl_basic_map_drop_div(bmap, div1);
4270 return bmap;
4273 /* First check if we can coalesce any pair of divs and
4274 * then continue with dropping more redundant divs.
4276 * We loop over all pairs of lower and upper bounds on a div
4277 * with coefficient 1 and -1, respectively, check if there
4278 * is any other div "c" with which we can coalesce the div
4279 * and if so, perform the coalescing.
4281 static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
4282 struct isl_basic_map *bmap, int *pairs, int n)
4284 int i, l, u;
4285 unsigned dim;
4287 dim = isl_space_dim(bmap->dim, isl_dim_all);
4289 for (i = 0; i < bmap->n_div; ++i) {
4290 if (!pairs[i])
4291 continue;
4292 for (l = 0; l < bmap->n_ineq; ++l) {
4293 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4294 continue;
4295 for (u = 0; u < bmap->n_ineq; ++u) {
4296 int c;
4298 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4299 continue;
4300 c = div_find_coalesce(bmap, pairs, i, l, u);
4301 if (c < 0)
4302 continue;
4303 free(pairs);
4304 bmap = coalesce_divs(bmap, i, c, l, u);
4305 return isl_basic_map_drop_redundant_divs(bmap);
4310 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4311 free(pairs);
4312 return bmap;
4315 return drop_more_redundant_divs(bmap, pairs, n);
4318 /* Are the "n" coefficients starting at "first" of inequality constraints
4319 * "i" and "j" of "bmap" equal to each other?
4321 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4322 int first, int n)
4324 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4327 /* Are the "n" coefficients starting at "first" of inequality constraints
4328 * "i" and "j" of "bmap" opposite to each other?
4330 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4331 int first, int n)
4333 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4336 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4337 * apart from the constant term?
4339 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4341 unsigned total;
4343 total = isl_basic_map_dim(bmap, isl_dim_all);
4344 return is_opposite_part(bmap, i, j, 1, total);
4347 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4348 * apart from the constant term and the coefficient at position "pos"?
4350 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4351 int pos)
4353 unsigned total;
4355 total = isl_basic_map_dim(bmap, isl_dim_all);
4356 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4357 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4360 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4361 * apart from the constant term and the coefficient at position "pos"?
4363 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4364 int pos)
4366 unsigned total;
4368 total = isl_basic_map_dim(bmap, isl_dim_all);
4369 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4370 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4373 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4374 * been modified, simplying it if "simplify" is set.
4375 * Free the temporary data structure "pairs" that was associated
4376 * to the old version of "bmap".
4378 static __isl_give isl_basic_map *drop_redundant_divs_again(
4379 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4381 if (simplify)
4382 bmap = isl_basic_map_simplify(bmap);
4383 free(pairs);
4384 return isl_basic_map_drop_redundant_divs(bmap);
4387 /* Is "div" the single unknown existentially quantified variable
4388 * in inequality constraint "ineq" of "bmap"?
4389 * "div" is known to have a non-zero coefficient in "ineq".
4391 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4392 int div)
4394 int i;
4395 unsigned n_div, o_div;
4396 isl_bool known;
4398 known = isl_basic_map_div_is_known(bmap, div);
4399 if (known < 0 || known)
4400 return isl_bool_not(known);
4401 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4402 if (n_div == 1)
4403 return isl_bool_true;
4404 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4405 for (i = 0; i < n_div; ++i) {
4406 isl_bool known;
4408 if (i == div)
4409 continue;
4410 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4411 continue;
4412 known = isl_basic_map_div_is_known(bmap, i);
4413 if (known < 0 || !known)
4414 return known;
4417 return isl_bool_true;
4420 /* Does integer division "div" have coefficient 1 in inequality constraint
4421 * "ineq" of "map"?
4423 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4425 unsigned o_div;
4427 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4428 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4429 return isl_bool_true;
4431 return isl_bool_false;
4434 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4435 * then try and drop redundant divs again,
4436 * freeing the temporary data structure "pairs" that was associated
4437 * to the old version of "bmap".
4439 static __isl_give isl_basic_map *set_eq_and_try_again(
4440 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4442 bmap = isl_basic_map_cow(bmap);
4443 isl_basic_map_inequality_to_equality(bmap, ineq);
4444 return drop_redundant_divs_again(bmap, pairs, 1);
4447 /* Drop the integer division at position "div", along with the two
4448 * inequality constraints "ineq1" and "ineq2" in which it appears
4449 * from "bmap" and then try and drop redundant divs again,
4450 * freeing the temporary data structure "pairs" that was associated
4451 * to the old version of "bmap".
4453 static __isl_give isl_basic_map *drop_div_and_try_again(
4454 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4455 __isl_take int *pairs)
4457 if (ineq1 > ineq2) {
4458 isl_basic_map_drop_inequality(bmap, ineq1);
4459 isl_basic_map_drop_inequality(bmap, ineq2);
4460 } else {
4461 isl_basic_map_drop_inequality(bmap, ineq2);
4462 isl_basic_map_drop_inequality(bmap, ineq1);
4464 bmap = isl_basic_map_drop_div(bmap, div);
4465 return drop_redundant_divs_again(bmap, pairs, 0);
4468 /* Given two inequality constraints
4470 * f(x) + n d + c >= 0, (ineq)
4472 * with d the variable at position "pos", and
4474 * f(x) + c0 >= 0, (lower)
4476 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4477 * determined by the first constraint.
4478 * That is, store
4480 * ceil((c0 - c)/n)
4482 * in *l.
4484 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4485 int ineq, int lower, int pos, isl_int *l)
4487 isl_int_neg(*l, bmap->ineq[ineq][0]);
4488 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4489 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4492 /* Given two inequality constraints
4494 * f(x) + n d + c >= 0, (ineq)
4496 * with d the variable at position "pos", and
4498 * -f(x) - c0 >= 0, (upper)
4500 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4501 * determined by the first constraint.
4502 * That is, store
4504 * ceil((-c1 - c)/n)
4506 * in *u.
4508 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4509 int ineq, int upper, int pos, isl_int *u)
4511 isl_int_neg(*u, bmap->ineq[ineq][0]);
4512 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4513 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4516 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4517 * does the corresponding lower bound have a fixed value in "bmap"?
4519 * In particular, "ineq" is of the form
4521 * f(x) + n d + c >= 0
4523 * with n > 0, c the constant term and
4524 * d the existentially quantified variable "div".
4525 * That is, the lower bound is
4527 * ceil((-f(x) - c)/n)
4529 * Look for a pair of constraints
4531 * f(x) + c0 >= 0
4532 * -f(x) + c1 >= 0
4534 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4535 * That is, check that
4537 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4539 * If so, return the index of inequality f(x) + c0 >= 0.
4540 * Otherwise, return -1.
4542 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4544 int i;
4545 int lower = -1, upper = -1;
4546 unsigned o_div;
4547 isl_int l, u;
4548 int equal;
4550 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4551 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4552 if (i == ineq)
4553 continue;
4554 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4555 continue;
4556 if (lower < 0 &&
4557 is_parallel_except(bmap, ineq, i, o_div + div)) {
4558 lower = i;
4559 continue;
4561 if (upper < 0 &&
4562 is_opposite_except(bmap, ineq, i, o_div + div)) {
4563 upper = i;
4567 if (lower < 0 || upper < 0)
4568 return -1;
4570 isl_int_init(l);
4571 isl_int_init(u);
4573 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4574 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4576 equal = isl_int_eq(l, u);
4578 isl_int_clear(l);
4579 isl_int_clear(u);
4581 return equal ? lower : -1;
4584 /* Given a lower bound constraint "ineq" on the existentially quantified
4585 * variable "div", such that the corresponding lower bound has
4586 * a fixed value in "bmap", assign this fixed value to the variable and
4587 * then try and drop redundant divs again,
4588 * freeing the temporary data structure "pairs" that was associated
4589 * to the old version of "bmap".
4590 * "lower" determines the constant value for the lower bound.
4592 * In particular, "ineq" is of the form
4594 * f(x) + n d + c >= 0,
4596 * while "lower" is of the form
4598 * f(x) + c0 >= 0
4600 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4601 * is ceil((c0 - c)/n).
4603 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4604 int div, int ineq, int lower, int *pairs)
4606 isl_int c;
4607 unsigned o_div;
4609 isl_int_init(c);
4611 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4612 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4613 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4614 free(pairs);
4616 isl_int_clear(c);
4618 return isl_basic_map_drop_redundant_divs(bmap);
4621 /* Remove divs that are not strictly needed based on the inequality
4622 * constraints.
4623 * In particular, if a div only occurs positively (or negatively)
4624 * in constraints, then it can simply be dropped.
4625 * Also, if a div occurs in only two constraints and if moreover
4626 * those two constraints are opposite to each other, except for the constant
4627 * term and if the sum of the constant terms is such that for any value
4628 * of the other values, there is always at least one integer value of the
4629 * div, i.e., if one plus this sum is greater than or equal to
4630 * the (absolute value) of the coefficient of the div in the constraints,
4631 * then we can also simply drop the div.
4633 * If an existentially quantified variable does not have an explicit
4634 * representation, appears in only a single lower bound that does not
4635 * involve any other such existentially quantified variables and appears
4636 * in this lower bound with coefficient 1,
4637 * then fix the variable to the value of the lower bound. That is,
4638 * turn the inequality into an equality.
4639 * If for any value of the other variables, there is any value
4640 * for the existentially quantified variable satisfying the constraints,
4641 * then this lower bound also satisfies the constraints.
4642 * It is therefore safe to pick this lower bound.
4644 * The same reasoning holds even if the coefficient is not one.
4645 * However, fixing the variable to the value of the lower bound may
4646 * in general introduce an extra integer division, in which case
4647 * it may be better to pick another value.
4648 * If this integer division has a known constant value, then plugging
4649 * in this constant value removes the existentially quantified variable
4650 * completely. In particular, if the lower bound is of the form
4651 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4652 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4653 * then the existentially quantified variable can be assigned this
4654 * shared value.
4656 * We skip divs that appear in equalities or in the definition of other divs.
4657 * Divs that appear in the definition of other divs usually occur in at least
4658 * 4 constraints, but the constraints may have been simplified.
4660 * If any divs are left after these simple checks then we move on
4661 * to more complicated cases in drop_more_redundant_divs.
4663 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4664 __isl_take isl_basic_map *bmap)
4666 int i, j;
4667 unsigned off;
4668 int *pairs = NULL;
4669 int n = 0;
4671 if (!bmap)
4672 goto error;
4673 if (bmap->n_div == 0)
4674 return bmap;
4676 off = isl_space_dim(bmap->dim, isl_dim_all);
4677 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4678 if (!pairs)
4679 goto error;
4681 for (i = 0; i < bmap->n_div; ++i) {
4682 int pos, neg;
4683 int last_pos, last_neg;
4684 int redundant;
4685 int defined;
4686 isl_bool opp, set_div;
4688 defined = !isl_int_is_zero(bmap->div[i][0]);
4689 for (j = i; j < bmap->n_div; ++j)
4690 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4691 break;
4692 if (j < bmap->n_div)
4693 continue;
4694 for (j = 0; j < bmap->n_eq; ++j)
4695 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4696 break;
4697 if (j < bmap->n_eq)
4698 continue;
4699 ++n;
4700 pos = neg = 0;
4701 for (j = 0; j < bmap->n_ineq; ++j) {
4702 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4703 last_pos = j;
4704 ++pos;
4706 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4707 last_neg = j;
4708 ++neg;
4711 pairs[i] = pos * neg;
4712 if (pairs[i] == 0) {
4713 for (j = bmap->n_ineq - 1; j >= 0; --j)
4714 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4715 isl_basic_map_drop_inequality(bmap, j);
4716 bmap = isl_basic_map_drop_div(bmap, i);
4717 return drop_redundant_divs_again(bmap, pairs, 0);
4719 if (pairs[i] != 1)
4720 opp = isl_bool_false;
4721 else
4722 opp = is_opposite(bmap, last_pos, last_neg);
4723 if (opp < 0)
4724 goto error;
4725 if (!opp) {
4726 int lower;
4727 isl_bool single, one;
4729 if (pos != 1)
4730 continue;
4731 single = single_unknown(bmap, last_pos, i);
4732 if (single < 0)
4733 goto error;
4734 if (!single)
4735 continue;
4736 one = has_coef_one(bmap, i, last_pos);
4737 if (one < 0)
4738 goto error;
4739 if (one)
4740 return set_eq_and_try_again(bmap, last_pos,
4741 pairs);
4742 lower = lower_bound_is_cst(bmap, i, last_pos);
4743 if (lower >= 0)
4744 return fix_cst_lower(bmap, i, last_pos, lower,
4745 pairs);
4746 continue;
4749 isl_int_add(bmap->ineq[last_pos][0],
4750 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4751 isl_int_add_ui(bmap->ineq[last_pos][0],
4752 bmap->ineq[last_pos][0], 1);
4753 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4754 bmap->ineq[last_pos][1+off+i]);
4755 isl_int_sub_ui(bmap->ineq[last_pos][0],
4756 bmap->ineq[last_pos][0], 1);
4757 isl_int_sub(bmap->ineq[last_pos][0],
4758 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4759 if (redundant)
4760 return drop_div_and_try_again(bmap, i,
4761 last_pos, last_neg, pairs);
4762 if (defined)
4763 set_div = isl_bool_false;
4764 else
4765 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4766 if (set_div < 0)
4767 return isl_basic_map_free(bmap);
4768 if (set_div) {
4769 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4770 return drop_redundant_divs_again(bmap, pairs, 1);
4772 pairs[i] = 0;
4773 --n;
4776 if (n > 0)
4777 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4779 free(pairs);
4780 return bmap;
4781 error:
4782 free(pairs);
4783 isl_basic_map_free(bmap);
4784 return NULL;
4787 /* Consider the coefficients at "c" as a row vector and replace
4788 * them with their product with "T". "T" is assumed to be a square matrix.
4790 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4792 int n;
4793 isl_ctx *ctx;
4794 isl_vec *v;
4796 if (!T)
4797 return isl_stat_error;
4798 n = isl_mat_rows(T);
4799 if (isl_seq_first_non_zero(c, n) == -1)
4800 return isl_stat_ok;
4801 ctx = isl_mat_get_ctx(T);
4802 v = isl_vec_alloc(ctx, n);
4803 if (!v)
4804 return isl_stat_error;
4805 isl_seq_swp_or_cpy(v->el, c, n);
4806 v = isl_vec_mat_product(v, isl_mat_copy(T));
4807 if (!v)
4808 return isl_stat_error;
4809 isl_seq_swp_or_cpy(c, v->el, n);
4810 isl_vec_free(v);
4812 return isl_stat_ok;
4815 /* Plug in T for the variables in "bmap" starting at "pos".
4816 * T is a linear unimodular matrix, i.e., without constant term.
4818 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4819 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4821 int i;
4822 unsigned n, total;
4824 bmap = isl_basic_map_cow(bmap);
4825 if (!bmap || !T)
4826 goto error;
4828 n = isl_mat_cols(T);
4829 if (n != isl_mat_rows(T))
4830 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4831 "expecting square matrix", goto error);
4833 total = isl_basic_map_dim(bmap, isl_dim_all);
4834 if (pos + n > total || pos + n < pos)
4835 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4836 "invalid range", goto error);
4838 for (i = 0; i < bmap->n_eq; ++i)
4839 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4840 goto error;
4841 for (i = 0; i < bmap->n_ineq; ++i)
4842 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4843 goto error;
4844 for (i = 0; i < bmap->n_div; ++i) {
4845 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4846 continue;
4847 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4848 goto error;
4851 isl_mat_free(T);
4852 return bmap;
4853 error:
4854 isl_basic_map_free(bmap);
4855 isl_mat_free(T);
4856 return NULL;
4859 /* Remove divs that are not strictly needed.
4861 * First look for an equality constraint involving two or more
4862 * existentially quantified variables without an explicit
4863 * representation. Replace the combination that appears
4864 * in the equality constraint by a single existentially quantified
4865 * variable such that the equality can be used to derive
4866 * an explicit representation for the variable.
4867 * If there are no more such equality constraints, then continue
4868 * with isl_basic_map_drop_redundant_divs_ineq.
4870 * In particular, if the equality constraint is of the form
4872 * f(x) + \sum_i c_i a_i = 0
4874 * with a_i existentially quantified variable without explicit
4875 * representation, then apply a transformation on the existentially
4876 * quantified variables to turn the constraint into
4878 * f(x) + g a_1' = 0
4880 * with g the gcd of the c_i.
4881 * In order to easily identify which existentially quantified variables
4882 * have a complete explicit representation, i.e., without being defined
4883 * in terms of other existentially quantified variables without
4884 * an explicit representation, the existentially quantified variables
4885 * are first sorted.
4887 * The variable transformation is computed by extending the row
4888 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4890 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4891 * [a_2'] [ a_2 ]
4892 * ... = U ....
4893 * [a_n'] [ a_n ]
4895 * with [c_1/g ... c_n/g] representing the first row of U.
4896 * The inverse of U is then plugged into the original constraints.
4897 * The call to isl_basic_map_simplify makes sure the explicit
4898 * representation for a_1' is extracted from the equality constraint.
4900 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4901 __isl_take isl_basic_map *bmap)
4903 int first;
4904 int i;
4905 unsigned o_div, n_div;
4906 int l;
4907 isl_ctx *ctx;
4908 isl_mat *T;
4910 if (!bmap)
4911 return NULL;
4912 if (isl_basic_map_divs_known(bmap))
4913 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4914 if (bmap->n_eq == 0)
4915 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4916 bmap = isl_basic_map_sort_divs(bmap);
4917 if (!bmap)
4918 return NULL;
4920 first = isl_basic_map_first_unknown_div(bmap);
4921 if (first < 0)
4922 return isl_basic_map_free(bmap);
4924 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4925 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4927 for (i = 0; i < bmap->n_eq; ++i) {
4928 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4929 n_div - (first));
4930 if (l < 0)
4931 continue;
4932 l += first;
4933 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4934 n_div - (l + 1)) == -1)
4935 continue;
4936 break;
4938 if (i >= bmap->n_eq)
4939 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4941 ctx = isl_basic_map_get_ctx(bmap);
4942 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
4943 if (!T)
4944 return isl_basic_map_free(bmap);
4945 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
4946 T = isl_mat_normalize_row(T, 0);
4947 T = isl_mat_unimodular_complete(T, 1);
4948 T = isl_mat_right_inverse(T);
4950 for (i = l; i < n_div; ++i)
4951 bmap = isl_basic_map_mark_div_unknown(bmap, i);
4952 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
4953 bmap = isl_basic_map_simplify(bmap);
4955 return isl_basic_map_drop_redundant_divs(bmap);
4958 /* Does "bmap" satisfy any equality that involves more than 2 variables
4959 * and/or has coefficients different from -1 and 1?
4961 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
4963 int i;
4964 unsigned total;
4966 total = isl_basic_map_dim(bmap, isl_dim_all);
4968 for (i = 0; i < bmap->n_eq; ++i) {
4969 int j, k;
4971 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
4972 if (j < 0)
4973 continue;
4974 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4975 !isl_int_is_negone(bmap->eq[i][1 + j]))
4976 return 1;
4978 j += 1;
4979 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4980 if (k < 0)
4981 continue;
4982 j += k;
4983 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
4984 !isl_int_is_negone(bmap->eq[i][1 + j]))
4985 return 1;
4987 j += 1;
4988 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
4989 if (k >= 0)
4990 return 1;
4993 return 0;
4996 /* Remove any common factor g from the constraint coefficients in "v".
4997 * The constant term is stored in the first position and is replaced
4998 * by floor(c/g). If any common factor is removed and if this results
4999 * in a tightening of the constraint, then set *tightened.
5001 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5002 int *tightened)
5004 isl_ctx *ctx;
5006 if (!v)
5007 return NULL;
5008 ctx = isl_vec_get_ctx(v);
5009 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5010 if (isl_int_is_zero(ctx->normalize_gcd))
5011 return v;
5012 if (isl_int_is_one(ctx->normalize_gcd))
5013 return v;
5014 v = isl_vec_cow(v);
5015 if (!v)
5016 return NULL;
5017 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5018 *tightened = 1;
5019 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5020 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5021 v->size - 1);
5022 return v;
5025 /* If "bmap" is an integer set that satisfies any equality involving
5026 * more than 2 variables and/or has coefficients different from -1 and 1,
5027 * then use variable compression to reduce the coefficients by removing
5028 * any (hidden) common factor.
5029 * In particular, apply the variable compression to each constraint,
5030 * factor out any common factor in the non-constant coefficients and
5031 * then apply the inverse of the compression.
5032 * At the end, we mark the basic map as having reduced constants.
5033 * If this flag is still set on the next invocation of this function,
5034 * then we skip the computation.
5036 * Removing a common factor may result in a tightening of some of
5037 * the constraints. If this happens, then we may end up with two
5038 * opposite inequalities that can be replaced by an equality.
5039 * We therefore call isl_basic_map_detect_inequality_pairs,
5040 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5041 * and isl_basic_map_gauss if such a pair was found.
5043 * Note that this function may leave the result in an inconsistent state.
5044 * In particular, the constraints may not be gaussed.
5045 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5046 * for some of the test cases to pass successfully.
5047 * Any potential modification of the representation is therefore only
5048 * performed on a single copy of the basic map.
5050 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5051 __isl_take isl_basic_map *bmap)
5053 unsigned total;
5054 isl_ctx *ctx;
5055 isl_vec *v;
5056 isl_mat *eq, *T, *T2;
5057 int i;
5058 int tightened;
5060 if (!bmap)
5061 return NULL;
5062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5063 return bmap;
5064 if (isl_basic_map_is_rational(bmap))
5065 return bmap;
5066 if (bmap->n_eq == 0)
5067 return bmap;
5068 if (!has_multiple_var_equality(bmap))
5069 return bmap;
5071 total = isl_basic_map_dim(bmap, isl_dim_all);
5072 ctx = isl_basic_map_get_ctx(bmap);
5073 v = isl_vec_alloc(ctx, 1 + total);
5074 if (!v)
5075 return isl_basic_map_free(bmap);
5077 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5078 T = isl_mat_variable_compression(eq, &T2);
5079 if (!T || !T2)
5080 goto error;
5081 if (T->n_col == 0) {
5082 isl_mat_free(T);
5083 isl_mat_free(T2);
5084 isl_vec_free(v);
5085 return isl_basic_map_set_to_empty(bmap);
5088 bmap = isl_basic_map_cow(bmap);
5089 if (!bmap)
5090 goto error;
5092 tightened = 0;
5093 for (i = 0; i < bmap->n_ineq; ++i) {
5094 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5095 v = isl_vec_mat_product(v, isl_mat_copy(T));
5096 v = normalize_constraint(v, &tightened);
5097 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5098 if (!v)
5099 goto error;
5100 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5103 isl_mat_free(T);
5104 isl_mat_free(T2);
5105 isl_vec_free(v);
5107 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5109 if (tightened) {
5110 int progress = 0;
5112 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5113 if (progress) {
5114 bmap = eliminate_divs_eq(bmap, &progress);
5115 bmap = isl_basic_map_gauss(bmap, NULL);
5119 return bmap;
5120 error:
5121 isl_mat_free(T);
5122 isl_mat_free(T2);
5123 isl_vec_free(v);
5124 return isl_basic_map_free(bmap);
5127 /* Shift the integer division at position "div" of "bmap"
5128 * by "shift" times the variable at position "pos".
5129 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5130 * corresponds to the constant term.
5132 * That is, if the integer division has the form
5134 * floor(f(x)/d)
5136 * then replace it by
5138 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5140 __isl_give isl_basic_map *isl_basic_map_shift_div(
5141 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5143 int i;
5144 unsigned total;
5146 if (isl_int_is_zero(shift))
5147 return bmap;
5148 if (!bmap)
5149 return NULL;
5151 total = isl_basic_map_dim(bmap, isl_dim_all);
5152 total -= isl_basic_map_dim(bmap, isl_dim_div);
5154 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5156 for (i = 0; i < bmap->n_eq; ++i) {
5157 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5158 continue;
5159 isl_int_submul(bmap->eq[i][pos],
5160 shift, bmap->eq[i][1 + total + div]);
5162 for (i = 0; i < bmap->n_ineq; ++i) {
5163 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5164 continue;
5165 isl_int_submul(bmap->ineq[i][pos],
5166 shift, bmap->ineq[i][1 + total + div]);
5168 for (i = 0; i < bmap->n_div; ++i) {
5169 if (isl_int_is_zero(bmap->div[i][0]))
5170 continue;
5171 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5172 continue;
5173 isl_int_submul(bmap->div[i][1 + pos],
5174 shift, bmap->div[i][1 + 1 + total + div]);
5177 return bmap;