extract out shared isl_point_dim
[isl.git] / isl_constraint.c
blob836a025c68c99dc3a034ecf4617425a44bdf0c47
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <isl_map_private.h>
14 #include <isl_constraint_private.h>
15 #include <isl_space_private.h>
16 #include <isl_seq.h>
17 #include <isl_aff_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 #include <bset_to_bmap.c>
23 #include <bset_from_bmap.c>
25 #undef BASE
26 #define BASE constraint
28 #include <isl_list_templ.c>
30 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
32 return c ? isl_local_space_get_ctx(c->ls) : NULL;
35 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
37 return isl_local_space_dim(c->ls, type);
40 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
42 return isl_local_space_offset(c->ls, type);
45 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
46 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
48 isl_constraint *constraint;
50 if (!ls || !v)
51 goto error;
53 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
54 if (!constraint)
55 goto error;
57 constraint->ref = 1;
58 constraint->eq = eq;
59 constraint->ls = ls;
60 constraint->v = v;
62 return constraint;
63 error:
64 isl_local_space_free(ls);
65 isl_vec_free(v);
66 return NULL;
69 __isl_give isl_constraint *isl_constraint_alloc(int eq,
70 __isl_take isl_local_space *ls)
72 isl_ctx *ctx;
73 isl_vec *v;
75 if (!ls)
76 return NULL;
78 ctx = isl_local_space_get_ctx(ls);
79 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
80 v = isl_vec_clr(v);
81 return isl_constraint_alloc_vec(eq, ls, v);
84 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
85 isl_int **line)
87 int eq;
88 isl_ctx *ctx;
89 isl_vec *v;
90 isl_local_space *ls = NULL;
91 isl_constraint *constraint;
93 if (!bmap || !line)
94 goto error;
96 eq = line >= bmap->eq;
98 ctx = isl_basic_map_get_ctx(bmap);
99 ls = isl_basic_map_get_local_space(bmap);
100 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
101 if (!v)
102 goto error;
103 isl_seq_cpy(v->el, line[0], v->size);
104 constraint = isl_constraint_alloc_vec(eq, ls, v);
106 isl_basic_map_free(bmap);
107 return constraint;
108 error:
109 isl_local_space_free(ls);
110 isl_basic_map_free(bmap);
111 return NULL;
114 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
115 isl_int **line)
117 return isl_basic_map_constraint(bset_to_bmap(bset), line);
120 __isl_give isl_constraint *isl_constraint_alloc_equality(
121 __isl_take isl_local_space *ls)
123 return isl_constraint_alloc(1, ls);
126 __isl_give isl_constraint *isl_constraint_alloc_inequality(
127 __isl_take isl_local_space *ls)
129 return isl_constraint_alloc(0, ls);
132 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
134 if (!c)
135 return NULL;
137 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
138 isl_vec_copy(c->v));
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 __isl_null isl_constraint *isl_constraint_free(__isl_take isl_constraint *c)
163 if (!c)
164 return NULL;
166 if (--c->ref > 0)
167 return NULL;
169 isl_local_space_free(c->ls);
170 isl_vec_free(c->v);
171 free(c);
173 return NULL;
176 /* Return the number of constraints in "bmap", i.e., the
177 * number of times isl_basic_map_foreach_constraint will
178 * call the callback.
180 int isl_basic_map_n_constraint(__isl_keep isl_basic_map *bmap)
182 if (!bmap)
183 return -1;
185 return bmap->n_eq + bmap->n_ineq;
188 /* Return the number of constraints in "bset", i.e., the
189 * number of times isl_basic_set_foreach_constraint will
190 * call the callback.
192 int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
194 return isl_basic_map_n_constraint(bset);
197 isl_stat isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
198 isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
200 int i;
201 struct isl_constraint *c;
203 if (!bmap)
204 return isl_stat_error;
206 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
207 return isl_stat_error);
209 for (i = 0; i < bmap->n_eq; ++i) {
210 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
211 &bmap->eq[i]);
212 if (!c)
213 return isl_stat_error;
214 if (fn(c, user) < 0)
215 return isl_stat_error;
218 for (i = 0; i < bmap->n_ineq; ++i) {
219 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
220 &bmap->ineq[i]);
221 if (!c)
222 return isl_stat_error;
223 if (fn(c, user) < 0)
224 return isl_stat_error;
227 return isl_stat_ok;
230 isl_stat isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
231 isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
233 return isl_basic_map_foreach_constraint(bset_to_bmap(bset), fn, user);
236 /* Add the constraint to the list that "user" points to, if it is not
237 * a div constraint.
239 static isl_stat collect_constraint(__isl_take isl_constraint *constraint,
240 void *user)
242 isl_constraint_list **list = user;
244 if (isl_constraint_is_div_constraint(constraint))
245 isl_constraint_free(constraint);
246 else
247 *list = isl_constraint_list_add(*list, constraint);
249 return isl_stat_ok;
252 /* Return a list of constraints that, when combined, are equivalent
253 * to "bmap". The input is required to have only known divs.
255 * There is no need to include the div constraints as they are
256 * implied by the div expressions.
258 __isl_give isl_constraint_list *isl_basic_map_get_constraint_list(
259 __isl_keep isl_basic_map *bmap)
261 int n;
262 isl_bool known;
263 isl_ctx *ctx;
264 isl_constraint_list *list;
266 known = isl_basic_map_divs_known(bmap);
267 if (known < 0)
268 return NULL;
269 ctx = isl_basic_map_get_ctx(bmap);
270 if (!known)
271 isl_die(ctx, isl_error_invalid,
272 "input involves unknown divs", return NULL);
274 n = isl_basic_map_n_constraint(bmap);
275 list = isl_constraint_list_alloc(ctx, n);
276 if (isl_basic_map_foreach_constraint(bmap,
277 &collect_constraint, &list) < 0)
278 list = isl_constraint_list_free(list);
280 return list;
283 /* Return a list of constraints that, when combined, are equivalent
284 * to "bset". The input is required to have only known divs.
286 __isl_give isl_constraint_list *isl_basic_set_get_constraint_list(
287 __isl_keep isl_basic_set *bset)
289 return isl_basic_map_get_constraint_list(bset);
292 int isl_constraint_is_equal(struct isl_constraint *constraint1,
293 struct isl_constraint *constraint2)
295 int equal;
297 if (!constraint1 || !constraint2)
298 return 0;
299 if (constraint1->eq != constraint2->eq)
300 return 0;
301 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
302 if (equal < 0 || !equal)
303 return equal;
304 return isl_vec_is_equal(constraint1->v, constraint2->v);
307 struct isl_basic_map *isl_basic_map_add_constraint(
308 struct isl_basic_map *bmap, struct isl_constraint *constraint)
310 isl_ctx *ctx;
311 isl_space *space;
312 int equal_space;
314 if (!bmap || !constraint)
315 goto error;
317 ctx = isl_constraint_get_ctx(constraint);
318 space = isl_constraint_get_space(constraint);
319 equal_space = isl_space_is_equal(bmap->dim, space);
320 isl_space_free(space);
321 isl_assert(ctx, equal_space, goto error);
323 bmap = isl_basic_map_intersect(bmap,
324 isl_basic_map_from_constraint(constraint));
325 return bmap;
326 error:
327 isl_basic_map_free(bmap);
328 isl_constraint_free(constraint);
329 return NULL;
332 struct isl_basic_set *isl_basic_set_add_constraint(
333 struct isl_basic_set *bset, struct isl_constraint *constraint)
335 return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset),
336 constraint));
339 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
340 __isl_take isl_constraint *constraint)
342 isl_basic_map *bmap;
344 bmap = isl_basic_map_from_constraint(constraint);
345 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
347 return map;
350 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
351 __isl_take isl_constraint *constraint)
353 return isl_map_add_constraint(set, constraint);
356 /* Return the space of "constraint".
358 static __isl_keep isl_space *isl_constraint_peek_space(
359 __isl_keep isl_constraint *constraint)
361 return constraint ? isl_local_space_peek_space(constraint->ls) : NULL;
364 __isl_give isl_space *isl_constraint_get_space(
365 __isl_keep isl_constraint *constraint)
367 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
370 __isl_give isl_local_space *isl_constraint_get_local_space(
371 __isl_keep isl_constraint *constraint)
373 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
376 int isl_constraint_dim(__isl_keep isl_constraint *constraint,
377 enum isl_dim_type type)
379 if (!constraint)
380 return -1;
381 return n(constraint, type);
384 #undef TYPE
385 #define TYPE isl_constraint
386 static
387 #include "check_type_range_templ.c"
389 isl_bool isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
390 enum isl_dim_type type, unsigned first, unsigned n)
392 int i;
393 int *active = NULL;
394 isl_bool involves = isl_bool_false;
396 if (!constraint)
397 return isl_bool_error;
398 if (n == 0)
399 return isl_bool_false;
401 if (isl_constraint_check_range(constraint, type, first, n) < 0)
402 return isl_bool_error;
404 active = isl_local_space_get_active(constraint->ls,
405 constraint->v->el + 1);
406 if (!active)
407 goto error;
409 first += isl_local_space_offset(constraint->ls, type) - 1;
410 for (i = 0; i < n; ++i)
411 if (active[first + i]) {
412 involves = isl_bool_true;
413 break;
416 free(active);
418 return involves;
419 error:
420 free(active);
421 return isl_bool_error;
424 /* Does the given constraint represent a lower bound on the given
425 * dimension?
427 isl_bool isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
428 enum isl_dim_type type, unsigned pos)
430 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
431 return isl_bool_error;
433 pos += isl_local_space_offset(constraint->ls, type);
434 return isl_int_is_pos(constraint->v->el[pos]);
437 /* Does the given constraint represent an upper bound on the given
438 * dimension?
440 isl_bool isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
441 enum isl_dim_type type, unsigned pos)
443 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
444 return isl_bool_error;
446 pos += isl_local_space_offset(constraint->ls, type);
447 return isl_int_is_neg(constraint->v->el[pos]);
450 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
451 enum isl_dim_type type, unsigned pos)
453 return constraint ?
454 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
457 void isl_constraint_get_constant(__isl_keep isl_constraint *constraint,
458 isl_int *v)
460 if (!constraint)
461 return;
462 isl_int_set(*v, constraint->v->el[0]);
465 /* Return the constant term of "constraint".
467 __isl_give isl_val *isl_constraint_get_constant_val(
468 __isl_keep isl_constraint *constraint)
470 isl_ctx *ctx;
472 if (!constraint)
473 return NULL;
475 ctx = isl_constraint_get_ctx(constraint);
476 return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
479 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
480 enum isl_dim_type type, int pos, isl_int *v)
482 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
483 return;
485 pos += isl_local_space_offset(constraint->ls, type);
486 isl_int_set(*v, constraint->v->el[pos]);
489 /* Return the coefficient of the variable of type "type" at position "pos"
490 * of "constraint".
492 __isl_give isl_val *isl_constraint_get_coefficient_val(
493 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
495 isl_ctx *ctx;
497 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
498 return NULL;
500 ctx = isl_constraint_get_ctx(constraint);
501 pos += isl_local_space_offset(constraint->ls, type);
502 return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
505 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
506 int pos)
508 if (!constraint)
509 return NULL;
511 return isl_local_space_get_div(constraint->ls, pos);
514 __isl_give isl_constraint *isl_constraint_set_constant(
515 __isl_take isl_constraint *constraint, isl_int v)
517 constraint = isl_constraint_cow(constraint);
518 if (!constraint)
519 return NULL;
521 constraint->v = isl_vec_cow(constraint->v);
522 if (!constraint->v)
523 return isl_constraint_free(constraint);
525 isl_int_set(constraint->v->el[0], v);
526 return constraint;
529 /* Replace the constant term of "constraint" by "v".
531 __isl_give isl_constraint *isl_constraint_set_constant_val(
532 __isl_take isl_constraint *constraint, __isl_take isl_val *v)
534 constraint = isl_constraint_cow(constraint);
535 if (!constraint || !v)
536 goto error;
537 if (!isl_val_is_int(v))
538 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
539 "expecting integer value", goto error);
540 constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
541 if (!constraint->v)
542 constraint = isl_constraint_free(constraint);
543 return constraint;
544 error:
545 isl_val_free(v);
546 return isl_constraint_free(constraint);
549 __isl_give isl_constraint *isl_constraint_set_constant_si(
550 __isl_take isl_constraint *constraint, int v)
552 constraint = isl_constraint_cow(constraint);
553 if (!constraint)
554 return NULL;
556 constraint->v = isl_vec_cow(constraint->v);
557 if (!constraint->v)
558 return isl_constraint_free(constraint);
560 isl_int_set_si(constraint->v->el[0], v);
561 return constraint;
564 __isl_give isl_constraint *isl_constraint_set_coefficient(
565 __isl_take isl_constraint *constraint,
566 enum isl_dim_type type, int pos, isl_int v)
568 constraint = isl_constraint_cow(constraint);
569 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
570 return isl_constraint_free(constraint);
572 constraint->v = isl_vec_cow(constraint->v);
573 if (!constraint->v)
574 return isl_constraint_free(constraint);
576 pos += isl_local_space_offset(constraint->ls, type);
577 isl_int_set(constraint->v->el[pos], v);
579 return constraint;
582 /* Replace the coefficient of the variable of type "type" at position "pos"
583 * of "constraint" by "v".
585 __isl_give isl_constraint *isl_constraint_set_coefficient_val(
586 __isl_take isl_constraint *constraint,
587 enum isl_dim_type type, int pos, __isl_take isl_val *v)
589 constraint = isl_constraint_cow(constraint);
590 if (!constraint || !v)
591 goto error;
592 if (!isl_val_is_int(v))
593 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
594 "expecting integer value", goto error);
595 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
596 goto error;
598 pos += isl_local_space_offset(constraint->ls, type);
599 constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
600 if (!constraint->v)
601 constraint = isl_constraint_free(constraint);
602 return constraint;
603 error:
604 isl_val_free(v);
605 return isl_constraint_free(constraint);
608 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
609 __isl_take isl_constraint *constraint,
610 enum isl_dim_type type, int pos, int v)
612 constraint = isl_constraint_cow(constraint);
613 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
614 return isl_constraint_free(constraint);
616 constraint->v = isl_vec_cow(constraint->v);
617 if (!constraint->v)
618 return isl_constraint_free(constraint);
620 pos += isl_local_space_offset(constraint->ls, type);
621 isl_int_set_si(constraint->v->el[pos], v);
623 return constraint;
626 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
628 isl_ctx *ctx;
630 constraint = isl_constraint_cow(constraint);
631 if (!constraint)
632 return NULL;
634 ctx = isl_constraint_get_ctx(constraint);
635 if (isl_constraint_is_equality(constraint))
636 isl_die(ctx, isl_error_invalid, "cannot negate equality",
637 return isl_constraint_free(constraint));
638 constraint->v = isl_vec_neg(constraint->v);
639 constraint->v = isl_vec_cow(constraint->v);
640 if (!constraint->v)
641 return isl_constraint_free(constraint);
642 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
643 return constraint;
646 isl_bool isl_constraint_is_equality(struct isl_constraint *constraint)
648 if (!constraint)
649 return isl_bool_error;
650 return constraint->eq;
653 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
655 int i;
656 int n_div;
658 if (!constraint)
659 return -1;
660 if (isl_constraint_is_equality(constraint))
661 return 0;
662 n_div = isl_constraint_dim(constraint, isl_dim_div);
663 for (i = 0; i < n_div; ++i) {
664 isl_bool is_div;
665 is_div = isl_local_space_is_div_constraint(constraint->ls,
666 constraint->v->el, i);
667 if (is_div < 0 || is_div)
668 return is_div;
671 return 0;
674 /* Is "constraint" an equality that corresponds to integer division "div"?
676 * That is, given an integer division of the form
678 * a = floor((f + c)/m)
680 * is the equality of the form
682 * -f + m d + c' = 0
684 * Note that the constant term is not checked explicitly, but given
685 * that this is a valid equality constraint, the constant c' necessarily
686 * has a value close to -c.
688 isl_bool isl_constraint_is_div_equality(__isl_keep isl_constraint *constraint,
689 unsigned div)
691 isl_bool equality;
693 equality = isl_constraint_is_equality(constraint);
694 if (equality < 0 || !equality)
695 return equality;
696 return isl_local_space_is_div_equality(constraint->ls,
697 constraint->v->el, div);
700 /* We manually set ISL_BASIC_SET_FINAL instead of calling
701 * isl_basic_map_finalize because we want to keep the position
702 * of the divs and we therefore do not want to throw away redundant divs.
703 * This is arguably a bit fragile.
705 __isl_give isl_basic_map *isl_basic_map_from_constraint(
706 __isl_take isl_constraint *constraint)
708 int k;
709 isl_local_space *ls;
710 struct isl_basic_map *bmap;
711 isl_int *c;
712 unsigned total;
714 if (!constraint)
715 return NULL;
717 ls = isl_local_space_copy(constraint->ls);
718 bmap = isl_basic_map_from_local_space(ls);
719 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
720 if (isl_constraint_is_equality(constraint)) {
721 k = isl_basic_map_alloc_equality(bmap);
722 if (k < 0)
723 goto error;
724 c = bmap->eq[k];
726 else {
727 k = isl_basic_map_alloc_inequality(bmap);
728 if (k < 0)
729 goto error;
730 c = bmap->ineq[k];
732 total = isl_basic_map_total_dim(bmap);
733 isl_seq_cpy(c, constraint->v->el, 1 + total);
734 isl_constraint_free(constraint);
735 if (bmap)
736 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
737 return bmap;
738 error:
739 isl_constraint_free(constraint);
740 isl_basic_map_free(bmap);
741 return NULL;
744 __isl_give isl_basic_set *isl_basic_set_from_constraint(
745 __isl_take isl_constraint *constraint)
747 isl_space *space;
749 space = isl_constraint_peek_space(constraint);
750 if (isl_space_check_is_set(space) < 0)
751 goto error;
752 return bset_from_bmap(isl_basic_map_from_constraint(constraint));
753 error:
754 isl_constraint_free(constraint);
755 return NULL;
758 /* Is the variable of "type" at position "pos" of "bmap" defined
759 * in terms of earlier dimensions through an equality?
761 * If so, and if c is not NULL, then return a copy of this equality in *c.
763 isl_bool isl_basic_map_has_defining_equality(
764 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
765 __isl_give isl_constraint **c)
767 int i;
768 unsigned offset;
769 unsigned total;
771 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
772 return isl_bool_error;
773 offset = isl_basic_map_offset(bmap, type);
774 total = isl_basic_map_total_dim(bmap);
775 for (i = 0; i < bmap->n_eq; ++i) {
776 if (isl_int_is_zero(bmap->eq[i][offset + pos]) ||
777 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
778 1+total-offset-pos-1) != -1)
779 continue;
780 if (c)
781 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
782 &bmap->eq[i]);
783 return isl_bool_true;
785 return isl_bool_false;
788 /* Is the variable of "type" at position "pos" of "bset" defined
789 * in terms of earlier dimensions through an equality?
791 * If so, and if c is not NULL, then return a copy of this equality in *c.
793 isl_bool isl_basic_set_has_defining_equality(
794 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
795 __isl_give isl_constraint **c)
797 return isl_basic_map_has_defining_equality(bset_to_bmap(bset),
798 type, pos, c);
801 isl_bool isl_basic_set_has_defining_inequalities(
802 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
803 struct isl_constraint **lower,
804 struct isl_constraint **upper)
806 int i, j;
807 unsigned offset;
808 unsigned total;
809 isl_int m;
810 isl_int **lower_line, **upper_line;
812 if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
813 return isl_bool_error;
814 offset = isl_basic_set_offset(bset, type);
815 total = isl_basic_set_total_dim(bset);
816 isl_int_init(m);
817 for (i = 0; i < bset->n_ineq; ++i) {
818 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
819 continue;
820 if (isl_int_is_one(bset->ineq[i][offset + pos]))
821 continue;
822 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
823 continue;
824 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
825 1+total-offset-pos-1) != -1)
826 continue;
827 for (j = i + 1; j < bset->n_ineq; ++j) {
828 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
829 total))
830 continue;
831 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
832 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
833 continue;
835 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
836 lower_line = &bset->ineq[i];
837 upper_line = &bset->ineq[j];
838 } else {
839 lower_line = &bset->ineq[j];
840 upper_line = &bset->ineq[i];
842 *lower = isl_basic_set_constraint(
843 isl_basic_set_copy(bset), lower_line);
844 *upper = isl_basic_set_constraint(
845 isl_basic_set_copy(bset), upper_line);
846 isl_int_clear(m);
847 return isl_bool_true;
850 *lower = NULL;
851 *upper = NULL;
852 isl_int_clear(m);
853 return isl_bool_false;
856 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
857 * (in "a" and "b"), add a constraint to "bset" that ensures that the
858 * bound implied by "a" is (strictly) larger than the bound implied by "b".
860 * If both constraints imply lower bounds, then this means that "a" is
861 * active in the result.
862 * If both constraints imply upper bounds, then this means that "b" is
863 * active in the result.
865 static __isl_give isl_basic_set *add_larger_bound_constraint(
866 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
867 unsigned abs_pos, int strict)
869 int k;
870 isl_int t;
871 unsigned total;
873 k = isl_basic_set_alloc_inequality(bset);
874 if (k < 0)
875 goto error;
877 total = isl_basic_set_dim(bset, isl_dim_all);
879 isl_int_init(t);
880 isl_int_neg(t, b[1 + abs_pos]);
882 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
883 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
884 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
885 total - abs_pos);
887 if (strict)
888 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
890 isl_int_clear(t);
892 return bset;
893 error:
894 isl_basic_set_free(bset);
895 return NULL;
898 /* Add constraints to "context" that ensure that "u" is the smallest
899 * (and therefore active) upper bound on "abs_pos" in "bset" and return
900 * the resulting basic set.
902 static __isl_give isl_basic_set *set_smallest_upper_bound(
903 __isl_keep isl_basic_set *context,
904 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
906 int j;
908 context = isl_basic_set_copy(context);
909 context = isl_basic_set_cow(context);
911 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
913 for (j = 0; j < bset->n_ineq; ++j) {
914 if (j == u)
915 continue;
916 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
917 continue;
918 context = add_larger_bound_constraint(context,
919 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
922 context = isl_basic_set_simplify(context);
923 context = isl_basic_set_finalize(context);
925 return context;
928 /* Add constraints to "context" that ensure that "u" is the largest
929 * (and therefore active) upper bound on "abs_pos" in "bset" and return
930 * the resulting basic set.
932 static __isl_give isl_basic_set *set_largest_lower_bound(
933 __isl_keep isl_basic_set *context,
934 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
936 int j;
938 context = isl_basic_set_copy(context);
939 context = isl_basic_set_cow(context);
941 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
943 for (j = 0; j < bset->n_ineq; ++j) {
944 if (j == l)
945 continue;
946 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
947 continue;
948 context = add_larger_bound_constraint(context,
949 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
952 context = isl_basic_set_simplify(context);
953 context = isl_basic_set_finalize(context);
955 return context;
958 static isl_stat foreach_upper_bound(__isl_keep isl_basic_set *bset,
959 enum isl_dim_type type, unsigned abs_pos,
960 __isl_take isl_basic_set *context, int n_upper,
961 isl_stat (*fn)(__isl_take isl_constraint *lower,
962 __isl_take isl_constraint *upper,
963 __isl_take isl_basic_set *bset, void *user), void *user)
965 isl_basic_set *context_i;
966 isl_constraint *upper = NULL;
967 int i;
969 for (i = 0; i < bset->n_ineq; ++i) {
970 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
971 continue;
973 context_i = set_smallest_upper_bound(context, bset,
974 abs_pos, n_upper, i);
975 if (isl_basic_set_is_empty(context_i)) {
976 isl_basic_set_free(context_i);
977 continue;
979 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
980 &bset->ineq[i]);
981 if (!upper || !context_i)
982 goto error;
983 if (fn(NULL, upper, context_i, user) < 0)
984 break;
987 isl_basic_set_free(context);
989 if (i < bset->n_ineq)
990 return isl_stat_error;
992 return isl_stat_ok;
993 error:
994 isl_constraint_free(upper);
995 isl_basic_set_free(context_i);
996 isl_basic_set_free(context);
997 return isl_stat_error;
1000 static isl_stat foreach_lower_bound(__isl_keep isl_basic_set *bset,
1001 enum isl_dim_type type, unsigned abs_pos,
1002 __isl_take isl_basic_set *context, int n_lower,
1003 isl_stat (*fn)(__isl_take isl_constraint *lower,
1004 __isl_take isl_constraint *upper,
1005 __isl_take isl_basic_set *bset, void *user), void *user)
1007 isl_basic_set *context_i;
1008 isl_constraint *lower = NULL;
1009 int i;
1011 for (i = 0; i < bset->n_ineq; ++i) {
1012 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
1013 continue;
1015 context_i = set_largest_lower_bound(context, bset,
1016 abs_pos, n_lower, i);
1017 if (isl_basic_set_is_empty(context_i)) {
1018 isl_basic_set_free(context_i);
1019 continue;
1021 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1022 &bset->ineq[i]);
1023 if (!lower || !context_i)
1024 goto error;
1025 if (fn(lower, NULL, context_i, user) < 0)
1026 break;
1029 isl_basic_set_free(context);
1031 if (i < bset->n_ineq)
1032 return isl_stat_error;
1034 return isl_stat_ok;
1035 error:
1036 isl_constraint_free(lower);
1037 isl_basic_set_free(context_i);
1038 isl_basic_set_free(context);
1039 return isl_stat_error;
1042 static isl_stat foreach_bound_pair(__isl_keep isl_basic_set *bset,
1043 enum isl_dim_type type, unsigned abs_pos,
1044 __isl_take isl_basic_set *context, int n_lower, int n_upper,
1045 isl_stat (*fn)(__isl_take isl_constraint *lower,
1046 __isl_take isl_constraint *upper,
1047 __isl_take isl_basic_set *bset, void *user), void *user)
1049 isl_basic_set *context_i, *context_j;
1050 isl_constraint *lower = NULL;
1051 isl_constraint *upper = NULL;
1052 int i, j;
1054 for (i = 0; i < bset->n_ineq; ++i) {
1055 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1056 continue;
1058 context_i = set_largest_lower_bound(context, bset,
1059 abs_pos, n_lower, i);
1060 if (isl_basic_set_is_empty(context_i)) {
1061 isl_basic_set_free(context_i);
1062 continue;
1065 for (j = 0; j < bset->n_ineq; ++j) {
1066 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
1067 continue;
1069 context_j = set_smallest_upper_bound(context_i, bset,
1070 abs_pos, n_upper, j);
1071 context_j = isl_basic_set_extend_constraints(context_j,
1072 0, 1);
1073 context_j = add_larger_bound_constraint(context_j,
1074 bset->ineq[i], bset->ineq[j], abs_pos, 0);
1075 context_j = isl_basic_set_simplify(context_j);
1076 context_j = isl_basic_set_finalize(context_j);
1077 if (isl_basic_set_is_empty(context_j)) {
1078 isl_basic_set_free(context_j);
1079 continue;
1081 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1082 &bset->ineq[i]);
1083 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1084 &bset->ineq[j]);
1085 if (!lower || !upper || !context_j)
1086 goto error;
1087 if (fn(lower, upper, context_j, user) < 0)
1088 break;
1091 isl_basic_set_free(context_i);
1093 if (j < bset->n_ineq)
1094 break;
1097 isl_basic_set_free(context);
1099 if (i < bset->n_ineq)
1100 return isl_stat_error;
1102 return isl_stat_ok;
1103 error:
1104 isl_constraint_free(lower);
1105 isl_constraint_free(upper);
1106 isl_basic_set_free(context_i);
1107 isl_basic_set_free(context_j);
1108 isl_basic_set_free(context);
1109 return isl_stat_error;
1112 /* For each pair of lower and upper bounds on the variable "pos"
1113 * of type "type", call "fn" with these lower and upper bounds and the
1114 * set of constraints on the remaining variables where these bounds
1115 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1117 * If the designated variable is equal to an affine combination of the
1118 * other variables then fn is called with both lower and upper
1119 * set to the corresponding equality.
1121 * If there is no lower (or upper) bound, then NULL is passed
1122 * as the corresponding bound.
1124 * We first check if the variable is involved in any equality.
1125 * If not, we count the number of lower and upper bounds and
1126 * act accordingly.
1128 isl_stat isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1129 enum isl_dim_type type, unsigned pos,
1130 isl_stat (*fn)(__isl_take isl_constraint *lower,
1131 __isl_take isl_constraint *upper,
1132 __isl_take isl_basic_set *bset, void *user), void *user)
1134 int i;
1135 isl_constraint *lower = NULL;
1136 isl_constraint *upper = NULL;
1137 isl_basic_set *context = NULL;
1138 unsigned abs_pos;
1139 int n_lower, n_upper;
1141 if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
1142 return isl_stat_error;
1143 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1144 return isl_stat_error);
1146 abs_pos = pos;
1147 if (type == isl_dim_set)
1148 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1150 for (i = 0; i < bset->n_eq; ++i) {
1151 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1152 continue;
1154 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1155 &bset->eq[i]);
1156 upper = isl_constraint_copy(lower);
1157 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1158 type, pos, 1);
1159 if (!lower || !upper || !context)
1160 goto error;
1161 return fn(lower, upper, context, user);
1164 n_lower = 0;
1165 n_upper = 0;
1166 for (i = 0; i < bset->n_ineq; ++i) {
1167 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1168 n_lower++;
1169 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1170 n_upper++;
1173 context = isl_basic_set_copy(bset);
1174 context = isl_basic_set_cow(context);
1175 if (!context)
1176 goto error;
1177 for (i = context->n_ineq - 1; i >= 0; --i)
1178 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1179 isl_basic_set_drop_inequality(context, i);
1181 context = isl_basic_set_drop(context, type, pos, 1);
1182 if (!n_lower && !n_upper)
1183 return fn(NULL, NULL, context, user);
1184 if (!n_lower)
1185 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1186 fn, user);
1187 if (!n_upper)
1188 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1189 fn, user);
1190 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1191 fn, user);
1192 error:
1193 isl_constraint_free(lower);
1194 isl_constraint_free(upper);
1195 isl_basic_set_free(context);
1196 return isl_stat_error;
1199 __isl_give isl_aff *isl_constraint_get_bound(
1200 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1202 isl_space *space;
1203 isl_aff *aff;
1204 isl_ctx *ctx;
1206 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
1207 return NULL;
1208 space = isl_constraint_peek_space(constraint);
1209 if (isl_space_check_is_set(space) < 0)
1210 return NULL;
1212 ctx = isl_constraint_get_ctx(constraint);
1213 pos += offset(constraint, type);
1214 if (isl_int_is_zero(constraint->v->el[pos]))
1215 isl_die(ctx, isl_error_invalid,
1216 "constraint does not define a bound on given dimension",
1217 return NULL);
1219 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1220 if (!aff)
1221 return NULL;
1223 if (isl_int_is_neg(constraint->v->el[pos]))
1224 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1225 else
1226 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1227 isl_int_set_si(aff->v->el[1 + pos], 0);
1228 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1230 return aff;
1233 /* For an inequality constraint
1235 * f >= 0
1237 * or an equality constraint
1239 * f = 0
1241 * return the affine expression f.
1243 __isl_give isl_aff *isl_constraint_get_aff(
1244 __isl_keep isl_constraint *constraint)
1246 isl_aff *aff;
1248 if (!constraint)
1249 return NULL;
1251 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1252 if (!aff)
1253 return NULL;
1255 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1256 isl_int_set_si(aff->v->el[0], 1);
1258 return aff;
1261 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1262 * In particular, construct aff >= 0 or aff = 0.
1264 * The denominator of "aff" can be ignored.
1266 static __isl_give isl_constraint *isl_constraint_alloc_aff(int eq,
1267 __isl_take isl_aff *aff)
1269 isl_local_space *ls;
1270 isl_vec *v;
1272 if (!aff)
1273 return NULL;
1274 ls = isl_aff_get_domain_local_space(aff);
1275 v = isl_vec_drop_els(isl_vec_copy(aff->v), 0, 1);
1276 isl_aff_free(aff);
1278 return isl_constraint_alloc_vec(eq, ls, v);
1281 /* Construct an equality constraint equating the given affine expression
1282 * to zero.
1284 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1286 return isl_constraint_alloc_aff(1, aff);
1289 /* Construct an inequality constraint enforcing the given affine expression
1290 * to be non-negative.
1292 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1294 return isl_constraint_alloc_aff(0, aff);
1297 /* Compare two isl_constraints.
1299 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1300 * than "c2" and 0 if they are equal.
1302 * The order is fairly arbitrary. We do consider constraints that only involve
1303 * earlier dimensions as "smaller".
1305 int isl_constraint_plain_cmp(__isl_keep isl_constraint *c1,
1306 __isl_keep isl_constraint *c2)
1308 int cmp;
1309 int last1, last2;
1311 if (c1 == c2)
1312 return 0;
1313 if (!c1)
1314 return -1;
1315 if (!c2)
1316 return 1;
1317 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1318 if (cmp != 0)
1319 return cmp;
1321 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1322 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1323 if (last1 != last2)
1324 return last1 - last2;
1326 return isl_seq_cmp(c1->v->el, c2->v->el, c1->v->size);
1329 /* Compare two constraints based on their final (non-zero) coefficients.
1330 * In particular, the constraint that involves later variables or
1331 * that has a larger coefficient for a shared latest variable
1332 * is considered "greater" than the other constraint.
1334 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1335 * than "c2" and 0 if they are equal.
1337 * If the constraints live in different local spaces, then we cannot
1338 * really compare the constraints so we compare the local spaces instead.
1340 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint *c1,
1341 __isl_keep isl_constraint *c2)
1343 int cmp;
1344 int last1, last2;
1346 if (c1 == c2)
1347 return 0;
1348 if (!c1)
1349 return -1;
1350 if (!c2)
1351 return 1;
1352 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1353 if (cmp != 0)
1354 return cmp;
1356 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1357 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1358 if (last1 != last2)
1359 return last1 - last2;
1360 if (last1 == -1)
1361 return 0;
1362 return isl_int_abs_cmp(c1->v->el[1 + last1], c2->v->el[1 + last2]);