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 void *isl_constraint_free(struct 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_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",
756 return isl_constraint_free(constraint
));
757 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
760 int isl_basic_map_has_defining_equality(
761 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
762 __isl_give isl_constraint
**c
)
770 offset
= basic_map_offset(bmap
, type
);
771 total
= isl_basic_map_total_dim(bmap
);
772 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
773 for (i
= 0; i
< bmap
->n_eq
; ++i
)
774 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
775 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
776 1+total
-offset
-pos
-1) == -1) {
777 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
784 int isl_basic_set_has_defining_equality(
785 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
786 __isl_give isl_constraint
**c
)
788 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
792 int isl_basic_set_has_defining_inequalities(
793 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
794 struct isl_constraint
**lower
,
795 struct isl_constraint
**upper
)
801 isl_int
**lower_line
, **upper_line
;
805 offset
= basic_set_offset(bset
, type
);
806 total
= isl_basic_set_total_dim(bset
);
807 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
809 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
810 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
812 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
814 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
816 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
817 1+total
-offset
-pos
-1) != -1)
819 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
820 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
823 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
824 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
827 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
828 lower_line
= &bset
->ineq
[i
];
829 upper_line
= &bset
->ineq
[j
];
831 lower_line
= &bset
->ineq
[j
];
832 upper_line
= &bset
->ineq
[i
];
834 *lower
= isl_basic_set_constraint(
835 isl_basic_set_copy(bset
), lower_line
);
836 *upper
= isl_basic_set_constraint(
837 isl_basic_set_copy(bset
), upper_line
);
848 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
849 * (in "a" and "b"), add a constraint to "bset" that ensures that the
850 * bound implied by "a" is (strictly) larger than the bound implied by "b".
852 * If both constraints imply lower bounds, then this means that "a" is
853 * active in the result.
854 * If both constraints imply upper bounds, then this means that "b" is
855 * active in the result.
857 static __isl_give isl_basic_set
*add_larger_bound_constraint(
858 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
859 unsigned abs_pos
, int strict
)
865 k
= isl_basic_set_alloc_inequality(bset
);
869 total
= isl_basic_set_dim(bset
, isl_dim_all
);
872 isl_int_neg(t
, b
[1 + abs_pos
]);
874 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
875 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
876 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
880 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
886 isl_basic_set_free(bset
);
890 /* Add constraints to "context" that ensure that "u" is the smallest
891 * (and therefore active) upper bound on "abs_pos" in "bset" and return
892 * the resulting basic set.
894 static __isl_give isl_basic_set
*set_smallest_upper_bound(
895 __isl_keep isl_basic_set
*context
,
896 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
900 context
= isl_basic_set_copy(context
);
901 context
= isl_basic_set_cow(context
);
903 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
905 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
908 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
910 context
= add_larger_bound_constraint(context
,
911 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
914 context
= isl_basic_set_simplify(context
);
915 context
= isl_basic_set_finalize(context
);
920 /* Add constraints to "context" that ensure that "u" is the largest
921 * (and therefore active) upper bound on "abs_pos" in "bset" and return
922 * the resulting basic set.
924 static __isl_give isl_basic_set
*set_largest_lower_bound(
925 __isl_keep isl_basic_set
*context
,
926 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
930 context
= isl_basic_set_copy(context
);
931 context
= isl_basic_set_cow(context
);
933 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
935 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
938 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
940 context
= add_larger_bound_constraint(context
,
941 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
944 context
= isl_basic_set_simplify(context
);
945 context
= isl_basic_set_finalize(context
);
950 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
951 enum isl_dim_type type
, unsigned abs_pos
,
952 __isl_take isl_basic_set
*context
, int n_upper
,
953 int (*fn
)(__isl_take isl_constraint
*lower
,
954 __isl_take isl_constraint
*upper
,
955 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
957 isl_basic_set
*context_i
;
958 isl_constraint
*upper
= NULL
;
961 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
962 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
965 context_i
= set_smallest_upper_bound(context
, bset
,
966 abs_pos
, n_upper
, i
);
967 if (isl_basic_set_is_empty(context_i
)) {
968 isl_basic_set_free(context_i
);
971 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
973 if (!upper
|| !context_i
)
975 if (fn(NULL
, upper
, context_i
, user
) < 0)
979 isl_basic_set_free(context
);
981 if (i
< bset
->n_ineq
)
986 isl_constraint_free(upper
);
987 isl_basic_set_free(context_i
);
988 isl_basic_set_free(context
);
992 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
993 enum isl_dim_type type
, unsigned abs_pos
,
994 __isl_take isl_basic_set
*context
, int n_lower
,
995 int (*fn
)(__isl_take isl_constraint
*lower
,
996 __isl_take isl_constraint
*upper
,
997 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
999 isl_basic_set
*context_i
;
1000 isl_constraint
*lower
= NULL
;
1003 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1004 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1007 context_i
= set_largest_lower_bound(context
, bset
,
1008 abs_pos
, n_lower
, i
);
1009 if (isl_basic_set_is_empty(context_i
)) {
1010 isl_basic_set_free(context_i
);
1013 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1015 if (!lower
|| !context_i
)
1017 if (fn(lower
, NULL
, context_i
, user
) < 0)
1021 isl_basic_set_free(context
);
1023 if (i
< bset
->n_ineq
)
1028 isl_constraint_free(lower
);
1029 isl_basic_set_free(context_i
);
1030 isl_basic_set_free(context
);
1034 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1035 enum isl_dim_type type
, unsigned abs_pos
,
1036 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1037 int (*fn
)(__isl_take isl_constraint
*lower
,
1038 __isl_take isl_constraint
*upper
,
1039 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1041 isl_basic_set
*context_i
, *context_j
;
1042 isl_constraint
*lower
= NULL
;
1043 isl_constraint
*upper
= NULL
;
1046 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1047 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1050 context_i
= set_largest_lower_bound(context
, bset
,
1051 abs_pos
, n_lower
, i
);
1052 if (isl_basic_set_is_empty(context_i
)) {
1053 isl_basic_set_free(context_i
);
1057 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1058 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1061 context_j
= set_smallest_upper_bound(context_i
, bset
,
1062 abs_pos
, n_upper
, j
);
1063 context_j
= isl_basic_set_extend_constraints(context_j
,
1065 context_j
= add_larger_bound_constraint(context_j
,
1066 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1067 context_j
= isl_basic_set_simplify(context_j
);
1068 context_j
= isl_basic_set_finalize(context_j
);
1069 if (isl_basic_set_is_empty(context_j
)) {
1070 isl_basic_set_free(context_j
);
1073 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1075 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1077 if (!lower
|| !upper
|| !context_j
)
1079 if (fn(lower
, upper
, context_j
, user
) < 0)
1083 isl_basic_set_free(context_i
);
1085 if (j
< bset
->n_ineq
)
1089 isl_basic_set_free(context
);
1091 if (i
< bset
->n_ineq
)
1096 isl_constraint_free(lower
);
1097 isl_constraint_free(upper
);
1098 isl_basic_set_free(context_i
);
1099 isl_basic_set_free(context_j
);
1100 isl_basic_set_free(context
);
1104 /* For each pair of lower and upper bounds on the variable "pos"
1105 * of type "type", call "fn" with these lower and upper bounds and the
1106 * set of constraints on the remaining variables where these bounds
1107 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1109 * If the designated variable is equal to an affine combination of the
1110 * other variables then fn is called with both lower and upper
1111 * set to the corresponding equality.
1113 * If there is no lower (or upper) bound, then NULL is passed
1114 * as the corresponding bound.
1116 * We first check if the variable is involved in any equality.
1117 * If not, we count the number of lower and upper bounds and
1120 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1121 enum isl_dim_type type
, unsigned pos
,
1122 int (*fn
)(__isl_take isl_constraint
*lower
,
1123 __isl_take isl_constraint
*upper
,
1124 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1127 isl_constraint
*lower
= NULL
;
1128 isl_constraint
*upper
= NULL
;
1129 isl_basic_set
*context
= NULL
;
1131 int n_lower
, n_upper
;
1135 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
1136 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1140 if (type
== isl_dim_set
)
1141 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1143 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1144 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1147 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1149 upper
= isl_constraint_copy(lower
);
1150 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1152 if (!lower
|| !upper
|| !context
)
1154 return fn(lower
, upper
, context
, user
);
1159 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1160 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1162 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1166 context
= isl_basic_set_copy(bset
);
1167 context
= isl_basic_set_cow(context
);
1170 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1171 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1172 isl_basic_set_drop_inequality(context
, i
);
1174 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1175 if (!n_lower
&& !n_upper
)
1176 return fn(NULL
, NULL
, context
, user
);
1178 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1181 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1183 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1186 isl_constraint_free(lower
);
1187 isl_constraint_free(upper
);
1188 isl_basic_set_free(context
);
1192 __isl_give isl_aff
*isl_constraint_get_bound(
1193 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1200 ctx
= isl_constraint_get_ctx(constraint
);
1201 if (pos
>= isl_constraint_dim(constraint
, type
))
1202 isl_die(ctx
, isl_error_invalid
,
1203 "index out of bounds", return NULL
);
1204 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1205 isl_die(ctx
, isl_error_invalid
,
1206 "not a set constraint", return NULL
);
1208 pos
+= offset(constraint
, type
);
1209 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1210 isl_die(ctx
, isl_error_invalid
,
1211 "constraint does not define a bound on given dimension",
1214 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1218 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1219 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1221 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1222 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1223 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1228 /* For an inequality constraint
1232 * or an equality constraint
1236 * return the affine expression f.
1238 __isl_give isl_aff
*isl_constraint_get_aff(
1239 __isl_keep isl_constraint
*constraint
)
1246 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1250 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1251 isl_int_set_si(aff
->v
->el
[0], 1);
1256 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1257 * In particular, construct aff >= 0 or aff = 0.
1259 * The denominator of "aff" can be ignored.
1261 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1262 __isl_take isl_aff
*aff
)
1264 isl_local_space
*ls
;
1269 ls
= isl_aff_get_domain_local_space(aff
);
1270 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1273 return isl_constraint_alloc_vec(eq
, ls
, v
);
1276 /* Construct an equality constraint equating the given affine expression
1279 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1281 return isl_constraint_alloc_aff(1, aff
);
1284 /* Construct an inequality constraint enforcing the given affine expression
1285 * to be non-negative.
1287 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1289 return isl_constraint_alloc_aff(0, aff
);