rename isl_basic_map_add to isl_basic_map_add_dims
[isl.git] / isl_constraint.c
bloba6e6add0e8d48e16e8b539f1a1562b52091d6f47
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>
21 #include <isl/deprecated/constraint_int.h>
23 #undef BASE
24 #define BASE constraint
26 #include <isl_list_templ.c>
28 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
30 return c ? isl_local_space_get_ctx(c->ls) : NULL;
33 static unsigned n(struct isl_constraint *c, enum isl_dim_type type)
35 return isl_local_space_dim(c->ls, type);
38 static unsigned offset(struct isl_constraint *c, enum isl_dim_type type)
40 return isl_local_space_offset(c->ls, type);
43 static unsigned basic_map_offset(__isl_keep isl_basic_map *bmap,
44 enum isl_dim_type type)
46 return type == isl_dim_div ? 1 + isl_space_dim(bmap->dim, isl_dim_all)
47 : 1 + isl_space_offset(bmap->dim, type);
50 static unsigned basic_set_offset(struct isl_basic_set *bset,
51 enum isl_dim_type type)
53 isl_space *dim = bset->dim;
54 switch (type) {
55 case isl_dim_param: return 1;
56 case isl_dim_in: return 1 + dim->nparam;
57 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
58 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
59 default: return 0;
63 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
64 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
66 isl_constraint *constraint;
68 if (!ls || !v)
69 goto error;
71 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
72 if (!constraint)
73 goto error;
75 constraint->ref = 1;
76 constraint->eq = eq;
77 constraint->ls = ls;
78 constraint->v = v;
80 return constraint;
81 error:
82 isl_local_space_free(ls);
83 isl_vec_free(v);
84 return NULL;
87 __isl_give isl_constraint *isl_constraint_alloc(int eq,
88 __isl_take isl_local_space *ls)
90 isl_ctx *ctx;
91 isl_vec *v;
93 if (!ls)
94 return NULL;
96 ctx = isl_local_space_get_ctx(ls);
97 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
98 v = isl_vec_clr(v);
99 return isl_constraint_alloc_vec(eq, ls, v);
102 struct isl_constraint *isl_basic_map_constraint(struct isl_basic_map *bmap,
103 isl_int **line)
105 int eq;
106 isl_ctx *ctx;
107 isl_vec *v;
108 isl_local_space *ls = NULL;
109 isl_constraint *constraint;
111 if (!bmap || !line)
112 goto error;
114 eq = line >= bmap->eq;
116 ctx = isl_basic_map_get_ctx(bmap);
117 ls = isl_basic_map_get_local_space(bmap);
118 v = isl_vec_alloc(ctx, 1 + isl_local_space_dim(ls, isl_dim_all));
119 if (!v)
120 goto error;
121 isl_seq_cpy(v->el, line[0], v->size);
122 constraint = isl_constraint_alloc_vec(eq, ls, v);
124 isl_basic_map_free(bmap);
125 return constraint;
126 error:
127 isl_local_space_free(ls);
128 isl_basic_map_free(bmap);
129 return NULL;
132 struct isl_constraint *isl_basic_set_constraint(struct isl_basic_set *bset,
133 isl_int **line)
135 return isl_basic_map_constraint((struct isl_basic_map *)bset, line);
138 __isl_give isl_constraint *isl_equality_alloc(__isl_take isl_local_space *ls)
140 return isl_constraint_alloc(1, ls);
143 __isl_give isl_constraint *isl_inequality_alloc(__isl_take isl_local_space *ls)
145 return isl_constraint_alloc(0, ls);
148 struct isl_constraint *isl_constraint_dup(struct isl_constraint *c)
150 if (!c)
151 return NULL;
153 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
154 isl_vec_copy(c->v));
157 struct isl_constraint *isl_constraint_cow(struct isl_constraint *c)
159 if (!c)
160 return NULL;
162 if (c->ref == 1)
163 return c;
164 c->ref--;
165 return isl_constraint_dup(c);
168 struct isl_constraint *isl_constraint_copy(struct isl_constraint *constraint)
170 if (!constraint)
171 return NULL;
173 constraint->ref++;
174 return constraint;
177 __isl_null isl_constraint *isl_constraint_free(__isl_take isl_constraint *c)
179 if (!c)
180 return NULL;
182 if (--c->ref > 0)
183 return NULL;
185 isl_local_space_free(c->ls);
186 isl_vec_free(c->v);
187 free(c);
189 return NULL;
192 /* Return the number of constraints in "bmap", i.e., the
193 * number of times isl_basic_map_foreach_constraint will
194 * call the callback.
196 int isl_basic_map_n_constraint(__isl_keep isl_basic_map *bmap)
198 if (!bmap)
199 return -1;
201 return bmap->n_eq + bmap->n_ineq;
204 /* Return the number of constraints in "bset", i.e., the
205 * number of times isl_basic_set_foreach_constraint will
206 * call the callback.
208 int isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
210 return isl_basic_map_n_constraint(bset);
213 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
214 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
216 int i;
217 struct isl_constraint *c;
219 if (!bmap)
220 return -1;
222 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
223 return -1);
225 for (i = 0; i < bmap->n_eq; ++i) {
226 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
227 &bmap->eq[i]);
228 if (!c)
229 return -1;
230 if (fn(c, user) < 0)
231 return -1;
234 for (i = 0; i < bmap->n_ineq; ++i) {
235 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
236 &bmap->ineq[i]);
237 if (!c)
238 return -1;
239 if (fn(c, user) < 0)
240 return -1;
243 return 0;
246 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
247 int (*fn)(__isl_take isl_constraint *c, void *user), void *user)
249 return isl_basic_map_foreach_constraint((isl_basic_map *)bset, fn, user);
252 /* Add the constraint to the list that "user" points to, if it is not
253 * a div constraint.
255 static int collect_constraint(__isl_take isl_constraint *constraint,
256 void *user)
258 isl_constraint_list **list = user;
260 if (isl_constraint_is_div_constraint(constraint))
261 isl_constraint_free(constraint);
262 else
263 *list = isl_constraint_list_add(*list, constraint);
265 return 0;
268 /* Return a list of constraints that, when combined, are equivalent
269 * to "bmap". The input is required to have only known divs.
271 * There is no need to include the div constraints as they are
272 * implied by the div expressions.
274 __isl_give isl_constraint_list *isl_basic_map_get_constraint_list(
275 __isl_keep isl_basic_map *bmap)
277 int n;
278 int known;
279 isl_ctx *ctx;
280 isl_constraint_list *list;
282 known = isl_basic_map_divs_known(bmap);
283 if (known < 0)
284 return NULL;
285 ctx = isl_basic_map_get_ctx(bmap);
286 if (!known)
287 isl_die(ctx, isl_error_invalid,
288 "input involves unknown divs", return NULL);
290 n = isl_basic_map_n_constraint(bmap);
291 list = isl_constraint_list_alloc(ctx, n);
292 if (isl_basic_map_foreach_constraint(bmap,
293 &collect_constraint, &list) < 0)
294 list = isl_constraint_list_free(list);
296 return list;
299 /* Return a list of constraints that, when combined, are equivalent
300 * to "bset". The input is required to have only known divs.
302 __isl_give isl_constraint_list *isl_basic_set_get_constraint_list(
303 __isl_keep isl_basic_set *bset)
305 return isl_basic_map_get_constraint_list(bset);
308 int isl_constraint_is_equal(struct isl_constraint *constraint1,
309 struct isl_constraint *constraint2)
311 int equal;
313 if (!constraint1 || !constraint2)
314 return 0;
315 if (constraint1->eq != constraint2->eq)
316 return 0;
317 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
318 if (equal < 0 || !equal)
319 return equal;
320 return isl_vec_is_equal(constraint1->v, constraint2->v);
323 struct isl_basic_map *isl_basic_map_add_constraint(
324 struct isl_basic_map *bmap, struct isl_constraint *constraint)
326 isl_ctx *ctx;
327 isl_space *dim;
328 int equal_space;
330 if (!bmap || !constraint)
331 goto error;
333 ctx = isl_constraint_get_ctx(constraint);
334 dim = isl_constraint_get_space(constraint);
335 equal_space = isl_space_is_equal(bmap->dim, dim);
336 isl_space_free(dim);
337 isl_assert(ctx, equal_space, goto error);
339 bmap = isl_basic_map_intersect(bmap,
340 isl_basic_map_from_constraint(constraint));
341 return bmap;
342 error:
343 isl_basic_map_free(bmap);
344 isl_constraint_free(constraint);
345 return NULL;
348 struct isl_basic_set *isl_basic_set_add_constraint(
349 struct isl_basic_set *bset, struct isl_constraint *constraint)
351 return (struct isl_basic_set *)
352 isl_basic_map_add_constraint((struct isl_basic_map *)bset,
353 constraint);
356 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
357 __isl_take isl_constraint *constraint)
359 isl_basic_map *bmap;
361 bmap = isl_basic_map_from_constraint(constraint);
362 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
364 return map;
367 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
368 __isl_take isl_constraint *constraint)
370 return isl_map_add_constraint(set, constraint);
373 __isl_give isl_space *isl_constraint_get_space(
374 __isl_keep isl_constraint *constraint)
376 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
379 __isl_give isl_local_space *isl_constraint_get_local_space(
380 __isl_keep isl_constraint *constraint)
382 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
385 int isl_constraint_dim(struct isl_constraint *constraint,
386 enum isl_dim_type type)
388 if (!constraint)
389 return -1;
390 return n(constraint, type);
393 int isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
394 enum isl_dim_type type, unsigned first, unsigned n)
396 int i;
397 isl_ctx *ctx;
398 int *active = NULL;
399 int involves = 0;
401 if (!constraint)
402 return -1;
403 if (n == 0)
404 return 0;
406 ctx = isl_constraint_get_ctx(constraint);
407 if (first + n > isl_constraint_dim(constraint, type))
408 isl_die(ctx, isl_error_invalid,
409 "range out of bounds", return -1);
411 active = isl_local_space_get_active(constraint->ls,
412 constraint->v->el + 1);
413 if (!active)
414 goto error;
416 first += isl_local_space_offset(constraint->ls, type) - 1;
417 for (i = 0; i < n; ++i)
418 if (active[first + i]) {
419 involves = 1;
420 break;
423 free(active);
425 return involves;
426 error:
427 free(active);
428 return -1;
431 /* Does the given constraint represent a lower bound on the given
432 * dimension?
434 int isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
435 enum isl_dim_type type, unsigned pos)
437 if (!constraint)
438 return -1;
440 if (pos >= isl_local_space_dim(constraint->ls, type))
441 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
442 "position out of bounds", return -1);
444 pos += isl_local_space_offset(constraint->ls, type);
445 return isl_int_is_pos(constraint->v->el[pos]);
448 /* Does the given constraint represent an upper bound on the given
449 * dimension?
451 int isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
452 enum isl_dim_type type, unsigned pos)
454 if (!constraint)
455 return -1;
457 if (pos >= isl_local_space_dim(constraint->ls, type))
458 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
459 "position out of bounds", return -1);
461 pos += isl_local_space_offset(constraint->ls, type);
462 return isl_int_is_neg(constraint->v->el[pos]);
465 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
466 enum isl_dim_type type, unsigned pos)
468 return constraint ?
469 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
472 void isl_constraint_get_constant(struct isl_constraint *constraint, isl_int *v)
474 if (!constraint)
475 return;
476 isl_int_set(*v, constraint->v->el[0]);
479 /* Return the constant term of "constraint".
481 __isl_give isl_val *isl_constraint_get_constant_val(
482 __isl_keep isl_constraint *constraint)
484 isl_ctx *ctx;
486 if (!constraint)
487 return NULL;
489 ctx = isl_constraint_get_ctx(constraint);
490 return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
493 void isl_constraint_get_coefficient(struct isl_constraint *constraint,
494 enum isl_dim_type type, int pos, isl_int *v)
496 if (!constraint)
497 return;
499 if (pos >= isl_local_space_dim(constraint->ls, type))
500 isl_die(constraint->v->ctx, isl_error_invalid,
501 "position out of bounds", return);
503 pos += isl_local_space_offset(constraint->ls, type);
504 isl_int_set(*v, constraint->v->el[pos]);
507 /* Return the coefficient of the variable of type "type" at position "pos"
508 * of "constraint".
510 __isl_give isl_val *isl_constraint_get_coefficient_val(
511 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
513 isl_ctx *ctx;
515 if (!constraint)
516 return NULL;
518 ctx = isl_constraint_get_ctx(constraint);
519 if (pos < 0 || pos >= isl_local_space_dim(constraint->ls, type))
520 isl_die(ctx, isl_error_invalid,
521 "position out of bounds", return NULL);
523 pos += isl_local_space_offset(constraint->ls, type);
524 return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
527 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
528 int pos)
530 if (!constraint)
531 return NULL;
533 return isl_local_space_get_div(constraint->ls, pos);
536 __isl_give isl_constraint *isl_constraint_set_constant(
537 __isl_take isl_constraint *constraint, isl_int v)
539 constraint = isl_constraint_cow(constraint);
540 if (!constraint)
541 return NULL;
543 constraint->v = isl_vec_cow(constraint->v);
544 if (!constraint->v)
545 return isl_constraint_free(constraint);
547 isl_int_set(constraint->v->el[0], v);
548 return constraint;
551 /* Replace the constant term of "constraint" by "v".
553 __isl_give isl_constraint *isl_constraint_set_constant_val(
554 __isl_take isl_constraint *constraint, __isl_take isl_val *v)
556 constraint = isl_constraint_cow(constraint);
557 if (!constraint || !v)
558 goto error;
559 if (!isl_val_is_int(v))
560 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
561 "expecting integer value", goto error);
562 constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
563 if (!constraint->v)
564 constraint = isl_constraint_free(constraint);
565 return constraint;
566 error:
567 isl_val_free(v);
568 return isl_constraint_free(constraint);
571 __isl_give isl_constraint *isl_constraint_set_constant_si(
572 __isl_take isl_constraint *constraint, int v)
574 constraint = isl_constraint_cow(constraint);
575 if (!constraint)
576 return NULL;
578 constraint->v = isl_vec_cow(constraint->v);
579 if (!constraint->v)
580 return isl_constraint_free(constraint);
582 isl_int_set_si(constraint->v->el[0], v);
583 return constraint;
586 __isl_give isl_constraint *isl_constraint_set_coefficient(
587 __isl_take isl_constraint *constraint,
588 enum isl_dim_type type, int pos, isl_int v)
590 constraint = isl_constraint_cow(constraint);
591 if (!constraint)
592 return NULL;
594 if (pos >= isl_local_space_dim(constraint->ls, type))
595 isl_die(constraint->v->ctx, isl_error_invalid,
596 "position out of bounds",
597 return isl_constraint_free(constraint));
599 constraint = isl_constraint_cow(constraint);
600 if (!constraint)
601 return NULL;
603 constraint->v = isl_vec_cow(constraint->v);
604 if (!constraint->v)
605 return isl_constraint_free(constraint);
607 pos += isl_local_space_offset(constraint->ls, type);
608 isl_int_set(constraint->v->el[pos], v);
610 return constraint;
613 /* Replace the coefficient of the variable of type "type" at position "pos"
614 * of "constraint" by "v".
616 __isl_give isl_constraint *isl_constraint_set_coefficient_val(
617 __isl_take isl_constraint *constraint,
618 enum isl_dim_type type, int pos, __isl_take isl_val *v)
620 constraint = isl_constraint_cow(constraint);
621 if (!constraint || !v)
622 goto error;
623 if (!isl_val_is_int(v))
624 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
625 "expecting integer value", goto error);
627 if (pos >= isl_local_space_dim(constraint->ls, type))
628 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
629 "position out of bounds", goto error);
631 pos += isl_local_space_offset(constraint->ls, type);
632 constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
633 if (!constraint->v)
634 constraint = isl_constraint_free(constraint);
635 return constraint;
636 error:
637 isl_val_free(v);
638 return isl_constraint_free(constraint);
641 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
642 __isl_take isl_constraint *constraint,
643 enum isl_dim_type type, int pos, int v)
645 constraint = isl_constraint_cow(constraint);
646 if (!constraint)
647 return NULL;
649 if (pos >= isl_local_space_dim(constraint->ls, type))
650 isl_die(constraint->v->ctx, isl_error_invalid,
651 "position out of bounds",
652 return isl_constraint_free(constraint));
654 constraint = isl_constraint_cow(constraint);
655 if (!constraint)
656 return NULL;
658 constraint->v = isl_vec_cow(constraint->v);
659 if (!constraint->v)
660 return isl_constraint_free(constraint);
662 pos += isl_local_space_offset(constraint->ls, type);
663 isl_int_set_si(constraint->v->el[pos], v);
665 return constraint;
668 /* Drop any constraint from "bset" that is identical to "constraint".
669 * In particular, this means that the local spaces of "bset" and
670 * "constraint" need to be the same.
672 * We manually set ISL_BASIC_SET_FINAL instead of calling
673 * isl_basic_set_finalize because this function is called by CLooG,
674 * which does not expect any variables to disappear.
676 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
677 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
679 int i;
680 unsigned n;
681 isl_int **row;
682 unsigned total;
683 isl_local_space *ls1;
684 int equal;
685 int equality;
687 if (!bset || !constraint)
688 goto error;
690 ls1 = isl_basic_set_get_local_space(bset);
691 equal = isl_local_space_is_equal(ls1, constraint->ls);
692 isl_local_space_free(ls1);
693 if (equal < 0)
694 goto error;
695 if (!equal) {
696 isl_constraint_free(constraint);
697 return bset;
700 bset = isl_basic_set_cow(bset);
701 if (!bset)
702 goto error;
704 equality = isl_constraint_is_equality(constraint);
705 if (equality) {
706 n = bset->n_eq;
707 row = bset->eq;
708 } else {
709 n = bset->n_ineq;
710 row = bset->ineq;
713 total = isl_constraint_dim(constraint, isl_dim_all);
714 for (i = 0; i < n; ++i) {
715 if (!isl_seq_eq(row[i], constraint->v->el, 1 + total))
716 continue;
717 if (equality && isl_basic_set_drop_equality(bset, i) < 0)
718 goto error;
719 if (!equality && isl_basic_set_drop_inequality(bset, i) < 0)
720 goto error;
721 break;
724 isl_constraint_free(constraint);
725 ISL_F_SET(bset, ISL_BASIC_SET_FINAL);
726 return bset;
727 error:
728 isl_constraint_free(constraint);
729 isl_basic_set_free(bset);
730 return NULL;
733 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
735 isl_ctx *ctx;
737 constraint = isl_constraint_cow(constraint);
738 if (!constraint)
739 return NULL;
741 ctx = isl_constraint_get_ctx(constraint);
742 if (isl_constraint_is_equality(constraint))
743 isl_die(ctx, isl_error_invalid, "cannot negate equality",
744 return isl_constraint_free(constraint));
745 constraint->v = isl_vec_neg(constraint->v);
746 constraint->v = isl_vec_cow(constraint->v);
747 if (!constraint->v)
748 return isl_constraint_free(constraint);
749 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
750 return constraint;
753 int isl_constraint_is_equality(struct isl_constraint *constraint)
755 if (!constraint)
756 return -1;
757 return constraint->eq;
760 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
762 int i;
763 int n_div;
765 if (!constraint)
766 return -1;
767 if (isl_constraint_is_equality(constraint))
768 return 0;
769 n_div = isl_constraint_dim(constraint, isl_dim_div);
770 for (i = 0; i < n_div; ++i) {
771 if (isl_local_space_is_div_constraint(constraint->ls,
772 constraint->v->el, i))
773 return 1;
776 return 0;
779 /* We manually set ISL_BASIC_SET_FINAL instead of calling
780 * isl_basic_map_finalize because we want to keep the position
781 * of the divs and we therefore do not want to throw away redundant divs.
782 * This is arguably a bit fragile.
784 __isl_give isl_basic_map *isl_basic_map_from_constraint(
785 __isl_take isl_constraint *constraint)
787 int k;
788 isl_local_space *ls;
789 struct isl_basic_map *bmap;
790 isl_int *c;
791 unsigned total;
793 if (!constraint)
794 return NULL;
796 ls = isl_local_space_copy(constraint->ls);
797 bmap = isl_basic_map_from_local_space(ls);
798 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
799 if (isl_constraint_is_equality(constraint)) {
800 k = isl_basic_map_alloc_equality(bmap);
801 if (k < 0)
802 goto error;
803 c = bmap->eq[k];
805 else {
806 k = isl_basic_map_alloc_inequality(bmap);
807 if (k < 0)
808 goto error;
809 c = bmap->ineq[k];
811 total = isl_basic_map_total_dim(bmap);
812 isl_seq_cpy(c, constraint->v->el, 1 + total);
813 isl_constraint_free(constraint);
814 if (bmap)
815 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
816 return bmap;
817 error:
818 isl_constraint_free(constraint);
819 isl_basic_map_free(bmap);
820 return NULL;
823 struct isl_basic_set *isl_basic_set_from_constraint(
824 struct isl_constraint *constraint)
826 if (!constraint)
827 return NULL;
829 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
830 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
831 "not a set constraint", goto error);
832 return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
833 error:
834 isl_constraint_free(constraint);
835 return NULL;
838 /* Is the variable of "type" at position "pos" of "bmap" defined
839 * in terms of earlier dimensions through an equality?
841 * If so, and if c is not NULL, then return a copy of this equality in *c.
843 int isl_basic_map_has_defining_equality(
844 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
845 __isl_give isl_constraint **c)
847 int i;
848 unsigned offset;
849 unsigned total;
851 if (!bmap)
852 return -1;
853 offset = basic_map_offset(bmap, type);
854 total = isl_basic_map_total_dim(bmap);
855 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
856 for (i = 0; i < bmap->n_eq; ++i) {
857 if (isl_int_is_zero(bmap->eq[i][offset + pos]) ||
858 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
859 1+total-offset-pos-1) != -1)
860 continue;
861 if (c)
862 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
863 &bmap->eq[i]);
864 return 1;
866 return 0;
869 /* Is the variable of "type" at position "pos" of "bset" defined
870 * in terms of earlier dimensions through an equality?
872 * If so, and if c is not NULL, then return a copy of this equality in *c.
874 int isl_basic_set_has_defining_equality(
875 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
876 __isl_give isl_constraint **c)
878 return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
879 type, pos, c);
882 int isl_basic_set_has_defining_inequalities(
883 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
884 struct isl_constraint **lower,
885 struct isl_constraint **upper)
887 int i, j;
888 unsigned offset;
889 unsigned total;
890 isl_int m;
891 isl_int **lower_line, **upper_line;
893 if (!bset)
894 return -1;
895 offset = basic_set_offset(bset, type);
896 total = isl_basic_set_total_dim(bset);
897 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
898 isl_int_init(m);
899 for (i = 0; i < bset->n_ineq; ++i) {
900 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
901 continue;
902 if (isl_int_is_one(bset->ineq[i][offset + pos]))
903 continue;
904 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
905 continue;
906 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
907 1+total-offset-pos-1) != -1)
908 continue;
909 for (j = i + 1; j < bset->n_ineq; ++j) {
910 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
911 total))
912 continue;
913 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
914 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
915 continue;
917 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
918 lower_line = &bset->ineq[i];
919 upper_line = &bset->ineq[j];
920 } else {
921 lower_line = &bset->ineq[j];
922 upper_line = &bset->ineq[i];
924 *lower = isl_basic_set_constraint(
925 isl_basic_set_copy(bset), lower_line);
926 *upper = isl_basic_set_constraint(
927 isl_basic_set_copy(bset), upper_line);
928 isl_int_clear(m);
929 return 1;
932 *lower = NULL;
933 *upper = NULL;
934 isl_int_clear(m);
935 return 0;
938 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
939 * (in "a" and "b"), add a constraint to "bset" that ensures that the
940 * bound implied by "a" is (strictly) larger than the bound implied by "b".
942 * If both constraints imply lower bounds, then this means that "a" is
943 * active in the result.
944 * If both constraints imply upper bounds, then this means that "b" is
945 * active in the result.
947 static __isl_give isl_basic_set *add_larger_bound_constraint(
948 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
949 unsigned abs_pos, int strict)
951 int k;
952 isl_int t;
953 unsigned total;
955 k = isl_basic_set_alloc_inequality(bset);
956 if (k < 0)
957 goto error;
959 total = isl_basic_set_dim(bset, isl_dim_all);
961 isl_int_init(t);
962 isl_int_neg(t, b[1 + abs_pos]);
964 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
965 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
966 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
967 total - abs_pos);
969 if (strict)
970 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
972 isl_int_clear(t);
974 return bset;
975 error:
976 isl_basic_set_free(bset);
977 return NULL;
980 /* Add constraints to "context" that ensure that "u" is the smallest
981 * (and therefore active) upper bound on "abs_pos" in "bset" and return
982 * the resulting basic set.
984 static __isl_give isl_basic_set *set_smallest_upper_bound(
985 __isl_keep isl_basic_set *context,
986 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
988 int j;
990 context = isl_basic_set_copy(context);
991 context = isl_basic_set_cow(context);
993 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
995 for (j = 0; j < bset->n_ineq; ++j) {
996 if (j == u)
997 continue;
998 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
999 continue;
1000 context = add_larger_bound_constraint(context,
1001 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
1004 context = isl_basic_set_simplify(context);
1005 context = isl_basic_set_finalize(context);
1007 return context;
1010 /* Add constraints to "context" that ensure that "u" is the largest
1011 * (and therefore active) upper bound on "abs_pos" in "bset" and return
1012 * the resulting basic set.
1014 static __isl_give isl_basic_set *set_largest_lower_bound(
1015 __isl_keep isl_basic_set *context,
1016 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
1018 int j;
1020 context = isl_basic_set_copy(context);
1021 context = isl_basic_set_cow(context);
1023 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
1025 for (j = 0; j < bset->n_ineq; ++j) {
1026 if (j == l)
1027 continue;
1028 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
1029 continue;
1030 context = add_larger_bound_constraint(context,
1031 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
1034 context = isl_basic_set_simplify(context);
1035 context = isl_basic_set_finalize(context);
1037 return context;
1040 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
1041 enum isl_dim_type type, unsigned abs_pos,
1042 __isl_take isl_basic_set *context, int n_upper,
1043 int (*fn)(__isl_take isl_constraint *lower,
1044 __isl_take isl_constraint *upper,
1045 __isl_take isl_basic_set *bset, void *user), void *user)
1047 isl_basic_set *context_i;
1048 isl_constraint *upper = NULL;
1049 int i;
1051 for (i = 0; i < bset->n_ineq; ++i) {
1052 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
1053 continue;
1055 context_i = set_smallest_upper_bound(context, bset,
1056 abs_pos, n_upper, i);
1057 if (isl_basic_set_is_empty(context_i)) {
1058 isl_basic_set_free(context_i);
1059 continue;
1061 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1062 &bset->ineq[i]);
1063 if (!upper || !context_i)
1064 goto error;
1065 if (fn(NULL, upper, context_i, user) < 0)
1066 break;
1069 isl_basic_set_free(context);
1071 if (i < bset->n_ineq)
1072 return -1;
1074 return 0;
1075 error:
1076 isl_constraint_free(upper);
1077 isl_basic_set_free(context_i);
1078 isl_basic_set_free(context);
1079 return -1;
1082 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
1083 enum isl_dim_type type, unsigned abs_pos,
1084 __isl_take isl_basic_set *context, int n_lower,
1085 int (*fn)(__isl_take isl_constraint *lower,
1086 __isl_take isl_constraint *upper,
1087 __isl_take isl_basic_set *bset, void *user), void *user)
1089 isl_basic_set *context_i;
1090 isl_constraint *lower = NULL;
1091 int i;
1093 for (i = 0; i < bset->n_ineq; ++i) {
1094 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
1095 continue;
1097 context_i = set_largest_lower_bound(context, bset,
1098 abs_pos, n_lower, i);
1099 if (isl_basic_set_is_empty(context_i)) {
1100 isl_basic_set_free(context_i);
1101 continue;
1103 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1104 &bset->ineq[i]);
1105 if (!lower || !context_i)
1106 goto error;
1107 if (fn(lower, NULL, context_i, user) < 0)
1108 break;
1111 isl_basic_set_free(context);
1113 if (i < bset->n_ineq)
1114 return -1;
1116 return 0;
1117 error:
1118 isl_constraint_free(lower);
1119 isl_basic_set_free(context_i);
1120 isl_basic_set_free(context);
1121 return -1;
1124 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
1125 enum isl_dim_type type, unsigned abs_pos,
1126 __isl_take isl_basic_set *context, int n_lower, int n_upper,
1127 int (*fn)(__isl_take isl_constraint *lower,
1128 __isl_take isl_constraint *upper,
1129 __isl_take isl_basic_set *bset, void *user), void *user)
1131 isl_basic_set *context_i, *context_j;
1132 isl_constraint *lower = NULL;
1133 isl_constraint *upper = NULL;
1134 int i, j;
1136 for (i = 0; i < bset->n_ineq; ++i) {
1137 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1138 continue;
1140 context_i = set_largest_lower_bound(context, bset,
1141 abs_pos, n_lower, i);
1142 if (isl_basic_set_is_empty(context_i)) {
1143 isl_basic_set_free(context_i);
1144 continue;
1147 for (j = 0; j < bset->n_ineq; ++j) {
1148 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
1149 continue;
1151 context_j = set_smallest_upper_bound(context_i, bset,
1152 abs_pos, n_upper, j);
1153 context_j = isl_basic_set_extend_constraints(context_j,
1154 0, 1);
1155 context_j = add_larger_bound_constraint(context_j,
1156 bset->ineq[i], bset->ineq[j], abs_pos, 0);
1157 context_j = isl_basic_set_simplify(context_j);
1158 context_j = isl_basic_set_finalize(context_j);
1159 if (isl_basic_set_is_empty(context_j)) {
1160 isl_basic_set_free(context_j);
1161 continue;
1163 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1164 &bset->ineq[i]);
1165 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1166 &bset->ineq[j]);
1167 if (!lower || !upper || !context_j)
1168 goto error;
1169 if (fn(lower, upper, context_j, user) < 0)
1170 break;
1173 isl_basic_set_free(context_i);
1175 if (j < bset->n_ineq)
1176 break;
1179 isl_basic_set_free(context);
1181 if (i < bset->n_ineq)
1182 return -1;
1184 return 0;
1185 error:
1186 isl_constraint_free(lower);
1187 isl_constraint_free(upper);
1188 isl_basic_set_free(context_i);
1189 isl_basic_set_free(context_j);
1190 isl_basic_set_free(context);
1191 return -1;
1194 /* For each pair of lower and upper bounds on the variable "pos"
1195 * of type "type", call "fn" with these lower and upper bounds and the
1196 * set of constraints on the remaining variables where these bounds
1197 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1199 * If the designated variable is equal to an affine combination of the
1200 * other variables then fn is called with both lower and upper
1201 * set to the corresponding equality.
1203 * If there is no lower (or upper) bound, then NULL is passed
1204 * as the corresponding bound.
1206 * We first check if the variable is involved in any equality.
1207 * If not, we count the number of lower and upper bounds and
1208 * act accordingly.
1210 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1211 enum isl_dim_type type, unsigned pos,
1212 int (*fn)(__isl_take isl_constraint *lower,
1213 __isl_take isl_constraint *upper,
1214 __isl_take isl_basic_set *bset, void *user), void *user)
1216 int i;
1217 isl_constraint *lower = NULL;
1218 isl_constraint *upper = NULL;
1219 isl_basic_set *context = NULL;
1220 unsigned abs_pos;
1221 int n_lower, n_upper;
1223 if (!bset)
1224 return -1;
1225 isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1226 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1227 return -1);
1229 abs_pos = pos;
1230 if (type == isl_dim_set)
1231 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1233 for (i = 0; i < bset->n_eq; ++i) {
1234 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1235 continue;
1237 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1238 &bset->eq[i]);
1239 upper = isl_constraint_copy(lower);
1240 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1241 type, pos, 1);
1242 if (!lower || !upper || !context)
1243 goto error;
1244 return fn(lower, upper, context, user);
1247 n_lower = 0;
1248 n_upper = 0;
1249 for (i = 0; i < bset->n_ineq; ++i) {
1250 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1251 n_lower++;
1252 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1253 n_upper++;
1256 context = isl_basic_set_copy(bset);
1257 context = isl_basic_set_cow(context);
1258 if (!context)
1259 goto error;
1260 for (i = context->n_ineq - 1; i >= 0; --i)
1261 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1262 isl_basic_set_drop_inequality(context, i);
1264 context = isl_basic_set_drop(context, type, pos, 1);
1265 if (!n_lower && !n_upper)
1266 return fn(NULL, NULL, context, user);
1267 if (!n_lower)
1268 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1269 fn, user);
1270 if (!n_upper)
1271 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1272 fn, user);
1273 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1274 fn, user);
1275 error:
1276 isl_constraint_free(lower);
1277 isl_constraint_free(upper);
1278 isl_basic_set_free(context);
1279 return -1;
1282 __isl_give isl_aff *isl_constraint_get_bound(
1283 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1285 isl_aff *aff;
1286 isl_ctx *ctx;
1288 if (!constraint)
1289 return NULL;
1290 ctx = isl_constraint_get_ctx(constraint);
1291 if (pos >= isl_constraint_dim(constraint, type))
1292 isl_die(ctx, isl_error_invalid,
1293 "index out of bounds", return NULL);
1294 if (isl_constraint_dim(constraint, isl_dim_in) != 0)
1295 isl_die(ctx, isl_error_invalid,
1296 "not a set constraint", return NULL);
1298 pos += offset(constraint, type);
1299 if (isl_int_is_zero(constraint->v->el[pos]))
1300 isl_die(ctx, isl_error_invalid,
1301 "constraint does not define a bound on given dimension",
1302 return NULL);
1304 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1305 if (!aff)
1306 return NULL;
1308 if (isl_int_is_neg(constraint->v->el[pos]))
1309 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1310 else
1311 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1312 isl_int_set_si(aff->v->el[1 + pos], 0);
1313 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1315 return aff;
1318 /* For an inequality constraint
1320 * f >= 0
1322 * or an equality constraint
1324 * f = 0
1326 * return the affine expression f.
1328 __isl_give isl_aff *isl_constraint_get_aff(
1329 __isl_keep isl_constraint *constraint)
1331 isl_aff *aff;
1333 if (!constraint)
1334 return NULL;
1336 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1337 if (!aff)
1338 return NULL;
1340 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1341 isl_int_set_si(aff->v->el[0], 1);
1343 return aff;
1346 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1347 * In particular, construct aff >= 0 or aff = 0.
1349 * The denominator of "aff" can be ignored.
1351 static __isl_give isl_constraint *isl_constraint_alloc_aff(int eq,
1352 __isl_take isl_aff *aff)
1354 isl_local_space *ls;
1355 isl_vec *v;
1357 if (!aff)
1358 return NULL;
1359 ls = isl_aff_get_domain_local_space(aff);
1360 v = isl_vec_drop_els(isl_vec_copy(aff->v), 0, 1);
1361 isl_aff_free(aff);
1363 return isl_constraint_alloc_vec(eq, ls, v);
1366 /* Construct an equality constraint equating the given affine expression
1367 * to zero.
1369 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1371 return isl_constraint_alloc_aff(1, aff);
1374 /* Construct an inequality constraint enforcing the given affine expression
1375 * to be non-negative.
1377 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1379 return isl_constraint_alloc_aff(0, aff);
1382 /* Compare two isl_constraints.
1384 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1385 * than "c2" and 0 if they are equal.
1387 * The order is fairly arbitrary. We do consider constraints that only involve
1388 * earlier dimensions as "smaller".
1390 int isl_constraint_plain_cmp(__isl_keep isl_constraint *c1,
1391 __isl_keep isl_constraint *c2)
1393 int cmp;
1394 int last1, last2;
1396 if (c1 == c2)
1397 return 0;
1398 if (!c1)
1399 return -1;
1400 if (!c2)
1401 return 1;
1402 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1403 if (cmp != 0)
1404 return cmp;
1406 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1407 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1408 if (last1 != last2)
1409 return last1 - last2;
1411 return isl_seq_cmp(c1->v->el, c2->v->el, c1->v->size);
1414 /* Compare two constraints based on their final (non-zero) coefficients.
1415 * In particular, the constraint that involves later variables or
1416 * that has a larger coefficient for a shared latest variable
1417 * is considered "greater" than the other constraint.
1419 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1420 * than "c2" and 0 if they are equal.
1422 * If the constraints live in different local spaces, then we cannot
1423 * really compare the constraints so we compare the local spaces instead.
1425 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint *c1,
1426 __isl_keep isl_constraint *c2)
1428 int cmp;
1429 int last1, last2;
1431 if (c1 == c2)
1432 return 0;
1433 if (!c1)
1434 return -1;
1435 if (!c2)
1436 return 1;
1437 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1438 if (cmp != 0)
1439 return cmp;
1441 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1442 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1443 if (last1 != last2)
1444 return last1 - last2;
1445 if (last1 == -1)
1446 return 0;
1447 return isl_int_abs_cmp(c1->v->el[1 + last1], c2->v->el[1 + last2]);