generator: extract out is_isl_neg_error
[isl.git] / isl_map_simplify.c
blobd5709c07ea862a062b00d18e0ea6ad6b3016dc29
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
50 int i;
51 isl_int gcd;
52 unsigned total = isl_basic_map_total_dim(bmap);
54 if (!bmap)
55 return NULL;
57 isl_int_init(gcd);
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
65 isl_basic_map_drop_equality(bmap, i);
66 continue;
68 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
69 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70 if (isl_int_is_one(gcd))
71 continue;
72 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
73 bmap = isl_basic_map_set_to_empty(bmap);
74 break;
76 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
79 for (i = bmap->n_ineq - 1; i >= 0; --i) {
80 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81 if (isl_int_is_zero(gcd)) {
82 if (isl_int_is_neg(bmap->ineq[i][0])) {
83 bmap = isl_basic_map_set_to_empty(bmap);
84 break;
86 isl_basic_map_drop_inequality(bmap, i);
87 continue;
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
90 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91 if (isl_int_is_one(gcd))
92 continue;
93 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
94 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
96 isl_int_clear(gcd);
98 return bmap;
101 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set *bset)
104 isl_basic_map *bmap = bset_to_bmap(bset);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
119 * Replace it by
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map *reduce_coefficient_in_div(
129 __isl_take isl_basic_map *bmap, int div, int pos)
131 isl_int shift;
132 int add_one;
134 isl_int_init(shift);
135 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136 isl_int_mul_ui(shift, shift, 2);
137 add_one = isl_int_gt(shift, bmap->div[div][0]);
138 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139 if (add_one)
140 isl_int_add_ui(shift, shift, 1);
141 isl_int_neg(shift, shift);
142 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143 isl_int_clear(shift);
145 return bmap;
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
152 * 2 * c != d.
154 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
155 int pos)
157 isl_bool r;
159 if (isl_int_is_zero(bmap->div[div][1 + pos]))
160 return isl_bool_false;
162 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
163 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
165 isl_int_divexact_ui(bmap->div[div][1 + pos],
166 bmap->div[div][1 + pos], 2);
168 return r;
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map *bmap, int div)
179 int i;
180 unsigned total = 1 + isl_basic_map_total_dim(bmap);
182 for (i = 0; i < total; ++i) {
183 isl_bool reduce;
185 reduce = needs_reduction(bmap, div, i);
186 if (reduce < 0)
187 return isl_basic_map_free(bmap);
188 if (!reduce)
189 continue;
190 bmap = reduce_coefficient_in_div(bmap, div, i);
191 if (!bmap)
192 break;
195 return bmap;
198 /* Reduce the coefficients (including the constant term) of
199 * the known integer divisions, if needed
200 * In particular, make sure all coefficients lie in
201 * the half-open interval (1/2,1/2].
203 static __isl_give isl_basic_map *reduce_div_coefficients(
204 __isl_take isl_basic_map *bmap)
206 int i;
208 if (!bmap)
209 return NULL;
210 if (bmap->n_div == 0)
211 return bmap;
213 for (i = 0; i < bmap->n_div; ++i) {
214 if (isl_int_is_zero(bmap->div[i][0]))
215 continue;
216 bmap = reduce_div_coefficients_of_div(bmap, i);
217 if (!bmap)
218 break;
221 return bmap;
224 /* Remove any common factor in numerator and denominator of the div expression,
225 * not taking into account the constant term.
226 * That is, if the div is of the form
228 * floor((a + m f(x))/(m d))
230 * then replace it by
232 * floor((floor(a/m) + f(x))/d)
234 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
235 * and can therefore not influence the result of the floor.
237 static __isl_give isl_basic_map *normalize_div_expression(
238 __isl_take isl_basic_map *bmap, int div)
240 unsigned total = isl_basic_map_total_dim(bmap);
241 isl_ctx *ctx = bmap->ctx;
243 if (isl_int_is_zero(bmap->div[div][0]))
244 return bmap;
245 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
246 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
247 if (isl_int_is_one(ctx->normalize_gcd))
248 return bmap;
249 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
250 ctx->normalize_gcd);
251 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
252 ctx->normalize_gcd);
253 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
254 ctx->normalize_gcd, total);
256 return bmap;
259 /* Remove any common factor in numerator and denominator of a div expression,
260 * not taking into account the constant term.
261 * That is, look for any div of the form
263 * floor((a + m f(x))/(m d))
265 * and replace it by
267 * floor((floor(a/m) + f(x))/d)
269 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
270 * and can therefore not influence the result of the floor.
272 static __isl_give isl_basic_map *normalize_div_expressions(
273 __isl_take isl_basic_map *bmap)
275 int i;
277 if (!bmap)
278 return NULL;
279 if (bmap->n_div == 0)
280 return bmap;
282 for (i = 0; i < bmap->n_div; ++i)
283 bmap = normalize_div_expression(bmap, i);
285 return bmap;
288 /* Assumes divs have been ordered if keep_divs is set.
290 static __isl_give isl_basic_map *eliminate_var_using_equality(
291 __isl_take isl_basic_map *bmap,
292 unsigned pos, isl_int *eq, int keep_divs, int *progress)
294 unsigned total;
295 int v_div;
296 int k;
297 int last_div;
299 total = isl_basic_map_total_dim(bmap);
300 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
301 if (v_div < 0)
302 return isl_basic_map_free(bmap);
303 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
304 for (k = 0; k < bmap->n_eq; ++k) {
305 if (bmap->eq[k] == eq)
306 continue;
307 if (isl_int_is_zero(bmap->eq[k][1+pos]))
308 continue;
309 if (progress)
310 *progress = 1;
311 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
312 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
315 for (k = 0; k < bmap->n_ineq; ++k) {
316 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
317 continue;
318 if (progress)
319 *progress = 1;
320 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
321 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
322 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
323 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
326 for (k = 0; k < bmap->n_div; ++k) {
327 if (isl_int_is_zero(bmap->div[k][0]))
328 continue;
329 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
330 continue;
331 if (progress)
332 *progress = 1;
333 /* We need to be careful about circular definitions,
334 * so for now we just remove the definition of div k
335 * if the equality contains any divs.
336 * If keep_divs is set, then the divs have been ordered
337 * and we can keep the definition as long as the result
338 * is still ordered.
340 if (last_div == -1 || (keep_divs && last_div < k)) {
341 isl_seq_elim(bmap->div[k]+1, eq,
342 1+pos, 1+total, &bmap->div[k][0]);
343 bmap = normalize_div_expression(bmap, k);
344 if (!bmap)
345 return NULL;
346 } else
347 isl_seq_clr(bmap->div[k], 1 + total);
350 return bmap;
353 /* Assumes divs have been ordered if keep_divs is set.
355 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
356 isl_int *eq, unsigned div, int keep_divs)
358 int v_div;
359 unsigned pos;
361 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
362 if (v_div < 0)
363 return isl_basic_map_free(bmap);
364 pos = v_div + div;
365 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
367 bmap = isl_basic_map_drop_div(bmap, div);
369 return bmap;
372 /* Check if elimination of div "div" using equality "eq" would not
373 * result in a div depending on a later div.
375 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
376 unsigned div)
378 int k;
379 int last_div;
380 int v_div;
381 unsigned pos;
383 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
384 if (v_div < 0)
385 return isl_bool_error;
386 pos = v_div + div;
388 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
389 if (last_div < 0 || last_div <= div)
390 return isl_bool_true;
392 for (k = 0; k <= last_div; ++k) {
393 if (isl_int_is_zero(bmap->div[k][0]))
394 continue;
395 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
396 return isl_bool_false;
399 return isl_bool_true;
402 /* Eliminate divs based on equalities
404 static __isl_give isl_basic_map *eliminate_divs_eq(
405 __isl_take isl_basic_map *bmap, int *progress)
407 int d;
408 int i;
409 int modified = 0;
410 unsigned off;
412 bmap = isl_basic_map_order_divs(bmap);
414 if (!bmap)
415 return NULL;
417 off = isl_basic_map_offset(bmap, isl_dim_div);
419 for (d = bmap->n_div - 1; d >= 0 ; --d) {
420 for (i = 0; i < bmap->n_eq; ++i) {
421 isl_bool ok;
423 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
424 !isl_int_is_negone(bmap->eq[i][off + d]))
425 continue;
426 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
427 if (ok < 0)
428 return isl_basic_map_free(bmap);
429 if (!ok)
430 continue;
431 modified = 1;
432 *progress = 1;
433 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
434 if (isl_basic_map_drop_equality(bmap, i) < 0)
435 return isl_basic_map_free(bmap);
436 break;
439 if (modified)
440 return eliminate_divs_eq(bmap, progress);
441 return bmap;
444 /* Eliminate divs based on inequalities
446 static __isl_give isl_basic_map *eliminate_divs_ineq(
447 __isl_take isl_basic_map *bmap, int *progress)
449 int d;
450 int i;
451 unsigned off;
452 struct isl_ctx *ctx;
454 if (!bmap)
455 return NULL;
457 ctx = bmap->ctx;
458 off = isl_basic_map_offset(bmap, isl_dim_div);
460 for (d = bmap->n_div - 1; d >= 0 ; --d) {
461 for (i = 0; i < bmap->n_eq; ++i)
462 if (!isl_int_is_zero(bmap->eq[i][off + d]))
463 break;
464 if (i < bmap->n_eq)
465 continue;
466 for (i = 0; i < bmap->n_ineq; ++i)
467 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
468 break;
469 if (i < bmap->n_ineq)
470 continue;
471 *progress = 1;
472 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
473 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
474 break;
475 bmap = isl_basic_map_drop_div(bmap, d);
476 if (!bmap)
477 break;
479 return bmap;
482 /* Does the equality constraint at position "eq" in "bmap" involve
483 * any local variables in the range [first, first + n)
484 * that are not marked as having an explicit representation?
486 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
487 int eq, unsigned first, unsigned n)
489 unsigned o_div;
490 int i;
492 if (!bmap)
493 return isl_bool_error;
495 o_div = isl_basic_map_offset(bmap, isl_dim_div);
496 for (i = 0; i < n; ++i) {
497 isl_bool unknown;
499 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
500 continue;
501 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
502 if (unknown < 0)
503 return isl_bool_error;
504 if (unknown)
505 return isl_bool_true;
508 return isl_bool_false;
511 /* The last local variable involved in the equality constraint
512 * at position "eq" in "bmap" is the local variable at position "div".
513 * It can therefore be used to extract an explicit representation
514 * for that variable.
515 * Do so unless the local variable already has an explicit representation or
516 * the explicit representation would involve any other local variables
517 * that in turn do not have an explicit representation.
518 * An equality constraint involving local variables without an explicit
519 * representation can be used in isl_basic_map_drop_redundant_divs
520 * to separate out an independent local variable. Introducing
521 * an explicit representation here would block this transformation,
522 * while the partial explicit representation in itself is not very useful.
523 * Set *progress if anything is changed.
525 * The equality constraint is of the form
527 * f(x) + n e >= 0
529 * with n a positive number. The explicit representation derived from
530 * this constraint is
532 * floor((-f(x))/n)
534 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
535 int div, int eq, int *progress)
537 unsigned total, o_div;
538 isl_bool involves;
540 if (!bmap)
541 return NULL;
543 if (!isl_int_is_zero(bmap->div[div][0]))
544 return bmap;
546 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
547 if (involves < 0)
548 return isl_basic_map_free(bmap);
549 if (involves)
550 return bmap;
552 total = isl_basic_map_dim(bmap, isl_dim_all);
553 o_div = isl_basic_map_offset(bmap, isl_dim_div);
554 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
555 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
556 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
557 if (progress)
558 *progress = 1;
560 return bmap;
563 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
564 int *progress)
566 int k;
567 int done;
568 int last_var;
569 unsigned total_var;
570 unsigned total;
572 bmap = isl_basic_map_order_divs(bmap);
574 if (!bmap)
575 return NULL;
577 total = isl_basic_map_total_dim(bmap);
578 total_var = total - bmap->n_div;
580 last_var = total - 1;
581 for (done = 0; done < bmap->n_eq; ++done) {
582 for (; last_var >= 0; --last_var) {
583 for (k = done; k < bmap->n_eq; ++k)
584 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
585 break;
586 if (k < bmap->n_eq)
587 break;
589 if (last_var < 0)
590 break;
591 if (k != done)
592 swap_equality(bmap, k, done);
593 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
594 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
596 bmap = eliminate_var_using_equality(bmap, last_var,
597 bmap->eq[done], 1, progress);
599 if (last_var >= total_var)
600 bmap = set_div_from_eq(bmap, last_var - total_var,
601 done, progress);
602 if (!bmap)
603 return NULL;
605 if (done == bmap->n_eq)
606 return bmap;
607 for (k = done; k < bmap->n_eq; ++k) {
608 if (isl_int_is_zero(bmap->eq[k][0]))
609 continue;
610 return isl_basic_map_set_to_empty(bmap);
612 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
613 return bmap;
616 __isl_give isl_basic_set *isl_basic_set_gauss(
617 __isl_take isl_basic_set *bset, int *progress)
619 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
620 progress));
624 static unsigned int round_up(unsigned int v)
626 int old_v = v;
628 while (v) {
629 old_v = v;
630 v ^= v & -v;
632 return old_v << 1;
635 /* Hash table of inequalities in a basic map.
636 * "index" is an array of addresses of inequalities in the basic map, some
637 * of which are NULL. The inequalities are hashed on the coefficients
638 * except the constant term.
639 * "size" is the number of elements in the array and is always a power of two
640 * "bits" is the number of bits need to represent an index into the array.
641 * "total" is the total dimension of the basic map.
643 struct isl_constraint_index {
644 unsigned int size;
645 int bits;
646 isl_int ***index;
647 unsigned total;
650 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
652 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
653 __isl_keep isl_basic_map *bmap)
655 isl_ctx *ctx;
657 ci->index = NULL;
658 if (!bmap)
659 return isl_stat_error;
660 ci->total = isl_basic_set_total_dim(bmap);
661 if (bmap->n_ineq == 0)
662 return isl_stat_ok;
663 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
664 ci->bits = ffs(ci->size) - 1;
665 ctx = isl_basic_map_get_ctx(bmap);
666 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
667 if (!ci->index)
668 return isl_stat_error;
670 return isl_stat_ok;
673 /* Free the memory allocated by create_constraint_index.
675 static void constraint_index_free(struct isl_constraint_index *ci)
677 free(ci->index);
680 /* Return the position in ci->index that contains the address of
681 * an inequality that is equal to *ineq up to the constant term,
682 * provided this address is not identical to "ineq".
683 * If there is no such inequality, then return the position where
684 * such an inequality should be inserted.
686 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
688 int h;
689 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
690 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
691 if (ineq != ci->index[h] &&
692 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
693 break;
694 return h;
697 /* Return the position in ci->index that contains the address of
698 * an inequality that is equal to the k'th inequality of "bmap"
699 * up to the constant term, provided it does not point to the very
700 * same inequality.
701 * If there is no such inequality, then return the position where
702 * such an inequality should be inserted.
704 static int hash_index(struct isl_constraint_index *ci,
705 __isl_keep isl_basic_map *bmap, int k)
707 return hash_index_ineq(ci, &bmap->ineq[k]);
710 static int set_hash_index(struct isl_constraint_index *ci,
711 __isl_keep isl_basic_set *bset, int k)
713 return hash_index(ci, bset, k);
716 /* Fill in the "ci" data structure with the inequalities of "bset".
718 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
719 __isl_keep isl_basic_set *bset)
721 int k, h;
723 if (create_constraint_index(ci, bset) < 0)
724 return isl_stat_error;
726 for (k = 0; k < bset->n_ineq; ++k) {
727 h = set_hash_index(ci, bset, k);
728 ci->index[h] = &bset->ineq[k];
731 return isl_stat_ok;
734 /* Is the inequality ineq (obviously) redundant with respect
735 * to the constraints in "ci"?
737 * Look for an inequality in "ci" with the same coefficients and then
738 * check if the contant term of "ineq" is greater than or equal
739 * to the constant term of that inequality. If so, "ineq" is clearly
740 * redundant.
742 * Note that hash_index_ineq ignores a stored constraint if it has
743 * the same address as the passed inequality. It is ok to pass
744 * the address of a local variable here since it will never be
745 * the same as the address of a constraint in "ci".
747 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
748 isl_int *ineq)
750 int h;
752 h = hash_index_ineq(ci, &ineq);
753 if (!ci->index[h])
754 return isl_bool_false;
755 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
758 /* If we can eliminate more than one div, then we need to make
759 * sure we do it from last div to first div, in order not to
760 * change the position of the other divs that still need to
761 * be removed.
763 static __isl_give isl_basic_map *remove_duplicate_divs(
764 __isl_take isl_basic_map *bmap, int *progress)
766 unsigned int size;
767 int *index;
768 int *elim_for;
769 int k, l, h;
770 int bits;
771 struct isl_blk eq;
772 int v_div;
773 unsigned total;
774 struct isl_ctx *ctx;
776 bmap = isl_basic_map_order_divs(bmap);
777 if (!bmap || bmap->n_div <= 1)
778 return bmap;
780 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
781 if (v_div < 0)
782 return isl_basic_map_free(bmap);
783 total = v_div + bmap->n_div;
785 ctx = bmap->ctx;
786 for (k = bmap->n_div - 1; k >= 0; --k)
787 if (!isl_int_is_zero(bmap->div[k][0]))
788 break;
789 if (k <= 0)
790 return bmap;
792 size = round_up(4 * bmap->n_div / 3 - 1);
793 if (size == 0)
794 return bmap;
795 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
796 bits = ffs(size) - 1;
797 index = isl_calloc_array(ctx, int, size);
798 if (!elim_for || !index)
799 goto out;
800 eq = isl_blk_alloc(ctx, 1+total);
801 if (isl_blk_is_error(eq))
802 goto out;
804 isl_seq_clr(eq.data, 1+total);
805 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
806 for (--k; k >= 0; --k) {
807 uint32_t hash;
809 if (isl_int_is_zero(bmap->div[k][0]))
810 continue;
812 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
813 for (h = hash; index[h]; h = (h+1) % size)
814 if (isl_seq_eq(bmap->div[k],
815 bmap->div[index[h]-1], 2+total))
816 break;
817 if (index[h]) {
818 *progress = 1;
819 l = index[h] - 1;
820 elim_for[l] = k + 1;
822 index[h] = k+1;
824 for (l = bmap->n_div - 1; l >= 0; --l) {
825 if (!elim_for[l])
826 continue;
827 k = elim_for[l] - 1;
828 isl_int_set_si(eq.data[1 + v_div + k], -1);
829 isl_int_set_si(eq.data[1 + v_div + l], 1);
830 bmap = eliminate_div(bmap, eq.data, l, 1);
831 if (!bmap)
832 break;
833 isl_int_set_si(eq.data[1 + v_div + k], 0);
834 isl_int_set_si(eq.data[1 + v_div + l], 0);
837 isl_blk_free(ctx, eq);
838 out:
839 free(index);
840 free(elim_for);
841 return bmap;
844 static int n_pure_div_eq(struct isl_basic_map *bmap)
846 int i, j;
847 int v_div;
849 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
850 if (v_div < 0)
851 return -1;
852 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
853 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
854 --j;
855 if (j < 0)
856 break;
857 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
858 return 0;
860 return i;
863 /* Normalize divs that appear in equalities.
865 * In particular, we assume that bmap contains some equalities
866 * of the form
868 * a x = m * e_i
870 * and we want to replace the set of e_i by a minimal set and
871 * such that the new e_i have a canonical representation in terms
872 * of the vector x.
873 * If any of the equalities involves more than one divs, then
874 * we currently simply bail out.
876 * Let us first additionally assume that all equalities involve
877 * a div. The equalities then express modulo constraints on the
878 * remaining variables and we can use "parameter compression"
879 * to find a minimal set of constraints. The result is a transformation
881 * x = T(x') = x_0 + G x'
883 * with G a lower-triangular matrix with all elements below the diagonal
884 * non-negative and smaller than the diagonal element on the same row.
885 * We first normalize x_0 by making the same property hold in the affine
886 * T matrix.
887 * The rows i of G with a 1 on the diagonal do not impose any modulo
888 * constraint and simply express x_i = x'_i.
889 * For each of the remaining rows i, we introduce a div and a corresponding
890 * equality. In particular
892 * g_ii e_j = x_i - g_i(x')
894 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
895 * corresponding div (if g_kk != 1).
897 * If there are any equalities not involving any div, then we
898 * first apply a variable compression on the variables x:
900 * x = C x'' x'' = C_2 x
902 * and perform the above parameter compression on A C instead of on A.
903 * The resulting compression is then of the form
905 * x'' = T(x') = x_0 + G x'
907 * and in constructing the new divs and the corresponding equalities,
908 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
909 * by the corresponding row from C_2.
911 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
912 int *progress)
914 int i, j, k;
915 int v_div;
916 int div_eq;
917 struct isl_mat *B;
918 struct isl_vec *d;
919 struct isl_mat *T = NULL;
920 struct isl_mat *C = NULL;
921 struct isl_mat *C2 = NULL;
922 isl_int v;
923 int *pos = NULL;
924 int dropped, needed;
926 if (!bmap)
927 return NULL;
929 if (bmap->n_div == 0)
930 return bmap;
932 if (bmap->n_eq == 0)
933 return bmap;
935 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
936 return bmap;
938 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
939 div_eq = n_pure_div_eq(bmap);
940 if (v_div < 0 || div_eq < 0)
941 return isl_basic_map_free(bmap);
942 if (div_eq == 0)
943 return bmap;
945 if (div_eq < bmap->n_eq) {
946 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
947 bmap->n_eq - div_eq, 0, 1 + v_div);
948 C = isl_mat_variable_compression(B, &C2);
949 if (!C || !C2)
950 goto error;
951 if (C->n_col == 0) {
952 bmap = isl_basic_map_set_to_empty(bmap);
953 isl_mat_free(C);
954 isl_mat_free(C2);
955 goto done;
959 d = isl_vec_alloc(bmap->ctx, div_eq);
960 if (!d)
961 goto error;
962 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
963 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
964 --j;
965 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
967 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
969 if (C) {
970 B = isl_mat_product(B, C);
971 C = NULL;
974 T = isl_mat_parameter_compression(B, d);
975 if (!T)
976 goto error;
977 if (T->n_col == 0) {
978 bmap = isl_basic_map_set_to_empty(bmap);
979 isl_mat_free(C2);
980 isl_mat_free(T);
981 goto done;
983 isl_int_init(v);
984 for (i = 0; i < T->n_row - 1; ++i) {
985 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
986 if (isl_int_is_zero(v))
987 continue;
988 isl_mat_col_submul(T, 0, v, 1 + i);
990 isl_int_clear(v);
991 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
992 if (!pos)
993 goto error;
994 /* We have to be careful because dropping equalities may reorder them */
995 dropped = 0;
996 for (j = bmap->n_div - 1; j >= 0; --j) {
997 for (i = 0; i < bmap->n_eq; ++i)
998 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
999 break;
1000 if (i < bmap->n_eq) {
1001 bmap = isl_basic_map_drop_div(bmap, j);
1002 isl_basic_map_drop_equality(bmap, i);
1003 ++dropped;
1006 pos[0] = 0;
1007 needed = 0;
1008 for (i = 1; i < T->n_row; ++i) {
1009 if (isl_int_is_one(T->row[i][i]))
1010 pos[i] = i;
1011 else
1012 needed++;
1014 if (needed > dropped) {
1015 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1016 needed, needed, 0);
1017 if (!bmap)
1018 goto error;
1020 for (i = 1; i < T->n_row; ++i) {
1021 if (isl_int_is_one(T->row[i][i]))
1022 continue;
1023 k = isl_basic_map_alloc_div(bmap);
1024 pos[i] = 1 + v_div + k;
1025 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1026 isl_int_set(bmap->div[k][0], T->row[i][i]);
1027 if (C2)
1028 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1029 else
1030 isl_int_set_si(bmap->div[k][1 + i], 1);
1031 for (j = 0; j < i; ++j) {
1032 if (isl_int_is_zero(T->row[i][j]))
1033 continue;
1034 if (pos[j] < T->n_row && C2)
1035 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1036 C2->row[pos[j]], 1 + v_div);
1037 else
1038 isl_int_neg(bmap->div[k][1 + pos[j]],
1039 T->row[i][j]);
1041 j = isl_basic_map_alloc_equality(bmap);
1042 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1043 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1045 free(pos);
1046 isl_mat_free(C2);
1047 isl_mat_free(T);
1049 if (progress)
1050 *progress = 1;
1051 done:
1052 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1054 return bmap;
1055 error:
1056 free(pos);
1057 isl_mat_free(C);
1058 isl_mat_free(C2);
1059 isl_mat_free(T);
1060 return bmap;
1063 static __isl_give isl_basic_map *set_div_from_lower_bound(
1064 __isl_take isl_basic_map *bmap, int div, int ineq)
1066 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1068 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1069 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1070 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1071 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1072 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1074 return bmap;
1077 /* Check whether it is ok to define a div based on an inequality.
1078 * To avoid the introduction of circular definitions of divs, we
1079 * do not allow such a definition if the resulting expression would refer to
1080 * any other undefined divs or if any known div is defined in
1081 * terms of the unknown div.
1083 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1084 int div, int ineq)
1086 int j;
1087 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1089 /* Not defined in terms of unknown divs */
1090 for (j = 0; j < bmap->n_div; ++j) {
1091 if (div == j)
1092 continue;
1093 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1094 continue;
1095 if (isl_int_is_zero(bmap->div[j][0]))
1096 return isl_bool_false;
1099 /* No other div defined in terms of this one => avoid loops */
1100 for (j = 0; j < bmap->n_div; ++j) {
1101 if (div == j)
1102 continue;
1103 if (isl_int_is_zero(bmap->div[j][0]))
1104 continue;
1105 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1106 return isl_bool_false;
1109 return isl_bool_true;
1112 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1113 * be a better expression than the current one?
1115 * If we do not have any expression yet, then any expression would be better.
1116 * Otherwise we check if the last variable involved in the inequality
1117 * (disregarding the div that it would define) is in an earlier position
1118 * than the last variable involved in the current div expression.
1120 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1121 int div, int ineq)
1123 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1124 int last_div;
1125 int last_ineq;
1127 if (isl_int_is_zero(bmap->div[div][0]))
1128 return isl_bool_true;
1130 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1131 bmap->n_div - (div + 1)) >= 0)
1132 return isl_bool_false;
1134 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1135 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1136 total + bmap->n_div);
1138 return last_ineq < last_div;
1141 /* Given two constraints "k" and "l" that are opposite to each other,
1142 * except for the constant term, check if we can use them
1143 * to obtain an expression for one of the hitherto unknown divs or
1144 * a "better" expression for a div for which we already have an expression.
1145 * "sum" is the sum of the constant terms of the constraints.
1146 * If this sum is strictly smaller than the coefficient of one
1147 * of the divs, then this pair can be used define the div.
1148 * To avoid the introduction of circular definitions of divs, we
1149 * do not use the pair if the resulting expression would refer to
1150 * any other undefined divs or if any known div is defined in
1151 * terms of the unknown div.
1153 static __isl_give isl_basic_map *check_for_div_constraints(
1154 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1155 int *progress)
1157 int i;
1158 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1160 for (i = 0; i < bmap->n_div; ++i) {
1161 isl_bool set_div;
1163 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1164 continue;
1165 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1166 continue;
1167 set_div = better_div_constraint(bmap, i, k);
1168 if (set_div >= 0 && set_div)
1169 set_div = ok_to_set_div_from_bound(bmap, i, k);
1170 if (set_div < 0)
1171 return isl_basic_map_free(bmap);
1172 if (!set_div)
1173 break;
1174 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1175 bmap = set_div_from_lower_bound(bmap, i, k);
1176 else
1177 bmap = set_div_from_lower_bound(bmap, i, l);
1178 if (progress)
1179 *progress = 1;
1180 break;
1182 return bmap;
1185 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1186 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1188 struct isl_constraint_index ci;
1189 int k, l, h;
1190 unsigned total = isl_basic_map_total_dim(bmap);
1191 isl_int sum;
1193 if (!bmap || bmap->n_ineq <= 1)
1194 return bmap;
1196 if (create_constraint_index(&ci, bmap) < 0)
1197 return bmap;
1199 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1200 ci.index[h] = &bmap->ineq[0];
1201 for (k = 1; k < bmap->n_ineq; ++k) {
1202 h = hash_index(&ci, bmap, k);
1203 if (!ci.index[h]) {
1204 ci.index[h] = &bmap->ineq[k];
1205 continue;
1207 if (progress)
1208 *progress = 1;
1209 l = ci.index[h] - &bmap->ineq[0];
1210 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1211 swap_inequality(bmap, k, l);
1212 isl_basic_map_drop_inequality(bmap, k);
1213 --k;
1215 isl_int_init(sum);
1216 for (k = 0; k < bmap->n_ineq-1; ++k) {
1217 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1218 h = hash_index(&ci, bmap, k);
1219 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1220 if (!ci.index[h])
1221 continue;
1222 l = ci.index[h] - &bmap->ineq[0];
1223 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1224 if (isl_int_is_pos(sum)) {
1225 if (detect_divs)
1226 bmap = check_for_div_constraints(bmap, k, l,
1227 sum, progress);
1228 continue;
1230 if (isl_int_is_zero(sum)) {
1231 /* We need to break out of the loop after these
1232 * changes since the contents of the hash
1233 * will no longer be valid.
1234 * Plus, we probably we want to regauss first.
1236 if (progress)
1237 *progress = 1;
1238 isl_basic_map_drop_inequality(bmap, l);
1239 isl_basic_map_inequality_to_equality(bmap, k);
1240 } else
1241 bmap = isl_basic_map_set_to_empty(bmap);
1242 break;
1244 isl_int_clear(sum);
1246 constraint_index_free(&ci);
1247 return bmap;
1250 /* Detect all pairs of inequalities that form an equality.
1252 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1253 * Call it repeatedly while it is making progress.
1255 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1256 __isl_take isl_basic_map *bmap, int *progress)
1258 int duplicate;
1260 do {
1261 duplicate = 0;
1262 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1263 &duplicate, 0);
1264 if (progress && duplicate)
1265 *progress = 1;
1266 } while (duplicate);
1268 return bmap;
1271 /* Eliminate knowns divs from constraints where they appear with
1272 * a (positive or negative) unit coefficient.
1274 * That is, replace
1276 * floor(e/m) + f >= 0
1278 * by
1280 * e + m f >= 0
1282 * and
1284 * -floor(e/m) + f >= 0
1286 * by
1288 * -e + m f + m - 1 >= 0
1290 * The first conversion is valid because floor(e/m) >= -f is equivalent
1291 * to e/m >= -f because -f is an integral expression.
1292 * The second conversion follows from the fact that
1294 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1297 * Note that one of the div constraints may have been eliminated
1298 * due to being redundant with respect to the constraint that is
1299 * being modified by this function. The modified constraint may
1300 * no longer imply this div constraint, so we add it back to make
1301 * sure we do not lose any information.
1303 * We skip integral divs, i.e., those with denominator 1, as we would
1304 * risk eliminating the div from the div constraints. We do not need
1305 * to handle those divs here anyway since the div constraints will turn
1306 * out to form an equality and this equality can then be used to eliminate
1307 * the div from all constraints.
1309 static __isl_give isl_basic_map *eliminate_unit_divs(
1310 __isl_take isl_basic_map *bmap, int *progress)
1312 int i, j;
1313 isl_ctx *ctx;
1314 unsigned total;
1316 if (!bmap)
1317 return NULL;
1319 ctx = isl_basic_map_get_ctx(bmap);
1320 total = isl_basic_map_offset(bmap, isl_dim_div);
1322 for (i = 0; i < bmap->n_div; ++i) {
1323 if (isl_int_is_zero(bmap->div[i][0]))
1324 continue;
1325 if (isl_int_is_one(bmap->div[i][0]))
1326 continue;
1327 for (j = 0; j < bmap->n_ineq; ++j) {
1328 int s;
1330 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1331 !isl_int_is_negone(bmap->ineq[j][total + i]))
1332 continue;
1334 *progress = 1;
1336 s = isl_int_sgn(bmap->ineq[j][total + i]);
1337 isl_int_set_si(bmap->ineq[j][total + i], 0);
1338 if (s < 0)
1339 isl_seq_combine(bmap->ineq[j],
1340 ctx->negone, bmap->div[i] + 1,
1341 bmap->div[i][0], bmap->ineq[j],
1342 total + bmap->n_div);
1343 else
1344 isl_seq_combine(bmap->ineq[j],
1345 ctx->one, bmap->div[i] + 1,
1346 bmap->div[i][0], bmap->ineq[j],
1347 total + bmap->n_div);
1348 if (s < 0) {
1349 isl_int_add(bmap->ineq[j][0],
1350 bmap->ineq[j][0], bmap->div[i][0]);
1351 isl_int_sub_ui(bmap->ineq[j][0],
1352 bmap->ineq[j][0], 1);
1355 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1356 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1357 if (!bmap)
1358 return NULL;
1362 return bmap;
1365 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1367 int progress = 1;
1368 if (!bmap)
1369 return NULL;
1370 while (progress) {
1371 isl_bool empty;
1373 progress = 0;
1374 empty = isl_basic_map_plain_is_empty(bmap);
1375 if (empty < 0)
1376 return isl_basic_map_free(bmap);
1377 if (empty)
1378 break;
1379 bmap = isl_basic_map_normalize_constraints(bmap);
1380 bmap = reduce_div_coefficients(bmap);
1381 bmap = normalize_div_expressions(bmap);
1382 bmap = remove_duplicate_divs(bmap, &progress);
1383 bmap = eliminate_unit_divs(bmap, &progress);
1384 bmap = eliminate_divs_eq(bmap, &progress);
1385 bmap = eliminate_divs_ineq(bmap, &progress);
1386 bmap = isl_basic_map_gauss(bmap, &progress);
1387 /* requires equalities in normal form */
1388 bmap = normalize_divs(bmap, &progress);
1389 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1390 &progress, 1);
1391 if (bmap && progress)
1392 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1394 return bmap;
1397 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1399 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1403 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1404 isl_int *constraint, unsigned div)
1406 unsigned pos;
1408 if (!bmap)
1409 return isl_bool_error;
1411 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1413 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1414 int neg;
1415 isl_int_sub(bmap->div[div][1],
1416 bmap->div[div][1], bmap->div[div][0]);
1417 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1418 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1419 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1420 isl_int_add(bmap->div[div][1],
1421 bmap->div[div][1], bmap->div[div][0]);
1422 if (!neg)
1423 return isl_bool_false;
1424 if (isl_seq_first_non_zero(constraint+pos+1,
1425 bmap->n_div-div-1) != -1)
1426 return isl_bool_false;
1427 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1428 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1429 return isl_bool_false;
1430 if (isl_seq_first_non_zero(constraint+pos+1,
1431 bmap->n_div-div-1) != -1)
1432 return isl_bool_false;
1433 } else
1434 return isl_bool_false;
1436 return isl_bool_true;
1439 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1440 isl_int *constraint, unsigned div)
1442 return isl_basic_map_is_div_constraint(bset, constraint, div);
1446 /* If the only constraints a div d=floor(f/m)
1447 * appears in are its two defining constraints
1449 * f - m d >=0
1450 * -(f - (m - 1)) + m d >= 0
1452 * then it can safely be removed.
1454 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1456 int i;
1457 unsigned pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1459 for (i = 0; i < bmap->n_eq; ++i)
1460 if (!isl_int_is_zero(bmap->eq[i][pos]))
1461 return isl_bool_false;
1463 for (i = 0; i < bmap->n_ineq; ++i) {
1464 isl_bool red;
1466 if (isl_int_is_zero(bmap->ineq[i][pos]))
1467 continue;
1468 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1469 if (red < 0 || !red)
1470 return red;
1473 for (i = 0; i < bmap->n_div; ++i) {
1474 if (isl_int_is_zero(bmap->div[i][0]))
1475 continue;
1476 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1477 return isl_bool_false;
1480 return isl_bool_true;
1484 * Remove divs that don't occur in any of the constraints or other divs.
1485 * These can arise when dropping constraints from a basic map or
1486 * when the divs of a basic map have been temporarily aligned
1487 * with the divs of another basic map.
1489 static __isl_give isl_basic_map *remove_redundant_divs(
1490 __isl_take isl_basic_map *bmap)
1492 int i;
1493 int v_div;
1495 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1496 if (v_div < 0)
1497 return isl_basic_map_free(bmap);
1499 for (i = bmap->n_div-1; i >= 0; --i) {
1500 isl_bool redundant;
1502 redundant = div_is_redundant(bmap, i);
1503 if (redundant < 0)
1504 return isl_basic_map_free(bmap);
1505 if (!redundant)
1506 continue;
1507 bmap = isl_basic_map_drop_constraints_involving(bmap,
1508 v_div + i, 1);
1509 bmap = isl_basic_map_drop_div(bmap, i);
1511 return bmap;
1514 /* Mark "bmap" as final, without checking for obviously redundant
1515 * integer divisions. This function should be used when "bmap"
1516 * is known not to involve any such integer divisions.
1518 __isl_give isl_basic_map *isl_basic_map_mark_final(
1519 __isl_take isl_basic_map *bmap)
1521 if (!bmap)
1522 return NULL;
1523 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1524 return bmap;
1527 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1529 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1531 bmap = remove_redundant_divs(bmap);
1532 bmap = isl_basic_map_mark_final(bmap);
1533 return bmap;
1536 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1538 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1541 /* Remove definition of any div that is defined in terms of the given variable.
1542 * The div itself is not removed. Functions such as
1543 * eliminate_divs_ineq depend on the other divs remaining in place.
1545 static __isl_give isl_basic_map *remove_dependent_vars(
1546 __isl_take isl_basic_map *bmap, int pos)
1548 int i;
1550 if (!bmap)
1551 return NULL;
1553 for (i = 0; i < bmap->n_div; ++i) {
1554 if (isl_int_is_zero(bmap->div[i][0]))
1555 continue;
1556 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1557 continue;
1558 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1559 if (!bmap)
1560 return NULL;
1562 return bmap;
1565 /* Eliminate the specified variables from the constraints using
1566 * Fourier-Motzkin. The variables themselves are not removed.
1568 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1569 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1571 int d;
1572 int i, j, k;
1573 unsigned total;
1574 int need_gauss = 0;
1576 if (n == 0)
1577 return bmap;
1578 if (!bmap)
1579 return NULL;
1580 total = isl_basic_map_total_dim(bmap);
1582 bmap = isl_basic_map_cow(bmap);
1583 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1584 bmap = remove_dependent_vars(bmap, d);
1585 if (!bmap)
1586 return NULL;
1588 for (d = pos + n - 1;
1589 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1590 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1591 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1592 int n_lower, n_upper;
1593 if (!bmap)
1594 return NULL;
1595 for (i = 0; i < bmap->n_eq; ++i) {
1596 if (isl_int_is_zero(bmap->eq[i][1+d]))
1597 continue;
1598 bmap = eliminate_var_using_equality(bmap, d,
1599 bmap->eq[i], 0, NULL);
1600 if (isl_basic_map_drop_equality(bmap, i) < 0)
1601 return isl_basic_map_free(bmap);
1602 need_gauss = 1;
1603 break;
1605 if (i < bmap->n_eq)
1606 continue;
1607 n_lower = 0;
1608 n_upper = 0;
1609 for (i = 0; i < bmap->n_ineq; ++i) {
1610 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1611 n_lower++;
1612 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1613 n_upper++;
1615 bmap = isl_basic_map_extend_constraints(bmap,
1616 0, n_lower * n_upper);
1617 if (!bmap)
1618 goto error;
1619 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1620 int last;
1621 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1622 continue;
1623 last = -1;
1624 for (j = 0; j < i; ++j) {
1625 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1626 continue;
1627 last = j;
1628 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1629 isl_int_sgn(bmap->ineq[j][1+d]))
1630 continue;
1631 k = isl_basic_map_alloc_inequality(bmap);
1632 if (k < 0)
1633 goto error;
1634 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1635 1+total);
1636 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1637 1+d, 1+total, NULL);
1639 isl_basic_map_drop_inequality(bmap, i);
1640 i = last + 1;
1642 if (n_lower > 0 && n_upper > 0) {
1643 bmap = isl_basic_map_normalize_constraints(bmap);
1644 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1645 NULL, 0);
1646 bmap = isl_basic_map_gauss(bmap, NULL);
1647 bmap = isl_basic_map_remove_redundancies(bmap);
1648 need_gauss = 0;
1649 if (!bmap)
1650 goto error;
1651 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1652 break;
1655 if (need_gauss)
1656 bmap = isl_basic_map_gauss(bmap, NULL);
1657 return bmap;
1658 error:
1659 isl_basic_map_free(bmap);
1660 return NULL;
1663 struct isl_basic_set *isl_basic_set_eliminate_vars(
1664 struct isl_basic_set *bset, unsigned pos, unsigned n)
1666 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1667 pos, n));
1670 /* Eliminate the specified n dimensions starting at first from the
1671 * constraints, without removing the dimensions from the space.
1672 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1673 * Otherwise, they are projected out and the original space is restored.
1675 __isl_give isl_basic_map *isl_basic_map_eliminate(
1676 __isl_take isl_basic_map *bmap,
1677 enum isl_dim_type type, unsigned first, unsigned n)
1679 isl_space *space;
1681 if (!bmap)
1682 return NULL;
1683 if (n == 0)
1684 return bmap;
1686 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1687 return isl_basic_map_free(bmap);
1689 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1690 first += isl_basic_map_offset(bmap, type) - 1;
1691 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1692 return isl_basic_map_finalize(bmap);
1695 space = isl_basic_map_get_space(bmap);
1696 bmap = isl_basic_map_project_out(bmap, type, first, n);
1697 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1698 bmap = isl_basic_map_reset_space(bmap, space);
1699 return bmap;
1702 __isl_give isl_basic_set *isl_basic_set_eliminate(
1703 __isl_take isl_basic_set *bset,
1704 enum isl_dim_type type, unsigned first, unsigned n)
1706 return isl_basic_map_eliminate(bset, type, first, n);
1709 /* Remove all constraints from "bmap" that reference any unknown local
1710 * variables (directly or indirectly).
1712 * Dropping all constraints on a local variable will make it redundant,
1713 * so it will get removed implicitly by
1714 * isl_basic_map_drop_constraints_involving_dims. Some other local
1715 * variables may also end up becoming redundant if they only appear
1716 * in constraints together with the unknown local variable.
1717 * Therefore, start over after calling
1718 * isl_basic_map_drop_constraints_involving_dims.
1720 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1721 __isl_take isl_basic_map *bmap)
1723 isl_bool known;
1724 int i, n_div, o_div;
1726 known = isl_basic_map_divs_known(bmap);
1727 if (known < 0)
1728 return isl_basic_map_free(bmap);
1729 if (known)
1730 return bmap;
1732 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1733 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1735 for (i = 0; i < n_div; ++i) {
1736 known = isl_basic_map_div_is_known(bmap, i);
1737 if (known < 0)
1738 return isl_basic_map_free(bmap);
1739 if (known)
1740 continue;
1741 bmap = remove_dependent_vars(bmap, o_div + i);
1742 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1743 isl_dim_div, i, 1);
1744 if (!bmap)
1745 return NULL;
1746 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1747 i = -1;
1750 return bmap;
1753 /* Remove all constraints from "map" that reference any unknown local
1754 * variables (directly or indirectly).
1756 * Since constraints may get dropped from the basic maps,
1757 * they may no longer be disjoint from each other.
1759 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1760 __isl_take isl_map *map)
1762 int i;
1763 isl_bool known;
1765 known = isl_map_divs_known(map);
1766 if (known < 0)
1767 return isl_map_free(map);
1768 if (known)
1769 return map;
1771 map = isl_map_cow(map);
1772 if (!map)
1773 return NULL;
1775 for (i = 0; i < map->n; ++i) {
1776 map->p[i] =
1777 isl_basic_map_drop_constraint_involving_unknown_divs(
1778 map->p[i]);
1779 if (!map->p[i])
1780 return isl_map_free(map);
1783 if (map->n > 1)
1784 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1786 return map;
1789 /* Don't assume equalities are in order, because align_divs
1790 * may have changed the order of the divs.
1792 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1794 int d, i;
1795 unsigned total;
1797 total = isl_space_dim(bmap->dim, isl_dim_all);
1798 for (d = 0; d < total; ++d)
1799 elim[d] = -1;
1800 for (i = 0; i < bmap->n_eq; ++i) {
1801 for (d = total - 1; d >= 0; --d) {
1802 if (isl_int_is_zero(bmap->eq[i][1+d]))
1803 continue;
1804 elim[d] = i;
1805 break;
1810 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1811 int *elim)
1813 compute_elimination_index(bset_to_bmap(bset), elim);
1816 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1817 __isl_keep isl_basic_map *bmap, int *elim)
1819 int d;
1820 int copied = 0;
1821 unsigned total;
1823 total = isl_space_dim(bmap->dim, isl_dim_all);
1824 for (d = total - 1; d >= 0; --d) {
1825 if (isl_int_is_zero(src[1+d]))
1826 continue;
1827 if (elim[d] == -1)
1828 continue;
1829 if (!copied) {
1830 isl_seq_cpy(dst, src, 1 + total);
1831 copied = 1;
1833 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1835 return copied;
1838 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1839 __isl_keep isl_basic_set *bset, int *elim)
1841 return reduced_using_equalities(dst, src,
1842 bset_to_bmap(bset), elim);
1845 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1846 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1848 int i;
1849 int *elim;
1851 if (!bset || !context)
1852 goto error;
1854 if (context->n_eq == 0) {
1855 isl_basic_set_free(context);
1856 return bset;
1859 bset = isl_basic_set_cow(bset);
1860 if (!bset)
1861 goto error;
1863 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1864 if (!elim)
1865 goto error;
1866 set_compute_elimination_index(context, elim);
1867 for (i = 0; i < bset->n_eq; ++i)
1868 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1869 context, elim);
1870 for (i = 0; i < bset->n_ineq; ++i)
1871 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1872 context, elim);
1873 isl_basic_set_free(context);
1874 free(elim);
1875 bset = isl_basic_set_simplify(bset);
1876 bset = isl_basic_set_finalize(bset);
1877 return bset;
1878 error:
1879 isl_basic_set_free(bset);
1880 isl_basic_set_free(context);
1881 return NULL;
1884 /* For each inequality in "ineq" that is a shifted (more relaxed)
1885 * copy of an inequality in "context", mark the corresponding entry
1886 * in "row" with -1.
1887 * If an inequality only has a non-negative constant term, then
1888 * mark it as well.
1890 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1891 __isl_keep isl_basic_set *context, int *row)
1893 struct isl_constraint_index ci;
1894 int n_ineq;
1895 unsigned total;
1896 int k;
1898 if (!ineq || !context)
1899 return isl_stat_error;
1900 if (context->n_ineq == 0)
1901 return isl_stat_ok;
1902 if (setup_constraint_index(&ci, context) < 0)
1903 return isl_stat_error;
1905 n_ineq = isl_mat_rows(ineq);
1906 total = isl_mat_cols(ineq) - 1;
1907 for (k = 0; k < n_ineq; ++k) {
1908 int l;
1909 isl_bool redundant;
1911 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1912 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1913 row[k] = -1;
1914 continue;
1916 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1917 if (redundant < 0)
1918 goto error;
1919 if (!redundant)
1920 continue;
1921 row[k] = -1;
1923 constraint_index_free(&ci);
1924 return isl_stat_ok;
1925 error:
1926 constraint_index_free(&ci);
1927 return isl_stat_error;
1930 static __isl_give isl_basic_set *remove_shifted_constraints(
1931 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1933 struct isl_constraint_index ci;
1934 int k;
1936 if (!bset || !context)
1937 return bset;
1939 if (context->n_ineq == 0)
1940 return bset;
1941 if (setup_constraint_index(&ci, context) < 0)
1942 return bset;
1944 for (k = 0; k < bset->n_ineq; ++k) {
1945 isl_bool redundant;
1947 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1948 if (redundant < 0)
1949 goto error;
1950 if (!redundant)
1951 continue;
1952 bset = isl_basic_set_cow(bset);
1953 if (!bset)
1954 goto error;
1955 isl_basic_set_drop_inequality(bset, k);
1956 --k;
1958 constraint_index_free(&ci);
1959 return bset;
1960 error:
1961 constraint_index_free(&ci);
1962 return bset;
1965 /* Remove constraints from "bmap" that are identical to constraints
1966 * in "context" or that are more relaxed (greater constant term).
1968 * We perform the test for shifted copies on the pure constraints
1969 * in remove_shifted_constraints.
1971 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1972 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1974 isl_basic_set *bset, *bset_context;
1976 if (!bmap || !context)
1977 goto error;
1979 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1980 isl_basic_map_free(context);
1981 return bmap;
1984 context = isl_basic_map_align_divs(context, bmap);
1985 bmap = isl_basic_map_align_divs(bmap, context);
1987 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1988 bset_context = isl_basic_map_underlying_set(context);
1989 bset = remove_shifted_constraints(bset, bset_context);
1990 isl_basic_set_free(bset_context);
1992 bmap = isl_basic_map_overlying_set(bset, bmap);
1994 return bmap;
1995 error:
1996 isl_basic_map_free(bmap);
1997 isl_basic_map_free(context);
1998 return NULL;
2001 /* Does the (linear part of a) constraint "c" involve any of the "len"
2002 * "relevant" dimensions?
2004 static int is_related(isl_int *c, int len, int *relevant)
2006 int i;
2008 for (i = 0; i < len; ++i) {
2009 if (!relevant[i])
2010 continue;
2011 if (!isl_int_is_zero(c[i]))
2012 return 1;
2015 return 0;
2018 /* Drop constraints from "bmap" that do not involve any of
2019 * the dimensions marked "relevant".
2021 static __isl_give isl_basic_map *drop_unrelated_constraints(
2022 __isl_take isl_basic_map *bmap, int *relevant)
2024 int i, dim;
2026 dim = isl_basic_map_dim(bmap, isl_dim_all);
2027 for (i = 0; i < dim; ++i)
2028 if (!relevant[i])
2029 break;
2030 if (i >= dim)
2031 return bmap;
2033 for (i = bmap->n_eq - 1; i >= 0; --i)
2034 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2035 bmap = isl_basic_map_cow(bmap);
2036 if (isl_basic_map_drop_equality(bmap, i) < 0)
2037 return isl_basic_map_free(bmap);
2040 for (i = bmap->n_ineq - 1; i >= 0; --i)
2041 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2042 bmap = isl_basic_map_cow(bmap);
2043 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2044 return isl_basic_map_free(bmap);
2047 return bmap;
2050 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2052 * In particular, for any variable involved in the constraint,
2053 * find the actual group id from before and replace the group
2054 * of the corresponding variable by the minimal group of all
2055 * the variables involved in the constraint considered so far
2056 * (if this minimum is smaller) or replace the minimum by this group
2057 * (if the minimum is larger).
2059 * At the end, all the variables in "c" will (indirectly) point
2060 * to the minimal of the groups that they referred to originally.
2062 static void update_groups(int dim, int *group, isl_int *c)
2064 int j;
2065 int min = dim;
2067 for (j = 0; j < dim; ++j) {
2068 if (isl_int_is_zero(c[j]))
2069 continue;
2070 while (group[j] >= 0 && group[group[j]] != group[j])
2071 group[j] = group[group[j]];
2072 if (group[j] == min)
2073 continue;
2074 if (group[j] < min) {
2075 if (min >= 0 && min < dim)
2076 group[min] = group[j];
2077 min = group[j];
2078 } else
2079 group[group[j]] = min;
2083 /* Allocate an array of groups of variables, one for each variable
2084 * in "context", initialized to zero.
2086 static int *alloc_groups(__isl_keep isl_basic_set *context)
2088 isl_ctx *ctx;
2089 int dim;
2091 dim = isl_basic_set_dim(context, isl_dim_set);
2092 ctx = isl_basic_set_get_ctx(context);
2093 return isl_calloc_array(ctx, int, dim);
2096 /* Drop constraints from "bmap" that only involve variables that are
2097 * not related to any of the variables marked with a "-1" in "group".
2099 * We construct groups of variables that collect variables that
2100 * (indirectly) appear in some common constraint of "bmap".
2101 * Each group is identified by the first variable in the group,
2102 * except for the special group of variables that was already identified
2103 * in the input as -1 (or are related to those variables).
2104 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2105 * otherwise the group of i is the group of group[i].
2107 * We first initialize groups for the remaining variables.
2108 * Then we iterate over the constraints of "bmap" and update the
2109 * group of the variables in the constraint by the smallest group.
2110 * Finally, we resolve indirect references to groups by running over
2111 * the variables.
2113 * After computing the groups, we drop constraints that do not involve
2114 * any variables in the -1 group.
2116 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2117 __isl_take isl_basic_map *bmap, __isl_take int *group)
2119 int dim;
2120 int i;
2121 int last;
2123 if (!bmap)
2124 return NULL;
2126 dim = isl_basic_map_dim(bmap, isl_dim_all);
2128 last = -1;
2129 for (i = 0; i < dim; ++i)
2130 if (group[i] >= 0)
2131 last = group[i] = i;
2132 if (last < 0) {
2133 free(group);
2134 return bmap;
2137 for (i = 0; i < bmap->n_eq; ++i)
2138 update_groups(dim, group, bmap->eq[i] + 1);
2139 for (i = 0; i < bmap->n_ineq; ++i)
2140 update_groups(dim, group, bmap->ineq[i] + 1);
2142 for (i = 0; i < dim; ++i)
2143 if (group[i] >= 0)
2144 group[i] = group[group[i]];
2146 for (i = 0; i < dim; ++i)
2147 group[i] = group[i] == -1;
2149 bmap = drop_unrelated_constraints(bmap, group);
2151 free(group);
2152 return bmap;
2155 /* Drop constraints from "context" that are irrelevant for computing
2156 * the gist of "bset".
2158 * In particular, drop constraints in variables that are not related
2159 * to any of the variables involved in the constraints of "bset"
2160 * in the sense that there is no sequence of constraints that connects them.
2162 * We first mark all variables that appear in "bset" as belonging
2163 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2165 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2166 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2168 int *group;
2169 int dim;
2170 int i, j;
2172 if (!context || !bset)
2173 return isl_basic_set_free(context);
2175 group = alloc_groups(context);
2177 if (!group)
2178 return isl_basic_set_free(context);
2180 dim = isl_basic_set_dim(bset, isl_dim_set);
2181 for (i = 0; i < dim; ++i) {
2182 for (j = 0; j < bset->n_eq; ++j)
2183 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2184 break;
2185 if (j < bset->n_eq) {
2186 group[i] = -1;
2187 continue;
2189 for (j = 0; j < bset->n_ineq; ++j)
2190 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2191 break;
2192 if (j < bset->n_ineq)
2193 group[i] = -1;
2196 return isl_basic_map_drop_unrelated_constraints(context, group);
2199 /* Drop constraints from "context" that are irrelevant for computing
2200 * the gist of the inequalities "ineq".
2201 * Inequalities in "ineq" for which the corresponding element of row
2202 * is set to -1 have already been marked for removal and should be ignored.
2204 * In particular, drop constraints in variables that are not related
2205 * to any of the variables involved in "ineq"
2206 * in the sense that there is no sequence of constraints that connects them.
2208 * We first mark all variables that appear in "bset" as belonging
2209 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2211 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2212 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2214 int *group;
2215 int dim;
2216 int i, j, n;
2218 if (!context || !ineq)
2219 return isl_basic_set_free(context);
2221 group = alloc_groups(context);
2223 if (!group)
2224 return isl_basic_set_free(context);
2226 dim = isl_basic_set_dim(context, isl_dim_set);
2227 n = isl_mat_rows(ineq);
2228 for (i = 0; i < dim; ++i) {
2229 for (j = 0; j < n; ++j) {
2230 if (row[j] < 0)
2231 continue;
2232 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2233 break;
2235 if (j < n)
2236 group[i] = -1;
2239 return isl_basic_map_drop_unrelated_constraints(context, group);
2242 /* Do all "n" entries of "row" contain a negative value?
2244 static int all_neg(int *row, int n)
2246 int i;
2248 for (i = 0; i < n; ++i)
2249 if (row[i] >= 0)
2250 return 0;
2252 return 1;
2255 /* Update the inequalities in "bset" based on the information in "row"
2256 * and "tab".
2258 * In particular, the array "row" contains either -1, meaning that
2259 * the corresponding inequality of "bset" is redundant, or the index
2260 * of an inequality in "tab".
2262 * If the row entry is -1, then drop the inequality.
2263 * Otherwise, if the constraint is marked redundant in the tableau,
2264 * then drop the inequality. Similarly, if it is marked as an equality
2265 * in the tableau, then turn the inequality into an equality and
2266 * perform Gaussian elimination.
2268 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2269 __isl_keep int *row, struct isl_tab *tab)
2271 int i;
2272 unsigned n_ineq;
2273 unsigned n_eq;
2274 int found_equality = 0;
2276 if (!bset)
2277 return NULL;
2278 if (tab && tab->empty)
2279 return isl_basic_set_set_to_empty(bset);
2281 n_ineq = bset->n_ineq;
2282 for (i = n_ineq - 1; i >= 0; --i) {
2283 if (row[i] < 0) {
2284 if (isl_basic_set_drop_inequality(bset, i) < 0)
2285 return isl_basic_set_free(bset);
2286 continue;
2288 if (!tab)
2289 continue;
2290 n_eq = tab->n_eq;
2291 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2292 isl_basic_map_inequality_to_equality(bset, i);
2293 found_equality = 1;
2294 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2295 if (isl_basic_set_drop_inequality(bset, i) < 0)
2296 return isl_basic_set_free(bset);
2300 if (found_equality)
2301 bset = isl_basic_set_gauss(bset, NULL);
2302 bset = isl_basic_set_finalize(bset);
2303 return bset;
2306 /* Update the inequalities in "bset" based on the information in "row"
2307 * and "tab" and free all arguments (other than "bset").
2309 static __isl_give isl_basic_set *update_ineq_free(
2310 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2311 __isl_take isl_basic_set *context, __isl_take int *row,
2312 struct isl_tab *tab)
2314 isl_mat_free(ineq);
2315 isl_basic_set_free(context);
2317 bset = update_ineq(bset, row, tab);
2319 free(row);
2320 isl_tab_free(tab);
2321 return bset;
2324 /* Remove all information from bset that is redundant in the context
2325 * of context.
2326 * "ineq" contains the (possibly transformed) inequalities of "bset",
2327 * in the same order.
2328 * The (explicit) equalities of "bset" are assumed to have been taken
2329 * into account by the transformation such that only the inequalities
2330 * are relevant.
2331 * "context" is assumed not to be empty.
2333 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2334 * A value of -1 means that the inequality is obviously redundant and may
2335 * not even appear in "tab".
2337 * We first mark the inequalities of "bset"
2338 * that are obviously redundant with respect to some inequality in "context".
2339 * Then we remove those constraints from "context" that have become
2340 * irrelevant for computing the gist of "bset".
2341 * Note that this removal of constraints cannot be replaced by
2342 * a factorization because factors in "bset" may still be connected
2343 * to each other through constraints in "context".
2345 * If there are any inequalities left, we construct a tableau for
2346 * the context and then add the inequalities of "bset".
2347 * Before adding these inequalities, we freeze all constraints such that
2348 * they won't be considered redundant in terms of the constraints of "bset".
2349 * Then we detect all redundant constraints (among the
2350 * constraints that weren't frozen), first by checking for redundancy in the
2351 * the tableau and then by checking if replacing a constraint by its negation
2352 * would lead to an empty set. This last step is fairly expensive
2353 * and could be optimized by more reuse of the tableau.
2354 * Finally, we update bset according to the results.
2356 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2357 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2359 int i, r;
2360 int *row = NULL;
2361 isl_ctx *ctx;
2362 isl_basic_set *combined = NULL;
2363 struct isl_tab *tab = NULL;
2364 unsigned n_eq, context_ineq;
2366 if (!bset || !ineq || !context)
2367 goto error;
2369 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2370 isl_basic_set_free(context);
2371 isl_mat_free(ineq);
2372 return bset;
2375 ctx = isl_basic_set_get_ctx(context);
2376 row = isl_calloc_array(ctx, int, bset->n_ineq);
2377 if (!row)
2378 goto error;
2380 if (mark_shifted_constraints(ineq, context, row) < 0)
2381 goto error;
2382 if (all_neg(row, bset->n_ineq))
2383 return update_ineq_free(bset, ineq, context, row, NULL);
2385 context = drop_irrelevant_constraints_marked(context, ineq, row);
2386 if (!context)
2387 goto error;
2388 if (isl_basic_set_plain_is_universe(context))
2389 return update_ineq_free(bset, ineq, context, row, NULL);
2391 n_eq = context->n_eq;
2392 context_ineq = context->n_ineq;
2393 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2394 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2395 tab = isl_tab_from_basic_set(combined, 0);
2396 for (i = 0; i < context_ineq; ++i)
2397 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2398 goto error;
2399 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2400 goto error;
2401 r = context_ineq;
2402 for (i = 0; i < bset->n_ineq; ++i) {
2403 if (row[i] < 0)
2404 continue;
2405 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2406 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2407 goto error;
2408 row[i] = r++;
2410 if (isl_tab_detect_implicit_equalities(tab) < 0)
2411 goto error;
2412 if (isl_tab_detect_redundant(tab) < 0)
2413 goto error;
2414 for (i = bset->n_ineq - 1; i >= 0; --i) {
2415 isl_basic_set *test;
2416 int is_empty;
2418 if (row[i] < 0)
2419 continue;
2420 r = row[i];
2421 if (tab->con[n_eq + r].is_redundant)
2422 continue;
2423 test = isl_basic_set_dup(combined);
2424 test = isl_inequality_negate(test, r);
2425 test = isl_basic_set_update_from_tab(test, tab);
2426 is_empty = isl_basic_set_is_empty(test);
2427 isl_basic_set_free(test);
2428 if (is_empty < 0)
2429 goto error;
2430 if (is_empty)
2431 tab->con[n_eq + r].is_redundant = 1;
2433 bset = update_ineq_free(bset, ineq, context, row, tab);
2434 if (bset) {
2435 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2436 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2439 isl_basic_set_free(combined);
2440 return bset;
2441 error:
2442 free(row);
2443 isl_mat_free(ineq);
2444 isl_tab_free(tab);
2445 isl_basic_set_free(combined);
2446 isl_basic_set_free(context);
2447 isl_basic_set_free(bset);
2448 return NULL;
2451 /* Extract the inequalities of "bset" as an isl_mat.
2453 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2455 unsigned total;
2456 isl_ctx *ctx;
2457 isl_mat *ineq;
2459 if (!bset)
2460 return NULL;
2462 ctx = isl_basic_set_get_ctx(bset);
2463 total = isl_basic_set_total_dim(bset);
2464 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2465 0, 1 + total);
2467 return ineq;
2470 /* Remove all information from "bset" that is redundant in the context
2471 * of "context", for the case where both "bset" and "context" are
2472 * full-dimensional.
2474 static __isl_give isl_basic_set *uset_gist_uncompressed(
2475 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2477 isl_mat *ineq;
2479 ineq = extract_ineq(bset);
2480 return uset_gist_full(bset, ineq, context);
2483 /* Remove all information from "bset" that is redundant in the context
2484 * of "context", for the case where the combined equalities of
2485 * "bset" and "context" allow for a compression that can be obtained
2486 * by preapplication of "T".
2488 * "bset" itself is not transformed by "T". Instead, the inequalities
2489 * are extracted from "bset" and those are transformed by "T".
2490 * uset_gist_full then determines which of the transformed inequalities
2491 * are redundant with respect to the transformed "context" and removes
2492 * the corresponding inequalities from "bset".
2494 * After preapplying "T" to the inequalities, any common factor is
2495 * removed from the coefficients. If this results in a tightening
2496 * of the constant term, then the same tightening is applied to
2497 * the corresponding untransformed inequality in "bset".
2498 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2500 * g f'(x) + r >= 0
2502 * with 0 <= r < g, then it is equivalent to
2504 * f'(x) >= 0
2506 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2507 * subspace compressed by T since the latter would be transformed to
2509 * g f'(x) >= 0
2511 static __isl_give isl_basic_set *uset_gist_compressed(
2512 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2513 __isl_take isl_mat *T)
2515 isl_ctx *ctx;
2516 isl_mat *ineq;
2517 int i, n_row, n_col;
2518 isl_int rem;
2520 ineq = extract_ineq(bset);
2521 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2522 context = isl_basic_set_preimage(context, T);
2524 if (!ineq || !context)
2525 goto error;
2526 if (isl_basic_set_plain_is_empty(context)) {
2527 isl_mat_free(ineq);
2528 isl_basic_set_free(context);
2529 return isl_basic_set_set_to_empty(bset);
2532 ctx = isl_mat_get_ctx(ineq);
2533 n_row = isl_mat_rows(ineq);
2534 n_col = isl_mat_cols(ineq);
2535 isl_int_init(rem);
2536 for (i = 0; i < n_row; ++i) {
2537 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2538 if (isl_int_is_zero(ctx->normalize_gcd))
2539 continue;
2540 if (isl_int_is_one(ctx->normalize_gcd))
2541 continue;
2542 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2543 ctx->normalize_gcd, n_col - 1);
2544 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2545 isl_int_fdiv_q(ineq->row[i][0],
2546 ineq->row[i][0], ctx->normalize_gcd);
2547 if (isl_int_is_zero(rem))
2548 continue;
2549 bset = isl_basic_set_cow(bset);
2550 if (!bset)
2551 break;
2552 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2554 isl_int_clear(rem);
2556 return uset_gist_full(bset, ineq, context);
2557 error:
2558 isl_mat_free(ineq);
2559 isl_basic_set_free(context);
2560 isl_basic_set_free(bset);
2561 return NULL;
2564 /* Project "bset" onto the variables that are involved in "template".
2566 static __isl_give isl_basic_set *project_onto_involved(
2567 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2569 int i, n;
2571 if (!bset || !template)
2572 return isl_basic_set_free(bset);
2574 n = isl_basic_set_dim(template, isl_dim_set);
2576 for (i = 0; i < n; ++i) {
2577 isl_bool involved;
2579 involved = isl_basic_set_involves_dims(template,
2580 isl_dim_set, i, 1);
2581 if (involved < 0)
2582 return isl_basic_set_free(bset);
2583 if (involved)
2584 continue;
2585 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2588 return bset;
2591 /* Remove all information from bset that is redundant in the context
2592 * of context. In particular, equalities that are linear combinations
2593 * of those in context are removed. Then the inequalities that are
2594 * redundant in the context of the equalities and inequalities of
2595 * context are removed.
2597 * First of all, we drop those constraints from "context"
2598 * that are irrelevant for computing the gist of "bset".
2599 * Alternatively, we could factorize the intersection of "context" and "bset".
2601 * We first compute the intersection of the integer affine hulls
2602 * of "bset" and "context",
2603 * compute the gist inside this intersection and then reduce
2604 * the constraints with respect to the equalities of the context
2605 * that only involve variables already involved in the input.
2607 * If two constraints are mutually redundant, then uset_gist_full
2608 * will remove the second of those constraints. We therefore first
2609 * sort the constraints so that constraints not involving existentially
2610 * quantified variables are given precedence over those that do.
2611 * We have to perform this sorting before the variable compression,
2612 * because that may effect the order of the variables.
2614 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2615 __isl_take isl_basic_set *context)
2617 isl_mat *eq;
2618 isl_mat *T;
2619 isl_basic_set *aff;
2620 isl_basic_set *aff_context;
2621 unsigned total;
2623 if (!bset || !context)
2624 goto error;
2626 context = drop_irrelevant_constraints(context, bset);
2628 bset = isl_basic_set_detect_equalities(bset);
2629 aff = isl_basic_set_copy(bset);
2630 aff = isl_basic_set_plain_affine_hull(aff);
2631 context = isl_basic_set_detect_equalities(context);
2632 aff_context = isl_basic_set_copy(context);
2633 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2634 aff = isl_basic_set_intersect(aff, aff_context);
2635 if (!aff)
2636 goto error;
2637 if (isl_basic_set_plain_is_empty(aff)) {
2638 isl_basic_set_free(bset);
2639 isl_basic_set_free(context);
2640 return aff;
2642 bset = isl_basic_set_sort_constraints(bset);
2643 if (aff->n_eq == 0) {
2644 isl_basic_set_free(aff);
2645 return uset_gist_uncompressed(bset, context);
2647 total = isl_basic_set_total_dim(bset);
2648 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2649 eq = isl_mat_cow(eq);
2650 T = isl_mat_variable_compression(eq, NULL);
2651 isl_basic_set_free(aff);
2652 if (T && T->n_col == 0) {
2653 isl_mat_free(T);
2654 isl_basic_set_free(context);
2655 return isl_basic_set_set_to_empty(bset);
2658 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2659 aff_context = project_onto_involved(aff_context, bset);
2661 bset = uset_gist_compressed(bset, context, T);
2662 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2664 if (bset) {
2665 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2666 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2669 return bset;
2670 error:
2671 isl_basic_set_free(bset);
2672 isl_basic_set_free(context);
2673 return NULL;
2676 /* Return the number of equality constraints in "bmap" that involve
2677 * local variables. This function assumes that Gaussian elimination
2678 * has been applied to the equality constraints.
2680 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2682 int i;
2683 int total, n_div;
2685 if (!bmap)
2686 return -1;
2688 if (bmap->n_eq == 0)
2689 return 0;
2691 total = isl_basic_map_dim(bmap, isl_dim_all);
2692 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2693 total -= n_div;
2695 for (i = 0; i < bmap->n_eq; ++i)
2696 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2697 n_div) == -1)
2698 return i;
2700 return bmap->n_eq;
2703 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2704 * The constraints are assumed not to involve any local variables.
2706 static __isl_give isl_basic_map *basic_map_from_equalities(
2707 __isl_take isl_space *space, __isl_take isl_mat *eq)
2709 int i, k;
2710 isl_basic_map *bmap = NULL;
2712 if (!space || !eq)
2713 goto error;
2715 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2716 isl_die(isl_space_get_ctx(space), isl_error_internal,
2717 "unexpected number of columns", goto error);
2719 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2720 0, eq->n_row, 0);
2721 for (i = 0; i < eq->n_row; ++i) {
2722 k = isl_basic_map_alloc_equality(bmap);
2723 if (k < 0)
2724 goto error;
2725 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2728 isl_space_free(space);
2729 isl_mat_free(eq);
2730 return bmap;
2731 error:
2732 isl_space_free(space);
2733 isl_mat_free(eq);
2734 isl_basic_map_free(bmap);
2735 return NULL;
2738 /* Construct and return a variable compression based on the equality
2739 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2740 * "n1" is the number of (initial) equality constraints in "bmap1"
2741 * that do involve local variables.
2742 * "n2" is the number of (initial) equality constraints in "bmap2"
2743 * that do involve local variables.
2744 * "total" is the total number of other variables.
2745 * This function assumes that Gaussian elimination
2746 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2747 * such that the equality constraints not involving local variables
2748 * are those that start at "n1" or "n2".
2750 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2751 * then simply compute the compression based on the equality constraints
2752 * in the other basic map.
2753 * Otherwise, combine the equality constraints from both into a new
2754 * basic map such that Gaussian elimination can be applied to this combination
2755 * and then construct a variable compression from the resulting
2756 * equality constraints.
2758 static __isl_give isl_mat *combined_variable_compression(
2759 __isl_keep isl_basic_map *bmap1, int n1,
2760 __isl_keep isl_basic_map *bmap2, int n2, int total)
2762 isl_ctx *ctx;
2763 isl_mat *E1, *E2, *V;
2764 isl_basic_map *bmap;
2766 ctx = isl_basic_map_get_ctx(bmap1);
2767 if (bmap1->n_eq == n1) {
2768 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2769 n2, bmap2->n_eq - n2, 0, 1 + total);
2770 return isl_mat_variable_compression(E2, NULL);
2772 if (bmap2->n_eq == n2) {
2773 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2774 n1, bmap1->n_eq - n1, 0, 1 + total);
2775 return isl_mat_variable_compression(E1, NULL);
2777 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2778 n1, bmap1->n_eq - n1, 0, 1 + total);
2779 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2780 n2, bmap2->n_eq - n2, 0, 1 + total);
2781 E1 = isl_mat_concat(E1, E2);
2782 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2783 bmap = isl_basic_map_gauss(bmap, NULL);
2784 if (!bmap)
2785 return NULL;
2786 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2787 V = isl_mat_variable_compression(E1, NULL);
2788 isl_basic_map_free(bmap);
2790 return V;
2793 /* Extract the stride constraints from "bmap", compressed
2794 * with respect to both the stride constraints in "context" and
2795 * the remaining equality constraints in both "bmap" and "context".
2796 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2797 * "context_n_eq" is the number of (initial) stride constraints in "context".
2799 * Let x be all variables in "bmap" (and "context") other than the local
2800 * variables. First compute a variable compression
2802 * x = V x'
2804 * based on the non-stride equality constraints in "bmap" and "context".
2805 * Consider the stride constraints of "context",
2807 * A(x) + B(y) = 0
2809 * with y the local variables and plug in the variable compression,
2810 * resulting in
2812 * A(V x') + B(y) = 0
2814 * Use these constraints to compute a parameter compression on x'
2816 * x' = T x''
2818 * Now consider the stride constraints of "bmap"
2820 * C(x) + D(y) = 0
2822 * and plug in x = V*T x''.
2823 * That is, return A = [C*V*T D].
2825 static __isl_give isl_mat *extract_compressed_stride_constraints(
2826 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2827 __isl_keep isl_basic_map *context, int context_n_eq)
2829 int total, n_div;
2830 isl_ctx *ctx;
2831 isl_mat *A, *B, *T, *V;
2833 total = isl_basic_map_dim(context, isl_dim_all);
2834 n_div = isl_basic_map_dim(context, isl_dim_div);
2835 total -= n_div;
2837 ctx = isl_basic_map_get_ctx(bmap);
2839 V = combined_variable_compression(bmap, bmap_n_eq,
2840 context, context_n_eq, total);
2842 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2843 B = isl_mat_sub_alloc6(ctx, context->eq,
2844 0, context_n_eq, 1 + total, n_div);
2845 A = isl_mat_product(A, isl_mat_copy(V));
2846 T = isl_mat_parameter_compression_ext(A, B);
2847 T = isl_mat_product(V, T);
2849 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2850 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2852 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2853 0, bmap_n_eq, 0, 1 + total + n_div);
2854 A = isl_mat_product(A, T);
2856 return A;
2859 /* Remove the prime factors from *g that have an exponent that
2860 * is strictly smaller than the exponent in "c".
2861 * All exponents in *g are known to be smaller than or equal
2862 * to those in "c".
2864 * That is, if *g is equal to
2866 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2868 * and "c" is equal to
2870 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2872 * then update *g to
2874 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2875 * p_n^{e_n * (e_n = f_n)}
2877 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2878 * neither does the gcd of *g and c / *g.
2879 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2880 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2881 * Dividing *g by this gcd therefore strictly reduces the exponent
2882 * of the prime factors that need to be removed, while leaving the
2883 * other prime factors untouched.
2884 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2885 * removes all undesired factors, without removing any others.
2887 static void remove_incomplete_powers(isl_int *g, isl_int c)
2889 isl_int t;
2891 isl_int_init(t);
2892 for (;;) {
2893 isl_int_divexact(t, c, *g);
2894 isl_int_gcd(t, t, *g);
2895 if (isl_int_is_one(t))
2896 break;
2897 isl_int_divexact(*g, *g, t);
2899 isl_int_clear(t);
2902 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2903 * of the same stride constraints in a compressed space that exploits
2904 * all equalities in the context and the other equalities in "bmap".
2906 * If the stride constraints of "bmap" are of the form
2908 * C(x) + D(y) = 0
2910 * then A is of the form
2912 * B(x') + D(y) = 0
2914 * If any of these constraints involves only a single local variable y,
2915 * then the constraint appears as
2917 * f(x) + m y_i = 0
2919 * in "bmap" and as
2921 * h(x') + m y_i = 0
2923 * in "A".
2925 * Let g be the gcd of m and the coefficients of h.
2926 * Then, in particular, g is a divisor of the coefficients of h and
2928 * f(x) = h(x')
2930 * is known to be a multiple of g.
2931 * If some prime factor in m appears with the same exponent in g,
2932 * then it can be removed from m because f(x) is already known
2933 * to be a multiple of g and therefore in particular of this power
2934 * of the prime factors.
2935 * Prime factors that appear with a smaller exponent in g cannot
2936 * be removed from m.
2937 * Let g' be the divisor of g containing all prime factors that
2938 * appear with the same exponent in m and g, then
2940 * f(x) + m y_i = 0
2942 * can be replaced by
2944 * f(x) + m/g' y_i' = 0
2946 * Note that (if g' != 1) this changes the explicit representation
2947 * of y_i to that of y_i', so the integer division at position i
2948 * is marked unknown and later recomputed by a call to
2949 * isl_basic_map_gauss.
2951 static __isl_give isl_basic_map *reduce_stride_constraints(
2952 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2954 int i;
2955 int total, n_div;
2956 int any = 0;
2957 isl_int gcd;
2959 if (!bmap || !A)
2960 return isl_basic_map_free(bmap);
2962 total = isl_basic_map_dim(bmap, isl_dim_all);
2963 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2964 total -= n_div;
2966 isl_int_init(gcd);
2967 for (i = 0; i < n; ++i) {
2968 int div;
2970 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2971 if (div < 0)
2972 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2973 "equality constraints modified unexpectedly",
2974 goto error);
2975 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
2976 n_div - div - 1) != -1)
2977 continue;
2978 if (isl_mat_row_gcd(A, i, &gcd) < 0)
2979 goto error;
2980 if (isl_int_is_one(gcd))
2981 continue;
2982 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2983 if (isl_int_is_one(gcd))
2984 continue;
2985 isl_int_divexact(bmap->eq[i][1 + total + div],
2986 bmap->eq[i][1 + total + div], gcd);
2987 bmap = isl_basic_map_mark_div_unknown(bmap, div);
2988 if (!bmap)
2989 goto error;
2990 any = 1;
2992 isl_int_clear(gcd);
2994 if (any)
2995 bmap = isl_basic_map_gauss(bmap, NULL);
2997 return bmap;
2998 error:
2999 isl_int_clear(gcd);
3000 isl_basic_map_free(bmap);
3001 return NULL;
3004 /* Simplify the stride constraints in "bmap" based on
3005 * the remaining equality constraints in "bmap" and all equality
3006 * constraints in "context".
3007 * Only do this if both "bmap" and "context" have stride constraints.
3009 * First extract a copy of the stride constraints in "bmap" in a compressed
3010 * space exploiting all the other equality constraints and then
3011 * use this compressed copy to simplify the original stride constraints.
3013 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3014 __isl_keep isl_basic_map *context)
3016 int bmap_n_eq, context_n_eq;
3017 isl_mat *A;
3019 if (!bmap || !context)
3020 return isl_basic_map_free(bmap);
3022 bmap_n_eq = n_div_eq(bmap);
3023 context_n_eq = n_div_eq(context);
3025 if (bmap_n_eq < 0 || context_n_eq < 0)
3026 return isl_basic_map_free(bmap);
3027 if (bmap_n_eq == 0 || context_n_eq == 0)
3028 return bmap;
3030 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3031 context, context_n_eq);
3032 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3034 isl_mat_free(A);
3036 return bmap;
3039 /* Return a basic map that has the same intersection with "context" as "bmap"
3040 * and that is as "simple" as possible.
3042 * The core computation is performed on the pure constraints.
3043 * When we add back the meaning of the integer divisions, we need
3044 * to (re)introduce the div constraints. If we happen to have
3045 * discovered that some of these integer divisions are equal to
3046 * some affine combination of other variables, then these div
3047 * constraints may end up getting simplified in terms of the equalities,
3048 * resulting in extra inequalities on the other variables that
3049 * may have been removed already or that may not even have been
3050 * part of the input. We try and remove those constraints of
3051 * this form that are most obviously redundant with respect to
3052 * the context. We also remove those div constraints that are
3053 * redundant with respect to the other constraints in the result.
3055 * The stride constraints among the equality constraints in "bmap" are
3056 * also simplified with respecting to the other equality constraints
3057 * in "bmap" and with respect to all equality constraints in "context".
3059 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3060 __isl_take isl_basic_map *context)
3062 isl_basic_set *bset, *eq;
3063 isl_basic_map *eq_bmap;
3064 unsigned total, n_div, extra, n_eq, n_ineq;
3066 if (!bmap || !context)
3067 goto error;
3069 if (isl_basic_map_plain_is_universe(bmap)) {
3070 isl_basic_map_free(context);
3071 return bmap;
3073 if (isl_basic_map_plain_is_empty(context)) {
3074 isl_space *space = isl_basic_map_get_space(bmap);
3075 isl_basic_map_free(bmap);
3076 isl_basic_map_free(context);
3077 return isl_basic_map_universe(space);
3079 if (isl_basic_map_plain_is_empty(bmap)) {
3080 isl_basic_map_free(context);
3081 return bmap;
3084 bmap = isl_basic_map_remove_redundancies(bmap);
3085 context = isl_basic_map_remove_redundancies(context);
3086 context = isl_basic_map_align_divs(context, bmap);
3087 if (!context)
3088 goto error;
3090 n_div = isl_basic_map_dim(context, isl_dim_div);
3091 total = isl_basic_map_dim(bmap, isl_dim_all);
3092 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3094 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3095 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3096 bset = uset_gist(bset,
3097 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3098 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3100 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3101 isl_basic_set_plain_is_empty(bset)) {
3102 isl_basic_map_free(context);
3103 return isl_basic_map_overlying_set(bset, bmap);
3106 n_eq = bset->n_eq;
3107 n_ineq = bset->n_ineq;
3108 eq = isl_basic_set_copy(bset);
3109 eq = isl_basic_set_cow(eq);
3110 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3111 eq = isl_basic_set_free(eq);
3112 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3113 bset = isl_basic_set_free(bset);
3115 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3116 eq_bmap = gist_strides(eq_bmap, context);
3117 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3118 bmap = isl_basic_map_overlying_set(bset, bmap);
3119 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3120 bmap = isl_basic_map_remove_redundancies(bmap);
3122 return bmap;
3123 error:
3124 isl_basic_map_free(bmap);
3125 isl_basic_map_free(context);
3126 return NULL;
3130 * Assumes context has no implicit divs.
3132 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3133 __isl_take isl_basic_map *context)
3135 int i;
3137 if (!map || !context)
3138 goto error;
3140 if (isl_basic_map_plain_is_empty(context)) {
3141 isl_space *space = isl_map_get_space(map);
3142 isl_map_free(map);
3143 isl_basic_map_free(context);
3144 return isl_map_universe(space);
3147 context = isl_basic_map_remove_redundancies(context);
3148 map = isl_map_cow(map);
3149 if (!map || !context)
3150 goto error;
3151 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3152 map = isl_map_compute_divs(map);
3153 if (!map)
3154 goto error;
3155 for (i = map->n - 1; i >= 0; --i) {
3156 map->p[i] = isl_basic_map_gist(map->p[i],
3157 isl_basic_map_copy(context));
3158 if (!map->p[i])
3159 goto error;
3160 if (isl_basic_map_plain_is_empty(map->p[i])) {
3161 isl_basic_map_free(map->p[i]);
3162 if (i != map->n - 1)
3163 map->p[i] = map->p[map->n - 1];
3164 map->n--;
3167 isl_basic_map_free(context);
3168 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3169 return map;
3170 error:
3171 isl_map_free(map);
3172 isl_basic_map_free(context);
3173 return NULL;
3176 /* Drop all inequalities from "bmap" that also appear in "context".
3177 * "context" is assumed to have only known local variables and
3178 * the initial local variables of "bmap" are assumed to be the same
3179 * as those of "context".
3180 * The constraints of both "bmap" and "context" are assumed
3181 * to have been sorted using isl_basic_map_sort_constraints.
3183 * Run through the inequality constraints of "bmap" and "context"
3184 * in sorted order.
3185 * If a constraint of "bmap" involves variables not in "context",
3186 * then it cannot appear in "context".
3187 * If a matching constraint is found, it is removed from "bmap".
3189 static __isl_give isl_basic_map *drop_inequalities(
3190 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3192 int i1, i2;
3193 unsigned total, extra;
3195 if (!bmap || !context)
3196 return isl_basic_map_free(bmap);
3198 total = isl_basic_map_total_dim(context);
3199 extra = isl_basic_map_total_dim(bmap) - total;
3201 i1 = bmap->n_ineq - 1;
3202 i2 = context->n_ineq - 1;
3203 while (bmap && i1 >= 0 && i2 >= 0) {
3204 int cmp;
3206 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3207 extra) != -1) {
3208 --i1;
3209 continue;
3211 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3212 context->ineq[i2]);
3213 if (cmp < 0) {
3214 --i2;
3215 continue;
3217 if (cmp > 0) {
3218 --i1;
3219 continue;
3221 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3222 bmap = isl_basic_map_cow(bmap);
3223 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3224 bmap = isl_basic_map_free(bmap);
3226 --i1;
3227 --i2;
3230 return bmap;
3233 /* Drop all equalities from "bmap" that also appear in "context".
3234 * "context" is assumed to have only known local variables and
3235 * the initial local variables of "bmap" are assumed to be the same
3236 * as those of "context".
3238 * Run through the equality constraints of "bmap" and "context"
3239 * in sorted order.
3240 * If a constraint of "bmap" involves variables not in "context",
3241 * then it cannot appear in "context".
3242 * If a matching constraint is found, it is removed from "bmap".
3244 static __isl_give isl_basic_map *drop_equalities(
3245 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3247 int i1, i2;
3248 unsigned total, extra;
3250 if (!bmap || !context)
3251 return isl_basic_map_free(bmap);
3253 total = isl_basic_map_total_dim(context);
3254 extra = isl_basic_map_total_dim(bmap) - total;
3256 i1 = bmap->n_eq - 1;
3257 i2 = context->n_eq - 1;
3259 while (bmap && i1 >= 0 && i2 >= 0) {
3260 int last1, last2;
3262 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3263 extra) != -1)
3264 break;
3265 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3266 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3267 if (last1 > last2) {
3268 --i2;
3269 continue;
3271 if (last1 < last2) {
3272 --i1;
3273 continue;
3275 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3276 bmap = isl_basic_map_cow(bmap);
3277 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3278 bmap = isl_basic_map_free(bmap);
3280 --i1;
3281 --i2;
3284 return bmap;
3287 /* Remove the constraints in "context" from "bmap".
3288 * "context" is assumed to have explicit representations
3289 * for all local variables.
3291 * First align the divs of "bmap" to those of "context" and
3292 * sort the constraints. Then drop all constraints from "bmap"
3293 * that appear in "context".
3295 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3296 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3298 isl_bool done, known;
3300 done = isl_basic_map_plain_is_universe(context);
3301 if (done == isl_bool_false)
3302 done = isl_basic_map_plain_is_universe(bmap);
3303 if (done == isl_bool_false)
3304 done = isl_basic_map_plain_is_empty(context);
3305 if (done == isl_bool_false)
3306 done = isl_basic_map_plain_is_empty(bmap);
3307 if (done < 0)
3308 goto error;
3309 if (done) {
3310 isl_basic_map_free(context);
3311 return bmap;
3313 known = isl_basic_map_divs_known(context);
3314 if (known < 0)
3315 goto error;
3316 if (!known)
3317 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3318 "context has unknown divs", goto error);
3320 bmap = isl_basic_map_align_divs(bmap, context);
3321 bmap = isl_basic_map_gauss(bmap, NULL);
3322 bmap = isl_basic_map_sort_constraints(bmap);
3323 context = isl_basic_map_sort_constraints(context);
3325 bmap = drop_inequalities(bmap, context);
3326 bmap = drop_equalities(bmap, context);
3328 isl_basic_map_free(context);
3329 bmap = isl_basic_map_finalize(bmap);
3330 return bmap;
3331 error:
3332 isl_basic_map_free(bmap);
3333 isl_basic_map_free(context);
3334 return NULL;
3337 /* Replace "map" by the disjunct at position "pos" and free "context".
3339 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3340 int pos, __isl_take isl_basic_map *context)
3342 isl_basic_map *bmap;
3344 bmap = isl_basic_map_copy(map->p[pos]);
3345 isl_map_free(map);
3346 isl_basic_map_free(context);
3347 return isl_map_from_basic_map(bmap);
3350 /* Remove the constraints in "context" from "map".
3351 * If any of the disjuncts in the result turns out to be the universe,
3352 * then return this universe.
3353 * "context" is assumed to have explicit representations
3354 * for all local variables.
3356 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3357 __isl_take isl_basic_map *context)
3359 int i;
3360 isl_bool univ, known;
3362 univ = isl_basic_map_plain_is_universe(context);
3363 if (univ < 0)
3364 goto error;
3365 if (univ) {
3366 isl_basic_map_free(context);
3367 return map;
3369 known = isl_basic_map_divs_known(context);
3370 if (known < 0)
3371 goto error;
3372 if (!known)
3373 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3374 "context has unknown divs", goto error);
3376 map = isl_map_cow(map);
3377 if (!map)
3378 goto error;
3379 for (i = 0; i < map->n; ++i) {
3380 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3381 isl_basic_map_copy(context));
3382 univ = isl_basic_map_plain_is_universe(map->p[i]);
3383 if (univ < 0)
3384 goto error;
3385 if (univ && map->n > 1)
3386 return replace_by_disjunct(map, i, context);
3389 isl_basic_map_free(context);
3390 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3391 if (map->n > 1)
3392 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3393 return map;
3394 error:
3395 isl_map_free(map);
3396 isl_basic_map_free(context);
3397 return NULL;
3400 /* Remove the constraints in "context" from "set".
3401 * If any of the disjuncts in the result turns out to be the universe,
3402 * then return this universe.
3403 * "context" is assumed to have explicit representations
3404 * for all local variables.
3406 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3407 __isl_take isl_basic_set *context)
3409 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3410 bset_to_bmap(context)));
3413 /* Remove the constraints in "context" from "map".
3414 * If any of the disjuncts in the result turns out to be the universe,
3415 * then return this universe.
3416 * "context" is assumed to consist of a single disjunct and
3417 * to have explicit representations for all local variables.
3419 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3420 __isl_take isl_map *context)
3422 isl_basic_map *hull;
3424 hull = isl_map_unshifted_simple_hull(context);
3425 return isl_map_plain_gist_basic_map(map, hull);
3428 /* Replace "map" by a universe map in the same space and free "drop".
3430 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3431 __isl_take isl_map *drop)
3433 isl_map *res;
3435 res = isl_map_universe(isl_map_get_space(map));
3436 isl_map_free(map);
3437 isl_map_free(drop);
3438 return res;
3441 /* Return a map that has the same intersection with "context" as "map"
3442 * and that is as "simple" as possible.
3444 * If "map" is already the universe, then we cannot make it any simpler.
3445 * Similarly, if "context" is the universe, then we cannot exploit it
3446 * to simplify "map"
3447 * If "map" and "context" are identical to each other, then we can
3448 * return the corresponding universe.
3450 * If either "map" or "context" consists of multiple disjuncts,
3451 * then check if "context" happens to be a subset of "map",
3452 * in which case all constraints can be removed.
3453 * In case of multiple disjuncts, the standard procedure
3454 * may not be able to detect that all constraints can be removed.
3456 * If none of these cases apply, we have to work a bit harder.
3457 * During this computation, we make use of a single disjunct context,
3458 * so if the original context consists of more than one disjunct
3459 * then we need to approximate the context by a single disjunct set.
3460 * Simply taking the simple hull may drop constraints that are
3461 * only implicitly available in each disjunct. We therefore also
3462 * look for constraints among those defining "map" that are valid
3463 * for the context. These can then be used to simplify away
3464 * the corresponding constraints in "map".
3466 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3467 __isl_take isl_map *context)
3469 int equal;
3470 int is_universe;
3471 int single_disjunct_map, single_disjunct_context;
3472 isl_bool subset;
3473 isl_basic_map *hull;
3475 is_universe = isl_map_plain_is_universe(map);
3476 if (is_universe >= 0 && !is_universe)
3477 is_universe = isl_map_plain_is_universe(context);
3478 if (is_universe < 0)
3479 goto error;
3480 if (is_universe) {
3481 isl_map_free(context);
3482 return map;
3485 equal = isl_map_plain_is_equal(map, context);
3486 if (equal < 0)
3487 goto error;
3488 if (equal)
3489 return replace_by_universe(map, context);
3491 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3492 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3493 if (!single_disjunct_map || !single_disjunct_context) {
3494 subset = isl_map_is_subset(context, map);
3495 if (subset < 0)
3496 goto error;
3497 if (subset)
3498 return replace_by_universe(map, context);
3501 context = isl_map_compute_divs(context);
3502 if (!context)
3503 goto error;
3504 if (single_disjunct_context) {
3505 hull = isl_map_simple_hull(context);
3506 } else {
3507 isl_ctx *ctx;
3508 isl_map_list *list;
3510 ctx = isl_map_get_ctx(map);
3511 list = isl_map_list_alloc(ctx, 2);
3512 list = isl_map_list_add(list, isl_map_copy(context));
3513 list = isl_map_list_add(list, isl_map_copy(map));
3514 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3515 list);
3517 return isl_map_gist_basic_map(map, hull);
3518 error:
3519 isl_map_free(map);
3520 isl_map_free(context);
3521 return NULL;
3524 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3525 __isl_take isl_map *context)
3527 return isl_map_align_params_map_map_and(map, context, &map_gist);
3530 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3531 struct isl_basic_set *context)
3533 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3534 bset_to_bmap(context)));
3537 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3538 __isl_take isl_basic_set *context)
3540 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3541 bset_to_bmap(context)));
3544 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3545 __isl_take isl_basic_set *context)
3547 isl_space *space = isl_set_get_space(set);
3548 isl_basic_set *dom_context = isl_basic_set_universe(space);
3549 dom_context = isl_basic_set_intersect_params(dom_context, context);
3550 return isl_set_gist_basic_set(set, dom_context);
3553 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3554 __isl_take isl_set *context)
3556 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3559 /* Compute the gist of "bmap" with respect to the constraints "context"
3560 * on the domain.
3562 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3563 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3565 isl_space *space = isl_basic_map_get_space(bmap);
3566 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3568 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3569 return isl_basic_map_gist(bmap, bmap_context);
3572 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3573 __isl_take isl_set *context)
3575 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3576 map_context = isl_map_intersect_domain(map_context, context);
3577 return isl_map_gist(map, map_context);
3580 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3581 __isl_take isl_set *context)
3583 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3584 map_context = isl_map_intersect_range(map_context, context);
3585 return isl_map_gist(map, map_context);
3588 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3589 __isl_take isl_set *context)
3591 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3592 map_context = isl_map_intersect_params(map_context, context);
3593 return isl_map_gist(map, map_context);
3596 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3597 __isl_take isl_set *context)
3599 return isl_map_gist_params(set, context);
3602 /* Quick check to see if two basic maps are disjoint.
3603 * In particular, we reduce the equalities and inequalities of
3604 * one basic map in the context of the equalities of the other
3605 * basic map and check if we get a contradiction.
3607 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3608 __isl_keep isl_basic_map *bmap2)
3610 struct isl_vec *v = NULL;
3611 int *elim = NULL;
3612 unsigned total;
3613 int i;
3615 if (!bmap1 || !bmap2)
3616 return isl_bool_error;
3617 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3618 return isl_bool_error);
3619 if (bmap1->n_div || bmap2->n_div)
3620 return isl_bool_false;
3621 if (!bmap1->n_eq && !bmap2->n_eq)
3622 return isl_bool_false;
3624 total = isl_space_dim(bmap1->dim, isl_dim_all);
3625 if (total == 0)
3626 return isl_bool_false;
3627 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3628 if (!v)
3629 goto error;
3630 elim = isl_alloc_array(bmap1->ctx, int, total);
3631 if (!elim)
3632 goto error;
3633 compute_elimination_index(bmap1, elim);
3634 for (i = 0; i < bmap2->n_eq; ++i) {
3635 int reduced;
3636 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3637 bmap1, elim);
3638 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3639 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3640 goto disjoint;
3642 for (i = 0; i < bmap2->n_ineq; ++i) {
3643 int reduced;
3644 reduced = reduced_using_equalities(v->block.data,
3645 bmap2->ineq[i], bmap1, elim);
3646 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3647 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3648 goto disjoint;
3650 compute_elimination_index(bmap2, elim);
3651 for (i = 0; i < bmap1->n_ineq; ++i) {
3652 int reduced;
3653 reduced = reduced_using_equalities(v->block.data,
3654 bmap1->ineq[i], bmap2, elim);
3655 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3656 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3657 goto disjoint;
3659 isl_vec_free(v);
3660 free(elim);
3661 return isl_bool_false;
3662 disjoint:
3663 isl_vec_free(v);
3664 free(elim);
3665 return isl_bool_true;
3666 error:
3667 isl_vec_free(v);
3668 free(elim);
3669 return isl_bool_error;
3672 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3673 __isl_keep isl_basic_set *bset2)
3675 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3676 bset_to_bmap(bset2));
3679 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3681 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3682 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3683 __isl_keep isl_basic_map *bmap2))
3685 int i, j;
3687 if (!map1 || !map2)
3688 return isl_bool_error;
3690 for (i = 0; i < map1->n; ++i) {
3691 for (j = 0; j < map2->n; ++j) {
3692 isl_bool d = test(map1->p[i], map2->p[j]);
3693 if (d != isl_bool_true)
3694 return d;
3698 return isl_bool_true;
3701 /* Are "map1" and "map2" obviously disjoint, based on information
3702 * that can be derived without looking at the individual basic maps?
3704 * In particular, if one of them is empty or if they live in different spaces
3705 * (ignoring parameters), then they are clearly disjoint.
3707 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3708 __isl_keep isl_map *map2)
3710 isl_bool disjoint;
3711 isl_bool match;
3713 if (!map1 || !map2)
3714 return isl_bool_error;
3716 disjoint = isl_map_plain_is_empty(map1);
3717 if (disjoint < 0 || disjoint)
3718 return disjoint;
3720 disjoint = isl_map_plain_is_empty(map2);
3721 if (disjoint < 0 || disjoint)
3722 return disjoint;
3724 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3725 map2->dim, isl_dim_in);
3726 if (match < 0 || !match)
3727 return match < 0 ? isl_bool_error : isl_bool_true;
3729 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3730 map2->dim, isl_dim_out);
3731 if (match < 0 || !match)
3732 return match < 0 ? isl_bool_error : isl_bool_true;
3734 return isl_bool_false;
3737 /* Are "map1" and "map2" obviously disjoint?
3739 * If one of them is empty or if they live in different spaces (ignoring
3740 * parameters), then they are clearly disjoint.
3741 * This is checked by isl_map_plain_is_disjoint_global.
3743 * If they have different parameters, then we skip any further tests.
3745 * If they are obviously equal, but not obviously empty, then we will
3746 * not be able to detect if they are disjoint.
3748 * Otherwise we check if each basic map in "map1" is obviously disjoint
3749 * from each basic map in "map2".
3751 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3752 __isl_keep isl_map *map2)
3754 isl_bool disjoint;
3755 isl_bool intersect;
3756 isl_bool match;
3758 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3759 if (disjoint < 0 || disjoint)
3760 return disjoint;
3762 match = isl_map_has_equal_params(map1, map2);
3763 if (match < 0 || !match)
3764 return match < 0 ? isl_bool_error : isl_bool_false;
3766 intersect = isl_map_plain_is_equal(map1, map2);
3767 if (intersect < 0 || intersect)
3768 return intersect < 0 ? isl_bool_error : isl_bool_false;
3770 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3773 /* Are "map1" and "map2" disjoint?
3774 * The parameters are assumed to have been aligned.
3776 * In particular, check whether all pairs of basic maps are disjoint.
3778 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3779 __isl_keep isl_map *map2)
3781 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3784 /* Are "map1" and "map2" disjoint?
3786 * They are disjoint if they are "obviously disjoint" or if one of them
3787 * is empty. Otherwise, they are not disjoint if one of them is universal.
3788 * If the two inputs are (obviously) equal and not empty, then they are
3789 * not disjoint.
3790 * If none of these cases apply, then check if all pairs of basic maps
3791 * are disjoint after aligning the parameters.
3793 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3795 isl_bool disjoint;
3796 isl_bool intersect;
3798 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3799 if (disjoint < 0 || disjoint)
3800 return disjoint;
3802 disjoint = isl_map_is_empty(map1);
3803 if (disjoint < 0 || disjoint)
3804 return disjoint;
3806 disjoint = isl_map_is_empty(map2);
3807 if (disjoint < 0 || disjoint)
3808 return disjoint;
3810 intersect = isl_map_plain_is_universe(map1);
3811 if (intersect < 0 || intersect)
3812 return intersect < 0 ? isl_bool_error : isl_bool_false;
3814 intersect = isl_map_plain_is_universe(map2);
3815 if (intersect < 0 || intersect)
3816 return intersect < 0 ? isl_bool_error : isl_bool_false;
3818 intersect = isl_map_plain_is_equal(map1, map2);
3819 if (intersect < 0 || intersect)
3820 return isl_bool_not(intersect);
3822 return isl_map_align_params_map_map_and_test(map1, map2,
3823 &isl_map_is_disjoint_aligned);
3826 /* Are "bmap1" and "bmap2" disjoint?
3828 * They are disjoint if they are "obviously disjoint" or if one of them
3829 * is empty. Otherwise, they are not disjoint if one of them is universal.
3830 * If none of these cases apply, we compute the intersection and see if
3831 * the result is empty.
3833 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3834 __isl_keep isl_basic_map *bmap2)
3836 isl_bool disjoint;
3837 isl_bool intersect;
3838 isl_basic_map *test;
3840 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3841 if (disjoint < 0 || disjoint)
3842 return disjoint;
3844 disjoint = isl_basic_map_is_empty(bmap1);
3845 if (disjoint < 0 || disjoint)
3846 return disjoint;
3848 disjoint = isl_basic_map_is_empty(bmap2);
3849 if (disjoint < 0 || disjoint)
3850 return disjoint;
3852 intersect = isl_basic_map_plain_is_universe(bmap1);
3853 if (intersect < 0 || intersect)
3854 return intersect < 0 ? isl_bool_error : isl_bool_false;
3856 intersect = isl_basic_map_plain_is_universe(bmap2);
3857 if (intersect < 0 || intersect)
3858 return intersect < 0 ? isl_bool_error : isl_bool_false;
3860 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3861 isl_basic_map_copy(bmap2));
3862 disjoint = isl_basic_map_is_empty(test);
3863 isl_basic_map_free(test);
3865 return disjoint;
3868 /* Are "bset1" and "bset2" disjoint?
3870 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3871 __isl_keep isl_basic_set *bset2)
3873 return isl_basic_map_is_disjoint(bset1, bset2);
3876 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3877 __isl_keep isl_set *set2)
3879 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3882 /* Are "set1" and "set2" disjoint?
3884 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3886 return isl_map_is_disjoint(set1, set2);
3889 /* Is "v" equal to 0, 1 or -1?
3891 static int is_zero_or_one(isl_int v)
3893 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3896 /* Are the "n" coefficients starting at "first" of inequality constraints
3897 * "i" and "j" of "bmap" opposite to each other?
3899 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
3900 int first, int n)
3902 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
3905 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3906 * apart from the constant term?
3908 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
3910 unsigned total;
3912 total = isl_basic_map_dim(bmap, isl_dim_all);
3913 return is_opposite_part(bmap, i, j, 1, total);
3916 /* Check if we can combine a given div with lower bound l and upper
3917 * bound u with some other div and if so return that other div.
3918 * Otherwise, return a position beyond the integer divisions.
3919 * Return -1 on error.
3921 * We first check that
3922 * - the bounds are opposites of each other (except for the constant
3923 * term)
3924 * - the bounds do not reference any other div
3925 * - no div is defined in terms of this div
3927 * Let m be the size of the range allowed on the div by the bounds.
3928 * That is, the bounds are of the form
3930 * e <= a <= e + m - 1
3932 * with e some expression in the other variables.
3933 * We look for another div b such that no third div is defined in terms
3934 * of this second div b and such that in any constraint that contains
3935 * a (except for the given lower and upper bound), also contains b
3936 * with a coefficient that is m times that of b.
3937 * That is, all constraints (except for the lower and upper bound)
3938 * are of the form
3940 * e + f (a + m b) >= 0
3942 * Furthermore, in the constraints that only contain b, the coefficient
3943 * of b should be equal to 1 or -1.
3944 * If so, we return b so that "a + m b" can be replaced by
3945 * a single div "c = a + m b".
3947 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
3948 unsigned div, unsigned l, unsigned u)
3950 int i, j;
3951 unsigned n_div;
3952 int v_div;
3953 int coalesce;
3954 isl_bool opp;
3956 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3957 if (n_div <= 1)
3958 return n_div;
3959 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3960 if (v_div < 0)
3961 return -1;
3962 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
3963 return n_div;
3964 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
3965 n_div - div - 1) != -1)
3966 return n_div;
3967 opp = is_opposite(bmap, l, u);
3968 if (opp < 0 || !opp)
3969 return opp < 0 ? -1 : n_div;
3971 for (i = 0; i < n_div; ++i) {
3972 if (isl_int_is_zero(bmap->div[i][0]))
3973 continue;
3974 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
3975 return n_div;
3978 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3979 if (isl_int_is_neg(bmap->ineq[l][0])) {
3980 isl_int_sub(bmap->ineq[l][0],
3981 bmap->ineq[l][0], bmap->ineq[u][0]);
3982 bmap = isl_basic_map_copy(bmap);
3983 bmap = isl_basic_map_set_to_empty(bmap);
3984 isl_basic_map_free(bmap);
3985 return n_div;
3987 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3988 coalesce = n_div;
3989 for (i = 0; i < n_div; ++i) {
3990 if (i == div)
3991 continue;
3992 if (!pairs[i])
3993 continue;
3994 for (j = 0; j < n_div; ++j) {
3995 if (isl_int_is_zero(bmap->div[j][0]))
3996 continue;
3997 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
3998 break;
4000 if (j < n_div)
4001 continue;
4002 for (j = 0; j < bmap->n_ineq; ++j) {
4003 int valid;
4004 if (j == l || j == u)
4005 continue;
4006 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4007 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4008 continue;
4009 break;
4011 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4012 break;
4013 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4014 bmap->ineq[j][1 + v_div + div],
4015 bmap->ineq[l][0]);
4016 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4017 bmap->ineq[j][1 + v_div + i]);
4018 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4019 bmap->ineq[j][1 + v_div + div],
4020 bmap->ineq[l][0]);
4021 if (!valid)
4022 break;
4024 if (j < bmap->n_ineq)
4025 continue;
4026 coalesce = i;
4027 break;
4029 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4030 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4031 return coalesce;
4034 /* Internal data structure used during the construction and/or evaluation of
4035 * an inequality that ensures that a pair of bounds always allows
4036 * for an integer value.
4038 * "tab" is the tableau in which the inequality is evaluated. It may
4039 * be NULL until it is actually needed.
4040 * "v" contains the inequality coefficients.
4041 * "g", "fl" and "fu" are temporary scalars used during the construction and
4042 * evaluation.
4044 struct test_ineq_data {
4045 struct isl_tab *tab;
4046 isl_vec *v;
4047 isl_int g;
4048 isl_int fl;
4049 isl_int fu;
4052 /* Free all the memory allocated by the fields of "data".
4054 static void test_ineq_data_clear(struct test_ineq_data *data)
4056 isl_tab_free(data->tab);
4057 isl_vec_free(data->v);
4058 isl_int_clear(data->g);
4059 isl_int_clear(data->fl);
4060 isl_int_clear(data->fu);
4063 /* Is the inequality stored in data->v satisfied by "bmap"?
4064 * That is, does it only attain non-negative values?
4065 * data->tab is a tableau corresponding to "bmap".
4067 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4068 struct test_ineq_data *data)
4070 isl_ctx *ctx;
4071 enum isl_lp_result res;
4073 ctx = isl_basic_map_get_ctx(bmap);
4074 if (!data->tab)
4075 data->tab = isl_tab_from_basic_map(bmap, 0);
4076 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4077 if (res == isl_lp_error)
4078 return isl_bool_error;
4079 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4082 /* Given a lower and an upper bound on div i, do they always allow
4083 * for an integer value of the given div?
4084 * Determine this property by constructing an inequality
4085 * such that the property is guaranteed when the inequality is nonnegative.
4086 * The lower bound is inequality l, while the upper bound is inequality u.
4087 * The constructed inequality is stored in data->v.
4089 * Let the upper bound be
4091 * -n_u a + e_u >= 0
4093 * and the lower bound
4095 * n_l a + e_l >= 0
4097 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4098 * We have
4100 * - f_u e_l <= f_u f_l g a <= f_l e_u
4102 * Since all variables are integer valued, this is equivalent to
4104 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4106 * If this interval is at least f_u f_l g, then it contains at least
4107 * one integer value for a.
4108 * That is, the test constraint is
4110 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4112 * or
4114 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4116 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4117 * then the constraint can be scaled down by a factor g',
4118 * with the constant term replaced by
4119 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4120 * Note that the result of applying Fourier-Motzkin to this pair
4121 * of constraints is
4123 * f_l e_u + f_u e_l >= 0
4125 * If the constant term of the scaled down version of this constraint,
4126 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4127 * term of the scaled down test constraint, then the test constraint
4128 * is known to hold and no explicit evaluation is required.
4129 * This is essentially the Omega test.
4131 * If the test constraint consists of only a constant term, then
4132 * it is sufficient to look at the sign of this constant term.
4134 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4135 int l, int u, struct test_ineq_data *data)
4137 unsigned offset, n_div;
4138 offset = isl_basic_map_offset(bmap, isl_dim_div);
4139 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4141 isl_int_gcd(data->g,
4142 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4143 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4144 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4145 isl_int_neg(data->fu, data->fu);
4146 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4147 data->fu, bmap->ineq[l], offset + n_div);
4148 isl_int_mul(data->g, data->g, data->fl);
4149 isl_int_mul(data->g, data->g, data->fu);
4150 isl_int_sub(data->g, data->g, data->fl);
4151 isl_int_sub(data->g, data->g, data->fu);
4152 isl_int_add_ui(data->g, data->g, 1);
4153 isl_int_sub(data->fl, data->v->el[0], data->g);
4155 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4156 if (isl_int_is_zero(data->g))
4157 return isl_int_is_nonneg(data->fl);
4158 if (isl_int_is_one(data->g)) {
4159 isl_int_set(data->v->el[0], data->fl);
4160 return test_ineq_is_satisfied(bmap, data);
4162 isl_int_fdiv_q(data->fl, data->fl, data->g);
4163 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4164 if (isl_int_eq(data->fl, data->v->el[0]))
4165 return isl_bool_true;
4166 isl_int_set(data->v->el[0], data->fl);
4167 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4168 offset - 1 + n_div);
4170 return test_ineq_is_satisfied(bmap, data);
4173 /* Remove more kinds of divs that are not strictly needed.
4174 * In particular, if all pairs of lower and upper bounds on a div
4175 * are such that they allow at least one integer value of the div,
4176 * then we can eliminate the div using Fourier-Motzkin without
4177 * introducing any spurious solutions.
4179 * If at least one of the two constraints has a unit coefficient for the div,
4180 * then the presence of such a value is guaranteed so there is no need to check.
4181 * In particular, the value attained by the bound with unit coefficient
4182 * can serve as this intermediate value.
4184 static __isl_give isl_basic_map *drop_more_redundant_divs(
4185 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4187 isl_ctx *ctx;
4188 struct test_ineq_data data = { NULL, NULL };
4189 unsigned off, n_div;
4190 int remove = -1;
4192 isl_int_init(data.g);
4193 isl_int_init(data.fl);
4194 isl_int_init(data.fu);
4196 if (!bmap)
4197 goto error;
4199 ctx = isl_basic_map_get_ctx(bmap);
4200 off = isl_basic_map_offset(bmap, isl_dim_div);
4201 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4202 data.v = isl_vec_alloc(ctx, off + n_div);
4203 if (!data.v)
4204 goto error;
4206 while (n > 0) {
4207 int i, l, u;
4208 int best = -1;
4209 isl_bool has_int;
4211 for (i = 0; i < n_div; ++i) {
4212 if (!pairs[i])
4213 continue;
4214 if (best >= 0 && pairs[best] <= pairs[i])
4215 continue;
4216 best = i;
4219 i = best;
4220 for (l = 0; l < bmap->n_ineq; ++l) {
4221 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4222 continue;
4223 if (isl_int_is_one(bmap->ineq[l][off + i]))
4224 continue;
4225 for (u = 0; u < bmap->n_ineq; ++u) {
4226 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4227 continue;
4228 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4229 continue;
4230 has_int = int_between_bounds(bmap, i, l, u,
4231 &data);
4232 if (has_int < 0)
4233 goto error;
4234 if (data.tab && data.tab->empty)
4235 break;
4236 if (!has_int)
4237 break;
4239 if (u < bmap->n_ineq)
4240 break;
4242 if (data.tab && data.tab->empty) {
4243 bmap = isl_basic_map_set_to_empty(bmap);
4244 break;
4246 if (l == bmap->n_ineq) {
4247 remove = i;
4248 break;
4250 pairs[i] = 0;
4251 --n;
4254 test_ineq_data_clear(&data);
4256 free(pairs);
4258 if (remove < 0)
4259 return bmap;
4261 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4262 return isl_basic_map_drop_redundant_divs(bmap);
4263 error:
4264 free(pairs);
4265 isl_basic_map_free(bmap);
4266 test_ineq_data_clear(&data);
4267 return NULL;
4270 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4271 * and the upper bound u, div1 always occurs together with div2 in the form
4272 * (div1 + m div2), where m is the constant range on the variable div1
4273 * allowed by l and u, replace the pair div1 and div2 by a single
4274 * div that is equal to div1 + m div2.
4276 * The new div will appear in the location that contains div2.
4277 * We need to modify all constraints that contain
4278 * div2 = (div - div1) / m
4279 * The coefficient of div2 is known to be equal to 1 or -1.
4280 * (If a constraint does not contain div2, it will also not contain div1.)
4281 * If the constraint also contains div1, then we know they appear
4282 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4283 * i.e., the coefficient of div is f.
4285 * Otherwise, we first need to introduce div1 into the constraint.
4286 * Let l be
4288 * div1 + f >=0
4290 * and u
4292 * -div1 + f' >= 0
4294 * A lower bound on div2
4296 * div2 + t >= 0
4298 * can be replaced by
4300 * m div2 + div1 + m t + f >= 0
4302 * An upper bound
4304 * -div2 + t >= 0
4306 * can be replaced by
4308 * -(m div2 + div1) + m t + f' >= 0
4310 * These constraint are those that we would obtain from eliminating
4311 * div1 using Fourier-Motzkin.
4313 * After all constraints have been modified, we drop the lower and upper
4314 * bound and then drop div1.
4315 * Since the new div is only placed in the same location that used
4316 * to store div2, but otherwise has a different meaning, any possible
4317 * explicit representation of the original div2 is removed.
4319 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4320 unsigned div1, unsigned div2, unsigned l, unsigned u)
4322 isl_ctx *ctx;
4323 isl_int m;
4324 int v_div;
4325 unsigned total;
4326 int i;
4328 ctx = isl_basic_map_get_ctx(bmap);
4330 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4331 if (v_div < 0)
4332 return isl_basic_map_free(bmap);
4333 total = 1 + v_div + bmap->n_div;
4335 isl_int_init(m);
4336 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4337 isl_int_add_ui(m, m, 1);
4339 for (i = 0; i < bmap->n_ineq; ++i) {
4340 if (i == l || i == u)
4341 continue;
4342 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4343 continue;
4344 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4345 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4346 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4347 ctx->one, bmap->ineq[l], total);
4348 else
4349 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4350 ctx->one, bmap->ineq[u], total);
4352 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4353 bmap->ineq[i][1 + v_div + div1]);
4354 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4357 isl_int_clear(m);
4358 if (l > u) {
4359 isl_basic_map_drop_inequality(bmap, l);
4360 isl_basic_map_drop_inequality(bmap, u);
4361 } else {
4362 isl_basic_map_drop_inequality(bmap, u);
4363 isl_basic_map_drop_inequality(bmap, l);
4365 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4366 bmap = isl_basic_map_drop_div(bmap, div1);
4367 return bmap;
4370 /* First check if we can coalesce any pair of divs and
4371 * then continue with dropping more redundant divs.
4373 * We loop over all pairs of lower and upper bounds on a div
4374 * with coefficient 1 and -1, respectively, check if there
4375 * is any other div "c" with which we can coalesce the div
4376 * and if so, perform the coalescing.
4378 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4379 __isl_take isl_basic_map *bmap, int *pairs, int n)
4381 int i, l, u;
4382 int v_div;
4383 unsigned n_div;
4385 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4386 if (v_div < 0)
4387 return isl_basic_map_free(bmap);
4389 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4390 for (i = 0; i < n_div; ++i) {
4391 if (!pairs[i])
4392 continue;
4393 for (l = 0; l < bmap->n_ineq; ++l) {
4394 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4395 continue;
4396 for (u = 0; u < bmap->n_ineq; ++u) {
4397 int c;
4399 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4400 continue;
4401 c = div_find_coalesce(bmap, pairs, i, l, u);
4402 if (c < 0)
4403 goto error;
4404 if (c >= n_div)
4405 continue;
4406 free(pairs);
4407 bmap = coalesce_divs(bmap, i, c, l, u);
4408 return isl_basic_map_drop_redundant_divs(bmap);
4413 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4414 free(pairs);
4415 return bmap;
4418 return drop_more_redundant_divs(bmap, pairs, n);
4419 error:
4420 free(pairs);
4421 isl_basic_map_free(bmap);
4422 return NULL;
4425 /* Are the "n" coefficients starting at "first" of inequality constraints
4426 * "i" and "j" of "bmap" equal to each other?
4428 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4429 int first, int n)
4431 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4434 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4435 * apart from the constant term and the coefficient at position "pos"?
4437 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4438 int pos)
4440 unsigned total;
4442 total = isl_basic_map_dim(bmap, isl_dim_all);
4443 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4444 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4447 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4448 * apart from the constant term and the coefficient at position "pos"?
4450 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4451 int pos)
4453 unsigned total;
4455 total = isl_basic_map_dim(bmap, isl_dim_all);
4456 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4457 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4460 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4461 * been modified, simplying it if "simplify" is set.
4462 * Free the temporary data structure "pairs" that was associated
4463 * to the old version of "bmap".
4465 static __isl_give isl_basic_map *drop_redundant_divs_again(
4466 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4468 if (simplify)
4469 bmap = isl_basic_map_simplify(bmap);
4470 free(pairs);
4471 return isl_basic_map_drop_redundant_divs(bmap);
4474 /* Is "div" the single unknown existentially quantified variable
4475 * in inequality constraint "ineq" of "bmap"?
4476 * "div" is known to have a non-zero coefficient in "ineq".
4478 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4479 int div)
4481 int i;
4482 unsigned n_div, o_div;
4483 isl_bool known;
4485 known = isl_basic_map_div_is_known(bmap, div);
4486 if (known < 0 || known)
4487 return isl_bool_not(known);
4488 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4489 if (n_div == 1)
4490 return isl_bool_true;
4491 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4492 for (i = 0; i < n_div; ++i) {
4493 isl_bool known;
4495 if (i == div)
4496 continue;
4497 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4498 continue;
4499 known = isl_basic_map_div_is_known(bmap, i);
4500 if (known < 0 || !known)
4501 return known;
4504 return isl_bool_true;
4507 /* Does integer division "div" have coefficient 1 in inequality constraint
4508 * "ineq" of "map"?
4510 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4512 unsigned o_div;
4514 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4515 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4516 return isl_bool_true;
4518 return isl_bool_false;
4521 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4522 * then try and drop redundant divs again,
4523 * freeing the temporary data structure "pairs" that was associated
4524 * to the old version of "bmap".
4526 static __isl_give isl_basic_map *set_eq_and_try_again(
4527 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4529 bmap = isl_basic_map_cow(bmap);
4530 isl_basic_map_inequality_to_equality(bmap, ineq);
4531 return drop_redundant_divs_again(bmap, pairs, 1);
4534 /* Drop the integer division at position "div", along with the two
4535 * inequality constraints "ineq1" and "ineq2" in which it appears
4536 * from "bmap" and then try and drop redundant divs again,
4537 * freeing the temporary data structure "pairs" that was associated
4538 * to the old version of "bmap".
4540 static __isl_give isl_basic_map *drop_div_and_try_again(
4541 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4542 __isl_take int *pairs)
4544 if (ineq1 > ineq2) {
4545 isl_basic_map_drop_inequality(bmap, ineq1);
4546 isl_basic_map_drop_inequality(bmap, ineq2);
4547 } else {
4548 isl_basic_map_drop_inequality(bmap, ineq2);
4549 isl_basic_map_drop_inequality(bmap, ineq1);
4551 bmap = isl_basic_map_drop_div(bmap, div);
4552 return drop_redundant_divs_again(bmap, pairs, 0);
4555 /* Given two inequality constraints
4557 * f(x) + n d + c >= 0, (ineq)
4559 * with d the variable at position "pos", and
4561 * f(x) + c0 >= 0, (lower)
4563 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4564 * determined by the first constraint.
4565 * That is, store
4567 * ceil((c0 - c)/n)
4569 * in *l.
4571 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4572 int ineq, int lower, int pos, isl_int *l)
4574 isl_int_neg(*l, bmap->ineq[ineq][0]);
4575 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4576 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4579 /* Given two inequality constraints
4581 * f(x) + n d + c >= 0, (ineq)
4583 * with d the variable at position "pos", and
4585 * -f(x) - c0 >= 0, (upper)
4587 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4588 * determined by the first constraint.
4589 * That is, store
4591 * ceil((-c1 - c)/n)
4593 * in *u.
4595 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4596 int ineq, int upper, int pos, isl_int *u)
4598 isl_int_neg(*u, bmap->ineq[ineq][0]);
4599 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4600 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4603 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4604 * does the corresponding lower bound have a fixed value in "bmap"?
4606 * In particular, "ineq" is of the form
4608 * f(x) + n d + c >= 0
4610 * with n > 0, c the constant term and
4611 * d the existentially quantified variable "div".
4612 * That is, the lower bound is
4614 * ceil((-f(x) - c)/n)
4616 * Look for a pair of constraints
4618 * f(x) + c0 >= 0
4619 * -f(x) + c1 >= 0
4621 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4622 * That is, check that
4624 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4626 * If so, return the index of inequality f(x) + c0 >= 0.
4627 * Otherwise, return bmap->n_ineq.
4628 * Return -1 on error.
4630 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4632 int i;
4633 int lower = -1, upper = -1;
4634 unsigned o_div;
4635 isl_int l, u;
4636 int equal;
4638 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4639 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4640 isl_bool par, opp;
4642 if (i == ineq)
4643 continue;
4644 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4645 continue;
4646 par = isl_bool_false;
4647 if (lower < 0)
4648 par = is_parallel_except(bmap, ineq, i, o_div + div);
4649 if (par < 0)
4650 return -1;
4651 if (par) {
4652 lower = i;
4653 continue;
4655 opp = isl_bool_false;
4656 if (upper < 0)
4657 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4658 if (opp < 0)
4659 return -1;
4660 if (opp)
4661 upper = i;
4664 if (lower < 0 || upper < 0)
4665 return bmap->n_ineq;
4667 isl_int_init(l);
4668 isl_int_init(u);
4670 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4671 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4673 equal = isl_int_eq(l, u);
4675 isl_int_clear(l);
4676 isl_int_clear(u);
4678 return equal ? lower : bmap->n_ineq;
4681 /* Given a lower bound constraint "ineq" on the existentially quantified
4682 * variable "div", such that the corresponding lower bound has
4683 * a fixed value in "bmap", assign this fixed value to the variable and
4684 * then try and drop redundant divs again,
4685 * freeing the temporary data structure "pairs" that was associated
4686 * to the old version of "bmap".
4687 * "lower" determines the constant value for the lower bound.
4689 * In particular, "ineq" is of the form
4691 * f(x) + n d + c >= 0,
4693 * while "lower" is of the form
4695 * f(x) + c0 >= 0
4697 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4698 * is ceil((c0 - c)/n).
4700 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4701 int div, int ineq, int lower, int *pairs)
4703 isl_int c;
4704 unsigned o_div;
4706 isl_int_init(c);
4708 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4709 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4710 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4711 free(pairs);
4713 isl_int_clear(c);
4715 return isl_basic_map_drop_redundant_divs(bmap);
4718 /* Remove divs that are not strictly needed based on the inequality
4719 * constraints.
4720 * In particular, if a div only occurs positively (or negatively)
4721 * in constraints, then it can simply be dropped.
4722 * Also, if a div occurs in only two constraints and if moreover
4723 * those two constraints are opposite to each other, except for the constant
4724 * term and if the sum of the constant terms is such that for any value
4725 * of the other values, there is always at least one integer value of the
4726 * div, i.e., if one plus this sum is greater than or equal to
4727 * the (absolute value) of the coefficient of the div in the constraints,
4728 * then we can also simply drop the div.
4730 * If an existentially quantified variable does not have an explicit
4731 * representation, appears in only a single lower bound that does not
4732 * involve any other such existentially quantified variables and appears
4733 * in this lower bound with coefficient 1,
4734 * then fix the variable to the value of the lower bound. That is,
4735 * turn the inequality into an equality.
4736 * If for any value of the other variables, there is any value
4737 * for the existentially quantified variable satisfying the constraints,
4738 * then this lower bound also satisfies the constraints.
4739 * It is therefore safe to pick this lower bound.
4741 * The same reasoning holds even if the coefficient is not one.
4742 * However, fixing the variable to the value of the lower bound may
4743 * in general introduce an extra integer division, in which case
4744 * it may be better to pick another value.
4745 * If this integer division has a known constant value, then plugging
4746 * in this constant value removes the existentially quantified variable
4747 * completely. In particular, if the lower bound is of the form
4748 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4749 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4750 * then the existentially quantified variable can be assigned this
4751 * shared value.
4753 * We skip divs that appear in equalities or in the definition of other divs.
4754 * Divs that appear in the definition of other divs usually occur in at least
4755 * 4 constraints, but the constraints may have been simplified.
4757 * If any divs are left after these simple checks then we move on
4758 * to more complicated cases in drop_more_redundant_divs.
4760 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4761 __isl_take isl_basic_map *bmap)
4763 int i, j;
4764 int off;
4765 int *pairs = NULL;
4766 int n = 0;
4767 int n_ineq;
4769 if (!bmap)
4770 goto error;
4771 if (bmap->n_div == 0)
4772 return bmap;
4774 off = isl_basic_map_var_offset(bmap, isl_dim_div);
4775 if (off < 0)
4776 return isl_basic_map_free(bmap);
4777 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4778 if (!pairs)
4779 goto error;
4781 n_ineq = isl_basic_map_n_inequality(bmap);
4782 for (i = 0; i < bmap->n_div; ++i) {
4783 int pos, neg;
4784 int last_pos, last_neg;
4785 int redundant;
4786 int defined;
4787 isl_bool opp, set_div;
4789 defined = !isl_int_is_zero(bmap->div[i][0]);
4790 for (j = i; j < bmap->n_div; ++j)
4791 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4792 break;
4793 if (j < bmap->n_div)
4794 continue;
4795 for (j = 0; j < bmap->n_eq; ++j)
4796 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4797 break;
4798 if (j < bmap->n_eq)
4799 continue;
4800 ++n;
4801 pos = neg = 0;
4802 for (j = 0; j < bmap->n_ineq; ++j) {
4803 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4804 last_pos = j;
4805 ++pos;
4807 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4808 last_neg = j;
4809 ++neg;
4812 pairs[i] = pos * neg;
4813 if (pairs[i] == 0) {
4814 for (j = bmap->n_ineq - 1; j >= 0; --j)
4815 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4816 isl_basic_map_drop_inequality(bmap, j);
4817 bmap = isl_basic_map_drop_div(bmap, i);
4818 return drop_redundant_divs_again(bmap, pairs, 0);
4820 if (pairs[i] != 1)
4821 opp = isl_bool_false;
4822 else
4823 opp = is_opposite(bmap, last_pos, last_neg);
4824 if (opp < 0)
4825 goto error;
4826 if (!opp) {
4827 int lower;
4828 isl_bool single, one;
4830 if (pos != 1)
4831 continue;
4832 single = single_unknown(bmap, last_pos, i);
4833 if (single < 0)
4834 goto error;
4835 if (!single)
4836 continue;
4837 one = has_coef_one(bmap, i, last_pos);
4838 if (one < 0)
4839 goto error;
4840 if (one)
4841 return set_eq_and_try_again(bmap, last_pos,
4842 pairs);
4843 lower = lower_bound_is_cst(bmap, i, last_pos);
4844 if (lower < 0)
4845 goto error;
4846 if (lower < n_ineq)
4847 return fix_cst_lower(bmap, i, last_pos, lower,
4848 pairs);
4849 continue;
4852 isl_int_add(bmap->ineq[last_pos][0],
4853 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4854 isl_int_add_ui(bmap->ineq[last_pos][0],
4855 bmap->ineq[last_pos][0], 1);
4856 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4857 bmap->ineq[last_pos][1+off+i]);
4858 isl_int_sub_ui(bmap->ineq[last_pos][0],
4859 bmap->ineq[last_pos][0], 1);
4860 isl_int_sub(bmap->ineq[last_pos][0],
4861 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4862 if (redundant)
4863 return drop_div_and_try_again(bmap, i,
4864 last_pos, last_neg, pairs);
4865 if (defined)
4866 set_div = isl_bool_false;
4867 else
4868 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4869 if (set_div < 0)
4870 return isl_basic_map_free(bmap);
4871 if (set_div) {
4872 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4873 return drop_redundant_divs_again(bmap, pairs, 1);
4875 pairs[i] = 0;
4876 --n;
4879 if (n > 0)
4880 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4882 free(pairs);
4883 return bmap;
4884 error:
4885 free(pairs);
4886 isl_basic_map_free(bmap);
4887 return NULL;
4890 /* Consider the coefficients at "c" as a row vector and replace
4891 * them with their product with "T". "T" is assumed to be a square matrix.
4893 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4895 int n;
4896 isl_ctx *ctx;
4897 isl_vec *v;
4899 if (!T)
4900 return isl_stat_error;
4901 n = isl_mat_rows(T);
4902 if (isl_seq_first_non_zero(c, n) == -1)
4903 return isl_stat_ok;
4904 ctx = isl_mat_get_ctx(T);
4905 v = isl_vec_alloc(ctx, n);
4906 if (!v)
4907 return isl_stat_error;
4908 isl_seq_swp_or_cpy(v->el, c, n);
4909 v = isl_vec_mat_product(v, isl_mat_copy(T));
4910 if (!v)
4911 return isl_stat_error;
4912 isl_seq_swp_or_cpy(c, v->el, n);
4913 isl_vec_free(v);
4915 return isl_stat_ok;
4918 /* Plug in T for the variables in "bmap" starting at "pos".
4919 * T is a linear unimodular matrix, i.e., without constant term.
4921 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4922 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4924 int i;
4925 unsigned n;
4927 bmap = isl_basic_map_cow(bmap);
4928 if (!bmap || !T)
4929 goto error;
4931 n = isl_mat_cols(T);
4932 if (n != isl_mat_rows(T))
4933 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4934 "expecting square matrix", goto error);
4936 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n) < 0)
4937 goto error;
4939 for (i = 0; i < bmap->n_eq; ++i)
4940 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4941 goto error;
4942 for (i = 0; i < bmap->n_ineq; ++i)
4943 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4944 goto error;
4945 for (i = 0; i < bmap->n_div; ++i) {
4946 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4947 continue;
4948 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4949 goto error;
4952 isl_mat_free(T);
4953 return bmap;
4954 error:
4955 isl_basic_map_free(bmap);
4956 isl_mat_free(T);
4957 return NULL;
4960 /* Remove divs that are not strictly needed.
4962 * First look for an equality constraint involving two or more
4963 * existentially quantified variables without an explicit
4964 * representation. Replace the combination that appears
4965 * in the equality constraint by a single existentially quantified
4966 * variable such that the equality can be used to derive
4967 * an explicit representation for the variable.
4968 * If there are no more such equality constraints, then continue
4969 * with isl_basic_map_drop_redundant_divs_ineq.
4971 * In particular, if the equality constraint is of the form
4973 * f(x) + \sum_i c_i a_i = 0
4975 * with a_i existentially quantified variable without explicit
4976 * representation, then apply a transformation on the existentially
4977 * quantified variables to turn the constraint into
4979 * f(x) + g a_1' = 0
4981 * with g the gcd of the c_i.
4982 * In order to easily identify which existentially quantified variables
4983 * have a complete explicit representation, i.e., without being defined
4984 * in terms of other existentially quantified variables without
4985 * an explicit representation, the existentially quantified variables
4986 * are first sorted.
4988 * The variable transformation is computed by extending the row
4989 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4991 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4992 * [a_2'] [ a_2 ]
4993 * ... = U ....
4994 * [a_n'] [ a_n ]
4996 * with [c_1/g ... c_n/g] representing the first row of U.
4997 * The inverse of U is then plugged into the original constraints.
4998 * The call to isl_basic_map_simplify makes sure the explicit
4999 * representation for a_1' is extracted from the equality constraint.
5001 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5002 __isl_take isl_basic_map *bmap)
5004 int first;
5005 int i;
5006 unsigned o_div, n_div;
5007 int l;
5008 isl_ctx *ctx;
5009 isl_mat *T;
5011 if (!bmap)
5012 return NULL;
5013 if (isl_basic_map_divs_known(bmap))
5014 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5015 if (bmap->n_eq == 0)
5016 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5017 bmap = isl_basic_map_sort_divs(bmap);
5018 if (!bmap)
5019 return NULL;
5021 first = isl_basic_map_first_unknown_div(bmap);
5022 if (first < 0)
5023 return isl_basic_map_free(bmap);
5025 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5026 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5028 for (i = 0; i < bmap->n_eq; ++i) {
5029 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5030 n_div - (first));
5031 if (l < 0)
5032 continue;
5033 l += first;
5034 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5035 n_div - (l + 1)) == -1)
5036 continue;
5037 break;
5039 if (i >= bmap->n_eq)
5040 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5042 ctx = isl_basic_map_get_ctx(bmap);
5043 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5044 if (!T)
5045 return isl_basic_map_free(bmap);
5046 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5047 T = isl_mat_normalize_row(T, 0);
5048 T = isl_mat_unimodular_complete(T, 1);
5049 T = isl_mat_right_inverse(T);
5051 for (i = l; i < n_div; ++i)
5052 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5053 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5054 bmap = isl_basic_map_simplify(bmap);
5056 return isl_basic_map_drop_redundant_divs(bmap);
5059 /* Does "bmap" satisfy any equality that involves more than 2 variables
5060 * and/or has coefficients different from -1 and 1?
5062 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5064 int i;
5065 unsigned total;
5067 total = isl_basic_map_dim(bmap, isl_dim_all);
5069 for (i = 0; i < bmap->n_eq; ++i) {
5070 int j, k;
5072 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5073 if (j < 0)
5074 continue;
5075 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5076 !isl_int_is_negone(bmap->eq[i][1 + j]))
5077 return isl_bool_true;
5079 j += 1;
5080 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5081 if (k < 0)
5082 continue;
5083 j += k;
5084 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5085 !isl_int_is_negone(bmap->eq[i][1 + j]))
5086 return isl_bool_true;
5088 j += 1;
5089 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5090 if (k >= 0)
5091 return isl_bool_true;
5094 return isl_bool_false;
5097 /* Remove any common factor g from the constraint coefficients in "v".
5098 * The constant term is stored in the first position and is replaced
5099 * by floor(c/g). If any common factor is removed and if this results
5100 * in a tightening of the constraint, then set *tightened.
5102 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5103 int *tightened)
5105 isl_ctx *ctx;
5107 if (!v)
5108 return NULL;
5109 ctx = isl_vec_get_ctx(v);
5110 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5111 if (isl_int_is_zero(ctx->normalize_gcd))
5112 return v;
5113 if (isl_int_is_one(ctx->normalize_gcd))
5114 return v;
5115 v = isl_vec_cow(v);
5116 if (!v)
5117 return NULL;
5118 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5119 *tightened = 1;
5120 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5121 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5122 v->size - 1);
5123 return v;
5126 /* If "bmap" is an integer set that satisfies any equality involving
5127 * more than 2 variables and/or has coefficients different from -1 and 1,
5128 * then use variable compression to reduce the coefficients by removing
5129 * any (hidden) common factor.
5130 * In particular, apply the variable compression to each constraint,
5131 * factor out any common factor in the non-constant coefficients and
5132 * then apply the inverse of the compression.
5133 * At the end, we mark the basic map as having reduced constants.
5134 * If this flag is still set on the next invocation of this function,
5135 * then we skip the computation.
5137 * Removing a common factor may result in a tightening of some of
5138 * the constraints. If this happens, then we may end up with two
5139 * opposite inequalities that can be replaced by an equality.
5140 * We therefore call isl_basic_map_detect_inequality_pairs,
5141 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5142 * and isl_basic_map_gauss if such a pair was found.
5144 * Note that this function may leave the result in an inconsistent state.
5145 * In particular, the constraints may not be gaussed.
5146 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5147 * for some of the test cases to pass successfully.
5148 * Any potential modification of the representation is therefore only
5149 * performed on a single copy of the basic map.
5151 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5152 __isl_take isl_basic_map *bmap)
5154 unsigned total;
5155 isl_bool multi;
5156 isl_ctx *ctx;
5157 isl_vec *v;
5158 isl_mat *eq, *T, *T2;
5159 int i;
5160 int tightened;
5162 if (!bmap)
5163 return NULL;
5164 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5165 return bmap;
5166 if (isl_basic_map_is_rational(bmap))
5167 return bmap;
5168 if (bmap->n_eq == 0)
5169 return bmap;
5170 multi = has_multiple_var_equality(bmap);
5171 if (multi < 0)
5172 return isl_basic_map_free(bmap);
5173 if (!multi)
5174 return bmap;
5176 total = isl_basic_map_dim(bmap, isl_dim_all);
5177 ctx = isl_basic_map_get_ctx(bmap);
5178 v = isl_vec_alloc(ctx, 1 + total);
5179 if (!v)
5180 return isl_basic_map_free(bmap);
5182 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5183 T = isl_mat_variable_compression(eq, &T2);
5184 if (!T || !T2)
5185 goto error;
5186 if (T->n_col == 0) {
5187 isl_mat_free(T);
5188 isl_mat_free(T2);
5189 isl_vec_free(v);
5190 return isl_basic_map_set_to_empty(bmap);
5193 bmap = isl_basic_map_cow(bmap);
5194 if (!bmap)
5195 goto error;
5197 tightened = 0;
5198 for (i = 0; i < bmap->n_ineq; ++i) {
5199 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5200 v = isl_vec_mat_product(v, isl_mat_copy(T));
5201 v = normalize_constraint(v, &tightened);
5202 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5203 if (!v)
5204 goto error;
5205 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5208 isl_mat_free(T);
5209 isl_mat_free(T2);
5210 isl_vec_free(v);
5212 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5214 if (tightened) {
5215 int progress = 0;
5217 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5218 if (progress) {
5219 bmap = eliminate_divs_eq(bmap, &progress);
5220 bmap = isl_basic_map_gauss(bmap, NULL);
5224 return bmap;
5225 error:
5226 isl_mat_free(T);
5227 isl_mat_free(T2);
5228 isl_vec_free(v);
5229 return isl_basic_map_free(bmap);
5232 /* Shift the integer division at position "div" of "bmap"
5233 * by "shift" times the variable at position "pos".
5234 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5235 * corresponds to the constant term.
5237 * That is, if the integer division has the form
5239 * floor(f(x)/d)
5241 * then replace it by
5243 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5245 __isl_give isl_basic_map *isl_basic_map_shift_div(
5246 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5248 int i;
5249 unsigned total;
5251 if (isl_int_is_zero(shift))
5252 return bmap;
5253 if (!bmap)
5254 return NULL;
5256 total = isl_basic_map_dim(bmap, isl_dim_all);
5257 total -= isl_basic_map_dim(bmap, isl_dim_div);
5259 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5261 for (i = 0; i < bmap->n_eq; ++i) {
5262 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5263 continue;
5264 isl_int_submul(bmap->eq[i][pos],
5265 shift, bmap->eq[i][1 + total + div]);
5267 for (i = 0; i < bmap->n_ineq; ++i) {
5268 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5269 continue;
5270 isl_int_submul(bmap->ineq[i][pos],
5271 shift, bmap->ineq[i][1 + total + div]);
5273 for (i = 0; i < bmap->n_div; ++i) {
5274 if (isl_int_is_zero(bmap->div[i][0]))
5275 continue;
5276 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5277 continue;
5278 isl_int_submul(bmap->div[i][1 + pos],
5279 shift, bmap->div[i][1 + 1 + total + div]);
5282 return bmap;