Polly 23.0.0git
isl_map_simplify.c
Go to the documentation of this file.
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
6 * Copyright 2021,2023 Cerebras Systems
7 *
8 * Use of this software is governed by the MIT license
9 *
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
15 * and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
16 */
17
18#include <isl_ctx_private.h>
19#include <isl_map_private.h>
20#include "isl_equalities.h"
21#include <isl/map.h>
22#include <isl_seq.h>
23#include "isl_tab.h"
24#include <isl_space_private.h>
25#include <isl_mat_private.h>
26#include <isl_vec_private.h>
27
28#include <bset_to_bmap.c>
29#include <bset_from_bmap.c>
30#include <set_to_map.c>
31#include <set_from_map.c>
32
33/* Mark "bmap" as having one or more inequality constraints modified.
34 * If "equivalent" is set, then this modification was done based
35 * on an equality constraint already available in "bmap".
36 *
37 * Any modification may result in the constraints no longer being sorted and
38 * may also undo the effect of reduce_coefficients.
39 *
40 * A modification that uses extra information may also result
41 * in the modified constraint(s) becoming redundant or
42 * turning into an implicit equality constraint.
43 */
46{
47 if (!bmap)
48 return NULL;
51 if (equivalent)
52 return bmap;
55 return bmap;
56}
57
58static void swap_equality(__isl_keep isl_basic_map *bmap, int a, int b)
59{
60 isl_int *t = bmap->eq[a];
61 bmap->eq[a] = bmap->eq[b];
62 bmap->eq[b] = t;
63}
64
65static void swap_inequality(__isl_keep isl_basic_map *bmap, int a, int b)
66{
67 if (a != b) {
68 isl_int *t = bmap->ineq[a];
69 bmap->ineq[a] = bmap->ineq[b];
70 bmap->ineq[b] = t;
71 }
72}
73
74/* Scale down the inequality constraint "ineq" of length "len"
75 * by a factor of "f".
76 * All the coefficients, except the constant term,
77 * are assumed to be multiples of "f".
78 */
79static void scale_down_inequality(isl_int *ineq, isl_int f, unsigned len)
80{
81 isl_int_fdiv_q(ineq[0], ineq[0], f);
82 isl_seq_scale_down(ineq + 1, ineq + 1, f, len);
83}
84
85/* Scale down the inequality constraint "ineq" of length "len"
86 * by a factor of "f".
87 * All the coefficients, except the constant term,
88 * are assumed to be multiples of "f".
89 *
90 * If the factor is 0 or 1, then no scaling needs to be performed.
91 *
92 * If scaling is performed then take into account that the constraint
93 * is modified (not simply based on an equality constraint).
94 */
96 __isl_take isl_basic_map *bmap, int ineq, isl_int f, unsigned len)
97{
98 if (!bmap)
99 return NULL;
100
102 return bmap;
103
104 scale_down_inequality(bmap->ineq[ineq], f, len);
105
106 bmap = isl_basic_map_modify_inequality(bmap, 0);
107
108 return bmap;
109}
110
113{
114 int i;
115 isl_int gcd;
117
118 if (total < 0)
119 return isl_basic_map_free(bmap);
120
122 for (i = bmap->n_eq - 1; i >= 0; --i) {
123 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
124 if (isl_int_is_zero(gcd)) {
125 if (!isl_int_is_zero(bmap->eq[i][0])) {
126 bmap = isl_basic_map_set_to_empty(bmap);
127 break;
128 }
129 if (isl_basic_map_drop_equality(bmap, i) < 0)
130 goto error;
131 continue;
132 }
134 isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
135 if (isl_int_is_one(gcd))
136 continue;
137 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
138 bmap = isl_basic_map_set_to_empty(bmap);
139 break;
140 }
141 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
142 }
143
144 for (i = bmap->n_ineq - 1; i >= 0; --i) {
145 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
146 if (isl_int_is_zero(gcd)) {
147 if (isl_int_is_neg(bmap->ineq[i][0])) {
148 bmap = isl_basic_map_set_to_empty(bmap);
149 break;
150 }
151 if (isl_basic_map_drop_inequality(bmap, i) < 0)
152 goto error;
153 continue;
154 }
156 isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
157 bmap = scale_down_bmap_inequality(bmap, i, gcd, total);
158 if (!bmap)
159 goto error;
160 }
162
163 return bmap;
164error:
166 isl_basic_map_free(bmap);
167 return NULL;
168}
169
176
177/* Reduce the coefficient of the variable at position "pos"
178 * in integer division "div", such that it lies in the half-open
179 * interval (1/2,1/2], extracting any excess value from this integer division.
180 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
181 * corresponds to the constant term.
182 *
183 * That is, the integer division is of the form
184 *
185 * floor((... + (c * d + r) * x_pos + ...)/d)
186 *
187 * with -d < 2 * r <= d.
188 * Replace it by
189 *
190 * floor((... + r * x_pos + ...)/d) + c * x_pos
191 *
192 * If 2 * ((c * d + r) % d) <= d, then c = floor((c * d + r)/d).
193 * Otherwise, c = floor((c * d + r)/d) + 1.
194 *
195 * This is the same normalization that is performed by isl_aff_floor.
196 */
198 __isl_take isl_basic_map *bmap, int div, int pos)
199{
200 isl_int shift;
201 int add_one;
202
203 isl_int_init(shift);
204 isl_int_fdiv_r(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
205 isl_int_mul_ui(shift, shift, 2);
206 add_one = isl_int_gt(shift, bmap->div[div][0]);
207 isl_int_fdiv_q(shift, bmap->div[div][1 + pos], bmap->div[div][0]);
208 if (add_one)
209 isl_int_add_ui(shift, shift, 1);
210 isl_int_neg(shift, shift);
211 bmap = isl_basic_map_shift_div(bmap, div, pos, shift);
212 isl_int_clear(shift);
213
214 return bmap;
215}
216
217/* Does the coefficient of the variable at position "pos"
218 * in integer division "div" need to be reduced?
219 * That is, does it lie outside the half-open interval (1/2,1/2]?
220 * The coefficient c/d lies outside this interval if abs(2 * c) >= d and
221 * 2 * c != d.
222 */
224 int pos)
225{
226 isl_bool r;
227
228 if (isl_int_is_zero(bmap->div[div][1 + pos]))
229 return isl_bool_false;
230
231 isl_int_mul_ui(bmap->div[div][1 + pos], bmap->div[div][1 + pos], 2);
232 r = isl_int_abs_ge(bmap->div[div][1 + pos], bmap->div[div][0]) &&
233 !isl_int_eq(bmap->div[div][1 + pos], bmap->div[div][0]);
234 isl_int_divexact_ui(bmap->div[div][1 + pos],
235 bmap->div[div][1 + pos], 2);
236
237 return r;
238}
239
240/* Reduce the coefficients (including the constant term) of
241 * integer division "div", if needed.
242 * In particular, make sure all coefficients lie in
243 * the half-open interval (1/2,1/2].
244 */
246 __isl_take isl_basic_map *bmap, int div)
247{
248 int i;
250
252 if (total < 0)
253 return isl_basic_map_free(bmap);
254 for (i = 0; i < 1 + total; ++i) {
255 isl_bool reduce;
256
257 reduce = needs_reduction(bmap, div, i);
258 if (reduce < 0)
259 return isl_basic_map_free(bmap);
260 if (!reduce)
261 continue;
262 bmap = reduce_coefficient_in_div(bmap, div, i);
263 if (!bmap)
264 break;
265 }
266
267 return bmap;
268}
269
270/* Reduce the coefficients (including the constant term) of
271 * the known integer divisions, if needed
272 * In particular, make sure all coefficients lie in
273 * the half-open interval (1/2,1/2].
274 */
277{
278 int i;
279
280 if (!bmap)
281 return NULL;
282 if (bmap->n_div == 0)
283 return bmap;
284
285 for (i = 0; i < bmap->n_div; ++i) {
286 if (isl_int_is_zero(bmap->div[i][0]))
287 continue;
288 bmap = reduce_div_coefficients_of_div(bmap, i);
289 if (!bmap)
290 break;
291 }
292
293 return bmap;
294}
295
296/* Remove any common factor in numerator and denominator of the div expression,
297 * not taking into account the constant term.
298 * That is, if the div is of the form
299 *
300 * floor((a + m f(x))/(m d))
301 *
302 * then replace it by
303 *
304 * floor((floor(a/m) + f(x))/d)
305 *
306 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
307 * and can therefore not influence the result of the floor.
308 */
310 __isl_take isl_basic_map *bmap, int div)
311{
313 isl_ctx *ctx = bmap->ctx;
314
315 if (total < 0)
316 return isl_basic_map_free(bmap);
317 if (isl_int_is_zero(bmap->div[div][0]))
318 return bmap;
319 isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
320 isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
322 return bmap;
323 isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
324 ctx->normalize_gcd);
325 isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
326 ctx->normalize_gcd);
327 isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
328 ctx->normalize_gcd, total);
329
330 return bmap;
331}
332
333/* Remove any common factor in numerator and denominator of a div expression,
334 * not taking into account the constant term.
335 * That is, look for any div of the form
336 *
337 * floor((a + m f(x))/(m d))
338 *
339 * and replace it by
340 *
341 * floor((floor(a/m) + f(x))/d)
342 *
343 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
344 * and can therefore not influence the result of the floor.
345 */
348{
349 int i;
350
351 if (!bmap)
352 return NULL;
353 if (bmap->n_div == 0)
354 return bmap;
355
356 for (i = 0; i < bmap->n_div; ++i)
357 bmap = normalize_div_expression(bmap, i);
358
359 return bmap;
360}
361
362/* Some progress has been made.
363 * Set *progress if "progress" is not NULL.
364 */
365static void mark_progress(int *progress)
366{
367 if (progress)
368 *progress = 1;
369}
370
371/* Eliminate the variable at position "pos" from the constraints of "bmap"
372 * using the equality constraint "eq".
373 * If "keep_divs" is set, then try and preserve
374 * the integer division expressions. In this case, these expressions
375 * are assumed to have been ordered.
376 * If "equivalent" is set, then the elimination is performed
377 * using an equality constraint of "bmap", meaning that the meaning
378 * of the constraints is preserved.
379 */
382 unsigned pos, isl_int *eq, int keep_divs, int equivalent, int *progress)
383{
385 isl_size v_div;
386 int k;
387 int last_div;
388 isl_ctx *ctx;
389
392 if (total < 0 || v_div < 0)
393 return isl_basic_map_free(bmap);
394 ctx = isl_basic_map_get_ctx(bmap);
395 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
396 for (k = 0; k < bmap->n_eq; ++k) {
397 if (bmap->eq[k] == eq)
398 continue;
399 if (isl_int_is_zero(bmap->eq[k][1+pos]))
400 continue;
401 mark_progress(progress);
402 isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
403 isl_seq_normalize(ctx, bmap->eq[k], 1 + total);
404 }
405
406 for (k = 0; k < bmap->n_ineq; ++k) {
407 if (isl_int_is_zero(bmap->ineq[k][1+pos]))
408 continue;
409 mark_progress(progress);
410 isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
411 isl_seq_gcd(bmap->ineq[k], 1 + total, &ctx->normalize_gcd);
412 bmap = scale_down_bmap_inequality(bmap, k, ctx->normalize_gcd,
413 total);
415 if (!bmap)
416 return NULL;
417 }
418
419 for (k = 0; k < bmap->n_div; ++k) {
420 if (isl_int_is_zero(bmap->div[k][0]))
421 continue;
422 if (isl_int_is_zero(bmap->div[k][1+1+pos]))
423 continue;
424 mark_progress(progress);
425 /* We need to be careful about circular definitions,
426 * so for now we just remove the definition of div k
427 * if the equality contains any divs.
428 * If keep_divs is set, then the divs have been ordered
429 * and we can keep the definition as long as the result
430 * is still ordered.
431 */
432 if (last_div == -1 || (keep_divs && last_div < k)) {
433 isl_seq_elim(bmap->div[k]+1, eq,
434 1+pos, 1+total, &bmap->div[k][0]);
435 bmap = normalize_div_expression(bmap, k);
436 if (!bmap)
437 return NULL;
438 } else
439 isl_seq_clr(bmap->div[k], 1 + total);
440 }
441
442 return bmap;
443}
444
445/* Eliminate and remove the local variable at position "pos" of "bmap"
446 * using the equality constraint "eq".
447 * If "keep_divs" is set, then try and preserve
448 * the integer division expressions. In this case, these expressions
449 * are assumed to have been ordered.
450 * If "equivalent" is set, then the elimination is performed
451 * using an equality constraint of "bmap", meaning that the meaning
452 * of the constraints is preserved.
453 */
455 isl_int *eq, unsigned div, int keep_divs, int equivalent)
456{
457 isl_size v_div;
458 unsigned pos;
459
461 if (v_div < 0)
462 return isl_basic_map_free(bmap);
463 pos = v_div + div;
464 bmap = eliminate_var_using_equality(bmap, pos, eq, keep_divs,
465 equivalent, NULL);
466
467 bmap = isl_basic_map_drop_div(bmap, div);
468
469 return bmap;
470}
471
472/* Check if elimination of div "div" using equality "eq" would not
473 * result in a div depending on a later div.
474 */
476 unsigned div)
477{
478 int k;
479 int last_div;
480 isl_size v_div;
481 unsigned pos;
482
484 if (v_div < 0)
485 return isl_bool_error;
486 pos = v_div + div;
487
488 last_div = isl_seq_last_non_zero(eq + 1 + v_div, bmap->n_div);
489 if (last_div < 0 || last_div <= div)
490 return isl_bool_true;
491
492 for (k = 0; k <= last_div; ++k) {
493 if (isl_int_is_zero(bmap->div[k][0]))
494 continue;
495 if (!isl_int_is_zero(bmap->div[k][1 + 1 + pos]))
496 return isl_bool_false;
497 }
498
499 return isl_bool_true;
500}
501
502/* Eliminate divs based on equalities
503 */
505 __isl_take isl_basic_map *bmap, int *progress)
506{
507 int d;
508 int i;
509 int modified = 0;
510 unsigned off;
511
512 bmap = isl_basic_map_order_divs(bmap);
513
514 if (!bmap)
515 return NULL;
516
518
519 for (d = bmap->n_div - 1; d >= 0 ; --d) {
520 for (i = 0; i < bmap->n_eq; ++i) {
521 isl_bool ok;
522
523 if (!isl_int_is_one(bmap->eq[i][off + d]) &&
524 !isl_int_is_negone(bmap->eq[i][off + d]))
525 continue;
526 ok = ok_to_eliminate_div(bmap, bmap->eq[i], d);
527 if (ok < 0)
528 return isl_basic_map_free(bmap);
529 if (!ok)
530 continue;
531 modified = 1;
532 mark_progress(progress);
533 bmap = eliminate_div(bmap, bmap->eq[i], d, 1, 1);
534 if (isl_basic_map_drop_equality(bmap, i) < 0)
535 return isl_basic_map_free(bmap);
536 break;
537 }
538 }
539 if (modified)
540 return eliminate_divs_eq(bmap, progress);
541 return bmap;
542}
543
544/* Eliminate divs based on inequalities
545 */
547 __isl_take isl_basic_map *bmap, int *progress)
548{
549 int d;
550 int i;
551 unsigned off;
552 struct isl_ctx *ctx;
553
554 if (!bmap)
555 return NULL;
556
557 ctx = bmap->ctx;
559
560 for (d = bmap->n_div - 1; d >= 0 ; --d) {
561 for (i = 0; i < bmap->n_eq; ++i)
562 if (!isl_int_is_zero(bmap->eq[i][off + d]))
563 break;
564 if (i < bmap->n_eq)
565 continue;
566 for (i = 0; i < bmap->n_ineq; ++i)
567 if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
568 break;
569 if (i < bmap->n_ineq)
570 continue;
571 mark_progress(progress);
572 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
573 if (!bmap || ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
574 break;
575 bmap = isl_basic_map_drop_div(bmap, d);
576 if (!bmap)
577 break;
578 }
579 return bmap;
580}
581
582/* Does the equality constraint at position "eq" in "bmap" involve
583 * any local variables in the range [first, first + n)
584 * that are not marked as having an explicit representation?
585 */
587 int eq, unsigned first, unsigned n)
588{
589 unsigned o_div;
590 int i;
591
592 if (!bmap)
593 return isl_bool_error;
594
595 o_div = isl_basic_map_offset(bmap, isl_dim_div);
596 for (i = 0; i < n; ++i) {
597 isl_bool unknown;
598
599 if (isl_int_is_zero(bmap->eq[eq][o_div + first + i]))
600 continue;
601 unknown = isl_basic_map_div_is_marked_unknown(bmap, first + i);
602 if (unknown < 0)
603 return isl_bool_error;
604 if (unknown)
605 return isl_bool_true;
606 }
607
608 return isl_bool_false;
609}
610
611/* The last local variable involved in the equality constraint
612 * at position "eq" in "bmap" is the local variable at position "div".
613 * It can therefore be used to extract an explicit representation
614 * for that variable.
615 * Do so unless the local variable already has an explicit representation or
616 * the explicit representation would involve any other local variables
617 * that in turn do not have an explicit representation.
618 * An equality constraint involving local variables without an explicit
619 * representation can be used in isl_basic_map_drop_redundant_divs
620 * to separate out an independent local variable. Introducing
621 * an explicit representation here would block this transformation,
622 * while the partial explicit representation in itself is not very useful.
623 * Set *progress if anything is changed.
624 *
625 * The equality constraint is of the form
626 *
627 * f(x) + n e >= 0
628 *
629 * with n a positive number. The explicit representation derived from
630 * this constraint is
631 *
632 * floor((-f(x))/n)
633 */
635 int div, int eq, int *progress)
636{
638 unsigned o_div;
639 isl_bool involves;
640
641 if (!bmap)
642 return NULL;
643
644 if (!isl_int_is_zero(bmap->div[div][0]))
645 return bmap;
646
647 involves = bmap_eq_involves_unknown_divs(bmap, eq, 0, div);
648 if (involves < 0)
649 return isl_basic_map_free(bmap);
650 if (involves)
651 return bmap;
652
654 if (total < 0)
655 return isl_basic_map_free(bmap);
656 o_div = isl_basic_map_offset(bmap, isl_dim_div);
657 isl_seq_neg(bmap->div[div] + 1, bmap->eq[eq], 1 + total);
658 isl_int_set_si(bmap->div[div][1 + o_div + div], 0);
659 isl_int_set(bmap->div[div][0], bmap->eq[eq][o_div + div]);
660 mark_progress(progress);
661
662 return bmap;
663}
664
665/* Perform fangcheng (Gaussian elimination) on the equality
666 * constraints of "bmap".
667 * That is, put them into row-echelon form, starting from the last column
668 * backward and use them to eliminate the corresponding coefficients
669 * from all constraints.
670 *
671 * If "progress" is not NULL, then it gets set if the elimination
672 * results in any changes.
673 * The elimination process may result in some equality constraints
674 * getting interchanged or removed.
675 * If "swap" or "drop" are not NULL, then they get called when
676 * two equality constraints get interchanged or
677 * when a number of final equality constraints get removed.
678 * As a special case, if the input turns out to be empty,
679 * then drop gets called with the number of removed equality
680 * constraints set to the total number of equality constraints.
681 * If "swap" or "drop" are not NULL, then the local variables (if any)
682 * are assumed to be in a valid order.
683 */
685 int *progress,
686 isl_stat (*swap)(unsigned a, unsigned b, void *user),
687 isl_stat (*drop)(unsigned n, void *user), void *user)
688{
689 int k;
690 int done;
691 int last_var;
692 unsigned total_var;
694 unsigned n_drop;
695
696 if (!swap && !drop)
697 bmap = isl_basic_map_order_divs(bmap);
698
700 if (total < 0)
701 return isl_basic_map_free(bmap);
702
703 total_var = total - bmap->n_div;
704
705 last_var = total - 1;
706 for (done = 0; done < bmap->n_eq; ++done) {
707 for (; last_var >= 0; --last_var) {
708 for (k = done; k < bmap->n_eq; ++k)
709 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
710 break;
711 if (k < bmap->n_eq)
712 break;
713 }
714 if (last_var < 0)
715 break;
716 if (k != done) {
717 swap_equality(bmap, k, done);
718 if (swap && swap(k, done, user) < 0)
719 return isl_basic_map_free(bmap);
720 }
721 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
722 isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
723
724 bmap = eliminate_var_using_equality(bmap, last_var,
725 bmap->eq[done], 1, 1, progress);
726
727 if (last_var >= total_var)
728 bmap = set_div_from_eq(bmap, last_var - total_var,
729 done, progress);
730 if (!bmap)
731 return NULL;
732 }
733 if (done == bmap->n_eq)
734 return bmap;
735 for (k = done; k < bmap->n_eq; ++k) {
736 if (isl_int_is_zero(bmap->eq[k][0]))
737 continue;
738 if (drop && drop(bmap->n_eq, user) < 0)
739 return isl_basic_map_free(bmap);
740 return isl_basic_map_set_to_empty(bmap);
741 }
742 n_drop = bmap->n_eq - done;
743 bmap = isl_basic_map_free_equality(bmap, n_drop);
744 if (drop && drop(n_drop, user) < 0)
745 return isl_basic_map_free(bmap);
746 return bmap;
747}
748
750 int *progress)
751{
752 return isl_basic_map_gauss5(bmap, progress, NULL, NULL, NULL);
753}
754
756 __isl_take isl_basic_set *bset, int *progress)
757{
759 progress));
760}
761
762
763static unsigned int round_up(unsigned int v)
764{
765 int old_v = v;
766
767 while (v) {
768 old_v = v;
769 v ^= v & -v;
770 }
771 return old_v << 1;
772}
773
774/* Hash table of inequalities in a basic map.
775 * "index" is an array of addresses of inequalities in the basic map, some
776 * of which are NULL. The inequalities are hashed on the coefficients
777 * except the constant term.
778 * "size" is the number of elements in the array and is always a power of two
779 * "bits" is the number of bits need to represent an index into the array.
780 * "total" is the total dimension of the basic map.
781 */
788
789/* Fill in the "ci" data structure for holding the inequalities of "bmap".
790 */
793{
794 isl_ctx *ctx;
795
796 ci->index = NULL;
797 if (!bmap)
798 return isl_stat_error;
800 if (ci->total < 0)
801 return isl_stat_error;
802 if (bmap->n_ineq == 0)
803 return isl_stat_ok;
804 ci->size = round_up(4 * (bmap->n_ineq + 1) / 3 - 1);
805 ci->bits = ffs(ci->size) - 1;
806 ctx = isl_basic_map_get_ctx(bmap);
807 ci->index = isl_calloc_array(ctx, isl_int **, ci->size);
808 if (!ci->index)
809 return isl_stat_error;
810
811 return isl_stat_ok;
812}
813
814/* Free the memory allocated by create_constraint_index.
815 */
817{
818 free(ci->index);
819}
820
821/* Return the position in ci->index that contains the address of
822 * an inequality that is equal to *ineq up to the constant term,
823 * provided this address is not identical to "ineq".
824 * If there is no such inequality, then return the position where
825 * such an inequality should be inserted.
826 */
827static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
828{
829 int h;
830 uint32_t hash = isl_seq_get_hash_bits((*ineq) + 1, ci->total, ci->bits);
831 for (h = hash; ci->index[h]; h = (h+1) % ci->size)
832 if (ineq != ci->index[h] &&
833 isl_seq_eq((*ineq) + 1, ci->index[h][0]+1, ci->total))
834 break;
835 return h;
836}
837
838/* Return the position in ci->index that contains the address of
839 * an inequality that is equal to the k'th inequality of "bmap"
840 * up to the constant term, provided it does not point to the very
841 * same inequality.
842 * If there is no such inequality, then return the position where
843 * such an inequality should be inserted.
844 */
845static int hash_index(struct isl_constraint_index *ci,
846 __isl_keep isl_basic_map *bmap, int k)
847{
848 return hash_index_ineq(ci, &bmap->ineq[k]);
849}
850
852 __isl_keep isl_basic_set *bset, int k)
853{
854 return hash_index(ci, bset, k);
855}
856
857/* Fill in the "ci" data structure with the inequalities of "bset".
858 */
861{
862 int k, h;
863
864 if (create_constraint_index(ci, bset) < 0)
865 return isl_stat_error;
866
867 for (k = 0; k < bset->n_ineq; ++k) {
868 h = set_hash_index(ci, bset, k);
869 ci->index[h] = &bset->ineq[k];
870 }
871
872 return isl_stat_ok;
873}
874
875/* Is the inequality ineq (obviously) redundant with respect
876 * to the constraints in "ci"?
877 *
878 * Look for an inequality in "ci" with the same coefficients and then
879 * check if the contant term of "ineq" is greater than or equal
880 * to the constant term of that inequality. If so, "ineq" is clearly
881 * redundant.
882 *
883 * Note that hash_index_ineq ignores a stored constraint if it has
884 * the same address as the passed inequality. It is ok to pass
885 * the address of a local variable here since it will never be
886 * the same as the address of a constraint in "ci".
887 */
889 isl_int *ineq)
890{
891 int h;
892
893 h = hash_index_ineq(ci, &ineq);
894 if (!ci->index[h])
895 return isl_bool_false;
896 return isl_int_ge(ineq[0], (*ci->index[h])[0]);
897}
898
899/* Look for pairs of inequality constraints in "bmap"
900 * that are opposite to each other apart from the constant term.
901 * "ci" contains a hash table of the inequality constraints of "bmap".
902 * Return an array of size equal to the number of inequality constraints
903 * with as entries either
904 * - zero, if the constraint has no opposite, or
905 * - 1 + the position of the opposite constraint.
906 */
909{
910 isl_size n_ineq;
911 int k, l, h;
912 isl_ctx *ctx;
913 int *opposite;
914
915 n_ineq = isl_basic_map_n_inequality(bmap);
916 if (n_ineq < 0)
917 return NULL;
918 ctx = isl_basic_map_get_ctx(bmap);
919 opposite = isl_calloc_array(ctx, int, n_ineq);
920 if (!opposite)
921 return NULL;
922
923 for (k = 0; k < n_ineq - 1; ++k) {
924 if (opposite[k])
925 continue;
926 isl_seq_neg(bmap->ineq[k] + 1, bmap->ineq[k] + 1, ci->total);
927 h = hash_index(ci, bmap, k);
928 isl_seq_neg(bmap->ineq[k] + 1, bmap->ineq[k] + 1, ci->total);
929 if (!ci->index[h])
930 continue;
931 l = ci->index[h] - &bmap->ineq[0];
932
933 opposite[k] = 1 + l;
934 opposite[l] = 1 + k;
935 }
936
937 return opposite;
938}
939
940/* If we can eliminate more than one div, then we need to make
941 * sure we do it from last div to first div, in order not to
942 * change the position of the other divs that still need to
943 * be removed.
944 */
946 __isl_take isl_basic_map *bmap, int *progress)
947{
948 unsigned int size;
949 int *index;
950 int *elim_for;
951 int k, l, h;
952 int bits;
953 struct isl_blk eq;
954 isl_size v_div;
955 unsigned total;
956 struct isl_ctx *ctx;
957
958 bmap = isl_basic_map_order_divs(bmap);
959 if (!bmap || bmap->n_div <= 1)
960 return bmap;
961
963 if (v_div < 0)
964 return isl_basic_map_free(bmap);
965 total = v_div + bmap->n_div;
966
967 ctx = bmap->ctx;
968 for (k = bmap->n_div - 1; k >= 0; --k)
969 if (!isl_int_is_zero(bmap->div[k][0]))
970 break;
971 if (k <= 0)
972 return bmap;
973
974 size = round_up(4 * bmap->n_div / 3 - 1);
975 if (size == 0)
976 return bmap;
977 elim_for = isl_calloc_array(ctx, int, bmap->n_div);
978 bits = ffs(size) - 1;
979 index = isl_calloc_array(ctx, int, size);
980 if (!elim_for || !index)
981 goto out;
982 eq = isl_blk_alloc(ctx, 1+total);
983 if (isl_blk_is_error(eq))
984 goto out;
985
986 isl_seq_clr(eq.data, 1+total);
987 index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
988 for (--k; k >= 0; --k) {
989 uint32_t hash;
990
991 if (isl_int_is_zero(bmap->div[k][0]))
992 continue;
993
994 hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
995 for (h = hash; index[h]; h = (h+1) % size)
996 if (isl_seq_eq(bmap->div[k],
997 bmap->div[index[h]-1], 2+total))
998 break;
999 if (index[h]) {
1000 mark_progress(progress);
1001 l = index[h] - 1;
1002 elim_for[l] = k + 1;
1003 }
1004 index[h] = k+1;
1005 }
1006 for (l = bmap->n_div - 1; l >= 0; --l) {
1007 if (!elim_for[l])
1008 continue;
1009 k = elim_for[l] - 1;
1010 isl_int_set_si(eq.data[1 + v_div + k], -1);
1011 isl_int_set_si(eq.data[1 + v_div + l], 1);
1012 bmap = eliminate_div(bmap, eq.data, l, 1, 0);
1013 if (!bmap)
1014 break;
1015 isl_int_set_si(eq.data[1 + v_div + k], 0);
1016 isl_int_set_si(eq.data[1 + v_div + l], 0);
1017 }
1018
1019 isl_blk_free(ctx, eq);
1020out:
1021 free(index);
1022 free(elim_for);
1023 return bmap;
1024}
1025
1026/* Is the local variable at position "div" of "bmap"
1027 * an integral integer division?
1028 */
1030{
1031 isl_bool unknown;
1032
1033 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
1034 if (unknown < 0 || unknown)
1035 return isl_bool_not(unknown);
1036 return isl_basic_map_div_is_integral(bmap, div);
1037}
1038
1039/* Eliminate local variable "div" from "bmap", given
1040 * that it represents an integer division with denominator 1.
1041 *
1042 * Construct an equality constraint that equates the local variable
1043 * to the argument of the integer division and use that to eliminate
1044 * the local variable.
1045 */
1047 __isl_take isl_basic_map *bmap, int div)
1048{
1049 isl_size total, v_div;
1050 isl_vec *v;
1051
1054 if (v_div < 0 || total < 0)
1055 return isl_basic_map_free(bmap);
1057 if (!v)
1058 return isl_basic_map_free(bmap);
1059 isl_seq_cpy(v->el, bmap->div[div] + 1, 1 + total);
1060 isl_int_set_si(v->el[1 + v_div + div], -1);
1061 bmap = eliminate_div(bmap, v->el, div, 1, 0);
1062 isl_vec_free(v);
1063
1064 return bmap;
1065}
1066
1067/* Eliminate all integer divisions with denominator 1.
1068 */
1070 __isl_take isl_basic_map *bmap, int *progress)
1071{
1072 int i;
1073 isl_size n_div;
1074
1075 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1076 if (n_div < 0)
1077 return isl_basic_map_free(bmap);
1078
1079 for (i = 0; i < n_div; ++i) {
1081
1083 if (eliminate < 0)
1084 return isl_basic_map_free(bmap);
1085 if (!eliminate)
1086 continue;
1087
1088 bmap = eliminate_integral_div(bmap, i);
1089 mark_progress(progress);
1090 i--;
1091 n_div--;
1092 }
1093
1094 return bmap;
1095}
1096
1098{
1099 int i, j;
1100 isl_size v_div;
1101
1103 if (v_div < 0)
1104 return -1;
1105 for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
1106 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1107 --j;
1108 if (j < 0)
1109 break;
1110 if (isl_seq_any_non_zero(bmap->eq[i] + 1 + v_div, j))
1111 return 0;
1112 }
1113 return i;
1114}
1115
1116/* Normalize divs that appear in equalities.
1117 *
1118 * In particular, we assume that bmap contains some equalities
1119 * of the form
1120 *
1121 * a x = m * e_i
1122 *
1123 * and we want to replace the set of e_i by a minimal set and
1124 * such that the new e_i have a canonical representation in terms
1125 * of the vector x.
1126 * If any of the equalities involves more than one divs, then
1127 * we currently simply bail out.
1128 *
1129 * Let us first additionally assume that all equalities involve
1130 * a div. The equalities then express modulo constraints on the
1131 * remaining variables and we can use "parameter compression"
1132 * to find a minimal set of constraints. The result is a transformation
1133 *
1134 * x = T(x') = x_0 + G x'
1135 *
1136 * with G a lower-triangular matrix with all elements below the diagonal
1137 * non-negative and smaller than the diagonal element on the same row.
1138 * We first normalize x_0 by making the same property hold in the affine
1139 * T matrix.
1140 * The rows i of G with a 1 on the diagonal do not impose any modulo
1141 * constraint and simply express x_i = x'_i.
1142 * For each of the remaining rows i, we introduce a div and a corresponding
1143 * equality. In particular
1144 *
1145 * g_ii e_j = x_i - g_i(x')
1146 *
1147 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1148 * corresponding div (if g_kk != 1).
1149 *
1150 * If there are any equalities not involving any div, then we
1151 * first apply a variable compression on the variables x:
1152 *
1153 * x = C x'' x'' = C_2 x
1154 *
1155 * and perform the above parameter compression on A C instead of on A.
1156 * The resulting compression is then of the form
1157 *
1158 * x'' = T(x') = x_0 + G x'
1159 *
1160 * and in constructing the new divs and the corresponding equalities,
1161 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1162 * by the corresponding row from C_2.
1163 */
1165 int *progress)
1166{
1167 int i, j, k;
1168 isl_size v_div;
1169 int div_eq;
1170 struct isl_mat *B;
1171 struct isl_vec *d;
1172 struct isl_mat *T = NULL;
1173 struct isl_mat *C = NULL;
1174 struct isl_mat *C2 = NULL;
1175 isl_int v;
1176 int *pos = NULL;
1177 int dropped, needed;
1178
1179 if (!bmap)
1180 return NULL;
1181
1182 if (bmap->n_div == 0)
1183 return bmap;
1184
1185 if (bmap->n_eq == 0)
1186 return bmap;
1187
1189 return bmap;
1190
1192 div_eq = n_pure_div_eq(bmap);
1193 if (v_div < 0 || div_eq < 0)
1194 return isl_basic_map_free(bmap);
1195 if (div_eq == 0)
1196 return bmap;
1197
1198 if (div_eq < bmap->n_eq) {
1199 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, div_eq,
1200 bmap->n_eq - div_eq, 0, 1 + v_div);
1202 if (!C || !C2)
1203 goto error;
1204 if (C->n_col == 0) {
1205 bmap = isl_basic_map_set_to_empty(bmap);
1206 isl_mat_free(C);
1207 isl_mat_free(C2);
1208 goto done;
1209 }
1210 }
1211
1212 d = isl_vec_alloc(bmap->ctx, div_eq);
1213 if (!d)
1214 goto error;
1215 for (i = 0, j = bmap->n_div-1; i < div_eq; ++i) {
1216 while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1217 --j;
1218 isl_int_set(d->block.data[i], bmap->eq[i][1 + v_div + j]);
1219 }
1220 B = isl_mat_sub_alloc6(bmap->ctx, bmap->eq, 0, div_eq, 0, 1 + v_div);
1221
1222 if (C) {
1223 B = isl_mat_product(B, C);
1224 C = NULL;
1225 }
1226
1228 if (!T)
1229 goto error;
1230 if (T->n_col == 0) {
1231 bmap = isl_basic_map_set_to_empty(bmap);
1232 isl_mat_free(C2);
1233 isl_mat_free(T);
1234 goto done;
1235 }
1236 isl_int_init(v);
1237 for (i = 0; i < T->n_row - 1; ++i) {
1238 isl_int_fdiv_q(v, T->row[1 + i][0], T->row[1 + i][1 + i]);
1239 if (isl_int_is_zero(v))
1240 continue;
1241 isl_mat_col_submul(T, 0, v, 1 + i);
1242 }
1243 isl_int_clear(v);
1244 pos = isl_alloc_array(bmap->ctx, int, T->n_row);
1245 if (!pos)
1246 goto error;
1247 /* We have to be careful because dropping equalities may reorder them */
1248 dropped = 0;
1249 for (j = bmap->n_div - 1; j >= 0; --j) {
1250 for (i = 0; i < bmap->n_eq; ++i)
1251 if (!isl_int_is_zero(bmap->eq[i][1 + v_div + j]))
1252 break;
1253 if (i < bmap->n_eq) {
1254 bmap = isl_basic_map_drop_div(bmap, j);
1255 if (isl_basic_map_drop_equality(bmap, i) < 0)
1256 goto error;
1257 ++dropped;
1258 }
1259 }
1260 pos[0] = 0;
1261 needed = 0;
1262 for (i = 1; i < T->n_row; ++i) {
1263 if (isl_int_is_one(T->row[i][i]))
1264 pos[i] = i;
1265 else
1266 needed++;
1267 }
1268 if (needed > dropped) {
1269 bmap = isl_basic_map_extend(bmap, needed, needed, 0);
1270 if (!bmap)
1271 goto error;
1272 }
1273 for (i = 1; i < T->n_row; ++i) {
1274 if (isl_int_is_one(T->row[i][i]))
1275 continue;
1276 k = isl_basic_map_alloc_div(bmap);
1277 pos[i] = 1 + v_div + k;
1278 isl_seq_clr(bmap->div[k] + 1, 1 + v_div + bmap->n_div);
1279 isl_int_set(bmap->div[k][0], T->row[i][i]);
1280 if (C2)
1281 isl_seq_cpy(bmap->div[k] + 1, C2->row[i], 1 + v_div);
1282 else
1283 isl_int_set_si(bmap->div[k][1 + i], 1);
1284 for (j = 0; j < i; ++j) {
1285 if (isl_int_is_zero(T->row[i][j]))
1286 continue;
1287 if (pos[j] < T->n_row && C2)
1288 isl_seq_submul(bmap->div[k] + 1, T->row[i][j],
1289 C2->row[pos[j]], 1 + v_div);
1290 else
1291 isl_int_neg(bmap->div[k][1 + pos[j]],
1292 T->row[i][j]);
1293 }
1295 isl_seq_neg(bmap->eq[j], bmap->div[k]+1, 1+v_div+bmap->n_div);
1296 isl_int_set(bmap->eq[j][pos[i]], bmap->div[k][0]);
1297 }
1298 free(pos);
1299 isl_mat_free(C2);
1300 isl_mat_free(T);
1301
1302 mark_progress(progress);
1303done:
1305
1306 return bmap;
1307error:
1308 free(pos);
1309 isl_mat_free(C);
1310 isl_mat_free(C2);
1311 isl_mat_free(T);
1312 isl_basic_map_free(bmap);
1313 return NULL;
1314}
1315
1317 __isl_take isl_basic_map *bmap, int div, isl_int *ineq)
1318{
1319 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1320
1321 isl_seq_neg(bmap->div[div] + 1, ineq, total + bmap->n_div);
1322 isl_int_set(bmap->div[div][0], ineq[total + div]);
1323 isl_int_add(bmap->div[div][1], bmap->div[div][1], bmap->div[div][0]);
1324 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
1325 isl_int_set_si(bmap->div[div][1 + total + div], 0);
1326
1327 return bmap;
1328}
1329
1330/* Check whether it is ok to define a div based on an inequality.
1331 * To avoid the introduction of circular definitions of divs, we
1332 * do not allow such a definition if the resulting expression would refer to
1333 * any other undefined divs or if any known div is defined in
1334 * terms of the unknown div.
1335 */
1337 int div, isl_int *ineq)
1338{
1339 int j;
1341 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
1342
1343 if (v_div < 0 || n_div < 0)
1344 return isl_bool_error;
1345
1346 /* Not defined in terms of unknown divs */
1347 for (j = 0; j < n_div; ++j) {
1348 isl_bool unknown;
1349
1350 if (div == j)
1351 continue;
1352 if (isl_int_is_zero(ineq[1 + v_div + j]))
1353 continue;
1354 unknown = isl_basic_map_div_is_marked_unknown(bmap, j);
1355 if (unknown < 0 || unknown)
1356 return isl_bool_not(unknown);
1357 }
1358
1359 /* No other div defined in terms of this one => avoid loops */
1360 for (j = 0; j < n_div; ++j) {
1361 if (div == j)
1362 continue;
1363 if (isl_int_is_zero(bmap->div[j][0]))
1364 continue;
1365 if (!isl_int_is_zero(bmap->div[j][1 + 1 + v_div + div]))
1366 return isl_bool_false;
1367 }
1368
1369 return isl_bool_true;
1370}
1371
1372/* Would an expression for div "div" based on inequality "ineq"
1373 * be a better expression than the current one in "bmap"?
1374 *
1375 * If we do not have any expression yet, then any expression would be better.
1376 * Otherwise we check if the last variable involved in the inequality
1377 * (disregarding the div that it would define) is in an earlier position
1378 * than the last variable involved in the current div expression.
1379 */
1381 int div, isl_int *ineq)
1382{
1383 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
1384 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
1385 int last_div;
1386 int last_ineq;
1387 isl_bool unknown;
1388
1389 if (n_div < 0)
1390 return isl_bool_error;
1391 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
1392 if (unknown < 0 || unknown)
1393 return unknown;
1394
1395 if (isl_seq_any_non_zero(ineq + total + div + 1, n_div - (div + 1)))
1396 return isl_bool_false;
1397
1398 last_ineq = isl_seq_last_non_zero(ineq, total + div);
1399 last_div = isl_seq_last_non_zero(bmap->div[div] + 1, total + n_div);
1400
1401 return last_ineq < last_div;
1402}
1403
1404/* Given a lower bound "ineq" on local variable "div" of "bmap"
1405 * that could in theory be used to define an integer division expression,
1406 * do so if it is better than the current expression (if any) and
1407 * if there is no risk of introducing circular definitions.
1408 * Set *progress if anything is changed.
1409 */
1411 __isl_take isl_basic_map *bmap, int div, isl_int *ineq, int *progress)
1412{
1414
1415 set_div = better_div_constraint(bmap, div, ineq);
1416 if (set_div >= 0 && set_div)
1417 set_div = ok_to_set_div_from_bound(bmap, div, ineq);
1418 if (set_div < 0)
1419 return isl_basic_map_free(bmap);
1420 if (!set_div)
1421 return bmap;
1422 bmap = set_div_from_lower_bound(bmap, div, ineq);
1423 mark_progress(progress);
1424 return bmap;
1425}
1426
1427/* Is the sequence of "len" coefficients "ineq" equal to "res"
1428 * plus some non-trivial coefficients that are all a multiple of some number
1429 * greater than "sum"?
1430 * If so, this factor is stored in "gcd".
1431 *
1432 * The current implementation requires the coefficients
1433 * in "res" to appear directly in "ineq", so that "gcd"
1434 * is the gcd of the remaining coefficients.
1435 * The same assumption is used in has_nested_unit_div.
1436 */
1437static int is_residue(isl_int *res, isl_int *ineq, isl_int sum, unsigned len,
1438 isl_int *gcd)
1439{
1440 int j;
1441
1442 isl_int_set_si(*gcd, 0);
1443 for (j = 0; j < len; ++j) {
1444 if (!isl_int_is_zero(res[1 + j])) {
1445 if (isl_int_eq(res[1 + j], ineq[1 + j]))
1446 continue;
1447 return 0;
1448 }
1449 if (!isl_int_is_zero(ineq[1 + j])) {
1450 isl_int_gcd(*gcd, *gcd, ineq[1 + j]);
1451 if (isl_int_le(*gcd, sum))
1452 return 0;
1453 }
1454 }
1455
1456 return !isl_int_is_zero(*gcd);
1457}
1458
1459/* Is
1460 *
1461 * (cst - cst2) mod n + sum
1462 *
1463 * greater than or equal to n?
1464 */
1466{
1467 int exceeded;
1468 isl_int t;
1469
1470 isl_int_init(t);
1471 isl_int_sub(t, cst, cst2);
1472 isl_int_fdiv_r(t, t, n);
1473 isl_int_add(t, t, sum);
1474 exceeded = isl_int_ge(t, n);
1476
1477 return exceeded;
1478}
1479
1480/* Given two constraints "k" and "l" that are opposite to each other,
1481 * except for the constant term, with "sum" the sum of these constant terms,
1482 * check if they can be used to simplify any integer division expression.
1483 *
1484 * In particular, let "k" and "l" be of the form
1485 *
1486 * f(x) >= 0
1487 * -f(x) + c >= 0
1488 *
1489 * That is,
1490 *
1491 * 0 <= f(x) <= c
1492 *
1493 * Note that the same constraint holds for "k" and "l" interchanged, i.e.,
1494 *
1495 * 0 <= -f(x) + c <= c
1496 *
1497 * That is, the reasoning below holds for both "f(x)" and "-f(x) + c".
1498 *
1499 * If there is an integer division definition of the form
1500 *
1501 * floor((f(x) + n h(x) + c')/(n * m))
1502 *
1503 * with
1504 *
1505 * c' % n < n - c
1506 *
1507 * then it is equal to
1508 *
1509 * floor((h(x) + floor(c'/n))/m)
1510 *
1511 * because
1512 *
1513 * floor((f(x) + n h(x) + c')/(n * m))
1514 * = floor((f(x) + c' % n + n (h(x) + floor(c'/n)))/(n * m))
1515 *
1516 * and
1517 *
1518 * 0 <= f(x) + c' % n < n
1519 *
1520 * Note that h(x) may be equal to zero, in which case the denominator
1521 * of the integer division can be used as n (and m = 1).
1522 */
1524 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1525 int *progress)
1526{
1527 int i;
1528 int p;
1529 isl_ctx *ctx;
1530 isl_size n_div, total;
1531
1532 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1534 if (n_div < 0 || total < 0)
1535 return isl_basic_map_free(bmap);
1536
1537 ctx = isl_basic_map_get_ctx(bmap);
1538 p = isl_seq_last_non_zero(bmap->ineq[k] + 1, total);
1539 for (i = 0; i < n_div; ++i) {
1540 int c;
1541 isl_bool unknown;
1542
1543 unknown = isl_basic_map_div_is_marked_unknown(bmap, i);
1544 if (unknown < 0)
1545 return isl_basic_map_free(bmap);
1546 if (unknown)
1547 continue;
1548 if (isl_int_le(bmap->div[i][0], sum))
1549 continue;
1550 if (isl_int_eq(bmap->div[i][2 + p], bmap->ineq[k][1 + p]))
1551 c = k;
1552 else if (isl_int_eq(bmap->div[i][2 + p], bmap->ineq[l][1 + p]))
1553 c = l;
1554 else
1555 continue;
1556
1557 if (isl_seq_eq(bmap->div[i] + 2, bmap->ineq[c] + 1, total))
1558 isl_int_set(ctx->normalize_gcd, bmap->div[i][0]);
1559 else if (!is_residue(bmap->ineq[c], bmap->div[i] + 1, sum,
1561 continue;
1562
1563 if (residue_exceeded(bmap->div[i][1], bmap->ineq[c][0],
1564 ctx->normalize_gcd, sum))
1565 continue;
1566
1567 if (!isl_int_is_divisible_by(bmap->div[i][0],
1569 continue;
1570
1571 isl_seq_sub(bmap->div[i] + 1, bmap->ineq[c], 1 + total);
1572 mark_progress(progress);
1573 }
1574
1575 return bmap;
1576}
1577
1578/* Is inequality "ineq" of "bmap" a constraint defining an integer division?
1579 * "v_div" is the position of the first local variable.
1580 * "n_div" is the number of local variables.
1581 *
1582 * A constraint defining an integer division must involve some local variable
1583 * and could only possibly define the last local variable involved since
1584 * it can only be defined in terms of earlier variables.
1585 */
1587 unsigned v_div, unsigned n_div)
1588{
1589 int last;
1590
1591 last = isl_seq_last_non_zero(bmap->ineq[ineq] + 1 + v_div, n_div);
1592 if (last < 0)
1593 return isl_bool_false;
1594 return isl_basic_map_is_div_constraint(bmap, bmap->ineq[ineq], last);
1595}
1596
1597/* Does the inequality constraint "ineq" of "bmap" involve nested integer
1598 * divisions with unit coefficient after removing the coefficients
1599 * of inequality constraint "base"?
1600 * "v_div" is the position of the first local variable.
1601 * "n_div" is the number of local variables.
1602 * "n" is the factor with which the constraint will be scaled down.
1603 *
1604 * Use the same simplifying assumption as "is_residue" that
1605 * the coefficients in "base" appear directly in "ineq".
1606 * Look for an integer division involving nested integer divisions
1607 * that does not appear in "base" and does appear in "ineq"
1608 * with a coefficient equal to "n" (up to a change of sign).
1609 */
1611 int base, int ineq, unsigned v_div, unsigned n_div, isl_int n)
1612{
1613 int j;
1614
1615 for (j = 0; j < n_div; ++j) {
1617
1618 if (!isl_int_is_zero(bmap->ineq[base][1 + v_div + j]))
1619 continue;
1620 if (!isl_int_abs_eq(bmap->ineq[ineq][1 + v_div + j], n))
1621 continue;
1623 v_div, n_div);
1624 if (nested < 0 || nested)
1625 return nested;
1626 }
1627
1628 return isl_bool_false;
1629}
1630
1631/* Given two constraints "k" and "l" that are opposite to each other,
1632 * except for the constant term, with "sum" the sum of these constant terms,
1633 * check if they can be used to simplify other constraints.
1634 * Only do this for integer basic maps.
1635 *
1636 * In particular, let "k" and "l" be of the form
1637 *
1638 * f(x) >= 0
1639 * -f(x) + c >= 0
1640 *
1641 * That is,
1642 *
1643 * 0 <= f(x) <= c
1644 *
1645 * Note that the same constraint holds for "k" and "l" interchanged, i.e.,
1646 *
1647 * 0 <= -f(x) + c <= c
1648 *
1649 * That is, the reasoning below holds for both "f(x)" and "-f(x) + c".
1650 *
1651 * If there is some other constraint
1652 *
1653 * g(x) >= 0
1654 *
1655 * such that
1656 *
1657 * g(x) - f(x) = n h(x) + c'
1658 *
1659 * with
1660 *
1661 * 0 <= c' < n - c
1662 *
1663 * in particular, for the constant term,
1664 *
1665 * (g(x) - f(x)) mod n + c < n
1666 *
1667 * then this other constraint is equivalent to
1668 *
1669 * h(x) >= 0
1670 *
1671 * (given "k" and "l") since
1672 *
1673 * 0 <= f(x) + c' < n
1674 *
1675 * Note that the constraint does not necessarily need to be scaled down here
1676 * since it would otherwise also be scaled down
1677 * by isl_basic_map_normalize_constraints, but since the scaling factor
1678 * is already known here, it might as well be done immediately.
1679 * Also note that the current implementation only checks for constraints
1680 * where the coefficients of f(x) appear directly in g(x), while it would
1681 * be sufficient for the differences with the corresponding coefficients
1682 * in g(x) to be multiples of n.
1683 *
1684 * Similarly, if there is an integer division definition of the form
1685 *
1686 * floor((f(x) + n h(x) + c')/(n * m))
1687 *
1688 * with
1689 *
1690 * c' % n < n - c
1691 *
1692 * then it is equal to
1693 *
1694 * floor((h(x) + floor(c'/n))/m)
1695 *
1696 *
1697 * Do not apply any simplification to constraint(s) defining integer divisions.
1698 * Such constraints would first be simplified to be of the form
1699 *
1700 * e + c'' >= 0
1701 *
1702 * and then the integer division definition would be plugged into
1703 * this constraint, resulting in the original constraint and
1704 * causing an infinite loop.
1705 * Simplifying the integer division definitions as well mitigates
1706 * some (possibly all) of this effect, but it is too fragile to rely on
1707 * for avoiding infinite loops.
1708 *
1709 * If any nested integer divisions are involved, then a similar effect
1710 * may be obtained even on constraints that do not (obviously)
1711 * define an integer division through multiple steps of such substitutions.
1712 * Any constraint that would result in an integer division with nested
1713 * integer divisions and a unit coefficient is therefore also left untouched.
1714 */
1716 __isl_take isl_basic_map *bmap, int k, int l, isl_int sum,
1717 int *progress)
1718{
1719 int i;
1720 int p;
1721 isl_ctx *ctx;
1722 isl_bool rat;
1723 isl_size n_ineq, total, v_div, n_div;
1724
1725 rat = isl_basic_map_is_rational(bmap);
1726 n_ineq = isl_basic_map_n_inequality(bmap);
1729 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1730 if (rat < 0 || n_ineq < 0 || total < 0 || v_div < 0 || n_div < 0)
1731 return isl_basic_map_free(bmap);
1732 if (rat)
1733 return bmap;
1734
1735 bmap = check_for_residues_in_divs(bmap, k, l, sum, progress);
1736 if (!bmap)
1737 return bmap;
1738
1739 ctx = isl_basic_map_get_ctx(bmap);
1740 p = isl_seq_last_non_zero(bmap->ineq[k] + 1, total);
1741 for (i = 0; i < n_ineq; ++i) {
1742 isl_bool skip;
1743 int c;
1744
1745 if (i == k || i == l)
1746 continue;
1747 if (isl_int_is_zero(bmap->ineq[i][1 + p]))
1748 continue;
1749 skip = is_div_constraint(bmap, i, v_div, n_div);
1750 if (skip < 0)
1751 return isl_basic_map_free(bmap);
1752 if (skip)
1753 continue;
1754 if (isl_int_eq(bmap->ineq[i][1 + p], bmap->ineq[k][1 + p]))
1755 c = k;
1756 else if (isl_int_eq(bmap->ineq[i][1 + p], bmap->ineq[l][1 + p]))
1757 c = l;
1758 else
1759 continue;
1760
1761 if (!is_residue(bmap->ineq[c], bmap->ineq[i], sum, total,
1762 &ctx->normalize_gcd))
1763 continue;
1764
1765 skip = has_nested_unit_div(bmap, c, i, v_div, n_div,
1767 if (skip < 0)
1768 return isl_basic_map_free(bmap);
1769 if (skip)
1770 continue;
1771 if (residue_exceeded(bmap->ineq[i][0], bmap->ineq[c][0],
1772 ctx->normalize_gcd, sum))
1773 continue;
1774
1775 isl_seq_sub(bmap->ineq[i], bmap->ineq[c], 1 + total);
1777 total);
1778 if (!bmap)
1779 return NULL;
1780 mark_progress(progress);
1781 }
1782
1783 return bmap;
1784}
1785
1786/* Given constraints of the form
1787 *
1788 * -n f + g + m n e + c1 >= 0 (l)
1789 * g + c3 >= 0 (i)
1790 * -g + c4 >= 0 (j)
1791 *
1792 * with e the local variable at position "div",
1793 * does the following condition hold?
1794 *
1795 * c3 + c4 + (c1 - c3) % n < m n
1796 *
1797 * "v_div" is the position of the first local variable.
1798 */
1800 int l, int div, unsigned v_div, int i, int j, isl_int n)
1801{
1802 isl_int tmp;
1803 int ok;
1804
1805 isl_int_init(tmp);
1806 isl_int_sub(tmp, bmap->ineq[l][0], bmap->ineq[i][0]);
1807 isl_int_fdiv_r(tmp, tmp, n);
1808 isl_int_add(tmp, tmp, bmap->ineq[i][0]);
1809 isl_int_add(tmp, tmp, bmap->ineq[j][0]);
1810 ok = isl_int_lt(tmp, bmap->ineq[l][1 + v_div + div]);
1811 isl_int_clear(tmp);
1812
1813 return ok;
1814}
1815
1816/* Given a lower bound constraint at position "l" of "bmap"
1817 * on local variable "div" such that the sum of the constant terms
1818 * of this constraint and the corresponding upper bound is equal to "sum",
1819 * as well as a separate pair of opposite constraints "i" and "j"
1820 * such that the sum of their constant terms is smaller than
1821 * the coefficient of "div" in "l",
1822 * check if this pair can be used to derive a reduced expression
1823 * for the local variable.
1824 * "total" is the total number of variables.
1825 * "v_div" is the position of the first local variable.
1826 * Set *progress if anything is changed.
1827 *
1828 * In particular, let l be the constraint
1829 *
1830 * -n f + g + m n e + c1 >= 0
1831 *
1832 * The opposite constraint is
1833 *
1834 * n f - g - m n e + c2 >= 0
1835 *
1836 * with c1 + c2 equal to "sum".
1837 *
1838 * The constraints i and j are of the form
1839 *
1840 * h + c3 >= 0
1841 * -h + c4 >= 0
1842 *
1843 * with c3 + c4 < m n.
1844 * First check that h is equal to g for some n greater than "sum".
1845 * The constraints i and j are then of the form
1846 *
1847 * g + c3 >= 0
1848 * -g + c4 >= 0
1849 *
1850 * From constraint "l" and its opposite,
1851 *
1852 * g + m n e - c2 <= n f <= g + m n e + c1
1853 *
1854 * combined with the fact that c1 + c2 < n, the following expression
1855 * can be obtained:
1856 *
1857 * f = floor((g + m n e + c1)/n) = floor((g + c1)/n) + m e
1858 *
1859 * Replacing c1 with
1860 *
1861 * c1 = c3 + (c1 - c3) = c3 + n floor((c1 - c3)/n) + (c1 - c3) % n
1862 *
1863 * results in
1864 *
1865 * f = floor((g + c3 + (c1 - c3) % n)/n) + floor((c1 - c3)/n) + m e
1866 *
1867 * From the constraints i and j, together with c3 + c4 < m n,
1868 *
1869 * 0 <= g + c3 < m n
1870 *
1871 * Since (c1 - c3) % n is non-negative,
1872 *
1873 * 0 <= g + c3 + (c1 - c3) % n
1874 *
1875 * holds as well. However,
1876 *
1877 * g + c3 + (c1 - c3) % n < m n
1878 *
1879 * may not necessarily hold. It is however the case if
1880 *
1881 * c3 + c4 + (c1 - c3) % n < m n
1882 *
1883 * holds, which is checked in is_residue_div_pair.
1884 *
1885 * Given
1886 *
1887 * 0 <= g + c3 + (c1 - c3) % n < m n
1888 *
1889 * also
1890 *
1891 * 0 <= floor((g + c3 + (c1 - c3) % n) / n) < m
1892 *
1893 * holds.
1894 *
1895 * So,
1896 *
1897 * f = floor((g + c3 + (c1 - c3) % n)/n) + floor((c1 - c3)/n) + m e
1898 *
1899 * and
1900 *
1901 * 0 <= floor((g + c3 + (c1 - c3) % n) / n) < m
1902 *
1903 * This means
1904 *
1905 * f - floor((c1 - c3)/n)
1906 *
1907 * is equal to a multiple of m plus a value between 0 and m - 1.
1908 * That is,
1909 *
1910 * e = floor((f - floor((c1 - c3)/n))/m)
1911 *
1912 * Construct the inequality constraint
1913 *
1914 * -n f + m n e + c1 - c3 >= 0
1915 *
1916 * scale it down to
1917 *
1918 * -f + m e + floor((c1 - c3)/n) >= 0
1919 *
1920 * and add m - 1, to obtain
1921 *
1922 * -f + m e + floor((c1 - c3)/n) + m - 1 >= 0
1923 *
1924 * This constraint can then be used by set_div_from_lower_bound_if_better
1925 * to obtain
1926 *
1927 * e = floor((f - floor((c1 - c3)/n))/m)
1928 *
1929 * Adding m - 1 is needed to be able to reuse set_div_from_lower_bound,
1930 * which subtracts this amount from the constraint (by adding it
1931 * to the negated constraint).
1932 */
1934 int l, isl_int sum, int div,
1935 unsigned v_div, unsigned total, int i, int j, int *progress)
1936{
1937 isl_ctx *ctx;
1938 isl_vec *v;
1939
1940 if (!bmap)
1941 return NULL;
1942
1943 ctx = isl_basic_map_get_ctx(bmap);
1944 if (!is_residue(bmap->ineq[i], bmap->ineq[l], sum, total,
1945 &ctx->normalize_gcd))
1946 return bmap;
1947
1948 if (!is_residue_div_pair(bmap, l, div, v_div, i, j, ctx->normalize_gcd))
1949 return bmap;
1950
1951 v = isl_vec_alloc(ctx, 1 + total);
1952 if (!v)
1953 return isl_basic_map_free(bmap);
1954 isl_seq_cpy(v->el, bmap->ineq[l], 1 + total);
1955 isl_seq_sub(v->el, bmap->ineq[i], 1 + total);
1957 isl_int_add(v->el[0], v->el[0], v->el[1 + v_div + div]);
1958 isl_int_sub_ui(v->el[0], v->el[0], 1);
1959 bmap = set_div_from_lower_bound_if_better(bmap, div, v->el, progress);
1960 isl_vec_free(v);
1961
1962 return bmap;
1963}
1964
1965/* Given a lower bound constraint at position "l" of "bmap"
1966 * on local variable "div" such that the sum of the constant terms
1967 * of this constraint and the corresponding upper bound is equal to "sum",
1968 * check if some other pair of constraints can be found in "opposite"
1969 * that can be used to derive a reduced expression for the local variable.
1970 * "opposite" describes all pairs of opposite constraints.
1971 * It is an array of size equal to the number of inequality constraints
1972 * with as entries either
1973 * - zero, if the constraint has no opposite, or
1974 * - 1 + the position of the opposite constraint.
1975 * Set *progress if anything is changed.
1976 *
1977 * In particular, let l be the constraint
1978 *
1979 * -n f + g + m n e + c1 >= 0
1980 *
1981 * The opposite constraint is
1982 *
1983 * n f - g - m n e + c2 >= 0
1984 *
1985 * with c1 + c2 equal to "sum".
1986 *
1987 * Look for a pair of constraints
1988 *
1989 * g + c3 >= 0
1990 * -g + c4 >= 0
1991 *
1992 * with c3 + c4 < m n that can be used to obtain a reduced expression for e.
1993 * First look for any pair of constraints with constant terms
1994 * that satisfy c3 + c4 < m n and then check whether either of them
1995 * can be used in this way.
1996 */
1998 __isl_take isl_basic_map *bmap, int *opposite, int l, isl_int sum,
1999 int div, int *progress)
2000{
2001 int i;
2002 isl_size n, v_div, total;
2003 isl_ctx *ctx;
2004
2008 if (v_div < 0 || total < 0 || n < 0)
2009 return isl_basic_map_free(bmap);
2010
2011 ctx = isl_basic_map_get_ctx(bmap);
2012 for (i = 0; i < n; ++i) {
2013 int j;
2014
2015 if (i == l)
2016 continue;
2017 if (!opposite[i])
2018 continue;
2019 j = opposite[i] - 1;
2020 if (j < i)
2021 continue;
2022 if (j == l)
2023 continue;
2024 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2025 continue;
2027 bmap->ineq[i][0], bmap->ineq[j][0]);
2029 bmap->ineq[l][1 + v_div + div]))
2030 continue;
2031 bmap = set_residue_div(bmap, l, sum, div, v_div, total, i, j,
2032 progress);
2033 bmap = set_residue_div(bmap, l, sum, div, v_div, total, j, i,
2034 progress);
2035 }
2036
2037 return bmap;
2038}
2039
2040/* Given two constraints "k" and "l" that are opposite to each other,
2041 * except for the constant term, check if we can use them
2042 * to obtain an expression for one of the hitherto unknown divs or
2043 * a "better" expression for a div for which we already have an expression.
2044 * "sum" is the sum of the constant terms of the constraints.
2045 * If this sum is strictly smaller than the coefficient of one
2046 * of the divs, then this pair can be used to define the div.
2047 * Use the lower bound of this pair if it is better than
2048 * any previously known expression and
2049 * if there is no risk of introducing circular definitions.
2050 * If, moreover, this coefficient is a non-trivial multiple
2051 * of a value greater than the sum and if some other pair of constraints
2052 * can be found that can be used to eliminate coefficients
2053 * that are not a multiple of this value, then a more simplified
2054 * expression can be obtained.
2055 * "opposite" describes all pairs of opposite constraints.
2056 * It is an array of size equal to the number of inequality constraints
2057 * with as entries either
2058 * - zero, if the constraint has no opposite, or
2059 * - 1 + the position of the opposite constraint.
2060 */
2062 __isl_take isl_basic_map *bmap, int *opposite,
2063 int k, int l, isl_int sum, int *progress)
2064{
2065 int i;
2066 unsigned total = isl_basic_map_offset(bmap, isl_dim_div);
2067 isl_size n_div = isl_basic_map_dim(bmap, isl_dim_div);
2068
2069 if (n_div < 0)
2070 return isl_basic_map_free(bmap);
2071
2072 for (i = 0; i < n_div; ++i) {
2073 int div_set = 0;
2074 int c;
2075
2076 if (isl_int_is_zero(bmap->ineq[k][total + i]))
2077 continue;
2078 if (isl_int_is_pos(bmap->ineq[k][total + i]))
2079 c = k;
2080 else
2081 c = l;
2082 if (isl_int_ge(sum, bmap->ineq[c][total + i]))
2083 continue;
2084 bmap = check_for_residue_div(bmap, opposite, c, sum, i,
2085 &div_set);
2087 bmap->ineq[c], &div_set);
2088 if (!bmap)
2089 return NULL;
2090 if (!div_set)
2091 continue;
2092 mark_progress(progress);
2093 break;
2094 }
2095 return bmap;
2096}
2097
2098/* Exploit the pairs of inequality constraints of "bmap"
2099 * with opposite coefficients. They are described by "opposite",
2100 * an array of size equal to the number of inequality constraints
2101 * with as entries either
2102 * - zero, if the constraint has no opposite, or
2103 * - 1 + the position of the opposite constraint.
2104 * Detect (better) integer division expressions if "detect_divs" is set.
2105 * Set *progress if any progress is made.
2106 *
2107 * For each pair of constraints with opposite coefficients,
2108 * consider the sum of the constant terms.
2109 * If the sum is smaller than zero, then the constraints conflict.
2110 * If the sum is equal to zero, then the constraints form
2111 * an equality constraint.
2112 * If the sum is greater than zero, then check whether this pair
2113 * can be used to simplify any other constraints and/or,
2114 * if "detect_divs" is set, whether a (better) integer division definition
2115 * can be read off from the pair.
2116 *
2117 * Only check each pair once (with k < l).
2118 */
2120 __isl_take isl_basic_map *bmap, int *opposite,
2121 int *progress, int detect_divs)
2122{
2123 int k, l;
2124 isl_int sum;
2125
2126 if (!opposite)
2127 return bmap;
2128 isl_int_init(sum);
2129 for (k = 0; bmap && k < bmap->n_ineq-1; ++k) {
2130 if (!opposite[k])
2131 continue;
2132 l = opposite[k] - 1;
2133 if (l < k)
2134 continue;
2135 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
2136 if (isl_int_is_pos(sum)) {
2137 int residue = 0;
2138
2139 bmap = check_for_residues(bmap, k, l, sum, &residue);
2140 if (detect_divs)
2141 bmap = check_for_div_constraints(bmap, opposite,
2142 k, l, sum, progress);
2143 if (!residue)
2144 continue;
2145 mark_progress(progress);
2146 break;
2147 }
2148 if (isl_int_is_zero(sum)) {
2149 /* We need to break out of the loop after these
2150 * changes since the contents of the hash
2151 * will no longer be valid.
2152 * Plus, we probably we want to regauss first.
2153 */
2154 mark_progress(progress);
2157 } else
2158 bmap = isl_basic_map_set_to_empty(bmap);
2159 break;
2160 }
2161 isl_int_clear(sum);
2162 free(opposite);
2163
2164 return bmap;
2165}
2166
2167/* Look for pairs of constraints that have equal or opposite coefficients.
2168 * Detect (better) integer division expressions if "detect_divs" is set.
2169 *
2170 * For each pair of constraints with equal coefficients, only keep
2171 * the one which imposes the most stringent constraint, i.e.,
2172 * the one with the smallest constant term.
2173 * Detect opposite constraints and handle them
2174 * in exploit_opposite_constraints.
2175 */
2177 __isl_take isl_basic_map *bmap, int *progress, int detect_divs)
2178{
2179 struct isl_constraint_index ci;
2180 int k, l, h;
2182 int *opposite;
2183
2184 if (total < 0 || bmap->n_ineq <= 1)
2185 return bmap;
2186
2187 if (create_constraint_index(&ci, bmap) < 0)
2188 return bmap;
2189
2190 h = isl_seq_get_hash_bits(bmap->ineq[0] + 1, total, ci.bits);
2191 ci.index[h] = &bmap->ineq[0];
2192 for (k = 1; k < bmap->n_ineq; ++k) {
2193 h = hash_index(&ci, bmap, k);
2194 if (!ci.index[h]) {
2195 ci.index[h] = &bmap->ineq[k];
2196 continue;
2197 }
2198 l = ci.index[h] - &bmap->ineq[0];
2199 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
2200 swap_inequality(bmap, k, l);
2202 --k;
2203 }
2204 opposite = detect_opposites(&ci, bmap);
2206 return exploit_opposite_constraints(bmap, opposite, progress,
2207 detect_divs);
2208}
2209
2210/* Detect all pairs of inequalities that form an equality.
2211 *
2212 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
2213 * Call it repeatedly while it is making progress.
2214 */
2216 __isl_take isl_basic_map *bmap, int *progress)
2217{
2218 int duplicate;
2219
2220 do {
2221 duplicate = 0;
2223 &duplicate, 0);
2224 if (duplicate)
2225 mark_progress(progress);
2226 } while (duplicate);
2227
2228 return bmap;
2229}
2230
2231/* Given a known integer division "div" that is not integral
2232 * (with denominator 1), eliminate it from the constraints in "bmap"
2233 * where it appears with a (positive or negative) unit coefficient.
2234 * If "progress" is not NULL, then it gets set if the elimination
2235 * results in any changes.
2236 *
2237 * That is, replace
2238 *
2239 * floor(e/m) + f >= 0
2240 *
2241 * by
2242 *
2243 * e + m f >= 0
2244 *
2245 * and
2246 *
2247 * -floor(e/m) + f >= 0
2248 *
2249 * by
2250 *
2251 * -e + m f + m - 1 >= 0
2252 *
2253 * The first conversion is valid because floor(e/m) >= -f is equivalent
2254 * to e/m >= -f because -f is an integral expression.
2255 * The second conversion follows from the fact that
2256 *
2257 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
2258 *
2259 *
2260 * Note that one of the div constraints may have been eliminated
2261 * due to being redundant with respect to the constraint that is
2262 * being modified by this function. The modified constraint may
2263 * no longer imply this div constraint, so we add it back to make
2264 * sure we do not lose any information.
2265 */
2267 __isl_take isl_basic_map *bmap, int div, int *progress)
2268{
2269 int j;
2270 isl_size v_div, dim;
2271 isl_ctx *ctx;
2272
2274 dim = isl_basic_map_dim(bmap, isl_dim_all);
2275 if (v_div < 0 || dim < 0)
2276 return isl_basic_map_free(bmap);
2277
2278 ctx = isl_basic_map_get_ctx(bmap);
2279
2280 for (j = 0; j < bmap->n_ineq; ++j) {
2281 int s;
2282
2283 if (!isl_int_is_one(bmap->ineq[j][1 + v_div + div]) &&
2284 !isl_int_is_negone(bmap->ineq[j][1 + v_div + div]))
2285 continue;
2286
2287 mark_progress(progress);
2288
2289 s = isl_int_sgn(bmap->ineq[j][1 + v_div + div]);
2290 isl_int_set_si(bmap->ineq[j][1 + v_div + div], 0);
2291 if (s < 0)
2292 isl_seq_combine(bmap->ineq[j],
2293 ctx->negone, bmap->div[div] + 1,
2294 bmap->div[div][0], bmap->ineq[j], 1 + dim);
2295 else
2296 isl_seq_combine(bmap->ineq[j],
2297 ctx->one, bmap->div[div] + 1,
2298 bmap->div[div][0], bmap->ineq[j], 1 + dim);
2299 if (s < 0) {
2300 isl_int_add(bmap->ineq[j][0],
2301 bmap->ineq[j][0], bmap->div[div][0]);
2302 isl_int_sub_ui(bmap->ineq[j][0],
2303 bmap->ineq[j][0], 1);
2304 }
2305
2306 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
2307 bmap = isl_basic_map_add_div_constraint(bmap, div, s);
2308 if (!bmap)
2309 return NULL;
2310 }
2311
2312 return bmap;
2313}
2314
2315/* Eliminate selected known divs from constraints where they appear with
2316 * a (positive or negative) unit coefficient.
2317 * In particular, only handle those for which "select" returns isl_bool_true.
2318 * If "progress" is not NULL, then it gets set if the elimination
2319 * results in any changes.
2320 *
2321 * We skip integral divs, i.e., those with denominator 1, as we would
2322 * risk eliminating the div from the div constraints.
2323 * They are eliminated in eliminate_integral_divs instead.
2324 */
2327 isl_bool (*select)(__isl_keep isl_basic_map *bmap, int div),
2328 int *progress)
2329{
2330 int i;
2331 isl_size n_div;
2332
2333 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2334 if (n_div < 0)
2335 return isl_basic_map_free(bmap);
2336
2337 for (i = 0; i < n_div; ++i) {
2338 isl_bool skip;
2339 isl_bool selected;
2340
2342 if (skip >= 0 && !skip)
2343 skip = isl_basic_map_div_is_integral(bmap, i);
2344 if (skip < 0)
2345 return isl_basic_map_free(bmap);
2346 if (skip)
2347 continue;
2348 selected = select(bmap, i);
2349 if (selected < 0)
2350 return isl_basic_map_free(bmap);
2351 if (!selected)
2352 continue;
2353 bmap = eliminate_unit_div(bmap, i, progress);
2354 if (!bmap)
2355 return NULL;
2356 }
2357
2358 return bmap;
2359}
2360
2361/* eliminate_selected_unit_divs callback that selects every
2362 * integer division.
2363 */
2365{
2366 return isl_bool_true;
2367}
2368
2369/* Eliminate known divs from constraints where they appear with
2370 * a (positive or negative) unit coefficient.
2371 * If "progress" is not NULL, then it gets set if the elimination
2372 * results in any changes.
2373 */
2375 __isl_take isl_basic_map *bmap, int *progress)
2376{
2377 return eliminate_selected_unit_divs(bmap, &is_any_div, progress);
2378}
2379
2380/* eliminate_selected_unit_divs callback that selects
2381 * integer divisions that only appear with
2382 * a (positive or negative) unit coefficient
2383 * (outside their div constraints).
2384 */
2386{
2387 int i;
2388 isl_size v_div, n_ineq;
2389
2391 n_ineq = isl_basic_map_n_inequality(bmap);
2392 if (v_div < 0 || n_ineq < 0)
2393 return isl_bool_error;
2394
2395 for (i = 0; i < n_ineq; ++i) {
2396 isl_bool skip;
2397
2398 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2399 continue;
2401 bmap->ineq[i], div);
2402 if (skip < 0)
2403 return isl_bool_error;
2404 if (skip)
2405 continue;
2406 if (!isl_int_is_one(bmap->ineq[i][1 + v_div + div]) &&
2407 !isl_int_is_negone(bmap->ineq[i][1 + v_div + div]))
2408 return isl_bool_false;
2409 }
2410
2411 return isl_bool_true;
2412}
2413
2414/* Eliminate known divs from constraints where they appear with
2415 * a (positive or negative) unit coefficient,
2416 * but only if they do not appear in any other constraints
2417 * (other than the div constraints).
2418 */
2424
2426{
2427 int progress = 1;
2428 if (!bmap)
2429 return NULL;
2430 while (progress) {
2431 isl_bool empty;
2432
2433 progress = 0;
2434 empty = isl_basic_map_plain_is_empty(bmap);
2435 if (empty < 0)
2436 return isl_basic_map_free(bmap);
2437 if (empty)
2438 break;
2440 bmap = reduce_div_coefficients(bmap);
2441 bmap = normalize_div_expressions(bmap);
2442 bmap = remove_duplicate_divs(bmap, &progress);
2443 bmap = eliminate_unit_divs(bmap, &progress);
2444 bmap = eliminate_divs_eq(bmap, &progress);
2445 bmap = eliminate_divs_ineq(bmap, &progress);
2446 bmap = eliminate_integral_divs(bmap, &progress);
2447 bmap = isl_basic_map_gauss(bmap, &progress);
2448 /* requires equalities in normal form */
2449 bmap = normalize_divs(bmap, &progress);
2451 &progress, 1);
2452 }
2453 return bmap;
2454}
2455
2461
2462
2464 isl_int *constraint, unsigned div)
2465{
2466 unsigned pos;
2467
2468 if (!bmap)
2469 return isl_bool_error;
2470
2471 pos = isl_basic_map_offset(bmap, isl_dim_div) + div;
2472
2473 if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
2474 int neg;
2475 isl_int_sub(bmap->div[div][1],
2476 bmap->div[div][1], bmap->div[div][0]);
2477 isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2478 neg = isl_seq_is_neg(constraint, bmap->div[div]+1, pos);
2479 isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2480 isl_int_add(bmap->div[div][1],
2481 bmap->div[div][1], bmap->div[div][0]);
2482 if (!neg)
2483 return isl_bool_false;
2484 if (isl_seq_any_non_zero(constraint+pos+1,
2485 bmap->n_div-div-1))
2486 return isl_bool_false;
2487 } else if (isl_int_abs_eq(constraint[pos], bmap->div[div][0])) {
2488 if (!isl_seq_eq(constraint, bmap->div[div]+1, pos))
2489 return isl_bool_false;
2490 if (isl_seq_any_non_zero(constraint+pos+1,
2491 bmap->n_div-div-1))
2492 return isl_bool_false;
2493 } else
2494 return isl_bool_false;
2495
2496 return isl_bool_true;
2497}
2498
2499/* If the only constraints a div d=floor(f/m)
2500 * appears in are its two defining constraints
2501 *
2502 * f - m d >=0
2503 * -(f - (m - 1)) + m d >= 0
2504 *
2505 * then it can safely be removed.
2506 */
2508{
2509 int i;
2510 isl_bool involves;
2512 unsigned pos = 1 + v_div + div;
2513
2514 if (v_div < 0)
2515 return isl_bool_error;
2516
2517 for (i = 0; i < bmap->n_eq; ++i)
2518 if (!isl_int_is_zero(bmap->eq[i][pos]))
2519 return isl_bool_false;
2520
2521 for (i = 0; i < bmap->n_ineq; ++i) {
2522 isl_bool red;
2523
2524 if (isl_int_is_zero(bmap->ineq[i][pos]))
2525 continue;
2526 red = isl_basic_map_is_div_constraint(bmap, bmap->ineq[i], div);
2527 if (red < 0 || !red)
2528 return red;
2529 }
2530
2531 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
2532 if (involves < 0 || involves)
2533 return isl_bool_not(involves);
2534
2535 return isl_bool_true;
2536}
2537
2538/*
2539 * Remove divs that don't occur in any of the constraints or other divs.
2540 * These can arise when dropping constraints from a basic map or
2541 * when the divs of a basic map have been temporarily aligned
2542 * with the divs of another basic map.
2543 */
2546{
2547 int i;
2548 isl_size v_div;
2549
2551 if (v_div < 0)
2552 return isl_basic_map_free(bmap);
2553
2554 for (i = bmap->n_div-1; i >= 0; --i) {
2555 isl_bool redundant;
2556
2557 redundant = div_is_redundant(bmap, i);
2558 if (redundant < 0)
2559 return isl_basic_map_free(bmap);
2560 if (!redundant)
2561 continue;
2563 v_div + i, 1);
2564 bmap = isl_basic_map_drop_div(bmap, i);
2565 }
2566 return bmap;
2567}
2568
2569/* Mark "bmap" as final, without checking for obviously redundant
2570 * integer divisions. This function should be used when "bmap"
2571 * is known not to involve any such integer divisions.
2572 */
2575{
2576 if (!bmap)
2577 return NULL;
2579 return bmap;
2580}
2581
2582/* Mark "bmap" as final, after removing obviously redundant integer divisions.
2583 */
2585{
2586 bmap = remove_redundant_divs(bmap);
2587 bmap = isl_basic_map_mark_final(bmap);
2588 return bmap;
2589}
2590
2596
2597/* Remove definition of any div that is defined in terms of the given variable.
2598 * The div itself is not removed. Functions such as
2599 * eliminate_divs_ineq depend on the other divs remaining in place.
2600 */
2602 __isl_take isl_basic_map *bmap, int pos)
2603{
2604 int i;
2605
2606 if (!bmap)
2607 return NULL;
2608
2609 for (i = 0; i < bmap->n_div; ++i) {
2610 if (isl_int_is_zero(bmap->div[i][0]))
2611 continue;
2612 if (isl_int_is_zero(bmap->div[i][1+1+pos]))
2613 continue;
2614 bmap = isl_basic_map_mark_div_unknown(bmap, i);
2615 if (!bmap)
2616 return NULL;
2617 }
2618 return bmap;
2619}
2620
2621/* Eliminate the specified variables from the constraints using
2622 * Fourier-Motzkin. The variables themselves are not removed.
2623 */
2625 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
2626{
2627 int d;
2628 int i, j, k;
2630 int need_gauss = 0;
2631
2632 if (n == 0)
2633 return bmap;
2635 if (total < 0)
2636 return isl_basic_map_free(bmap);
2637
2638 bmap = isl_basic_map_cow(bmap);
2639 for (d = pos + n - 1; d >= 0 && d >= pos; --d)
2640 bmap = remove_dependent_vars(bmap, d);
2641 if (!bmap)
2642 return NULL;
2643
2644 for (d = pos + n - 1;
2645 d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
2646 isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
2647 for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
2648 int n_lower, n_upper;
2649 if (!bmap)
2650 return NULL;
2651 for (i = 0; i < bmap->n_eq; ++i) {
2652 if (isl_int_is_zero(bmap->eq[i][1+d]))
2653 continue;
2654 bmap = eliminate_var_using_equality(bmap, d,
2655 bmap->eq[i], 0, 1, NULL);
2656 if (isl_basic_map_drop_equality(bmap, i) < 0)
2657 return isl_basic_map_free(bmap);
2658 need_gauss = 1;
2659 break;
2660 }
2661 if (i < bmap->n_eq)
2662 continue;
2663 n_lower = 0;
2664 n_upper = 0;
2665 for (i = 0; i < bmap->n_ineq; ++i) {
2666 if (isl_int_is_pos(bmap->ineq[i][1+d]))
2667 n_lower++;
2668 else if (isl_int_is_neg(bmap->ineq[i][1+d]))
2669 n_upper++;
2670 }
2672 0, n_lower * n_upper);
2673 if (!bmap)
2674 goto error;
2675 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2676 int last;
2677 if (isl_int_is_zero(bmap->ineq[i][1+d]))
2678 continue;
2679 last = -1;
2680 for (j = 0; j < i; ++j) {
2681 if (isl_int_is_zero(bmap->ineq[j][1+d]))
2682 continue;
2683 last = j;
2684 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
2685 isl_int_sgn(bmap->ineq[j][1+d]))
2686 continue;
2688 if (k < 0)
2689 goto error;
2690 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
2691 1+total);
2692 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
2693 1+d, 1+total, NULL);
2694 }
2696 i = last + 1;
2697 }
2698 if (n_lower > 0 && n_upper > 0) {
2701 NULL, 0);
2702 bmap = isl_basic_map_gauss(bmap, NULL);
2704 need_gauss = 0;
2705 if (!bmap)
2706 goto error;
2708 break;
2709 }
2710 }
2711 if (need_gauss)
2712 bmap = isl_basic_map_gauss(bmap, NULL);
2713 return bmap;
2714error:
2715 isl_basic_map_free(bmap);
2716 return NULL;
2717}
2718
2725
2726/* Eliminate the specified n dimensions starting at first from the
2727 * constraints, without removing the dimensions from the space.
2728 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2729 * Otherwise, they are projected out and the original space is restored.
2730 */
2733 enum isl_dim_type type, unsigned first, unsigned n)
2734{
2735 isl_space *space;
2736
2737 if (!bmap)
2738 return NULL;
2739 if (n == 0)
2740 return bmap;
2741
2742 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2743 return isl_basic_map_free(bmap);
2744
2746 first += isl_basic_map_offset(bmap, type) - 1;
2747 bmap = isl_basic_map_eliminate_vars(bmap, first, n);
2748 return isl_basic_map_finalize(bmap);
2749 }
2750
2751 space = isl_basic_map_get_space(bmap);
2752 bmap = isl_basic_map_project_out(bmap, type, first, n);
2753 bmap = isl_basic_map_insert_dims(bmap, type, first, n);
2754 bmap = isl_basic_map_reset_space(bmap, space);
2755 return bmap;
2756}
2757
2760 enum isl_dim_type type, unsigned first, unsigned n)
2761{
2762 return isl_basic_map_eliminate(bset, type, first, n);
2763}
2764
2765/* Remove all constraints from "bmap" that reference any unknown local
2766 * variables (directly or indirectly).
2767 *
2768 * Dropping all constraints on a local variable will make it redundant,
2769 * so it will get removed implicitly by
2770 * isl_basic_map_drop_constraints_involving_dims. Some other local
2771 * variables may also end up becoming redundant if they only appear
2772 * in constraints together with the unknown local variable.
2773 * Therefore, start over after calling
2774 * isl_basic_map_drop_constraints_involving_dims.
2775 */
2778{
2779 isl_bool known;
2780 isl_size n_div;
2781 int i, o_div;
2782
2783 known = isl_basic_map_divs_known(bmap);
2784 if (known < 0)
2785 return isl_basic_map_free(bmap);
2786 if (known)
2787 return bmap;
2788
2789 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2790 if (n_div < 0)
2791 return isl_basic_map_free(bmap);
2792 o_div = isl_basic_map_offset(bmap, isl_dim_div) - 1;
2793
2794 for (i = 0; i < n_div; ++i) {
2795 known = isl_basic_map_div_is_known(bmap, i);
2796 if (known < 0)
2797 return isl_basic_map_free(bmap);
2798 if (known)
2799 continue;
2800 bmap = remove_dependent_vars(bmap, o_div + i);
2802 isl_dim_div, i, 1);
2803 n_div = isl_basic_map_dim(bmap, isl_dim_div);
2804 if (n_div < 0)
2805 return isl_basic_map_free(bmap);
2806 i = -1;
2807 }
2808
2809 return bmap;
2810}
2811
2812/* Remove all constraints from "bset" that reference any unknown local
2813 * variables (directly or indirectly).
2814 */
2824
2825/* Remove all constraints from "map" that reference any unknown local
2826 * variables (directly or indirectly).
2827 *
2828 * Since constraints may get dropped from the basic maps,
2829 * they may no longer be disjoint from each other.
2830 */
2833{
2834 int i;
2835 isl_bool known;
2836
2837 known = isl_map_divs_known(map);
2838 if (known < 0)
2839 return isl_map_free(map);
2840 if (known)
2841 return map;
2842
2843 map = isl_map_cow(map);
2844 if (!map)
2845 return NULL;
2846
2847 for (i = 0; i < map->n; ++i) {
2848 map->p[i] =
2850 map->p[i]);
2851 if (!map->p[i])
2852 return isl_map_free(map);
2853 }
2854
2855 if (map->n > 1)
2857
2858 return map;
2859}
2860
2861/* Don't assume equalities are in order, because align_divs
2862 * may have changed the order of the divs.
2863 */
2865 unsigned len)
2866{
2867 int d, i;
2868
2869 for (d = 0; d < len; ++d)
2870 elim[d] = -1;
2871 for (i = 0; i < bmap->n_eq; ++i) {
2872 for (d = len - 1; d >= 0; --d) {
2873 if (isl_int_is_zero(bmap->eq[i][1+d]))
2874 continue;
2875 elim[d] = i;
2876 break;
2877 }
2878 }
2879}
2880
2882 int *elim, unsigned len)
2883{
2884 compute_elimination_index(bset_to_bmap(bset), elim, len);
2885}
2886
2888 __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
2889{
2890 int d;
2891 int copied = 0;
2892
2893 for (d = total - 1; d >= 0; --d) {
2894 if (isl_int_is_zero(src[1+d]))
2895 continue;
2896 if (elim[d] == -1)
2897 continue;
2898 if (!copied) {
2899 isl_seq_cpy(dst, src, 1 + total);
2900 copied = 1;
2901 }
2902 isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
2903 }
2904 return copied;
2905}
2906
2908 __isl_keep isl_basic_set *bset, int *elim, unsigned total)
2909{
2910 return reduced_using_equalities(dst, src,
2911 bset_to_bmap(bset), elim, total);
2912}
2913
2916{
2917 int i;
2918 int *elim;
2919 isl_size dim;
2920
2921 if (!bset || !context)
2922 goto error;
2923
2924 if (context->n_eq == 0) {
2926 return bset;
2927 }
2928
2929 bset = isl_basic_set_cow(bset);
2930 dim = isl_basic_set_dim(bset, isl_dim_set);
2931 if (dim < 0)
2932 goto error;
2933
2934 elim = isl_alloc_array(bset->ctx, int, dim);
2935 if (!elim)
2936 goto error;
2938 for (i = 0; i < bset->n_eq; ++i)
2939 set_reduced_using_equalities(bset->eq[i], bset->eq[i],
2940 context, elim, dim);
2941 for (i = 0; i < bset->n_ineq; ++i)
2942 set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
2943 context, elim, dim);
2945 free(elim);
2946 bset = isl_basic_set_simplify(bset);
2947 bset = isl_basic_set_finalize(bset);
2948 return bset;
2949error:
2950 isl_basic_set_free(bset);
2952 return NULL;
2953}
2954
2955/* For each inequality in "ineq" that is a shifted (more relaxed)
2956 * copy of an inequality in "context", mark the corresponding entry
2957 * in "row" with -1.
2958 * If an inequality only has a non-negative constant term, then
2959 * mark it as well.
2960 */
2962 __isl_keep isl_basic_set *context, int *row)
2963{
2964 struct isl_constraint_index ci;
2965 isl_size n_ineq, cols;
2966 unsigned total;
2967 int k;
2968
2969 if (!ineq || !context)
2970 return isl_stat_error;
2971 if (context->n_ineq == 0)
2972 return isl_stat_ok;
2973 if (setup_constraint_index(&ci, context) < 0)
2974 return isl_stat_error;
2975
2976 n_ineq = isl_mat_rows(ineq);
2977 cols = isl_mat_cols(ineq);
2978 if (n_ineq < 0 || cols < 0)
2979 return isl_stat_error;
2980 total = cols - 1;
2981 for (k = 0; k < n_ineq; ++k) {
2982 int l;
2983 isl_bool redundant;
2984
2985 l = isl_seq_first_non_zero(ineq->row[k] + 1, total);
2986 if (l < 0 && isl_int_is_nonneg(ineq->row[k][0])) {
2987 row[k] = -1;
2988 continue;
2989 }
2990 redundant = constraint_index_is_redundant(&ci, ineq->row[k]);
2991 if (redundant < 0)
2992 goto error;
2993 if (!redundant)
2994 continue;
2995 row[k] = -1;
2996 }
2998 return isl_stat_ok;
2999error:
3001 return isl_stat_error;
3002}
3003
3006{
3007 struct isl_constraint_index ci;
3008 int k;
3009
3010 if (!bset || !context)
3011 return bset;
3012
3013 if (context->n_ineq == 0)
3014 return bset;
3015 if (setup_constraint_index(&ci, context) < 0)
3016 return bset;
3017
3018 for (k = 0; k < bset->n_ineq; ++k) {
3019 isl_bool redundant;
3020
3021 redundant = constraint_index_is_redundant(&ci, bset->ineq[k]);
3022 if (redundant < 0)
3023 goto error;
3024 if (!redundant)
3025 continue;
3026 bset = isl_basic_set_cow(bset);
3027 if (!bset)
3028 goto error;
3030 --k;
3031 }
3033 return bset;
3034error:
3036 return bset;
3037}
3038
3039/* Remove constraints from "bmap" that are identical to constraints
3040 * in "context" or that are more relaxed (greater constant term).
3041 *
3042 * We perform the test for shifted copies on the pure constraints
3043 * in remove_shifted_constraints.
3044 */
3047{
3048 isl_basic_set *bset, *bset_context;
3049
3050 if (!bmap || !context)
3051 goto error;
3052
3053 if (bmap->n_ineq == 0 || context->n_ineq == 0) {
3055 return bmap;
3056 }
3057
3059 context);
3061
3063 bmap = isl_basic_map_align_divs(bmap, context);
3065
3067 bset_context = isl_basic_map_underlying_set(context);
3068 bset = remove_shifted_constraints(bset, bset_context);
3069 isl_basic_set_free(bset_context);
3070
3071 bmap = isl_basic_map_overlying_set(bset, bmap);
3072
3073 return bmap;
3074error:
3075 isl_basic_map_free(bmap);
3077 return NULL;
3078}
3079
3080/* Does the (linear part of a) constraint "c" involve any of the "len"
3081 * "relevant" dimensions?
3082 */
3083static int is_related(isl_int *c, int len, int *relevant)
3084{
3085 int i;
3086
3087 for (i = 0; i < len; ++i) {
3088 if (!relevant[i])
3089 continue;
3090 if (!isl_int_is_zero(c[i]))
3091 return 1;
3092 }
3093
3094 return 0;
3095}
3096
3097/* Drop constraints from "bmap" that do not involve any of
3098 * the dimensions marked "relevant".
3099 */
3101 __isl_take isl_basic_map *bmap, int *relevant)
3102{
3103 int i;
3104 isl_size dim;
3105
3106 dim = isl_basic_map_dim(bmap, isl_dim_all);
3107 if (dim < 0)
3108 return isl_basic_map_free(bmap);
3109 for (i = 0; i < dim; ++i)
3110 if (!relevant[i])
3111 break;
3112 if (i >= dim)
3113 return bmap;
3114
3115 for (i = bmap->n_eq - 1; i >= 0; --i)
3116 if (!is_related(bmap->eq[i] + 1, dim, relevant)) {
3117 bmap = isl_basic_map_cow(bmap);
3118 if (isl_basic_map_drop_equality(bmap, i) < 0)
3119 return isl_basic_map_free(bmap);
3120 }
3121
3122 for (i = bmap->n_ineq - 1; i >= 0; --i)
3123 if (!is_related(bmap->ineq[i] + 1, dim, relevant)) {
3124 bmap = isl_basic_map_cow(bmap);
3125 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3126 return isl_basic_map_free(bmap);
3127 }
3128
3129 return bmap;
3130}
3131
3132/* Update the groups in "group" based on the (linear part of a) constraint "c".
3133 *
3134 * In particular, for any variable involved in the constraint,
3135 * find the actual group id from before and replace the group
3136 * of the corresponding variable by the minimal group of all
3137 * the variables involved in the constraint considered so far
3138 * (if this minimum is smaller) or replace the minimum by this group
3139 * (if the minimum is larger).
3140 *
3141 * At the end, all the variables in "c" will (indirectly) point
3142 * to the minimal of the groups that they referred to originally.
3143 */
3144static void update_groups(int dim, int *group, isl_int *c)
3145{
3146 int j;
3147 int min = dim;
3148
3149 for (j = 0; j < dim; ++j) {
3150 if (isl_int_is_zero(c[j]))
3151 continue;
3152 while (group[j] >= 0 && group[group[j]] != group[j])
3153 group[j] = group[group[j]];
3154 if (group[j] == min)
3155 continue;
3156 if (group[j] < min) {
3157 if (min >= 0 && min < dim)
3158 group[min] = group[j];
3159 min = group[j];
3160 } else
3161 group[group[j]] = min;
3162 }
3163}
3164
3165/* Allocate an array of groups of variables, one for each variable
3166 * in "context", initialized to zero.
3167 */
3169{
3170 isl_ctx *ctx;
3171 isl_size dim;
3172
3174 if (dim < 0)
3175 return NULL;
3177 return isl_calloc_array(ctx, int, dim);
3178}
3179
3180/* Drop constraints from "bmap" that only involve variables that are
3181 * not related to any of the variables marked with a "-1" in "group".
3182 *
3183 * We construct groups of variables that collect variables that
3184 * (indirectly) appear in some common constraint of "bmap".
3185 * Each group is identified by the first variable in the group,
3186 * except for the special group of variables that was already identified
3187 * in the input as -1 (or are related to those variables).
3188 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
3189 * otherwise the group of i is the group of group[i].
3190 *
3191 * We first initialize groups for the remaining variables.
3192 * Then we iterate over the constraints of "bmap" and update the
3193 * group of the variables in the constraint by the smallest group.
3194 * Finally, we resolve indirect references to groups by running over
3195 * the variables.
3196 *
3197 * After computing the groups, we drop constraints that do not involve
3198 * any variables in the -1 group.
3199 */
3201 __isl_take isl_basic_map *bmap, __isl_take int *group)
3202{
3203 isl_size dim;
3204 int i;
3205 int last;
3206
3207 dim = isl_basic_map_dim(bmap, isl_dim_all);
3208 if (dim < 0)
3209 goto error;
3210
3211 last = -1;
3212 for (i = 0; i < dim; ++i)
3213 if (group[i] >= 0)
3214 last = group[i] = i;
3215 if (last < 0) {
3216 free(group);
3217 return bmap;
3218 }
3219
3220 for (i = 0; i < bmap->n_eq; ++i)
3221 update_groups(dim, group, bmap->eq[i] + 1);
3222 for (i = 0; i < bmap->n_ineq; ++i)
3223 update_groups(dim, group, bmap->ineq[i] + 1);
3224
3225 for (i = 0; i < dim; ++i)
3226 if (group[i] >= 0)
3227 group[i] = group[group[i]];
3228
3229 for (i = 0; i < dim; ++i)
3230 group[i] = group[i] == -1;
3231
3232 bmap = drop_unrelated_constraints(bmap, group);
3233
3234 free(group);
3235 return bmap;
3236error:
3237 free(group);
3238 isl_basic_map_free(bmap);
3239 return NULL;
3240}
3241
3242/* Drop constraints from "context" that are irrelevant for computing
3243 * the gist of "bset".
3244 *
3245 * In particular, drop constraints in variables that are not related
3246 * to any of the variables involved in the constraints of "bset"
3247 * in the sense that there is no sequence of constraints that connects them.
3248 *
3249 * We first mark all variables that appear in "bset" as belonging
3250 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
3251 */
3254{
3255 int *group;
3256 isl_size dim;
3257 int i, j;
3258
3259 dim = isl_basic_set_dim(bset, isl_dim_set);
3260 if (!context || dim < 0)
3262
3263 group = alloc_groups(context);
3264
3265 if (!group)
3267
3268 for (i = 0; i < dim; ++i) {
3269 for (j = 0; j < bset->n_eq; ++j)
3270 if (!isl_int_is_zero(bset->eq[j][1 + i]))
3271 break;
3272 if (j < bset->n_eq) {
3273 group[i] = -1;
3274 continue;
3275 }
3276 for (j = 0; j < bset->n_ineq; ++j)
3277 if (!isl_int_is_zero(bset->ineq[j][1 + i]))
3278 break;
3279 if (j < bset->n_ineq)
3280 group[i] = -1;
3281 }
3282
3284}
3285
3286/* Drop constraints from "context" that are irrelevant for computing
3287 * the gist of the inequalities "ineq".
3288 * Inequalities in "ineq" for which the corresponding element of row
3289 * is set to -1 have already been marked for removal and should be ignored.
3290 *
3291 * In particular, drop constraints in variables that are not related
3292 * to any of the variables involved in "ineq"
3293 * in the sense that there is no sequence of constraints that connects them.
3294 *
3295 * We first mark all variables that appear in "bset" as belonging
3296 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
3297 */
3300{
3301 int *group;
3302 isl_size dim;
3303 int i, j;
3304 isl_size n;
3305
3307 n = isl_mat_rows(ineq);
3308 if (dim < 0 || n < 0)
3310
3311 group = alloc_groups(context);
3312
3313 if (!group)
3315
3316 for (i = 0; i < dim; ++i) {
3317 for (j = 0; j < n; ++j) {
3318 if (row[j] < 0)
3319 continue;
3320 if (!isl_int_is_zero(ineq->row[j][1 + i]))
3321 break;
3322 }
3323 if (j < n)
3324 group[i] = -1;
3325 }
3326
3328}
3329
3330/* Do all "n" entries of "row" contain a negative value?
3331 */
3332static int all_neg(int *row, int n)
3333{
3334 int i;
3335
3336 for (i = 0; i < n; ++i)
3337 if (row[i] >= 0)
3338 return 0;
3339
3340 return 1;
3341}
3342
3343/* Update the inequalities in "bset" based on the information in "row"
3344 * and "tab".
3345 *
3346 * In particular, the array "row" contains either -1, meaning that
3347 * the corresponding inequality of "bset" is redundant, or the index
3348 * of an inequality in "tab".
3349 *
3350 * If the row entry is -1, then drop the inequality.
3351 * Otherwise, if the constraint is marked redundant in the tableau,
3352 * then drop the inequality. Similarly, if it is marked as an equality
3353 * in the tableau, then turn the inequality into an equality and
3354 * perform Gaussian elimination.
3355 */
3357 __isl_keep int *row, struct isl_tab *tab)
3358{
3359 int i;
3360 unsigned n_ineq;
3361 unsigned n_eq;
3362 int found_equality = 0;
3363
3364 if (!bset)
3365 return NULL;
3366 if (tab && tab->empty)
3367 return isl_basic_set_set_to_empty(bset);
3368
3369 n_ineq = bset->n_ineq;
3370 for (i = n_ineq - 1; i >= 0; --i) {
3371 if (row[i] < 0) {
3372 if (isl_basic_set_drop_inequality(bset, i) < 0)
3373 return isl_basic_set_free(bset);
3374 continue;
3375 }
3376 if (!tab)
3377 continue;
3378 n_eq = tab->n_eq;
3379 if (isl_tab_is_equality(tab, n_eq + row[i])) {
3381 found_equality = 1;
3382 } else if (isl_tab_is_redundant(tab, n_eq + row[i])) {
3383 if (isl_basic_set_drop_inequality(bset, i) < 0)
3384 return isl_basic_set_free(bset);
3385 }
3386 }
3387
3388 if (found_equality)
3389 bset = isl_basic_set_gauss(bset, NULL);
3390 bset = isl_basic_set_finalize(bset);
3391 return bset;
3392}
3393
3394/* Update the inequalities in "bset" based on the information in "row"
3395 * and "tab" and free all arguments (other than "bset").
3396 */
3400 struct isl_tab *tab)
3401{
3402 isl_mat_free(ineq);
3404
3405 bset = update_ineq(bset, row, tab);
3406
3407 free(row);
3408 isl_tab_free(tab);
3409 return bset;
3410}
3411
3412/* Remove all information from bset that is redundant in the context
3413 * of context.
3414 * "ineq" contains the (possibly transformed) inequalities of "bset",
3415 * in the same order.
3416 * The (explicit) equalities of "bset" are assumed to have been taken
3417 * into account by the transformation such that only the inequalities
3418 * are relevant.
3419 * "context" is assumed not to be empty.
3420 *
3421 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
3422 * A value of -1 means that the inequality is obviously redundant and may
3423 * not even appear in "tab".
3424 *
3425 * We first mark the inequalities of "bset"
3426 * that are obviously redundant with respect to some inequality in "context".
3427 * Then we remove those constraints from "context" that have become
3428 * irrelevant for computing the gist of "bset".
3429 * Note that this removal of constraints cannot be replaced by
3430 * a factorization because factors in "bset" may still be connected
3431 * to each other through constraints in "context".
3432 *
3433 * If there are any inequalities left, we construct a tableau for
3434 * the context and then add the inequalities of "bset".
3435 * Before adding these inequalities, we freeze all constraints such that
3436 * they won't be considered redundant in terms of the constraints of "bset".
3437 * Then we detect all redundant constraints (among the
3438 * constraints that weren't frozen), first by checking for redundancy in the
3439 * the tableau and then by checking if replacing a constraint by its negation
3440 * would lead to an empty set. This last step is fairly expensive
3441 * and could be optimized by more reuse of the tableau.
3442 * Finally, we update bset according to the results.
3443 */
3446{
3447 int i, r;
3448 int *row = NULL;
3449 isl_ctx *ctx;
3450 isl_basic_set *combined = NULL;
3451 struct isl_tab *tab = NULL;
3452 unsigned n_eq, context_ineq;
3453
3454 if (!bset || !ineq || !context)
3455 goto error;
3456
3457 if (bset->n_ineq == 0 || isl_basic_set_plain_is_universe(context)) {
3459 isl_mat_free(ineq);
3460 return bset;
3461 }
3462
3464 row = isl_calloc_array(ctx, int, bset->n_ineq);
3465 if (!row)
3466 goto error;
3467
3468 if (mark_shifted_constraints(ineq, context, row) < 0)
3469 goto error;
3470 if (all_neg(row, bset->n_ineq))
3471 return update_ineq_free(bset, ineq, context, row, NULL);
3472
3474 if (!context)
3475 goto error;
3477 return update_ineq_free(bset, ineq, context, row, NULL);
3478
3479 n_eq = context->n_eq;
3480 context_ineq = context->n_ineq;
3482 combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
3483 tab = isl_tab_from_basic_set(combined, 0);
3484 for (i = 0; i < context_ineq; ++i)
3485 if (isl_tab_freeze_constraint(tab, n_eq + i) < 0)
3486 goto error;
3487 if (isl_tab_extend_cons(tab, bset->n_ineq) < 0)
3488 goto error;
3489 r = context_ineq;
3490 for (i = 0; i < bset->n_ineq; ++i) {
3491 if (row[i] < 0)
3492 continue;
3493 combined = isl_basic_set_add_ineq(combined, ineq->row[i]);
3494 if (isl_tab_add_ineq(tab, ineq->row[i]) < 0)
3495 goto error;
3496 row[i] = r++;
3497 }
3499 goto error;
3500 if (isl_tab_detect_redundant(tab) < 0)
3501 goto error;
3502 for (i = bset->n_ineq - 1; i >= 0; --i) {
3504 int is_empty;
3505
3506 if (row[i] < 0)
3507 continue;
3508 r = row[i];
3509 if (tab->con[n_eq + r].is_redundant)
3510 continue;
3511 test = isl_basic_set_dup(combined);
3514 is_empty = isl_basic_set_is_empty(test);
3516 if (is_empty < 0)
3517 goto error;
3518 if (is_empty)
3519 tab->con[n_eq + r].is_redundant = 1;
3520 }
3521 bset = update_ineq_free(bset, ineq, context, row, tab);
3522 if (bset) {
3525 }
3526
3527 isl_basic_set_free(combined);
3528 return bset;
3529error:
3530 free(row);
3531 isl_mat_free(ineq);
3532 isl_tab_free(tab);
3533 isl_basic_set_free(combined);
3535 isl_basic_set_free(bset);
3536 return NULL;
3537}
3538
3539/* Extract the inequalities of "bset" as an isl_mat.
3540 */
3542{
3544 isl_ctx *ctx;
3545 isl_mat *ineq;
3546
3548 if (total < 0)
3549 return NULL;
3550
3551 ctx = isl_basic_set_get_ctx(bset);
3552 ineq = isl_mat_sub_alloc6(ctx, bset->ineq, 0, bset->n_ineq,
3553 0, 1 + total);
3554
3555 return ineq;
3556}
3557
3558/* Remove all information from "bset" that is redundant in the context
3559 * of "context", for the case where both "bset" and "context" are
3560 * full-dimensional.
3561 */
3564{
3565 isl_mat *ineq;
3566
3567 ineq = extract_ineq(bset);
3568 return uset_gist_full(bset, ineq, context);
3569}
3570
3571/* Replace "bset" by an empty basic set in the same space.
3572 */
3575{
3576 isl_space *space;
3577
3578 space = isl_basic_set_get_space(bset);
3579 isl_basic_set_free(bset);
3580 return isl_basic_set_empty(space);
3581}
3582
3583/* Remove all information from "bset" that is redundant in the context
3584 * of "context", for the case where the combined equalities of
3585 * "bset" and "context" allow for a compression that can be obtained
3586 * by preapplication of "T".
3587 * If the compression of "context" is empty, meaning that "bset" and
3588 * "context" do not intersect, then return the empty set.
3589 *
3590 * "bset" itself is not transformed by "T". Instead, the inequalities
3591 * are extracted from "bset" and those are transformed by "T".
3592 * uset_gist_full then determines which of the transformed inequalities
3593 * are redundant with respect to the transformed "context" and removes
3594 * the corresponding inequalities from "bset".
3595 *
3596 * After preapplying "T" to the inequalities, any common factor is
3597 * removed from the coefficients. If this results in a tightening
3598 * of the constant term, then the same tightening is applied to
3599 * the corresponding untransformed inequality in "bset".
3600 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
3601 *
3602 * g f'(x) + r >= 0
3603 *
3604 * with 0 <= r < g, then it is equivalent to
3605 *
3606 * f'(x) >= 0
3607 *
3608 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
3609 * subspace compressed by T since the latter would be transformed to
3610 *
3611 * g f'(x) >= 0
3612 */
3616{
3617 isl_ctx *ctx;
3618 isl_mat *ineq;
3619 int i;
3621 isl_int rem;
3622
3623 ineq = extract_ineq(bset);
3624 ineq = isl_mat_product(ineq, isl_mat_copy(T));
3626
3627 if (!ineq || !context)
3628 goto error;
3630 isl_mat_free(ineq);
3632 return replace_by_empty(bset);
3633 }
3634
3635 ctx = isl_mat_get_ctx(ineq);
3636 n_row = isl_mat_rows(ineq);
3637 n_col = isl_mat_cols(ineq);
3638 if (n_row < 0 || n_col < 0)
3639 goto error;
3640 isl_int_init(rem);
3641 for (i = 0; i < n_row; ++i) {
3642 isl_seq_gcd(ineq->row[i] + 1, n_col - 1, &ctx->normalize_gcd);
3644 continue;
3645 if (isl_int_is_one(ctx->normalize_gcd))
3646 continue;
3647 isl_seq_scale_down(ineq->row[i] + 1, ineq->row[i] + 1,
3648 ctx->normalize_gcd, n_col - 1);
3649 isl_int_fdiv_r(rem, ineq->row[i][0], ctx->normalize_gcd);
3650 isl_int_fdiv_q(ineq->row[i][0],
3651 ineq->row[i][0], ctx->normalize_gcd);
3652 if (isl_int_is_zero(rem))
3653 continue;
3654 bset = isl_basic_set_cow(bset);
3655 if (!bset)
3656 break;
3657 isl_int_sub(bset->ineq[i][0], bset->ineq[i][0], rem);
3658 }
3659 isl_int_clear(rem);
3660
3661 return uset_gist_full(bset, ineq, context);
3662error:
3663 isl_mat_free(ineq);
3665 isl_basic_set_free(bset);
3666 return NULL;
3667}
3668
3669/* Project "bset" onto the variables that are involved in "template".
3670 */
3673{
3674 int i;
3675 isl_size n;
3676
3677 n = isl_basic_set_dim(template, isl_dim_set);
3678 if (n < 0 || !template)
3679 return isl_basic_set_free(bset);
3680
3681 for (i = 0; i < n; ++i) {
3682 isl_bool involved;
3683
3684 involved = isl_basic_set_involves_dims(template,
3685 isl_dim_set, i, 1);
3686 if (involved < 0)
3687 return isl_basic_set_free(bset);
3688 if (involved)
3689 continue;
3690 bset = isl_basic_set_eliminate_vars(bset, i, 1);
3691 }
3692
3693 return bset;
3694}
3695
3696/* Remove all information from bset that is redundant in the context
3697 * of context. In particular, equalities that are linear combinations
3698 * of those in context are removed. Then the inequalities that are
3699 * redundant in the context of the equalities and inequalities of
3700 * context are removed.
3701 *
3702 * First of all, we drop those constraints from "context"
3703 * that are irrelevant for computing the gist of "bset".
3704 * Alternatively, we could factorize the intersection of "context" and "bset".
3705 *
3706 * We first compute the intersection of the integer affine hulls
3707 * of "bset" and "context",
3708 * compute the gist inside this intersection and then reduce
3709 * the constraints with respect to the equalities of the context
3710 * that only involve variables already involved in the input.
3711 * If the intersection of the affine hulls turns out to be empty,
3712 * then return the empty set.
3713 *
3714 * If two constraints are mutually redundant, then uset_gist_full
3715 * will remove the second of those constraints. We therefore first
3716 * sort the constraints so that constraints not involving existentially
3717 * quantified variables are given precedence over those that do.
3718 * We have to perform this sorting before the variable compression,
3719 * because that may effect the order of the variables.
3720 */
3723{
3724 isl_mat *eq;
3725 isl_mat *T;
3727 isl_basic_set *aff_context;
3729
3731 if (total < 0 || !context)
3732 goto error;
3733
3735
3737 aff = isl_basic_set_copy(bset);
3740 aff_context = isl_basic_set_copy(context);
3741 aff_context = isl_basic_set_plain_affine_hull(aff_context);
3742 aff = isl_basic_set_intersect(aff, aff_context);
3743 if (!aff)
3744 goto error;
3746 isl_basic_set_free(bset);
3748 return aff;
3749 }
3750 bset = isl_basic_set_sort_constraints(bset);
3751 if (aff->n_eq == 0) {
3753 return uset_gist_uncompressed(bset, context);
3754 }
3755 eq = isl_mat_sub_alloc6(bset->ctx, aff->eq, 0, aff->n_eq, 0, 1 + total);
3756 eq = isl_mat_cow(eq);
3757 T = isl_mat_variable_compression(eq, NULL);
3759 if (T && T->n_col == 0) {
3760 isl_mat_free(T);
3762 return replace_by_empty(bset);
3763 }
3764
3766 aff_context = project_onto_involved(aff_context, bset);
3767
3768 bset = uset_gist_compressed(bset, context, T);
3769 bset = isl_basic_set_reduce_using_equalities(bset, aff_context);
3770
3771 if (bset) {
3774 }
3775
3776 return bset;
3777error:
3778 isl_basic_set_free(bset);
3780 return NULL;
3781}
3782
3783/* Return the number of equality constraints in "bmap" that involve
3784 * local variables. This function assumes that Gaussian elimination
3785 * has been applied to the equality constraints.
3786 */
3788{
3789 int i;
3791
3792 if (!bmap)
3793 return -1;
3794
3795 if (bmap->n_eq == 0)
3796 return 0;
3797
3800 if (total < 0 || n_div < 0)
3801 return -1;
3802 total -= n_div;
3803
3804 for (i = 0; i < bmap->n_eq; ++i)
3805 if (!isl_seq_any_non_zero(bmap->eq[i] + 1 + total, n_div))
3806 return i;
3807
3808 return bmap->n_eq;
3809}
3810
3811/* Construct a basic map in "space" defined by the equality constraints in "eq".
3812 * The constraints are assumed not to involve any local variables.
3813 */
3816{
3817 int i, k;
3819 isl_basic_map *bmap = NULL;
3820
3822 if (total < 0 || !eq)
3823 goto error;
3824
3825 if (1 + total != eq->n_col)
3827 "unexpected number of columns", goto error);
3828
3830 0, eq->n_row, 0);
3831 for (i = 0; i < eq->n_row; ++i) {
3833 if (k < 0)
3834 goto error;
3835 isl_seq_cpy(bmap->eq[k], eq->row[i], eq->n_col);
3836 }
3837
3838 isl_space_free(space);
3839 isl_mat_free(eq);
3840 return bmap;
3841error:
3842 isl_space_free(space);
3843 isl_mat_free(eq);
3845 return NULL;
3846}
3847
3848/* Construct and return a variable compression based on the equality
3849 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
3850 * "n1" is the number of (initial) equality constraints in "bmap1"
3851 * that do involve local variables.
3852 * "n2" is the number of (initial) equality constraints in "bmap2"
3853 * that do involve local variables.
3854 * "total" is the total number of other variables.
3855 * This function assumes that Gaussian elimination
3856 * has been applied to the equality constraints in both "bmap1" and "bmap2"
3857 * such that the equality constraints not involving local variables
3858 * are those that start at "n1" or "n2".
3859 *
3860 * If either of "bmap1" and "bmap2" does not have such equality constraints,
3861 * then simply compute the compression based on the equality constraints
3862 * in the other basic map.
3863 * Otherwise, combine the equality constraints from both into a new
3864 * basic map such that Gaussian elimination can be applied to this combination
3865 * and then construct a variable compression from the resulting
3866 * equality constraints.
3867 */
3869 __isl_keep isl_basic_map *bmap1, int n1,
3870 __isl_keep isl_basic_map *bmap2, int n2, int total)
3871{
3872 isl_ctx *ctx;
3873 isl_mat *E1, *E2, *V;
3875
3876 ctx = isl_basic_map_get_ctx(bmap1);
3877 if (bmap1->n_eq == n1) {
3878 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3879 n2, bmap2->n_eq - n2, 0, 1 + total);
3880 return isl_mat_variable_compression(E2, NULL);
3881 }
3882 if (bmap2->n_eq == n2) {
3883 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3884 n1, bmap1->n_eq - n1, 0, 1 + total);
3885 return isl_mat_variable_compression(E1, NULL);
3886 }
3887 E1 = isl_mat_sub_alloc6(ctx, bmap1->eq,
3888 n1, bmap1->n_eq - n1, 0, 1 + total);
3889 E2 = isl_mat_sub_alloc6(ctx, bmap2->eq,
3890 n2, bmap2->n_eq - n2, 0, 1 + total);
3891 E1 = isl_mat_concat(E1, E2);
3893 bmap = isl_basic_map_gauss(bmap, NULL);
3894 if (!bmap)
3895 return NULL;
3896 E1 = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq, 0, 1 + total);
3897 V = isl_mat_variable_compression(E1, NULL);
3899
3900 return V;
3901}
3902
3903/* Extract the stride constraints from "bmap", compressed
3904 * with respect to both the stride constraints in "context" and
3905 * the remaining equality constraints in both "bmap" and "context".
3906 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
3907 * "context_n_eq" is the number of (initial) stride constraints in "context".
3908 *
3909 * Let x be all variables in "bmap" (and "context") other than the local
3910 * variables. First compute a variable compression
3911 *
3912 * x = V x'
3913 *
3914 * based on the non-stride equality constraints in "bmap" and "context".
3915 * Consider the stride constraints of "context",
3916 *
3917 * A(x) + B(y) = 0
3918 *
3919 * with y the local variables and plug in the variable compression,
3920 * resulting in
3921 *
3922 * A(V x') + B(y) = 0
3923 *
3924 * Use these constraints to compute a parameter compression on x'
3925 *
3926 * x' = T x''
3927 *
3928 * Now consider the stride constraints of "bmap"
3929 *
3930 * C(x) + D(y) = 0
3931 *
3932 * and plug in x = V*T x''.
3933 * That is, return A = [C*V*T D].
3934 */
3936 __isl_keep isl_basic_map *bmap, int bmap_n_eq,
3937 __isl_keep isl_basic_map *context, int context_n_eq)
3938{
3940 isl_ctx *ctx;
3941 isl_mat *A, *B, *T, *V;
3942
3945 if (total < 0 || n_div < 0)
3946 return NULL;
3947 total -= n_div;
3948
3950
3951 V = combined_variable_compression(bmap, bmap_n_eq,
3952 context, context_n_eq, total);
3953
3954 A = isl_mat_sub_alloc6(ctx, context->eq, 0, context_n_eq, 0, 1 + total);
3955 B = isl_mat_sub_alloc6(ctx, context->eq,
3956 0, context_n_eq, 1 + total, n_div);
3959 T = isl_mat_product(V, T);
3960
3962 if (n_div < 0)
3963 T = isl_mat_free(T);
3964 else
3966
3967 A = isl_mat_sub_alloc6(ctx, bmap->eq,
3968 0, bmap_n_eq, 0, 1 + total + n_div);
3969 A = isl_mat_product(A, T);
3970
3971 return A;
3972}
3973
3974/* Remove the prime factors from *g that have an exponent that
3975 * is strictly smaller than the exponent in "c".
3976 * All exponents in *g are known to be smaller than or equal
3977 * to those in "c".
3978 *
3979 * That is, if *g is equal to
3980 *
3981 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3982 *
3983 * and "c" is equal to
3984 *
3985 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3986 *
3987 * then update *g to
3988 *
3989 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3990 * p_n^{e_n * (e_n = f_n)}
3991 *
3992 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3993 * neither does the gcd of *g and c / *g.
3994 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3995 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3996 * Dividing *g by this gcd therefore strictly reduces the exponent
3997 * of the prime factors that need to be removed, while leaving the
3998 * other prime factors untouched.
3999 * Repeating this process until gcd(*g, c / *g) = 1 therefore
4000 * removes all undesired factors, without removing any others.
4001 */
4003{
4004 isl_int t;
4005
4006 isl_int_init(t);
4007 for (;;) {
4008 isl_int_divexact(t, c, *g);
4009 isl_int_gcd(t, t, *g);
4010 if (isl_int_is_one(t))
4011 break;
4012 isl_int_divexact(*g, *g, t);
4013 }
4015}
4016
4017/* Reduce the "n" stride constraints in "bmap" based on a copy "A"
4018 * of the same stride constraints in a compressed space that exploits
4019 * all equalities in the context and the other equalities in "bmap".
4020 *
4021 * If the stride constraints of "bmap" are of the form
4022 *
4023 * C(x) + D(y) = 0
4024 *
4025 * then A is of the form
4026 *
4027 * B(x') + D(y) = 0
4028 *
4029 * If any of these constraints involves only a single local variable y,
4030 * then the constraint appears as
4031 *
4032 * f(x) + m y_i = 0
4033 *
4034 * in "bmap" and as
4035 *
4036 * h(x') + m y_i = 0
4037 *
4038 * in "A".
4039 *
4040 * Let g be the gcd of m and the coefficients of h.
4041 * Then, in particular, g is a divisor of the coefficients of h and
4042 *
4043 * f(x) = h(x')
4044 *
4045 * is known to be a multiple of g.
4046 * If some prime factor in m appears with the same exponent in g,
4047 * then it can be removed from m because f(x) is already known
4048 * to be a multiple of g and therefore in particular of this power
4049 * of the prime factors.
4050 * Prime factors that appear with a smaller exponent in g cannot
4051 * be removed from m.
4052 * Let g' be the divisor of g containing all prime factors that
4053 * appear with the same exponent in m and g, then
4054 *
4055 * f(x) + m y_i = 0
4056 *
4057 * can be replaced by
4058 *
4059 * f(x) + m/g' y_i' = 0
4060 *
4061 * Note that (if g' != 1) this changes the explicit representation
4062 * of y_i to that of y_i', so the integer division at position i
4063 * is marked unknown and later recomputed by a call to
4064 * isl_basic_map_gauss.
4065 */
4068{
4069 int i;
4071 int any = 0;
4072 isl_int gcd;
4073
4076 if (total < 0 || n_div < 0 || !A)
4077 return isl_basic_map_free(bmap);
4078 total -= n_div;
4079
4081 for (i = 0; i < n; ++i) {
4082 int div;
4083
4084 div = isl_seq_first_non_zero(bmap->eq[i] + 1 + total, n_div);
4085 if (div < 0)
4087 "equality constraints modified unexpectedly",
4088 goto error);
4089 if (isl_seq_any_non_zero(bmap->eq[i] + 1 + total + div + 1,
4090 n_div - div - 1))
4091 continue;
4092 if (isl_mat_row_gcd(A, i, &gcd) < 0)
4093 goto error;
4094 if (isl_int_is_one(gcd))
4095 continue;
4096 remove_incomplete_powers(&gcd, bmap->eq[i][1 + total + div]);
4097 if (isl_int_is_one(gcd))
4098 continue;
4099 isl_int_divexact(bmap->eq[i][1 + total + div],
4100 bmap->eq[i][1 + total + div], gcd);
4102 if (!bmap)
4103 goto error;
4104 any = 1;
4105 }
4107
4108 if (any)
4109 bmap = isl_basic_map_gauss(bmap, NULL);
4110
4111 return bmap;
4112error:
4115 return NULL;
4116}
4117
4118/* Simplify the stride constraints in "bmap" based on
4119 * the remaining equality constraints in "bmap" and all equality
4120 * constraints in "context".
4121 * Only do this if both "bmap" and "context" have stride constraints.
4122 *
4123 * First extract a copy of the stride constraints in "bmap" in a compressed
4124 * space exploiting all the other equality constraints and then
4125 * use this compressed copy to simplify the original stride constraints.
4126 */
4129{
4130 int bmap_n_eq, context_n_eq;
4131 isl_mat *A;
4132
4133 if (!bmap || !context)
4134 return isl_basic_map_free(bmap);
4135
4136 bmap_n_eq = n_div_eq(bmap);
4137 context_n_eq = n_div_eq(context);
4138
4139 if (bmap_n_eq < 0 || context_n_eq < 0)
4140 return isl_basic_map_free(bmap);
4141 if (bmap_n_eq == 0 || context_n_eq == 0)
4142 return bmap;
4143
4145 context, context_n_eq);
4146 bmap = reduce_stride_constraints(bmap, bmap_n_eq, A);
4147
4148 isl_mat_free(A);
4149
4150 return bmap;
4151}
4152
4153/* Return a basic map that has the same intersection with "context" as "bmap"
4154 * and that is as "simple" as possible.
4155 *
4156 * The core computation is performed on the pure constraints.
4157 * When we add back the meaning of the integer divisions, we need
4158 * to (re)introduce the div constraints. If we happen to have
4159 * discovered that some of these integer divisions are equal to
4160 * some affine combination of other variables, then these div
4161 * constraints may end up getting simplified in terms of the equalities,
4162 * resulting in extra inequalities on the other variables that
4163 * may have been removed already or that may not even have been
4164 * part of the input. We try and remove those constraints of
4165 * this form that are most obviously redundant with respect to
4166 * the context. We also remove those div constraints that are
4167 * redundant with respect to the other constraints in the result.
4168 *
4169 * The stride constraints among the equality constraints in "bmap" are
4170 * also simplified with respecting to the other equality constraints
4171 * in "bmap" and with respect to all equality constraints in "context".
4172 */
4175{
4176 isl_basic_set *bset, *eq;
4177 isl_basic_map *eq_bmap;
4178 isl_size total, n_div, n_div_bmap;
4179 unsigned extra, n_eq, n_ineq;
4180
4181 if (!bmap || !context)
4182 goto error;
4183
4186 return bmap;
4187 }
4192 return isl_basic_map_universe(space);
4193 }
4196 return bmap;
4197 }
4198
4203
4206 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
4207 if (n_div < 0 || total < 0 || n_div_bmap < 0)
4208 goto error;
4209 extra = n_div - n_div_bmap;
4210
4212 bset = isl_basic_set_add_dims(bset, isl_dim_set, extra);
4213 bset = uset_gist(bset,
4215 bset = isl_basic_set_project_out(bset, isl_dim_set, total, extra);
4216
4217 if (!bset || bset->n_eq == 0 || n_div == 0 ||
4220 return isl_basic_map_overlying_set(bset, bmap);
4221 }
4222
4223 n_eq = bset->n_eq;
4224 n_ineq = bset->n_ineq;
4225 eq = isl_basic_set_copy(bset);
4226 eq = isl_basic_set_cow(eq);
4227 eq = isl_basic_set_free_inequality(eq, n_ineq);
4228 bset = isl_basic_set_free_equality(bset, n_eq);
4229
4231 eq_bmap = gist_strides(eq_bmap, context);
4234 bmap = isl_basic_map_intersect(bmap, eq_bmap);
4236
4237 return bmap;
4238error:
4241 return NULL;
4242}
4243
4244/*
4245 * Assumes context has no implicit divs.
4246 */
4249{
4250 int i;
4251
4252 if (!map || !context)
4253 goto error;
4254
4259 return isl_map_universe(space);
4260 }
4261
4263 map = isl_map_cow(map);
4265 goto error;
4267 if (!map)
4268 goto error;
4269 for (i = map->n - 1; i >= 0; --i) {
4270 map->p[i] = isl_basic_map_gist(map->p[i],
4272 if (!map->p[i])
4273 goto error;
4274 if (isl_basic_map_plain_is_empty(map->p[i])) {
4275 isl_basic_map_free(map->p[i]);
4276 if (i != map->n - 1)
4277 map->p[i] = map->p[map->n - 1];
4278 map->n--;
4279 }
4280 }
4283 return map;
4284error:
4287 return NULL;
4288}
4289
4290/* Drop all inequalities from "bmap" that also appear in "context".
4291 * "context" is assumed to have only known local variables and
4292 * the initial local variables of "bmap" are assumed to be the same
4293 * as those of "context".
4294 * The constraints of both "bmap" and "context" are assumed
4295 * to have been sorted using isl_basic_map_sort_constraints.
4296 *
4297 * Run through the inequality constraints of "bmap" and "context"
4298 * in sorted order.
4299 * If a constraint of "bmap" involves variables not in "context",
4300 * then it cannot appear in "context".
4301 * If a matching constraint is found, it is removed from "bmap".
4302 */
4305{
4306 int i1, i2;
4307 isl_size total, bmap_total;
4308 unsigned extra;
4309
4311 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
4312 if (total < 0 || bmap_total < 0)
4313 return isl_basic_map_free(bmap);
4314
4315 extra = bmap_total - total;
4316
4317 i1 = bmap->n_ineq - 1;
4318 i2 = context->n_ineq - 1;
4319 while (bmap && i1 >= 0 && i2 >= 0) {
4320 int cmp;
4321
4322 if (isl_seq_any_non_zero(bmap->ineq[i1] + 1 + total, extra)) {
4323 --i1;
4324 continue;
4325 }
4327 context->ineq[i2]);
4328 if (cmp < 0) {
4329 --i2;
4330 continue;
4331 }
4332 if (cmp > 0) {
4333 --i1;
4334 continue;
4335 }
4336 if (isl_int_eq(bmap->ineq[i1][0], context->ineq[i2][0])) {
4340 }
4341 --i1;
4342 --i2;
4343 }
4344
4345 return bmap;
4346}
4347
4348/* Drop all equalities from "bmap" that also appear in "context".
4349 * "context" is assumed to have only known local variables and
4350 * the initial local variables of "bmap" are assumed to be the same
4351 * as those of "context".
4352 *
4353 * Run through the equality constraints of "bmap" and "context"
4354 * in sorted order.
4355 * If a constraint of "bmap" involves variables not in "context",
4356 * then it cannot appear in "context".
4357 * If a matching constraint is found, it is removed from "bmap".
4358 */
4361{
4362 int i1, i2;
4363 isl_size total, bmap_total;
4364 unsigned extra;
4365
4367 bmap_total = isl_basic_map_dim(bmap, isl_dim_all);
4368 if (total < 0 || bmap_total < 0)
4369 return isl_basic_map_free(bmap);
4370
4371 extra = bmap_total - total;
4372
4373 i1 = bmap->n_eq - 1;
4374 i2 = context->n_eq - 1;
4375
4376 while (bmap && i1 >= 0 && i2 >= 0) {
4377 int last1, last2;
4378
4379 if (isl_seq_any_non_zero(bmap->eq[i1] + 1 + total, extra))
4380 break;
4381 last1 = isl_seq_last_non_zero(bmap->eq[i1] + 1, total);
4382 last2 = isl_seq_last_non_zero(context->eq[i2] + 1, total);
4383 if (last1 > last2) {
4384 --i2;
4385 continue;
4386 }
4387 if (last1 < last2) {
4388 --i1;
4389 continue;
4390 }
4391 if (isl_seq_eq(bmap->eq[i1], context->eq[i2], 1 + total)) {
4393 if (isl_basic_map_drop_equality(bmap, i1) < 0)
4395 }
4396 --i1;
4397 --i2;
4398 }
4399
4400 return bmap;
4401}
4402
4403/* Remove the constraints in "context" from "bmap".
4404 * "context" is assumed to have explicit representations
4405 * for all local variables.
4406 *
4407 * First align the divs of "bmap" to those of "context" and
4408 * sort the constraints. Then drop all constraints from "bmap"
4409 * that appear in "context".
4410 */
4413{
4414 isl_bool done, known;
4415
4417 if (done == isl_bool_false)
4419 if (done == isl_bool_false)
4421 if (done == isl_bool_false)
4423 if (done < 0)
4424 goto error;
4425 if (done) {
4427 return bmap;
4428 }
4430 if (known < 0)
4431 goto error;
4432 if (!known)
4434 "context has unknown divs", goto error);
4435
4438 bmap = isl_basic_map_gauss(bmap, NULL);
4441
4444
4447 return bmap;
4448error:
4451 return NULL;
4452}
4453
4454/* Replace "map" by the disjunct at position "pos" and free "context".
4455 */
4466
4467/* Remove the constraints in "context" from "map".
4468 * If any of the disjuncts in the result turns out to be the universe,
4469 * then return this universe.
4470 * "context" is assumed to have explicit representations
4471 * for all local variables.
4472 */
4475{
4476 int i;
4477 isl_bool univ, known;
4478
4480 if (univ < 0)
4481 goto error;
4482 if (univ) {
4484 return map;
4485 }
4487 if (known < 0)
4488 goto error;
4489 if (!known)
4491 "context has unknown divs", goto error);
4492
4493 map = isl_map_cow(map);
4494 if (!map)
4495 goto error;
4496 for (i = 0; i < map->n; ++i) {
4497 map->p[i] = isl_basic_map_plain_gist(map->p[i],
4500 if (univ < 0)
4501 goto error;
4502 if (univ && map->n > 1)
4503 return replace_by_disjunct(map, i, context);
4504 }
4505
4508 if (map->n > 1)
4510 return map;
4511error:
4514 return NULL;
4515}
4516
4517/* Remove the constraints in "context" from "set".
4518 * If any of the disjuncts in the result turns out to be the universe,
4519 * then return this universe.
4520 * "context" is assumed to have explicit representations
4521 * for all local variables.
4522 */
4529
4530/* Remove the constraints in "context" from "map".
4531 * If any of the disjuncts in the result turns out to be the universe,
4532 * then return this universe.
4533 * "context" is assumed to consist of a single disjunct and
4534 * to have explicit representations for all local variables.
4535 */
4544
4545/* Replace "map" by a universe map in the same space and free "drop".
4546 */
4557
4558/* Return a map that has the same intersection with "context" as "map"
4559 * and that is as "simple" as possible.
4560 *
4561 * If "map" is already the universe, then we cannot make it any simpler.
4562 * Similarly, if "context" is the universe, then we cannot exploit it
4563 * to simplify "map"
4564 * If "map" and "context" are identical to each other, then we can
4565 * return the corresponding universe.
4566 *
4567 * If either "map" or "context" consists of multiple disjuncts,
4568 * then check if "context" happens to be a subset of "map",
4569 * in which case all constraints can be removed.
4570 * In case of multiple disjuncts, the standard procedure
4571 * may not be able to detect that all constraints can be removed.
4572 *
4573 * If none of these cases apply, we have to work a bit harder.
4574 * During this computation, we make use of a single disjunct context,
4575 * so if the original context consists of more than one disjunct
4576 * then we need to approximate the context by a single disjunct set.
4577 * Simply taking the simple hull may drop constraints that are
4578 * only implicitly available in each disjunct. We therefore also
4579 * look for constraints among those defining "map" that are valid
4580 * for the context. These can then be used to simplify away
4581 * the corresponding constraints in "map".
4582 */
4585{
4586 int equal;
4587 int is_universe;
4588 isl_size n_disjunct_map, n_disjunct_context;
4591
4592 is_universe = isl_map_plain_is_universe(map);
4593 if (is_universe >= 0 && !is_universe)
4594 is_universe = isl_map_plain_is_universe(context);
4595 if (is_universe < 0)
4596 goto error;
4597 if (is_universe) {
4599 return map;
4600 }
4601
4604 if (equal < 0)
4605 goto error;
4606 if (equal)
4608
4609 n_disjunct_map = isl_map_n_basic_map(map);
4610 n_disjunct_context = isl_map_n_basic_map(context);
4611 if (n_disjunct_map < 0 || n_disjunct_context < 0)
4612 goto error;
4613 if (n_disjunct_map != 1 || n_disjunct_context != 1) {
4615 if (subset < 0)
4616 goto error;
4617 if (subset)
4619 }
4620
4622 if (!context)
4623 goto error;
4624 if (n_disjunct_context == 1) {
4626 } else {
4627 isl_ctx *ctx;
4628 isl_map_list *list;
4629
4630 ctx = isl_map_get_ctx(map);
4631 list = isl_map_list_alloc(ctx, 2);
4632 list = isl_map_list_add(list, isl_map_copy(context));
4633 list = isl_map_list_add(list, isl_map_copy(map));
4635 list);
4636 }
4638error:
4641 return NULL;
4642}
4643
4650
4657
4666
4672
4673/* Compute the gist of "bmap" with respect to the constraints "context"
4674 * on the domain.
4675 */
4685
4688{
4690 map_context = isl_map_intersect_domain(map_context, context);
4691 return isl_map_gist(map, map_context);
4692}
4693
4696{
4698 map_context = isl_map_intersect_range(map_context, context);
4699 return isl_map_gist(map, map_context);
4700}
4701
4704{
4706 map_context = isl_map_intersect_params(map_context, context);
4707 return isl_map_gist(map, map_context);
4708}
4709
4715
4716/* Quick check to see if two basic maps are disjoint.
4717 * In particular, we reduce the equalities and inequalities of
4718 * one basic map in the context of the equalities of the other
4719 * basic map and check if we get a contradiction.
4720 */
4723{
4724 struct isl_vec *v = NULL;
4725 int *elim = NULL;
4727 int i;
4728
4729 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4730 return isl_bool_error;
4731 if (bmap1->n_div || bmap2->n_div)
4732 return isl_bool_false;
4733 if (!bmap1->n_eq && !bmap2->n_eq)
4734 return isl_bool_false;
4735
4736 total = isl_space_dim(bmap1->dim, isl_dim_all);
4737 if (total < 0)
4738 return isl_bool_error;
4739 if (total == 0)
4740 return isl_bool_false;
4741 v = isl_vec_alloc(bmap1->ctx, 1 + total);
4742 if (!v)
4743 goto error;
4744 elim = isl_alloc_array(bmap1->ctx, int, total);
4745 if (!elim)
4746 goto error;
4747 compute_elimination_index(bmap1, elim, total);
4748 for (i = 0; i < bmap2->n_eq; ++i) {
4749 int reduced;
4750 reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
4751 bmap1, elim, total);
4752 if (reduced && !isl_int_is_zero(v->block.data[0]) &&
4754 goto disjoint;
4755 }
4756 for (i = 0; i < bmap2->n_ineq; ++i) {
4757 int reduced;
4758 reduced = reduced_using_equalities(v->block.data,
4759 bmap2->ineq[i], bmap1, elim, total);
4760 if (reduced && isl_int_is_neg(v->block.data[0]) &&
4762 goto disjoint;
4763 }
4764 compute_elimination_index(bmap2, elim, total);
4765 for (i = 0; i < bmap1->n_ineq; ++i) {
4766 int reduced;
4767 reduced = reduced_using_equalities(v->block.data,
4768 bmap1->ineq[i], bmap2, elim, total);
4769 if (reduced && isl_int_is_neg(v->block.data[0]) &&
4771 goto disjoint;
4772 }
4773 isl_vec_free(v);
4774 free(elim);
4775 return isl_bool_false;
4776disjoint:
4777 isl_vec_free(v);
4778 free(elim);
4779 return isl_bool_true;
4780error:
4781 isl_vec_free(v);
4782 free(elim);
4783 return isl_bool_error;
4784}
4785
4792
4793/* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
4794 */
4797 __isl_keep isl_basic_map *bmap2))
4798{
4799 int i, j;
4800
4801 if (!map1 || !map2)
4802 return isl_bool_error;
4803
4804 for (i = 0; i < map1->n; ++i) {
4805 for (j = 0; j < map2->n; ++j) {
4806 isl_bool d = test(map1->p[i], map2->p[j]);
4807 if (d != isl_bool_true)
4808 return d;
4809 }
4810 }
4811
4812 return isl_bool_true;
4813}
4814
4815/* Are "map1" and "map2" obviously disjoint, based on information
4816 * that can be derived without looking at the individual basic maps?
4817 *
4818 * In particular, if one of them is empty or if they live in different spaces
4819 * (ignoring parameters), then they are clearly disjoint.
4820 */
4823{
4824 isl_bool disjoint;
4826
4827 if (!map1 || !map2)
4828 return isl_bool_error;
4829
4830 disjoint = isl_map_plain_is_empty(map1);
4831 if (disjoint < 0 || disjoint)
4832 return disjoint;
4833
4834 disjoint = isl_map_plain_is_empty(map2);
4835 if (disjoint < 0 || disjoint)
4836 return disjoint;
4837
4839 if (match < 0 || !match)
4840 return match < 0 ? isl_bool_error : isl_bool_true;
4841
4843 if (match < 0 || !match)
4844 return match < 0 ? isl_bool_error : isl_bool_true;
4845
4846 return isl_bool_false;
4847}
4848
4849/* Are "map1" and "map2" obviously disjoint?
4850 *
4851 * If one of them is empty or if they live in different spaces (ignoring
4852 * parameters), then they are clearly disjoint.
4853 * This is checked by isl_map_plain_is_disjoint_global.
4854 *
4855 * If they have different parameters, then we skip any further tests.
4856 *
4857 * If they are obviously equal, but not obviously empty, then we will
4858 * not be able to detect if they are disjoint.
4859 *
4860 * Otherwise we check if each basic map in "map1" is obviously disjoint
4861 * from each basic map in "map2".
4862 */
4865{
4866 isl_bool disjoint;
4869
4871 if (disjoint < 0 || disjoint)
4872 return disjoint;
4873
4875 if (match < 0 || !match)
4876 return match < 0 ? isl_bool_error : isl_bool_false;
4877
4879 if (intersect < 0 || intersect)
4881
4883}
4884
4885/* Are "map1" and "map2" disjoint?
4886 * The parameters are assumed to have been aligned.
4887 *
4888 * In particular, check whether all pairs of basic maps are disjoint.
4889 */
4895
4896/* Are "map1" and "map2" disjoint?
4897 *
4898 * They are disjoint if they are "obviously disjoint" or if one of them
4899 * is empty. Otherwise, they are not disjoint if one of them is universal.
4900 * If the two inputs are (obviously) equal and not empty, then they are
4901 * not disjoint.
4902 * If none of these cases apply, then check if all pairs of basic maps
4903 * are disjoint after aligning the parameters.
4904 */
4906{
4907 isl_bool disjoint;
4909
4911 if (disjoint < 0 || disjoint)
4912 return disjoint;
4913
4914 disjoint = isl_map_is_empty(map1);
4915 if (disjoint < 0 || disjoint)
4916 return disjoint;
4917
4918 disjoint = isl_map_is_empty(map2);
4919 if (disjoint < 0 || disjoint)
4920 return disjoint;
4921
4923 if (intersect < 0 || intersect)
4924 return isl_bool_not(intersect);
4925
4927 if (intersect < 0 || intersect)
4928 return isl_bool_not(intersect);
4929
4931 if (intersect < 0 || intersect)
4932 return isl_bool_not(intersect);
4933
4936}
4937
4938/* Are "bmap1" and "bmap2" disjoint?
4939 *
4940 * They are disjoint if they are "obviously disjoint" or if one of them
4941 * is empty. Otherwise, they are not disjoint if one of them is universal.
4942 * If none of these cases apply, we compute the intersection and see if
4943 * the result is empty.
4944 */
4947{
4948 isl_bool disjoint;
4951
4952 disjoint = isl_basic_map_plain_is_disjoint(bmap1, bmap2);
4953 if (disjoint < 0 || disjoint)
4954 return disjoint;
4955
4956 disjoint = isl_basic_map_is_empty(bmap1);
4957 if (disjoint < 0 || disjoint)
4958 return disjoint;
4959
4960 disjoint = isl_basic_map_is_empty(bmap2);
4961 if (disjoint < 0 || disjoint)
4962 return disjoint;
4963
4965 if (intersect < 0 || intersect)
4966 return isl_bool_not(intersect);
4967
4969 if (intersect < 0 || intersect)
4970 return isl_bool_not(intersect);
4971
4973 isl_basic_map_copy(bmap2));
4974 disjoint = isl_basic_map_is_empty(test);
4976
4977 return disjoint;
4978}
4979
4980/* Are "bset1" and "bset2" disjoint?
4981 */
4987
4993
4994/* Are "set1" and "set2" disjoint?
4995 */
5000
5001/* Is "v" equal to 0, 1 or -1?
5002 */
5004{
5005 return isl_int_is_zero(v) || isl_int_is_one(v) || isl_int_is_negone(v);
5006}
5007
5008/* Are the "n" coefficients starting at "first" of inequality constraints
5009 * "i" and "j" of "bmap" opposite to each other?
5010 */
5011static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j,
5012 int first, int n)
5013{
5014 return isl_seq_is_neg(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
5015}
5016
5017/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
5018 * apart from the constant term?
5019 */
5021{
5023
5025 if (total < 0)
5026 return isl_bool_error;
5027 return is_opposite_part(bmap, i, j, 1, total);
5028}
5029
5030/* Check if we can combine a given div with lower bound l and upper
5031 * bound u with some other div and if so return that other div.
5032 * Otherwise, return a position beyond the integer divisions.
5033 * Return isl_size_error on error.
5034 *
5035 * We first check that
5036 * - the bounds are opposites of each other (except for the constant
5037 * term)
5038 * - the bounds do not reference any other div
5039 * - no div is defined in terms of this div
5040 *
5041 * Let m be the size of the range allowed on the div by the bounds.
5042 * That is, the bounds are of the form
5043 *
5044 * e <= a <= e + m - 1
5045 *
5046 * with e some expression in the other variables.
5047 * We look for another div b such that no third div is defined in terms
5048 * of this second div b and such that in any constraint that contains
5049 * a (except for the given lower and upper bound), also contains b
5050 * with a coefficient that is m times that of b.
5051 * That is, all constraints (except for the lower and upper bound)
5052 * are of the form
5053 *
5054 * e + f (a + m b) >= 0
5055 *
5056 * Furthermore, in the constraints that only contain b, the coefficient
5057 * of b should be equal to 1 or -1.
5058 * If so, we return b so that "a + m b" can be replaced by
5059 * a single div "c = a + m b".
5060 */
5062 unsigned div, unsigned l, unsigned u)
5063{
5064 int i, j;
5065 isl_size n_div;
5066 isl_size v_div;
5068 isl_bool involves, opp;
5069
5070 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5071 if (n_div <= 1)
5072 return n_div;
5074 if (v_div < 0)
5075 return isl_size_error;
5076 if (isl_seq_any_non_zero(bmap->ineq[l] + 1 + v_div, div))
5077 return n_div;
5078 if (isl_seq_any_non_zero(bmap->ineq[l] + 1 + v_div + div + 1,
5079 n_div - div - 1))
5080 return n_div;
5081 opp = is_opposite(bmap, l, u);
5082 if (opp < 0 || !opp)
5083 return opp < 0 ? isl_size_error : n_div;
5084
5085 involves = isl_basic_map_any_div_involves_vars(bmap, v_div + div, 1);
5086 if (involves < 0 || involves)
5087 return involves < 0 ? isl_size_error : n_div;
5088
5089 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
5090 if (isl_int_is_neg(bmap->ineq[l][0])) {
5091 isl_int_sub(bmap->ineq[l][0],
5092 bmap->ineq[l][0], bmap->ineq[u][0]);
5093 bmap = isl_basic_map_copy(bmap);
5094 bmap = isl_basic_map_set_to_empty(bmap);
5095 isl_basic_map_free(bmap);
5096 return n_div;
5097 }
5098 isl_int_add_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5099 coalesce = n_div;
5100 for (i = 0; i < n_div; ++i) {
5101 if (i == div)
5102 continue;
5103 if (!pairs[i])
5104 continue;
5106 v_div + i, 1);
5107 if (involves < 0)
5108 goto error;
5109 if (involves)
5110 continue;
5111 for (j = 0; j < bmap->n_ineq; ++j) {
5112 int valid;
5113 if (j == l || j == u)
5114 continue;
5115 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + div])) {
5116 if (is_zero_or_one(bmap->ineq[j][1 + v_div + i]))
5117 continue;
5118 break;
5119 }
5120 if (isl_int_is_zero(bmap->ineq[j][1 + v_div + i]))
5121 break;
5122 isl_int_mul(bmap->ineq[j][1 + v_div + div],
5123 bmap->ineq[j][1 + v_div + div],
5124 bmap->ineq[l][0]);
5125 valid = isl_int_eq(bmap->ineq[j][1 + v_div + div],
5126 bmap->ineq[j][1 + v_div + i]);
5127 isl_int_divexact(bmap->ineq[j][1 + v_div + div],
5128 bmap->ineq[j][1 + v_div + div],
5129 bmap->ineq[l][0]);
5130 if (!valid)
5131 break;
5132 }
5133 if (j < bmap->n_ineq)
5134 continue;
5135 coalesce = i;
5136 break;
5137 }
5138 if (0)
5139error: coalesce = isl_size_error;
5140 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
5141 isl_int_sub(bmap->ineq[l][0], bmap->ineq[l][0], bmap->ineq[u][0]);
5142 return coalesce;
5143}
5144
5145/* Internal data structure used during the construction and/or evaluation of
5146 * an inequality that ensures that a pair of bounds always allows
5147 * for an integer value.
5148 *
5149 * "tab" is the tableau in which the inequality is evaluated. It may
5150 * be NULL until it is actually needed.
5151 * "v" contains the inequality coefficients.
5152 * "g", "fl" and "fu" are temporary scalars used during the construction and
5153 * evaluation.
5154 */
5162
5163/* Free all the memory allocated by the fields of "data".
5164 */
5165static void test_ineq_data_clear(struct test_ineq_data *data)
5166{
5167 isl_tab_free(data->tab);
5168 isl_vec_free(data->v);
5169 isl_int_clear(data->g);
5170 isl_int_clear(data->fl);
5171 isl_int_clear(data->fu);
5172}
5173
5174/* Is the inequality stored in data->v satisfied by "bmap"?
5175 * That is, does it only attain non-negative values?
5176 * data->tab is a tableau corresponding to "bmap".
5177 */
5179 struct test_ineq_data *data)
5180{
5181 isl_ctx *ctx;
5182 enum isl_lp_result res;
5183
5185 if (!data->tab)
5186 data->tab = isl_tab_from_basic_map(bmap, 0);
5187 res = isl_tab_min(data->tab, data->v->el, ctx->one, &data->g, NULL, 0);
5188 if (res == isl_lp_error)
5189 return isl_bool_error;
5190 return res == isl_lp_ok && isl_int_is_nonneg(data->g);
5191}
5192
5193/* Given a lower and an upper bound on div i, do they always allow
5194 * for an integer value of the given div?
5195 * Determine this property by constructing an inequality
5196 * such that the property is guaranteed when the inequality is nonnegative.
5197 * The lower bound is inequality l, while the upper bound is inequality u.
5198 * The constructed inequality is stored in data->v.
5199 *
5200 * Let the upper bound be
5201 *
5202 * -n_u a + e_u >= 0
5203 *
5204 * and the lower bound
5205 *
5206 * n_l a + e_l >= 0
5207 *
5208 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
5209 * We have
5210 *
5211 * - f_u e_l <= f_u f_l g a <= f_l e_u
5212 *
5213 * Since all variables are integer valued, this is equivalent to
5214 *
5215 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
5216 *
5217 * If this interval is at least f_u f_l g, then it contains at least
5218 * one integer value for a.
5219 * That is, the test constraint is
5220 *
5221 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
5222 *
5223 * or
5224 *
5225 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
5226 *
5227 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
5228 * then the constraint can be scaled down by a factor g',
5229 * with the constant term replaced by
5230 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
5231 * Note that the result of applying Fourier-Motzkin to this pair
5232 * of constraints is
5233 *
5234 * f_l e_u + f_u e_l >= 0
5235 *
5236 * If the constant term of the scaled down version of this constraint,
5237 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
5238 * term of the scaled down test constraint, then the test constraint
5239 * is known to hold and no explicit evaluation is required.
5240 * This is essentially the Omega test.
5241 *
5242 * If the test constraint consists of only a constant term, then
5243 * it is sufficient to look at the sign of this constant term.
5244 */
5246 int l, int u, struct test_ineq_data *data)
5247{
5248 unsigned offset;
5250
5253 if (n_div < 0)
5254 return isl_bool_error;
5255
5256 isl_int_gcd(data->g,
5257 bmap->ineq[l][offset + i], bmap->ineq[u][offset + i]);
5258 isl_int_divexact(data->fl, bmap->ineq[l][offset + i], data->g);
5259 isl_int_divexact(data->fu, bmap->ineq[u][offset + i], data->g);
5260 isl_int_neg(data->fu, data->fu);
5261 isl_seq_combine(data->v->el, data->fl, bmap->ineq[u],
5262 data->fu, bmap->ineq[l], offset + n_div);
5263 isl_int_mul(data->g, data->g, data->fl);
5264 isl_int_mul(data->g, data->g, data->fu);
5265 isl_int_sub(data->g, data->g, data->fl);
5266 isl_int_sub(data->g, data->g, data->fu);
5267 isl_int_add_ui(data->g, data->g, 1);
5268 isl_int_sub(data->fl, data->v->el[0], data->g);
5269
5270 isl_seq_gcd(data->v->el + 1, offset - 1 + n_div, &data->g);
5271 if (isl_int_is_zero(data->g))
5272 return isl_int_is_nonneg(data->fl);
5273 if (isl_int_is_one(data->g)) {
5274 isl_int_set(data->v->el[0], data->fl);
5275 return test_ineq_is_satisfied(bmap, data);
5276 }
5277 isl_int_fdiv_q(data->fl, data->fl, data->g);
5278 isl_int_fdiv_q(data->v->el[0], data->v->el[0], data->g);
5279 if (isl_int_eq(data->fl, data->v->el[0]))
5280 return isl_bool_true;
5281 isl_int_set(data->v->el[0], data->fl);
5282 isl_seq_scale_down(data->v->el + 1, data->v->el + 1, data->g,
5283 offset - 1 + n_div);
5284
5285 return test_ineq_is_satisfied(bmap, data);
5286}
5287
5288/* Remove more kinds of divs that are not strictly needed.
5289 * In particular, if all pairs of lower and upper bounds on a div
5290 * are such that they allow at least one integer value of the div,
5291 * then we can eliminate the div using Fourier-Motzkin without
5292 * introducing any spurious solutions.
5293 *
5294 * If at least one of the two constraints has a unit coefficient for the div,
5295 * then the presence of such a value is guaranteed so there is no need to check.
5296 * In particular, the value attained by the bound with unit coefficient
5297 * can serve as this intermediate value.
5298 */
5300 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
5301{
5302 isl_ctx *ctx;
5303 struct test_ineq_data data = { NULL, NULL };
5304 unsigned off;
5305 isl_size n_div;
5306 int remove = -1;
5307
5308 isl_int_init(data.g);
5309 isl_int_init(data.fl);
5310 isl_int_init(data.fu);
5311
5312 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5313 if (n_div < 0)
5314 goto error;
5315
5316 ctx = isl_basic_map_get_ctx(bmap);
5317 off = isl_basic_map_offset(bmap, isl_dim_div);
5318 data.v = isl_vec_alloc(ctx, off + n_div);
5319 if (!data.v)
5320 goto error;
5321
5322 while (n > 0) {
5323 int i, l, u;
5324 int best = -1;
5325 isl_bool has_int;
5326
5327 for (i = 0; i < n_div; ++i) {
5328 if (!pairs[i])
5329 continue;
5330 if (best >= 0 && pairs[best] <= pairs[i])
5331 continue;
5332 best = i;
5333 }
5334
5335 i = best;
5336 for (l = 0; l < bmap->n_ineq; ++l) {
5337 if (!isl_int_is_pos(bmap->ineq[l][off + i]))
5338 continue;
5339 if (isl_int_is_one(bmap->ineq[l][off + i]))
5340 continue;
5341 for (u = 0; u < bmap->n_ineq; ++u) {
5342 if (!isl_int_is_neg(bmap->ineq[u][off + i]))
5343 continue;
5344 if (isl_int_is_negone(bmap->ineq[u][off + i]))
5345 continue;
5346 has_int = int_between_bounds(bmap, i, l, u,
5347 &data);
5348 if (has_int < 0)
5349 goto error;
5350 if (data.tab && data.tab->empty)
5351 break;
5352 if (!has_int)
5353 break;
5354 }
5355 if (u < bmap->n_ineq)
5356 break;
5357 }
5358 if (data.tab && data.tab->empty) {
5359 bmap = isl_basic_map_set_to_empty(bmap);
5360 break;
5361 }
5362 if (l == bmap->n_ineq) {
5363 remove = i;
5364 break;
5365 }
5366 pairs[i] = 0;
5367 --n;
5368 }
5369
5370 test_ineq_data_clear(&data);
5371
5372 free(pairs);
5373
5374 if (remove < 0)
5375 return bmap;
5376
5377 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, remove, 1);
5379error:
5380 free(pairs);
5381 isl_basic_map_free(bmap);
5382 test_ineq_data_clear(&data);
5383 return NULL;
5384}
5385
5386/* Given a pair of divs div1 and div2 such that, except for the lower bound l
5387 * and the upper bound u, div1 always occurs together with div2 in the form
5388 * (div1 + m div2), where m is the constant range on the variable div1
5389 * allowed by l and u, replace the pair div1 and div2 by a single
5390 * div that is equal to div1 + m div2.
5391 *
5392 * The new div will appear in the location that contains div2.
5393 * We need to modify all constraints that contain
5394 * div2 = (div - div1) / m
5395 * The coefficient of div2 is known to be equal to 1 or -1.
5396 * (If a constraint does not contain div2, it will also not contain div1.)
5397 * If the constraint also contains div1, then we know they appear
5398 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
5399 * i.e., the coefficient of div is f.
5400 *
5401 * Otherwise, we first need to introduce div1 into the constraint.
5402 * Let l be
5403 *
5404 * div1 + f >=0
5405 *
5406 * and u
5407 *
5408 * -div1 + f' >= 0
5409 *
5410 * A lower bound on div2
5411 *
5412 * div2 + t >= 0
5413 *
5414 * can be replaced by
5415 *
5416 * m div2 + div1 + m t + f >= 0
5417 *
5418 * An upper bound
5419 *
5420 * -div2 + t >= 0
5421 *
5422 * can be replaced by
5423 *
5424 * -(m div2 + div1) + m t + f' >= 0
5425 *
5426 * These constraint are those that we would obtain from eliminating
5427 * div1 using Fourier-Motzkin.
5428 *
5429 * After all constraints have been modified, we drop the lower and upper
5430 * bound and then drop div1.
5431 * Since the new div is only placed in the same location that used
5432 * to store div2, but otherwise has a different meaning, any possible
5433 * explicit representation of the original div2 is removed.
5434 */
5436 unsigned div1, unsigned div2, unsigned l, unsigned u)
5437{
5438 isl_ctx *ctx;
5439 isl_int m;
5440 isl_size v_div;
5441 unsigned total;
5442 int i;
5443
5444 ctx = isl_basic_map_get_ctx(bmap);
5445
5447 if (v_div < 0)
5448 return isl_basic_map_free(bmap);
5449 total = 1 + v_div + bmap->n_div;
5450
5451 isl_int_init(m);
5452 isl_int_add(m, bmap->ineq[l][0], bmap->ineq[u][0]);
5453 isl_int_add_ui(m, m, 1);
5454
5455 for (i = 0; i < bmap->n_ineq; ++i) {
5456 if (i == l || i == u)
5457 continue;
5458 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div2]))
5459 continue;
5460 if (isl_int_is_zero(bmap->ineq[i][1 + v_div + div1])) {
5461 if (isl_int_is_pos(bmap->ineq[i][1 + v_div + div2]))
5462 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
5463 ctx->one, bmap->ineq[l], total);
5464 else
5465 isl_seq_combine(bmap->ineq[i], m, bmap->ineq[i],
5466 ctx->one, bmap->ineq[u], total);
5467 }
5468 isl_int_set(bmap->ineq[i][1 + v_div + div2],
5469 bmap->ineq[i][1 + v_div + div1]);
5470 isl_int_set_si(bmap->ineq[i][1 + v_div + div1], 0);
5471 }
5472
5474 if (l > u) {
5477 } else {
5480 }
5481 bmap = isl_basic_map_mark_div_unknown(bmap, div2);
5482 bmap = isl_basic_map_drop_div(bmap, div1);
5483 return bmap;
5484}
5485
5486/* First check if we can coalesce any pair of divs and
5487 * then continue with dropping more redundant divs.
5488 *
5489 * We loop over all pairs of lower and upper bounds on a div
5490 * with coefficient 1 and -1, respectively, check if there
5491 * is any other div "c" with which we can coalesce the div
5492 * and if so, perform the coalescing.
5493 */
5495 __isl_take isl_basic_map *bmap, int *pairs, int n)
5496{
5497 int i, l, u;
5498 isl_size v_div;
5499 isl_size n_div;
5500
5502 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5503 if (v_div < 0 || n_div < 0)
5504 return isl_basic_map_free(bmap);
5505
5506 for (i = 0; i < n_div; ++i) {
5507 if (!pairs[i])
5508 continue;
5509 for (l = 0; l < bmap->n_ineq; ++l) {
5510 if (!isl_int_is_one(bmap->ineq[l][1 + v_div + i]))
5511 continue;
5512 for (u = 0; u < bmap->n_ineq; ++u) {
5513 int c;
5514
5515 if (!isl_int_is_negone(bmap->ineq[u][1+v_div+i]))
5516 continue;
5517 c = div_find_coalesce(bmap, pairs, i, l, u);
5518 if (c < 0)
5519 goto error;
5520 if (c >= n_div)
5521 continue;
5522 free(pairs);
5523 bmap = coalesce_divs(bmap, i, c, l, u);
5525 }
5526 }
5527 }
5528
5529 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)) {
5530 free(pairs);
5531 return bmap;
5532 }
5533
5534 return drop_more_redundant_divs(bmap, pairs, n);
5535error:
5536 free(pairs);
5537 isl_basic_map_free(bmap);
5538 return NULL;
5539}
5540
5541/* Are the "n" coefficients starting at "first" of inequality constraints
5542 * "i" and "j" of "bmap" equal to each other?
5543 */
5544static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j,
5545 int first, int n)
5546{
5547 return isl_seq_eq(bmap->ineq[i] + first, bmap->ineq[j] + first, n);
5548}
5549
5550/* Are inequality constraints "i" and "j" of "bmap" equal to each other,
5551 * apart from the constant term and the coefficient at position "pos"?
5552 */
5554 int pos)
5555{
5557
5559 if (total < 0)
5560 return isl_bool_error;
5561 return is_parallel_part(bmap, i, j, 1, pos - 1) &&
5562 is_parallel_part(bmap, i, j, pos + 1, total - pos);
5563}
5564
5565/* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
5566 * apart from the constant term and the coefficient at position "pos"?
5567 */
5569 int pos)
5570{
5572
5574 if (total < 0)
5575 return isl_bool_error;
5576 return is_opposite_part(bmap, i, j, 1, pos - 1) &&
5577 is_opposite_part(bmap, i, j, pos + 1, total - pos);
5578}
5579
5580/* Restart isl_basic_map_drop_redundant_divs after "bmap" has
5581 * been modified, simplying it if "simplify" is set.
5582 * Free the temporary data structure "pairs" that was associated
5583 * to the old version of "bmap".
5584 */
5586 __isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
5587{
5588 if (simplify)
5589 bmap = isl_basic_map_simplify(bmap);
5590 free(pairs);
5592}
5593
5594/* Is "div" the single unknown existentially quantified variable
5595 * in inequality constraint "ineq" of "bmap"?
5596 * "div" is known to have a non-zero coefficient in "ineq".
5597 */
5599 int div)
5600{
5601 int i;
5602 isl_size n_div;
5603 unsigned o_div;
5604 isl_bool known;
5605
5606 known = isl_basic_map_div_is_known(bmap, div);
5607 if (known < 0 || known)
5608 return isl_bool_not(known);
5609 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5610 if (n_div < 0)
5611 return isl_bool_error;
5612 if (n_div == 1)
5613 return isl_bool_true;
5614 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5615 for (i = 0; i < n_div; ++i) {
5616 isl_bool known;
5617
5618 if (i == div)
5619 continue;
5620 if (isl_int_is_zero(bmap->ineq[ineq][o_div + i]))
5621 continue;
5622 known = isl_basic_map_div_is_known(bmap, i);
5623 if (known < 0 || !known)
5624 return known;
5625 }
5626
5627 return isl_bool_true;
5628}
5629
5630/* Does integer division "div" have coefficient 1 in inequality constraint
5631 * "ineq" of "map"?
5632 */
5633static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
5634{
5635 unsigned o_div;
5636
5637 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5638 if (isl_int_is_one(bmap->ineq[ineq][o_div + div]))
5639 return isl_bool_true;
5640
5641 return isl_bool_false;
5642}
5643
5644/* Turn inequality constraint "ineq" of "bmap" into an equality and
5645 * then try and drop redundant divs again,
5646 * freeing the temporary data structure "pairs" that was associated
5647 * to the old version of "bmap".
5648 */
5650 __isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
5651{
5652 bmap = isl_basic_map_cow(bmap);
5654 return drop_redundant_divs_again(bmap, pairs, 1);
5655}
5656
5657/* Drop the integer division at position "div", along with the two
5658 * inequality constraints "ineq1" and "ineq2" in which it appears
5659 * from "bmap" and then try and drop redundant divs again,
5660 * freeing the temporary data structure "pairs" that was associated
5661 * to the old version of "bmap".
5662 */
5664 __isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2,
5665 __isl_take int *pairs)
5666{
5667 if (ineq1 > ineq2) {
5668 isl_basic_map_drop_inequality(bmap, ineq1);
5669 isl_basic_map_drop_inequality(bmap, ineq2);
5670 } else {
5671 isl_basic_map_drop_inequality(bmap, ineq2);
5672 isl_basic_map_drop_inequality(bmap, ineq1);
5673 }
5674 bmap = isl_basic_map_drop_div(bmap, div);
5675 return drop_redundant_divs_again(bmap, pairs, 0);
5676}
5677
5678/* Given two inequality constraints
5679 *
5680 * f(x) + n d + c >= 0, (ineq)
5681 *
5682 * with d the variable at position "pos", and
5683 *
5684 * f(x) + c0 >= 0, (lower)
5685 *
5686 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
5687 * determined by the first constraint.
5688 * That is, store
5689 *
5690 * ceil((c0 - c)/n)
5691 *
5692 * in *l.
5693 */
5695 int ineq, int lower, int pos, isl_int *l)
5696{
5697 isl_int_neg(*l, bmap->ineq[ineq][0]);
5698 isl_int_add(*l, *l, bmap->ineq[lower][0]);
5699 isl_int_cdiv_q(*l, *l, bmap->ineq[ineq][pos]);
5700}
5701
5702/* Given two inequality constraints
5703 *
5704 * f(x) + n d + c >= 0, (ineq)
5705 *
5706 * with d the variable at position "pos", and
5707 *
5708 * -f(x) - c0 >= 0, (upper)
5709 *
5710 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
5711 * determined by the first constraint.
5712 * That is, store
5713 *
5714 * ceil((-c1 - c)/n)
5715 *
5716 * in *u.
5717 */
5719 int ineq, int upper, int pos, isl_int *u)
5720{
5721 isl_int_neg(*u, bmap->ineq[ineq][0]);
5722 isl_int_sub(*u, *u, bmap->ineq[upper][0]);
5723 isl_int_cdiv_q(*u, *u, bmap->ineq[ineq][pos]);
5724}
5725
5726/* Given a lower bound constraint "ineq" on "div" in "bmap",
5727 * does the corresponding lower bound have a fixed value in "bmap"?
5728 *
5729 * In particular, "ineq" is of the form
5730 *
5731 * f(x) + n d + c >= 0
5732 *
5733 * with n > 0, c the constant term and
5734 * d the existentially quantified variable "div".
5735 * That is, the lower bound is
5736 *
5737 * ceil((-f(x) - c)/n)
5738 *
5739 * Look for a pair of constraints
5740 *
5741 * f(x) + c0 >= 0
5742 * -f(x) + c1 >= 0
5743 *
5744 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
5745 * That is, check that
5746 *
5747 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
5748 *
5749 * If so, return the index of inequality f(x) + c0 >= 0.
5750 * Otherwise, return bmap->n_ineq.
5751 * Return -1 on error.
5752 */
5753static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
5754{
5755 int i;
5756 int lower = -1, upper = -1;
5757 unsigned o_div;
5758 isl_int l, u;
5759 int equal;
5760
5761 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5762 for (i = 0; i < bmap->n_ineq && (lower < 0 || upper < 0); ++i) {
5763 isl_bool par, opp;
5764
5765 if (i == ineq)
5766 continue;
5767 if (!isl_int_is_zero(bmap->ineq[i][o_div + div]))
5768 continue;
5769 par = isl_bool_false;
5770 if (lower < 0)
5771 par = is_parallel_except(bmap, ineq, i, o_div + div);
5772 if (par < 0)
5773 return -1;
5774 if (par) {
5775 lower = i;
5776 continue;
5777 }
5778 opp = isl_bool_false;
5779 if (upper < 0)
5780 opp = is_opposite_except(bmap, ineq, i, o_div + div);
5781 if (opp < 0)
5782 return -1;
5783 if (opp)
5784 upper = i;
5785 }
5786
5787 if (lower < 0 || upper < 0)
5788 return bmap->n_ineq;
5789
5790 isl_int_init(l);
5791 isl_int_init(u);
5792
5793 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &l);
5794 lower_bound_from_opposite(bmap, ineq, upper, o_div + div, &u);
5795
5796 equal = isl_int_eq(l, u);
5797
5798 isl_int_clear(l);
5799 isl_int_clear(u);
5800
5801 return equal ? lower : bmap->n_ineq;
5802}
5803
5804/* Given a lower bound constraint "ineq" on the existentially quantified
5805 * variable "div", such that the corresponding lower bound has
5806 * a fixed value in "bmap", assign this fixed value to the variable and
5807 * then try and drop redundant divs again,
5808 * freeing the temporary data structure "pairs" that was associated
5809 * to the old version of "bmap".
5810 * "lower" determines the constant value for the lower bound.
5811 *
5812 * In particular, "ineq" is of the form
5813 *
5814 * f(x) + n d + c >= 0,
5815 *
5816 * while "lower" is of the form
5817 *
5818 * f(x) + c0 >= 0
5819 *
5820 * The lower bound is ceil((-f(x) - c)/n) and its constant value
5821 * is ceil((c0 - c)/n).
5822 */
5824 int div, int ineq, int lower, int *pairs)
5825{
5826 isl_int c;
5827 unsigned o_div;
5828
5829 isl_int_init(c);
5830
5831 o_div = isl_basic_map_offset(bmap, isl_dim_div);
5832 lower_bound_from_parallel(bmap, ineq, lower, o_div + div, &c);
5833 bmap = isl_basic_map_fix(bmap, isl_dim_div, div, c);
5834 free(pairs);
5835
5836 isl_int_clear(c);
5837
5839}
5840
5841/* Do any of the integer divisions of "bmap" involve integer division "div"?
5842 *
5843 * The integer division "div" could only ever appear in any later
5844 * integer division (with an explicit representation).
5845 */
5847{
5848 int i;
5849 isl_size v_div, n_div;
5850
5852 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5853 if (v_div < 0 || n_div < 0)
5854 return isl_bool_error;
5855
5856 for (i = div + 1; i < n_div; ++i) {
5857 isl_bool involves;
5858
5859 involves = isl_basic_map_div_expr_involves_vars(bmap, i,
5860 v_div + div, 1);
5861 if (involves < 0 || involves)
5862 return involves;
5863 }
5864
5865 return isl_bool_false;
5866}
5867
5868/* Remove divs that are not strictly needed based on the inequality
5869 * constraints.
5870 * In particular, if a div only occurs positively (or negatively)
5871 * in constraints, then it can simply be dropped.
5872 * Also, if a div occurs in only two constraints and if moreover
5873 * those two constraints are opposite to each other, except for the constant
5874 * term and if the sum of the constant terms is such that for any value
5875 * of the other values, there is always at least one integer value of the
5876 * div, i.e., if one plus this sum is greater than or equal to
5877 * the (absolute value) of the coefficient of the div in the constraints,
5878 * then we can also simply drop the div.
5879 *
5880 * If an existentially quantified variable does not have an explicit
5881 * representation, appears in only a single lower bound that does not
5882 * involve any other such existentially quantified variables and appears
5883 * in this lower bound with coefficient 1,
5884 * then fix the variable to the value of the lower bound. That is,
5885 * turn the inequality into an equality.
5886 * If for any value of the other variables, there is any value
5887 * for the existentially quantified variable satisfying the constraints,
5888 * then this lower bound also satisfies the constraints.
5889 * It is therefore safe to pick this lower bound.
5890 *
5891 * The same reasoning holds even if the coefficient is not one.
5892 * However, fixing the variable to the value of the lower bound may
5893 * in general introduce an extra integer division, in which case
5894 * it may be better to pick another value.
5895 * If this integer division has a known constant value, then plugging
5896 * in this constant value removes the existentially quantified variable
5897 * completely. In particular, if the lower bound is of the form
5898 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
5899 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
5900 * then the existentially quantified variable can be assigned this
5901 * shared value.
5902 *
5903 * We skip divs that appear in equalities or in the definition of other divs.
5904 * Divs that appear in the definition of other divs usually occur in at least
5905 * 4 constraints, but the constraints may have been simplified.
5906 *
5907 * If any divs are left after these simple checks then we move on
5908 * to more complicated cases in drop_more_redundant_divs.
5909 */
5912{
5913 int i, j;
5914 isl_size off;
5915 int *pairs = NULL;
5916 int n = 0;
5917 isl_size n_ineq;
5918
5919 if (!bmap)
5920 goto error;
5921 if (bmap->n_div == 0)
5922 return bmap;
5923
5925 if (off < 0)
5926 return isl_basic_map_free(bmap);
5927 pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
5928 if (!pairs)
5929 goto error;
5930
5931 n_ineq = isl_basic_map_n_inequality(bmap);
5932 if (n_ineq < 0)
5933 goto error;
5934 for (i = 0; i < bmap->n_div; ++i) {
5935 int pos, neg;
5936 int last_pos, last_neg;
5937 int redundant;
5938 int defined;
5939 isl_bool involves, opp, set_div;
5940
5941 defined = !isl_int_is_zero(bmap->div[i][0]);
5942 involves = any_div_involves_div(bmap, i);
5943 if (involves < 0)
5944 goto error;
5945 if (involves)
5946 continue;
5947 for (j = 0; j < bmap->n_eq; ++j)
5948 if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
5949 break;
5950 if (j < bmap->n_eq)
5951 continue;
5952 ++n;
5953 pos = neg = 0;
5954 for (j = 0; j < bmap->n_ineq; ++j) {
5955 if (isl_int_is_pos(bmap->ineq[j][1 + off + i])) {
5956 last_pos = j;
5957 ++pos;
5958 }
5959 if (isl_int_is_neg(bmap->ineq[j][1 + off + i])) {
5960 last_neg = j;
5961 ++neg;
5962 }
5963 }
5964 pairs[i] = pos * neg;
5965 if (pairs[i] == 0) {
5966 for (j = bmap->n_ineq - 1; j >= 0; --j)
5967 if (!isl_int_is_zero(bmap->ineq[j][1+off+i]))
5969 bmap = isl_basic_map_drop_div(bmap, i);
5970 return drop_redundant_divs_again(bmap, pairs, 0);
5971 }
5972 if (pairs[i] != 1)
5973 opp = isl_bool_false;
5974 else
5975 opp = is_opposite(bmap, last_pos, last_neg);
5976 if (opp < 0)
5977 goto error;
5978 if (!opp) {
5979 int lower;
5980 isl_bool single, one;
5981
5982 if (pos != 1)
5983 continue;
5984 single = single_unknown(bmap, last_pos, i);
5985 if (single < 0)
5986 goto error;
5987 if (!single)
5988 continue;
5989 one = has_coef_one(bmap, i, last_pos);
5990 if (one < 0)
5991 goto error;
5992 if (one)
5993 return set_eq_and_try_again(bmap, last_pos,
5994 pairs);
5995 lower = lower_bound_is_cst(bmap, i, last_pos);
5996 if (lower < 0)
5997 goto error;
5998 if (lower < n_ineq)
5999 return fix_cst_lower(bmap, i, last_pos, lower,
6000 pairs);
6001 continue;
6002 }
6003
6004 isl_int_add(bmap->ineq[last_pos][0],
6005 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
6006 isl_int_add_ui(bmap->ineq[last_pos][0],
6007 bmap->ineq[last_pos][0], 1);
6008 redundant = isl_int_ge(bmap->ineq[last_pos][0],
6009 bmap->ineq[last_pos][1+off+i]);
6010 isl_int_sub_ui(bmap->ineq[last_pos][0],
6011 bmap->ineq[last_pos][0], 1);
6012 isl_int_sub(bmap->ineq[last_pos][0],
6013 bmap->ineq[last_pos][0], bmap->ineq[last_neg][0]);
6014 if (redundant)
6015 return drop_div_and_try_again(bmap, i,
6016 last_pos, last_neg, pairs);
6017 if (defined)
6019 else
6021 bmap->ineq[last_pos]);
6022 if (set_div < 0)
6023 return isl_basic_map_free(bmap);
6024 if (set_div) {
6025 bmap = set_div_from_lower_bound(bmap, i,
6026 bmap->ineq[last_pos]);
6027 return drop_redundant_divs_again(bmap, pairs, 1);
6028 }
6029 pairs[i] = 0;
6030 --n;
6031 }
6032
6033 if (n > 0)
6034 return coalesce_or_drop_more_redundant_divs(bmap, pairs, n);
6035
6036 free(pairs);
6037 return bmap;
6038error:
6039 free(pairs);
6040 isl_basic_map_free(bmap);
6041 return NULL;
6042}
6043
6044/* Consider the coefficients at "c" as a row vector and replace
6045 * them with their product with "T". "T" is assumed to be a square matrix.
6046 */
6048{
6049 isl_size n;
6050 isl_ctx *ctx;
6051 isl_vec *v;
6052
6053 n = isl_mat_rows(T);
6054 if (n < 0)
6055 return isl_stat_error;
6056 if (!isl_seq_any_non_zero(c, n))
6057 return isl_stat_ok;
6058 ctx = isl_mat_get_ctx(T);
6059 v = isl_vec_alloc(ctx, n);
6060 if (!v)
6061 return isl_stat_error;
6062 isl_seq_swp_or_cpy(v->el, c, n);
6064 if (!v)
6065 return isl_stat_error;
6066 isl_seq_swp_or_cpy(c, v->el, n);
6067 isl_vec_free(v);
6068
6069 return isl_stat_ok;
6070}
6071
6072/* Plug in T for the variables in "bmap" starting at "pos".
6073 * T is a linear unimodular matrix, i.e., without constant term.
6074 */
6076 __isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
6077{
6078 int i;
6079 isl_size n_row, n_col;
6080
6081 bmap = isl_basic_map_cow(bmap);
6082 n_row = isl_mat_rows(T);
6083 n_col = isl_mat_cols(T);
6084 if (!bmap || n_row < 0 || n_col < 0)
6085 goto error;
6086
6087 if (n_col != n_row)
6089 "expecting square matrix", goto error);
6090
6091 if (isl_basic_map_check_range(bmap, isl_dim_all, pos, n_col) < 0)
6092 goto error;
6093
6094 for (i = 0; i < bmap->n_eq; ++i)
6095 if (preimage(bmap->eq[i] + 1 + pos, T) < 0)
6096 goto error;
6097 for (i = 0; i < bmap->n_ineq; ++i)
6098 if (preimage(bmap->ineq[i] + 1 + pos, T) < 0)
6099 goto error;
6100 for (i = 0; i < bmap->n_div; ++i) {
6102 continue;
6103 if (preimage(bmap->div[i] + 1 + 1 + pos, T) < 0)
6104 goto error;
6105 }
6106
6107 isl_mat_free(T);
6108 return bmap;
6109error:
6110 isl_basic_map_free(bmap);
6111 isl_mat_free(T);
6112 return NULL;
6113}
6114
6115/* Remove divs that are not strictly needed.
6116 *
6117 * First look for an equality constraint involving two or more
6118 * existentially quantified variables without an explicit
6119 * representation. Replace the combination that appears
6120 * in the equality constraint by a single existentially quantified
6121 * variable such that the equality can be used to derive
6122 * an explicit representation for the variable.
6123 * If there are no more such equality constraints, then continue
6124 * with isl_basic_map_drop_redundant_divs_ineq.
6125 *
6126 * In particular, if the equality constraint is of the form
6127 *
6128 * f(x) + \sum_i c_i a_i = 0
6129 *
6130 * with a_i existentially quantified variable without explicit
6131 * representation, then apply a transformation on the existentially
6132 * quantified variables to turn the constraint into
6133 *
6134 * f(x) + g a_1' = 0
6135 *
6136 * with g the gcd of the c_i.
6137 * In order to easily identify which existentially quantified variables
6138 * have a complete explicit representation, i.e., without being defined
6139 * in terms of other existentially quantified variables without
6140 * an explicit representation, the existentially quantified variables
6141 * are first sorted.
6142 *
6143 * The variable transformation is computed by extending the row
6144 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
6145 *
6146 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
6147 * [a_2'] [ a_2 ]
6148 * ... = U ....
6149 * [a_n'] [ a_n ]
6150 *
6151 * with [c_1/g ... c_n/g] representing the first row of U.
6152 * The inverse of U is then plugged into the original constraints.
6153 * The call to isl_basic_map_simplify makes sure the explicit
6154 * representation for a_1' is extracted from the equality constraint.
6155 */
6158{
6159 int first;
6160 int i;
6161 unsigned o_div;
6162 isl_size n_div;
6163 int l;
6164 isl_ctx *ctx;
6165 isl_mat *T;
6166
6167 if (!bmap)
6168 return NULL;
6169 if (isl_basic_map_divs_known(bmap))
6171 if (bmap->n_eq == 0)
6173 bmap = isl_basic_map_sort_divs(bmap);
6174 if (!bmap)
6175 return NULL;
6176
6177 first = isl_basic_map_first_unknown_div(bmap);
6178 if (first < 0)
6179 return isl_basic_map_free(bmap);
6180
6181 o_div = isl_basic_map_offset(bmap, isl_dim_div);
6182 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6183 if (n_div < 0)
6184 return isl_basic_map_free(bmap);
6185
6186 for (i = 0; i < bmap->n_eq; ++i) {
6187 l = isl_seq_first_non_zero(bmap->eq[i] + o_div + first,
6188 n_div - (first));
6189 if (l < 0)
6190 continue;
6191 l += first;
6192 if (!isl_seq_any_non_zero(bmap->eq[i] + o_div + l + 1,
6193 n_div - (l + 1)))
6194 continue;
6195 break;
6196 }
6197 if (i >= bmap->n_eq)
6199
6200 ctx = isl_basic_map_get_ctx(bmap);
6201 T = isl_mat_alloc(ctx, n_div - l, n_div - l);
6202 if (!T)
6203 return isl_basic_map_free(bmap);
6204 isl_seq_cpy(T->row[0], bmap->eq[i] + o_div + l, n_div - l);
6205 T = isl_mat_normalize_row(T, 0);
6207 T = isl_mat_right_inverse(T);
6208
6209 for (i = l; i < n_div; ++i)
6210 bmap = isl_basic_map_mark_div_unknown(bmap, i);
6211 bmap = isl_basic_map_preimage_vars(bmap, o_div - 1 + l, T);
6212 bmap = isl_basic_map_simplify(bmap);
6213
6215}
6216
6217/* Does "bmap" satisfy any equality that involves more than 2 variables
6218 * and/or has coefficients different from -1 and 1?
6219 */
6221{
6222 int i;
6224
6226 if (total < 0)
6227 return isl_bool_error;
6228
6229 for (i = 0; i < bmap->n_eq; ++i) {
6230 int j, k;
6231
6232 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
6233 if (j < 0)
6234 continue;
6235 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
6236 !isl_int_is_negone(bmap->eq[i][1 + j]))
6237 return isl_bool_true;
6238
6239 j += 1;
6240 k = isl_seq_first_non_zero(bmap->eq[i] + 1 + j, total - j);
6241 if (k < 0)
6242 continue;
6243 j += k;
6244 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
6245 !isl_int_is_negone(bmap->eq[i][1 + j]))
6246 return isl_bool_true;
6247
6248 j += 1;
6249 if (isl_seq_any_non_zero(bmap->eq[i] + 1 + j, total - j))
6250 return isl_bool_true;
6251 }
6252
6253 return isl_bool_false;
6254}
6255
6256/* Remove any common factor g from the constraint coefficients in "v".
6257 * The constant term is stored in the first position and is replaced
6258 * by floor(c/g). If any common factor is removed and if this results
6259 * in a tightening of the constraint, then set *tightened.
6260 */
6262 int *tightened)
6263{
6264 isl_ctx *ctx;
6265
6266 if (!v)
6267 return NULL;
6268 ctx = isl_vec_get_ctx(v);
6269 isl_seq_gcd(v->el + 1, v->size - 1, &ctx->normalize_gcd);
6271 return v;
6272 if (isl_int_is_one(ctx->normalize_gcd))
6273 return v;
6274 v = isl_vec_cow(v);
6275 if (!v)
6276 return NULL;
6277 if (tightened && !isl_int_is_divisible_by(v->el[0], ctx->normalize_gcd))
6278 *tightened = 1;
6279 isl_int_fdiv_q(v->el[0], v->el[0], ctx->normalize_gcd);
6280 isl_seq_scale_down(v->el + 1, v->el + 1, ctx->normalize_gcd,
6281 v->size - 1);
6282 return v;
6283}
6284
6285/* Internal representation used by isl_basic_map_reduce_coefficients.
6286 *
6287 * "total" is the total dimensionality of the original basic map.
6288 * "v" is a temporary vector of size 1 + total that can be used
6289 * to store constraint coefficients.
6290 * "T" is the variable compression.
6291 * "T2" is the inverse transformation.
6292 * "tightened" is set if any constant term got tightened
6293 * while reducing the coefficients.
6294 */
6302
6303/* Free all memory allocated in "data".
6304 */
6306 struct isl_reduce_coefficients_data *data)
6307{
6308 data->T = isl_mat_free(data->T);
6309 data->T2 = isl_mat_free(data->T2);
6310 data->v = isl_vec_free(data->v);
6311}
6312
6313/* Initialize "data" for "bmap", freeing all allocated memory
6314 * if anything goes wrong.
6315 *
6316 * In particular, construct a variable compression
6317 * from the equality constraints of "bmap" and
6318 * allocate a temporary vector.
6319 */
6322 struct isl_reduce_coefficients_data *data)
6323{
6324 isl_ctx *ctx;
6325 isl_mat *eq;
6326
6327 data->v = NULL;
6328 data->T = NULL;
6329 data->T2 = NULL;
6330 data->tightened = 0;
6331
6332 data->total = isl_basic_map_dim(bmap, isl_dim_all);
6333 if (data->total < 0)
6334 return isl_stat_error;
6335 ctx = isl_basic_map_get_ctx(bmap);
6336 data->v = isl_vec_alloc(ctx, 1 + data->total);
6337 if (!data->v)
6338 return isl_stat_error;
6339
6340 eq = isl_mat_sub_alloc6(ctx, bmap->eq, 0, bmap->n_eq,
6341 0, 1 + data->total);
6342 data->T = isl_mat_variable_compression(eq, &data->T2);
6343 if (!data->T || !data->T2)
6344 goto error;
6345
6346 return isl_stat_ok;
6347error:
6349 return isl_stat_error;
6350}
6351
6352/* Reduce the coefficients of "bmap" by applying the variable compression
6353 * in "data".
6354 * In particular, apply the variable compression to each constraint,
6355 * factor out any common factor in the non-constant coefficients and
6356 * then apply the inverse of the compression.
6357 *
6358 * Only apply the reduction on a single copy of the basic map
6359 * since the reduction may leave the result in an inconsistent state.
6360 * In particular, the constraints may not be gaussed.
6361 */
6364 struct isl_reduce_coefficients_data *data)
6365{
6366 int i;
6368
6370 if (total < 0)
6371 return isl_basic_map_free(bmap);
6372 if (total != data->total)
6374 "total dimensionality changed unexpectedly",
6375 return isl_basic_map_free(bmap));
6376
6377 bmap = isl_basic_map_cow(bmap);
6378 if (!bmap)
6379 return NULL;
6380
6381 for (i = 0; i < bmap->n_ineq; ++i) {
6382 isl_seq_cpy(data->v->el, bmap->ineq[i], 1 + data->total);
6383 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T));
6384 data->v = normalize_constraint(data->v, &data->tightened);
6385 data->v = isl_vec_mat_product(data->v, isl_mat_copy(data->T2));
6386 if (!data->v)
6387 return isl_basic_map_free(bmap);
6388 isl_seq_cpy(bmap->ineq[i], data->v->el, 1 + data->total);
6389 }
6390
6392
6393 return bmap;
6394}
6395
6396/* If "bmap" is an integer set that satisfies any equality involving
6397 * more than 2 variables and/or has coefficients different from -1 and 1,
6398 * then use variable compression to reduce the coefficients by removing
6399 * any (hidden) common factor.
6400 * In particular, apply the variable compression to each constraint,
6401 * factor out any common factor in the non-constant coefficients and
6402 * then apply the inverse of the compression.
6403 * At the end, we mark the basic map as having reduced constants.
6404 * If this flag is still set on the next invocation of this function,
6405 * then we skip the computation.
6406 *
6407 * Removing a common factor may result in a tightening of some of
6408 * the constraints. If this happens, then we may end up with two
6409 * opposite inequalities that can be replaced by an equality.
6410 * We therefore call isl_basic_map_detect_inequality_pairs,
6411 * which checks for such pairs of inequalities as well as eliminate_divs_eq
6412 * and isl_basic_map_gauss if such a pair was found.
6413 * This call to isl_basic_map_gauss may undo much of the effect
6414 * of the reduction on which isl_map_coalesce depends.
6415 * In particular, constraints in terms of (compressed) local variables
6416 * get reformulated in terms of the set variables again.
6417 * The reduction is therefore applied again afterwards.
6418 * This has to be done before the call to eliminate_divs_eq, however,
6419 * since that may remove some local variables, while
6420 * the data used during the reduction is formulated in terms
6421 * of the original variables.
6422 *
6423 * Tightening may also result in some other constraints becoming
6424 * (rationally) redundant with respect to the tightened constraint
6425 * (in combination with other constraints). The basic map may
6426 * therefore no longer be assumed to have no redundant constraints.
6427 *
6428 * Note that this function may leave the result in an inconsistent state.
6429 * In particular, the constraints may not be gaussed.
6430 * Unfortunately, isl_map_coalesce actually depends on this inconsistent state
6431 * for some of the test cases to pass successfully.
6432 * Any potential modification of the representation is therefore only
6433 * performed on a single copy of the basic map.
6434 */
6437{
6438 struct isl_reduce_coefficients_data data;
6439 isl_bool multi;
6440
6441 if (!bmap)
6442 return NULL;
6444 return bmap;
6445 if (isl_basic_map_is_rational(bmap))
6446 return bmap;
6447 if (bmap->n_eq == 0)
6448 return bmap;
6449 multi = has_multiple_var_equality(bmap);
6450 if (multi < 0)
6451 return isl_basic_map_free(bmap);
6452 if (!multi)
6453 return bmap;
6454
6455 if (isl_reduce_coefficients_data_init(bmap, &data) < 0)
6456 return isl_basic_map_free(bmap);
6457
6458 if (data.T->n_col == 0) {
6460 return isl_basic_map_set_to_empty(bmap);
6461 }
6462
6463 bmap = reduce_coefficients(bmap, &data);
6464 if (!bmap)
6465 goto error;
6466
6467 if (data.tightened) {
6468 int progress = 0;
6469
6471 bmap = isl_basic_map_detect_inequality_pairs(bmap, &progress);
6472 if (progress) {
6473 isl_bool empty;
6474
6475 bmap = isl_basic_map_gauss(bmap, NULL);
6476 empty = isl_basic_map_plain_is_empty(bmap);
6477 if (empty < 0)
6478 goto error;
6479 if (!empty)
6480 bmap = reduce_coefficients(bmap, &data);
6481 bmap = eliminate_divs_eq(bmap, &progress);
6482 }
6483 }
6484
6486
6487 return bmap;
6488error:
6490 return isl_basic_map_free(bmap);
6491}
6492
6493/* Shift the integer division at position "div" of "bmap"
6494 * by "shift" times the variable at position "pos".
6495 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
6496 * corresponds to the constant term.
6497 *
6498 * That is, if the integer division has the form
6499 *
6500 * floor(f(x)/d)
6501 *
6502 * then replace it by
6503 *
6504 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
6505 */
6507 __isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
6508{
6509 int i;
6510 isl_size total, n_div;
6511
6512 if (isl_int_is_zero(shift))
6513 return bmap;
6515 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6516 total -= n_div;
6517 if (total < 0 || n_div < 0)
6518 return isl_basic_map_free(bmap);
6519
6520 isl_int_addmul(bmap->div[div][1 + pos], shift, bmap->div[div][0]);
6521
6522 for (i = 0; i < bmap->n_eq; ++i) {
6523 if (isl_int_is_zero(bmap->eq[i][1 + total + div]))
6524 continue;
6525 isl_int_submul(bmap->eq[i][pos],
6526 shift, bmap->eq[i][1 + total + div]);
6527 }
6528 for (i = 0; i < bmap->n_ineq; ++i) {
6529 if (isl_int_is_zero(bmap->ineq[i][1 + total + div]))
6530 continue;
6531 isl_int_submul(bmap->ineq[i][pos],
6532 shift, bmap->ineq[i][1 + total + div]);
6533 }
6534 for (i = 0; i < bmap->n_div; ++i) {
6535 if (isl_int_is_zero(bmap->div[i][0]))
6536 continue;
6537 if (isl_int_is_zero(bmap->div[i][1 + 1 + total + div]))
6538 continue;
6539 isl_int_submul(bmap->div[i][1 + pos],
6540 shift, bmap->div[i][1 + 1 + total + div]);
6541 }
6542
6543 return bmap;
6544}
static __isl_give isl_basic_set * bset_from_bmap(__isl_take isl_basic_map *bmap)
static __isl_give isl_basic_map * bset_to_bmap(__isl_take isl_basic_set *bset)
Definition bset_to_bmap.c:7
A()
#define __isl_take
Definition ctx.h:23
isl_stat
Definition ctx.h:85
@ isl_stat_error
Definition ctx.h:86
@ isl_stat_ok
Definition ctx.h:87
#define __isl_give
Definition ctx.h:20
#define isl_size_error
Definition ctx.h:99
#define isl_die(ctx, errno, msg, code)
Definition ctx.h:139
@ isl_error_invalid
Definition ctx.h:81
@ isl_error_internal
Definition ctx.h:80
#define isl_alloc_array(ctx, type, n)
Definition ctx.h:133
#define isl_calloc_array(ctx, type, n)
Definition ctx.h:134
#define ISL_F_ISSET(p, f)
Definition ctx.h:119
#define __isl_keep
Definition ctx.h:26
int isl_size
Definition ctx.h:98
isl_bool isl_bool_not(isl_bool b)
Definition isl_ctx.c:44
isl_bool
Definition ctx.h:90
@ isl_bool_false
Definition ctx.h:92
@ isl_bool_true
Definition ctx.h:93
@ isl_bool_error
Definition ctx.h:91
#define ISL_F_SET(p, f)
Definition ctx.h:117
#define ISL_F_CLR(p, f)
Definition ctx.h:118
B()
m
Definition guard1-0.c:2
isl_stat isl_stat void * user
Definition hmap.h:39
isl_bool isl_bool(* test)(__isl_keep ISL_KEY *key, __isl_keep ISL_VAL *val, void *user)
Definition hmap.h:41
int GMPQAPI cmp(mp_rat op1, mp_rat op2)
void GMPZAPI neg(mp_int rop, mp_int op)
void GMPZAPI gcd(mp_int rop, mp_int op1, mp_int op2)
void GMPZAPI swap(mp_int rop1, mp_int rop2)
__isl_give isl_basic_set * isl_basic_set_plain_affine_hull(__isl_take isl_basic_set *bset)
int isl_blk_is_error(struct isl_blk block)
Definition isl_blk.c:37
void isl_blk_free(struct isl_ctx *ctx, struct isl_blk block)
Definition isl_blk.c:116
struct isl_blk isl_blk_alloc(struct isl_ctx *ctx, size_t n)
Definition isl_blk.c:74
static int coalesce(isl_ctx *ctx, int n, struct isl_coalesce_info *info)
static void drop(struct isl_coalesce_info *info)
static unsigned offset(__isl_keep isl_constraint *c, enum isl_dim_type type)
__isl_give isl_mat * isl_mat_parameter_compression(__isl_take isl_mat *B, __isl_take isl_vec *d)
__isl_give isl_mat * isl_mat_parameter_compression_ext(__isl_take isl_mat *B, __isl_take isl_mat *A)
__isl_give isl_mat * isl_mat_variable_compression(__isl_take isl_mat *B, __isl_give isl_mat **T2)
#define isl_int_is_nonneg(i)
Definition isl_int.h:37
#define isl_int_is_zero(i)
Definition isl_int.h:31
#define isl_int_is_one(i)
Definition isl_int.h:32
#define isl_int_is_pos(i)
Definition isl_int.h:34
#define isl_int_is_negone(i)
Definition isl_int.h:33
#define isl_int_is_neg(i)
Definition isl_int.h:35
#define isl_int_divexact_ui(r, i, j)
Definition isl_int_gmp.h:45
#define isl_int_gcd(r, i, j)
Definition isl_int_gmp.h:42
#define isl_int_neg(r, i)
Definition isl_int_gmp.h:24
#define isl_int_abs_eq(i, j)
Definition isl_int_gmp.h:64
#define isl_int_gt(i, j)
Definition isl_int_gmp.h:61
#define isl_int_add_ui(r, i, j)
Definition isl_int_gmp.h:27
#define isl_int_le(i, j)
Definition isl_int_gmp.h:60
#define isl_int_add(r, i, j)
Definition isl_int_gmp.h:30
#define isl_int_abs_ge(i, j)
Definition isl_int_gmp.h:68
#define isl_int_addmul(r, i, j)
Definition isl_int_gmp.h:37
#define isl_int_is_divisible_by(i, j)
Definition isl_int_gmp.h:69
#define isl_int_eq(i, j)
Definition isl_int_gmp.h:57
#define isl_int_fdiv_r(r, i, j)
Definition isl_int_gmp.h:50
#define isl_int_abs_gt(i, j)
Definition isl_int_gmp.h:67
#define isl_int_set(r, i)
Definition isl_int_gmp.h:14
#define isl_int_cdiv_q(r, i, j)
Definition isl_int_gmp.h:47
#define isl_int_mul_ui(r, i, j)
Definition isl_int_gmp.h:35
#define isl_int_divexact(r, i, j)
Definition isl_int_gmp.h:44
#define isl_int_sgn(i)
Definition isl_int_gmp.h:54
#define isl_int_mul(r, i, j)
Definition isl_int_gmp.h:32
#define isl_int_lt(i, j)
Definition isl_int_gmp.h:59
#define isl_int_set_si(r, i)
Definition isl_int_gmp.h:15
#define isl_int_ge(i, j)
Definition isl_int_gmp.h:62
mpz_t isl_int
Definition isl_int_gmp.h:9
#define isl_int_sub_ui(r, i, j)
Definition isl_int_gmp.h:28
#define isl_int_fdiv_q(r, i, j)
Definition isl_int_gmp.h:49
#define isl_int_sub(r, i, j)
Definition isl_int_gmp.h:31
#define isl_int_init(i)
Definition isl_int_gmp.h:11
#define isl_int_clear(i)
Definition isl_int_gmp.h:12
#define isl_int_submul(r, i, j)
Definition isl_int_gmp.h:39
__isl_give isl_basic_map * isl_basic_map_sort_divs(__isl_take isl_basic_map *bmap)
isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap, enum isl_dim_type type)
Definition isl_map.c:147
__isl_give isl_basic_map * isl_basic_map_order_divs(__isl_take isl_basic_map *bmap)
Definition isl_map.c:10156
isl_bool isl_basic_map_any_div_involves_vars(__isl_keep isl_basic_map *bmap, unsigned first, unsigned n)
Definition isl_map.c:2882
isl_bool isl_basic_map_div_expr_involves_vars(__isl_keep isl_basic_map *bmap, int div, unsigned first, unsigned n)
Definition isl_map.c:2834
__isl_give isl_basic_map * isl_basic_map_drop_constraints_involving(__isl_take isl_basic_map *bmap, unsigned first, unsigned n)
Definition isl_map.c:3571
__isl_give isl_basic_set * isl_basic_set_set_to_empty(__isl_take isl_basic_set *bset)
Definition isl_map.c:2219
__isl_give isl_basic_set * isl_basic_set_dup(__isl_keep isl_basic_set *bset)
Definition isl_map.c:1457
void isl_basic_map_inequality_to_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1719
isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:266
int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1694
__isl_give isl_basic_map * isl_inequality_negate(__isl_take isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:3959
isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
Definition isl_map.c:3779
unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap, enum isl_dim_type type)
Definition isl_map.c:178
__isl_give isl_basic_set * isl_basic_set_add_ineq(__isl_take isl_basic_set *bset, isl_int *ineq)
Definition isl_map.c:1864
__isl_give isl_basic_set * isl_basic_set_extend_constraints(__isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
Definition isl_map.c:2051
__isl_give isl_basic_map * isl_basic_map_fix(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned pos, isl_int value)
Definition isl_map.c:7119
static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_map.c:73
int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap, isl_int *c1, isl_int *c2)
Definition isl_map.c:10946
int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1870
isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap, int div)
Definition isl_map.c:8631
__isl_give isl_basic_map * isl_basic_map_set_to_empty(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2177
__isl_give isl_basic_map * isl_basic_map_remove_unknown_divs(__isl_take isl_basic_map *bmap)
Definition isl_map.c:3804
int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:8645
__isl_give isl_basic_set * isl_basic_set_free_inequality(__isl_take isl_basic_set *bset, unsigned n)
Definition isl_map.c:1780
__isl_give isl_basic_map * isl_basic_map_free_equality(__isl_take isl_basic_map *bmap, unsigned n)
Definition isl_map.c:1671
int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
Definition isl_map.c:1787
int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1742
__isl_give isl_basic_map * isl_basic_map_add_div_constraint(__isl_take isl_basic_map *bmap, unsigned div, int sign)
Definition isl_map.c:6229
isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Definition isl_map.c:295
__isl_give isl_map * isl_map_cow(__isl_take isl_map *map)
Definition isl_map.c:2107
__isl_give isl_basic_map * isl_basic_map_mark_div_unknown(__isl_take isl_basic_map *bmap, int div)
Definition isl_map.c:8606
__isl_give isl_basic_set * isl_basic_map_underlying_set(__isl_take isl_basic_map *bmap)
Definition isl_map.c:6238
__isl_give isl_basic_set * isl_basic_set_free_equality(__isl_take isl_basic_set *bset, unsigned n)
Definition isl_map.c:1684
__isl_give isl_basic_map * isl_basic_map_cow(__isl_take isl_basic_map *bmap)
Definition isl_map.c:2064
isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1, __isl_keep isl_map *map2, isl_bool(*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
Definition isl_map.c:1607
__isl_give isl_basic_set * isl_basic_set_cow(__isl_take isl_basic_set *bset)
Definition isl_map.c:2059
__isl_give isl_basic_map * isl_basic_map_sort_constraints(__isl_take isl_basic_map *bmap)
Definition isl_map.c:10959
__isl_give isl_basic_map * isl_basic_map_drop_div(__isl_take isl_basic_map *bmap, unsigned div)
Definition isl_map.c:2707
int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
Definition isl_map.c:1804
int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1631
isl_bool isl_basic_map_div_is_integral(__isl_keep isl_basic_map *bmap, int div)
Definition isl_map.c:8596
__isl_give isl_basic_map * isl_basic_map_extend_constraints(__isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
Definition isl_map.c:2045
isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:8672
__isl_give isl_basic_map * isl_basic_map_reset_space(__isl_take isl_basic_map *bmap, __isl_take isl_space *space)
Definition isl_map.c:6415
__isl_give isl_basic_map * isl_basic_map_extend(__isl_take isl_basic_map *base, unsigned extra, unsigned n_eq, unsigned n_ineq)
Definition isl_map.c:1996
__isl_give isl_basic_map * isl_basic_map_overlying_set(__isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
Definition isl_map.c:6301
__isl_give isl_basic_map * isl_basic_map_align_divs(__isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
Definition isl_map.c:10334
isl_bool isl_map_divs_known(__isl_keep isl_map *map)
Definition isl_map.c:8687
isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1, enum isl_dim_type type1, __isl_keep isl_map *map2, enum isl_dim_type type2)
Definition isl_map.c:9401
__isl_give isl_basic_set * isl_basic_set_sort_constraints(__isl_take isl_basic_set *bset)
Definition isl_map.c:10982
__isl_give isl_basic_map * isl_basic_map_alloc_space(__isl_take isl_space *space, unsigned extra, unsigned n_eq, unsigned n_ineq)
Definition isl_map.c:1375
#define ISL_BASIC_MAP_SORTED
#define ISL_BASIC_MAP_NO_REDUNDANT
isl_stat isl_map_align_params_bin(__isl_keep isl_map **map1, __isl_keep isl_map **map2)
#define ISL_BASIC_SET_NO_REDUNDANT
#define ISL_BASIC_MAP_EMPTY
__isl_give isl_basic_set * isl_basic_set_preimage(__isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
Definition isl_mat.c:1360
isl_stat isl_basic_map_check_equal_space(__isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
#define ISL_MAP_DISJOINT
#define ISL_BASIC_MAP_REDUCED_COEFFICIENTS
#define ISL_BASIC_SET_NO_IMPLICIT
#define ISL_BASIC_SET_FINAL
isl_stat isl_map_basic_map_check_equal_space(__isl_keep isl_map *map, __isl_keep isl_basic_map *bmap)
#define ISL_BASIC_MAP_RATIONAL
#define ISL_BASIC_MAP_NO_IMPLICIT
#define ISL_BASIC_MAP_NORMALIZED_DIVS
#define ISL_MAP_NORMALIZED
#define isl_set
#define isl_basic_set
isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap, enum isl_dim_type type, unsigned first, unsigned n)
static __isl_give isl_basic_map * isl_basic_map_preimage_vars(__isl_take isl_basic_map *bmap, unsigned pos, __isl_take isl_mat *T)
static __isl_give isl_map * replace_by_disjunct(__isl_take isl_map *map, int pos, __isl_take isl_basic_map *context)
static isl_bool is_pure_unit_div(__isl_keep isl_basic_map *bmap, int div)
static __isl_give isl_basic_set * drop_irrelevant_constraints(__isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
static __isl_give isl_basic_map * eliminate_div(__isl_take isl_basic_map *bmap, isl_int *eq, unsigned div, int keep_divs, int equivalent)
static void scale_down_inequality(isl_int *ineq, isl_int f, unsigned len)
static __isl_give isl_mat * combined_variable_compression(__isl_keep isl_basic_map *bmap1, int n1, __isl_keep isl_basic_map *bmap2, int n2, int total)
static __isl_give isl_basic_map * drop_more_redundant_divs(__isl_take isl_basic_map *bmap, __isl_take int *pairs, int n)
static __isl_give isl_basic_map * set_eq_and_try_again(__isl_take isl_basic_map *bmap, int ineq, __isl_take int *pairs)
static int set_hash_index(struct isl_constraint_index *ci, __isl_keep isl_basic_set *bset, int k)
__isl_give isl_basic_map * isl_basic_map_remove_duplicate_constraints(__isl_take isl_basic_map *bmap, int *progress, int detect_divs)
static isl_bool isl_map_plain_is_disjoint_global(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
static isl_stat mark_shifted_constraints(__isl_keep isl_mat *ineq, __isl_keep isl_basic_set *context, int *row)
static isl_bool has_multiple_var_equality(__isl_keep isl_basic_map *bmap)
static isl_bool all_pairs(__isl_keep isl_map *map1, __isl_keep isl_map *map2, isl_bool(*test)(__isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2))
static __isl_give isl_basic_map * set_div_from_lower_bound(__isl_take isl_basic_map *bmap, int div, isl_int *ineq)
__isl_give isl_basic_map * isl_basic_map_eliminate_vars(__isl_take isl_basic_map *bmap, unsigned pos, unsigned n)
__isl_give isl_set * isl_set_plain_gist_basic_set(__isl_take isl_set *set, __isl_take isl_basic_set *context)
static __isl_give isl_basic_set * isl_basic_set_reduce_using_equalities(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
__isl_give isl_basic_map * isl_basic_map_simplify(__isl_take isl_basic_map *bmap)
static void set_compute_elimination_index(__isl_keep isl_basic_set *bset, int *elim, unsigned len)
static void constraint_index_free(struct isl_constraint_index *ci)
static __isl_give isl_basic_set * drop_irrelevant_constraints_marked(__isl_take isl_basic_set *context, __isl_keep isl_mat *ineq, int *row)
static __isl_give isl_basic_map * drop_div_and_try_again(__isl_take isl_basic_map *bmap, int div, int ineq1, int ineq2, __isl_take int *pairs)
static void test_ineq_data_clear(struct test_ineq_data *data)
static __isl_give isl_basic_map * gist_strides(__isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
__isl_give isl_basic_map * isl_basic_map_gist_domain(__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *context)
static __isl_give isl_basic_map * coalesce_divs(__isl_take isl_basic_map *bmap, unsigned div1, unsigned div2, unsigned l, unsigned u)
static void isl_reduce_coefficients_data_clear(struct isl_reduce_coefficients_data *data)
static isl_bool has_nested_unit_div(__isl_keep isl_basic_map *bmap, int base, int ineq, unsigned v_div, unsigned n_div, isl_int n)
static __isl_give isl_basic_map * isl_basic_map_drop_redundant_divs_ineq(__isl_take isl_basic_map *bmap)
static __isl_give isl_basic_map * check_for_residues(__isl_take isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
static unsigned int round_up(unsigned int v)
static isl_bool is_opposite(__isl_keep isl_basic_map *bmap, int i, int j)
static isl_bool div_is_redundant(__isl_keep isl_basic_map *bmap, int div)
static __isl_give isl_basic_map * normalize_div_expression(__isl_take isl_basic_map *bmap, int div)
static int hash_index_ineq(struct isl_constraint_index *ci, isl_int **ineq)
__isl_give isl_basic_set * isl_basic_set_drop_constraints_involving_unknown_divs(__isl_take isl_basic_set *bset)
static __isl_give isl_basic_map * normalize_div_expressions(__isl_take isl_basic_map *bmap)
isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
static __isl_give isl_basic_set * uset_gist_uncompressed(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
static __isl_give isl_mat * extract_ineq(__isl_keep isl_basic_set *bset)
__isl_give isl_basic_map * isl_basic_map_finalize(__isl_take isl_basic_map *bmap)
static isl_bool is_known_integral_div(__isl_keep isl_basic_map *bmap, int div)
static isl_bool ok_to_eliminate_div(__isl_keep isl_basic_map *bmap, isl_int *eq, unsigned div)
static __isl_give isl_basic_set * project_onto_involved(__isl_take isl_basic_set *bset, __isl_keep isl_basic_set *template)
__isl_give isl_basic_map * isl_basic_map_gauss(__isl_take isl_basic_map *bmap, int *progress)
static __isl_give isl_basic_map * drop_equalities(__isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
static __isl_give isl_basic_map * isl_basic_map_remove_shifted_constraints(__isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2)
static __isl_give isl_basic_map * eliminate_unit_div(__isl_take isl_basic_map *bmap, int div, int *progress)
__isl_give isl_basic_map * isl_basic_map_gist(__isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
__isl_give isl_map * isl_map_gist(__isl_take isl_map *map, __isl_take isl_map *context)
static int is_residue_div_pair(__isl_take isl_basic_map *bmap, int l, int div, unsigned v_div, int i, int j, isl_int n)
static __isl_give isl_basic_map * exploit_opposite_constraints(__isl_take isl_basic_map *bmap, int *opposite, int *progress, int detect_divs)
static isl_stat create_constraint_index(struct isl_constraint_index *ci, __isl_keep isl_basic_map *bmap)
static __isl_give isl_basic_map * set_div_from_eq(__isl_take isl_basic_map *bmap, int div, int eq, int *progress)
static __isl_give isl_basic_map * reduce_coefficients(__isl_take isl_basic_map *bmap, struct isl_reduce_coefficients_data *data)
__isl_give isl_basic_set * isl_basic_set_gist(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
__isl_give isl_basic_map * isl_basic_map_eliminate(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first, unsigned n)
static __isl_give isl_basic_map * coalesce_or_drop_more_redundant_divs(__isl_take isl_basic_map *bmap, int *pairs, int n)
__isl_give isl_basic_map * isl_basic_map_detect_inequality_pairs(__isl_take isl_basic_map *bmap, int *progress)
static __isl_give isl_mat * extract_compressed_stride_constraints(__isl_keep isl_basic_map *bmap, int bmap_n_eq, __isl_keep isl_basic_map *context, int context_n_eq)
__isl_give isl_basic_map * isl_basic_map_reduce_coefficients(__isl_take isl_basic_map *bmap)
static isl_bool is_any_div(__isl_keep isl_basic_map *bmap, int div)
isl_bool isl_basic_set_is_disjoint(__isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2)
static int is_opposite_part(__isl_keep isl_basic_map *bmap, int i, int j, int first, int n)
isl_bool isl_basic_map_is_div_constraint(__isl_keep isl_basic_map *bmap, isl_int *constraint, unsigned div)
__isl_give isl_basic_map * isl_basic_map_shift_div(__isl_take isl_basic_map *bmap, int div, int pos, isl_int shift)
static __isl_give isl_basic_map * drop_redundant_divs_again(__isl_take isl_basic_map *bmap, __isl_take int *pairs, int simplify)
__isl_give isl_map * isl_map_gist_params(__isl_take isl_map *map, __isl_take isl_set *context)
static int is_related(isl_int *c, int len, int *relevant)
__isl_give isl_map * isl_map_plain_gist_basic_map(__isl_take isl_map *map, __isl_take isl_basic_map *context)
static __isl_give isl_basic_map * remove_dependent_vars(__isl_take isl_basic_map *bmap, int pos)
static __isl_give isl_basic_set * uset_gist_compressed(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *context, __isl_take isl_mat *T)
isl_bool isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
static int n_pure_div_eq(__isl_keep isl_basic_map *bmap)
static isl_bool isl_map_is_disjoint_aligned(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
static __isl_give isl_basic_set * replace_by_empty(__isl_take isl_basic_set *bset)
static __isl_give isl_basic_map * reduce_stride_constraints(__isl_take isl_basic_map *bmap, int n, __isl_keep isl_mat *A)
static __isl_give isl_map * replace_by_universe(__isl_take isl_map *map, __isl_take isl_map *drop)
static __isl_give isl_basic_map * reduce_coefficient_in_div(__isl_take isl_basic_map *bmap, int div, int pos)
static void lower_bound_from_parallel(__isl_keep isl_basic_map *bmap, int ineq, int lower, int pos, isl_int *l)
__isl_give isl_basic_set * isl_basic_set_simplify(__isl_take isl_basic_set *bset)
__isl_give isl_basic_map * isl_basic_map_gauss5(__isl_take isl_basic_map *bmap, int *progress, isl_stat(*swap)(unsigned a, unsigned b, void *user), isl_stat(*drop)(unsigned n, void *user), void *user)
__isl_give isl_basic_map * isl_basic_map_plain_gist(__isl_take isl_basic_map *bmap, __isl_take isl_basic_map *context)
__isl_give isl_basic_map * isl_basic_map_drop_redundant_divs(__isl_take isl_basic_map *bmap)
static isl_bool ok_to_set_div_from_bound(__isl_keep isl_basic_map *bmap, int div, isl_int *ineq)
static void swap_inequality(__isl_keep isl_basic_map *bmap, int a, int b)
static __isl_give isl_basic_map * eliminate_divs_eq(__isl_take isl_basic_map *bmap, int *progress)
__isl_give isl_set * isl_set_gist(__isl_take isl_set *set, __isl_take isl_set *context)
__isl_give isl_basic_map * isl_basic_map_eliminate_pure_unit_divs(__isl_take isl_basic_map *bmap)
__isl_give isl_basic_map * isl_basic_map_drop_constraints_involving_unknown_divs(__isl_take isl_basic_map *bmap)
static isl_bool int_between_bounds(__isl_keep isl_basic_map *bmap, int i, int l, int u, struct test_ineq_data *data)
static __isl_give isl_basic_map * remove_redundant_divs(__isl_take isl_basic_map *bmap)
static int hash_index(struct isl_constraint_index *ci, __isl_keep isl_basic_map *bmap, int k)
static isl_stat isl_reduce_coefficients_data_init(__isl_keep isl_basic_map *bmap, struct isl_reduce_coefficients_data *data)
static int set_reduced_using_equalities(isl_int *dst, isl_int *src, __isl_keep isl_basic_set *bset, int *elim, unsigned total)
static __isl_give isl_basic_map * check_for_div_constraints(__isl_take isl_basic_map *bmap, int *opposite, int k, int l, isl_int sum, int *progress)
isl_bool isl_map_plain_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
static isl_bool constraint_index_is_redundant(struct isl_constraint_index *ci, isl_int *ineq)
static __isl_give isl_basic_map * set_residue_div(__isl_take isl_basic_map *bmap, int l, isl_int sum, int div, unsigned v_div, unsigned total, int i, int j, int *progress)
static isl_bool needs_reduction(__isl_keep isl_basic_map *bmap, int div, int pos)
static __isl_give isl_basic_map * eliminate_divs_ineq(__isl_take isl_basic_map *bmap, int *progress)
__isl_give isl_map * isl_map_drop_constraints_involving_unknown_divs(__isl_take isl_map *map)
static isl_size div_find_coalesce(__isl_keep isl_basic_map *bmap, int *pairs, unsigned div, unsigned l, unsigned u)
static int is_residue(isl_int *res, isl_int *ineq, isl_int sum, unsigned len, isl_int *gcd)
__isl_give isl_basic_map * isl_basic_map_mark_final(__isl_take isl_basic_map *bmap)
__isl_give isl_map * isl_map_plain_gist(__isl_take isl_map *map, __isl_take isl_map *context)
static __isl_give isl_basic_map * reduce_div_coefficients(__isl_take isl_basic_map *bmap)
static isl_bool single_unknown(__isl_keep isl_basic_map *bmap, int ineq, int div)
__isl_give isl_basic_set * isl_basic_set_eliminate(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first, unsigned n)
static void swap_equality(__isl_keep isl_basic_map *bmap, int a, int b)
__isl_give isl_basic_set * isl_basic_set_gauss(__isl_take isl_basic_set *bset, int *progress)
static __isl_give isl_basic_map * basic_map_from_equalities(__isl_take isl_space *space, __isl_take isl_mat *eq)
static void mark_progress(int *progress)
__isl_give isl_basic_set * isl_basic_set_eliminate_vars(__isl_take isl_basic_set *bset, unsigned pos, unsigned n)
__isl_give isl_basic_set * isl_basic_set_finalize(__isl_take isl_basic_set *bset)
__isl_give isl_map * isl_map_gist_domain(__isl_take isl_map *map, __isl_take isl_set *context)
static void update_groups(int dim, int *group, isl_int *c)
static isl_bool is_parallel_except(__isl_keep isl_basic_map *bmap, int i, int j, int pos)
__isl_give isl_map * isl_map_gist_basic_map(__isl_take isl_map *map, __isl_take isl_basic_map *context)
static isl_bool test_ineq_is_satisfied(__isl_keep isl_basic_map *bmap, struct test_ineq_data *data)
static int all_neg(int *row, int n)
__isl_give isl_set * isl_set_gist_basic_set(__isl_take isl_set *set, __isl_take isl_basic_set *context)
static int lower_bound_is_cst(__isl_keep isl_basic_map *bmap, int div, int ineq)
static __isl_give isl_basic_map * drop_unrelated_constraints(__isl_take isl_basic_map *bmap, int *relevant)
__isl_give isl_basic_map * isl_basic_map_drop_unrelated_constraints(__isl_take isl_basic_map *bmap, __isl_take int *group)
static __isl_give isl_basic_map * remove_duplicate_divs(__isl_take isl_basic_map *bmap, int *progress)
__isl_give isl_map * isl_map_gist_range(__isl_take isl_map *map, __isl_take isl_set *context)
static int is_zero_or_one(isl_int v)
isl_bool isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
static __isl_give isl_basic_map * drop_inequalities(__isl_take isl_basic_map *bmap, __isl_keep isl_basic_map *context)
static void remove_incomplete_powers(isl_int *g, isl_int c)
__isl_give isl_basic_map * isl_basic_map_normalize_constraints(__isl_take isl_basic_map *bmap)
static int * alloc_groups(__isl_keep isl_basic_set *context)
static void compute_elimination_index(__isl_keep isl_basic_map *bmap, int *elim, unsigned len)
static __isl_give isl_basic_set * uset_gist_full(__isl_take isl_basic_set *bset, __isl_take isl_mat *ineq, __isl_take isl_basic_set *context)
__isl_give isl_set * isl_set_gist_params(__isl_take isl_set *set, __isl_take isl_set *context)
static isl_bool has_coef_one(__isl_keep isl_basic_map *bmap, int div, int ineq)
static isl_bool is_div_constraint(__isl_keep isl_basic_map *bmap, int ineq, unsigned v_div, unsigned n_div)
isl_bool isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
static __isl_give isl_basic_map * eliminate_integral_divs(__isl_take isl_basic_map *bmap, int *progress)
static __isl_give isl_basic_set * remove_shifted_constraints(__isl_take isl_basic_set *bset, __isl_keep isl_basic_set *context)
static isl_bool better_div_constraint(__isl_keep isl_basic_map *bmap, int div, isl_int *ineq)
static isl_stat preimage(isl_int *c, __isl_keep isl_mat *T)
static __isl_give isl_vec * normalize_constraint(__isl_take isl_vec *v, int *tightened)
__isl_give isl_set * isl_set_gist_params_basic_set(__isl_take isl_set *set, __isl_take isl_basic_set *context)
static __isl_give isl_basic_set * update_ineq_free(__isl_take isl_basic_set *bset, __isl_take isl_mat *ineq, __isl_take isl_basic_set *context, __isl_take int *row, struct isl_tab *tab)
static __isl_give isl_basic_map * reduce_div_coefficients_of_div(__isl_take isl_basic_map *bmap, int div)
static isl_bool any_div_involves_div(__isl_keep isl_basic_map *bmap, int div)
static isl_bool is_opposite_except(__isl_keep isl_basic_map *bmap, int i, int j, int pos)
static int n_div_eq(__isl_keep isl_basic_map *bmap)
static __isl_give isl_basic_map * check_for_residue_div(__isl_take isl_basic_map *bmap, int *opposite, int l, isl_int sum, int div, int *progress)
static int reduced_using_equalities(isl_int *dst, isl_int *src, __isl_keep isl_basic_map *bmap, int *elim, unsigned total)
static __isl_give isl_basic_map * check_for_residues_in_divs(__isl_take isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
static __isl_give isl_basic_map * eliminate_unit_divs(__isl_take isl_basic_map *bmap, int *progress)
__isl_give isl_basic_set * isl_basic_set_normalize_constraints(__isl_take isl_basic_set *bset)
static __isl_give isl_basic_set * update_ineq(__isl_take isl_basic_set *bset, __isl_keep int *row, struct isl_tab *tab)
static isl_bool bmap_eq_involves_unknown_divs(__isl_keep isl_basic_map *bmap, int eq, unsigned first, unsigned n)
static void lower_bound_from_opposite(__isl_keep isl_basic_map *bmap, int ineq, int upper, int pos, isl_int *u)
static __isl_give isl_basic_set * uset_gist(__isl_take isl_basic_set *bset, __isl_take isl_basic_set *context)
static __isl_give isl_basic_map * eliminate_selected_unit_divs(__isl_take isl_basic_map *bmap, isl_bool(*select)(__isl_keep isl_basic_map *bmap, int div), int *progress)
static int is_parallel_part(__isl_keep isl_basic_map *bmap, int i, int j, int first, int n)
static __isl_give isl_basic_map * eliminate_var_using_equality(__isl_take isl_basic_map *bmap, unsigned pos, isl_int *eq, int keep_divs, int equivalent, int *progress)
static __isl_give isl_basic_map * eliminate_integral_div(__isl_take isl_basic_map *bmap, int div)
isl_bool isl_set_plain_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
static isl_stat setup_constraint_index(struct isl_constraint_index *ci, __isl_keep isl_basic_set *bset)
static int * detect_opposites(struct isl_constraint_index *ci, __isl_keep isl_basic_map *bmap)
static __isl_give isl_basic_map * fix_cst_lower(__isl_take isl_basic_map *bmap, int div, int ineq, int lower, int *pairs)
static int residue_exceeded(isl_int cst, isl_int cst2, isl_int n, isl_int sum)
static __isl_give isl_basic_map * scale_down_bmap_inequality(__isl_take isl_basic_map *bmap, int ineq, isl_int f, unsigned len)
static __isl_give isl_basic_map * isl_basic_map_modify_inequality(__isl_take isl_basic_map *bmap, int equivalent)
static __isl_give isl_basic_map * set_div_from_lower_bound_if_better(__isl_take isl_basic_map *bmap, int div, isl_int *ineq, int *progress)
static __isl_give isl_basic_map * normalize_divs(__isl_take isl_basic_map *bmap, int *progress)
__isl_give isl_mat * isl_mat_cow(__isl_take isl_mat *mat)
Definition isl_mat.c:226
__isl_give isl_mat * isl_mat_sub_alloc6(isl_ctx *ctx, isl_int **row, unsigned first_row, unsigned n_row, unsigned first_col, unsigned n_col)
Definition isl_mat.c:148
isl_stat isl_mat_row_gcd(__isl_keep isl_mat *mat, int row, isl_int *gcd)
Definition isl_mat.c:1911
void isl_mat_col_submul(__isl_keep isl_mat *mat, int dst_col, isl_int f, int src_col)
Definition isl_mat.c:1649
static __isl_give isl_mat * eliminate(__isl_take isl_mat *mat, int row, int col)
Definition isl_mat.c:725
static isl_stat set_div(__isl_take isl_set *set, __isl_take isl_qpolynomial *qp, int div, isl_int v, struct isl_split_periods_data *data)
void isl_seq_submul(isl_int *dst, isl_int f, isl_int *src, unsigned len)
Definition isl_seq.c:60
void isl_seq_combine(isl_int *dst, isl_int m1, isl_int *src1, isl_int m2, isl_int *src2, unsigned len)
Definition isl_seq.c:116
int isl_seq_last_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:217
int isl_seq_is_neg(isl_int *p1, isl_int *p2, unsigned len)
Definition isl_seq.c:192
int isl_seq_first_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:207
void isl_seq_scale_down(isl_int *dst, isl_int *src, isl_int m, unsigned len)
Definition isl_seq.c:88
void isl_seq_swp_or_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:74
void isl_seq_gcd(isl_int *p, unsigned len, isl_int *gcd)
Definition isl_seq.c:260
void isl_seq_sub(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:52
void isl_seq_clr(isl_int *p, unsigned len)
Definition isl_seq.c:14
void isl_seq_elim(isl_int *dst, isl_int *src, unsigned pos, unsigned len, isl_int *m)
Definition isl_seq.c:146
void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:42
int isl_seq_eq(isl_int *p1, isl_int *p2, unsigned len)
Definition isl_seq.c:173
int isl_seq_any_non_zero(isl_int *p, unsigned len)
Definition isl_seq.c:230
uint32_t isl_seq_get_hash_bits(isl_int *p, unsigned len, unsigned bits)
Definition isl_seq.c:364
void isl_seq_normalize(struct isl_ctx *ctx, isl_int *p, unsigned len)
Definition isl_seq.c:278
void isl_seq_neg(isl_int *dst, isl_int *src, unsigned len)
Definition isl_seq.c:35
static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1, __isl_keep isl_space *space2, enum isl_dim_type type2)
Definition isl_space.c:1145
static __isl_keep isl_space * nested(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:1015
int isl_tab_detect_redundant(struct isl_tab *tab)
Definition isl_tab.c:3343
void isl_tab_free(struct isl_tab *tab)
Definition isl_tab.c:204
int isl_tab_is_redundant(struct isl_tab *tab, int con)
Definition isl_tab.c:3507
int isl_tab_is_equality(struct isl_tab *tab, int con)
Definition isl_tab.c:3397
__isl_give isl_basic_set * isl_basic_set_update_from_tab(__isl_take isl_basic_set *bset, struct isl_tab *tab)
Definition isl_tab.c:2663
isl_stat isl_tab_add_ineq(struct isl_tab *tab, isl_int *ineq)
Definition isl_tab.c:1906
enum isl_lp_result isl_tab_min(struct isl_tab *tab, isl_int *f, isl_int denom, isl_int *opt, isl_int *opt_denom, unsigned flags)
Definition isl_tab.c:3430
int isl_tab_detect_implicit_equalities(struct isl_tab *tab)
Definition isl_tab.c:2979
__isl_give struct isl_tab * isl_tab_from_basic_map(__isl_keep isl_basic_map *bmap, int track)
Definition isl_tab.c:2394
__isl_give struct isl_tab * isl_tab_from_basic_set(__isl_keep isl_basic_set *bset, int track)
Definition isl_tab.c:2434
int isl_tab_freeze_constraint(struct isl_tab *tab, int con)
Definition isl_tab.c:1017
int isl_tab_extend_cons(struct isl_tab *tab, unsigned n_new)
Definition isl_tab.c:105
#define C(FN,...)
Definition isl_test2.cc:266
enum isl_fold type
Definition isl_test.c:3810
const char * set
Definition isl_test.c:1364
const char * hull
Definition isl_test.c:1493
const char * map
Definition isl_test.c:1734
int equal
Definition isl_test.c:7663
const char * p
Definition isl_test.c:8397
const char * context
Definition isl_test.c:1735
const char * map1
Definition isl_test.c:365
const char * aff
Definition isl_test.c:7073
const char * map2
Definition isl_test.c:366
const char * set1
Definition isl_test.c:3998
const char * res
Definition isl_test.c:783
const char * set2
Definition isl_test.c:3999
int subset
Definition isl_test.c:4000
const char * f
Definition isl_test.c:8396
static __isl_give isl_union_map * total(__isl_take isl_union_map *umap, __isl_give isl_map *(*fn)(__isl_take isl_map *))
__isl_give isl_vec * isl_vec_cow(__isl_take isl_vec *vec)
Definition isl_vec.c:220
t0 *a *b *t *a *b * t
isl_lp_result
Definition lp.h:17
@ isl_lp_error
Definition lp.h:18
@ isl_lp_ok
Definition lp.h:19
__isl_export __isl_give isl_map * isl_map_intersect_range(__isl_take isl_map *map, __isl_take isl_set *set)
Definition isl_map.c:8973
__isl_export isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:10044
__isl_give isl_space * isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:417
isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:9950
__isl_export isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
Definition isl_map.c:11923
__isl_export __isl_give isl_map * isl_map_intersect_params(__isl_take isl_map *map, __isl_take isl_set *params)
Definition isl_map.c:4531
__isl_give isl_basic_map * isl_basic_map_project_out(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:5125
__isl_export isl_bool isl_map_is_empty(__isl_keep isl_map *map)
Definition isl_map.c:9801
__isl_export __isl_give isl_map * isl_map_universe(__isl_take isl_space *space)
Definition isl_map.c:6967
__isl_give isl_basic_map * isl_basic_map_insert_dims(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned pos, unsigned n)
Definition isl_map.c:4654
isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:10089
__isl_give isl_basic_map * isl_basic_map_drop_constraints_involving_dims(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:3666
isl_bool isl_basic_map_is_disjoint(__isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
__isl_give isl_basic_map * isl_map_unshifted_simple_hull_from_map_list(__isl_take isl_map *map, __isl_take isl_map_list *list)
__isl_give isl_map * isl_map_copy(__isl_keep isl_map *map)
Definition isl_map.c:1494
__isl_null isl_basic_map * isl_basic_map_free(__isl_take isl_basic_map *bmap)
Definition isl_map.c:1503
isl_ctx * isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:382
__isl_give isl_basic_map * isl_basic_map_remove_dims(__isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:2813
__isl_export __isl_give isl_space * isl_map_get_space(__isl_keep isl_map *map)
Definition isl_map.c:599
isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap, enum isl_dim_type type)
Definition isl_map.c:83
isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
Definition isl_map.c:10023
isl_ctx * isl_map_get_ctx(__isl_keep isl_map *map)
Definition isl_map.c:392
__isl_export __isl_give isl_map * isl_map_intersect_domain(__isl_take isl_map *map, __isl_take isl_set *set)
Definition isl_map.c:9001
isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
Definition isl_map.c:9818
__isl_give isl_basic_map * isl_basic_map_remove_redundancies(__isl_take isl_basic_map *bmap)
__isl_export __isl_give isl_basic_map * isl_basic_map_intersect_domain(__isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
Definition isl_map.c:4118
__isl_export __isl_give isl_basic_map * isl_map_unshifted_simple_hull(__isl_take isl_map *map)
isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1136
__isl_null isl_map * isl_map_free(__isl_take isl_map *map)
Definition isl_map.c:7040
__isl_constructor __isl_give isl_map * isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
Definition isl_map.c:4037
__isl_give isl_basic_map * isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
Definition isl_map.c:1479
isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
Definition isl_map.c:11204
__isl_give isl_map * isl_map_compute_divs(__isl_take isl_map *map)
Definition isl_map.c:8734
__isl_export __isl_give isl_basic_map * isl_basic_map_intersect(__isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
Definition isl_map.c:4246
__isl_give isl_basic_map * isl_map_simple_hull(__isl_take isl_map *map)
__isl_export isl_bool isl_map_is_subset(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
__isl_give isl_basic_map * isl_basic_map_universe(__isl_take isl_space *space)
Definition isl_map.c:6902
__isl_give isl_mat * isl_mat_copy(__isl_keep isl_mat *mat)
Definition isl_mat.c:202
__isl_give isl_mat * isl_mat_normalize_row(__isl_take isl_mat *mat, int row)
Definition isl_mat.c:2004
__isl_give isl_mat * isl_mat_product(__isl_take isl_mat *left, __isl_take isl_mat *right)
Definition isl_mat.c:1271
isl_size isl_mat_cols(__isl_keep isl_mat *mat)
Definition isl_mat.c:262
isl_size isl_mat_rows(__isl_keep isl_mat *mat)
Definition isl_mat.c:257
__isl_give isl_mat * isl_mat_identity(isl_ctx *ctx, unsigned n_row)
Definition isl_mat.c:419
__isl_give isl_mat * isl_mat_diagonal(__isl_take isl_mat *mat1, __isl_take isl_mat *mat2)
Definition isl_mat.c:921
__isl_give isl_mat * isl_mat_right_inverse(__isl_take isl_mat *mat)
Definition isl_mat.c:1124
__isl_null isl_mat * isl_mat_free(__isl_take isl_mat *mat)
Definition isl_mat.c:240
__isl_give isl_mat * isl_mat_alloc(isl_ctx *ctx, unsigned n_row, unsigned n_col)
Definition isl_mat.c:53
isl_ctx * isl_mat_get_ctx(__isl_keep isl_mat *mat)
Definition isl_mat.c:25
__isl_give isl_mat * isl_mat_concat(__isl_take isl_mat *top, __isl_take isl_mat *bot)
Definition isl_mat.c:1766
__isl_give isl_mat * isl_mat_unimodular_complete(__isl_take isl_mat *M, int row)
Definition isl_mat.c:1738
__isl_give isl_vec * isl_vec_mat_product(__isl_take isl_vec *vec, __isl_take isl_mat *mat)
Definition isl_mat.c:506
static bool any(const std::vector< bool > &vector)
Definition python.cc:576
a(0)
b(9)
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition isl_map.c:202
isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
Definition isl_map.c:9959
__isl_export __isl_give isl_basic_set * isl_basic_set_detect_equalities(__isl_take isl_basic_set *bset)
isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:3522
__isl_export __isl_give isl_space * isl_set_get_space(__isl_keep isl_set *set)
Definition isl_map.c:604
__isl_give isl_space * isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
Definition isl_map.c:422
__isl_give isl_basic_set * isl_basic_set_project_out(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first, unsigned n)
Definition isl_map.c:5170
__isl_export __isl_give isl_basic_set * isl_basic_set_intersect_params(__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
Definition isl_map.c:4329
__isl_null isl_basic_set * isl_basic_set_free(__isl_take isl_basic_set *bset)
Definition isl_map.c:1523
isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
Definition isl_map.c:10096
__isl_give isl_basic_set * isl_basic_set_add_dims(__isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned n)
Definition isl_map.c:4734
__isl_give isl_basic_set * isl_basic_set_empty(__isl_take isl_space *space)
Definition isl_map.c:6894
__isl_export isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
Definition isl_map.c:10123
__isl_export __isl_give isl_basic_set * isl_basic_set_affine_hull(__isl_take isl_basic_set *bset)
isl_ctx * isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
Definition isl_map.c:387
__isl_give isl_basic_set * isl_basic_set_copy(__isl_keep isl_basic_set *bset)
Definition isl_map.c:1465
__isl_export __isl_give isl_basic_set * isl_basic_set_intersect(__isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
Definition isl_map.c:4312
__isl_give isl_basic_set * isl_basic_set_universe(__isl_take isl_space *space)
Definition isl_map.c:6910
static __isl_give isl_set * set_from_map(__isl_take isl_map *map)
Definition set_from_map.c:5
__isl_null isl_space * isl_space_free(__isl_take isl_space *space)
Definition isl_space.c:478
isl_ctx * isl_space_get_ctx(__isl_keep isl_space *space)
Definition isl_space.c:23
__isl_give isl_space * isl_space_copy(__isl_keep isl_space *space)
Definition isl_space.c:469
isl_size isl_space_dim(__isl_keep isl_space *space, enum isl_dim_type type)
Definition isl_space.c:372
isl_dim_type
Definition space_type.h:13
@ isl_dim_in
Definition space_type.h:16
@ isl_dim_set
Definition space_type.h:18
@ isl_dim_all
Definition space_type.h:20
@ isl_dim_div
Definition space_type.h:19
@ isl_dim_out
Definition space_type.h:17
isl_int ** ineq
isl_int * data
Definition isl_blk.h:21
isl_int normalize_gcd
isl_int one
isl_int negone
unsigned n_row
unsigned n_col
struct isl_ctx * ctx
isl_int ** row
unsigned is_redundant
Definition isl_tab.h:25
unsigned n_col
Definition isl_tab.h:140
unsigned empty
Definition isl_tab.h:179
unsigned n_eq
Definition isl_tab.h:149
struct isl_tab_var * con
Definition isl_tab.h:152
struct isl_basic_map * bmap
Definition isl_tab.h:161
unsigned n_row
Definition isl_tab.h:139
unsigned n_div
Definition isl_tab.h:146
isl_int * el
struct isl_blk block
unsigned size
struct isl_tab * tab
static bool equivalent(const Kind &kind1, const Kind &kind2)
static Signature set_to_map
static std::vector< std::string > intersect(const std::vector< std::string > &v1, const std::vector< std::string > &v2)
isl_ctx * isl_vec_get_ctx(__isl_keep isl_vec *vec)
Definition isl_vec.c:18
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition isl_vec.c:234
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition isl_vec.c:33
n
Definition youcefn.c:8