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_dim_private.h>
16 #include <isl_div_private.h>
18 #include <isl_aff_private.h>
20 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
22 return c ? c->ctx : NULL;
25 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
27 return isl_basic_map_dim(c->bmap, type);
30 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
32 struct isl_dim *dim = c->bmap->dim;
34 case isl_dim_param: return 1;
35 case isl_dim_in: return 1 + dim->nparam;
36 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
37 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
42 static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
43 enum isl_dim_type type)
45 return type == isl_dim_div ? 1 + isl_dim_total(bmap->dim)
46 : 1 + isl_dim_offset(bmap->dim, type);
49 static unsigned basic_set_offset(struct isl_basic_set *bset,
50 enum isl_dim_type type)
52 struct isl_dim *dim = bset->dim;
54 case isl_dim_param: return 1;
55 case isl_dim_in: return 1 + dim->nparam;
56 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
57 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
62 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
65 struct isl_constraint *constraint;
70 constraint = isl_alloc_type(bmap->ctx, struct isl_constraint);
74 constraint->ctx = bmap->ctx;
75 isl_ctx_ref(constraint->ctx);
77 constraint->bmap = bmap;
78 constraint->line = line;
82 isl_basic_map_free(bmap);
86 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
89 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
92 struct isl_constraint *isl_equality_alloc(struct isl_dim *dim)
94 struct isl_basic_map *bmap;
99 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
103 isl_basic_map_alloc_equality(bmap);
104 isl_seq_clr(bmap->eq[0], 1 + isl_basic_map_total_dim(bmap));
105 return isl_basic_map_constraint(bmap, &bmap->eq[0]);
108 struct isl_constraint *isl_inequality_alloc(struct isl_dim *dim)
110 struct isl_basic_map *bmap;
115 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 1);
119 isl_basic_map_alloc_inequality(bmap);
120 isl_seq_clr(bmap->ineq[0], 1 + isl_basic_map_total_dim(bmap));
121 return isl_basic_map_constraint(bmap, &bmap->ineq[0]);
124 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
126 struct isl_basic_map *bmap;
133 eq = c->line < c->bmap->eq + c->bmap->n_eq;
134 i = eq ? c->line - c->bmap->eq : c->line - c->bmap->ineq;
135 bmap = isl_basic_map_copy(c->bmap);
138 return isl_basic_map_constraint(bmap, eq ? bmap->eq + i : bmap->ineq + i);
141 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
149 return isl_constraint_dup(c);
152 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
161 void isl_constraint_free(struct isl_constraint *c)
169 isl_basic_map_free(c->bmap);
170 isl_ctx_deref(c->ctx);
174 __isl_give isl_constraint *isl_basic_map_first_constraint(
175 __isl_take isl_basic_map *bmap)
181 return isl_basic_map_constraint(bmap, &bmap->eq[0]);
183 if (bmap->n_ineq > 0)
184 return isl_basic_map_constraint(bmap, &bmap->ineq[0]);
186 isl_basic_map_free(bmap);
190 __isl_give isl_constraint *isl_basic_set_first_constraint(
191 __isl_take isl_basic_set *bset)
193 return isl_basic_map_first_constraint((struct isl_basic_map *)bset);
196 struct isl_constraint *isl_constraint_next(struct isl_constraint *c)
198 c = isl_constraint_cow(c);
199 if (c->line >= c->bmap->eq) {
201 if (c->line < c->bmap->eq + c->bmap->n_eq)
203 c->line = c->bmap->ineq;
206 if (c->line < c->bmap->ineq + c->bmap->n_ineq)
208 isl_constraint_free(c);
212 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
213 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
216 struct isl_constraint *c;
221 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
224 for (i = 0; i < bmap->n_eq; ++i) {
225 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
233 for (i = 0; i < bmap->n_ineq; ++i) {
234 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
245 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
246 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
248 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
251 int isl_constraint_is_equal(struct isl_constraint *constraint1,
252 struct isl_constraint *constraint2)
254 if (!constraint1 || !constraint2)
256 return constraint1->bmap == constraint2->bmap &&
257 constraint1->line == constraint2->line;
260 struct isl_basic_map *isl_basic_map_add_constraint(
261 struct isl_basic_map *bmap, struct isl_constraint *constraint)
263 if (!bmap || !constraint)
266 isl_assert(constraint->ctx,
267 isl_dim_equal(bmap->dim, constraint->bmap->dim), goto error);
269 bmap = isl_basic_map_intersect(bmap,
270 isl_basic_map_from_constraint(constraint));
273 isl_basic_map_free(bmap);
274 isl_constraint_free(constraint);
278 struct isl_basic_set *isl_basic_set_add_constraint(
279 struct isl_basic_set *bset, struct isl_constraint *constraint)
281 return (struct isl_basic_set *)
282 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
286 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
287 __isl_take isl_constraint *constraint)
291 bmap = isl_basic_map_from_constraint(constraint);
292 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
297 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
298 __isl_take isl_constraint *constraint)
300 return isl_map_add_constraint(set, constraint);
303 struct isl_constraint *isl_constraint_add_div(struct isl_constraint *constraint,
304 struct isl_div *div, int *pos)
306 if (!constraint || !div)
309 isl_assert(constraint->ctx,
310 isl_dim_equal(div->bmap->dim, constraint->bmap->dim), goto error);
311 isl_assert(constraint->ctx,
312 constraint->bmap->n_eq + constraint->bmap->n_ineq == 1, goto error);
314 constraint->bmap = isl_basic_map_cow(constraint->bmap);
315 constraint->bmap = isl_basic_map_extend_dim(constraint->bmap,
316 isl_dim_copy(constraint->bmap->dim), 1, 0, 0);
317 if (!constraint->bmap)
319 constraint->line = &constraint->bmap->ineq[0];
320 *pos = isl_basic_map_alloc_div(constraint->bmap);
323 isl_seq_cpy(constraint->bmap->div[*pos], div->line[0],
324 1 + 1 + isl_basic_map_total_dim(constraint->bmap));
328 isl_constraint_free(constraint);
333 __isl_give isl_dim *isl_constraint_get_dim(
334 __isl_keep isl_constraint *constraint)
336 return constraint ? isl_basic_map_get_dim(constraint->bmap) : NULL;
339 int isl_constraint_dim(struct isl_constraint *constraint,
340 enum isl_dim_type type)
344 return n(constraint, type);
347 int isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
348 enum isl_dim_type type, unsigned first, unsigned n)
353 if (first + n > isl_basic_set_dim(constraint->bmap, type))
354 isl_die(constraint->ctx, isl_error_invalid,
355 "index out of bounds", return -1);
357 first += isl_basic_map_offset(constraint->bmap, type);
359 if (isl_seq_first_non_zero(constraint->line[0] + first, n) >= 0)
365 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
366 enum isl_dim_type type, unsigned pos)
369 isl_basic_map_get_dim_name(constraint->bmap, type, pos) : NULL;
372 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
376 isl_int_set(*v, constraint->line[0][0]);
379 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
380 enum isl_dim_type type, int pos, isl_int *v)
385 isl_assert(constraint->ctx, pos < n(constraint, type), return);
386 isl_int_set(*v, constraint->line[0][offset(constraint, type) + pos]);
389 struct isl_div *isl_constraint_div(struct isl_constraint *constraint, int pos)
394 isl_assert(constraint->ctx, pos < n(constraint, isl_dim_div),
396 isl_assert(constraint->ctx,
397 !isl_int_is_zero(constraint->bmap->div[pos][0]), return NULL);
398 return isl_basic_map_div(isl_basic_map_copy(constraint->bmap), pos);
401 void isl_constraint_set_constant(struct isl_constraint *constraint, isl_int v)
405 isl_int_set(constraint->line[0][0], v);
408 void isl_constraint_set_constant_si(__isl_keep isl_constraint *constraint,
413 isl_int_set_si(constraint->line[0][0], v);
416 void isl_constraint_set_coefficient(struct isl_constraint *constraint,
417 enum isl_dim_type type, int pos, isl_int v)
422 isl_assert(constraint->ctx, pos < n(constraint, type), return);
423 isl_int_set(constraint->line[0][offset(constraint, type) + pos], v);
426 void isl_constraint_set_coefficient_si(__isl_keep isl_constraint *constraint,
427 enum isl_dim_type type, int pos, int v)
432 isl_assert(constraint->ctx, pos < n(constraint, type), return);
433 isl_int_set_si(constraint->line[0][offset(constraint, type) + pos], v);
436 void isl_constraint_clear(struct isl_constraint *constraint)
442 total = isl_basic_map_total_dim(constraint->bmap);
443 isl_seq_clr(constraint->line[0], 1 + total);
446 /* Check whether the two basic maps have identical divs in the same order.
448 static int equal_divs(__isl_keep isl_basic_map *bmap1,
449 __isl_keep isl_basic_map *bmap2)
454 if (!isl_basic_map_divs_known(bmap1))
456 if (!isl_basic_map_divs_known(bmap2))
458 if (bmap1->n_div != bmap2->n_div)
461 total = isl_basic_map_total_dim(bmap1);
462 for (i = 0; i < bmap1->n_div; ++i)
463 if (!isl_seq_eq(bmap1->div[i], bmap2->div[i], 2 + total))
469 /* Drop any constraint from "bset" that is identical to "constraint".
470 * In particular, this means that the local spaces of "bset" and
471 * "constraint" need to be the same.
473 * Since the given constraint may actually be a pointer into the bset,
474 * we have to be careful not to reorder the constraints as the user
475 * may be holding on to other constraints from the same bset.
476 * This should be cleaned up when the internal representation of
477 * isl_constraint is changed to use isl_aff.
479 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
480 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
487 if (!bset || !constraint)
490 if (!isl_dim_equal(bset->dim, constraint->bmap->dim))
491 isl_die(bset->ctx, isl_error_invalid,
492 "spaces don't match", goto error);
494 if (bset != constraint->bmap && !equal_divs(bset, constraint->bmap)) {
495 isl_constraint_free(constraint);
499 if (isl_constraint_is_equality(constraint)) {
507 total = isl_basic_map_total_dim(constraint->bmap);
508 for (i = 0; i < n; ++i)
509 if (isl_seq_eq(row[i], constraint->line[0], 1 + total))
510 isl_seq_clr(row[i], 1 + total);
512 isl_constraint_free(constraint);
515 isl_constraint_free(constraint);
516 isl_basic_set_free(bset);
520 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
527 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
529 isl_assert(constraint->ctx, constraint->bmap->ref == 1, goto error);
530 total = isl_basic_map_total_dim(constraint->bmap);
531 isl_seq_neg(constraint->line[0], constraint->line[0], 1 + total);
532 isl_int_sub_ui(constraint->line[0][0], constraint->line[0][0], 1);
533 ISL_F_CLR(constraint->bmap, ISL_BASIC_MAP_NORMALIZED);
536 isl_constraint_free(constraint);
540 int isl_constraint_is_equality(struct isl_constraint *constraint)
544 return constraint->line >= constraint->bmap->eq;
547 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
553 if (isl_constraint_is_equality(constraint))
555 for (i = 0; i < constraint->bmap->n_div; ++i) {
556 if (isl_int_is_zero(constraint->bmap->div[i][0]))
558 if (isl_basic_map_is_div_constraint(constraint->bmap,
559 constraint->line[0], i))
566 /* We manually set ISL_BASIC_SET_FINAL instead of calling
567 * isl_basic_map_finalize because we want to keep the position
568 * of the divs and we therefore do not want to throw away redundant divs.
569 * This is arguably a bit fragile.
571 __isl_give isl_basic_map *isl_basic_map_from_constraint(
572 __isl_take isl_constraint *constraint)
575 struct isl_basic_map *bmap;
582 if (constraint->bmap->n_eq == 1 && constraint->bmap->n_ineq == 0) {
583 bmap = isl_basic_map_copy(constraint->bmap);
584 isl_constraint_free(constraint);
588 bmap = isl_basic_map_universe_like(constraint->bmap);
589 bmap = isl_basic_map_align_divs(bmap, constraint->bmap);
590 bmap = isl_basic_map_cow(bmap);
591 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
592 if (isl_constraint_is_equality(constraint)) {
593 k = isl_basic_map_alloc_equality(bmap);
599 k = isl_basic_map_alloc_inequality(bmap);
604 total = isl_basic_map_total_dim(bmap);
605 isl_seq_cpy(c, constraint->line[0], 1 + total);
606 isl_constraint_free(constraint);
608 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
611 isl_constraint_free(constraint);
612 isl_basic_map_free(bmap);
616 struct isl_basic_set *isl_basic_set_from_constraint(
617 struct isl_constraint *constraint)
622 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
623 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
625 isl_constraint_free(constraint);
629 int isl_basic_map_has_defining_equality(
630 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
631 __isl_give isl_constraint **c)
639 offset = basic_map_offset(bmap, type);
640 total = isl_basic_map_total_dim(bmap);
641 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
642 for (i = 0; i < bmap->n_eq; ++i)
643 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
644 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
645 1+total-offset-pos-1) == -1) {
646 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
653 int isl_basic_set_has_defining_equality(
654 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
655 __isl_give isl_constraint **c)
657 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
661 int isl_basic_set_has_defining_inequalities(
662 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
663 struct isl_constraint **lower,
664 struct isl_constraint **upper)
670 isl_int **lower_line, **upper_line;
674 offset = basic_set_offset(bset, type);
675 total = isl_basic_set_total_dim(bset);
676 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
678 for (i = 0; i < bset->n_ineq; ++i) {
679 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
681 if (isl_int_is_one(bset->ineq[i][offset + pos]))
683 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
685 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
686 1+total-offset-pos-1) != -1)
688 for (j = i + 1; j < bset->n_ineq; ++j) {
689 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
692 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
693 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
696 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
697 lower_line = &bset->ineq[i];
698 upper_line = &bset->ineq[j];
700 lower_line = &bset->ineq[j];
701 upper_line = &bset->ineq[i];
703 *lower = isl_basic_set_constraint(
704 isl_basic_set_copy(bset), lower_line);
705 *upper = isl_basic_set_constraint(
706 isl_basic_set_copy(bset), upper_line);
717 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
718 * (in "a" and "b"), add a constraint to "bset" that ensures that the
719 * bound implied by "a" is (strictly) larger than the bound implied by "b".
721 * If both constraints imply lower bounds, then this means that "a" is
722 * active in the result.
723 * If both constraints imply upper bounds, then this means that "b" is
724 * active in the result.
726 static __isl_give isl_basic_set *add_larger_bound_constraint(
727 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
728 unsigned abs_pos, int strict)
734 k = isl_basic_set_alloc_inequality(bset);
738 total = isl_basic_set_dim(bset, isl_dim_all);
741 isl_int_neg(t, b[1 + abs_pos]);
743 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
744 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
745 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
749 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
755 isl_basic_set_free(bset);
759 /* Add constraints to "context" that ensure that "u" is the smallest
760 * (and therefore active) upper bound on "abs_pos" in "bset" and return
761 * the resulting basic set.
763 static __isl_give isl_basic_set *set_smallest_upper_bound(
764 __isl_keep isl_basic_set *context,
765 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
769 context = isl_basic_set_copy(context);
770 context = isl_basic_set_cow(context);
772 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
774 for (j = 0; j < bset->n_ineq; ++j) {
777 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
779 context = add_larger_bound_constraint(context,
780 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
783 context = isl_basic_set_simplify(context);
784 context = isl_basic_set_finalize(context);
789 /* Add constraints to "context" that ensure that "u" is the largest
790 * (and therefore active) upper bound on "abs_pos" in "bset" and return
791 * the resulting basic set.
793 static __isl_give isl_basic_set *set_largest_lower_bound(
794 __isl_keep isl_basic_set *context,
795 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
799 context = isl_basic_set_copy(context);
800 context = isl_basic_set_cow(context);
802 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
804 for (j = 0; j < bset->n_ineq; ++j) {
807 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
809 context = add_larger_bound_constraint(context,
810 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
813 context = isl_basic_set_simplify(context);
814 context = isl_basic_set_finalize(context);
819 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
820 enum isl_dim_type type, unsigned abs_pos,
821 __isl_take isl_basic_set *context, int n_upper,
822 int (*fn)(__isl_take isl_constraint *lower,
823 __isl_take isl_constraint *upper,
824 __isl_take isl_basic_set *bset, void *user), void *user)
826 isl_basic_set *context_i;
827 isl_constraint *upper = NULL;
830 for (i = 0; i < bset->n_ineq; ++i) {
831 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
834 context_i = set_smallest_upper_bound(context, bset,
835 abs_pos, n_upper, i);
836 if (isl_basic_set_is_empty(context_i)) {
837 isl_basic_set_free(context_i);
840 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
842 if (!upper || !context_i)
844 if (fn(NULL, upper, context_i, user) < 0)
848 isl_basic_set_free(context);
850 if (i < bset->n_ineq)
855 isl_constraint_free(upper);
856 isl_basic_set_free(context_i);
857 isl_basic_set_free(context);
861 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
862 enum isl_dim_type type, unsigned abs_pos,
863 __isl_take isl_basic_set *context, int n_lower,
864 int (*fn)(__isl_take isl_constraint *lower,
865 __isl_take isl_constraint *upper,
866 __isl_take isl_basic_set *bset, void *user), void *user)
868 isl_basic_set *context_i;
869 isl_constraint *lower = NULL;
872 for (i = 0; i < bset->n_ineq; ++i) {
873 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
876 context_i = set_largest_lower_bound(context, bset,
877 abs_pos, n_lower, i);
878 if (isl_basic_set_is_empty(context_i)) {
879 isl_basic_set_free(context_i);
882 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
884 if (!lower || !context_i)
886 if (fn(lower, NULL, context_i, user) < 0)
890 isl_basic_set_free(context);
892 if (i < bset->n_ineq)
897 isl_constraint_free(lower);
898 isl_basic_set_free(context_i);
899 isl_basic_set_free(context);
903 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
904 enum isl_dim_type type, unsigned abs_pos,
905 __isl_take isl_basic_set *context, int n_lower, int n_upper,
906 int (*fn)(__isl_take isl_constraint *lower,
907 __isl_take isl_constraint *upper,
908 __isl_take isl_basic_set *bset, void *user), void *user)
910 isl_basic_set *context_i, *context_j;
911 isl_constraint *lower = NULL;
912 isl_constraint *upper = NULL;
915 for (i = 0; i < bset->n_ineq; ++i) {
916 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
919 context_i = set_largest_lower_bound(context, bset,
920 abs_pos, n_lower, i);
921 if (isl_basic_set_is_empty(context_i)) {
922 isl_basic_set_free(context_i);
926 for (j = 0; j < bset->n_ineq; ++j) {
927 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
930 context_j = set_smallest_upper_bound(context_i, bset,
931 abs_pos, n_upper, j);
932 context_j = isl_basic_set_extend_constraints(context_j,
934 context_j = add_larger_bound_constraint(context_j,
935 bset->ineq[i], bset->ineq[j], abs_pos, 0);
936 context_j = isl_basic_set_simplify(context_j);
937 context_j = isl_basic_set_finalize(context_j);
938 if (isl_basic_set_is_empty(context_j)) {
939 isl_basic_set_free(context_j);
942 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
944 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
946 if (!lower || !upper || !context_j)
948 if (fn(lower, upper, context_j, user) < 0)
952 isl_basic_set_free(context_i);
954 if (j < bset->n_ineq)
958 isl_basic_set_free(context);
960 if (i < bset->n_ineq)
965 isl_constraint_free(lower);
966 isl_constraint_free(upper);
967 isl_basic_set_free(context_i);
968 isl_basic_set_free(context_j);
969 isl_basic_set_free(context);
973 /* For each pair of lower and upper bounds on the variable "pos"
974 * of type "type", call "fn" with these lower and upper bounds and the
975 * set of constraints on the remaining variables where these bounds
976 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
978 * If the designated variable is equal to an affine combination of the
979 * other variables then fn is called with both lower and upper
980 * set to the corresponding equality.
982 * If there is no lower (or upper) bound, then NULL is passed
983 * as the corresponding bound.
985 * We first check if the variable is involved in any equality.
986 * If not, we count the number of lower and upper bounds and
989 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
990 enum isl_dim_type type, unsigned pos,
991 int (*fn)(__isl_take isl_constraint *lower,
992 __isl_take isl_constraint *upper,
993 __isl_take isl_basic_set *bset, void *user), void *user)
996 isl_constraint *lower = NULL;
997 isl_constraint *upper = NULL;
998 isl_basic_set *context = NULL;
1000 int n_lower, n_upper;
1004 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1005 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1009 if (type == isl_dim_set)
1010 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1012 for (i = 0; i < bset->n_eq; ++i) {
1013 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1016 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1018 upper = isl_constraint_copy(lower);
1019 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1021 if (!lower || !upper || !context)
1023 return fn(lower, upper, context, user);
1028 for (i = 0; i < bset->n_ineq; ++i) {
1029 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1031 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1035 context = isl_basic_set_copy(bset);
1036 context = isl_basic_set_cow(context);
1039 for (i = context->n_ineq - 1; i >= 0; --i)
1040 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1041 isl_basic_set_drop_inequality(context, i);
1043 context = isl_basic_set_drop(context, type, pos, 1);
1044 if (!n_lower && !n_upper)
1045 return fn(NULL, NULL, context, user);
1047 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1050 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1052 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1055 isl_constraint_free(lower);
1056 isl_constraint_free(upper);
1057 isl_basic_set_free(context);
1061 __isl_give isl_aff *isl_constraint_get_bound(
1062 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1065 isl_local_space *ls;
1069 if (pos >= isl_basic_set_dim(constraint->bmap, type))
1070 isl_die(constraint->ctx, isl_error_invalid,
1071 "index out of bounds", return NULL);
1072 if (!isl_basic_map_may_be_set(constraint->bmap))
1073 isl_die(constraint->ctx, isl_error_invalid,
1074 "not a set constraint", return NULL);
1076 pos += offset(constraint, type);
1077 if (isl_int_is_zero(constraint->line[0][pos]))
1078 isl_die(constraint->ctx, isl_error_invalid,
1079 "constraint does not define a bound on given dimension",
1082 ls = isl_basic_set_get_local_space(constraint->bmap);
1083 aff = isl_aff_alloc(ls);
1087 if (isl_int_is_neg(constraint->line[0][pos]))
1088 isl_seq_cpy(aff->v->el + 1, constraint->line[0],
1091 isl_seq_neg(aff->v->el + 1, constraint->line[0],
1093 isl_int_set_si(aff->v->el[1 + pos], 0);
1094 isl_int_abs(aff->v->el[0], constraint->line[0][pos]);
1099 /* For an inequality constraint
1103 * or an equality constraint
1107 * return the affine expression f.
1109 __isl_give isl_aff *isl_constraint_get_aff(
1110 __isl_keep isl_constraint *constraint)
1113 isl_local_space *ls;
1118 ls = isl_basic_set_get_local_space(constraint->bmap);
1119 aff = isl_aff_alloc(ls);
1123 isl_seq_cpy(aff->v->el + 1, constraint->line[0], aff->v->size - 1);
1124 isl_int_set_si(aff->v->el[0], 1);
1129 /* Construct an equality constraint equating the given affine expression
1132 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1135 isl_basic_set *bset;
1140 bset = isl_basic_set_from_local_space(isl_aff_get_local_space(aff));
1141 bset = isl_basic_set_extend_constraints(bset, 1, 0);
1142 k = isl_basic_set_alloc_equality(bset);
1146 isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1149 return isl_basic_set_constraint(bset, &bset->eq[k]);
1152 isl_basic_set_free(bset);
1156 /* Construct an inequality constraint enforcing the given affine expression
1157 * to be non-negative.
1159 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1162 isl_basic_set *bset;
1167 bset = isl_basic_set_from_local_space(isl_aff_get_local_space(aff));
1168 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1169 k = isl_basic_set_alloc_inequality(bset);
1173 isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1176 return isl_basic_set_constraint(bset, &bset->ineq[k]);
1179 isl_basic_set_free(bset);