2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2012-2013 Ecole Normale Superieure
4 * Copyright 2014-2015 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include "isl_equalities.h"
22 #include <isl_space_private.h>
23 #include <isl_mat_private.h>
24 #include <isl_vec_private.h>
26 #include <bset_to_bmap.c>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
31 static void swap_equality(struct isl_basic_map
*bmap
, int a
, int b
)
33 isl_int
*t
= bmap
->eq
[a
];
34 bmap
->eq
[a
] = bmap
->eq
[b
];
38 static void swap_inequality(struct isl_basic_map
*bmap
, int a
, int b
)
41 isl_int
*t
= bmap
->ineq
[a
];
42 bmap
->ineq
[a
] = bmap
->ineq
[b
];
47 static void constraint_drop_vars(isl_int
*c
, unsigned n
, unsigned rem
)
49 isl_seq_cpy(c
, c
+ n
, rem
);
50 isl_seq_clr(c
+ rem
, n
);
53 /* Drop n dimensions starting at first.
55 * In principle, this frees up some extra variables as the number
56 * of columns remains constant, but we would have to extend
57 * the div array too as the number of rows in this array is assumed
58 * to be equal to extra.
60 struct isl_basic_set
*isl_basic_set_drop_dims(
61 struct isl_basic_set
*bset
, unsigned first
, unsigned n
)
68 isl_assert(bset
->ctx
, first
+ n
<= bset
->dim
->n_out
, goto error
);
70 if (n
== 0 && !isl_space_get_tuple_name(bset
->dim
, isl_dim_set
))
73 bset
= isl_basic_set_cow(bset
);
77 for (i
= 0; i
< bset
->n_eq
; ++i
)
78 constraint_drop_vars(bset
->eq
[i
]+1+bset
->dim
->nparam
+first
, n
,
79 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
81 for (i
= 0; i
< bset
->n_ineq
; ++i
)
82 constraint_drop_vars(bset
->ineq
[i
]+1+bset
->dim
->nparam
+first
, n
,
83 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
85 for (i
= 0; i
< bset
->n_div
; ++i
)
86 constraint_drop_vars(bset
->div
[i
]+1+1+bset
->dim
->nparam
+first
, n
,
87 (bset
->dim
->n_out
-first
-n
)+bset
->extra
);
89 bset
->dim
= isl_space_drop_outputs(bset
->dim
, first
, n
);
93 ISL_F_CLR(bset
, ISL_BASIC_SET_NORMALIZED
);
94 bset
= isl_basic_set_simplify(bset
);
95 return isl_basic_set_finalize(bset
);
97 isl_basic_set_free(bset
);
101 struct isl_set
*isl_set_drop_dims(
102 struct isl_set
*set
, unsigned first
, unsigned n
)
109 isl_assert(set
->ctx
, first
+ n
<= set
->dim
->n_out
, goto error
);
111 if (n
== 0 && !isl_space_get_tuple_name(set
->dim
, isl_dim_set
))
113 set
= isl_set_cow(set
);
116 set
->dim
= isl_space_drop_outputs(set
->dim
, first
, n
);
120 for (i
= 0; i
< set
->n
; ++i
) {
121 set
->p
[i
] = isl_basic_set_drop_dims(set
->p
[i
], first
, n
);
126 ISL_F_CLR(set
, ISL_SET_NORMALIZED
);
133 /* Move "n" divs starting at "first" to the end of the list of divs.
135 static struct isl_basic_map
*move_divs_last(struct isl_basic_map
*bmap
,
136 unsigned first
, unsigned n
)
141 if (first
+ n
== bmap
->n_div
)
144 div
= isl_alloc_array(bmap
->ctx
, isl_int
*, n
);
147 for (i
= 0; i
< n
; ++i
)
148 div
[i
] = bmap
->div
[first
+ i
];
149 for (i
= 0; i
< bmap
->n_div
- first
- n
; ++i
)
150 bmap
->div
[first
+ i
] = bmap
->div
[first
+ n
+ i
];
151 for (i
= 0; i
< n
; ++i
)
152 bmap
->div
[bmap
->n_div
- n
+ i
] = div
[i
];
156 isl_basic_map_free(bmap
);
160 /* Drop "n" dimensions of type "type" starting at "first".
162 * In principle, this frees up some extra variables as the number
163 * of columns remains constant, but we would have to extend
164 * the div array too as the number of rows in this array is assumed
165 * to be equal to extra.
167 struct isl_basic_map
*isl_basic_map_drop(struct isl_basic_map
*bmap
,
168 enum isl_dim_type type
, unsigned first
, unsigned n
)
178 dim
= isl_basic_map_dim(bmap
, type
);
179 isl_assert(bmap
->ctx
, first
+ n
<= dim
, goto error
);
181 if (n
== 0 && !isl_space_is_named_or_nested(bmap
->dim
, type
))
184 bmap
= isl_basic_map_cow(bmap
);
188 offset
= isl_basic_map_offset(bmap
, type
) + first
;
189 left
= isl_basic_map_total_dim(bmap
) - (offset
- 1) - n
;
190 for (i
= 0; i
< bmap
->n_eq
; ++i
)
191 constraint_drop_vars(bmap
->eq
[i
]+offset
, n
, left
);
193 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
194 constraint_drop_vars(bmap
->ineq
[i
]+offset
, n
, left
);
196 for (i
= 0; i
< bmap
->n_div
; ++i
)
197 constraint_drop_vars(bmap
->div
[i
]+1+offset
, n
, left
);
199 if (type
== isl_dim_div
) {
200 bmap
= move_divs_last(bmap
, first
, n
);
203 isl_basic_map_free_div(bmap
, n
);
205 bmap
->dim
= isl_space_drop_dims(bmap
->dim
, type
, first
, n
);
209 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
210 bmap
= isl_basic_map_simplify(bmap
);
211 return isl_basic_map_finalize(bmap
);
213 isl_basic_map_free(bmap
);
217 __isl_give isl_basic_set
*isl_basic_set_drop(__isl_take isl_basic_set
*bset
,
218 enum isl_dim_type type
, unsigned first
, unsigned n
)
220 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset
),
224 struct isl_basic_map
*isl_basic_map_drop_inputs(
225 struct isl_basic_map
*bmap
, unsigned first
, unsigned n
)
227 return isl_basic_map_drop(bmap
, isl_dim_in
, first
, n
);
230 struct isl_map
*isl_map_drop(struct isl_map
*map
,
231 enum isl_dim_type type
, unsigned first
, unsigned n
)
238 isl_assert(map
->ctx
, first
+ n
<= isl_map_dim(map
, type
), goto error
);
240 if (n
== 0 && !isl_space_get_tuple_name(map
->dim
, type
))
242 map
= isl_map_cow(map
);
245 map
->dim
= isl_space_drop_dims(map
->dim
, type
, first
, n
);
249 for (i
= 0; i
< map
->n
; ++i
) {
250 map
->p
[i
] = isl_basic_map_drop(map
->p
[i
], type
, first
, n
);
254 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
262 struct isl_set
*isl_set_drop(struct isl_set
*set
,
263 enum isl_dim_type type
, unsigned first
, unsigned n
)
265 return set_from_map(isl_map_drop(set_to_map(set
), type
, first
, n
));
268 struct isl_map
*isl_map_drop_inputs(
269 struct isl_map
*map
, unsigned first
, unsigned n
)
271 return isl_map_drop(map
, isl_dim_in
, first
, n
);
275 * We don't cow, as the div is assumed to be redundant.
277 __isl_give isl_basic_map
*isl_basic_map_drop_div(
278 __isl_take isl_basic_map
*bmap
, unsigned div
)
286 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
288 isl_assert(bmap
->ctx
, div
< bmap
->n_div
, goto error
);
290 for (i
= 0; i
< bmap
->n_eq
; ++i
)
291 constraint_drop_vars(bmap
->eq
[i
]+pos
, 1, bmap
->extra
-div
-1);
293 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
294 if (!isl_int_is_zero(bmap
->ineq
[i
][pos
])) {
295 isl_basic_map_drop_inequality(bmap
, i
);
299 constraint_drop_vars(bmap
->ineq
[i
]+pos
, 1, bmap
->extra
-div
-1);
302 for (i
= 0; i
< bmap
->n_div
; ++i
)
303 constraint_drop_vars(bmap
->div
[i
]+1+pos
, 1, bmap
->extra
-div
-1);
305 if (div
!= bmap
->n_div
- 1) {
307 isl_int
*t
= bmap
->div
[div
];
309 for (j
= div
; j
< bmap
->n_div
- 1; ++j
)
310 bmap
->div
[j
] = bmap
->div
[j
+1];
312 bmap
->div
[bmap
->n_div
- 1] = t
;
314 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
315 isl_basic_map_free_div(bmap
, 1);
319 isl_basic_map_free(bmap
);
323 struct isl_basic_map
*isl_basic_map_normalize_constraints(
324 struct isl_basic_map
*bmap
)
328 unsigned total
= isl_basic_map_total_dim(bmap
);
334 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
) {
335 isl_seq_gcd(bmap
->eq
[i
]+1, total
, &gcd
);
336 if (isl_int_is_zero(gcd
)) {
337 if (!isl_int_is_zero(bmap
->eq
[i
][0])) {
338 bmap
= isl_basic_map_set_to_empty(bmap
);
341 isl_basic_map_drop_equality(bmap
, i
);
344 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
345 isl_int_gcd(gcd
, gcd
, bmap
->eq
[i
][0]);
346 if (isl_int_is_one(gcd
))
348 if (!isl_int_is_divisible_by(bmap
->eq
[i
][0], gcd
)) {
349 bmap
= isl_basic_map_set_to_empty(bmap
);
352 isl_seq_scale_down(bmap
->eq
[i
], bmap
->eq
[i
], gcd
, 1+total
);
355 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
356 isl_seq_gcd(bmap
->ineq
[i
]+1, total
, &gcd
);
357 if (isl_int_is_zero(gcd
)) {
358 if (isl_int_is_neg(bmap
->ineq
[i
][0])) {
359 bmap
= isl_basic_map_set_to_empty(bmap
);
362 isl_basic_map_drop_inequality(bmap
, i
);
365 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
))
366 isl_int_gcd(gcd
, gcd
, bmap
->ineq
[i
][0]);
367 if (isl_int_is_one(gcd
))
369 isl_int_fdiv_q(bmap
->ineq
[i
][0], bmap
->ineq
[i
][0], gcd
);
370 isl_seq_scale_down(bmap
->ineq
[i
]+1, bmap
->ineq
[i
]+1, gcd
, total
);
377 struct isl_basic_set
*isl_basic_set_normalize_constraints(
378 struct isl_basic_set
*bset
)
380 isl_basic_map
*bmap
= bset_to_bmap(bset
);
381 return bset_from_bmap(isl_basic_map_normalize_constraints(bmap
));
384 /* Assuming the variable at position "pos" has an integer coefficient
385 * in integer division "div", extract it from this integer division.
386 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
387 * corresponds to the constant term.
389 * That is, the integer division is of the form
391 * floor((... + c * d * x_pos + ...)/d)
395 * floor((... + 0 * x_pos + ...)/d) + c * x_pos
397 static __isl_give isl_basic_map
*remove_var_from_div(
398 __isl_take isl_basic_map
*bmap
, int div
, int pos
)
403 isl_int_divexact(shift
, bmap
->div
[div
][1 + pos
], bmap
->div
[div
][0]);
404 isl_int_neg(shift
, shift
);
405 bmap
= isl_basic_map_shift_div(bmap
, div
, pos
, shift
);
406 isl_int_clear(shift
);
411 /* Check if integer division "div" has any integral coefficient
412 * (or constant term). If so, extract them from the integer division.
414 static __isl_give isl_basic_map
*remove_independent_vars_from_div(
415 __isl_take isl_basic_map
*bmap
, int div
)
418 unsigned total
= 1 + isl_basic_map_total_dim(bmap
);
420 for (i
= 0; i
< total
; ++i
) {
421 if (isl_int_is_zero(bmap
->div
[div
][1 + i
]))
423 if (!isl_int_is_divisible_by(bmap
->div
[div
][1 + i
],
426 bmap
= remove_var_from_div(bmap
, div
, i
);
434 /* Check if any known integer division has any integral coefficient
435 * (or constant term). If so, extract them from the integer division.
437 static __isl_give isl_basic_map
*remove_independent_vars_from_divs(
438 __isl_take isl_basic_map
*bmap
)
444 if (bmap
->n_div
== 0)
447 for (i
= 0; i
< bmap
->n_div
; ++i
) {
448 if (isl_int_is_zero(bmap
->div
[i
][0]))
450 bmap
= remove_independent_vars_from_div(bmap
, i
);
458 /* Remove any common factor in numerator and denominator of the div expression,
459 * not taking into account the constant term.
460 * That is, if the div is of the form
462 * floor((a + m f(x))/(m d))
466 * floor((floor(a/m) + f(x))/d)
468 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
469 * and can therefore not influence the result of the floor.
471 static void normalize_div_expression(__isl_keep isl_basic_map
*bmap
, int div
)
473 unsigned total
= isl_basic_map_total_dim(bmap
);
474 isl_ctx
*ctx
= bmap
->ctx
;
476 if (isl_int_is_zero(bmap
->div
[div
][0]))
478 isl_seq_gcd(bmap
->div
[div
] + 2, total
, &ctx
->normalize_gcd
);
479 isl_int_gcd(ctx
->normalize_gcd
, ctx
->normalize_gcd
, bmap
->div
[div
][0]);
480 if (isl_int_is_one(ctx
->normalize_gcd
))
482 isl_int_fdiv_q(bmap
->div
[div
][1], bmap
->div
[div
][1],
484 isl_int_divexact(bmap
->div
[div
][0], bmap
->div
[div
][0],
486 isl_seq_scale_down(bmap
->div
[div
] + 2, bmap
->div
[div
] + 2,
487 ctx
->normalize_gcd
, total
);
490 /* Remove any common factor in numerator and denominator of a div expression,
491 * not taking into account the constant term.
492 * That is, look for any div of the form
494 * floor((a + m f(x))/(m d))
498 * floor((floor(a/m) + f(x))/d)
500 * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
501 * and can therefore not influence the result of the floor.
503 static __isl_give isl_basic_map
*normalize_div_expressions(
504 __isl_take isl_basic_map
*bmap
)
510 if (bmap
->n_div
== 0)
513 for (i
= 0; i
< bmap
->n_div
; ++i
)
514 normalize_div_expression(bmap
, i
);
519 /* Assumes divs have been ordered if keep_divs is set.
521 static void eliminate_var_using_equality(struct isl_basic_map
*bmap
,
522 unsigned pos
, isl_int
*eq
, int keep_divs
, int *progress
)
525 unsigned space_total
;
529 total
= isl_basic_map_total_dim(bmap
);
530 space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
531 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
532 for (k
= 0; k
< bmap
->n_eq
; ++k
) {
533 if (bmap
->eq
[k
] == eq
)
535 if (isl_int_is_zero(bmap
->eq
[k
][1+pos
]))
539 isl_seq_elim(bmap
->eq
[k
], eq
, 1+pos
, 1+total
, NULL
);
540 isl_seq_normalize(bmap
->ctx
, bmap
->eq
[k
], 1 + total
);
543 for (k
= 0; k
< bmap
->n_ineq
; ++k
) {
544 if (isl_int_is_zero(bmap
->ineq
[k
][1+pos
]))
548 isl_seq_elim(bmap
->ineq
[k
], eq
, 1+pos
, 1+total
, NULL
);
549 isl_seq_normalize(bmap
->ctx
, bmap
->ineq
[k
], 1 + total
);
550 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
553 for (k
= 0; k
< bmap
->n_div
; ++k
) {
554 if (isl_int_is_zero(bmap
->div
[k
][0]))
556 if (isl_int_is_zero(bmap
->div
[k
][1+1+pos
]))
560 /* We need to be careful about circular definitions,
561 * so for now we just remove the definition of div k
562 * if the equality contains any divs.
563 * If keep_divs is set, then the divs have been ordered
564 * and we can keep the definition as long as the result
567 if (last_div
== -1 || (keep_divs
&& last_div
< k
)) {
568 isl_seq_elim(bmap
->div
[k
]+1, eq
,
569 1+pos
, 1+total
, &bmap
->div
[k
][0]);
570 normalize_div_expression(bmap
, k
);
572 isl_seq_clr(bmap
->div
[k
], 1 + total
);
573 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
577 /* Assumes divs have been ordered if keep_divs is set.
579 static __isl_give isl_basic_map
*eliminate_div(__isl_take isl_basic_map
*bmap
,
580 isl_int
*eq
, unsigned div
, int keep_divs
)
582 unsigned pos
= isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
584 eliminate_var_using_equality(bmap
, pos
, eq
, keep_divs
, NULL
);
586 bmap
= isl_basic_map_drop_div(bmap
, div
);
591 /* Check if elimination of div "div" using equality "eq" would not
592 * result in a div depending on a later div.
594 static int ok_to_eliminate_div(struct isl_basic_map
*bmap
, isl_int
*eq
,
599 unsigned space_total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
600 unsigned pos
= space_total
+ div
;
602 last_div
= isl_seq_last_non_zero(eq
+ 1 + space_total
, bmap
->n_div
);
603 if (last_div
< 0 || last_div
<= div
)
606 for (k
= 0; k
<= last_div
; ++k
) {
607 if (isl_int_is_zero(bmap
->div
[k
][0]))
609 if (!isl_int_is_zero(bmap
->div
[k
][1 + 1 + pos
]))
616 /* Elimininate divs based on equalities
618 static struct isl_basic_map
*eliminate_divs_eq(
619 struct isl_basic_map
*bmap
, int *progress
)
626 bmap
= isl_basic_map_order_divs(bmap
);
631 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
633 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
634 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
635 if (!isl_int_is_one(bmap
->eq
[i
][off
+ d
]) &&
636 !isl_int_is_negone(bmap
->eq
[i
][off
+ d
]))
638 if (!ok_to_eliminate_div(bmap
, bmap
->eq
[i
], d
))
642 bmap
= eliminate_div(bmap
, bmap
->eq
[i
], d
, 1);
643 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
644 return isl_basic_map_free(bmap
);
649 return eliminate_divs_eq(bmap
, progress
);
653 /* Elimininate divs based on inequalities
655 static struct isl_basic_map
*eliminate_divs_ineq(
656 struct isl_basic_map
*bmap
, int *progress
)
667 off
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
669 for (d
= bmap
->n_div
- 1; d
>= 0 ; --d
) {
670 for (i
= 0; i
< bmap
->n_eq
; ++i
)
671 if (!isl_int_is_zero(bmap
->eq
[i
][off
+ d
]))
675 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
676 if (isl_int_abs_gt(bmap
->ineq
[i
][off
+ d
], ctx
->one
))
678 if (i
< bmap
->n_ineq
)
681 bmap
= isl_basic_map_eliminate_vars(bmap
, (off
-1)+d
, 1);
682 if (!bmap
|| ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
684 bmap
= isl_basic_map_drop_div(bmap
, d
);
691 /* The last local variable involved in the equality constraint
692 * at position "eq" in "bmap" is the local variable at position "div".
693 * It can therefore be used to extract an explicit representation
695 * Do so unless the local variable already has an explicit representation.
696 * Set *progress if anything is changed.
698 * The equality constraint is of the form
702 * with n a positive number. The explicit representation derived from
707 static __isl_give isl_basic_map
*set_div_from_eq(__isl_take isl_basic_map
*bmap
,
708 int div
, int eq
, int *progress
)
710 unsigned total
, o_div
;
715 if (!isl_int_is_zero(bmap
->div
[div
][0]))
718 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
719 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
720 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->eq
[eq
], 1 + total
);
721 isl_int_set_si(bmap
->div
[div
][1 + o_div
+ div
], 0);
722 isl_int_set(bmap
->div
[div
][0], bmap
->eq
[eq
][o_div
+ div
]);
725 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
730 struct isl_basic_map
*isl_basic_map_gauss(
731 struct isl_basic_map
*bmap
, int *progress
)
739 bmap
= isl_basic_map_order_divs(bmap
);
744 total
= isl_basic_map_total_dim(bmap
);
745 total_var
= total
- bmap
->n_div
;
747 last_var
= total
- 1;
748 for (done
= 0; done
< bmap
->n_eq
; ++done
) {
749 for (; last_var
>= 0; --last_var
) {
750 for (k
= done
; k
< bmap
->n_eq
; ++k
)
751 if (!isl_int_is_zero(bmap
->eq
[k
][1+last_var
]))
759 swap_equality(bmap
, k
, done
);
760 if (isl_int_is_neg(bmap
->eq
[done
][1+last_var
]))
761 isl_seq_neg(bmap
->eq
[done
], bmap
->eq
[done
], 1+total
);
763 eliminate_var_using_equality(bmap
, last_var
, bmap
->eq
[done
], 1,
766 if (last_var
>= total_var
)
767 bmap
= set_div_from_eq(bmap
, last_var
- total_var
,
772 if (done
== bmap
->n_eq
)
774 for (k
= done
; k
< bmap
->n_eq
; ++k
) {
775 if (isl_int_is_zero(bmap
->eq
[k
][0]))
777 return isl_basic_map_set_to_empty(bmap
);
779 isl_basic_map_free_equality(bmap
, bmap
->n_eq
-done
);
783 struct isl_basic_set
*isl_basic_set_gauss(
784 struct isl_basic_set
*bset
, int *progress
)
786 return bset_from_bmap(isl_basic_map_gauss(bset_to_bmap(bset
),
791 static unsigned int round_up(unsigned int v
)
802 /* Hash table of inequalities in a basic map.
803 * "index" is an array of addresses of inequalities in the basic map, some
804 * of which are NULL. The inequalities are hashed on the coefficients
805 * except the constant term.
806 * "size" is the number of elements in the array and is always a power of two
807 * "bits" is the number of bits need to represent an index into the array.
808 * "total" is the total dimension of the basic map.
810 struct isl_constraint_index
{
817 /* Fill in the "ci" data structure for holding the inequalities of "bmap".
819 static isl_stat
create_constraint_index(struct isl_constraint_index
*ci
,
820 __isl_keep isl_basic_map
*bmap
)
826 return isl_stat_error
;
827 ci
->total
= isl_basic_set_total_dim(bmap
);
828 if (bmap
->n_ineq
== 0)
830 ci
->size
= round_up(4 * (bmap
->n_ineq
+ 1) / 3 - 1);
831 ci
->bits
= ffs(ci
->size
) - 1;
832 ctx
= isl_basic_map_get_ctx(bmap
);
833 ci
->index
= isl_calloc_array(ctx
, isl_int
**, ci
->size
);
835 return isl_stat_error
;
840 /* Free the memory allocated by create_constraint_index.
842 static void constraint_index_free(struct isl_constraint_index
*ci
)
847 /* Return the position in ci->index that contains the address of
848 * an inequality that is equal to *ineq up to the constant term,
849 * provided this address is not identical to "ineq".
850 * If there is no such inequality, then return the position where
851 * such an inequality should be inserted.
853 static int hash_index_ineq(struct isl_constraint_index
*ci
, isl_int
**ineq
)
856 uint32_t hash
= isl_seq_get_hash_bits((*ineq
) + 1, ci
->total
, ci
->bits
);
857 for (h
= hash
; ci
->index
[h
]; h
= (h
+1) % ci
->size
)
858 if (ineq
!= ci
->index
[h
] &&
859 isl_seq_eq((*ineq
) + 1, ci
->index
[h
][0]+1, ci
->total
))
864 /* Return the position in ci->index that contains the address of
865 * an inequality that is equal to the k'th inequality of "bmap"
866 * up to the constant term, provided it does not point to the very
868 * If there is no such inequality, then return the position where
869 * such an inequality should be inserted.
871 static int hash_index(struct isl_constraint_index
*ci
,
872 __isl_keep isl_basic_map
*bmap
, int k
)
874 return hash_index_ineq(ci
, &bmap
->ineq
[k
]);
877 static int set_hash_index(struct isl_constraint_index
*ci
,
878 struct isl_basic_set
*bset
, int k
)
880 return hash_index(ci
, bset
, k
);
883 /* Fill in the "ci" data structure with the inequalities of "bset".
885 static isl_stat
setup_constraint_index(struct isl_constraint_index
*ci
,
886 __isl_keep isl_basic_set
*bset
)
890 if (create_constraint_index(ci
, bset
) < 0)
891 return isl_stat_error
;
893 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
894 h
= set_hash_index(ci
, bset
, k
);
895 ci
->index
[h
] = &bset
->ineq
[k
];
901 /* Is the inequality ineq (obviously) redundant with respect
902 * to the constraints in "ci"?
904 * Look for an inequality in "ci" with the same coefficients and then
905 * check if the contant term of "ineq" is greater than or equal
906 * to the constant term of that inequality. If so, "ineq" is clearly
909 * Note that hash_index_ineq ignores a stored constraint if it has
910 * the same address as the passed inequality. It is ok to pass
911 * the address of a local variable here since it will never be
912 * the same as the address of a constraint in "ci".
914 static isl_bool
constraint_index_is_redundant(struct isl_constraint_index
*ci
,
919 h
= hash_index_ineq(ci
, &ineq
);
921 return isl_bool_false
;
922 return isl_int_ge(ineq
[0], (*ci
->index
[h
])[0]);
925 /* If we can eliminate more than one div, then we need to make
926 * sure we do it from last div to first div, in order not to
927 * change the position of the other divs that still need to
930 static struct isl_basic_map
*remove_duplicate_divs(
931 struct isl_basic_map
*bmap
, int *progress
)
943 bmap
= isl_basic_map_order_divs(bmap
);
944 if (!bmap
|| bmap
->n_div
<= 1)
947 total_var
= isl_space_dim(bmap
->dim
, isl_dim_all
);
948 total
= total_var
+ bmap
->n_div
;
951 for (k
= bmap
->n_div
- 1; k
>= 0; --k
)
952 if (!isl_int_is_zero(bmap
->div
[k
][0]))
957 size
= round_up(4 * bmap
->n_div
/ 3 - 1);
960 elim_for
= isl_calloc_array(ctx
, int, bmap
->n_div
);
961 bits
= ffs(size
) - 1;
962 index
= isl_calloc_array(ctx
, int, size
);
963 if (!elim_for
|| !index
)
965 eq
= isl_blk_alloc(ctx
, 1+total
);
966 if (isl_blk_is_error(eq
))
969 isl_seq_clr(eq
.data
, 1+total
);
970 index
[isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
)] = k
+ 1;
971 for (--k
; k
>= 0; --k
) {
974 if (isl_int_is_zero(bmap
->div
[k
][0]))
977 hash
= isl_seq_get_hash_bits(bmap
->div
[k
], 2+total
, bits
);
978 for (h
= hash
; index
[h
]; h
= (h
+1) % size
)
979 if (isl_seq_eq(bmap
->div
[k
],
980 bmap
->div
[index
[h
]-1], 2+total
))
989 for (l
= bmap
->n_div
- 1; l
>= 0; --l
) {
993 isl_int_set_si(eq
.data
[1+total_var
+k
], -1);
994 isl_int_set_si(eq
.data
[1+total_var
+l
], 1);
995 bmap
= eliminate_div(bmap
, eq
.data
, l
, 1);
998 isl_int_set_si(eq
.data
[1+total_var
+k
], 0);
999 isl_int_set_si(eq
.data
[1+total_var
+l
], 0);
1002 isl_blk_free(ctx
, eq
);
1009 static int n_pure_div_eq(struct isl_basic_map
*bmap
)
1014 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1015 for (i
= 0, j
= bmap
->n_div
-1; i
< bmap
->n_eq
; ++i
) {
1016 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1020 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, j
) != -1)
1026 /* Normalize divs that appear in equalities.
1028 * In particular, we assume that bmap contains some equalities
1033 * and we want to replace the set of e_i by a minimal set and
1034 * such that the new e_i have a canonical representation in terms
1036 * If any of the equalities involves more than one divs, then
1037 * we currently simply bail out.
1039 * Let us first additionally assume that all equalities involve
1040 * a div. The equalities then express modulo constraints on the
1041 * remaining variables and we can use "parameter compression"
1042 * to find a minimal set of constraints. The result is a transformation
1044 * x = T(x') = x_0 + G x'
1046 * with G a lower-triangular matrix with all elements below the diagonal
1047 * non-negative and smaller than the diagonal element on the same row.
1048 * We first normalize x_0 by making the same property hold in the affine
1050 * The rows i of G with a 1 on the diagonal do not impose any modulo
1051 * constraint and simply express x_i = x'_i.
1052 * For each of the remaining rows i, we introduce a div and a corresponding
1053 * equality. In particular
1055 * g_ii e_j = x_i - g_i(x')
1057 * where each x'_k is replaced either by x_k (if g_kk = 1) or the
1058 * corresponding div (if g_kk != 1).
1060 * If there are any equalities not involving any div, then we
1061 * first apply a variable compression on the variables x:
1063 * x = C x'' x'' = C_2 x
1065 * and perform the above parameter compression on A C instead of on A.
1066 * The resulting compression is then of the form
1068 * x'' = T(x') = x_0 + G x'
1070 * and in constructing the new divs and the corresponding equalities,
1071 * we have to replace each x'', i.e., the x'_k with (g_kk = 1),
1072 * by the corresponding row from C_2.
1074 static struct isl_basic_map
*normalize_divs(
1075 struct isl_basic_map
*bmap
, int *progress
)
1082 struct isl_mat
*T
= NULL
;
1083 struct isl_mat
*C
= NULL
;
1084 struct isl_mat
*C2
= NULL
;
1087 int dropped
, needed
;
1092 if (bmap
->n_div
== 0)
1095 if (bmap
->n_eq
== 0)
1098 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
))
1101 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1102 div_eq
= n_pure_div_eq(bmap
);
1106 if (div_eq
< bmap
->n_eq
) {
1107 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, div_eq
,
1108 bmap
->n_eq
- div_eq
, 0, 1 + total
);
1109 C
= isl_mat_variable_compression(B
, &C2
);
1112 if (C
->n_col
== 0) {
1113 bmap
= isl_basic_map_set_to_empty(bmap
);
1120 d
= isl_vec_alloc(bmap
->ctx
, div_eq
);
1123 for (i
= 0, j
= bmap
->n_div
-1; i
< div_eq
; ++i
) {
1124 while (j
>= 0 && isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1126 isl_int_set(d
->block
.data
[i
], bmap
->eq
[i
][1 + total
+ j
]);
1128 B
= isl_mat_sub_alloc6(bmap
->ctx
, bmap
->eq
, 0, div_eq
, 0, 1 + total
);
1131 B
= isl_mat_product(B
, C
);
1135 T
= isl_mat_parameter_compression(B
, d
);
1138 if (T
->n_col
== 0) {
1139 bmap
= isl_basic_map_set_to_empty(bmap
);
1145 for (i
= 0; i
< T
->n_row
- 1; ++i
) {
1146 isl_int_fdiv_q(v
, T
->row
[1 + i
][0], T
->row
[1 + i
][1 + i
]);
1147 if (isl_int_is_zero(v
))
1149 isl_mat_col_submul(T
, 0, v
, 1 + i
);
1152 pos
= isl_alloc_array(bmap
->ctx
, int, T
->n_row
);
1155 /* We have to be careful because dropping equalities may reorder them */
1157 for (j
= bmap
->n_div
- 1; j
>= 0; --j
) {
1158 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1159 if (!isl_int_is_zero(bmap
->eq
[i
][1 + total
+ j
]))
1161 if (i
< bmap
->n_eq
) {
1162 bmap
= isl_basic_map_drop_div(bmap
, j
);
1163 isl_basic_map_drop_equality(bmap
, i
);
1169 for (i
= 1; i
< T
->n_row
; ++i
) {
1170 if (isl_int_is_one(T
->row
[i
][i
]))
1175 if (needed
> dropped
) {
1176 bmap
= isl_basic_map_extend_space(bmap
, isl_space_copy(bmap
->dim
),
1181 for (i
= 1; i
< T
->n_row
; ++i
) {
1182 if (isl_int_is_one(T
->row
[i
][i
]))
1184 k
= isl_basic_map_alloc_div(bmap
);
1185 pos
[i
] = 1 + total
+ k
;
1186 isl_seq_clr(bmap
->div
[k
] + 1, 1 + total
+ bmap
->n_div
);
1187 isl_int_set(bmap
->div
[k
][0], T
->row
[i
][i
]);
1189 isl_seq_cpy(bmap
->div
[k
] + 1, C2
->row
[i
], 1 + total
);
1191 isl_int_set_si(bmap
->div
[k
][1 + i
], 1);
1192 for (j
= 0; j
< i
; ++j
) {
1193 if (isl_int_is_zero(T
->row
[i
][j
]))
1195 if (pos
[j
] < T
->n_row
&& C2
)
1196 isl_seq_submul(bmap
->div
[k
] + 1, T
->row
[i
][j
],
1197 C2
->row
[pos
[j
]], 1 + total
);
1199 isl_int_neg(bmap
->div
[k
][1 + pos
[j
]],
1202 j
= isl_basic_map_alloc_equality(bmap
);
1203 isl_seq_neg(bmap
->eq
[j
], bmap
->div
[k
]+1, 1+total
+bmap
->n_div
);
1204 isl_int_set(bmap
->eq
[j
][pos
[i
]], bmap
->div
[k
][0]);
1213 ISL_F_SET(bmap
, ISL_BASIC_MAP_NORMALIZED_DIVS
);
1223 static struct isl_basic_map
*set_div_from_lower_bound(
1224 struct isl_basic_map
*bmap
, int div
, int ineq
)
1226 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1228 isl_seq_neg(bmap
->div
[div
] + 1, bmap
->ineq
[ineq
], total
+ bmap
->n_div
);
1229 isl_int_set(bmap
->div
[div
][0], bmap
->ineq
[ineq
][total
+ div
]);
1230 isl_int_add(bmap
->div
[div
][1], bmap
->div
[div
][1], bmap
->div
[div
][0]);
1231 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1232 isl_int_set_si(bmap
->div
[div
][1 + total
+ div
], 0);
1237 /* Check whether it is ok to define a div based on an inequality.
1238 * To avoid the introduction of circular definitions of divs, we
1239 * do not allow such a definition if the resulting expression would refer to
1240 * any other undefined divs or if any known div is defined in
1241 * terms of the unknown div.
1243 static int ok_to_set_div_from_bound(struct isl_basic_map
*bmap
,
1247 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1249 /* Not defined in terms of unknown divs */
1250 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1253 if (isl_int_is_zero(bmap
->ineq
[ineq
][total
+ j
]))
1255 if (isl_int_is_zero(bmap
->div
[j
][0]))
1259 /* No other div defined in terms of this one => avoid loops */
1260 for (j
= 0; j
< bmap
->n_div
; ++j
) {
1263 if (isl_int_is_zero(bmap
->div
[j
][0]))
1265 if (!isl_int_is_zero(bmap
->div
[j
][1 + total
+ div
]))
1272 /* Would an expression for div "div" based on inequality "ineq" of "bmap"
1273 * be a better expression than the current one?
1275 * If we do not have any expression yet, then any expression would be better.
1276 * Otherwise we check if the last variable involved in the inequality
1277 * (disregarding the div that it would define) is in an earlier position
1278 * than the last variable involved in the current div expression.
1280 static int better_div_constraint(__isl_keep isl_basic_map
*bmap
,
1283 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1287 if (isl_int_is_zero(bmap
->div
[div
][0]))
1290 if (isl_seq_last_non_zero(bmap
->ineq
[ineq
] + total
+ div
+ 1,
1291 bmap
->n_div
- (div
+ 1)) >= 0)
1294 last_ineq
= isl_seq_last_non_zero(bmap
->ineq
[ineq
], total
+ div
);
1295 last_div
= isl_seq_last_non_zero(bmap
->div
[div
] + 1,
1296 total
+ bmap
->n_div
);
1298 return last_ineq
< last_div
;
1301 /* Given two constraints "k" and "l" that are opposite to each other,
1302 * except for the constant term, check if we can use them
1303 * to obtain an expression for one of the hitherto unknown divs or
1304 * a "better" expression for a div for which we already have an expression.
1305 * "sum" is the sum of the constant terms of the constraints.
1306 * If this sum is strictly smaller than the coefficient of one
1307 * of the divs, then this pair can be used define the div.
1308 * To avoid the introduction of circular definitions of divs, we
1309 * do not use the pair if the resulting expression would refer to
1310 * any other undefined divs or if any known div is defined in
1311 * terms of the unknown div.
1313 static struct isl_basic_map
*check_for_div_constraints(
1314 struct isl_basic_map
*bmap
, int k
, int l
, isl_int sum
, int *progress
)
1317 unsigned total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1319 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1320 if (isl_int_is_zero(bmap
->ineq
[k
][total
+ i
]))
1322 if (isl_int_abs_ge(sum
, bmap
->ineq
[k
][total
+ i
]))
1324 if (!better_div_constraint(bmap
, i
, k
))
1326 if (!ok_to_set_div_from_bound(bmap
, i
, k
))
1328 if (isl_int_is_pos(bmap
->ineq
[k
][total
+ i
]))
1329 bmap
= set_div_from_lower_bound(bmap
, i
, k
);
1331 bmap
= set_div_from_lower_bound(bmap
, i
, l
);
1339 __isl_give isl_basic_map
*isl_basic_map_remove_duplicate_constraints(
1340 __isl_take isl_basic_map
*bmap
, int *progress
, int detect_divs
)
1342 struct isl_constraint_index ci
;
1344 unsigned total
= isl_basic_map_total_dim(bmap
);
1347 if (!bmap
|| bmap
->n_ineq
<= 1)
1350 if (create_constraint_index(&ci
, bmap
) < 0)
1353 h
= isl_seq_get_hash_bits(bmap
->ineq
[0] + 1, total
, ci
.bits
);
1354 ci
.index
[h
] = &bmap
->ineq
[0];
1355 for (k
= 1; k
< bmap
->n_ineq
; ++k
) {
1356 h
= hash_index(&ci
, bmap
, k
);
1358 ci
.index
[h
] = &bmap
->ineq
[k
];
1363 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1364 if (isl_int_lt(bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]))
1365 swap_inequality(bmap
, k
, l
);
1366 isl_basic_map_drop_inequality(bmap
, k
);
1370 for (k
= 0; k
< bmap
->n_ineq
-1; ++k
) {
1371 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1372 h
= hash_index(&ci
, bmap
, k
);
1373 isl_seq_neg(bmap
->ineq
[k
]+1, bmap
->ineq
[k
]+1, total
);
1376 l
= ci
.index
[h
] - &bmap
->ineq
[0];
1377 isl_int_add(sum
, bmap
->ineq
[k
][0], bmap
->ineq
[l
][0]);
1378 if (isl_int_is_pos(sum
)) {
1380 bmap
= check_for_div_constraints(bmap
, k
, l
,
1384 if (isl_int_is_zero(sum
)) {
1385 /* We need to break out of the loop after these
1386 * changes since the contents of the hash
1387 * will no longer be valid.
1388 * Plus, we probably we want to regauss first.
1392 isl_basic_map_drop_inequality(bmap
, l
);
1393 isl_basic_map_inequality_to_equality(bmap
, k
);
1395 bmap
= isl_basic_map_set_to_empty(bmap
);
1400 constraint_index_free(&ci
);
1404 /* Detect all pairs of inequalities that form an equality.
1406 * isl_basic_map_remove_duplicate_constraints detects at most one such pair.
1407 * Call it repeatedly while it is making progress.
1409 __isl_give isl_basic_map
*isl_basic_map_detect_inequality_pairs(
1410 __isl_take isl_basic_map
*bmap
, int *progress
)
1416 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1418 if (progress
&& duplicate
)
1420 } while (duplicate
);
1425 /* Eliminate knowns divs from constraints where they appear with
1426 * a (positive or negative) unit coefficient.
1430 * floor(e/m) + f >= 0
1438 * -floor(e/m) + f >= 0
1442 * -e + m f + m - 1 >= 0
1444 * The first conversion is valid because floor(e/m) >= -f is equivalent
1445 * to e/m >= -f because -f is an integral expression.
1446 * The second conversion follows from the fact that
1448 * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
1451 * Note that one of the div constraints may have been eliminated
1452 * due to being redundant with respect to the constraint that is
1453 * being modified by this function. The modified constraint may
1454 * no longer imply this div constraint, so we add it back to make
1455 * sure we do not lose any information.
1457 * We skip integral divs, i.e., those with denominator 1, as we would
1458 * risk eliminating the div from the div constraints. We do not need
1459 * to handle those divs here anyway since the div constraints will turn
1460 * out to form an equality and this equality can then be used to eliminate
1461 * the div from all constraints.
1463 static __isl_give isl_basic_map
*eliminate_unit_divs(
1464 __isl_take isl_basic_map
*bmap
, int *progress
)
1473 ctx
= isl_basic_map_get_ctx(bmap
);
1474 total
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
);
1476 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1477 if (isl_int_is_zero(bmap
->div
[i
][0]))
1479 if (isl_int_is_one(bmap
->div
[i
][0]))
1481 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
1484 if (!isl_int_is_one(bmap
->ineq
[j
][total
+ i
]) &&
1485 !isl_int_is_negone(bmap
->ineq
[j
][total
+ i
]))
1490 s
= isl_int_sgn(bmap
->ineq
[j
][total
+ i
]);
1491 isl_int_set_si(bmap
->ineq
[j
][total
+ i
], 0);
1493 isl_seq_combine(bmap
->ineq
[j
],
1494 ctx
->negone
, bmap
->div
[i
] + 1,
1495 bmap
->div
[i
][0], bmap
->ineq
[j
],
1496 total
+ bmap
->n_div
);
1498 isl_seq_combine(bmap
->ineq
[j
],
1499 ctx
->one
, bmap
->div
[i
] + 1,
1500 bmap
->div
[i
][0], bmap
->ineq
[j
],
1501 total
+ bmap
->n_div
);
1503 isl_int_add(bmap
->ineq
[j
][0],
1504 bmap
->ineq
[j
][0], bmap
->div
[i
][0]);
1505 isl_int_sub_ui(bmap
->ineq
[j
][0],
1506 bmap
->ineq
[j
][0], 1);
1509 bmap
= isl_basic_map_extend_constraints(bmap
, 0, 1);
1510 if (isl_basic_map_add_div_constraint(bmap
, i
, s
) < 0)
1511 return isl_basic_map_free(bmap
);
1518 struct isl_basic_map
*isl_basic_map_simplify(struct isl_basic_map
*bmap
)
1527 if (isl_basic_map_plain_is_empty(bmap
))
1529 bmap
= isl_basic_map_normalize_constraints(bmap
);
1530 bmap
= remove_independent_vars_from_divs(bmap
);
1531 bmap
= normalize_div_expressions(bmap
);
1532 bmap
= remove_duplicate_divs(bmap
, &progress
);
1533 bmap
= eliminate_unit_divs(bmap
, &progress
);
1534 bmap
= eliminate_divs_eq(bmap
, &progress
);
1535 bmap
= eliminate_divs_ineq(bmap
, &progress
);
1536 bmap
= isl_basic_map_gauss(bmap
, &progress
);
1537 /* requires equalities in normal form */
1538 bmap
= normalize_divs(bmap
, &progress
);
1539 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1541 if (bmap
&& progress
)
1542 ISL_F_CLR(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
1547 struct isl_basic_set
*isl_basic_set_simplify(struct isl_basic_set
*bset
)
1549 return bset_from_bmap(isl_basic_map_simplify(bset_to_bmap(bset
)));
1553 int isl_basic_map_is_div_constraint(__isl_keep isl_basic_map
*bmap
,
1554 isl_int
*constraint
, unsigned div
)
1561 pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1563 if (isl_int_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1565 isl_int_sub(bmap
->div
[div
][1],
1566 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1567 isl_int_add_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1568 neg
= isl_seq_is_neg(constraint
, bmap
->div
[div
]+1, pos
);
1569 isl_int_sub_ui(bmap
->div
[div
][1], bmap
->div
[div
][1], 1);
1570 isl_int_add(bmap
->div
[div
][1],
1571 bmap
->div
[div
][1], bmap
->div
[div
][0]);
1574 if (isl_seq_first_non_zero(constraint
+pos
+1,
1575 bmap
->n_div
-div
-1) != -1)
1577 } else if (isl_int_abs_eq(constraint
[pos
], bmap
->div
[div
][0])) {
1578 if (!isl_seq_eq(constraint
, bmap
->div
[div
]+1, pos
))
1580 if (isl_seq_first_non_zero(constraint
+pos
+1,
1581 bmap
->n_div
-div
-1) != -1)
1589 int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set
*bset
,
1590 isl_int
*constraint
, unsigned div
)
1592 return isl_basic_map_is_div_constraint(bset
, constraint
, div
);
1596 /* If the only constraints a div d=floor(f/m)
1597 * appears in are its two defining constraints
1600 * -(f - (m - 1)) + m d >= 0
1602 * then it can safely be removed.
1604 static int div_is_redundant(struct isl_basic_map
*bmap
, int div
)
1607 unsigned pos
= 1 + isl_space_dim(bmap
->dim
, isl_dim_all
) + div
;
1609 for (i
= 0; i
< bmap
->n_eq
; ++i
)
1610 if (!isl_int_is_zero(bmap
->eq
[i
][pos
]))
1613 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1614 if (isl_int_is_zero(bmap
->ineq
[i
][pos
]))
1616 if (!isl_basic_map_is_div_constraint(bmap
, bmap
->ineq
[i
], div
))
1620 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1621 if (isl_int_is_zero(bmap
->div
[i
][0]))
1623 if (!isl_int_is_zero(bmap
->div
[i
][1+pos
]))
1631 * Remove divs that don't occur in any of the constraints or other divs.
1632 * These can arise when dropping constraints from a basic map or
1633 * when the divs of a basic map have been temporarily aligned
1634 * with the divs of another basic map.
1636 static struct isl_basic_map
*remove_redundant_divs(struct isl_basic_map
*bmap
)
1643 for (i
= bmap
->n_div
-1; i
>= 0; --i
) {
1644 if (!div_is_redundant(bmap
, i
))
1646 bmap
= isl_basic_map_drop_div(bmap
, i
);
1651 /* Mark "bmap" as final, without checking for obviously redundant
1652 * integer divisions. This function should be used when "bmap"
1653 * is known not to involve any such integer divisions.
1655 __isl_give isl_basic_map
*isl_basic_map_mark_final(
1656 __isl_take isl_basic_map
*bmap
)
1660 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
1664 /* Mark "bmap" as final, after removing obviously redundant integer divisions.
1666 struct isl_basic_map
*isl_basic_map_finalize(struct isl_basic_map
*bmap
)
1668 bmap
= remove_redundant_divs(bmap
);
1669 bmap
= isl_basic_map_mark_final(bmap
);
1673 struct isl_basic_set
*isl_basic_set_finalize(struct isl_basic_set
*bset
)
1675 return bset_from_bmap(isl_basic_map_finalize(bset_to_bmap(bset
)));
1678 struct isl_set
*isl_set_finalize(struct isl_set
*set
)
1684 for (i
= 0; i
< set
->n
; ++i
) {
1685 set
->p
[i
] = isl_basic_set_finalize(set
->p
[i
]);
1695 struct isl_map
*isl_map_finalize(struct isl_map
*map
)
1701 for (i
= 0; i
< map
->n
; ++i
) {
1702 map
->p
[i
] = isl_basic_map_finalize(map
->p
[i
]);
1706 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
1714 /* Remove definition of any div that is defined in terms of the given variable.
1715 * The div itself is not removed. Functions such as
1716 * eliminate_divs_ineq depend on the other divs remaining in place.
1718 static struct isl_basic_map
*remove_dependent_vars(struct isl_basic_map
*bmap
,
1726 for (i
= 0; i
< bmap
->n_div
; ++i
) {
1727 if (isl_int_is_zero(bmap
->div
[i
][0]))
1729 if (isl_int_is_zero(bmap
->div
[i
][1+1+pos
]))
1731 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
1738 /* Eliminate the specified variables from the constraints using
1739 * Fourier-Motzkin. The variables themselves are not removed.
1741 struct isl_basic_map
*isl_basic_map_eliminate_vars(
1742 struct isl_basic_map
*bmap
, unsigned pos
, unsigned n
)
1753 total
= isl_basic_map_total_dim(bmap
);
1755 bmap
= isl_basic_map_cow(bmap
);
1756 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
)
1757 bmap
= remove_dependent_vars(bmap
, d
);
1761 for (d
= pos
+ n
- 1;
1762 d
>= 0 && d
>= total
- bmap
->n_div
&& d
>= pos
; --d
)
1763 isl_seq_clr(bmap
->div
[d
-(total
-bmap
->n_div
)], 2+total
);
1764 for (d
= pos
+ n
- 1; d
>= 0 && d
>= pos
; --d
) {
1765 int n_lower
, n_upper
;
1768 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1769 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1771 eliminate_var_using_equality(bmap
, d
, bmap
->eq
[i
], 0, NULL
);
1772 isl_basic_map_drop_equality(bmap
, i
);
1780 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
1781 if (isl_int_is_pos(bmap
->ineq
[i
][1+d
]))
1783 else if (isl_int_is_neg(bmap
->ineq
[i
][1+d
]))
1786 bmap
= isl_basic_map_extend_constraints(bmap
,
1787 0, n_lower
* n_upper
);
1790 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
) {
1792 if (isl_int_is_zero(bmap
->ineq
[i
][1+d
]))
1795 for (j
= 0; j
< i
; ++j
) {
1796 if (isl_int_is_zero(bmap
->ineq
[j
][1+d
]))
1799 if (isl_int_sgn(bmap
->ineq
[i
][1+d
]) ==
1800 isl_int_sgn(bmap
->ineq
[j
][1+d
]))
1802 k
= isl_basic_map_alloc_inequality(bmap
);
1805 isl_seq_cpy(bmap
->ineq
[k
], bmap
->ineq
[i
],
1807 isl_seq_elim(bmap
->ineq
[k
], bmap
->ineq
[j
],
1808 1+d
, 1+total
, NULL
);
1810 isl_basic_map_drop_inequality(bmap
, i
);
1813 if (n_lower
> 0 && n_upper
> 0) {
1814 bmap
= isl_basic_map_normalize_constraints(bmap
);
1815 bmap
= isl_basic_map_remove_duplicate_constraints(bmap
,
1817 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1818 bmap
= isl_basic_map_remove_redundancies(bmap
);
1822 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
1826 ISL_F_CLR(bmap
, ISL_BASIC_MAP_NORMALIZED
);
1828 bmap
= isl_basic_map_gauss(bmap
, NULL
);
1831 isl_basic_map_free(bmap
);
1835 struct isl_basic_set
*isl_basic_set_eliminate_vars(
1836 struct isl_basic_set
*bset
, unsigned pos
, unsigned n
)
1838 return bset_from_bmap(isl_basic_map_eliminate_vars(bset_to_bmap(bset
),
1842 /* Eliminate the specified n dimensions starting at first from the
1843 * constraints, without removing the dimensions from the space.
1844 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1845 * Otherwise, they are projected out and the original space is restored.
1847 __isl_give isl_basic_map
*isl_basic_map_eliminate(
1848 __isl_take isl_basic_map
*bmap
,
1849 enum isl_dim_type type
, unsigned first
, unsigned n
)
1858 if (first
+ n
> isl_basic_map_dim(bmap
, type
) || first
+ n
< first
)
1859 isl_die(bmap
->ctx
, isl_error_invalid
,
1860 "index out of bounds", goto error
);
1862 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_RATIONAL
)) {
1863 first
+= isl_basic_map_offset(bmap
, type
) - 1;
1864 bmap
= isl_basic_map_eliminate_vars(bmap
, first
, n
);
1865 return isl_basic_map_finalize(bmap
);
1868 space
= isl_basic_map_get_space(bmap
);
1869 bmap
= isl_basic_map_project_out(bmap
, type
, first
, n
);
1870 bmap
= isl_basic_map_insert_dims(bmap
, type
, first
, n
);
1871 bmap
= isl_basic_map_reset_space(bmap
, space
);
1874 isl_basic_map_free(bmap
);
1878 __isl_give isl_basic_set
*isl_basic_set_eliminate(
1879 __isl_take isl_basic_set
*bset
,
1880 enum isl_dim_type type
, unsigned first
, unsigned n
)
1882 return isl_basic_map_eliminate(bset
, type
, first
, n
);
1885 /* Remove all constraints from "bmap" that reference any unknown local
1886 * variables (directly or indirectly).
1888 * Dropping all constraints on a local variable will make it redundant,
1889 * so it will get removed implicitly by
1890 * isl_basic_map_drop_constraints_involving_dims. Some other local
1891 * variables may also end up becoming redundant if they only appear
1892 * in constraints together with the unknown local variable.
1893 * Therefore, start over after calling
1894 * isl_basic_map_drop_constraints_involving_dims.
1896 __isl_give isl_basic_map
*isl_basic_map_drop_constraint_involving_unknown_divs(
1897 __isl_take isl_basic_map
*bmap
)
1900 int i
, n_div
, o_div
;
1902 known
= isl_basic_map_divs_known(bmap
);
1904 return isl_basic_map_free(bmap
);
1908 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1909 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
) - 1;
1911 for (i
= 0; i
< n_div
; ++i
) {
1912 known
= isl_basic_map_div_is_known(bmap
, i
);
1914 return isl_basic_map_free(bmap
);
1917 bmap
= remove_dependent_vars(bmap
, o_div
+ i
);
1918 bmap
= isl_basic_map_drop_constraints_involving_dims(bmap
,
1922 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
1929 /* Remove all constraints from "map" that reference any unknown local
1930 * variables (directly or indirectly).
1932 * Since constraints may get dropped from the basic maps,
1933 * they may no longer be disjoint from each other.
1935 __isl_give isl_map
*isl_map_drop_constraint_involving_unknown_divs(
1936 __isl_take isl_map
*map
)
1941 known
= isl_map_divs_known(map
);
1943 return isl_map_free(map
);
1947 map
= isl_map_cow(map
);
1951 for (i
= 0; i
< map
->n
; ++i
) {
1953 isl_basic_map_drop_constraint_involving_unknown_divs(
1956 return isl_map_free(map
);
1960 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
1965 /* Don't assume equalities are in order, because align_divs
1966 * may have changed the order of the divs.
1968 static void compute_elimination_index(struct isl_basic_map
*bmap
, int *elim
)
1973 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1974 for (d
= 0; d
< total
; ++d
)
1976 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
1977 for (d
= total
- 1; d
>= 0; --d
) {
1978 if (isl_int_is_zero(bmap
->eq
[i
][1+d
]))
1986 static void set_compute_elimination_index(struct isl_basic_set
*bset
, int *elim
)
1988 compute_elimination_index(bset_to_bmap(bset
), elim
);
1991 static int reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
1992 struct isl_basic_map
*bmap
, int *elim
)
1998 total
= isl_space_dim(bmap
->dim
, isl_dim_all
);
1999 for (d
= total
- 1; d
>= 0; --d
) {
2000 if (isl_int_is_zero(src
[1+d
]))
2005 isl_seq_cpy(dst
, src
, 1 + total
);
2008 isl_seq_elim(dst
, bmap
->eq
[elim
[d
]], 1 + d
, 1 + total
, NULL
);
2013 static int set_reduced_using_equalities(isl_int
*dst
, isl_int
*src
,
2014 struct isl_basic_set
*bset
, int *elim
)
2016 return reduced_using_equalities(dst
, src
,
2017 bset_to_bmap(bset
), elim
);
2020 static struct isl_basic_set
*isl_basic_set_reduce_using_equalities(
2021 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2026 if (!bset
|| !context
)
2029 if (context
->n_eq
== 0) {
2030 isl_basic_set_free(context
);
2034 bset
= isl_basic_set_cow(bset
);
2038 elim
= isl_alloc_array(bset
->ctx
, int, isl_basic_set_n_dim(bset
));
2041 set_compute_elimination_index(context
, elim
);
2042 for (i
= 0; i
< bset
->n_eq
; ++i
)
2043 set_reduced_using_equalities(bset
->eq
[i
], bset
->eq
[i
],
2045 for (i
= 0; i
< bset
->n_ineq
; ++i
)
2046 set_reduced_using_equalities(bset
->ineq
[i
], bset
->ineq
[i
],
2048 isl_basic_set_free(context
);
2050 bset
= isl_basic_set_simplify(bset
);
2051 bset
= isl_basic_set_finalize(bset
);
2054 isl_basic_set_free(bset
);
2055 isl_basic_set_free(context
);
2059 /* For each inequality in "ineq" that is a shifted (more relaxed)
2060 * copy of an inequality in "context", mark the corresponding entry
2062 * If an inequality only has a non-negative constant term, then
2065 static isl_stat
mark_shifted_constraints(__isl_keep isl_mat
*ineq
,
2066 __isl_keep isl_basic_set
*context
, int *row
)
2068 struct isl_constraint_index ci
;
2073 if (!ineq
|| !context
)
2074 return isl_stat_error
;
2075 if (context
->n_ineq
== 0)
2077 if (setup_constraint_index(&ci
, context
) < 0)
2078 return isl_stat_error
;
2080 n_ineq
= isl_mat_rows(ineq
);
2081 total
= isl_mat_cols(ineq
) - 1;
2082 for (k
= 0; k
< n_ineq
; ++k
) {
2086 l
= isl_seq_first_non_zero(ineq
->row
[k
] + 1, total
);
2087 if (l
< 0 && isl_int_is_nonneg(ineq
->row
[k
][0])) {
2091 redundant
= constraint_index_is_redundant(&ci
, ineq
->row
[k
]);
2098 constraint_index_free(&ci
);
2101 constraint_index_free(&ci
);
2102 return isl_stat_error
;
2105 static struct isl_basic_set
*remove_shifted_constraints(
2106 struct isl_basic_set
*bset
, struct isl_basic_set
*context
)
2108 struct isl_constraint_index ci
;
2111 if (!bset
|| !context
)
2114 if (context
->n_ineq
== 0)
2116 if (setup_constraint_index(&ci
, context
) < 0)
2119 for (k
= 0; k
< bset
->n_ineq
; ++k
) {
2122 redundant
= constraint_index_is_redundant(&ci
, bset
->ineq
[k
]);
2127 bset
= isl_basic_set_cow(bset
);
2130 isl_basic_set_drop_inequality(bset
, k
);
2133 constraint_index_free(&ci
);
2136 constraint_index_free(&ci
);
2140 /* Remove constraints from "bmap" that are identical to constraints
2141 * in "context" or that are more relaxed (greater constant term).
2143 * We perform the test for shifted copies on the pure constraints
2144 * in remove_shifted_constraints.
2146 static __isl_give isl_basic_map
*isl_basic_map_remove_shifted_constraints(
2147 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
2149 isl_basic_set
*bset
, *bset_context
;
2151 if (!bmap
|| !context
)
2154 if (bmap
->n_ineq
== 0 || context
->n_ineq
== 0) {
2155 isl_basic_map_free(context
);
2159 context
= isl_basic_map_align_divs(context
, bmap
);
2160 bmap
= isl_basic_map_align_divs(bmap
, context
);
2162 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
2163 bset_context
= isl_basic_map_underlying_set(context
);
2164 bset
= remove_shifted_constraints(bset
, bset_context
);
2165 isl_basic_set_free(bset_context
);
2167 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
2171 isl_basic_map_free(bmap
);
2172 isl_basic_map_free(context
);
2176 /* Does the (linear part of a) constraint "c" involve any of the "len"
2177 * "relevant" dimensions?
2179 static int is_related(isl_int
*c
, int len
, int *relevant
)
2183 for (i
= 0; i
< len
; ++i
) {
2186 if (!isl_int_is_zero(c
[i
]))
2193 /* Drop constraints from "bmap" that do not involve any of
2194 * the dimensions marked "relevant".
2196 static __isl_give isl_basic_map
*drop_unrelated_constraints(
2197 __isl_take isl_basic_map
*bmap
, int *relevant
)
2201 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2202 for (i
= 0; i
< dim
; ++i
)
2208 for (i
= bmap
->n_eq
- 1; i
>= 0; --i
)
2209 if (!is_related(bmap
->eq
[i
] + 1, dim
, relevant
)) {
2210 bmap
= isl_basic_map_cow(bmap
);
2211 if (isl_basic_map_drop_equality(bmap
, i
) < 0)
2212 return isl_basic_map_free(bmap
);
2215 for (i
= bmap
->n_ineq
- 1; i
>= 0; --i
)
2216 if (!is_related(bmap
->ineq
[i
] + 1, dim
, relevant
)) {
2217 bmap
= isl_basic_map_cow(bmap
);
2218 if (isl_basic_map_drop_inequality(bmap
, i
) < 0)
2219 return isl_basic_map_free(bmap
);
2225 /* Update the groups in "group" based on the (linear part of a) constraint "c".
2227 * In particular, for any variable involved in the constraint,
2228 * find the actual group id from before and replace the group
2229 * of the corresponding variable by the minimal group of all
2230 * the variables involved in the constraint considered so far
2231 * (if this minimum is smaller) or replace the minimum by this group
2232 * (if the minimum is larger).
2234 * At the end, all the variables in "c" will (indirectly) point
2235 * to the minimal of the groups that they referred to originally.
2237 static void update_groups(int dim
, int *group
, isl_int
*c
)
2242 for (j
= 0; j
< dim
; ++j
) {
2243 if (isl_int_is_zero(c
[j
]))
2245 while (group
[j
] >= 0 && group
[group
[j
]] != group
[j
])
2246 group
[j
] = group
[group
[j
]];
2247 if (group
[j
] == min
)
2249 if (group
[j
] < min
) {
2250 if (min
>= 0 && min
< dim
)
2251 group
[min
] = group
[j
];
2254 group
[group
[j
]] = min
;
2258 /* Allocate an array of groups of variables, one for each variable
2259 * in "context", initialized to zero.
2261 static int *alloc_groups(__isl_keep isl_basic_set
*context
)
2266 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2267 ctx
= isl_basic_set_get_ctx(context
);
2268 return isl_calloc_array(ctx
, int, dim
);
2271 /* Drop constraints from "bmap" that only involve variables that are
2272 * not related to any of the variables marked with a "-1" in "group".
2274 * We construct groups of variables that collect variables that
2275 * (indirectly) appear in some common constraint of "bmap".
2276 * Each group is identified by the first variable in the group,
2277 * except for the special group of variables that was already identified
2278 * in the input as -1 (or are related to those variables).
2279 * If group[i] is equal to i (or -1), then the group of i is i (or -1),
2280 * otherwise the group of i is the group of group[i].
2282 * We first initialize groups for the remaining variables.
2283 * Then we iterate over the constraints of "bmap" and update the
2284 * group of the variables in the constraint by the smallest group.
2285 * Finally, we resolve indirect references to groups by running over
2288 * After computing the groups, we drop constraints that do not involve
2289 * any variables in the -1 group.
2291 __isl_give isl_basic_map
*isl_basic_map_drop_unrelated_constraints(
2292 __isl_take isl_basic_map
*bmap
, __isl_take
int *group
)
2301 dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
2304 for (i
= 0; i
< dim
; ++i
)
2306 last
= group
[i
] = i
;
2312 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2313 update_groups(dim
, group
, bmap
->eq
[i
] + 1);
2314 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
2315 update_groups(dim
, group
, bmap
->ineq
[i
] + 1);
2317 for (i
= 0; i
< dim
; ++i
)
2319 group
[i
] = group
[group
[i
]];
2321 for (i
= 0; i
< dim
; ++i
)
2322 group
[i
] = group
[i
] == -1;
2324 bmap
= drop_unrelated_constraints(bmap
, group
);
2330 /* Drop constraints from "context" that are irrelevant for computing
2331 * the gist of "bset".
2333 * In particular, drop constraints in variables that are not related
2334 * to any of the variables involved in the constraints of "bset"
2335 * in the sense that there is no sequence of constraints that connects them.
2337 * We first mark all variables that appear in "bset" as belonging
2338 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2340 static __isl_give isl_basic_set
*drop_irrelevant_constraints(
2341 __isl_take isl_basic_set
*context
, __isl_keep isl_basic_set
*bset
)
2347 if (!context
|| !bset
)
2348 return isl_basic_set_free(context
);
2350 group
= alloc_groups(context
);
2353 return isl_basic_set_free(context
);
2355 dim
= isl_basic_set_dim(bset
, isl_dim_set
);
2356 for (i
= 0; i
< dim
; ++i
) {
2357 for (j
= 0; j
< bset
->n_eq
; ++j
)
2358 if (!isl_int_is_zero(bset
->eq
[j
][1 + i
]))
2360 if (j
< bset
->n_eq
) {
2364 for (j
= 0; j
< bset
->n_ineq
; ++j
)
2365 if (!isl_int_is_zero(bset
->ineq
[j
][1 + i
]))
2367 if (j
< bset
->n_ineq
)
2371 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2374 /* Drop constraints from "context" that are irrelevant for computing
2375 * the gist of the inequalities "ineq".
2376 * Inequalities in "ineq" for which the corresponding element of row
2377 * is set to -1 have already been marked for removal and should be ignored.
2379 * In particular, drop constraints in variables that are not related
2380 * to any of the variables involved in "ineq"
2381 * in the sense that there is no sequence of constraints that connects them.
2383 * We first mark all variables that appear in "bset" as belonging
2384 * to a "-1" group and then continue with group_and_drop_irrelevant_constraints.
2386 static __isl_give isl_basic_set
*drop_irrelevant_constraints_marked(
2387 __isl_take isl_basic_set
*context
, __isl_keep isl_mat
*ineq
, int *row
)
2393 if (!context
|| !ineq
)
2394 return isl_basic_set_free(context
);
2396 group
= alloc_groups(context
);
2399 return isl_basic_set_free(context
);
2401 dim
= isl_basic_set_dim(context
, isl_dim_set
);
2402 n
= isl_mat_rows(ineq
);
2403 for (i
= 0; i
< dim
; ++i
) {
2404 for (j
= 0; j
< n
; ++j
) {
2407 if (!isl_int_is_zero(ineq
->row
[j
][1 + i
]))
2414 return isl_basic_map_drop_unrelated_constraints(context
, group
);
2417 /* Do all "n" entries of "row" contain a negative value?
2419 static int all_neg(int *row
, int n
)
2423 for (i
= 0; i
< n
; ++i
)
2430 /* Update the inequalities in "bset" based on the information in "row"
2433 * In particular, the array "row" contains either -1, meaning that
2434 * the corresponding inequality of "bset" is redundant, or the index
2435 * of an inequality in "tab".
2437 * If the row entry is -1, then drop the inequality.
2438 * Otherwise, if the constraint is marked redundant in the tableau,
2439 * then drop the inequality. Similarly, if it is marked as an equality
2440 * in the tableau, then turn the inequality into an equality and
2441 * perform Gaussian elimination.
2443 static __isl_give isl_basic_set
*update_ineq(__isl_take isl_basic_set
*bset
,
2444 __isl_keep
int *row
, struct isl_tab
*tab
)
2449 int found_equality
= 0;
2453 if (tab
&& tab
->empty
)
2454 return isl_basic_set_set_to_empty(bset
);
2456 n_ineq
= bset
->n_ineq
;
2457 for (i
= n_ineq
- 1; i
>= 0; --i
) {
2459 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2460 return isl_basic_set_free(bset
);
2466 if (isl_tab_is_equality(tab
, n_eq
+ row
[i
])) {
2467 isl_basic_map_inequality_to_equality(bset
, i
);
2469 } else if (isl_tab_is_redundant(tab
, n_eq
+ row
[i
])) {
2470 if (isl_basic_set_drop_inequality(bset
, i
) < 0)
2471 return isl_basic_set_free(bset
);
2476 bset
= isl_basic_set_gauss(bset
, NULL
);
2477 bset
= isl_basic_set_finalize(bset
);
2481 /* Update the inequalities in "bset" based on the information in "row"
2482 * and "tab" and free all arguments (other than "bset").
2484 static __isl_give isl_basic_set
*update_ineq_free(
2485 __isl_take isl_basic_set
*bset
, __isl_take isl_mat
*ineq
,
2486 __isl_take isl_basic_set
*context
, __isl_take
int *row
,
2487 struct isl_tab
*tab
)
2490 isl_basic_set_free(context
);
2492 bset
= update_ineq(bset
, row
, tab
);
2499 /* Remove all information from bset that is redundant in the context
2501 * "ineq" contains the (possibly transformed) inequalities of "bset",
2502 * in the same order.
2503 * The (explicit) equalities of "bset" are assumed to have been taken
2504 * into account by the transformation such that only the inequalities
2506 * "context" is assumed not to be empty.
2508 * "row" keeps track of the constraint index of a "bset" inequality in "tab".
2509 * A value of -1 means that the inequality is obviously redundant and may
2510 * not even appear in "tab".
2512 * We first mark the inequalities of "bset"
2513 * that are obviously redundant with respect to some inequality in "context".
2514 * Then we remove those constraints from "context" that have become
2515 * irrelevant for computing the gist of "bset".
2516 * Note that this removal of constraints cannot be replaced by
2517 * a factorization because factors in "bset" may still be connected
2518 * to each other through constraints in "context".
2520 * If there are any inequalities left, we construct a tableau for
2521 * the context and then add the inequalities of "bset".
2522 * Before adding these inequalities, we freeze all constraints such that
2523 * they won't be considered redundant in terms of the constraints of "bset".
2524 * Then we detect all redundant constraints (among the
2525 * constraints that weren't frozen), first by checking for redundancy in the
2526 * the tableau and then by checking if replacing a constraint by its negation
2527 * would lead to an empty set. This last step is fairly expensive
2528 * and could be optimized by more reuse of the tableau.
2529 * Finally, we update bset according to the results.
2531 static __isl_give isl_basic_set
*uset_gist_full(__isl_take isl_basic_set
*bset
,
2532 __isl_take isl_mat
*ineq
, __isl_take isl_basic_set
*context
)
2537 isl_basic_set
*combined
= NULL
;
2538 struct isl_tab
*tab
= NULL
;
2539 unsigned n_eq
, context_ineq
;
2542 if (!bset
|| !ineq
|| !context
)
2545 if (bset
->n_ineq
== 0 || isl_basic_set_plain_is_universe(context
)) {
2546 isl_basic_set_free(context
);
2551 ctx
= isl_basic_set_get_ctx(context
);
2552 row
= isl_calloc_array(ctx
, int, bset
->n_ineq
);
2556 if (mark_shifted_constraints(ineq
, context
, row
) < 0)
2558 if (all_neg(row
, bset
->n_ineq
))
2559 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2561 context
= drop_irrelevant_constraints_marked(context
, ineq
, row
);
2564 if (isl_basic_set_plain_is_universe(context
))
2565 return update_ineq_free(bset
, ineq
, context
, row
, NULL
);
2567 n_eq
= context
->n_eq
;
2568 context_ineq
= context
->n_ineq
;
2569 combined
= isl_basic_set_cow(isl_basic_set_copy(context
));
2570 combined
= isl_basic_set_extend_constraints(combined
, 0, bset
->n_ineq
);
2571 tab
= isl_tab_from_basic_set(combined
, 0);
2572 for (i
= 0; i
< context_ineq
; ++i
)
2573 if (isl_tab_freeze_constraint(tab
, n_eq
+ i
) < 0)
2575 if (isl_tab_extend_cons(tab
, bset
->n_ineq
) < 0)
2578 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
2581 combined
= isl_basic_set_add_ineq(combined
, ineq
->row
[i
]);
2582 if (isl_tab_add_ineq(tab
, ineq
->row
[i
]) < 0)
2586 if (isl_tab_detect_implicit_equalities(tab
) < 0)
2588 if (isl_tab_detect_redundant(tab
) < 0)
2590 total
= isl_basic_set_total_dim(bset
);
2591 for (i
= bset
->n_ineq
- 1; i
>= 0; --i
) {
2592 isl_basic_set
*test
;
2598 if (tab
->con
[n_eq
+ r
].is_redundant
)
2600 test
= isl_basic_set_dup(combined
);
2601 if (isl_inequality_negate(test
, r
) < 0)
2602 test
= isl_basic_set_free(test
);
2603 test
= isl_basic_set_update_from_tab(test
, tab
);
2604 is_empty
= isl_basic_set_is_empty(test
);
2605 isl_basic_set_free(test
);
2609 tab
->con
[n_eq
+ r
].is_redundant
= 1;
2611 bset
= update_ineq_free(bset
, ineq
, context
, row
, tab
);
2613 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2614 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2617 isl_basic_set_free(combined
);
2623 isl_basic_set_free(combined
);
2624 isl_basic_set_free(context
);
2625 isl_basic_set_free(bset
);
2629 /* Extract the inequalities of "bset" as an isl_mat.
2631 static __isl_give isl_mat
*extract_ineq(__isl_keep isl_basic_set
*bset
)
2640 ctx
= isl_basic_set_get_ctx(bset
);
2641 total
= isl_basic_set_total_dim(bset
);
2642 ineq
= isl_mat_sub_alloc6(ctx
, bset
->ineq
, 0, bset
->n_ineq
,
2648 /* Remove all information from "bset" that is redundant in the context
2649 * of "context", for the case where both "bset" and "context" are
2652 static __isl_give isl_basic_set
*uset_gist_uncompressed(
2653 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
)
2657 ineq
= extract_ineq(bset
);
2658 return uset_gist_full(bset
, ineq
, context
);
2661 /* Remove all information from "bset" that is redundant in the context
2662 * of "context", for the case where the combined equalities of
2663 * "bset" and "context" allow for a compression that can be obtained
2664 * by preapplication of "T".
2666 * "bset" itself is not transformed by "T". Instead, the inequalities
2667 * are extracted from "bset" and those are transformed by "T".
2668 * uset_gist_full then determines which of the transformed inequalities
2669 * are redundant with respect to the transformed "context" and removes
2670 * the corresponding inequalities from "bset".
2672 * After preapplying "T" to the inequalities, any common factor is
2673 * removed from the coefficients. If this results in a tightening
2674 * of the constant term, then the same tightening is applied to
2675 * the corresponding untransformed inequality in "bset".
2676 * That is, if after plugging in T, a constraint f(x) >= 0 is of the form
2680 * with 0 <= r < g, then it is equivalent to
2684 * This means that f(x) >= 0 is equivalent to f(x) - r >= 0 in the affine
2685 * subspace compressed by T since the latter would be transformed to
2689 static __isl_give isl_basic_set
*uset_gist_compressed(
2690 __isl_take isl_basic_set
*bset
, __isl_take isl_basic_set
*context
,
2691 __isl_take isl_mat
*T
)
2695 int i
, n_row
, n_col
;
2698 ineq
= extract_ineq(bset
);
2699 ineq
= isl_mat_product(ineq
, isl_mat_copy(T
));
2700 context
= isl_basic_set_preimage(context
, T
);
2702 if (!ineq
|| !context
)
2704 if (isl_basic_set_plain_is_empty(context
)) {
2706 isl_basic_set_free(context
);
2707 return isl_basic_set_set_to_empty(bset
);
2710 ctx
= isl_mat_get_ctx(ineq
);
2711 n_row
= isl_mat_rows(ineq
);
2712 n_col
= isl_mat_cols(ineq
);
2714 for (i
= 0; i
< n_row
; ++i
) {
2715 isl_seq_gcd(ineq
->row
[i
] + 1, n_col
- 1, &ctx
->normalize_gcd
);
2716 if (isl_int_is_zero(ctx
->normalize_gcd
))
2718 if (isl_int_is_one(ctx
->normalize_gcd
))
2720 isl_seq_scale_down(ineq
->row
[i
] + 1, ineq
->row
[i
] + 1,
2721 ctx
->normalize_gcd
, n_col
- 1);
2722 isl_int_fdiv_r(rem
, ineq
->row
[i
][0], ctx
->normalize_gcd
);
2723 isl_int_fdiv_q(ineq
->row
[i
][0],
2724 ineq
->row
[i
][0], ctx
->normalize_gcd
);
2725 if (isl_int_is_zero(rem
))
2727 bset
= isl_basic_set_cow(bset
);
2730 isl_int_sub(bset
->ineq
[i
][0], bset
->ineq
[i
][0], rem
);
2734 return uset_gist_full(bset
, ineq
, context
);
2737 isl_basic_set_free(context
);
2738 isl_basic_set_free(bset
);
2742 /* Project "bset" onto the variables that are involved in "template".
2744 static __isl_give isl_basic_set
*project_onto_involved(
2745 __isl_take isl_basic_set
*bset
, __isl_keep isl_basic_set
*template)
2749 if (!bset
|| !template)
2750 return isl_basic_set_free(bset
);
2752 n
= isl_basic_set_dim(template, isl_dim_set
);
2754 for (i
= 0; i
< n
; ++i
) {
2757 involved
= isl_basic_set_involves_dims(template,
2760 return isl_basic_set_free(bset
);
2763 bset
= isl_basic_set_eliminate_vars(bset
, i
, 1);
2769 /* Remove all information from bset that is redundant in the context
2770 * of context. In particular, equalities that are linear combinations
2771 * of those in context are removed. Then the inequalities that are
2772 * redundant in the context of the equalities and inequalities of
2773 * context are removed.
2775 * First of all, we drop those constraints from "context"
2776 * that are irrelevant for computing the gist of "bset".
2777 * Alternatively, we could factorize the intersection of "context" and "bset".
2779 * We first compute the intersection of the integer affine hulls
2780 * of "bset" and "context",
2781 * compute the gist inside this intersection and then reduce
2782 * the constraints with respect to the equalities of the context
2783 * that only involve variables already involved in the input.
2785 * If two constraints are mutually redundant, then uset_gist_full
2786 * will remove the second of those constraints. We therefore first
2787 * sort the constraints so that constraints not involving existentially
2788 * quantified variables are given precedence over those that do.
2789 * We have to perform this sorting before the variable compression,
2790 * because that may effect the order of the variables.
2792 static __isl_give isl_basic_set
*uset_gist(__isl_take isl_basic_set
*bset
,
2793 __isl_take isl_basic_set
*context
)
2798 isl_basic_set
*aff_context
;
2801 if (!bset
|| !context
)
2804 context
= drop_irrelevant_constraints(context
, bset
);
2806 bset
= isl_basic_set_detect_equalities(bset
);
2807 aff
= isl_basic_set_copy(bset
);
2808 aff
= isl_basic_set_plain_affine_hull(aff
);
2809 context
= isl_basic_set_detect_equalities(context
);
2810 aff_context
= isl_basic_set_copy(context
);
2811 aff_context
= isl_basic_set_plain_affine_hull(aff_context
);
2812 aff
= isl_basic_set_intersect(aff
, aff_context
);
2815 if (isl_basic_set_plain_is_empty(aff
)) {
2816 isl_basic_set_free(bset
);
2817 isl_basic_set_free(context
);
2820 bset
= isl_basic_set_sort_constraints(bset
);
2821 if (aff
->n_eq
== 0) {
2822 isl_basic_set_free(aff
);
2823 return uset_gist_uncompressed(bset
, context
);
2825 total
= isl_basic_set_total_dim(bset
);
2826 eq
= isl_mat_sub_alloc6(bset
->ctx
, aff
->eq
, 0, aff
->n_eq
, 0, 1 + total
);
2827 eq
= isl_mat_cow(eq
);
2828 T
= isl_mat_variable_compression(eq
, NULL
);
2829 isl_basic_set_free(aff
);
2830 if (T
&& T
->n_col
== 0) {
2832 isl_basic_set_free(context
);
2833 return isl_basic_set_set_to_empty(bset
);
2836 aff_context
= isl_basic_set_affine_hull(isl_basic_set_copy(context
));
2837 aff_context
= project_onto_involved(aff_context
, bset
);
2839 bset
= uset_gist_compressed(bset
, context
, T
);
2840 bset
= isl_basic_set_reduce_using_equalities(bset
, aff_context
);
2843 ISL_F_SET(bset
, ISL_BASIC_SET_NO_IMPLICIT
);
2844 ISL_F_SET(bset
, ISL_BASIC_SET_NO_REDUNDANT
);
2849 isl_basic_set_free(bset
);
2850 isl_basic_set_free(context
);
2854 /* Return the number of equality constraints in "bmap" that involve
2855 * local variables. This function assumes that Gaussian elimination
2856 * has been applied to the equality constraints.
2858 static int n_div_eq(__isl_keep isl_basic_map
*bmap
)
2866 if (bmap
->n_eq
== 0)
2869 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
2870 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
2873 for (i
= 0; i
< bmap
->n_eq
; ++i
)
2874 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
,
2881 /* Construct a basic map in "space" defined by the equality constraints in "eq".
2882 * The constraints are assumed not to involve any local variables.
2884 static __isl_give isl_basic_map
*basic_map_from_equalities(
2885 __isl_take isl_space
*space
, __isl_take isl_mat
*eq
)
2888 isl_basic_map
*bmap
= NULL
;
2893 if (1 + isl_space_dim(space
, isl_dim_all
) != eq
->n_col
)
2894 isl_die(isl_space_get_ctx(space
), isl_error_internal
,
2895 "unexpected number of columns", goto error
);
2897 bmap
= isl_basic_map_alloc_space(isl_space_copy(space
),
2899 for (i
= 0; i
< eq
->n_row
; ++i
) {
2900 k
= isl_basic_map_alloc_equality(bmap
);
2903 isl_seq_cpy(bmap
->eq
[k
], eq
->row
[i
], eq
->n_col
);
2906 isl_space_free(space
);
2910 isl_space_free(space
);
2912 isl_basic_map_free(bmap
);
2916 /* Construct and return a variable compression based on the equality
2917 * constraints in "bmap1" and "bmap2" that do not involve the local variables.
2918 * "n1" is the number of (initial) equality constraints in "bmap1"
2919 * that do involve local variables.
2920 * "n2" is the number of (initial) equality constraints in "bmap2"
2921 * that do involve local variables.
2922 * "total" is the total number of other variables.
2923 * This function assumes that Gaussian elimination
2924 * has been applied to the equality constraints in both "bmap1" and "bmap2"
2925 * such that the equality constraints not involving local variables
2926 * are those that start at "n1" or "n2".
2928 * If either of "bmap1" and "bmap2" does not have such equality constraints,
2929 * then simply compute the compression based on the equality constraints
2930 * in the other basic map.
2931 * Otherwise, combine the equality constraints from both into a new
2932 * basic map such that Gaussian elimination can be applied to this combination
2933 * and then construct a variable compression from the resulting
2934 * equality constraints.
2936 static __isl_give isl_mat
*combined_variable_compression(
2937 __isl_keep isl_basic_map
*bmap1
, int n1
,
2938 __isl_keep isl_basic_map
*bmap2
, int n2
, int total
)
2941 isl_mat
*E1
, *E2
, *V
;
2942 isl_basic_map
*bmap
;
2944 ctx
= isl_basic_map_get_ctx(bmap1
);
2945 if (bmap1
->n_eq
== n1
) {
2946 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2947 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2948 return isl_mat_variable_compression(E2
, NULL
);
2950 if (bmap2
->n_eq
== n2
) {
2951 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2952 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2953 return isl_mat_variable_compression(E1
, NULL
);
2955 E1
= isl_mat_sub_alloc6(ctx
, bmap1
->eq
,
2956 n1
, bmap1
->n_eq
- n1
, 0, 1 + total
);
2957 E2
= isl_mat_sub_alloc6(ctx
, bmap2
->eq
,
2958 n2
, bmap2
->n_eq
- n2
, 0, 1 + total
);
2959 E1
= isl_mat_concat(E1
, E2
);
2960 bmap
= basic_map_from_equalities(isl_basic_map_get_space(bmap1
), E1
);
2961 bmap
= isl_basic_map_gauss(bmap
, NULL
);
2964 E1
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
2965 V
= isl_mat_variable_compression(E1
, NULL
);
2966 isl_basic_map_free(bmap
);
2971 /* Extract the stride constraints from "bmap", compressed
2972 * with respect to both the stride constraints in "context" and
2973 * the remaining equality constraints in both "bmap" and "context".
2974 * "bmap_n_eq" is the number of (initial) stride constraints in "bmap".
2975 * "context_n_eq" is the number of (initial) stride constraints in "context".
2977 * Let x be all variables in "bmap" (and "context") other than the local
2978 * variables. First compute a variable compression
2982 * based on the non-stride equality constraints in "bmap" and "context".
2983 * Consider the stride constraints of "context",
2987 * with y the local variables and plug in the variable compression,
2990 * A(V x') + B(y) = 0
2992 * Use these constraints to compute a parameter compression on x'
2996 * Now consider the stride constraints of "bmap"
3000 * and plug in x = V*T x''.
3001 * That is, return A = [C*V*T D].
3003 static __isl_give isl_mat
*extract_compressed_stride_constraints(
3004 __isl_keep isl_basic_map
*bmap
, int bmap_n_eq
,
3005 __isl_keep isl_basic_map
*context
, int context_n_eq
)
3009 isl_mat
*A
, *B
, *T
, *V
;
3011 total
= isl_basic_map_dim(context
, isl_dim_all
);
3012 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3015 ctx
= isl_basic_map_get_ctx(bmap
);
3017 V
= combined_variable_compression(bmap
, bmap_n_eq
,
3018 context
, context_n_eq
, total
);
3020 A
= isl_mat_sub_alloc6(ctx
, context
->eq
, 0, context_n_eq
, 0, 1 + total
);
3021 B
= isl_mat_sub_alloc6(ctx
, context
->eq
,
3022 0, context_n_eq
, 1 + total
, n_div
);
3023 A
= isl_mat_product(A
, isl_mat_copy(V
));
3024 T
= isl_mat_parameter_compression_ext(A
, B
);
3025 T
= isl_mat_product(V
, T
);
3027 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3028 T
= isl_mat_diagonal(T
, isl_mat_identity(ctx
, n_div
));
3030 A
= isl_mat_sub_alloc6(ctx
, bmap
->eq
,
3031 0, bmap_n_eq
, 0, 1 + total
+ n_div
);
3032 A
= isl_mat_product(A
, T
);
3037 /* Remove the prime factors from *g that have an exponent that
3038 * is strictly smaller than the exponent in "c".
3039 * All exponents in *g are known to be smaller than or equal
3042 * That is, if *g is equal to
3044 * p_1^{e_1} p_2^{e_2} ... p_n^{e_n}
3046 * and "c" is equal to
3048 * p_1^{f_1} p_2^{f_2} ... p_n^{f_n}
3052 * p_1^{e_1 * (e_1 = f_1)} p_2^{e_2 * (e_2 = f_2)} ...
3053 * p_n^{e_n * (e_n = f_n)}
3055 * If e_i = f_i, then c / *g does not have any p_i factors and therefore
3056 * neither does the gcd of *g and c / *g.
3057 * If e_i < f_i, then the gcd of *g and c / *g has a positive
3058 * power min(e_i, s_i) of p_i with s_i = f_i - e_i among its factors.
3059 * Dividing *g by this gcd therefore strictly reduces the exponent
3060 * of the prime factors that need to be removed, while leaving the
3061 * other prime factors untouched.
3062 * Repeating this process until gcd(*g, c / *g) = 1 therefore
3063 * removes all undesired factors, without removing any others.
3065 static void remove_incomplete_powers(isl_int
*g
, isl_int c
)
3071 isl_int_divexact(t
, c
, *g
);
3072 isl_int_gcd(t
, t
, *g
);
3073 if (isl_int_is_one(t
))
3075 isl_int_divexact(*g
, *g
, t
);
3080 /* Reduce the "n" stride constraints in "bmap" based on a copy "A"
3081 * of the same stride constraints in a compressed space that exploits
3082 * all equalities in the context and the other equalities in "bmap".
3084 * If the stride constraints of "bmap" are of the form
3088 * then A is of the form
3092 * If any of these constraints involves only a single local variable y,
3093 * then the constraint appears as
3103 * Let g be the gcd of m and the coefficients of h.
3104 * Then, in particular, g is a divisor of the coefficients of h and
3108 * is known to be a multiple of g.
3109 * If some prime factor in m appears with the same exponent in g,
3110 * then it can be removed from m because f(x) is already known
3111 * to be a multiple of g and therefore in particular of this power
3112 * of the prime factors.
3113 * Prime factors that appear with a smaller exponent in g cannot
3114 * be removed from m.
3115 * Let g' be the divisor of g containing all prime factors that
3116 * appear with the same exponent in m and g, then
3120 * can be replaced by
3122 * f(x) + m/g' y_i' = 0
3124 * Note that (if g' != 1) this changes the explicit representation
3125 * of y_i to that of y_i', so the integer division at position i
3126 * is marked unknown and later recomputed by a call to
3127 * isl_basic_map_gauss.
3129 static __isl_give isl_basic_map
*reduce_stride_constraints(
3130 __isl_take isl_basic_map
*bmap
, int n
, __isl_keep isl_mat
*A
)
3138 return isl_basic_map_free(bmap
);
3140 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3141 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
3145 for (i
= 0; i
< n
; ++i
) {
3148 div
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
, n_div
);
3150 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_internal
,
3151 "equality constraints modified unexpectedly",
3153 if (isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + total
+ div
+ 1,
3154 n_div
- div
- 1) != -1)
3156 if (isl_mat_row_gcd(A
, i
, &gcd
) < 0)
3158 if (isl_int_is_one(gcd
))
3160 remove_incomplete_powers(&gcd
, bmap
->eq
[i
][1 + total
+ div
]);
3161 if (isl_int_is_one(gcd
))
3163 isl_int_divexact(bmap
->eq
[i
][1 + total
+ div
],
3164 bmap
->eq
[i
][1 + total
+ div
], gcd
);
3165 bmap
= isl_basic_map_mark_div_unknown(bmap
, div
);
3173 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3178 isl_basic_map_free(bmap
);
3182 /* Simplify the stride constraints in "bmap" based on
3183 * the remaining equality constraints in "bmap" and all equality
3184 * constraints in "context".
3185 * Only do this if both "bmap" and "context" have stride constraints.
3187 * First extract a copy of the stride constraints in "bmap" in a compressed
3188 * space exploiting all the other equality constraints and then
3189 * use this compressed copy to simplify the original stride constraints.
3191 static __isl_give isl_basic_map
*gist_strides(__isl_take isl_basic_map
*bmap
,
3192 __isl_keep isl_basic_map
*context
)
3194 int bmap_n_eq
, context_n_eq
;
3197 if (!bmap
|| !context
)
3198 return isl_basic_map_free(bmap
);
3200 bmap_n_eq
= n_div_eq(bmap
);
3201 context_n_eq
= n_div_eq(context
);
3203 if (bmap_n_eq
< 0 || context_n_eq
< 0)
3204 return isl_basic_map_free(bmap
);
3205 if (bmap_n_eq
== 0 || context_n_eq
== 0)
3208 A
= extract_compressed_stride_constraints(bmap
, bmap_n_eq
,
3209 context
, context_n_eq
);
3210 bmap
= reduce_stride_constraints(bmap
, bmap_n_eq
, A
);
3217 /* Return a basic map that has the same intersection with "context" as "bmap"
3218 * and that is as "simple" as possible.
3220 * The core computation is performed on the pure constraints.
3221 * When we add back the meaning of the integer divisions, we need
3222 * to (re)introduce the div constraints. If we happen to have
3223 * discovered that some of these integer divisions are equal to
3224 * some affine combination of other variables, then these div
3225 * constraints may end up getting simplified in terms of the equalities,
3226 * resulting in extra inequalities on the other variables that
3227 * may have been removed already or that may not even have been
3228 * part of the input. We try and remove those constraints of
3229 * this form that are most obviously redundant with respect to
3230 * the context. We also remove those div constraints that are
3231 * redundant with respect to the other constraints in the result.
3233 * The stride constraints among the equality constraints in "bmap" are
3234 * also simplified with respecting to the other equality constraints
3235 * in "bmap" and with respect to all equality constraints in "context".
3237 struct isl_basic_map
*isl_basic_map_gist(struct isl_basic_map
*bmap
,
3238 struct isl_basic_map
*context
)
3240 isl_basic_set
*bset
, *eq
;
3241 isl_basic_map
*eq_bmap
;
3242 unsigned total
, n_div
, extra
, n_eq
, n_ineq
;
3244 if (!bmap
|| !context
)
3247 if (isl_basic_map_plain_is_universe(bmap
)) {
3248 isl_basic_map_free(context
);
3251 if (isl_basic_map_plain_is_empty(context
)) {
3252 isl_space
*space
= isl_basic_map_get_space(bmap
);
3253 isl_basic_map_free(bmap
);
3254 isl_basic_map_free(context
);
3255 return isl_basic_map_universe(space
);
3257 if (isl_basic_map_plain_is_empty(bmap
)) {
3258 isl_basic_map_free(context
);
3262 bmap
= isl_basic_map_remove_redundancies(bmap
);
3263 context
= isl_basic_map_remove_redundancies(context
);
3267 context
= isl_basic_map_align_divs(context
, bmap
);
3268 n_div
= isl_basic_map_dim(context
, isl_dim_div
);
3269 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
3270 extra
= n_div
- isl_basic_map_dim(bmap
, isl_dim_div
);
3272 bset
= isl_basic_map_underlying_set(isl_basic_map_copy(bmap
));
3273 bset
= isl_basic_set_add_dims(bset
, isl_dim_set
, extra
);
3274 bset
= uset_gist(bset
,
3275 isl_basic_map_underlying_set(isl_basic_map_copy(context
)));
3276 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, total
, extra
);
3278 if (!bset
|| bset
->n_eq
== 0 || n_div
== 0 ||
3279 isl_basic_set_plain_is_empty(bset
)) {
3280 isl_basic_map_free(context
);
3281 return isl_basic_map_overlying_set(bset
, bmap
);
3285 n_ineq
= bset
->n_ineq
;
3286 eq
= isl_basic_set_copy(bset
);
3287 eq
= isl_basic_set_cow(eq
);
3288 if (isl_basic_set_free_inequality(eq
, n_ineq
) < 0)
3289 eq
= isl_basic_set_free(eq
);
3290 if (isl_basic_set_free_equality(bset
, n_eq
) < 0)
3291 bset
= isl_basic_set_free(bset
);
3293 eq_bmap
= isl_basic_map_overlying_set(eq
, isl_basic_map_copy(bmap
));
3294 eq_bmap
= gist_strides(eq_bmap
, context
);
3295 eq_bmap
= isl_basic_map_remove_shifted_constraints(eq_bmap
, context
);
3296 bmap
= isl_basic_map_overlying_set(bset
, bmap
);
3297 bmap
= isl_basic_map_intersect(bmap
, eq_bmap
);
3298 bmap
= isl_basic_map_remove_redundancies(bmap
);
3302 isl_basic_map_free(bmap
);
3303 isl_basic_map_free(context
);
3308 * Assumes context has no implicit divs.
3310 __isl_give isl_map
*isl_map_gist_basic_map(__isl_take isl_map
*map
,
3311 __isl_take isl_basic_map
*context
)
3315 if (!map
|| !context
)
3318 if (isl_basic_map_plain_is_empty(context
)) {
3319 isl_space
*space
= isl_map_get_space(map
);
3321 isl_basic_map_free(context
);
3322 return isl_map_universe(space
);
3325 context
= isl_basic_map_remove_redundancies(context
);
3326 map
= isl_map_cow(map
);
3327 if (!map
|| !context
)
3329 isl_assert(map
->ctx
, isl_space_is_equal(map
->dim
, context
->dim
), goto error
);
3330 map
= isl_map_compute_divs(map
);
3333 for (i
= map
->n
- 1; i
>= 0; --i
) {
3334 map
->p
[i
] = isl_basic_map_gist(map
->p
[i
],
3335 isl_basic_map_copy(context
));
3338 if (isl_basic_map_plain_is_empty(map
->p
[i
])) {
3339 isl_basic_map_free(map
->p
[i
]);
3340 if (i
!= map
->n
- 1)
3341 map
->p
[i
] = map
->p
[map
->n
- 1];
3345 isl_basic_map_free(context
);
3346 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3350 isl_basic_map_free(context
);
3354 /* Drop all inequalities from "bmap" that also appear in "context".
3355 * "context" is assumed to have only known local variables and
3356 * the initial local variables of "bmap" are assumed to be the same
3357 * as those of "context".
3358 * The constraints of both "bmap" and "context" are assumed
3359 * to have been sorted using isl_basic_map_sort_constraints.
3361 * Run through the inequality constraints of "bmap" and "context"
3363 * If a constraint of "bmap" involves variables not in "context",
3364 * then it cannot appear in "context".
3365 * If a matching constraint is found, it is removed from "bmap".
3367 static __isl_give isl_basic_map
*drop_inequalities(
3368 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3371 unsigned total
, extra
;
3373 if (!bmap
|| !context
)
3374 return isl_basic_map_free(bmap
);
3376 total
= isl_basic_map_total_dim(context
);
3377 extra
= isl_basic_map_total_dim(bmap
) - total
;
3379 i1
= bmap
->n_ineq
- 1;
3380 i2
= context
->n_ineq
- 1;
3381 while (bmap
&& i1
>= 0 && i2
>= 0) {
3384 if (isl_seq_first_non_zero(bmap
->ineq
[i1
] + 1 + total
,
3389 cmp
= isl_basic_map_constraint_cmp(context
, bmap
->ineq
[i1
],
3399 if (isl_int_eq(bmap
->ineq
[i1
][0], context
->ineq
[i2
][0])) {
3400 bmap
= isl_basic_map_cow(bmap
);
3401 if (isl_basic_map_drop_inequality(bmap
, i1
) < 0)
3402 bmap
= isl_basic_map_free(bmap
);
3411 /* Drop all equalities from "bmap" that also appear in "context".
3412 * "context" is assumed to have only known local variables and
3413 * the initial local variables of "bmap" are assumed to be the same
3414 * as those of "context".
3416 * Run through the equality constraints of "bmap" and "context"
3418 * If a constraint of "bmap" involves variables not in "context",
3419 * then it cannot appear in "context".
3420 * If a matching constraint is found, it is removed from "bmap".
3422 static __isl_give isl_basic_map
*drop_equalities(
3423 __isl_take isl_basic_map
*bmap
, __isl_keep isl_basic_map
*context
)
3426 unsigned total
, extra
;
3428 if (!bmap
|| !context
)
3429 return isl_basic_map_free(bmap
);
3431 total
= isl_basic_map_total_dim(context
);
3432 extra
= isl_basic_map_total_dim(bmap
) - total
;
3434 i1
= bmap
->n_eq
- 1;
3435 i2
= context
->n_eq
- 1;
3437 while (bmap
&& i1
>= 0 && i2
>= 0) {
3440 if (isl_seq_first_non_zero(bmap
->eq
[i1
] + 1 + total
,
3443 last1
= isl_seq_last_non_zero(bmap
->eq
[i1
] + 1, total
);
3444 last2
= isl_seq_last_non_zero(context
->eq
[i2
] + 1, total
);
3445 if (last1
> last2
) {
3449 if (last1
< last2
) {
3453 if (isl_seq_eq(bmap
->eq
[i1
], context
->eq
[i2
], 1 + total
)) {
3454 bmap
= isl_basic_map_cow(bmap
);
3455 if (isl_basic_map_drop_equality(bmap
, i1
) < 0)
3456 bmap
= isl_basic_map_free(bmap
);
3465 /* Remove the constraints in "context" from "bmap".
3466 * "context" is assumed to have explicit representations
3467 * for all local variables.
3469 * First align the divs of "bmap" to those of "context" and
3470 * sort the constraints. Then drop all constraints from "bmap"
3471 * that appear in "context".
3473 __isl_give isl_basic_map
*isl_basic_map_plain_gist(
3474 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_map
*context
)
3476 isl_bool done
, known
;
3478 done
= isl_basic_map_plain_is_universe(context
);
3479 if (done
== isl_bool_false
)
3480 done
= isl_basic_map_plain_is_universe(bmap
);
3481 if (done
== isl_bool_false
)
3482 done
= isl_basic_map_plain_is_empty(context
);
3483 if (done
== isl_bool_false
)
3484 done
= isl_basic_map_plain_is_empty(bmap
);
3488 isl_basic_map_free(context
);
3491 known
= isl_basic_map_divs_known(context
);
3495 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
3496 "context has unknown divs", goto error
);
3498 bmap
= isl_basic_map_align_divs(bmap
, context
);
3499 bmap
= isl_basic_map_gauss(bmap
, NULL
);
3500 bmap
= isl_basic_map_sort_constraints(bmap
);
3501 context
= isl_basic_map_sort_constraints(context
);
3503 bmap
= drop_inequalities(bmap
, context
);
3504 bmap
= drop_equalities(bmap
, context
);
3506 isl_basic_map_free(context
);
3507 bmap
= isl_basic_map_finalize(bmap
);
3510 isl_basic_map_free(bmap
);
3511 isl_basic_map_free(context
);
3515 /* Replace "map" by the disjunct at position "pos" and free "context".
3517 static __isl_give isl_map
*replace_by_disjunct(__isl_take isl_map
*map
,
3518 int pos
, __isl_take isl_basic_map
*context
)
3520 isl_basic_map
*bmap
;
3522 bmap
= isl_basic_map_copy(map
->p
[pos
]);
3524 isl_basic_map_free(context
);
3525 return isl_map_from_basic_map(bmap
);
3528 /* Remove the constraints in "context" from "map".
3529 * If any of the disjuncts in the result turns out to be the universe,
3530 * then return this universe.
3531 * "context" is assumed to have explicit representations
3532 * for all local variables.
3534 __isl_give isl_map
*isl_map_plain_gist_basic_map(__isl_take isl_map
*map
,
3535 __isl_take isl_basic_map
*context
)
3538 isl_bool univ
, known
;
3540 univ
= isl_basic_map_plain_is_universe(context
);
3544 isl_basic_map_free(context
);
3547 known
= isl_basic_map_divs_known(context
);
3551 isl_die(isl_map_get_ctx(map
), isl_error_invalid
,
3552 "context has unknown divs", goto error
);
3554 map
= isl_map_cow(map
);
3557 for (i
= 0; i
< map
->n
; ++i
) {
3558 map
->p
[i
] = isl_basic_map_plain_gist(map
->p
[i
],
3559 isl_basic_map_copy(context
));
3560 univ
= isl_basic_map_plain_is_universe(map
->p
[i
]);
3563 if (univ
&& map
->n
> 1)
3564 return replace_by_disjunct(map
, i
, context
);
3567 isl_basic_map_free(context
);
3568 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
3570 ISL_F_CLR(map
, ISL_MAP_DISJOINT
);
3574 isl_basic_map_free(context
);
3578 /* Replace "map" by a universe map in the same space and free "drop".
3580 static __isl_give isl_map
*replace_by_universe(__isl_take isl_map
*map
,
3581 __isl_take isl_map
*drop
)
3585 res
= isl_map_universe(isl_map_get_space(map
));
3591 /* Return a map that has the same intersection with "context" as "map"
3592 * and that is as "simple" as possible.
3594 * If "map" is already the universe, then we cannot make it any simpler.
3595 * Similarly, if "context" is the universe, then we cannot exploit it
3597 * If "map" and "context" are identical to each other, then we can
3598 * return the corresponding universe.
3600 * If either "map" or "context" consists of multiple disjuncts,
3601 * then check if "context" happens to be a subset of "map",
3602 * in which case all constraints can be removed.
3603 * In case of multiple disjuncts, the standard procedure
3604 * may not be able to detect that all constraints can be removed.
3606 * If none of these cases apply, we have to work a bit harder.
3607 * During this computation, we make use of a single disjunct context,
3608 * so if the original context consists of more than one disjunct
3609 * then we need to approximate the context by a single disjunct set.
3610 * Simply taking the simple hull may drop constraints that are
3611 * only implicitly available in each disjunct. We therefore also
3612 * look for constraints among those defining "map" that are valid
3613 * for the context. These can then be used to simplify away
3614 * the corresponding constraints in "map".
3616 static __isl_give isl_map
*map_gist(__isl_take isl_map
*map
,
3617 __isl_take isl_map
*context
)
3621 int single_disjunct_map
, single_disjunct_context
;
3623 isl_basic_map
*hull
;
3625 is_universe
= isl_map_plain_is_universe(map
);
3626 if (is_universe
>= 0 && !is_universe
)
3627 is_universe
= isl_map_plain_is_universe(context
);
3628 if (is_universe
< 0)
3631 isl_map_free(context
);
3635 equal
= isl_map_plain_is_equal(map
, context
);
3639 return replace_by_universe(map
, context
);
3641 single_disjunct_map
= isl_map_n_basic_map(map
) == 1;
3642 single_disjunct_context
= isl_map_n_basic_map(context
) == 1;
3643 if (!single_disjunct_map
|| !single_disjunct_context
) {
3644 subset
= isl_map_is_subset(context
, map
);
3648 return replace_by_universe(map
, context
);
3651 context
= isl_map_compute_divs(context
);
3654 if (single_disjunct_context
) {
3655 hull
= isl_map_simple_hull(context
);
3660 ctx
= isl_map_get_ctx(map
);
3661 list
= isl_map_list_alloc(ctx
, 2);
3662 list
= isl_map_list_add(list
, isl_map_copy(context
));
3663 list
= isl_map_list_add(list
, isl_map_copy(map
));
3664 hull
= isl_map_unshifted_simple_hull_from_map_list(context
,
3667 return isl_map_gist_basic_map(map
, hull
);
3670 isl_map_free(context
);
3674 __isl_give isl_map
*isl_map_gist(__isl_take isl_map
*map
,
3675 __isl_take isl_map
*context
)
3677 return isl_map_align_params_map_map_and(map
, context
, &map_gist
);
3680 struct isl_basic_set
*isl_basic_set_gist(struct isl_basic_set
*bset
,
3681 struct isl_basic_set
*context
)
3683 return bset_from_bmap(isl_basic_map_gist(bset_to_bmap(bset
),
3684 bset_to_bmap(context
)));
3687 __isl_give isl_set
*isl_set_gist_basic_set(__isl_take isl_set
*set
,
3688 __isl_take isl_basic_set
*context
)
3690 return set_from_map(isl_map_gist_basic_map(set_to_map(set
),
3691 bset_to_bmap(context
)));
3694 __isl_give isl_set
*isl_set_gist_params_basic_set(__isl_take isl_set
*set
,
3695 __isl_take isl_basic_set
*context
)
3697 isl_space
*space
= isl_set_get_space(set
);
3698 isl_basic_set
*dom_context
= isl_basic_set_universe(space
);
3699 dom_context
= isl_basic_set_intersect_params(dom_context
, context
);
3700 return isl_set_gist_basic_set(set
, dom_context
);
3703 __isl_give isl_set
*isl_set_gist(__isl_take isl_set
*set
,
3704 __isl_take isl_set
*context
)
3706 return set_from_map(isl_map_gist(set_to_map(set
), set_to_map(context
)));
3709 /* Compute the gist of "bmap" with respect to the constraints "context"
3712 __isl_give isl_basic_map
*isl_basic_map_gist_domain(
3713 __isl_take isl_basic_map
*bmap
, __isl_take isl_basic_set
*context
)
3715 isl_space
*space
= isl_basic_map_get_space(bmap
);
3716 isl_basic_map
*bmap_context
= isl_basic_map_universe(space
);
3718 bmap_context
= isl_basic_map_intersect_domain(bmap_context
, context
);
3719 return isl_basic_map_gist(bmap
, bmap_context
);
3722 __isl_give isl_map
*isl_map_gist_domain(__isl_take isl_map
*map
,
3723 __isl_take isl_set
*context
)
3725 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3726 map_context
= isl_map_intersect_domain(map_context
, context
);
3727 return isl_map_gist(map
, map_context
);
3730 __isl_give isl_map
*isl_map_gist_range(__isl_take isl_map
*map
,
3731 __isl_take isl_set
*context
)
3733 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3734 map_context
= isl_map_intersect_range(map_context
, context
);
3735 return isl_map_gist(map
, map_context
);
3738 __isl_give isl_map
*isl_map_gist_params(__isl_take isl_map
*map
,
3739 __isl_take isl_set
*context
)
3741 isl_map
*map_context
= isl_map_universe(isl_map_get_space(map
));
3742 map_context
= isl_map_intersect_params(map_context
, context
);
3743 return isl_map_gist(map
, map_context
);
3746 __isl_give isl_set
*isl_set_gist_params(__isl_take isl_set
*set
,
3747 __isl_take isl_set
*context
)
3749 return isl_map_gist_params(set
, context
);
3752 /* Quick check to see if two basic maps are disjoint.
3753 * In particular, we reduce the equalities and inequalities of
3754 * one basic map in the context of the equalities of the other
3755 * basic map and check if we get a contradiction.
3757 isl_bool
isl_basic_map_plain_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3758 __isl_keep isl_basic_map
*bmap2
)
3760 struct isl_vec
*v
= NULL
;
3765 if (!bmap1
|| !bmap2
)
3766 return isl_bool_error
;
3767 isl_assert(bmap1
->ctx
, isl_space_is_equal(bmap1
->dim
, bmap2
->dim
),
3768 return isl_bool_error
);
3769 if (bmap1
->n_div
|| bmap2
->n_div
)
3770 return isl_bool_false
;
3771 if (!bmap1
->n_eq
&& !bmap2
->n_eq
)
3772 return isl_bool_false
;
3774 total
= isl_space_dim(bmap1
->dim
, isl_dim_all
);
3776 return isl_bool_false
;
3777 v
= isl_vec_alloc(bmap1
->ctx
, 1 + total
);
3780 elim
= isl_alloc_array(bmap1
->ctx
, int, total
);
3783 compute_elimination_index(bmap1
, elim
);
3784 for (i
= 0; i
< bmap2
->n_eq
; ++i
) {
3786 reduced
= reduced_using_equalities(v
->block
.data
, bmap2
->eq
[i
],
3788 if (reduced
&& !isl_int_is_zero(v
->block
.data
[0]) &&
3789 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3792 for (i
= 0; i
< bmap2
->n_ineq
; ++i
) {
3794 reduced
= reduced_using_equalities(v
->block
.data
,
3795 bmap2
->ineq
[i
], bmap1
, elim
);
3796 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3797 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3800 compute_elimination_index(bmap2
, elim
);
3801 for (i
= 0; i
< bmap1
->n_ineq
; ++i
) {
3803 reduced
= reduced_using_equalities(v
->block
.data
,
3804 bmap1
->ineq
[i
], bmap2
, elim
);
3805 if (reduced
&& isl_int_is_neg(v
->block
.data
[0]) &&
3806 isl_seq_first_non_zero(v
->block
.data
+ 1, total
) == -1)
3811 return isl_bool_false
;
3815 return isl_bool_true
;
3819 return isl_bool_error
;
3822 int isl_basic_set_plain_is_disjoint(__isl_keep isl_basic_set
*bset1
,
3823 __isl_keep isl_basic_set
*bset2
)
3825 return isl_basic_map_plain_is_disjoint(bset_to_bmap(bset1
),
3826 bset_to_bmap(bset2
));
3829 /* Does "test" hold for all pairs of basic maps in "map1" and "map2"?
3831 static isl_bool
all_pairs(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
,
3832 isl_bool (*test
)(__isl_keep isl_basic_map
*bmap1
,
3833 __isl_keep isl_basic_map
*bmap2
))
3838 return isl_bool_error
;
3840 for (i
= 0; i
< map1
->n
; ++i
) {
3841 for (j
= 0; j
< map2
->n
; ++j
) {
3842 isl_bool d
= test(map1
->p
[i
], map2
->p
[j
]);
3843 if (d
!= isl_bool_true
)
3848 return isl_bool_true
;
3851 /* Are "map1" and "map2" obviously disjoint, based on information
3852 * that can be derived without looking at the individual basic maps?
3854 * In particular, if one of them is empty or if they live in different spaces
3855 * (ignoring parameters), then they are clearly disjoint.
3857 static isl_bool
isl_map_plain_is_disjoint_global(__isl_keep isl_map
*map1
,
3858 __isl_keep isl_map
*map2
)
3864 return isl_bool_error
;
3866 disjoint
= isl_map_plain_is_empty(map1
);
3867 if (disjoint
< 0 || disjoint
)
3870 disjoint
= isl_map_plain_is_empty(map2
);
3871 if (disjoint
< 0 || disjoint
)
3874 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_in
,
3875 map2
->dim
, isl_dim_in
);
3876 if (match
< 0 || !match
)
3877 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3879 match
= isl_space_tuple_is_equal(map1
->dim
, isl_dim_out
,
3880 map2
->dim
, isl_dim_out
);
3881 if (match
< 0 || !match
)
3882 return match
< 0 ? isl_bool_error
: isl_bool_true
;
3884 return isl_bool_false
;
3887 /* Are "map1" and "map2" obviously disjoint?
3889 * If one of them is empty or if they live in different spaces (ignoring
3890 * parameters), then they are clearly disjoint.
3891 * This is checked by isl_map_plain_is_disjoint_global.
3893 * If they have different parameters, then we skip any further tests.
3895 * If they are obviously equal, but not obviously empty, then we will
3896 * not be able to detect if they are disjoint.
3898 * Otherwise we check if each basic map in "map1" is obviously disjoint
3899 * from each basic map in "map2".
3901 isl_bool
isl_map_plain_is_disjoint(__isl_keep isl_map
*map1
,
3902 __isl_keep isl_map
*map2
)
3908 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3909 if (disjoint
< 0 || disjoint
)
3912 match
= isl_space_match(map1
->dim
, isl_dim_param
,
3913 map2
->dim
, isl_dim_param
);
3914 if (match
< 0 || !match
)
3915 return match
< 0 ? isl_bool_error
: isl_bool_false
;
3917 intersect
= isl_map_plain_is_equal(map1
, map2
);
3918 if (intersect
< 0 || intersect
)
3919 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3921 return all_pairs(map1
, map2
, &isl_basic_map_plain_is_disjoint
);
3924 /* Are "map1" and "map2" disjoint?
3926 * They are disjoint if they are "obviously disjoint" or if one of them
3927 * is empty. Otherwise, they are not disjoint if one of them is universal.
3928 * If the two inputs are (obviously) equal and not empty, then they are
3930 * If none of these cases apply, then check if all pairs of basic maps
3933 isl_bool
isl_map_is_disjoint(__isl_keep isl_map
*map1
, __isl_keep isl_map
*map2
)
3938 disjoint
= isl_map_plain_is_disjoint_global(map1
, map2
);
3939 if (disjoint
< 0 || disjoint
)
3942 disjoint
= isl_map_is_empty(map1
);
3943 if (disjoint
< 0 || disjoint
)
3946 disjoint
= isl_map_is_empty(map2
);
3947 if (disjoint
< 0 || disjoint
)
3950 intersect
= isl_map_plain_is_universe(map1
);
3951 if (intersect
< 0 || intersect
)
3952 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3954 intersect
= isl_map_plain_is_universe(map2
);
3955 if (intersect
< 0 || intersect
)
3956 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3958 intersect
= isl_map_plain_is_equal(map1
, map2
);
3959 if (intersect
< 0 || intersect
)
3960 return isl_bool_not(intersect
);
3962 return all_pairs(map1
, map2
, &isl_basic_map_is_disjoint
);
3965 /* Are "bmap1" and "bmap2" disjoint?
3967 * They are disjoint if they are "obviously disjoint" or if one of them
3968 * is empty. Otherwise, they are not disjoint if one of them is universal.
3969 * If none of these cases apply, we compute the intersection and see if
3970 * the result is empty.
3972 isl_bool
isl_basic_map_is_disjoint(__isl_keep isl_basic_map
*bmap1
,
3973 __isl_keep isl_basic_map
*bmap2
)
3977 isl_basic_map
*test
;
3979 disjoint
= isl_basic_map_plain_is_disjoint(bmap1
, bmap2
);
3980 if (disjoint
< 0 || disjoint
)
3983 disjoint
= isl_basic_map_is_empty(bmap1
);
3984 if (disjoint
< 0 || disjoint
)
3987 disjoint
= isl_basic_map_is_empty(bmap2
);
3988 if (disjoint
< 0 || disjoint
)
3991 intersect
= isl_basic_map_plain_is_universe(bmap1
);
3992 if (intersect
< 0 || intersect
)
3993 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3995 intersect
= isl_basic_map_plain_is_universe(bmap2
);
3996 if (intersect
< 0 || intersect
)
3997 return intersect
< 0 ? isl_bool_error
: isl_bool_false
;
3999 test
= isl_basic_map_intersect(isl_basic_map_copy(bmap1
),
4000 isl_basic_map_copy(bmap2
));
4001 disjoint
= isl_basic_map_is_empty(test
);
4002 isl_basic_map_free(test
);
4007 /* Are "bset1" and "bset2" disjoint?
4009 isl_bool
isl_basic_set_is_disjoint(__isl_keep isl_basic_set
*bset1
,
4010 __isl_keep isl_basic_set
*bset2
)
4012 return isl_basic_map_is_disjoint(bset1
, bset2
);
4015 isl_bool
isl_set_plain_is_disjoint(__isl_keep isl_set
*set1
,
4016 __isl_keep isl_set
*set2
)
4018 return isl_map_plain_is_disjoint(set_to_map(set1
), set_to_map(set2
));
4021 /* Are "set1" and "set2" disjoint?
4023 isl_bool
isl_set_is_disjoint(__isl_keep isl_set
*set1
, __isl_keep isl_set
*set2
)
4025 return isl_map_is_disjoint(set1
, set2
);
4028 /* Is "v" equal to 0, 1 or -1?
4030 static int is_zero_or_one(isl_int v
)
4032 return isl_int_is_zero(v
) || isl_int_is_one(v
) || isl_int_is_negone(v
);
4035 /* Check if we can combine a given div with lower bound l and upper
4036 * bound u with some other div and if so return that other div.
4037 * Otherwise return -1.
4039 * We first check that
4040 * - the bounds are opposites of each other (except for the constant
4042 * - the bounds do not reference any other div
4043 * - no div is defined in terms of this div
4045 * Let m be the size of the range allowed on the div by the bounds.
4046 * That is, the bounds are of the form
4048 * e <= a <= e + m - 1
4050 * with e some expression in the other variables.
4051 * We look for another div b such that no third div is defined in terms
4052 * of this second div b and such that in any constraint that contains
4053 * a (except for the given lower and upper bound), also contains b
4054 * with a coefficient that is m times that of b.
4055 * That is, all constraints (execpt for the lower and upper bound)
4058 * e + f (a + m b) >= 0
4060 * Furthermore, in the constraints that only contain b, the coefficient
4061 * of b should be equal to 1 or -1.
4062 * If so, we return b so that "a + m b" can be replaced by
4063 * a single div "c = a + m b".
4065 static int div_find_coalesce(struct isl_basic_map
*bmap
, int *pairs
,
4066 unsigned div
, unsigned l
, unsigned u
)
4072 if (bmap
->n_div
<= 1)
4074 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4075 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
, div
) != -1)
4077 if (isl_seq_first_non_zero(bmap
->ineq
[l
] + 1 + dim
+ div
+ 1,
4078 bmap
->n_div
- div
- 1) != -1)
4080 if (!isl_seq_is_neg(bmap
->ineq
[l
] + 1, bmap
->ineq
[u
] + 1,
4084 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4085 if (isl_int_is_zero(bmap
->div
[i
][0]))
4087 if (!isl_int_is_zero(bmap
->div
[i
][1 + 1 + dim
+ div
]))
4091 isl_int_add(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4092 if (isl_int_is_neg(bmap
->ineq
[l
][0])) {
4093 isl_int_sub(bmap
->ineq
[l
][0],
4094 bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4095 bmap
= isl_basic_map_copy(bmap
);
4096 bmap
= isl_basic_map_set_to_empty(bmap
);
4097 isl_basic_map_free(bmap
);
4100 isl_int_add_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4101 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4106 for (j
= 0; j
< bmap
->n_div
; ++j
) {
4107 if (isl_int_is_zero(bmap
->div
[j
][0]))
4109 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + dim
+ i
]))
4112 if (j
< bmap
->n_div
)
4114 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4116 if (j
== l
|| j
== u
)
4118 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ div
])) {
4119 if (is_zero_or_one(bmap
->ineq
[j
][1 + dim
+ i
]))
4123 if (isl_int_is_zero(bmap
->ineq
[j
][1 + dim
+ i
]))
4125 isl_int_mul(bmap
->ineq
[j
][1 + dim
+ div
],
4126 bmap
->ineq
[j
][1 + dim
+ div
],
4128 valid
= isl_int_eq(bmap
->ineq
[j
][1 + dim
+ div
],
4129 bmap
->ineq
[j
][1 + dim
+ i
]);
4130 isl_int_divexact(bmap
->ineq
[j
][1 + dim
+ div
],
4131 bmap
->ineq
[j
][1 + dim
+ div
],
4136 if (j
< bmap
->n_ineq
)
4141 isl_int_sub_ui(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], 1);
4142 isl_int_sub(bmap
->ineq
[l
][0], bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4146 /* Internal data structure used during the construction and/or evaluation of
4147 * an inequality that ensures that a pair of bounds always allows
4148 * for an integer value.
4150 * "tab" is the tableau in which the inequality is evaluated. It may
4151 * be NULL until it is actually needed.
4152 * "v" contains the inequality coefficients.
4153 * "g", "fl" and "fu" are temporary scalars used during the construction and
4156 struct test_ineq_data
{
4157 struct isl_tab
*tab
;
4164 /* Free all the memory allocated by the fields of "data".
4166 static void test_ineq_data_clear(struct test_ineq_data
*data
)
4168 isl_tab_free(data
->tab
);
4169 isl_vec_free(data
->v
);
4170 isl_int_clear(data
->g
);
4171 isl_int_clear(data
->fl
);
4172 isl_int_clear(data
->fu
);
4175 /* Is the inequality stored in data->v satisfied by "bmap"?
4176 * That is, does it only attain non-negative values?
4177 * data->tab is a tableau corresponding to "bmap".
4179 static isl_bool
test_ineq_is_satisfied(__isl_keep isl_basic_map
*bmap
,
4180 struct test_ineq_data
*data
)
4183 enum isl_lp_result res
;
4185 ctx
= isl_basic_map_get_ctx(bmap
);
4187 data
->tab
= isl_tab_from_basic_map(bmap
, 0);
4188 res
= isl_tab_min(data
->tab
, data
->v
->el
, ctx
->one
, &data
->g
, NULL
, 0);
4189 if (res
== isl_lp_error
)
4190 return isl_bool_error
;
4191 return res
== isl_lp_ok
&& isl_int_is_nonneg(data
->g
);
4194 /* Given a lower and an upper bound on div i, do they always allow
4195 * for an integer value of the given div?
4196 * Determine this property by constructing an inequality
4197 * such that the property is guaranteed when the inequality is nonnegative.
4198 * The lower bound is inequality l, while the upper bound is inequality u.
4199 * The constructed inequality is stored in data->v.
4201 * Let the upper bound be
4205 * and the lower bound
4209 * Let n_u = f_u g and n_l = f_l g, with g = gcd(n_u, n_l).
4212 * - f_u e_l <= f_u f_l g a <= f_l e_u
4214 * Since all variables are integer valued, this is equivalent to
4216 * - f_u e_l - (f_u - 1) <= f_u f_l g a <= f_l e_u + (f_l - 1)
4218 * If this interval is at least f_u f_l g, then it contains at least
4219 * one integer value for a.
4220 * That is, the test constraint is
4222 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 >= f_u f_l g
4226 * f_l e_u + f_u e_l + f_l - 1 + f_u - 1 + 1 - f_u f_l g >= 0
4228 * If the coefficients of f_l e_u + f_u e_l have a common divisor g',
4229 * then the constraint can be scaled down by a factor g',
4230 * with the constant term replaced by
4231 * floor((f_l e_{u,0} + f_u e_{l,0} + f_l - 1 + f_u - 1 + 1 - f_u f_l g)/g').
4232 * Note that the result of applying Fourier-Motzkin to this pair
4235 * f_l e_u + f_u e_l >= 0
4237 * If the constant term of the scaled down version of this constraint,
4238 * i.e., floor((f_l e_{u,0} + f_u e_{l,0})/g') is equal to the constant
4239 * term of the scaled down test constraint, then the test constraint
4240 * is known to hold and no explicit evaluation is required.
4241 * This is essentially the Omega test.
4243 * If the test constraint consists of only a constant term, then
4244 * it is sufficient to look at the sign of this constant term.
4246 static isl_bool
int_between_bounds(__isl_keep isl_basic_map
*bmap
, int i
,
4247 int l
, int u
, struct test_ineq_data
*data
)
4249 unsigned offset
, n_div
;
4250 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
4251 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4253 isl_int_gcd(data
->g
,
4254 bmap
->ineq
[l
][offset
+ i
], bmap
->ineq
[u
][offset
+ i
]);
4255 isl_int_divexact(data
->fl
, bmap
->ineq
[l
][offset
+ i
], data
->g
);
4256 isl_int_divexact(data
->fu
, bmap
->ineq
[u
][offset
+ i
], data
->g
);
4257 isl_int_neg(data
->fu
, data
->fu
);
4258 isl_seq_combine(data
->v
->el
, data
->fl
, bmap
->ineq
[u
],
4259 data
->fu
, bmap
->ineq
[l
], offset
+ n_div
);
4260 isl_int_mul(data
->g
, data
->g
, data
->fl
);
4261 isl_int_mul(data
->g
, data
->g
, data
->fu
);
4262 isl_int_sub(data
->g
, data
->g
, data
->fl
);
4263 isl_int_sub(data
->g
, data
->g
, data
->fu
);
4264 isl_int_add_ui(data
->g
, data
->g
, 1);
4265 isl_int_sub(data
->fl
, data
->v
->el
[0], data
->g
);
4267 isl_seq_gcd(data
->v
->el
+ 1, offset
- 1 + n_div
, &data
->g
);
4268 if (isl_int_is_zero(data
->g
))
4269 return isl_int_is_nonneg(data
->fl
);
4270 if (isl_int_is_one(data
->g
)) {
4271 isl_int_set(data
->v
->el
[0], data
->fl
);
4272 return test_ineq_is_satisfied(bmap
, data
);
4274 isl_int_fdiv_q(data
->fl
, data
->fl
, data
->g
);
4275 isl_int_fdiv_q(data
->v
->el
[0], data
->v
->el
[0], data
->g
);
4276 if (isl_int_eq(data
->fl
, data
->v
->el
[0]))
4277 return isl_bool_true
;
4278 isl_int_set(data
->v
->el
[0], data
->fl
);
4279 isl_seq_scale_down(data
->v
->el
+ 1, data
->v
->el
+ 1, data
->g
,
4280 offset
- 1 + n_div
);
4282 return test_ineq_is_satisfied(bmap
, data
);
4285 /* Remove more kinds of divs that are not strictly needed.
4286 * In particular, if all pairs of lower and upper bounds on a div
4287 * are such that they allow at least one integer value of the div,
4288 * then we can eliminate the div using Fourier-Motzkin without
4289 * introducing any spurious solutions.
4291 * If at least one of the two constraints has a unit coefficient for the div,
4292 * then the presence of such a value is guaranteed so there is no need to check.
4293 * In particular, the value attained by the bound with unit coefficient
4294 * can serve as this intermediate value.
4296 static struct isl_basic_map
*drop_more_redundant_divs(
4297 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4300 struct test_ineq_data data
= { NULL
, NULL
};
4301 unsigned off
, n_div
;
4304 isl_int_init(data
.g
);
4305 isl_int_init(data
.fl
);
4306 isl_int_init(data
.fu
);
4311 ctx
= isl_basic_map_get_ctx(bmap
);
4312 off
= isl_basic_map_offset(bmap
, isl_dim_div
);
4313 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4314 data
.v
= isl_vec_alloc(ctx
, off
+ n_div
);
4323 for (i
= 0; i
< n_div
; ++i
) {
4326 if (best
>= 0 && pairs
[best
] <= pairs
[i
])
4332 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4333 if (!isl_int_is_pos(bmap
->ineq
[l
][off
+ i
]))
4335 if (isl_int_is_one(bmap
->ineq
[l
][off
+ i
]))
4337 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4338 if (!isl_int_is_neg(bmap
->ineq
[u
][off
+ i
]))
4340 if (isl_int_is_negone(bmap
->ineq
[u
][off
+ i
]))
4342 has_int
= int_between_bounds(bmap
, i
, l
, u
,
4346 if (data
.tab
&& data
.tab
->empty
)
4351 if (u
< bmap
->n_ineq
)
4354 if (data
.tab
&& data
.tab
->empty
) {
4355 bmap
= isl_basic_map_set_to_empty(bmap
);
4358 if (l
== bmap
->n_ineq
) {
4366 test_ineq_data_clear(&data
);
4373 bmap
= isl_basic_map_remove_dims(bmap
, isl_dim_div
, remove
, 1);
4374 return isl_basic_map_drop_redundant_divs(bmap
);
4377 isl_basic_map_free(bmap
);
4378 test_ineq_data_clear(&data
);
4382 /* Given a pair of divs div1 and div2 such that, except for the lower bound l
4383 * and the upper bound u, div1 always occurs together with div2 in the form
4384 * (div1 + m div2), where m is the constant range on the variable div1
4385 * allowed by l and u, replace the pair div1 and div2 by a single
4386 * div that is equal to div1 + m div2.
4388 * The new div will appear in the location that contains div2.
4389 * We need to modify all constraints that contain
4390 * div2 = (div - div1) / m
4391 * The coefficient of div2 is known to be equal to 1 or -1.
4392 * (If a constraint does not contain div2, it will also not contain div1.)
4393 * If the constraint also contains div1, then we know they appear
4394 * as f (div1 + m div2) and we can simply replace (div1 + m div2) by div,
4395 * i.e., the coefficient of div is f.
4397 * Otherwise, we first need to introduce div1 into the constraint.
4406 * A lower bound on div2
4410 * can be replaced by
4412 * m div2 + div1 + m t + f >= 0
4418 * can be replaced by
4420 * -(m div2 + div1) + m t + f' >= 0
4422 * These constraint are those that we would obtain from eliminating
4423 * div1 using Fourier-Motzkin.
4425 * After all constraints have been modified, we drop the lower and upper
4426 * bound and then drop div1.
4428 static struct isl_basic_map
*coalesce_divs(struct isl_basic_map
*bmap
,
4429 unsigned div1
, unsigned div2
, unsigned l
, unsigned u
)
4433 unsigned dim
, total
;
4436 ctx
= isl_basic_map_get_ctx(bmap
);
4438 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4439 total
= 1 + dim
+ bmap
->n_div
;
4442 isl_int_add(m
, bmap
->ineq
[l
][0], bmap
->ineq
[u
][0]);
4443 isl_int_add_ui(m
, m
, 1);
4445 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
4446 if (i
== l
|| i
== u
)
4448 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div2
]))
4450 if (isl_int_is_zero(bmap
->ineq
[i
][1 + dim
+ div1
])) {
4451 if (isl_int_is_pos(bmap
->ineq
[i
][1 + dim
+ div2
]))
4452 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4453 ctx
->one
, bmap
->ineq
[l
], total
);
4455 isl_seq_combine(bmap
->ineq
[i
], m
, bmap
->ineq
[i
],
4456 ctx
->one
, bmap
->ineq
[u
], total
);
4458 isl_int_set(bmap
->ineq
[i
][1 + dim
+ div2
],
4459 bmap
->ineq
[i
][1 + dim
+ div1
]);
4460 isl_int_set_si(bmap
->ineq
[i
][1 + dim
+ div1
], 0);
4465 isl_basic_map_drop_inequality(bmap
, l
);
4466 isl_basic_map_drop_inequality(bmap
, u
);
4468 isl_basic_map_drop_inequality(bmap
, u
);
4469 isl_basic_map_drop_inequality(bmap
, l
);
4471 bmap
= isl_basic_map_drop_div(bmap
, div1
);
4475 /* First check if we can coalesce any pair of divs and
4476 * then continue with dropping more redundant divs.
4478 * We loop over all pairs of lower and upper bounds on a div
4479 * with coefficient 1 and -1, respectively, check if there
4480 * is any other div "c" with which we can coalesce the div
4481 * and if so, perform the coalescing.
4483 static struct isl_basic_map
*coalesce_or_drop_more_redundant_divs(
4484 struct isl_basic_map
*bmap
, int *pairs
, int n
)
4489 dim
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4491 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4494 for (l
= 0; l
< bmap
->n_ineq
; ++l
) {
4495 if (!isl_int_is_one(bmap
->ineq
[l
][1 + dim
+ i
]))
4497 for (u
= 0; u
< bmap
->n_ineq
; ++u
) {
4500 if (!isl_int_is_negone(bmap
->ineq
[u
][1+dim
+i
]))
4502 c
= div_find_coalesce(bmap
, pairs
, i
, l
, u
);
4506 bmap
= coalesce_divs(bmap
, i
, c
, l
, u
);
4507 return isl_basic_map_drop_redundant_divs(bmap
);
4512 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_EMPTY
))
4515 return drop_more_redundant_divs(bmap
, pairs
, n
);
4518 /* Are the "n" coefficients starting at "first" of inequality constraints
4519 * "i" and "j" of "bmap" equal to each other?
4521 static int is_parallel_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4524 return isl_seq_eq(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4527 /* Are the "n" coefficients starting at "first" of inequality constraints
4528 * "i" and "j" of "bmap" opposite to each other?
4530 static int is_opposite_part(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4533 return isl_seq_is_neg(bmap
->ineq
[i
] + first
, bmap
->ineq
[j
] + first
, n
);
4536 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4537 * apart from the constant term?
4539 static int is_opposite(__isl_keep isl_basic_map
*bmap
, int i
, int j
)
4543 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4544 return is_opposite_part(bmap
, i
, j
, 1, total
);
4547 /* Are inequality constraints "i" and "j" of "bmap" equal to each other,
4548 * apart from the constant term and the coefficient at position "pos"?
4550 static int is_parallel_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4555 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4556 return is_parallel_part(bmap
, i
, j
, 1, pos
- 1) &&
4557 is_parallel_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4560 /* Are inequality constraints "i" and "j" of "bmap" opposite to each other,
4561 * apart from the constant term and the coefficient at position "pos"?
4563 static int is_opposite_except(__isl_keep isl_basic_map
*bmap
, int i
, int j
,
4568 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
4569 return is_opposite_part(bmap
, i
, j
, 1, pos
- 1) &&
4570 is_opposite_part(bmap
, i
, j
, pos
+ 1, total
- pos
);
4573 /* Restart isl_basic_map_drop_redundant_divs after "bmap" has
4574 * been modified, simplying it if "simplify" is set.
4575 * Free the temporary data structure "pairs" that was associated
4576 * to the old version of "bmap".
4578 static __isl_give isl_basic_map
*drop_redundant_divs_again(
4579 __isl_take isl_basic_map
*bmap
, __isl_take
int *pairs
, int simplify
)
4582 bmap
= isl_basic_map_simplify(bmap
);
4584 return isl_basic_map_drop_redundant_divs(bmap
);
4587 /* Is "div" the single unknown existentially quantified variable
4588 * in inequality constraint "ineq" of "bmap"?
4589 * "div" is known to have a non-zero coefficient in "ineq".
4591 static int single_unknown(__isl_keep isl_basic_map
*bmap
, int ineq
, int div
)
4594 unsigned n_div
, o_div
;
4596 if (isl_basic_map_div_is_known(bmap
, div
))
4598 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4601 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4602 for (i
= 0; i
< n_div
; ++i
) {
4605 if (isl_int_is_zero(bmap
->ineq
[ineq
][o_div
+ i
]))
4607 if (!isl_basic_map_div_is_known(bmap
, i
))
4614 /* Does integer division "div" have coefficient 1 in inequality constraint
4617 static int has_coef_one(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4621 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4622 if (isl_int_is_one(bmap
->ineq
[ineq
][o_div
+ div
]))
4628 /* Turn inequality constraint "ineq" of "bmap" into an equality and
4629 * then try and drop redundant divs again,
4630 * freeing the temporary data structure "pairs" that was associated
4631 * to the old version of "bmap".
4633 static __isl_give isl_basic_map
*set_eq_and_try_again(
4634 __isl_take isl_basic_map
*bmap
, int ineq
, __isl_take
int *pairs
)
4636 bmap
= isl_basic_map_cow(bmap
);
4637 isl_basic_map_inequality_to_equality(bmap
, ineq
);
4638 return drop_redundant_divs_again(bmap
, pairs
, 1);
4641 /* Drop the integer division at position "div", along with the two
4642 * inequality constraints "ineq1" and "ineq2" in which it appears
4643 * from "bmap" and then try and drop redundant divs again,
4644 * freeing the temporary data structure "pairs" that was associated
4645 * to the old version of "bmap".
4647 static __isl_give isl_basic_map
*drop_div_and_try_again(
4648 __isl_take isl_basic_map
*bmap
, int div
, int ineq1
, int ineq2
,
4649 __isl_take
int *pairs
)
4651 if (ineq1
> ineq2
) {
4652 isl_basic_map_drop_inequality(bmap
, ineq1
);
4653 isl_basic_map_drop_inequality(bmap
, ineq2
);
4655 isl_basic_map_drop_inequality(bmap
, ineq2
);
4656 isl_basic_map_drop_inequality(bmap
, ineq1
);
4658 bmap
= isl_basic_map_drop_div(bmap
, div
);
4659 return drop_redundant_divs_again(bmap
, pairs
, 0);
4662 /* Given two inequality constraints
4664 * f(x) + n d + c >= 0, (ineq)
4666 * with d the variable at position "pos", and
4668 * f(x) + c0 >= 0, (lower)
4670 * compute the maximal value of the lower bound ceil((-f(x) - c)/n)
4671 * determined by the first constraint.
4678 static void lower_bound_from_parallel(__isl_keep isl_basic_map
*bmap
,
4679 int ineq
, int lower
, int pos
, isl_int
*l
)
4681 isl_int_neg(*l
, bmap
->ineq
[ineq
][0]);
4682 isl_int_add(*l
, *l
, bmap
->ineq
[lower
][0]);
4683 isl_int_cdiv_q(*l
, *l
, bmap
->ineq
[ineq
][pos
]);
4686 /* Given two inequality constraints
4688 * f(x) + n d + c >= 0, (ineq)
4690 * with d the variable at position "pos", and
4692 * -f(x) - c0 >= 0, (upper)
4694 * compute the minimal value of the lower bound ceil((-f(x) - c)/n)
4695 * determined by the first constraint.
4702 static void lower_bound_from_opposite(__isl_keep isl_basic_map
*bmap
,
4703 int ineq
, int upper
, int pos
, isl_int
*u
)
4705 isl_int_neg(*u
, bmap
->ineq
[ineq
][0]);
4706 isl_int_sub(*u
, *u
, bmap
->ineq
[upper
][0]);
4707 isl_int_cdiv_q(*u
, *u
, bmap
->ineq
[ineq
][pos
]);
4710 /* Given a lower bound constraint "ineq" on "div" in "bmap",
4711 * does the corresponding lower bound have a fixed value in "bmap"?
4713 * In particular, "ineq" is of the form
4715 * f(x) + n d + c >= 0
4717 * with n > 0, c the constant term and
4718 * d the existentially quantified variable "div".
4719 * That is, the lower bound is
4721 * ceil((-f(x) - c)/n)
4723 * Look for a pair of constraints
4728 * i.e., -c1 <= -f(x) <= c0, that fix ceil((-f(x) - c)/n) to a constant value.
4729 * That is, check that
4731 * ceil((-c1 - c)/n) = ceil((c0 - c)/n)
4733 * If so, return the index of inequality f(x) + c0 >= 0.
4734 * Otherwise, return -1.
4736 static int lower_bound_is_cst(__isl_keep isl_basic_map
*bmap
, int div
, int ineq
)
4739 int lower
= -1, upper
= -1;
4740 unsigned o_div
, n_div
;
4744 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
4745 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4746 for (i
= 0; i
< bmap
->n_ineq
&& (lower
< 0 || upper
< 0); ++i
) {
4749 if (!isl_int_is_zero(bmap
->ineq
[i
][o_div
+ div
]))
4752 is_parallel_except(bmap
, ineq
, i
, o_div
+ div
)) {
4757 is_opposite_except(bmap
, ineq
, i
, o_div
+ div
)) {
4762 if (lower
< 0 || upper
< 0)
4768 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &l
);
4769 lower_bound_from_opposite(bmap
, ineq
, upper
, o_div
+ div
, &u
);
4771 equal
= isl_int_eq(l
, u
);
4776 return equal
? lower
: -1;
4779 /* Given a lower bound constraint "ineq" on the existentially quantified
4780 * variable "div", such that the corresponding lower bound has
4781 * a fixed value in "bmap", assign this fixed value to the variable and
4782 * then try and drop redundant divs again,
4783 * freeing the temporary data structure "pairs" that was associated
4784 * to the old version of "bmap".
4785 * "lower" determines the constant value for the lower bound.
4787 * In particular, "ineq" is of the form
4789 * f(x) + n d + c >= 0,
4791 * while "lower" is of the form
4795 * The lower bound is ceil((-f(x) - c)/n) and its constant value
4796 * is ceil((c0 - c)/n).
4798 static __isl_give isl_basic_map
*fix_cst_lower(__isl_take isl_basic_map
*bmap
,
4799 int div
, int ineq
, int lower
, int *pairs
)
4806 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
4807 lower_bound_from_parallel(bmap
, ineq
, lower
, o_div
+ div
, &c
);
4808 bmap
= isl_basic_map_fix(bmap
, isl_dim_div
, div
, c
);
4813 return isl_basic_map_drop_redundant_divs(bmap
);
4816 /* Remove divs that are not strictly needed based on the inequality
4818 * In particular, if a div only occurs positively (or negatively)
4819 * in constraints, then it can simply be dropped.
4820 * Also, if a div occurs in only two constraints and if moreover
4821 * those two constraints are opposite to each other, except for the constant
4822 * term and if the sum of the constant terms is such that for any value
4823 * of the other values, there is always at least one integer value of the
4824 * div, i.e., if one plus this sum is greater than or equal to
4825 * the (absolute value) of the coefficient of the div in the constraints,
4826 * then we can also simply drop the div.
4828 * If an existentially quantified variable does not have an explicit
4829 * representation, appears in only a single lower bound that does not
4830 * involve any other such existentially quantified variables and appears
4831 * in this lower bound with coefficient 1,
4832 * then fix the variable to the value of the lower bound. That is,
4833 * turn the inequality into an equality.
4834 * If for any value of the other variables, there is any value
4835 * for the existentially quantified variable satisfying the constraints,
4836 * then this lower bound also satisfies the constraints.
4837 * It is therefore safe to pick this lower bound.
4839 * The same reasoning holds even if the coefficient is not one.
4840 * However, fixing the variable to the value of the lower bound may
4841 * in general introduce an extra integer division, in which case
4842 * it may be better to pick another value.
4843 * If this integer division has a known constant value, then plugging
4844 * in this constant value removes the existentially quantified variable
4845 * completely. In particular, if the lower bound is of the form
4846 * ceil((-f(x) - c)/n) and there are two constraints, f(x) + c0 >= 0 and
4847 * -f(x) + c1 >= 0 such that ceil((-c1 - c)/n) = ceil((c0 - c)/n),
4848 * then the existentially quantified variable can be assigned this
4851 * We skip divs that appear in equalities or in the definition of other divs.
4852 * Divs that appear in the definition of other divs usually occur in at least
4853 * 4 constraints, but the constraints may have been simplified.
4855 * If any divs are left after these simple checks then we move on
4856 * to more complicated cases in drop_more_redundant_divs.
4858 static __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs_ineq(
4859 __isl_take isl_basic_map
*bmap
)
4868 if (bmap
->n_div
== 0)
4871 off
= isl_space_dim(bmap
->dim
, isl_dim_all
);
4872 pairs
= isl_calloc_array(bmap
->ctx
, int, bmap
->n_div
);
4876 for (i
= 0; i
< bmap
->n_div
; ++i
) {
4878 int last_pos
, last_neg
;
4882 defined
= !isl_int_is_zero(bmap
->div
[i
][0]);
4883 for (j
= i
; j
< bmap
->n_div
; ++j
)
4884 if (!isl_int_is_zero(bmap
->div
[j
][1 + 1 + off
+ i
]))
4886 if (j
< bmap
->n_div
)
4888 for (j
= 0; j
< bmap
->n_eq
; ++j
)
4889 if (!isl_int_is_zero(bmap
->eq
[j
][1 + off
+ i
]))
4895 for (j
= 0; j
< bmap
->n_ineq
; ++j
) {
4896 if (isl_int_is_pos(bmap
->ineq
[j
][1 + off
+ i
])) {
4900 if (isl_int_is_neg(bmap
->ineq
[j
][1 + off
+ i
])) {
4905 pairs
[i
] = pos
* neg
;
4906 if (pairs
[i
] == 0) {
4907 for (j
= bmap
->n_ineq
- 1; j
>= 0; --j
)
4908 if (!isl_int_is_zero(bmap
->ineq
[j
][1+off
+i
]))
4909 isl_basic_map_drop_inequality(bmap
, j
);
4910 bmap
= isl_basic_map_drop_div(bmap
, i
);
4911 return drop_redundant_divs_again(bmap
, pairs
, 0);
4913 if (pairs
[i
] != 1 || !is_opposite(bmap
, last_pos
, last_neg
)) {
4917 single
= single_unknown(bmap
, last_pos
, i
);
4920 if (has_coef_one(bmap
, i
, last_pos
))
4921 return set_eq_and_try_again(bmap
, last_pos
,
4923 lower
= lower_bound_is_cst(bmap
, i
, last_pos
);
4925 return fix_cst_lower(bmap
, i
, last_pos
, lower
,
4930 isl_int_add(bmap
->ineq
[last_pos
][0],
4931 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4932 isl_int_add_ui(bmap
->ineq
[last_pos
][0],
4933 bmap
->ineq
[last_pos
][0], 1);
4934 redundant
= isl_int_ge(bmap
->ineq
[last_pos
][0],
4935 bmap
->ineq
[last_pos
][1+off
+i
]);
4936 isl_int_sub_ui(bmap
->ineq
[last_pos
][0],
4937 bmap
->ineq
[last_pos
][0], 1);
4938 isl_int_sub(bmap
->ineq
[last_pos
][0],
4939 bmap
->ineq
[last_pos
][0], bmap
->ineq
[last_neg
][0]);
4941 return drop_div_and_try_again(bmap
, i
,
4942 last_pos
, last_neg
, pairs
);
4943 if (!defined
&& ok_to_set_div_from_bound(bmap
, i
, last_pos
)) {
4944 bmap
= set_div_from_lower_bound(bmap
, i
, last_pos
);
4945 return drop_redundant_divs_again(bmap
, pairs
, 1);
4952 return coalesce_or_drop_more_redundant_divs(bmap
, pairs
, n
);
4958 isl_basic_map_free(bmap
);
4962 /* Consider the coefficients at "c" as a row vector and replace
4963 * them with their product with "T". "T" is assumed to be a square matrix.
4965 static isl_stat
preimage(isl_int
*c
, __isl_keep isl_mat
*T
)
4972 return isl_stat_error
;
4973 n
= isl_mat_rows(T
);
4974 if (isl_seq_first_non_zero(c
, n
) == -1)
4976 ctx
= isl_mat_get_ctx(T
);
4977 v
= isl_vec_alloc(ctx
, n
);
4979 return isl_stat_error
;
4980 isl_seq_swp_or_cpy(v
->el
, c
, n
);
4981 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
4983 return isl_stat_error
;
4984 isl_seq_swp_or_cpy(c
, v
->el
, n
);
4990 /* Plug in T for the variables in "bmap" starting at "pos".
4991 * T is a linear unimodular matrix, i.e., without constant term.
4993 static __isl_give isl_basic_map
*isl_basic_map_preimage_vars(
4994 __isl_take isl_basic_map
*bmap
, unsigned pos
, __isl_take isl_mat
*T
)
4999 bmap
= isl_basic_map_cow(bmap
);
5003 n
= isl_mat_cols(T
);
5004 if (n
!= isl_mat_rows(T
))
5005 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5006 "expecting square matrix", goto error
);
5008 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5009 if (pos
+ n
> total
|| pos
+ n
< pos
)
5010 isl_die(isl_mat_get_ctx(T
), isl_error_invalid
,
5011 "invalid range", goto error
);
5013 for (i
= 0; i
< bmap
->n_eq
; ++i
)
5014 if (preimage(bmap
->eq
[i
] + 1 + pos
, T
) < 0)
5016 for (i
= 0; i
< bmap
->n_ineq
; ++i
)
5017 if (preimage(bmap
->ineq
[i
] + 1 + pos
, T
) < 0)
5019 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5020 if (isl_basic_map_div_is_marked_unknown(bmap
, i
))
5022 if (preimage(bmap
->div
[i
] + 1 + 1 + pos
, T
) < 0)
5029 isl_basic_map_free(bmap
);
5034 /* Remove divs that are not strictly needed.
5036 * First look for an equality constraint involving two or more
5037 * existentially quantified variables without an explicit
5038 * representation. Replace the combination that appears
5039 * in the equality constraint by a single existentially quantified
5040 * variable such that the equality can be used to derive
5041 * an explicit representation for the variable.
5042 * If there are no more such equality constraints, then continue
5043 * with isl_basic_map_drop_redundant_divs_ineq.
5045 * In particular, if the equality constraint is of the form
5047 * f(x) + \sum_i c_i a_i = 0
5049 * with a_i existentially quantified variable without explicit
5050 * representation, then apply a transformation on the existentially
5051 * quantified variables to turn the constraint into
5055 * with g the gcd of the c_i.
5056 * In order to easily identify which existentially quantified variables
5057 * have a complete explicit representation, i.e., without being defined
5058 * in terms of other existentially quantified variables without
5059 * an explicit representation, the existentially quantified variables
5062 * The variable transformation is computed by extending the row
5063 * [c_1/g ... c_n/g] to a unimodular matrix, obtaining the transformation
5065 * [a_1'] [c_1/g ... c_n/g] [ a_1 ]
5070 * with [c_1/g ... c_n/g] representing the first row of U.
5071 * The inverse of U is then plugged into the original constraints.
5072 * The call to isl_basic_map_simplify makes sure the explicit
5073 * representation for a_1' is extracted from the equality constraint.
5075 __isl_give isl_basic_map
*isl_basic_map_drop_redundant_divs(
5076 __isl_take isl_basic_map
*bmap
)
5080 unsigned o_div
, n_div
;
5087 if (isl_basic_map_divs_known(bmap
))
5088 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5089 if (bmap
->n_eq
== 0)
5090 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5091 bmap
= isl_basic_map_sort_divs(bmap
);
5095 first
= isl_basic_map_first_unknown_div(bmap
);
5097 return isl_basic_map_free(bmap
);
5099 o_div
= isl_basic_map_offset(bmap
, isl_dim_div
);
5100 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
5102 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5103 l
= isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ first
,
5108 if (isl_seq_first_non_zero(bmap
->eq
[i
] + o_div
+ l
+ 1,
5109 n_div
- (l
+ 1)) == -1)
5113 if (i
>= bmap
->n_eq
)
5114 return isl_basic_map_drop_redundant_divs_ineq(bmap
);
5116 ctx
= isl_basic_map_get_ctx(bmap
);
5117 T
= isl_mat_alloc(ctx
, n_div
- l
, n_div
- l
);
5119 return isl_basic_map_free(bmap
);
5120 isl_seq_cpy(T
->row
[0], bmap
->eq
[i
] + o_div
+ l
, n_div
- l
);
5121 T
= isl_mat_normalize_row(T
, 0);
5122 T
= isl_mat_unimodular_complete(T
, 1);
5123 T
= isl_mat_right_inverse(T
);
5125 for (i
= l
; i
< n_div
; ++i
)
5126 bmap
= isl_basic_map_mark_div_unknown(bmap
, i
);
5127 bmap
= isl_basic_map_preimage_vars(bmap
, o_div
- 1 + l
, T
);
5128 bmap
= isl_basic_map_simplify(bmap
);
5130 return isl_basic_map_drop_redundant_divs(bmap
);
5133 struct isl_basic_set
*isl_basic_set_drop_redundant_divs(
5134 struct isl_basic_set
*bset
)
5136 isl_basic_map
*bmap
= bset_to_bmap(bset
);
5137 return bset_from_bmap(isl_basic_map_drop_redundant_divs(bmap
));
5140 struct isl_map
*isl_map_drop_redundant_divs(struct isl_map
*map
)
5146 for (i
= 0; i
< map
->n
; ++i
) {
5147 map
->p
[i
] = isl_basic_map_drop_redundant_divs(map
->p
[i
]);
5151 ISL_F_CLR(map
, ISL_MAP_NORMALIZED
);
5158 struct isl_set
*isl_set_drop_redundant_divs(struct isl_set
*set
)
5160 return set_from_map(isl_map_drop_redundant_divs(set_to_map(set
)));
5163 /* Does "bmap" satisfy any equality that involves more than 2 variables
5164 * and/or has coefficients different from -1 and 1?
5166 static int has_multiple_var_equality(__isl_keep isl_basic_map
*bmap
)
5171 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5173 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5176 j
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1, total
);
5179 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5180 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5184 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5188 if (!isl_int_is_one(bmap
->eq
[i
][1 + j
]) &&
5189 !isl_int_is_negone(bmap
->eq
[i
][1 + j
]))
5193 k
= isl_seq_first_non_zero(bmap
->eq
[i
] + 1 + j
, total
- j
);
5201 /* Remove any common factor g from the constraint coefficients in "v".
5202 * The constant term is stored in the first position and is replaced
5203 * by floor(c/g). If any common factor is removed and if this results
5204 * in a tightening of the constraint, then set *tightened.
5206 static __isl_give isl_vec
*normalize_constraint(__isl_take isl_vec
*v
,
5213 ctx
= isl_vec_get_ctx(v
);
5214 isl_seq_gcd(v
->el
+ 1, v
->size
- 1, &ctx
->normalize_gcd
);
5215 if (isl_int_is_zero(ctx
->normalize_gcd
))
5217 if (isl_int_is_one(ctx
->normalize_gcd
))
5222 if (tightened
&& !isl_int_is_divisible_by(v
->el
[0], ctx
->normalize_gcd
))
5224 isl_int_fdiv_q(v
->el
[0], v
->el
[0], ctx
->normalize_gcd
);
5225 isl_seq_scale_down(v
->el
+ 1, v
->el
+ 1, ctx
->normalize_gcd
,
5230 /* If "bmap" is an integer set that satisfies any equality involving
5231 * more than 2 variables and/or has coefficients different from -1 and 1,
5232 * then use variable compression to reduce the coefficients by removing
5233 * any (hidden) common factor.
5234 * In particular, apply the variable compression to each constraint,
5235 * factor out any common factor in the non-constant coefficients and
5236 * then apply the inverse of the compression.
5237 * At the end, we mark the basic map as having reduced constants.
5238 * If this flag is still set on the next invocation of this function,
5239 * then we skip the computation.
5241 * Removing a common factor may result in a tightening of some of
5242 * the constraints. If this happens, then we may end up with two
5243 * opposite inequalities that can be replaced by an equality.
5244 * We therefore call isl_basic_map_detect_inequality_pairs,
5245 * which checks for such pairs of inequalities as well as eliminate_divs_eq
5246 * and isl_basic_map_gauss if such a pair was found.
5248 __isl_give isl_basic_map
*isl_basic_map_reduce_coefficients(
5249 __isl_take isl_basic_map
*bmap
)
5254 isl_mat
*eq
, *T
, *T2
;
5260 if (ISL_F_ISSET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
))
5262 if (isl_basic_map_is_rational(bmap
))
5264 if (bmap
->n_eq
== 0)
5266 if (!has_multiple_var_equality(bmap
))
5269 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5270 ctx
= isl_basic_map_get_ctx(bmap
);
5271 v
= isl_vec_alloc(ctx
, 1 + total
);
5273 return isl_basic_map_free(bmap
);
5275 eq
= isl_mat_sub_alloc6(ctx
, bmap
->eq
, 0, bmap
->n_eq
, 0, 1 + total
);
5276 T
= isl_mat_variable_compression(eq
, &T2
);
5279 if (T
->n_col
== 0) {
5283 return isl_basic_map_set_to_empty(bmap
);
5287 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5288 isl_seq_cpy(v
->el
, bmap
->ineq
[i
], 1 + total
);
5289 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
5290 v
= normalize_constraint(v
, &tightened
);
5291 v
= isl_vec_mat_product(v
, isl_mat_copy(T2
));
5294 isl_seq_cpy(bmap
->ineq
[i
], v
->el
, 1 + total
);
5301 ISL_F_SET(bmap
, ISL_BASIC_MAP_REDUCED_COEFFICIENTS
);
5306 bmap
= isl_basic_map_detect_inequality_pairs(bmap
, &progress
);
5308 bmap
= eliminate_divs_eq(bmap
, &progress
);
5309 bmap
= isl_basic_map_gauss(bmap
, NULL
);
5318 return isl_basic_map_free(bmap
);
5321 /* Shift the integer division at position "div" of "bmap"
5322 * by "shift" times the variable at position "pos".
5323 * "pos" is as determined by isl_basic_map_offset, i.e., pos == 0
5324 * corresponds to the constant term.
5326 * That is, if the integer division has the form
5330 * then replace it by
5332 * floor((f(x) + shift * d * x_pos)/d) - shift * x_pos
5334 __isl_give isl_basic_map
*isl_basic_map_shift_div(
5335 __isl_take isl_basic_map
*bmap
, int div
, int pos
, isl_int shift
)
5340 if (isl_int_is_zero(shift
))
5345 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
5346 total
-= isl_basic_map_dim(bmap
, isl_dim_div
);
5348 isl_int_addmul(bmap
->div
[div
][1 + pos
], shift
, bmap
->div
[div
][0]);
5350 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
5351 if (isl_int_is_zero(bmap
->eq
[i
][1 + total
+ div
]))
5353 isl_int_submul(bmap
->eq
[i
][pos
],
5354 shift
, bmap
->eq
[i
][1 + total
+ div
]);
5356 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
5357 if (isl_int_is_zero(bmap
->ineq
[i
][1 + total
+ div
]))
5359 isl_int_submul(bmap
->ineq
[i
][pos
],
5360 shift
, bmap
->ineq
[i
][1 + total
+ div
]);
5362 for (i
= 0; i
< bmap
->n_div
; ++i
) {
5363 if (isl_int_is_zero(bmap
->div
[i
][0]))
5365 if (isl_int_is_zero(bmap
->div
[i
][1 + 1 + total
+ div
]))
5367 isl_int_submul(bmap
->div
[i
][1 + pos
],
5368 shift
, bmap
->div
[i
][1 + 1 + total
+ div
]);