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>
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>
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
;
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
;
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
;
71 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
82 isl_local_space_free(ls
);
87 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
88 __isl_take isl_local_space
*ls
)
96 ctx
= isl_local_space_get_ctx(ls
);
97 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
99 return isl_constraint_alloc_vec(eq
, ls
, v
);
102 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
108 isl_local_space
*ls
= NULL
;
109 isl_constraint
*constraint
;
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
));
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
);
127 isl_local_space_free(ls
);
128 isl_basic_map_free(bmap
);
132 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
135 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
138 __isl_give isl_constraint
*isl_constraint_alloc_equality(
139 __isl_take isl_local_space
*ls
)
141 return isl_constraint_alloc(1, ls
);
144 __isl_give isl_constraint
*isl_constraint_alloc_inequality(
145 __isl_take isl_local_space
*ls
)
147 return isl_constraint_alloc(0, ls
);
150 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
155 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
159 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
167 return isl_constraint_dup(c
);
170 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
179 __isl_null isl_constraint
*isl_constraint_free(__isl_take isl_constraint
*c
)
187 isl_local_space_free(c
->ls
);
194 /* Return the number of constraints in "bmap", i.e., the
195 * number of times isl_basic_map_foreach_constraint will
198 int isl_basic_map_n_constraint(__isl_keep isl_basic_map
*bmap
)
203 return bmap
->n_eq
+ bmap
->n_ineq
;
206 /* Return the number of constraints in "bset", i.e., the
207 * number of times isl_basic_set_foreach_constraint will
210 int isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
212 return isl_basic_map_n_constraint(bset
);
215 isl_stat
isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
216 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
219 struct isl_constraint
*c
;
222 return isl_stat_error
;
224 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
225 return isl_stat_error
);
227 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
228 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
231 return isl_stat_error
;
233 return isl_stat_error
;
236 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
237 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
240 return isl_stat_error
;
242 return isl_stat_error
;
248 isl_stat
isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
249 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
251 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
254 /* Add the constraint to the list that "user" points to, if it is not
257 static isl_stat
collect_constraint(__isl_take isl_constraint
*constraint
,
260 isl_constraint_list
**list
= user
;
262 if (isl_constraint_is_div_constraint(constraint
))
263 isl_constraint_free(constraint
);
265 *list
= isl_constraint_list_add(*list
, constraint
);
270 /* Return a list of constraints that, when combined, are equivalent
271 * to "bmap". The input is required to have only known divs.
273 * There is no need to include the div constraints as they are
274 * implied by the div expressions.
276 __isl_give isl_constraint_list
*isl_basic_map_get_constraint_list(
277 __isl_keep isl_basic_map
*bmap
)
282 isl_constraint_list
*list
;
284 known
= isl_basic_map_divs_known(bmap
);
287 ctx
= isl_basic_map_get_ctx(bmap
);
289 isl_die(ctx
, isl_error_invalid
,
290 "input involves unknown divs", return NULL
);
292 n
= isl_basic_map_n_constraint(bmap
);
293 list
= isl_constraint_list_alloc(ctx
, n
);
294 if (isl_basic_map_foreach_constraint(bmap
,
295 &collect_constraint
, &list
) < 0)
296 list
= isl_constraint_list_free(list
);
301 /* Return a list of constraints that, when combined, are equivalent
302 * to "bset". The input is required to have only known divs.
304 __isl_give isl_constraint_list
*isl_basic_set_get_constraint_list(
305 __isl_keep isl_basic_set
*bset
)
307 return isl_basic_map_get_constraint_list(bset
);
310 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
311 struct isl_constraint
*constraint2
)
315 if (!constraint1
|| !constraint2
)
317 if (constraint1
->eq
!= constraint2
->eq
)
319 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
320 if (equal
< 0 || !equal
)
322 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
325 struct isl_basic_map
*isl_basic_map_add_constraint(
326 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
332 if (!bmap
|| !constraint
)
335 ctx
= isl_constraint_get_ctx(constraint
);
336 dim
= isl_constraint_get_space(constraint
);
337 equal_space
= isl_space_is_equal(bmap
->dim
, dim
);
339 isl_assert(ctx
, equal_space
, goto error
);
341 bmap
= isl_basic_map_intersect(bmap
,
342 isl_basic_map_from_constraint(constraint
));
345 isl_basic_map_free(bmap
);
346 isl_constraint_free(constraint
);
350 struct isl_basic_set
*isl_basic_set_add_constraint(
351 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
353 return (struct isl_basic_set
*)
354 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
358 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
359 __isl_take isl_constraint
*constraint
)
363 bmap
= isl_basic_map_from_constraint(constraint
);
364 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
369 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
370 __isl_take isl_constraint
*constraint
)
372 return isl_map_add_constraint(set
, constraint
);
375 __isl_give isl_space
*isl_constraint_get_space(
376 __isl_keep isl_constraint
*constraint
)
378 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
381 __isl_give isl_local_space
*isl_constraint_get_local_space(
382 __isl_keep isl_constraint
*constraint
)
384 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
387 int isl_constraint_dim(struct isl_constraint
*constraint
,
388 enum isl_dim_type type
)
392 return n(constraint
, type
);
395 isl_bool
isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
396 enum isl_dim_type type
, unsigned first
, unsigned n
)
401 isl_bool involves
= isl_bool_false
;
404 return isl_bool_error
;
406 return isl_bool_false
;
408 ctx
= isl_constraint_get_ctx(constraint
);
409 if (first
+ n
> isl_constraint_dim(constraint
, type
))
410 isl_die(ctx
, isl_error_invalid
,
411 "range out of bounds", return isl_bool_error
);
413 active
= isl_local_space_get_active(constraint
->ls
,
414 constraint
->v
->el
+ 1);
418 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
419 for (i
= 0; i
< n
; ++i
)
420 if (active
[first
+ i
]) {
421 involves
= isl_bool_true
;
430 return isl_bool_error
;
433 /* Does the given constraint represent a lower bound on the given
436 isl_bool
isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
437 enum isl_dim_type type
, unsigned pos
)
440 return isl_bool_error
;
442 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
443 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
444 "position out of bounds", return isl_bool_error
);
446 pos
+= isl_local_space_offset(constraint
->ls
, type
);
447 return isl_int_is_pos(constraint
->v
->el
[pos
]);
450 /* Does the given constraint represent an upper bound on the given
453 isl_bool
isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
454 enum isl_dim_type type
, unsigned pos
)
457 return isl_bool_error
;
459 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
460 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
461 "position out of bounds", return isl_bool_error
);
463 pos
+= isl_local_space_offset(constraint
->ls
, type
);
464 return isl_int_is_neg(constraint
->v
->el
[pos
]);
467 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
468 enum isl_dim_type type
, unsigned pos
)
471 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
474 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
478 isl_int_set(*v
, constraint
->v
->el
[0]);
481 /* Return the constant term of "constraint".
483 __isl_give isl_val
*isl_constraint_get_constant_val(
484 __isl_keep isl_constraint
*constraint
)
491 ctx
= isl_constraint_get_ctx(constraint
);
492 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
495 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
496 enum isl_dim_type type
, int pos
, isl_int
*v
)
501 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
502 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
503 "position out of bounds", return);
505 pos
+= isl_local_space_offset(constraint
->ls
, type
);
506 isl_int_set(*v
, constraint
->v
->el
[pos
]);
509 /* Return the coefficient of the variable of type "type" at position "pos"
512 __isl_give isl_val
*isl_constraint_get_coefficient_val(
513 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
520 ctx
= isl_constraint_get_ctx(constraint
);
521 if (pos
< 0 || pos
>= isl_local_space_dim(constraint
->ls
, type
))
522 isl_die(ctx
, isl_error_invalid
,
523 "position out of bounds", return NULL
);
525 pos
+= isl_local_space_offset(constraint
->ls
, type
);
526 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
529 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
535 return isl_local_space_get_div(constraint
->ls
, pos
);
538 __isl_give isl_constraint
*isl_constraint_set_constant(
539 __isl_take isl_constraint
*constraint
, isl_int v
)
541 constraint
= isl_constraint_cow(constraint
);
545 constraint
->v
= isl_vec_cow(constraint
->v
);
547 return isl_constraint_free(constraint
);
549 isl_int_set(constraint
->v
->el
[0], v
);
553 /* Replace the constant term of "constraint" by "v".
555 __isl_give isl_constraint
*isl_constraint_set_constant_val(
556 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
558 constraint
= isl_constraint_cow(constraint
);
559 if (!constraint
|| !v
)
561 if (!isl_val_is_int(v
))
562 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
563 "expecting integer value", goto error
);
564 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
566 constraint
= isl_constraint_free(constraint
);
570 return isl_constraint_free(constraint
);
573 __isl_give isl_constraint
*isl_constraint_set_constant_si(
574 __isl_take isl_constraint
*constraint
, int v
)
576 constraint
= isl_constraint_cow(constraint
);
580 constraint
->v
= isl_vec_cow(constraint
->v
);
582 return isl_constraint_free(constraint
);
584 isl_int_set_si(constraint
->v
->el
[0], v
);
588 __isl_give isl_constraint
*isl_constraint_set_coefficient(
589 __isl_take isl_constraint
*constraint
,
590 enum isl_dim_type type
, int pos
, isl_int v
)
592 constraint
= isl_constraint_cow(constraint
);
596 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
597 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
598 "position out of bounds",
599 return isl_constraint_free(constraint
));
601 constraint
= isl_constraint_cow(constraint
);
605 constraint
->v
= isl_vec_cow(constraint
->v
);
607 return isl_constraint_free(constraint
);
609 pos
+= isl_local_space_offset(constraint
->ls
, type
);
610 isl_int_set(constraint
->v
->el
[pos
], v
);
615 /* Replace the coefficient of the variable of type "type" at position "pos"
616 * of "constraint" by "v".
618 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
619 __isl_take isl_constraint
*constraint
,
620 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
622 constraint
= isl_constraint_cow(constraint
);
623 if (!constraint
|| !v
)
625 if (!isl_val_is_int(v
))
626 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
627 "expecting integer value", goto error
);
629 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
630 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
631 "position out of bounds", goto error
);
633 pos
+= isl_local_space_offset(constraint
->ls
, type
);
634 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
636 constraint
= isl_constraint_free(constraint
);
640 return isl_constraint_free(constraint
);
643 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
644 __isl_take isl_constraint
*constraint
,
645 enum isl_dim_type type
, int pos
, int v
)
647 constraint
= isl_constraint_cow(constraint
);
651 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
652 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
653 "position out of bounds",
654 return isl_constraint_free(constraint
));
656 constraint
= isl_constraint_cow(constraint
);
660 constraint
->v
= isl_vec_cow(constraint
->v
);
662 return isl_constraint_free(constraint
);
664 pos
+= isl_local_space_offset(constraint
->ls
, type
);
665 isl_int_set_si(constraint
->v
->el
[pos
], v
);
670 /* Drop any constraint from "bset" that is identical to "constraint".
671 * In particular, this means that the local spaces of "bset" and
672 * "constraint" need to be the same.
674 * We manually set ISL_BASIC_SET_FINAL instead of calling
675 * isl_basic_set_finalize because this function is called by CLooG,
676 * which does not expect any variables to disappear.
678 __isl_give isl_basic_set
*isl_basic_set_drop_constraint(
679 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
685 isl_local_space
*ls1
;
689 if (!bset
|| !constraint
)
692 ls1
= isl_basic_set_get_local_space(bset
);
693 equal
= isl_local_space_is_equal(ls1
, constraint
->ls
);
694 isl_local_space_free(ls1
);
698 isl_constraint_free(constraint
);
702 bset
= isl_basic_set_cow(bset
);
706 equality
= isl_constraint_is_equality(constraint
);
715 total
= isl_constraint_dim(constraint
, isl_dim_all
);
716 for (i
= 0; i
< n
; ++i
) {
717 if (!isl_seq_eq(row
[i
], constraint
->v
->el
, 1 + total
))
719 if (equality
&& isl_basic_set_drop_equality(bset
, i
) < 0)
721 if (!equality
&& isl_basic_set_drop_inequality(bset
, i
) < 0)
726 isl_constraint_free(constraint
);
727 ISL_F_SET(bset
, ISL_BASIC_SET_FINAL
);
730 isl_constraint_free(constraint
);
731 isl_basic_set_free(bset
);
735 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
739 constraint
= isl_constraint_cow(constraint
);
743 ctx
= isl_constraint_get_ctx(constraint
);
744 if (isl_constraint_is_equality(constraint
))
745 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
746 return isl_constraint_free(constraint
));
747 constraint
->v
= isl_vec_neg(constraint
->v
);
748 constraint
->v
= isl_vec_cow(constraint
->v
);
750 return isl_constraint_free(constraint
);
751 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
755 isl_bool
isl_constraint_is_equality(struct isl_constraint
*constraint
)
758 return isl_bool_error
;
759 return constraint
->eq
;
762 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
769 if (isl_constraint_is_equality(constraint
))
771 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
772 for (i
= 0; i
< n_div
; ++i
) {
773 if (isl_local_space_is_div_constraint(constraint
->ls
,
774 constraint
->v
->el
, i
))
781 /* We manually set ISL_BASIC_SET_FINAL instead of calling
782 * isl_basic_map_finalize because we want to keep the position
783 * of the divs and we therefore do not want to throw away redundant divs.
784 * This is arguably a bit fragile.
786 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
787 __isl_take isl_constraint
*constraint
)
791 struct isl_basic_map
*bmap
;
798 ls
= isl_local_space_copy(constraint
->ls
);
799 bmap
= isl_basic_map_from_local_space(ls
);
800 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
801 if (isl_constraint_is_equality(constraint
)) {
802 k
= isl_basic_map_alloc_equality(bmap
);
808 k
= isl_basic_map_alloc_inequality(bmap
);
813 total
= isl_basic_map_total_dim(bmap
);
814 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
815 isl_constraint_free(constraint
);
817 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
820 isl_constraint_free(constraint
);
821 isl_basic_map_free(bmap
);
825 struct isl_basic_set
*isl_basic_set_from_constraint(
826 struct isl_constraint
*constraint
)
831 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
832 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
833 "not a set constraint", goto error
);
834 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
836 isl_constraint_free(constraint
);
840 /* Is the variable of "type" at position "pos" of "bmap" defined
841 * in terms of earlier dimensions through an equality?
843 * If so, and if c is not NULL, then return a copy of this equality in *c.
845 int isl_basic_map_has_defining_equality(
846 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
847 __isl_give isl_constraint
**c
)
855 offset
= basic_map_offset(bmap
, type
);
856 total
= isl_basic_map_total_dim(bmap
);
857 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
858 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
859 if (isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) ||
860 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
861 1+total
-offset
-pos
-1) != -1)
864 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
871 /* Is the variable of "type" at position "pos" of "bset" defined
872 * in terms of earlier dimensions through an equality?
874 * If so, and if c is not NULL, then return a copy of this equality in *c.
876 int isl_basic_set_has_defining_equality(
877 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
878 __isl_give isl_constraint
**c
)
880 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
884 int isl_basic_set_has_defining_inequalities(
885 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
886 struct isl_constraint
**lower
,
887 struct isl_constraint
**upper
)
893 isl_int
**lower_line
, **upper_line
;
897 offset
= basic_set_offset(bset
, type
);
898 total
= isl_basic_set_total_dim(bset
);
899 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
901 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
902 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
904 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
906 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
908 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
909 1+total
-offset
-pos
-1) != -1)
911 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
912 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
915 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
916 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
919 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
920 lower_line
= &bset
->ineq
[i
];
921 upper_line
= &bset
->ineq
[j
];
923 lower_line
= &bset
->ineq
[j
];
924 upper_line
= &bset
->ineq
[i
];
926 *lower
= isl_basic_set_constraint(
927 isl_basic_set_copy(bset
), lower_line
);
928 *upper
= isl_basic_set_constraint(
929 isl_basic_set_copy(bset
), upper_line
);
940 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
941 * (in "a" and "b"), add a constraint to "bset" that ensures that the
942 * bound implied by "a" is (strictly) larger than the bound implied by "b".
944 * If both constraints imply lower bounds, then this means that "a" is
945 * active in the result.
946 * If both constraints imply upper bounds, then this means that "b" is
947 * active in the result.
949 static __isl_give isl_basic_set
*add_larger_bound_constraint(
950 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
951 unsigned abs_pos
, int strict
)
957 k
= isl_basic_set_alloc_inequality(bset
);
961 total
= isl_basic_set_dim(bset
, isl_dim_all
);
964 isl_int_neg(t
, b
[1 + abs_pos
]);
966 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
967 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
968 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
972 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
978 isl_basic_set_free(bset
);
982 /* Add constraints to "context" that ensure that "u" is the smallest
983 * (and therefore active) upper bound on "abs_pos" in "bset" and return
984 * the resulting basic set.
986 static __isl_give isl_basic_set
*set_smallest_upper_bound(
987 __isl_keep isl_basic_set
*context
,
988 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
992 context
= isl_basic_set_copy(context
);
993 context
= isl_basic_set_cow(context
);
995 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
997 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1000 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1002 context
= add_larger_bound_constraint(context
,
1003 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
1006 context
= isl_basic_set_simplify(context
);
1007 context
= isl_basic_set_finalize(context
);
1012 /* Add constraints to "context" that ensure that "u" is the largest
1013 * (and therefore active) upper bound on "abs_pos" in "bset" and return
1014 * the resulting basic set.
1016 static __isl_give isl_basic_set
*set_largest_lower_bound(
1017 __isl_keep isl_basic_set
*context
,
1018 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
1022 context
= isl_basic_set_copy(context
);
1023 context
= isl_basic_set_cow(context
);
1025 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
1027 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1030 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
1032 context
= add_larger_bound_constraint(context
,
1033 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
1036 context
= isl_basic_set_simplify(context
);
1037 context
= isl_basic_set_finalize(context
);
1042 static isl_stat
foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
1043 enum isl_dim_type type
, unsigned abs_pos
,
1044 __isl_take isl_basic_set
*context
, 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
;
1050 isl_constraint
*upper
= NULL
;
1053 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1054 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1057 context_i
= set_smallest_upper_bound(context
, bset
,
1058 abs_pos
, n_upper
, i
);
1059 if (isl_basic_set_is_empty(context_i
)) {
1060 isl_basic_set_free(context_i
);
1063 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1065 if (!upper
|| !context_i
)
1067 if (fn(NULL
, upper
, context_i
, user
) < 0)
1071 isl_basic_set_free(context
);
1073 if (i
< bset
->n_ineq
)
1074 return isl_stat_error
;
1078 isl_constraint_free(upper
);
1079 isl_basic_set_free(context_i
);
1080 isl_basic_set_free(context
);
1081 return isl_stat_error
;
1084 static isl_stat
foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
1085 enum isl_dim_type type
, unsigned abs_pos
,
1086 __isl_take isl_basic_set
*context
, int n_lower
,
1087 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1088 __isl_take isl_constraint
*upper
,
1089 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1091 isl_basic_set
*context_i
;
1092 isl_constraint
*lower
= NULL
;
1095 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1096 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1099 context_i
= set_largest_lower_bound(context
, bset
,
1100 abs_pos
, n_lower
, i
);
1101 if (isl_basic_set_is_empty(context_i
)) {
1102 isl_basic_set_free(context_i
);
1105 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1107 if (!lower
|| !context_i
)
1109 if (fn(lower
, NULL
, context_i
, user
) < 0)
1113 isl_basic_set_free(context
);
1115 if (i
< bset
->n_ineq
)
1116 return isl_stat_error
;
1120 isl_constraint_free(lower
);
1121 isl_basic_set_free(context_i
);
1122 isl_basic_set_free(context
);
1123 return isl_stat_error
;
1126 static isl_stat
foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1127 enum isl_dim_type type
, unsigned abs_pos
,
1128 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1129 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1130 __isl_take isl_constraint
*upper
,
1131 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1133 isl_basic_set
*context_i
, *context_j
;
1134 isl_constraint
*lower
= NULL
;
1135 isl_constraint
*upper
= NULL
;
1138 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1139 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1142 context_i
= set_largest_lower_bound(context
, bset
,
1143 abs_pos
, n_lower
, i
);
1144 if (isl_basic_set_is_empty(context_i
)) {
1145 isl_basic_set_free(context_i
);
1149 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1150 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1153 context_j
= set_smallest_upper_bound(context_i
, bset
,
1154 abs_pos
, n_upper
, j
);
1155 context_j
= isl_basic_set_extend_constraints(context_j
,
1157 context_j
= add_larger_bound_constraint(context_j
,
1158 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1159 context_j
= isl_basic_set_simplify(context_j
);
1160 context_j
= isl_basic_set_finalize(context_j
);
1161 if (isl_basic_set_is_empty(context_j
)) {
1162 isl_basic_set_free(context_j
);
1165 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1167 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1169 if (!lower
|| !upper
|| !context_j
)
1171 if (fn(lower
, upper
, context_j
, user
) < 0)
1175 isl_basic_set_free(context_i
);
1177 if (j
< bset
->n_ineq
)
1181 isl_basic_set_free(context
);
1183 if (i
< bset
->n_ineq
)
1184 return isl_stat_error
;
1188 isl_constraint_free(lower
);
1189 isl_constraint_free(upper
);
1190 isl_basic_set_free(context_i
);
1191 isl_basic_set_free(context_j
);
1192 isl_basic_set_free(context
);
1193 return isl_stat_error
;
1196 /* For each pair of lower and upper bounds on the variable "pos"
1197 * of type "type", call "fn" with these lower and upper bounds and the
1198 * set of constraints on the remaining variables where these bounds
1199 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1201 * If the designated variable is equal to an affine combination of the
1202 * other variables then fn is called with both lower and upper
1203 * set to the corresponding equality.
1205 * If there is no lower (or upper) bound, then NULL is passed
1206 * as the corresponding bound.
1208 * We first check if the variable is involved in any equality.
1209 * If not, we count the number of lower and upper bounds and
1212 isl_stat
isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1213 enum isl_dim_type type
, unsigned pos
,
1214 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1215 __isl_take isl_constraint
*upper
,
1216 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1219 isl_constraint
*lower
= NULL
;
1220 isl_constraint
*upper
= NULL
;
1221 isl_basic_set
*context
= NULL
;
1223 int n_lower
, n_upper
;
1226 return isl_stat_error
;
1227 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
),
1228 return isl_stat_error
);
1229 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1230 return isl_stat_error
);
1233 if (type
== isl_dim_set
)
1234 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1236 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1237 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1240 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1242 upper
= isl_constraint_copy(lower
);
1243 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1245 if (!lower
|| !upper
|| !context
)
1247 return fn(lower
, upper
, context
, user
);
1252 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1253 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1255 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1259 context
= isl_basic_set_copy(bset
);
1260 context
= isl_basic_set_cow(context
);
1263 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1264 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1265 isl_basic_set_drop_inequality(context
, i
);
1267 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1268 if (!n_lower
&& !n_upper
)
1269 return fn(NULL
, NULL
, context
, user
);
1271 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1274 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1276 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1279 isl_constraint_free(lower
);
1280 isl_constraint_free(upper
);
1281 isl_basic_set_free(context
);
1285 __isl_give isl_aff
*isl_constraint_get_bound(
1286 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1293 ctx
= isl_constraint_get_ctx(constraint
);
1294 if (pos
>= isl_constraint_dim(constraint
, type
))
1295 isl_die(ctx
, isl_error_invalid
,
1296 "index out of bounds", return NULL
);
1297 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1298 isl_die(ctx
, isl_error_invalid
,
1299 "not a set constraint", return NULL
);
1301 pos
+= offset(constraint
, type
);
1302 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1303 isl_die(ctx
, isl_error_invalid
,
1304 "constraint does not define a bound on given dimension",
1307 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1311 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1312 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1314 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1315 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1316 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1321 /* For an inequality constraint
1325 * or an equality constraint
1329 * return the affine expression f.
1331 __isl_give isl_aff
*isl_constraint_get_aff(
1332 __isl_keep isl_constraint
*constraint
)
1339 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1343 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1344 isl_int_set_si(aff
->v
->el
[0], 1);
1349 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1350 * In particular, construct aff >= 0 or aff = 0.
1352 * The denominator of "aff" can be ignored.
1354 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1355 __isl_take isl_aff
*aff
)
1357 isl_local_space
*ls
;
1362 ls
= isl_aff_get_domain_local_space(aff
);
1363 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1366 return isl_constraint_alloc_vec(eq
, ls
, v
);
1369 /* Construct an equality constraint equating the given affine expression
1372 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1374 return isl_constraint_alloc_aff(1, aff
);
1377 /* Construct an inequality constraint enforcing the given affine expression
1378 * to be non-negative.
1380 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1382 return isl_constraint_alloc_aff(0, aff
);
1385 /* Compare two isl_constraints.
1387 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1388 * than "c2" and 0 if they are equal.
1390 * The order is fairly arbitrary. We do consider constraints that only involve
1391 * earlier dimensions as "smaller".
1393 int isl_constraint_plain_cmp(__isl_keep isl_constraint
*c1
,
1394 __isl_keep isl_constraint
*c2
)
1405 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1409 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1410 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1412 return last1
- last2
;
1414 return isl_seq_cmp(c1
->v
->el
, c2
->v
->el
, c1
->v
->size
);
1417 /* Compare two constraints based on their final (non-zero) coefficients.
1418 * In particular, the constraint that involves later variables or
1419 * that has a larger coefficient for a shared latest variable
1420 * is considered "greater" than the other constraint.
1422 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1423 * than "c2" and 0 if they are equal.
1425 * If the constraints live in different local spaces, then we cannot
1426 * really compare the constraints so we compare the local spaces instead.
1428 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint
*c1
,
1429 __isl_keep isl_constraint
*c2
)
1440 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1444 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1445 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1447 return last1
- last2
;
1450 return isl_int_abs_cmp(c1
->v
->el
[1 + last1
], c2
->v
->el
[1 + last2
]);