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>
16 #include <isl_div_private.h>
18 #include <isl_aff_private.h>
20 isl_ctx
*isl_constraint_get_ctx(__isl_keep isl_constraint
*c
)
22 return c
? c
->ctx
: NULL
;
25 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
27 return isl_basic_map_dim(c
->bmap
, type
);
30 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
32 struct isl_dim
*dim
= c
->bmap
->dim
;
34 case isl_dim_param
: return 1;
35 case isl_dim_in
: return 1 + dim
->nparam
;
36 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
37 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
42 static unsigned basic_map_offset(__isl_keep isl_basic_map
*bmap
,
43 enum isl_dim_type type
)
45 return type
== isl_dim_div
? 1 + isl_dim_total(bmap
->dim
)
46 : 1 + isl_dim_offset(bmap
->dim
, type
);
49 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
50 enum isl_dim_type type
)
52 struct isl_dim
*dim
= bset
->dim
;
54 case isl_dim_param
: return 1;
55 case isl_dim_in
: return 1 + dim
->nparam
;
56 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
57 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
62 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
65 struct isl_constraint
*constraint
;
70 constraint
= isl_alloc_type(bmap
->ctx
, struct isl_constraint
);
74 constraint
->ctx
= bmap
->ctx
;
75 isl_ctx_ref(constraint
->ctx
);
77 constraint
->bmap
= bmap
;
78 constraint
->line
= line
;
82 isl_basic_map_free(bmap
);
86 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
89 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
92 struct isl_constraint
*isl_equality_alloc(struct isl_dim
*dim
)
94 struct isl_basic_map
*bmap
;
99 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
103 isl_basic_map_alloc_equality(bmap
);
104 isl_seq_clr(bmap
->eq
[0], 1 + isl_basic_map_total_dim(bmap
));
105 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
108 struct isl_constraint
*isl_inequality_alloc(struct isl_dim
*dim
)
110 struct isl_basic_map
*bmap
;
115 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 1);
119 isl_basic_map_alloc_inequality(bmap
);
120 isl_seq_clr(bmap
->ineq
[0], 1 + isl_basic_map_total_dim(bmap
));
121 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
124 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
126 struct isl_basic_map
*bmap
;
133 eq
= c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
;
134 i
= eq
? c
->line
- c
->bmap
->eq
: c
->line
- c
->bmap
->ineq
;
135 bmap
= isl_basic_map_copy(c
->bmap
);
138 return isl_basic_map_constraint(bmap
, eq
? bmap
->eq
+ i
: bmap
->ineq
+ i
);
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 void isl_constraint_free(struct isl_constraint
*c
)
169 isl_basic_map_free(c
->bmap
);
170 isl_ctx_deref(c
->ctx
);
174 __isl_give isl_constraint
*isl_basic_map_first_constraint(
175 __isl_take isl_basic_map
*bmap
)
181 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
183 if (bmap
->n_ineq
> 0)
184 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
186 isl_basic_map_free(bmap
);
190 __isl_give isl_constraint
*isl_basic_set_first_constraint(
191 __isl_take isl_basic_set
*bset
)
193 return isl_basic_map_first_constraint((struct isl_basic_map
*)bset
);
196 struct isl_constraint
*isl_constraint_next(struct isl_constraint
*c
)
198 c
= isl_constraint_cow(c
);
199 if (c
->line
>= c
->bmap
->eq
) {
201 if (c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
)
203 c
->line
= c
->bmap
->ineq
;
206 if (c
->line
< c
->bmap
->ineq
+ c
->bmap
->n_ineq
)
208 isl_constraint_free(c
);
212 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
213 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
216 struct isl_constraint
*c
;
221 isl_assert(bmap
->ctx
, ISL_F_ISSET(bmap
, ISL_BASIC_MAP_FINAL
),
224 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
225 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
233 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
234 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
245 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
246 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
248 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
251 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
252 struct isl_constraint
*constraint2
)
254 if (!constraint1
|| !constraint2
)
256 return constraint1
->bmap
== constraint2
->bmap
&&
257 constraint1
->line
== constraint2
->line
;
260 struct isl_basic_map
*isl_basic_map_add_constraint(
261 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
263 if (!bmap
|| !constraint
)
266 isl_assert(constraint
->ctx
,
267 isl_dim_equal(bmap
->dim
, constraint
->bmap
->dim
), goto error
);
269 bmap
= isl_basic_map_intersect(bmap
,
270 isl_basic_map_from_constraint(constraint
));
273 isl_basic_map_free(bmap
);
274 isl_constraint_free(constraint
);
278 struct isl_basic_set
*isl_basic_set_add_constraint(
279 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
281 return (struct isl_basic_set
*)
282 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
286 __isl_give isl_map
*isl_map_add_constraint(__isl_take isl_map
*map
,
287 __isl_take isl_constraint
*constraint
)
291 bmap
= isl_basic_map_from_constraint(constraint
);
292 map
= isl_map_intersect(map
, isl_map_from_basic_map(bmap
));
297 __isl_give isl_set
*isl_set_add_constraint(__isl_take isl_set
*set
,
298 __isl_take isl_constraint
*constraint
)
300 return isl_map_add_constraint(set
, constraint
);
303 struct isl_constraint
*isl_constraint_add_div(struct isl_constraint
*constraint
,
304 struct isl_div
*div
, int *pos
)
306 if (!constraint
|| !div
)
309 isl_assert(constraint
->ctx
,
310 isl_dim_equal(div
->bmap
->dim
, constraint
->bmap
->dim
), goto error
);
311 isl_assert(constraint
->ctx
,
312 constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1, goto error
);
314 constraint
->bmap
= isl_basic_map_cow(constraint
->bmap
);
315 constraint
->bmap
= isl_basic_map_extend_dim(constraint
->bmap
,
316 isl_dim_copy(constraint
->bmap
->dim
), 1, 0, 0);
317 if (!constraint
->bmap
)
319 constraint
->line
= &constraint
->bmap
->ineq
[0];
320 *pos
= isl_basic_map_alloc_div(constraint
->bmap
);
323 isl_seq_cpy(constraint
->bmap
->div
[*pos
], div
->line
[0],
324 1 + 1 + isl_basic_map_total_dim(constraint
->bmap
));
328 isl_constraint_free(constraint
);
333 __isl_give isl_dim
*isl_constraint_get_dim(
334 __isl_keep isl_constraint
*constraint
)
336 return constraint
? isl_basic_map_get_dim(constraint
->bmap
) : NULL
;
339 int isl_constraint_dim(struct isl_constraint
*constraint
,
340 enum isl_dim_type type
)
344 return n(constraint
, type
);
347 int isl_constraint_involves_dims(__isl_keep isl_constraint
*constraint
,
348 enum isl_dim_type type
, unsigned first
, unsigned n
)
353 if (first
+ n
> isl_basic_set_dim(constraint
->bmap
, type
))
354 isl_die(constraint
->ctx
, isl_error_invalid
,
355 "index out of bounds", return -1);
357 first
+= isl_basic_map_offset(constraint
->bmap
, type
);
359 if (isl_seq_first_non_zero(constraint
->line
[0] + first
, n
) >= 0)
365 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint
*constraint
,
366 enum isl_dim_type type
, unsigned pos
)
369 isl_basic_map_get_dim_name(constraint
->bmap
, type
, pos
) : NULL
;
372 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
376 isl_int_set(*v
, constraint
->line
[0][0]);
379 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
380 enum isl_dim_type type
, int pos
, isl_int
*v
)
385 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
386 isl_int_set(*v
, constraint
->line
[0][offset(constraint
, type
) + pos
]);
389 struct isl_div
*isl_constraint_div(struct isl_constraint
*constraint
, int pos
)
394 isl_assert(constraint
->ctx
, pos
< n(constraint
, isl_dim_div
),
396 isl_assert(constraint
->ctx
,
397 !isl_int_is_zero(constraint
->bmap
->div
[pos
][0]), return NULL
);
398 return isl_basic_map_div(isl_basic_map_copy(constraint
->bmap
), pos
);
401 void isl_constraint_set_constant(struct isl_constraint
*constraint
, isl_int v
)
405 isl_int_set(constraint
->line
[0][0], v
);
408 void isl_constraint_set_constant_si(__isl_keep isl_constraint
*constraint
,
413 isl_int_set_si(constraint
->line
[0][0], v
);
416 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
417 enum isl_dim_type type
, int pos
, isl_int v
)
422 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
423 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
426 void isl_constraint_set_coefficient_si(__isl_keep isl_constraint
*constraint
,
427 enum isl_dim_type type
, int pos
, int v
)
432 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
433 isl_int_set_si(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
436 void isl_constraint_clear(struct isl_constraint
*constraint
)
442 total
= isl_basic_map_total_dim(constraint
->bmap
);
443 isl_seq_clr(constraint
->line
[0], 1 + total
);
446 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
453 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
455 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
456 total
= isl_basic_map_total_dim(constraint
->bmap
);
457 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
458 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
459 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
462 isl_constraint_free(constraint
);
466 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
470 return constraint
->line
>= constraint
->bmap
->eq
;
473 int isl_constraint_is_div_constraint(__isl_keep isl_constraint
*constraint
)
479 if (isl_constraint_is_equality(constraint
))
481 for (i
= 0; i
< constraint
->bmap
->n_div
; ++i
) {
482 if (isl_int_is_zero(constraint
->bmap
->div
[i
][0]))
484 if (isl_basic_map_is_div_constraint(constraint
->bmap
,
485 constraint
->line
[0], i
))
492 /* We manually set ISL_BASIC_SET_FINAL instead of calling
493 * isl_basic_map_finalize because we want to keep the position
494 * of the divs and we therefore do not want to throw away redundant divs.
495 * This is arguably a bit fragile.
497 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
498 __isl_take isl_constraint
*constraint
)
501 struct isl_basic_map
*bmap
;
508 if (constraint
->bmap
->n_eq
== 1 && constraint
->bmap
->n_ineq
== 0) {
509 bmap
= isl_basic_map_copy(constraint
->bmap
);
510 isl_constraint_free(constraint
);
514 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
515 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
516 bmap
= isl_basic_map_cow(bmap
);
517 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
518 if (isl_constraint_is_equality(constraint
)) {
519 k
= isl_basic_map_alloc_equality(bmap
);
525 k
= isl_basic_map_alloc_inequality(bmap
);
530 total
= isl_basic_map_total_dim(bmap
);
531 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
532 isl_constraint_free(constraint
);
534 ISL_F_SET(bmap
, ISL_BASIC_SET_FINAL
);
537 isl_constraint_free(constraint
);
538 isl_basic_map_free(bmap
);
542 struct isl_basic_set
*isl_basic_set_from_constraint(
543 struct isl_constraint
*constraint
)
548 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
549 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
551 isl_constraint_free(constraint
);
555 int isl_basic_map_has_defining_equality(
556 __isl_keep isl_basic_map
*bmap
, enum isl_dim_type type
, int pos
,
557 __isl_give isl_constraint
**c
)
565 offset
= basic_map_offset(bmap
, type
);
566 total
= isl_basic_map_total_dim(bmap
);
567 isl_assert(bmap
->ctx
, pos
< isl_basic_map_dim(bmap
, type
), return -1);
568 for (i
= 0; i
< bmap
->n_eq
; ++i
)
569 if (!isl_int_is_zero(bmap
->eq
[i
][offset
+ pos
]) &&
570 isl_seq_first_non_zero(bmap
->eq
[i
]+offset
+pos
+1,
571 1+total
-offset
-pos
-1) == -1) {
572 *c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
579 int isl_basic_set_has_defining_equality(
580 __isl_keep isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
581 __isl_give isl_constraint
**c
)
583 return isl_basic_map_has_defining_equality((isl_basic_map
*)bset
,
587 int isl_basic_set_has_defining_inequalities(
588 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
589 struct isl_constraint
**lower
,
590 struct isl_constraint
**upper
)
596 isl_int
**lower_line
, **upper_line
;
600 offset
= basic_set_offset(bset
, type
);
601 total
= isl_basic_set_total_dim(bset
);
602 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
604 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
605 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
607 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
609 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
611 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
612 1+total
-offset
-pos
-1) != -1)
614 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
615 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
618 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
619 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
622 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
623 lower_line
= &bset
->ineq
[i
];
624 upper_line
= &bset
->ineq
[j
];
626 lower_line
= &bset
->ineq
[j
];
627 upper_line
= &bset
->ineq
[i
];
629 *lower
= isl_basic_set_constraint(
630 isl_basic_set_copy(bset
), lower_line
);
631 *upper
= isl_basic_set_constraint(
632 isl_basic_set_copy(bset
), upper_line
);
643 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
644 * (in "a" and "b"), add a constraint to "bset" that ensures that the
645 * bound implied by "a" is (strictly) larger than the bound implied by "b".
647 * If both constraints imply lower bounds, then this means that "a" is
648 * active in the result.
649 * If both constraints imply upper bounds, then this means that "b" is
650 * active in the result.
652 static __isl_give isl_basic_set
*add_larger_bound_constraint(
653 __isl_take isl_basic_set
*bset
, isl_int
*a
, isl_int
*b
,
654 unsigned abs_pos
, int strict
)
660 k
= isl_basic_set_alloc_inequality(bset
);
664 total
= isl_basic_set_dim(bset
, isl_dim_all
);
667 isl_int_neg(t
, b
[1 + abs_pos
]);
669 isl_seq_combine(bset
->ineq
[k
], t
, a
, a
[1 + abs_pos
], b
, 1 + abs_pos
);
670 isl_seq_combine(bset
->ineq
[k
] + 1 + abs_pos
,
671 t
, a
+ 1 + abs_pos
+ 1, a
[1 + abs_pos
], b
+ 1 + abs_pos
+ 1,
675 isl_int_sub_ui(bset
->ineq
[k
][0], bset
->ineq
[k
][0], 1);
681 isl_basic_set_free(bset
);
685 /* Add constraints to "context" that ensure that "u" is the smallest
686 * (and therefore active) upper bound on "abs_pos" in "bset" and return
687 * the resulting basic set.
689 static __isl_give isl_basic_set
*set_smallest_upper_bound(
690 __isl_keep isl_basic_set
*context
,
691 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_upper
, int u
)
695 context
= isl_basic_set_copy(context
);
696 context
= isl_basic_set_cow(context
);
698 context
= isl_basic_set_extend_constraints(context
, 0, n_upper
- 1);
700 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
703 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
705 context
= add_larger_bound_constraint(context
,
706 bset
->ineq
[j
], bset
->ineq
[u
], abs_pos
, j
> u
);
709 context
= isl_basic_set_simplify(context
);
710 context
= isl_basic_set_finalize(context
);
715 /* Add constraints to "context" that ensure that "u" is the largest
716 * (and therefore active) upper bound on "abs_pos" in "bset" and return
717 * the resulting basic set.
719 static __isl_give isl_basic_set
*set_largest_lower_bound(
720 __isl_keep isl_basic_set
*context
,
721 __isl_keep isl_basic_set
*bset
, unsigned abs_pos
, int n_lower
, int l
)
725 context
= isl_basic_set_copy(context
);
726 context
= isl_basic_set_cow(context
);
728 context
= isl_basic_set_extend_constraints(context
, 0, n_lower
- 1);
730 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
733 if (!isl_int_is_pos(bset
->ineq
[j
][1 + abs_pos
]))
735 context
= add_larger_bound_constraint(context
,
736 bset
->ineq
[l
], bset
->ineq
[j
], abs_pos
, j
> l
);
739 context
= isl_basic_set_simplify(context
);
740 context
= isl_basic_set_finalize(context
);
745 static int foreach_upper_bound(__isl_keep isl_basic_set
*bset
,
746 enum isl_dim_type type
, unsigned abs_pos
,
747 __isl_take isl_basic_set
*context
, int n_upper
,
748 int (*fn
)(__isl_take isl_constraint
*lower
,
749 __isl_take isl_constraint
*upper
,
750 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
752 isl_basic_set
*context_i
;
753 isl_constraint
*upper
= NULL
;
756 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
757 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
760 context_i
= set_smallest_upper_bound(context
, bset
,
761 abs_pos
, n_upper
, i
);
762 if (isl_basic_set_is_empty(context_i
)) {
763 isl_basic_set_free(context_i
);
766 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
768 if (!upper
|| !context_i
)
770 if (fn(NULL
, upper
, context_i
, user
) < 0)
774 isl_basic_set_free(context
);
776 if (i
< bset
->n_ineq
)
781 isl_constraint_free(upper
);
782 isl_basic_set_free(context_i
);
783 isl_basic_set_free(context
);
787 static int foreach_lower_bound(__isl_keep isl_basic_set
*bset
,
788 enum isl_dim_type type
, unsigned abs_pos
,
789 __isl_take isl_basic_set
*context
, int n_lower
,
790 int (*fn
)(__isl_take isl_constraint
*lower
,
791 __isl_take isl_constraint
*upper
,
792 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
794 isl_basic_set
*context_i
;
795 isl_constraint
*lower
= NULL
;
798 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
799 if (isl_int_is_zero(bset
->ineq
[i
][1 + abs_pos
]))
802 context_i
= set_largest_lower_bound(context
, bset
,
803 abs_pos
, n_lower
, i
);
804 if (isl_basic_set_is_empty(context_i
)) {
805 isl_basic_set_free(context_i
);
808 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
810 if (!lower
|| !context_i
)
812 if (fn(lower
, NULL
, context_i
, user
) < 0)
816 isl_basic_set_free(context
);
818 if (i
< bset
->n_ineq
)
823 isl_constraint_free(lower
);
824 isl_basic_set_free(context_i
);
825 isl_basic_set_free(context
);
829 static int foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
830 enum isl_dim_type type
, unsigned abs_pos
,
831 __isl_take isl_basic_set
*context
, int n_lower
, int n_upper
,
832 int (*fn
)(__isl_take isl_constraint
*lower
,
833 __isl_take isl_constraint
*upper
,
834 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
836 isl_basic_set
*context_i
, *context_j
;
837 isl_constraint
*lower
= NULL
;
838 isl_constraint
*upper
= NULL
;
841 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
842 if (!isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
845 context_i
= set_largest_lower_bound(context
, bset
,
846 abs_pos
, n_lower
, i
);
847 if (isl_basic_set_is_empty(context_i
)) {
848 isl_basic_set_free(context_i
);
852 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
853 if (!isl_int_is_neg(bset
->ineq
[j
][1 + abs_pos
]))
856 context_j
= set_smallest_upper_bound(context_i
, bset
,
857 abs_pos
, n_upper
, j
);
858 context_j
= isl_basic_set_extend_constraints(context_j
,
860 context_j
= add_larger_bound_constraint(context_j
,
861 bset
->ineq
[i
], bset
->ineq
[j
], abs_pos
, 0);
862 context_j
= isl_basic_set_simplify(context_j
);
863 context_j
= isl_basic_set_finalize(context_j
);
864 if (isl_basic_set_is_empty(context_j
)) {
865 isl_basic_set_free(context_j
);
868 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
870 upper
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
872 if (!lower
|| !upper
|| !context_j
)
874 if (fn(lower
, upper
, context_j
, user
) < 0)
878 isl_basic_set_free(context_i
);
880 if (j
< bset
->n_ineq
)
884 isl_basic_set_free(context
);
886 if (i
< bset
->n_ineq
)
891 isl_constraint_free(lower
);
892 isl_constraint_free(upper
);
893 isl_basic_set_free(context_i
);
894 isl_basic_set_free(context_j
);
895 isl_basic_set_free(context
);
899 /* For each pair of lower and upper bounds on the variable "pos"
900 * of type "type", call "fn" with these lower and upper bounds and the
901 * set of constraints on the remaining variables where these bounds
902 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
904 * If the designated variable is equal to an affine combination of the
905 * other variables then fn is called with both lower and upper
906 * set to the corresponding equality.
908 * If there is no lower (or upper) bound, then NULL is passed
909 * as the corresponding bound.
911 * We first check if the variable is involved in any equality.
912 * If not, we count the number of lower and upper bounds and
915 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set
*bset
,
916 enum isl_dim_type type
, unsigned pos
,
917 int (*fn
)(__isl_take isl_constraint
*lower
,
918 __isl_take isl_constraint
*upper
,
919 __isl_take isl_basic_set
*bset
, void *user
), void *user
)
922 isl_constraint
*lower
= NULL
;
923 isl_constraint
*upper
= NULL
;
924 isl_basic_set
*context
= NULL
;
926 int n_lower
, n_upper
;
930 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
931 isl_assert(bset
->ctx
, type
== isl_dim_param
|| type
== isl_dim_set
,
935 if (type
== isl_dim_set
)
936 abs_pos
+= isl_basic_set_dim(bset
, isl_dim_param
);
938 for (i
= 0; i
< bset
->n_eq
; ++i
) {
939 if (isl_int_is_zero(bset
->eq
[i
][1 + abs_pos
]))
942 lower
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
944 upper
= isl_constraint_copy(lower
);
945 context
= isl_basic_set_remove_dims(isl_basic_set_copy(bset
),
947 if (!lower
|| !upper
|| !context
)
949 return fn(lower
, upper
, context
, user
);
954 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
955 if (isl_int_is_pos(bset
->ineq
[i
][1 + abs_pos
]))
957 else if (isl_int_is_neg(bset
->ineq
[i
][1 + abs_pos
]))
961 context
= isl_basic_set_copy(bset
);
962 context
= isl_basic_set_cow(context
);
965 for (i
= context
->n_ineq
- 1; i
>= 0; --i
)
966 if (!isl_int_is_zero(context
->ineq
[i
][1 + abs_pos
]))
967 isl_basic_set_drop_inequality(context
, i
);
969 context
= isl_basic_set_drop(context
, type
, pos
, 1);
970 if (!n_lower
&& !n_upper
)
971 return fn(NULL
, NULL
, context
, user
);
973 return foreach_upper_bound(bset
, type
, abs_pos
, context
, n_upper
,
976 return foreach_lower_bound(bset
, type
, abs_pos
, context
, n_lower
,
978 return foreach_bound_pair(bset
, type
, abs_pos
, context
, n_lower
, n_upper
,
981 isl_constraint_free(lower
);
982 isl_constraint_free(upper
);
983 isl_basic_set_free(context
);
987 __isl_give isl_aff
*isl_constraint_get_bound(
988 __isl_keep isl_constraint
*constraint
, enum isl_dim_type type
, int pos
)
995 if (pos
>= isl_basic_set_dim(constraint
->bmap
, type
))
996 isl_die(constraint
->ctx
, isl_error_invalid
,
997 "index out of bounds", return NULL
);
998 if (!isl_basic_map_may_be_set(constraint
->bmap
))
999 isl_die(constraint
->ctx
, isl_error_invalid
,
1000 "not a set constraint", return NULL
);
1002 pos
+= offset(constraint
, type
);
1003 if (isl_int_is_zero(constraint
->line
[0][pos
]))
1004 isl_die(constraint
->ctx
, isl_error_invalid
,
1005 "constraint does not define a bound on given dimension",
1008 ls
= isl_basic_set_get_local_space(constraint
->bmap
);
1009 aff
= isl_aff_alloc(ls
);
1013 if (isl_int_is_neg(constraint
->line
[0][pos
]))
1014 isl_seq_cpy(aff
->v
->el
+ 1, constraint
->line
[0],
1017 isl_seq_neg(aff
->v
->el
+ 1, constraint
->line
[0],
1019 isl_int_set_si(aff
->v
->el
[1 + pos
], 0);
1020 isl_int_abs(aff
->v
->el
[0], constraint
->line
[0][pos
]);
1025 /* Construct an equality constraint equating the given affine expression
1028 __isl_give isl_constraint
*isl_equality_from_aff(__isl_take isl_aff
*aff
)
1031 isl_basic_set
*bset
;
1036 bset
= isl_basic_set_from_local_space(isl_aff_get_local_space(aff
));
1037 bset
= isl_basic_set_extend_constraints(bset
, 1, 0);
1038 k
= isl_basic_set_alloc_equality(bset
);
1042 isl_seq_cpy(bset
->eq
[k
], aff
->v
->el
+ 1, aff
->v
->size
- 1);
1045 return isl_basic_set_constraint(bset
, &bset
->eq
[k
]);
1048 isl_basic_set_free(bset
);
1052 /* Construct an inequality constraint enforcing the given affine expression
1053 * to be non-negative.
1055 __isl_give isl_constraint
*isl_inequality_from_aff(__isl_take isl_aff
*aff
)
1058 isl_basic_set
*bset
;
1063 bset
= isl_basic_set_from_local_space(isl_aff_get_local_space(aff
));
1064 bset
= isl_basic_set_extend_constraints(bset
, 0, 1);
1065 k
= isl_basic_set_alloc_inequality(bset
);
1069 isl_seq_cpy(bset
->ineq
[k
], aff
->v
->el
+ 1, aff
->v
->size
- 1);
1072 return isl_basic_set_constraint(bset
, &bset
->ineq
[k
]);
1075 isl_basic_set_free(bset
);