2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 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>
20 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
22 return c ? isl_local_space_get_ctx(c->ls) : NULL;
25 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
27 return isl_local_space_dim(c->ls, type);
30 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
32 return isl_local_space_offset(c->ls, type);
35 static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
36 enum isl_dim_type type)
38 return type == isl_dim_div ? 1 + isl_space_dim(bmap->dim, isl_dim_all)
39 : 1 + isl_space_offset(bmap->dim, type);
42 static unsigned basic_set_offset(struct isl_basic_set *bset,
43 enum isl_dim_type type)
45 isl_space *dim = bset->dim;
47 case isl_dim_param: return 1;
48 case isl_dim_in: return 1 + dim->nparam;
49 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
50 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
55 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
56 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
58 isl_constraint *constraint;
63 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
74 isl_local_space_free(ls);
79 __isl_give isl_constraint *isl_constraint_alloc(int eq,
80 __isl_take isl_local_space *ls)
88 ctx = isl_local_space_get_ctx(ls);
89 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
91 return isl_constraint_alloc_vec(eq, ls, v);
94 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
100 isl_local_space *ls = NULL;
101 isl_constraint *constraint;
106 eq = line >= bmap->eq;
108 ctx = isl_basic_map_get_ctx(bmap);
109 ls = isl_basic_map_get_local_space(bmap);
110 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
113 isl_seq_cpy(v->el, line[0], v->size);
114 constraint = isl_constraint_alloc_vec(eq, ls, v);
116 isl_basic_map_free(bmap);
119 isl_local_space_free(ls);
120 isl_basic_map_free(bmap);
124 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
127 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
130 __isl_give isl_constraint *isl_equality_alloc(__isl_take isl_local_space *ls)
132 return isl_constraint_alloc(1, ls);
135 __isl_give isl_constraint *isl_inequality_alloc(__isl_take isl_local_space *ls)
137 return isl_constraint_alloc(0, ls);
140 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
145 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
149 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
157 return isl_constraint_dup(c);
160 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
169 void *isl_constraint_free(struct isl_constraint *c)
177 isl_local_space_free(c->ls);
184 /* Return the number of constraints in "bset", i.e., the
185 * number of times isl_basic_set_foreach_constraint will
188 int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
193 return bset->n_eq + bset->n_ineq;
196 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
197 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
200 struct isl_constraint *c;
205 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
208 for (i = 0; i < bmap->n_eq; ++i) {
209 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
217 for (i = 0; i < bmap->n_ineq; ++i) {
218 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
229 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
230 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
232 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
235 int isl_constraint_is_equal(struct isl_constraint *constraint1,
236 struct isl_constraint *constraint2)
240 if (!constraint1 || !constraint2)
242 if (constraint1->eq != constraint2->eq)
244 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
245 if (equal < 0 || !equal)
247 return isl_vec_is_equal(constraint1->v, constraint2->v);
250 struct isl_basic_map *isl_basic_map_add_constraint(
251 struct isl_basic_map *bmap, struct isl_constraint *constraint)
257 if (!bmap || !constraint)
260 ctx = isl_constraint_get_ctx(constraint);
261 dim = isl_constraint_get_space(constraint);
262 equal_space = isl_space_is_equal(bmap->dim, dim);
264 isl_assert(ctx, equal_space, goto error);
266 bmap = isl_basic_map_intersect(bmap,
267 isl_basic_map_from_constraint(constraint));
270 isl_basic_map_free(bmap);
271 isl_constraint_free(constraint);
275 struct isl_basic_set *isl_basic_set_add_constraint(
276 struct isl_basic_set *bset, struct isl_constraint *constraint)
278 return (struct isl_basic_set *)
279 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
283 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
284 __isl_take isl_constraint *constraint)
288 bmap = isl_basic_map_from_constraint(constraint);
289 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
294 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
295 __isl_take isl_constraint *constraint)
297 return isl_map_add_constraint(set, constraint);
300 __isl_give isl_space *isl_constraint_get_space(
301 __isl_keep isl_constraint *constraint)
303 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
306 __isl_give isl_local_space *isl_constraint_get_local_space(
307 __isl_keep isl_constraint *constraint)
309 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
312 int isl_constraint_dim(struct isl_constraint *constraint,
313 enum isl_dim_type type)
317 return n(constraint, type);
320 int isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
321 enum isl_dim_type type, unsigned first, unsigned n)
333 ctx = isl_constraint_get_ctx(constraint);
334 if (first + n > isl_constraint_dim(constraint, type))
335 isl_die(ctx, isl_error_invalid,
336 "range out of bounds", return -1);
338 active = isl_local_space_get_active(constraint->ls,
339 constraint->v->el + 1);
343 first += isl_local_space_offset(constraint->ls, type) - 1;
344 for (i = 0; i < n; ++i)
345 if (active[first + i]) {
358 /* Does the given constraint represent a lower bound on the given
361 int isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
362 enum isl_dim_type type, unsigned pos)
367 if (pos >= isl_local_space_dim(constraint->ls, type))
368 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
369 "position out of bounds", return -1);
371 pos += isl_local_space_offset(constraint->ls, type);
372 return isl_int_is_pos(constraint->v->el[pos]);
375 /* Does the given constraint represent an upper bound on the given
378 int isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
379 enum isl_dim_type type, unsigned pos)
384 if (pos >= isl_local_space_dim(constraint->ls, type))
385 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
386 "position out of bounds", return -1);
388 pos += isl_local_space_offset(constraint->ls, type);
389 return isl_int_is_neg(constraint->v->el[pos]);
392 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
393 enum isl_dim_type type, unsigned pos)
396 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
399 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
403 isl_int_set(*v, constraint->v->el[0]);
406 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
407 enum isl_dim_type type, int pos, isl_int *v)
412 if (pos >= isl_local_space_dim(constraint->ls, type))
413 isl_die(constraint->v->ctx, isl_error_invalid,
414 "position out of bounds", return);
416 pos += isl_local_space_offset(constraint->ls, type);
417 isl_int_set(*v, constraint->v->el[pos]);
420 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
426 return isl_local_space_get_div(constraint->ls, pos);
429 __isl_give isl_constraint *isl_constraint_set_constant(
430 __isl_take isl_constraint *constraint, isl_int v)
432 constraint = isl_constraint_cow(constraint);
436 constraint->v = isl_vec_cow(constraint->v);
438 return isl_constraint_free(constraint);
440 isl_int_set(constraint->v->el[0], v);
444 __isl_give isl_constraint *isl_constraint_set_constant_si(
445 __isl_take isl_constraint *constraint, int v)
447 constraint = isl_constraint_cow(constraint);
451 constraint->v = isl_vec_cow(constraint->v);
453 return isl_constraint_free(constraint);
455 isl_int_set_si(constraint->v->el[0], v);
459 __isl_give isl_constraint *isl_constraint_set_coefficient(
460 __isl_take isl_constraint *constraint,
461 enum isl_dim_type type, int pos, isl_int v)
463 constraint = isl_constraint_cow(constraint);
467 if (pos >= isl_local_space_dim(constraint->ls, type))
468 isl_die(constraint->v->ctx, isl_error_invalid,
469 "position out of bounds",
470 return isl_constraint_free(constraint));
472 constraint = isl_constraint_cow(constraint);
476 constraint->v = isl_vec_cow(constraint->v);
478 return isl_constraint_free(constraint);
480 pos += isl_local_space_offset(constraint->ls, type);
481 isl_int_set(constraint->v->el[pos], v);
486 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
487 __isl_take isl_constraint *constraint,
488 enum isl_dim_type type, int pos, int v)
490 constraint = isl_constraint_cow(constraint);
494 if (pos >= isl_local_space_dim(constraint->ls, type))
495 isl_die(constraint->v->ctx, isl_error_invalid,
496 "position out of bounds",
497 return isl_constraint_free(constraint));
499 constraint = isl_constraint_cow(constraint);
503 constraint->v = isl_vec_cow(constraint->v);
505 return isl_constraint_free(constraint);
507 pos += isl_local_space_offset(constraint->ls, type);
508 isl_int_set_si(constraint->v->el[pos], v);
513 /* Drop any constraint from "bset" that is identical to "constraint".
514 * In particular, this means that the local spaces of "bset" and
515 * "constraint" need to be the same.
517 * Since the given constraint may actually be a pointer into the bset,
518 * we have to be careful not to reorder the constraints as the user
519 * may be holding on to other constraints from the same bset.
520 * This should be cleaned up when the internal representation of
521 * isl_constraint is changed to use isl_aff.
523 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
524 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
530 isl_local_space *ls1;
533 if (!bset || !constraint)
536 ls1 = isl_basic_set_get_local_space(bset);
537 equal = isl_local_space_is_equal(ls1, constraint->ls);
538 isl_local_space_free(ls1);
542 isl_constraint_free(constraint);
546 if (isl_constraint_is_equality(constraint)) {
554 total = isl_constraint_dim(constraint, isl_dim_all);
555 for (i = 0; i < n; ++i)
556 if (isl_seq_eq(row[i], constraint->v->el, 1 + total))
557 isl_seq_clr(row[i], 1 + total);
559 isl_constraint_free(constraint);
562 isl_constraint_free(constraint);
563 isl_basic_set_free(bset);
567 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
571 constraint = isl_constraint_cow(constraint);
575 ctx = isl_constraint_get_ctx(constraint);
576 if (isl_constraint_is_equality(constraint))
577 isl_die(ctx, isl_error_invalid, "cannot negate equality",
578 return isl_constraint_free(constraint));
579 constraint->v = isl_vec_neg(constraint->v);
580 constraint->v = isl_vec_cow(constraint->v);
582 return isl_constraint_free(constraint);
583 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
587 int isl_constraint_is_equality(struct isl_constraint *constraint)
591 return constraint->eq;
594 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
601 if (isl_constraint_is_equality(constraint))
603 n_div = isl_constraint_dim(constraint, isl_dim_div);
604 for (i = 0; i < n_div; ++i) {
605 if (isl_local_space_is_div_constraint(constraint->ls,
606 constraint->v->el, i))
613 /* We manually set ISL_BASIC_SET_FINAL instead of calling
614 * isl_basic_map_finalize because we want to keep the position
615 * of the divs and we therefore do not want to throw away redundant divs.
616 * This is arguably a bit fragile.
618 __isl_give isl_basic_map *isl_basic_map_from_constraint(
619 __isl_take isl_constraint *constraint)
623 struct isl_basic_map *bmap;
630 ls = isl_local_space_copy(constraint->ls);
631 bmap = isl_basic_map_from_local_space(ls);
632 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
633 if (isl_constraint_is_equality(constraint)) {
634 k = isl_basic_map_alloc_equality(bmap);
640 k = isl_basic_map_alloc_inequality(bmap);
645 total = isl_basic_map_total_dim(bmap);
646 isl_seq_cpy(c, constraint->v->el, 1 + total);
647 isl_constraint_free(constraint);
649 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
652 isl_constraint_free(constraint);
653 isl_basic_map_free(bmap);
657 struct isl_basic_set *isl_basic_set_from_constraint(
658 struct isl_constraint *constraint)
663 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
664 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
665 "not a set constraint",
666 return isl_constraint_free(constraint));
667 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
670 int isl_basic_map_has_defining_equality(
671 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
672 __isl_give isl_constraint **c)
680 offset = basic_map_offset(bmap, type);
681 total = isl_basic_map_total_dim(bmap);
682 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
683 for (i = 0; i < bmap->n_eq; ++i)
684 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
685 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
686 1+total-offset-pos-1) == -1) {
687 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
694 int isl_basic_set_has_defining_equality(
695 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
696 __isl_give isl_constraint **c)
698 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
702 int isl_basic_set_has_defining_inequalities(
703 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
704 struct isl_constraint **lower,
705 struct isl_constraint **upper)
711 isl_int **lower_line, **upper_line;
715 offset = basic_set_offset(bset, type);
716 total = isl_basic_set_total_dim(bset);
717 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
719 for (i = 0; i < bset->n_ineq; ++i) {
720 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
722 if (isl_int_is_one(bset->ineq[i][offset + pos]))
724 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
726 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
727 1+total-offset-pos-1) != -1)
729 for (j = i + 1; j < bset->n_ineq; ++j) {
730 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
733 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
734 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
737 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
738 lower_line = &bset->ineq[i];
739 upper_line = &bset->ineq[j];
741 lower_line = &bset->ineq[j];
742 upper_line = &bset->ineq[i];
744 *lower = isl_basic_set_constraint(
745 isl_basic_set_copy(bset), lower_line);
746 *upper = isl_basic_set_constraint(
747 isl_basic_set_copy(bset), upper_line);
758 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
759 * (in "a" and "b"), add a constraint to "bset" that ensures that the
760 * bound implied by "a" is (strictly) larger than the bound implied by "b".
762 * If both constraints imply lower bounds, then this means that "a" is
763 * active in the result.
764 * If both constraints imply upper bounds, then this means that "b" is
765 * active in the result.
767 static __isl_give isl_basic_set *add_larger_bound_constraint(
768 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
769 unsigned abs_pos, int strict)
775 k = isl_basic_set_alloc_inequality(bset);
779 total = isl_basic_set_dim(bset, isl_dim_all);
782 isl_int_neg(t, b[1 + abs_pos]);
784 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
785 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
786 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
790 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
796 isl_basic_set_free(bset);
800 /* Add constraints to "context" that ensure that "u" is the smallest
801 * (and therefore active) upper bound on "abs_pos" in "bset" and return
802 * the resulting basic set.
804 static __isl_give isl_basic_set *set_smallest_upper_bound(
805 __isl_keep isl_basic_set *context,
806 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
810 context = isl_basic_set_copy(context);
811 context = isl_basic_set_cow(context);
813 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
815 for (j = 0; j < bset->n_ineq; ++j) {
818 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
820 context = add_larger_bound_constraint(context,
821 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
824 context = isl_basic_set_simplify(context);
825 context = isl_basic_set_finalize(context);
830 /* Add constraints to "context" that ensure that "u" is the largest
831 * (and therefore active) upper bound on "abs_pos" in "bset" and return
832 * the resulting basic set.
834 static __isl_give isl_basic_set *set_largest_lower_bound(
835 __isl_keep isl_basic_set *context,
836 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
840 context = isl_basic_set_copy(context);
841 context = isl_basic_set_cow(context);
843 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
845 for (j = 0; j < bset->n_ineq; ++j) {
848 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
850 context = add_larger_bound_constraint(context,
851 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
854 context = isl_basic_set_simplify(context);
855 context = isl_basic_set_finalize(context);
860 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
861 enum isl_dim_type type, unsigned abs_pos,
862 __isl_take isl_basic_set *context, int n_upper,
863 int (*fn)(__isl_take isl_constraint *lower,
864 __isl_take isl_constraint *upper,
865 __isl_take isl_basic_set *bset, void *user), void *user)
867 isl_basic_set *context_i;
868 isl_constraint *upper = NULL;
871 for (i = 0; i < bset->n_ineq; ++i) {
872 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
875 context_i = set_smallest_upper_bound(context, bset,
876 abs_pos, n_upper, i);
877 if (isl_basic_set_is_empty(context_i)) {
878 isl_basic_set_free(context_i);
881 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
883 if (!upper || !context_i)
885 if (fn(NULL, upper, context_i, user) < 0)
889 isl_basic_set_free(context);
891 if (i < bset->n_ineq)
896 isl_constraint_free(upper);
897 isl_basic_set_free(context_i);
898 isl_basic_set_free(context);
902 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
903 enum isl_dim_type type, unsigned abs_pos,
904 __isl_take isl_basic_set *context, int n_lower,
905 int (*fn)(__isl_take isl_constraint *lower,
906 __isl_take isl_constraint *upper,
907 __isl_take isl_basic_set *bset, void *user), void *user)
909 isl_basic_set *context_i;
910 isl_constraint *lower = NULL;
913 for (i = 0; i < bset->n_ineq; ++i) {
914 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
917 context_i = set_largest_lower_bound(context, bset,
918 abs_pos, n_lower, i);
919 if (isl_basic_set_is_empty(context_i)) {
920 isl_basic_set_free(context_i);
923 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
925 if (!lower || !context_i)
927 if (fn(lower, NULL, context_i, user) < 0)
931 isl_basic_set_free(context);
933 if (i < bset->n_ineq)
938 isl_constraint_free(lower);
939 isl_basic_set_free(context_i);
940 isl_basic_set_free(context);
944 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
945 enum isl_dim_type type, unsigned abs_pos,
946 __isl_take isl_basic_set *context, int n_lower, int n_upper,
947 int (*fn)(__isl_take isl_constraint *lower,
948 __isl_take isl_constraint *upper,
949 __isl_take isl_basic_set *bset, void *user), void *user)
951 isl_basic_set *context_i, *context_j;
952 isl_constraint *lower = NULL;
953 isl_constraint *upper = NULL;
956 for (i = 0; i < bset->n_ineq; ++i) {
957 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
960 context_i = set_largest_lower_bound(context, bset,
961 abs_pos, n_lower, i);
962 if (isl_basic_set_is_empty(context_i)) {
963 isl_basic_set_free(context_i);
967 for (j = 0; j < bset->n_ineq; ++j) {
968 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
971 context_j = set_smallest_upper_bound(context_i, bset,
972 abs_pos, n_upper, j);
973 context_j = isl_basic_set_extend_constraints(context_j,
975 context_j = add_larger_bound_constraint(context_j,
976 bset->ineq[i], bset->ineq[j], abs_pos, 0);
977 context_j = isl_basic_set_simplify(context_j);
978 context_j = isl_basic_set_finalize(context_j);
979 if (isl_basic_set_is_empty(context_j)) {
980 isl_basic_set_free(context_j);
983 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
985 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
987 if (!lower || !upper || !context_j)
989 if (fn(lower, upper, context_j, user) < 0)
993 isl_basic_set_free(context_i);
995 if (j < bset->n_ineq)
999 isl_basic_set_free(context);
1001 if (i < bset->n_ineq)
1006 isl_constraint_free(lower);
1007 isl_constraint_free(upper);
1008 isl_basic_set_free(context_i);
1009 isl_basic_set_free(context_j);
1010 isl_basic_set_free(context);
1014 /* For each pair of lower and upper bounds on the variable "pos"
1015 * of type "type", call "fn" with these lower and upper bounds and the
1016 * set of constraints on the remaining variables where these bounds
1017 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1019 * If the designated variable is equal to an affine combination of the
1020 * other variables then fn is called with both lower and upper
1021 * set to the corresponding equality.
1023 * If there is no lower (or upper) bound, then NULL is passed
1024 * as the corresponding bound.
1026 * We first check if the variable is involved in any equality.
1027 * If not, we count the number of lower and upper bounds and
1030 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1031 enum isl_dim_type type, unsigned pos,
1032 int (*fn)(__isl_take isl_constraint *lower,
1033 __isl_take isl_constraint *upper,
1034 __isl_take isl_basic_set *bset, void *user), void *user)
1037 isl_constraint *lower = NULL;
1038 isl_constraint *upper = NULL;
1039 isl_basic_set *context = NULL;
1041 int n_lower, n_upper;
1045 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1046 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1050 if (type == isl_dim_set)
1051 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1053 for (i = 0; i < bset->n_eq; ++i) {
1054 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1057 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1059 upper = isl_constraint_copy(lower);
1060 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1062 if (!lower || !upper || !context)
1064 return fn(lower, upper, context, user);
1069 for (i = 0; i < bset->n_ineq; ++i) {
1070 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1072 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1076 context = isl_basic_set_copy(bset);
1077 context = isl_basic_set_cow(context);
1080 for (i = context->n_ineq - 1; i >= 0; --i)
1081 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1082 isl_basic_set_drop_inequality(context, i);
1084 context = isl_basic_set_drop(context, type, pos, 1);
1085 if (!n_lower && !n_upper)
1086 return fn(NULL, NULL, context, user);
1088 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1091 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1093 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1096 isl_constraint_free(lower);
1097 isl_constraint_free(upper);
1098 isl_basic_set_free(context);
1102 __isl_give isl_aff *isl_constraint_get_bound(
1103 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1110 ctx = isl_constraint_get_ctx(constraint);
1111 if (pos >= isl_constraint_dim(constraint, type))
1112 isl_die(ctx, isl_error_invalid,
1113 "index out of bounds", return NULL);
1114 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
1115 isl_die(ctx, isl_error_invalid,
1116 "not a set constraint", return NULL);
1118 pos += offset(constraint, type);
1119 if (isl_int_is_zero(constraint->v->el[pos]))
1120 isl_die(ctx, isl_error_invalid,
1121 "constraint does not define a bound on given dimension",
1124 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1128 if (isl_int_is_neg(constraint->v->el[pos]))
1129 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1131 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1132 isl_int_set_si(aff->v->el[1 + pos], 0);
1133 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1138 /* For an inequality constraint
1142 * or an equality constraint
1146 * return the affine expression f.
1148 __isl_give isl_aff *isl_constraint_get_aff(
1149 __isl_keep isl_constraint *constraint)
1156 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1160 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1161 isl_int_set_si(aff->v->el[0], 1);
1166 /* Construct an equality constraint equating the given affine expression
1169 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1172 isl_local_space *ls;
1173 isl_basic_set *bset;
1178 ls = isl_aff_get_domain_local_space(aff);
1179 bset = isl_basic_set_from_local_space(ls);
1180 bset = isl_basic_set_extend_constraints(bset, 1, 0);
1181 k = isl_basic_set_alloc_equality(bset);
1185 isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1188 return isl_basic_set_constraint(bset, &bset->eq[k]);
1191 isl_basic_set_free(bset);
1195 /* Construct an inequality constraint enforcing the given affine expression
1196 * to be non-negative.
1198 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1201 isl_local_space *ls;
1202 isl_basic_set *bset;
1207 ls = isl_aff_get_domain_local_space(aff);
1208 bset = isl_basic_set_from_local_space(ls);
1209 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1210 k = isl_basic_set_alloc_inequality(bset);
1214 isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1217 return isl_basic_set_constraint(bset, &bset->ineq[k]);
1220 isl_basic_set_free(bset);