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/constraint.h>
14 #include <isl_dim_private.h>
16 #include "isl_map_private.h"
18 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
20 return isl_basic_map_dim(c
->bmap
, type
);
23 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
25 struct isl_dim
*dim
= c
->bmap
->dim
;
27 case isl_dim_param
: return 1;
28 case isl_dim_in
: return 1 + dim
->nparam
;
29 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
30 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
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_dim_total(bmap
->dim
)
39 : 1 + isl_dim_offset(bmap
->dim
, type
);
42 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
43 enum isl_dim_type type
)
45 struct isl_dim
*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 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
58 struct isl_constraint
*constraint
;
63 constraint
= isl_alloc_type(bmap
->ctx
, struct isl_constraint
);
67 constraint
->ctx
= bmap
->ctx
;
68 isl_ctx_ref(constraint
->ctx
);
70 constraint
->bmap
= bmap
;
71 constraint
->line
= line
;
75 isl_basic_map_free(bmap
);
79 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
82 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
85 struct isl_constraint
*isl_equality_alloc(struct isl_dim
*dim
)
87 struct isl_basic_map
*bmap
;
92 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
96 isl_basic_map_alloc_equality(bmap
);
97 isl_seq_clr(bmap
->eq
[0], 1 + isl_basic_map_total_dim(bmap
));
98 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
101 struct isl_constraint
*isl_inequality_alloc(struct isl_dim
*dim
)
103 struct isl_basic_map
*bmap
;
108 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 1);
112 isl_basic_map_alloc_inequality(bmap
);
113 isl_seq_clr(bmap
->ineq
[0], 1 + isl_basic_map_total_dim(bmap
));
114 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
117 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
119 struct isl_basic_map
*bmap
;
126 eq
= c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
;
127 i
= eq
? c
->line
- c
->bmap
->eq
: c
->line
- c
->bmap
->ineq
;
128 bmap
= isl_basic_map_copy(c
->bmap
);
131 return isl_basic_map_constraint(bmap
, eq
? bmap
->eq
+ i
: bmap
->ineq
+ i
);
134 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
142 return isl_constraint_dup(c
);
145 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
154 void isl_constraint_free(struct isl_constraint
*c
)
162 isl_basic_map_free(c
->bmap
);
163 isl_ctx_deref(c
->ctx
);
167 __isl_give isl_constraint
*isl_basic_map_first_constraint(
168 __isl_take isl_basic_map
*bmap
)
174 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
176 if (bmap
->n_ineq
> 0)
177 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
179 isl_basic_map_free(bmap
);
183 __isl_give isl_constraint
*isl_basic_set_first_constraint(
184 __isl_take isl_basic_set
*bset
)
186 return isl_basic_map_first_constraint((struct isl_basic_map
*)bset
);
189 struct isl_constraint
*isl_constraint_next(struct isl_constraint
*c
)
191 c
= isl_constraint_cow(c
);
192 if (c
->line
>= c
->bmap
->eq
) {
194 if (c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
)
196 c
->line
= c
->bmap
->ineq
;
199 if (c
->line
< c
->bmap
->ineq
+ c
->bmap
->n_ineq
)
201 isl_constraint_free(c
);
205 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
206 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
209 struct isl_constraint
*c
;
214 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
217 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
218 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
226 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
227 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
238 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
239 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
241 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
244 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
245 struct isl_constraint
*constraint2
)
247 if (!constraint1
|| !constraint2
)
249 return constraint1
->bmap
== constraint2
->bmap
&&
250 constraint1
->line
== constraint2
->line
;
253 struct isl_basic_map
*isl_basic_map_add_constraint(
254 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
256 if (!bmap
|| !constraint
)
259 isl_assert(constraint
->ctx
,
260 isl_dim_equal(bmap
->dim
, constraint
->bmap
->dim
), goto error
);
262 bmap
= isl_basic_map_intersect(bmap
,
263 isl_basic_map_from_constraint(constraint
));
266 isl_basic_map_free(bmap
);
267 isl_constraint_free(constraint
);
271 struct isl_basic_set
*isl_basic_set_add_constraint(
272 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
274 return (struct isl_basic_set
*)
275 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
279 struct isl_constraint
*isl_constraint_add_div(struct isl_constraint
*constraint
,
280 struct isl_div
*div
, int *pos
)
282 if (!constraint
|| !div
)
285 isl_assert(constraint
->ctx
,
286 isl_dim_equal(div
->bmap
->dim
, constraint
->bmap
->dim
), goto error
);
287 isl_assert(constraint
->ctx
,
288 constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1, goto error
);
290 constraint
->bmap
= isl_basic_map_cow(constraint
->bmap
);
291 constraint
->bmap
= isl_basic_map_extend_dim(constraint
->bmap
,
292 isl_dim_copy(constraint
->bmap
->dim
), 1, 0, 0);
293 if (!constraint
->bmap
)
295 constraint
->line
= &constraint
->bmap
->ineq
[0];
296 *pos
= isl_basic_map_alloc_div(constraint
->bmap
);
299 isl_seq_cpy(constraint
->bmap
->div
[*pos
], div
->line
[0],
300 1 + 1 + isl_basic_map_total_dim(constraint
->bmap
));
304 isl_constraint_free(constraint
);
309 int isl_constraint_dim(struct isl_constraint
*constraint
,
310 enum isl_dim_type type
)
314 return n(constraint
, type
);
317 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
318 enum isl_dim_type type
, unsigned pos
)
321 isl_basic_map_get_dim_name(constraint
->bmap
, type
, pos
) : NULL
;
324 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
328 isl_int_set(*v
, constraint
->line
[0][0]);
331 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
332 enum isl_dim_type type
, int pos
, isl_int
*v
)
337 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
338 isl_int_set(*v
, constraint
->line
[0][offset(constraint
, type
) + pos
]);
341 struct isl_div
*isl_constraint_div(struct isl_constraint
*constraint
, int pos
)
346 isl_assert(constraint
->ctx
, pos
< n(constraint
, isl_dim_div
),
348 isl_assert(constraint
->ctx
,
349 !isl_int_is_zero(constraint
->bmap
->div
[pos
][0]), return NULL
);
350 return isl_basic_map_div(isl_basic_map_copy(constraint
->bmap
), pos
);
353 void isl_constraint_set_constant(struct isl_constraint
*constraint
, isl_int v
)
357 isl_int_set(constraint
->line
[0][0], v
);
360 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
361 enum isl_dim_type type
, int pos
, isl_int v
)
366 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
367 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
370 void isl_constraint_clear(struct isl_constraint
*constraint
)
376 total
= isl_basic_map_total_dim(constraint
->bmap
);
377 isl_seq_clr(constraint
->line
[0], 1 + total
);
380 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
387 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
389 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
390 total
= isl_basic_map_total_dim(constraint
->bmap
);
391 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
392 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
393 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
396 isl_constraint_free(constraint
);
400 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
404 return constraint
->line
>= constraint
->bmap
->eq
;
407 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
413 if (isl_constraint_is_equality(constraint
))
415 for (i
= 0; i
< constraint
->bmap
->n_div
; ++i
) {
416 if (isl_int_is_zero(constraint
->bmap
->div
[i
][0]))
418 if (isl_basic_map_is_div_constraint(constraint
->bmap
,
419 constraint
->line
[0], i
))
426 /* We manually set ISL_BASIC_SET_FINAL instead of calling
427 * isl_basic_map_finalize because we want to keep the position
428 * of the divs and we therefore do not want to throw away redundant divs.
429 * This is arguably a bit fragile.
431 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
432 __isl_take isl_constraint
*constraint
)
435 struct isl_basic_map
*bmap
;
442 if (constraint
->bmap
->n_eq
== 1 && constraint
->bmap
->n_ineq
== 0) {
443 bmap
= isl_basic_map_copy(constraint
->bmap
);
444 isl_constraint_free(constraint
);
448 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
449 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
450 bmap
= isl_basic_map_cow(bmap
);
451 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
452 if (isl_constraint_is_equality(constraint
)) {
453 k
= isl_basic_map_alloc_equality(bmap
);
459 k
= isl_basic_map_alloc_inequality(bmap
);
464 total
= isl_basic_map_total_dim(bmap
);
465 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
466 isl_constraint_free(constraint
);
468 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
471 isl_constraint_free(constraint
);
472 isl_basic_map_free(bmap
);
476 struct isl_basic_set
*isl_basic_set_from_constraint(
477 struct isl_constraint
*constraint
)
482 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
483 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
485 isl_constraint_free(constraint
);
489 int isl_basic_map_has_defining_equality(
490 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
491 __isl_give isl_constraint
**c
)
499 offset
= basic_map_offset(bmap
, type
);
500 total
= isl_basic_map_total_dim(bmap
);
501 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
502 for (i
= 0; i
< bmap
->n_eq
; ++i
)
503 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
504 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
505 1+total
-offset
-pos
-1) == -1) {
506 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
513 int isl_basic_set_has_defining_equality(
514 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
515 __isl_give isl_constraint
**c
)
517 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
521 int isl_basic_set_has_defining_inequalities(
522 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
523 struct isl_constraint
**lower
,
524 struct isl_constraint
**upper
)
530 isl_int
**lower_line
, **upper_line
;
534 offset
= basic_set_offset(bset
, type
);
535 total
= isl_basic_set_total_dim(bset
);
536 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
538 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
539 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
541 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
543 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
545 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
546 1+total
-offset
-pos
-1) != -1)
548 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
549 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
552 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
553 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
556 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
557 lower_line
= &bset
->ineq
[i
];
558 upper_line
= &bset
->ineq
[j
];
560 lower_line
= &bset
->ineq
[j
];
561 upper_line
= &bset
->ineq
[i
];
563 *lower
= isl_basic_set_constraint(
564 isl_basic_set_copy(bset
), lower_line
);
565 *upper
= isl_basic_set_constraint(
566 isl_basic_set_copy(bset
), upper_line
);
577 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
578 * (in "a" and "b"), add a constraint to "bset" that ensures that the
579 * bound implied by "a" is (strictly) larger than the bound implied by "b".
581 * If both constraints imply lower bounds, then this means that "a" is
582 * active in the result.
583 * If both constraints imply upper bounds, then this means that "b" is
584 * active in the result.
586 static __isl_give isl_basic_set
*add_larger_bound_constraint(
587 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
588 unsigned abs_pos
, int strict
)
594 k
= isl_basic_set_alloc_inequality(bset
);
598 total
= isl_basic_set_dim(bset
, isl_dim_all
);
601 isl_int_neg(t
, b
[1 + abs_pos
]);
603 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
604 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
605 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
609 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
615 isl_basic_set_free(bset
);
619 /* Add constraints to "context" that ensure that "u" is the smallest
620 * (and therefore active) upper bound on "abs_pos" in "bset" and return
621 * the resulting basic set.
623 static __isl_give isl_basic_set
*set_smallest_upper_bound(
624 __isl_keep isl_basic_set
*context
,
625 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
629 context
= isl_basic_set_copy(context
);
630 context
= isl_basic_set_cow(context
);
632 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
634 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
637 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
639 context
= add_larger_bound_constraint(context
,
640 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
643 context
= isl_basic_set_simplify(context
);
644 context
= isl_basic_set_finalize(context
);
649 /* Add constraints to "context" that ensure that "u" is the largest
650 * (and therefore active) upper bound on "abs_pos" in "bset" and return
651 * the resulting basic set.
653 static __isl_give isl_basic_set
*set_largest_lower_bound(
654 __isl_keep isl_basic_set
*context
,
655 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
659 context
= isl_basic_set_copy(context
);
660 context
= isl_basic_set_cow(context
);
662 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
664 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
667 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
669 context
= add_larger_bound_constraint(context
,
670 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
673 context
= isl_basic_set_simplify(context
);
674 context
= isl_basic_set_finalize(context
);
679 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
680 enum isl_dim_type type
, unsigned abs_pos
,
681 __isl_take isl_basic_set
*context
, int n_upper
,
682 int (*fn
)(__isl_take isl_constraint
*lower
,
683 __isl_take isl_constraint
*upper
,
684 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
686 isl_basic_set
*context_i
;
687 isl_constraint
*upper
= NULL
;
690 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
691 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
694 context_i
= set_smallest_upper_bound(context
, bset
,
695 abs_pos
, n_upper
, i
);
696 if (isl_basic_set_is_empty(context_i
)) {
697 isl_basic_set_free(context_i
);
700 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
702 if (!upper
|| !context_i
)
704 if (fn(NULL
, upper
, context_i
, user
) < 0)
708 isl_basic_set_free(context
);
710 if (i
< bset
->n_ineq
)
715 isl_constraint_free(upper
);
716 isl_basic_set_free(context_i
);
717 isl_basic_set_free(context
);
721 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
722 enum isl_dim_type type
, unsigned abs_pos
,
723 __isl_take isl_basic_set
*context
, int n_lower
,
724 int (*fn
)(__isl_take isl_constraint
*lower
,
725 __isl_take isl_constraint
*upper
,
726 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
728 isl_basic_set
*context_i
;
729 isl_constraint
*lower
= NULL
;
732 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
733 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
736 context_i
= set_largest_lower_bound(context
, bset
,
737 abs_pos
, n_lower
, i
);
738 if (isl_basic_set_is_empty(context_i
)) {
739 isl_basic_set_free(context_i
);
742 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
744 if (!lower
|| !context_i
)
746 if (fn(lower
, NULL
, context_i
, user
) < 0)
750 isl_basic_set_free(context
);
752 if (i
< bset
->n_ineq
)
757 isl_constraint_free(lower
);
758 isl_basic_set_free(context_i
);
759 isl_basic_set_free(context
);
763 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
764 enum isl_dim_type type
, unsigned abs_pos
,
765 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
766 int (*fn
)(__isl_take isl_constraint
*lower
,
767 __isl_take isl_constraint
*upper
,
768 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
770 isl_basic_set
*context_i
, *context_j
;
771 isl_constraint
*lower
= NULL
;
772 isl_constraint
*upper
= NULL
;
775 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
776 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
779 context_i
= set_largest_lower_bound(context
, bset
,
780 abs_pos
, n_lower
, i
);
781 if (isl_basic_set_is_empty(context_i
)) {
782 isl_basic_set_free(context_i
);
786 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
787 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
790 context_j
= set_smallest_upper_bound(context_i
, bset
,
791 abs_pos
, n_upper
, j
);
792 context_j
= isl_basic_set_extend_constraints(context_j
,
794 context_j
= add_larger_bound_constraint(context_j
,
795 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
796 context_j
= isl_basic_set_simplify(context_j
);
797 context_j
= isl_basic_set_finalize(context_j
);
798 if (isl_basic_set_is_empty(context_j
)) {
799 isl_basic_set_free(context_j
);
802 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
804 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
806 if (!lower
|| !upper
|| !context_j
)
808 if (fn(lower
, upper
, context_j
, user
) < 0)
812 isl_basic_set_free(context_i
);
814 if (j
< bset
->n_ineq
)
818 isl_basic_set_free(context
);
820 if (i
< bset
->n_ineq
)
825 isl_constraint_free(lower
);
826 isl_constraint_free(upper
);
827 isl_basic_set_free(context_i
);
828 isl_basic_set_free(context_j
);
829 isl_basic_set_free(context
);
833 /* For each pair of lower and upper bounds on the variable "pos"
834 * of type "type", call "fn" with these lower and upper bounds and the
835 * set of constraints on the remaining variables where these bounds
836 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
838 * If the designated variable is equal to an affine combination of the
839 * other variables then fn is called with both lower and upper
840 * set to the corresponding equality.
842 * If there is no lower (or upper) bound, then NULL is passed
843 * as the corresponding bound.
845 * We first check if the variable is involved in any equality.
846 * If not, we count the number of lower and upper bounds and
849 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
850 enum isl_dim_type type
, unsigned pos
,
851 int (*fn
)(__isl_take isl_constraint
*lower
,
852 __isl_take isl_constraint
*upper
,
853 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
856 isl_constraint
*lower
= NULL
;
857 isl_constraint
*upper
= NULL
;
858 isl_basic_set
*context
= NULL
;
860 int n_lower
, n_upper
;
864 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
865 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
869 if (type
== isl_dim_set
)
870 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
872 for (i
= 0; i
< bset
->n_eq
; ++i
) {
873 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
876 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
878 upper
= isl_constraint_copy(lower
);
879 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
881 if (!lower
|| !upper
|| !context
)
883 return fn(lower
, upper
, context
, user
);
888 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
889 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
891 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
895 context
= isl_basic_set_copy(bset
);
896 context
= isl_basic_set_cow(context
);
899 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
900 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
901 isl_basic_set_drop_inequality(context
, i
);
903 context
= isl_basic_set_drop(context
, type
, pos
, 1);
904 if (!n_lower
&& !n_upper
)
905 return fn(NULL
, NULL
, context
, user
);
907 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
910 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
912 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
915 isl_constraint_free(lower
);
916 isl_constraint_free(upper
);
917 isl_basic_set_free(context
);