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>
23 #include <bset_to_bmap.c>
24 #include <bset_from_bmap.c>
27 #define BASE constraint
29 #include <isl_list_templ.c>
31 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
33 return c
? isl_local_space_get_ctx(c
->ls
) : NULL
;
36 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
38 return isl_local_space_dim(c
->ls
, type
);
41 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
43 return isl_local_space_offset(c
->ls
, type
);
46 static unsigned basic_map_offset(__isl_keep isl_basic_map
*bmap
,
47 enum isl_dim_type type
)
49 return type
== isl_dim_div
? 1 + isl_space_dim(bmap
->dim
, isl_dim_all
)
50 : 1 + isl_space_offset(bmap
->dim
, type
);
53 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
54 enum isl_dim_type type
)
56 isl_space
*dim
= bset
->dim
;
58 case isl_dim_param
: return 1;
59 case isl_dim_in
: return 1 + dim
->nparam
;
60 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
61 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
66 __isl_give isl_constraint
*isl_constraint_alloc_vec(int eq
,
67 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
69 isl_constraint
*constraint
;
74 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
85 isl_local_space_free(ls
);
90 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
91 __isl_take isl_local_space
*ls
)
99 ctx
= isl_local_space_get_ctx(ls
);
100 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
102 return isl_constraint_alloc_vec(eq
, ls
, v
);
105 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
111 isl_local_space
*ls
= NULL
;
112 isl_constraint
*constraint
;
117 eq
= line
>= bmap
->eq
;
119 ctx
= isl_basic_map_get_ctx(bmap
);
120 ls
= isl_basic_map_get_local_space(bmap
);
121 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
124 isl_seq_cpy(v
->el
, line
[0], v
->size
);
125 constraint
= isl_constraint_alloc_vec(eq
, ls
, v
);
127 isl_basic_map_free(bmap
);
130 isl_local_space_free(ls
);
131 isl_basic_map_free(bmap
);
135 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
138 return isl_basic_map_constraint(bset_to_bmap(bset
), line
);
141 __isl_give isl_constraint
*isl_constraint_alloc_equality(
142 __isl_take isl_local_space
*ls
)
144 return isl_constraint_alloc(1, ls
);
147 __isl_give isl_constraint
*isl_constraint_alloc_inequality(
148 __isl_take isl_local_space
*ls
)
150 return isl_constraint_alloc(0, ls
);
153 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
158 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
162 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
170 return isl_constraint_dup(c
);
173 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
182 __isl_null isl_constraint
*isl_constraint_free(__isl_take isl_constraint
*c
)
190 isl_local_space_free(c
->ls
);
197 /* Return the number of constraints in "bmap", i.e., the
198 * number of times isl_basic_map_foreach_constraint will
201 int isl_basic_map_n_constraint(__isl_keep isl_basic_map
*bmap
)
206 return bmap
->n_eq
+ bmap
->n_ineq
;
209 /* Return the number of constraints in "bset", i.e., the
210 * number of times isl_basic_set_foreach_constraint will
213 int isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
215 return isl_basic_map_n_constraint(bset
);
218 isl_stat
isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
219 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
222 struct isl_constraint
*c
;
225 return isl_stat_error
;
227 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
228 return isl_stat_error
);
230 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
231 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
234 return isl_stat_error
;
236 return isl_stat_error
;
239 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
240 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
243 return isl_stat_error
;
245 return isl_stat_error
;
251 isl_stat
isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
252 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
254 return isl_basic_map_foreach_constraint(bset_to_bmap(bset
), fn
, user
);
257 /* Add the constraint to the list that "user" points to, if it is not
260 static isl_stat
collect_constraint(__isl_take isl_constraint
*constraint
,
263 isl_constraint_list
**list
= user
;
265 if (isl_constraint_is_div_constraint(constraint
))
266 isl_constraint_free(constraint
);
268 *list
= isl_constraint_list_add(*list
, constraint
);
273 /* Return a list of constraints that, when combined, are equivalent
274 * to "bmap". The input is required to have only known divs.
276 * There is no need to include the div constraints as they are
277 * implied by the div expressions.
279 __isl_give isl_constraint_list
*isl_basic_map_get_constraint_list(
280 __isl_keep isl_basic_map
*bmap
)
285 isl_constraint_list
*list
;
287 known
= isl_basic_map_divs_known(bmap
);
290 ctx
= isl_basic_map_get_ctx(bmap
);
292 isl_die(ctx
, isl_error_invalid
,
293 "input involves unknown divs", return NULL
);
295 n
= isl_basic_map_n_constraint(bmap
);
296 list
= isl_constraint_list_alloc(ctx
, n
);
297 if (isl_basic_map_foreach_constraint(bmap
,
298 &collect_constraint
, &list
) < 0)
299 list
= isl_constraint_list_free(list
);
304 /* Return a list of constraints that, when combined, are equivalent
305 * to "bset". The input is required to have only known divs.
307 __isl_give isl_constraint_list
*isl_basic_set_get_constraint_list(
308 __isl_keep isl_basic_set
*bset
)
310 return isl_basic_map_get_constraint_list(bset
);
313 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
314 struct isl_constraint
*constraint2
)
318 if (!constraint1
|| !constraint2
)
320 if (constraint1
->eq
!= constraint2
->eq
)
322 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
323 if (equal
< 0 || !equal
)
325 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
328 struct isl_basic_map
*isl_basic_map_add_constraint(
329 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
335 if (!bmap
|| !constraint
)
338 ctx
= isl_constraint_get_ctx(constraint
);
339 dim
= isl_constraint_get_space(constraint
);
340 equal_space
= isl_space_is_equal(bmap
->dim
, dim
);
342 isl_assert(ctx
, equal_space
, goto error
);
344 bmap
= isl_basic_map_intersect(bmap
,
345 isl_basic_map_from_constraint(constraint
));
348 isl_basic_map_free(bmap
);
349 isl_constraint_free(constraint
);
353 struct isl_basic_set
*isl_basic_set_add_constraint(
354 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
356 return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset
),
360 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
361 __isl_take isl_constraint
*constraint
)
365 bmap
= isl_basic_map_from_constraint(constraint
);
366 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
371 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
372 __isl_take isl_constraint
*constraint
)
374 return isl_map_add_constraint(set
, constraint
);
377 __isl_give isl_space
*isl_constraint_get_space(
378 __isl_keep isl_constraint
*constraint
)
380 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
383 __isl_give isl_local_space
*isl_constraint_get_local_space(
384 __isl_keep isl_constraint
*constraint
)
386 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
389 int isl_constraint_dim(struct isl_constraint
*constraint
,
390 enum isl_dim_type type
)
394 return n(constraint
, type
);
397 isl_bool
isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
398 enum isl_dim_type type
, unsigned first
, unsigned n
)
403 isl_bool involves
= isl_bool_false
;
406 return isl_bool_error
;
408 return isl_bool_false
;
410 ctx
= isl_constraint_get_ctx(constraint
);
411 if (first
+ n
> isl_constraint_dim(constraint
, type
))
412 isl_die(ctx
, isl_error_invalid
,
413 "range out of bounds", return isl_bool_error
);
415 active
= isl_local_space_get_active(constraint
->ls
,
416 constraint
->v
->el
+ 1);
420 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
421 for (i
= 0; i
< n
; ++i
)
422 if (active
[first
+ i
]) {
423 involves
= isl_bool_true
;
432 return isl_bool_error
;
435 /* Does the given constraint represent a lower bound on the given
438 isl_bool
isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
439 enum isl_dim_type type
, unsigned pos
)
442 return isl_bool_error
;
444 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
445 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
446 "position out of bounds", return isl_bool_error
);
448 pos
+= isl_local_space_offset(constraint
->ls
, type
);
449 return isl_int_is_pos(constraint
->v
->el
[pos
]);
452 /* Does the given constraint represent an upper bound on the given
455 isl_bool
isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
456 enum isl_dim_type type
, unsigned pos
)
459 return isl_bool_error
;
461 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
462 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
463 "position out of bounds", return isl_bool_error
);
465 pos
+= isl_local_space_offset(constraint
->ls
, type
);
466 return isl_int_is_neg(constraint
->v
->el
[pos
]);
469 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
470 enum isl_dim_type type
, unsigned pos
)
473 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
476 void isl_constraint_get_constant(__isl_keep isl_constraint
*constraint
,
481 isl_int_set(*v
, constraint
->v
->el
[0]);
484 /* Return the constant term of "constraint".
486 __isl_give isl_val
*isl_constraint_get_constant_val(
487 __isl_keep isl_constraint
*constraint
)
494 ctx
= isl_constraint_get_ctx(constraint
);
495 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
498 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
499 enum isl_dim_type type
, int pos
, isl_int
*v
)
504 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
505 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
506 "position out of bounds", return);
508 pos
+= isl_local_space_offset(constraint
->ls
, type
);
509 isl_int_set(*v
, constraint
->v
->el
[pos
]);
512 /* Return the coefficient of the variable of type "type" at position "pos"
515 __isl_give isl_val
*isl_constraint_get_coefficient_val(
516 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
523 ctx
= isl_constraint_get_ctx(constraint
);
524 if (pos
< 0 || pos
>= isl_local_space_dim(constraint
->ls
, type
))
525 isl_die(ctx
, isl_error_invalid
,
526 "position out of bounds", return NULL
);
528 pos
+= isl_local_space_offset(constraint
->ls
, type
);
529 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
532 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
538 return isl_local_space_get_div(constraint
->ls
, pos
);
541 __isl_give isl_constraint
*isl_constraint_set_constant(
542 __isl_take isl_constraint
*constraint
, isl_int v
)
544 constraint
= isl_constraint_cow(constraint
);
548 constraint
->v
= isl_vec_cow(constraint
->v
);
550 return isl_constraint_free(constraint
);
552 isl_int_set(constraint
->v
->el
[0], v
);
556 /* Replace the constant term of "constraint" by "v".
558 __isl_give isl_constraint
*isl_constraint_set_constant_val(
559 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
561 constraint
= isl_constraint_cow(constraint
);
562 if (!constraint
|| !v
)
564 if (!isl_val_is_int(v
))
565 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
566 "expecting integer value", goto error
);
567 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
569 constraint
= isl_constraint_free(constraint
);
573 return isl_constraint_free(constraint
);
576 __isl_give isl_constraint
*isl_constraint_set_constant_si(
577 __isl_take isl_constraint
*constraint
, int v
)
579 constraint
= isl_constraint_cow(constraint
);
583 constraint
->v
= isl_vec_cow(constraint
->v
);
585 return isl_constraint_free(constraint
);
587 isl_int_set_si(constraint
->v
->el
[0], v
);
591 __isl_give isl_constraint
*isl_constraint_set_coefficient(
592 __isl_take isl_constraint
*constraint
,
593 enum isl_dim_type type
, int pos
, isl_int v
)
595 constraint
= isl_constraint_cow(constraint
);
599 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
600 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
601 "position out of bounds",
602 return isl_constraint_free(constraint
));
604 constraint
= isl_constraint_cow(constraint
);
608 constraint
->v
= isl_vec_cow(constraint
->v
);
610 return isl_constraint_free(constraint
);
612 pos
+= isl_local_space_offset(constraint
->ls
, type
);
613 isl_int_set(constraint
->v
->el
[pos
], v
);
618 /* Replace the coefficient of the variable of type "type" at position "pos"
619 * of "constraint" by "v".
621 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
622 __isl_take isl_constraint
*constraint
,
623 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
625 constraint
= isl_constraint_cow(constraint
);
626 if (!constraint
|| !v
)
628 if (!isl_val_is_int(v
))
629 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
630 "expecting integer value", goto error
);
632 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
633 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
634 "position out of bounds", goto error
);
636 pos
+= isl_local_space_offset(constraint
->ls
, type
);
637 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
639 constraint
= isl_constraint_free(constraint
);
643 return isl_constraint_free(constraint
);
646 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
647 __isl_take isl_constraint
*constraint
,
648 enum isl_dim_type type
, int pos
, int v
)
650 constraint
= isl_constraint_cow(constraint
);
654 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
655 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
656 "position out of bounds",
657 return isl_constraint_free(constraint
));
659 constraint
= isl_constraint_cow(constraint
);
663 constraint
->v
= isl_vec_cow(constraint
->v
);
665 return isl_constraint_free(constraint
);
667 pos
+= isl_local_space_offset(constraint
->ls
, type
);
668 isl_int_set_si(constraint
->v
->el
[pos
], v
);
673 /* Drop any constraint from "bset" that is identical to "constraint".
674 * In particular, this means that the local spaces of "bset" and
675 * "constraint" need to be the same.
677 * We manually set ISL_BASIC_SET_FINAL instead of calling
678 * isl_basic_set_finalize because this function is called by CLooG,
679 * which does not expect any variables to disappear.
681 __isl_give isl_basic_set
*isl_basic_set_drop_constraint(
682 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
688 isl_local_space
*ls1
;
692 if (!bset
|| !constraint
)
695 ls1
= isl_basic_set_get_local_space(bset
);
696 equal
= isl_local_space_is_equal(ls1
, constraint
->ls
);
697 isl_local_space_free(ls1
);
701 isl_constraint_free(constraint
);
705 bset
= isl_basic_set_cow(bset
);
709 equality
= isl_constraint_is_equality(constraint
);
718 total
= isl_constraint_dim(constraint
, isl_dim_all
);
719 for (i
= 0; i
< n
; ++i
) {
720 if (!isl_seq_eq(row
[i
], constraint
->v
->el
, 1 + total
))
722 if (equality
&& isl_basic_set_drop_equality(bset
, i
) < 0)
724 if (!equality
&& isl_basic_set_drop_inequality(bset
, i
) < 0)
729 isl_constraint_free(constraint
);
730 ISL_F_SET(bset
, ISL_BASIC_SET_FINAL
);
733 isl_constraint_free(constraint
);
734 isl_basic_set_free(bset
);
738 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
742 constraint
= isl_constraint_cow(constraint
);
746 ctx
= isl_constraint_get_ctx(constraint
);
747 if (isl_constraint_is_equality(constraint
))
748 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
749 return isl_constraint_free(constraint
));
750 constraint
->v
= isl_vec_neg(constraint
->v
);
751 constraint
->v
= isl_vec_cow(constraint
->v
);
753 return isl_constraint_free(constraint
);
754 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
758 isl_bool
isl_constraint_is_equality(struct isl_constraint
*constraint
)
761 return isl_bool_error
;
762 return constraint
->eq
;
765 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
772 if (isl_constraint_is_equality(constraint
))
774 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
775 for (i
= 0; i
< n_div
; ++i
) {
777 is_div
= isl_local_space_is_div_constraint(constraint
->ls
,
778 constraint
->v
->el
, i
);
779 if (is_div
< 0 || is_div
)
786 /* We manually set ISL_BASIC_SET_FINAL instead of calling
787 * isl_basic_map_finalize because we want to keep the position
788 * of the divs and we therefore do not want to throw away redundant divs.
789 * This is arguably a bit fragile.
791 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
792 __isl_take isl_constraint
*constraint
)
796 struct isl_basic_map
*bmap
;
803 ls
= isl_local_space_copy(constraint
->ls
);
804 bmap
= isl_basic_map_from_local_space(ls
);
805 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
806 if (isl_constraint_is_equality(constraint
)) {
807 k
= isl_basic_map_alloc_equality(bmap
);
813 k
= isl_basic_map_alloc_inequality(bmap
);
818 total
= isl_basic_map_total_dim(bmap
);
819 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
820 isl_constraint_free(constraint
);
822 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
825 isl_constraint_free(constraint
);
826 isl_basic_map_free(bmap
);
830 struct isl_basic_set
*isl_basic_set_from_constraint(
831 struct isl_constraint
*constraint
)
836 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
837 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
838 "not a set constraint", goto error
);
839 return bset_from_bmap(isl_basic_map_from_constraint(constraint
));
841 isl_constraint_free(constraint
);
845 /* Is the variable of "type" at position "pos" of "bmap" defined
846 * in terms of earlier dimensions through an equality?
848 * If so, and if c is not NULL, then return a copy of this equality in *c.
850 isl_bool
isl_basic_map_has_defining_equality(
851 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
852 __isl_give isl_constraint
**c
)
859 return isl_bool_error
;
860 offset
= basic_map_offset(bmap
, type
);
861 total
= isl_basic_map_total_dim(bmap
);
862 if (pos
>= isl_basic_map_dim(bmap
, type
))
863 isl_die(isl_basic_map_get_ctx(bmap
), isl_error_invalid
,
864 "invalid position", return isl_bool_error
);
865 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
866 if (isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) ||
867 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
868 1+total
-offset
-pos
-1) != -1)
871 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
873 return isl_bool_true
;
875 return isl_bool_false
;
878 /* Is the variable of "type" at position "pos" of "bset" defined
879 * in terms of earlier dimensions through an equality?
881 * If so, and if c is not NULL, then return a copy of this equality in *c.
883 isl_bool
isl_basic_set_has_defining_equality(
884 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
885 __isl_give isl_constraint
**c
)
887 return isl_basic_map_has_defining_equality(bset_to_bmap(bset
),
891 isl_bool
isl_basic_set_has_defining_inequalities(
892 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
893 struct isl_constraint
**lower
,
894 struct isl_constraint
**upper
)
900 isl_int
**lower_line
, **upper_line
;
903 return isl_bool_error
;
904 offset
= basic_set_offset(bset
, type
);
905 total
= isl_basic_set_total_dim(bset
);
906 if (pos
>= isl_basic_set_dim(bset
, type
))
907 isl_die(isl_basic_set_get_ctx(bset
), isl_error_invalid
,
908 "invalid position", return isl_bool_error
);
910 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
911 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
913 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
915 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
917 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
918 1+total
-offset
-pos
-1) != -1)
920 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
921 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
924 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
925 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
928 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
929 lower_line
= &bset
->ineq
[i
];
930 upper_line
= &bset
->ineq
[j
];
932 lower_line
= &bset
->ineq
[j
];
933 upper_line
= &bset
->ineq
[i
];
935 *lower
= isl_basic_set_constraint(
936 isl_basic_set_copy(bset
), lower_line
);
937 *upper
= isl_basic_set_constraint(
938 isl_basic_set_copy(bset
), upper_line
);
940 return isl_bool_true
;
946 return isl_bool_false
;
949 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
950 * (in "a" and "b"), add a constraint to "bset" that ensures that the
951 * bound implied by "a" is (strictly) larger than the bound implied by "b".
953 * If both constraints imply lower bounds, then this means that "a" is
954 * active in the result.
955 * If both constraints imply upper bounds, then this means that "b" is
956 * active in the result.
958 static __isl_give isl_basic_set
*add_larger_bound_constraint(
959 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
960 unsigned abs_pos
, int strict
)
966 k
= isl_basic_set_alloc_inequality(bset
);
970 total
= isl_basic_set_dim(bset
, isl_dim_all
);
973 isl_int_neg(t
, b
[1 + abs_pos
]);
975 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
976 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
977 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
981 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
987 isl_basic_set_free(bset
);
991 /* Add constraints to "context" that ensure that "u" is the smallest
992 * (and therefore active) upper bound on "abs_pos" in "bset" and return
993 * the resulting basic set.
995 static __isl_give isl_basic_set
*set_smallest_upper_bound(
996 __isl_keep isl_basic_set
*context
,
997 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
1001 context
= isl_basic_set_copy(context
);
1002 context
= isl_basic_set_cow(context
);
1004 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
1006 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1009 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1011 context
= add_larger_bound_constraint(context
,
1012 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
1015 context
= isl_basic_set_simplify(context
);
1016 context
= isl_basic_set_finalize(context
);
1021 /* Add constraints to "context" that ensure that "u" is the largest
1022 * (and therefore active) upper bound on "abs_pos" in "bset" and return
1023 * the resulting basic set.
1025 static __isl_give isl_basic_set
*set_largest_lower_bound(
1026 __isl_keep isl_basic_set
*context
,
1027 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
1031 context
= isl_basic_set_copy(context
);
1032 context
= isl_basic_set_cow(context
);
1034 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
1036 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1039 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
1041 context
= add_larger_bound_constraint(context
,
1042 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
1045 context
= isl_basic_set_simplify(context
);
1046 context
= isl_basic_set_finalize(context
);
1051 static isl_stat
foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
1052 enum isl_dim_type type
, unsigned abs_pos
,
1053 __isl_take isl_basic_set
*context
, int n_upper
,
1054 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1055 __isl_take isl_constraint
*upper
,
1056 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1058 isl_basic_set
*context_i
;
1059 isl_constraint
*upper
= NULL
;
1062 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1063 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1066 context_i
= set_smallest_upper_bound(context
, bset
,
1067 abs_pos
, n_upper
, i
);
1068 if (isl_basic_set_is_empty(context_i
)) {
1069 isl_basic_set_free(context_i
);
1072 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1074 if (!upper
|| !context_i
)
1076 if (fn(NULL
, upper
, context_i
, user
) < 0)
1080 isl_basic_set_free(context
);
1082 if (i
< bset
->n_ineq
)
1083 return isl_stat_error
;
1087 isl_constraint_free(upper
);
1088 isl_basic_set_free(context_i
);
1089 isl_basic_set_free(context
);
1090 return isl_stat_error
;
1093 static isl_stat
foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
1094 enum isl_dim_type type
, unsigned abs_pos
,
1095 __isl_take isl_basic_set
*context
, int n_lower
,
1096 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1097 __isl_take isl_constraint
*upper
,
1098 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1100 isl_basic_set
*context_i
;
1101 isl_constraint
*lower
= NULL
;
1104 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1105 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1108 context_i
= set_largest_lower_bound(context
, bset
,
1109 abs_pos
, n_lower
, i
);
1110 if (isl_basic_set_is_empty(context_i
)) {
1111 isl_basic_set_free(context_i
);
1114 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1116 if (!lower
|| !context_i
)
1118 if (fn(lower
, NULL
, context_i
, user
) < 0)
1122 isl_basic_set_free(context
);
1124 if (i
< bset
->n_ineq
)
1125 return isl_stat_error
;
1129 isl_constraint_free(lower
);
1130 isl_basic_set_free(context_i
);
1131 isl_basic_set_free(context
);
1132 return isl_stat_error
;
1135 static isl_stat
foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1136 enum isl_dim_type type
, unsigned abs_pos
,
1137 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1138 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1139 __isl_take isl_constraint
*upper
,
1140 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1142 isl_basic_set
*context_i
, *context_j
;
1143 isl_constraint
*lower
= NULL
;
1144 isl_constraint
*upper
= NULL
;
1147 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1148 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1151 context_i
= set_largest_lower_bound(context
, bset
,
1152 abs_pos
, n_lower
, i
);
1153 if (isl_basic_set_is_empty(context_i
)) {
1154 isl_basic_set_free(context_i
);
1158 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1159 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1162 context_j
= set_smallest_upper_bound(context_i
, bset
,
1163 abs_pos
, n_upper
, j
);
1164 context_j
= isl_basic_set_extend_constraints(context_j
,
1166 context_j
= add_larger_bound_constraint(context_j
,
1167 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1168 context_j
= isl_basic_set_simplify(context_j
);
1169 context_j
= isl_basic_set_finalize(context_j
);
1170 if (isl_basic_set_is_empty(context_j
)) {
1171 isl_basic_set_free(context_j
);
1174 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1176 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1178 if (!lower
|| !upper
|| !context_j
)
1180 if (fn(lower
, upper
, context_j
, user
) < 0)
1184 isl_basic_set_free(context_i
);
1186 if (j
< bset
->n_ineq
)
1190 isl_basic_set_free(context
);
1192 if (i
< bset
->n_ineq
)
1193 return isl_stat_error
;
1197 isl_constraint_free(lower
);
1198 isl_constraint_free(upper
);
1199 isl_basic_set_free(context_i
);
1200 isl_basic_set_free(context_j
);
1201 isl_basic_set_free(context
);
1202 return isl_stat_error
;
1205 /* For each pair of lower and upper bounds on the variable "pos"
1206 * of type "type", call "fn" with these lower and upper bounds and the
1207 * set of constraints on the remaining variables where these bounds
1208 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1210 * If the designated variable is equal to an affine combination of the
1211 * other variables then fn is called with both lower and upper
1212 * set to the corresponding equality.
1214 * If there is no lower (or upper) bound, then NULL is passed
1215 * as the corresponding bound.
1217 * We first check if the variable is involved in any equality.
1218 * If not, we count the number of lower and upper bounds and
1221 isl_stat
isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1222 enum isl_dim_type type
, unsigned pos
,
1223 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1224 __isl_take isl_constraint
*upper
,
1225 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1228 isl_constraint
*lower
= NULL
;
1229 isl_constraint
*upper
= NULL
;
1230 isl_basic_set
*context
= NULL
;
1232 int n_lower
, n_upper
;
1235 return isl_stat_error
;
1236 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
),
1237 return isl_stat_error
);
1238 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1239 return isl_stat_error
);
1242 if (type
== isl_dim_set
)
1243 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1245 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1246 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1249 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1251 upper
= isl_constraint_copy(lower
);
1252 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1254 if (!lower
|| !upper
|| !context
)
1256 return fn(lower
, upper
, context
, user
);
1261 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1262 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1264 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1268 context
= isl_basic_set_copy(bset
);
1269 context
= isl_basic_set_cow(context
);
1272 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1273 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1274 isl_basic_set_drop_inequality(context
, i
);
1276 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1277 if (!n_lower
&& !n_upper
)
1278 return fn(NULL
, NULL
, context
, user
);
1280 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1283 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1285 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1288 isl_constraint_free(lower
);
1289 isl_constraint_free(upper
);
1290 isl_basic_set_free(context
);
1294 __isl_give isl_aff
*isl_constraint_get_bound(
1295 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1302 ctx
= isl_constraint_get_ctx(constraint
);
1303 if (pos
>= isl_constraint_dim(constraint
, type
))
1304 isl_die(ctx
, isl_error_invalid
,
1305 "index out of bounds", return NULL
);
1306 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1307 isl_die(ctx
, isl_error_invalid
,
1308 "not a set constraint", return NULL
);
1310 pos
+= offset(constraint
, type
);
1311 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1312 isl_die(ctx
, isl_error_invalid
,
1313 "constraint does not define a bound on given dimension",
1316 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1320 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1321 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1323 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1324 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1325 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1330 /* For an inequality constraint
1334 * or an equality constraint
1338 * return the affine expression f.
1340 __isl_give isl_aff
*isl_constraint_get_aff(
1341 __isl_keep isl_constraint
*constraint
)
1348 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1352 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1353 isl_int_set_si(aff
->v
->el
[0], 1);
1358 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1359 * In particular, construct aff >= 0 or aff = 0.
1361 * The denominator of "aff" can be ignored.
1363 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1364 __isl_take isl_aff
*aff
)
1366 isl_local_space
*ls
;
1371 ls
= isl_aff_get_domain_local_space(aff
);
1372 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1375 return isl_constraint_alloc_vec(eq
, ls
, v
);
1378 /* Construct an equality constraint equating the given affine expression
1381 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1383 return isl_constraint_alloc_aff(1, aff
);
1386 /* Construct an inequality constraint enforcing the given affine expression
1387 * to be non-negative.
1389 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1391 return isl_constraint_alloc_aff(0, aff
);
1394 /* Compare two isl_constraints.
1396 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1397 * than "c2" and 0 if they are equal.
1399 * The order is fairly arbitrary. We do consider constraints that only involve
1400 * earlier dimensions as "smaller".
1402 int isl_constraint_plain_cmp(__isl_keep isl_constraint
*c1
,
1403 __isl_keep isl_constraint
*c2
)
1414 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1418 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1419 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1421 return last1
- last2
;
1423 return isl_seq_cmp(c1
->v
->el
, c2
->v
->el
, c1
->v
->size
);
1426 /* Compare two constraints based on their final (non-zero) coefficients.
1427 * In particular, the constraint that involves later variables or
1428 * that has a larger coefficient for a shared latest variable
1429 * is considered "greater" than the other constraint.
1431 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1432 * than "c2" and 0 if they are equal.
1434 * If the constraints live in different local spaces, then we cannot
1435 * really compare the constraints so we compare the local spaces instead.
1437 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint
*c1
,
1438 __isl_keep isl_constraint
*c2
)
1449 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1453 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1454 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1456 return last1
- last2
;
1459 return isl_int_abs_cmp(c1
->v
->el
[1 + last1
], c2
->v
->el
[1 + last2
]);