2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 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>
20 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
22 return c
? isl_local_space_get_ctx(c
->ls
) : NULL
;
25 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
27 return isl_local_space_dim(c
->ls
, type
);
30 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
32 return isl_local_space_offset(c
->ls
, type
);
35 static unsigned basic_map_offset(__isl_keep isl_basic_map
*bmap
,
36 enum isl_dim_type type
)
38 return type
== isl_dim_div
? 1 + isl_space_dim(bmap
->dim
, isl_dim_all
)
39 : 1 + isl_space_offset(bmap
->dim
, type
);
42 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
43 enum isl_dim_type type
)
45 isl_space
*dim
= bset
->dim
;
47 case isl_dim_param
: return 1;
48 case isl_dim_in
: return 1 + dim
->nparam
;
49 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
50 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
55 __isl_give isl_constraint
*isl_constraint_alloc_vec(int eq
,
56 __isl_take isl_local_space
*ls
, __isl_take isl_vec
*v
)
58 isl_constraint
*constraint
;
63 constraint
= isl_alloc_type(isl_vec_get_ctx(v
), isl_constraint
);
74 isl_local_space_free(ls
);
79 __isl_give isl_constraint
*isl_constraint_alloc(int eq
,
80 __isl_take isl_local_space
*ls
)
88 ctx
= isl_local_space_get_ctx(ls
);
89 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
91 return isl_constraint_alloc_vec(eq
, ls
, v
);
94 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
100 isl_local_space
*ls
= NULL
;
101 isl_constraint
*constraint
;
106 eq
= line
>= bmap
->eq
;
108 ctx
= isl_basic_map_get_ctx(bmap
);
109 ls
= isl_basic_map_get_local_space(bmap
);
110 v
= isl_vec_alloc(ctx
, 1 + isl_local_space_dim(ls
, isl_dim_all
));
113 isl_seq_cpy(v
->el
, line
[0], v
->size
);
114 constraint
= isl_constraint_alloc_vec(eq
, ls
, v
);
116 isl_basic_map_free(bmap
);
119 isl_local_space_free(ls
);
120 isl_basic_map_free(bmap
);
124 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
127 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
130 __isl_give isl_constraint
*isl_equality_alloc(__isl_take isl_local_space
*ls
)
132 return isl_constraint_alloc(1, ls
);
135 __isl_give isl_constraint
*isl_inequality_alloc(__isl_take isl_local_space
*ls
)
137 return isl_constraint_alloc(0, ls
);
140 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
145 return isl_constraint_alloc_vec(c
->eq
, isl_local_space_copy(c
->ls
),
149 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
157 return isl_constraint_dup(c
);
160 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
169 void *isl_constraint_free(struct isl_constraint
*c
)
177 isl_local_space_free(c
->ls
);
184 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
185 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
188 struct isl_constraint
*c
;
193 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
196 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
197 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
205 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
206 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
217 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
218 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
220 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
223 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
224 struct isl_constraint
*constraint2
)
228 if (!constraint1
|| !constraint2
)
230 if (constraint1
->eq
!= constraint2
->eq
)
232 equal
= isl_local_space_is_equal(constraint1
->ls
, constraint2
->ls
);
233 if (equal
< 0 || !equal
)
235 return isl_vec_is_equal(constraint1
->v
, constraint2
->v
);
238 struct isl_basic_map
*isl_basic_map_add_constraint(
239 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
245 if (!bmap
|| !constraint
)
248 ctx
= isl_constraint_get_ctx(constraint
);
249 dim
= isl_constraint_get_space(constraint
);
250 equal_space
= isl_space_is_equal(bmap
->dim
, dim
);
252 isl_assert(ctx
, equal_space
, goto error
);
254 bmap
= isl_basic_map_intersect(bmap
,
255 isl_basic_map_from_constraint(constraint
));
258 isl_basic_map_free(bmap
);
259 isl_constraint_free(constraint
);
263 struct isl_basic_set
*isl_basic_set_add_constraint(
264 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
266 return (struct isl_basic_set
*)
267 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
271 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
272 __isl_take isl_constraint
*constraint
)
276 bmap
= isl_basic_map_from_constraint(constraint
);
277 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
282 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
283 __isl_take isl_constraint
*constraint
)
285 return isl_map_add_constraint(set
, constraint
);
288 __isl_give isl_space
*isl_constraint_get_space(
289 __isl_keep isl_constraint
*constraint
)
291 return constraint
? isl_local_space_get_space(constraint
->ls
) : NULL
;
294 int isl_constraint_dim(struct isl_constraint
*constraint
,
295 enum isl_dim_type type
)
299 return n(constraint
, type
);
302 int isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
303 enum isl_dim_type type
, unsigned first
, unsigned n
)
315 ctx
= isl_constraint_get_ctx(constraint
);
316 if (first
+ n
> isl_constraint_dim(constraint
, type
))
317 isl_die(ctx
, isl_error_invalid
,
318 "range out of bounds", return -1);
320 active
= isl_local_space_get_active(constraint
->ls
,
321 constraint
->v
->el
+ 1);
325 first
+= isl_local_space_offset(constraint
->ls
, type
) - 1;
326 for (i
= 0; i
< n
; ++i
)
327 if (active
[first
+ i
]) {
340 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
341 enum isl_dim_type type
, unsigned pos
)
344 isl_local_space_get_dim_name(constraint
->ls
, type
, pos
) : NULL
;
347 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
351 isl_int_set(*v
, constraint
->v
->el
[0]);
354 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
355 enum isl_dim_type type
, int pos
, isl_int
*v
)
360 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
361 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
362 "position out of bounds", return);
364 pos
+= isl_local_space_offset(constraint
->ls
, type
);
365 isl_int_set(*v
, constraint
->v
->el
[pos
]);
368 __isl_give isl_aff
*isl_constraint_get_div(__isl_keep isl_constraint
*constraint
,
374 return isl_local_space_get_div(constraint
->ls
, pos
);
377 __isl_give isl_constraint
*isl_constraint_set_constant(
378 __isl_take isl_constraint
*constraint
, isl_int v
)
380 constraint
= isl_constraint_cow(constraint
);
384 constraint
->v
= isl_vec_cow(constraint
->v
);
386 return isl_constraint_free(constraint
);
388 isl_int_set(constraint
->v
->el
[0], v
);
392 __isl_give isl_constraint
*isl_constraint_set_constant_si(
393 __isl_take isl_constraint
*constraint
, int v
)
395 constraint
= isl_constraint_cow(constraint
);
399 constraint
->v
= isl_vec_cow(constraint
->v
);
401 return isl_constraint_free(constraint
);
403 isl_int_set_si(constraint
->v
->el
[0], v
);
407 __isl_give isl_constraint
*isl_constraint_set_coefficient(
408 __isl_take isl_constraint
*constraint
,
409 enum isl_dim_type type
, int pos
, isl_int v
)
411 constraint
= isl_constraint_cow(constraint
);
415 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
416 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
417 "position out of bounds",
418 return isl_constraint_free(constraint
));
420 constraint
= isl_constraint_cow(constraint
);
424 constraint
->v
= isl_vec_cow(constraint
->v
);
426 return isl_constraint_free(constraint
);
428 pos
+= isl_local_space_offset(constraint
->ls
, type
);
429 isl_int_set(constraint
->v
->el
[pos
], v
);
434 __isl_give isl_constraint
*isl_constraint_set_coefficient_si(
435 __isl_take isl_constraint
*constraint
,
436 enum isl_dim_type type
, int pos
, int v
)
438 constraint
= isl_constraint_cow(constraint
);
442 if (pos
>= isl_local_space_dim(constraint
->ls
, type
))
443 isl_die(constraint
->v
->ctx
, isl_error_invalid
,
444 "position out of bounds",
445 return isl_constraint_free(constraint
));
447 constraint
= isl_constraint_cow(constraint
);
451 constraint
->v
= isl_vec_cow(constraint
->v
);
453 return isl_constraint_free(constraint
);
455 pos
+= isl_local_space_offset(constraint
->ls
, type
);
456 isl_int_set_si(constraint
->v
->el
[pos
], v
);
461 /* Drop any constraint from "bset" that is identical to "constraint".
462 * In particular, this means that the local spaces of "bset" and
463 * "constraint" need to be the same.
465 * Since the given constraint may actually be a pointer into the bset,
466 * we have to be careful not to reorder the constraints as the user
467 * may be holding on to other constraints from the same bset.
468 * This should be cleaned up when the internal representation of
469 * isl_constraint is changed to use isl_aff.
471 __isl_give isl_basic_set
*isl_basic_set_drop_constraint(
472 __isl_take isl_basic_set
*bset
, __isl_take isl_constraint
*constraint
)
478 isl_local_space
*ls1
;
481 if (!bset
|| !constraint
)
484 ls1
= isl_basic_set_get_local_space(bset
);
485 equal
= isl_local_space_is_equal(ls1
, constraint
->ls
);
486 isl_local_space_free(ls1
);
490 isl_constraint_free(constraint
);
494 if (isl_constraint_is_equality(constraint
)) {
502 total
= isl_constraint_dim(constraint
, isl_dim_all
);
503 for (i
= 0; i
< n
; ++i
)
504 if (isl_seq_eq(row
[i
], constraint
->v
->el
, 1 + total
))
505 isl_seq_clr(row
[i
], 1 + total
);
507 isl_constraint_free(constraint
);
510 isl_constraint_free(constraint
);
511 isl_basic_set_free(bset
);
515 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
519 constraint
= isl_constraint_cow(constraint
);
523 ctx
= isl_constraint_get_ctx(constraint
);
524 if (isl_constraint_is_equality(constraint
))
525 isl_die(ctx
, isl_error_invalid
, "cannot negate equality",
526 return isl_constraint_free(constraint
));
527 constraint
->v
= isl_vec_neg(constraint
->v
);
528 constraint
->v
= isl_vec_cow(constraint
->v
);
530 return isl_constraint_free(constraint
);
531 isl_int_sub_ui(constraint
->v
->el
[0], constraint
->v
->el
[0], 1);
535 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
539 return constraint
->eq
;
542 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
549 if (isl_constraint_is_equality(constraint
))
551 n_div
= isl_constraint_dim(constraint
, isl_dim_div
);
552 for (i
= 0; i
< n_div
; ++i
) {
553 if (isl_local_space_is_div_constraint(constraint
->ls
,
554 constraint
->v
->el
, i
))
561 /* We manually set ISL_BASIC_SET_FINAL instead of calling
562 * isl_basic_map_finalize because we want to keep the position
563 * of the divs and we therefore do not want to throw away redundant divs.
564 * This is arguably a bit fragile.
566 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
567 __isl_take isl_constraint
*constraint
)
571 struct isl_basic_map
*bmap
;
578 ls
= isl_local_space_copy(constraint
->ls
);
579 bmap
= isl_basic_map_from_local_space(ls
);
580 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
581 if (isl_constraint_is_equality(constraint
)) {
582 k
= isl_basic_map_alloc_equality(bmap
);
588 k
= isl_basic_map_alloc_inequality(bmap
);
593 total
= isl_basic_map_total_dim(bmap
);
594 isl_seq_cpy(c
, constraint
->v
->el
, 1 + total
);
595 isl_constraint_free(constraint
);
597 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
600 isl_constraint_free(constraint
);
601 isl_basic_map_free(bmap
);
605 struct isl_basic_set
*isl_basic_set_from_constraint(
606 struct isl_constraint
*constraint
)
611 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
612 isl_die(isl_constraint_get_ctx(constraint
), isl_error_invalid
,
613 "not a set constraint",
614 return isl_constraint_free(constraint
));
615 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
618 int isl_basic_map_has_defining_equality(
619 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
620 __isl_give isl_constraint
**c
)
628 offset
= basic_map_offset(bmap
, type
);
629 total
= isl_basic_map_total_dim(bmap
);
630 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
631 for (i
= 0; i
< bmap
->n_eq
; ++i
)
632 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
633 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
634 1+total
-offset
-pos
-1) == -1) {
635 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
642 int isl_basic_set_has_defining_equality(
643 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
644 __isl_give isl_constraint
**c
)
646 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
650 int isl_basic_set_has_defining_inequalities(
651 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
652 struct isl_constraint
**lower
,
653 struct isl_constraint
**upper
)
659 isl_int
**lower_line
, **upper_line
;
663 offset
= basic_set_offset(bset
, type
);
664 total
= isl_basic_set_total_dim(bset
);
665 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
667 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
668 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
670 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
672 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
674 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
675 1+total
-offset
-pos
-1) != -1)
677 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
678 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
681 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
682 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
685 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
686 lower_line
= &bset
->ineq
[i
];
687 upper_line
= &bset
->ineq
[j
];
689 lower_line
= &bset
->ineq
[j
];
690 upper_line
= &bset
->ineq
[i
];
692 *lower
= isl_basic_set_constraint(
693 isl_basic_set_copy(bset
), lower_line
);
694 *upper
= isl_basic_set_constraint(
695 isl_basic_set_copy(bset
), upper_line
);
706 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
707 * (in "a" and "b"), add a constraint to "bset" that ensures that the
708 * bound implied by "a" is (strictly) larger than the bound implied by "b".
710 * If both constraints imply lower bounds, then this means that "a" is
711 * active in the result.
712 * If both constraints imply upper bounds, then this means that "b" is
713 * active in the result.
715 static __isl_give isl_basic_set
*add_larger_bound_constraint(
716 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
717 unsigned abs_pos
, int strict
)
723 k
= isl_basic_set_alloc_inequality(bset
);
727 total
= isl_basic_set_dim(bset
, isl_dim_all
);
730 isl_int_neg(t
, b
[1 + abs_pos
]);
732 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
733 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
734 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
738 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
744 isl_basic_set_free(bset
);
748 /* Add constraints to "context" that ensure that "u" is the smallest
749 * (and therefore active) upper bound on "abs_pos" in "bset" and return
750 * the resulting basic set.
752 static __isl_give isl_basic_set
*set_smallest_upper_bound(
753 __isl_keep isl_basic_set
*context
,
754 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
758 context
= isl_basic_set_copy(context
);
759 context
= isl_basic_set_cow(context
);
761 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
763 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
766 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
768 context
= add_larger_bound_constraint(context
,
769 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
772 context
= isl_basic_set_simplify(context
);
773 context
= isl_basic_set_finalize(context
);
778 /* Add constraints to "context" that ensure that "u" is the largest
779 * (and therefore active) upper bound on "abs_pos" in "bset" and return
780 * the resulting basic set.
782 static __isl_give isl_basic_set
*set_largest_lower_bound(
783 __isl_keep isl_basic_set
*context
,
784 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
788 context
= isl_basic_set_copy(context
);
789 context
= isl_basic_set_cow(context
);
791 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
793 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
796 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
798 context
= add_larger_bound_constraint(context
,
799 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
802 context
= isl_basic_set_simplify(context
);
803 context
= isl_basic_set_finalize(context
);
808 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
809 enum isl_dim_type type
, unsigned abs_pos
,
810 __isl_take isl_basic_set
*context
, int n_upper
,
811 int (*fn
)(__isl_take isl_constraint
*lower
,
812 __isl_take isl_constraint
*upper
,
813 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
815 isl_basic_set
*context_i
;
816 isl_constraint
*upper
= NULL
;
819 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
820 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
823 context_i
= set_smallest_upper_bound(context
, bset
,
824 abs_pos
, n_upper
, i
);
825 if (isl_basic_set_is_empty(context_i
)) {
826 isl_basic_set_free(context_i
);
829 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
831 if (!upper
|| !context_i
)
833 if (fn(NULL
, upper
, context_i
, user
) < 0)
837 isl_basic_set_free(context
);
839 if (i
< bset
->n_ineq
)
844 isl_constraint_free(upper
);
845 isl_basic_set_free(context_i
);
846 isl_basic_set_free(context
);
850 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
851 enum isl_dim_type type
, unsigned abs_pos
,
852 __isl_take isl_basic_set
*context
, int n_lower
,
853 int (*fn
)(__isl_take isl_constraint
*lower
,
854 __isl_take isl_constraint
*upper
,
855 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
857 isl_basic_set
*context_i
;
858 isl_constraint
*lower
= NULL
;
861 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
862 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
865 context_i
= set_largest_lower_bound(context
, bset
,
866 abs_pos
, n_lower
, i
);
867 if (isl_basic_set_is_empty(context_i
)) {
868 isl_basic_set_free(context_i
);
871 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
873 if (!lower
|| !context_i
)
875 if (fn(lower
, NULL
, context_i
, user
) < 0)
879 isl_basic_set_free(context
);
881 if (i
< bset
->n_ineq
)
886 isl_constraint_free(lower
);
887 isl_basic_set_free(context_i
);
888 isl_basic_set_free(context
);
892 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
893 enum isl_dim_type type
, unsigned abs_pos
,
894 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
895 int (*fn
)(__isl_take isl_constraint
*lower
,
896 __isl_take isl_constraint
*upper
,
897 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
899 isl_basic_set
*context_i
, *context_j
;
900 isl_constraint
*lower
= NULL
;
901 isl_constraint
*upper
= NULL
;
904 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
905 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
908 context_i
= set_largest_lower_bound(context
, bset
,
909 abs_pos
, n_lower
, i
);
910 if (isl_basic_set_is_empty(context_i
)) {
911 isl_basic_set_free(context_i
);
915 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
916 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
919 context_j
= set_smallest_upper_bound(context_i
, bset
,
920 abs_pos
, n_upper
, j
);
921 context_j
= isl_basic_set_extend_constraints(context_j
,
923 context_j
= add_larger_bound_constraint(context_j
,
924 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
925 context_j
= isl_basic_set_simplify(context_j
);
926 context_j
= isl_basic_set_finalize(context_j
);
927 if (isl_basic_set_is_empty(context_j
)) {
928 isl_basic_set_free(context_j
);
931 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
933 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
935 if (!lower
|| !upper
|| !context_j
)
937 if (fn(lower
, upper
, context_j
, user
) < 0)
941 isl_basic_set_free(context_i
);
943 if (j
< bset
->n_ineq
)
947 isl_basic_set_free(context
);
949 if (i
< bset
->n_ineq
)
954 isl_constraint_free(lower
);
955 isl_constraint_free(upper
);
956 isl_basic_set_free(context_i
);
957 isl_basic_set_free(context_j
);
958 isl_basic_set_free(context
);
962 /* For each pair of lower and upper bounds on the variable "pos"
963 * of type "type", call "fn" with these lower and upper bounds and the
964 * set of constraints on the remaining variables where these bounds
965 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
967 * If the designated variable is equal to an affine combination of the
968 * other variables then fn is called with both lower and upper
969 * set to the corresponding equality.
971 * If there is no lower (or upper) bound, then NULL is passed
972 * as the corresponding bound.
974 * We first check if the variable is involved in any equality.
975 * If not, we count the number of lower and upper bounds and
978 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
979 enum isl_dim_type type
, unsigned pos
,
980 int (*fn
)(__isl_take isl_constraint
*lower
,
981 __isl_take isl_constraint
*upper
,
982 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
985 isl_constraint
*lower
= NULL
;
986 isl_constraint
*upper
= NULL
;
987 isl_basic_set
*context
= NULL
;
989 int n_lower
, n_upper
;
993 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
994 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
998 if (type
== isl_dim_set
)
999 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
1001 for (i
= 0; i
< bset
->n_eq
; ++i
) {
1002 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
1005 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
1007 upper
= isl_constraint_copy(lower
);
1008 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
1010 if (!lower
|| !upper
|| !context
)
1012 return fn(lower
, upper
, context
, user
);
1017 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
1018 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
1020 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
1024 context
= isl_basic_set_copy(bset
);
1025 context
= isl_basic_set_cow(context
);
1028 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
1029 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
1030 isl_basic_set_drop_inequality(context
, i
);
1032 context
= isl_basic_set_drop(context
, type
, pos
, 1);
1033 if (!n_lower
&& !n_upper
)
1034 return fn(NULL
, NULL
, context
, user
);
1036 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
1039 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
1041 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
1044 isl_constraint_free(lower
);
1045 isl_constraint_free(upper
);
1046 isl_basic_set_free(context
);
1050 __isl_give isl_aff
*isl_constraint_get_bound(
1051 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
1058 ctx
= isl_constraint_get_ctx(constraint
);
1059 if (pos
>= isl_constraint_dim(constraint
, type
))
1060 isl_die(ctx
, isl_error_invalid
,
1061 "index out of bounds", return NULL
);
1062 if (isl_constraint_dim(constraint
, isl_dim_in
) != 0)
1063 isl_die(ctx
, isl_error_invalid
,
1064 "not a set constraint", return NULL
);
1066 pos
+= offset(constraint
, type
);
1067 if (isl_int_is_zero(constraint
->v
->el
[pos
]))
1068 isl_die(ctx
, isl_error_invalid
,
1069 "constraint does not define a bound on given dimension",
1072 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1076 if (isl_int_is_neg(constraint
->v
->el
[pos
]))
1077 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1079 isl_seq_neg(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1080 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1081 isl_int_abs(aff
->v
->el
[0], constraint
->v
->el
[pos
]);
1086 /* For an inequality constraint
1090 * or an equality constraint
1094 * return the affine expression f.
1096 __isl_give isl_aff
*isl_constraint_get_aff(
1097 __isl_keep isl_constraint
*constraint
)
1104 aff
= isl_aff_alloc(isl_local_space_copy(constraint
->ls
));
1108 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->v
->el
, aff
->v
->size
- 1);
1109 isl_int_set_si(aff
->v
->el
[0], 1);
1114 /* Construct an equality constraint equating the given affine expression
1117 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1120 isl_local_space
*ls
;
1121 isl_basic_set
*bset
;
1126 ls
= isl_aff_get_domain_local_space(aff
);
1127 bset
= isl_basic_set_from_local_space(ls
);
1128 bset
= isl_basic_set_extend_constraints(bset
, 1, 0);
1129 k
= isl_basic_set_alloc_equality(bset
);
1133 isl_seq_cpy(bset
->eq
[k
], aff
->v
->el
+ 1, aff
->v
->size
- 1);
1136 return isl_basic_set_constraint(bset
, &bset
->eq
[k
]);
1139 isl_basic_set_free(bset
);
1143 /* Construct an inequality constraint enforcing the given affine expression
1144 * to be non-negative.
1146 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1149 isl_local_space
*ls
;
1150 isl_basic_set
*bset
;
1155 ls
= isl_aff_get_domain_local_space(aff
);
1156 bset
= isl_basic_set_from_local_space(ls
);
1157 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1158 k
= isl_basic_set_alloc_inequality(bset
);
1162 isl_seq_cpy(bset
->ineq
[k
], aff
->v
->el
+ 1, aff
->v
->size
- 1);
1165 return isl_basic_set_constraint(bset
, &bset
->ineq
[k
]);
1168 isl_basic_set_free(bset
);