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.h>
15 #include <isl_dim_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_constant_si(__isl_keep isl_constraint
*constraint
,
365 isl_int_set_si(constraint
->line
[0][0], v
);
368 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
369 enum isl_dim_type type
, int pos
, isl_int v
)
374 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
375 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
378 void isl_constraint_set_coefficient_si(__isl_keep isl_constraint
*constraint
,
379 enum isl_dim_type type
, int pos
, int v
)
384 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
385 isl_int_set_si(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
388 void isl_constraint_clear(struct isl_constraint
*constraint
)
394 total
= isl_basic_map_total_dim(constraint
->bmap
);
395 isl_seq_clr(constraint
->line
[0], 1 + total
);
398 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
405 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
407 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
408 total
= isl_basic_map_total_dim(constraint
->bmap
);
409 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
410 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
411 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
414 isl_constraint_free(constraint
);
418 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
422 return constraint
->line
>= constraint
->bmap
->eq
;
425 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
431 if (isl_constraint_is_equality(constraint
))
433 for (i
= 0; i
< constraint
->bmap
->n_div
; ++i
) {
434 if (isl_int_is_zero(constraint
->bmap
->div
[i
][0]))
436 if (isl_basic_map_is_div_constraint(constraint
->bmap
,
437 constraint
->line
[0], i
))
444 /* We manually set ISL_BASIC_SET_FINAL instead of calling
445 * isl_basic_map_finalize because we want to keep the position
446 * of the divs and we therefore do not want to throw away redundant divs.
447 * This is arguably a bit fragile.
449 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
450 __isl_take isl_constraint
*constraint
)
453 struct isl_basic_map
*bmap
;
460 if (constraint
->bmap
->n_eq
== 1 && constraint
->bmap
->n_ineq
== 0) {
461 bmap
= isl_basic_map_copy(constraint
->bmap
);
462 isl_constraint_free(constraint
);
466 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
467 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
468 bmap
= isl_basic_map_cow(bmap
);
469 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
470 if (isl_constraint_is_equality(constraint
)) {
471 k
= isl_basic_map_alloc_equality(bmap
);
477 k
= isl_basic_map_alloc_inequality(bmap
);
482 total
= isl_basic_map_total_dim(bmap
);
483 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
484 isl_constraint_free(constraint
);
486 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
489 isl_constraint_free(constraint
);
490 isl_basic_map_free(bmap
);
494 struct isl_basic_set
*isl_basic_set_from_constraint(
495 struct isl_constraint
*constraint
)
500 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
501 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
503 isl_constraint_free(constraint
);
507 int isl_basic_map_has_defining_equality(
508 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
509 __isl_give isl_constraint
**c
)
517 offset
= basic_map_offset(bmap
, type
);
518 total
= isl_basic_map_total_dim(bmap
);
519 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
520 for (i
= 0; i
< bmap
->n_eq
; ++i
)
521 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
522 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
523 1+total
-offset
-pos
-1) == -1) {
524 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
531 int isl_basic_set_has_defining_equality(
532 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
533 __isl_give isl_constraint
**c
)
535 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
539 int isl_basic_set_has_defining_inequalities(
540 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
541 struct isl_constraint
**lower
,
542 struct isl_constraint
**upper
)
548 isl_int
**lower_line
, **upper_line
;
552 offset
= basic_set_offset(bset
, type
);
553 total
= isl_basic_set_total_dim(bset
);
554 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
556 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
557 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
559 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
561 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
563 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
564 1+total
-offset
-pos
-1) != -1)
566 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
567 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
570 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
571 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
574 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
575 lower_line
= &bset
->ineq
[i
];
576 upper_line
= &bset
->ineq
[j
];
578 lower_line
= &bset
->ineq
[j
];
579 upper_line
= &bset
->ineq
[i
];
581 *lower
= isl_basic_set_constraint(
582 isl_basic_set_copy(bset
), lower_line
);
583 *upper
= isl_basic_set_constraint(
584 isl_basic_set_copy(bset
), upper_line
);
595 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
596 * (in "a" and "b"), add a constraint to "bset" that ensures that the
597 * bound implied by "a" is (strictly) larger than the bound implied by "b".
599 * If both constraints imply lower bounds, then this means that "a" is
600 * active in the result.
601 * If both constraints imply upper bounds, then this means that "b" is
602 * active in the result.
604 static __isl_give isl_basic_set
*add_larger_bound_constraint(
605 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
606 unsigned abs_pos
, int strict
)
612 k
= isl_basic_set_alloc_inequality(bset
);
616 total
= isl_basic_set_dim(bset
, isl_dim_all
);
619 isl_int_neg(t
, b
[1 + abs_pos
]);
621 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
622 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
623 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
627 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
633 isl_basic_set_free(bset
);
637 /* Add constraints to "context" that ensure that "u" is the smallest
638 * (and therefore active) upper bound on "abs_pos" in "bset" and return
639 * the resulting basic set.
641 static __isl_give isl_basic_set
*set_smallest_upper_bound(
642 __isl_keep isl_basic_set
*context
,
643 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
647 context
= isl_basic_set_copy(context
);
648 context
= isl_basic_set_cow(context
);
650 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
652 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
655 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
657 context
= add_larger_bound_constraint(context
,
658 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
661 context
= isl_basic_set_simplify(context
);
662 context
= isl_basic_set_finalize(context
);
667 /* Add constraints to "context" that ensure that "u" is the largest
668 * (and therefore active) upper bound on "abs_pos" in "bset" and return
669 * the resulting basic set.
671 static __isl_give isl_basic_set
*set_largest_lower_bound(
672 __isl_keep isl_basic_set
*context
,
673 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
677 context
= isl_basic_set_copy(context
);
678 context
= isl_basic_set_cow(context
);
680 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
682 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
685 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
687 context
= add_larger_bound_constraint(context
,
688 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
691 context
= isl_basic_set_simplify(context
);
692 context
= isl_basic_set_finalize(context
);
697 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
698 enum isl_dim_type type
, unsigned abs_pos
,
699 __isl_take isl_basic_set
*context
, int n_upper
,
700 int (*fn
)(__isl_take isl_constraint
*lower
,
701 __isl_take isl_constraint
*upper
,
702 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
704 isl_basic_set
*context_i
;
705 isl_constraint
*upper
= NULL
;
708 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
709 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
712 context_i
= set_smallest_upper_bound(context
, bset
,
713 abs_pos
, n_upper
, i
);
714 if (isl_basic_set_is_empty(context_i
)) {
715 isl_basic_set_free(context_i
);
718 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
720 if (!upper
|| !context_i
)
722 if (fn(NULL
, upper
, context_i
, user
) < 0)
726 isl_basic_set_free(context
);
728 if (i
< bset
->n_ineq
)
733 isl_constraint_free(upper
);
734 isl_basic_set_free(context_i
);
735 isl_basic_set_free(context
);
739 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
740 enum isl_dim_type type
, unsigned abs_pos
,
741 __isl_take isl_basic_set
*context
, int n_lower
,
742 int (*fn
)(__isl_take isl_constraint
*lower
,
743 __isl_take isl_constraint
*upper
,
744 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
746 isl_basic_set
*context_i
;
747 isl_constraint
*lower
= NULL
;
750 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
751 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
754 context_i
= set_largest_lower_bound(context
, bset
,
755 abs_pos
, n_lower
, i
);
756 if (isl_basic_set_is_empty(context_i
)) {
757 isl_basic_set_free(context_i
);
760 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
762 if (!lower
|| !context_i
)
764 if (fn(lower
, NULL
, context_i
, user
) < 0)
768 isl_basic_set_free(context
);
770 if (i
< bset
->n_ineq
)
775 isl_constraint_free(lower
);
776 isl_basic_set_free(context_i
);
777 isl_basic_set_free(context
);
781 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
782 enum isl_dim_type type
, unsigned abs_pos
,
783 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
784 int (*fn
)(__isl_take isl_constraint
*lower
,
785 __isl_take isl_constraint
*upper
,
786 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
788 isl_basic_set
*context_i
, *context_j
;
789 isl_constraint
*lower
= NULL
;
790 isl_constraint
*upper
= NULL
;
793 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
794 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
797 context_i
= set_largest_lower_bound(context
, bset
,
798 abs_pos
, n_lower
, i
);
799 if (isl_basic_set_is_empty(context_i
)) {
800 isl_basic_set_free(context_i
);
804 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
805 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
808 context_j
= set_smallest_upper_bound(context_i
, bset
,
809 abs_pos
, n_upper
, j
);
810 context_j
= isl_basic_set_extend_constraints(context_j
,
812 context_j
= add_larger_bound_constraint(context_j
,
813 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
814 context_j
= isl_basic_set_simplify(context_j
);
815 context_j
= isl_basic_set_finalize(context_j
);
816 if (isl_basic_set_is_empty(context_j
)) {
817 isl_basic_set_free(context_j
);
820 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
822 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
824 if (!lower
|| !upper
|| !context_j
)
826 if (fn(lower
, upper
, context_j
, user
) < 0)
830 isl_basic_set_free(context_i
);
832 if (j
< bset
->n_ineq
)
836 isl_basic_set_free(context
);
838 if (i
< bset
->n_ineq
)
843 isl_constraint_free(lower
);
844 isl_constraint_free(upper
);
845 isl_basic_set_free(context_i
);
846 isl_basic_set_free(context_j
);
847 isl_basic_set_free(context
);
851 /* For each pair of lower and upper bounds on the variable "pos"
852 * of type "type", call "fn" with these lower and upper bounds and the
853 * set of constraints on the remaining variables where these bounds
854 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
856 * If the designated variable is equal to an affine combination of the
857 * other variables then fn is called with both lower and upper
858 * set to the corresponding equality.
860 * If there is no lower (or upper) bound, then NULL is passed
861 * as the corresponding bound.
863 * We first check if the variable is involved in any equality.
864 * If not, we count the number of lower and upper bounds and
867 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
868 enum isl_dim_type type
, unsigned pos
,
869 int (*fn
)(__isl_take isl_constraint
*lower
,
870 __isl_take isl_constraint
*upper
,
871 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
874 isl_constraint
*lower
= NULL
;
875 isl_constraint
*upper
= NULL
;
876 isl_basic_set
*context
= NULL
;
878 int n_lower
, n_upper
;
882 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
883 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
887 if (type
== isl_dim_set
)
888 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
890 for (i
= 0; i
< bset
->n_eq
; ++i
) {
891 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
894 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
896 upper
= isl_constraint_copy(lower
);
897 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
899 if (!lower
|| !upper
|| !context
)
901 return fn(lower
, upper
, context
, user
);
906 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
907 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
909 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
913 context
= isl_basic_set_copy(bset
);
914 context
= isl_basic_set_cow(context
);
917 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
918 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
919 isl_basic_set_drop_inequality(context
, i
);
921 context
= isl_basic_set_drop(context
, type
, pos
, 1);
922 if (!n_lower
&& !n_upper
)
923 return fn(NULL
, NULL
, context
, user
);
925 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
928 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
930 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
933 isl_constraint_free(lower
);
934 isl_constraint_free(upper
);
935 isl_basic_set_free(context
);