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>
22 #include <bset_to_bmap.c>
23 #include <bset_from_bmap.c>
26 #define BASE constraint
28 #include <isl_list_templ.c>
30 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
32 return c
? isl_local_space_get_ctx(c
->ls
) : NULL
;
35 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
37 return isl_local_space_dim(c
->ls
, type
);
40 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
42 return isl_local_space_offset(c
->ls
, type
);
45 __isl_give isl_constraint
*isl_constraint_alloc_vec(int eq
,
46 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
48 isl_constraint
*constraint
;
53 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
64 isl_local_space_free(ls
);
69 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
70 __isl_take isl_local_space
*ls
)
78 ctx
= isl_local_space_get_ctx(ls
);
79 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
81 return isl_constraint_alloc_vec(eq
, ls
, v
);
84 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
90 isl_local_space
*ls
= NULL
;
91 isl_constraint
*constraint
;
96 eq
= line
>= bmap
->eq
;
98 ctx
= isl_basic_map_get_ctx(bmap
);
99 ls
= isl_basic_map_get_local_space(bmap
);
100 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
103 isl_seq_cpy(v
->el
, line
[0], v
->size
);
104 constraint
= isl_constraint_alloc_vec(eq
, ls
, v
);
106 isl_basic_map_free(bmap
);
109 isl_local_space_free(ls
);
110 isl_basic_map_free(bmap
);
114 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
117 return isl_basic_map_constraint(bset_to_bmap(bset
), line
);
120 __isl_give isl_constraint
*isl_constraint_alloc_equality(
121 __isl_take isl_local_space
*ls
)
123 return isl_constraint_alloc(1, ls
);
126 __isl_give isl_constraint
*isl_constraint_alloc_inequality(
127 __isl_take isl_local_space
*ls
)
129 return isl_constraint_alloc(0, ls
);
132 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
137 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
141 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
149 return isl_constraint_dup(c
);
152 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
161 __isl_null isl_constraint
*isl_constraint_free(__isl_take isl_constraint
*c
)
169 isl_local_space_free(c
->ls
);
176 /* Return the number of constraints in "bmap", i.e., the
177 * number of times isl_basic_map_foreach_constraint will
180 int isl_basic_map_n_constraint(__isl_keep isl_basic_map
*bmap
)
185 return bmap
->n_eq
+ bmap
->n_ineq
;
188 /* Return the number of constraints in "bset", i.e., the
189 * number of times isl_basic_set_foreach_constraint will
192 int isl_basic_set_n_constraint(__isl_keep isl_basic_set
*bset
)
194 return isl_basic_map_n_constraint(bset
);
197 isl_stat
isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
198 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
201 struct isl_constraint
*c
;
204 return isl_stat_error
;
206 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
207 return isl_stat_error
);
209 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
210 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
213 return isl_stat_error
;
215 return isl_stat_error
;
218 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
219 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
222 return isl_stat_error
;
224 return isl_stat_error
;
230 isl_stat
isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
231 isl_stat (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
233 return isl_basic_map_foreach_constraint(bset_to_bmap(bset
), fn
, user
);
236 /* Add the constraint to the list that "user" points to, if it is not
239 static isl_stat
collect_constraint(__isl_take isl_constraint
*constraint
,
242 isl_constraint_list
**list
= user
;
244 if (isl_constraint_is_div_constraint(constraint
))
245 isl_constraint_free(constraint
);
247 *list
= isl_constraint_list_add(*list
, constraint
);
252 /* Return a list of constraints that, when combined, are equivalent
253 * to "bmap". The input is required to have only known divs.
255 * There is no need to include the div constraints as they are
256 * implied by the div expressions.
258 __isl_give isl_constraint_list
*isl_basic_map_get_constraint_list(
259 __isl_keep isl_basic_map
*bmap
)
264 isl_constraint_list
*list
;
266 known
= isl_basic_map_divs_known(bmap
);
269 ctx
= isl_basic_map_get_ctx(bmap
);
271 isl_die(ctx
, isl_error_invalid
,
272 "input involves unknown divs", return NULL
);
274 n
= isl_basic_map_n_constraint(bmap
);
275 list
= isl_constraint_list_alloc(ctx
, n
);
276 if (isl_basic_map_foreach_constraint(bmap
,
277 &collect_constraint
, &list
) < 0)
278 list
= isl_constraint_list_free(list
);
283 /* Return a list of constraints that, when combined, are equivalent
284 * to "bset". The input is required to have only known divs.
286 __isl_give isl_constraint_list
*isl_basic_set_get_constraint_list(
287 __isl_keep isl_basic_set
*bset
)
289 return isl_basic_map_get_constraint_list(bset
);
292 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
293 struct isl_constraint
*constraint2
)
297 if (!constraint1
|| !constraint2
)
299 if (constraint1
->eq
!= constraint2
->eq
)
301 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
302 if (equal
< 0 || !equal
)
304 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
307 struct isl_basic_map
*isl_basic_map_add_constraint(
308 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
314 if (!bmap
|| !constraint
)
317 ctx
= isl_constraint_get_ctx(constraint
);
318 space
= isl_constraint_get_space(constraint
);
319 equal_space
= isl_space_is_equal(bmap
->dim
, space
);
320 isl_space_free(space
);
321 isl_assert(ctx
, equal_space
, goto error
);
323 bmap
= isl_basic_map_intersect(bmap
,
324 isl_basic_map_from_constraint(constraint
));
327 isl_basic_map_free(bmap
);
328 isl_constraint_free(constraint
);
332 struct isl_basic_set
*isl_basic_set_add_constraint(
333 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
335 return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset
),
339 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
340 __isl_take isl_constraint
*constraint
)
344 bmap
= isl_basic_map_from_constraint(constraint
);
345 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
350 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
351 __isl_take isl_constraint
*constraint
)
353 return isl_map_add_constraint(set
, constraint
);
356 /* Return the space of "constraint".
358 static __isl_keep isl_space
*isl_constraint_peek_space(
359 __isl_keep isl_constraint
*constraint
)
361 return constraint
? isl_local_space_peek_space(constraint
->ls
) : NULL
;
364 __isl_give isl_space
*isl_constraint_get_space(
365 __isl_keep isl_constraint
*constraint
)
367 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
370 __isl_give isl_local_space
*isl_constraint_get_local_space(
371 __isl_keep isl_constraint
*constraint
)
373 return constraint
? isl_local_space_copy(constraint
->ls
) : NULL
;
376 int isl_constraint_dim(__isl_keep isl_constraint
*constraint
,
377 enum isl_dim_type type
)
381 return n(constraint
, type
);
385 #define TYPE isl_constraint
387 #include "check_type_range_templ.c"
389 isl_bool
isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
390 enum isl_dim_type type
, unsigned first
, unsigned n
)
394 isl_bool involves
= isl_bool_false
;
397 return isl_bool_error
;
399 return isl_bool_false
;
401 if (isl_constraint_check_range(constraint
, type
, first
, n
) < 0)
402 return isl_bool_error
;
404 active
= isl_local_space_get_active(constraint
->ls
,
405 constraint
->v
->el
+ 1);
409 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
410 for (i
= 0; i
< n
; ++i
)
411 if (active
[first
+ i
]) {
412 involves
= isl_bool_true
;
421 return isl_bool_error
;
424 /* Does the given constraint represent a lower bound on the given
427 isl_bool
isl_constraint_is_lower_bound(__isl_keep isl_constraint
*constraint
,
428 enum isl_dim_type type
, unsigned pos
)
430 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
431 return isl_bool_error
;
433 pos
+= isl_local_space_offset(constraint
->ls
, type
);
434 return isl_int_is_pos(constraint
->v
->el
[pos
]);
437 /* Does the given constraint represent an upper bound on the given
440 isl_bool
isl_constraint_is_upper_bound(__isl_keep isl_constraint
*constraint
,
441 enum isl_dim_type type
, unsigned pos
)
443 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
444 return isl_bool_error
;
446 pos
+= isl_local_space_offset(constraint
->ls
, type
);
447 return isl_int_is_neg(constraint
->v
->el
[pos
]);
450 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
451 enum isl_dim_type type
, unsigned pos
)
454 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
457 void isl_constraint_get_constant(__isl_keep isl_constraint
*constraint
,
462 isl_int_set(*v
, constraint
->v
->el
[0]);
465 /* Return the constant term of "constraint".
467 __isl_give isl_val
*isl_constraint_get_constant_val(
468 __isl_keep isl_constraint
*constraint
)
475 ctx
= isl_constraint_get_ctx(constraint
);
476 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[0]);
479 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
480 enum isl_dim_type type
, int pos
, isl_int
*v
)
482 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
485 pos
+= isl_local_space_offset(constraint
->ls
, type
);
486 isl_int_set(*v
, constraint
->v
->el
[pos
]);
489 /* Return the coefficient of the variable of type "type" at position "pos"
492 __isl_give isl_val
*isl_constraint_get_coefficient_val(
493 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
497 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
500 ctx
= isl_constraint_get_ctx(constraint
);
501 pos
+= isl_local_space_offset(constraint
->ls
, type
);
502 return isl_val_int_from_isl_int(ctx
, constraint
->v
->el
[pos
]);
505 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
511 return isl_local_space_get_div(constraint
->ls
, pos
);
514 __isl_give isl_constraint
*isl_constraint_set_constant(
515 __isl_take isl_constraint
*constraint
, isl_int v
)
517 constraint
= isl_constraint_cow(constraint
);
521 constraint
->v
= isl_vec_cow(constraint
->v
);
523 return isl_constraint_free(constraint
);
525 isl_int_set(constraint
->v
->el
[0], v
);
529 /* Replace the constant term of "constraint" by "v".
531 __isl_give isl_constraint
*isl_constraint_set_constant_val(
532 __isl_take isl_constraint
*constraint
, __isl_take isl_val
*v
)
534 constraint
= isl_constraint_cow(constraint
);
535 if (!constraint
|| !v
)
537 if (!isl_val_is_int(v
))
538 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
539 "expecting integer value", goto error
);
540 constraint
->v
= isl_vec_set_element_val(constraint
->v
, 0, v
);
542 constraint
= isl_constraint_free(constraint
);
546 return isl_constraint_free(constraint
);
549 __isl_give isl_constraint
*isl_constraint_set_constant_si(
550 __isl_take isl_constraint
*constraint
, int v
)
552 constraint
= isl_constraint_cow(constraint
);
556 constraint
->v
= isl_vec_cow(constraint
->v
);
558 return isl_constraint_free(constraint
);
560 isl_int_set_si(constraint
->v
->el
[0], v
);
564 __isl_give isl_constraint
*isl_constraint_set_coefficient(
565 __isl_take isl_constraint
*constraint
,
566 enum isl_dim_type type
, int pos
, isl_int v
)
568 constraint
= isl_constraint_cow(constraint
);
569 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
570 return isl_constraint_free(constraint
);
572 constraint
->v
= isl_vec_cow(constraint
->v
);
574 return isl_constraint_free(constraint
);
576 pos
+= isl_local_space_offset(constraint
->ls
, type
);
577 isl_int_set(constraint
->v
->el
[pos
], v
);
582 /* Replace the coefficient of the variable of type "type" at position "pos"
583 * of "constraint" by "v".
585 __isl_give isl_constraint
*isl_constraint_set_coefficient_val(
586 __isl_take isl_constraint
*constraint
,
587 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
589 constraint
= isl_constraint_cow(constraint
);
590 if (!constraint
|| !v
)
592 if (!isl_val_is_int(v
))
593 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
594 "expecting integer value", goto error
);
595 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
598 pos
+= isl_local_space_offset(constraint
->ls
, type
);
599 constraint
->v
= isl_vec_set_element_val(constraint
->v
, pos
, v
);
601 constraint
= isl_constraint_free(constraint
);
605 return isl_constraint_free(constraint
);
608 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
609 __isl_take isl_constraint
*constraint
,
610 enum isl_dim_type type
, int pos
, int v
)
612 constraint
= isl_constraint_cow(constraint
);
613 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
614 return isl_constraint_free(constraint
);
616 constraint
->v
= isl_vec_cow(constraint
->v
);
618 return isl_constraint_free(constraint
);
620 pos
+= isl_local_space_offset(constraint
->ls
, type
);
621 isl_int_set_si(constraint
->v
->el
[pos
], v
);
626 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
630 constraint
= isl_constraint_cow(constraint
);
634 ctx
= isl_constraint_get_ctx(constraint
);
635 if (isl_constraint_is_equality(constraint
))
636 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
637 return isl_constraint_free(constraint
));
638 constraint
->v
= isl_vec_neg(constraint
->v
);
639 constraint
->v
= isl_vec_cow(constraint
->v
);
641 return isl_constraint_free(constraint
);
642 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
646 isl_bool
isl_constraint_is_equality(struct isl_constraint
*constraint
)
649 return isl_bool_error
;
650 return constraint
->eq
;
653 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
660 if (isl_constraint_is_equality(constraint
))
662 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
663 for (i
= 0; i
< n_div
; ++i
) {
665 is_div
= isl_local_space_is_div_constraint(constraint
->ls
,
666 constraint
->v
->el
, i
);
667 if (is_div
< 0 || is_div
)
674 /* Is "constraint" an equality that corresponds to integer division "div"?
676 * That is, given an integer division of the form
678 * a = floor((f + c)/m)
680 * is the equality of the form
684 * Note that the constant term is not checked explicitly, but given
685 * that this is a valid equality constraint, the constant c' necessarily
686 * has a value close to -c.
688 isl_bool
isl_constraint_is_div_equality(__isl_keep isl_constraint
*constraint
,
693 equality
= isl_constraint_is_equality(constraint
);
694 if (equality
< 0 || !equality
)
696 return isl_local_space_is_div_equality(constraint
->ls
,
697 constraint
->v
->el
, div
);
700 /* We manually set ISL_BASIC_SET_FINAL instead of calling
701 * isl_basic_map_finalize because we want to keep the position
702 * of the divs and we therefore do not want to throw away redundant divs.
703 * This is arguably a bit fragile.
705 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
706 __isl_take isl_constraint
*constraint
)
710 struct isl_basic_map
*bmap
;
717 ls
= isl_local_space_copy(constraint
->ls
);
718 bmap
= isl_basic_map_from_local_space(ls
);
719 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
720 if (isl_constraint_is_equality(constraint
)) {
721 k
= isl_basic_map_alloc_equality(bmap
);
727 k
= isl_basic_map_alloc_inequality(bmap
);
732 total
= isl_basic_map_total_dim(bmap
);
733 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
734 isl_constraint_free(constraint
);
736 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
739 isl_constraint_free(constraint
);
740 isl_basic_map_free(bmap
);
744 __isl_give isl_basic_set
*isl_basic_set_from_constraint(
745 __isl_take isl_constraint
*constraint
)
749 space
= isl_constraint_peek_space(constraint
);
750 if (isl_space_check_is_set(space
) < 0)
752 return bset_from_bmap(isl_basic_map_from_constraint(constraint
));
754 isl_constraint_free(constraint
);
758 /* Is the variable of "type" at position "pos" of "bmap" defined
759 * in terms of earlier dimensions through an equality?
761 * If so, and if c is not NULL, then return a copy of this equality in *c.
763 isl_bool
isl_basic_map_has_defining_equality(
764 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
765 __isl_give isl_constraint
**c
)
771 if (isl_basic_map_check_range(bmap
, type
, pos
, 1) < 0)
772 return isl_bool_error
;
773 offset
= isl_basic_map_offset(bmap
, type
);
774 total
= isl_basic_map_total_dim(bmap
);
775 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
776 if (isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) ||
777 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
778 1+total
-offset
-pos
-1) != -1)
781 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
783 return isl_bool_true
;
785 return isl_bool_false
;
788 /* Is the variable of "type" at position "pos" of "bset" defined
789 * in terms of earlier dimensions through an equality?
791 * If so, and if c is not NULL, then return a copy of this equality in *c.
793 isl_bool
isl_basic_set_has_defining_equality(
794 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
795 __isl_give isl_constraint
**c
)
797 return isl_basic_map_has_defining_equality(bset_to_bmap(bset
),
801 isl_bool
isl_basic_set_has_defining_inequalities(
802 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
803 struct isl_constraint
**lower
,
804 struct isl_constraint
**upper
)
810 isl_int
**lower_line
, **upper_line
;
812 if (isl_basic_set_check_range(bset
, type
, pos
, 1) < 0)
813 return isl_bool_error
;
814 offset
= isl_basic_set_offset(bset
, type
);
815 total
= isl_basic_set_total_dim(bset
);
817 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
818 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
820 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
822 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
824 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
825 1+total
-offset
-pos
-1) != -1)
827 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
828 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
831 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
832 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
835 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
836 lower_line
= &bset
->ineq
[i
];
837 upper_line
= &bset
->ineq
[j
];
839 lower_line
= &bset
->ineq
[j
];
840 upper_line
= &bset
->ineq
[i
];
842 *lower
= isl_basic_set_constraint(
843 isl_basic_set_copy(bset
), lower_line
);
844 *upper
= isl_basic_set_constraint(
845 isl_basic_set_copy(bset
), upper_line
);
847 return isl_bool_true
;
853 return isl_bool_false
;
856 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
857 * (in "a" and "b"), add a constraint to "bset" that ensures that the
858 * bound implied by "a" is (strictly) larger than the bound implied by "b".
860 * If both constraints imply lower bounds, then this means that "a" is
861 * active in the result.
862 * If both constraints imply upper bounds, then this means that "b" is
863 * active in the result.
865 static __isl_give isl_basic_set
*add_larger_bound_constraint(
866 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
867 unsigned abs_pos
, int strict
)
873 k
= isl_basic_set_alloc_inequality(bset
);
877 total
= isl_basic_set_dim(bset
, isl_dim_all
);
880 isl_int_neg(t
, b
[1 + abs_pos
]);
882 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
883 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
884 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
888 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
894 isl_basic_set_free(bset
);
898 /* Add constraints to "context" that ensure that "u" is the smallest
899 * (and therefore active) upper bound on "abs_pos" in "bset" and return
900 * the resulting basic set.
902 static __isl_give isl_basic_set
*set_smallest_upper_bound(
903 __isl_keep isl_basic_set
*context
,
904 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
908 context
= isl_basic_set_copy(context
);
909 context
= isl_basic_set_cow(context
);
911 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
913 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
916 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
918 context
= add_larger_bound_constraint(context
,
919 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
922 context
= isl_basic_set_simplify(context
);
923 context
= isl_basic_set_finalize(context
);
928 /* Add constraints to "context" that ensure that "u" is the largest
929 * (and therefore active) upper bound on "abs_pos" in "bset" and return
930 * the resulting basic set.
932 static __isl_give isl_basic_set
*set_largest_lower_bound(
933 __isl_keep isl_basic_set
*context
,
934 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
938 context
= isl_basic_set_copy(context
);
939 context
= isl_basic_set_cow(context
);
941 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
943 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
946 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
948 context
= add_larger_bound_constraint(context
,
949 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
952 context
= isl_basic_set_simplify(context
);
953 context
= isl_basic_set_finalize(context
);
958 static isl_stat
foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
959 enum isl_dim_type type
, unsigned abs_pos
,
960 __isl_take isl_basic_set
*context
, int n_upper
,
961 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
962 __isl_take isl_constraint
*upper
,
963 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
965 isl_basic_set
*context_i
;
966 isl_constraint
*upper
= NULL
;
969 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
970 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
973 context_i
= set_smallest_upper_bound(context
, bset
,
974 abs_pos
, n_upper
, i
);
975 if (isl_basic_set_is_empty(context_i
)) {
976 isl_basic_set_free(context_i
);
979 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
981 if (!upper
|| !context_i
)
983 if (fn(NULL
, upper
, context_i
, user
) < 0)
987 isl_basic_set_free(context
);
989 if (i
< bset
->n_ineq
)
990 return isl_stat_error
;
994 isl_constraint_free(upper
);
995 isl_basic_set_free(context_i
);
996 isl_basic_set_free(context
);
997 return isl_stat_error
;
1000 static isl_stat
foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
1001 enum isl_dim_type type
, unsigned abs_pos
,
1002 __isl_take isl_basic_set
*context
, int n_lower
,
1003 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1004 __isl_take isl_constraint
*upper
,
1005 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1007 isl_basic_set
*context_i
;
1008 isl_constraint
*lower
= NULL
;
1011 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1012 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
1015 context_i
= set_largest_lower_bound(context
, bset
,
1016 abs_pos
, n_lower
, i
);
1017 if (isl_basic_set_is_empty(context_i
)) {
1018 isl_basic_set_free(context_i
);
1021 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1023 if (!lower
|| !context_i
)
1025 if (fn(lower
, NULL
, context_i
, user
) < 0)
1029 isl_basic_set_free(context
);
1031 if (i
< bset
->n_ineq
)
1032 return isl_stat_error
;
1036 isl_constraint_free(lower
);
1037 isl_basic_set_free(context_i
);
1038 isl_basic_set_free(context
);
1039 return isl_stat_error
;
1042 static isl_stat
foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1043 enum isl_dim_type type
, unsigned abs_pos
,
1044 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
1045 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1046 __isl_take isl_constraint
*upper
,
1047 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1049 isl_basic_set
*context_i
, *context_j
;
1050 isl_constraint
*lower
= NULL
;
1051 isl_constraint
*upper
= NULL
;
1054 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1055 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1058 context_i
= set_largest_lower_bound(context
, bset
,
1059 abs_pos
, n_lower
, i
);
1060 if (isl_basic_set_is_empty(context_i
)) {
1061 isl_basic_set_free(context_i
);
1065 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
1066 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
1069 context_j
= set_smallest_upper_bound(context_i
, bset
,
1070 abs_pos
, n_upper
, j
);
1071 context_j
= isl_basic_set_extend_constraints(context_j
,
1073 context_j
= add_larger_bound_constraint(context_j
,
1074 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
1075 context_j
= isl_basic_set_simplify(context_j
);
1076 context_j
= isl_basic_set_finalize(context_j
);
1077 if (isl_basic_set_is_empty(context_j
)) {
1078 isl_basic_set_free(context_j
);
1081 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1083 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1085 if (!lower
|| !upper
|| !context_j
)
1087 if (fn(lower
, upper
, context_j
, user
) < 0)
1091 isl_basic_set_free(context_i
);
1093 if (j
< bset
->n_ineq
)
1097 isl_basic_set_free(context
);
1099 if (i
< bset
->n_ineq
)
1100 return isl_stat_error
;
1104 isl_constraint_free(lower
);
1105 isl_constraint_free(upper
);
1106 isl_basic_set_free(context_i
);
1107 isl_basic_set_free(context_j
);
1108 isl_basic_set_free(context
);
1109 return isl_stat_error
;
1112 /* For each pair of lower and upper bounds on the variable "pos"
1113 * of type "type", call "fn" with these lower and upper bounds and the
1114 * set of constraints on the remaining variables where these bounds
1115 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1117 * If the designated variable is equal to an affine combination of the
1118 * other variables then fn is called with both lower and upper
1119 * set to the corresponding equality.
1121 * If there is no lower (or upper) bound, then NULL is passed
1122 * as the corresponding bound.
1124 * We first check if the variable is involved in any equality.
1125 * If not, we count the number of lower and upper bounds and
1128 isl_stat
isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
1129 enum isl_dim_type type
, unsigned pos
,
1130 isl_stat (*fn
)(__isl_take isl_constraint
*lower
,
1131 __isl_take isl_constraint
*upper
,
1132 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
1135 isl_constraint
*lower
= NULL
;
1136 isl_constraint
*upper
= NULL
;
1137 isl_basic_set
*context
= NULL
;
1139 int n_lower
, n_upper
;
1141 if (isl_basic_set_check_range(bset
, type
, pos
, 1) < 0)
1142 return isl_stat_error
;
1143 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
1144 return isl_stat_error
);
1147 if (type
== isl_dim_set
)
1148 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1150 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1151 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1154 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1156 upper
= isl_constraint_copy(lower
);
1157 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1159 if (!lower
|| !upper
|| !context
)
1161 return fn(lower
, upper
, context
, user
);
1166 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1167 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1169 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1173 context
= isl_basic_set_copy(bset
);
1174 context
= isl_basic_set_cow(context
);
1177 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1178 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1179 isl_basic_set_drop_inequality(context
, i
);
1181 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1182 if (!n_lower
&& !n_upper
)
1183 return fn(NULL
, NULL
, context
, user
);
1185 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1188 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1190 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1193 isl_constraint_free(lower
);
1194 isl_constraint_free(upper
);
1195 isl_basic_set_free(context
);
1196 return isl_stat_error
;
1199 __isl_give isl_aff
*isl_constraint_get_bound(
1200 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1206 if (isl_constraint_check_range(constraint
, type
, pos
, 1) < 0)
1208 space
= isl_constraint_peek_space(constraint
);
1209 if (isl_space_check_is_set(space
) < 0)
1212 ctx
= isl_constraint_get_ctx(constraint
);
1213 pos
+= offset(constraint
, type
);
1214 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1215 isl_die(ctx
, isl_error_invalid
,
1216 "constraint does not define a bound on given dimension",
1219 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1223 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1224 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1226 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1227 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1228 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1233 /* For an inequality constraint
1237 * or an equality constraint
1241 * return the affine expression f.
1243 __isl_give isl_aff
*isl_constraint_get_aff(
1244 __isl_keep isl_constraint
*constraint
)
1251 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1255 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1256 isl_int_set_si(aff
->v
->el
[0], 1);
1261 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1262 * In particular, construct aff >= 0 or aff = 0.
1264 * The denominator of "aff" can be ignored.
1266 static __isl_give isl_constraint
*isl_constraint_alloc_aff(int eq
,
1267 __isl_take isl_aff
*aff
)
1269 isl_local_space
*ls
;
1274 ls
= isl_aff_get_domain_local_space(aff
);
1275 v
= isl_vec_drop_els(isl_vec_copy(aff
->v
), 0, 1);
1278 return isl_constraint_alloc_vec(eq
, ls
, v
);
1281 /* Construct an equality constraint equating the given affine expression
1284 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1286 return isl_constraint_alloc_aff(1, aff
);
1289 /* Construct an inequality constraint enforcing the given affine expression
1290 * to be non-negative.
1292 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1294 return isl_constraint_alloc_aff(0, aff
);
1297 /* Compare two isl_constraints.
1299 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1300 * than "c2" and 0 if they are equal.
1302 * The order is fairly arbitrary. We do consider constraints that only involve
1303 * earlier dimensions as "smaller".
1305 int isl_constraint_plain_cmp(__isl_keep isl_constraint
*c1
,
1306 __isl_keep isl_constraint
*c2
)
1317 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1321 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1322 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1324 return last1
- last2
;
1326 return isl_seq_cmp(c1
->v
->el
, c2
->v
->el
, c1
->v
->size
);
1329 /* Compare two constraints based on their final (non-zero) coefficients.
1330 * In particular, the constraint that involves later variables or
1331 * that has a larger coefficient for a shared latest variable
1332 * is considered "greater" than the other constraint.
1334 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1335 * than "c2" and 0 if they are equal.
1337 * If the constraints live in different local spaces, then we cannot
1338 * really compare the constraints so we compare the local spaces instead.
1340 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint
*c1
,
1341 __isl_keep isl_constraint
*c2
)
1352 cmp
= isl_local_space_cmp(c1
->ls
, c2
->ls
);
1356 last1
= isl_seq_last_non_zero(c1
->v
->el
+ 1, c1
->v
->size
- 1);
1357 last2
= isl_seq_last_non_zero(c2
->v
->el
+ 1, c1
->v
->size
- 1);
1359 return last1
- last2
;
1362 return isl_int_abs_cmp(c1
->v
->el
[1 + last1
], c2
->v
->el
[1 + last2
]);