export isl_union_map_dump
[isl.git] / isl_constraint.c
blob760f9e580994709041e603e55055ea1033dff90e
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/constraint.h>
14 #include <isl_dim_private.h>
15 #include <isl/seq.h>
16 #include "isl_map_private.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_coefficient(struct isl_constraint *constraint,
361 enum isl_dim_type type, int pos, isl_int v)
363 if (!constraint)
364 return;
366 isl_assert(constraint->ctx, pos < n(constraint, type), return);
367 isl_int_set(constraint->line[0][offset(constraint, type) + pos], v);
370 void isl_constraint_clear(struct isl_constraint *constraint)
372 unsigned total;
374 if (!constraint)
375 return;
376 total = isl_basic_map_total_dim(constraint->bmap);
377 isl_seq_clr(constraint->line[0], 1 + total);
380 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
382 unsigned total;
384 if (!constraint)
385 return NULL;
387 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
388 goto error);
389 isl_assert(constraint->ctx, constraint->bmap->ref == 1, goto error);
390 total = isl_basic_map_total_dim(constraint->bmap);
391 isl_seq_neg(constraint->line[0], constraint->line[0], 1 + total);
392 isl_int_sub_ui(constraint->line[0][0], constraint->line[0][0], 1);
393 ISL_F_CLR(constraint->bmap, ISL_BASIC_MAP_NORMALIZED);
394 return constraint;
395 error:
396 isl_constraint_free(constraint);
397 return NULL;
400 int isl_constraint_is_equality(struct isl_constraint *constraint)
402 if (!constraint)
403 return -1;
404 return constraint->line >= constraint->bmap->eq;
407 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
409 int i;
411 if (!constraint)
412 return -1;
413 if (isl_constraint_is_equality(constraint))
414 return 0;
415 for (i = 0; i < constraint->bmap->n_div; ++i) {
416 if (isl_int_is_zero(constraint->bmap->div[i][0]))
417 continue;
418 if (isl_basic_map_is_div_constraint(constraint->bmap,
419 constraint->line[0], i))
420 return 1;
423 return 0;
426 /* We manually set ISL_BASIC_SET_FINAL instead of calling
427 * isl_basic_map_finalize because we want to keep the position
428 * of the divs and we therefore do not want to throw away redundant divs.
429 * This is arguably a bit fragile.
431 __isl_give isl_basic_map *isl_basic_map_from_constraint(
432 __isl_take isl_constraint *constraint)
434 int k;
435 struct isl_basic_map *bmap;
436 isl_int *c;
437 unsigned total;
439 if (!constraint)
440 return NULL;
442 if (constraint->bmap->n_eq == 1 && constraint->bmap->n_ineq == 0) {
443 bmap = isl_basic_map_copy(constraint->bmap);
444 isl_constraint_free(constraint);
445 return bmap;
448 bmap = isl_basic_map_universe_like(constraint->bmap);
449 bmap = isl_basic_map_align_divs(bmap, constraint->bmap);
450 bmap = isl_basic_map_cow(bmap);
451 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
452 if (isl_constraint_is_equality(constraint)) {
453 k = isl_basic_map_alloc_equality(bmap);
454 if (k < 0)
455 goto error;
456 c = bmap->eq[k];
458 else {
459 k = isl_basic_map_alloc_inequality(bmap);
460 if (k < 0)
461 goto error;
462 c = bmap->ineq[k];
464 total = isl_basic_map_total_dim(bmap);
465 isl_seq_cpy(c, constraint->line[0], 1 + total);
466 isl_constraint_free(constraint);
467 if (bmap)
468 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
469 return bmap;
470 error:
471 isl_constraint_free(constraint);
472 isl_basic_map_free(bmap);
473 return NULL;
476 struct isl_basic_set *isl_basic_set_from_constraint(
477 struct isl_constraint *constraint)
479 if (!constraint)
480 return NULL;
482 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
483 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
484 error:
485 isl_constraint_free(constraint);
486 return NULL;
489 int isl_basic_map_has_defining_equality(
490 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
491 __isl_give isl_constraint **c)
493 int i;
494 unsigned offset;
495 unsigned total;
497 if (!bmap)
498 return -1;
499 offset = basic_map_offset(bmap, type);
500 total = isl_basic_map_total_dim(bmap);
501 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
502 for (i = 0; i < bmap->n_eq; ++i)
503 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
504 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
505 1+total-offset-pos-1) == -1) {
506 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
507 &bmap->eq[i]);
508 return 1;
510 return 0;
513 int isl_basic_set_has_defining_equality(
514 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
515 __isl_give isl_constraint **c)
517 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
518 type, pos, c);
521 int isl_basic_set_has_defining_inequalities(
522 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
523 struct isl_constraint **lower,
524 struct isl_constraint **upper)
526 int i, j;
527 unsigned offset;
528 unsigned total;
529 isl_int m;
530 isl_int **lower_line, **upper_line;
532 if (!bset)
533 return -1;
534 offset = basic_set_offset(bset, type);
535 total = isl_basic_set_total_dim(bset);
536 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
537 isl_int_init(m);
538 for (i = 0; i < bset->n_ineq; ++i) {
539 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
540 continue;
541 if (isl_int_is_one(bset->ineq[i][offset + pos]))
542 continue;
543 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
544 continue;
545 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
546 1+total-offset-pos-1) != -1)
547 continue;
548 for (j = i + 1; j < bset->n_ineq; ++j) {
549 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
550 total))
551 continue;
552 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
553 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
554 continue;
556 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
557 lower_line = &bset->ineq[i];
558 upper_line = &bset->ineq[j];
559 } else {
560 lower_line = &bset->ineq[j];
561 upper_line = &bset->ineq[i];
563 *lower = isl_basic_set_constraint(
564 isl_basic_set_copy(bset), lower_line);
565 *upper = isl_basic_set_constraint(
566 isl_basic_set_copy(bset), upper_line);
567 isl_int_clear(m);
568 return 1;
571 *lower = NULL;
572 *upper = NULL;
573 isl_int_clear(m);
574 return 0;
577 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
578 * (in "a" and "b"), add a constraint to "bset" that ensures that the
579 * bound implied by "a" is (strictly) larger than the bound implied by "b".
581 * If both constraints imply lower bounds, then this means that "a" is
582 * active in the result.
583 * If both constraints imply upper bounds, then this means that "b" is
584 * active in the result.
586 static __isl_give isl_basic_set *add_larger_bound_constraint(
587 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
588 unsigned abs_pos, int strict)
590 int k;
591 isl_int t;
592 unsigned total;
594 k = isl_basic_set_alloc_inequality(bset);
595 if (k < 0)
596 goto error;
598 total = isl_basic_set_dim(bset, isl_dim_all);
600 isl_int_init(t);
601 isl_int_neg(t, b[1 + abs_pos]);
603 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
604 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
605 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
606 total - abs_pos);
608 if (strict)
609 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
611 isl_int_clear(t);
613 return bset;
614 error:
615 isl_basic_set_free(bset);
616 return NULL;
619 /* Add constraints to "context" that ensure that "u" is the smallest
620 * (and therefore active) upper bound on "abs_pos" in "bset" and return
621 * the resulting basic set.
623 static __isl_give isl_basic_set *set_smallest_upper_bound(
624 __isl_keep isl_basic_set *context,
625 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
627 int j;
629 context = isl_basic_set_copy(context);
630 context = isl_basic_set_cow(context);
632 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
634 for (j = 0; j < bset->n_ineq; ++j) {
635 if (j == u)
636 continue;
637 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
638 continue;
639 context = add_larger_bound_constraint(context,
640 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
643 context = isl_basic_set_simplify(context);
644 context = isl_basic_set_finalize(context);
646 return context;
649 /* Add constraints to "context" that ensure that "u" is the largest
650 * (and therefore active) upper bound on "abs_pos" in "bset" and return
651 * the resulting basic set.
653 static __isl_give isl_basic_set *set_largest_lower_bound(
654 __isl_keep isl_basic_set *context,
655 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
657 int j;
659 context = isl_basic_set_copy(context);
660 context = isl_basic_set_cow(context);
662 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
664 for (j = 0; j < bset->n_ineq; ++j) {
665 if (j == l)
666 continue;
667 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
668 continue;
669 context = add_larger_bound_constraint(context,
670 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
673 context = isl_basic_set_simplify(context);
674 context = isl_basic_set_finalize(context);
676 return context;
679 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
680 enum isl_dim_type type, unsigned abs_pos,
681 __isl_take isl_basic_set *context, int n_upper,
682 int (*fn)(__isl_take isl_constraint *lower,
683 __isl_take isl_constraint *upper,
684 __isl_take isl_basic_set *bset, void *user), void *user)
686 isl_basic_set *context_i;
687 isl_constraint *upper = NULL;
688 int i;
690 for (i = 0; i < bset->n_ineq; ++i) {
691 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
692 continue;
694 context_i = set_smallest_upper_bound(context, bset,
695 abs_pos, n_upper, i);
696 if (isl_basic_set_is_empty(context_i)) {
697 isl_basic_set_free(context_i);
698 continue;
700 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
701 &bset->ineq[i]);
702 if (!upper || !context_i)
703 goto error;
704 if (fn(NULL, upper, context_i, user) < 0)
705 break;
708 isl_basic_set_free(context);
710 if (i < bset->n_ineq)
711 return -1;
713 return 0;
714 error:
715 isl_constraint_free(upper);
716 isl_basic_set_free(context_i);
717 isl_basic_set_free(context);
718 return -1;
721 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
722 enum isl_dim_type type, unsigned abs_pos,
723 __isl_take isl_basic_set *context, int n_lower,
724 int (*fn)(__isl_take isl_constraint *lower,
725 __isl_take isl_constraint *upper,
726 __isl_take isl_basic_set *bset, void *user), void *user)
728 isl_basic_set *context_i;
729 isl_constraint *lower = NULL;
730 int i;
732 for (i = 0; i < bset->n_ineq; ++i) {
733 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
734 continue;
736 context_i = set_largest_lower_bound(context, bset,
737 abs_pos, n_lower, i);
738 if (isl_basic_set_is_empty(context_i)) {
739 isl_basic_set_free(context_i);
740 continue;
742 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
743 &bset->ineq[i]);
744 if (!lower || !context_i)
745 goto error;
746 if (fn(lower, NULL, context_i, user) < 0)
747 break;
750 isl_basic_set_free(context);
752 if (i < bset->n_ineq)
753 return -1;
755 return 0;
756 error:
757 isl_constraint_free(lower);
758 isl_basic_set_free(context_i);
759 isl_basic_set_free(context);
760 return -1;
763 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
764 enum isl_dim_type type, unsigned abs_pos,
765 __isl_take isl_basic_set *context, int n_lower, int n_upper,
766 int (*fn)(__isl_take isl_constraint *lower,
767 __isl_take isl_constraint *upper,
768 __isl_take isl_basic_set *bset, void *user), void *user)
770 isl_basic_set *context_i, *context_j;
771 isl_constraint *lower = NULL;
772 isl_constraint *upper = NULL;
773 int i, j;
775 for (i = 0; i < bset->n_ineq; ++i) {
776 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
777 continue;
779 context_i = set_largest_lower_bound(context, bset,
780 abs_pos, n_lower, i);
781 if (isl_basic_set_is_empty(context_i)) {
782 isl_basic_set_free(context_i);
783 continue;
786 for (j = 0; j < bset->n_ineq; ++j) {
787 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
788 continue;
790 context_j = set_smallest_upper_bound(context_i, bset,
791 abs_pos, n_upper, j);
792 context_j = isl_basic_set_extend_constraints(context_j,
793 0, 1);
794 context_j = add_larger_bound_constraint(context_j,
795 bset->ineq[i], bset->ineq[j], abs_pos, 0);
796 context_j = isl_basic_set_simplify(context_j);
797 context_j = isl_basic_set_finalize(context_j);
798 if (isl_basic_set_is_empty(context_j)) {
799 isl_basic_set_free(context_j);
800 continue;
802 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
803 &bset->ineq[i]);
804 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
805 &bset->ineq[j]);
806 if (!lower || !upper || !context_j)
807 goto error;
808 if (fn(lower, upper, context_j, user) < 0)
809 break;
812 isl_basic_set_free(context_i);
814 if (j < bset->n_ineq)
815 break;
818 isl_basic_set_free(context);
820 if (i < bset->n_ineq)
821 return -1;
823 return 0;
824 error:
825 isl_constraint_free(lower);
826 isl_constraint_free(upper);
827 isl_basic_set_free(context_i);
828 isl_basic_set_free(context_j);
829 isl_basic_set_free(context);
830 return -1;
833 /* For each pair of lower and upper bounds on the variable "pos"
834 * of type "type", call "fn" with these lower and upper bounds and the
835 * set of constraints on the remaining variables where these bounds
836 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
838 * If the designated variable is equal to an affine combination of the
839 * other variables then fn is called with both lower and upper
840 * set to the corresponding equality.
842 * If there is no lower (or upper) bound, then NULL is passed
843 * as the corresponding bound.
845 * We first check if the variable is involved in any equality.
846 * If not, we count the number of lower and upper bounds and
847 * act accordingly.
849 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
850 enum isl_dim_type type, unsigned pos,
851 int (*fn)(__isl_take isl_constraint *lower,
852 __isl_take isl_constraint *upper,
853 __isl_take isl_basic_set *bset, void *user), void *user)
855 int i;
856 isl_constraint *lower = NULL;
857 isl_constraint *upper = NULL;
858 isl_basic_set *context = NULL;
859 unsigned abs_pos;
860 int n_lower, n_upper;
862 if (!bset)
863 return -1;
864 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
865 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
866 return -1);
868 abs_pos = pos;
869 if (type == isl_dim_set)
870 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
872 for (i = 0; i < bset->n_eq; ++i) {
873 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
874 continue;
876 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
877 &bset->eq[i]);
878 upper = isl_constraint_copy(lower);
879 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
880 type, pos, 1);
881 if (!lower || !upper || !context)
882 goto error;
883 return fn(lower, upper, context, user);
886 n_lower = 0;
887 n_upper = 0;
888 for (i = 0; i < bset->n_ineq; ++i) {
889 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
890 n_lower++;
891 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
892 n_upper++;
895 context = isl_basic_set_copy(bset);
896 context = isl_basic_set_cow(context);
897 if (!context)
898 goto error;
899 for (i = context->n_ineq - 1; i >= 0; --i)
900 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
901 isl_basic_set_drop_inequality(context, i);
903 context = isl_basic_set_drop(context, type, pos, 1);
904 if (!n_lower && !n_upper)
905 return fn(NULL, NULL, context, user);
906 if (!n_lower)
907 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
908 fn, user);
909 if (!n_upper)
910 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
911 fn, user);
912 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
913 fn, user);
914 error:
915 isl_constraint_free(lower);
916 isl_constraint_free(upper);
917 isl_basic_set_free(context);
918 return -1;