isl_local_space.c: normalize_div: return modified result
[isl.git] / isl_map_simplify.c
blob50bab5529b5f88167c8c7d440ef9692102f15911
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
19 #include <isl/map.h>
20 #include <isl_seq.h>
21 #include "isl_tab.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
33 isl_int *t = bmap->eq[a];
34 bmap->eq[a] = bmap->eq[b];
35 bmap->eq[b] = t;
38 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
40 if (a != b) {
41 isl_int *t = bmap->ineq[a];
42 bmap->ineq[a] = bmap->ineq[b];
43 bmap->ineq[b] = t;
47 __isl_give isl_basic_map *isl_basic_map_normalize_constraints(
48 __isl_take isl_basic_map *bmap)
50 int i;
51 isl_int gcd;
52 unsigned total = isl_basic_map_total_dim(bmap);
54 if (!bmap)
55 return NULL;
57 isl_int_init(gcd);
58 for (i = bmap->n_eq - 1; i >= 0; --i) {
59 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
60 if (isl_int_is_zero(gcd)) {
61 if (!isl_int_is_zero(bmap->eq[i][0])) {
62 bmap = isl_basic_map_set_to_empty(bmap);
63 break;
65 isl_basic_map_drop_equality(bmap, i);
66 continue;
68 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
69 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
70 if (isl_int_is_one(gcd))
71 continue;
72 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
73 bmap = isl_basic_map_set_to_empty(bmap);
74 break;
76 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
79 for (i = bmap->n_ineq - 1; i >= 0; --i) {
80 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
81 if (isl_int_is_zero(gcd)) {
82 if (isl_int_is_neg(bmap->ineq[i][0])) {
83 bmap = isl_basic_map_set_to_empty(bmap);
84 break;
86 isl_basic_map_drop_inequality(bmap, i);
87 continue;
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
90 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
91 if (isl_int_is_one(gcd))
92 continue;
93 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
94 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
96 isl_int_clear(gcd);
98 return bmap;
101 __isl_give isl_basic_set *isl_basic_set_normalize_constraints(
102 __isl_take isl_basic_set *bset)
104 isl_basic_map *bmap = bset_to_bmap(bset);
105 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap));
108 /* Reduce the coefficient of the variable at position "pos"
109 * in integer division "div", such that it lies in the half-open
110 * interval (1/2,1/2], extracting any excess value from this integer division.
111 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
112 * corresponds to the constant term.
114 * That is, the integer division is of the form
116 * floor((... + (c * d + r) * x_pos + ...)/d)
118 * with -d < 2 * r <= d.
119 * Replace it by
121 * floor((... + r * x_pos + ...)/d) + c * x_pos
123 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
124 * Otherwise, c = floor((c * d + r)/d) + 1.
126 * This is the same normalization that is performed by isl_aff_floor.
128 static __isl_give isl_basic_map *reduce_coefficient_in_div(
129 __isl_take isl_basic_map *bmap, int div, int pos)
131 isl_int shift;
132 int add_one;
134 isl_int_init(shift);
135 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
136 isl_int_mul_ui(shift, shift, 2);
137 add_one = isl_int_gt(shift, bmap->div[div][0]);
138 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
139 if (add_one)
140 isl_int_add_ui(shift, shift, 1);
141 isl_int_neg(shift, shift);
142 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
143 isl_int_clear(shift);
145 return bmap;
148 /* Does the coefficient of the variable at position "pos"
149 * in integer division "div" need to be reduced?
150 * That is, does it lie outside the half-open interval (1/2,1/2]?
151 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
152 * 2 * c != d.
154 static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div,
155 int pos)
157 isl_bool r;
159 if (isl_int_is_zero(bmap->div[div][1 + pos]))
160 return isl_bool_false;
162 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
163 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
164 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
165 isl_int_divexact_ui(bmap->div[div][1 + pos],
166 bmap->div[div][1 + pos], 2);
168 return r;
171 /* Reduce the coefficients (including the constant term) of
172 * integer division "div", if needed.
173 * In particular, make sure all coefficients lie in
174 * the half-open interval (1/2,1/2].
176 static __isl_give isl_basic_map *reduce_div_coefficients_of_div(
177 __isl_take isl_basic_map *bmap, int div)
179 int i;
180 unsigned total = 1 + isl_basic_map_total_dim(bmap);
182 for (i = 0; i < total; ++i) {
183 isl_bool reduce;
185 reduce = needs_reduction(bmap, div, i);
186 if (reduce < 0)
187 return isl_basic_map_free(bmap);
188 if (!reduce)
189 continue;
190 bmap = reduce_coefficient_in_div(bmap, div, i);
191 if (!bmap)
192 break;
195 return bmap;
198 /* Reduce the coefficients (including the constant term) of
199 * the known integer divisions, if needed
200 * In particular, make sure all coefficients lie in
201 * the half-open interval (1/2,1/2].
203 static __isl_give isl_basic_map *reduce_div_coefficients(
204 __isl_take isl_basic_map *bmap)
206 int i;
208 if (!bmap)
209 return NULL;
210 if (bmap->n_div == 0)
211 return bmap;
213 for (i = 0; i < bmap->n_div; ++i) {
214 if (isl_int_is_zero(bmap->div[i][0]))
215 continue;
216 bmap = reduce_div_coefficients_of_div(bmap, i);
217 if (!bmap)
218 break;
221 return bmap;
224 /* Remove any common factor in numerator and denominator of the div expression,
225 * not taking into account the constant term.
226 * That is, if the div is of the form
228 * floor((a + m f(x))/(m d))
230 * then replace it by
232 * floor((floor(a/m) + f(x))/d)
234 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
235 * and can therefore not influence the result of the floor.
237 static __isl_give isl_basic_map *normalize_div_expression(
238 __isl_take isl_basic_map *bmap, int div)
240 unsigned total = isl_basic_map_total_dim(bmap);
241 isl_ctx *ctx = bmap->ctx;
243 if (isl_int_is_zero(bmap->div[div][0]))
244 return bmap;
245 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
246 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
247 if (isl_int_is_one(ctx->normalize_gcd))
248 return bmap;
249 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
250 ctx->normalize_gcd);
251 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
252 ctx->normalize_gcd);
253 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
254 ctx->normalize_gcd, total);
256 return bmap;
259 /* Remove any common factor in numerator and denominator of a div expression,
260 * not taking into account the constant term.
261 * That is, look for any div of the form
263 * floor((a + m f(x))/(m d))
265 * and replace it by
267 * floor((floor(a/m) + f(x))/d)
269 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
270 * and can therefore not influence the result of the floor.
272 static __isl_give isl_basic_map *normalize_div_expressions(
273 __isl_take isl_basic_map *bmap)
275 int i;
277 if (!bmap)
278 return NULL;
279 if (bmap->n_div == 0)
280 return bmap;
282 for (i = 0; i < bmap->n_div; ++i)
283 bmap = normalize_div_expression(bmap, i);
285 return bmap;
288 /* Assumes divs have been ordered if keep_divs is set.
290 static __isl_give isl_basic_map *eliminate_var_using_equality(
291 __isl_take isl_basic_map *bmap,
292 unsigned pos, isl_int *eq, int keep_divs, int *progress)
294 unsigned total;
295 unsigned space_total;
296 int k;
297 int last_div;
299 total = isl_basic_map_total_dim(bmap);
300 space_total = isl_space_dim(bmap->dim, isl_dim_all);
301 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
302 for (k = 0; k < bmap->n_eq; ++k) {
303 if (bmap->eq[k] == eq)
304 continue;
305 if (isl_int_is_zero(bmap->eq[k][1+pos]))
306 continue;
307 if (progress)
308 *progress = 1;
309 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
310 isl_seq_normalize(bmap->ctx, bmap->eq[k], 1 + total);
313 for (k = 0; k < bmap->n_ineq; ++k) {
314 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
315 continue;
316 if (progress)
317 *progress = 1;
318 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
319 isl_seq_normalize(bmap->ctx, bmap->ineq[k], 1 + total);
320 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
321 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
324 for (k = 0; k < bmap->n_div; ++k) {
325 if (isl_int_is_zero(bmap->div[k][0]))
326 continue;
327 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
328 continue;
329 if (progress)
330 *progress = 1;
331 /* We need to be careful about circular definitions,
332 * so for now we just remove the definition of div k
333 * if the equality contains any divs.
334 * If keep_divs is set, then the divs have been ordered
335 * and we can keep the definition as long as the result
336 * is still ordered.
338 if (last_div == -1 || (keep_divs && last_div < k)) {
339 isl_seq_elim(bmap->div[k]+1, eq,
340 1+pos, 1+total, &bmap->div[k][0]);
341 bmap = normalize_div_expression(bmap, k);
342 if (!bmap)
343 return NULL;
344 } else
345 isl_seq_clr(bmap->div[k], 1 + total);
348 return bmap;
351 /* Assumes divs have been ordered if keep_divs is set.
353 static __isl_give isl_basic_map *eliminate_div(__isl_take isl_basic_map *bmap,
354 isl_int *eq, unsigned div, int keep_divs)
356 unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
358 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
360 bmap = isl_basic_map_drop_div(bmap, div);
362 return bmap;
365 /* Check if elimination of div "div" using equality "eq" would not
366 * result in a div depending on a later div.
368 static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq,
369 unsigned div)
371 int k;
372 int last_div;
373 unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
374 unsigned pos = space_total + div;
376 last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
377 if (last_div < 0 || last_div <= div)
378 return isl_bool_true;
380 for (k = 0; k <= last_div; ++k) {
381 if (isl_int_is_zero(bmap->div[k][0]))
382 continue;
383 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
384 return isl_bool_false;
387 return isl_bool_true;
390 /* Eliminate divs based on equalities
392 static __isl_give isl_basic_map *eliminate_divs_eq(
393 __isl_take isl_basic_map *bmap, int *progress)
395 int d;
396 int i;
397 int modified = 0;
398 unsigned off;
400 bmap = isl_basic_map_order_divs(bmap);
402 if (!bmap)
403 return NULL;
405 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
407 for (d = bmap->n_div - 1; d >= 0 ; --d) {
408 for (i = 0; i < bmap->n_eq; ++i) {
409 isl_bool ok;
411 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
412 !isl_int_is_negone(bmap->eq[i][off + d]))
413 continue;
414 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
415 if (ok < 0)
416 return isl_basic_map_free(bmap);
417 if (!ok)
418 continue;
419 modified = 1;
420 *progress = 1;
421 bmap = eliminate_div(bmap, bmap->eq[i], d, 1);
422 if (isl_basic_map_drop_equality(bmap, i) < 0)
423 return isl_basic_map_free(bmap);
424 break;
427 if (modified)
428 return eliminate_divs_eq(bmap, progress);
429 return bmap;
432 /* Eliminate divs based on inequalities
434 static __isl_give isl_basic_map *eliminate_divs_ineq(
435 __isl_take isl_basic_map *bmap, int *progress)
437 int d;
438 int i;
439 unsigned off;
440 struct isl_ctx *ctx;
442 if (!bmap)
443 return NULL;
445 ctx = bmap->ctx;
446 off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
448 for (d = bmap->n_div - 1; d >= 0 ; --d) {
449 for (i = 0; i < bmap->n_eq; ++i)
450 if (!isl_int_is_zero(bmap->eq[i][off + d]))
451 break;
452 if (i < bmap->n_eq)
453 continue;
454 for (i = 0; i < bmap->n_ineq; ++i)
455 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
456 break;
457 if (i < bmap->n_ineq)
458 continue;
459 *progress = 1;
460 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
461 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
462 break;
463 bmap = isl_basic_map_drop_div(bmap, d);
464 if (!bmap)
465 break;
467 return bmap;
470 /* Does the equality constraint at position "eq" in "bmap" involve
471 * any local variables in the range [first, first + n)
472 * that are not marked as having an explicit representation?
474 static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap,
475 int eq, unsigned first, unsigned n)
477 unsigned o_div;
478 int i;
480 if (!bmap)
481 return isl_bool_error;
483 o_div = isl_basic_map_offset(bmap, isl_dim_div);
484 for (i = 0; i < n; ++i) {
485 isl_bool unknown;
487 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
488 continue;
489 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
490 if (unknown < 0)
491 return isl_bool_error;
492 if (unknown)
493 return isl_bool_true;
496 return isl_bool_false;
499 /* The last local variable involved in the equality constraint
500 * at position "eq" in "bmap" is the local variable at position "div".
501 * It can therefore be used to extract an explicit representation
502 * for that variable.
503 * Do so unless the local variable already has an explicit representation or
504 * the explicit representation would involve any other local variables
505 * that in turn do not have an explicit representation.
506 * An equality constraint involving local variables without an explicit
507 * representation can be used in isl_basic_map_drop_redundant_divs
508 * to separate out an independent local variable. Introducing
509 * an explicit representation here would block this transformation,
510 * while the partial explicit representation in itself is not very useful.
511 * Set *progress if anything is changed.
513 * The equality constraint is of the form
515 * f(x) + n e >= 0
517 * with n a positive number. The explicit representation derived from
518 * this constraint is
520 * floor((-f(x))/n)
522 static __isl_give isl_basic_map *set_div_from_eq(__isl_take isl_basic_map *bmap,
523 int div, int eq, int *progress)
525 unsigned total, o_div;
526 isl_bool involves;
528 if (!bmap)
529 return NULL;
531 if (!isl_int_is_zero(bmap->div[div][0]))
532 return bmap;
534 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
535 if (involves < 0)
536 return isl_basic_map_free(bmap);
537 if (involves)
538 return bmap;
540 total = isl_basic_map_dim(bmap, isl_dim_all);
541 o_div = isl_basic_map_offset(bmap, isl_dim_div);
542 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
543 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
544 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
545 if (progress)
546 *progress = 1;
548 return bmap;
551 __isl_give isl_basic_map *isl_basic_map_gauss(__isl_take isl_basic_map *bmap,
552 int *progress)
554 int k;
555 int done;
556 int last_var;
557 unsigned total_var;
558 unsigned total;
560 bmap = isl_basic_map_order_divs(bmap);
562 if (!bmap)
563 return NULL;
565 total = isl_basic_map_total_dim(bmap);
566 total_var = total - bmap->n_div;
568 last_var = total - 1;
569 for (done = 0; done < bmap->n_eq; ++done) {
570 for (; last_var >= 0; --last_var) {
571 for (k = done; k < bmap->n_eq; ++k)
572 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
573 break;
574 if (k < bmap->n_eq)
575 break;
577 if (last_var < 0)
578 break;
579 if (k != done)
580 swap_equality(bmap, k, done);
581 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
582 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
584 bmap = eliminate_var_using_equality(bmap, last_var,
585 bmap->eq[done], 1, progress);
587 if (last_var >= total_var)
588 bmap = set_div_from_eq(bmap, last_var - total_var,
589 done, progress);
590 if (!bmap)
591 return NULL;
593 if (done == bmap->n_eq)
594 return bmap;
595 for (k = done; k < bmap->n_eq; ++k) {
596 if (isl_int_is_zero(bmap->eq[k][0]))
597 continue;
598 return isl_basic_map_set_to_empty(bmap);
600 isl_basic_map_free_equality(bmap, bmap->n_eq-done);
601 return bmap;
604 __isl_give isl_basic_set *isl_basic_set_gauss(
605 __isl_take isl_basic_set *bset, int *progress)
607 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset),
608 progress));
612 static unsigned int round_up(unsigned int v)
614 int old_v = v;
616 while (v) {
617 old_v = v;
618 v ^= v & -v;
620 return old_v << 1;
623 /* Hash table of inequalities in a basic map.
624 * "index" is an array of addresses of inequalities in the basic map, some
625 * of which are NULL. The inequalities are hashed on the coefficients
626 * except the constant term.
627 * "size" is the number of elements in the array and is always a power of two
628 * "bits" is the number of bits need to represent an index into the array.
629 * "total" is the total dimension of the basic map.
631 struct isl_constraint_index {
632 unsigned int size;
633 int bits;
634 isl_int ***index;
635 unsigned total;
638 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
640 static isl_stat create_constraint_index(struct isl_constraint_index *ci,
641 __isl_keep isl_basic_map *bmap)
643 isl_ctx *ctx;
645 ci->index = NULL;
646 if (!bmap)
647 return isl_stat_error;
648 ci->total = isl_basic_set_total_dim(bmap);
649 if (bmap->n_ineq == 0)
650 return isl_stat_ok;
651 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
652 ci->bits = ffs(ci->size) - 1;
653 ctx = isl_basic_map_get_ctx(bmap);
654 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
655 if (!ci->index)
656 return isl_stat_error;
658 return isl_stat_ok;
661 /* Free the memory allocated by create_constraint_index.
663 static void constraint_index_free(struct isl_constraint_index *ci)
665 free(ci->index);
668 /* Return the position in ci->index that contains the address of
669 * an inequality that is equal to *ineq up to the constant term,
670 * provided this address is not identical to "ineq".
671 * If there is no such inequality, then return the position where
672 * such an inequality should be inserted.
674 static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
676 int h;
677 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
678 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
679 if (ineq != ci->index[h] &&
680 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
681 break;
682 return h;
685 /* Return the position in ci->index that contains the address of
686 * an inequality that is equal to the k'th inequality of "bmap"
687 * up to the constant term, provided it does not point to the very
688 * same inequality.
689 * If there is no such inequality, then return the position where
690 * such an inequality should be inserted.
692 static int hash_index(struct isl_constraint_index *ci,
693 __isl_keep isl_basic_map *bmap, int k)
695 return hash_index_ineq(ci, &bmap->ineq[k]);
698 static int set_hash_index(struct isl_constraint_index *ci,
699 __isl_keep isl_basic_set *bset, int k)
701 return hash_index(ci, bset, k);
704 /* Fill in the "ci" data structure with the inequalities of "bset".
706 static isl_stat setup_constraint_index(struct isl_constraint_index *ci,
707 __isl_keep isl_basic_set *bset)
709 int k, h;
711 if (create_constraint_index(ci, bset) < 0)
712 return isl_stat_error;
714 for (k = 0; k < bset->n_ineq; ++k) {
715 h = set_hash_index(ci, bset, k);
716 ci->index[h] = &bset->ineq[k];
719 return isl_stat_ok;
722 /* Is the inequality ineq (obviously) redundant with respect
723 * to the constraints in "ci"?
725 * Look for an inequality in "ci" with the same coefficients and then
726 * check if the contant term of "ineq" is greater than or equal
727 * to the constant term of that inequality. If so, "ineq" is clearly
728 * redundant.
730 * Note that hash_index_ineq ignores a stored constraint if it has
731 * the same address as the passed inequality. It is ok to pass
732 * the address of a local variable here since it will never be
733 * the same as the address of a constraint in "ci".
735 static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci,
736 isl_int *ineq)
738 int h;
740 h = hash_index_ineq(ci, &ineq);
741 if (!ci->index[h])
742 return isl_bool_false;
743 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
746 /* If we can eliminate more than one div, then we need to make
747 * sure we do it from last div to first div, in order not to
748 * change the position of the other divs that still need to
749 * be removed.
751 static __isl_give isl_basic_map *remove_duplicate_divs(
752 __isl_take isl_basic_map *bmap, int *progress)
754 unsigned int size;
755 int *index;
756 int *elim_for;
757 int k, l, h;
758 int bits;
759 struct isl_blk eq;
760 unsigned total_var;
761 unsigned total;
762 struct isl_ctx *ctx;
764 bmap = isl_basic_map_order_divs(bmap);
765 if (!bmap || bmap->n_div <= 1)
766 return bmap;
768 total_var = isl_space_dim(bmap->dim, isl_dim_all);
769 total = total_var + bmap->n_div;
771 ctx = bmap->ctx;
772 for (k = bmap->n_div - 1; k >= 0; --k)
773 if (!isl_int_is_zero(bmap->div[k][0]))
774 break;
775 if (k <= 0)
776 return bmap;
778 size = round_up(4 * bmap->n_div / 3 - 1);
779 if (size == 0)
780 return bmap;
781 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
782 bits = ffs(size) - 1;
783 index = isl_calloc_array(ctx, int, size);
784 if (!elim_for || !index)
785 goto out;
786 eq = isl_blk_alloc(ctx, 1+total);
787 if (isl_blk_is_error(eq))
788 goto out;
790 isl_seq_clr(eq.data, 1+total);
791 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
792 for (--k; k >= 0; --k) {
793 uint32_t hash;
795 if (isl_int_is_zero(bmap->div[k][0]))
796 continue;
798 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
799 for (h = hash; index[h]; h = (h+1) % size)
800 if (isl_seq_eq(bmap->div[k],
801 bmap->div[index[h]-1], 2+total))
802 break;
803 if (index[h]) {
804 *progress = 1;
805 l = index[h] - 1;
806 elim_for[l] = k + 1;
808 index[h] = k+1;
810 for (l = bmap->n_div - 1; l >= 0; --l) {
811 if (!elim_for[l])
812 continue;
813 k = elim_for[l] - 1;
814 isl_int_set_si(eq.data[1+total_var+k], -1);
815 isl_int_set_si(eq.data[1+total_var+l], 1);
816 bmap = eliminate_div(bmap, eq.data, l, 1);
817 if (!bmap)
818 break;
819 isl_int_set_si(eq.data[1+total_var+k], 0);
820 isl_int_set_si(eq.data[1+total_var+l], 0);
823 isl_blk_free(ctx, eq);
824 out:
825 free(index);
826 free(elim_for);
827 return bmap;
830 static int n_pure_div_eq(struct isl_basic_map *bmap)
832 int i, j;
833 unsigned total;
835 total = isl_space_dim(bmap->dim, isl_dim_all);
836 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
837 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
838 --j;
839 if (j < 0)
840 break;
841 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total, j) != -1)
842 return 0;
844 return i;
847 /* Normalize divs that appear in equalities.
849 * In particular, we assume that bmap contains some equalities
850 * of the form
852 * a x = m * e_i
854 * and we want to replace the set of e_i by a minimal set and
855 * such that the new e_i have a canonical representation in terms
856 * of the vector x.
857 * If any of the equalities involves more than one divs, then
858 * we currently simply bail out.
860 * Let us first additionally assume that all equalities involve
861 * a div. The equalities then express modulo constraints on the
862 * remaining variables and we can use "parameter compression"
863 * to find a minimal set of constraints. The result is a transformation
865 * x = T(x') = x_0 + G x'
867 * with G a lower-triangular matrix with all elements below the diagonal
868 * non-negative and smaller than the diagonal element on the same row.
869 * We first normalize x_0 by making the same property hold in the affine
870 * T matrix.
871 * The rows i of G with a 1 on the diagonal do not impose any modulo
872 * constraint and simply express x_i = x'_i.
873 * For each of the remaining rows i, we introduce a div and a corresponding
874 * equality. In particular
876 * g_ii e_j = x_i - g_i(x')
878 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
879 * corresponding div (if g_kk != 1).
881 * If there are any equalities not involving any div, then we
882 * first apply a variable compression on the variables x:
884 * x = C x'' x'' = C_2 x
886 * and perform the above parameter compression on A C instead of on A.
887 * The resulting compression is then of the form
889 * x'' = T(x') = x_0 + G x'
891 * and in constructing the new divs and the corresponding equalities,
892 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
893 * by the corresponding row from C_2.
895 static __isl_give isl_basic_map *normalize_divs(__isl_take isl_basic_map *bmap,
896 int *progress)
898 int i, j, k;
899 int total;
900 int div_eq;
901 struct isl_mat *B;
902 struct isl_vec *d;
903 struct isl_mat *T = NULL;
904 struct isl_mat *C = NULL;
905 struct isl_mat *C2 = NULL;
906 isl_int v;
907 int *pos = NULL;
908 int dropped, needed;
910 if (!bmap)
911 return NULL;
913 if (bmap->n_div == 0)
914 return bmap;
916 if (bmap->n_eq == 0)
917 return bmap;
919 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
920 return bmap;
922 total = isl_space_dim(bmap->dim, isl_dim_all);
923 div_eq = n_pure_div_eq(bmap);
924 if (div_eq == 0)
925 return bmap;
927 if (div_eq < bmap->n_eq) {
928 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
929 bmap->n_eq - div_eq, 0, 1 + total);
930 C = isl_mat_variable_compression(B, &C2);
931 if (!C || !C2)
932 goto error;
933 if (C->n_col == 0) {
934 bmap = isl_basic_map_set_to_empty(bmap);
935 isl_mat_free(C);
936 isl_mat_free(C2);
937 goto done;
941 d = isl_vec_alloc(bmap->ctx, div_eq);
942 if (!d)
943 goto error;
944 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
945 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
946 --j;
947 isl_int_set(d->block.data[i], bmap->eq[i][1 + total + j]);
949 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + total);
951 if (C) {
952 B = isl_mat_product(B, C);
953 C = NULL;
956 T = isl_mat_parameter_compression(B, d);
957 if (!T)
958 goto error;
959 if (T->n_col == 0) {
960 bmap = isl_basic_map_set_to_empty(bmap);
961 isl_mat_free(C2);
962 isl_mat_free(T);
963 goto done;
965 isl_int_init(v);
966 for (i = 0; i < T->n_row - 1; ++i) {
967 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
968 if (isl_int_is_zero(v))
969 continue;
970 isl_mat_col_submul(T, 0, v, 1 + i);
972 isl_int_clear(v);
973 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
974 if (!pos)
975 goto error;
976 /* We have to be careful because dropping equalities may reorder them */
977 dropped = 0;
978 for (j = bmap->n_div - 1; j >= 0; --j) {
979 for (i = 0; i < bmap->n_eq; ++i)
980 if (!isl_int_is_zero(bmap->eq[i][1 + total + j]))
981 break;
982 if (i < bmap->n_eq) {
983 bmap = isl_basic_map_drop_div(bmap, j);
984 isl_basic_map_drop_equality(bmap, i);
985 ++dropped;
988 pos[0] = 0;
989 needed = 0;
990 for (i = 1; i < T->n_row; ++i) {
991 if (isl_int_is_one(T->row[i][i]))
992 pos[i] = i;
993 else
994 needed++;
996 if (needed > dropped) {
997 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
998 needed, needed, 0);
999 if (!bmap)
1000 goto error;
1002 for (i = 1; i < T->n_row; ++i) {
1003 if (isl_int_is_one(T->row[i][i]))
1004 continue;
1005 k = isl_basic_map_alloc_div(bmap);
1006 pos[i] = 1 + total + k;
1007 isl_seq_clr(bmap->div[k] + 1, 1 + total + bmap->n_div);
1008 isl_int_set(bmap->div[k][0], T->row[i][i]);
1009 if (C2)
1010 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + total);
1011 else
1012 isl_int_set_si(bmap->div[k][1 + i], 1);
1013 for (j = 0; j < i; ++j) {
1014 if (isl_int_is_zero(T->row[i][j]))
1015 continue;
1016 if (pos[j] < T->n_row && C2)
1017 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1018 C2->row[pos[j]], 1 + total);
1019 else
1020 isl_int_neg(bmap->div[k][1 + pos[j]],
1021 T->row[i][j]);
1023 j = isl_basic_map_alloc_equality(bmap);
1024 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+total+bmap->n_div);
1025 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1027 free(pos);
1028 isl_mat_free(C2);
1029 isl_mat_free(T);
1031 if (progress)
1032 *progress = 1;
1033 done:
1034 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1036 return bmap;
1037 error:
1038 free(pos);
1039 isl_mat_free(C);
1040 isl_mat_free(C2);
1041 isl_mat_free(T);
1042 return bmap;
1045 static __isl_give isl_basic_map *set_div_from_lower_bound(
1046 __isl_take isl_basic_map *bmap, int div, int ineq)
1048 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1050 isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
1051 isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
1052 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1053 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1054 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1056 return bmap;
1059 /* Check whether it is ok to define a div based on an inequality.
1060 * To avoid the introduction of circular definitions of divs, we
1061 * do not allow such a definition if the resulting expression would refer to
1062 * any other undefined divs or if any known div is defined in
1063 * terms of the unknown div.
1065 static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap,
1066 int div, int ineq)
1068 int j;
1069 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1071 /* Not defined in terms of unknown divs */
1072 for (j = 0; j < bmap->n_div; ++j) {
1073 if (div == j)
1074 continue;
1075 if (isl_int_is_zero(bmap->ineq[ineq][total + j]))
1076 continue;
1077 if (isl_int_is_zero(bmap->div[j][0]))
1078 return isl_bool_false;
1081 /* No other div defined in terms of this one => avoid loops */
1082 for (j = 0; j < bmap->n_div; ++j) {
1083 if (div == j)
1084 continue;
1085 if (isl_int_is_zero(bmap->div[j][0]))
1086 continue;
1087 if (!isl_int_is_zero(bmap->div[j][1 + total + div]))
1088 return isl_bool_false;
1091 return isl_bool_true;
1094 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1095 * be a better expression than the current one?
1097 * If we do not have any expression yet, then any expression would be better.
1098 * Otherwise we check if the last variable involved in the inequality
1099 * (disregarding the div that it would define) is in an earlier position
1100 * than the last variable involved in the current div expression.
1102 static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap,
1103 int div, int ineq)
1105 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1106 int last_div;
1107 int last_ineq;
1109 if (isl_int_is_zero(bmap->div[div][0]))
1110 return isl_bool_true;
1112 if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
1113 bmap->n_div - (div + 1)) >= 0)
1114 return isl_bool_false;
1116 last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
1117 last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
1118 total + bmap->n_div);
1120 return last_ineq < last_div;
1123 /* Given two constraints "k" and "l" that are opposite to each other,
1124 * except for the constant term, check if we can use them
1125 * to obtain an expression for one of the hitherto unknown divs or
1126 * a "better" expression for a div for which we already have an expression.
1127 * "sum" is the sum of the constant terms of the constraints.
1128 * If this sum is strictly smaller than the coefficient of one
1129 * of the divs, then this pair can be used define the div.
1130 * To avoid the introduction of circular definitions of divs, we
1131 * do not use the pair if the resulting expression would refer to
1132 * any other undefined divs or if any known div is defined in
1133 * terms of the unknown div.
1135 static __isl_give isl_basic_map *check_for_div_constraints(
1136 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1137 int *progress)
1139 int i;
1140 unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1142 for (i = 0; i < bmap->n_div; ++i) {
1143 isl_bool set_div;
1145 if (isl_int_is_zero(bmap->ineq[k][total + i]))
1146 continue;
1147 if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
1148 continue;
1149 set_div = better_div_constraint(bmap, i, k);
1150 if (set_div >= 0 && set_div)
1151 set_div = ok_to_set_div_from_bound(bmap, i, k);
1152 if (set_div < 0)
1153 return isl_basic_map_free(bmap);
1154 if (!set_div)
1155 break;
1156 if (isl_int_is_pos(bmap->ineq[k][total + i]))
1157 bmap = set_div_from_lower_bound(bmap, i, k);
1158 else
1159 bmap = set_div_from_lower_bound(bmap, i, l);
1160 if (progress)
1161 *progress = 1;
1162 break;
1164 return bmap;
1167 __isl_give isl_basic_map *isl_basic_map_remove_duplicate_constraints(
1168 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
1170 struct isl_constraint_index ci;
1171 int k, l, h;
1172 unsigned total = isl_basic_map_total_dim(bmap);
1173 isl_int sum;
1175 if (!bmap || bmap->n_ineq <= 1)
1176 return bmap;
1178 if (create_constraint_index(&ci, bmap) < 0)
1179 return bmap;
1181 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
1182 ci.index[h] = &bmap->ineq[0];
1183 for (k = 1; k < bmap->n_ineq; ++k) {
1184 h = hash_index(&ci, bmap, k);
1185 if (!ci.index[h]) {
1186 ci.index[h] = &bmap->ineq[k];
1187 continue;
1189 if (progress)
1190 *progress = 1;
1191 l = ci.index[h] - &bmap->ineq[0];
1192 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
1193 swap_inequality(bmap, k, l);
1194 isl_basic_map_drop_inequality(bmap, k);
1195 --k;
1197 isl_int_init(sum);
1198 for (k = 0; k < bmap->n_ineq-1; ++k) {
1199 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1200 h = hash_index(&ci, bmap, k);
1201 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
1202 if (!ci.index[h])
1203 continue;
1204 l = ci.index[h] - &bmap->ineq[0];
1205 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
1206 if (isl_int_is_pos(sum)) {
1207 if (detect_divs)
1208 bmap = check_for_div_constraints(bmap, k, l,
1209 sum, progress);
1210 continue;
1212 if (isl_int_is_zero(sum)) {
1213 /* We need to break out of the loop after these
1214 * changes since the contents of the hash
1215 * will no longer be valid.
1216 * Plus, we probably we want to regauss first.
1218 if (progress)
1219 *progress = 1;
1220 isl_basic_map_drop_inequality(bmap, l);
1221 isl_basic_map_inequality_to_equality(bmap, k);
1222 } else
1223 bmap = isl_basic_map_set_to_empty(bmap);
1224 break;
1226 isl_int_clear(sum);
1228 constraint_index_free(&ci);
1229 return bmap;
1232 /* Detect all pairs of inequalities that form an equality.
1234 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1235 * Call it repeatedly while it is making progress.
1237 __isl_give isl_basic_map *isl_basic_map_detect_inequality_pairs(
1238 __isl_take isl_basic_map *bmap, int *progress)
1240 int duplicate;
1242 do {
1243 duplicate = 0;
1244 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1245 &duplicate, 0);
1246 if (progress && duplicate)
1247 *progress = 1;
1248 } while (duplicate);
1250 return bmap;
1253 /* Eliminate knowns divs from constraints where they appear with
1254 * a (positive or negative) unit coefficient.
1256 * That is, replace
1258 * floor(e/m) + f >= 0
1260 * by
1262 * e + m f >= 0
1264 * and
1266 * -floor(e/m) + f >= 0
1268 * by
1270 * -e + m f + m - 1 >= 0
1272 * The first conversion is valid because floor(e/m) >= -f is equivalent
1273 * to e/m >= -f because -f is an integral expression.
1274 * The second conversion follows from the fact that
1276 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1279 * Note that one of the div constraints may have been eliminated
1280 * due to being redundant with respect to the constraint that is
1281 * being modified by this function. The modified constraint may
1282 * no longer imply this div constraint, so we add it back to make
1283 * sure we do not lose any information.
1285 * We skip integral divs, i.e., those with denominator 1, as we would
1286 * risk eliminating the div from the div constraints. We do not need
1287 * to handle those divs here anyway since the div constraints will turn
1288 * out to form an equality and this equality can then be used to eliminate
1289 * the div from all constraints.
1291 static __isl_give isl_basic_map *eliminate_unit_divs(
1292 __isl_take isl_basic_map *bmap, int *progress)
1294 int i, j;
1295 isl_ctx *ctx;
1296 unsigned total;
1298 if (!bmap)
1299 return NULL;
1301 ctx = isl_basic_map_get_ctx(bmap);
1302 total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
1304 for (i = 0; i < bmap->n_div; ++i) {
1305 if (isl_int_is_zero(bmap->div[i][0]))
1306 continue;
1307 if (isl_int_is_one(bmap->div[i][0]))
1308 continue;
1309 for (j = 0; j < bmap->n_ineq; ++j) {
1310 int s;
1312 if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
1313 !isl_int_is_negone(bmap->ineq[j][total + i]))
1314 continue;
1316 *progress = 1;
1318 s = isl_int_sgn(bmap->ineq[j][total + i]);
1319 isl_int_set_si(bmap->ineq[j][total + i], 0);
1320 if (s < 0)
1321 isl_seq_combine(bmap->ineq[j],
1322 ctx->negone, bmap->div[i] + 1,
1323 bmap->div[i][0], bmap->ineq[j],
1324 total + bmap->n_div);
1325 else
1326 isl_seq_combine(bmap->ineq[j],
1327 ctx->one, bmap->div[i] + 1,
1328 bmap->div[i][0], bmap->ineq[j],
1329 total + bmap->n_div);
1330 if (s < 0) {
1331 isl_int_add(bmap->ineq[j][0],
1332 bmap->ineq[j][0], bmap->div[i][0]);
1333 isl_int_sub_ui(bmap->ineq[j][0],
1334 bmap->ineq[j][0], 1);
1337 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1338 if (isl_basic_map_add_div_constraint(bmap, i, s) < 0)
1339 return isl_basic_map_free(bmap);
1343 return bmap;
1346 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1348 int progress = 1;
1349 if (!bmap)
1350 return NULL;
1351 while (progress) {
1352 isl_bool empty;
1354 progress = 0;
1355 empty = isl_basic_map_plain_is_empty(bmap);
1356 if (empty < 0)
1357 return isl_basic_map_free(bmap);
1358 if (empty)
1359 break;
1360 bmap = isl_basic_map_normalize_constraints(bmap);
1361 bmap = reduce_div_coefficients(bmap);
1362 bmap = normalize_div_expressions(bmap);
1363 bmap = remove_duplicate_divs(bmap, &progress);
1364 bmap = eliminate_unit_divs(bmap, &progress);
1365 bmap = eliminate_divs_eq(bmap, &progress);
1366 bmap = eliminate_divs_ineq(bmap, &progress);
1367 bmap = isl_basic_map_gauss(bmap, &progress);
1368 /* requires equalities in normal form */
1369 bmap = normalize_divs(bmap, &progress);
1370 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1371 &progress, 1);
1372 if (bmap && progress)
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1375 return bmap;
1378 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1380 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1384 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1385 isl_int *constraint, unsigned div)
1387 unsigned pos;
1389 if (!bmap)
1390 return isl_bool_error;
1392 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1394 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1395 int neg;
1396 isl_int_sub(bmap->div[div][1],
1397 bmap->div[div][1], bmap->div[div][0]);
1398 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1399 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1400 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1401 isl_int_add(bmap->div[div][1],
1402 bmap->div[div][1], bmap->div[div][0]);
1403 if (!neg)
1404 return isl_bool_false;
1405 if (isl_seq_first_non_zero(constraint+pos+1,
1406 bmap->n_div-div-1) != -1)
1407 return isl_bool_false;
1408 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1409 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1410 return isl_bool_false;
1411 if (isl_seq_first_non_zero(constraint+pos+1,
1412 bmap->n_div-div-1) != -1)
1413 return isl_bool_false;
1414 } else
1415 return isl_bool_false;
1417 return isl_bool_true;
1420 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1421 isl_int *constraint, unsigned div)
1423 return isl_basic_map_is_div_constraint(bset, constraint, div);
1427 /* If the only constraints a div d=floor(f/m)
1428 * appears in are its two defining constraints
1430 * f - m d >=0
1431 * -(f - (m - 1)) + m d >= 0
1433 * then it can safely be removed.
1435 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1437 int i;
1438 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1440 for (i = 0; i < bmap->n_eq; ++i)
1441 if (!isl_int_is_zero(bmap->eq[i][pos]))
1442 return isl_bool_false;
1444 for (i = 0; i < bmap->n_ineq; ++i) {
1445 isl_bool red;
1447 if (isl_int_is_zero(bmap->ineq[i][pos]))
1448 continue;
1449 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1450 if (red < 0 || !red)
1451 return red;
1454 for (i = 0; i < bmap->n_div; ++i) {
1455 if (isl_int_is_zero(bmap->div[i][0]))
1456 continue;
1457 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1458 return isl_bool_false;
1461 return isl_bool_true;
1465 * Remove divs that don't occur in any of the constraints or other divs.
1466 * These can arise when dropping constraints from a basic map or
1467 * when the divs of a basic map have been temporarily aligned
1468 * with the divs of another basic map.
1470 static __isl_give isl_basic_map *remove_redundant_divs(
1471 __isl_take isl_basic_map *bmap)
1473 int i;
1474 int v_div;
1476 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1477 if (v_div < 0)
1478 return isl_basic_map_free(bmap);
1480 for (i = bmap->n_div-1; i >= 0; --i) {
1481 isl_bool redundant;
1483 redundant = div_is_redundant(bmap, i);
1484 if (redundant < 0)
1485 return isl_basic_map_free(bmap);
1486 if (!redundant)
1487 continue;
1488 bmap = isl_basic_map_drop_constraints_involving(bmap,
1489 v_div + i, 1);
1490 bmap = isl_basic_map_drop_div(bmap, i);
1492 return bmap;
1495 /* Mark "bmap" as final, without checking for obviously redundant
1496 * integer divisions. This function should be used when "bmap"
1497 * is known not to involve any such integer divisions.
1499 __isl_give isl_basic_map *isl_basic_map_mark_final(
1500 __isl_take isl_basic_map *bmap)
1502 if (!bmap)
1503 return NULL;
1504 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1505 return bmap;
1508 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1510 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1512 bmap = remove_redundant_divs(bmap);
1513 bmap = isl_basic_map_mark_final(bmap);
1514 return bmap;
1517 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1519 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1522 /* Remove definition of any div that is defined in terms of the given variable.
1523 * The div itself is not removed. Functions such as
1524 * eliminate_divs_ineq depend on the other divs remaining in place.
1526 static __isl_give isl_basic_map *remove_dependent_vars(
1527 __isl_take isl_basic_map *bmap, int pos)
1529 int i;
1531 if (!bmap)
1532 return NULL;
1534 for (i = 0; i < bmap->n_div; ++i) {
1535 if (isl_int_is_zero(bmap->div[i][0]))
1536 continue;
1537 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1538 continue;
1539 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1540 if (!bmap)
1541 return NULL;
1543 return bmap;
1546 /* Eliminate the specified variables from the constraints using
1547 * Fourier-Motzkin. The variables themselves are not removed.
1549 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1550 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1552 int d;
1553 int i, j, k;
1554 unsigned total;
1555 int need_gauss = 0;
1557 if (n == 0)
1558 return bmap;
1559 if (!bmap)
1560 return NULL;
1561 total = isl_basic_map_total_dim(bmap);
1563 bmap = isl_basic_map_cow(bmap);
1564 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1565 bmap = remove_dependent_vars(bmap, d);
1566 if (!bmap)
1567 return NULL;
1569 for (d = pos + n - 1;
1570 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1571 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1572 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1573 int n_lower, n_upper;
1574 if (!bmap)
1575 return NULL;
1576 for (i = 0; i < bmap->n_eq; ++i) {
1577 if (isl_int_is_zero(bmap->eq[i][1+d]))
1578 continue;
1579 bmap = eliminate_var_using_equality(bmap, d,
1580 bmap->eq[i], 0, NULL);
1581 if (isl_basic_map_drop_equality(bmap, i) < 0)
1582 return isl_basic_map_free(bmap);
1583 need_gauss = 1;
1584 break;
1586 if (i < bmap->n_eq)
1587 continue;
1588 n_lower = 0;
1589 n_upper = 0;
1590 for (i = 0; i < bmap->n_ineq; ++i) {
1591 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1592 n_lower++;
1593 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1594 n_upper++;
1596 bmap = isl_basic_map_extend_constraints(bmap,
1597 0, n_lower * n_upper);
1598 if (!bmap)
1599 goto error;
1600 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1601 int last;
1602 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1603 continue;
1604 last = -1;
1605 for (j = 0; j < i; ++j) {
1606 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1607 continue;
1608 last = j;
1609 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1610 isl_int_sgn(bmap->ineq[j][1+d]))
1611 continue;
1612 k = isl_basic_map_alloc_inequality(bmap);
1613 if (k < 0)
1614 goto error;
1615 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1616 1+total);
1617 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1618 1+d, 1+total, NULL);
1620 isl_basic_map_drop_inequality(bmap, i);
1621 i = last + 1;
1623 if (n_lower > 0 && n_upper > 0) {
1624 bmap = isl_basic_map_normalize_constraints(bmap);
1625 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1626 NULL, 0);
1627 bmap = isl_basic_map_gauss(bmap, NULL);
1628 bmap = isl_basic_map_remove_redundancies(bmap);
1629 need_gauss = 0;
1630 if (!bmap)
1631 goto error;
1632 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1633 break;
1636 if (need_gauss)
1637 bmap = isl_basic_map_gauss(bmap, NULL);
1638 return bmap;
1639 error:
1640 isl_basic_map_free(bmap);
1641 return NULL;
1644 struct isl_basic_set *isl_basic_set_eliminate_vars(
1645 struct isl_basic_set *bset, unsigned pos, unsigned n)
1647 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1648 pos, n));
1651 /* Eliminate the specified n dimensions starting at first from the
1652 * constraints, without removing the dimensions from the space.
1653 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1654 * Otherwise, they are projected out and the original space is restored.
1656 __isl_give isl_basic_map *isl_basic_map_eliminate(
1657 __isl_take isl_basic_map *bmap,
1658 enum isl_dim_type type, unsigned first, unsigned n)
1660 isl_space *space;
1662 if (!bmap)
1663 return NULL;
1664 if (n == 0)
1665 return bmap;
1667 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1668 return isl_basic_map_free(bmap);
1670 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1671 first += isl_basic_map_offset(bmap, type) - 1;
1672 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1673 return isl_basic_map_finalize(bmap);
1676 space = isl_basic_map_get_space(bmap);
1677 bmap = isl_basic_map_project_out(bmap, type, first, n);
1678 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1679 bmap = isl_basic_map_reset_space(bmap, space);
1680 return bmap;
1683 __isl_give isl_basic_set *isl_basic_set_eliminate(
1684 __isl_take isl_basic_set *bset,
1685 enum isl_dim_type type, unsigned first, unsigned n)
1687 return isl_basic_map_eliminate(bset, type, first, n);
1690 /* Remove all constraints from "bmap" that reference any unknown local
1691 * variables (directly or indirectly).
1693 * Dropping all constraints on a local variable will make it redundant,
1694 * so it will get removed implicitly by
1695 * isl_basic_map_drop_constraints_involving_dims. Some other local
1696 * variables may also end up becoming redundant if they only appear
1697 * in constraints together with the unknown local variable.
1698 * Therefore, start over after calling
1699 * isl_basic_map_drop_constraints_involving_dims.
1701 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1702 __isl_take isl_basic_map *bmap)
1704 isl_bool known;
1705 int i, n_div, o_div;
1707 known = isl_basic_map_divs_known(bmap);
1708 if (known < 0)
1709 return isl_basic_map_free(bmap);
1710 if (known)
1711 return bmap;
1713 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1714 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1716 for (i = 0; i < n_div; ++i) {
1717 known = isl_basic_map_div_is_known(bmap, i);
1718 if (known < 0)
1719 return isl_basic_map_free(bmap);
1720 if (known)
1721 continue;
1722 bmap = remove_dependent_vars(bmap, o_div + i);
1723 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1724 isl_dim_div, i, 1);
1725 if (!bmap)
1726 return NULL;
1727 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1728 i = -1;
1731 return bmap;
1734 /* Remove all constraints from "map" that reference any unknown local
1735 * variables (directly or indirectly).
1737 * Since constraints may get dropped from the basic maps,
1738 * they may no longer be disjoint from each other.
1740 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1741 __isl_take isl_map *map)
1743 int i;
1744 isl_bool known;
1746 known = isl_map_divs_known(map);
1747 if (known < 0)
1748 return isl_map_free(map);
1749 if (known)
1750 return map;
1752 map = isl_map_cow(map);
1753 if (!map)
1754 return NULL;
1756 for (i = 0; i < map->n; ++i) {
1757 map->p[i] =
1758 isl_basic_map_drop_constraint_involving_unknown_divs(
1759 map->p[i]);
1760 if (!map->p[i])
1761 return isl_map_free(map);
1764 if (map->n > 1)
1765 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1767 return map;
1770 /* Don't assume equalities are in order, because align_divs
1771 * may have changed the order of the divs.
1773 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1775 int d, i;
1776 unsigned total;
1778 total = isl_space_dim(bmap->dim, isl_dim_all);
1779 for (d = 0; d < total; ++d)
1780 elim[d] = -1;
1781 for (i = 0; i < bmap->n_eq; ++i) {
1782 for (d = total - 1; d >= 0; --d) {
1783 if (isl_int_is_zero(bmap->eq[i][1+d]))
1784 continue;
1785 elim[d] = i;
1786 break;
1791 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1792 int *elim)
1794 compute_elimination_index(bset_to_bmap(bset), elim);
1797 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1798 __isl_keep isl_basic_map *bmap, int *elim)
1800 int d;
1801 int copied = 0;
1802 unsigned total;
1804 total = isl_space_dim(bmap->dim, isl_dim_all);
1805 for (d = total - 1; d >= 0; --d) {
1806 if (isl_int_is_zero(src[1+d]))
1807 continue;
1808 if (elim[d] == -1)
1809 continue;
1810 if (!copied) {
1811 isl_seq_cpy(dst, src, 1 + total);
1812 copied = 1;
1814 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1816 return copied;
1819 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1820 __isl_keep isl_basic_set *bset, int *elim)
1822 return reduced_using_equalities(dst, src,
1823 bset_to_bmap(bset), elim);
1826 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1827 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1829 int i;
1830 int *elim;
1832 if (!bset || !context)
1833 goto error;
1835 if (context->n_eq == 0) {
1836 isl_basic_set_free(context);
1837 return bset;
1840 bset = isl_basic_set_cow(bset);
1841 if (!bset)
1842 goto error;
1844 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1845 if (!elim)
1846 goto error;
1847 set_compute_elimination_index(context, elim);
1848 for (i = 0; i < bset->n_eq; ++i)
1849 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1850 context, elim);
1851 for (i = 0; i < bset->n_ineq; ++i)
1852 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1853 context, elim);
1854 isl_basic_set_free(context);
1855 free(elim);
1856 bset = isl_basic_set_simplify(bset);
1857 bset = isl_basic_set_finalize(bset);
1858 return bset;
1859 error:
1860 isl_basic_set_free(bset);
1861 isl_basic_set_free(context);
1862 return NULL;
1865 /* For each inequality in "ineq" that is a shifted (more relaxed)
1866 * copy of an inequality in "context", mark the corresponding entry
1867 * in "row" with -1.
1868 * If an inequality only has a non-negative constant term, then
1869 * mark it as well.
1871 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1872 __isl_keep isl_basic_set *context, int *row)
1874 struct isl_constraint_index ci;
1875 int n_ineq;
1876 unsigned total;
1877 int k;
1879 if (!ineq || !context)
1880 return isl_stat_error;
1881 if (context->n_ineq == 0)
1882 return isl_stat_ok;
1883 if (setup_constraint_index(&ci, context) < 0)
1884 return isl_stat_error;
1886 n_ineq = isl_mat_rows(ineq);
1887 total = isl_mat_cols(ineq) - 1;
1888 for (k = 0; k < n_ineq; ++k) {
1889 int l;
1890 isl_bool redundant;
1892 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1893 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1894 row[k] = -1;
1895 continue;
1897 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1898 if (redundant < 0)
1899 goto error;
1900 if (!redundant)
1901 continue;
1902 row[k] = -1;
1904 constraint_index_free(&ci);
1905 return isl_stat_ok;
1906 error:
1907 constraint_index_free(&ci);
1908 return isl_stat_error;
1911 static __isl_give isl_basic_set *remove_shifted_constraints(
1912 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1914 struct isl_constraint_index ci;
1915 int k;
1917 if (!bset || !context)
1918 return bset;
1920 if (context->n_ineq == 0)
1921 return bset;
1922 if (setup_constraint_index(&ci, context) < 0)
1923 return bset;
1925 for (k = 0; k < bset->n_ineq; ++k) {
1926 isl_bool redundant;
1928 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1929 if (redundant < 0)
1930 goto error;
1931 if (!redundant)
1932 continue;
1933 bset = isl_basic_set_cow(bset);
1934 if (!bset)
1935 goto error;
1936 isl_basic_set_drop_inequality(bset, k);
1937 --k;
1939 constraint_index_free(&ci);
1940 return bset;
1941 error:
1942 constraint_index_free(&ci);
1943 return bset;
1946 /* Remove constraints from "bmap" that are identical to constraints
1947 * in "context" or that are more relaxed (greater constant term).
1949 * We perform the test for shifted copies on the pure constraints
1950 * in remove_shifted_constraints.
1952 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1953 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1955 isl_basic_set *bset, *bset_context;
1957 if (!bmap || !context)
1958 goto error;
1960 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1961 isl_basic_map_free(context);
1962 return bmap;
1965 context = isl_basic_map_align_divs(context, bmap);
1966 bmap = isl_basic_map_align_divs(bmap, context);
1968 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1969 bset_context = isl_basic_map_underlying_set(context);
1970 bset = remove_shifted_constraints(bset, bset_context);
1971 isl_basic_set_free(bset_context);
1973 bmap = isl_basic_map_overlying_set(bset, bmap);
1975 return bmap;
1976 error:
1977 isl_basic_map_free(bmap);
1978 isl_basic_map_free(context);
1979 return NULL;
1982 /* Does the (linear part of a) constraint "c" involve any of the "len"
1983 * "relevant" dimensions?
1985 static int is_related(isl_int *c, int len, int *relevant)
1987 int i;
1989 for (i = 0; i < len; ++i) {
1990 if (!relevant[i])
1991 continue;
1992 if (!isl_int_is_zero(c[i]))
1993 return 1;
1996 return 0;
1999 /* Drop constraints from "bmap" that do not involve any of
2000 * the dimensions marked "relevant".
2002 static __isl_give isl_basic_map *drop_unrelated_constraints(
2003 __isl_take isl_basic_map *bmap, int *relevant)
2005 int i, dim;
2007 dim = isl_basic_map_dim(bmap, isl_dim_all);
2008 for (i = 0; i < dim; ++i)
2009 if (!relevant[i])
2010 break;
2011 if (i >= dim)
2012 return bmap;
2014 for (i = bmap->n_eq - 1; i >= 0; --i)
2015 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2016 bmap = isl_basic_map_cow(bmap);
2017 if (isl_basic_map_drop_equality(bmap, i) < 0)
2018 return isl_basic_map_free(bmap);
2021 for (i = bmap->n_ineq - 1; i >= 0; --i)
2022 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2023 bmap = isl_basic_map_cow(bmap);
2024 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2025 return isl_basic_map_free(bmap);
2028 return bmap;
2031 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2033 * In particular, for any variable involved in the constraint,
2034 * find the actual group id from before and replace the group
2035 * of the corresponding variable by the minimal group of all
2036 * the variables involved in the constraint considered so far
2037 * (if this minimum is smaller) or replace the minimum by this group
2038 * (if the minimum is larger).
2040 * At the end, all the variables in "c" will (indirectly) point
2041 * to the minimal of the groups that they referred to originally.
2043 static void update_groups(int dim, int *group, isl_int *c)
2045 int j;
2046 int min = dim;
2048 for (j = 0; j < dim; ++j) {
2049 if (isl_int_is_zero(c[j]))
2050 continue;
2051 while (group[j] >= 0 && group[group[j]] != group[j])
2052 group[j] = group[group[j]];
2053 if (group[j] == min)
2054 continue;
2055 if (group[j] < min) {
2056 if (min >= 0 && min < dim)
2057 group[min] = group[j];
2058 min = group[j];
2059 } else
2060 group[group[j]] = min;
2064 /* Allocate an array of groups of variables, one for each variable
2065 * in "context", initialized to zero.
2067 static int *alloc_groups(__isl_keep isl_basic_set *context)
2069 isl_ctx *ctx;
2070 int dim;
2072 dim = isl_basic_set_dim(context, isl_dim_set);
2073 ctx = isl_basic_set_get_ctx(context);
2074 return isl_calloc_array(ctx, int, dim);
2077 /* Drop constraints from "bmap" that only involve variables that are
2078 * not related to any of the variables marked with a "-1" in "group".
2080 * We construct groups of variables that collect variables that
2081 * (indirectly) appear in some common constraint of "bmap".
2082 * Each group is identified by the first variable in the group,
2083 * except for the special group of variables that was already identified
2084 * in the input as -1 (or are related to those variables).
2085 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2086 * otherwise the group of i is the group of group[i].
2088 * We first initialize groups for the remaining variables.
2089 * Then we iterate over the constraints of "bmap" and update the
2090 * group of the variables in the constraint by the smallest group.
2091 * Finally, we resolve indirect references to groups by running over
2092 * the variables.
2094 * After computing the groups, we drop constraints that do not involve
2095 * any variables in the -1 group.
2097 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2098 __isl_take isl_basic_map *bmap, __isl_take int *group)
2100 int dim;
2101 int i;
2102 int last;
2104 if (!bmap)
2105 return NULL;
2107 dim = isl_basic_map_dim(bmap, isl_dim_all);
2109 last = -1;
2110 for (i = 0; i < dim; ++i)
2111 if (group[i] >= 0)
2112 last = group[i] = i;
2113 if (last < 0) {
2114 free(group);
2115 return bmap;
2118 for (i = 0; i < bmap->n_eq; ++i)
2119 update_groups(dim, group, bmap->eq[i] + 1);
2120 for (i = 0; i < bmap->n_ineq; ++i)
2121 update_groups(dim, group, bmap->ineq[i] + 1);
2123 for (i = 0; i < dim; ++i)
2124 if (group[i] >= 0)
2125 group[i] = group[group[i]];
2127 for (i = 0; i < dim; ++i)
2128 group[i] = group[i] == -1;
2130 bmap = drop_unrelated_constraints(bmap, group);
2132 free(group);
2133 return bmap;
2136 /* Drop constraints from "context" that are irrelevant for computing
2137 * the gist of "bset".
2139 * In particular, drop constraints in variables that are not related
2140 * to any of the variables involved in the constraints of "bset"
2141 * in the sense that there is no sequence of constraints that connects them.
2143 * We first mark all variables that appear in "bset" as belonging
2144 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2146 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2147 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2149 int *group;
2150 int dim;
2151 int i, j;
2153 if (!context || !bset)
2154 return isl_basic_set_free(context);
2156 group = alloc_groups(context);
2158 if (!group)
2159 return isl_basic_set_free(context);
2161 dim = isl_basic_set_dim(bset, isl_dim_set);
2162 for (i = 0; i < dim; ++i) {
2163 for (j = 0; j < bset->n_eq; ++j)
2164 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2165 break;
2166 if (j < bset->n_eq) {
2167 group[i] = -1;
2168 continue;
2170 for (j = 0; j < bset->n_ineq; ++j)
2171 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2172 break;
2173 if (j < bset->n_ineq)
2174 group[i] = -1;
2177 return isl_basic_map_drop_unrelated_constraints(context, group);
2180 /* Drop constraints from "context" that are irrelevant for computing
2181 * the gist of the inequalities "ineq".
2182 * Inequalities in "ineq" for which the corresponding element of row
2183 * is set to -1 have already been marked for removal and should be ignored.
2185 * In particular, drop constraints in variables that are not related
2186 * to any of the variables involved in "ineq"
2187 * in the sense that there is no sequence of constraints that connects them.
2189 * We first mark all variables that appear in "bset" as belonging
2190 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2192 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2193 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2195 int *group;
2196 int dim;
2197 int i, j, n;
2199 if (!context || !ineq)
2200 return isl_basic_set_free(context);
2202 group = alloc_groups(context);
2204 if (!group)
2205 return isl_basic_set_free(context);
2207 dim = isl_basic_set_dim(context, isl_dim_set);
2208 n = isl_mat_rows(ineq);
2209 for (i = 0; i < dim; ++i) {
2210 for (j = 0; j < n; ++j) {
2211 if (row[j] < 0)
2212 continue;
2213 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2214 break;
2216 if (j < n)
2217 group[i] = -1;
2220 return isl_basic_map_drop_unrelated_constraints(context, group);
2223 /* Do all "n" entries of "row" contain a negative value?
2225 static int all_neg(int *row, int n)
2227 int i;
2229 for (i = 0; i < n; ++i)
2230 if (row[i] >= 0)
2231 return 0;
2233 return 1;
2236 /* Update the inequalities in "bset" based on the information in "row"
2237 * and "tab".
2239 * In particular, the array "row" contains either -1, meaning that
2240 * the corresponding inequality of "bset" is redundant, or the index
2241 * of an inequality in "tab".
2243 * If the row entry is -1, then drop the inequality.
2244 * Otherwise, if the constraint is marked redundant in the tableau,
2245 * then drop the inequality. Similarly, if it is marked as an equality
2246 * in the tableau, then turn the inequality into an equality and
2247 * perform Gaussian elimination.
2249 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2250 __isl_keep int *row, struct isl_tab *tab)
2252 int i;
2253 unsigned n_ineq;
2254 unsigned n_eq;
2255 int found_equality = 0;
2257 if (!bset)
2258 return NULL;
2259 if (tab && tab->empty)
2260 return isl_basic_set_set_to_empty(bset);
2262 n_ineq = bset->n_ineq;
2263 for (i = n_ineq - 1; i >= 0; --i) {
2264 if (row[i] < 0) {
2265 if (isl_basic_set_drop_inequality(bset, i) < 0)
2266 return isl_basic_set_free(bset);
2267 continue;
2269 if (!tab)
2270 continue;
2271 n_eq = tab->n_eq;
2272 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2273 isl_basic_map_inequality_to_equality(bset, i);
2274 found_equality = 1;
2275 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2276 if (isl_basic_set_drop_inequality(bset, i) < 0)
2277 return isl_basic_set_free(bset);
2281 if (found_equality)
2282 bset = isl_basic_set_gauss(bset, NULL);
2283 bset = isl_basic_set_finalize(bset);
2284 return bset;
2287 /* Update the inequalities in "bset" based on the information in "row"
2288 * and "tab" and free all arguments (other than "bset").
2290 static __isl_give isl_basic_set *update_ineq_free(
2291 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2292 __isl_take isl_basic_set *context, __isl_take int *row,
2293 struct isl_tab *tab)
2295 isl_mat_free(ineq);
2296 isl_basic_set_free(context);
2298 bset = update_ineq(bset, row, tab);
2300 free(row);
2301 isl_tab_free(tab);
2302 return bset;
2305 /* Remove all information from bset that is redundant in the context
2306 * of context.
2307 * "ineq" contains the (possibly transformed) inequalities of "bset",
2308 * in the same order.
2309 * The (explicit) equalities of "bset" are assumed to have been taken
2310 * into account by the transformation such that only the inequalities
2311 * are relevant.
2312 * "context" is assumed not to be empty.
2314 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2315 * A value of -1 means that the inequality is obviously redundant and may
2316 * not even appear in "tab".
2318 * We first mark the inequalities of "bset"
2319 * that are obviously redundant with respect to some inequality in "context".
2320 * Then we remove those constraints from "context" that have become
2321 * irrelevant for computing the gist of "bset".
2322 * Note that this removal of constraints cannot be replaced by
2323 * a factorization because factors in "bset" may still be connected
2324 * to each other through constraints in "context".
2326 * If there are any inequalities left, we construct a tableau for
2327 * the context and then add the inequalities of "bset".
2328 * Before adding these inequalities, we freeze all constraints such that
2329 * they won't be considered redundant in terms of the constraints of "bset".
2330 * Then we detect all redundant constraints (among the
2331 * constraints that weren't frozen), first by checking for redundancy in the
2332 * the tableau and then by checking if replacing a constraint by its negation
2333 * would lead to an empty set. This last step is fairly expensive
2334 * and could be optimized by more reuse of the tableau.
2335 * Finally, we update bset according to the results.
2337 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2338 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2340 int i, r;
2341 int *row = NULL;
2342 isl_ctx *ctx;
2343 isl_basic_set *combined = NULL;
2344 struct isl_tab *tab = NULL;
2345 unsigned n_eq, context_ineq;
2347 if (!bset || !ineq || !context)
2348 goto error;
2350 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2351 isl_basic_set_free(context);
2352 isl_mat_free(ineq);
2353 return bset;
2356 ctx = isl_basic_set_get_ctx(context);
2357 row = isl_calloc_array(ctx, int, bset->n_ineq);
2358 if (!row)
2359 goto error;
2361 if (mark_shifted_constraints(ineq, context, row) < 0)
2362 goto error;
2363 if (all_neg(row, bset->n_ineq))
2364 return update_ineq_free(bset, ineq, context, row, NULL);
2366 context = drop_irrelevant_constraints_marked(context, ineq, row);
2367 if (!context)
2368 goto error;
2369 if (isl_basic_set_plain_is_universe(context))
2370 return update_ineq_free(bset, ineq, context, row, NULL);
2372 n_eq = context->n_eq;
2373 context_ineq = context->n_ineq;
2374 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2375 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2376 tab = isl_tab_from_basic_set(combined, 0);
2377 for (i = 0; i < context_ineq; ++i)
2378 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2379 goto error;
2380 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2381 goto error;
2382 r = context_ineq;
2383 for (i = 0; i < bset->n_ineq; ++i) {
2384 if (row[i] < 0)
2385 continue;
2386 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2387 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2388 goto error;
2389 row[i] = r++;
2391 if (isl_tab_detect_implicit_equalities(tab) < 0)
2392 goto error;
2393 if (isl_tab_detect_redundant(tab) < 0)
2394 goto error;
2395 for (i = bset->n_ineq - 1; i >= 0; --i) {
2396 isl_basic_set *test;
2397 int is_empty;
2399 if (row[i] < 0)
2400 continue;
2401 r = row[i];
2402 if (tab->con[n_eq + r].is_redundant)
2403 continue;
2404 test = isl_basic_set_dup(combined);
2405 test = isl_inequality_negate(test, r);
2406 test = isl_basic_set_update_from_tab(test, tab);
2407 is_empty = isl_basic_set_is_empty(test);
2408 isl_basic_set_free(test);
2409 if (is_empty < 0)
2410 goto error;
2411 if (is_empty)
2412 tab->con[n_eq + r].is_redundant = 1;
2414 bset = update_ineq_free(bset, ineq, context, row, tab);
2415 if (bset) {
2416 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2417 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2420 isl_basic_set_free(combined);
2421 return bset;
2422 error:
2423 free(row);
2424 isl_mat_free(ineq);
2425 isl_tab_free(tab);
2426 isl_basic_set_free(combined);
2427 isl_basic_set_free(context);
2428 isl_basic_set_free(bset);
2429 return NULL;
2432 /* Extract the inequalities of "bset" as an isl_mat.
2434 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2436 unsigned total;
2437 isl_ctx *ctx;
2438 isl_mat *ineq;
2440 if (!bset)
2441 return NULL;
2443 ctx = isl_basic_set_get_ctx(bset);
2444 total = isl_basic_set_total_dim(bset);
2445 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2446 0, 1 + total);
2448 return ineq;
2451 /* Remove all information from "bset" that is redundant in the context
2452 * of "context", for the case where both "bset" and "context" are
2453 * full-dimensional.
2455 static __isl_give isl_basic_set *uset_gist_uncompressed(
2456 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2458 isl_mat *ineq;
2460 ineq = extract_ineq(bset);
2461 return uset_gist_full(bset, ineq, context);
2464 /* Remove all information from "bset" that is redundant in the context
2465 * of "context", for the case where the combined equalities of
2466 * "bset" and "context" allow for a compression that can be obtained
2467 * by preapplication of "T".
2469 * "bset" itself is not transformed by "T". Instead, the inequalities
2470 * are extracted from "bset" and those are transformed by "T".
2471 * uset_gist_full then determines which of the transformed inequalities
2472 * are redundant with respect to the transformed "context" and removes
2473 * the corresponding inequalities from "bset".
2475 * After preapplying "T" to the inequalities, any common factor is
2476 * removed from the coefficients. If this results in a tightening
2477 * of the constant term, then the same tightening is applied to
2478 * the corresponding untransformed inequality in "bset".
2479 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2481 * g f'(x) + r >= 0
2483 * with 0 <= r < g, then it is equivalent to
2485 * f'(x) >= 0
2487 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2488 * subspace compressed by T since the latter would be transformed to
2490 * g f'(x) >= 0
2492 static __isl_give isl_basic_set *uset_gist_compressed(
2493 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2494 __isl_take isl_mat *T)
2496 isl_ctx *ctx;
2497 isl_mat *ineq;
2498 int i, n_row, n_col;
2499 isl_int rem;
2501 ineq = extract_ineq(bset);
2502 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2503 context = isl_basic_set_preimage(context, T);
2505 if (!ineq || !context)
2506 goto error;
2507 if (isl_basic_set_plain_is_empty(context)) {
2508 isl_mat_free(ineq);
2509 isl_basic_set_free(context);
2510 return isl_basic_set_set_to_empty(bset);
2513 ctx = isl_mat_get_ctx(ineq);
2514 n_row = isl_mat_rows(ineq);
2515 n_col = isl_mat_cols(ineq);
2516 isl_int_init(rem);
2517 for (i = 0; i < n_row; ++i) {
2518 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2519 if (isl_int_is_zero(ctx->normalize_gcd))
2520 continue;
2521 if (isl_int_is_one(ctx->normalize_gcd))
2522 continue;
2523 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2524 ctx->normalize_gcd, n_col - 1);
2525 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2526 isl_int_fdiv_q(ineq->row[i][0],
2527 ineq->row[i][0], ctx->normalize_gcd);
2528 if (isl_int_is_zero(rem))
2529 continue;
2530 bset = isl_basic_set_cow(bset);
2531 if (!bset)
2532 break;
2533 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2535 isl_int_clear(rem);
2537 return uset_gist_full(bset, ineq, context);
2538 error:
2539 isl_mat_free(ineq);
2540 isl_basic_set_free(context);
2541 isl_basic_set_free(bset);
2542 return NULL;
2545 /* Project "bset" onto the variables that are involved in "template".
2547 static __isl_give isl_basic_set *project_onto_involved(
2548 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2550 int i, n;
2552 if (!bset || !template)
2553 return isl_basic_set_free(bset);
2555 n = isl_basic_set_dim(template, isl_dim_set);
2557 for (i = 0; i < n; ++i) {
2558 isl_bool involved;
2560 involved = isl_basic_set_involves_dims(template,
2561 isl_dim_set, i, 1);
2562 if (involved < 0)
2563 return isl_basic_set_free(bset);
2564 if (involved)
2565 continue;
2566 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2569 return bset;
2572 /* Remove all information from bset that is redundant in the context
2573 * of context. In particular, equalities that are linear combinations
2574 * of those in context are removed. Then the inequalities that are
2575 * redundant in the context of the equalities and inequalities of
2576 * context are removed.
2578 * First of all, we drop those constraints from "context"
2579 * that are irrelevant for computing the gist of "bset".
2580 * Alternatively, we could factorize the intersection of "context" and "bset".
2582 * We first compute the intersection of the integer affine hulls
2583 * of "bset" and "context",
2584 * compute the gist inside this intersection and then reduce
2585 * the constraints with respect to the equalities of the context
2586 * that only involve variables already involved in the input.
2588 * If two constraints are mutually redundant, then uset_gist_full
2589 * will remove the second of those constraints. We therefore first
2590 * sort the constraints so that constraints not involving existentially
2591 * quantified variables are given precedence over those that do.
2592 * We have to perform this sorting before the variable compression,
2593 * because that may effect the order of the variables.
2595 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2596 __isl_take isl_basic_set *context)
2598 isl_mat *eq;
2599 isl_mat *T;
2600 isl_basic_set *aff;
2601 isl_basic_set *aff_context;
2602 unsigned total;
2604 if (!bset || !context)
2605 goto error;
2607 context = drop_irrelevant_constraints(context, bset);
2609 bset = isl_basic_set_detect_equalities(bset);
2610 aff = isl_basic_set_copy(bset);
2611 aff = isl_basic_set_plain_affine_hull(aff);
2612 context = isl_basic_set_detect_equalities(context);
2613 aff_context = isl_basic_set_copy(context);
2614 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2615 aff = isl_basic_set_intersect(aff, aff_context);
2616 if (!aff)
2617 goto error;
2618 if (isl_basic_set_plain_is_empty(aff)) {
2619 isl_basic_set_free(bset);
2620 isl_basic_set_free(context);
2621 return aff;
2623 bset = isl_basic_set_sort_constraints(bset);
2624 if (aff->n_eq == 0) {
2625 isl_basic_set_free(aff);
2626 return uset_gist_uncompressed(bset, context);
2628 total = isl_basic_set_total_dim(bset);
2629 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2630 eq = isl_mat_cow(eq);
2631 T = isl_mat_variable_compression(eq, NULL);
2632 isl_basic_set_free(aff);
2633 if (T && T->n_col == 0) {
2634 isl_mat_free(T);
2635 isl_basic_set_free(context);
2636 return isl_basic_set_set_to_empty(bset);
2639 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2640 aff_context = project_onto_involved(aff_context, bset);
2642 bset = uset_gist_compressed(bset, context, T);
2643 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2645 if (bset) {
2646 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2647 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2650 return bset;
2651 error:
2652 isl_basic_set_free(bset);
2653 isl_basic_set_free(context);
2654 return NULL;
2657 /* Return the number of equality constraints in "bmap" that involve
2658 * local variables. This function assumes that Gaussian elimination
2659 * has been applied to the equality constraints.
2661 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2663 int i;
2664 int total, n_div;
2666 if (!bmap)
2667 return -1;
2669 if (bmap->n_eq == 0)
2670 return 0;
2672 total = isl_basic_map_dim(bmap, isl_dim_all);
2673 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2674 total -= n_div;
2676 for (i = 0; i < bmap->n_eq; ++i)
2677 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2678 n_div) == -1)
2679 return i;
2681 return bmap->n_eq;
2684 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2685 * The constraints are assumed not to involve any local variables.
2687 static __isl_give isl_basic_map *basic_map_from_equalities(
2688 __isl_take isl_space *space, __isl_take isl_mat *eq)
2690 int i, k;
2691 isl_basic_map *bmap = NULL;
2693 if (!space || !eq)
2694 goto error;
2696 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2697 isl_die(isl_space_get_ctx(space), isl_error_internal,
2698 "unexpected number of columns", goto error);
2700 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2701 0, eq->n_row, 0);
2702 for (i = 0; i < eq->n_row; ++i) {
2703 k = isl_basic_map_alloc_equality(bmap);
2704 if (k < 0)
2705 goto error;
2706 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2709 isl_space_free(space);
2710 isl_mat_free(eq);
2711 return bmap;
2712 error:
2713 isl_space_free(space);
2714 isl_mat_free(eq);
2715 isl_basic_map_free(bmap);
2716 return NULL;
2719 /* Construct and return a variable compression based on the equality
2720 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2721 * "n1" is the number of (initial) equality constraints in "bmap1"
2722 * that do involve local variables.
2723 * "n2" is the number of (initial) equality constraints in "bmap2"
2724 * that do involve local variables.
2725 * "total" is the total number of other variables.
2726 * This function assumes that Gaussian elimination
2727 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2728 * such that the equality constraints not involving local variables
2729 * are those that start at "n1" or "n2".
2731 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2732 * then simply compute the compression based on the equality constraints
2733 * in the other basic map.
2734 * Otherwise, combine the equality constraints from both into a new
2735 * basic map such that Gaussian elimination can be applied to this combination
2736 * and then construct a variable compression from the resulting
2737 * equality constraints.
2739 static __isl_give isl_mat *combined_variable_compression(
2740 __isl_keep isl_basic_map *bmap1, int n1,
2741 __isl_keep isl_basic_map *bmap2, int n2, int total)
2743 isl_ctx *ctx;
2744 isl_mat *E1, *E2, *V;
2745 isl_basic_map *bmap;
2747 ctx = isl_basic_map_get_ctx(bmap1);
2748 if (bmap1->n_eq == n1) {
2749 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2750 n2, bmap2->n_eq - n2, 0, 1 + total);
2751 return isl_mat_variable_compression(E2, NULL);
2753 if (bmap2->n_eq == n2) {
2754 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2755 n1, bmap1->n_eq - n1, 0, 1 + total);
2756 return isl_mat_variable_compression(E1, NULL);
2758 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2759 n1, bmap1->n_eq - n1, 0, 1 + total);
2760 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2761 n2, bmap2->n_eq - n2, 0, 1 + total);
2762 E1 = isl_mat_concat(E1, E2);
2763 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2764 bmap = isl_basic_map_gauss(bmap, NULL);
2765 if (!bmap)
2766 return NULL;
2767 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2768 V = isl_mat_variable_compression(E1, NULL);
2769 isl_basic_map_free(bmap);
2771 return V;
2774 /* Extract the stride constraints from "bmap", compressed
2775 * with respect to both the stride constraints in "context" and
2776 * the remaining equality constraints in both "bmap" and "context".
2777 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2778 * "context_n_eq" is the number of (initial) stride constraints in "context".
2780 * Let x be all variables in "bmap" (and "context") other than the local
2781 * variables. First compute a variable compression
2783 * x = V x'
2785 * based on the non-stride equality constraints in "bmap" and "context".
2786 * Consider the stride constraints of "context",
2788 * A(x) + B(y) = 0
2790 * with y the local variables and plug in the variable compression,
2791 * resulting in
2793 * A(V x') + B(y) = 0
2795 * Use these constraints to compute a parameter compression on x'
2797 * x' = T x''
2799 * Now consider the stride constraints of "bmap"
2801 * C(x) + D(y) = 0
2803 * and plug in x = V*T x''.
2804 * That is, return A = [C*V*T D].
2806 static __isl_give isl_mat *extract_compressed_stride_constraints(
2807 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2808 __isl_keep isl_basic_map *context, int context_n_eq)
2810 int total, n_div;
2811 isl_ctx *ctx;
2812 isl_mat *A, *B, *T, *V;
2814 total = isl_basic_map_dim(context, isl_dim_all);
2815 n_div = isl_basic_map_dim(context, isl_dim_div);
2816 total -= n_div;
2818 ctx = isl_basic_map_get_ctx(bmap);
2820 V = combined_variable_compression(bmap, bmap_n_eq,
2821 context, context_n_eq, total);
2823 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2824 B = isl_mat_sub_alloc6(ctx, context->eq,
2825 0, context_n_eq, 1 + total, n_div);
2826 A = isl_mat_product(A, isl_mat_copy(V));
2827 T = isl_mat_parameter_compression_ext(A, B);
2828 T = isl_mat_product(V, T);
2830 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2831 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2833 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2834 0, bmap_n_eq, 0, 1 + total + n_div);
2835 A = isl_mat_product(A, T);
2837 return A;
2840 /* Remove the prime factors from *g that have an exponent that
2841 * is strictly smaller than the exponent in "c".
2842 * All exponents in *g are known to be smaller than or equal
2843 * to those in "c".
2845 * That is, if *g is equal to
2847 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2849 * and "c" is equal to
2851 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2853 * then update *g to
2855 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2856 * p_n^{e_n * (e_n = f_n)}
2858 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2859 * neither does the gcd of *g and c / *g.
2860 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2861 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2862 * Dividing *g by this gcd therefore strictly reduces the exponent
2863 * of the prime factors that need to be removed, while leaving the
2864 * other prime factors untouched.
2865 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2866 * removes all undesired factors, without removing any others.
2868 static void remove_incomplete_powers(isl_int *g, isl_int c)
2870 isl_int t;
2872 isl_int_init(t);
2873 for (;;) {
2874 isl_int_divexact(t, c, *g);
2875 isl_int_gcd(t, t, *g);
2876 if (isl_int_is_one(t))
2877 break;
2878 isl_int_divexact(*g, *g, t);
2880 isl_int_clear(t);
2883 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2884 * of the same stride constraints in a compressed space that exploits
2885 * all equalities in the context and the other equalities in "bmap".
2887 * If the stride constraints of "bmap" are of the form
2889 * C(x) + D(y) = 0
2891 * then A is of the form
2893 * B(x') + D(y) = 0
2895 * If any of these constraints involves only a single local variable y,
2896 * then the constraint appears as
2898 * f(x) + m y_i = 0
2900 * in "bmap" and as
2902 * h(x') + m y_i = 0
2904 * in "A".
2906 * Let g be the gcd of m and the coefficients of h.
2907 * Then, in particular, g is a divisor of the coefficients of h and
2909 * f(x) = h(x')
2911 * is known to be a multiple of g.
2912 * If some prime factor in m appears with the same exponent in g,
2913 * then it can be removed from m because f(x) is already known
2914 * to be a multiple of g and therefore in particular of this power
2915 * of the prime factors.
2916 * Prime factors that appear with a smaller exponent in g cannot
2917 * be removed from m.
2918 * Let g' be the divisor of g containing all prime factors that
2919 * appear with the same exponent in m and g, then
2921 * f(x) + m y_i = 0
2923 * can be replaced by
2925 * f(x) + m/g' y_i' = 0
2927 * Note that (if g' != 1) this changes the explicit representation
2928 * of y_i to that of y_i', so the integer division at position i
2929 * is marked unknown and later recomputed by a call to
2930 * isl_basic_map_gauss.
2932 static __isl_give isl_basic_map *reduce_stride_constraints(
2933 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2935 int i;
2936 int total, n_div;
2937 int any = 0;
2938 isl_int gcd;
2940 if (!bmap || !A)
2941 return isl_basic_map_free(bmap);
2943 total = isl_basic_map_dim(bmap, isl_dim_all);
2944 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2945 total -= n_div;
2947 isl_int_init(gcd);
2948 for (i = 0; i < n; ++i) {
2949 int div;
2951 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2952 if (div < 0)
2953 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2954 "equality constraints modified unexpectedly",
2955 goto error);
2956 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
2957 n_div - div - 1) != -1)
2958 continue;
2959 if (isl_mat_row_gcd(A, i, &gcd) < 0)
2960 goto error;
2961 if (isl_int_is_one(gcd))
2962 continue;
2963 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2964 if (isl_int_is_one(gcd))
2965 continue;
2966 isl_int_divexact(bmap->eq[i][1 + total + div],
2967 bmap->eq[i][1 + total + div], gcd);
2968 bmap = isl_basic_map_mark_div_unknown(bmap, div);
2969 if (!bmap)
2970 goto error;
2971 any = 1;
2973 isl_int_clear(gcd);
2975 if (any)
2976 bmap = isl_basic_map_gauss(bmap, NULL);
2978 return bmap;
2979 error:
2980 isl_int_clear(gcd);
2981 isl_basic_map_free(bmap);
2982 return NULL;
2985 /* Simplify the stride constraints in "bmap" based on
2986 * the remaining equality constraints in "bmap" and all equality
2987 * constraints in "context".
2988 * Only do this if both "bmap" and "context" have stride constraints.
2990 * First extract a copy of the stride constraints in "bmap" in a compressed
2991 * space exploiting all the other equality constraints and then
2992 * use this compressed copy to simplify the original stride constraints.
2994 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
2995 __isl_keep isl_basic_map *context)
2997 int bmap_n_eq, context_n_eq;
2998 isl_mat *A;
3000 if (!bmap || !context)
3001 return isl_basic_map_free(bmap);
3003 bmap_n_eq = n_div_eq(bmap);
3004 context_n_eq = n_div_eq(context);
3006 if (bmap_n_eq < 0 || context_n_eq < 0)
3007 return isl_basic_map_free(bmap);
3008 if (bmap_n_eq == 0 || context_n_eq == 0)
3009 return bmap;
3011 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3012 context, context_n_eq);
3013 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3015 isl_mat_free(A);
3017 return bmap;
3020 /* Return a basic map that has the same intersection with "context" as "bmap"
3021 * and that is as "simple" as possible.
3023 * The core computation is performed on the pure constraints.
3024 * When we add back the meaning of the integer divisions, we need
3025 * to (re)introduce the div constraints. If we happen to have
3026 * discovered that some of these integer divisions are equal to
3027 * some affine combination of other variables, then these div
3028 * constraints may end up getting simplified in terms of the equalities,
3029 * resulting in extra inequalities on the other variables that
3030 * may have been removed already or that may not even have been
3031 * part of the input. We try and remove those constraints of
3032 * this form that are most obviously redundant with respect to
3033 * the context. We also remove those div constraints that are
3034 * redundant with respect to the other constraints in the result.
3036 * The stride constraints among the equality constraints in "bmap" are
3037 * also simplified with respecting to the other equality constraints
3038 * in "bmap" and with respect to all equality constraints in "context".
3040 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3041 __isl_take isl_basic_map *context)
3043 isl_basic_set *bset, *eq;
3044 isl_basic_map *eq_bmap;
3045 unsigned total, n_div, extra, n_eq, n_ineq;
3047 if (!bmap || !context)
3048 goto error;
3050 if (isl_basic_map_plain_is_universe(bmap)) {
3051 isl_basic_map_free(context);
3052 return bmap;
3054 if (isl_basic_map_plain_is_empty(context)) {
3055 isl_space *space = isl_basic_map_get_space(bmap);
3056 isl_basic_map_free(bmap);
3057 isl_basic_map_free(context);
3058 return isl_basic_map_universe(space);
3060 if (isl_basic_map_plain_is_empty(bmap)) {
3061 isl_basic_map_free(context);
3062 return bmap;
3065 bmap = isl_basic_map_remove_redundancies(bmap);
3066 context = isl_basic_map_remove_redundancies(context);
3067 context = isl_basic_map_align_divs(context, bmap);
3068 if (!context)
3069 goto error;
3071 n_div = isl_basic_map_dim(context, isl_dim_div);
3072 total = isl_basic_map_dim(bmap, isl_dim_all);
3073 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3075 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3076 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3077 bset = uset_gist(bset,
3078 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3079 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3081 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3082 isl_basic_set_plain_is_empty(bset)) {
3083 isl_basic_map_free(context);
3084 return isl_basic_map_overlying_set(bset, bmap);
3087 n_eq = bset->n_eq;
3088 n_ineq = bset->n_ineq;
3089 eq = isl_basic_set_copy(bset);
3090 eq = isl_basic_set_cow(eq);
3091 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3092 eq = isl_basic_set_free(eq);
3093 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3094 bset = isl_basic_set_free(bset);
3096 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3097 eq_bmap = gist_strides(eq_bmap, context);
3098 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3099 bmap = isl_basic_map_overlying_set(bset, bmap);
3100 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3101 bmap = isl_basic_map_remove_redundancies(bmap);
3103 return bmap;
3104 error:
3105 isl_basic_map_free(bmap);
3106 isl_basic_map_free(context);
3107 return NULL;
3111 * Assumes context has no implicit divs.
3113 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3114 __isl_take isl_basic_map *context)
3116 int i;
3118 if (!map || !context)
3119 goto error;
3121 if (isl_basic_map_plain_is_empty(context)) {
3122 isl_space *space = isl_map_get_space(map);
3123 isl_map_free(map);
3124 isl_basic_map_free(context);
3125 return isl_map_universe(space);
3128 context = isl_basic_map_remove_redundancies(context);
3129 map = isl_map_cow(map);
3130 if (!map || !context)
3131 goto error;
3132 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3133 map = isl_map_compute_divs(map);
3134 if (!map)
3135 goto error;
3136 for (i = map->n - 1; i >= 0; --i) {
3137 map->p[i] = isl_basic_map_gist(map->p[i],
3138 isl_basic_map_copy(context));
3139 if (!map->p[i])
3140 goto error;
3141 if (isl_basic_map_plain_is_empty(map->p[i])) {
3142 isl_basic_map_free(map->p[i]);
3143 if (i != map->n - 1)
3144 map->p[i] = map->p[map->n - 1];
3145 map->n--;
3148 isl_basic_map_free(context);
3149 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3150 return map;
3151 error:
3152 isl_map_free(map);
3153 isl_basic_map_free(context);
3154 return NULL;
3157 /* Drop all inequalities from "bmap" that also appear in "context".
3158 * "context" is assumed to have only known local variables and
3159 * the initial local variables of "bmap" are assumed to be the same
3160 * as those of "context".
3161 * The constraints of both "bmap" and "context" are assumed
3162 * to have been sorted using isl_basic_map_sort_constraints.
3164 * Run through the inequality constraints of "bmap" and "context"
3165 * in sorted order.
3166 * If a constraint of "bmap" involves variables not in "context",
3167 * then it cannot appear in "context".
3168 * If a matching constraint is found, it is removed from "bmap".
3170 static __isl_give isl_basic_map *drop_inequalities(
3171 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3173 int i1, i2;
3174 unsigned total, extra;
3176 if (!bmap || !context)
3177 return isl_basic_map_free(bmap);
3179 total = isl_basic_map_total_dim(context);
3180 extra = isl_basic_map_total_dim(bmap) - total;
3182 i1 = bmap->n_ineq - 1;
3183 i2 = context->n_ineq - 1;
3184 while (bmap && i1 >= 0 && i2 >= 0) {
3185 int cmp;
3187 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3188 extra) != -1) {
3189 --i1;
3190 continue;
3192 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3193 context->ineq[i2]);
3194 if (cmp < 0) {
3195 --i2;
3196 continue;
3198 if (cmp > 0) {
3199 --i1;
3200 continue;
3202 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3203 bmap = isl_basic_map_cow(bmap);
3204 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3205 bmap = isl_basic_map_free(bmap);
3207 --i1;
3208 --i2;
3211 return bmap;
3214 /* Drop all equalities from "bmap" that also appear in "context".
3215 * "context" is assumed to have only known local variables and
3216 * the initial local variables of "bmap" are assumed to be the same
3217 * as those of "context".
3219 * Run through the equality constraints of "bmap" and "context"
3220 * in sorted order.
3221 * If a constraint of "bmap" involves variables not in "context",
3222 * then it cannot appear in "context".
3223 * If a matching constraint is found, it is removed from "bmap".
3225 static __isl_give isl_basic_map *drop_equalities(
3226 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3228 int i1, i2;
3229 unsigned total, extra;
3231 if (!bmap || !context)
3232 return isl_basic_map_free(bmap);
3234 total = isl_basic_map_total_dim(context);
3235 extra = isl_basic_map_total_dim(bmap) - total;
3237 i1 = bmap->n_eq - 1;
3238 i2 = context->n_eq - 1;
3240 while (bmap && i1 >= 0 && i2 >= 0) {
3241 int last1, last2;
3243 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3244 extra) != -1)
3245 break;
3246 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3247 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3248 if (last1 > last2) {
3249 --i2;
3250 continue;
3252 if (last1 < last2) {
3253 --i1;
3254 continue;
3256 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3257 bmap = isl_basic_map_cow(bmap);
3258 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3259 bmap = isl_basic_map_free(bmap);
3261 --i1;
3262 --i2;
3265 return bmap;
3268 /* Remove the constraints in "context" from "bmap".
3269 * "context" is assumed to have explicit representations
3270 * for all local variables.
3272 * First align the divs of "bmap" to those of "context" and
3273 * sort the constraints. Then drop all constraints from "bmap"
3274 * that appear in "context".
3276 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3277 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3279 isl_bool done, known;
3281 done = isl_basic_map_plain_is_universe(context);
3282 if (done == isl_bool_false)
3283 done = isl_basic_map_plain_is_universe(bmap);
3284 if (done == isl_bool_false)
3285 done = isl_basic_map_plain_is_empty(context);
3286 if (done == isl_bool_false)
3287 done = isl_basic_map_plain_is_empty(bmap);
3288 if (done < 0)
3289 goto error;
3290 if (done) {
3291 isl_basic_map_free(context);
3292 return bmap;
3294 known = isl_basic_map_divs_known(context);
3295 if (known < 0)
3296 goto error;
3297 if (!known)
3298 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3299 "context has unknown divs", goto error);
3301 bmap = isl_basic_map_align_divs(bmap, context);
3302 bmap = isl_basic_map_gauss(bmap, NULL);
3303 bmap = isl_basic_map_sort_constraints(bmap);
3304 context = isl_basic_map_sort_constraints(context);
3306 bmap = drop_inequalities(bmap, context);
3307 bmap = drop_equalities(bmap, context);
3309 isl_basic_map_free(context);
3310 bmap = isl_basic_map_finalize(bmap);
3311 return bmap;
3312 error:
3313 isl_basic_map_free(bmap);
3314 isl_basic_map_free(context);
3315 return NULL;
3318 /* Replace "map" by the disjunct at position "pos" and free "context".
3320 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3321 int pos, __isl_take isl_basic_map *context)
3323 isl_basic_map *bmap;
3325 bmap = isl_basic_map_copy(map->p[pos]);
3326 isl_map_free(map);
3327 isl_basic_map_free(context);
3328 return isl_map_from_basic_map(bmap);
3331 /* Remove the constraints in "context" from "map".
3332 * If any of the disjuncts in the result turns out to be the universe,
3333 * then return this universe.
3334 * "context" is assumed to have explicit representations
3335 * for all local variables.
3337 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3338 __isl_take isl_basic_map *context)
3340 int i;
3341 isl_bool univ, known;
3343 univ = isl_basic_map_plain_is_universe(context);
3344 if (univ < 0)
3345 goto error;
3346 if (univ) {
3347 isl_basic_map_free(context);
3348 return map;
3350 known = isl_basic_map_divs_known(context);
3351 if (known < 0)
3352 goto error;
3353 if (!known)
3354 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3355 "context has unknown divs", goto error);
3357 map = isl_map_cow(map);
3358 if (!map)
3359 goto error;
3360 for (i = 0; i < map->n; ++i) {
3361 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3362 isl_basic_map_copy(context));
3363 univ = isl_basic_map_plain_is_universe(map->p[i]);
3364 if (univ < 0)
3365 goto error;
3366 if (univ && map->n > 1)
3367 return replace_by_disjunct(map, i, context);
3370 isl_basic_map_free(context);
3371 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3372 if (map->n > 1)
3373 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3374 return map;
3375 error:
3376 isl_map_free(map);
3377 isl_basic_map_free(context);
3378 return NULL;
3381 /* Remove the constraints in "context" from "set".
3382 * If any of the disjuncts in the result turns out to be the universe,
3383 * then return this universe.
3384 * "context" is assumed to have explicit representations
3385 * for all local variables.
3387 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3388 __isl_take isl_basic_set *context)
3390 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3391 bset_to_bmap(context)));
3394 /* Remove the constraints in "context" from "map".
3395 * If any of the disjuncts in the result turns out to be the universe,
3396 * then return this universe.
3397 * "context" is assumed to consist of a single disjunct and
3398 * to have explicit representations for all local variables.
3400 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3401 __isl_take isl_map *context)
3403 isl_basic_map *hull;
3405 hull = isl_map_unshifted_simple_hull(context);
3406 return isl_map_plain_gist_basic_map(map, hull);
3409 /* Replace "map" by a universe map in the same space and free "drop".
3411 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3412 __isl_take isl_map *drop)
3414 isl_map *res;
3416 res = isl_map_universe(isl_map_get_space(map));
3417 isl_map_free(map);
3418 isl_map_free(drop);
3419 return res;
3422 /* Return a map that has the same intersection with "context" as "map"
3423 * and that is as "simple" as possible.
3425 * If "map" is already the universe, then we cannot make it any simpler.
3426 * Similarly, if "context" is the universe, then we cannot exploit it
3427 * to simplify "map"
3428 * If "map" and "context" are identical to each other, then we can
3429 * return the corresponding universe.
3431 * If either "map" or "context" consists of multiple disjuncts,
3432 * then check if "context" happens to be a subset of "map",
3433 * in which case all constraints can be removed.
3434 * In case of multiple disjuncts, the standard procedure
3435 * may not be able to detect that all constraints can be removed.
3437 * If none of these cases apply, we have to work a bit harder.
3438 * During this computation, we make use of a single disjunct context,
3439 * so if the original context consists of more than one disjunct
3440 * then we need to approximate the context by a single disjunct set.
3441 * Simply taking the simple hull may drop constraints that are
3442 * only implicitly available in each disjunct. We therefore also
3443 * look for constraints among those defining "map" that are valid
3444 * for the context. These can then be used to simplify away
3445 * the corresponding constraints in "map".
3447 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3448 __isl_take isl_map *context)
3450 int equal;
3451 int is_universe;
3452 int single_disjunct_map, single_disjunct_context;
3453 isl_bool subset;
3454 isl_basic_map *hull;
3456 is_universe = isl_map_plain_is_universe(map);
3457 if (is_universe >= 0 && !is_universe)
3458 is_universe = isl_map_plain_is_universe(context);
3459 if (is_universe < 0)
3460 goto error;
3461 if (is_universe) {
3462 isl_map_free(context);
3463 return map;
3466 equal = isl_map_plain_is_equal(map, context);
3467 if (equal < 0)
3468 goto error;
3469 if (equal)
3470 return replace_by_universe(map, context);
3472 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3473 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3474 if (!single_disjunct_map || !single_disjunct_context) {
3475 subset = isl_map_is_subset(context, map);
3476 if (subset < 0)
3477 goto error;
3478 if (subset)
3479 return replace_by_universe(map, context);
3482 context = isl_map_compute_divs(context);
3483 if (!context)
3484 goto error;
3485 if (single_disjunct_context) {
3486 hull = isl_map_simple_hull(context);
3487 } else {
3488 isl_ctx *ctx;
3489 isl_map_list *list;
3491 ctx = isl_map_get_ctx(map);
3492 list = isl_map_list_alloc(ctx, 2);
3493 list = isl_map_list_add(list, isl_map_copy(context));
3494 list = isl_map_list_add(list, isl_map_copy(map));
3495 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3496 list);
3498 return isl_map_gist_basic_map(map, hull);
3499 error:
3500 isl_map_free(map);
3501 isl_map_free(context);
3502 return NULL;
3505 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3506 __isl_take isl_map *context)
3508 return isl_map_align_params_map_map_and(map, context, &map_gist);
3511 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3512 struct isl_basic_set *context)
3514 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3515 bset_to_bmap(context)));
3518 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3519 __isl_take isl_basic_set *context)
3521 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3522 bset_to_bmap(context)));
3525 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3526 __isl_take isl_basic_set *context)
3528 isl_space *space = isl_set_get_space(set);
3529 isl_basic_set *dom_context = isl_basic_set_universe(space);
3530 dom_context = isl_basic_set_intersect_params(dom_context, context);
3531 return isl_set_gist_basic_set(set, dom_context);
3534 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3535 __isl_take isl_set *context)
3537 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3540 /* Compute the gist of "bmap" with respect to the constraints "context"
3541 * on the domain.
3543 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3544 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3546 isl_space *space = isl_basic_map_get_space(bmap);
3547 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3549 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3550 return isl_basic_map_gist(bmap, bmap_context);
3553 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3554 __isl_take isl_set *context)
3556 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3557 map_context = isl_map_intersect_domain(map_context, context);
3558 return isl_map_gist(map, map_context);
3561 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3562 __isl_take isl_set *context)
3564 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3565 map_context = isl_map_intersect_range(map_context, context);
3566 return isl_map_gist(map, map_context);
3569 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3570 __isl_take isl_set *context)
3572 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3573 map_context = isl_map_intersect_params(map_context, context);
3574 return isl_map_gist(map, map_context);
3577 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3578 __isl_take isl_set *context)
3580 return isl_map_gist_params(set, context);
3583 /* Quick check to see if two basic maps are disjoint.
3584 * In particular, we reduce the equalities and inequalities of
3585 * one basic map in the context of the equalities of the other
3586 * basic map and check if we get a contradiction.
3588 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3589 __isl_keep isl_basic_map *bmap2)
3591 struct isl_vec *v = NULL;
3592 int *elim = NULL;
3593 unsigned total;
3594 int i;
3596 if (!bmap1 || !bmap2)
3597 return isl_bool_error;
3598 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3599 return isl_bool_error);
3600 if (bmap1->n_div || bmap2->n_div)
3601 return isl_bool_false;
3602 if (!bmap1->n_eq && !bmap2->n_eq)
3603 return isl_bool_false;
3605 total = isl_space_dim(bmap1->dim, isl_dim_all);
3606 if (total == 0)
3607 return isl_bool_false;
3608 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3609 if (!v)
3610 goto error;
3611 elim = isl_alloc_array(bmap1->ctx, int, total);
3612 if (!elim)
3613 goto error;
3614 compute_elimination_index(bmap1, elim);
3615 for (i = 0; i < bmap2->n_eq; ++i) {
3616 int reduced;
3617 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3618 bmap1, elim);
3619 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3620 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3621 goto disjoint;
3623 for (i = 0; i < bmap2->n_ineq; ++i) {
3624 int reduced;
3625 reduced = reduced_using_equalities(v->block.data,
3626 bmap2->ineq[i], bmap1, elim);
3627 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3628 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3629 goto disjoint;
3631 compute_elimination_index(bmap2, elim);
3632 for (i = 0; i < bmap1->n_ineq; ++i) {
3633 int reduced;
3634 reduced = reduced_using_equalities(v->block.data,
3635 bmap1->ineq[i], bmap2, elim);
3636 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3637 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3638 goto disjoint;
3640 isl_vec_free(v);
3641 free(elim);
3642 return isl_bool_false;
3643 disjoint:
3644 isl_vec_free(v);
3645 free(elim);
3646 return isl_bool_true;
3647 error:
3648 isl_vec_free(v);
3649 free(elim);
3650 return isl_bool_error;
3653 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3654 __isl_keep isl_basic_set *bset2)
3656 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3657 bset_to_bmap(bset2));
3660 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3662 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3663 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3664 __isl_keep isl_basic_map *bmap2))
3666 int i, j;
3668 if (!map1 || !map2)
3669 return isl_bool_error;
3671 for (i = 0; i < map1->n; ++i) {
3672 for (j = 0; j < map2->n; ++j) {
3673 isl_bool d = test(map1->p[i], map2->p[j]);
3674 if (d != isl_bool_true)
3675 return d;
3679 return isl_bool_true;
3682 /* Are "map1" and "map2" obviously disjoint, based on information
3683 * that can be derived without looking at the individual basic maps?
3685 * In particular, if one of them is empty or if they live in different spaces
3686 * (ignoring parameters), then they are clearly disjoint.
3688 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3689 __isl_keep isl_map *map2)
3691 isl_bool disjoint;
3692 isl_bool match;
3694 if (!map1 || !map2)
3695 return isl_bool_error;
3697 disjoint = isl_map_plain_is_empty(map1);
3698 if (disjoint < 0 || disjoint)
3699 return disjoint;
3701 disjoint = isl_map_plain_is_empty(map2);
3702 if (disjoint < 0 || disjoint)
3703 return disjoint;
3705 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3706 map2->dim, isl_dim_in);
3707 if (match < 0 || !match)
3708 return match < 0 ? isl_bool_error : isl_bool_true;
3710 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3711 map2->dim, isl_dim_out);
3712 if (match < 0 || !match)
3713 return match < 0 ? isl_bool_error : isl_bool_true;
3715 return isl_bool_false;
3718 /* Are "map1" and "map2" obviously disjoint?
3720 * If one of them is empty or if they live in different spaces (ignoring
3721 * parameters), then they are clearly disjoint.
3722 * This is checked by isl_map_plain_is_disjoint_global.
3724 * If they have different parameters, then we skip any further tests.
3726 * If they are obviously equal, but not obviously empty, then we will
3727 * not be able to detect if they are disjoint.
3729 * Otherwise we check if each basic map in "map1" is obviously disjoint
3730 * from each basic map in "map2".
3732 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3733 __isl_keep isl_map *map2)
3735 isl_bool disjoint;
3736 isl_bool intersect;
3737 isl_bool match;
3739 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3740 if (disjoint < 0 || disjoint)
3741 return disjoint;
3743 match = isl_map_has_equal_params(map1, map2);
3744 if (match < 0 || !match)
3745 return match < 0 ? isl_bool_error : isl_bool_false;
3747 intersect = isl_map_plain_is_equal(map1, map2);
3748 if (intersect < 0 || intersect)
3749 return intersect < 0 ? isl_bool_error : isl_bool_false;
3751 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3754 /* Are "map1" and "map2" disjoint?
3755 * The parameters are assumed to have been aligned.
3757 * In particular, check whether all pairs of basic maps are disjoint.
3759 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3760 __isl_keep isl_map *map2)
3762 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3765 /* Are "map1" and "map2" disjoint?
3767 * They are disjoint if they are "obviously disjoint" or if one of them
3768 * is empty. Otherwise, they are not disjoint if one of them is universal.
3769 * If the two inputs are (obviously) equal and not empty, then they are
3770 * not disjoint.
3771 * If none of these cases apply, then check if all pairs of basic maps
3772 * are disjoint after aligning the parameters.
3774 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3776 isl_bool disjoint;
3777 isl_bool intersect;
3779 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3780 if (disjoint < 0 || disjoint)
3781 return disjoint;
3783 disjoint = isl_map_is_empty(map1);
3784 if (disjoint < 0 || disjoint)
3785 return disjoint;
3787 disjoint = isl_map_is_empty(map2);
3788 if (disjoint < 0 || disjoint)
3789 return disjoint;
3791 intersect = isl_map_plain_is_universe(map1);
3792 if (intersect < 0 || intersect)
3793 return intersect < 0 ? isl_bool_error : isl_bool_false;
3795 intersect = isl_map_plain_is_universe(map2);
3796 if (intersect < 0 || intersect)
3797 return intersect < 0 ? isl_bool_error : isl_bool_false;
3799 intersect = isl_map_plain_is_equal(map1, map2);
3800 if (intersect < 0 || intersect)
3801 return isl_bool_not(intersect);
3803 return isl_map_align_params_map_map_and_test(map1, map2,
3804 &isl_map_is_disjoint_aligned);
3807 /* Are "bmap1" and "bmap2" disjoint?
3809 * They are disjoint if they are "obviously disjoint" or if one of them
3810 * is empty. Otherwise, they are not disjoint if one of them is universal.
3811 * If none of these cases apply, we compute the intersection and see if
3812 * the result is empty.
3814 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3815 __isl_keep isl_basic_map *bmap2)
3817 isl_bool disjoint;
3818 isl_bool intersect;
3819 isl_basic_map *test;
3821 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3822 if (disjoint < 0 || disjoint)
3823 return disjoint;
3825 disjoint = isl_basic_map_is_empty(bmap1);
3826 if (disjoint < 0 || disjoint)
3827 return disjoint;
3829 disjoint = isl_basic_map_is_empty(bmap2);
3830 if (disjoint < 0 || disjoint)
3831 return disjoint;
3833 intersect = isl_basic_map_plain_is_universe(bmap1);
3834 if (intersect < 0 || intersect)
3835 return intersect < 0 ? isl_bool_error : isl_bool_false;
3837 intersect = isl_basic_map_plain_is_universe(bmap2);
3838 if (intersect < 0 || intersect)
3839 return intersect < 0 ? isl_bool_error : isl_bool_false;
3841 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3842 isl_basic_map_copy(bmap2));
3843 disjoint = isl_basic_map_is_empty(test);
3844 isl_basic_map_free(test);
3846 return disjoint;
3849 /* Are "bset1" and "bset2" disjoint?
3851 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3852 __isl_keep isl_basic_set *bset2)
3854 return isl_basic_map_is_disjoint(bset1, bset2);
3857 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3858 __isl_keep isl_set *set2)
3860 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3863 /* Are "set1" and "set2" disjoint?
3865 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3867 return isl_map_is_disjoint(set1, set2);
3870 /* Is "v" equal to 0, 1 or -1?
3872 static int is_zero_or_one(isl_int v)
3874 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3877 /* Check if we can combine a given div with lower bound l and upper
3878 * bound u with some other div and if so return that other div.
3879 * Otherwise return -1.
3881 * We first check that
3882 * - the bounds are opposites of each other (except for the constant
3883 * term)
3884 * - the bounds do not reference any other div
3885 * - no div is defined in terms of this div
3887 * Let m be the size of the range allowed on the div by the bounds.
3888 * That is, the bounds are of the form
3890 * e <= a <= e + m - 1
3892 * with e some expression in the other variables.
3893 * We look for another div b such that no third div is defined in terms
3894 * of this second div b and such that in any constraint that contains
3895 * a (except for the given lower and upper bound), also contains b
3896 * with a coefficient that is m times that of b.
3897 * That is, all constraints (except for the lower and upper bound)
3898 * are of the form
3900 * e + f (a + m b) >= 0
3902 * Furthermore, in the constraints that only contain b, the coefficient
3903 * of b should be equal to 1 or -1.
3904 * If so, we return b so that "a + m b" can be replaced by
3905 * a single div "c = a + m b".
3907 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
3908 unsigned div, unsigned l, unsigned u)
3910 int i, j;
3911 unsigned dim;
3912 int coalesce = -1;
3914 if (bmap->n_div <= 1)
3915 return -1;
3916 dim = isl_space_dim(bmap->dim, isl_dim_all);
3917 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3918 return -1;
3919 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3920 bmap->n_div - div - 1) != -1)
3921 return -1;
3922 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3923 dim + bmap->n_div))
3924 return -1;
3926 for (i = 0; i < bmap->n_div; ++i) {
3927 if (isl_int_is_zero(bmap->div[i][0]))
3928 continue;
3929 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3930 return -1;
3933 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3934 if (isl_int_is_neg(bmap->ineq[l][0])) {
3935 isl_int_sub(bmap->ineq[l][0],
3936 bmap->ineq[l][0], bmap->ineq[u][0]);
3937 bmap = isl_basic_map_copy(bmap);
3938 bmap = isl_basic_map_set_to_empty(bmap);
3939 isl_basic_map_free(bmap);
3940 return -1;
3942 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3943 for (i = 0; i < bmap->n_div; ++i) {
3944 if (i == div)
3945 continue;
3946 if (!pairs[i])
3947 continue;
3948 for (j = 0; j < bmap->n_div; ++j) {
3949 if (isl_int_is_zero(bmap->div[j][0]))
3950 continue;
3951 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3952 break;
3954 if (j < bmap->n_div)
3955 continue;
3956 for (j = 0; j < bmap->n_ineq; ++j) {
3957 int valid;
3958 if (j == l || j == u)
3959 continue;
3960 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3961 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3962 continue;
3963 break;
3965 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3966 break;
3967 isl_int_mul(bmap->ineq[j][1 + dim + div],
3968 bmap->ineq[j][1 + dim + div],
3969 bmap->ineq[l][0]);
3970 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3971 bmap->ineq[j][1 + dim + i]);
3972 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3973 bmap->ineq[j][1 + dim + div],
3974 bmap->ineq[l][0]);
3975 if (!valid)
3976 break;
3978 if (j < bmap->n_ineq)
3979 continue;
3980 coalesce = i;
3981 break;
3983 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3984 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3985 return coalesce;
3988 /* Internal data structure used during the construction and/or evaluation of
3989 * an inequality that ensures that a pair of bounds always allows
3990 * for an integer value.
3992 * "tab" is the tableau in which the inequality is evaluated. It may
3993 * be NULL until it is actually needed.
3994 * "v" contains the inequality coefficients.
3995 * "g", "fl" and "fu" are temporary scalars used during the construction and
3996 * evaluation.
3998 struct test_ineq_data {
3999 struct isl_tab *tab;
4000 isl_vec *v;
4001 isl_int g;
4002 isl_int fl;
4003 isl_int fu;
4006 /* Free all the memory allocated by the fields of "data".
4008 static void test_ineq_data_clear(struct test_ineq_data *data)
4010 isl_tab_free(data->tab);
4011 isl_vec_free(data->v);
4012 isl_int_clear(data->g);
4013 isl_int_clear(data->fl);
4014 isl_int_clear(data->fu);
4017 /* Is the inequality stored in data->v satisfied by "bmap"?
4018 * That is, does it only attain non-negative values?
4019 * data->tab is a tableau corresponding to "bmap".
4021 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4022 struct test_ineq_data *data)
4024 isl_ctx *ctx;
4025 enum isl_lp_result res;
4027 ctx = isl_basic_map_get_ctx(bmap);
4028 if (!data->tab)
4029 data->tab = isl_tab_from_basic_map(bmap, 0);
4030 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4031 if (res == isl_lp_error)
4032 return isl_bool_error;
4033 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4036 /* Given a lower and an upper bound on div i, do they always allow
4037 * for an integer value of the given div?
4038 * Determine this property by constructing an inequality
4039 * such that the property is guaranteed when the inequality is nonnegative.
4040 * The lower bound is inequality l, while the upper bound is inequality u.
4041 * The constructed inequality is stored in data->v.
4043 * Let the upper bound be
4045 * -n_u a + e_u >= 0
4047 * and the lower bound
4049 * n_l a + e_l >= 0
4051 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4052 * We have
4054 * - f_u e_l <= f_u f_l g a <= f_l e_u
4056 * Since all variables are integer valued, this is equivalent to
4058 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4060 * If this interval is at least f_u f_l g, then it contains at least
4061 * one integer value for a.
4062 * That is, the test constraint is
4064 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4066 * or
4068 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4070 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4071 * then the constraint can be scaled down by a factor g',
4072 * with the constant term replaced by
4073 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4074 * Note that the result of applying Fourier-Motzkin to this pair
4075 * of constraints is
4077 * f_l e_u + f_u e_l >= 0
4079 * If the constant term of the scaled down version of this constraint,
4080 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4081 * term of the scaled down test constraint, then the test constraint
4082 * is known to hold and no explicit evaluation is required.
4083 * This is essentially the Omega test.
4085 * If the test constraint consists of only a constant term, then
4086 * it is sufficient to look at the sign of this constant term.
4088 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4089 int l, int u, struct test_ineq_data *data)
4091 unsigned offset, n_div;
4092 offset = isl_basic_map_offset(bmap, isl_dim_div);
4093 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4095 isl_int_gcd(data->g,
4096 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4097 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4098 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4099 isl_int_neg(data->fu, data->fu);
4100 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4101 data->fu, bmap->ineq[l], offset + n_div);
4102 isl_int_mul(data->g, data->g, data->fl);
4103 isl_int_mul(data->g, data->g, data->fu);
4104 isl_int_sub(data->g, data->g, data->fl);
4105 isl_int_sub(data->g, data->g, data->fu);
4106 isl_int_add_ui(data->g, data->g, 1);
4107 isl_int_sub(data->fl, data->v->el[0], data->g);
4109 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4110 if (isl_int_is_zero(data->g))
4111 return isl_int_is_nonneg(data->fl);
4112 if (isl_int_is_one(data->g)) {
4113 isl_int_set(data->v->el[0], data->fl);
4114 return test_ineq_is_satisfied(bmap, data);
4116 isl_int_fdiv_q(data->fl, data->fl, data->g);
4117 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4118 if (isl_int_eq(data->fl, data->v->el[0]))
4119 return isl_bool_true;
4120 isl_int_set(data->v->el[0], data->fl);
4121 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4122 offset - 1 + n_div);
4124 return test_ineq_is_satisfied(bmap, data);
4127 /* Remove more kinds of divs that are not strictly needed.
4128 * In particular, if all pairs of lower and upper bounds on a div
4129 * are such that they allow at least one integer value of the div,
4130 * then we can eliminate the div using Fourier-Motzkin without
4131 * introducing any spurious solutions.
4133 * If at least one of the two constraints has a unit coefficient for the div,
4134 * then the presence of such a value is guaranteed so there is no need to check.
4135 * In particular, the value attained by the bound with unit coefficient
4136 * can serve as this intermediate value.
4138 static __isl_give isl_basic_map *drop_more_redundant_divs(
4139 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4141 isl_ctx *ctx;
4142 struct test_ineq_data data = { NULL, NULL };
4143 unsigned off, n_div;
4144 int remove = -1;
4146 isl_int_init(data.g);
4147 isl_int_init(data.fl);
4148 isl_int_init(data.fu);
4150 if (!bmap)
4151 goto error;
4153 ctx = isl_basic_map_get_ctx(bmap);
4154 off = isl_basic_map_offset(bmap, isl_dim_div);
4155 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4156 data.v = isl_vec_alloc(ctx, off + n_div);
4157 if (!data.v)
4158 goto error;
4160 while (n > 0) {
4161 int i, l, u;
4162 int best = -1;
4163 isl_bool has_int;
4165 for (i = 0; i < n_div; ++i) {
4166 if (!pairs[i])
4167 continue;
4168 if (best >= 0 && pairs[best] <= pairs[i])
4169 continue;
4170 best = i;
4173 i = best;
4174 for (l = 0; l < bmap->n_ineq; ++l) {
4175 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4176 continue;
4177 if (isl_int_is_one(bmap->ineq[l][off + i]))
4178 continue;
4179 for (u = 0; u < bmap->n_ineq; ++u) {
4180 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4181 continue;
4182 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4183 continue;
4184 has_int = int_between_bounds(bmap, i, l, u,
4185 &data);
4186 if (has_int < 0)
4187 goto error;
4188 if (data.tab && data.tab->empty)
4189 break;
4190 if (!has_int)
4191 break;
4193 if (u < bmap->n_ineq)
4194 break;
4196 if (data.tab && data.tab->empty) {
4197 bmap = isl_basic_map_set_to_empty(bmap);
4198 break;
4200 if (l == bmap->n_ineq) {
4201 remove = i;
4202 break;
4204 pairs[i] = 0;
4205 --n;
4208 test_ineq_data_clear(&data);
4210 free(pairs);
4212 if (remove < 0)
4213 return bmap;
4215 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4216 return isl_basic_map_drop_redundant_divs(bmap);
4217 error:
4218 free(pairs);
4219 isl_basic_map_free(bmap);
4220 test_ineq_data_clear(&data);
4221 return NULL;
4224 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4225 * and the upper bound u, div1 always occurs together with div2 in the form
4226 * (div1 + m div2), where m is the constant range on the variable div1
4227 * allowed by l and u, replace the pair div1 and div2 by a single
4228 * div that is equal to div1 + m div2.
4230 * The new div will appear in the location that contains div2.
4231 * We need to modify all constraints that contain
4232 * div2 = (div - div1) / m
4233 * The coefficient of div2 is known to be equal to 1 or -1.
4234 * (If a constraint does not contain div2, it will also not contain div1.)
4235 * If the constraint also contains div1, then we know they appear
4236 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4237 * i.e., the coefficient of div is f.
4239 * Otherwise, we first need to introduce div1 into the constraint.
4240 * Let l be
4242 * div1 + f >=0
4244 * and u
4246 * -div1 + f' >= 0
4248 * A lower bound on div2
4250 * div2 + t >= 0
4252 * can be replaced by
4254 * m div2 + div1 + m t + f >= 0
4256 * An upper bound
4258 * -div2 + t >= 0
4260 * can be replaced by
4262 * -(m div2 + div1) + m t + f' >= 0
4264 * These constraint are those that we would obtain from eliminating
4265 * div1 using Fourier-Motzkin.
4267 * After all constraints have been modified, we drop the lower and upper
4268 * bound and then drop div1.
4269 * Since the new div is only placed in the same location that used
4270 * to store div2, but otherwise has a different meaning, any possible
4271 * explicit representation of the original div2 is removed.
4273 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4274 unsigned div1, unsigned div2, unsigned l, unsigned u)
4276 isl_ctx *ctx;
4277 isl_int m;
4278 unsigned dim, total;
4279 int i;
4281 ctx = isl_basic_map_get_ctx(bmap);
4283 dim = isl_space_dim(bmap->dim, isl_dim_all);
4284 total = 1 + dim + bmap->n_div;
4286 isl_int_init(m);
4287 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4288 isl_int_add_ui(m, m, 1);
4290 for (i = 0; i < bmap->n_ineq; ++i) {
4291 if (i == l || i == u)
4292 continue;
4293 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4294 continue;
4295 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4296 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4297 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4298 ctx->one, bmap->ineq[l], total);
4299 else
4300 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4301 ctx->one, bmap->ineq[u], total);
4303 isl_int_set(bmap->ineq[i][1 + dim + div2],
4304 bmap->ineq[i][1 + dim + div1]);
4305 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4308 isl_int_clear(m);
4309 if (l > u) {
4310 isl_basic_map_drop_inequality(bmap, l);
4311 isl_basic_map_drop_inequality(bmap, u);
4312 } else {
4313 isl_basic_map_drop_inequality(bmap, u);
4314 isl_basic_map_drop_inequality(bmap, l);
4316 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4317 bmap = isl_basic_map_drop_div(bmap, div1);
4318 return bmap;
4321 /* First check if we can coalesce any pair of divs and
4322 * then continue with dropping more redundant divs.
4324 * We loop over all pairs of lower and upper bounds on a div
4325 * with coefficient 1 and -1, respectively, check if there
4326 * is any other div "c" with which we can coalesce the div
4327 * and if so, perform the coalescing.
4329 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4330 __isl_take isl_basic_map *bmap, int *pairs, int n)
4332 int i, l, u;
4333 unsigned dim;
4335 dim = isl_space_dim(bmap->dim, isl_dim_all);
4337 for (i = 0; i < bmap->n_div; ++i) {
4338 if (!pairs[i])
4339 continue;
4340 for (l = 0; l < bmap->n_ineq; ++l) {
4341 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4342 continue;
4343 for (u = 0; u < bmap->n_ineq; ++u) {
4344 int c;
4346 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4347 continue;
4348 c = div_find_coalesce(bmap, pairs, i, l, u);
4349 if (c < 0)
4350 continue;
4351 free(pairs);
4352 bmap = coalesce_divs(bmap, i, c, l, u);
4353 return isl_basic_map_drop_redundant_divs(bmap);
4358 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4359 free(pairs);
4360 return bmap;
4363 return drop_more_redundant_divs(bmap, pairs, n);
4366 /* Are the "n" coefficients starting at "first" of inequality constraints
4367 * "i" and "j" of "bmap" equal to each other?
4369 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4370 int first, int n)
4372 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4375 /* Are the "n" coefficients starting at "first" of inequality constraints
4376 * "i" and "j" of "bmap" opposite to each other?
4378 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4379 int first, int n)
4381 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4384 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4385 * apart from the constant term?
4387 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4389 unsigned total;
4391 total = isl_basic_map_dim(bmap, isl_dim_all);
4392 return is_opposite_part(bmap, i, j, 1, total);
4395 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4396 * apart from the constant term and the coefficient at position "pos"?
4398 static int is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4399 int pos)
4401 unsigned total;
4403 total = isl_basic_map_dim(bmap, isl_dim_all);
4404 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4405 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4408 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4409 * apart from the constant term and the coefficient at position "pos"?
4411 static int is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4412 int pos)
4414 unsigned total;
4416 total = isl_basic_map_dim(bmap, isl_dim_all);
4417 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4418 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4421 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4422 * been modified, simplying it if "simplify" is set.
4423 * Free the temporary data structure "pairs" that was associated
4424 * to the old version of "bmap".
4426 static __isl_give isl_basic_map *drop_redundant_divs_again(
4427 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4429 if (simplify)
4430 bmap = isl_basic_map_simplify(bmap);
4431 free(pairs);
4432 return isl_basic_map_drop_redundant_divs(bmap);
4435 /* Is "div" the single unknown existentially quantified variable
4436 * in inequality constraint "ineq" of "bmap"?
4437 * "div" is known to have a non-zero coefficient in "ineq".
4439 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4440 int div)
4442 int i;
4443 unsigned n_div, o_div;
4444 isl_bool known;
4446 known = isl_basic_map_div_is_known(bmap, div);
4447 if (known < 0 || known)
4448 return isl_bool_not(known);
4449 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4450 if (n_div == 1)
4451 return isl_bool_true;
4452 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4453 for (i = 0; i < n_div; ++i) {
4454 isl_bool known;
4456 if (i == div)
4457 continue;
4458 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4459 continue;
4460 known = isl_basic_map_div_is_known(bmap, i);
4461 if (known < 0 || !known)
4462 return known;
4465 return isl_bool_true;
4468 /* Does integer division "div" have coefficient 1 in inequality constraint
4469 * "ineq" of "map"?
4471 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4473 unsigned o_div;
4475 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4476 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4477 return isl_bool_true;
4479 return isl_bool_false;
4482 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4483 * then try and drop redundant divs again,
4484 * freeing the temporary data structure "pairs" that was associated
4485 * to the old version of "bmap".
4487 static __isl_give isl_basic_map *set_eq_and_try_again(
4488 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4490 bmap = isl_basic_map_cow(bmap);
4491 isl_basic_map_inequality_to_equality(bmap, ineq);
4492 return drop_redundant_divs_again(bmap, pairs, 1);
4495 /* Drop the integer division at position "div", along with the two
4496 * inequality constraints "ineq1" and "ineq2" in which it appears
4497 * from "bmap" and then try and drop redundant divs again,
4498 * freeing the temporary data structure "pairs" that was associated
4499 * to the old version of "bmap".
4501 static __isl_give isl_basic_map *drop_div_and_try_again(
4502 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4503 __isl_take int *pairs)
4505 if (ineq1 > ineq2) {
4506 isl_basic_map_drop_inequality(bmap, ineq1);
4507 isl_basic_map_drop_inequality(bmap, ineq2);
4508 } else {
4509 isl_basic_map_drop_inequality(bmap, ineq2);
4510 isl_basic_map_drop_inequality(bmap, ineq1);
4512 bmap = isl_basic_map_drop_div(bmap, div);
4513 return drop_redundant_divs_again(bmap, pairs, 0);
4516 /* Given two inequality constraints
4518 * f(x) + n d + c >= 0, (ineq)
4520 * with d the variable at position "pos", and
4522 * f(x) + c0 >= 0, (lower)
4524 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4525 * determined by the first constraint.
4526 * That is, store
4528 * ceil((c0 - c)/n)
4530 * in *l.
4532 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4533 int ineq, int lower, int pos, isl_int *l)
4535 isl_int_neg(*l, bmap->ineq[ineq][0]);
4536 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4537 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4540 /* Given two inequality constraints
4542 * f(x) + n d + c >= 0, (ineq)
4544 * with d the variable at position "pos", and
4546 * -f(x) - c0 >= 0, (upper)
4548 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4549 * determined by the first constraint.
4550 * That is, store
4552 * ceil((-c1 - c)/n)
4554 * in *u.
4556 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4557 int ineq, int upper, int pos, isl_int *u)
4559 isl_int_neg(*u, bmap->ineq[ineq][0]);
4560 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4561 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4564 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4565 * does the corresponding lower bound have a fixed value in "bmap"?
4567 * In particular, "ineq" is of the form
4569 * f(x) + n d + c >= 0
4571 * with n > 0, c the constant term and
4572 * d the existentially quantified variable "div".
4573 * That is, the lower bound is
4575 * ceil((-f(x) - c)/n)
4577 * Look for a pair of constraints
4579 * f(x) + c0 >= 0
4580 * -f(x) + c1 >= 0
4582 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4583 * That is, check that
4585 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4587 * If so, return the index of inequality f(x) + c0 >= 0.
4588 * Otherwise, return -1.
4590 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4592 int i;
4593 int lower = -1, upper = -1;
4594 unsigned o_div;
4595 isl_int l, u;
4596 int equal;
4598 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4599 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4600 if (i == ineq)
4601 continue;
4602 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4603 continue;
4604 if (lower < 0 &&
4605 is_parallel_except(bmap, ineq, i, o_div + div)) {
4606 lower = i;
4607 continue;
4609 if (upper < 0 &&
4610 is_opposite_except(bmap, ineq, i, o_div + div)) {
4611 upper = i;
4615 if (lower < 0 || upper < 0)
4616 return -1;
4618 isl_int_init(l);
4619 isl_int_init(u);
4621 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4622 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4624 equal = isl_int_eq(l, u);
4626 isl_int_clear(l);
4627 isl_int_clear(u);
4629 return equal ? lower : -1;
4632 /* Given a lower bound constraint "ineq" on the existentially quantified
4633 * variable "div", such that the corresponding lower bound has
4634 * a fixed value in "bmap", assign this fixed value to the variable and
4635 * then try and drop redundant divs again,
4636 * freeing the temporary data structure "pairs" that was associated
4637 * to the old version of "bmap".
4638 * "lower" determines the constant value for the lower bound.
4640 * In particular, "ineq" is of the form
4642 * f(x) + n d + c >= 0,
4644 * while "lower" is of the form
4646 * f(x) + c0 >= 0
4648 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4649 * is ceil((c0 - c)/n).
4651 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4652 int div, int ineq, int lower, int *pairs)
4654 isl_int c;
4655 unsigned o_div;
4657 isl_int_init(c);
4659 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4660 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4661 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4662 free(pairs);
4664 isl_int_clear(c);
4666 return isl_basic_map_drop_redundant_divs(bmap);
4669 /* Remove divs that are not strictly needed based on the inequality
4670 * constraints.
4671 * In particular, if a div only occurs positively (or negatively)
4672 * in constraints, then it can simply be dropped.
4673 * Also, if a div occurs in only two constraints and if moreover
4674 * those two constraints are opposite to each other, except for the constant
4675 * term and if the sum of the constant terms is such that for any value
4676 * of the other values, there is always at least one integer value of the
4677 * div, i.e., if one plus this sum is greater than or equal to
4678 * the (absolute value) of the coefficient of the div in the constraints,
4679 * then we can also simply drop the div.
4681 * If an existentially quantified variable does not have an explicit
4682 * representation, appears in only a single lower bound that does not
4683 * involve any other such existentially quantified variables and appears
4684 * in this lower bound with coefficient 1,
4685 * then fix the variable to the value of the lower bound. That is,
4686 * turn the inequality into an equality.
4687 * If for any value of the other variables, there is any value
4688 * for the existentially quantified variable satisfying the constraints,
4689 * then this lower bound also satisfies the constraints.
4690 * It is therefore safe to pick this lower bound.
4692 * The same reasoning holds even if the coefficient is not one.
4693 * However, fixing the variable to the value of the lower bound may
4694 * in general introduce an extra integer division, in which case
4695 * it may be better to pick another value.
4696 * If this integer division has a known constant value, then plugging
4697 * in this constant value removes the existentially quantified variable
4698 * completely. In particular, if the lower bound is of the form
4699 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4700 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4701 * then the existentially quantified variable can be assigned this
4702 * shared value.
4704 * We skip divs that appear in equalities or in the definition of other divs.
4705 * Divs that appear in the definition of other divs usually occur in at least
4706 * 4 constraints, but the constraints may have been simplified.
4708 * If any divs are left after these simple checks then we move on
4709 * to more complicated cases in drop_more_redundant_divs.
4711 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4712 __isl_take isl_basic_map *bmap)
4714 int i, j;
4715 unsigned off;
4716 int *pairs = NULL;
4717 int n = 0;
4719 if (!bmap)
4720 goto error;
4721 if (bmap->n_div == 0)
4722 return bmap;
4724 off = isl_space_dim(bmap->dim, isl_dim_all);
4725 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4726 if (!pairs)
4727 goto error;
4729 for (i = 0; i < bmap->n_div; ++i) {
4730 int pos, neg;
4731 int last_pos, last_neg;
4732 int redundant;
4733 int defined;
4734 isl_bool opp, set_div;
4736 defined = !isl_int_is_zero(bmap->div[i][0]);
4737 for (j = i; j < bmap->n_div; ++j)
4738 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4739 break;
4740 if (j < bmap->n_div)
4741 continue;
4742 for (j = 0; j < bmap->n_eq; ++j)
4743 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4744 break;
4745 if (j < bmap->n_eq)
4746 continue;
4747 ++n;
4748 pos = neg = 0;
4749 for (j = 0; j < bmap->n_ineq; ++j) {
4750 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4751 last_pos = j;
4752 ++pos;
4754 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4755 last_neg = j;
4756 ++neg;
4759 pairs[i] = pos * neg;
4760 if (pairs[i] == 0) {
4761 for (j = bmap->n_ineq - 1; j >= 0; --j)
4762 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4763 isl_basic_map_drop_inequality(bmap, j);
4764 bmap = isl_basic_map_drop_div(bmap, i);
4765 return drop_redundant_divs_again(bmap, pairs, 0);
4767 if (pairs[i] != 1)
4768 opp = isl_bool_false;
4769 else
4770 opp = is_opposite(bmap, last_pos, last_neg);
4771 if (opp < 0)
4772 goto error;
4773 if (!opp) {
4774 int lower;
4775 isl_bool single, one;
4777 if (pos != 1)
4778 continue;
4779 single = single_unknown(bmap, last_pos, i);
4780 if (single < 0)
4781 goto error;
4782 if (!single)
4783 continue;
4784 one = has_coef_one(bmap, i, last_pos);
4785 if (one < 0)
4786 goto error;
4787 if (one)
4788 return set_eq_and_try_again(bmap, last_pos,
4789 pairs);
4790 lower = lower_bound_is_cst(bmap, i, last_pos);
4791 if (lower >= 0)
4792 return fix_cst_lower(bmap, i, last_pos, lower,
4793 pairs);
4794 continue;
4797 isl_int_add(bmap->ineq[last_pos][0],
4798 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4799 isl_int_add_ui(bmap->ineq[last_pos][0],
4800 bmap->ineq[last_pos][0], 1);
4801 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4802 bmap->ineq[last_pos][1+off+i]);
4803 isl_int_sub_ui(bmap->ineq[last_pos][0],
4804 bmap->ineq[last_pos][0], 1);
4805 isl_int_sub(bmap->ineq[last_pos][0],
4806 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4807 if (redundant)
4808 return drop_div_and_try_again(bmap, i,
4809 last_pos, last_neg, pairs);
4810 if (defined)
4811 set_div = isl_bool_false;
4812 else
4813 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4814 if (set_div < 0)
4815 return isl_basic_map_free(bmap);
4816 if (set_div) {
4817 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4818 return drop_redundant_divs_again(bmap, pairs, 1);
4820 pairs[i] = 0;
4821 --n;
4824 if (n > 0)
4825 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4827 free(pairs);
4828 return bmap;
4829 error:
4830 free(pairs);
4831 isl_basic_map_free(bmap);
4832 return NULL;
4835 /* Consider the coefficients at "c" as a row vector and replace
4836 * them with their product with "T". "T" is assumed to be a square matrix.
4838 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4840 int n;
4841 isl_ctx *ctx;
4842 isl_vec *v;
4844 if (!T)
4845 return isl_stat_error;
4846 n = isl_mat_rows(T);
4847 if (isl_seq_first_non_zero(c, n) == -1)
4848 return isl_stat_ok;
4849 ctx = isl_mat_get_ctx(T);
4850 v = isl_vec_alloc(ctx, n);
4851 if (!v)
4852 return isl_stat_error;
4853 isl_seq_swp_or_cpy(v->el, c, n);
4854 v = isl_vec_mat_product(v, isl_mat_copy(T));
4855 if (!v)
4856 return isl_stat_error;
4857 isl_seq_swp_or_cpy(c, v->el, n);
4858 isl_vec_free(v);
4860 return isl_stat_ok;
4863 /* Plug in T for the variables in "bmap" starting at "pos".
4864 * T is a linear unimodular matrix, i.e., without constant term.
4866 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4867 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4869 int i;
4870 unsigned n;
4872 bmap = isl_basic_map_cow(bmap);
4873 if (!bmap || !T)
4874 goto error;
4876 n = isl_mat_cols(T);
4877 if (n != isl_mat_rows(T))
4878 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4879 "expecting square matrix", goto error);
4881 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n) < 0)
4882 goto error;
4884 for (i = 0; i < bmap->n_eq; ++i)
4885 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4886 goto error;
4887 for (i = 0; i < bmap->n_ineq; ++i)
4888 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4889 goto error;
4890 for (i = 0; i < bmap->n_div; ++i) {
4891 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4892 continue;
4893 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4894 goto error;
4897 isl_mat_free(T);
4898 return bmap;
4899 error:
4900 isl_basic_map_free(bmap);
4901 isl_mat_free(T);
4902 return NULL;
4905 /* Remove divs that are not strictly needed.
4907 * First look for an equality constraint involving two or more
4908 * existentially quantified variables without an explicit
4909 * representation. Replace the combination that appears
4910 * in the equality constraint by a single existentially quantified
4911 * variable such that the equality can be used to derive
4912 * an explicit representation for the variable.
4913 * If there are no more such equality constraints, then continue
4914 * with isl_basic_map_drop_redundant_divs_ineq.
4916 * In particular, if the equality constraint is of the form
4918 * f(x) + \sum_i c_i a_i = 0
4920 * with a_i existentially quantified variable without explicit
4921 * representation, then apply a transformation on the existentially
4922 * quantified variables to turn the constraint into
4924 * f(x) + g a_1' = 0
4926 * with g the gcd of the c_i.
4927 * In order to easily identify which existentially quantified variables
4928 * have a complete explicit representation, i.e., without being defined
4929 * in terms of other existentially quantified variables without
4930 * an explicit representation, the existentially quantified variables
4931 * are first sorted.
4933 * The variable transformation is computed by extending the row
4934 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4936 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4937 * [a_2'] [ a_2 ]
4938 * ... = U ....
4939 * [a_n'] [ a_n ]
4941 * with [c_1/g ... c_n/g] representing the first row of U.
4942 * The inverse of U is then plugged into the original constraints.
4943 * The call to isl_basic_map_simplify makes sure the explicit
4944 * representation for a_1' is extracted from the equality constraint.
4946 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4947 __isl_take isl_basic_map *bmap)
4949 int first;
4950 int i;
4951 unsigned o_div, n_div;
4952 int l;
4953 isl_ctx *ctx;
4954 isl_mat *T;
4956 if (!bmap)
4957 return NULL;
4958 if (isl_basic_map_divs_known(bmap))
4959 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4960 if (bmap->n_eq == 0)
4961 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4962 bmap = isl_basic_map_sort_divs(bmap);
4963 if (!bmap)
4964 return NULL;
4966 first = isl_basic_map_first_unknown_div(bmap);
4967 if (first < 0)
4968 return isl_basic_map_free(bmap);
4970 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4971 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4973 for (i = 0; i < bmap->n_eq; ++i) {
4974 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4975 n_div - (first));
4976 if (l < 0)
4977 continue;
4978 l += first;
4979 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4980 n_div - (l + 1)) == -1)
4981 continue;
4982 break;
4984 if (i >= bmap->n_eq)
4985 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4987 ctx = isl_basic_map_get_ctx(bmap);
4988 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
4989 if (!T)
4990 return isl_basic_map_free(bmap);
4991 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
4992 T = isl_mat_normalize_row(T, 0);
4993 T = isl_mat_unimodular_complete(T, 1);
4994 T = isl_mat_right_inverse(T);
4996 for (i = l; i < n_div; ++i)
4997 bmap = isl_basic_map_mark_div_unknown(bmap, i);
4998 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
4999 bmap = isl_basic_map_simplify(bmap);
5001 return isl_basic_map_drop_redundant_divs(bmap);
5004 /* Does "bmap" satisfy any equality that involves more than 2 variables
5005 * and/or has coefficients different from -1 and 1?
5007 static int has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5009 int i;
5010 unsigned total;
5012 total = isl_basic_map_dim(bmap, isl_dim_all);
5014 for (i = 0; i < bmap->n_eq; ++i) {
5015 int j, k;
5017 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5018 if (j < 0)
5019 continue;
5020 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5021 !isl_int_is_negone(bmap->eq[i][1 + j]))
5022 return 1;
5024 j += 1;
5025 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5026 if (k < 0)
5027 continue;
5028 j += k;
5029 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5030 !isl_int_is_negone(bmap->eq[i][1 + j]))
5031 return 1;
5033 j += 1;
5034 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5035 if (k >= 0)
5036 return 1;
5039 return 0;
5042 /* Remove any common factor g from the constraint coefficients in "v".
5043 * The constant term is stored in the first position and is replaced
5044 * by floor(c/g). If any common factor is removed and if this results
5045 * in a tightening of the constraint, then set *tightened.
5047 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5048 int *tightened)
5050 isl_ctx *ctx;
5052 if (!v)
5053 return NULL;
5054 ctx = isl_vec_get_ctx(v);
5055 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5056 if (isl_int_is_zero(ctx->normalize_gcd))
5057 return v;
5058 if (isl_int_is_one(ctx->normalize_gcd))
5059 return v;
5060 v = isl_vec_cow(v);
5061 if (!v)
5062 return NULL;
5063 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5064 *tightened = 1;
5065 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5066 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5067 v->size - 1);
5068 return v;
5071 /* If "bmap" is an integer set that satisfies any equality involving
5072 * more than 2 variables and/or has coefficients different from -1 and 1,
5073 * then use variable compression to reduce the coefficients by removing
5074 * any (hidden) common factor.
5075 * In particular, apply the variable compression to each constraint,
5076 * factor out any common factor in the non-constant coefficients and
5077 * then apply the inverse of the compression.
5078 * At the end, we mark the basic map as having reduced constants.
5079 * If this flag is still set on the next invocation of this function,
5080 * then we skip the computation.
5082 * Removing a common factor may result in a tightening of some of
5083 * the constraints. If this happens, then we may end up with two
5084 * opposite inequalities that can be replaced by an equality.
5085 * We therefore call isl_basic_map_detect_inequality_pairs,
5086 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5087 * and isl_basic_map_gauss if such a pair was found.
5089 * Note that this function may leave the result in an inconsistent state.
5090 * In particular, the constraints may not be gaussed.
5091 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5092 * for some of the test cases to pass successfully.
5093 * Any potential modification of the representation is therefore only
5094 * performed on a single copy of the basic map.
5096 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5097 __isl_take isl_basic_map *bmap)
5099 unsigned total;
5100 isl_ctx *ctx;
5101 isl_vec *v;
5102 isl_mat *eq, *T, *T2;
5103 int i;
5104 int tightened;
5106 if (!bmap)
5107 return NULL;
5108 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5109 return bmap;
5110 if (isl_basic_map_is_rational(bmap))
5111 return bmap;
5112 if (bmap->n_eq == 0)
5113 return bmap;
5114 if (!has_multiple_var_equality(bmap))
5115 return bmap;
5117 total = isl_basic_map_dim(bmap, isl_dim_all);
5118 ctx = isl_basic_map_get_ctx(bmap);
5119 v = isl_vec_alloc(ctx, 1 + total);
5120 if (!v)
5121 return isl_basic_map_free(bmap);
5123 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5124 T = isl_mat_variable_compression(eq, &T2);
5125 if (!T || !T2)
5126 goto error;
5127 if (T->n_col == 0) {
5128 isl_mat_free(T);
5129 isl_mat_free(T2);
5130 isl_vec_free(v);
5131 return isl_basic_map_set_to_empty(bmap);
5134 bmap = isl_basic_map_cow(bmap);
5135 if (!bmap)
5136 goto error;
5138 tightened = 0;
5139 for (i = 0; i < bmap->n_ineq; ++i) {
5140 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5141 v = isl_vec_mat_product(v, isl_mat_copy(T));
5142 v = normalize_constraint(v, &tightened);
5143 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5144 if (!v)
5145 goto error;
5146 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5149 isl_mat_free(T);
5150 isl_mat_free(T2);
5151 isl_vec_free(v);
5153 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5155 if (tightened) {
5156 int progress = 0;
5158 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5159 if (progress) {
5160 bmap = eliminate_divs_eq(bmap, &progress);
5161 bmap = isl_basic_map_gauss(bmap, NULL);
5165 return bmap;
5166 error:
5167 isl_mat_free(T);
5168 isl_mat_free(T2);
5169 isl_vec_free(v);
5170 return isl_basic_map_free(bmap);
5173 /* Shift the integer division at position "div" of "bmap"
5174 * by "shift" times the variable at position "pos".
5175 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5176 * corresponds to the constant term.
5178 * That is, if the integer division has the form
5180 * floor(f(x)/d)
5182 * then replace it by
5184 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5186 __isl_give isl_basic_map *isl_basic_map_shift_div(
5187 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5189 int i;
5190 unsigned total;
5192 if (isl_int_is_zero(shift))
5193 return bmap;
5194 if (!bmap)
5195 return NULL;
5197 total = isl_basic_map_dim(bmap, isl_dim_all);
5198 total -= isl_basic_map_dim(bmap, isl_dim_div);
5200 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5202 for (i = 0; i < bmap->n_eq; ++i) {
5203 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5204 continue;
5205 isl_int_submul(bmap->eq[i][pos],
5206 shift, bmap->eq[i][1 + total + div]);
5208 for (i = 0; i < bmap->n_ineq; ++i) {
5209 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5210 continue;
5211 isl_int_submul(bmap->ineq[i][pos],
5212 shift, bmap->ineq[i][1 + total + div]);
5214 for (i = 0; i < bmap->n_div; ++i) {
5215 if (isl_int_is_zero(bmap->div[i][0]))
5216 continue;
5217 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5218 continue;
5219 isl_int_submul(bmap->div[i][1 + pos],
5220 shift, bmap->div[i][1 + 1 + total + div]);
5223 return bmap;