isl_map_simplify.c: has_multiple_var_equality: return isl_bool
[isl.git] / isl_map_simplify.c
blobff73325b13122123154decd952bc856cd4e8dcde
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 bmap = isl_basic_map_add_div_constraint(bmap, i, s);
1339 if (!bmap)
1340 return NULL;
1344 return bmap;
1347 __isl_give isl_basic_map *isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
1349 int progress = 1;
1350 if (!bmap)
1351 return NULL;
1352 while (progress) {
1353 isl_bool empty;
1355 progress = 0;
1356 empty = isl_basic_map_plain_is_empty(bmap);
1357 if (empty < 0)
1358 return isl_basic_map_free(bmap);
1359 if (empty)
1360 break;
1361 bmap = isl_basic_map_normalize_constraints(bmap);
1362 bmap = reduce_div_coefficients(bmap);
1363 bmap = normalize_div_expressions(bmap);
1364 bmap = remove_duplicate_divs(bmap, &progress);
1365 bmap = eliminate_unit_divs(bmap, &progress);
1366 bmap = eliminate_divs_eq(bmap, &progress);
1367 bmap = eliminate_divs_ineq(bmap, &progress);
1368 bmap = isl_basic_map_gauss(bmap, &progress);
1369 /* requires equalities in normal form */
1370 bmap = normalize_divs(bmap, &progress);
1371 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1372 &progress, 1);
1373 if (bmap && progress)
1374 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1376 return bmap;
1379 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1381 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset)));
1385 isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap,
1386 isl_int *constraint, unsigned div)
1388 unsigned pos;
1390 if (!bmap)
1391 return isl_bool_error;
1393 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1395 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
1396 int neg;
1397 isl_int_sub(bmap->div[div][1],
1398 bmap->div[div][1], bmap->div[div][0]);
1399 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
1400 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
1401 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1402 isl_int_add(bmap->div[div][1],
1403 bmap->div[div][1], bmap->div[div][0]);
1404 if (!neg)
1405 return isl_bool_false;
1406 if (isl_seq_first_non_zero(constraint+pos+1,
1407 bmap->n_div-div-1) != -1)
1408 return isl_bool_false;
1409 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
1410 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
1411 return isl_bool_false;
1412 if (isl_seq_first_non_zero(constraint+pos+1,
1413 bmap->n_div-div-1) != -1)
1414 return isl_bool_false;
1415 } else
1416 return isl_bool_false;
1418 return isl_bool_true;
1421 isl_bool isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
1422 isl_int *constraint, unsigned div)
1424 return isl_basic_map_is_div_constraint(bset, constraint, div);
1428 /* If the only constraints a div d=floor(f/m)
1429 * appears in are its two defining constraints
1431 * f - m d >=0
1432 * -(f - (m - 1)) + m d >= 0
1434 * then it can safely be removed.
1436 static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
1438 int i;
1439 unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
1441 for (i = 0; i < bmap->n_eq; ++i)
1442 if (!isl_int_is_zero(bmap->eq[i][pos]))
1443 return isl_bool_false;
1445 for (i = 0; i < bmap->n_ineq; ++i) {
1446 isl_bool red;
1448 if (isl_int_is_zero(bmap->ineq[i][pos]))
1449 continue;
1450 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
1451 if (red < 0 || !red)
1452 return red;
1455 for (i = 0; i < bmap->n_div; ++i) {
1456 if (isl_int_is_zero(bmap->div[i][0]))
1457 continue;
1458 if (!isl_int_is_zero(bmap->div[i][1+pos]))
1459 return isl_bool_false;
1462 return isl_bool_true;
1466 * Remove divs that don't occur in any of the constraints or other divs.
1467 * These can arise when dropping constraints from a basic map or
1468 * when the divs of a basic map have been temporarily aligned
1469 * with the divs of another basic map.
1471 static __isl_give isl_basic_map *remove_redundant_divs(
1472 __isl_take isl_basic_map *bmap)
1474 int i;
1475 int v_div;
1477 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
1478 if (v_div < 0)
1479 return isl_basic_map_free(bmap);
1481 for (i = bmap->n_div-1; i >= 0; --i) {
1482 isl_bool redundant;
1484 redundant = div_is_redundant(bmap, i);
1485 if (redundant < 0)
1486 return isl_basic_map_free(bmap);
1487 if (!redundant)
1488 continue;
1489 bmap = isl_basic_map_drop_constraints_involving(bmap,
1490 v_div + i, 1);
1491 bmap = isl_basic_map_drop_div(bmap, i);
1493 return bmap;
1496 /* Mark "bmap" as final, without checking for obviously redundant
1497 * integer divisions. This function should be used when "bmap"
1498 * is known not to involve any such integer divisions.
1500 __isl_give isl_basic_map *isl_basic_map_mark_final(
1501 __isl_take isl_basic_map *bmap)
1503 if (!bmap)
1504 return NULL;
1505 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1506 return bmap;
1509 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1511 __isl_give isl_basic_map *isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
1513 bmap = remove_redundant_divs(bmap);
1514 bmap = isl_basic_map_mark_final(bmap);
1515 return bmap;
1518 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
1520 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset)));
1523 /* Remove definition of any div that is defined in terms of the given variable.
1524 * The div itself is not removed. Functions such as
1525 * eliminate_divs_ineq depend on the other divs remaining in place.
1527 static __isl_give isl_basic_map *remove_dependent_vars(
1528 __isl_take isl_basic_map *bmap, int pos)
1530 int i;
1532 if (!bmap)
1533 return NULL;
1535 for (i = 0; i < bmap->n_div; ++i) {
1536 if (isl_int_is_zero(bmap->div[i][0]))
1537 continue;
1538 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
1539 continue;
1540 bmap = isl_basic_map_mark_div_unknown(bmap, i);
1541 if (!bmap)
1542 return NULL;
1544 return bmap;
1547 /* Eliminate the specified variables from the constraints using
1548 * Fourier-Motzkin. The variables themselves are not removed.
1550 __isl_give isl_basic_map *isl_basic_map_eliminate_vars(
1551 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
1553 int d;
1554 int i, j, k;
1555 unsigned total;
1556 int need_gauss = 0;
1558 if (n == 0)
1559 return bmap;
1560 if (!bmap)
1561 return NULL;
1562 total = isl_basic_map_total_dim(bmap);
1564 bmap = isl_basic_map_cow(bmap);
1565 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
1566 bmap = remove_dependent_vars(bmap, d);
1567 if (!bmap)
1568 return NULL;
1570 for (d = pos + n - 1;
1571 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
1572 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
1573 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
1574 int n_lower, n_upper;
1575 if (!bmap)
1576 return NULL;
1577 for (i = 0; i < bmap->n_eq; ++i) {
1578 if (isl_int_is_zero(bmap->eq[i][1+d]))
1579 continue;
1580 bmap = eliminate_var_using_equality(bmap, d,
1581 bmap->eq[i], 0, NULL);
1582 if (isl_basic_map_drop_equality(bmap, i) < 0)
1583 return isl_basic_map_free(bmap);
1584 need_gauss = 1;
1585 break;
1587 if (i < bmap->n_eq)
1588 continue;
1589 n_lower = 0;
1590 n_upper = 0;
1591 for (i = 0; i < bmap->n_ineq; ++i) {
1592 if (isl_int_is_pos(bmap->ineq[i][1+d]))
1593 n_lower++;
1594 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1595 n_upper++;
1597 bmap = isl_basic_map_extend_constraints(bmap,
1598 0, n_lower * n_upper);
1599 if (!bmap)
1600 goto error;
1601 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1602 int last;
1603 if (isl_int_is_zero(bmap->ineq[i][1+d]))
1604 continue;
1605 last = -1;
1606 for (j = 0; j < i; ++j) {
1607 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1608 continue;
1609 last = j;
1610 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1611 isl_int_sgn(bmap->ineq[j][1+d]))
1612 continue;
1613 k = isl_basic_map_alloc_inequality(bmap);
1614 if (k < 0)
1615 goto error;
1616 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1617 1+total);
1618 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1619 1+d, 1+total, NULL);
1621 isl_basic_map_drop_inequality(bmap, i);
1622 i = last + 1;
1624 if (n_lower > 0 && n_upper > 0) {
1625 bmap = isl_basic_map_normalize_constraints(bmap);
1626 bmap = isl_basic_map_remove_duplicate_constraints(bmap,
1627 NULL, 0);
1628 bmap = isl_basic_map_gauss(bmap, NULL);
1629 bmap = isl_basic_map_remove_redundancies(bmap);
1630 need_gauss = 0;
1631 if (!bmap)
1632 goto error;
1633 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1634 break;
1637 if (need_gauss)
1638 bmap = isl_basic_map_gauss(bmap, NULL);
1639 return bmap;
1640 error:
1641 isl_basic_map_free(bmap);
1642 return NULL;
1645 struct isl_basic_set *isl_basic_set_eliminate_vars(
1646 struct isl_basic_set *bset, unsigned pos, unsigned n)
1648 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset),
1649 pos, n));
1652 /* Eliminate the specified n dimensions starting at first from the
1653 * constraints, without removing the dimensions from the space.
1654 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1655 * Otherwise, they are projected out and the original space is restored.
1657 __isl_give isl_basic_map *isl_basic_map_eliminate(
1658 __isl_take isl_basic_map *bmap,
1659 enum isl_dim_type type, unsigned first, unsigned n)
1661 isl_space *space;
1663 if (!bmap)
1664 return NULL;
1665 if (n == 0)
1666 return bmap;
1668 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
1669 return isl_basic_map_free(bmap);
1671 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
1672 first += isl_basic_map_offset(bmap, type) - 1;
1673 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
1674 return isl_basic_map_finalize(bmap);
1677 space = isl_basic_map_get_space(bmap);
1678 bmap = isl_basic_map_project_out(bmap, type, first, n);
1679 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
1680 bmap = isl_basic_map_reset_space(bmap, space);
1681 return bmap;
1684 __isl_give isl_basic_set *isl_basic_set_eliminate(
1685 __isl_take isl_basic_set *bset,
1686 enum isl_dim_type type, unsigned first, unsigned n)
1688 return isl_basic_map_eliminate(bset, type, first, n);
1691 /* Remove all constraints from "bmap" that reference any unknown local
1692 * variables (directly or indirectly).
1694 * Dropping all constraints on a local variable will make it redundant,
1695 * so it will get removed implicitly by
1696 * isl_basic_map_drop_constraints_involving_dims. Some other local
1697 * variables may also end up becoming redundant if they only appear
1698 * in constraints together with the unknown local variable.
1699 * Therefore, start over after calling
1700 * isl_basic_map_drop_constraints_involving_dims.
1702 __isl_give isl_basic_map *isl_basic_map_drop_constraint_involving_unknown_divs(
1703 __isl_take isl_basic_map *bmap)
1705 isl_bool known;
1706 int i, n_div, o_div;
1708 known = isl_basic_map_divs_known(bmap);
1709 if (known < 0)
1710 return isl_basic_map_free(bmap);
1711 if (known)
1712 return bmap;
1714 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1715 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
1717 for (i = 0; i < n_div; ++i) {
1718 known = isl_basic_map_div_is_known(bmap, i);
1719 if (known < 0)
1720 return isl_basic_map_free(bmap);
1721 if (known)
1722 continue;
1723 bmap = remove_dependent_vars(bmap, o_div + i);
1724 bmap = isl_basic_map_drop_constraints_involving_dims(bmap,
1725 isl_dim_div, i, 1);
1726 if (!bmap)
1727 return NULL;
1728 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1729 i = -1;
1732 return bmap;
1735 /* Remove all constraints from "map" that reference any unknown local
1736 * variables (directly or indirectly).
1738 * Since constraints may get dropped from the basic maps,
1739 * they may no longer be disjoint from each other.
1741 __isl_give isl_map *isl_map_drop_constraint_involving_unknown_divs(
1742 __isl_take isl_map *map)
1744 int i;
1745 isl_bool known;
1747 known = isl_map_divs_known(map);
1748 if (known < 0)
1749 return isl_map_free(map);
1750 if (known)
1751 return map;
1753 map = isl_map_cow(map);
1754 if (!map)
1755 return NULL;
1757 for (i = 0; i < map->n; ++i) {
1758 map->p[i] =
1759 isl_basic_map_drop_constraint_involving_unknown_divs(
1760 map->p[i]);
1761 if (!map->p[i])
1762 return isl_map_free(map);
1765 if (map->n > 1)
1766 ISL_F_CLR(map, ISL_MAP_DISJOINT);
1768 return map;
1771 /* Don't assume equalities are in order, because align_divs
1772 * may have changed the order of the divs.
1774 static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim)
1776 int d, i;
1777 unsigned total;
1779 total = isl_space_dim(bmap->dim, isl_dim_all);
1780 for (d = 0; d < total; ++d)
1781 elim[d] = -1;
1782 for (i = 0; i < bmap->n_eq; ++i) {
1783 for (d = total - 1; d >= 0; --d) {
1784 if (isl_int_is_zero(bmap->eq[i][1+d]))
1785 continue;
1786 elim[d] = i;
1787 break;
1792 static void set_compute_elimination_index(__isl_keep isl_basic_set *bset,
1793 int *elim)
1795 compute_elimination_index(bset_to_bmap(bset), elim);
1798 static int reduced_using_equalities(isl_int *dst, isl_int *src,
1799 __isl_keep isl_basic_map *bmap, int *elim)
1801 int d;
1802 int copied = 0;
1803 unsigned total;
1805 total = isl_space_dim(bmap->dim, isl_dim_all);
1806 for (d = total - 1; d >= 0; --d) {
1807 if (isl_int_is_zero(src[1+d]))
1808 continue;
1809 if (elim[d] == -1)
1810 continue;
1811 if (!copied) {
1812 isl_seq_cpy(dst, src, 1 + total);
1813 copied = 1;
1815 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
1817 return copied;
1820 static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
1821 __isl_keep isl_basic_set *bset, int *elim)
1823 return reduced_using_equalities(dst, src,
1824 bset_to_bmap(bset), elim);
1827 static __isl_give isl_basic_set *isl_basic_set_reduce_using_equalities(
1828 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
1830 int i;
1831 int *elim;
1833 if (!bset || !context)
1834 goto error;
1836 if (context->n_eq == 0) {
1837 isl_basic_set_free(context);
1838 return bset;
1841 bset = isl_basic_set_cow(bset);
1842 if (!bset)
1843 goto error;
1845 elim = isl_alloc_array(bset->ctx, int, isl_basic_set_n_dim(bset));
1846 if (!elim)
1847 goto error;
1848 set_compute_elimination_index(context, elim);
1849 for (i = 0; i < bset->n_eq; ++i)
1850 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
1851 context, elim);
1852 for (i = 0; i < bset->n_ineq; ++i)
1853 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
1854 context, elim);
1855 isl_basic_set_free(context);
1856 free(elim);
1857 bset = isl_basic_set_simplify(bset);
1858 bset = isl_basic_set_finalize(bset);
1859 return bset;
1860 error:
1861 isl_basic_set_free(bset);
1862 isl_basic_set_free(context);
1863 return NULL;
1866 /* For each inequality in "ineq" that is a shifted (more relaxed)
1867 * copy of an inequality in "context", mark the corresponding entry
1868 * in "row" with -1.
1869 * If an inequality only has a non-negative constant term, then
1870 * mark it as well.
1872 static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq,
1873 __isl_keep isl_basic_set *context, int *row)
1875 struct isl_constraint_index ci;
1876 int n_ineq;
1877 unsigned total;
1878 int k;
1880 if (!ineq || !context)
1881 return isl_stat_error;
1882 if (context->n_ineq == 0)
1883 return isl_stat_ok;
1884 if (setup_constraint_index(&ci, context) < 0)
1885 return isl_stat_error;
1887 n_ineq = isl_mat_rows(ineq);
1888 total = isl_mat_cols(ineq) - 1;
1889 for (k = 0; k < n_ineq; ++k) {
1890 int l;
1891 isl_bool redundant;
1893 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
1894 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
1895 row[k] = -1;
1896 continue;
1898 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
1899 if (redundant < 0)
1900 goto error;
1901 if (!redundant)
1902 continue;
1903 row[k] = -1;
1905 constraint_index_free(&ci);
1906 return isl_stat_ok;
1907 error:
1908 constraint_index_free(&ci);
1909 return isl_stat_error;
1912 static __isl_give isl_basic_set *remove_shifted_constraints(
1913 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
1915 struct isl_constraint_index ci;
1916 int k;
1918 if (!bset || !context)
1919 return bset;
1921 if (context->n_ineq == 0)
1922 return bset;
1923 if (setup_constraint_index(&ci, context) < 0)
1924 return bset;
1926 for (k = 0; k < bset->n_ineq; ++k) {
1927 isl_bool redundant;
1929 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
1930 if (redundant < 0)
1931 goto error;
1932 if (!redundant)
1933 continue;
1934 bset = isl_basic_set_cow(bset);
1935 if (!bset)
1936 goto error;
1937 isl_basic_set_drop_inequality(bset, k);
1938 --k;
1940 constraint_index_free(&ci);
1941 return bset;
1942 error:
1943 constraint_index_free(&ci);
1944 return bset;
1947 /* Remove constraints from "bmap" that are identical to constraints
1948 * in "context" or that are more relaxed (greater constant term).
1950 * We perform the test for shifted copies on the pure constraints
1951 * in remove_shifted_constraints.
1953 static __isl_give isl_basic_map *isl_basic_map_remove_shifted_constraints(
1954 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
1956 isl_basic_set *bset, *bset_context;
1958 if (!bmap || !context)
1959 goto error;
1961 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
1962 isl_basic_map_free(context);
1963 return bmap;
1966 context = isl_basic_map_align_divs(context, bmap);
1967 bmap = isl_basic_map_align_divs(bmap, context);
1969 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
1970 bset_context = isl_basic_map_underlying_set(context);
1971 bset = remove_shifted_constraints(bset, bset_context);
1972 isl_basic_set_free(bset_context);
1974 bmap = isl_basic_map_overlying_set(bset, bmap);
1976 return bmap;
1977 error:
1978 isl_basic_map_free(bmap);
1979 isl_basic_map_free(context);
1980 return NULL;
1983 /* Does the (linear part of a) constraint "c" involve any of the "len"
1984 * "relevant" dimensions?
1986 static int is_related(isl_int *c, int len, int *relevant)
1988 int i;
1990 for (i = 0; i < len; ++i) {
1991 if (!relevant[i])
1992 continue;
1993 if (!isl_int_is_zero(c[i]))
1994 return 1;
1997 return 0;
2000 /* Drop constraints from "bmap" that do not involve any of
2001 * the dimensions marked "relevant".
2003 static __isl_give isl_basic_map *drop_unrelated_constraints(
2004 __isl_take isl_basic_map *bmap, int *relevant)
2006 int i, dim;
2008 dim = isl_basic_map_dim(bmap, isl_dim_all);
2009 for (i = 0; i < dim; ++i)
2010 if (!relevant[i])
2011 break;
2012 if (i >= dim)
2013 return bmap;
2015 for (i = bmap->n_eq - 1; i >= 0; --i)
2016 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
2017 bmap = isl_basic_map_cow(bmap);
2018 if (isl_basic_map_drop_equality(bmap, i) < 0)
2019 return isl_basic_map_free(bmap);
2022 for (i = bmap->n_ineq - 1; i >= 0; --i)
2023 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
2024 bmap = isl_basic_map_cow(bmap);
2025 if (isl_basic_map_drop_inequality(bmap, i) < 0)
2026 return isl_basic_map_free(bmap);
2029 return bmap;
2032 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2034 * In particular, for any variable involved in the constraint,
2035 * find the actual group id from before and replace the group
2036 * of the corresponding variable by the minimal group of all
2037 * the variables involved in the constraint considered so far
2038 * (if this minimum is smaller) or replace the minimum by this group
2039 * (if the minimum is larger).
2041 * At the end, all the variables in "c" will (indirectly) point
2042 * to the minimal of the groups that they referred to originally.
2044 static void update_groups(int dim, int *group, isl_int *c)
2046 int j;
2047 int min = dim;
2049 for (j = 0; j < dim; ++j) {
2050 if (isl_int_is_zero(c[j]))
2051 continue;
2052 while (group[j] >= 0 && group[group[j]] != group[j])
2053 group[j] = group[group[j]];
2054 if (group[j] == min)
2055 continue;
2056 if (group[j] < min) {
2057 if (min >= 0 && min < dim)
2058 group[min] = group[j];
2059 min = group[j];
2060 } else
2061 group[group[j]] = min;
2065 /* Allocate an array of groups of variables, one for each variable
2066 * in "context", initialized to zero.
2068 static int *alloc_groups(__isl_keep isl_basic_set *context)
2070 isl_ctx *ctx;
2071 int dim;
2073 dim = isl_basic_set_dim(context, isl_dim_set);
2074 ctx = isl_basic_set_get_ctx(context);
2075 return isl_calloc_array(ctx, int, dim);
2078 /* Drop constraints from "bmap" that only involve variables that are
2079 * not related to any of the variables marked with a "-1" in "group".
2081 * We construct groups of variables that collect variables that
2082 * (indirectly) appear in some common constraint of "bmap".
2083 * Each group is identified by the first variable in the group,
2084 * except for the special group of variables that was already identified
2085 * in the input as -1 (or are related to those variables).
2086 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2087 * otherwise the group of i is the group of group[i].
2089 * We first initialize groups for the remaining variables.
2090 * Then we iterate over the constraints of "bmap" and update the
2091 * group of the variables in the constraint by the smallest group.
2092 * Finally, we resolve indirect references to groups by running over
2093 * the variables.
2095 * After computing the groups, we drop constraints that do not involve
2096 * any variables in the -1 group.
2098 __isl_give isl_basic_map *isl_basic_map_drop_unrelated_constraints(
2099 __isl_take isl_basic_map *bmap, __isl_take int *group)
2101 int dim;
2102 int i;
2103 int last;
2105 if (!bmap)
2106 return NULL;
2108 dim = isl_basic_map_dim(bmap, isl_dim_all);
2110 last = -1;
2111 for (i = 0; i < dim; ++i)
2112 if (group[i] >= 0)
2113 last = group[i] = i;
2114 if (last < 0) {
2115 free(group);
2116 return bmap;
2119 for (i = 0; i < bmap->n_eq; ++i)
2120 update_groups(dim, group, bmap->eq[i] + 1);
2121 for (i = 0; i < bmap->n_ineq; ++i)
2122 update_groups(dim, group, bmap->ineq[i] + 1);
2124 for (i = 0; i < dim; ++i)
2125 if (group[i] >= 0)
2126 group[i] = group[group[i]];
2128 for (i = 0; i < dim; ++i)
2129 group[i] = group[i] == -1;
2131 bmap = drop_unrelated_constraints(bmap, group);
2133 free(group);
2134 return bmap;
2137 /* Drop constraints from "context" that are irrelevant for computing
2138 * the gist of "bset".
2140 * In particular, drop constraints in variables that are not related
2141 * to any of the variables involved in the constraints of "bset"
2142 * in the sense that there is no sequence of constraints that connects them.
2144 * We first mark all variables that appear in "bset" as belonging
2145 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2147 static __isl_give isl_basic_set *drop_irrelevant_constraints(
2148 __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
2150 int *group;
2151 int dim;
2152 int i, j;
2154 if (!context || !bset)
2155 return isl_basic_set_free(context);
2157 group = alloc_groups(context);
2159 if (!group)
2160 return isl_basic_set_free(context);
2162 dim = isl_basic_set_dim(bset, isl_dim_set);
2163 for (i = 0; i < dim; ++i) {
2164 for (j = 0; j < bset->n_eq; ++j)
2165 if (!isl_int_is_zero(bset->eq[j][1 + i]))
2166 break;
2167 if (j < bset->n_eq) {
2168 group[i] = -1;
2169 continue;
2171 for (j = 0; j < bset->n_ineq; ++j)
2172 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
2173 break;
2174 if (j < bset->n_ineq)
2175 group[i] = -1;
2178 return isl_basic_map_drop_unrelated_constraints(context, group);
2181 /* Drop constraints from "context" that are irrelevant for computing
2182 * the gist of the inequalities "ineq".
2183 * Inequalities in "ineq" for which the corresponding element of row
2184 * is set to -1 have already been marked for removal and should be ignored.
2186 * In particular, drop constraints in variables that are not related
2187 * to any of the variables involved in "ineq"
2188 * in the sense that there is no sequence of constraints that connects them.
2190 * We first mark all variables that appear in "bset" as belonging
2191 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2193 static __isl_give isl_basic_set *drop_irrelevant_constraints_marked(
2194 __isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
2196 int *group;
2197 int dim;
2198 int i, j, n;
2200 if (!context || !ineq)
2201 return isl_basic_set_free(context);
2203 group = alloc_groups(context);
2205 if (!group)
2206 return isl_basic_set_free(context);
2208 dim = isl_basic_set_dim(context, isl_dim_set);
2209 n = isl_mat_rows(ineq);
2210 for (i = 0; i < dim; ++i) {
2211 for (j = 0; j < n; ++j) {
2212 if (row[j] < 0)
2213 continue;
2214 if (!isl_int_is_zero(ineq->row[j][1 + i]))
2215 break;
2217 if (j < n)
2218 group[i] = -1;
2221 return isl_basic_map_drop_unrelated_constraints(context, group);
2224 /* Do all "n" entries of "row" contain a negative value?
2226 static int all_neg(int *row, int n)
2228 int i;
2230 for (i = 0; i < n; ++i)
2231 if (row[i] >= 0)
2232 return 0;
2234 return 1;
2237 /* Update the inequalities in "bset" based on the information in "row"
2238 * and "tab".
2240 * In particular, the array "row" contains either -1, meaning that
2241 * the corresponding inequality of "bset" is redundant, or the index
2242 * of an inequality in "tab".
2244 * If the row entry is -1, then drop the inequality.
2245 * Otherwise, if the constraint is marked redundant in the tableau,
2246 * then drop the inequality. Similarly, if it is marked as an equality
2247 * in the tableau, then turn the inequality into an equality and
2248 * perform Gaussian elimination.
2250 static __isl_give isl_basic_set *update_ineq(__isl_take isl_basic_set *bset,
2251 __isl_keep int *row, struct isl_tab *tab)
2253 int i;
2254 unsigned n_ineq;
2255 unsigned n_eq;
2256 int found_equality = 0;
2258 if (!bset)
2259 return NULL;
2260 if (tab && tab->empty)
2261 return isl_basic_set_set_to_empty(bset);
2263 n_ineq = bset->n_ineq;
2264 for (i = n_ineq - 1; i >= 0; --i) {
2265 if (row[i] < 0) {
2266 if (isl_basic_set_drop_inequality(bset, i) < 0)
2267 return isl_basic_set_free(bset);
2268 continue;
2270 if (!tab)
2271 continue;
2272 n_eq = tab->n_eq;
2273 if (isl_tab_is_equality(tab, n_eq + row[i])) {
2274 isl_basic_map_inequality_to_equality(bset, i);
2275 found_equality = 1;
2276 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
2277 if (isl_basic_set_drop_inequality(bset, i) < 0)
2278 return isl_basic_set_free(bset);
2282 if (found_equality)
2283 bset = isl_basic_set_gauss(bset, NULL);
2284 bset = isl_basic_set_finalize(bset);
2285 return bset;
2288 /* Update the inequalities in "bset" based on the information in "row"
2289 * and "tab" and free all arguments (other than "bset").
2291 static __isl_give isl_basic_set *update_ineq_free(
2292 __isl_take isl_basic_set *bset, __isl_take isl_mat *ineq,
2293 __isl_take isl_basic_set *context, __isl_take int *row,
2294 struct isl_tab *tab)
2296 isl_mat_free(ineq);
2297 isl_basic_set_free(context);
2299 bset = update_ineq(bset, row, tab);
2301 free(row);
2302 isl_tab_free(tab);
2303 return bset;
2306 /* Remove all information from bset that is redundant in the context
2307 * of context.
2308 * "ineq" contains the (possibly transformed) inequalities of "bset",
2309 * in the same order.
2310 * The (explicit) equalities of "bset" are assumed to have been taken
2311 * into account by the transformation such that only the inequalities
2312 * are relevant.
2313 * "context" is assumed not to be empty.
2315 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2316 * A value of -1 means that the inequality is obviously redundant and may
2317 * not even appear in "tab".
2319 * We first mark the inequalities of "bset"
2320 * that are obviously redundant with respect to some inequality in "context".
2321 * Then we remove those constraints from "context" that have become
2322 * irrelevant for computing the gist of "bset".
2323 * Note that this removal of constraints cannot be replaced by
2324 * a factorization because factors in "bset" may still be connected
2325 * to each other through constraints in "context".
2327 * If there are any inequalities left, we construct a tableau for
2328 * the context and then add the inequalities of "bset".
2329 * Before adding these inequalities, we freeze all constraints such that
2330 * they won't be considered redundant in terms of the constraints of "bset".
2331 * Then we detect all redundant constraints (among the
2332 * constraints that weren't frozen), first by checking for redundancy in the
2333 * the tableau and then by checking if replacing a constraint by its negation
2334 * would lead to an empty set. This last step is fairly expensive
2335 * and could be optimized by more reuse of the tableau.
2336 * Finally, we update bset according to the results.
2338 static __isl_give isl_basic_set *uset_gist_full(__isl_take isl_basic_set *bset,
2339 __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
2341 int i, r;
2342 int *row = NULL;
2343 isl_ctx *ctx;
2344 isl_basic_set *combined = NULL;
2345 struct isl_tab *tab = NULL;
2346 unsigned n_eq, context_ineq;
2348 if (!bset || !ineq || !context)
2349 goto error;
2351 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
2352 isl_basic_set_free(context);
2353 isl_mat_free(ineq);
2354 return bset;
2357 ctx = isl_basic_set_get_ctx(context);
2358 row = isl_calloc_array(ctx, int, bset->n_ineq);
2359 if (!row)
2360 goto error;
2362 if (mark_shifted_constraints(ineq, context, row) < 0)
2363 goto error;
2364 if (all_neg(row, bset->n_ineq))
2365 return update_ineq_free(bset, ineq, context, row, NULL);
2367 context = drop_irrelevant_constraints_marked(context, ineq, row);
2368 if (!context)
2369 goto error;
2370 if (isl_basic_set_plain_is_universe(context))
2371 return update_ineq_free(bset, ineq, context, row, NULL);
2373 n_eq = context->n_eq;
2374 context_ineq = context->n_ineq;
2375 combined = isl_basic_set_cow(isl_basic_set_copy(context));
2376 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
2377 tab = isl_tab_from_basic_set(combined, 0);
2378 for (i = 0; i < context_ineq; ++i)
2379 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
2380 goto error;
2381 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
2382 goto error;
2383 r = context_ineq;
2384 for (i = 0; i < bset->n_ineq; ++i) {
2385 if (row[i] < 0)
2386 continue;
2387 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
2388 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
2389 goto error;
2390 row[i] = r++;
2392 if (isl_tab_detect_implicit_equalities(tab) < 0)
2393 goto error;
2394 if (isl_tab_detect_redundant(tab) < 0)
2395 goto error;
2396 for (i = bset->n_ineq - 1; i >= 0; --i) {
2397 isl_basic_set *test;
2398 int is_empty;
2400 if (row[i] < 0)
2401 continue;
2402 r = row[i];
2403 if (tab->con[n_eq + r].is_redundant)
2404 continue;
2405 test = isl_basic_set_dup(combined);
2406 test = isl_inequality_negate(test, r);
2407 test = isl_basic_set_update_from_tab(test, tab);
2408 is_empty = isl_basic_set_is_empty(test);
2409 isl_basic_set_free(test);
2410 if (is_empty < 0)
2411 goto error;
2412 if (is_empty)
2413 tab->con[n_eq + r].is_redundant = 1;
2415 bset = update_ineq_free(bset, ineq, context, row, tab);
2416 if (bset) {
2417 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2418 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2421 isl_basic_set_free(combined);
2422 return bset;
2423 error:
2424 free(row);
2425 isl_mat_free(ineq);
2426 isl_tab_free(tab);
2427 isl_basic_set_free(combined);
2428 isl_basic_set_free(context);
2429 isl_basic_set_free(bset);
2430 return NULL;
2433 /* Extract the inequalities of "bset" as an isl_mat.
2435 static __isl_give isl_mat *extract_ineq(__isl_keep isl_basic_set *bset)
2437 unsigned total;
2438 isl_ctx *ctx;
2439 isl_mat *ineq;
2441 if (!bset)
2442 return NULL;
2444 ctx = isl_basic_set_get_ctx(bset);
2445 total = isl_basic_set_total_dim(bset);
2446 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
2447 0, 1 + total);
2449 return ineq;
2452 /* Remove all information from "bset" that is redundant in the context
2453 * of "context", for the case where both "bset" and "context" are
2454 * full-dimensional.
2456 static __isl_give isl_basic_set *uset_gist_uncompressed(
2457 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
2459 isl_mat *ineq;
2461 ineq = extract_ineq(bset);
2462 return uset_gist_full(bset, ineq, context);
2465 /* Remove all information from "bset" that is redundant in the context
2466 * of "context", for the case where the combined equalities of
2467 * "bset" and "context" allow for a compression that can be obtained
2468 * by preapplication of "T".
2470 * "bset" itself is not transformed by "T". Instead, the inequalities
2471 * are extracted from "bset" and those are transformed by "T".
2472 * uset_gist_full then determines which of the transformed inequalities
2473 * are redundant with respect to the transformed "context" and removes
2474 * the corresponding inequalities from "bset".
2476 * After preapplying "T" to the inequalities, any common factor is
2477 * removed from the coefficients. If this results in a tightening
2478 * of the constant term, then the same tightening is applied to
2479 * the corresponding untransformed inequality in "bset".
2480 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2482 * g f'(x) + r >= 0
2484 * with 0 <= r < g, then it is equivalent to
2486 * f'(x) >= 0
2488 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2489 * subspace compressed by T since the latter would be transformed to
2491 * g f'(x) >= 0
2493 static __isl_give isl_basic_set *uset_gist_compressed(
2494 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *context,
2495 __isl_take isl_mat *T)
2497 isl_ctx *ctx;
2498 isl_mat *ineq;
2499 int i, n_row, n_col;
2500 isl_int rem;
2502 ineq = extract_ineq(bset);
2503 ineq = isl_mat_product(ineq, isl_mat_copy(T));
2504 context = isl_basic_set_preimage(context, T);
2506 if (!ineq || !context)
2507 goto error;
2508 if (isl_basic_set_plain_is_empty(context)) {
2509 isl_mat_free(ineq);
2510 isl_basic_set_free(context);
2511 return isl_basic_set_set_to_empty(bset);
2514 ctx = isl_mat_get_ctx(ineq);
2515 n_row = isl_mat_rows(ineq);
2516 n_col = isl_mat_cols(ineq);
2517 isl_int_init(rem);
2518 for (i = 0; i < n_row; ++i) {
2519 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
2520 if (isl_int_is_zero(ctx->normalize_gcd))
2521 continue;
2522 if (isl_int_is_one(ctx->normalize_gcd))
2523 continue;
2524 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
2525 ctx->normalize_gcd, n_col - 1);
2526 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
2527 isl_int_fdiv_q(ineq->row[i][0],
2528 ineq->row[i][0], ctx->normalize_gcd);
2529 if (isl_int_is_zero(rem))
2530 continue;
2531 bset = isl_basic_set_cow(bset);
2532 if (!bset)
2533 break;
2534 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
2536 isl_int_clear(rem);
2538 return uset_gist_full(bset, ineq, context);
2539 error:
2540 isl_mat_free(ineq);
2541 isl_basic_set_free(context);
2542 isl_basic_set_free(bset);
2543 return NULL;
2546 /* Project "bset" onto the variables that are involved in "template".
2548 static __isl_give isl_basic_set *project_onto_involved(
2549 __isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
2551 int i, n;
2553 if (!bset || !template)
2554 return isl_basic_set_free(bset);
2556 n = isl_basic_set_dim(template, isl_dim_set);
2558 for (i = 0; i < n; ++i) {
2559 isl_bool involved;
2561 involved = isl_basic_set_involves_dims(template,
2562 isl_dim_set, i, 1);
2563 if (involved < 0)
2564 return isl_basic_set_free(bset);
2565 if (involved)
2566 continue;
2567 bset = isl_basic_set_eliminate_vars(bset, i, 1);
2570 return bset;
2573 /* Remove all information from bset that is redundant in the context
2574 * of context. In particular, equalities that are linear combinations
2575 * of those in context are removed. Then the inequalities that are
2576 * redundant in the context of the equalities and inequalities of
2577 * context are removed.
2579 * First of all, we drop those constraints from "context"
2580 * that are irrelevant for computing the gist of "bset".
2581 * Alternatively, we could factorize the intersection of "context" and "bset".
2583 * We first compute the intersection of the integer affine hulls
2584 * of "bset" and "context",
2585 * compute the gist inside this intersection and then reduce
2586 * the constraints with respect to the equalities of the context
2587 * that only involve variables already involved in the input.
2589 * If two constraints are mutually redundant, then uset_gist_full
2590 * will remove the second of those constraints. We therefore first
2591 * sort the constraints so that constraints not involving existentially
2592 * quantified variables are given precedence over those that do.
2593 * We have to perform this sorting before the variable compression,
2594 * because that may effect the order of the variables.
2596 static __isl_give isl_basic_set *uset_gist(__isl_take isl_basic_set *bset,
2597 __isl_take isl_basic_set *context)
2599 isl_mat *eq;
2600 isl_mat *T;
2601 isl_basic_set *aff;
2602 isl_basic_set *aff_context;
2603 unsigned total;
2605 if (!bset || !context)
2606 goto error;
2608 context = drop_irrelevant_constraints(context, bset);
2610 bset = isl_basic_set_detect_equalities(bset);
2611 aff = isl_basic_set_copy(bset);
2612 aff = isl_basic_set_plain_affine_hull(aff);
2613 context = isl_basic_set_detect_equalities(context);
2614 aff_context = isl_basic_set_copy(context);
2615 aff_context = isl_basic_set_plain_affine_hull(aff_context);
2616 aff = isl_basic_set_intersect(aff, aff_context);
2617 if (!aff)
2618 goto error;
2619 if (isl_basic_set_plain_is_empty(aff)) {
2620 isl_basic_set_free(bset);
2621 isl_basic_set_free(context);
2622 return aff;
2624 bset = isl_basic_set_sort_constraints(bset);
2625 if (aff->n_eq == 0) {
2626 isl_basic_set_free(aff);
2627 return uset_gist_uncompressed(bset, context);
2629 total = isl_basic_set_total_dim(bset);
2630 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
2631 eq = isl_mat_cow(eq);
2632 T = isl_mat_variable_compression(eq, NULL);
2633 isl_basic_set_free(aff);
2634 if (T && T->n_col == 0) {
2635 isl_mat_free(T);
2636 isl_basic_set_free(context);
2637 return isl_basic_set_set_to_empty(bset);
2640 aff_context = isl_basic_set_affine_hull(isl_basic_set_copy(context));
2641 aff_context = project_onto_involved(aff_context, bset);
2643 bset = uset_gist_compressed(bset, context, T);
2644 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
2646 if (bset) {
2647 ISL_F_SET(bset, ISL_BASIC_SET_NO_IMPLICIT);
2648 ISL_F_SET(bset, ISL_BASIC_SET_NO_REDUNDANT);
2651 return bset;
2652 error:
2653 isl_basic_set_free(bset);
2654 isl_basic_set_free(context);
2655 return NULL;
2658 /* Return the number of equality constraints in "bmap" that involve
2659 * local variables. This function assumes that Gaussian elimination
2660 * has been applied to the equality constraints.
2662 static int n_div_eq(__isl_keep isl_basic_map *bmap)
2664 int i;
2665 int total, n_div;
2667 if (!bmap)
2668 return -1;
2670 if (bmap->n_eq == 0)
2671 return 0;
2673 total = isl_basic_map_dim(bmap, isl_dim_all);
2674 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2675 total -= n_div;
2677 for (i = 0; i < bmap->n_eq; ++i)
2678 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total,
2679 n_div) == -1)
2680 return i;
2682 return bmap->n_eq;
2685 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2686 * The constraints are assumed not to involve any local variables.
2688 static __isl_give isl_basic_map *basic_map_from_equalities(
2689 __isl_take isl_space *space, __isl_take isl_mat *eq)
2691 int i, k;
2692 isl_basic_map *bmap = NULL;
2694 if (!space || !eq)
2695 goto error;
2697 if (1 + isl_space_dim(space, isl_dim_all) != eq->n_col)
2698 isl_die(isl_space_get_ctx(space), isl_error_internal,
2699 "unexpected number of columns", goto error);
2701 bmap = isl_basic_map_alloc_space(isl_space_copy(space),
2702 0, eq->n_row, 0);
2703 for (i = 0; i < eq->n_row; ++i) {
2704 k = isl_basic_map_alloc_equality(bmap);
2705 if (k < 0)
2706 goto error;
2707 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
2710 isl_space_free(space);
2711 isl_mat_free(eq);
2712 return bmap;
2713 error:
2714 isl_space_free(space);
2715 isl_mat_free(eq);
2716 isl_basic_map_free(bmap);
2717 return NULL;
2720 /* Construct and return a variable compression based on the equality
2721 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2722 * "n1" is the number of (initial) equality constraints in "bmap1"
2723 * that do involve local variables.
2724 * "n2" is the number of (initial) equality constraints in "bmap2"
2725 * that do involve local variables.
2726 * "total" is the total number of other variables.
2727 * This function assumes that Gaussian elimination
2728 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2729 * such that the equality constraints not involving local variables
2730 * are those that start at "n1" or "n2".
2732 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2733 * then simply compute the compression based on the equality constraints
2734 * in the other basic map.
2735 * Otherwise, combine the equality constraints from both into a new
2736 * basic map such that Gaussian elimination can be applied to this combination
2737 * and then construct a variable compression from the resulting
2738 * equality constraints.
2740 static __isl_give isl_mat *combined_variable_compression(
2741 __isl_keep isl_basic_map *bmap1, int n1,
2742 __isl_keep isl_basic_map *bmap2, int n2, int total)
2744 isl_ctx *ctx;
2745 isl_mat *E1, *E2, *V;
2746 isl_basic_map *bmap;
2748 ctx = isl_basic_map_get_ctx(bmap1);
2749 if (bmap1->n_eq == n1) {
2750 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2751 n2, bmap2->n_eq - n2, 0, 1 + total);
2752 return isl_mat_variable_compression(E2, NULL);
2754 if (bmap2->n_eq == n2) {
2755 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2756 n1, bmap1->n_eq - n1, 0, 1 + total);
2757 return isl_mat_variable_compression(E1, NULL);
2759 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
2760 n1, bmap1->n_eq - n1, 0, 1 + total);
2761 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
2762 n2, bmap2->n_eq - n2, 0, 1 + total);
2763 E1 = isl_mat_concat(E1, E2);
2764 bmap = basic_map_from_equalities(isl_basic_map_get_space(bmap1), E1);
2765 bmap = isl_basic_map_gauss(bmap, NULL);
2766 if (!bmap)
2767 return NULL;
2768 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
2769 V = isl_mat_variable_compression(E1, NULL);
2770 isl_basic_map_free(bmap);
2772 return V;
2775 /* Extract the stride constraints from "bmap", compressed
2776 * with respect to both the stride constraints in "context" and
2777 * the remaining equality constraints in both "bmap" and "context".
2778 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2779 * "context_n_eq" is the number of (initial) stride constraints in "context".
2781 * Let x be all variables in "bmap" (and "context") other than the local
2782 * variables. First compute a variable compression
2784 * x = V x'
2786 * based on the non-stride equality constraints in "bmap" and "context".
2787 * Consider the stride constraints of "context",
2789 * A(x) + B(y) = 0
2791 * with y the local variables and plug in the variable compression,
2792 * resulting in
2794 * A(V x') + B(y) = 0
2796 * Use these constraints to compute a parameter compression on x'
2798 * x' = T x''
2800 * Now consider the stride constraints of "bmap"
2802 * C(x) + D(y) = 0
2804 * and plug in x = V*T x''.
2805 * That is, return A = [C*V*T D].
2807 static __isl_give isl_mat *extract_compressed_stride_constraints(
2808 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
2809 __isl_keep isl_basic_map *context, int context_n_eq)
2811 int total, n_div;
2812 isl_ctx *ctx;
2813 isl_mat *A, *B, *T, *V;
2815 total = isl_basic_map_dim(context, isl_dim_all);
2816 n_div = isl_basic_map_dim(context, isl_dim_div);
2817 total -= n_div;
2819 ctx = isl_basic_map_get_ctx(bmap);
2821 V = combined_variable_compression(bmap, bmap_n_eq,
2822 context, context_n_eq, total);
2824 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
2825 B = isl_mat_sub_alloc6(ctx, context->eq,
2826 0, context_n_eq, 1 + total, n_div);
2827 A = isl_mat_product(A, isl_mat_copy(V));
2828 T = isl_mat_parameter_compression_ext(A, B);
2829 T = isl_mat_product(V, T);
2831 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2832 T = isl_mat_diagonal(T, isl_mat_identity(ctx, n_div));
2834 A = isl_mat_sub_alloc6(ctx, bmap->eq,
2835 0, bmap_n_eq, 0, 1 + total + n_div);
2836 A = isl_mat_product(A, T);
2838 return A;
2841 /* Remove the prime factors from *g that have an exponent that
2842 * is strictly smaller than the exponent in "c".
2843 * All exponents in *g are known to be smaller than or equal
2844 * to those in "c".
2846 * That is, if *g is equal to
2848 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
2850 * and "c" is equal to
2852 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
2854 * then update *g to
2856 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
2857 * p_n^{e_n * (e_n = f_n)}
2859 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
2860 * neither does the gcd of *g and c / *g.
2861 * If e_i < f_i, then the gcd of *g and c / *g has a positive
2862 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
2863 * Dividing *g by this gcd therefore strictly reduces the exponent
2864 * of the prime factors that need to be removed, while leaving the
2865 * other prime factors untouched.
2866 * Repeating this process until gcd(*g, c / *g) = 1 therefore
2867 * removes all undesired factors, without removing any others.
2869 static void remove_incomplete_powers(isl_int *g, isl_int c)
2871 isl_int t;
2873 isl_int_init(t);
2874 for (;;) {
2875 isl_int_divexact(t, c, *g);
2876 isl_int_gcd(t, t, *g);
2877 if (isl_int_is_one(t))
2878 break;
2879 isl_int_divexact(*g, *g, t);
2881 isl_int_clear(t);
2884 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
2885 * of the same stride constraints in a compressed space that exploits
2886 * all equalities in the context and the other equalities in "bmap".
2888 * If the stride constraints of "bmap" are of the form
2890 * C(x) + D(y) = 0
2892 * then A is of the form
2894 * B(x') + D(y) = 0
2896 * If any of these constraints involves only a single local variable y,
2897 * then the constraint appears as
2899 * f(x) + m y_i = 0
2901 * in "bmap" and as
2903 * h(x') + m y_i = 0
2905 * in "A".
2907 * Let g be the gcd of m and the coefficients of h.
2908 * Then, in particular, g is a divisor of the coefficients of h and
2910 * f(x) = h(x')
2912 * is known to be a multiple of g.
2913 * If some prime factor in m appears with the same exponent in g,
2914 * then it can be removed from m because f(x) is already known
2915 * to be a multiple of g and therefore in particular of this power
2916 * of the prime factors.
2917 * Prime factors that appear with a smaller exponent in g cannot
2918 * be removed from m.
2919 * Let g' be the divisor of g containing all prime factors that
2920 * appear with the same exponent in m and g, then
2922 * f(x) + m y_i = 0
2924 * can be replaced by
2926 * f(x) + m/g' y_i' = 0
2928 * Note that (if g' != 1) this changes the explicit representation
2929 * of y_i to that of y_i', so the integer division at position i
2930 * is marked unknown and later recomputed by a call to
2931 * isl_basic_map_gauss.
2933 static __isl_give isl_basic_map *reduce_stride_constraints(
2934 __isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
2936 int i;
2937 int total, n_div;
2938 int any = 0;
2939 isl_int gcd;
2941 if (!bmap || !A)
2942 return isl_basic_map_free(bmap);
2944 total = isl_basic_map_dim(bmap, isl_dim_all);
2945 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2946 total -= n_div;
2948 isl_int_init(gcd);
2949 for (i = 0; i < n; ++i) {
2950 int div;
2952 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
2953 if (div < 0)
2954 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
2955 "equality constraints modified unexpectedly",
2956 goto error);
2957 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + total + div + 1,
2958 n_div - div - 1) != -1)
2959 continue;
2960 if (isl_mat_row_gcd(A, i, &gcd) < 0)
2961 goto error;
2962 if (isl_int_is_one(gcd))
2963 continue;
2964 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
2965 if (isl_int_is_one(gcd))
2966 continue;
2967 isl_int_divexact(bmap->eq[i][1 + total + div],
2968 bmap->eq[i][1 + total + div], gcd);
2969 bmap = isl_basic_map_mark_div_unknown(bmap, div);
2970 if (!bmap)
2971 goto error;
2972 any = 1;
2974 isl_int_clear(gcd);
2976 if (any)
2977 bmap = isl_basic_map_gauss(bmap, NULL);
2979 return bmap;
2980 error:
2981 isl_int_clear(gcd);
2982 isl_basic_map_free(bmap);
2983 return NULL;
2986 /* Simplify the stride constraints in "bmap" based on
2987 * the remaining equality constraints in "bmap" and all equality
2988 * constraints in "context".
2989 * Only do this if both "bmap" and "context" have stride constraints.
2991 * First extract a copy of the stride constraints in "bmap" in a compressed
2992 * space exploiting all the other equality constraints and then
2993 * use this compressed copy to simplify the original stride constraints.
2995 static __isl_give isl_basic_map *gist_strides(__isl_take isl_basic_map *bmap,
2996 __isl_keep isl_basic_map *context)
2998 int bmap_n_eq, context_n_eq;
2999 isl_mat *A;
3001 if (!bmap || !context)
3002 return isl_basic_map_free(bmap);
3004 bmap_n_eq = n_div_eq(bmap);
3005 context_n_eq = n_div_eq(context);
3007 if (bmap_n_eq < 0 || context_n_eq < 0)
3008 return isl_basic_map_free(bmap);
3009 if (bmap_n_eq == 0 || context_n_eq == 0)
3010 return bmap;
3012 A = extract_compressed_stride_constraints(bmap, bmap_n_eq,
3013 context, context_n_eq);
3014 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
3016 isl_mat_free(A);
3018 return bmap;
3021 /* Return a basic map that has the same intersection with "context" as "bmap"
3022 * and that is as "simple" as possible.
3024 * The core computation is performed on the pure constraints.
3025 * When we add back the meaning of the integer divisions, we need
3026 * to (re)introduce the div constraints. If we happen to have
3027 * discovered that some of these integer divisions are equal to
3028 * some affine combination of other variables, then these div
3029 * constraints may end up getting simplified in terms of the equalities,
3030 * resulting in extra inequalities on the other variables that
3031 * may have been removed already or that may not even have been
3032 * part of the input. We try and remove those constraints of
3033 * this form that are most obviously redundant with respect to
3034 * the context. We also remove those div constraints that are
3035 * redundant with respect to the other constraints in the result.
3037 * The stride constraints among the equality constraints in "bmap" are
3038 * also simplified with respecting to the other equality constraints
3039 * in "bmap" and with respect to all equality constraints in "context".
3041 __isl_give isl_basic_map *isl_basic_map_gist(__isl_take isl_basic_map *bmap,
3042 __isl_take isl_basic_map *context)
3044 isl_basic_set *bset, *eq;
3045 isl_basic_map *eq_bmap;
3046 unsigned total, n_div, extra, n_eq, n_ineq;
3048 if (!bmap || !context)
3049 goto error;
3051 if (isl_basic_map_plain_is_universe(bmap)) {
3052 isl_basic_map_free(context);
3053 return bmap;
3055 if (isl_basic_map_plain_is_empty(context)) {
3056 isl_space *space = isl_basic_map_get_space(bmap);
3057 isl_basic_map_free(bmap);
3058 isl_basic_map_free(context);
3059 return isl_basic_map_universe(space);
3061 if (isl_basic_map_plain_is_empty(bmap)) {
3062 isl_basic_map_free(context);
3063 return bmap;
3066 bmap = isl_basic_map_remove_redundancies(bmap);
3067 context = isl_basic_map_remove_redundancies(context);
3068 context = isl_basic_map_align_divs(context, bmap);
3069 if (!context)
3070 goto error;
3072 n_div = isl_basic_map_dim(context, isl_dim_div);
3073 total = isl_basic_map_dim(bmap, isl_dim_all);
3074 extra = n_div - isl_basic_map_dim(bmap, isl_dim_div);
3076 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3077 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
3078 bset = uset_gist(bset,
3079 isl_basic_map_underlying_set(isl_basic_map_copy(context)));
3080 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
3082 if (!bset || bset->n_eq == 0 || n_div == 0 ||
3083 isl_basic_set_plain_is_empty(bset)) {
3084 isl_basic_map_free(context);
3085 return isl_basic_map_overlying_set(bset, bmap);
3088 n_eq = bset->n_eq;
3089 n_ineq = bset->n_ineq;
3090 eq = isl_basic_set_copy(bset);
3091 eq = isl_basic_set_cow(eq);
3092 if (isl_basic_set_free_inequality(eq, n_ineq) < 0)
3093 eq = isl_basic_set_free(eq);
3094 if (isl_basic_set_free_equality(bset, n_eq) < 0)
3095 bset = isl_basic_set_free(bset);
3097 eq_bmap = isl_basic_map_overlying_set(eq, isl_basic_map_copy(bmap));
3098 eq_bmap = gist_strides(eq_bmap, context);
3099 eq_bmap = isl_basic_map_remove_shifted_constraints(eq_bmap, context);
3100 bmap = isl_basic_map_overlying_set(bset, bmap);
3101 bmap = isl_basic_map_intersect(bmap, eq_bmap);
3102 bmap = isl_basic_map_remove_redundancies(bmap);
3104 return bmap;
3105 error:
3106 isl_basic_map_free(bmap);
3107 isl_basic_map_free(context);
3108 return NULL;
3112 * Assumes context has no implicit divs.
3114 __isl_give isl_map *isl_map_gist_basic_map(__isl_take isl_map *map,
3115 __isl_take isl_basic_map *context)
3117 int i;
3119 if (!map || !context)
3120 goto error;
3122 if (isl_basic_map_plain_is_empty(context)) {
3123 isl_space *space = isl_map_get_space(map);
3124 isl_map_free(map);
3125 isl_basic_map_free(context);
3126 return isl_map_universe(space);
3129 context = isl_basic_map_remove_redundancies(context);
3130 map = isl_map_cow(map);
3131 if (!map || !context)
3132 goto error;
3133 isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
3134 map = isl_map_compute_divs(map);
3135 if (!map)
3136 goto error;
3137 for (i = map->n - 1; i >= 0; --i) {
3138 map->p[i] = isl_basic_map_gist(map->p[i],
3139 isl_basic_map_copy(context));
3140 if (!map->p[i])
3141 goto error;
3142 if (isl_basic_map_plain_is_empty(map->p[i])) {
3143 isl_basic_map_free(map->p[i]);
3144 if (i != map->n - 1)
3145 map->p[i] = map->p[map->n - 1];
3146 map->n--;
3149 isl_basic_map_free(context);
3150 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3151 return map;
3152 error:
3153 isl_map_free(map);
3154 isl_basic_map_free(context);
3155 return NULL;
3158 /* Drop all inequalities from "bmap" that also appear in "context".
3159 * "context" is assumed to have only known local variables and
3160 * the initial local variables of "bmap" are assumed to be the same
3161 * as those of "context".
3162 * The constraints of both "bmap" and "context" are assumed
3163 * to have been sorted using isl_basic_map_sort_constraints.
3165 * Run through the inequality constraints of "bmap" and "context"
3166 * in sorted order.
3167 * If a constraint of "bmap" involves variables not in "context",
3168 * then it cannot appear in "context".
3169 * If a matching constraint is found, it is removed from "bmap".
3171 static __isl_give isl_basic_map *drop_inequalities(
3172 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3174 int i1, i2;
3175 unsigned total, extra;
3177 if (!bmap || !context)
3178 return isl_basic_map_free(bmap);
3180 total = isl_basic_map_total_dim(context);
3181 extra = isl_basic_map_total_dim(bmap) - total;
3183 i1 = bmap->n_ineq - 1;
3184 i2 = context->n_ineq - 1;
3185 while (bmap && i1 >= 0 && i2 >= 0) {
3186 int cmp;
3188 if (isl_seq_first_non_zero(bmap->ineq[i1] + 1 + total,
3189 extra) != -1) {
3190 --i1;
3191 continue;
3193 cmp = isl_basic_map_constraint_cmp(context, bmap->ineq[i1],
3194 context->ineq[i2]);
3195 if (cmp < 0) {
3196 --i2;
3197 continue;
3199 if (cmp > 0) {
3200 --i1;
3201 continue;
3203 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
3204 bmap = isl_basic_map_cow(bmap);
3205 if (isl_basic_map_drop_inequality(bmap, i1) < 0)
3206 bmap = isl_basic_map_free(bmap);
3208 --i1;
3209 --i2;
3212 return bmap;
3215 /* Drop all equalities from "bmap" that also appear in "context".
3216 * "context" is assumed to have only known local variables and
3217 * the initial local variables of "bmap" are assumed to be the same
3218 * as those of "context".
3220 * Run through the equality constraints of "bmap" and "context"
3221 * in sorted order.
3222 * If a constraint of "bmap" involves variables not in "context",
3223 * then it cannot appear in "context".
3224 * If a matching constraint is found, it is removed from "bmap".
3226 static __isl_give isl_basic_map *drop_equalities(
3227 __isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
3229 int i1, i2;
3230 unsigned total, extra;
3232 if (!bmap || !context)
3233 return isl_basic_map_free(bmap);
3235 total = isl_basic_map_total_dim(context);
3236 extra = isl_basic_map_total_dim(bmap) - total;
3238 i1 = bmap->n_eq - 1;
3239 i2 = context->n_eq - 1;
3241 while (bmap && i1 >= 0 && i2 >= 0) {
3242 int last1, last2;
3244 if (isl_seq_first_non_zero(bmap->eq[i1] + 1 + total,
3245 extra) != -1)
3246 break;
3247 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
3248 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
3249 if (last1 > last2) {
3250 --i2;
3251 continue;
3253 if (last1 < last2) {
3254 --i1;
3255 continue;
3257 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
3258 bmap = isl_basic_map_cow(bmap);
3259 if (isl_basic_map_drop_equality(bmap, i1) < 0)
3260 bmap = isl_basic_map_free(bmap);
3262 --i1;
3263 --i2;
3266 return bmap;
3269 /* Remove the constraints in "context" from "bmap".
3270 * "context" is assumed to have explicit representations
3271 * for all local variables.
3273 * First align the divs of "bmap" to those of "context" and
3274 * sort the constraints. Then drop all constraints from "bmap"
3275 * that appear in "context".
3277 __isl_give isl_basic_map *isl_basic_map_plain_gist(
3278 __isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
3280 isl_bool done, known;
3282 done = isl_basic_map_plain_is_universe(context);
3283 if (done == isl_bool_false)
3284 done = isl_basic_map_plain_is_universe(bmap);
3285 if (done == isl_bool_false)
3286 done = isl_basic_map_plain_is_empty(context);
3287 if (done == isl_bool_false)
3288 done = isl_basic_map_plain_is_empty(bmap);
3289 if (done < 0)
3290 goto error;
3291 if (done) {
3292 isl_basic_map_free(context);
3293 return bmap;
3295 known = isl_basic_map_divs_known(context);
3296 if (known < 0)
3297 goto error;
3298 if (!known)
3299 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3300 "context has unknown divs", goto error);
3302 bmap = isl_basic_map_align_divs(bmap, context);
3303 bmap = isl_basic_map_gauss(bmap, NULL);
3304 bmap = isl_basic_map_sort_constraints(bmap);
3305 context = isl_basic_map_sort_constraints(context);
3307 bmap = drop_inequalities(bmap, context);
3308 bmap = drop_equalities(bmap, context);
3310 isl_basic_map_free(context);
3311 bmap = isl_basic_map_finalize(bmap);
3312 return bmap;
3313 error:
3314 isl_basic_map_free(bmap);
3315 isl_basic_map_free(context);
3316 return NULL;
3319 /* Replace "map" by the disjunct at position "pos" and free "context".
3321 static __isl_give isl_map *replace_by_disjunct(__isl_take isl_map *map,
3322 int pos, __isl_take isl_basic_map *context)
3324 isl_basic_map *bmap;
3326 bmap = isl_basic_map_copy(map->p[pos]);
3327 isl_map_free(map);
3328 isl_basic_map_free(context);
3329 return isl_map_from_basic_map(bmap);
3332 /* Remove the constraints in "context" from "map".
3333 * If any of the disjuncts in the result turns out to be the universe,
3334 * then return this universe.
3335 * "context" is assumed to have explicit representations
3336 * for all local variables.
3338 __isl_give isl_map *isl_map_plain_gist_basic_map(__isl_take isl_map *map,
3339 __isl_take isl_basic_map *context)
3341 int i;
3342 isl_bool univ, known;
3344 univ = isl_basic_map_plain_is_universe(context);
3345 if (univ < 0)
3346 goto error;
3347 if (univ) {
3348 isl_basic_map_free(context);
3349 return map;
3351 known = isl_basic_map_divs_known(context);
3352 if (known < 0)
3353 goto error;
3354 if (!known)
3355 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3356 "context has unknown divs", goto error);
3358 map = isl_map_cow(map);
3359 if (!map)
3360 goto error;
3361 for (i = 0; i < map->n; ++i) {
3362 map->p[i] = isl_basic_map_plain_gist(map->p[i],
3363 isl_basic_map_copy(context));
3364 univ = isl_basic_map_plain_is_universe(map->p[i]);
3365 if (univ < 0)
3366 goto error;
3367 if (univ && map->n > 1)
3368 return replace_by_disjunct(map, i, context);
3371 isl_basic_map_free(context);
3372 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3373 if (map->n > 1)
3374 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3375 return map;
3376 error:
3377 isl_map_free(map);
3378 isl_basic_map_free(context);
3379 return NULL;
3382 /* Remove the constraints in "context" from "set".
3383 * If any of the disjuncts in the result turns out to be the universe,
3384 * then return this universe.
3385 * "context" is assumed to have explicit representations
3386 * for all local variables.
3388 __isl_give isl_set *isl_set_plain_gist_basic_set(__isl_take isl_set *set,
3389 __isl_take isl_basic_set *context)
3391 return set_from_map(isl_map_plain_gist_basic_map(set_to_map(set),
3392 bset_to_bmap(context)));
3395 /* Remove the constraints in "context" from "map".
3396 * If any of the disjuncts in the result turns out to be the universe,
3397 * then return this universe.
3398 * "context" is assumed to consist of a single disjunct and
3399 * to have explicit representations for all local variables.
3401 __isl_give isl_map *isl_map_plain_gist(__isl_take isl_map *map,
3402 __isl_take isl_map *context)
3404 isl_basic_map *hull;
3406 hull = isl_map_unshifted_simple_hull(context);
3407 return isl_map_plain_gist_basic_map(map, hull);
3410 /* Replace "map" by a universe map in the same space and free "drop".
3412 static __isl_give isl_map *replace_by_universe(__isl_take isl_map *map,
3413 __isl_take isl_map *drop)
3415 isl_map *res;
3417 res = isl_map_universe(isl_map_get_space(map));
3418 isl_map_free(map);
3419 isl_map_free(drop);
3420 return res;
3423 /* Return a map that has the same intersection with "context" as "map"
3424 * and that is as "simple" as possible.
3426 * If "map" is already the universe, then we cannot make it any simpler.
3427 * Similarly, if "context" is the universe, then we cannot exploit it
3428 * to simplify "map"
3429 * If "map" and "context" are identical to each other, then we can
3430 * return the corresponding universe.
3432 * If either "map" or "context" consists of multiple disjuncts,
3433 * then check if "context" happens to be a subset of "map",
3434 * in which case all constraints can be removed.
3435 * In case of multiple disjuncts, the standard procedure
3436 * may not be able to detect that all constraints can be removed.
3438 * If none of these cases apply, we have to work a bit harder.
3439 * During this computation, we make use of a single disjunct context,
3440 * so if the original context consists of more than one disjunct
3441 * then we need to approximate the context by a single disjunct set.
3442 * Simply taking the simple hull may drop constraints that are
3443 * only implicitly available in each disjunct. We therefore also
3444 * look for constraints among those defining "map" that are valid
3445 * for the context. These can then be used to simplify away
3446 * the corresponding constraints in "map".
3448 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
3449 __isl_take isl_map *context)
3451 int equal;
3452 int is_universe;
3453 int single_disjunct_map, single_disjunct_context;
3454 isl_bool subset;
3455 isl_basic_map *hull;
3457 is_universe = isl_map_plain_is_universe(map);
3458 if (is_universe >= 0 && !is_universe)
3459 is_universe = isl_map_plain_is_universe(context);
3460 if (is_universe < 0)
3461 goto error;
3462 if (is_universe) {
3463 isl_map_free(context);
3464 return map;
3467 equal = isl_map_plain_is_equal(map, context);
3468 if (equal < 0)
3469 goto error;
3470 if (equal)
3471 return replace_by_universe(map, context);
3473 single_disjunct_map = isl_map_n_basic_map(map) == 1;
3474 single_disjunct_context = isl_map_n_basic_map(context) == 1;
3475 if (!single_disjunct_map || !single_disjunct_context) {
3476 subset = isl_map_is_subset(context, map);
3477 if (subset < 0)
3478 goto error;
3479 if (subset)
3480 return replace_by_universe(map, context);
3483 context = isl_map_compute_divs(context);
3484 if (!context)
3485 goto error;
3486 if (single_disjunct_context) {
3487 hull = isl_map_simple_hull(context);
3488 } else {
3489 isl_ctx *ctx;
3490 isl_map_list *list;
3492 ctx = isl_map_get_ctx(map);
3493 list = isl_map_list_alloc(ctx, 2);
3494 list = isl_map_list_add(list, isl_map_copy(context));
3495 list = isl_map_list_add(list, isl_map_copy(map));
3496 hull = isl_map_unshifted_simple_hull_from_map_list(context,
3497 list);
3499 return isl_map_gist_basic_map(map, hull);
3500 error:
3501 isl_map_free(map);
3502 isl_map_free(context);
3503 return NULL;
3506 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
3507 __isl_take isl_map *context)
3509 return isl_map_align_params_map_map_and(map, context, &map_gist);
3512 struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
3513 struct isl_basic_set *context)
3515 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset),
3516 bset_to_bmap(context)));
3519 __isl_give isl_set *isl_set_gist_basic_set(__isl_take isl_set *set,
3520 __isl_take isl_basic_set *context)
3522 return set_from_map(isl_map_gist_basic_map(set_to_map(set),
3523 bset_to_bmap(context)));
3526 __isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
3527 __isl_take isl_basic_set *context)
3529 isl_space *space = isl_set_get_space(set);
3530 isl_basic_set *dom_context = isl_basic_set_universe(space);
3531 dom_context = isl_basic_set_intersect_params(dom_context, context);
3532 return isl_set_gist_basic_set(set, dom_context);
3535 __isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
3536 __isl_take isl_set *context)
3538 return set_from_map(isl_map_gist(set_to_map(set), set_to_map(context)));
3541 /* Compute the gist of "bmap" with respect to the constraints "context"
3542 * on the domain.
3544 __isl_give isl_basic_map *isl_basic_map_gist_domain(
3545 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
3547 isl_space *space = isl_basic_map_get_space(bmap);
3548 isl_basic_map *bmap_context = isl_basic_map_universe(space);
3550 bmap_context = isl_basic_map_intersect_domain(bmap_context, context);
3551 return isl_basic_map_gist(bmap, bmap_context);
3554 __isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
3555 __isl_take isl_set *context)
3557 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3558 map_context = isl_map_intersect_domain(map_context, context);
3559 return isl_map_gist(map, map_context);
3562 __isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
3563 __isl_take isl_set *context)
3565 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3566 map_context = isl_map_intersect_range(map_context, context);
3567 return isl_map_gist(map, map_context);
3570 __isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
3571 __isl_take isl_set *context)
3573 isl_map *map_context = isl_map_universe(isl_map_get_space(map));
3574 map_context = isl_map_intersect_params(map_context, context);
3575 return isl_map_gist(map, map_context);
3578 __isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
3579 __isl_take isl_set *context)
3581 return isl_map_gist_params(set, context);
3584 /* Quick check to see if two basic maps are disjoint.
3585 * In particular, we reduce the equalities and inequalities of
3586 * one basic map in the context of the equalities of the other
3587 * basic map and check if we get a contradiction.
3589 isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1,
3590 __isl_keep isl_basic_map *bmap2)
3592 struct isl_vec *v = NULL;
3593 int *elim = NULL;
3594 unsigned total;
3595 int i;
3597 if (!bmap1 || !bmap2)
3598 return isl_bool_error;
3599 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3600 return isl_bool_error);
3601 if (bmap1->n_div || bmap2->n_div)
3602 return isl_bool_false;
3603 if (!bmap1->n_eq && !bmap2->n_eq)
3604 return isl_bool_false;
3606 total = isl_space_dim(bmap1->dim, isl_dim_all);
3607 if (total == 0)
3608 return isl_bool_false;
3609 v = isl_vec_alloc(bmap1->ctx, 1 + total);
3610 if (!v)
3611 goto error;
3612 elim = isl_alloc_array(bmap1->ctx, int, total);
3613 if (!elim)
3614 goto error;
3615 compute_elimination_index(bmap1, elim);
3616 for (i = 0; i < bmap2->n_eq; ++i) {
3617 int reduced;
3618 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
3619 bmap1, elim);
3620 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
3621 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3622 goto disjoint;
3624 for (i = 0; i < bmap2->n_ineq; ++i) {
3625 int reduced;
3626 reduced = reduced_using_equalities(v->block.data,
3627 bmap2->ineq[i], bmap1, elim);
3628 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3629 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3630 goto disjoint;
3632 compute_elimination_index(bmap2, elim);
3633 for (i = 0; i < bmap1->n_ineq; ++i) {
3634 int reduced;
3635 reduced = reduced_using_equalities(v->block.data,
3636 bmap1->ineq[i], bmap2, elim);
3637 if (reduced && isl_int_is_neg(v->block.data[0]) &&
3638 isl_seq_first_non_zero(v->block.data + 1, total) == -1)
3639 goto disjoint;
3641 isl_vec_free(v);
3642 free(elim);
3643 return isl_bool_false;
3644 disjoint:
3645 isl_vec_free(v);
3646 free(elim);
3647 return isl_bool_true;
3648 error:
3649 isl_vec_free(v);
3650 free(elim);
3651 return isl_bool_error;
3654 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1,
3655 __isl_keep isl_basic_set *bset2)
3657 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1),
3658 bset_to_bmap(bset2));
3661 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3663 static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2,
3664 isl_bool (*test)(__isl_keep isl_basic_map *bmap1,
3665 __isl_keep isl_basic_map *bmap2))
3667 int i, j;
3669 if (!map1 || !map2)
3670 return isl_bool_error;
3672 for (i = 0; i < map1->n; ++i) {
3673 for (j = 0; j < map2->n; ++j) {
3674 isl_bool d = test(map1->p[i], map2->p[j]);
3675 if (d != isl_bool_true)
3676 return d;
3680 return isl_bool_true;
3683 /* Are "map1" and "map2" obviously disjoint, based on information
3684 * that can be derived without looking at the individual basic maps?
3686 * In particular, if one of them is empty or if they live in different spaces
3687 * (ignoring parameters), then they are clearly disjoint.
3689 static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1,
3690 __isl_keep isl_map *map2)
3692 isl_bool disjoint;
3693 isl_bool match;
3695 if (!map1 || !map2)
3696 return isl_bool_error;
3698 disjoint = isl_map_plain_is_empty(map1);
3699 if (disjoint < 0 || disjoint)
3700 return disjoint;
3702 disjoint = isl_map_plain_is_empty(map2);
3703 if (disjoint < 0 || disjoint)
3704 return disjoint;
3706 match = isl_space_tuple_is_equal(map1->dim, isl_dim_in,
3707 map2->dim, isl_dim_in);
3708 if (match < 0 || !match)
3709 return match < 0 ? isl_bool_error : isl_bool_true;
3711 match = isl_space_tuple_is_equal(map1->dim, isl_dim_out,
3712 map2->dim, isl_dim_out);
3713 if (match < 0 || !match)
3714 return match < 0 ? isl_bool_error : isl_bool_true;
3716 return isl_bool_false;
3719 /* Are "map1" and "map2" obviously disjoint?
3721 * If one of them is empty or if they live in different spaces (ignoring
3722 * parameters), then they are clearly disjoint.
3723 * This is checked by isl_map_plain_is_disjoint_global.
3725 * If they have different parameters, then we skip any further tests.
3727 * If they are obviously equal, but not obviously empty, then we will
3728 * not be able to detect if they are disjoint.
3730 * Otherwise we check if each basic map in "map1" is obviously disjoint
3731 * from each basic map in "map2".
3733 isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
3734 __isl_keep isl_map *map2)
3736 isl_bool disjoint;
3737 isl_bool intersect;
3738 isl_bool match;
3740 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3741 if (disjoint < 0 || disjoint)
3742 return disjoint;
3744 match = isl_map_has_equal_params(map1, map2);
3745 if (match < 0 || !match)
3746 return match < 0 ? isl_bool_error : isl_bool_false;
3748 intersect = isl_map_plain_is_equal(map1, map2);
3749 if (intersect < 0 || intersect)
3750 return intersect < 0 ? isl_bool_error : isl_bool_false;
3752 return all_pairs(map1, map2, &isl_basic_map_plain_is_disjoint);
3755 /* Are "map1" and "map2" disjoint?
3756 * The parameters are assumed to have been aligned.
3758 * In particular, check whether all pairs of basic maps are disjoint.
3760 static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1,
3761 __isl_keep isl_map *map2)
3763 return all_pairs(map1, map2, &isl_basic_map_is_disjoint);
3766 /* Are "map1" and "map2" disjoint?
3768 * They are disjoint if they are "obviously disjoint" or if one of them
3769 * is empty. Otherwise, they are not disjoint if one of them is universal.
3770 * If the two inputs are (obviously) equal and not empty, then they are
3771 * not disjoint.
3772 * If none of these cases apply, then check if all pairs of basic maps
3773 * are disjoint after aligning the parameters.
3775 isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
3777 isl_bool disjoint;
3778 isl_bool intersect;
3780 disjoint = isl_map_plain_is_disjoint_global(map1, map2);
3781 if (disjoint < 0 || disjoint)
3782 return disjoint;
3784 disjoint = isl_map_is_empty(map1);
3785 if (disjoint < 0 || disjoint)
3786 return disjoint;
3788 disjoint = isl_map_is_empty(map2);
3789 if (disjoint < 0 || disjoint)
3790 return disjoint;
3792 intersect = isl_map_plain_is_universe(map1);
3793 if (intersect < 0 || intersect)
3794 return intersect < 0 ? isl_bool_error : isl_bool_false;
3796 intersect = isl_map_plain_is_universe(map2);
3797 if (intersect < 0 || intersect)
3798 return intersect < 0 ? isl_bool_error : isl_bool_false;
3800 intersect = isl_map_plain_is_equal(map1, map2);
3801 if (intersect < 0 || intersect)
3802 return isl_bool_not(intersect);
3804 return isl_map_align_params_map_map_and_test(map1, map2,
3805 &isl_map_is_disjoint_aligned);
3808 /* Are "bmap1" and "bmap2" disjoint?
3810 * They are disjoint if they are "obviously disjoint" or if one of them
3811 * is empty. Otherwise, they are not disjoint if one of them is universal.
3812 * If none of these cases apply, we compute the intersection and see if
3813 * the result is empty.
3815 isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1,
3816 __isl_keep isl_basic_map *bmap2)
3818 isl_bool disjoint;
3819 isl_bool intersect;
3820 isl_basic_map *test;
3822 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
3823 if (disjoint < 0 || disjoint)
3824 return disjoint;
3826 disjoint = isl_basic_map_is_empty(bmap1);
3827 if (disjoint < 0 || disjoint)
3828 return disjoint;
3830 disjoint = isl_basic_map_is_empty(bmap2);
3831 if (disjoint < 0 || disjoint)
3832 return disjoint;
3834 intersect = isl_basic_map_plain_is_universe(bmap1);
3835 if (intersect < 0 || intersect)
3836 return intersect < 0 ? isl_bool_error : isl_bool_false;
3838 intersect = isl_basic_map_plain_is_universe(bmap2);
3839 if (intersect < 0 || intersect)
3840 return intersect < 0 ? isl_bool_error : isl_bool_false;
3842 test = isl_basic_map_intersect(isl_basic_map_copy(bmap1),
3843 isl_basic_map_copy(bmap2));
3844 disjoint = isl_basic_map_is_empty(test);
3845 isl_basic_map_free(test);
3847 return disjoint;
3850 /* Are "bset1" and "bset2" disjoint?
3852 isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1,
3853 __isl_keep isl_basic_set *bset2)
3855 return isl_basic_map_is_disjoint(bset1, bset2);
3858 isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
3859 __isl_keep isl_set *set2)
3861 return isl_map_plain_is_disjoint(set_to_map(set1), set_to_map(set2));
3864 /* Are "set1" and "set2" disjoint?
3866 isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
3868 return isl_map_is_disjoint(set1, set2);
3871 /* Is "v" equal to 0, 1 or -1?
3873 static int is_zero_or_one(isl_int v)
3875 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
3878 /* Check if we can combine a given div with lower bound l and upper
3879 * bound u with some other div and if so return that other div.
3880 * Otherwise return -1.
3882 * We first check that
3883 * - the bounds are opposites of each other (except for the constant
3884 * term)
3885 * - the bounds do not reference any other div
3886 * - no div is defined in terms of this div
3888 * Let m be the size of the range allowed on the div by the bounds.
3889 * That is, the bounds are of the form
3891 * e <= a <= e + m - 1
3893 * with e some expression in the other variables.
3894 * We look for another div b such that no third div is defined in terms
3895 * of this second div b and such that in any constraint that contains
3896 * a (except for the given lower and upper bound), also contains b
3897 * with a coefficient that is m times that of b.
3898 * That is, all constraints (except for the lower and upper bound)
3899 * are of the form
3901 * e + f (a + m b) >= 0
3903 * Furthermore, in the constraints that only contain b, the coefficient
3904 * of b should be equal to 1 or -1.
3905 * If so, we return b so that "a + m b" can be replaced by
3906 * a single div "c = a + m b".
3908 static int div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs,
3909 unsigned div, unsigned l, unsigned u)
3911 int i, j;
3912 unsigned dim;
3913 int coalesce = -1;
3915 if (bmap->n_div <= 1)
3916 return -1;
3917 dim = isl_space_dim(bmap->dim, isl_dim_all);
3918 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
3919 return -1;
3920 if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
3921 bmap->n_div - div - 1) != -1)
3922 return -1;
3923 if (!isl_seq_is_neg(bmap->ineq[l] + 1, bmap->ineq[u] + 1,
3924 dim + bmap->n_div))
3925 return -1;
3927 for (i = 0; i < bmap->n_div; ++i) {
3928 if (isl_int_is_zero(bmap->div[i][0]))
3929 continue;
3930 if (!isl_int_is_zero(bmap->div[i][1 + 1 + dim + div]))
3931 return -1;
3934 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3935 if (isl_int_is_neg(bmap->ineq[l][0])) {
3936 isl_int_sub(bmap->ineq[l][0],
3937 bmap->ineq[l][0], bmap->ineq[u][0]);
3938 bmap = isl_basic_map_copy(bmap);
3939 bmap = isl_basic_map_set_to_empty(bmap);
3940 isl_basic_map_free(bmap);
3941 return -1;
3943 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3944 for (i = 0; i < bmap->n_div; ++i) {
3945 if (i == div)
3946 continue;
3947 if (!pairs[i])
3948 continue;
3949 for (j = 0; j < bmap->n_div; ++j) {
3950 if (isl_int_is_zero(bmap->div[j][0]))
3951 continue;
3952 if (!isl_int_is_zero(bmap->div[j][1 + 1 + dim + i]))
3953 break;
3955 if (j < bmap->n_div)
3956 continue;
3957 for (j = 0; j < bmap->n_ineq; ++j) {
3958 int valid;
3959 if (j == l || j == u)
3960 continue;
3961 if (isl_int_is_zero(bmap->ineq[j][1 + dim + div])) {
3962 if (is_zero_or_one(bmap->ineq[j][1 + dim + i]))
3963 continue;
3964 break;
3966 if (isl_int_is_zero(bmap->ineq[j][1 + dim + i]))
3967 break;
3968 isl_int_mul(bmap->ineq[j][1 + dim + div],
3969 bmap->ineq[j][1 + dim + div],
3970 bmap->ineq[l][0]);
3971 valid = isl_int_eq(bmap->ineq[j][1 + dim + div],
3972 bmap->ineq[j][1 + dim + i]);
3973 isl_int_divexact(bmap->ineq[j][1 + dim + div],
3974 bmap->ineq[j][1 + dim + div],
3975 bmap->ineq[l][0]);
3976 if (!valid)
3977 break;
3979 if (j < bmap->n_ineq)
3980 continue;
3981 coalesce = i;
3982 break;
3984 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
3985 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
3986 return coalesce;
3989 /* Internal data structure used during the construction and/or evaluation of
3990 * an inequality that ensures that a pair of bounds always allows
3991 * for an integer value.
3993 * "tab" is the tableau in which the inequality is evaluated. It may
3994 * be NULL until it is actually needed.
3995 * "v" contains the inequality coefficients.
3996 * "g", "fl" and "fu" are temporary scalars used during the construction and
3997 * evaluation.
3999 struct test_ineq_data {
4000 struct isl_tab *tab;
4001 isl_vec *v;
4002 isl_int g;
4003 isl_int fl;
4004 isl_int fu;
4007 /* Free all the memory allocated by the fields of "data".
4009 static void test_ineq_data_clear(struct test_ineq_data *data)
4011 isl_tab_free(data->tab);
4012 isl_vec_free(data->v);
4013 isl_int_clear(data->g);
4014 isl_int_clear(data->fl);
4015 isl_int_clear(data->fu);
4018 /* Is the inequality stored in data->v satisfied by "bmap"?
4019 * That is, does it only attain non-negative values?
4020 * data->tab is a tableau corresponding to "bmap".
4022 static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap,
4023 struct test_ineq_data *data)
4025 isl_ctx *ctx;
4026 enum isl_lp_result res;
4028 ctx = isl_basic_map_get_ctx(bmap);
4029 if (!data->tab)
4030 data->tab = isl_tab_from_basic_map(bmap, 0);
4031 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
4032 if (res == isl_lp_error)
4033 return isl_bool_error;
4034 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
4037 /* Given a lower and an upper bound on div i, do they always allow
4038 * for an integer value of the given div?
4039 * Determine this property by constructing an inequality
4040 * such that the property is guaranteed when the inequality is nonnegative.
4041 * The lower bound is inequality l, while the upper bound is inequality u.
4042 * The constructed inequality is stored in data->v.
4044 * Let the upper bound be
4046 * -n_u a + e_u >= 0
4048 * and the lower bound
4050 * n_l a + e_l >= 0
4052 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4053 * We have
4055 * - f_u e_l <= f_u f_l g a <= f_l e_u
4057 * Since all variables are integer valued, this is equivalent to
4059 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4061 * If this interval is at least f_u f_l g, then it contains at least
4062 * one integer value for a.
4063 * That is, the test constraint is
4065 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4067 * or
4069 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4071 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4072 * then the constraint can be scaled down by a factor g',
4073 * with the constant term replaced by
4074 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4075 * Note that the result of applying Fourier-Motzkin to this pair
4076 * of constraints is
4078 * f_l e_u + f_u e_l >= 0
4080 * If the constant term of the scaled down version of this constraint,
4081 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4082 * term of the scaled down test constraint, then the test constraint
4083 * is known to hold and no explicit evaluation is required.
4084 * This is essentially the Omega test.
4086 * If the test constraint consists of only a constant term, then
4087 * it is sufficient to look at the sign of this constant term.
4089 static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i,
4090 int l, int u, struct test_ineq_data *data)
4092 unsigned offset, n_div;
4093 offset = isl_basic_map_offset(bmap, isl_dim_div);
4094 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4096 isl_int_gcd(data->g,
4097 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
4098 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
4099 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
4100 isl_int_neg(data->fu, data->fu);
4101 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
4102 data->fu, bmap->ineq[l], offset + n_div);
4103 isl_int_mul(data->g, data->g, data->fl);
4104 isl_int_mul(data->g, data->g, data->fu);
4105 isl_int_sub(data->g, data->g, data->fl);
4106 isl_int_sub(data->g, data->g, data->fu);
4107 isl_int_add_ui(data->g, data->g, 1);
4108 isl_int_sub(data->fl, data->v->el[0], data->g);
4110 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
4111 if (isl_int_is_zero(data->g))
4112 return isl_int_is_nonneg(data->fl);
4113 if (isl_int_is_one(data->g)) {
4114 isl_int_set(data->v->el[0], data->fl);
4115 return test_ineq_is_satisfied(bmap, data);
4117 isl_int_fdiv_q(data->fl, data->fl, data->g);
4118 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
4119 if (isl_int_eq(data->fl, data->v->el[0]))
4120 return isl_bool_true;
4121 isl_int_set(data->v->el[0], data->fl);
4122 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
4123 offset - 1 + n_div);
4125 return test_ineq_is_satisfied(bmap, data);
4128 /* Remove more kinds of divs that are not strictly needed.
4129 * In particular, if all pairs of lower and upper bounds on a div
4130 * are such that they allow at least one integer value of the div,
4131 * then we can eliminate the div using Fourier-Motzkin without
4132 * introducing any spurious solutions.
4134 * If at least one of the two constraints has a unit coefficient for the div,
4135 * then the presence of such a value is guaranteed so there is no need to check.
4136 * In particular, the value attained by the bound with unit coefficient
4137 * can serve as this intermediate value.
4139 static __isl_give isl_basic_map *drop_more_redundant_divs(
4140 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
4142 isl_ctx *ctx;
4143 struct test_ineq_data data = { NULL, NULL };
4144 unsigned off, n_div;
4145 int remove = -1;
4147 isl_int_init(data.g);
4148 isl_int_init(data.fl);
4149 isl_int_init(data.fu);
4151 if (!bmap)
4152 goto error;
4154 ctx = isl_basic_map_get_ctx(bmap);
4155 off = isl_basic_map_offset(bmap, isl_dim_div);
4156 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4157 data.v = isl_vec_alloc(ctx, off + n_div);
4158 if (!data.v)
4159 goto error;
4161 while (n > 0) {
4162 int i, l, u;
4163 int best = -1;
4164 isl_bool has_int;
4166 for (i = 0; i < n_div; ++i) {
4167 if (!pairs[i])
4168 continue;
4169 if (best >= 0 && pairs[best] <= pairs[i])
4170 continue;
4171 best = i;
4174 i = best;
4175 for (l = 0; l < bmap->n_ineq; ++l) {
4176 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
4177 continue;
4178 if (isl_int_is_one(bmap->ineq[l][off + i]))
4179 continue;
4180 for (u = 0; u < bmap->n_ineq; ++u) {
4181 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
4182 continue;
4183 if (isl_int_is_negone(bmap->ineq[u][off + i]))
4184 continue;
4185 has_int = int_between_bounds(bmap, i, l, u,
4186 &data);
4187 if (has_int < 0)
4188 goto error;
4189 if (data.tab && data.tab->empty)
4190 break;
4191 if (!has_int)
4192 break;
4194 if (u < bmap->n_ineq)
4195 break;
4197 if (data.tab && data.tab->empty) {
4198 bmap = isl_basic_map_set_to_empty(bmap);
4199 break;
4201 if (l == bmap->n_ineq) {
4202 remove = i;
4203 break;
4205 pairs[i] = 0;
4206 --n;
4209 test_ineq_data_clear(&data);
4211 free(pairs);
4213 if (remove < 0)
4214 return bmap;
4216 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
4217 return isl_basic_map_drop_redundant_divs(bmap);
4218 error:
4219 free(pairs);
4220 isl_basic_map_free(bmap);
4221 test_ineq_data_clear(&data);
4222 return NULL;
4225 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4226 * and the upper bound u, div1 always occurs together with div2 in the form
4227 * (div1 + m div2), where m is the constant range on the variable div1
4228 * allowed by l and u, replace the pair div1 and div2 by a single
4229 * div that is equal to div1 + m div2.
4231 * The new div will appear in the location that contains div2.
4232 * We need to modify all constraints that contain
4233 * div2 = (div - div1) / m
4234 * The coefficient of div2 is known to be equal to 1 or -1.
4235 * (If a constraint does not contain div2, it will also not contain div1.)
4236 * If the constraint also contains div1, then we know they appear
4237 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4238 * i.e., the coefficient of div is f.
4240 * Otherwise, we first need to introduce div1 into the constraint.
4241 * Let l be
4243 * div1 + f >=0
4245 * and u
4247 * -div1 + f' >= 0
4249 * A lower bound on div2
4251 * div2 + t >= 0
4253 * can be replaced by
4255 * m div2 + div1 + m t + f >= 0
4257 * An upper bound
4259 * -div2 + t >= 0
4261 * can be replaced by
4263 * -(m div2 + div1) + m t + f' >= 0
4265 * These constraint are those that we would obtain from eliminating
4266 * div1 using Fourier-Motzkin.
4268 * After all constraints have been modified, we drop the lower and upper
4269 * bound and then drop div1.
4270 * Since the new div is only placed in the same location that used
4271 * to store div2, but otherwise has a different meaning, any possible
4272 * explicit representation of the original div2 is removed.
4274 static __isl_give isl_basic_map *coalesce_divs(__isl_take isl_basic_map *bmap,
4275 unsigned div1, unsigned div2, unsigned l, unsigned u)
4277 isl_ctx *ctx;
4278 isl_int m;
4279 unsigned dim, total;
4280 int i;
4282 ctx = isl_basic_map_get_ctx(bmap);
4284 dim = isl_space_dim(bmap->dim, isl_dim_all);
4285 total = 1 + dim + bmap->n_div;
4287 isl_int_init(m);
4288 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
4289 isl_int_add_ui(m, m, 1);
4291 for (i = 0; i < bmap->n_ineq; ++i) {
4292 if (i == l || i == u)
4293 continue;
4294 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div2]))
4295 continue;
4296 if (isl_int_is_zero(bmap->ineq[i][1 + dim + div1])) {
4297 if (isl_int_is_pos(bmap->ineq[i][1 + dim + div2]))
4298 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4299 ctx->one, bmap->ineq[l], total);
4300 else
4301 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
4302 ctx->one, bmap->ineq[u], total);
4304 isl_int_set(bmap->ineq[i][1 + dim + div2],
4305 bmap->ineq[i][1 + dim + div1]);
4306 isl_int_set_si(bmap->ineq[i][1 + dim + div1], 0);
4309 isl_int_clear(m);
4310 if (l > u) {
4311 isl_basic_map_drop_inequality(bmap, l);
4312 isl_basic_map_drop_inequality(bmap, u);
4313 } else {
4314 isl_basic_map_drop_inequality(bmap, u);
4315 isl_basic_map_drop_inequality(bmap, l);
4317 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
4318 bmap = isl_basic_map_drop_div(bmap, div1);
4319 return bmap;
4322 /* First check if we can coalesce any pair of divs and
4323 * then continue with dropping more redundant divs.
4325 * We loop over all pairs of lower and upper bounds on a div
4326 * with coefficient 1 and -1, respectively, check if there
4327 * is any other div "c" with which we can coalesce the div
4328 * and if so, perform the coalescing.
4330 static __isl_give isl_basic_map *coalesce_or_drop_more_redundant_divs(
4331 __isl_take isl_basic_map *bmap, int *pairs, int n)
4333 int i, l, u;
4334 unsigned dim;
4336 dim = isl_space_dim(bmap->dim, isl_dim_all);
4338 for (i = 0; i < bmap->n_div; ++i) {
4339 if (!pairs[i])
4340 continue;
4341 for (l = 0; l < bmap->n_ineq; ++l) {
4342 if (!isl_int_is_one(bmap->ineq[l][1 + dim + i]))
4343 continue;
4344 for (u = 0; u < bmap->n_ineq; ++u) {
4345 int c;
4347 if (!isl_int_is_negone(bmap->ineq[u][1+dim+i]))
4348 continue;
4349 c = div_find_coalesce(bmap, pairs, i, l, u);
4350 if (c < 0)
4351 continue;
4352 free(pairs);
4353 bmap = coalesce_divs(bmap, i, c, l, u);
4354 return isl_basic_map_drop_redundant_divs(bmap);
4359 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
4360 free(pairs);
4361 return bmap;
4364 return drop_more_redundant_divs(bmap, pairs, n);
4367 /* Are the "n" coefficients starting at "first" of inequality constraints
4368 * "i" and "j" of "bmap" equal to each other?
4370 static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
4371 int first, int n)
4373 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4376 /* Are the "n" coefficients starting at "first" of inequality constraints
4377 * "i" and "j" of "bmap" opposite to each other?
4379 static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
4380 int first, int n)
4382 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
4385 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4386 * apart from the constant term?
4388 static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
4390 unsigned total;
4392 total = isl_basic_map_dim(bmap, isl_dim_all);
4393 return is_opposite_part(bmap, i, j, 1, total);
4396 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4397 * apart from the constant term and the coefficient at position "pos"?
4399 static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j,
4400 int pos)
4402 unsigned total;
4404 total = isl_basic_map_dim(bmap, isl_dim_all);
4405 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
4406 is_parallel_part(bmap, i, j, pos + 1, total - pos);
4409 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4410 * apart from the constant term and the coefficient at position "pos"?
4412 static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j,
4413 int pos)
4415 unsigned total;
4417 total = isl_basic_map_dim(bmap, isl_dim_all);
4418 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
4419 is_opposite_part(bmap, i, j, pos + 1, total - pos);
4422 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4423 * been modified, simplying it if "simplify" is set.
4424 * Free the temporary data structure "pairs" that was associated
4425 * to the old version of "bmap".
4427 static __isl_give isl_basic_map *drop_redundant_divs_again(
4428 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
4430 if (simplify)
4431 bmap = isl_basic_map_simplify(bmap);
4432 free(pairs);
4433 return isl_basic_map_drop_redundant_divs(bmap);
4436 /* Is "div" the single unknown existentially quantified variable
4437 * in inequality constraint "ineq" of "bmap"?
4438 * "div" is known to have a non-zero coefficient in "ineq".
4440 static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq,
4441 int div)
4443 int i;
4444 unsigned n_div, o_div;
4445 isl_bool known;
4447 known = isl_basic_map_div_is_known(bmap, div);
4448 if (known < 0 || known)
4449 return isl_bool_not(known);
4450 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4451 if (n_div == 1)
4452 return isl_bool_true;
4453 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4454 for (i = 0; i < n_div; ++i) {
4455 isl_bool known;
4457 if (i == div)
4458 continue;
4459 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
4460 continue;
4461 known = isl_basic_map_div_is_known(bmap, i);
4462 if (known < 0 || !known)
4463 return known;
4466 return isl_bool_true;
4469 /* Does integer division "div" have coefficient 1 in inequality constraint
4470 * "ineq" of "map"?
4472 static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
4474 unsigned o_div;
4476 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4477 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
4478 return isl_bool_true;
4480 return isl_bool_false;
4483 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4484 * then try and drop redundant divs again,
4485 * freeing the temporary data structure "pairs" that was associated
4486 * to the old version of "bmap".
4488 static __isl_give isl_basic_map *set_eq_and_try_again(
4489 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
4491 bmap = isl_basic_map_cow(bmap);
4492 isl_basic_map_inequality_to_equality(bmap, ineq);
4493 return drop_redundant_divs_again(bmap, pairs, 1);
4496 /* Drop the integer division at position "div", along with the two
4497 * inequality constraints "ineq1" and "ineq2" in which it appears
4498 * from "bmap" and then try and drop redundant divs again,
4499 * freeing the temporary data structure "pairs" that was associated
4500 * to the old version of "bmap".
4502 static __isl_give isl_basic_map *drop_div_and_try_again(
4503 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
4504 __isl_take int *pairs)
4506 if (ineq1 > ineq2) {
4507 isl_basic_map_drop_inequality(bmap, ineq1);
4508 isl_basic_map_drop_inequality(bmap, ineq2);
4509 } else {
4510 isl_basic_map_drop_inequality(bmap, ineq2);
4511 isl_basic_map_drop_inequality(bmap, ineq1);
4513 bmap = isl_basic_map_drop_div(bmap, div);
4514 return drop_redundant_divs_again(bmap, pairs, 0);
4517 /* Given two inequality constraints
4519 * f(x) + n d + c >= 0, (ineq)
4521 * with d the variable at position "pos", and
4523 * f(x) + c0 >= 0, (lower)
4525 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4526 * determined by the first constraint.
4527 * That is, store
4529 * ceil((c0 - c)/n)
4531 * in *l.
4533 static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap,
4534 int ineq, int lower, int pos, isl_int *l)
4536 isl_int_neg(*l, bmap->ineq[ineq][0]);
4537 isl_int_add(*l, *l, bmap->ineq[lower][0]);
4538 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
4541 /* Given two inequality constraints
4543 * f(x) + n d + c >= 0, (ineq)
4545 * with d the variable at position "pos", and
4547 * -f(x) - c0 >= 0, (upper)
4549 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4550 * determined by the first constraint.
4551 * That is, store
4553 * ceil((-c1 - c)/n)
4555 * in *u.
4557 static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap,
4558 int ineq, int upper, int pos, isl_int *u)
4560 isl_int_neg(*u, bmap->ineq[ineq][0]);
4561 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
4562 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
4565 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4566 * does the corresponding lower bound have a fixed value in "bmap"?
4568 * In particular, "ineq" is of the form
4570 * f(x) + n d + c >= 0
4572 * with n > 0, c the constant term and
4573 * d the existentially quantified variable "div".
4574 * That is, the lower bound is
4576 * ceil((-f(x) - c)/n)
4578 * Look for a pair of constraints
4580 * f(x) + c0 >= 0
4581 * -f(x) + c1 >= 0
4583 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4584 * That is, check that
4586 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4588 * If so, return the index of inequality f(x) + c0 >= 0.
4589 * Otherwise, return bmap->n_ineq.
4590 * Return -1 on error.
4592 static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
4594 int i;
4595 int lower = -1, upper = -1;
4596 unsigned o_div;
4597 isl_int l, u;
4598 int equal;
4600 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4601 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
4602 isl_bool par, opp;
4604 if (i == ineq)
4605 continue;
4606 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
4607 continue;
4608 par = isl_bool_false;
4609 if (lower < 0)
4610 par = is_parallel_except(bmap, ineq, i, o_div + div);
4611 if (par < 0)
4612 return -1;
4613 if (par) {
4614 lower = i;
4615 continue;
4617 opp = isl_bool_false;
4618 if (upper < 0)
4619 opp = is_opposite_except(bmap, ineq, i, o_div + div);
4620 if (opp < 0)
4621 return -1;
4622 if (opp)
4623 upper = i;
4626 if (lower < 0 || upper < 0)
4627 return bmap->n_ineq;
4629 isl_int_init(l);
4630 isl_int_init(u);
4632 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
4633 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
4635 equal = isl_int_eq(l, u);
4637 isl_int_clear(l);
4638 isl_int_clear(u);
4640 return equal ? lower : bmap->n_ineq;
4643 /* Given a lower bound constraint "ineq" on the existentially quantified
4644 * variable "div", such that the corresponding lower bound has
4645 * a fixed value in "bmap", assign this fixed value to the variable and
4646 * then try and drop redundant divs again,
4647 * freeing the temporary data structure "pairs" that was associated
4648 * to the old version of "bmap".
4649 * "lower" determines the constant value for the lower bound.
4651 * In particular, "ineq" is of the form
4653 * f(x) + n d + c >= 0,
4655 * while "lower" is of the form
4657 * f(x) + c0 >= 0
4659 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4660 * is ceil((c0 - c)/n).
4662 static __isl_give isl_basic_map *fix_cst_lower(__isl_take isl_basic_map *bmap,
4663 int div, int ineq, int lower, int *pairs)
4665 isl_int c;
4666 unsigned o_div;
4668 isl_int_init(c);
4670 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4671 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
4672 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
4673 free(pairs);
4675 isl_int_clear(c);
4677 return isl_basic_map_drop_redundant_divs(bmap);
4680 /* Remove divs that are not strictly needed based on the inequality
4681 * constraints.
4682 * In particular, if a div only occurs positively (or negatively)
4683 * in constraints, then it can simply be dropped.
4684 * Also, if a div occurs in only two constraints and if moreover
4685 * those two constraints are opposite to each other, except for the constant
4686 * term and if the sum of the constant terms is such that for any value
4687 * of the other values, there is always at least one integer value of the
4688 * div, i.e., if one plus this sum is greater than or equal to
4689 * the (absolute value) of the coefficient of the div in the constraints,
4690 * then we can also simply drop the div.
4692 * If an existentially quantified variable does not have an explicit
4693 * representation, appears in only a single lower bound that does not
4694 * involve any other such existentially quantified variables and appears
4695 * in this lower bound with coefficient 1,
4696 * then fix the variable to the value of the lower bound. That is,
4697 * turn the inequality into an equality.
4698 * If for any value of the other variables, there is any value
4699 * for the existentially quantified variable satisfying the constraints,
4700 * then this lower bound also satisfies the constraints.
4701 * It is therefore safe to pick this lower bound.
4703 * The same reasoning holds even if the coefficient is not one.
4704 * However, fixing the variable to the value of the lower bound may
4705 * in general introduce an extra integer division, in which case
4706 * it may be better to pick another value.
4707 * If this integer division has a known constant value, then plugging
4708 * in this constant value removes the existentially quantified variable
4709 * completely. In particular, if the lower bound is of the form
4710 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4711 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4712 * then the existentially quantified variable can be assigned this
4713 * shared value.
4715 * We skip divs that appear in equalities or in the definition of other divs.
4716 * Divs that appear in the definition of other divs usually occur in at least
4717 * 4 constraints, but the constraints may have been simplified.
4719 * If any divs are left after these simple checks then we move on
4720 * to more complicated cases in drop_more_redundant_divs.
4722 static __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs_ineq(
4723 __isl_take isl_basic_map *bmap)
4725 int i, j;
4726 unsigned off;
4727 int *pairs = NULL;
4728 int n = 0;
4729 int n_ineq;
4731 if (!bmap)
4732 goto error;
4733 if (bmap->n_div == 0)
4734 return bmap;
4736 off = isl_space_dim(bmap->dim, isl_dim_all);
4737 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
4738 if (!pairs)
4739 goto error;
4741 n_ineq = isl_basic_map_n_inequality(bmap);
4742 for (i = 0; i < bmap->n_div; ++i) {
4743 int pos, neg;
4744 int last_pos, last_neg;
4745 int redundant;
4746 int defined;
4747 isl_bool opp, set_div;
4749 defined = !isl_int_is_zero(bmap->div[i][0]);
4750 for (j = i; j < bmap->n_div; ++j)
4751 if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
4752 break;
4753 if (j < bmap->n_div)
4754 continue;
4755 for (j = 0; j < bmap->n_eq; ++j)
4756 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
4757 break;
4758 if (j < bmap->n_eq)
4759 continue;
4760 ++n;
4761 pos = neg = 0;
4762 for (j = 0; j < bmap->n_ineq; ++j) {
4763 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
4764 last_pos = j;
4765 ++pos;
4767 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
4768 last_neg = j;
4769 ++neg;
4772 pairs[i] = pos * neg;
4773 if (pairs[i] == 0) {
4774 for (j = bmap->n_ineq - 1; j >= 0; --j)
4775 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
4776 isl_basic_map_drop_inequality(bmap, j);
4777 bmap = isl_basic_map_drop_div(bmap, i);
4778 return drop_redundant_divs_again(bmap, pairs, 0);
4780 if (pairs[i] != 1)
4781 opp = isl_bool_false;
4782 else
4783 opp = is_opposite(bmap, last_pos, last_neg);
4784 if (opp < 0)
4785 goto error;
4786 if (!opp) {
4787 int lower;
4788 isl_bool single, one;
4790 if (pos != 1)
4791 continue;
4792 single = single_unknown(bmap, last_pos, i);
4793 if (single < 0)
4794 goto error;
4795 if (!single)
4796 continue;
4797 one = has_coef_one(bmap, i, last_pos);
4798 if (one < 0)
4799 goto error;
4800 if (one)
4801 return set_eq_and_try_again(bmap, last_pos,
4802 pairs);
4803 lower = lower_bound_is_cst(bmap, i, last_pos);
4804 if (lower < 0)
4805 goto error;
4806 if (lower < n_ineq)
4807 return fix_cst_lower(bmap, i, last_pos, lower,
4808 pairs);
4809 continue;
4812 isl_int_add(bmap->ineq[last_pos][0],
4813 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4814 isl_int_add_ui(bmap->ineq[last_pos][0],
4815 bmap->ineq[last_pos][0], 1);
4816 redundant = isl_int_ge(bmap->ineq[last_pos][0],
4817 bmap->ineq[last_pos][1+off+i]);
4818 isl_int_sub_ui(bmap->ineq[last_pos][0],
4819 bmap->ineq[last_pos][0], 1);
4820 isl_int_sub(bmap->ineq[last_pos][0],
4821 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
4822 if (redundant)
4823 return drop_div_and_try_again(bmap, i,
4824 last_pos, last_neg, pairs);
4825 if (defined)
4826 set_div = isl_bool_false;
4827 else
4828 set_div = ok_to_set_div_from_bound(bmap, i, last_pos);
4829 if (set_div < 0)
4830 return isl_basic_map_free(bmap);
4831 if (set_div) {
4832 bmap = set_div_from_lower_bound(bmap, i, last_pos);
4833 return drop_redundant_divs_again(bmap, pairs, 1);
4835 pairs[i] = 0;
4836 --n;
4839 if (n > 0)
4840 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
4842 free(pairs);
4843 return bmap;
4844 error:
4845 free(pairs);
4846 isl_basic_map_free(bmap);
4847 return NULL;
4850 /* Consider the coefficients at "c" as a row vector and replace
4851 * them with their product with "T". "T" is assumed to be a square matrix.
4853 static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
4855 int n;
4856 isl_ctx *ctx;
4857 isl_vec *v;
4859 if (!T)
4860 return isl_stat_error;
4861 n = isl_mat_rows(T);
4862 if (isl_seq_first_non_zero(c, n) == -1)
4863 return isl_stat_ok;
4864 ctx = isl_mat_get_ctx(T);
4865 v = isl_vec_alloc(ctx, n);
4866 if (!v)
4867 return isl_stat_error;
4868 isl_seq_swp_or_cpy(v->el, c, n);
4869 v = isl_vec_mat_product(v, isl_mat_copy(T));
4870 if (!v)
4871 return isl_stat_error;
4872 isl_seq_swp_or_cpy(c, v->el, n);
4873 isl_vec_free(v);
4875 return isl_stat_ok;
4878 /* Plug in T for the variables in "bmap" starting at "pos".
4879 * T is a linear unimodular matrix, i.e., without constant term.
4881 static __isl_give isl_basic_map *isl_basic_map_preimage_vars(
4882 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
4884 int i;
4885 unsigned n;
4887 bmap = isl_basic_map_cow(bmap);
4888 if (!bmap || !T)
4889 goto error;
4891 n = isl_mat_cols(T);
4892 if (n != isl_mat_rows(T))
4893 isl_die(isl_mat_get_ctx(T), isl_error_invalid,
4894 "expecting square matrix", goto error);
4896 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n) < 0)
4897 goto error;
4899 for (i = 0; i < bmap->n_eq; ++i)
4900 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
4901 goto error;
4902 for (i = 0; i < bmap->n_ineq; ++i)
4903 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
4904 goto error;
4905 for (i = 0; i < bmap->n_div; ++i) {
4906 if (isl_basic_map_div_is_marked_unknown(bmap, i))
4907 continue;
4908 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
4909 goto error;
4912 isl_mat_free(T);
4913 return bmap;
4914 error:
4915 isl_basic_map_free(bmap);
4916 isl_mat_free(T);
4917 return NULL;
4920 /* Remove divs that are not strictly needed.
4922 * First look for an equality constraint involving two or more
4923 * existentially quantified variables without an explicit
4924 * representation. Replace the combination that appears
4925 * in the equality constraint by a single existentially quantified
4926 * variable such that the equality can be used to derive
4927 * an explicit representation for the variable.
4928 * If there are no more such equality constraints, then continue
4929 * with isl_basic_map_drop_redundant_divs_ineq.
4931 * In particular, if the equality constraint is of the form
4933 * f(x) + \sum_i c_i a_i = 0
4935 * with a_i existentially quantified variable without explicit
4936 * representation, then apply a transformation on the existentially
4937 * quantified variables to turn the constraint into
4939 * f(x) + g a_1' = 0
4941 * with g the gcd of the c_i.
4942 * In order to easily identify which existentially quantified variables
4943 * have a complete explicit representation, i.e., without being defined
4944 * in terms of other existentially quantified variables without
4945 * an explicit representation, the existentially quantified variables
4946 * are first sorted.
4948 * The variable transformation is computed by extending the row
4949 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
4951 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
4952 * [a_2'] [ a_2 ]
4953 * ... = U ....
4954 * [a_n'] [ a_n ]
4956 * with [c_1/g ... c_n/g] representing the first row of U.
4957 * The inverse of U is then plugged into the original constraints.
4958 * The call to isl_basic_map_simplify makes sure the explicit
4959 * representation for a_1' is extracted from the equality constraint.
4961 __isl_give isl_basic_map *isl_basic_map_drop_redundant_divs(
4962 __isl_take isl_basic_map *bmap)
4964 int first;
4965 int i;
4966 unsigned o_div, n_div;
4967 int l;
4968 isl_ctx *ctx;
4969 isl_mat *T;
4971 if (!bmap)
4972 return NULL;
4973 if (isl_basic_map_divs_known(bmap))
4974 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4975 if (bmap->n_eq == 0)
4976 return isl_basic_map_drop_redundant_divs_ineq(bmap);
4977 bmap = isl_basic_map_sort_divs(bmap);
4978 if (!bmap)
4979 return NULL;
4981 first = isl_basic_map_first_unknown_div(bmap);
4982 if (first < 0)
4983 return isl_basic_map_free(bmap);
4985 o_div = isl_basic_map_offset(bmap, isl_dim_div);
4986 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4988 for (i = 0; i < bmap->n_eq; ++i) {
4989 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
4990 n_div - (first));
4991 if (l < 0)
4992 continue;
4993 l += first;
4994 if (isl_seq_first_non_zero(bmap->eq[i] + o_div + l + 1,
4995 n_div - (l + 1)) == -1)
4996 continue;
4997 break;
4999 if (i >= bmap->n_eq)
5000 return isl_basic_map_drop_redundant_divs_ineq(bmap);
5002 ctx = isl_basic_map_get_ctx(bmap);
5003 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
5004 if (!T)
5005 return isl_basic_map_free(bmap);
5006 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
5007 T = isl_mat_normalize_row(T, 0);
5008 T = isl_mat_unimodular_complete(T, 1);
5009 T = isl_mat_right_inverse(T);
5011 for (i = l; i < n_div; ++i)
5012 bmap = isl_basic_map_mark_div_unknown(bmap, i);
5013 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
5014 bmap = isl_basic_map_simplify(bmap);
5016 return isl_basic_map_drop_redundant_divs(bmap);
5019 /* Does "bmap" satisfy any equality that involves more than 2 variables
5020 * and/or has coefficients different from -1 and 1?
5022 static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
5024 int i;
5025 unsigned total;
5027 total = isl_basic_map_dim(bmap, isl_dim_all);
5029 for (i = 0; i < bmap->n_eq; ++i) {
5030 int j, k;
5032 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
5033 if (j < 0)
5034 continue;
5035 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5036 !isl_int_is_negone(bmap->eq[i][1 + j]))
5037 return isl_bool_true;
5039 j += 1;
5040 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5041 if (k < 0)
5042 continue;
5043 j += k;
5044 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
5045 !isl_int_is_negone(bmap->eq[i][1 + j]))
5046 return isl_bool_true;
5048 j += 1;
5049 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
5050 if (k >= 0)
5051 return isl_bool_true;
5054 return isl_bool_false;
5057 /* Remove any common factor g from the constraint coefficients in "v".
5058 * The constant term is stored in the first position and is replaced
5059 * by floor(c/g). If any common factor is removed and if this results
5060 * in a tightening of the constraint, then set *tightened.
5062 static __isl_give isl_vec *normalize_constraint(__isl_take isl_vec *v,
5063 int *tightened)
5065 isl_ctx *ctx;
5067 if (!v)
5068 return NULL;
5069 ctx = isl_vec_get_ctx(v);
5070 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
5071 if (isl_int_is_zero(ctx->normalize_gcd))
5072 return v;
5073 if (isl_int_is_one(ctx->normalize_gcd))
5074 return v;
5075 v = isl_vec_cow(v);
5076 if (!v)
5077 return NULL;
5078 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
5079 *tightened = 1;
5080 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
5081 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
5082 v->size - 1);
5083 return v;
5086 /* If "bmap" is an integer set that satisfies any equality involving
5087 * more than 2 variables and/or has coefficients different from -1 and 1,
5088 * then use variable compression to reduce the coefficients by removing
5089 * any (hidden) common factor.
5090 * In particular, apply the variable compression to each constraint,
5091 * factor out any common factor in the non-constant coefficients and
5092 * then apply the inverse of the compression.
5093 * At the end, we mark the basic map as having reduced constants.
5094 * If this flag is still set on the next invocation of this function,
5095 * then we skip the computation.
5097 * Removing a common factor may result in a tightening of some of
5098 * the constraints. If this happens, then we may end up with two
5099 * opposite inequalities that can be replaced by an equality.
5100 * We therefore call isl_basic_map_detect_inequality_pairs,
5101 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5102 * and isl_basic_map_gauss if such a pair was found.
5104 * Note that this function may leave the result in an inconsistent state.
5105 * In particular, the constraints may not be gaussed.
5106 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
5107 * for some of the test cases to pass successfully.
5108 * Any potential modification of the representation is therefore only
5109 * performed on a single copy of the basic map.
5111 __isl_give isl_basic_map *isl_basic_map_reduce_coefficients(
5112 __isl_take isl_basic_map *bmap)
5114 unsigned total;
5115 isl_bool multi;
5116 isl_ctx *ctx;
5117 isl_vec *v;
5118 isl_mat *eq, *T, *T2;
5119 int i;
5120 int tightened;
5122 if (!bmap)
5123 return NULL;
5124 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS))
5125 return bmap;
5126 if (isl_basic_map_is_rational(bmap))
5127 return bmap;
5128 if (bmap->n_eq == 0)
5129 return bmap;
5130 multi = has_multiple_var_equality(bmap);
5131 if (multi < 0)
5132 return isl_basic_map_free(bmap);
5133 if (!multi)
5134 return bmap;
5136 total = isl_basic_map_dim(bmap, isl_dim_all);
5137 ctx = isl_basic_map_get_ctx(bmap);
5138 v = isl_vec_alloc(ctx, 1 + total);
5139 if (!v)
5140 return isl_basic_map_free(bmap);
5142 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
5143 T = isl_mat_variable_compression(eq, &T2);
5144 if (!T || !T2)
5145 goto error;
5146 if (T->n_col == 0) {
5147 isl_mat_free(T);
5148 isl_mat_free(T2);
5149 isl_vec_free(v);
5150 return isl_basic_map_set_to_empty(bmap);
5153 bmap = isl_basic_map_cow(bmap);
5154 if (!bmap)
5155 goto error;
5157 tightened = 0;
5158 for (i = 0; i < bmap->n_ineq; ++i) {
5159 isl_seq_cpy(v->el, bmap->ineq[i], 1 + total);
5160 v = isl_vec_mat_product(v, isl_mat_copy(T));
5161 v = normalize_constraint(v, &tightened);
5162 v = isl_vec_mat_product(v, isl_mat_copy(T2));
5163 if (!v)
5164 goto error;
5165 isl_seq_cpy(bmap->ineq[i], v->el, 1 + total);
5168 isl_mat_free(T);
5169 isl_mat_free(T2);
5170 isl_vec_free(v);
5172 ISL_F_SET(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
5174 if (tightened) {
5175 int progress = 0;
5177 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
5178 if (progress) {
5179 bmap = eliminate_divs_eq(bmap, &progress);
5180 bmap = isl_basic_map_gauss(bmap, NULL);
5184 return bmap;
5185 error:
5186 isl_mat_free(T);
5187 isl_mat_free(T2);
5188 isl_vec_free(v);
5189 return isl_basic_map_free(bmap);
5192 /* Shift the integer division at position "div" of "bmap"
5193 * by "shift" times the variable at position "pos".
5194 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5195 * corresponds to the constant term.
5197 * That is, if the integer division has the form
5199 * floor(f(x)/d)
5201 * then replace it by
5203 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5205 __isl_give isl_basic_map *isl_basic_map_shift_div(
5206 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
5208 int i;
5209 unsigned total;
5211 if (isl_int_is_zero(shift))
5212 return bmap;
5213 if (!bmap)
5214 return NULL;
5216 total = isl_basic_map_dim(bmap, isl_dim_all);
5217 total -= isl_basic_map_dim(bmap, isl_dim_div);
5219 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
5221 for (i = 0; i < bmap->n_eq; ++i) {
5222 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
5223 continue;
5224 isl_int_submul(bmap->eq[i][pos],
5225 shift, bmap->eq[i][1 + total + div]);
5227 for (i = 0; i < bmap->n_ineq; ++i) {
5228 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
5229 continue;
5230 isl_int_submul(bmap->ineq[i][pos],
5231 shift, bmap->ineq[i][1 + total + div]);
5233 for (i = 0; i < bmap->n_div; ++i) {
5234 if (isl_int_is_zero(bmap->div[i][0]))
5235 continue;
5236 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
5237 continue;
5238 isl_int_submul(bmap->div[i][1 + pos],
5239 shift, bmap->div[i][1 + 1 + total + div]);
5242 return bmap;