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>
22 #define BASE constraint
24 #include <isl_list_templ.c>
26 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
28 return c ? isl_local_space_get_ctx(c->ls) : NULL;
31 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
33 return isl_local_space_dim(c->ls, type);
36 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
38 return isl_local_space_offset(c->ls, type);
41 static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
42 enum isl_dim_type type)
44 return type == isl_dim_div ? 1 + isl_space_dim(bmap->dim, isl_dim_all)
45 : 1 + isl_space_offset(bmap->dim, type);
48 static unsigned basic_set_offset(struct isl_basic_set *bset,
49 enum isl_dim_type type)
51 isl_space *dim = bset->dim;
53 case isl_dim_param: return 1;
54 case isl_dim_in: return 1 + dim->nparam;
55 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
56 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
61 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
62 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
64 isl_constraint *constraint;
69 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
80 isl_local_space_free(ls);
85 __isl_give isl_constraint *isl_constraint_alloc(int eq,
86 __isl_take isl_local_space *ls)
94 ctx = isl_local_space_get_ctx(ls);
95 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
97 return isl_constraint_alloc_vec(eq, ls, v);
100 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
106 isl_local_space *ls = NULL;
107 isl_constraint *constraint;
112 eq = line >= bmap->eq;
114 ctx = isl_basic_map_get_ctx(bmap);
115 ls = isl_basic_map_get_local_space(bmap);
116 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
119 isl_seq_cpy(v->el, line[0], v->size);
120 constraint = isl_constraint_alloc_vec(eq, ls, v);
122 isl_basic_map_free(bmap);
125 isl_local_space_free(ls);
126 isl_basic_map_free(bmap);
130 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
133 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
136 __isl_give isl_constraint *isl_equality_alloc(__isl_take isl_local_space *ls)
138 return isl_constraint_alloc(1, ls);
141 __isl_give isl_constraint *isl_inequality_alloc(__isl_take isl_local_space *ls)
143 return isl_constraint_alloc(0, ls);
146 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
151 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
155 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
163 return isl_constraint_dup(c);
166 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
175 void *isl_constraint_free(struct isl_constraint *c)
183 isl_local_space_free(c->ls);
190 /* Return the number of constraints in "bset", i.e., the
191 * number of times isl_basic_set_foreach_constraint will
194 int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
199 return bset->n_eq + bset->n_ineq;
202 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
203 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
206 struct isl_constraint *c;
211 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
214 for (i = 0; i < bmap->n_eq; ++i) {
215 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
223 for (i = 0; i < bmap->n_ineq; ++i) {
224 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
235 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
236 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
238 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
241 int isl_constraint_is_equal(struct isl_constraint *constraint1,
242 struct isl_constraint *constraint2)
246 if (!constraint1 || !constraint2)
248 if (constraint1->eq != constraint2->eq)
250 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
251 if (equal < 0 || !equal)
253 return isl_vec_is_equal(constraint1->v, constraint2->v);
256 struct isl_basic_map *isl_basic_map_add_constraint(
257 struct isl_basic_map *bmap, struct isl_constraint *constraint)
263 if (!bmap || !constraint)
266 ctx = isl_constraint_get_ctx(constraint);
267 dim = isl_constraint_get_space(constraint);
268 equal_space = isl_space_is_equal(bmap->dim, dim);
270 isl_assert(ctx, equal_space, goto error);
272 bmap = isl_basic_map_intersect(bmap,
273 isl_basic_map_from_constraint(constraint));
276 isl_basic_map_free(bmap);
277 isl_constraint_free(constraint);
281 struct isl_basic_set *isl_basic_set_add_constraint(
282 struct isl_basic_set *bset, struct isl_constraint *constraint)
284 return (struct isl_basic_set *)
285 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
289 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
290 __isl_take isl_constraint *constraint)
294 bmap = isl_basic_map_from_constraint(constraint);
295 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
300 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
301 __isl_take isl_constraint *constraint)
303 return isl_map_add_constraint(set, constraint);
306 __isl_give isl_space *isl_constraint_get_space(
307 __isl_keep isl_constraint *constraint)
309 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
312 __isl_give isl_local_space *isl_constraint_get_local_space(
313 __isl_keep isl_constraint *constraint)
315 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
318 int isl_constraint_dim(struct isl_constraint *constraint,
319 enum isl_dim_type type)
323 return n(constraint, type);
326 int isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
327 enum isl_dim_type type, unsigned first, unsigned n)
339 ctx = isl_constraint_get_ctx(constraint);
340 if (first + n > isl_constraint_dim(constraint, type))
341 isl_die(ctx, isl_error_invalid,
342 "range out of bounds", return -1);
344 active = isl_local_space_get_active(constraint->ls,
345 constraint->v->el + 1);
349 first += isl_local_space_offset(constraint->ls, type) - 1;
350 for (i = 0; i < n; ++i)
351 if (active[first + i]) {
364 /* Does the given constraint represent a lower bound on the given
367 int isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
368 enum isl_dim_type type, unsigned pos)
373 if (pos >= isl_local_space_dim(constraint->ls, type))
374 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
375 "position out of bounds", return -1);
377 pos += isl_local_space_offset(constraint->ls, type);
378 return isl_int_is_pos(constraint->v->el[pos]);
381 /* Does the given constraint represent an upper bound on the given
384 int isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
385 enum isl_dim_type type, unsigned pos)
390 if (pos >= isl_local_space_dim(constraint->ls, type))
391 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
392 "position out of bounds", return -1);
394 pos += isl_local_space_offset(constraint->ls, type);
395 return isl_int_is_neg(constraint->v->el[pos]);
398 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
399 enum isl_dim_type type, unsigned pos)
402 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
405 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
409 isl_int_set(*v, constraint->v->el[0]);
412 /* Return the constant term of "constraint".
414 __isl_give isl_val *isl_constraint_get_constant_val(
415 __isl_keep isl_constraint *constraint)
422 ctx = isl_constraint_get_ctx(constraint);
423 return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
426 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
427 enum isl_dim_type type, int pos, isl_int *v)
432 if (pos >= isl_local_space_dim(constraint->ls, type))
433 isl_die(constraint->v->ctx, isl_error_invalid,
434 "position out of bounds", return);
436 pos += isl_local_space_offset(constraint->ls, type);
437 isl_int_set(*v, constraint->v->el[pos]);
440 /* Return the coefficient of the variable of type "type" at position "pos"
443 __isl_give isl_val *isl_constraint_get_coefficient_val(
444 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
451 ctx = isl_constraint_get_ctx(constraint);
452 if (pos < 0 || pos >= isl_local_space_dim(constraint->ls, type))
453 isl_die(ctx, isl_error_invalid,
454 "position out of bounds", return NULL);
456 pos += isl_local_space_offset(constraint->ls, type);
457 return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
460 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
466 return isl_local_space_get_div(constraint->ls, pos);
469 __isl_give isl_constraint *isl_constraint_set_constant(
470 __isl_take isl_constraint *constraint, isl_int v)
472 constraint = isl_constraint_cow(constraint);
476 constraint->v = isl_vec_cow(constraint->v);
478 return isl_constraint_free(constraint);
480 isl_int_set(constraint->v->el[0], v);
484 /* Replace the constant term of "constraint" by "v".
486 __isl_give isl_constraint *isl_constraint_set_constant_val(
487 __isl_take isl_constraint *constraint, __isl_take isl_val *v)
489 constraint = isl_constraint_cow(constraint);
490 if (!constraint || !v)
492 if (!isl_val_is_int(v))
493 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
494 "expecting integer value", goto error);
495 constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
497 constraint = isl_constraint_free(constraint);
501 return isl_constraint_free(constraint);
504 __isl_give isl_constraint *isl_constraint_set_constant_si(
505 __isl_take isl_constraint *constraint, int v)
507 constraint = isl_constraint_cow(constraint);
511 constraint->v = isl_vec_cow(constraint->v);
513 return isl_constraint_free(constraint);
515 isl_int_set_si(constraint->v->el[0], v);
519 __isl_give isl_constraint *isl_constraint_set_coefficient(
520 __isl_take isl_constraint *constraint,
521 enum isl_dim_type type, int pos, isl_int v)
523 constraint = isl_constraint_cow(constraint);
527 if (pos >= isl_local_space_dim(constraint->ls, type))
528 isl_die(constraint->v->ctx, isl_error_invalid,
529 "position out of bounds",
530 return isl_constraint_free(constraint));
532 constraint = isl_constraint_cow(constraint);
536 constraint->v = isl_vec_cow(constraint->v);
538 return isl_constraint_free(constraint);
540 pos += isl_local_space_offset(constraint->ls, type);
541 isl_int_set(constraint->v->el[pos], v);
546 /* Replace the coefficient of the variable of type "type" at position "pos"
547 * of "constraint" by "v".
549 __isl_give isl_constraint *isl_constraint_set_coefficient_val(
550 __isl_take isl_constraint *constraint,
551 enum isl_dim_type type, int pos, isl_val *v)
553 constraint = isl_constraint_cow(constraint);
554 if (!constraint || !v)
556 if (!isl_val_is_int(v))
557 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
558 "expecting integer value", goto error);
560 if (pos >= isl_local_space_dim(constraint->ls, type))
561 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
562 "position out of bounds", goto error);
564 pos += isl_local_space_offset(constraint->ls, type);
565 constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
567 constraint = isl_constraint_free(constraint);
571 return isl_constraint_free(constraint);
574 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
575 __isl_take isl_constraint *constraint,
576 enum isl_dim_type type, int pos, int v)
578 constraint = isl_constraint_cow(constraint);
582 if (pos >= isl_local_space_dim(constraint->ls, type))
583 isl_die(constraint->v->ctx, isl_error_invalid,
584 "position out of bounds",
585 return isl_constraint_free(constraint));
587 constraint = isl_constraint_cow(constraint);
591 constraint->v = isl_vec_cow(constraint->v);
593 return isl_constraint_free(constraint);
595 pos += isl_local_space_offset(constraint->ls, type);
596 isl_int_set_si(constraint->v->el[pos], v);
601 /* Drop any constraint from "bset" that is identical to "constraint".
602 * In particular, this means that the local spaces of "bset" and
603 * "constraint" need to be the same.
605 * Since the given constraint may actually be a pointer into the bset,
606 * we have to be careful not to reorder the constraints as the user
607 * may be holding on to other constraints from the same bset.
608 * This should be cleaned up when the internal representation of
609 * isl_constraint is changed to use isl_aff.
611 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
612 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
618 isl_local_space *ls1;
621 if (!bset || !constraint)
624 ls1 = isl_basic_set_get_local_space(bset);
625 equal = isl_local_space_is_equal(ls1, constraint->ls);
626 isl_local_space_free(ls1);
630 isl_constraint_free(constraint);
634 if (isl_constraint_is_equality(constraint)) {
642 total = isl_constraint_dim(constraint, isl_dim_all);
643 for (i = 0; i < n; ++i)
644 if (isl_seq_eq(row[i], constraint->v->el, 1 + total))
645 isl_seq_clr(row[i], 1 + total);
647 isl_constraint_free(constraint);
650 isl_constraint_free(constraint);
651 isl_basic_set_free(bset);
655 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
659 constraint = isl_constraint_cow(constraint);
663 ctx = isl_constraint_get_ctx(constraint);
664 if (isl_constraint_is_equality(constraint))
665 isl_die(ctx, isl_error_invalid, "cannot negate equality",
666 return isl_constraint_free(constraint));
667 constraint->v = isl_vec_neg(constraint->v);
668 constraint->v = isl_vec_cow(constraint->v);
670 return isl_constraint_free(constraint);
671 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
675 int isl_constraint_is_equality(struct isl_constraint *constraint)
679 return constraint->eq;
682 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
689 if (isl_constraint_is_equality(constraint))
691 n_div = isl_constraint_dim(constraint, isl_dim_div);
692 for (i = 0; i < n_div; ++i) {
693 if (isl_local_space_is_div_constraint(constraint->ls,
694 constraint->v->el, i))
701 /* We manually set ISL_BASIC_SET_FINAL instead of calling
702 * isl_basic_map_finalize because we want to keep the position
703 * of the divs and we therefore do not want to throw away redundant divs.
704 * This is arguably a bit fragile.
706 __isl_give isl_basic_map *isl_basic_map_from_constraint(
707 __isl_take isl_constraint *constraint)
711 struct isl_basic_map *bmap;
718 ls = isl_local_space_copy(constraint->ls);
719 bmap = isl_basic_map_from_local_space(ls);
720 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
721 if (isl_constraint_is_equality(constraint)) {
722 k = isl_basic_map_alloc_equality(bmap);
728 k = isl_basic_map_alloc_inequality(bmap);
733 total = isl_basic_map_total_dim(bmap);
734 isl_seq_cpy(c, constraint->v->el, 1 + total);
735 isl_constraint_free(constraint);
737 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
740 isl_constraint_free(constraint);
741 isl_basic_map_free(bmap);
745 struct isl_basic_set *isl_basic_set_from_constraint(
746 struct isl_constraint *constraint)
751 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
752 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
753 "not a set constraint",
754 return isl_constraint_free(constraint));
755 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
758 int isl_basic_map_has_defining_equality(
759 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
760 __isl_give isl_constraint **c)
768 offset = basic_map_offset(bmap, type);
769 total = isl_basic_map_total_dim(bmap);
770 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
771 for (i = 0; i < bmap->n_eq; ++i)
772 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
773 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
774 1+total-offset-pos-1) == -1) {
775 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
782 int isl_basic_set_has_defining_equality(
783 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
784 __isl_give isl_constraint **c)
786 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
790 int isl_basic_set_has_defining_inequalities(
791 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
792 struct isl_constraint **lower,
793 struct isl_constraint **upper)
799 isl_int **lower_line, **upper_line;
803 offset = basic_set_offset(bset, type);
804 total = isl_basic_set_total_dim(bset);
805 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
807 for (i = 0; i < bset->n_ineq; ++i) {
808 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
810 if (isl_int_is_one(bset->ineq[i][offset + pos]))
812 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
814 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
815 1+total-offset-pos-1) != -1)
817 for (j = i + 1; j < bset->n_ineq; ++j) {
818 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
821 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
822 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
825 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
826 lower_line = &bset->ineq[i];
827 upper_line = &bset->ineq[j];
829 lower_line = &bset->ineq[j];
830 upper_line = &bset->ineq[i];
832 *lower = isl_basic_set_constraint(
833 isl_basic_set_copy(bset), lower_line);
834 *upper = isl_basic_set_constraint(
835 isl_basic_set_copy(bset), upper_line);
846 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
847 * (in "a" and "b"), add a constraint to "bset" that ensures that the
848 * bound implied by "a" is (strictly) larger than the bound implied by "b".
850 * If both constraints imply lower bounds, then this means that "a" is
851 * active in the result.
852 * If both constraints imply upper bounds, then this means that "b" is
853 * active in the result.
855 static __isl_give isl_basic_set *add_larger_bound_constraint(
856 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
857 unsigned abs_pos, int strict)
863 k = isl_basic_set_alloc_inequality(bset);
867 total = isl_basic_set_dim(bset, isl_dim_all);
870 isl_int_neg(t, b[1 + abs_pos]);
872 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
873 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
874 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
878 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
884 isl_basic_set_free(bset);
888 /* Add constraints to "context" that ensure that "u" is the smallest
889 * (and therefore active) upper bound on "abs_pos" in "bset" and return
890 * the resulting basic set.
892 static __isl_give isl_basic_set *set_smallest_upper_bound(
893 __isl_keep isl_basic_set *context,
894 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
898 context = isl_basic_set_copy(context);
899 context = isl_basic_set_cow(context);
901 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
903 for (j = 0; j < bset->n_ineq; ++j) {
906 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
908 context = add_larger_bound_constraint(context,
909 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
912 context = isl_basic_set_simplify(context);
913 context = isl_basic_set_finalize(context);
918 /* Add constraints to "context" that ensure that "u" is the largest
919 * (and therefore active) upper bound on "abs_pos" in "bset" and return
920 * the resulting basic set.
922 static __isl_give isl_basic_set *set_largest_lower_bound(
923 __isl_keep isl_basic_set *context,
924 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
928 context = isl_basic_set_copy(context);
929 context = isl_basic_set_cow(context);
931 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
933 for (j = 0; j < bset->n_ineq; ++j) {
936 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
938 context = add_larger_bound_constraint(context,
939 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
942 context = isl_basic_set_simplify(context);
943 context = isl_basic_set_finalize(context);
948 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
949 enum isl_dim_type type, unsigned abs_pos,
950 __isl_take isl_basic_set *context, int n_upper,
951 int (*fn)(__isl_take isl_constraint *lower,
952 __isl_take isl_constraint *upper,
953 __isl_take isl_basic_set *bset, void *user), void *user)
955 isl_basic_set *context_i;
956 isl_constraint *upper = NULL;
959 for (i = 0; i < bset->n_ineq; ++i) {
960 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
963 context_i = set_smallest_upper_bound(context, bset,
964 abs_pos, n_upper, i);
965 if (isl_basic_set_is_empty(context_i)) {
966 isl_basic_set_free(context_i);
969 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
971 if (!upper || !context_i)
973 if (fn(NULL, upper, context_i, user) < 0)
977 isl_basic_set_free(context);
979 if (i < bset->n_ineq)
984 isl_constraint_free(upper);
985 isl_basic_set_free(context_i);
986 isl_basic_set_free(context);
990 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
991 enum isl_dim_type type, unsigned abs_pos,
992 __isl_take isl_basic_set *context, int n_lower,
993 int (*fn)(__isl_take isl_constraint *lower,
994 __isl_take isl_constraint *upper,
995 __isl_take isl_basic_set *bset, void *user), void *user)
997 isl_basic_set *context_i;
998 isl_constraint *lower = NULL;
1001 for (i = 0; i < bset->n_ineq; ++i) {
1002 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
1005 context_i = set_largest_lower_bound(context, bset,
1006 abs_pos, n_lower, i);
1007 if (isl_basic_set_is_empty(context_i)) {
1008 isl_basic_set_free(context_i);
1011 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1013 if (!lower || !context_i)
1015 if (fn(lower, NULL, context_i, user) < 0)
1019 isl_basic_set_free(context);
1021 if (i < bset->n_ineq)
1026 isl_constraint_free(lower);
1027 isl_basic_set_free(context_i);
1028 isl_basic_set_free(context);
1032 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
1033 enum isl_dim_type type, unsigned abs_pos,
1034 __isl_take isl_basic_set *context, int n_lower, int n_upper,
1035 int (*fn)(__isl_take isl_constraint *lower,
1036 __isl_take isl_constraint *upper,
1037 __isl_take isl_basic_set *bset, void *user), void *user)
1039 isl_basic_set *context_i, *context_j;
1040 isl_constraint *lower = NULL;
1041 isl_constraint *upper = NULL;
1044 for (i = 0; i < bset->n_ineq; ++i) {
1045 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1048 context_i = set_largest_lower_bound(context, bset,
1049 abs_pos, n_lower, i);
1050 if (isl_basic_set_is_empty(context_i)) {
1051 isl_basic_set_free(context_i);
1055 for (j = 0; j < bset->n_ineq; ++j) {
1056 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
1059 context_j = set_smallest_upper_bound(context_i, bset,
1060 abs_pos, n_upper, j);
1061 context_j = isl_basic_set_extend_constraints(context_j,
1063 context_j = add_larger_bound_constraint(context_j,
1064 bset->ineq[i], bset->ineq[j], abs_pos, 0);
1065 context_j = isl_basic_set_simplify(context_j);
1066 context_j = isl_basic_set_finalize(context_j);
1067 if (isl_basic_set_is_empty(context_j)) {
1068 isl_basic_set_free(context_j);
1071 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1073 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1075 if (!lower || !upper || !context_j)
1077 if (fn(lower, upper, context_j, user) < 0)
1081 isl_basic_set_free(context_i);
1083 if (j < bset->n_ineq)
1087 isl_basic_set_free(context);
1089 if (i < bset->n_ineq)
1094 isl_constraint_free(lower);
1095 isl_constraint_free(upper);
1096 isl_basic_set_free(context_i);
1097 isl_basic_set_free(context_j);
1098 isl_basic_set_free(context);
1102 /* For each pair of lower and upper bounds on the variable "pos"
1103 * of type "type", call "fn" with these lower and upper bounds and the
1104 * set of constraints on the remaining variables where these bounds
1105 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1107 * If the designated variable is equal to an affine combination of the
1108 * other variables then fn is called with both lower and upper
1109 * set to the corresponding equality.
1111 * If there is no lower (or upper) bound, then NULL is passed
1112 * as the corresponding bound.
1114 * We first check if the variable is involved in any equality.
1115 * If not, we count the number of lower and upper bounds and
1118 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1119 enum isl_dim_type type, unsigned pos,
1120 int (*fn)(__isl_take isl_constraint *lower,
1121 __isl_take isl_constraint *upper,
1122 __isl_take isl_basic_set *bset, void *user), void *user)
1125 isl_constraint *lower = NULL;
1126 isl_constraint *upper = NULL;
1127 isl_basic_set *context = NULL;
1129 int n_lower, n_upper;
1133 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1134 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1138 if (type == isl_dim_set)
1139 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1141 for (i = 0; i < bset->n_eq; ++i) {
1142 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1145 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1147 upper = isl_constraint_copy(lower);
1148 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1150 if (!lower || !upper || !context)
1152 return fn(lower, upper, context, user);
1157 for (i = 0; i < bset->n_ineq; ++i) {
1158 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1160 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1164 context = isl_basic_set_copy(bset);
1165 context = isl_basic_set_cow(context);
1168 for (i = context->n_ineq - 1; i >= 0; --i)
1169 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1170 isl_basic_set_drop_inequality(context, i);
1172 context = isl_basic_set_drop(context, type, pos, 1);
1173 if (!n_lower && !n_upper)
1174 return fn(NULL, NULL, context, user);
1176 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1179 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1181 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1184 isl_constraint_free(lower);
1185 isl_constraint_free(upper);
1186 isl_basic_set_free(context);
1190 __isl_give isl_aff *isl_constraint_get_bound(
1191 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1198 ctx = isl_constraint_get_ctx(constraint);
1199 if (pos >= isl_constraint_dim(constraint, type))
1200 isl_die(ctx, isl_error_invalid,
1201 "index out of bounds", return NULL);
1202 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
1203 isl_die(ctx, isl_error_invalid,
1204 "not a set constraint", return NULL);
1206 pos += offset(constraint, type);
1207 if (isl_int_is_zero(constraint->v->el[pos]))
1208 isl_die(ctx, isl_error_invalid,
1209 "constraint does not define a bound on given dimension",
1212 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1216 if (isl_int_is_neg(constraint->v->el[pos]))
1217 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1219 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1220 isl_int_set_si(aff->v->el[1 + pos], 0);
1221 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1226 /* For an inequality constraint
1230 * or an equality constraint
1234 * return the affine expression f.
1236 __isl_give isl_aff *isl_constraint_get_aff(
1237 __isl_keep isl_constraint *constraint)
1244 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1248 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1249 isl_int_set_si(aff->v->el[0], 1);
1254 /* Construct an equality constraint equating the given affine expression
1257 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1260 isl_local_space *ls;
1261 isl_basic_set *bset;
1266 ls = isl_aff_get_domain_local_space(aff);
1267 bset = isl_basic_set_from_local_space(ls);
1268 bset = isl_basic_set_extend_constraints(bset, 1, 0);
1269 k = isl_basic_set_alloc_equality(bset);
1273 isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1276 return isl_basic_set_constraint(bset, &bset->eq[k]);
1279 isl_basic_set_free(bset);
1283 /* Construct an inequality constraint enforcing the given affine expression
1284 * to be non-negative.
1286 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1289 isl_local_space *ls;
1290 isl_basic_set *bset;
1295 ls = isl_aff_get_domain_local_space(aff);
1296 bset = isl_basic_set_from_local_space(ls);
1297 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1298 k = isl_basic_set_alloc_inequality(bset);
1302 isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1305 return isl_basic_set_constraint(bset, &bset->ineq[k]);
1308 isl_basic_set_free(bset);