2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_map_private.h>
14 #include <isl_constraint_private.h>
15 #include <isl_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 #include <bset_to_bmap.c>
23 #include <bset_from_bmap.c>
26 #define EL_BASE constraint
28 #include <isl_list_templ.c>
30 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
32 return c
? isl_local_space_get_ctx(c
->ls
) : NULL
;
35 static isl_size
n(__isl_keep isl_constraint
*c
, enum isl_dim_type type
)
37 return isl_local_space_dim(c
->ls
, type
);
40 static unsigned offset(__isl_keep isl_constraint
*c
, enum isl_dim_type type
)
42 return isl_local_space_offset(c
->ls
, type
);
45 __isl_give isl_constraint
*isl_constraint_alloc_vec(int eq
,
46 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
48 isl_constraint
*constraint
;
53 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
64 isl_local_space_free(ls
);
69 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
70 __isl_take isl_local_space
*ls
)
76 dim
= isl_local_space_dim(ls
, isl_dim_all
);
78 ls
= isl_local_space_free(ls
);
82 ctx
= isl_local_space_get_ctx(ls
);
83 v
= isl_vec_alloc(ctx
, 1 + dim
);
85 return isl_constraint_alloc_vec(eq
, ls
, v
);
88 __isl_give isl_constraint
*isl_basic_map_constraint(
89 __isl_take isl_basic_map
*bmap
, isl_int
**line
)
95 isl_local_space
*ls
= NULL
;
96 isl_constraint
*constraint
;
101 eq
= line
>= bmap
->eq
;
103 ctx
= isl_basic_map_get_ctx(bmap
);
104 ls
= isl_basic_map_get_local_space(bmap
);
105 dim
= isl_local_space_dim(ls
, isl_dim_all
);
108 v
= isl_vec_alloc(ctx
, 1 + dim
);
111 isl_seq_cpy(v
->el
, line
[0], v
->size
);
112 constraint
= isl_constraint_alloc_vec(eq
, ls
, v
);
114 isl_basic_map_free(bmap
);
117 isl_local_space_free(ls
);
118 isl_basic_map_free(bmap
);
122 __isl_give isl_constraint
*isl_basic_set_constraint(
123 __isl_take isl_basic_set
*bset
, isl_int
**line
)
125 return isl_basic_map_constraint(bset_to_bmap(bset
), line
);
128 __isl_give isl_constraint
*isl_constraint_alloc_equality(
129 __isl_take isl_local_space
*ls
)
131 return isl_constraint_alloc(1, ls
);
134 __isl_give isl_constraint
*isl_constraint_alloc_inequality(
135 __isl_take isl_local_space
*ls
)
137 return isl_constraint_alloc(0, ls
);
140 __isl_give isl_constraint
*isl_constraint_dup(__isl_keep isl_constraint
*c
)
145 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
149 __isl_give isl_constraint
*isl_constraint_cow(__isl_take isl_constraint
*c
)
157 return isl_constraint_dup(c
);
160 __isl_give isl_constraint
*isl_constraint_copy(
161 __isl_keep isl_constraint
*constraint
)
170 __isl_null isl_constraint
*isl_constraint_free(__isl_take isl_constraint
*c
)
178 isl_local_space_free(c
->ls
);
185 /* Return the number of constraints in "bmap", i.e., the
186 * number of times isl_basic_map_foreach_constraint will
189 isl_size
isl_basic_map_n_constraint(__isl_keep isl_basic_map
*bmap
)
192 return isl_size_error
;
194 return bmap
->n_eq
+ bmap
->n_ineq
;
197 /* Return the number of constraints in "bset", i.e., the
198 * number of times isl_basic_set_foreach_constraint will
201 isl_size
isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
203 return isl_basic_map_n_constraint(bset
);
206 isl_stat
isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
207 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
210 struct isl_constraint
*c
;
213 return isl_stat_error
;
215 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
216 return isl_stat_error
);
218 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
219 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
222 return isl_stat_error
;
224 return isl_stat_error
;
227 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
228 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
231 return isl_stat_error
;
233 return isl_stat_error
;
239 isl_stat
isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
240 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
242 return isl_basic_map_foreach_constraint(bset_to_bmap(bset
), fn
, user
);
245 /* Add the constraint to the list that "user" points to, if it is not
248 static isl_stat
collect_constraint(__isl_take isl_constraint
*constraint
,
251 isl_constraint_list
**list
= user
;
254 is_div
= isl_constraint_is_div_constraint(constraint
);
255 if (is_div
< 0 || is_div
)
256 isl_constraint_free(constraint
);
258 *list
= isl_constraint_list_add(*list
, constraint
);
260 return is_div
< 0 ? isl_stat_error
: isl_stat_ok
;
263 /* Return a list of constraints that, when combined, are equivalent
264 * to "bmap". The input is required to have only known divs.
266 * There is no need to include the div constraints as they are
267 * implied by the div expressions.
269 __isl_give isl_constraint_list
*isl_basic_map_get_constraint_list(
270 __isl_keep isl_basic_map
*bmap
)
275 isl_constraint_list
*list
;
277 known
= isl_basic_map_divs_known(bmap
);
280 ctx
= isl_basic_map_get_ctx(bmap
);
282 isl_die(ctx
, isl_error_invalid
,
283 "input involves unknown divs", return NULL
);
285 n
= isl_basic_map_n_constraint(bmap
);
288 list
= isl_constraint_list_alloc(ctx
, n
);
289 if (isl_basic_map_foreach_constraint(bmap
,
290 &collect_constraint
, &list
) < 0)
291 list
= isl_constraint_list_free(list
);
296 /* Return a list of constraints that, when combined, are equivalent
297 * to "bset". The input is required to have only known divs.
299 __isl_give isl_constraint_list
*isl_basic_set_get_constraint_list(
300 __isl_keep isl_basic_set
*bset
)
302 return isl_basic_map_get_constraint_list(bset
);
305 int isl_constraint_is_equal(__isl_keep isl_constraint
*constraint1
,
306 __isl_keep isl_constraint
*constraint2
)
310 if (!constraint1
|| !constraint2
)
312 if (constraint1
->eq
!= constraint2
->eq
)
314 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
315 if (equal
< 0 || !equal
)
317 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
320 __isl_give isl_basic_map
*isl_basic_map_add_constraint(
321 __isl_take isl_basic_map
*bmap
, __isl_take isl_constraint
*constraint
)
327 if (!bmap
|| !constraint
)
330 ctx
= isl_constraint_get_ctx(constraint
);
331 space
= isl_constraint_get_space(constraint
);
332 equal_space
= isl_space_is_equal(bmap
->dim
, space
);
333 isl_space_free(space
);
334 isl_assert(ctx
, equal_space
, goto error
);
336 bmap
= isl_basic_map_intersect(bmap
,
337 isl_basic_map_from_constraint(constraint
));
340 isl_basic_map_free(bmap
);
341 isl_constraint_free(constraint
);
345 __isl_give isl_basic_set
*isl_basic_set_add_constraint(
346 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
348 return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset
),
352 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
353 __isl_take isl_constraint
*constraint
)
357 bmap
= isl_basic_map_from_constraint(constraint
);
358 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
363 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
364 __isl_take isl_constraint
*constraint
)
366 return isl_map_add_constraint(set
, constraint
);
369 /* Return the space of "constraint".
371 static __isl_keep isl_space
*isl_constraint_peek_space(
372 __isl_keep isl_constraint
*constraint
)
374 return constraint
? isl_local_space_peek_space(constraint
->ls
) : NULL
;
377 __isl_give isl_space
*isl_constraint_get_space(
378 __isl_keep isl_constraint
*constraint
)
380 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
383 __isl_give isl_local_space
*isl_constraint_get_local_space(
384 __isl_keep isl_constraint
*constraint
)
386 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
389 isl_size
isl_constraint_dim(__isl_keep isl_constraint
*constraint
,
390 enum isl_dim_type type
)
393 return isl_size_error
;
394 return n(constraint
, type
);
398 #define TYPE isl_constraint
400 #include "check_type_range_templ.c"
402 isl_bool
isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
403 enum isl_dim_type type
, unsigned first
, unsigned n
)
407 isl_bool involves
= isl_bool_false
;
410 return isl_bool_error
;
412 return isl_bool_false
;
414 if (isl_constraint_check_range(constraint
, type
, first
, n
) < 0)
415 return isl_bool_error
;
417 active
= isl_local_space_get_active(constraint
->ls
,
418 constraint
->v
->el
+ 1);
422 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
423 for (i
= 0; i
< n
; ++i
)
424 if (active
[first
+ i
]) {
425 involves
= isl_bool_true
;
434 return isl_bool_error
;
437 /* Does the given constraint represent a lower bound on the given
440 isl_bool
isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
441 enum isl_dim_type type
, unsigned pos
)
443 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
444 return isl_bool_error
;
446 pos
+= isl_local_space_offset(constraint
->ls
, type
);
447 return isl_bool_ok(isl_int_is_pos(constraint
->v
->el
[pos
]));
450 /* Does the given constraint represent an upper bound on the given
453 isl_bool
isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
454 enum isl_dim_type type
, unsigned pos
)
456 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
457 return isl_bool_error
;
459 pos
+= isl_local_space_offset(constraint
->ls
, type
);
460 return isl_bool_ok(isl_int_is_neg(constraint
->v
->el
[pos
]));
463 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
464 enum isl_dim_type type
, unsigned pos
)
467 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
470 void isl_constraint_get_constant(__isl_keep isl_constraint
*constraint
,
475 isl_int_set(*v
, constraint
->v
->el
[0]);
478 /* Return the constant term of "constraint".
480 __isl_give isl_val
*isl_constraint_get_constant_val(
481 __isl_keep isl_constraint
*constraint
)
488 ctx
= isl_constraint_get_ctx(constraint
);
489 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
492 void isl_constraint_get_coefficient(__isl_keep isl_constraint
*constraint
,
493 enum isl_dim_type type
, int pos
, isl_int
*v
)
495 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
498 pos
+= isl_local_space_offset(constraint
->ls
, type
);
499 isl_int_set(*v
, constraint
->v
->el
[pos
]);
502 /* Return the coefficient of the variable of type "type" at position "pos"
505 __isl_give isl_val
*isl_constraint_get_coefficient_val(
506 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
510 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
513 ctx
= isl_constraint_get_ctx(constraint
);
514 pos
+= isl_local_space_offset(constraint
->ls
, type
);
515 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
518 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
524 return isl_local_space_get_div(constraint
->ls
, pos
);
527 __isl_give isl_constraint
*isl_constraint_set_constant(
528 __isl_take isl_constraint
*constraint
, isl_int v
)
530 constraint
= isl_constraint_cow(constraint
);
534 constraint
->v
= isl_vec_cow(constraint
->v
);
536 return isl_constraint_free(constraint
);
538 isl_int_set(constraint
->v
->el
[0], v
);
542 /* Replace the constant term of "constraint" by "v".
544 __isl_give isl_constraint
*isl_constraint_set_constant_val(
545 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
547 constraint
= isl_constraint_cow(constraint
);
548 if (!constraint
|| !v
)
550 if (!isl_val_is_int(v
))
551 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
552 "expecting integer value", goto error
);
553 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
555 constraint
= isl_constraint_free(constraint
);
559 return isl_constraint_free(constraint
);
562 __isl_give isl_constraint
*isl_constraint_set_constant_si(
563 __isl_take isl_constraint
*constraint
, int v
)
565 constraint
= isl_constraint_cow(constraint
);
569 constraint
->v
= isl_vec_cow(constraint
->v
);
571 return isl_constraint_free(constraint
);
573 isl_int_set_si(constraint
->v
->el
[0], v
);
577 /* Replace the coefficient of the variable of type "type" at position "pos"
578 * of "constraint" by "v".
580 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
581 __isl_take isl_constraint
*constraint
,
582 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
584 constraint
= isl_constraint_cow(constraint
);
585 if (!constraint
|| !v
)
587 if (!isl_val_is_int(v
))
588 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
589 "expecting integer value", goto error
);
590 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
593 pos
+= isl_local_space_offset(constraint
->ls
, type
);
594 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
596 constraint
= isl_constraint_free(constraint
);
600 return isl_constraint_free(constraint
);
603 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
604 __isl_take isl_constraint
*constraint
,
605 enum isl_dim_type type
, int pos
, int v
)
607 constraint
= isl_constraint_cow(constraint
);
608 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
609 return isl_constraint_free(constraint
);
611 constraint
->v
= isl_vec_cow(constraint
->v
);
613 return isl_constraint_free(constraint
);
615 pos
+= isl_local_space_offset(constraint
->ls
, type
);
616 isl_int_set_si(constraint
->v
->el
[pos
], v
);
621 __isl_give isl_constraint
*isl_constraint_negate(
622 __isl_take isl_constraint
*constraint
)
626 constraint
= isl_constraint_cow(constraint
);
630 ctx
= isl_constraint_get_ctx(constraint
);
631 if (isl_constraint_is_equality(constraint
))
632 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
633 return isl_constraint_free(constraint
));
634 constraint
->v
= isl_vec_neg(constraint
->v
);
635 constraint
->v
= isl_vec_cow(constraint
->v
);
637 return isl_constraint_free(constraint
);
638 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
642 isl_bool
isl_constraint_is_equality(struct isl_constraint
*constraint
)
645 return isl_bool_error
;
646 return isl_bool_ok(constraint
->eq
);
649 isl_bool
isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
655 return isl_bool_error
;
656 if (isl_constraint_is_equality(constraint
))
657 return isl_bool_false
;
658 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
660 return isl_bool_error
;
661 for (i
= 0; i
< n_div
; ++i
) {
663 is_div
= isl_local_space_is_div_constraint(constraint
->ls
,
664 constraint
->v
->el
, i
);
665 if (is_div
< 0 || is_div
)
669 return isl_bool_false
;
672 /* Is "constraint" an equality that corresponds to integer division "div"?
674 * That is, given an integer division of the form
676 * a = floor((f + c)/m)
678 * is the equality of the form
682 * Note that the constant term is not checked explicitly, but given
683 * that this is a valid equality constraint, the constant c' necessarily
684 * has a value close to -c.
686 isl_bool
isl_constraint_is_div_equality(__isl_keep isl_constraint
*constraint
,
691 equality
= isl_constraint_is_equality(constraint
);
692 if (equality
< 0 || !equality
)
694 return isl_local_space_is_div_equality(constraint
->ls
,
695 constraint
->v
->el
, div
);
698 /* We manually set ISL_BASIC_SET_FINAL instead of calling
699 * isl_basic_map_finalize because we want to keep the position
700 * of the divs and we therefore do not want to throw away redundant divs.
701 * This is arguably a bit fragile.
703 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
704 __isl_take isl_constraint
*constraint
)
708 struct isl_basic_map
*bmap
;
715 ls
= isl_local_space_copy(constraint
->ls
);
716 bmap
= isl_basic_map_from_local_space(ls
);
717 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
718 if (isl_constraint_is_equality(constraint
)) {
719 k
= isl_basic_map_alloc_equality(bmap
);
725 k
= isl_basic_map_alloc_inequality(bmap
);
730 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
733 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
734 isl_constraint_free(constraint
);
736 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
739 isl_constraint_free(constraint
);
740 isl_basic_map_free(bmap
);
744 __isl_give isl_basic_set
*isl_basic_set_from_constraint(
745 __isl_take isl_constraint
*constraint
)
749 space
= isl_constraint_peek_space(constraint
);
750 if (isl_space_check_is_set(space
) < 0)
752 return bset_from_bmap(isl_basic_map_from_constraint(constraint
));
754 isl_constraint_free(constraint
);
758 /* Is the variable of "type" at position "pos" of "bmap" defined
759 * in terms of earlier dimensions through an equality?
761 * If so, and if c is not NULL, then return a copy of this equality in *c.
763 isl_bool
isl_basic_map_has_defining_equality(
764 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
765 __isl_give isl_constraint
**c
)
771 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
772 return isl_bool_error
;
773 offset
= isl_basic_map_offset(bmap
, type
);
774 total
= isl_basic_map_dim(bmap
, isl_dim_all
);
776 return isl_bool_error
;
777 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
778 if (isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) ||
779 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
780 1+total
-offset
-pos
-1) != -1)
783 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
785 return isl_bool_true
;
787 return isl_bool_false
;
790 /* Is the variable of "type" at position "pos" of "bset" defined
791 * in terms of earlier dimensions through an equality?
793 * If so, and if c is not NULL, then return a copy of this equality in *c.
795 isl_bool
isl_basic_set_has_defining_equality(
796 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
797 __isl_give isl_constraint
**c
)
799 return isl_basic_map_has_defining_equality(bset_to_bmap(bset
),
803 isl_bool
isl_basic_set_has_defining_inequalities(
804 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
805 struct isl_constraint
**lower
,
806 struct isl_constraint
**upper
)
812 isl_int
**lower_line
, **upper_line
;
814 if (isl_basic_set_check_range(bset
, type
, pos
, 1) < 0)
815 return isl_bool_error
;
816 offset
= isl_basic_set_offset(bset
, type
);
817 total
= isl_basic_set_dim(bset
, isl_dim_all
);
819 return isl_bool_error
;
821 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
822 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
824 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
826 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
828 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
829 1+total
-offset
-pos
-1) != -1)
831 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
832 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
835 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
836 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
839 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
840 lower_line
= &bset
->ineq
[i
];
841 upper_line
= &bset
->ineq
[j
];
843 lower_line
= &bset
->ineq
[j
];
844 upper_line
= &bset
->ineq
[i
];
846 *lower
= isl_basic_set_constraint(
847 isl_basic_set_copy(bset
), lower_line
);
848 *upper
= isl_basic_set_constraint(
849 isl_basic_set_copy(bset
), upper_line
);
851 return isl_bool_true
;
857 return isl_bool_false
;
860 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
861 * (in "a" and "b"), add a constraint to "bset" that ensures that the
862 * bound implied by "a" is (strictly) larger than the bound implied by "b".
864 * If both constraints imply lower bounds, then this means that "a" is
865 * active in the result.
866 * If both constraints imply upper bounds, then this means that "b" is
867 * active in the result.
869 static __isl_give isl_basic_set
*add_larger_bound_constraint(
870 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
871 unsigned abs_pos
, int strict
)
877 total
= isl_basic_set_dim(bset
, isl_dim_all
);
879 return isl_basic_set_free(bset
);
880 k
= isl_basic_set_alloc_inequality(bset
);
885 isl_int_neg(t
, b
[1 + abs_pos
]);
887 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
888 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
889 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
893 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
899 isl_basic_set_free(bset
);
903 /* Add constraints to "context" that ensure that "u" is the smallest
904 * (and therefore active) upper bound on "abs_pos" in "bset" and return
905 * the resulting basic set.
907 static __isl_give isl_basic_set
*set_smallest_upper_bound(
908 __isl_keep isl_basic_set
*context
,
909 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
913 context
= isl_basic_set_copy(context
);
914 context
= isl_basic_set_cow(context
);
916 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
918 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
921 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
923 context
= add_larger_bound_constraint(context
,
924 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
927 context
= isl_basic_set_simplify(context
);
928 context
= isl_basic_set_finalize(context
);
933 /* Add constraints to "context" that ensure that "u" is the largest
934 * (and therefore active) upper bound on "abs_pos" in "bset" and return
935 * the resulting basic set.
937 static __isl_give isl_basic_set
*set_largest_lower_bound(
938 __isl_keep isl_basic_set
*context
,
939 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
943 context
= isl_basic_set_copy(context
);
944 context
= isl_basic_set_cow(context
);
946 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
948 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
951 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
953 context
= add_larger_bound_constraint(context
,
954 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
957 context
= isl_basic_set_simplify(context
);
958 context
= isl_basic_set_finalize(context
);
963 static isl_stat
foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
964 enum isl_dim_type type
, unsigned abs_pos
,
965 __isl_take isl_basic_set
*context
, int n_upper
,
966 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
967 __isl_take isl_constraint
*upper
,
968 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
970 isl_basic_set
*context_i
;
971 isl_constraint
*upper
= NULL
;
974 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
975 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
978 context_i
= set_smallest_upper_bound(context
, bset
,
979 abs_pos
, n_upper
, i
);
980 if (isl_basic_set_is_empty(context_i
)) {
981 isl_basic_set_free(context_i
);
984 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
986 if (!upper
|| !context_i
)
988 if (fn(NULL
, upper
, context_i
, user
) < 0)
992 isl_basic_set_free(context
);
994 if (i
< bset
->n_ineq
)
995 return isl_stat_error
;
999 isl_constraint_free(upper
);
1000 isl_basic_set_free(context_i
);
1001 isl_basic_set_free(context
);
1002 return isl_stat_error
;
1005 static isl_stat
foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
1006 enum isl_dim_type type
, unsigned abs_pos
,
1007 __isl_take isl_basic_set
*context
, int n_lower
,
1008 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1009 __isl_take isl_constraint
*upper
,
1010 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1012 isl_basic_set
*context_i
;
1013 isl_constraint
*lower
= NULL
;
1016 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1017 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1020 context_i
= set_largest_lower_bound(context
, bset
,
1021 abs_pos
, n_lower
, i
);
1022 if (isl_basic_set_is_empty(context_i
)) {
1023 isl_basic_set_free(context_i
);
1026 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1028 if (!lower
|| !context_i
)
1030 if (fn(lower
, NULL
, context_i
, user
) < 0)
1034 isl_basic_set_free(context
);
1036 if (i
< bset
->n_ineq
)
1037 return isl_stat_error
;
1041 isl_constraint_free(lower
);
1042 isl_basic_set_free(context_i
);
1043 isl_basic_set_free(context
);
1044 return isl_stat_error
;
1047 static isl_stat
foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1048 enum isl_dim_type type
, unsigned abs_pos
,
1049 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1050 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1051 __isl_take isl_constraint
*upper
,
1052 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1054 isl_basic_set
*context_i
, *context_j
;
1055 isl_constraint
*lower
= NULL
;
1056 isl_constraint
*upper
= NULL
;
1059 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1060 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1063 context_i
= set_largest_lower_bound(context
, bset
,
1064 abs_pos
, n_lower
, i
);
1065 if (isl_basic_set_is_empty(context_i
)) {
1066 isl_basic_set_free(context_i
);
1070 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1071 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1074 context_j
= set_smallest_upper_bound(context_i
, bset
,
1075 abs_pos
, n_upper
, j
);
1076 context_j
= isl_basic_set_extend_constraints(context_j
,
1078 context_j
= add_larger_bound_constraint(context_j
,
1079 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1080 context_j
= isl_basic_set_simplify(context_j
);
1081 context_j
= isl_basic_set_finalize(context_j
);
1082 if (isl_basic_set_is_empty(context_j
)) {
1083 isl_basic_set_free(context_j
);
1086 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1088 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1090 if (!lower
|| !upper
|| !context_j
)
1092 if (fn(lower
, upper
, context_j
, user
) < 0)
1096 isl_basic_set_free(context_i
);
1098 if (j
< bset
->n_ineq
)
1102 isl_basic_set_free(context
);
1104 if (i
< bset
->n_ineq
)
1105 return isl_stat_error
;
1109 isl_constraint_free(lower
);
1110 isl_constraint_free(upper
);
1111 isl_basic_set_free(context_i
);
1112 isl_basic_set_free(context_j
);
1113 isl_basic_set_free(context
);
1114 return isl_stat_error
;
1117 /* For each pair of lower and upper bounds on the variable "pos"
1118 * of type "type", call "fn" with these lower and upper bounds and the
1119 * set of constraints on the remaining variables where these bounds
1120 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1122 * If the designated variable is equal to an affine combination of the
1123 * other variables then fn is called with both lower and upper
1124 * set to the corresponding equality.
1126 * If there is no lower (or upper) bound, then NULL is passed
1127 * as the corresponding bound.
1129 * We first check if the variable is involved in any equality.
1130 * If not, we count the number of lower and upper bounds and
1133 isl_stat
isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1134 enum isl_dim_type type
, unsigned pos
,
1135 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1136 __isl_take isl_constraint
*upper
,
1137 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1140 isl_constraint
*lower
= NULL
;
1141 isl_constraint
*upper
= NULL
;
1142 isl_basic_set
*context
= NULL
;
1144 int n_lower
, n_upper
;
1147 if (isl_basic_set_check_range(bset
, type
, pos
, 1) < 0)
1148 return isl_stat_error
;
1149 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1150 return isl_stat_error
);
1152 off
= isl_basic_set_var_offset(bset
, type
);
1154 return isl_stat_error
;
1155 abs_pos
= off
+ pos
;
1157 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1158 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1161 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1163 upper
= isl_constraint_copy(lower
);
1164 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1166 if (!lower
|| !upper
|| !context
)
1168 return fn(lower
, upper
, context
, user
);
1173 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1174 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1176 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1180 context
= isl_basic_set_copy(bset
);
1181 context
= isl_basic_set_cow(context
);
1184 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1185 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1186 isl_basic_set_drop_inequality(context
, i
);
1188 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1189 if (!n_lower
&& !n_upper
)
1190 return fn(NULL
, NULL
, context
, user
);
1192 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1195 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1197 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1200 isl_constraint_free(lower
);
1201 isl_constraint_free(upper
);
1202 isl_basic_set_free(context
);
1203 return isl_stat_error
;
1206 __isl_give isl_aff
*isl_constraint_get_bound(
1207 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1213 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
1215 space
= isl_constraint_peek_space(constraint
);
1216 if (isl_space_check_is_set(space
) < 0)
1219 ctx
= isl_constraint_get_ctx(constraint
);
1220 pos
+= offset(constraint
, type
);
1221 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1222 isl_die(ctx
, isl_error_invalid
,
1223 "constraint does not define a bound on given dimension",
1226 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1230 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1231 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1233 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1234 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1235 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1236 aff
= isl_aff_normalize(aff
);
1241 /* For an inequality constraint
1245 * or an equality constraint
1249 * return the affine expression f.
1251 __isl_give isl_aff
*isl_constraint_get_aff(
1252 __isl_keep isl_constraint
*constraint
)
1259 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1263 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1264 isl_int_set_si(aff
->v
->el
[0], 1);
1269 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1270 * In particular, construct aff >= 0 or aff = 0.
1272 * The denominator of "aff" can be ignored.
1274 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1275 __isl_take isl_aff
*aff
)
1277 isl_local_space
*ls
;
1282 ls
= isl_aff_get_domain_local_space(aff
);
1283 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1286 return isl_constraint_alloc_vec(eq
, ls
, v
);
1289 /* Construct an equality constraint equating the given affine expression
1292 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1294 return isl_constraint_alloc_aff(1, aff
);
1297 /* Construct an inequality constraint enforcing the given affine expression
1298 * to be non-negative.
1300 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1302 return isl_constraint_alloc_aff(0, aff
);
1305 /* Compare two isl_constraints.
1307 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1308 * than "c2" and 0 if they are equal.
1310 * The order is fairly arbitrary. We do consider constraints that only involve
1311 * earlier dimensions as "smaller".
1313 int isl_constraint_plain_cmp(__isl_keep isl_constraint
*c1
,
1314 __isl_keep isl_constraint
*c2
)
1325 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1329 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1330 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1332 return last1
- last2
;
1334 return isl_seq_cmp(c1
->v
->el
, c2
->v
->el
, c1
->v
->size
);
1337 /* Compare two constraints based on their final (non-zero) coefficients.
1338 * In particular, the constraint that involves later variables or
1339 * that has a larger coefficient for a shared latest variable
1340 * is considered "greater" than the other constraint.
1342 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1343 * than "c2" and 0 if they are equal.
1345 * If the constraints live in different local spaces, then we cannot
1346 * really compare the constraints so we compare the local spaces instead.
1348 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint
*c1
,
1349 __isl_keep isl_constraint
*c2
)
1360 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1364 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1365 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1367 return last1
- last2
;
1370 return isl_int_abs_cmp(c1
->v
->el
[1 + last1
], c2
->v
->el
[1 + last2
]);