export isl_pw_aff_nonneg_set
[isl.git] / isl_constraint.c
blob7c4b77828d045020ad12e146f717c97a326abc0a
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_private.h>
15 #include <isl_dim_private.h>
16 #include <isl_div_private.h>
17 #include <isl/seq.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;
33 switch (type) {
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;
38 default: return 0;
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;
53 switch (type) {
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;
58 default: return 0;
62 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
63 isl_int **line)
65 struct isl_constraint *constraint;
67 if (!bmap || !line)
68 goto error;
70 constraint = isl_alloc_type(bmap->ctx, struct isl_constraint);
71 if (!constraint)
72 goto error;
74 constraint->ctx = bmap->ctx;
75 isl_ctx_ref(constraint->ctx);
76 constraint->ref = 1;
77 constraint->bmap = bmap;
78 constraint->line = line;
80 return constraint;
81 error:
82 isl_basic_map_free(bmap);
83 return NULL;
86 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
87 isl_int **line)
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;
96 if (!dim)
97 return NULL;
99 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
100 if (!bmap)
101 return NULL;
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;
112 if (!dim)
113 return NULL;
115 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 1);
116 if (!bmap)
117 return NULL;
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;
127 int i;
128 int eq;
130 if (!c)
131 return NULL;
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);
136 if (!bmap)
137 return NULL;
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)
143 if (!c)
144 return NULL;
146 if (c->ref == 1)
147 return c;
148 c->ref--;
149 return isl_constraint_dup(c);
152 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
154 if (!constraint)
155 return NULL;
157 constraint->ref++;
158 return constraint;
161 void isl_constraint_free(struct isl_constraint *c)
163 if (!c)
164 return;
166 if (--c->ref > 0)
167 return;
169 isl_basic_map_free(c->bmap);
170 isl_ctx_deref(c->ctx);
171 free(c);
174 __isl_give isl_constraint *isl_basic_map_first_constraint(
175 __isl_take isl_basic_map *bmap)
177 if (!bmap)
178 return NULL;
180 if (bmap->n_eq > 0)
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);
187 return NULL;
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) {
200 c->line++;
201 if (c->line < c->bmap->eq + c->bmap->n_eq)
202 return c;
203 c->line = c->bmap->ineq;
204 } else
205 c->line++;
206 if (c->line < c->bmap->ineq + c->bmap->n_ineq)
207 return c;
208 isl_constraint_free(c);
209 return NULL;
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)
215 int i;
216 struct isl_constraint *c;
218 if (!bmap)
219 return -1;
221 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
222 return -1);
224 for (i = 0; i < bmap->n_eq; ++i) {
225 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
226 &bmap->eq[i]);
227 if (!c)
228 return -1;
229 if (fn(c, user) < 0)
230 return -1;
233 for (i = 0; i < bmap->n_ineq; ++i) {
234 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
235 &bmap->ineq[i]);
236 if (!c)
237 return -1;
238 if (fn(c, user) < 0)
239 return -1;
242 return 0;
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)
255 return 0;
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)
264 goto error;
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));
271 return bmap;
272 error:
273 isl_basic_map_free(bmap);
274 isl_constraint_free(constraint);
275 return NULL;
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,
283 constraint);
286 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
287 __isl_take isl_constraint *constraint)
289 isl_basic_map *bmap;
291 bmap = isl_basic_map_from_constraint(constraint);
292 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
294 return map;
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)
307 goto error;
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)
318 goto error;
319 constraint->line = &constraint->bmap->ineq[0];
320 *pos = isl_basic_map_alloc_div(constraint->bmap);
321 if (*pos < 0)
322 goto error;
323 isl_seq_cpy(constraint->bmap->div[*pos], div->line[0],
324 1 + 1 + isl_basic_map_total_dim(constraint->bmap));
325 isl_div_free(div);
326 return constraint;
327 error:
328 isl_constraint_free(constraint);
329 isl_div_free(div);
330 return NULL;
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)
342 if (!constraint)
343 return -1;
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)
350 if (!constraint)
351 return -1;
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)
360 return 1;
362 return 0;
365 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
366 enum isl_dim_type type, unsigned pos)
368 return constraint ?
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)
374 if (!constraint)
375 return;
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)
382 if (!constraint)
383 return;
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)
391 if (!constraint)
392 return NULL;
394 isl_assert(constraint->ctx, pos < n(constraint, isl_dim_div),
395 return NULL);
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)
403 if (!constraint)
404 return;
405 isl_int_set(constraint->line[0][0], v);
408 void isl_constraint_set_constant_si(__isl_keep isl_constraint *constraint,
409 int v)
411 if (!constraint)
412 return;
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)
419 if (!constraint)
420 return;
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)
429 if (!constraint)
430 return;
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)
438 unsigned total;
440 if (!constraint)
441 return;
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)
451 int i;
452 unsigned total;
454 if (!isl_basic_map_divs_known(bmap1))
455 return 0;
456 if (!isl_basic_map_divs_known(bmap2))
457 return 0;
458 if (bmap1->n_div != bmap2->n_div)
459 return 0;
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))
464 return 0;
466 return 1;
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)
482 int i;
483 unsigned n;
484 isl_int **row;
485 unsigned total;
487 if (!bset || !constraint)
488 goto error;
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);
496 return bset;
499 if (isl_constraint_is_equality(constraint)) {
500 n = bset->n_eq;
501 row = bset->eq;
502 } else {
503 n = bset->n_ineq;
504 row = bset->ineq;
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);
513 return bset;
514 error:
515 isl_constraint_free(constraint);
516 isl_basic_set_free(bset);
517 return NULL;
520 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
522 unsigned total;
524 if (!constraint)
525 return NULL;
527 isl_assert(constraint->ctx, !isl_constraint_is_equality(constraint),
528 goto error);
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);
534 return constraint;
535 error:
536 isl_constraint_free(constraint);
537 return NULL;
540 int isl_constraint_is_equality(struct isl_constraint *constraint)
542 if (!constraint)
543 return -1;
544 return constraint->line >= constraint->bmap->eq;
547 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
549 int i;
551 if (!constraint)
552 return -1;
553 if (isl_constraint_is_equality(constraint))
554 return 0;
555 for (i = 0; i < constraint->bmap->n_div; ++i) {
556 if (isl_int_is_zero(constraint->bmap->div[i][0]))
557 continue;
558 if (isl_basic_map_is_div_constraint(constraint->bmap,
559 constraint->line[0], i))
560 return 1;
563 return 0;
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)
574 int k;
575 struct isl_basic_map *bmap;
576 isl_int *c;
577 unsigned total;
579 if (!constraint)
580 return NULL;
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);
585 return bmap;
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);
594 if (k < 0)
595 goto error;
596 c = bmap->eq[k];
598 else {
599 k = isl_basic_map_alloc_inequality(bmap);
600 if (k < 0)
601 goto error;
602 c = bmap->ineq[k];
604 total = isl_basic_map_total_dim(bmap);
605 isl_seq_cpy(c, constraint->line[0], 1 + total);
606 isl_constraint_free(constraint);
607 if (bmap)
608 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
609 return bmap;
610 error:
611 isl_constraint_free(constraint);
612 isl_basic_map_free(bmap);
613 return NULL;
616 struct isl_basic_set *isl_basic_set_from_constraint(
617 struct isl_constraint *constraint)
619 if (!constraint)
620 return NULL;
622 isl_assert(constraint->ctx,n(constraint, isl_dim_in) == 0, goto error);
623 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
624 error:
625 isl_constraint_free(constraint);
626 return NULL;
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)
633 int i;
634 unsigned offset;
635 unsigned total;
637 if (!bmap)
638 return -1;
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),
647 &bmap->eq[i]);
648 return 1;
650 return 0;
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,
658 type, pos, c);
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)
666 int i, j;
667 unsigned offset;
668 unsigned total;
669 isl_int m;
670 isl_int **lower_line, **upper_line;
672 if (!bset)
673 return -1;
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);
677 isl_int_init(m);
678 for (i = 0; i < bset->n_ineq; ++i) {
679 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
680 continue;
681 if (isl_int_is_one(bset->ineq[i][offset + pos]))
682 continue;
683 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
684 continue;
685 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
686 1+total-offset-pos-1) != -1)
687 continue;
688 for (j = i + 1; j < bset->n_ineq; ++j) {
689 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
690 total))
691 continue;
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]))
694 continue;
696 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
697 lower_line = &bset->ineq[i];
698 upper_line = &bset->ineq[j];
699 } else {
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);
707 isl_int_clear(m);
708 return 1;
711 *lower = NULL;
712 *upper = NULL;
713 isl_int_clear(m);
714 return 0;
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)
730 int k;
731 isl_int t;
732 unsigned total;
734 k = isl_basic_set_alloc_inequality(bset);
735 if (k < 0)
736 goto error;
738 total = isl_basic_set_dim(bset, isl_dim_all);
740 isl_int_init(t);
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,
746 total - abs_pos);
748 if (strict)
749 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
751 isl_int_clear(t);
753 return bset;
754 error:
755 isl_basic_set_free(bset);
756 return NULL;
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)
767 int j;
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) {
775 if (j == u)
776 continue;
777 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
778 continue;
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);
786 return 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)
797 int j;
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) {
805 if (j == l)
806 continue;
807 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
808 continue;
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);
816 return 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;
828 int i;
830 for (i = 0; i < bset->n_ineq; ++i) {
831 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
832 continue;
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);
838 continue;
840 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
841 &bset->ineq[i]);
842 if (!upper || !context_i)
843 goto error;
844 if (fn(NULL, upper, context_i, user) < 0)
845 break;
848 isl_basic_set_free(context);
850 if (i < bset->n_ineq)
851 return -1;
853 return 0;
854 error:
855 isl_constraint_free(upper);
856 isl_basic_set_free(context_i);
857 isl_basic_set_free(context);
858 return -1;
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;
870 int i;
872 for (i = 0; i < bset->n_ineq; ++i) {
873 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
874 continue;
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);
880 continue;
882 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
883 &bset->ineq[i]);
884 if (!lower || !context_i)
885 goto error;
886 if (fn(lower, NULL, context_i, user) < 0)
887 break;
890 isl_basic_set_free(context);
892 if (i < bset->n_ineq)
893 return -1;
895 return 0;
896 error:
897 isl_constraint_free(lower);
898 isl_basic_set_free(context_i);
899 isl_basic_set_free(context);
900 return -1;
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;
913 int i, j;
915 for (i = 0; i < bset->n_ineq; ++i) {
916 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
917 continue;
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);
923 continue;
926 for (j = 0; j < bset->n_ineq; ++j) {
927 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
928 continue;
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,
933 0, 1);
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);
940 continue;
942 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
943 &bset->ineq[i]);
944 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
945 &bset->ineq[j]);
946 if (!lower || !upper || !context_j)
947 goto error;
948 if (fn(lower, upper, context_j, user) < 0)
949 break;
952 isl_basic_set_free(context_i);
954 if (j < bset->n_ineq)
955 break;
958 isl_basic_set_free(context);
960 if (i < bset->n_ineq)
961 return -1;
963 return 0;
964 error:
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);
970 return -1;
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
987 * act accordingly.
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)
995 int i;
996 isl_constraint *lower = NULL;
997 isl_constraint *upper = NULL;
998 isl_basic_set *context = NULL;
999 unsigned abs_pos;
1000 int n_lower, n_upper;
1002 if (!bset)
1003 return -1;
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,
1006 return -1);
1008 abs_pos = pos;
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]))
1014 continue;
1016 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1017 &bset->eq[i]);
1018 upper = isl_constraint_copy(lower);
1019 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1020 type, pos, 1);
1021 if (!lower || !upper || !context)
1022 goto error;
1023 return fn(lower, upper, context, user);
1026 n_lower = 0;
1027 n_upper = 0;
1028 for (i = 0; i < bset->n_ineq; ++i) {
1029 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1030 n_lower++;
1031 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1032 n_upper++;
1035 context = isl_basic_set_copy(bset);
1036 context = isl_basic_set_cow(context);
1037 if (!context)
1038 goto error;
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);
1046 if (!n_lower)
1047 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1048 fn, user);
1049 if (!n_upper)
1050 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1051 fn, user);
1052 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1053 fn, user);
1054 error:
1055 isl_constraint_free(lower);
1056 isl_constraint_free(upper);
1057 isl_basic_set_free(context);
1058 return -1;
1061 __isl_give isl_aff *isl_constraint_get_bound(
1062 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1064 isl_aff *aff;
1065 isl_local_space *ls;
1067 if (!constraint)
1068 return NULL;
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",
1080 return NULL);
1082 ls = isl_basic_set_get_local_space(constraint->bmap);
1083 aff = isl_aff_alloc(ls);
1084 if (!aff)
1085 return NULL;
1087 if (isl_int_is_neg(constraint->line[0][pos]))
1088 isl_seq_cpy(aff->v->el + 1, constraint->line[0],
1089 aff->v->size - 1);
1090 else
1091 isl_seq_neg(aff->v->el + 1, constraint->line[0],
1092 aff->v->size - 1);
1093 isl_int_set_si(aff->v->el[1 + pos], 0);
1094 isl_int_abs(aff->v->el[0], constraint->line[0][pos]);
1096 return aff;
1099 /* For an inequality constraint
1101 * f >= 0
1103 * or an equality constraint
1105 * f = 0
1107 * return the affine expression f.
1109 __isl_give isl_aff *isl_constraint_get_aff(
1110 __isl_keep isl_constraint *constraint)
1112 isl_aff *aff;
1113 isl_local_space *ls;
1115 if (!constraint)
1116 return NULL;
1118 ls = isl_basic_set_get_local_space(constraint->bmap);
1119 aff = isl_aff_alloc(ls);
1120 if (!aff)
1121 return NULL;
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);
1126 return aff;
1129 /* Construct an equality constraint equating the given affine expression
1130 * to zero.
1132 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1134 int k;
1135 isl_basic_set *bset;
1137 if (!aff)
1138 return NULL;
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);
1143 if (k < 0)
1144 goto error;
1146 isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1147 isl_aff_free(aff);
1149 return isl_basic_set_constraint(bset, &bset->eq[k]);
1150 error:
1151 isl_aff_free(aff);
1152 isl_basic_set_free(bset);
1153 return NULL;
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)
1161 int k;
1162 isl_basic_set *bset;
1164 if (!aff)
1165 return NULL;
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);
1170 if (k < 0)
1171 goto error;
1173 isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1174 isl_aff_free(aff);
1176 return isl_basic_set_constraint(bset, &bset->ineq[k]);
1177 error:
1178 isl_aff_free(aff);
1179 isl_basic_set_free(bset);
1180 return NULL;