isl_val_n_abs_num_chunks: return isl_size
[isl.git] / isl_map_simplify.c
blob5b9bb09bed1e6ccb1365b418ee8a395e9dcdd269
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 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
54 if (total < 0)
55 return isl_basic_map_free(bmap);
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 isl_size total;
182 total = isl_basic_map_dim(bmap, isl_dim_all);
183 if (total < 0)
184 return isl_basic_map_free(bmap);
185 for (i = 0; i < 1 + total; ++i) {
186 isl_bool reduce;
188 reduce = needs_reduction(bmap, div, i);
189 if (reduce < 0)
190 return isl_basic_map_free(bmap);
191 if (!reduce)
192 continue;
193 bmap = reduce_coefficient_in_div(bmap, div, i);
194 if (!bmap)
195 break;
198 return bmap;
201 /* Reduce the coefficients (including the constant term) of
202 * the known integer divisions, if needed
203 * In particular, make sure all coefficients lie in
204 * the half-open interval (1/2,1/2].
206 static __isl_give isl_basic_map *reduce_div_coefficients(
207 __isl_take isl_basic_map *bmap)
209 int i;
211 if (!bmap)
212 return NULL;
213 if (bmap->n_div == 0)
214 return bmap;
216 for (i = 0; i < bmap->n_div; ++i) {
217 if (isl_int_is_zero(bmap->div[i][0]))
218 continue;
219 bmap = reduce_div_coefficients_of_div(bmap, i);
220 if (!bmap)
221 break;
224 return bmap;
227 /* Remove any common factor in numerator and denominator of the div expression,
228 * not taking into account the constant term.
229 * That is, if the div is of the form
231 * floor((a + m f(x))/(m d))
233 * then replace it by
235 * floor((floor(a/m) + f(x))/d)
237 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
238 * and can therefore not influence the result of the floor.
240 static __isl_give isl_basic_map *normalize_div_expression(
241 __isl_take isl_basic_map *bmap, int div)
243 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
244 isl_ctx *ctx = bmap->ctx;
246 if (total < 0)
247 return isl_basic_map_free(bmap);
248 if (isl_int_is_zero(bmap->div[div][0]))
249 return bmap;
250 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
251 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
252 if (isl_int_is_one(ctx->normalize_gcd))
253 return bmap;
254 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
255 ctx->normalize_gcd);
256 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
257 ctx->normalize_gcd);
258 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
259 ctx->normalize_gcd, total);
261 return bmap;
264 /* Remove any common factor in numerator and denominator of a div expression,
265 * not taking into account the constant term.
266 * That is, look for any div of the form
268 * floor((a + m f(x))/(m d))
270 * and replace it by
272 * floor((floor(a/m) + f(x))/d)
274 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
275 * and can therefore not influence the result of the floor.
277 static __isl_give isl_basic_map *normalize_div_expressions(
278 __isl_take isl_basic_map *bmap)
280 int i;
282 if (!bmap)
283 return NULL;
284 if (bmap->n_div == 0)
285 return bmap;
287 for (i = 0; i < bmap->n_div; ++i)
288 bmap = normalize_div_expression(bmap, i);
290 return bmap;
293 /* Assumes divs have been ordered if keep_divs is set.
295 static __isl_give isl_basic_map *eliminate_var_using_equality(
296 __isl_take isl_basic_map *bmap,
297 unsigned pos, isl_int *eq, int keep_divs, int *progress)
299 isl_size total;
300 int v_div;
301 int k;
302 int last_div;
304 total = isl_basic_map_dim(bmap, isl_dim_all);
305 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
306 if (total < 0 || v_div < 0)
307 return isl_basic_map_free(bmap);
308 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
309 for (k = 0; k < bmap->n_eq; ++k) {
310 if (bmap->eq[k] == eq)
311 continue;
312 if (isl_int_is_zero(bmap->eq[k][1+pos]))
313 continue;
314 if (progress)
315 *progress = 1;
316 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
317 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
320 for (k = 0; k < bmap->n_ineq; ++k) {
321 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
322 continue;
323 if (progress)
324 *progress = 1;
325 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
326 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
327 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
328 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
331 for (k = 0; k < bmap->n_div; ++k) {
332 if (isl_int_is_zero(bmap->div[k][0]))
333 continue;
334 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
335 continue;
336 if (progress)
337 *progress = 1;
338 /* We need to be careful about circular definitions,
339 * so for now we just remove the definition of div k
340 * if the equality contains any divs.
341 * If keep_divs is set, then the divs have been ordered
342 * and we can keep the definition as long as the result
343 * is still ordered.
345 if (last_div == -1 || (keep_divs && last_div < k)) {
346 isl_seq_elim(bmap->div[k]+1, eq,
347 1+pos, 1+total, &bmap->div[k][0]);
348 bmap = normalize_div_expression(bmap, k);
349 if (!bmap)
350 return NULL;
351 } else
352 isl_seq_clr(bmap->div[k], 1 + total);
355 return bmap;
358 /* Assumes divs have been ordered if keep_divs is set.
360 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
361 isl_int *eq, unsigned div, int keep_divs)
363 int v_div;
364 unsigned pos;
366 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
367 if (v_div < 0)
368 return isl_basic_map_free(bmap);
369 pos = v_div + div;
370 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
372 bmap = isl_basic_map_drop_div(bmap, div);
374 return bmap;
377 /* Check if elimination of div "div" using equality "eq" would not
378 * result in a div depending on a later div.
380 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
381 unsigned div)
383 int k;
384 int last_div;
385 int v_div;
386 unsigned pos;
388 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
389 if (v_div < 0)
390 return isl_bool_error;
391 pos = v_div + div;
393 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
394 if (last_div < 0 || last_div <= div)
395 return isl_bool_true;
397 for (k = 0; k <= last_div; ++k) {
398 if (isl_int_is_zero(bmap->div[k][0]))
399 continue;
400 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
401 return isl_bool_false;
404 return isl_bool_true;
407 /* Eliminate divs based on equalities
409 static __isl_give isl_basic_map *eliminate_divs_eq(
410 __isl_take isl_basic_map *bmap, int *progress)
412 int d;
413 int i;
414 int modified = 0;
415 unsigned off;
417 bmap = isl_basic_map_order_divs(bmap);
419 if (!bmap)
420 return NULL;
422 off = isl_basic_map_offset(bmap, isl_dim_div);
424 for (d = bmap->n_div - 1; d >= 0 ; --d) {
425 for (i = 0; i < bmap->n_eq; ++i) {
426 isl_bool ok;
428 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
429 !isl_int_is_negone(bmap->eq[i][off + d]))
430 continue;
431 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
432 if (ok < 0)
433 return isl_basic_map_free(bmap);
434 if (!ok)
435 continue;
436 modified = 1;
437 *progress = 1;
438 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
439 if (isl_basic_map_drop_equality(bmap, i) < 0)
440 return isl_basic_map_free(bmap);
441 break;
444 if (modified)
445 return eliminate_divs_eq(bmap, progress);
446 return bmap;
449 /* Eliminate divs based on inequalities
451 static __isl_give isl_basic_map *eliminate_divs_ineq(
452 __isl_take isl_basic_map *bmap, int *progress)
454 int d;
455 int i;
456 unsigned off;
457 struct isl_ctx *ctx;
459 if (!bmap)
460 return NULL;
462 ctx = bmap->ctx;
463 off = isl_basic_map_offset(bmap, isl_dim_div);
465 for (d = bmap->n_div - 1; d >= 0 ; --d) {
466 for (i = 0; i < bmap->n_eq; ++i)
467 if (!isl_int_is_zero(bmap->eq[i][off + d]))
468 break;
469 if (i < bmap->n_eq)
470 continue;
471 for (i = 0; i < bmap->n_ineq; ++i)
472 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
473 break;
474 if (i < bmap->n_ineq)
475 continue;
476 *progress = 1;
477 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
478 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
479 break;
480 bmap = isl_basic_map_drop_div(bmap, d);
481 if (!bmap)
482 break;
484 return bmap;
487 /* Does the equality constraint at position "eq" in "bmap" involve
488 * any local variables in the range [first, first + n)
489 * that are not marked as having an explicit representation?
491 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
492 int eq, unsigned first, unsigned n)
494 unsigned o_div;
495 int i;
497 if (!bmap)
498 return isl_bool_error;
500 o_div = isl_basic_map_offset(bmap, isl_dim_div);
501 for (i = 0; i < n; ++i) {
502 isl_bool unknown;
504 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
505 continue;
506 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
507 if (unknown < 0)
508 return isl_bool_error;
509 if (unknown)
510 return isl_bool_true;
513 return isl_bool_false;
516 /* The last local variable involved in the equality constraint
517 * at position "eq" in "bmap" is the local variable at position "div".
518 * It can therefore be used to extract an explicit representation
519 * for that variable.
520 * Do so unless the local variable already has an explicit representation or
521 * the explicit representation would involve any other local variables
522 * that in turn do not have an explicit representation.
523 * An equality constraint involving local variables without an explicit
524 * representation can be used in isl_basic_map_drop_redundant_divs
525 * to separate out an independent local variable. Introducing
526 * an explicit representation here would block this transformation,
527 * while the partial explicit representation in itself is not very useful.
528 * Set *progress if anything is changed.
530 * The equality constraint is of the form
532 * f(x) + n e >= 0
534 * with n a positive number. The explicit representation derived from
535 * this constraint is
537 * floor((-f(x))/n)
539 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
540 int div, int eq, int *progress)
542 isl_size total;
543 unsigned o_div;
544 isl_bool involves;
546 if (!bmap)
547 return NULL;
549 if (!isl_int_is_zero(bmap->div[div][0]))
550 return bmap;
552 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
553 if (involves < 0)
554 return isl_basic_map_free(bmap);
555 if (involves)
556 return bmap;
558 total = isl_basic_map_dim(bmap, isl_dim_all);
559 if (total < 0)
560 return isl_basic_map_free(bmap);
561 o_div = isl_basic_map_offset(bmap, isl_dim_div);
562 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
563 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
564 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
565 if (progress)
566 *progress = 1;
568 return bmap;
571 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
572 int *progress)
574 int k;
575 int done;
576 int last_var;
577 unsigned total_var;
578 isl_size total;
580 bmap = isl_basic_map_order_divs(bmap);
582 total = isl_basic_map_dim(bmap, isl_dim_all);
583 if (total < 0)
584 return isl_basic_map_free(bmap);
586 total_var = total - bmap->n_div;
588 last_var = total - 1;
589 for (done = 0; done < bmap->n_eq; ++done) {
590 for (; last_var >= 0; --last_var) {
591 for (k = done; k < bmap->n_eq; ++k)
592 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
593 break;
594 if (k < bmap->n_eq)
595 break;
597 if (last_var < 0)
598 break;
599 if (k != done)
600 swap_equality(bmap, k, done);
601 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
602 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
604 bmap = eliminate_var_using_equality(bmap, last_var,
605 bmap->eq[done], 1, progress);
607 if (last_var >= total_var)
608 bmap = set_div_from_eq(bmap, last_var - total_var,
609 done, progress);
610 if (!bmap)
611 return NULL;
613 if (done == bmap->n_eq)
614 return bmap;
615 for (k = done; k < bmap->n_eq; ++k) {
616 if (isl_int_is_zero(bmap->eq[k][0]))
617 continue;
618 return isl_basic_map_set_to_empty(bmap);
620 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
621 return bmap;
624 __isl_give isl_basic_set *isl_basic_set_gauss(
625 __isl_take isl_basic_set *bset, int *progress)
627 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
628 progress));
632 static unsigned int round_up(unsigned int v)
634 int old_v = v;
636 while (v) {
637 old_v = v;
638 v ^= v & -v;
640 return old_v << 1;
643 /* Hash table of inequalities in a basic map.
644 * "index" is an array of addresses of inequalities in the basic map, some
645 * of which are NULL. The inequalities are hashed on the coefficients
646 * except the constant term.
647 * "size" is the number of elements in the array and is always a power of two
648 * "bits" is the number of bits need to represent an index into the array.
649 * "total" is the total dimension of the basic map.
651 struct isl_constraint_index {
652 unsigned int size;
653 int bits;
654 isl_int ***index;
655 isl_size total;
658 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
660 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
661 __isl_keep isl_basic_map *bmap)
663 isl_ctx *ctx;
665 ci->index = NULL;
666 if (!bmap)
667 return isl_stat_error;
668 ci->total = isl_basic_map_dim(bmap, isl_dim_all);
669 if (ci->total < 0)
670 return isl_stat_error;
671 if (bmap->n_ineq == 0)
672 return isl_stat_ok;
673 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
674 ci->bits = ffs(ci->size) - 1;
675 ctx = isl_basic_map_get_ctx(bmap);
676 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
677 if (!ci->index)
678 return isl_stat_error;
680 return isl_stat_ok;
683 /* Free the memory allocated by create_constraint_index.
685 static void constraint_index_free(struct isl_constraint_index *ci)
687 free(ci->index);
690 /* Return the position in ci->index that contains the address of
691 * an inequality that is equal to *ineq up to the constant term,
692 * provided this address is not identical to "ineq".
693 * If there is no such inequality, then return the position where
694 * such an inequality should be inserted.
696 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
698 int h;
699 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
700 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
701 if (ineq != ci->index[h] &&
702 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
703 break;
704 return h;
707 /* Return the position in ci->index that contains the address of
708 * an inequality that is equal to the k'th inequality of "bmap"
709 * up to the constant term, provided it does not point to the very
710 * same inequality.
711 * If there is no such inequality, then return the position where
712 * such an inequality should be inserted.
714 static int hash_index(struct isl_constraint_index *ci,
715 __isl_keep isl_basic_map *bmap, int k)
717 return hash_index_ineq(ci, &bmap->ineq[k]);
720 static int set_hash_index(struct isl_constraint_index *ci,
721 __isl_keep isl_basic_set *bset, int k)
723 return hash_index(ci, bset, k);
726 /* Fill in the "ci" data structure with the inequalities of "bset".
728 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
729 __isl_keep isl_basic_set *bset)
731 int k, h;
733 if (create_constraint_index(ci, bset) < 0)
734 return isl_stat_error;
736 for (k = 0; k < bset->n_ineq; ++k) {
737 h = set_hash_index(ci, bset, k);
738 ci->index[h] = &bset->ineq[k];
741 return isl_stat_ok;
744 /* Is the inequality ineq (obviously) redundant with respect
745 * to the constraints in "ci"?
747 * Look for an inequality in "ci" with the same coefficients and then
748 * check if the contant term of "ineq" is greater than or equal
749 * to the constant term of that inequality. If so, "ineq" is clearly
750 * redundant.
752 * Note that hash_index_ineq ignores a stored constraint if it has
753 * the same address as the passed inequality. It is ok to pass
754 * the address of a local variable here since it will never be
755 * the same as the address of a constraint in "ci".
757 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
758 isl_int *ineq)
760 int h;
762 h = hash_index_ineq(ci, &ineq);
763 if (!ci->index[h])
764 return isl_bool_false;
765 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
768 /* If we can eliminate more than one div, then we need to make
769 * sure we do it from last div to first div, in order not to
770 * change the position of the other divs that still need to
771 * be removed.
773 static __isl_give isl_basic_map *remove_duplicate_divs(
774 __isl_take isl_basic_map *bmap, int *progress)
776 unsigned int size;
777 int *index;
778 int *elim_for;
779 int k, l, h;
780 int bits;
781 struct isl_blk eq;
782 int v_div;
783 unsigned total;
784 struct isl_ctx *ctx;
786 bmap = isl_basic_map_order_divs(bmap);
787 if (!bmap || bmap->n_div <= 1)
788 return bmap;
790 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
791 if (v_div < 0)
792 return isl_basic_map_free(bmap);
793 total = v_div + bmap->n_div;
795 ctx = bmap->ctx;
796 for (k = bmap->n_div - 1; k >= 0; --k)
797 if (!isl_int_is_zero(bmap->div[k][0]))
798 break;
799 if (k <= 0)
800 return bmap;
802 size = round_up(4 * bmap->n_div / 3 - 1);
803 if (size == 0)
804 return bmap;
805 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
806 bits = ffs(size) - 1;
807 index = isl_calloc_array(ctx, int, size);
808 if (!elim_for || !index)
809 goto out;
810 eq = isl_blk_alloc(ctx, 1+total);
811 if (isl_blk_is_error(eq))
812 goto out;
814 isl_seq_clr(eq.data, 1+total);
815 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
816 for (--k; k >= 0; --k) {
817 uint32_t hash;
819 if (isl_int_is_zero(bmap->div[k][0]))
820 continue;
822 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
823 for (h = hash; index[h]; h = (h+1) % size)
824 if (isl_seq_eq(bmap->div[k],
825 bmap->div[index[h]-1], 2+total))
826 break;
827 if (index[h]) {
828 *progress = 1;
829 l = index[h] - 1;
830 elim_for[l] = k + 1;
832 index[h] = k+1;
834 for (l = bmap->n_div - 1; l >= 0; --l) {
835 if (!elim_for[l])
836 continue;
837 k = elim_for[l] - 1;
838 isl_int_set_si(eq.data[1 + v_div + k], -1);
839 isl_int_set_si(eq.data[1 + v_div + l], 1);
840 bmap = eliminate_div(bmap, eq.data, l, 1);
841 if (!bmap)
842 break;
843 isl_int_set_si(eq.data[1 + v_div + k], 0);
844 isl_int_set_si(eq.data[1 + v_div + l], 0);
847 isl_blk_free(ctx, eq);
848 out:
849 free(index);
850 free(elim_for);
851 return bmap;
854 static int n_pure_div_eq(struct isl_basic_map *bmap)
856 int i, j;
857 int v_div;
859 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
860 if (v_div < 0)
861 return -1;
862 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
863 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
864 --j;
865 if (j < 0)
866 break;
867 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + v_div, j) != -1)
868 return 0;
870 return i;
873 /* Normalize divs that appear in equalities.
875 * In particular, we assume that bmap contains some equalities
876 * of the form
878 * a x = m * e_i
880 * and we want to replace the set of e_i by a minimal set and
881 * such that the new e_i have a canonical representation in terms
882 * of the vector x.
883 * If any of the equalities involves more than one divs, then
884 * we currently simply bail out.
886 * Let us first additionally assume that all equalities involve
887 * a div. The equalities then express modulo constraints on the
888 * remaining variables and we can use "parameter compression"
889 * to find a minimal set of constraints. The result is a transformation
891 * x = T(x') = x_0 + G x'
893 * with G a lower-triangular matrix with all elements below the diagonal
894 * non-negative and smaller than the diagonal element on the same row.
895 * We first normalize x_0 by making the same property hold in the affine
896 * T matrix.
897 * The rows i of G with a 1 on the diagonal do not impose any modulo
898 * constraint and simply express x_i = x'_i.
899 * For each of the remaining rows i, we introduce a div and a corresponding
900 * equality. In particular
902 * g_ii e_j = x_i - g_i(x')
904 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
905 * corresponding div (if g_kk != 1).
907 * If there are any equalities not involving any div, then we
908 * first apply a variable compression on the variables x:
910 * x = C x'' x'' = C_2 x
912 * and perform the above parameter compression on A C instead of on A.
913 * The resulting compression is then of the form
915 * x'' = T(x') = x_0 + G x'
917 * and in constructing the new divs and the corresponding equalities,
918 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
919 * by the corresponding row from C_2.
921 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
922 int *progress)
924 int i, j, k;
925 int v_div;
926 int div_eq;
927 struct isl_mat *B;
928 struct isl_vec *d;
929 struct isl_mat *T = NULL;
930 struct isl_mat *C = NULL;
931 struct isl_mat *C2 = NULL;
932 isl_int v;
933 int *pos = NULL;
934 int dropped, needed;
936 if (!bmap)
937 return NULL;
939 if (bmap->n_div == 0)
940 return bmap;
942 if (bmap->n_eq == 0)
943 return bmap;
945 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
946 return bmap;
948 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
949 div_eq = n_pure_div_eq(bmap);
950 if (v_div < 0 || div_eq < 0)
951 return isl_basic_map_free(bmap);
952 if (div_eq == 0)
953 return bmap;
955 if (div_eq < bmap->n_eq) {
956 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
957 bmap->n_eq - div_eq, 0, 1 + v_div);
958 C = isl_mat_variable_compression(B, &C2);
959 if (!C || !C2)
960 goto error;
961 if (C->n_col == 0) {
962 bmap = isl_basic_map_set_to_empty(bmap);
963 isl_mat_free(C);
964 isl_mat_free(C2);
965 goto done;
969 d = isl_vec_alloc(bmap->ctx, div_eq);
970 if (!d)
971 goto error;
972 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
973 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
974 --j;
975 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
977 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
979 if (C) {
980 B = isl_mat_product(B, C);
981 C = NULL;
984 T = isl_mat_parameter_compression(B, d);
985 if (!T)
986 goto error;
987 if (T->n_col == 0) {
988 bmap = isl_basic_map_set_to_empty(bmap);
989 isl_mat_free(C2);
990 isl_mat_free(T);
991 goto done;
993 isl_int_init(v);
994 for (i = 0; i < T->n_row - 1; ++i) {
995 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
996 if (isl_int_is_zero(v))
997 continue;
998 isl_mat_col_submul(T, 0, v, 1 + i);
1000 isl_int_clear(v);
1001 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1002 if (!pos)
1003 goto error;
1004 /* We have to be careful because dropping equalities may reorder them */
1005 dropped = 0;
1006 for (j = bmap->n_div - 1; j >= 0; --j) {
1007 for (i = 0; i < bmap->n_eq; ++i)
1008 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1009 break;
1010 if (i < bmap->n_eq) {
1011 bmap = isl_basic_map_drop_div(bmap, j);
1012 isl_basic_map_drop_equality(bmap, i);
1013 ++dropped;
1016 pos[0] = 0;
1017 needed = 0;
1018 for (i = 1; i < T->n_row; ++i) {
1019 if (isl_int_is_one(T->row[i][i]))
1020 pos[i] = i;
1021 else
1022 needed++;
1024 if (needed > dropped) {
1025 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
1026 needed, needed, 0);
1027 if (!bmap)
1028 goto error;
1030 for (i = 1; i < T->n_row; ++i) {
1031 if (isl_int_is_one(T->row[i][i]))
1032 continue;
1033 k = isl_basic_map_alloc_div(bmap);
1034 pos[i] = 1 + v_div + k;
1035 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1036 isl_int_set(bmap->div[k][0], T->row[i][i]);
1037 if (C2)
1038 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1039 else
1040 isl_int_set_si(bmap->div[k][1 + i], 1);
1041 for (j = 0; j < i; ++j) {
1042 if (isl_int_is_zero(T->row[i][j]))
1043 continue;
1044 if (pos[j] < T->n_row && C2)
1045 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1046 C2->row[pos[j]], 1 + v_div);
1047 else
1048 isl_int_neg(bmap->div[k][1 + pos[j]],
1049 T->row[i][j]);
1051 j = isl_basic_map_alloc_equality(bmap);
1052 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1053 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1055 free(pos);
1056 isl_mat_free(C2);
1057 isl_mat_free(T);
1059 if (progress)
1060 *progress = 1;
1061 done:
1062 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1064 return bmap;
1065 error:
1066 free(pos);
1067 isl_mat_free(C);
1068 isl_mat_free(C2);
1069 isl_mat_free(T);
1070 return bmap;
1073 static __isl_give isl_basic_map *set_div_from_lower_bound(
1074 __isl_take isl_basic_map *bmap, int div, int ineq)
1076 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1078 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1079 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1080 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1081 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1082 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1084 return bmap;
1087 /* Check whether it is ok to define a div based on an inequality.
1088 * To avoid the introduction of circular definitions of divs, we
1089 * do not allow such a definition if the resulting expression would refer to
1090 * any other undefined divs or if any known div is defined in
1091 * terms of the unknown div.
1093 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1094 int div, int ineq)
1096 int j;
1097 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1099 /* Not defined in terms of unknown divs */
1100 for (j = 0; j < bmap->n_div; ++j) {
1101 if (div == j)
1102 continue;
1103 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1104 continue;
1105 if (isl_int_is_zero(bmap->div[j][0]))
1106 return isl_bool_false;
1109 /* No other div defined in terms of this one => avoid loops */
1110 for (j = 0; j < bmap->n_div; ++j) {
1111 if (div == j)
1112 continue;
1113 if (isl_int_is_zero(bmap->div[j][0]))
1114 continue;
1115 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1116 return isl_bool_false;
1119 return isl_bool_true;
1122 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1123 * be a better expression than the current one?
1125 * If we do not have any expression yet, then any expression would be better.
1126 * Otherwise we check if the last variable involved in the inequality
1127 * (disregarding the div that it would define) is in an earlier position
1128 * than the last variable involved in the current div expression.
1130 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1131 int div, int ineq)
1133 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1134 int last_div;
1135 int last_ineq;
1137 if (isl_int_is_zero(bmap->div[div][0]))
1138 return isl_bool_true;
1140 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1141 bmap->n_div - (div + 1)) >= 0)
1142 return isl_bool_false;
1144 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1145 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1146 total + bmap->n_div);
1148 return last_ineq < last_div;
1151 /* Given two constraints "k" and "l" that are opposite to each other,
1152 * except for the constant term, check if we can use them
1153 * to obtain an expression for one of the hitherto unknown divs or
1154 * a "better" expression for a div for which we already have an expression.
1155 * "sum" is the sum of the constant terms of the constraints.
1156 * If this sum is strictly smaller than the coefficient of one
1157 * of the divs, then this pair can be used define the div.
1158 * To avoid the introduction of circular definitions of divs, we
1159 * do not use the pair if the resulting expression would refer to
1160 * any other undefined divs or if any known div is defined in
1161 * terms of the unknown div.
1163 static __isl_give isl_basic_map *check_for_div_constraints(
1164 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1165 int *progress)
1167 int i;
1168 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1170 for (i = 0; i < bmap->n_div; ++i) {
1171 isl_bool set_div;
1173 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1174 continue;
1175 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1176 continue;
1177 set_div = better_div_constraint(bmap, i, k);
1178 if (set_div >= 0 && set_div)
1179 set_div = ok_to_set_div_from_bound(bmap, i, k);
1180 if (set_div < 0)
1181 return isl_basic_map_free(bmap);
1182 if (!set_div)
1183 break;
1184 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1185 bmap = set_div_from_lower_bound(bmap, i, k);
1186 else
1187 bmap = set_div_from_lower_bound(bmap, i, l);
1188 if (progress)
1189 *progress = 1;
1190 break;
1192 return bmap;
1195 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1196 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1198 struct isl_constraint_index ci;
1199 int k, l, h;
1200 isl_size total = isl_basic_map_dim(bmap, isl_dim_all);
1201 isl_int sum;
1203 if (total < 0 || bmap->n_ineq <= 1)
1204 return bmap;
1206 if (create_constraint_index(&ci, bmap) < 0)
1207 return bmap;
1209 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1210 ci.index[h] = &bmap->ineq[0];
1211 for (k = 1; k < bmap->n_ineq; ++k) {
1212 h = hash_index(&ci, bmap, k);
1213 if (!ci.index[h]) {
1214 ci.index[h] = &bmap->ineq[k];
1215 continue;
1217 if (progress)
1218 *progress = 1;
1219 l = ci.index[h] - &bmap->ineq[0];
1220 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1221 swap_inequality(bmap, k, l);
1222 isl_basic_map_drop_inequality(bmap, k);
1223 --k;
1225 isl_int_init(sum);
1226 for (k = 0; k < bmap->n_ineq-1; ++k) {
1227 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1228 h = hash_index(&ci, bmap, k);
1229 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1230 if (!ci.index[h])
1231 continue;
1232 l = ci.index[h] - &bmap->ineq[0];
1233 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1234 if (isl_int_is_pos(sum)) {
1235 if (detect_divs)
1236 bmap = check_for_div_constraints(bmap, k, l,
1237 sum, progress);
1238 continue;
1240 if (isl_int_is_zero(sum)) {
1241 /* We need to break out of the loop after these
1242 * changes since the contents of the hash
1243 * will no longer be valid.
1244 * Plus, we probably we want to regauss first.
1246 if (progress)
1247 *progress = 1;
1248 isl_basic_map_drop_inequality(bmap, l);
1249 isl_basic_map_inequality_to_equality(bmap, k);
1250 } else
1251 bmap = isl_basic_map_set_to_empty(bmap);
1252 break;
1254 isl_int_clear(sum);
1256 constraint_index_free(&ci);
1257 return bmap;
1260 /* Detect all pairs of inequalities that form an equality.
1262 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1263 * Call it repeatedly while it is making progress.
1265 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1266 __isl_take isl_basic_map *bmap, int *progress)
1268 int duplicate;
1270 do {
1271 duplicate = 0;
1272 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1273 &duplicate, 0);
1274 if (progress && duplicate)
1275 *progress = 1;
1276 } while (duplicate);
1278 return bmap;
1281 /* Eliminate knowns divs from constraints where they appear with
1282 * a (positive or negative) unit coefficient.
1284 * That is, replace
1286 * floor(e/m) + f >= 0
1288 * by
1290 * e + m f >= 0
1292 * and
1294 * -floor(e/m) + f >= 0
1296 * by
1298 * -e + m f + m - 1 >= 0
1300 * The first conversion is valid because floor(e/m) >= -f is equivalent
1301 * to e/m >= -f because -f is an integral expression.
1302 * The second conversion follows from the fact that
1304 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1307 * Note that one of the div constraints may have been eliminated
1308 * due to being redundant with respect to the constraint that is
1309 * being modified by this function. The modified constraint may
1310 * no longer imply this div constraint, so we add it back to make
1311 * sure we do not lose any information.
1313 * We skip integral divs, i.e., those with denominator 1, as we would
1314 * risk eliminating the div from the div constraints. We do not need
1315 * to handle those divs here anyway since the div constraints will turn
1316 * out to form an equality and this equality can then be used to eliminate
1317 * the div from all constraints.
1319 static __isl_give isl_basic_map *eliminate_unit_divs(
1320 __isl_take isl_basic_map *bmap, int *progress)
1322 int i, j;
1323 isl_ctx *ctx;
1324 unsigned total;
1326 if (!bmap)
1327 return NULL;
1329 ctx = isl_basic_map_get_ctx(bmap);
1330 total = isl_basic_map_offset(bmap, isl_dim_div);
1332 for (i = 0; i < bmap->n_div; ++i) {
1333 if (isl_int_is_zero(bmap->div[i][0]))
1334 continue;
1335 if (isl_int_is_one(bmap->div[i][0]))
1336 continue;
1337 for (j = 0; j < bmap->n_ineq; ++j) {
1338 int s;
1340 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1341 !isl_int_is_negone(bmap->ineq[j][total + i]))
1342 continue;
1344 *progress = 1;
1346 s = isl_int_sgn(bmap->ineq[j][total + i]);
1347 isl_int_set_si(bmap->ineq[j][total + i], 0);
1348 if (s < 0)
1349 isl_seq_combine(bmap->ineq[j],
1350 ctx->negone, bmap->div[i] + 1,
1351 bmap->div[i][0], bmap->ineq[j],
1352 total + bmap->n_div);
1353 else
1354 isl_seq_combine(bmap->ineq[j],
1355 ctx->one, bmap->div[i] + 1,
1356 bmap->div[i][0], bmap->ineq[j],
1357 total + bmap->n_div);
1358 if (s < 0) {
1359 isl_int_add(bmap->ineq[j][0],
1360 bmap->ineq[j][0], bmap->div[i][0]);
1361 isl_int_sub_ui(bmap->ineq[j][0],
1362 bmap->ineq[j][0], 1);
1365 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1366 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1367 if (!bmap)
1368 return NULL;
1372 return bmap;
1375 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1377 int progress = 1;
1378 if (!bmap)
1379 return NULL;
1380 while (progress) {
1381 isl_bool empty;
1383 progress = 0;
1384 empty = isl_basic_map_plain_is_empty(bmap);
1385 if (empty < 0)
1386 return isl_basic_map_free(bmap);
1387 if (empty)
1388 break;
1389 bmap = isl_basic_map_normalize_constraints(bmap);
1390 bmap = reduce_div_coefficients(bmap);
1391 bmap = normalize_div_expressions(bmap);
1392 bmap = remove_duplicate_divs(bmap, &progress);
1393 bmap = eliminate_unit_divs(bmap, &progress);
1394 bmap = eliminate_divs_eq(bmap, &progress);
1395 bmap = eliminate_divs_ineq(bmap, &progress);
1396 bmap = isl_basic_map_gauss(bmap, &progress);
1397 /* requires equalities in normal form */
1398 bmap = normalize_divs(bmap, &progress);
1399 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1400 &progress, 1);
1401 if (bmap && progress)
1402 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1404 return bmap;
1407 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1409 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1413 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1414 isl_int *constraint, unsigned div)
1416 unsigned pos;
1418 if (!bmap)
1419 return isl_bool_error;
1421 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1423 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1424 int neg;
1425 isl_int_sub(bmap->div[div][1],
1426 bmap->div[div][1], bmap->div[div][0]);
1427 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1428 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1429 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1430 isl_int_add(bmap->div[div][1],
1431 bmap->div[div][1], bmap->div[div][0]);
1432 if (!neg)
1433 return isl_bool_false;
1434 if (isl_seq_first_non_zero(constraint+pos+1,
1435 bmap->n_div-div-1) != -1)
1436 return isl_bool_false;
1437 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1438 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1439 return isl_bool_false;
1440 if (isl_seq_first_non_zero(constraint+pos+1,
1441 bmap->n_div-div-1) != -1)
1442 return isl_bool_false;
1443 } else
1444 return isl_bool_false;
1446 return isl_bool_true;
1449 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1450 isl_int *constraint, unsigned div)
1452 return isl_basic_map_is_div_constraint(bset, constraint, div);
1456 /* If the only constraints a div d=floor(f/m)
1457 * appears in are its two defining constraints
1459 * f - m d >=0
1460 * -(f - (m - 1)) + m d >= 0
1462 * then it can safely be removed.
1464 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1466 int i;
1467 unsigned pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
1469 for (i = 0; i < bmap->n_eq; ++i)
1470 if (!isl_int_is_zero(bmap->eq[i][pos]))
1471 return isl_bool_false;
1473 for (i = 0; i < bmap->n_ineq; ++i) {
1474 isl_bool red;
1476 if (isl_int_is_zero(bmap->ineq[i][pos]))
1477 continue;
1478 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1479 if (red < 0 || !red)
1480 return red;
1483 for (i = 0; i < bmap->n_div; ++i) {
1484 if (isl_int_is_zero(bmap->div[i][0]))
1485 continue;
1486 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1487 return isl_bool_false;
1490 return isl_bool_true;
1494 * Remove divs that don't occur in any of the constraints or other divs.
1495 * These can arise when dropping constraints from a basic map or
1496 * when the divs of a basic map have been temporarily aligned
1497 * with the divs of another basic map.
1499 static __isl_give isl_basic_map *remove_redundant_divs(
1500 __isl_take isl_basic_map *bmap)
1502 int i;
1503 int v_div;
1505 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1506 if (v_div < 0)
1507 return isl_basic_map_free(bmap);
1509 for (i = bmap->n_div-1; i >= 0; --i) {
1510 isl_bool redundant;
1512 redundant = div_is_redundant(bmap, i);
1513 if (redundant < 0)
1514 return isl_basic_map_free(bmap);
1515 if (!redundant)
1516 continue;
1517 bmap = isl_basic_map_drop_constraints_involving(bmap,
1518 v_div + i, 1);
1519 bmap = isl_basic_map_drop_div(bmap, i);
1521 return bmap;
1524 /* Mark "bmap" as final, without checking for obviously redundant
1525 * integer divisions. This function should be used when "bmap"
1526 * is known not to involve any such integer divisions.
1528 __isl_give isl_basic_map *isl_basic_map_mark_final(
1529 __isl_take isl_basic_map *bmap)
1531 if (!bmap)
1532 return NULL;
1533 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1534 return bmap;
1537 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1539 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1541 bmap = remove_redundant_divs(bmap);
1542 bmap = isl_basic_map_mark_final(bmap);
1543 return bmap;
1546 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1548 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1551 /* Remove definition of any div that is defined in terms of the given variable.
1552 * The div itself is not removed. Functions such as
1553 * eliminate_divs_ineq depend on the other divs remaining in place.
1555 static __isl_give isl_basic_map *remove_dependent_vars(
1556 __isl_take isl_basic_map *bmap, int pos)
1558 int i;
1560 if (!bmap)
1561 return NULL;
1563 for (i = 0; i < bmap->n_div; ++i) {
1564 if (isl_int_is_zero(bmap->div[i][0]))
1565 continue;
1566 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1567 continue;
1568 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1569 if (!bmap)
1570 return NULL;
1572 return bmap;
1575 /* Eliminate the specified variables from the constraints using
1576 * Fourier-Motzkin. The variables themselves are not removed.
1578 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1579 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1581 int d;
1582 int i, j, k;
1583 isl_size total;
1584 int need_gauss = 0;
1586 if (n == 0)
1587 return bmap;
1588 total = isl_basic_map_dim(bmap, isl_dim_all);
1589 if (total < 0)
1590 return isl_basic_map_free(bmap);
1592 bmap = isl_basic_map_cow(bmap);
1593 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1594 bmap = remove_dependent_vars(bmap, d);
1595 if (!bmap)
1596 return NULL;
1598 for (d = pos + n - 1;
1599 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1600 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1601 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1602 int n_lower, n_upper;
1603 if (!bmap)
1604 return NULL;
1605 for (i = 0; i < bmap->n_eq; ++i) {
1606 if (isl_int_is_zero(bmap->eq[i][1+d]))
1607 continue;
1608 bmap = eliminate_var_using_equality(bmap, d,
1609 bmap->eq[i], 0, NULL);
1610 if (isl_basic_map_drop_equality(bmap, i) < 0)
1611 return isl_basic_map_free(bmap);
1612 need_gauss = 1;
1613 break;
1615 if (i < bmap->n_eq)
1616 continue;
1617 n_lower = 0;
1618 n_upper = 0;
1619 for (i = 0; i < bmap->n_ineq; ++i) {
1620 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1621 n_lower++;
1622 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1623 n_upper++;
1625 bmap = isl_basic_map_extend_constraints(bmap,
1626 0, n_lower * n_upper);
1627 if (!bmap)
1628 goto error;
1629 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1630 int last;
1631 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1632 continue;
1633 last = -1;
1634 for (j = 0; j < i; ++j) {
1635 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1636 continue;
1637 last = j;
1638 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1639 isl_int_sgn(bmap->ineq[j][1+d]))
1640 continue;
1641 k = isl_basic_map_alloc_inequality(bmap);
1642 if (k < 0)
1643 goto error;
1644 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1645 1+total);
1646 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1647 1+d, 1+total, NULL);
1649 isl_basic_map_drop_inequality(bmap, i);
1650 i = last + 1;
1652 if (n_lower > 0 && n_upper > 0) {
1653 bmap = isl_basic_map_normalize_constraints(bmap);
1654 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1655 NULL, 0);
1656 bmap = isl_basic_map_gauss(bmap, NULL);
1657 bmap = isl_basic_map_remove_redundancies(bmap);
1658 need_gauss = 0;
1659 if (!bmap)
1660 goto error;
1661 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1662 break;
1665 if (need_gauss)
1666 bmap = isl_basic_map_gauss(bmap, NULL);
1667 return bmap;
1668 error:
1669 isl_basic_map_free(bmap);
1670 return NULL;
1673 struct isl_basic_set *isl_basic_set_eliminate_vars(
1674 struct isl_basic_set *bset, unsigned pos, unsigned n)
1676 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1677 pos, n));
1680 /* Eliminate the specified n dimensions starting at first from the
1681 * constraints, without removing the dimensions from the space.
1682 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1683 * Otherwise, they are projected out and the original space is restored.
1685 __isl_give isl_basic_map *isl_basic_map_eliminate(
1686 __isl_take isl_basic_map *bmap,
1687 enum isl_dim_type type, unsigned first, unsigned n)
1689 isl_space *space;
1691 if (!bmap)
1692 return NULL;
1693 if (n == 0)
1694 return bmap;
1696 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1697 return isl_basic_map_free(bmap);
1699 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1700 first += isl_basic_map_offset(bmap, type) - 1;
1701 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1702 return isl_basic_map_finalize(bmap);
1705 space = isl_basic_map_get_space(bmap);
1706 bmap = isl_basic_map_project_out(bmap, type, first, n);
1707 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1708 bmap = isl_basic_map_reset_space(bmap, space);
1709 return bmap;
1712 __isl_give isl_basic_set *isl_basic_set_eliminate(
1713 __isl_take isl_basic_set *bset,
1714 enum isl_dim_type type, unsigned first, unsigned n)
1716 return isl_basic_map_eliminate(bset, type, first, n);
1719 /* Remove all constraints from "bmap" that reference any unknown local
1720 * variables (directly or indirectly).
1722 * Dropping all constraints on a local variable will make it redundant,
1723 * so it will get removed implicitly by
1724 * isl_basic_map_drop_constraints_involving_dims. Some other local
1725 * variables may also end up becoming redundant if they only appear
1726 * in constraints together with the unknown local variable.
1727 * Therefore, start over after calling
1728 * isl_basic_map_drop_constraints_involving_dims.
1730 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1731 __isl_take isl_basic_map *bmap)
1733 isl_bool known;
1734 isl_size n_div;
1735 int i, o_div;
1737 known = isl_basic_map_divs_known(bmap);
1738 if (known < 0)
1739 return isl_basic_map_free(bmap);
1740 if (known)
1741 return bmap;
1743 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1744 if (n_div < 0)
1745 return isl_basic_map_free(bmap);
1746 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1748 for (i = 0; i < n_div; ++i) {
1749 known = isl_basic_map_div_is_known(bmap, i);
1750 if (known < 0)
1751 return isl_basic_map_free(bmap);
1752 if (known)
1753 continue;
1754 bmap = remove_dependent_vars(bmap, o_div + i);
1755 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1756 isl_dim_div, i, 1);
1757 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1758 if (n_div < 0)
1759 return isl_basic_map_free(bmap);
1760 i = -1;
1763 return bmap;
1766 /* Remove all constraints from "map" that reference any unknown local
1767 * variables (directly or indirectly).
1769 * Since constraints may get dropped from the basic maps,
1770 * they may no longer be disjoint from each other.
1772 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1773 __isl_take isl_map *map)
1775 int i;
1776 isl_bool known;
1778 known = isl_map_divs_known(map);
1779 if (known < 0)
1780 return isl_map_free(map);
1781 if (known)
1782 return map;
1784 map = isl_map_cow(map);
1785 if (!map)
1786 return NULL;
1788 for (i = 0; i < map->n; ++i) {
1789 map->p[i] =
1790 isl_basic_map_drop_constraint_involving_unknown_divs(
1791 map->p[i]);
1792 if (!map->p[i])
1793 return isl_map_free(map);
1796 if (map->n > 1)
1797 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1799 return map;
1802 /* Don't assume equalities are in order, because align_divs
1803 * may have changed the order of the divs.
1805 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1807 int d, i;
1808 unsigned total;
1810 total = isl_space_dim(bmap->dim, isl_dim_all);
1811 for (d = 0; d < total; ++d)
1812 elim[d] = -1;
1813 for (i = 0; i < bmap->n_eq; ++i) {
1814 for (d = total - 1; d >= 0; --d) {
1815 if (isl_int_is_zero(bmap->eq[i][1+d]))
1816 continue;
1817 elim[d] = i;
1818 break;
1823 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1824 int *elim)
1826 compute_elimination_index(bset_to_bmap(bset), elim);
1829 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1830 __isl_keep isl_basic_map *bmap, int *elim)
1832 int d;
1833 int copied = 0;
1834 unsigned total;
1836 total = isl_space_dim(bmap->dim, isl_dim_all);
1837 for (d = total - 1; d >= 0; --d) {
1838 if (isl_int_is_zero(src[1+d]))
1839 continue;
1840 if (elim[d] == -1)
1841 continue;
1842 if (!copied) {
1843 isl_seq_cpy(dst, src, 1 + total);
1844 copied = 1;
1846 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1848 return copied;
1851 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1852 __isl_keep isl_basic_set *bset, int *elim)
1854 return reduced_using_equalities(dst, src,
1855 bset_to_bmap(bset), elim);
1858 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1859 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1861 int i;
1862 int *elim;
1863 isl_size dim;
1865 if (!bset || !context)
1866 goto error;
1868 if (context->n_eq == 0) {
1869 isl_basic_set_free(context);
1870 return bset;
1873 bset = isl_basic_set_cow(bset);
1874 dim = isl_basic_set_dim(bset, isl_dim_set);
1875 if (dim < 0)
1876 goto error;
1878 elim = isl_alloc_array(bset->ctx, int, dim);
1879 if (!elim)
1880 goto error;
1881 set_compute_elimination_index(context, elim);
1882 for (i = 0; i < bset->n_eq; ++i)
1883 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1884 context, elim);
1885 for (i = 0; i < bset->n_ineq; ++i)
1886 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1887 context, elim);
1888 isl_basic_set_free(context);
1889 free(elim);
1890 bset = isl_basic_set_simplify(bset);
1891 bset = isl_basic_set_finalize(bset);
1892 return bset;
1893 error:
1894 isl_basic_set_free(bset);
1895 isl_basic_set_free(context);
1896 return NULL;
1899 /* For each inequality in "ineq" that is a shifted (more relaxed)
1900 * copy of an inequality in "context", mark the corresponding entry
1901 * in "row" with -1.
1902 * If an inequality only has a non-negative constant term, then
1903 * mark it as well.
1905 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1906 __isl_keep isl_basic_set *context, int *row)
1908 struct isl_constraint_index ci;
1909 int n_ineq;
1910 unsigned total;
1911 int k;
1913 if (!ineq || !context)
1914 return isl_stat_error;
1915 if (context->n_ineq == 0)
1916 return isl_stat_ok;
1917 if (setup_constraint_index(&ci, context) < 0)
1918 return isl_stat_error;
1920 n_ineq = isl_mat_rows(ineq);
1921 total = isl_mat_cols(ineq) - 1;
1922 for (k = 0; k < n_ineq; ++k) {
1923 int l;
1924 isl_bool redundant;
1926 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1927 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1928 row[k] = -1;
1929 continue;
1931 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1932 if (redundant < 0)
1933 goto error;
1934 if (!redundant)
1935 continue;
1936 row[k] = -1;
1938 constraint_index_free(&ci);
1939 return isl_stat_ok;
1940 error:
1941 constraint_index_free(&ci);
1942 return isl_stat_error;
1945 static __isl_give isl_basic_set *remove_shifted_constraints(
1946 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1948 struct isl_constraint_index ci;
1949 int k;
1951 if (!bset || !context)
1952 return bset;
1954 if (context->n_ineq == 0)
1955 return bset;
1956 if (setup_constraint_index(&ci, context) < 0)
1957 return bset;
1959 for (k = 0; k < bset->n_ineq; ++k) {
1960 isl_bool redundant;
1962 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1963 if (redundant < 0)
1964 goto error;
1965 if (!redundant)
1966 continue;
1967 bset = isl_basic_set_cow(bset);
1968 if (!bset)
1969 goto error;
1970 isl_basic_set_drop_inequality(bset, k);
1971 --k;
1973 constraint_index_free(&ci);
1974 return bset;
1975 error:
1976 constraint_index_free(&ci);
1977 return bset;
1980 /* Remove constraints from "bmap" that are identical to constraints
1981 * in "context" or that are more relaxed (greater constant term).
1983 * We perform the test for shifted copies on the pure constraints
1984 * in remove_shifted_constraints.
1986 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1987 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1989 isl_basic_set *bset, *bset_context;
1991 if (!bmap || !context)
1992 goto error;
1994 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1995 isl_basic_map_free(context);
1996 return bmap;
1999 context = isl_basic_map_align_divs(context, bmap);
2000 bmap = isl_basic_map_align_divs(bmap, context);
2002 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
2003 bset_context = isl_basic_map_underlying_set(context);
2004 bset = remove_shifted_constraints(bset, bset_context);
2005 isl_basic_set_free(bset_context);
2007 bmap = isl_basic_map_overlying_set(bset, bmap);
2009 return bmap;
2010 error:
2011 isl_basic_map_free(bmap);
2012 isl_basic_map_free(context);
2013 return NULL;
2016 /* Does the (linear part of a) constraint "c" involve any of the "len"
2017 * "relevant" dimensions?
2019 static int is_related(isl_int *c, int len, int *relevant)
2021 int i;
2023 for (i = 0; i < len; ++i) {
2024 if (!relevant[i])
2025 continue;
2026 if (!isl_int_is_zero(c[i]))
2027 return 1;
2030 return 0;
2033 /* Drop constraints from "bmap" that do not involve any of
2034 * the dimensions marked "relevant".
2036 static __isl_give isl_basic_map *drop_unrelated_constraints(
2037 __isl_take isl_basic_map *bmap, int *relevant)
2039 int i;
2040 isl_size dim;
2042 dim = isl_basic_map_dim(bmap, isl_dim_all);
2043 if (dim < 0)
2044 return isl_basic_map_free(bmap);
2045 for (i = 0; i < dim; ++i)
2046 if (!relevant[i])
2047 break;
2048 if (i >= dim)
2049 return bmap;
2051 for (i = bmap->n_eq - 1; i >= 0; --i)
2052 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2053 bmap = isl_basic_map_cow(bmap);
2054 if (isl_basic_map_drop_equality(bmap, i) < 0)
2055 return isl_basic_map_free(bmap);
2058 for (i = bmap->n_ineq - 1; i >= 0; --i)
2059 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2060 bmap = isl_basic_map_cow(bmap);
2061 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2062 return isl_basic_map_free(bmap);
2065 return bmap;
2068 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2070 * In particular, for any variable involved in the constraint,
2071 * find the actual group id from before and replace the group
2072 * of the corresponding variable by the minimal group of all
2073 * the variables involved in the constraint considered so far
2074 * (if this minimum is smaller) or replace the minimum by this group
2075 * (if the minimum is larger).
2077 * At the end, all the variables in "c" will (indirectly) point
2078 * to the minimal of the groups that they referred to originally.
2080 static void update_groups(int dim, int *group, isl_int *c)
2082 int j;
2083 int min = dim;
2085 for (j = 0; j < dim; ++j) {
2086 if (isl_int_is_zero(c[j]))
2087 continue;
2088 while (group[j] >= 0 && group[group[j]] != group[j])
2089 group[j] = group[group[j]];
2090 if (group[j] == min)
2091 continue;
2092 if (group[j] < min) {
2093 if (min >= 0 && min < dim)
2094 group[min] = group[j];
2095 min = group[j];
2096 } else
2097 group[group[j]] = min;
2101 /* Allocate an array of groups of variables, one for each variable
2102 * in "context", initialized to zero.
2104 static int *alloc_groups(__isl_keep isl_basic_set *context)
2106 isl_ctx *ctx;
2107 isl_size dim;
2109 dim = isl_basic_set_dim(context, isl_dim_set);
2110 if (dim < 0)
2111 return NULL;
2112 ctx = isl_basic_set_get_ctx(context);
2113 return isl_calloc_array(ctx, int, dim);
2116 /* Drop constraints from "bmap" that only involve variables that are
2117 * not related to any of the variables marked with a "-1" in "group".
2119 * We construct groups of variables that collect variables that
2120 * (indirectly) appear in some common constraint of "bmap".
2121 * Each group is identified by the first variable in the group,
2122 * except for the special group of variables that was already identified
2123 * in the input as -1 (or are related to those variables).
2124 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2125 * otherwise the group of i is the group of group[i].
2127 * We first initialize groups for the remaining variables.
2128 * Then we iterate over the constraints of "bmap" and update the
2129 * group of the variables in the constraint by the smallest group.
2130 * Finally, we resolve indirect references to groups by running over
2131 * the variables.
2133 * After computing the groups, we drop constraints that do not involve
2134 * any variables in the -1 group.
2136 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2137 __isl_take isl_basic_map *bmap, __isl_take int *group)
2139 isl_size dim;
2140 int i;
2141 int last;
2143 dim = isl_basic_map_dim(bmap, isl_dim_all);
2144 if (dim < 0)
2145 return isl_basic_map_free(bmap);
2147 last = -1;
2148 for (i = 0; i < dim; ++i)
2149 if (group[i] >= 0)
2150 last = group[i] = i;
2151 if (last < 0) {
2152 free(group);
2153 return bmap;
2156 for (i = 0; i < bmap->n_eq; ++i)
2157 update_groups(dim, group, bmap->eq[i] + 1);
2158 for (i = 0; i < bmap->n_ineq; ++i)
2159 update_groups(dim, group, bmap->ineq[i] + 1);
2161 for (i = 0; i < dim; ++i)
2162 if (group[i] >= 0)
2163 group[i] = group[group[i]];
2165 for (i = 0; i < dim; ++i)
2166 group[i] = group[i] == -1;
2168 bmap = drop_unrelated_constraints(bmap, group);
2170 free(group);
2171 return bmap;
2174 /* Drop constraints from "context" that are irrelevant for computing
2175 * the gist of "bset".
2177 * In particular, drop constraints in variables that are not related
2178 * to any of the variables involved in the constraints of "bset"
2179 * in the sense that there is no sequence of constraints that connects them.
2181 * We first mark all variables that appear in "bset" as belonging
2182 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2184 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2185 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2187 int *group;
2188 isl_size dim;
2189 int i, j;
2191 dim = isl_basic_set_dim(bset, isl_dim_set);
2192 if (!context || dim < 0)
2193 return isl_basic_set_free(context);
2195 group = alloc_groups(context);
2197 if (!group)
2198 return isl_basic_set_free(context);
2200 for (i = 0; i < dim; ++i) {
2201 for (j = 0; j < bset->n_eq; ++j)
2202 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2203 break;
2204 if (j < bset->n_eq) {
2205 group[i] = -1;
2206 continue;
2208 for (j = 0; j < bset->n_ineq; ++j)
2209 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2210 break;
2211 if (j < bset->n_ineq)
2212 group[i] = -1;
2215 return isl_basic_map_drop_unrelated_constraints(context, group);
2218 /* Drop constraints from "context" that are irrelevant for computing
2219 * the gist of the inequalities "ineq".
2220 * Inequalities in "ineq" for which the corresponding element of row
2221 * is set to -1 have already been marked for removal and should be ignored.
2223 * In particular, drop constraints in variables that are not related
2224 * to any of the variables involved in "ineq"
2225 * in the sense that there is no sequence of constraints that connects them.
2227 * We first mark all variables that appear in "bset" as belonging
2228 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2230 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2231 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2233 int *group;
2234 isl_size dim;
2235 int i, j, n;
2237 dim = isl_basic_set_dim(context, isl_dim_set);
2238 if (dim < 0 || !ineq)
2239 return isl_basic_set_free(context);
2241 group = alloc_groups(context);
2243 if (!group)
2244 return isl_basic_set_free(context);
2246 n = isl_mat_rows(ineq);
2247 for (i = 0; i < dim; ++i) {
2248 for (j = 0; j < n; ++j) {
2249 if (row[j] < 0)
2250 continue;
2251 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2252 break;
2254 if (j < n)
2255 group[i] = -1;
2258 return isl_basic_map_drop_unrelated_constraints(context, group);
2261 /* Do all "n" entries of "row" contain a negative value?
2263 static int all_neg(int *row, int n)
2265 int i;
2267 for (i = 0; i < n; ++i)
2268 if (row[i] >= 0)
2269 return 0;
2271 return 1;
2274 /* Update the inequalities in "bset" based on the information in "row"
2275 * and "tab".
2277 * In particular, the array "row" contains either -1, meaning that
2278 * the corresponding inequality of "bset" is redundant, or the index
2279 * of an inequality in "tab".
2281 * If the row entry is -1, then drop the inequality.
2282 * Otherwise, if the constraint is marked redundant in the tableau,
2283 * then drop the inequality. Similarly, if it is marked as an equality
2284 * in the tableau, then turn the inequality into an equality and
2285 * perform Gaussian elimination.
2287 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2288 __isl_keep int *row, struct isl_tab *tab)
2290 int i;
2291 unsigned n_ineq;
2292 unsigned n_eq;
2293 int found_equality = 0;
2295 if (!bset)
2296 return NULL;
2297 if (tab && tab->empty)
2298 return isl_basic_set_set_to_empty(bset);
2300 n_ineq = bset->n_ineq;
2301 for (i = n_ineq - 1; i >= 0; --i) {
2302 if (row[i] < 0) {
2303 if (isl_basic_set_drop_inequality(bset, i) < 0)
2304 return isl_basic_set_free(bset);
2305 continue;
2307 if (!tab)
2308 continue;
2309 n_eq = tab->n_eq;
2310 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2311 isl_basic_map_inequality_to_equality(bset, i);
2312 found_equality = 1;
2313 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2314 if (isl_basic_set_drop_inequality(bset, i) < 0)
2315 return isl_basic_set_free(bset);
2319 if (found_equality)
2320 bset = isl_basic_set_gauss(bset, NULL);
2321 bset = isl_basic_set_finalize(bset);
2322 return bset;
2325 /* Update the inequalities in "bset" based on the information in "row"
2326 * and "tab" and free all arguments (other than "bset").
2328 static __isl_give isl_basic_set *update_ineq_free(
2329 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2330 __isl_take isl_basic_set *context, __isl_take int *row,
2331 struct isl_tab *tab)
2333 isl_mat_free(ineq);
2334 isl_basic_set_free(context);
2336 bset = update_ineq(bset, row, tab);
2338 free(row);
2339 isl_tab_free(tab);
2340 return bset;
2343 /* Remove all information from bset that is redundant in the context
2344 * of context.
2345 * "ineq" contains the (possibly transformed) inequalities of "bset",
2346 * in the same order.
2347 * The (explicit) equalities of "bset" are assumed to have been taken
2348 * into account by the transformation such that only the inequalities
2349 * are relevant.
2350 * "context" is assumed not to be empty.
2352 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2353 * A value of -1 means that the inequality is obviously redundant and may
2354 * not even appear in "tab".
2356 * We first mark the inequalities of "bset"
2357 * that are obviously redundant with respect to some inequality in "context".
2358 * Then we remove those constraints from "context" that have become
2359 * irrelevant for computing the gist of "bset".
2360 * Note that this removal of constraints cannot be replaced by
2361 * a factorization because factors in "bset" may still be connected
2362 * to each other through constraints in "context".
2364 * If there are any inequalities left, we construct a tableau for
2365 * the context and then add the inequalities of "bset".
2366 * Before adding these inequalities, we freeze all constraints such that
2367 * they won't be considered redundant in terms of the constraints of "bset".
2368 * Then we detect all redundant constraints (among the
2369 * constraints that weren't frozen), first by checking for redundancy in the
2370 * the tableau and then by checking if replacing a constraint by its negation
2371 * would lead to an empty set. This last step is fairly expensive
2372 * and could be optimized by more reuse of the tableau.
2373 * Finally, we update bset according to the results.
2375 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2376 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2378 int i, r;
2379 int *row = NULL;
2380 isl_ctx *ctx;
2381 isl_basic_set *combined = NULL;
2382 struct isl_tab *tab = NULL;
2383 unsigned n_eq, context_ineq;
2385 if (!bset || !ineq || !context)
2386 goto error;
2388 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2389 isl_basic_set_free(context);
2390 isl_mat_free(ineq);
2391 return bset;
2394 ctx = isl_basic_set_get_ctx(context);
2395 row = isl_calloc_array(ctx, int, bset->n_ineq);
2396 if (!row)
2397 goto error;
2399 if (mark_shifted_constraints(ineq, context, row) < 0)
2400 goto error;
2401 if (all_neg(row, bset->n_ineq))
2402 return update_ineq_free(bset, ineq, context, row, NULL);
2404 context = drop_irrelevant_constraints_marked(context, ineq, row);
2405 if (!context)
2406 goto error;
2407 if (isl_basic_set_plain_is_universe(context))
2408 return update_ineq_free(bset, ineq, context, row, NULL);
2410 n_eq = context->n_eq;
2411 context_ineq = context->n_ineq;
2412 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2413 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2414 tab = isl_tab_from_basic_set(combined, 0);
2415 for (i = 0; i < context_ineq; ++i)
2416 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2417 goto error;
2418 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2419 goto error;
2420 r = context_ineq;
2421 for (i = 0; i < bset->n_ineq; ++i) {
2422 if (row[i] < 0)
2423 continue;
2424 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2425 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2426 goto error;
2427 row[i] = r++;
2429 if (isl_tab_detect_implicit_equalities(tab) < 0)
2430 goto error;
2431 if (isl_tab_detect_redundant(tab) < 0)
2432 goto error;
2433 for (i = bset->n_ineq - 1; i >= 0; --i) {
2434 isl_basic_set *test;
2435 int is_empty;
2437 if (row[i] < 0)
2438 continue;
2439 r = row[i];
2440 if (tab->con[n_eq + r].is_redundant)
2441 continue;
2442 test = isl_basic_set_dup(combined);
2443 test = isl_inequality_negate(test, r);
2444 test = isl_basic_set_update_from_tab(test, tab);
2445 is_empty = isl_basic_set_is_empty(test);
2446 isl_basic_set_free(test);
2447 if (is_empty < 0)
2448 goto error;
2449 if (is_empty)
2450 tab->con[n_eq + r].is_redundant = 1;
2452 bset = update_ineq_free(bset, ineq, context, row, tab);
2453 if (bset) {
2454 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2455 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2458 isl_basic_set_free(combined);
2459 return bset;
2460 error:
2461 free(row);
2462 isl_mat_free(ineq);
2463 isl_tab_free(tab);
2464 isl_basic_set_free(combined);
2465 isl_basic_set_free(context);
2466 isl_basic_set_free(bset);
2467 return NULL;
2470 /* Extract the inequalities of "bset" as an isl_mat.
2472 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2474 isl_size total;
2475 isl_ctx *ctx;
2476 isl_mat *ineq;
2478 total = isl_basic_set_dim(bset, isl_dim_all);
2479 if (total < 0)
2480 return NULL;
2482 ctx = isl_basic_set_get_ctx(bset);
2483 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2484 0, 1 + total);
2486 return ineq;
2489 /* Remove all information from "bset" that is redundant in the context
2490 * of "context", for the case where both "bset" and "context" are
2491 * full-dimensional.
2493 static __isl_give isl_basic_set *uset_gist_uncompressed(
2494 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2496 isl_mat *ineq;
2498 ineq = extract_ineq(bset);
2499 return uset_gist_full(bset, ineq, context);
2502 /* Remove all information from "bset" that is redundant in the context
2503 * of "context", for the case where the combined equalities of
2504 * "bset" and "context" allow for a compression that can be obtained
2505 * by preapplication of "T".
2507 * "bset" itself is not transformed by "T". Instead, the inequalities
2508 * are extracted from "bset" and those are transformed by "T".
2509 * uset_gist_full then determines which of the transformed inequalities
2510 * are redundant with respect to the transformed "context" and removes
2511 * the corresponding inequalities from "bset".
2513 * After preapplying "T" to the inequalities, any common factor is
2514 * removed from the coefficients. If this results in a tightening
2515 * of the constant term, then the same tightening is applied to
2516 * the corresponding untransformed inequality in "bset".
2517 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2519 * g f'(x) + r >= 0
2521 * with 0 <= r < g, then it is equivalent to
2523 * f'(x) >= 0
2525 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2526 * subspace compressed by T since the latter would be transformed to
2528 * g f'(x) >= 0
2530 static __isl_give isl_basic_set *uset_gist_compressed(
2531 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2532 __isl_take isl_mat *T)
2534 isl_ctx *ctx;
2535 isl_mat *ineq;
2536 int i, n_row, n_col;
2537 isl_int rem;
2539 ineq = extract_ineq(bset);
2540 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2541 context = isl_basic_set_preimage(context, T);
2543 if (!ineq || !context)
2544 goto error;
2545 if (isl_basic_set_plain_is_empty(context)) {
2546 isl_mat_free(ineq);
2547 isl_basic_set_free(context);
2548 return isl_basic_set_set_to_empty(bset);
2551 ctx = isl_mat_get_ctx(ineq);
2552 n_row = isl_mat_rows(ineq);
2553 n_col = isl_mat_cols(ineq);
2554 isl_int_init(rem);
2555 for (i = 0; i < n_row; ++i) {
2556 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2557 if (isl_int_is_zero(ctx->normalize_gcd))
2558 continue;
2559 if (isl_int_is_one(ctx->normalize_gcd))
2560 continue;
2561 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2562 ctx->normalize_gcd, n_col - 1);
2563 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2564 isl_int_fdiv_q(ineq->row[i][0],
2565 ineq->row[i][0], ctx->normalize_gcd);
2566 if (isl_int_is_zero(rem))
2567 continue;
2568 bset = isl_basic_set_cow(bset);
2569 if (!bset)
2570 break;
2571 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2573 isl_int_clear(rem);
2575 return uset_gist_full(bset, ineq, context);
2576 error:
2577 isl_mat_free(ineq);
2578 isl_basic_set_free(context);
2579 isl_basic_set_free(bset);
2580 return NULL;
2583 /* Project "bset" onto the variables that are involved in "template".
2585 static __isl_give isl_basic_set *project_onto_involved(
2586 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2588 int i;
2589 isl_size n;
2591 n = isl_basic_set_dim(template, isl_dim_set);
2592 if (n < 0 || !template)
2593 return isl_basic_set_free(bset);
2595 for (i = 0; i < n; ++i) {
2596 isl_bool involved;
2598 involved = isl_basic_set_involves_dims(template,
2599 isl_dim_set, i, 1);
2600 if (involved < 0)
2601 return isl_basic_set_free(bset);
2602 if (involved)
2603 continue;
2604 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2607 return bset;
2610 /* Remove all information from bset that is redundant in the context
2611 * of context. In particular, equalities that are linear combinations
2612 * of those in context are removed. Then the inequalities that are
2613 * redundant in the context of the equalities and inequalities of
2614 * context are removed.
2616 * First of all, we drop those constraints from "context"
2617 * that are irrelevant for computing the gist of "bset".
2618 * Alternatively, we could factorize the intersection of "context" and "bset".
2620 * We first compute the intersection of the integer affine hulls
2621 * of "bset" and "context",
2622 * compute the gist inside this intersection and then reduce
2623 * the constraints with respect to the equalities of the context
2624 * that only involve variables already involved in the input.
2626 * If two constraints are mutually redundant, then uset_gist_full
2627 * will remove the second of those constraints. We therefore first
2628 * sort the constraints so that constraints not involving existentially
2629 * quantified variables are given precedence over those that do.
2630 * We have to perform this sorting before the variable compression,
2631 * because that may effect the order of the variables.
2633 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2634 __isl_take isl_basic_set *context)
2636 isl_mat *eq;
2637 isl_mat *T;
2638 isl_basic_set *aff;
2639 isl_basic_set *aff_context;
2640 isl_size total;
2642 total = isl_basic_set_dim(bset, isl_dim_all);
2643 if (total < 0 || !context)
2644 goto error;
2646 context = drop_irrelevant_constraints(context, bset);
2648 bset = isl_basic_set_detect_equalities(bset);
2649 aff = isl_basic_set_copy(bset);
2650 aff = isl_basic_set_plain_affine_hull(aff);
2651 context = isl_basic_set_detect_equalities(context);
2652 aff_context = isl_basic_set_copy(context);
2653 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2654 aff = isl_basic_set_intersect(aff, aff_context);
2655 if (!aff)
2656 goto error;
2657 if (isl_basic_set_plain_is_empty(aff)) {
2658 isl_basic_set_free(bset);
2659 isl_basic_set_free(context);
2660 return aff;
2662 bset = isl_basic_set_sort_constraints(bset);
2663 if (aff->n_eq == 0) {
2664 isl_basic_set_free(aff);
2665 return uset_gist_uncompressed(bset, context);
2667 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2668 eq = isl_mat_cow(eq);
2669 T = isl_mat_variable_compression(eq, NULL);
2670 isl_basic_set_free(aff);
2671 if (T && T->n_col == 0) {
2672 isl_mat_free(T);
2673 isl_basic_set_free(context);
2674 return isl_basic_set_set_to_empty(bset);
2677 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2678 aff_context = project_onto_involved(aff_context, bset);
2680 bset = uset_gist_compressed(bset, context, T);
2681 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2683 if (bset) {
2684 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2685 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2688 return bset;
2689 error:
2690 isl_basic_set_free(bset);
2691 isl_basic_set_free(context);
2692 return NULL;
2695 /* Return the number of equality constraints in "bmap" that involve
2696 * local variables. This function assumes that Gaussian elimination
2697 * has been applied to the equality constraints.
2699 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2701 int i;
2702 isl_size total, n_div;
2704 if (!bmap)
2705 return -1;
2707 if (bmap->n_eq == 0)
2708 return 0;
2710 total = isl_basic_map_dim(bmap, isl_dim_all);
2711 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2712 if (total < 0 || n_div < 0)
2713 return -1;
2714 total -= n_div;
2716 for (i = 0; i < bmap->n_eq; ++i)
2717 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2718 n_div) == -1)
2719 return i;
2721 return bmap->n_eq;
2724 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2725 * The constraints are assumed not to involve any local variables.
2727 static __isl_give isl_basic_map *basic_map_from_equalities(
2728 __isl_take isl_space *space, __isl_take isl_mat *eq)
2730 int i, k;
2731 isl_size total;
2732 isl_basic_map *bmap = NULL;
2734 total = isl_space_dim(space, isl_dim_all);
2735 if (total < 0 || !eq)
2736 goto error;
2738 if (1 + total != eq->n_col)
2739 isl_die(isl_space_get_ctx(space), isl_error_internal,
2740 "unexpected number of columns", goto error);
2742 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2743 0, eq->n_row, 0);
2744 for (i = 0; i < eq->n_row; ++i) {
2745 k = isl_basic_map_alloc_equality(bmap);
2746 if (k < 0)
2747 goto error;
2748 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2751 isl_space_free(space);
2752 isl_mat_free(eq);
2753 return bmap;
2754 error:
2755 isl_space_free(space);
2756 isl_mat_free(eq);
2757 isl_basic_map_free(bmap);
2758 return NULL;
2761 /* Construct and return a variable compression based on the equality
2762 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2763 * "n1" is the number of (initial) equality constraints in "bmap1"
2764 * that do involve local variables.
2765 * "n2" is the number of (initial) equality constraints in "bmap2"
2766 * that do involve local variables.
2767 * "total" is the total number of other variables.
2768 * This function assumes that Gaussian elimination
2769 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2770 * such that the equality constraints not involving local variables
2771 * are those that start at "n1" or "n2".
2773 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2774 * then simply compute the compression based on the equality constraints
2775 * in the other basic map.
2776 * Otherwise, combine the equality constraints from both into a new
2777 * basic map such that Gaussian elimination can be applied to this combination
2778 * and then construct a variable compression from the resulting
2779 * equality constraints.
2781 static __isl_give isl_mat *combined_variable_compression(
2782 __isl_keep isl_basic_map *bmap1, int n1,
2783 __isl_keep isl_basic_map *bmap2, int n2, int total)
2785 isl_ctx *ctx;
2786 isl_mat *E1, *E2, *V;
2787 isl_basic_map *bmap;
2789 ctx = isl_basic_map_get_ctx(bmap1);
2790 if (bmap1->n_eq == n1) {
2791 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2792 n2, bmap2->n_eq - n2, 0, 1 + total);
2793 return isl_mat_variable_compression(E2, NULL);
2795 if (bmap2->n_eq == n2) {
2796 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2797 n1, bmap1->n_eq - n1, 0, 1 + total);
2798 return isl_mat_variable_compression(E1, NULL);
2800 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2801 n1, bmap1->n_eq - n1, 0, 1 + total);
2802 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2803 n2, bmap2->n_eq - n2, 0, 1 + total);
2804 E1 = isl_mat_concat(E1, E2);
2805 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2806 bmap = isl_basic_map_gauss(bmap, NULL);
2807 if (!bmap)
2808 return NULL;
2809 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2810 V = isl_mat_variable_compression(E1, NULL);
2811 isl_basic_map_free(bmap);
2813 return V;
2816 /* Extract the stride constraints from "bmap", compressed
2817 * with respect to both the stride constraints in "context" and
2818 * the remaining equality constraints in both "bmap" and "context".
2819 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2820 * "context_n_eq" is the number of (initial) stride constraints in "context".
2822 * Let x be all variables in "bmap" (and "context") other than the local
2823 * variables. First compute a variable compression
2825 * x = V x'
2827 * based on the non-stride equality constraints in "bmap" and "context".
2828 * Consider the stride constraints of "context",
2830 * A(x) + B(y) = 0
2832 * with y the local variables and plug in the variable compression,
2833 * resulting in
2835 * A(V x') + B(y) = 0
2837 * Use these constraints to compute a parameter compression on x'
2839 * x' = T x''
2841 * Now consider the stride constraints of "bmap"
2843 * C(x) + D(y) = 0
2845 * and plug in x = V*T x''.
2846 * That is, return A = [C*V*T D].
2848 static __isl_give isl_mat *extract_compressed_stride_constraints(
2849 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2850 __isl_keep isl_basic_map *context, int context_n_eq)
2852 isl_size total, n_div;
2853 isl_ctx *ctx;
2854 isl_mat *A, *B, *T, *V;
2856 total = isl_basic_map_dim(context, isl_dim_all);
2857 n_div = isl_basic_map_dim(context, isl_dim_div);
2858 if (total < 0 || n_div < 0)
2859 return NULL;
2860 total -= n_div;
2862 ctx = isl_basic_map_get_ctx(bmap);
2864 V = combined_variable_compression(bmap, bmap_n_eq,
2865 context, context_n_eq, total);
2867 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2868 B = isl_mat_sub_alloc6(ctx, context->eq,
2869 0, context_n_eq, 1 + total, n_div);
2870 A = isl_mat_product(A, isl_mat_copy(V));
2871 T = isl_mat_parameter_compression_ext(A, B);
2872 T = isl_mat_product(V, T);
2874 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2875 if (n_div < 0)
2876 T = isl_mat_free(T);
2877 else
2878 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2880 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2881 0, bmap_n_eq, 0, 1 + total + n_div);
2882 A = isl_mat_product(A, T);
2884 return A;
2887 /* Remove the prime factors from *g that have an exponent that
2888 * is strictly smaller than the exponent in "c".
2889 * All exponents in *g are known to be smaller than or equal
2890 * to those in "c".
2892 * That is, if *g is equal to
2894 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2896 * and "c" is equal to
2898 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2900 * then update *g to
2902 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2903 * p_n^{e_n * (e_n = f_n)}
2905 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2906 * neither does the gcd of *g and c / *g.
2907 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2908 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2909 * Dividing *g by this gcd therefore strictly reduces the exponent
2910 * of the prime factors that need to be removed, while leaving the
2911 * other prime factors untouched.
2912 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2913 * removes all undesired factors, without removing any others.
2915 static void remove_incomplete_powers(isl_int *g, isl_int c)
2917 isl_int t;
2919 isl_int_init(t);
2920 for (;;) {
2921 isl_int_divexact(t, c, *g);
2922 isl_int_gcd(t, t, *g);
2923 if (isl_int_is_one(t))
2924 break;
2925 isl_int_divexact(*g, *g, t);
2927 isl_int_clear(t);
2930 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2931 * of the same stride constraints in a compressed space that exploits
2932 * all equalities in the context and the other equalities in "bmap".
2934 * If the stride constraints of "bmap" are of the form
2936 * C(x) + D(y) = 0
2938 * then A is of the form
2940 * B(x') + D(y) = 0
2942 * If any of these constraints involves only a single local variable y,
2943 * then the constraint appears as
2945 * f(x) + m y_i = 0
2947 * in "bmap" and as
2949 * h(x') + m y_i = 0
2951 * in "A".
2953 * Let g be the gcd of m and the coefficients of h.
2954 * Then, in particular, g is a divisor of the coefficients of h and
2956 * f(x) = h(x')
2958 * is known to be a multiple of g.
2959 * If some prime factor in m appears with the same exponent in g,
2960 * then it can be removed from m because f(x) is already known
2961 * to be a multiple of g and therefore in particular of this power
2962 * of the prime factors.
2963 * Prime factors that appear with a smaller exponent in g cannot
2964 * be removed from m.
2965 * Let g' be the divisor of g containing all prime factors that
2966 * appear with the same exponent in m and g, then
2968 * f(x) + m y_i = 0
2970 * can be replaced by
2972 * f(x) + m/g' y_i' = 0
2974 * Note that (if g' != 1) this changes the explicit representation
2975 * of y_i to that of y_i', so the integer division at position i
2976 * is marked unknown and later recomputed by a call to
2977 * isl_basic_map_gauss.
2979 static __isl_give isl_basic_map *reduce_stride_constraints(
2980 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2982 int i;
2983 isl_size total, n_div;
2984 int any = 0;
2985 isl_int gcd;
2987 total = isl_basic_map_dim(bmap, isl_dim_all);
2988 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2989 if (total < 0 || n_div < 0 || !A)
2990 return isl_basic_map_free(bmap);
2991 total -= n_div;
2993 isl_int_init(gcd);
2994 for (i = 0; i < n; ++i) {
2995 int div;
2997 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2998 if (div < 0)
2999 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
3000 "equality constraints modified unexpectedly",
3001 goto error);
3002 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
3003 n_div - div - 1) != -1)
3004 continue;
3005 if (isl_mat_row_gcd(A, i, &gcd) < 0)
3006 goto error;
3007 if (isl_int_is_one(gcd))
3008 continue;
3009 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
3010 if (isl_int_is_one(gcd))
3011 continue;
3012 isl_int_divexact(bmap->eq[i][1 + total + div],
3013 bmap->eq[i][1 + total + div], gcd);
3014 bmap = isl_basic_map_mark_div_unknown(bmap, div);
3015 if (!bmap)
3016 goto error;
3017 any = 1;
3019 isl_int_clear(gcd);
3021 if (any)
3022 bmap = isl_basic_map_gauss(bmap, NULL);
3024 return bmap;
3025 error:
3026 isl_int_clear(gcd);
3027 isl_basic_map_free(bmap);
3028 return NULL;
3031 /* Simplify the stride constraints in "bmap" based on
3032 * the remaining equality constraints in "bmap" and all equality
3033 * constraints in "context".
3034 * Only do this if both "bmap" and "context" have stride constraints.
3036 * First extract a copy of the stride constraints in "bmap" in a compressed
3037 * space exploiting all the other equality constraints and then
3038 * use this compressed copy to simplify the original stride constraints.
3040 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
3041 __isl_keep isl_basic_map *context)
3043 int bmap_n_eq, context_n_eq;
3044 isl_mat *A;
3046 if (!bmap || !context)
3047 return isl_basic_map_free(bmap);
3049 bmap_n_eq = n_div_eq(bmap);
3050 context_n_eq = n_div_eq(context);
3052 if (bmap_n_eq < 0 || context_n_eq < 0)
3053 return isl_basic_map_free(bmap);
3054 if (bmap_n_eq == 0 || context_n_eq == 0)
3055 return bmap;
3057 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3058 context, context_n_eq);
3059 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3061 isl_mat_free(A);
3063 return bmap;
3066 /* Return a basic map that has the same intersection with "context" as "bmap"
3067 * and that is as "simple" as possible.
3069 * The core computation is performed on the pure constraints.
3070 * When we add back the meaning of the integer divisions, we need
3071 * to (re)introduce the div constraints. If we happen to have
3072 * discovered that some of these integer divisions are equal to
3073 * some affine combination of other variables, then these div
3074 * constraints may end up getting simplified in terms of the equalities,
3075 * resulting in extra inequalities on the other variables that
3076 * may have been removed already or that may not even have been
3077 * part of the input. We try and remove those constraints of
3078 * this form that are most obviously redundant with respect to
3079 * the context. We also remove those div constraints that are
3080 * redundant with respect to the other constraints in the result.
3082 * The stride constraints among the equality constraints in "bmap" are
3083 * also simplified with respecting to the other equality constraints
3084 * in "bmap" and with respect to all equality constraints in "context".
3086 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3087 __isl_take isl_basic_map *context)
3089 isl_basic_set *bset, *eq;
3090 isl_basic_map *eq_bmap;
3091 isl_size total, n_div, n_div_bmap;
3092 unsigned extra, n_eq, n_ineq;
3094 if (!bmap || !context)
3095 goto error;
3097 if (isl_basic_map_plain_is_universe(bmap)) {
3098 isl_basic_map_free(context);
3099 return bmap;
3101 if (isl_basic_map_plain_is_empty(context)) {
3102 isl_space *space = isl_basic_map_get_space(bmap);
3103 isl_basic_map_free(bmap);
3104 isl_basic_map_free(context);
3105 return isl_basic_map_universe(space);
3107 if (isl_basic_map_plain_is_empty(bmap)) {
3108 isl_basic_map_free(context);
3109 return bmap;
3112 bmap = isl_basic_map_remove_redundancies(bmap);
3113 context = isl_basic_map_remove_redundancies(context);
3114 context = isl_basic_map_align_divs(context, bmap);
3116 n_div = isl_basic_map_dim(context, isl_dim_div);
3117 total = isl_basic_map_dim(bmap, isl_dim_all);
3118 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
3119 if (n_div < 0 || total < 0 || n_div_bmap < 0)
3120 goto error;
3121 extra = n_div - n_div_bmap;
3123 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3124 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3125 bset = uset_gist(bset,
3126 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3127 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3129 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3130 isl_basic_set_plain_is_empty(bset)) {
3131 isl_basic_map_free(context);
3132 return isl_basic_map_overlying_set(bset, bmap);
3135 n_eq = bset->n_eq;
3136 n_ineq = bset->n_ineq;
3137 eq = isl_basic_set_copy(bset);
3138 eq = isl_basic_set_cow(eq);
3139 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3140 eq = isl_basic_set_free(eq);
3141 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3142 bset = isl_basic_set_free(bset);
3144 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3145 eq_bmap = gist_strides(eq_bmap, context);
3146 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3147 bmap = isl_basic_map_overlying_set(bset, bmap);
3148 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3149 bmap = isl_basic_map_remove_redundancies(bmap);
3151 return bmap;
3152 error:
3153 isl_basic_map_free(bmap);
3154 isl_basic_map_free(context);
3155 return NULL;
3159 * Assumes context has no implicit divs.
3161 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3162 __isl_take isl_basic_map *context)
3164 int i;
3166 if (!map || !context)
3167 goto error;
3169 if (isl_basic_map_plain_is_empty(context)) {
3170 isl_space *space = isl_map_get_space(map);
3171 isl_map_free(map);
3172 isl_basic_map_free(context);
3173 return isl_map_universe(space);
3176 context = isl_basic_map_remove_redundancies(context);
3177 map = isl_map_cow(map);
3178 if (!map || !context)
3179 goto error;
3180 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3181 map = isl_map_compute_divs(map);
3182 if (!map)
3183 goto error;
3184 for (i = map->n - 1; i >= 0; --i) {
3185 map->p[i] = isl_basic_map_gist(map->p[i],
3186 isl_basic_map_copy(context));
3187 if (!map->p[i])
3188 goto error;
3189 if (isl_basic_map_plain_is_empty(map->p[i])) {
3190 isl_basic_map_free(map->p[i]);
3191 if (i != map->n - 1)
3192 map->p[i] = map->p[map->n - 1];
3193 map->n--;
3196 isl_basic_map_free(context);
3197 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3198 return map;
3199 error:
3200 isl_map_free(map);
3201 isl_basic_map_free(context);
3202 return NULL;
3205 /* Drop all inequalities from "bmap" that also appear in "context".
3206 * "context" is assumed to have only known local variables and
3207 * the initial local variables of "bmap" are assumed to be the same
3208 * as those of "context".
3209 * The constraints of both "bmap" and "context" are assumed
3210 * to have been sorted using isl_basic_map_sort_constraints.
3212 * Run through the inequality constraints of "bmap" and "context"
3213 * in sorted order.
3214 * If a constraint of "bmap" involves variables not in "context",
3215 * then it cannot appear in "context".
3216 * If a matching constraint is found, it is removed from "bmap".
3218 static __isl_give isl_basic_map *drop_inequalities(
3219 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3221 int i1, i2;
3222 isl_size total, bmap_total;
3223 unsigned extra;
3225 total = isl_basic_map_dim(context, isl_dim_all);
3226 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3227 if (total < 0 || bmap_total < 0)
3228 return isl_basic_map_free(bmap);
3230 extra = bmap_total - total;
3232 i1 = bmap->n_ineq - 1;
3233 i2 = context->n_ineq - 1;
3234 while (bmap && i1 >= 0 && i2 >= 0) {
3235 int cmp;
3237 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3238 extra) != -1) {
3239 --i1;
3240 continue;
3242 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3243 context->ineq[i2]);
3244 if (cmp < 0) {
3245 --i2;
3246 continue;
3248 if (cmp > 0) {
3249 --i1;
3250 continue;
3252 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3253 bmap = isl_basic_map_cow(bmap);
3254 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3255 bmap = isl_basic_map_free(bmap);
3257 --i1;
3258 --i2;
3261 return bmap;
3264 /* Drop all equalities from "bmap" that also appear in "context".
3265 * "context" is assumed to have only known local variables and
3266 * the initial local variables of "bmap" are assumed to be the same
3267 * as those of "context".
3269 * Run through the equality constraints of "bmap" and "context"
3270 * in sorted order.
3271 * If a constraint of "bmap" involves variables not in "context",
3272 * then it cannot appear in "context".
3273 * If a matching constraint is found, it is removed from "bmap".
3275 static __isl_give isl_basic_map *drop_equalities(
3276 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3278 int i1, i2;
3279 isl_size total, bmap_total;
3280 unsigned extra;
3282 total = isl_basic_map_dim(context, isl_dim_all);
3283 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
3284 if (total < 0 || bmap_total < 0)
3285 return isl_basic_map_free(bmap);
3287 extra = bmap_total - total;
3289 i1 = bmap->n_eq - 1;
3290 i2 = context->n_eq - 1;
3292 while (bmap && i1 >= 0 && i2 >= 0) {
3293 int last1, last2;
3295 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3296 extra) != -1)
3297 break;
3298 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3299 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3300 if (last1 > last2) {
3301 --i2;
3302 continue;
3304 if (last1 < last2) {
3305 --i1;
3306 continue;
3308 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3309 bmap = isl_basic_map_cow(bmap);
3310 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3311 bmap = isl_basic_map_free(bmap);
3313 --i1;
3314 --i2;
3317 return bmap;
3320 /* Remove the constraints in "context" from "bmap".
3321 * "context" is assumed to have explicit representations
3322 * for all local variables.
3324 * First align the divs of "bmap" to those of "context" and
3325 * sort the constraints. Then drop all constraints from "bmap"
3326 * that appear in "context".
3328 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3329 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3331 isl_bool done, known;
3333 done = isl_basic_map_plain_is_universe(context);
3334 if (done == isl_bool_false)
3335 done = isl_basic_map_plain_is_universe(bmap);
3336 if (done == isl_bool_false)
3337 done = isl_basic_map_plain_is_empty(context);
3338 if (done == isl_bool_false)
3339 done = isl_basic_map_plain_is_empty(bmap);
3340 if (done < 0)
3341 goto error;
3342 if (done) {
3343 isl_basic_map_free(context);
3344 return bmap;
3346 known = isl_basic_map_divs_known(context);
3347 if (known < 0)
3348 goto error;
3349 if (!known)
3350 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3351 "context has unknown divs", goto error);
3353 bmap = isl_basic_map_align_divs(bmap, context);
3354 bmap = isl_basic_map_gauss(bmap, NULL);
3355 bmap = isl_basic_map_sort_constraints(bmap);
3356 context = isl_basic_map_sort_constraints(context);
3358 bmap = drop_inequalities(bmap, context);
3359 bmap = drop_equalities(bmap, context);
3361 isl_basic_map_free(context);
3362 bmap = isl_basic_map_finalize(bmap);
3363 return bmap;
3364 error:
3365 isl_basic_map_free(bmap);
3366 isl_basic_map_free(context);
3367 return NULL;
3370 /* Replace "map" by the disjunct at position "pos" and free "context".
3372 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3373 int pos, __isl_take isl_basic_map *context)
3375 isl_basic_map *bmap;
3377 bmap = isl_basic_map_copy(map->p[pos]);
3378 isl_map_free(map);
3379 isl_basic_map_free(context);
3380 return isl_map_from_basic_map(bmap);
3383 /* Remove the constraints in "context" from "map".
3384 * If any of the disjuncts in the result turns out to be the universe,
3385 * then return this universe.
3386 * "context" is assumed to have explicit representations
3387 * for all local variables.
3389 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3390 __isl_take isl_basic_map *context)
3392 int i;
3393 isl_bool univ, known;
3395 univ = isl_basic_map_plain_is_universe(context);
3396 if (univ < 0)
3397 goto error;
3398 if (univ) {
3399 isl_basic_map_free(context);
3400 return map;
3402 known = isl_basic_map_divs_known(context);
3403 if (known < 0)
3404 goto error;
3405 if (!known)
3406 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3407 "context has unknown divs", goto error);
3409 map = isl_map_cow(map);
3410 if (!map)
3411 goto error;
3412 for (i = 0; i < map->n; ++i) {
3413 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3414 isl_basic_map_copy(context));
3415 univ = isl_basic_map_plain_is_universe(map->p[i]);
3416 if (univ < 0)
3417 goto error;
3418 if (univ && map->n > 1)
3419 return replace_by_disjunct(map, i, context);
3422 isl_basic_map_free(context);
3423 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3424 if (map->n > 1)
3425 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3426 return map;
3427 error:
3428 isl_map_free(map);
3429 isl_basic_map_free(context);
3430 return NULL;
3433 /* Remove the constraints in "context" from "set".
3434 * If any of the disjuncts in the result turns out to be the universe,
3435 * then return this universe.
3436 * "context" is assumed to have explicit representations
3437 * for all local variables.
3439 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3440 __isl_take isl_basic_set *context)
3442 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3443 bset_to_bmap(context)));
3446 /* Remove the constraints in "context" from "map".
3447 * If any of the disjuncts in the result turns out to be the universe,
3448 * then return this universe.
3449 * "context" is assumed to consist of a single disjunct and
3450 * to have explicit representations for all local variables.
3452 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3453 __isl_take isl_map *context)
3455 isl_basic_map *hull;
3457 hull = isl_map_unshifted_simple_hull(context);
3458 return isl_map_plain_gist_basic_map(map, hull);
3461 /* Replace "map" by a universe map in the same space and free "drop".
3463 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3464 __isl_take isl_map *drop)
3466 isl_map *res;
3468 res = isl_map_universe(isl_map_get_space(map));
3469 isl_map_free(map);
3470 isl_map_free(drop);
3471 return res;
3474 /* Return a map that has the same intersection with "context" as "map"
3475 * and that is as "simple" as possible.
3477 * If "map" is already the universe, then we cannot make it any simpler.
3478 * Similarly, if "context" is the universe, then we cannot exploit it
3479 * to simplify "map"
3480 * If "map" and "context" are identical to each other, then we can
3481 * return the corresponding universe.
3483 * If either "map" or "context" consists of multiple disjuncts,
3484 * then check if "context" happens to be a subset of "map",
3485 * in which case all constraints can be removed.
3486 * In case of multiple disjuncts, the standard procedure
3487 * may not be able to detect that all constraints can be removed.
3489 * If none of these cases apply, we have to work a bit harder.
3490 * During this computation, we make use of a single disjunct context,
3491 * so if the original context consists of more than one disjunct
3492 * then we need to approximate the context by a single disjunct set.
3493 * Simply taking the simple hull may drop constraints that are
3494 * only implicitly available in each disjunct. We therefore also
3495 * look for constraints among those defining "map" that are valid
3496 * for the context. These can then be used to simplify away
3497 * the corresponding constraints in "map".
3499 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3500 __isl_take isl_map *context)
3502 int equal;
3503 int is_universe;
3504 int single_disjunct_map, single_disjunct_context;
3505 isl_bool subset;
3506 isl_basic_map *hull;
3508 is_universe = isl_map_plain_is_universe(map);
3509 if (is_universe >= 0 && !is_universe)
3510 is_universe = isl_map_plain_is_universe(context);
3511 if (is_universe < 0)
3512 goto error;
3513 if (is_universe) {
3514 isl_map_free(context);
3515 return map;
3518 equal = isl_map_plain_is_equal(map, context);
3519 if (equal < 0)
3520 goto error;
3521 if (equal)
3522 return replace_by_universe(map, context);
3524 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3525 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3526 if (!single_disjunct_map || !single_disjunct_context) {
3527 subset = isl_map_is_subset(context, map);
3528 if (subset < 0)
3529 goto error;
3530 if (subset)
3531 return replace_by_universe(map, context);
3534 context = isl_map_compute_divs(context);
3535 if (!context)
3536 goto error;
3537 if (single_disjunct_context) {
3538 hull = isl_map_simple_hull(context);
3539 } else {
3540 isl_ctx *ctx;
3541 isl_map_list *list;
3543 ctx = isl_map_get_ctx(map);
3544 list = isl_map_list_alloc(ctx, 2);
3545 list = isl_map_list_add(list, isl_map_copy(context));
3546 list = isl_map_list_add(list, isl_map_copy(map));
3547 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3548 list);
3550 return isl_map_gist_basic_map(map, hull);
3551 error:
3552 isl_map_free(map);
3553 isl_map_free(context);
3554 return NULL;
3557 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3558 __isl_take isl_map *context)
3560 return isl_map_align_params_map_map_and(map, context, &map_gist);
3563 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3564 struct isl_basic_set *context)
3566 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3567 bset_to_bmap(context)));
3570 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3571 __isl_take isl_basic_set *context)
3573 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3574 bset_to_bmap(context)));
3577 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3578 __isl_take isl_basic_set *context)
3580 isl_space *space = isl_set_get_space(set);
3581 isl_basic_set *dom_context = isl_basic_set_universe(space);
3582 dom_context = isl_basic_set_intersect_params(dom_context, context);
3583 return isl_set_gist_basic_set(set, dom_context);
3586 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3587 __isl_take isl_set *context)
3589 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3592 /* Compute the gist of "bmap" with respect to the constraints "context"
3593 * on the domain.
3595 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3596 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3598 isl_space *space = isl_basic_map_get_space(bmap);
3599 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3601 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3602 return isl_basic_map_gist(bmap, bmap_context);
3605 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3606 __isl_take isl_set *context)
3608 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3609 map_context = isl_map_intersect_domain(map_context, context);
3610 return isl_map_gist(map, map_context);
3613 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3614 __isl_take isl_set *context)
3616 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3617 map_context = isl_map_intersect_range(map_context, context);
3618 return isl_map_gist(map, map_context);
3621 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3622 __isl_take isl_set *context)
3624 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3625 map_context = isl_map_intersect_params(map_context, context);
3626 return isl_map_gist(map, map_context);
3629 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3630 __isl_take isl_set *context)
3632 return isl_map_gist_params(set, context);
3635 /* Quick check to see if two basic maps are disjoint.
3636 * In particular, we reduce the equalities and inequalities of
3637 * one basic map in the context of the equalities of the other
3638 * basic map and check if we get a contradiction.
3640 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3641 __isl_keep isl_basic_map *bmap2)
3643 struct isl_vec *v = NULL;
3644 int *elim = NULL;
3645 isl_size total;
3646 int i;
3648 if (!bmap1 || !bmap2)
3649 return isl_bool_error;
3650 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3651 return isl_bool_error);
3652 if (bmap1->n_div || bmap2->n_div)
3653 return isl_bool_false;
3654 if (!bmap1->n_eq && !bmap2->n_eq)
3655 return isl_bool_false;
3657 total = isl_space_dim(bmap1->dim, isl_dim_all);
3658 if (total < 0)
3659 return isl_bool_error;
3660 if (total == 0)
3661 return isl_bool_false;
3662 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3663 if (!v)
3664 goto error;
3665 elim = isl_alloc_array(bmap1->ctx, int, total);
3666 if (!elim)
3667 goto error;
3668 compute_elimination_index(bmap1, elim);
3669 for (i = 0; i < bmap2->n_eq; ++i) {
3670 int reduced;
3671 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3672 bmap1, elim);
3673 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3674 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3675 goto disjoint;
3677 for (i = 0; i < bmap2->n_ineq; ++i) {
3678 int reduced;
3679 reduced = reduced_using_equalities(v->block.data,
3680 bmap2->ineq[i], bmap1, elim);
3681 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3682 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3683 goto disjoint;
3685 compute_elimination_index(bmap2, elim);
3686 for (i = 0; i < bmap1->n_ineq; ++i) {
3687 int reduced;
3688 reduced = reduced_using_equalities(v->block.data,
3689 bmap1->ineq[i], bmap2, elim);
3690 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3691 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3692 goto disjoint;
3694 isl_vec_free(v);
3695 free(elim);
3696 return isl_bool_false;
3697 disjoint:
3698 isl_vec_free(v);
3699 free(elim);
3700 return isl_bool_true;
3701 error:
3702 isl_vec_free(v);
3703 free(elim);
3704 return isl_bool_error;
3707 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3708 __isl_keep isl_basic_set *bset2)
3710 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3711 bset_to_bmap(bset2));
3714 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3716 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3717 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3718 __isl_keep isl_basic_map *bmap2))
3720 int i, j;
3722 if (!map1 || !map2)
3723 return isl_bool_error;
3725 for (i = 0; i < map1->n; ++i) {
3726 for (j = 0; j < map2->n; ++j) {
3727 isl_bool d = test(map1->p[i], map2->p[j]);
3728 if (d != isl_bool_true)
3729 return d;
3733 return isl_bool_true;
3736 /* Are "map1" and "map2" obviously disjoint, based on information
3737 * that can be derived without looking at the individual basic maps?
3739 * In particular, if one of them is empty or if they live in different spaces
3740 * (ignoring parameters), then they are clearly disjoint.
3742 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3743 __isl_keep isl_map *map2)
3745 isl_bool disjoint;
3746 isl_bool match;
3748 if (!map1 || !map2)
3749 return isl_bool_error;
3751 disjoint = isl_map_plain_is_empty(map1);
3752 if (disjoint < 0 || disjoint)
3753 return disjoint;
3755 disjoint = isl_map_plain_is_empty(map2);
3756 if (disjoint < 0 || disjoint)
3757 return disjoint;
3759 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3760 map2->dim, isl_dim_in);
3761 if (match < 0 || !match)
3762 return match < 0 ? isl_bool_error : isl_bool_true;
3764 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3765 map2->dim, isl_dim_out);
3766 if (match < 0 || !match)
3767 return match < 0 ? isl_bool_error : isl_bool_true;
3769 return isl_bool_false;
3772 /* Are "map1" and "map2" obviously disjoint?
3774 * If one of them is empty or if they live in different spaces (ignoring
3775 * parameters), then they are clearly disjoint.
3776 * This is checked by isl_map_plain_is_disjoint_global.
3778 * If they have different parameters, then we skip any further tests.
3780 * If they are obviously equal, but not obviously empty, then we will
3781 * not be able to detect if they are disjoint.
3783 * Otherwise we check if each basic map in "map1" is obviously disjoint
3784 * from each basic map in "map2".
3786 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3787 __isl_keep isl_map *map2)
3789 isl_bool disjoint;
3790 isl_bool intersect;
3791 isl_bool match;
3793 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3794 if (disjoint < 0 || disjoint)
3795 return disjoint;
3797 match = isl_map_has_equal_params(map1, map2);
3798 if (match < 0 || !match)
3799 return match < 0 ? isl_bool_error : isl_bool_false;
3801 intersect = isl_map_plain_is_equal(map1, map2);
3802 if (intersect < 0 || intersect)
3803 return intersect < 0 ? isl_bool_error : isl_bool_false;
3805 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3808 /* Are "map1" and "map2" disjoint?
3809 * The parameters are assumed to have been aligned.
3811 * In particular, check whether all pairs of basic maps are disjoint.
3813 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3814 __isl_keep isl_map *map2)
3816 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3819 /* Are "map1" and "map2" disjoint?
3821 * They are disjoint if they are "obviously disjoint" or if one of them
3822 * is empty. Otherwise, they are not disjoint if one of them is universal.
3823 * If the two inputs are (obviously) equal and not empty, then they are
3824 * not disjoint.
3825 * If none of these cases apply, then check if all pairs of basic maps
3826 * are disjoint after aligning the parameters.
3828 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3830 isl_bool disjoint;
3831 isl_bool intersect;
3833 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3834 if (disjoint < 0 || disjoint)
3835 return disjoint;
3837 disjoint = isl_map_is_empty(map1);
3838 if (disjoint < 0 || disjoint)
3839 return disjoint;
3841 disjoint = isl_map_is_empty(map2);
3842 if (disjoint < 0 || disjoint)
3843 return disjoint;
3845 intersect = isl_map_plain_is_universe(map1);
3846 if (intersect < 0 || intersect)
3847 return intersect < 0 ? isl_bool_error : isl_bool_false;
3849 intersect = isl_map_plain_is_universe(map2);
3850 if (intersect < 0 || intersect)
3851 return intersect < 0 ? isl_bool_error : isl_bool_false;
3853 intersect = isl_map_plain_is_equal(map1, map2);
3854 if (intersect < 0 || intersect)
3855 return isl_bool_not(intersect);
3857 return isl_map_align_params_map_map_and_test(map1, map2,
3858 &isl_map_is_disjoint_aligned);
3861 /* Are "bmap1" and "bmap2" disjoint?
3863 * They are disjoint if they are "obviously disjoint" or if one of them
3864 * is empty. Otherwise, they are not disjoint if one of them is universal.
3865 * If none of these cases apply, we compute the intersection and see if
3866 * the result is empty.
3868 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3869 __isl_keep isl_basic_map *bmap2)
3871 isl_bool disjoint;
3872 isl_bool intersect;
3873 isl_basic_map *test;
3875 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3876 if (disjoint < 0 || disjoint)
3877 return disjoint;
3879 disjoint = isl_basic_map_is_empty(bmap1);
3880 if (disjoint < 0 || disjoint)
3881 return disjoint;
3883 disjoint = isl_basic_map_is_empty(bmap2);
3884 if (disjoint < 0 || disjoint)
3885 return disjoint;
3887 intersect = isl_basic_map_plain_is_universe(bmap1);
3888 if (intersect < 0 || intersect)
3889 return intersect < 0 ? isl_bool_error : isl_bool_false;
3891 intersect = isl_basic_map_plain_is_universe(bmap2);
3892 if (intersect < 0 || intersect)
3893 return intersect < 0 ? isl_bool_error : isl_bool_false;
3895 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3896 isl_basic_map_copy(bmap2));
3897 disjoint = isl_basic_map_is_empty(test);
3898 isl_basic_map_free(test);
3900 return disjoint;
3903 /* Are "bset1" and "bset2" disjoint?
3905 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3906 __isl_keep isl_basic_set *bset2)
3908 return isl_basic_map_is_disjoint(bset1, bset2);
3911 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3912 __isl_keep isl_set *set2)
3914 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3917 /* Are "set1" and "set2" disjoint?
3919 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3921 return isl_map_is_disjoint(set1, set2);
3924 /* Is "v" equal to 0, 1 or -1?
3926 static int is_zero_or_one(isl_int v)
3928 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3931 /* Are the "n" coefficients starting at "first" of inequality constraints
3932 * "i" and "j" of "bmap" opposite to each other?
3934 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
3935 int first, int n)
3937 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
3940 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
3941 * apart from the constant term?
3943 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
3945 isl_size total;
3947 total = isl_basic_map_dim(bmap, isl_dim_all);
3948 if (total < 0)
3949 return isl_bool_error;
3950 return is_opposite_part(bmap, i, j, 1, total);
3953 /* Check if we can combine a given div with lower bound l and upper
3954 * bound u with some other div and if so return that other div.
3955 * Otherwise, return a position beyond the integer divisions.
3956 * Return -1 on error.
3958 * We first check that
3959 * - the bounds are opposites of each other (except for the constant
3960 * term)
3961 * - the bounds do not reference any other div
3962 * - no div is defined in terms of this div
3964 * Let m be the size of the range allowed on the div by the bounds.
3965 * That is, the bounds are of the form
3967 * e <= a <= e + m - 1
3969 * with e some expression in the other variables.
3970 * We look for another div b such that no third div is defined in terms
3971 * of this second div b and such that in any constraint that contains
3972 * a (except for the given lower and upper bound), also contains b
3973 * with a coefficient that is m times that of b.
3974 * That is, all constraints (except for the lower and upper bound)
3975 * are of the form
3977 * e + f (a + m b) >= 0
3979 * Furthermore, in the constraints that only contain b, the coefficient
3980 * of b should be equal to 1 or -1.
3981 * If so, we return b so that "a + m b" can be replaced by
3982 * a single div "c = a + m b".
3984 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
3985 unsigned div, unsigned l, unsigned u)
3987 int i, j;
3988 unsigned n_div;
3989 int v_div;
3990 int coalesce;
3991 isl_bool opp;
3993 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3994 if (n_div <= 1)
3995 return n_div;
3996 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
3997 if (v_div < 0)
3998 return -1;
3999 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div, div) != -1)
4000 return n_div;
4001 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
4002 n_div - div - 1) != -1)
4003 return n_div;
4004 opp = is_opposite(bmap, l, u);
4005 if (opp < 0 || !opp)
4006 return opp < 0 ? -1 : n_div;
4008 for (i = 0; i < n_div; ++i) {
4009 if (isl_int_is_zero(bmap->div[i][0]))
4010 continue;
4011 if (!isl_int_is_zero(bmap->div[i][1 + 1 + v_div + div]))
4012 return n_div;
4015 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4016 if (isl_int_is_neg(bmap->ineq[l][0])) {
4017 isl_int_sub(bmap->ineq[l][0],
4018 bmap->ineq[l][0], bmap->ineq[u][0]);
4019 bmap = isl_basic_map_copy(bmap);
4020 bmap = isl_basic_map_set_to_empty(bmap);
4021 isl_basic_map_free(bmap);
4022 return n_div;
4024 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4025 coalesce = n_div;
4026 for (i = 0; i < n_div; ++i) {
4027 if (i == div)
4028 continue;
4029 if (!pairs[i])
4030 continue;
4031 for (j = 0; j < n_div; ++j) {
4032 if (isl_int_is_zero(bmap->div[j][0]))
4033 continue;
4034 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + i]))
4035 break;
4037 if (j < n_div)
4038 continue;
4039 for (j = 0; j < bmap->n_ineq; ++j) {
4040 int valid;
4041 if (j == l || j == u)
4042 continue;
4043 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
4044 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
4045 continue;
4046 break;
4048 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
4049 break;
4050 isl_int_mul(bmap->ineq[j][1 + v_div + div],
4051 bmap->ineq[j][1 + v_div + div],
4052 bmap->ineq[l][0]);
4053 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
4054 bmap->ineq[j][1 + v_div + i]);
4055 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
4056 bmap->ineq[j][1 + v_div + div],
4057 bmap->ineq[l][0]);
4058 if (!valid)
4059 break;
4061 if (j < bmap->n_ineq)
4062 continue;
4063 coalesce = i;
4064 break;
4066 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
4067 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
4068 return coalesce;
4071 /* Internal data structure used during the construction and/or evaluation of
4072 * an inequality that ensures that a pair of bounds always allows
4073 * for an integer value.
4075 * "tab" is the tableau in which the inequality is evaluated. It may
4076 * be NULL until it is actually needed.
4077 * "v" contains the inequality coefficients.
4078 * "g", "fl" and "fu" are temporary scalars used during the construction and
4079 * evaluation.
4081 struct test_ineq_data {
4082 struct isl_tab *tab;
4083 isl_vec *v;
4084 isl_int g;
4085 isl_int fl;
4086 isl_int fu;
4089 /* Free all the memory allocated by the fields of "data".
4091 static void test_ineq_data_clear(struct test_ineq_data *data)
4093 isl_tab_free(data->tab);
4094 isl_vec_free(data->v);
4095 isl_int_clear(data->g);
4096 isl_int_clear(data->fl);
4097 isl_int_clear(data->fu);
4100 /* Is the inequality stored in data->v satisfied by "bmap"?
4101 * That is, does it only attain non-negative values?
4102 * data->tab is a tableau corresponding to "bmap".
4104 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4105 struct test_ineq_data *data)
4107 isl_ctx *ctx;
4108 enum isl_lp_result res;
4110 ctx = isl_basic_map_get_ctx(bmap);
4111 if (!data->tab)
4112 data->tab = isl_tab_from_basic_map(bmap, 0);
4113 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4114 if (res == isl_lp_error)
4115 return isl_bool_error;
4116 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4119 /* Given a lower and an upper bound on div i, do they always allow
4120 * for an integer value of the given div?
4121 * Determine this property by constructing an inequality
4122 * such that the property is guaranteed when the inequality is nonnegative.
4123 * The lower bound is inequality l, while the upper bound is inequality u.
4124 * The constructed inequality is stored in data->v.
4126 * Let the upper bound be
4128 * -n_u a + e_u >= 0
4130 * and the lower bound
4132 * n_l a + e_l >= 0
4134 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4135 * We have
4137 * - f_u e_l <= f_u f_l g a <= f_l e_u
4139 * Since all variables are integer valued, this is equivalent to
4141 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4143 * If this interval is at least f_u f_l g, then it contains at least
4144 * one integer value for a.
4145 * That is, the test constraint is
4147 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4149 * or
4151 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4153 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4154 * then the constraint can be scaled down by a factor g',
4155 * with the constant term replaced by
4156 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4157 * Note that the result of applying Fourier-Motzkin to this pair
4158 * of constraints is
4160 * f_l e_u + f_u e_l >= 0
4162 * If the constant term of the scaled down version of this constraint,
4163 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4164 * term of the scaled down test constraint, then the test constraint
4165 * is known to hold and no explicit evaluation is required.
4166 * This is essentially the Omega test.
4168 * If the test constraint consists of only a constant term, then
4169 * it is sufficient to look at the sign of this constant term.
4171 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4172 int l, int u, struct test_ineq_data *data)
4174 unsigned offset;
4175 isl_size n_div;
4177 offset = isl_basic_map_offset(bmap, isl_dim_div);
4178 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4179 if (n_div < 0)
4180 return isl_bool_error;
4182 isl_int_gcd(data->g,
4183 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4184 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4185 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4186 isl_int_neg(data->fu, data->fu);
4187 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4188 data->fu, bmap->ineq[l], offset + n_div);
4189 isl_int_mul(data->g, data->g, data->fl);
4190 isl_int_mul(data->g, data->g, data->fu);
4191 isl_int_sub(data->g, data->g, data->fl);
4192 isl_int_sub(data->g, data->g, data->fu);
4193 isl_int_add_ui(data->g, data->g, 1);
4194 isl_int_sub(data->fl, data->v->el[0], data->g);
4196 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4197 if (isl_int_is_zero(data->g))
4198 return isl_int_is_nonneg(data->fl);
4199 if (isl_int_is_one(data->g)) {
4200 isl_int_set(data->v->el[0], data->fl);
4201 return test_ineq_is_satisfied(bmap, data);
4203 isl_int_fdiv_q(data->fl, data->fl, data->g);
4204 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4205 if (isl_int_eq(data->fl, data->v->el[0]))
4206 return isl_bool_true;
4207 isl_int_set(data->v->el[0], data->fl);
4208 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4209 offset - 1 + n_div);
4211 return test_ineq_is_satisfied(bmap, data);
4214 /* Remove more kinds of divs that are not strictly needed.
4215 * In particular, if all pairs of lower and upper bounds on a div
4216 * are such that they allow at least one integer value of the div,
4217 * then we can eliminate the div using Fourier-Motzkin without
4218 * introducing any spurious solutions.
4220 * If at least one of the two constraints has a unit coefficient for the div,
4221 * then the presence of such a value is guaranteed so there is no need to check.
4222 * In particular, the value attained by the bound with unit coefficient
4223 * can serve as this intermediate value.
4225 static __isl_give isl_basic_map *drop_more_redundant_divs(
4226 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4228 isl_ctx *ctx;
4229 struct test_ineq_data data = { NULL, NULL };
4230 unsigned off;
4231 isl_size n_div;
4232 int remove = -1;
4234 isl_int_init(data.g);
4235 isl_int_init(data.fl);
4236 isl_int_init(data.fu);
4238 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4239 if (n_div < 0)
4240 goto error;
4242 ctx = isl_basic_map_get_ctx(bmap);
4243 off = isl_basic_map_offset(bmap, isl_dim_div);
4244 data.v = isl_vec_alloc(ctx, off + n_div);
4245 if (!data.v)
4246 goto error;
4248 while (n > 0) {
4249 int i, l, u;
4250 int best = -1;
4251 isl_bool has_int;
4253 for (i = 0; i < n_div; ++i) {
4254 if (!pairs[i])
4255 continue;
4256 if (best >= 0 && pairs[best] <= pairs[i])
4257 continue;
4258 best = i;
4261 i = best;
4262 for (l = 0; l < bmap->n_ineq; ++l) {
4263 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4264 continue;
4265 if (isl_int_is_one(bmap->ineq[l][off + i]))
4266 continue;
4267 for (u = 0; u < bmap->n_ineq; ++u) {
4268 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4269 continue;
4270 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4271 continue;
4272 has_int = int_between_bounds(bmap, i, l, u,
4273 &data);
4274 if (has_int < 0)
4275 goto error;
4276 if (data.tab && data.tab->empty)
4277 break;
4278 if (!has_int)
4279 break;
4281 if (u < bmap->n_ineq)
4282 break;
4284 if (data.tab && data.tab->empty) {
4285 bmap = isl_basic_map_set_to_empty(bmap);
4286 break;
4288 if (l == bmap->n_ineq) {
4289 remove = i;
4290 break;
4292 pairs[i] = 0;
4293 --n;
4296 test_ineq_data_clear(&data);
4298 free(pairs);
4300 if (remove < 0)
4301 return bmap;
4303 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4304 return isl_basic_map_drop_redundant_divs(bmap);
4305 error:
4306 free(pairs);
4307 isl_basic_map_free(bmap);
4308 test_ineq_data_clear(&data);
4309 return NULL;
4312 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4313 * and the upper bound u, div1 always occurs together with div2 in the form
4314 * (div1 + m div2), where m is the constant range on the variable div1
4315 * allowed by l and u, replace the pair div1 and div2 by a single
4316 * div that is equal to div1 + m div2.
4318 * The new div will appear in the location that contains div2.
4319 * We need to modify all constraints that contain
4320 * div2 = (div - div1) / m
4321 * The coefficient of div2 is known to be equal to 1 or -1.
4322 * (If a constraint does not contain div2, it will also not contain div1.)
4323 * If the constraint also contains div1, then we know they appear
4324 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4325 * i.e., the coefficient of div is f.
4327 * Otherwise, we first need to introduce div1 into the constraint.
4328 * Let l be
4330 * div1 + f >=0
4332 * and u
4334 * -div1 + f' >= 0
4336 * A lower bound on div2
4338 * div2 + t >= 0
4340 * can be replaced by
4342 * m div2 + div1 + m t + f >= 0
4344 * An upper bound
4346 * -div2 + t >= 0
4348 * can be replaced by
4350 * -(m div2 + div1) + m t + f' >= 0
4352 * These constraint are those that we would obtain from eliminating
4353 * div1 using Fourier-Motzkin.
4355 * After all constraints have been modified, we drop the lower and upper
4356 * bound and then drop div1.
4357 * Since the new div is only placed in the same location that used
4358 * to store div2, but otherwise has a different meaning, any possible
4359 * explicit representation of the original div2 is removed.
4361 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4362 unsigned div1, unsigned div2, unsigned l, unsigned u)
4364 isl_ctx *ctx;
4365 isl_int m;
4366 int v_div;
4367 unsigned total;
4368 int i;
4370 ctx = isl_basic_map_get_ctx(bmap);
4372 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4373 if (v_div < 0)
4374 return isl_basic_map_free(bmap);
4375 total = 1 + v_div + bmap->n_div;
4377 isl_int_init(m);
4378 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4379 isl_int_add_ui(m, m, 1);
4381 for (i = 0; i < bmap->n_ineq; ++i) {
4382 if (i == l || i == u)
4383 continue;
4384 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
4385 continue;
4386 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
4387 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
4388 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4389 ctx->one, bmap->ineq[l], total);
4390 else
4391 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4392 ctx->one, bmap->ineq[u], total);
4394 isl_int_set(bmap->ineq[i][1 + v_div + div2],
4395 bmap->ineq[i][1 + v_div + div1]);
4396 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
4399 isl_int_clear(m);
4400 if (l > u) {
4401 isl_basic_map_drop_inequality(bmap, l);
4402 isl_basic_map_drop_inequality(bmap, u);
4403 } else {
4404 isl_basic_map_drop_inequality(bmap, u);
4405 isl_basic_map_drop_inequality(bmap, l);
4407 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4408 bmap = isl_basic_map_drop_div(bmap, div1);
4409 return bmap;
4412 /* First check if we can coalesce any pair of divs and
4413 * then continue with dropping more redundant divs.
4415 * We loop over all pairs of lower and upper bounds on a div
4416 * with coefficient 1 and -1, respectively, check if there
4417 * is any other div "c" with which we can coalesce the div
4418 * and if so, perform the coalescing.
4420 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4421 __isl_take isl_basic_map *bmap, int *pairs, int n)
4423 int i, l, u;
4424 int v_div;
4425 isl_size n_div;
4427 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
4428 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4429 if (v_div < 0 || n_div < 0)
4430 return isl_basic_map_free(bmap);
4432 for (i = 0; i < n_div; ++i) {
4433 if (!pairs[i])
4434 continue;
4435 for (l = 0; l < bmap->n_ineq; ++l) {
4436 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
4437 continue;
4438 for (u = 0; u < bmap->n_ineq; ++u) {
4439 int c;
4441 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
4442 continue;
4443 c = div_find_coalesce(bmap, pairs, i, l, u);
4444 if (c < 0)
4445 goto error;
4446 if (c >= n_div)
4447 continue;
4448 free(pairs);
4449 bmap = coalesce_divs(bmap, i, c, l, u);
4450 return isl_basic_map_drop_redundant_divs(bmap);
4455 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4456 free(pairs);
4457 return bmap;
4460 return drop_more_redundant_divs(bmap, pairs, n);
4461 error:
4462 free(pairs);
4463 isl_basic_map_free(bmap);
4464 return NULL;
4467 /* Are the "n" coefficients starting at "first" of inequality constraints
4468 * "i" and "j" of "bmap" equal to each other?
4470 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4471 int first, int n)
4473 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4476 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4477 * apart from the constant term and the coefficient at position "pos"?
4479 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4480 int pos)
4482 isl_size total;
4484 total = isl_basic_map_dim(bmap, isl_dim_all);
4485 if (total < 0)
4486 return isl_bool_error;
4487 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4488 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4491 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4492 * apart from the constant term and the coefficient at position "pos"?
4494 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4495 int pos)
4497 isl_size total;
4499 total = isl_basic_map_dim(bmap, isl_dim_all);
4500 if (total < 0)
4501 return isl_bool_error;
4502 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4503 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4506 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4507 * been modified, simplying it if "simplify" is set.
4508 * Free the temporary data structure "pairs" that was associated
4509 * to the old version of "bmap".
4511 static __isl_give isl_basic_map *drop_redundant_divs_again(
4512 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4514 if (simplify)
4515 bmap = isl_basic_map_simplify(bmap);
4516 free(pairs);
4517 return isl_basic_map_drop_redundant_divs(bmap);
4520 /* Is "div" the single unknown existentially quantified variable
4521 * in inequality constraint "ineq" of "bmap"?
4522 * "div" is known to have a non-zero coefficient in "ineq".
4524 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4525 int div)
4527 int i;
4528 isl_size n_div;
4529 unsigned o_div;
4530 isl_bool known;
4532 known = isl_basic_map_div_is_known(bmap, div);
4533 if (known < 0 || known)
4534 return isl_bool_not(known);
4535 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4536 if (n_div < 0)
4537 return isl_bool_error;
4538 if (n_div == 1)
4539 return isl_bool_true;
4540 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4541 for (i = 0; i < n_div; ++i) {
4542 isl_bool known;
4544 if (i == div)
4545 continue;
4546 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4547 continue;
4548 known = isl_basic_map_div_is_known(bmap, i);
4549 if (known < 0 || !known)
4550 return known;
4553 return isl_bool_true;
4556 /* Does integer division "div" have coefficient 1 in inequality constraint
4557 * "ineq" of "map"?
4559 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4561 unsigned o_div;
4563 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4564 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4565 return isl_bool_true;
4567 return isl_bool_false;
4570 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4571 * then try and drop redundant divs again,
4572 * freeing the temporary data structure "pairs" that was associated
4573 * to the old version of "bmap".
4575 static __isl_give isl_basic_map *set_eq_and_try_again(
4576 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4578 bmap = isl_basic_map_cow(bmap);
4579 isl_basic_map_inequality_to_equality(bmap, ineq);
4580 return drop_redundant_divs_again(bmap, pairs, 1);
4583 /* Drop the integer division at position "div", along with the two
4584 * inequality constraints "ineq1" and "ineq2" in which it appears
4585 * from "bmap" and then try and drop redundant divs again,
4586 * freeing the temporary data structure "pairs" that was associated
4587 * to the old version of "bmap".
4589 static __isl_give isl_basic_map *drop_div_and_try_again(
4590 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4591 __isl_take int *pairs)
4593 if (ineq1 > ineq2) {
4594 isl_basic_map_drop_inequality(bmap, ineq1);
4595 isl_basic_map_drop_inequality(bmap, ineq2);
4596 } else {
4597 isl_basic_map_drop_inequality(bmap, ineq2);
4598 isl_basic_map_drop_inequality(bmap, ineq1);
4600 bmap = isl_basic_map_drop_div(bmap, div);
4601 return drop_redundant_divs_again(bmap, pairs, 0);
4604 /* Given two inequality constraints
4606 * f(x) + n d + c >= 0, (ineq)
4608 * with d the variable at position "pos", and
4610 * f(x) + c0 >= 0, (lower)
4612 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4613 * determined by the first constraint.
4614 * That is, store
4616 * ceil((c0 - c)/n)
4618 * in *l.
4620 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4621 int ineq, int lower, int pos, isl_int *l)
4623 isl_int_neg(*l, bmap->ineq[ineq][0]);
4624 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4625 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4628 /* Given two inequality constraints
4630 * f(x) + n d + c >= 0, (ineq)
4632 * with d the variable at position "pos", and
4634 * -f(x) - c0 >= 0, (upper)
4636 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4637 * determined by the first constraint.
4638 * That is, store
4640 * ceil((-c1 - c)/n)
4642 * in *u.
4644 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4645 int ineq, int upper, int pos, isl_int *u)
4647 isl_int_neg(*u, bmap->ineq[ineq][0]);
4648 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4649 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4652 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4653 * does the corresponding lower bound have a fixed value in "bmap"?
4655 * In particular, "ineq" is of the form
4657 * f(x) + n d + c >= 0
4659 * with n > 0, c the constant term and
4660 * d the existentially quantified variable "div".
4661 * That is, the lower bound is
4663 * ceil((-f(x) - c)/n)
4665 * Look for a pair of constraints
4667 * f(x) + c0 >= 0
4668 * -f(x) + c1 >= 0
4670 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4671 * That is, check that
4673 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4675 * If so, return the index of inequality f(x) + c0 >= 0.
4676 * Otherwise, return bmap->n_ineq.
4677 * Return -1 on error.
4679 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4681 int i;
4682 int lower = -1, upper = -1;
4683 unsigned o_div;
4684 isl_int l, u;
4685 int equal;
4687 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4688 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4689 isl_bool par, opp;
4691 if (i == ineq)
4692 continue;
4693 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4694 continue;
4695 par = isl_bool_false;
4696 if (lower < 0)
4697 par = is_parallel_except(bmap, ineq, i, o_div + div);
4698 if (par < 0)
4699 return -1;
4700 if (par) {
4701 lower = i;
4702 continue;
4704 opp = isl_bool_false;
4705 if (upper < 0)
4706 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4707 if (opp < 0)
4708 return -1;
4709 if (opp)
4710 upper = i;
4713 if (lower < 0 || upper < 0)
4714 return bmap->n_ineq;
4716 isl_int_init(l);
4717 isl_int_init(u);
4719 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4720 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4722 equal = isl_int_eq(l, u);
4724 isl_int_clear(l);
4725 isl_int_clear(u);
4727 return equal ? lower : bmap->n_ineq;
4730 /* Given a lower bound constraint "ineq" on the existentially quantified
4731 * variable "div", such that the corresponding lower bound has
4732 * a fixed value in "bmap", assign this fixed value to the variable and
4733 * then try and drop redundant divs again,
4734 * freeing the temporary data structure "pairs" that was associated
4735 * to the old version of "bmap".
4736 * "lower" determines the constant value for the lower bound.
4738 * In particular, "ineq" is of the form
4740 * f(x) + n d + c >= 0,
4742 * while "lower" is of the form
4744 * f(x) + c0 >= 0
4746 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4747 * is ceil((c0 - c)/n).
4749 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4750 int div, int ineq, int lower, int *pairs)
4752 isl_int c;
4753 unsigned o_div;
4755 isl_int_init(c);
4757 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4758 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4759 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4760 free(pairs);
4762 isl_int_clear(c);
4764 return isl_basic_map_drop_redundant_divs(bmap);
4767 /* Remove divs that are not strictly needed based on the inequality
4768 * constraints.
4769 * In particular, if a div only occurs positively (or negatively)
4770 * in constraints, then it can simply be dropped.
4771 * Also, if a div occurs in only two constraints and if moreover
4772 * those two constraints are opposite to each other, except for the constant
4773 * term and if the sum of the constant terms is such that for any value
4774 * of the other values, there is always at least one integer value of the
4775 * div, i.e., if one plus this sum is greater than or equal to
4776 * the (absolute value) of the coefficient of the div in the constraints,
4777 * then we can also simply drop the div.
4779 * If an existentially quantified variable does not have an explicit
4780 * representation, appears in only a single lower bound that does not
4781 * involve any other such existentially quantified variables and appears
4782 * in this lower bound with coefficient 1,
4783 * then fix the variable to the value of the lower bound. That is,
4784 * turn the inequality into an equality.
4785 * If for any value of the other variables, there is any value
4786 * for the existentially quantified variable satisfying the constraints,
4787 * then this lower bound also satisfies the constraints.
4788 * It is therefore safe to pick this lower bound.
4790 * The same reasoning holds even if the coefficient is not one.
4791 * However, fixing the variable to the value of the lower bound may
4792 * in general introduce an extra integer division, in which case
4793 * it may be better to pick another value.
4794 * If this integer division has a known constant value, then plugging
4795 * in this constant value removes the existentially quantified variable
4796 * completely. In particular, if the lower bound is of the form
4797 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4798 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4799 * then the existentially quantified variable can be assigned this
4800 * shared value.
4802 * We skip divs that appear in equalities or in the definition of other divs.
4803 * Divs that appear in the definition of other divs usually occur in at least
4804 * 4 constraints, but the constraints may have been simplified.
4806 * If any divs are left after these simple checks then we move on
4807 * to more complicated cases in drop_more_redundant_divs.
4809 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4810 __isl_take isl_basic_map *bmap)
4812 int i, j;
4813 int off;
4814 int *pairs = NULL;
4815 int n = 0;
4816 int n_ineq;
4818 if (!bmap)
4819 goto error;
4820 if (bmap->n_div == 0)
4821 return bmap;
4823 off = isl_basic_map_var_offset(bmap, isl_dim_div);
4824 if (off < 0)
4825 return isl_basic_map_free(bmap);
4826 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4827 if (!pairs)
4828 goto error;
4830 n_ineq = isl_basic_map_n_inequality(bmap);
4831 for (i = 0; i < bmap->n_div; ++i) {
4832 int pos, neg;
4833 int last_pos, last_neg;
4834 int redundant;
4835 int defined;
4836 isl_bool opp, set_div;
4838 defined = !isl_int_is_zero(bmap->div[i][0]);
4839 for (j = i; j < bmap->n_div; ++j)
4840 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4841 break;
4842 if (j < bmap->n_div)
4843 continue;
4844 for (j = 0; j < bmap->n_eq; ++j)
4845 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4846 break;
4847 if (j < bmap->n_eq)
4848 continue;
4849 ++n;
4850 pos = neg = 0;
4851 for (j = 0; j < bmap->n_ineq; ++j) {
4852 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4853 last_pos = j;
4854 ++pos;
4856 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4857 last_neg = j;
4858 ++neg;
4861 pairs[i] = pos * neg;
4862 if (pairs[i] == 0) {
4863 for (j = bmap->n_ineq - 1; j >= 0; --j)
4864 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4865 isl_basic_map_drop_inequality(bmap, j);
4866 bmap = isl_basic_map_drop_div(bmap, i);
4867 return drop_redundant_divs_again(bmap, pairs, 0);
4869 if (pairs[i] != 1)
4870 opp = isl_bool_false;
4871 else
4872 opp = is_opposite(bmap, last_pos, last_neg);
4873 if (opp < 0)
4874 goto error;
4875 if (!opp) {
4876 int lower;
4877 isl_bool single, one;
4879 if (pos != 1)
4880 continue;
4881 single = single_unknown(bmap, last_pos, i);
4882 if (single < 0)
4883 goto error;
4884 if (!single)
4885 continue;
4886 one = has_coef_one(bmap, i, last_pos);
4887 if (one < 0)
4888 goto error;
4889 if (one)
4890 return set_eq_and_try_again(bmap, last_pos,
4891 pairs);
4892 lower = lower_bound_is_cst(bmap, i, last_pos);
4893 if (lower < 0)
4894 goto error;
4895 if (lower < n_ineq)
4896 return fix_cst_lower(bmap, i, last_pos, lower,
4897 pairs);
4898 continue;
4901 isl_int_add(bmap->ineq[last_pos][0],
4902 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4903 isl_int_add_ui(bmap->ineq[last_pos][0],
4904 bmap->ineq[last_pos][0], 1);
4905 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4906 bmap->ineq[last_pos][1+off+i]);
4907 isl_int_sub_ui(bmap->ineq[last_pos][0],
4908 bmap->ineq[last_pos][0], 1);
4909 isl_int_sub(bmap->ineq[last_pos][0],
4910 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4911 if (redundant)
4912 return drop_div_and_try_again(bmap, i,
4913 last_pos, last_neg, pairs);
4914 if (defined)
4915 set_div = isl_bool_false;
4916 else
4917 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4918 if (set_div < 0)
4919 return isl_basic_map_free(bmap);
4920 if (set_div) {
4921 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4922 return drop_redundant_divs_again(bmap, pairs, 1);
4924 pairs[i] = 0;
4925 --n;
4928 if (n > 0)
4929 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4931 free(pairs);
4932 return bmap;
4933 error:
4934 free(pairs);
4935 isl_basic_map_free(bmap);
4936 return NULL;
4939 /* Consider the coefficients at "c" as a row vector and replace
4940 * them with their product with "T". "T" is assumed to be a square matrix.
4942 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4944 int n;
4945 isl_ctx *ctx;
4946 isl_vec *v;
4948 if (!T)
4949 return isl_stat_error;
4950 n = isl_mat_rows(T);
4951 if (isl_seq_first_non_zero(c, n) == -1)
4952 return isl_stat_ok;
4953 ctx = isl_mat_get_ctx(T);
4954 v = isl_vec_alloc(ctx, n);
4955 if (!v)
4956 return isl_stat_error;
4957 isl_seq_swp_or_cpy(v->el, c, n);
4958 v = isl_vec_mat_product(v, isl_mat_copy(T));
4959 if (!v)
4960 return isl_stat_error;
4961 isl_seq_swp_or_cpy(c, v->el, n);
4962 isl_vec_free(v);
4964 return isl_stat_ok;
4967 /* Plug in T for the variables in "bmap" starting at "pos".
4968 * T is a linear unimodular matrix, i.e., without constant term.
4970 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4971 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4973 int i;
4974 unsigned n;
4976 bmap = isl_basic_map_cow(bmap);
4977 if (!bmap || !T)
4978 goto error;
4980 n = isl_mat_cols(T);
4981 if (n != isl_mat_rows(T))
4982 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4983 "expecting square matrix", goto error);
4985 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n) < 0)
4986 goto error;
4988 for (i = 0; i < bmap->n_eq; ++i)
4989 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4990 goto error;
4991 for (i = 0; i < bmap->n_ineq; ++i)
4992 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4993 goto error;
4994 for (i = 0; i < bmap->n_div; ++i) {
4995 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4996 continue;
4997 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4998 goto error;
5001 isl_mat_free(T);
5002 return bmap;
5003 error:
5004 isl_basic_map_free(bmap);
5005 isl_mat_free(T);
5006 return NULL;
5009 /* Remove divs that are not strictly needed.
5011 * First look for an equality constraint involving two or more
5012 * existentially quantified variables without an explicit
5013 * representation. Replace the combination that appears
5014 * in the equality constraint by a single existentially quantified
5015 * variable such that the equality can be used to derive
5016 * an explicit representation for the variable.
5017 * If there are no more such equality constraints, then continue
5018 * with isl_basic_map_drop_redundant_divs_ineq.
5020 * In particular, if the equality constraint is of the form
5022 * f(x) + \sum_i c_i a_i = 0
5024 * with a_i existentially quantified variable without explicit
5025 * representation, then apply a transformation on the existentially
5026 * quantified variables to turn the constraint into
5028 * f(x) + g a_1' = 0
5030 * with g the gcd of the c_i.
5031 * In order to easily identify which existentially quantified variables
5032 * have a complete explicit representation, i.e., without being defined
5033 * in terms of other existentially quantified variables without
5034 * an explicit representation, the existentially quantified variables
5035 * are first sorted.
5037 * The variable transformation is computed by extending the row
5038 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5040 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5041 * [a_2'] [ a_2 ]
5042 * ... = U ....
5043 * [a_n'] [ a_n ]
5045 * with [c_1/g ... c_n/g] representing the first row of U.
5046 * The inverse of U is then plugged into the original constraints.
5047 * The call to isl_basic_map_simplify makes sure the explicit
5048 * representation for a_1' is extracted from the equality constraint.
5050 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
5051 __isl_take isl_basic_map *bmap)
5053 int first;
5054 int i;
5055 unsigned o_div;
5056 isl_size n_div;
5057 int l;
5058 isl_ctx *ctx;
5059 isl_mat *T;
5061 if (!bmap)
5062 return NULL;
5063 if (isl_basic_map_divs_known(bmap))
5064 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5065 if (bmap->n_eq == 0)
5066 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5067 bmap = isl_basic_map_sort_divs(bmap);
5068 if (!bmap)
5069 return NULL;
5071 first = isl_basic_map_first_unknown_div(bmap);
5072 if (first < 0)
5073 return isl_basic_map_free(bmap);
5075 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5076 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5077 if (n_div < 0)
5078 return isl_basic_map_free(bmap);
5080 for (i = 0; i < bmap->n_eq; ++i) {
5081 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
5082 n_div - (first));
5083 if (l < 0)
5084 continue;
5085 l += first;
5086 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
5087 n_div - (l + 1)) == -1)
5088 continue;
5089 break;
5091 if (i >= bmap->n_eq)
5092 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5094 ctx = isl_basic_map_get_ctx(bmap);
5095 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5096 if (!T)
5097 return isl_basic_map_free(bmap);
5098 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5099 T = isl_mat_normalize_row(T, 0);
5100 T = isl_mat_unimodular_complete(T, 1);
5101 T = isl_mat_right_inverse(T);
5103 for (i = l; i < n_div; ++i)
5104 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5105 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5106 bmap = isl_basic_map_simplify(bmap);
5108 return isl_basic_map_drop_redundant_divs(bmap);
5111 /* Does "bmap" satisfy any equality that involves more than 2 variables
5112 * and/or has coefficients different from -1 and 1?
5114 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5116 int i;
5117 isl_size total;
5119 total = isl_basic_map_dim(bmap, isl_dim_all);
5120 if (total < 0)
5121 return isl_bool_error;
5123 for (i = 0; i < bmap->n_eq; ++i) {
5124 int j, k;
5126 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5127 if (j < 0)
5128 continue;
5129 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5130 !isl_int_is_negone(bmap->eq[i][1 + j]))
5131 return isl_bool_true;
5133 j += 1;
5134 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5135 if (k < 0)
5136 continue;
5137 j += k;
5138 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5139 !isl_int_is_negone(bmap->eq[i][1 + j]))
5140 return isl_bool_true;
5142 j += 1;
5143 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5144 if (k >= 0)
5145 return isl_bool_true;
5148 return isl_bool_false;
5151 /* Remove any common factor g from the constraint coefficients in "v".
5152 * The constant term is stored in the first position and is replaced
5153 * by floor(c/g). If any common factor is removed and if this results
5154 * in a tightening of the constraint, then set *tightened.
5156 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5157 int *tightened)
5159 isl_ctx *ctx;
5161 if (!v)
5162 return NULL;
5163 ctx = isl_vec_get_ctx(v);
5164 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5165 if (isl_int_is_zero(ctx->normalize_gcd))
5166 return v;
5167 if (isl_int_is_one(ctx->normalize_gcd))
5168 return v;
5169 v = isl_vec_cow(v);
5170 if (!v)
5171 return NULL;
5172 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5173 *tightened = 1;
5174 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5175 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5176 v->size - 1);
5177 return v;
5180 /* If "bmap" is an integer set that satisfies any equality involving
5181 * more than 2 variables and/or has coefficients different from -1 and 1,
5182 * then use variable compression to reduce the coefficients by removing
5183 * any (hidden) common factor.
5184 * In particular, apply the variable compression to each constraint,
5185 * factor out any common factor in the non-constant coefficients and
5186 * then apply the inverse of the compression.
5187 * At the end, we mark the basic map as having reduced constants.
5188 * If this flag is still set on the next invocation of this function,
5189 * then we skip the computation.
5191 * Removing a common factor may result in a tightening of some of
5192 * the constraints. If this happens, then we may end up with two
5193 * opposite inequalities that can be replaced by an equality.
5194 * We therefore call isl_basic_map_detect_inequality_pairs,
5195 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5196 * and isl_basic_map_gauss if such a pair was found.
5198 * Note that this function may leave the result in an inconsistent state.
5199 * In particular, the constraints may not be gaussed.
5200 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5201 * for some of the test cases to pass successfully.
5202 * Any potential modification of the representation is therefore only
5203 * performed on a single copy of the basic map.
5205 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5206 __isl_take isl_basic_map *bmap)
5208 isl_size total;
5209 isl_bool multi;
5210 isl_ctx *ctx;
5211 isl_vec *v;
5212 isl_mat *eq, *T, *T2;
5213 int i;
5214 int tightened;
5216 if (!bmap)
5217 return NULL;
5218 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5219 return bmap;
5220 if (isl_basic_map_is_rational(bmap))
5221 return bmap;
5222 if (bmap->n_eq == 0)
5223 return bmap;
5224 multi = has_multiple_var_equality(bmap);
5225 if (multi < 0)
5226 return isl_basic_map_free(bmap);
5227 if (!multi)
5228 return bmap;
5230 total = isl_basic_map_dim(bmap, isl_dim_all);
5231 if (total < 0)
5232 return isl_basic_map_free(bmap);
5233 ctx = isl_basic_map_get_ctx(bmap);
5234 v = isl_vec_alloc(ctx, 1 + total);
5235 if (!v)
5236 return isl_basic_map_free(bmap);
5238 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5239 T = isl_mat_variable_compression(eq, &T2);
5240 if (!T || !T2)
5241 goto error;
5242 if (T->n_col == 0) {
5243 isl_mat_free(T);
5244 isl_mat_free(T2);
5245 isl_vec_free(v);
5246 return isl_basic_map_set_to_empty(bmap);
5249 bmap = isl_basic_map_cow(bmap);
5250 if (!bmap)
5251 goto error;
5253 tightened = 0;
5254 for (i = 0; i < bmap->n_ineq; ++i) {
5255 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5256 v = isl_vec_mat_product(v, isl_mat_copy(T));
5257 v = normalize_constraint(v, &tightened);
5258 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5259 if (!v)
5260 goto error;
5261 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5264 isl_mat_free(T);
5265 isl_mat_free(T2);
5266 isl_vec_free(v);
5268 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5270 if (tightened) {
5271 int progress = 0;
5273 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5274 if (progress) {
5275 bmap = eliminate_divs_eq(bmap, &progress);
5276 bmap = isl_basic_map_gauss(bmap, NULL);
5280 return bmap;
5281 error:
5282 isl_mat_free(T);
5283 isl_mat_free(T2);
5284 isl_vec_free(v);
5285 return isl_basic_map_free(bmap);
5288 /* Shift the integer division at position "div" of "bmap"
5289 * by "shift" times the variable at position "pos".
5290 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5291 * corresponds to the constant term.
5293 * That is, if the integer division has the form
5295 * floor(f(x)/d)
5297 * then replace it by
5299 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5301 __isl_give isl_basic_map *isl_basic_map_shift_div(
5302 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5304 int i;
5305 isl_size total, n_div;
5307 if (isl_int_is_zero(shift))
5308 return bmap;
5309 total = isl_basic_map_dim(bmap, isl_dim_all);
5310 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5311 total -= n_div;
5312 if (total < 0 || n_div < 0)
5313 return isl_basic_map_free(bmap);
5315 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5317 for (i = 0; i < bmap->n_eq; ++i) {
5318 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5319 continue;
5320 isl_int_submul(bmap->eq[i][pos],
5321 shift, bmap->eq[i][1 + total + div]);
5323 for (i = 0; i < bmap->n_ineq; ++i) {
5324 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5325 continue;
5326 isl_int_submul(bmap->ineq[i][pos],
5327 shift, bmap->ineq[i][1 + total + div]);
5329 for (i = 0; i < bmap->n_div; ++i) {
5330 if (isl_int_is_zero(bmap->div[i][0]))
5331 continue;
5332 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5333 continue;
5334 isl_int_submul(bmap->div[i][1 + pos],
5335 shift, bmap->div[i][1 + 1 + total + div]);
5338 return bmap;