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 "bmap", i.e., the
193 * number of times isl_basic_map_foreach_constraint will
196 int isl_basic_map_n_constraint(__isl_keep isl_basic_map
*bmap
)
201 return bmap
->n_eq
+ bmap
->n_ineq
;
204 /* Return the number of constraints in "bset", i.e., the
205 * number of times isl_basic_set_foreach_constraint will
208 int isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
210 return isl_basic_map_n_constraint(bset
);
213 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
214 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
217 struct isl_constraint
*c
;
222 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
225 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
226 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
234 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
235 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
246 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
247 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
249 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
252 /* Add the constraint to the list that "user" points to, if it is not
255 static int collect_constraint(__isl_take isl_constraint
*constraint
,
258 isl_constraint_list
**list
= user
;
260 if (isl_constraint_is_div_constraint(constraint
))
261 isl_constraint_free(constraint
);
263 *list
= isl_constraint_list_add(*list
, constraint
);
268 /* Return a list of constraints that, when combined, are equivalent
269 * to "bmap". The input is required to have only known divs.
271 * There is no need to include the div constraints as they are
272 * implied by the div expressions.
274 __isl_give isl_constraint_list
*isl_basic_map_get_constraint_list(
275 __isl_keep isl_basic_map
*bmap
)
280 isl_constraint_list
*list
;
282 known
= isl_basic_map_divs_known(bmap
);
285 ctx
= isl_basic_map_get_ctx(bmap
);
287 isl_die(ctx
, isl_error_invalid
,
288 "input involves unknown divs", return NULL
);
290 n
= isl_basic_map_n_constraint(bmap
);
291 list
= isl_constraint_list_alloc(ctx
, n
);
292 if (isl_basic_map_foreach_constraint(bmap
,
293 &collect_constraint
, &list
) < 0)
294 list
= isl_constraint_list_free(list
);
299 /* Return a list of constraints that, when combined, are equivalent
300 * to "bset". The input is required to have only known divs.
302 __isl_give isl_constraint_list
*isl_basic_set_get_constraint_list(
303 __isl_keep isl_basic_set
*bset
)
305 return isl_basic_map_get_constraint_list(bset
);
308 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
309 struct isl_constraint
*constraint2
)
313 if (!constraint1
|| !constraint2
)
315 if (constraint1
->eq
!= constraint2
->eq
)
317 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
318 if (equal
< 0 || !equal
)
320 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
323 struct isl_basic_map
*isl_basic_map_add_constraint(
324 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
330 if (!bmap
|| !constraint
)
333 ctx
= isl_constraint_get_ctx(constraint
);
334 dim
= isl_constraint_get_space(constraint
);
335 equal_space
= isl_space_is_equal(bmap
->dim
, dim
);
337 isl_assert(ctx
, equal_space
, goto error
);
339 bmap
= isl_basic_map_intersect(bmap
,
340 isl_basic_map_from_constraint(constraint
));
343 isl_basic_map_free(bmap
);
344 isl_constraint_free(constraint
);
348 struct isl_basic_set
*isl_basic_set_add_constraint(
349 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
351 return (struct isl_basic_set
*)
352 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
356 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
357 __isl_take isl_constraint
*constraint
)
361 bmap
= isl_basic_map_from_constraint(constraint
);
362 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
367 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
368 __isl_take isl_constraint
*constraint
)
370 return isl_map_add_constraint(set
, constraint
);
373 __isl_give isl_space
*isl_constraint_get_space(
374 __isl_keep isl_constraint
*constraint
)
376 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
379 __isl_give isl_local_space
*isl_constraint_get_local_space(
380 __isl_keep isl_constraint
*constraint
)
382 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
385 int isl_constraint_dim(struct isl_constraint
*constraint
,
386 enum isl_dim_type type
)
390 return n(constraint
, type
);
393 int isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
394 enum isl_dim_type type
, unsigned first
, unsigned n
)
406 ctx
= isl_constraint_get_ctx(constraint
);
407 if (first
+ n
> isl_constraint_dim(constraint
, type
))
408 isl_die(ctx
, isl_error_invalid
,
409 "range out of bounds", return -1);
411 active
= isl_local_space_get_active(constraint
->ls
,
412 constraint
->v
->el
+ 1);
416 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
417 for (i
= 0; i
< n
; ++i
)
418 if (active
[first
+ i
]) {
431 /* Does the given constraint represent a lower bound on the given
434 int isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
435 enum isl_dim_type type
, unsigned pos
)
440 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
441 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
442 "position out of bounds", return -1);
444 pos
+= isl_local_space_offset(constraint
->ls
, type
);
445 return isl_int_is_pos(constraint
->v
->el
[pos
]);
448 /* Does the given constraint represent an upper bound on the given
451 int isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
452 enum isl_dim_type type
, unsigned pos
)
457 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
458 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
459 "position out of bounds", return -1);
461 pos
+= isl_local_space_offset(constraint
->ls
, type
);
462 return isl_int_is_neg(constraint
->v
->el
[pos
]);
465 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
466 enum isl_dim_type type
, unsigned pos
)
469 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
472 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
476 isl_int_set(*v
, constraint
->v
->el
[0]);
479 /* Return the constant term of "constraint".
481 __isl_give isl_val
*isl_constraint_get_constant_val(
482 __isl_keep isl_constraint
*constraint
)
489 ctx
= isl_constraint_get_ctx(constraint
);
490 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
493 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
494 enum isl_dim_type type
, int pos
, isl_int
*v
)
499 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
500 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
501 "position out of bounds", return);
503 pos
+= isl_local_space_offset(constraint
->ls
, type
);
504 isl_int_set(*v
, constraint
->v
->el
[pos
]);
507 /* Return the coefficient of the variable of type "type" at position "pos"
510 __isl_give isl_val
*isl_constraint_get_coefficient_val(
511 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
518 ctx
= isl_constraint_get_ctx(constraint
);
519 if (pos
< 0 || pos
>= isl_local_space_dim(constraint
->ls
, type
))
520 isl_die(ctx
, isl_error_invalid
,
521 "position out of bounds", return NULL
);
523 pos
+= isl_local_space_offset(constraint
->ls
, type
);
524 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
527 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
533 return isl_local_space_get_div(constraint
->ls
, pos
);
536 __isl_give isl_constraint
*isl_constraint_set_constant(
537 __isl_take isl_constraint
*constraint
, isl_int v
)
539 constraint
= isl_constraint_cow(constraint
);
543 constraint
->v
= isl_vec_cow(constraint
->v
);
545 return isl_constraint_free(constraint
);
547 isl_int_set(constraint
->v
->el
[0], v
);
551 /* Replace the constant term of "constraint" by "v".
553 __isl_give isl_constraint
*isl_constraint_set_constant_val(
554 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
556 constraint
= isl_constraint_cow(constraint
);
557 if (!constraint
|| !v
)
559 if (!isl_val_is_int(v
))
560 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
561 "expecting integer value", goto error
);
562 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
564 constraint
= isl_constraint_free(constraint
);
568 return isl_constraint_free(constraint
);
571 __isl_give isl_constraint
*isl_constraint_set_constant_si(
572 __isl_take isl_constraint
*constraint
, int v
)
574 constraint
= isl_constraint_cow(constraint
);
578 constraint
->v
= isl_vec_cow(constraint
->v
);
580 return isl_constraint_free(constraint
);
582 isl_int_set_si(constraint
->v
->el
[0], v
);
586 __isl_give isl_constraint
*isl_constraint_set_coefficient(
587 __isl_take isl_constraint
*constraint
,
588 enum isl_dim_type type
, int pos
, isl_int v
)
590 constraint
= isl_constraint_cow(constraint
);
594 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
595 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
596 "position out of bounds",
597 return isl_constraint_free(constraint
));
599 constraint
= isl_constraint_cow(constraint
);
603 constraint
->v
= isl_vec_cow(constraint
->v
);
605 return isl_constraint_free(constraint
);
607 pos
+= isl_local_space_offset(constraint
->ls
, type
);
608 isl_int_set(constraint
->v
->el
[pos
], v
);
613 /* Replace the coefficient of the variable of type "type" at position "pos"
614 * of "constraint" by "v".
616 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
617 __isl_take isl_constraint
*constraint
,
618 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
620 constraint
= isl_constraint_cow(constraint
);
621 if (!constraint
|| !v
)
623 if (!isl_val_is_int(v
))
624 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
625 "expecting integer value", goto error
);
627 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
628 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
629 "position out of bounds", goto error
);
631 pos
+= isl_local_space_offset(constraint
->ls
, type
);
632 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
634 constraint
= isl_constraint_free(constraint
);
638 return isl_constraint_free(constraint
);
641 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
642 __isl_take isl_constraint
*constraint
,
643 enum isl_dim_type type
, int pos
, int v
)
645 constraint
= isl_constraint_cow(constraint
);
649 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
650 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
651 "position out of bounds",
652 return isl_constraint_free(constraint
));
654 constraint
= isl_constraint_cow(constraint
);
658 constraint
->v
= isl_vec_cow(constraint
->v
);
660 return isl_constraint_free(constraint
);
662 pos
+= isl_local_space_offset(constraint
->ls
, type
);
663 isl_int_set_si(constraint
->v
->el
[pos
], v
);
668 /* Drop any constraint from "bset" that is identical to "constraint".
669 * In particular, this means that the local spaces of "bset" and
670 * "constraint" need to be the same.
672 * Since the given constraint may actually be a pointer into the bset,
673 * we have to be careful not to reorder the constraints as the user
674 * may be holding on to other constraints from the same bset.
675 * This should be cleaned up when the internal representation of
676 * isl_constraint is changed to use isl_aff.
678 __isl_give isl_basic_set
*isl_basic_set_drop_constraint(
679 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
685 isl_local_space
*ls1
;
688 if (!bset
|| !constraint
)
691 ls1
= isl_basic_set_get_local_space(bset
);
692 equal
= isl_local_space_is_equal(ls1
, constraint
->ls
);
693 isl_local_space_free(ls1
);
697 isl_constraint_free(constraint
);
701 if (isl_constraint_is_equality(constraint
)) {
709 total
= isl_constraint_dim(constraint
, isl_dim_all
);
710 for (i
= 0; i
< n
; ++i
)
711 if (isl_seq_eq(row
[i
], constraint
->v
->el
, 1 + total
))
712 isl_seq_clr(row
[i
], 1 + total
);
714 isl_constraint_free(constraint
);
717 isl_constraint_free(constraint
);
718 isl_basic_set_free(bset
);
722 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
726 constraint
= isl_constraint_cow(constraint
);
730 ctx
= isl_constraint_get_ctx(constraint
);
731 if (isl_constraint_is_equality(constraint
))
732 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
733 return isl_constraint_free(constraint
));
734 constraint
->v
= isl_vec_neg(constraint
->v
);
735 constraint
->v
= isl_vec_cow(constraint
->v
);
737 return isl_constraint_free(constraint
);
738 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
742 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
746 return constraint
->eq
;
749 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
756 if (isl_constraint_is_equality(constraint
))
758 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
759 for (i
= 0; i
< n_div
; ++i
) {
760 if (isl_local_space_is_div_constraint(constraint
->ls
,
761 constraint
->v
->el
, i
))
768 /* We manually set ISL_BASIC_SET_FINAL instead of calling
769 * isl_basic_map_finalize because we want to keep the position
770 * of the divs and we therefore do not want to throw away redundant divs.
771 * This is arguably a bit fragile.
773 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
774 __isl_take isl_constraint
*constraint
)
778 struct isl_basic_map
*bmap
;
785 ls
= isl_local_space_copy(constraint
->ls
);
786 bmap
= isl_basic_map_from_local_space(ls
);
787 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
788 if (isl_constraint_is_equality(constraint
)) {
789 k
= isl_basic_map_alloc_equality(bmap
);
795 k
= isl_basic_map_alloc_inequality(bmap
);
800 total
= isl_basic_map_total_dim(bmap
);
801 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
802 isl_constraint_free(constraint
);
804 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
807 isl_constraint_free(constraint
);
808 isl_basic_map_free(bmap
);
812 struct isl_basic_set
*isl_basic_set_from_constraint(
813 struct isl_constraint
*constraint
)
818 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
819 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
820 "not a set constraint", goto error
);
821 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
823 isl_constraint_free(constraint
);
827 int isl_basic_map_has_defining_equality(
828 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
829 __isl_give isl_constraint
**c
)
837 offset
= basic_map_offset(bmap
, type
);
838 total
= isl_basic_map_total_dim(bmap
);
839 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
840 for (i
= 0; i
< bmap
->n_eq
; ++i
)
841 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
842 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
843 1+total
-offset
-pos
-1) == -1) {
844 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
851 int isl_basic_set_has_defining_equality(
852 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
853 __isl_give isl_constraint
**c
)
855 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
859 int isl_basic_set_has_defining_inequalities(
860 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
861 struct isl_constraint
**lower
,
862 struct isl_constraint
**upper
)
868 isl_int
**lower_line
, **upper_line
;
872 offset
= basic_set_offset(bset
, type
);
873 total
= isl_basic_set_total_dim(bset
);
874 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
876 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
877 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
879 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
881 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
883 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
884 1+total
-offset
-pos
-1) != -1)
886 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
887 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
890 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
891 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
894 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
895 lower_line
= &bset
->ineq
[i
];
896 upper_line
= &bset
->ineq
[j
];
898 lower_line
= &bset
->ineq
[j
];
899 upper_line
= &bset
->ineq
[i
];
901 *lower
= isl_basic_set_constraint(
902 isl_basic_set_copy(bset
), lower_line
);
903 *upper
= isl_basic_set_constraint(
904 isl_basic_set_copy(bset
), upper_line
);
915 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
916 * (in "a" and "b"), add a constraint to "bset" that ensures that the
917 * bound implied by "a" is (strictly) larger than the bound implied by "b".
919 * If both constraints imply lower bounds, then this means that "a" is
920 * active in the result.
921 * If both constraints imply upper bounds, then this means that "b" is
922 * active in the result.
924 static __isl_give isl_basic_set
*add_larger_bound_constraint(
925 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
926 unsigned abs_pos
, int strict
)
932 k
= isl_basic_set_alloc_inequality(bset
);
936 total
= isl_basic_set_dim(bset
, isl_dim_all
);
939 isl_int_neg(t
, b
[1 + abs_pos
]);
941 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
942 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
943 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
947 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
953 isl_basic_set_free(bset
);
957 /* Add constraints to "context" that ensure that "u" is the smallest
958 * (and therefore active) upper bound on "abs_pos" in "bset" and return
959 * the resulting basic set.
961 static __isl_give isl_basic_set
*set_smallest_upper_bound(
962 __isl_keep isl_basic_set
*context
,
963 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
967 context
= isl_basic_set_copy(context
);
968 context
= isl_basic_set_cow(context
);
970 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
972 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
975 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
977 context
= add_larger_bound_constraint(context
,
978 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
981 context
= isl_basic_set_simplify(context
);
982 context
= isl_basic_set_finalize(context
);
987 /* Add constraints to "context" that ensure that "u" is the largest
988 * (and therefore active) upper bound on "abs_pos" in "bset" and return
989 * the resulting basic set.
991 static __isl_give isl_basic_set
*set_largest_lower_bound(
992 __isl_keep isl_basic_set
*context
,
993 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
997 context
= isl_basic_set_copy(context
);
998 context
= isl_basic_set_cow(context
);
1000 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
1002 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1005 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
1007 context
= add_larger_bound_constraint(context
,
1008 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
1011 context
= isl_basic_set_simplify(context
);
1012 context
= isl_basic_set_finalize(context
);
1017 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
1018 enum isl_dim_type type
, unsigned abs_pos
,
1019 __isl_take isl_basic_set
*context
, int n_upper
,
1020 int (*fn
)(__isl_take isl_constraint
*lower
,
1021 __isl_take isl_constraint
*upper
,
1022 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1024 isl_basic_set
*context_i
;
1025 isl_constraint
*upper
= NULL
;
1028 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1029 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1032 context_i
= set_smallest_upper_bound(context
, bset
,
1033 abs_pos
, n_upper
, i
);
1034 if (isl_basic_set_is_empty(context_i
)) {
1035 isl_basic_set_free(context_i
);
1038 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1040 if (!upper
|| !context_i
)
1042 if (fn(NULL
, upper
, context_i
, user
) < 0)
1046 isl_basic_set_free(context
);
1048 if (i
< bset
->n_ineq
)
1053 isl_constraint_free(upper
);
1054 isl_basic_set_free(context_i
);
1055 isl_basic_set_free(context
);
1059 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
1060 enum isl_dim_type type
, unsigned abs_pos
,
1061 __isl_take isl_basic_set
*context
, int n_lower
,
1062 int (*fn
)(__isl_take isl_constraint
*lower
,
1063 __isl_take isl_constraint
*upper
,
1064 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1066 isl_basic_set
*context_i
;
1067 isl_constraint
*lower
= NULL
;
1070 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1071 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1074 context_i
= set_largest_lower_bound(context
, bset
,
1075 abs_pos
, n_lower
, i
);
1076 if (isl_basic_set_is_empty(context_i
)) {
1077 isl_basic_set_free(context_i
);
1080 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1082 if (!lower
|| !context_i
)
1084 if (fn(lower
, NULL
, context_i
, user
) < 0)
1088 isl_basic_set_free(context
);
1090 if (i
< bset
->n_ineq
)
1095 isl_constraint_free(lower
);
1096 isl_basic_set_free(context_i
);
1097 isl_basic_set_free(context
);
1101 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1102 enum isl_dim_type type
, unsigned abs_pos
,
1103 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1104 int (*fn
)(__isl_take isl_constraint
*lower
,
1105 __isl_take isl_constraint
*upper
,
1106 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1108 isl_basic_set
*context_i
, *context_j
;
1109 isl_constraint
*lower
= NULL
;
1110 isl_constraint
*upper
= NULL
;
1113 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1114 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1117 context_i
= set_largest_lower_bound(context
, bset
,
1118 abs_pos
, n_lower
, i
);
1119 if (isl_basic_set_is_empty(context_i
)) {
1120 isl_basic_set_free(context_i
);
1124 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1125 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1128 context_j
= set_smallest_upper_bound(context_i
, bset
,
1129 abs_pos
, n_upper
, j
);
1130 context_j
= isl_basic_set_extend_constraints(context_j
,
1132 context_j
= add_larger_bound_constraint(context_j
,
1133 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1134 context_j
= isl_basic_set_simplify(context_j
);
1135 context_j
= isl_basic_set_finalize(context_j
);
1136 if (isl_basic_set_is_empty(context_j
)) {
1137 isl_basic_set_free(context_j
);
1140 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1142 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1144 if (!lower
|| !upper
|| !context_j
)
1146 if (fn(lower
, upper
, context_j
, user
) < 0)
1150 isl_basic_set_free(context_i
);
1152 if (j
< bset
->n_ineq
)
1156 isl_basic_set_free(context
);
1158 if (i
< bset
->n_ineq
)
1163 isl_constraint_free(lower
);
1164 isl_constraint_free(upper
);
1165 isl_basic_set_free(context_i
);
1166 isl_basic_set_free(context_j
);
1167 isl_basic_set_free(context
);
1171 /* For each pair of lower and upper bounds on the variable "pos"
1172 * of type "type", call "fn" with these lower and upper bounds and the
1173 * set of constraints on the remaining variables where these bounds
1174 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1176 * If the designated variable is equal to an affine combination of the
1177 * other variables then fn is called with both lower and upper
1178 * set to the corresponding equality.
1180 * If there is no lower (or upper) bound, then NULL is passed
1181 * as the corresponding bound.
1183 * We first check if the variable is involved in any equality.
1184 * If not, we count the number of lower and upper bounds and
1187 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1188 enum isl_dim_type type
, unsigned pos
,
1189 int (*fn
)(__isl_take isl_constraint
*lower
,
1190 __isl_take isl_constraint
*upper
,
1191 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1194 isl_constraint
*lower
= NULL
;
1195 isl_constraint
*upper
= NULL
;
1196 isl_basic_set
*context
= NULL
;
1198 int n_lower
, n_upper
;
1202 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
1203 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1207 if (type
== isl_dim_set
)
1208 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1210 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1211 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1214 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1216 upper
= isl_constraint_copy(lower
);
1217 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1219 if (!lower
|| !upper
|| !context
)
1221 return fn(lower
, upper
, context
, user
);
1226 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1227 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1229 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1233 context
= isl_basic_set_copy(bset
);
1234 context
= isl_basic_set_cow(context
);
1237 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1238 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1239 isl_basic_set_drop_inequality(context
, i
);
1241 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1242 if (!n_lower
&& !n_upper
)
1243 return fn(NULL
, NULL
, context
, user
);
1245 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1248 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1250 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1253 isl_constraint_free(lower
);
1254 isl_constraint_free(upper
);
1255 isl_basic_set_free(context
);
1259 __isl_give isl_aff
*isl_constraint_get_bound(
1260 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1267 ctx
= isl_constraint_get_ctx(constraint
);
1268 if (pos
>= isl_constraint_dim(constraint
, type
))
1269 isl_die(ctx
, isl_error_invalid
,
1270 "index out of bounds", return NULL
);
1271 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1272 isl_die(ctx
, isl_error_invalid
,
1273 "not a set constraint", return NULL
);
1275 pos
+= offset(constraint
, type
);
1276 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1277 isl_die(ctx
, isl_error_invalid
,
1278 "constraint does not define a bound on given dimension",
1281 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1285 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1286 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1288 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1289 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1290 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1295 /* For an inequality constraint
1299 * or an equality constraint
1303 * return the affine expression f.
1305 __isl_give isl_aff
*isl_constraint_get_aff(
1306 __isl_keep isl_constraint
*constraint
)
1313 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1317 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1318 isl_int_set_si(aff
->v
->el
[0], 1);
1323 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1324 * In particular, construct aff >= 0 or aff = 0.
1326 * The denominator of "aff" can be ignored.
1328 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1329 __isl_take isl_aff
*aff
)
1331 isl_local_space
*ls
;
1336 ls
= isl_aff_get_domain_local_space(aff
);
1337 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1340 return isl_constraint_alloc_vec(eq
, ls
, v
);
1343 /* Construct an equality constraint equating the given affine expression
1346 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1348 return isl_constraint_alloc_aff(1, aff
);
1351 /* Construct an inequality constraint enforcing the given affine expression
1352 * to be non-negative.
1354 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1356 return isl_constraint_alloc_aff(0, aff
);