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>
21 #include <isl/deprecated/constraint_int.h>
24 #define BASE constraint
26 #include <isl_list_templ.c>
28 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
30 return c
? isl_local_space_get_ctx(c
->ls
) : NULL
;
33 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
35 return isl_local_space_dim(c
->ls
, type
);
38 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
40 return isl_local_space_offset(c
->ls
, type
);
43 static unsigned basic_map_offset(__isl_keep isl_basic_map
*bmap
,
44 enum isl_dim_type type
)
46 return type
== isl_dim_div
? 1 + isl_space_dim(bmap
->dim
, isl_dim_all
)
47 : 1 + isl_space_offset(bmap
->dim
, type
);
50 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
51 enum isl_dim_type type
)
53 isl_space
*dim
= bset
->dim
;
55 case isl_dim_param
: return 1;
56 case isl_dim_in
: return 1 + dim
->nparam
;
57 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
58 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
63 __isl_give isl_constraint
*isl_constraint_alloc_vec(int eq
,
64 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
66 isl_constraint
*constraint
;
71 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
82 isl_local_space_free(ls
);
87 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
88 __isl_take isl_local_space
*ls
)
96 ctx
= isl_local_space_get_ctx(ls
);
97 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
99 return isl_constraint_alloc_vec(eq
, ls
, v
);
102 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
108 isl_local_space
*ls
= NULL
;
109 isl_constraint
*constraint
;
114 eq
= line
>= bmap
->eq
;
116 ctx
= isl_basic_map_get_ctx(bmap
);
117 ls
= isl_basic_map_get_local_space(bmap
);
118 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
121 isl_seq_cpy(v
->el
, line
[0], v
->size
);
122 constraint
= isl_constraint_alloc_vec(eq
, ls
, v
);
124 isl_basic_map_free(bmap
);
127 isl_local_space_free(ls
);
128 isl_basic_map_free(bmap
);
132 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
135 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
138 __isl_give isl_constraint
*isl_equality_alloc(__isl_take isl_local_space
*ls
)
140 return isl_constraint_alloc(1, ls
);
143 __isl_give isl_constraint
*isl_inequality_alloc(__isl_take isl_local_space
*ls
)
145 return isl_constraint_alloc(0, ls
);
148 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
153 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
157 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
165 return isl_constraint_dup(c
);
168 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
177 __isl_null isl_constraint
*isl_constraint_free(__isl_take isl_constraint
*c
)
185 isl_local_space_free(c
->ls
);
192 /* Return the number of constraints in "bset", i.e., the
193 * number of times isl_basic_set_foreach_constraint will
196 int isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
201 return bset
->n_eq
+ bset
->n_ineq
;
204 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
205 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
208 struct isl_constraint
*c
;
213 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
216 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
217 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
225 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
226 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
237 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
238 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
240 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
243 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
244 struct isl_constraint
*constraint2
)
248 if (!constraint1
|| !constraint2
)
250 if (constraint1
->eq
!= constraint2
->eq
)
252 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
253 if (equal
< 0 || !equal
)
255 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
258 struct isl_basic_map
*isl_basic_map_add_constraint(
259 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
265 if (!bmap
|| !constraint
)
268 ctx
= isl_constraint_get_ctx(constraint
);
269 dim
= isl_constraint_get_space(constraint
);
270 equal_space
= isl_space_is_equal(bmap
->dim
, dim
);
272 isl_assert(ctx
, equal_space
, goto error
);
274 bmap
= isl_basic_map_intersect(bmap
,
275 isl_basic_map_from_constraint(constraint
));
278 isl_basic_map_free(bmap
);
279 isl_constraint_free(constraint
);
283 struct isl_basic_set
*isl_basic_set_add_constraint(
284 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
286 return (struct isl_basic_set
*)
287 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
291 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
292 __isl_take isl_constraint
*constraint
)
296 bmap
= isl_basic_map_from_constraint(constraint
);
297 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
302 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
303 __isl_take isl_constraint
*constraint
)
305 return isl_map_add_constraint(set
, constraint
);
308 __isl_give isl_space
*isl_constraint_get_space(
309 __isl_keep isl_constraint
*constraint
)
311 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
314 __isl_give isl_local_space
*isl_constraint_get_local_space(
315 __isl_keep isl_constraint
*constraint
)
317 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
320 int isl_constraint_dim(struct isl_constraint
*constraint
,
321 enum isl_dim_type type
)
325 return n(constraint
, type
);
328 int isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
329 enum isl_dim_type type
, unsigned first
, unsigned n
)
341 ctx
= isl_constraint_get_ctx(constraint
);
342 if (first
+ n
> isl_constraint_dim(constraint
, type
))
343 isl_die(ctx
, isl_error_invalid
,
344 "range out of bounds", return -1);
346 active
= isl_local_space_get_active(constraint
->ls
,
347 constraint
->v
->el
+ 1);
351 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
352 for (i
= 0; i
< n
; ++i
)
353 if (active
[first
+ i
]) {
366 /* Does the given constraint represent a lower bound on the given
369 int isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
370 enum isl_dim_type type
, unsigned pos
)
375 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
376 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
377 "position out of bounds", return -1);
379 pos
+= isl_local_space_offset(constraint
->ls
, type
);
380 return isl_int_is_pos(constraint
->v
->el
[pos
]);
383 /* Does the given constraint represent an upper bound on the given
386 int isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
387 enum isl_dim_type type
, unsigned pos
)
392 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
393 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
394 "position out of bounds", return -1);
396 pos
+= isl_local_space_offset(constraint
->ls
, type
);
397 return isl_int_is_neg(constraint
->v
->el
[pos
]);
400 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
401 enum isl_dim_type type
, unsigned pos
)
404 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
407 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
411 isl_int_set(*v
, constraint
->v
->el
[0]);
414 /* Return the constant term of "constraint".
416 __isl_give isl_val
*isl_constraint_get_constant_val(
417 __isl_keep isl_constraint
*constraint
)
424 ctx
= isl_constraint_get_ctx(constraint
);
425 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
428 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
429 enum isl_dim_type type
, int pos
, isl_int
*v
)
434 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
435 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
436 "position out of bounds", return);
438 pos
+= isl_local_space_offset(constraint
->ls
, type
);
439 isl_int_set(*v
, constraint
->v
->el
[pos
]);
442 /* Return the coefficient of the variable of type "type" at position "pos"
445 __isl_give isl_val
*isl_constraint_get_coefficient_val(
446 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
453 ctx
= isl_constraint_get_ctx(constraint
);
454 if (pos
< 0 || pos
>= isl_local_space_dim(constraint
->ls
, type
))
455 isl_die(ctx
, isl_error_invalid
,
456 "position out of bounds", return NULL
);
458 pos
+= isl_local_space_offset(constraint
->ls
, type
);
459 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
462 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
468 return isl_local_space_get_div(constraint
->ls
, pos
);
471 __isl_give isl_constraint
*isl_constraint_set_constant(
472 __isl_take isl_constraint
*constraint
, isl_int v
)
474 constraint
= isl_constraint_cow(constraint
);
478 constraint
->v
= isl_vec_cow(constraint
->v
);
480 return isl_constraint_free(constraint
);
482 isl_int_set(constraint
->v
->el
[0], v
);
486 /* Replace the constant term of "constraint" by "v".
488 __isl_give isl_constraint
*isl_constraint_set_constant_val(
489 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
491 constraint
= isl_constraint_cow(constraint
);
492 if (!constraint
|| !v
)
494 if (!isl_val_is_int(v
))
495 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
496 "expecting integer value", goto error
);
497 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
499 constraint
= isl_constraint_free(constraint
);
503 return isl_constraint_free(constraint
);
506 __isl_give isl_constraint
*isl_constraint_set_constant_si(
507 __isl_take isl_constraint
*constraint
, int v
)
509 constraint
= isl_constraint_cow(constraint
);
513 constraint
->v
= isl_vec_cow(constraint
->v
);
515 return isl_constraint_free(constraint
);
517 isl_int_set_si(constraint
->v
->el
[0], v
);
521 __isl_give isl_constraint
*isl_constraint_set_coefficient(
522 __isl_take isl_constraint
*constraint
,
523 enum isl_dim_type type
, int pos
, isl_int v
)
525 constraint
= isl_constraint_cow(constraint
);
529 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
530 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
531 "position out of bounds",
532 return isl_constraint_free(constraint
));
534 constraint
= isl_constraint_cow(constraint
);
538 constraint
->v
= isl_vec_cow(constraint
->v
);
540 return isl_constraint_free(constraint
);
542 pos
+= isl_local_space_offset(constraint
->ls
, type
);
543 isl_int_set(constraint
->v
->el
[pos
], v
);
548 /* Replace the coefficient of the variable of type "type" at position "pos"
549 * of "constraint" by "v".
551 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
552 __isl_take isl_constraint
*constraint
,
553 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
555 constraint
= isl_constraint_cow(constraint
);
556 if (!constraint
|| !v
)
558 if (!isl_val_is_int(v
))
559 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
560 "expecting integer value", goto error
);
562 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
563 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
564 "position out of bounds", goto error
);
566 pos
+= isl_local_space_offset(constraint
->ls
, type
);
567 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
569 constraint
= isl_constraint_free(constraint
);
573 return isl_constraint_free(constraint
);
576 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
577 __isl_take isl_constraint
*constraint
,
578 enum isl_dim_type type
, int pos
, int v
)
580 constraint
= isl_constraint_cow(constraint
);
584 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
585 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
586 "position out of bounds",
587 return isl_constraint_free(constraint
));
589 constraint
= isl_constraint_cow(constraint
);
593 constraint
->v
= isl_vec_cow(constraint
->v
);
595 return isl_constraint_free(constraint
);
597 pos
+= isl_local_space_offset(constraint
->ls
, type
);
598 isl_int_set_si(constraint
->v
->el
[pos
], v
);
603 /* Drop any constraint from "bset" that is identical to "constraint".
604 * In particular, this means that the local spaces of "bset" and
605 * "constraint" need to be the same.
607 * Since the given constraint may actually be a pointer into the bset,
608 * we have to be careful not to reorder the constraints as the user
609 * may be holding on to other constraints from the same bset.
610 * This should be cleaned up when the internal representation of
611 * isl_constraint is changed to use isl_aff.
613 __isl_give isl_basic_set
*isl_basic_set_drop_constraint(
614 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
620 isl_local_space
*ls1
;
623 if (!bset
|| !constraint
)
626 ls1
= isl_basic_set_get_local_space(bset
);
627 equal
= isl_local_space_is_equal(ls1
, constraint
->ls
);
628 isl_local_space_free(ls1
);
632 isl_constraint_free(constraint
);
636 if (isl_constraint_is_equality(constraint
)) {
644 total
= isl_constraint_dim(constraint
, isl_dim_all
);
645 for (i
= 0; i
< n
; ++i
)
646 if (isl_seq_eq(row
[i
], constraint
->v
->el
, 1 + total
))
647 isl_seq_clr(row
[i
], 1 + total
);
649 isl_constraint_free(constraint
);
652 isl_constraint_free(constraint
);
653 isl_basic_set_free(bset
);
657 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
661 constraint
= isl_constraint_cow(constraint
);
665 ctx
= isl_constraint_get_ctx(constraint
);
666 if (isl_constraint_is_equality(constraint
))
667 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
668 return isl_constraint_free(constraint
));
669 constraint
->v
= isl_vec_neg(constraint
->v
);
670 constraint
->v
= isl_vec_cow(constraint
->v
);
672 return isl_constraint_free(constraint
);
673 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
677 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
681 return constraint
->eq
;
684 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
691 if (isl_constraint_is_equality(constraint
))
693 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
694 for (i
= 0; i
< n_div
; ++i
) {
695 if (isl_local_space_is_div_constraint(constraint
->ls
,
696 constraint
->v
->el
, i
))
703 /* We manually set ISL_BASIC_SET_FINAL instead of calling
704 * isl_basic_map_finalize because we want to keep the position
705 * of the divs and we therefore do not want to throw away redundant divs.
706 * This is arguably a bit fragile.
708 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
709 __isl_take isl_constraint
*constraint
)
713 struct isl_basic_map
*bmap
;
720 ls
= isl_local_space_copy(constraint
->ls
);
721 bmap
= isl_basic_map_from_local_space(ls
);
722 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
723 if (isl_constraint_is_equality(constraint
)) {
724 k
= isl_basic_map_alloc_equality(bmap
);
730 k
= isl_basic_map_alloc_inequality(bmap
);
735 total
= isl_basic_map_total_dim(bmap
);
736 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
737 isl_constraint_free(constraint
);
739 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
742 isl_constraint_free(constraint
);
743 isl_basic_map_free(bmap
);
747 struct isl_basic_set
*isl_basic_set_from_constraint(
748 struct isl_constraint
*constraint
)
753 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
754 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
755 "not a set constraint", goto error
);
756 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
758 isl_constraint_free(constraint
);
762 int isl_basic_map_has_defining_equality(
763 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
764 __isl_give isl_constraint
**c
)
772 offset
= basic_map_offset(bmap
, type
);
773 total
= isl_basic_map_total_dim(bmap
);
774 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
775 for (i
= 0; i
< bmap
->n_eq
; ++i
)
776 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
777 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
778 1+total
-offset
-pos
-1) == -1) {
779 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
786 int isl_basic_set_has_defining_equality(
787 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
788 __isl_give isl_constraint
**c
)
790 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
794 int isl_basic_set_has_defining_inequalities(
795 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
796 struct isl_constraint
**lower
,
797 struct isl_constraint
**upper
)
803 isl_int
**lower_line
, **upper_line
;
807 offset
= basic_set_offset(bset
, type
);
808 total
= isl_basic_set_total_dim(bset
);
809 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
811 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
812 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
814 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
816 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
818 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
819 1+total
-offset
-pos
-1) != -1)
821 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
822 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
825 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
826 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
829 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
830 lower_line
= &bset
->ineq
[i
];
831 upper_line
= &bset
->ineq
[j
];
833 lower_line
= &bset
->ineq
[j
];
834 upper_line
= &bset
->ineq
[i
];
836 *lower
= isl_basic_set_constraint(
837 isl_basic_set_copy(bset
), lower_line
);
838 *upper
= isl_basic_set_constraint(
839 isl_basic_set_copy(bset
), upper_line
);
850 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
851 * (in "a" and "b"), add a constraint to "bset" that ensures that the
852 * bound implied by "a" is (strictly) larger than the bound implied by "b".
854 * If both constraints imply lower bounds, then this means that "a" is
855 * active in the result.
856 * If both constraints imply upper bounds, then this means that "b" is
857 * active in the result.
859 static __isl_give isl_basic_set
*add_larger_bound_constraint(
860 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
861 unsigned abs_pos
, int strict
)
867 k
= isl_basic_set_alloc_inequality(bset
);
871 total
= isl_basic_set_dim(bset
, isl_dim_all
);
874 isl_int_neg(t
, b
[1 + abs_pos
]);
876 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
877 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
878 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
882 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
888 isl_basic_set_free(bset
);
892 /* Add constraints to "context" that ensure that "u" is the smallest
893 * (and therefore active) upper bound on "abs_pos" in "bset" and return
894 * the resulting basic set.
896 static __isl_give isl_basic_set
*set_smallest_upper_bound(
897 __isl_keep isl_basic_set
*context
,
898 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
902 context
= isl_basic_set_copy(context
);
903 context
= isl_basic_set_cow(context
);
905 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
907 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
910 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
912 context
= add_larger_bound_constraint(context
,
913 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
916 context
= isl_basic_set_simplify(context
);
917 context
= isl_basic_set_finalize(context
);
922 /* Add constraints to "context" that ensure that "u" is the largest
923 * (and therefore active) upper bound on "abs_pos" in "bset" and return
924 * the resulting basic set.
926 static __isl_give isl_basic_set
*set_largest_lower_bound(
927 __isl_keep isl_basic_set
*context
,
928 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
932 context
= isl_basic_set_copy(context
);
933 context
= isl_basic_set_cow(context
);
935 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
937 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
940 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
942 context
= add_larger_bound_constraint(context
,
943 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
946 context
= isl_basic_set_simplify(context
);
947 context
= isl_basic_set_finalize(context
);
952 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
953 enum isl_dim_type type
, unsigned abs_pos
,
954 __isl_take isl_basic_set
*context
, int n_upper
,
955 int (*fn
)(__isl_take isl_constraint
*lower
,
956 __isl_take isl_constraint
*upper
,
957 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
959 isl_basic_set
*context_i
;
960 isl_constraint
*upper
= NULL
;
963 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
964 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
967 context_i
= set_smallest_upper_bound(context
, bset
,
968 abs_pos
, n_upper
, i
);
969 if (isl_basic_set_is_empty(context_i
)) {
970 isl_basic_set_free(context_i
);
973 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
975 if (!upper
|| !context_i
)
977 if (fn(NULL
, upper
, context_i
, user
) < 0)
981 isl_basic_set_free(context
);
983 if (i
< bset
->n_ineq
)
988 isl_constraint_free(upper
);
989 isl_basic_set_free(context_i
);
990 isl_basic_set_free(context
);
994 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
995 enum isl_dim_type type
, unsigned abs_pos
,
996 __isl_take isl_basic_set
*context
, int n_lower
,
997 int (*fn
)(__isl_take isl_constraint
*lower
,
998 __isl_take isl_constraint
*upper
,
999 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1001 isl_basic_set
*context_i
;
1002 isl_constraint
*lower
= NULL
;
1005 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1006 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1009 context_i
= set_largest_lower_bound(context
, bset
,
1010 abs_pos
, n_lower
, i
);
1011 if (isl_basic_set_is_empty(context_i
)) {
1012 isl_basic_set_free(context_i
);
1015 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1017 if (!lower
|| !context_i
)
1019 if (fn(lower
, NULL
, context_i
, user
) < 0)
1023 isl_basic_set_free(context
);
1025 if (i
< bset
->n_ineq
)
1030 isl_constraint_free(lower
);
1031 isl_basic_set_free(context_i
);
1032 isl_basic_set_free(context
);
1036 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1037 enum isl_dim_type type
, unsigned abs_pos
,
1038 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1039 int (*fn
)(__isl_take isl_constraint
*lower
,
1040 __isl_take isl_constraint
*upper
,
1041 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1043 isl_basic_set
*context_i
, *context_j
;
1044 isl_constraint
*lower
= NULL
;
1045 isl_constraint
*upper
= NULL
;
1048 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1049 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1052 context_i
= set_largest_lower_bound(context
, bset
,
1053 abs_pos
, n_lower
, i
);
1054 if (isl_basic_set_is_empty(context_i
)) {
1055 isl_basic_set_free(context_i
);
1059 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1060 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1063 context_j
= set_smallest_upper_bound(context_i
, bset
,
1064 abs_pos
, n_upper
, j
);
1065 context_j
= isl_basic_set_extend_constraints(context_j
,
1067 context_j
= add_larger_bound_constraint(context_j
,
1068 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1069 context_j
= isl_basic_set_simplify(context_j
);
1070 context_j
= isl_basic_set_finalize(context_j
);
1071 if (isl_basic_set_is_empty(context_j
)) {
1072 isl_basic_set_free(context_j
);
1075 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1077 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1079 if (!lower
|| !upper
|| !context_j
)
1081 if (fn(lower
, upper
, context_j
, user
) < 0)
1085 isl_basic_set_free(context_i
);
1087 if (j
< bset
->n_ineq
)
1091 isl_basic_set_free(context
);
1093 if (i
< bset
->n_ineq
)
1098 isl_constraint_free(lower
);
1099 isl_constraint_free(upper
);
1100 isl_basic_set_free(context_i
);
1101 isl_basic_set_free(context_j
);
1102 isl_basic_set_free(context
);
1106 /* For each pair of lower and upper bounds on the variable "pos"
1107 * of type "type", call "fn" with these lower and upper bounds and the
1108 * set of constraints on the remaining variables where these bounds
1109 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1111 * If the designated variable is equal to an affine combination of the
1112 * other variables then fn is called with both lower and upper
1113 * set to the corresponding equality.
1115 * If there is no lower (or upper) bound, then NULL is passed
1116 * as the corresponding bound.
1118 * We first check if the variable is involved in any equality.
1119 * If not, we count the number of lower and upper bounds and
1122 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1123 enum isl_dim_type type
, unsigned pos
,
1124 int (*fn
)(__isl_take isl_constraint
*lower
,
1125 __isl_take isl_constraint
*upper
,
1126 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1129 isl_constraint
*lower
= NULL
;
1130 isl_constraint
*upper
= NULL
;
1131 isl_basic_set
*context
= NULL
;
1133 int n_lower
, n_upper
;
1137 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
1138 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1142 if (type
== isl_dim_set
)
1143 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1145 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1146 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1149 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1151 upper
= isl_constraint_copy(lower
);
1152 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1154 if (!lower
|| !upper
|| !context
)
1156 return fn(lower
, upper
, context
, user
);
1161 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1162 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1164 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1168 context
= isl_basic_set_copy(bset
);
1169 context
= isl_basic_set_cow(context
);
1172 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1173 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1174 isl_basic_set_drop_inequality(context
, i
);
1176 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1177 if (!n_lower
&& !n_upper
)
1178 return fn(NULL
, NULL
, context
, user
);
1180 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1183 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1185 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1188 isl_constraint_free(lower
);
1189 isl_constraint_free(upper
);
1190 isl_basic_set_free(context
);
1194 __isl_give isl_aff
*isl_constraint_get_bound(
1195 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1202 ctx
= isl_constraint_get_ctx(constraint
);
1203 if (pos
>= isl_constraint_dim(constraint
, type
))
1204 isl_die(ctx
, isl_error_invalid
,
1205 "index out of bounds", return NULL
);
1206 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1207 isl_die(ctx
, isl_error_invalid
,
1208 "not a set constraint", return NULL
);
1210 pos
+= offset(constraint
, type
);
1211 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1212 isl_die(ctx
, isl_error_invalid
,
1213 "constraint does not define a bound on given dimension",
1216 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1220 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1221 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1223 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1224 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1225 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1230 /* For an inequality constraint
1234 * or an equality constraint
1238 * return the affine expression f.
1240 __isl_give isl_aff
*isl_constraint_get_aff(
1241 __isl_keep isl_constraint
*constraint
)
1248 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1252 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1253 isl_int_set_si(aff
->v
->el
[0], 1);
1258 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1259 * In particular, construct aff >= 0 or aff = 0.
1261 * The denominator of "aff" can be ignored.
1263 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1264 __isl_take isl_aff
*aff
)
1266 isl_local_space
*ls
;
1271 ls
= isl_aff_get_domain_local_space(aff
);
1272 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1275 return isl_constraint_alloc_vec(eq
, ls
, v
);
1278 /* Construct an equality constraint equating the given affine expression
1281 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1283 return isl_constraint_alloc_aff(1, aff
);
1286 /* Construct an inequality constraint enforcing the given affine expression
1287 * to be non-negative.
1289 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1291 return isl_constraint_alloc_aff(0, aff
);