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>
21 #define BASE constraint
23 #include <isl_list_templ.c>
25 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
27 return c ? isl_local_space_get_ctx(c->ls) : NULL;
30 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
32 return isl_local_space_dim(c->ls, type);
35 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
37 return isl_local_space_offset(c->ls, type);
40 static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
41 enum isl_dim_type type)
43 return type == isl_dim_div ? 1 + isl_space_dim(bmap->dim, isl_dim_all)
44 : 1 + isl_space_offset(bmap->dim, type);
47 static unsigned basic_set_offset(struct isl_basic_set *bset,
48 enum isl_dim_type type)
50 isl_space *dim = bset->dim;
52 case isl_dim_param: return 1;
53 case isl_dim_in: return 1 + dim->nparam;
54 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
55 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
60 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
61 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
63 isl_constraint *constraint;
68 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
79 isl_local_space_free(ls);
84 __isl_give isl_constraint *isl_constraint_alloc(int eq,
85 __isl_take isl_local_space *ls)
93 ctx = isl_local_space_get_ctx(ls);
94 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
96 return isl_constraint_alloc_vec(eq, ls, v);
99 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
105 isl_local_space *ls = NULL;
106 isl_constraint *constraint;
111 eq = line >= bmap->eq;
113 ctx = isl_basic_map_get_ctx(bmap);
114 ls = isl_basic_map_get_local_space(bmap);
115 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
118 isl_seq_cpy(v->el, line[0], v->size);
119 constraint = isl_constraint_alloc_vec(eq, ls, v);
121 isl_basic_map_free(bmap);
124 isl_local_space_free(ls);
125 isl_basic_map_free(bmap);
129 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
132 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
135 __isl_give isl_constraint *isl_equality_alloc(__isl_take isl_local_space *ls)
137 return isl_constraint_alloc(1, ls);
140 __isl_give isl_constraint *isl_inequality_alloc(__isl_take isl_local_space *ls)
142 return isl_constraint_alloc(0, ls);
145 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
150 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
154 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
162 return isl_constraint_dup(c);
165 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
174 void *isl_constraint_free(struct isl_constraint *c)
182 isl_local_space_free(c->ls);
189 /* Return the number of constraints in "bset", i.e., the
190 * number of times isl_basic_set_foreach_constraint will
193 int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
198 return bset->n_eq + bset->n_ineq;
201 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
202 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
205 struct isl_constraint *c;
210 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
213 for (i = 0; i < bmap->n_eq; ++i) {
214 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
222 for (i = 0; i < bmap->n_ineq; ++i) {
223 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
234 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
235 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
237 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
240 int isl_constraint_is_equal(struct isl_constraint *constraint1,
241 struct isl_constraint *constraint2)
245 if (!constraint1 || !constraint2)
247 if (constraint1->eq != constraint2->eq)
249 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
250 if (equal < 0 || !equal)
252 return isl_vec_is_equal(constraint1->v, constraint2->v);
255 struct isl_basic_map *isl_basic_map_add_constraint(
256 struct isl_basic_map *bmap, struct isl_constraint *constraint)
262 if (!bmap || !constraint)
265 ctx = isl_constraint_get_ctx(constraint);
266 dim = isl_constraint_get_space(constraint);
267 equal_space = isl_space_is_equal(bmap->dim, dim);
269 isl_assert(ctx, equal_space, goto error);
271 bmap = isl_basic_map_intersect(bmap,
272 isl_basic_map_from_constraint(constraint));
275 isl_basic_map_free(bmap);
276 isl_constraint_free(constraint);
280 struct isl_basic_set *isl_basic_set_add_constraint(
281 struct isl_basic_set *bset, struct isl_constraint *constraint)
283 return (struct isl_basic_set *)
284 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
288 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
289 __isl_take isl_constraint *constraint)
293 bmap = isl_basic_map_from_constraint(constraint);
294 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
299 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
300 __isl_take isl_constraint *constraint)
302 return isl_map_add_constraint(set, constraint);
305 __isl_give isl_space *isl_constraint_get_space(
306 __isl_keep isl_constraint *constraint)
308 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
311 __isl_give isl_local_space *isl_constraint_get_local_space(
312 __isl_keep isl_constraint *constraint)
314 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
317 int isl_constraint_dim(struct isl_constraint *constraint,
318 enum isl_dim_type type)
322 return n(constraint, type);
325 int isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
326 enum isl_dim_type type, unsigned first, unsigned n)
338 ctx = isl_constraint_get_ctx(constraint);
339 if (first + n > isl_constraint_dim(constraint, type))
340 isl_die(ctx, isl_error_invalid,
341 "range out of bounds", return -1);
343 active = isl_local_space_get_active(constraint->ls,
344 constraint->v->el + 1);
348 first += isl_local_space_offset(constraint->ls, type) - 1;
349 for (i = 0; i < n; ++i)
350 if (active[first + i]) {
363 /* Does the given constraint represent a lower bound on the given
366 int isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
367 enum isl_dim_type type, unsigned pos)
372 if (pos >= isl_local_space_dim(constraint->ls, type))
373 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
374 "position out of bounds", return -1);
376 pos += isl_local_space_offset(constraint->ls, type);
377 return isl_int_is_pos(constraint->v->el[pos]);
380 /* Does the given constraint represent an upper bound on the given
383 int isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
384 enum isl_dim_type type, unsigned pos)
389 if (pos >= isl_local_space_dim(constraint->ls, type))
390 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
391 "position out of bounds", return -1);
393 pos += isl_local_space_offset(constraint->ls, type);
394 return isl_int_is_neg(constraint->v->el[pos]);
397 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
398 enum isl_dim_type type, unsigned pos)
401 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
404 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
408 isl_int_set(*v, constraint->v->el[0]);
411 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
412 enum isl_dim_type type, int pos, isl_int *v)
417 if (pos >= isl_local_space_dim(constraint->ls, type))
418 isl_die(constraint->v->ctx, isl_error_invalid,
419 "position out of bounds", return);
421 pos += isl_local_space_offset(constraint->ls, type);
422 isl_int_set(*v, constraint->v->el[pos]);
425 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
431 return isl_local_space_get_div(constraint->ls, pos);
434 __isl_give isl_constraint *isl_constraint_set_constant(
435 __isl_take isl_constraint *constraint, isl_int v)
437 constraint = isl_constraint_cow(constraint);
441 constraint->v = isl_vec_cow(constraint->v);
443 return isl_constraint_free(constraint);
445 isl_int_set(constraint->v->el[0], v);
449 __isl_give isl_constraint *isl_constraint_set_constant_si(
450 __isl_take isl_constraint *constraint, int v)
452 constraint = isl_constraint_cow(constraint);
456 constraint->v = isl_vec_cow(constraint->v);
458 return isl_constraint_free(constraint);
460 isl_int_set_si(constraint->v->el[0], v);
464 __isl_give isl_constraint *isl_constraint_set_coefficient(
465 __isl_take isl_constraint *constraint,
466 enum isl_dim_type type, int pos, isl_int v)
468 constraint = isl_constraint_cow(constraint);
472 if (pos >= isl_local_space_dim(constraint->ls, type))
473 isl_die(constraint->v->ctx, isl_error_invalid,
474 "position out of bounds",
475 return isl_constraint_free(constraint));
477 constraint = isl_constraint_cow(constraint);
481 constraint->v = isl_vec_cow(constraint->v);
483 return isl_constraint_free(constraint);
485 pos += isl_local_space_offset(constraint->ls, type);
486 isl_int_set(constraint->v->el[pos], v);
491 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
492 __isl_take isl_constraint *constraint,
493 enum isl_dim_type type, int pos, int v)
495 constraint = isl_constraint_cow(constraint);
499 if (pos >= isl_local_space_dim(constraint->ls, type))
500 isl_die(constraint->v->ctx, isl_error_invalid,
501 "position out of bounds",
502 return isl_constraint_free(constraint));
504 constraint = isl_constraint_cow(constraint);
508 constraint->v = isl_vec_cow(constraint->v);
510 return isl_constraint_free(constraint);
512 pos += isl_local_space_offset(constraint->ls, type);
513 isl_int_set_si(constraint->v->el[pos], v);
518 /* Drop any constraint from "bset" that is identical to "constraint".
519 * In particular, this means that the local spaces of "bset" and
520 * "constraint" need to be the same.
522 * Since the given constraint may actually be a pointer into the bset,
523 * we have to be careful not to reorder the constraints as the user
524 * may be holding on to other constraints from the same bset.
525 * This should be cleaned up when the internal representation of
526 * isl_constraint is changed to use isl_aff.
528 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
529 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
535 isl_local_space *ls1;
538 if (!bset || !constraint)
541 ls1 = isl_basic_set_get_local_space(bset);
542 equal = isl_local_space_is_equal(ls1, constraint->ls);
543 isl_local_space_free(ls1);
547 isl_constraint_free(constraint);
551 if (isl_constraint_is_equality(constraint)) {
559 total = isl_constraint_dim(constraint, isl_dim_all);
560 for (i = 0; i < n; ++i)
561 if (isl_seq_eq(row[i], constraint->v->el, 1 + total))
562 isl_seq_clr(row[i], 1 + total);
564 isl_constraint_free(constraint);
567 isl_constraint_free(constraint);
568 isl_basic_set_free(bset);
572 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
576 constraint = isl_constraint_cow(constraint);
580 ctx = isl_constraint_get_ctx(constraint);
581 if (isl_constraint_is_equality(constraint))
582 isl_die(ctx, isl_error_invalid, "cannot negate equality",
583 return isl_constraint_free(constraint));
584 constraint->v = isl_vec_neg(constraint->v);
585 constraint->v = isl_vec_cow(constraint->v);
587 return isl_constraint_free(constraint);
588 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
592 int isl_constraint_is_equality(struct isl_constraint *constraint)
596 return constraint->eq;
599 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
606 if (isl_constraint_is_equality(constraint))
608 n_div = isl_constraint_dim(constraint, isl_dim_div);
609 for (i = 0; i < n_div; ++i) {
610 if (isl_local_space_is_div_constraint(constraint->ls,
611 constraint->v->el, i))
618 /* We manually set ISL_BASIC_SET_FINAL instead of calling
619 * isl_basic_map_finalize because we want to keep the position
620 * of the divs and we therefore do not want to throw away redundant divs.
621 * This is arguably a bit fragile.
623 __isl_give isl_basic_map *isl_basic_map_from_constraint(
624 __isl_take isl_constraint *constraint)
628 struct isl_basic_map *bmap;
635 ls = isl_local_space_copy(constraint->ls);
636 bmap = isl_basic_map_from_local_space(ls);
637 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
638 if (isl_constraint_is_equality(constraint)) {
639 k = isl_basic_map_alloc_equality(bmap);
645 k = isl_basic_map_alloc_inequality(bmap);
650 total = isl_basic_map_total_dim(bmap);
651 isl_seq_cpy(c, constraint->v->el, 1 + total);
652 isl_constraint_free(constraint);
654 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
657 isl_constraint_free(constraint);
658 isl_basic_map_free(bmap);
662 struct isl_basic_set *isl_basic_set_from_constraint(
663 struct isl_constraint *constraint)
668 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
669 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
670 "not a set constraint",
671 return isl_constraint_free(constraint));
672 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
675 int isl_basic_map_has_defining_equality(
676 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
677 __isl_give isl_constraint **c)
685 offset = basic_map_offset(bmap, type);
686 total = isl_basic_map_total_dim(bmap);
687 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
688 for (i = 0; i < bmap->n_eq; ++i)
689 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
690 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
691 1+total-offset-pos-1) == -1) {
692 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
699 int isl_basic_set_has_defining_equality(
700 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
701 __isl_give isl_constraint **c)
703 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
707 int isl_basic_set_has_defining_inequalities(
708 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
709 struct isl_constraint **lower,
710 struct isl_constraint **upper)
716 isl_int **lower_line, **upper_line;
720 offset = basic_set_offset(bset, type);
721 total = isl_basic_set_total_dim(bset);
722 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
724 for (i = 0; i < bset->n_ineq; ++i) {
725 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
727 if (isl_int_is_one(bset->ineq[i][offset + pos]))
729 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
731 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
732 1+total-offset-pos-1) != -1)
734 for (j = i + 1; j < bset->n_ineq; ++j) {
735 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
738 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
739 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
742 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
743 lower_line = &bset->ineq[i];
744 upper_line = &bset->ineq[j];
746 lower_line = &bset->ineq[j];
747 upper_line = &bset->ineq[i];
749 *lower = isl_basic_set_constraint(
750 isl_basic_set_copy(bset), lower_line);
751 *upper = isl_basic_set_constraint(
752 isl_basic_set_copy(bset), upper_line);
763 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
764 * (in "a" and "b"), add a constraint to "bset" that ensures that the
765 * bound implied by "a" is (strictly) larger than the bound implied by "b".
767 * If both constraints imply lower bounds, then this means that "a" is
768 * active in the result.
769 * If both constraints imply upper bounds, then this means that "b" is
770 * active in the result.
772 static __isl_give isl_basic_set *add_larger_bound_constraint(
773 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
774 unsigned abs_pos, int strict)
780 k = isl_basic_set_alloc_inequality(bset);
784 total = isl_basic_set_dim(bset, isl_dim_all);
787 isl_int_neg(t, b[1 + abs_pos]);
789 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
790 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
791 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
795 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
801 isl_basic_set_free(bset);
805 /* Add constraints to "context" that ensure that "u" is the smallest
806 * (and therefore active) upper bound on "abs_pos" in "bset" and return
807 * the resulting basic set.
809 static __isl_give isl_basic_set *set_smallest_upper_bound(
810 __isl_keep isl_basic_set *context,
811 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
815 context = isl_basic_set_copy(context);
816 context = isl_basic_set_cow(context);
818 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
820 for (j = 0; j < bset->n_ineq; ++j) {
823 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
825 context = add_larger_bound_constraint(context,
826 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
829 context = isl_basic_set_simplify(context);
830 context = isl_basic_set_finalize(context);
835 /* Add constraints to "context" that ensure that "u" is the largest
836 * (and therefore active) upper bound on "abs_pos" in "bset" and return
837 * the resulting basic set.
839 static __isl_give isl_basic_set *set_largest_lower_bound(
840 __isl_keep isl_basic_set *context,
841 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
845 context = isl_basic_set_copy(context);
846 context = isl_basic_set_cow(context);
848 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
850 for (j = 0; j < bset->n_ineq; ++j) {
853 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
855 context = add_larger_bound_constraint(context,
856 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
859 context = isl_basic_set_simplify(context);
860 context = isl_basic_set_finalize(context);
865 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
866 enum isl_dim_type type, unsigned abs_pos,
867 __isl_take isl_basic_set *context, int n_upper,
868 int (*fn)(__isl_take isl_constraint *lower,
869 __isl_take isl_constraint *upper,
870 __isl_take isl_basic_set *bset, void *user), void *user)
872 isl_basic_set *context_i;
873 isl_constraint *upper = NULL;
876 for (i = 0; i < bset->n_ineq; ++i) {
877 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
880 context_i = set_smallest_upper_bound(context, bset,
881 abs_pos, n_upper, i);
882 if (isl_basic_set_is_empty(context_i)) {
883 isl_basic_set_free(context_i);
886 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
888 if (!upper || !context_i)
890 if (fn(NULL, upper, context_i, user) < 0)
894 isl_basic_set_free(context);
896 if (i < bset->n_ineq)
901 isl_constraint_free(upper);
902 isl_basic_set_free(context_i);
903 isl_basic_set_free(context);
907 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
908 enum isl_dim_type type, unsigned abs_pos,
909 __isl_take isl_basic_set *context, int n_lower,
910 int (*fn)(__isl_take isl_constraint *lower,
911 __isl_take isl_constraint *upper,
912 __isl_take isl_basic_set *bset, void *user), void *user)
914 isl_basic_set *context_i;
915 isl_constraint *lower = NULL;
918 for (i = 0; i < bset->n_ineq; ++i) {
919 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
922 context_i = set_largest_lower_bound(context, bset,
923 abs_pos, n_lower, i);
924 if (isl_basic_set_is_empty(context_i)) {
925 isl_basic_set_free(context_i);
928 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
930 if (!lower || !context_i)
932 if (fn(lower, NULL, context_i, user) < 0)
936 isl_basic_set_free(context);
938 if (i < bset->n_ineq)
943 isl_constraint_free(lower);
944 isl_basic_set_free(context_i);
945 isl_basic_set_free(context);
949 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
950 enum isl_dim_type type, unsigned abs_pos,
951 __isl_take isl_basic_set *context, int n_lower, int n_upper,
952 int (*fn)(__isl_take isl_constraint *lower,
953 __isl_take isl_constraint *upper,
954 __isl_take isl_basic_set *bset, void *user), void *user)
956 isl_basic_set *context_i, *context_j;
957 isl_constraint *lower = NULL;
958 isl_constraint *upper = NULL;
961 for (i = 0; i < bset->n_ineq; ++i) {
962 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
965 context_i = set_largest_lower_bound(context, bset,
966 abs_pos, n_lower, i);
967 if (isl_basic_set_is_empty(context_i)) {
968 isl_basic_set_free(context_i);
972 for (j = 0; j < bset->n_ineq; ++j) {
973 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
976 context_j = set_smallest_upper_bound(context_i, bset,
977 abs_pos, n_upper, j);
978 context_j = isl_basic_set_extend_constraints(context_j,
980 context_j = add_larger_bound_constraint(context_j,
981 bset->ineq[i], bset->ineq[j], abs_pos, 0);
982 context_j = isl_basic_set_simplify(context_j);
983 context_j = isl_basic_set_finalize(context_j);
984 if (isl_basic_set_is_empty(context_j)) {
985 isl_basic_set_free(context_j);
988 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
990 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
992 if (!lower || !upper || !context_j)
994 if (fn(lower, upper, context_j, user) < 0)
998 isl_basic_set_free(context_i);
1000 if (j < bset->n_ineq)
1004 isl_basic_set_free(context);
1006 if (i < bset->n_ineq)
1011 isl_constraint_free(lower);
1012 isl_constraint_free(upper);
1013 isl_basic_set_free(context_i);
1014 isl_basic_set_free(context_j);
1015 isl_basic_set_free(context);
1019 /* For each pair of lower and upper bounds on the variable "pos"
1020 * of type "type", call "fn" with these lower and upper bounds and the
1021 * set of constraints on the remaining variables where these bounds
1022 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1024 * If the designated variable is equal to an affine combination of the
1025 * other variables then fn is called with both lower and upper
1026 * set to the corresponding equality.
1028 * If there is no lower (or upper) bound, then NULL is passed
1029 * as the corresponding bound.
1031 * We first check if the variable is involved in any equality.
1032 * If not, we count the number of lower and upper bounds and
1035 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1036 enum isl_dim_type type, unsigned pos,
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)
1042 isl_constraint *lower = NULL;
1043 isl_constraint *upper = NULL;
1044 isl_basic_set *context = NULL;
1046 int n_lower, n_upper;
1050 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1051 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1055 if (type == isl_dim_set)
1056 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1058 for (i = 0; i < bset->n_eq; ++i) {
1059 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1062 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1064 upper = isl_constraint_copy(lower);
1065 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1067 if (!lower || !upper || !context)
1069 return fn(lower, upper, context, user);
1074 for (i = 0; i < bset->n_ineq; ++i) {
1075 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1077 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1081 context = isl_basic_set_copy(bset);
1082 context = isl_basic_set_cow(context);
1085 for (i = context->n_ineq - 1; i >= 0; --i)
1086 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1087 isl_basic_set_drop_inequality(context, i);
1089 context = isl_basic_set_drop(context, type, pos, 1);
1090 if (!n_lower && !n_upper)
1091 return fn(NULL, NULL, context, user);
1093 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1096 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1098 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1101 isl_constraint_free(lower);
1102 isl_constraint_free(upper);
1103 isl_basic_set_free(context);
1107 __isl_give isl_aff *isl_constraint_get_bound(
1108 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1115 ctx = isl_constraint_get_ctx(constraint);
1116 if (pos >= isl_constraint_dim(constraint, type))
1117 isl_die(ctx, isl_error_invalid,
1118 "index out of bounds", return NULL);
1119 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
1120 isl_die(ctx, isl_error_invalid,
1121 "not a set constraint", return NULL);
1123 pos += offset(constraint, type);
1124 if (isl_int_is_zero(constraint->v->el[pos]))
1125 isl_die(ctx, isl_error_invalid,
1126 "constraint does not define a bound on given dimension",
1129 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1133 if (isl_int_is_neg(constraint->v->el[pos]))
1134 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1136 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1137 isl_int_set_si(aff->v->el[1 + pos], 0);
1138 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1143 /* For an inequality constraint
1147 * or an equality constraint
1151 * return the affine expression f.
1153 __isl_give isl_aff *isl_constraint_get_aff(
1154 __isl_keep isl_constraint *constraint)
1161 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1165 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1166 isl_int_set_si(aff->v->el[0], 1);
1171 /* Construct an equality constraint equating the given affine expression
1174 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1177 isl_local_space *ls;
1178 isl_basic_set *bset;
1183 ls = isl_aff_get_domain_local_space(aff);
1184 bset = isl_basic_set_from_local_space(ls);
1185 bset = isl_basic_set_extend_constraints(bset, 1, 0);
1186 k = isl_basic_set_alloc_equality(bset);
1190 isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1193 return isl_basic_set_constraint(bset, &bset->eq[k]);
1196 isl_basic_set_free(bset);
1200 /* Construct an inequality constraint enforcing the given affine expression
1201 * to be non-negative.
1203 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1206 isl_local_space *ls;
1207 isl_basic_set *bset;
1212 ls = isl_aff_get_domain_local_space(aff);
1213 bset = isl_basic_set_from_local_space(ls);
1214 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1215 k = isl_basic_set_alloc_inequality(bset);
1219 isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1222 return isl_basic_set_constraint(bset, &bset->ineq[k]);
1225 isl_basic_set_free(bset);