add isl_basic_map_set_rational
[isl.git] / isl_constraint.c
blob9a8050c2efb94bc7c382cd28d44f6a820e7859e4
1 /*
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.h>
15 #include <isl_dim_private.h>
16 #include <isl/seq.h>
18 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
20 return isl_basic_map_dim(c->bmap, type);
23 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
25 struct isl_dim *dim = c->bmap->dim;
26 switch (type) {
27 case isl_dim_param: return 1;
28 case isl_dim_in: return 1 + dim->nparam;
29 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
30 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
31 default: return 0;
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_dim_total(bmap->dim)
39 : 1 + isl_dim_offset(bmap->dim, type);
42 static unsigned basic_set_offset(struct isl_basic_set *bset,
43 enum isl_dim_type type)
45 struct isl_dim *dim = bset->dim;
46 switch (type) {
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;
51 default: return 0;
55 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
56 isl_int **line)
58 struct isl_constraint *constraint;
60 if (!bmap || !line)
61 goto error;
63 constraint = isl_alloc_type(bmap->ctx, struct isl_constraint);
64 if (!constraint)
65 goto error;
67 constraint->ctx = bmap->ctx;
68 isl_ctx_ref(constraint->ctx);
69 constraint->ref = 1;
70 constraint->bmap = bmap;
71 constraint->line = line;
73 return constraint;
74 error:
75 isl_basic_map_free(bmap);
76 return NULL;
79 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
80 isl_int **line)
82 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
85 struct isl_constraint *isl_equality_alloc(struct isl_dim *dim)
87 struct isl_basic_map *bmap;
89 if (!dim)
90 return NULL;
92 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
93 if (!bmap)
94 return NULL;
96 isl_basic_map_alloc_equality(bmap);
97 isl_seq_clr(bmap->eq[0], 1 + isl_basic_map_total_dim(bmap));
98 return isl_basic_map_constraint(bmap, &bmap->eq[0]);
101 struct isl_constraint *isl_inequality_alloc(struct isl_dim *dim)
103 struct isl_basic_map *bmap;
105 if (!dim)
106 return NULL;
108 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 1);
109 if (!bmap)
110 return NULL;
112 isl_basic_map_alloc_inequality(bmap);
113 isl_seq_clr(bmap->ineq[0], 1 + isl_basic_map_total_dim(bmap));
114 return isl_basic_map_constraint(bmap, &bmap->ineq[0]);
117 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
119 struct isl_basic_map *bmap;
120 int i;
121 int eq;
123 if (!c)
124 return NULL;
126 eq = c->line < c->bmap->eq + c->bmap->n_eq;
127 i = eq ? c->line - c->bmap->eq : c->line - c->bmap->ineq;
128 bmap = isl_basic_map_copy(c->bmap);
129 if (!bmap)
130 return NULL;
131 return isl_basic_map_constraint(bmap, eq ? bmap->eq + i : bmap->ineq + i);
134 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
136 if (!c)
137 return NULL;
139 if (c->ref == 1)
140 return c;
141 c->ref--;
142 return isl_constraint_dup(c);
145 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
147 if (!constraint)
148 return NULL;
150 constraint->ref++;
151 return constraint;
154 void isl_constraint_free(struct isl_constraint *c)
156 if (!c)
157 return;
159 if (--c->ref > 0)
160 return;
162 isl_basic_map_free(c->bmap);
163 isl_ctx_deref(c->ctx);
164 free(c);
167 __isl_give isl_constraint *isl_basic_map_first_constraint(
168 __isl_take isl_basic_map *bmap)
170 if (!bmap)
171 return NULL;
173 if (bmap->n_eq > 0)
174 return isl_basic_map_constraint(bmap, &bmap->eq[0]);
176 if (bmap->n_ineq > 0)
177 return isl_basic_map_constraint(bmap, &bmap->ineq[0]);
179 isl_basic_map_free(bmap);
180 return NULL;
183 __isl_give isl_constraint *isl_basic_set_first_constraint(
184 __isl_take isl_basic_set *bset)
186 return isl_basic_map_first_constraint((struct isl_basic_map *)bset);
189 struct isl_constraint *isl_constraint_next(struct isl_constraint *c)
191 c = isl_constraint_cow(c);
192 if (c->line >= c->bmap->eq) {
193 c->line++;
194 if (c->line < c->bmap->eq + c->bmap->n_eq)
195 return c;
196 c->line = c->bmap->ineq;
197 } else
198 c->line++;
199 if (c->line < c->bmap->ineq + c->bmap->n_ineq)
200 return c;
201 isl_constraint_free(c);
202 return NULL;
205 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
206 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
208 int i;
209 struct isl_constraint *c;
211 if (!bmap)
212 return -1;
214 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
215 return -1);
217 for (i = 0; i < bmap->n_eq; ++i) {
218 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
219 &bmap->eq[i]);
220 if (!c)
221 return -1;
222 if (fn(c, user) < 0)
223 return -1;
226 for (i = 0; i < bmap->n_ineq; ++i) {
227 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
228 &bmap->ineq[i]);
229 if (!c)
230 return -1;
231 if (fn(c, user) < 0)
232 return -1;
235 return 0;
238 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
239 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
241 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
244 int isl_constraint_is_equal(struct isl_constraint *constraint1,
245 struct isl_constraint *constraint2)
247 if (!constraint1 || !constraint2)
248 return 0;
249 return constraint1->bmap == constraint2->bmap &&
250 constraint1->line == constraint2->line;
253 struct isl_basic_map *isl_basic_map_add_constraint(
254 struct isl_basic_map *bmap, struct isl_constraint *constraint)
256 if (!bmap || !constraint)
257 goto error;
259 isl_assert(constraint->ctx,
260 isl_dim_equal(bmap->dim, constraint->bmap->dim), goto error);
262 bmap = isl_basic_map_intersect(bmap,
263 isl_basic_map_from_constraint(constraint));
264 return bmap;
265 error:
266 isl_basic_map_free(bmap);
267 isl_constraint_free(constraint);
268 return NULL;
271 struct isl_basic_set *isl_basic_set_add_constraint(
272 struct isl_basic_set *bset, struct isl_constraint *constraint)
274 return (struct isl_basic_set *)
275 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
276 constraint);
279 struct isl_constraint *isl_constraint_add_div(struct isl_constraint *constraint,
280 struct isl_div *div, int *pos)
282 if (!constraint || !div)
283 goto error;
285 isl_assert(constraint->ctx,
286 isl_dim_equal(div->bmap->dim, constraint->bmap->dim), goto error);
287 isl_assert(constraint->ctx,
288 constraint->bmap->n_eq + constraint->bmap->n_ineq == 1, goto error);
290 constraint->bmap = isl_basic_map_cow(constraint->bmap);
291 constraint->bmap = isl_basic_map_extend_dim(constraint->bmap,
292 isl_dim_copy(constraint->bmap->dim), 1, 0, 0);
293 if (!constraint->bmap)
294 goto error;
295 constraint->line = &constraint->bmap->ineq[0];
296 *pos = isl_basic_map_alloc_div(constraint->bmap);
297 if (*pos < 0)
298 goto error;
299 isl_seq_cpy(constraint->bmap->div[*pos], div->line[0],
300 1 + 1 + isl_basic_map_total_dim(constraint->bmap));
301 isl_div_free(div);
302 return constraint;
303 error:
304 isl_constraint_free(constraint);
305 isl_div_free(div);
306 return NULL;
309 int isl_constraint_dim(struct isl_constraint *constraint,
310 enum isl_dim_type type)
312 if (!constraint)
313 return -1;
314 return n(constraint, type);
317 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
318 enum isl_dim_type type, unsigned pos)
320 return constraint ?
321 isl_basic_map_get_dim_name(constraint->bmap, type, pos) : NULL;
324 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
326 if (!constraint)
327 return;
328 isl_int_set(*v, constraint->line[0][0]);
331 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
332 enum isl_dim_type type, int pos, isl_int *v)
334 if (!constraint)
335 return;
337 isl_assert(constraint->ctx, pos < n(constraint, type), return);
338 isl_int_set(*v, constraint->line[0][offset(constraint, type) + pos]);
341 struct isl_div *isl_constraint_div(struct isl_constraint *constraint, int pos)
343 if (!constraint)
344 return NULL;
346 isl_assert(constraint->ctx, pos < n(constraint, isl_dim_div),
347 return NULL);
348 isl_assert(constraint->ctx,
349 !isl_int_is_zero(constraint->bmap->div[pos][0]), return NULL);
350 return isl_basic_map_div(isl_basic_map_copy(constraint->bmap), pos);
353 void isl_constraint_set_constant(struct isl_constraint *constraint, isl_int v)
355 if (!constraint)
356 return;
357 isl_int_set(constraint->line[0][0], v);
360 void isl_constraint_set_constant_si(__isl_keep isl_constraint *constraint,
361 int v)
363 if (!constraint)
364 return;
365 isl_int_set_si(constraint->line[0][0], v);
368 void isl_constraint_set_coefficient(struct isl_constraint *constraint,
369 enum isl_dim_type type, int pos, isl_int v)
371 if (!constraint)
372 return;
374 isl_assert(constraint->ctx, pos < n(constraint, type), return);
375 isl_int_set(constraint->line[0][offset(constraint, type) + pos], v);
378 void isl_constraint_set_coefficient_si(__isl_keep isl_constraint *constraint,
379 enum isl_dim_type type, int pos, int v)
381 if (!constraint)
382 return;
384 isl_assert(constraint->ctx, pos < n(constraint, type), return);
385 isl_int_set_si(constraint->line[0][offset(constraint, type) + pos], v);
388 void isl_constraint_clear(struct isl_constraint *constraint)
390 unsigned total;
392 if (!constraint)
393 return;
394 total = isl_basic_map_total_dim(constraint->bmap);
395 isl_seq_clr(constraint->line[0], 1 + total);
398 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
400 unsigned total;
402 if (!constraint)
403 return NULL;
405 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
406 goto error);
407 isl_assert(constraint->ctx, constraint->bmap->ref == 1, goto error);
408 total = isl_basic_map_total_dim(constraint->bmap);
409 isl_seq_neg(constraint->line[0], constraint->line[0], 1 + total);
410 isl_int_sub_ui(constraint->line[0][0], constraint->line[0][0], 1);
411 ISL_F_CLR(constraint->bmap, ISL_BASIC_MAP_NORMALIZED);
412 return constraint;
413 error:
414 isl_constraint_free(constraint);
415 return NULL;
418 int isl_constraint_is_equality(struct isl_constraint *constraint)
420 if (!constraint)
421 return -1;
422 return constraint->line >= constraint->bmap->eq;
425 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
427 int i;
429 if (!constraint)
430 return -1;
431 if (isl_constraint_is_equality(constraint))
432 return 0;
433 for (i = 0; i < constraint->bmap->n_div; ++i) {
434 if (isl_int_is_zero(constraint->bmap->div[i][0]))
435 continue;
436 if (isl_basic_map_is_div_constraint(constraint->bmap,
437 constraint->line[0], i))
438 return 1;
441 return 0;
444 /* We manually set ISL_BASIC_SET_FINAL instead of calling
445 * isl_basic_map_finalize because we want to keep the position
446 * of the divs and we therefore do not want to throw away redundant divs.
447 * This is arguably a bit fragile.
449 __isl_give isl_basic_map *isl_basic_map_from_constraint(
450 __isl_take isl_constraint *constraint)
452 int k;
453 struct isl_basic_map *bmap;
454 isl_int *c;
455 unsigned total;
457 if (!constraint)
458 return NULL;
460 if (constraint->bmap->n_eq == 1 && constraint->bmap->n_ineq == 0) {
461 bmap = isl_basic_map_copy(constraint->bmap);
462 isl_constraint_free(constraint);
463 return bmap;
466 bmap = isl_basic_map_universe_like(constraint->bmap);
467 bmap = isl_basic_map_align_divs(bmap, constraint->bmap);
468 bmap = isl_basic_map_cow(bmap);
469 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
470 if (isl_constraint_is_equality(constraint)) {
471 k = isl_basic_map_alloc_equality(bmap);
472 if (k < 0)
473 goto error;
474 c = bmap->eq[k];
476 else {
477 k = isl_basic_map_alloc_inequality(bmap);
478 if (k < 0)
479 goto error;
480 c = bmap->ineq[k];
482 total = isl_basic_map_total_dim(bmap);
483 isl_seq_cpy(c, constraint->line[0], 1 + total);
484 isl_constraint_free(constraint);
485 if (bmap)
486 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
487 return bmap;
488 error:
489 isl_constraint_free(constraint);
490 isl_basic_map_free(bmap);
491 return NULL;
494 struct isl_basic_set *isl_basic_set_from_constraint(
495 struct isl_constraint *constraint)
497 if (!constraint)
498 return NULL;
500 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
501 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
502 error:
503 isl_constraint_free(constraint);
504 return NULL;
507 int isl_basic_map_has_defining_equality(
508 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
509 __isl_give isl_constraint **c)
511 int i;
512 unsigned offset;
513 unsigned total;
515 if (!bmap)
516 return -1;
517 offset = basic_map_offset(bmap, type);
518 total = isl_basic_map_total_dim(bmap);
519 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
520 for (i = 0; i < bmap->n_eq; ++i)
521 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
522 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
523 1+total-offset-pos-1) == -1) {
524 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
525 &bmap->eq[i]);
526 return 1;
528 return 0;
531 int isl_basic_set_has_defining_equality(
532 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
533 __isl_give isl_constraint **c)
535 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
536 type, pos, c);
539 int isl_basic_set_has_defining_inequalities(
540 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
541 struct isl_constraint **lower,
542 struct isl_constraint **upper)
544 int i, j;
545 unsigned offset;
546 unsigned total;
547 isl_int m;
548 isl_int **lower_line, **upper_line;
550 if (!bset)
551 return -1;
552 offset = basic_set_offset(bset, type);
553 total = isl_basic_set_total_dim(bset);
554 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
555 isl_int_init(m);
556 for (i = 0; i < bset->n_ineq; ++i) {
557 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
558 continue;
559 if (isl_int_is_one(bset->ineq[i][offset + pos]))
560 continue;
561 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
562 continue;
563 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
564 1+total-offset-pos-1) != -1)
565 continue;
566 for (j = i + 1; j < bset->n_ineq; ++j) {
567 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
568 total))
569 continue;
570 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
571 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
572 continue;
574 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
575 lower_line = &bset->ineq[i];
576 upper_line = &bset->ineq[j];
577 } else {
578 lower_line = &bset->ineq[j];
579 upper_line = &bset->ineq[i];
581 *lower = isl_basic_set_constraint(
582 isl_basic_set_copy(bset), lower_line);
583 *upper = isl_basic_set_constraint(
584 isl_basic_set_copy(bset), upper_line);
585 isl_int_clear(m);
586 return 1;
589 *lower = NULL;
590 *upper = NULL;
591 isl_int_clear(m);
592 return 0;
595 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
596 * (in "a" and "b"), add a constraint to "bset" that ensures that the
597 * bound implied by "a" is (strictly) larger than the bound implied by "b".
599 * If both constraints imply lower bounds, then this means that "a" is
600 * active in the result.
601 * If both constraints imply upper bounds, then this means that "b" is
602 * active in the result.
604 static __isl_give isl_basic_set *add_larger_bound_constraint(
605 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
606 unsigned abs_pos, int strict)
608 int k;
609 isl_int t;
610 unsigned total;
612 k = isl_basic_set_alloc_inequality(bset);
613 if (k < 0)
614 goto error;
616 total = isl_basic_set_dim(bset, isl_dim_all);
618 isl_int_init(t);
619 isl_int_neg(t, b[1 + abs_pos]);
621 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
622 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
623 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
624 total - abs_pos);
626 if (strict)
627 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
629 isl_int_clear(t);
631 return bset;
632 error:
633 isl_basic_set_free(bset);
634 return NULL;
637 /* Add constraints to "context" that ensure that "u" is the smallest
638 * (and therefore active) upper bound on "abs_pos" in "bset" and return
639 * the resulting basic set.
641 static __isl_give isl_basic_set *set_smallest_upper_bound(
642 __isl_keep isl_basic_set *context,
643 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
645 int j;
647 context = isl_basic_set_copy(context);
648 context = isl_basic_set_cow(context);
650 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
652 for (j = 0; j < bset->n_ineq; ++j) {
653 if (j == u)
654 continue;
655 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
656 continue;
657 context = add_larger_bound_constraint(context,
658 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
661 context = isl_basic_set_simplify(context);
662 context = isl_basic_set_finalize(context);
664 return context;
667 /* Add constraints to "context" that ensure that "u" is the largest
668 * (and therefore active) upper bound on "abs_pos" in "bset" and return
669 * the resulting basic set.
671 static __isl_give isl_basic_set *set_largest_lower_bound(
672 __isl_keep isl_basic_set *context,
673 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
675 int j;
677 context = isl_basic_set_copy(context);
678 context = isl_basic_set_cow(context);
680 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
682 for (j = 0; j < bset->n_ineq; ++j) {
683 if (j == l)
684 continue;
685 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
686 continue;
687 context = add_larger_bound_constraint(context,
688 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
691 context = isl_basic_set_simplify(context);
692 context = isl_basic_set_finalize(context);
694 return context;
697 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
698 enum isl_dim_type type, unsigned abs_pos,
699 __isl_take isl_basic_set *context, int n_upper,
700 int (*fn)(__isl_take isl_constraint *lower,
701 __isl_take isl_constraint *upper,
702 __isl_take isl_basic_set *bset, void *user), void *user)
704 isl_basic_set *context_i;
705 isl_constraint *upper = NULL;
706 int i;
708 for (i = 0; i < bset->n_ineq; ++i) {
709 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
710 continue;
712 context_i = set_smallest_upper_bound(context, bset,
713 abs_pos, n_upper, i);
714 if (isl_basic_set_is_empty(context_i)) {
715 isl_basic_set_free(context_i);
716 continue;
718 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
719 &bset->ineq[i]);
720 if (!upper || !context_i)
721 goto error;
722 if (fn(NULL, upper, context_i, user) < 0)
723 break;
726 isl_basic_set_free(context);
728 if (i < bset->n_ineq)
729 return -1;
731 return 0;
732 error:
733 isl_constraint_free(upper);
734 isl_basic_set_free(context_i);
735 isl_basic_set_free(context);
736 return -1;
739 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
740 enum isl_dim_type type, unsigned abs_pos,
741 __isl_take isl_basic_set *context, int n_lower,
742 int (*fn)(__isl_take isl_constraint *lower,
743 __isl_take isl_constraint *upper,
744 __isl_take isl_basic_set *bset, void *user), void *user)
746 isl_basic_set *context_i;
747 isl_constraint *lower = NULL;
748 int i;
750 for (i = 0; i < bset->n_ineq; ++i) {
751 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
752 continue;
754 context_i = set_largest_lower_bound(context, bset,
755 abs_pos, n_lower, i);
756 if (isl_basic_set_is_empty(context_i)) {
757 isl_basic_set_free(context_i);
758 continue;
760 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
761 &bset->ineq[i]);
762 if (!lower || !context_i)
763 goto error;
764 if (fn(lower, NULL, context_i, user) < 0)
765 break;
768 isl_basic_set_free(context);
770 if (i < bset->n_ineq)
771 return -1;
773 return 0;
774 error:
775 isl_constraint_free(lower);
776 isl_basic_set_free(context_i);
777 isl_basic_set_free(context);
778 return -1;
781 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
782 enum isl_dim_type type, unsigned abs_pos,
783 __isl_take isl_basic_set *context, int n_lower, int n_upper,
784 int (*fn)(__isl_take isl_constraint *lower,
785 __isl_take isl_constraint *upper,
786 __isl_take isl_basic_set *bset, void *user), void *user)
788 isl_basic_set *context_i, *context_j;
789 isl_constraint *lower = NULL;
790 isl_constraint *upper = NULL;
791 int i, j;
793 for (i = 0; i < bset->n_ineq; ++i) {
794 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
795 continue;
797 context_i = set_largest_lower_bound(context, bset,
798 abs_pos, n_lower, i);
799 if (isl_basic_set_is_empty(context_i)) {
800 isl_basic_set_free(context_i);
801 continue;
804 for (j = 0; j < bset->n_ineq; ++j) {
805 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
806 continue;
808 context_j = set_smallest_upper_bound(context_i, bset,
809 abs_pos, n_upper, j);
810 context_j = isl_basic_set_extend_constraints(context_j,
811 0, 1);
812 context_j = add_larger_bound_constraint(context_j,
813 bset->ineq[i], bset->ineq[j], abs_pos, 0);
814 context_j = isl_basic_set_simplify(context_j);
815 context_j = isl_basic_set_finalize(context_j);
816 if (isl_basic_set_is_empty(context_j)) {
817 isl_basic_set_free(context_j);
818 continue;
820 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
821 &bset->ineq[i]);
822 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
823 &bset->ineq[j]);
824 if (!lower || !upper || !context_j)
825 goto error;
826 if (fn(lower, upper, context_j, user) < 0)
827 break;
830 isl_basic_set_free(context_i);
832 if (j < bset->n_ineq)
833 break;
836 isl_basic_set_free(context);
838 if (i < bset->n_ineq)
839 return -1;
841 return 0;
842 error:
843 isl_constraint_free(lower);
844 isl_constraint_free(upper);
845 isl_basic_set_free(context_i);
846 isl_basic_set_free(context_j);
847 isl_basic_set_free(context);
848 return -1;
851 /* For each pair of lower and upper bounds on the variable "pos"
852 * of type "type", call "fn" with these lower and upper bounds and the
853 * set of constraints on the remaining variables where these bounds
854 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
856 * If the designated variable is equal to an affine combination of the
857 * other variables then fn is called with both lower and upper
858 * set to the corresponding equality.
860 * If there is no lower (or upper) bound, then NULL is passed
861 * as the corresponding bound.
863 * We first check if the variable is involved in any equality.
864 * If not, we count the number of lower and upper bounds and
865 * act accordingly.
867 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
868 enum isl_dim_type type, unsigned pos,
869 int (*fn)(__isl_take isl_constraint *lower,
870 __isl_take isl_constraint *upper,
871 __isl_take isl_basic_set *bset, void *user), void *user)
873 int i;
874 isl_constraint *lower = NULL;
875 isl_constraint *upper = NULL;
876 isl_basic_set *context = NULL;
877 unsigned abs_pos;
878 int n_lower, n_upper;
880 if (!bset)
881 return -1;
882 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
883 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
884 return -1);
886 abs_pos = pos;
887 if (type == isl_dim_set)
888 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
890 for (i = 0; i < bset->n_eq; ++i) {
891 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
892 continue;
894 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
895 &bset->eq[i]);
896 upper = isl_constraint_copy(lower);
897 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
898 type, pos, 1);
899 if (!lower || !upper || !context)
900 goto error;
901 return fn(lower, upper, context, user);
904 n_lower = 0;
905 n_upper = 0;
906 for (i = 0; i < bset->n_ineq; ++i) {
907 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
908 n_lower++;
909 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
910 n_upper++;
913 context = isl_basic_set_copy(bset);
914 context = isl_basic_set_cow(context);
915 if (!context)
916 goto error;
917 for (i = context->n_ineq - 1; i >= 0; --i)
918 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
919 isl_basic_set_drop_inequality(context, i);
921 context = isl_basic_set_drop(context, type, pos, 1);
922 if (!n_lower && !n_upper)
923 return fn(NULL, NULL, context, user);
924 if (!n_lower)
925 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
926 fn, user);
927 if (!n_upper)
928 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
929 fn, user);
930 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
931 fn, user);
932 error:
933 isl_constraint_free(lower);
934 isl_constraint_free(upper);
935 isl_basic_set_free(context);
936 return -1;