2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_constraint.h>
12 #include "isl_map_private.h"
14 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
16 return isl_basic_map_dim(c
->bmap
, type
);
19 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
21 struct isl_dim
*dim
= c
->bmap
->dim
;
23 case isl_dim_param
: return 1;
24 case isl_dim_in
: return 1 + dim
->nparam
;
25 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
26 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
30 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
31 enum isl_dim_type type
)
33 struct isl_dim
*dim
= bset
->dim
;
35 case isl_dim_param
: return 1;
36 case isl_dim_in
: return 1 + dim
->nparam
;
37 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
38 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
42 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
45 struct isl_constraint
*constraint
;
50 constraint
= isl_alloc_type(bmap
->ctx
, struct isl_constraint
);
54 constraint
->ctx
= bmap
->ctx
;
55 isl_ctx_ref(constraint
->ctx
);
57 constraint
->bmap
= bmap
;
58 constraint
->line
= line
;
62 isl_basic_map_free(bmap
);
66 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
69 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
72 struct isl_constraint
*isl_equality_alloc(struct isl_dim
*dim
)
74 struct isl_basic_map
*bmap
;
79 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
83 isl_basic_map_alloc_equality(bmap
);
84 isl_seq_clr(bmap
->eq
[0], 1 + isl_basic_map_total_dim(bmap
));
85 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
88 struct isl_constraint
*isl_inequality_alloc(struct isl_dim
*dim
)
90 struct isl_basic_map
*bmap
;
95 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 1);
99 isl_basic_map_alloc_inequality(bmap
);
100 isl_seq_clr(bmap
->ineq
[0], 1 + isl_basic_map_total_dim(bmap
));
101 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
104 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
106 struct isl_basic_map
*bmap
;
113 eq
= c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
;
114 i
= eq
? c
->line
- c
->bmap
->eq
: c
->line
- c
->bmap
->ineq
;
115 bmap
= isl_basic_map_copy(c
->bmap
);
118 return isl_basic_map_constraint(bmap
, eq
? bmap
->eq
+ i
: bmap
->ineq
+ i
);
121 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
129 return isl_constraint_dup(c
);
132 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
141 void isl_constraint_free(struct isl_constraint
*c
)
149 isl_basic_map_free(c
->bmap
);
150 isl_ctx_deref(c
->ctx
);
154 __isl_give isl_constraint
*isl_basic_map_first_constraint(
155 __isl_take isl_basic_map
*bmap
)
161 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
163 if (bmap
->n_ineq
> 0)
164 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
166 isl_basic_map_free(bmap
);
170 __isl_give isl_constraint
*isl_basic_set_first_constraint(
171 __isl_take isl_basic_set
*bset
)
173 return isl_basic_map_first_constraint((struct isl_basic_map
*)bset
);
176 struct isl_constraint
*isl_constraint_next(struct isl_constraint
*c
)
178 c
= isl_constraint_cow(c
);
179 if (c
->line
>= c
->bmap
->eq
) {
181 if (c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
)
183 c
->line
= c
->bmap
->ineq
;
186 if (c
->line
< c
->bmap
->ineq
+ c
->bmap
->n_ineq
)
188 isl_constraint_free(c
);
192 int isl_basic_map_foreach_constraint(__isl_keep isl_basic_map
*bmap
,
193 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
196 struct isl_constraint
*c
;
201 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
202 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
210 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
211 c
= isl_basic_map_constraint(isl_basic_map_copy(bmap
),
222 int isl_basic_set_foreach_constraint(__isl_keep isl_basic_set
*bset
,
223 int (*fn
)(__isl_take isl_constraint
*c
, void *user
), void *user
)
225 return isl_basic_map_foreach_constraint((isl_basic_map
*)bset
, fn
, user
);
228 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
229 struct isl_constraint
*constraint2
)
231 if (!constraint1
|| !constraint2
)
233 return constraint1
->bmap
== constraint2
->bmap
&&
234 constraint1
->line
== constraint2
->line
;
237 struct isl_basic_map
*isl_basic_map_add_constraint(
238 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
240 if (!bmap
|| !constraint
)
243 isl_assert(constraint
->ctx
,
244 isl_dim_equal(bmap
->dim
, constraint
->bmap
->dim
), goto error
);
246 bmap
= isl_basic_map_intersect(bmap
,
247 isl_basic_map_from_constraint(constraint
));
250 isl_basic_map_free(bmap
);
251 isl_constraint_free(constraint
);
255 struct isl_basic_set
*isl_basic_set_add_constraint(
256 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
258 return (struct isl_basic_set
*)
259 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
263 struct isl_constraint
*isl_constraint_add_div(struct isl_constraint
*constraint
,
264 struct isl_div
*div
, int *pos
)
266 if (!constraint
|| !div
)
269 isl_assert(constraint
->ctx
,
270 isl_dim_equal(div
->bmap
->dim
, constraint
->bmap
->dim
), goto error
);
271 isl_assert(constraint
->ctx
,
272 constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1, goto error
);
274 constraint
->bmap
= isl_basic_map_cow(constraint
->bmap
);
275 constraint
->bmap
= isl_basic_map_extend_dim(constraint
->bmap
,
276 isl_dim_copy(constraint
->bmap
->dim
), 1, 0, 0);
277 if (!constraint
->bmap
)
279 constraint
->line
= &constraint
->bmap
->eq
[0];
280 *pos
= isl_basic_map_alloc_div(constraint
->bmap
);
283 isl_seq_cpy(constraint
->bmap
->div
[*pos
], div
->line
[0],
284 1 + 1 + isl_basic_map_total_dim(constraint
->bmap
));
288 isl_constraint_free(constraint
);
293 int isl_constraint_dim(struct isl_constraint
*constraint
,
294 enum isl_dim_type type
)
298 return n(constraint
, type
);
301 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
305 isl_int_set(*v
, constraint
->line
[0][0]);
308 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
309 enum isl_dim_type type
, int pos
, isl_int
*v
)
314 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
315 isl_int_set(*v
, constraint
->line
[0][offset(constraint
, type
) + pos
]);
318 struct isl_div
*isl_constraint_div(struct isl_constraint
*constraint
, int pos
)
323 isl_assert(constraint
->ctx
, pos
< n(constraint
, isl_dim_div
),
325 isl_assert(constraint
->ctx
,
326 !isl_int_is_zero(constraint
->bmap
->div
[pos
][0]), return NULL
);
327 return isl_basic_map_div(isl_basic_map_copy(constraint
->bmap
), pos
);
330 void isl_constraint_set_constant(struct isl_constraint
*constraint
, isl_int v
)
334 isl_int_set(constraint
->line
[0][0], v
);
337 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
338 enum isl_dim_type type
, int pos
, isl_int v
)
343 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
344 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
347 void isl_constraint_clear(struct isl_constraint
*constraint
)
353 total
= isl_basic_map_total_dim(constraint
->bmap
);
354 isl_seq_clr(constraint
->line
[0], 1 + total
);
357 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
364 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
366 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
367 total
= isl_basic_map_total_dim(constraint
->bmap
);
368 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
369 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
370 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
373 isl_constraint_free(constraint
);
377 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
381 return constraint
->line
>= constraint
->bmap
->eq
;
384 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
385 __isl_take isl_constraint
*constraint
)
388 struct isl_basic_map
*bmap
;
395 if (constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1) {
396 bmap
= isl_basic_map_copy(constraint
->bmap
);
397 isl_constraint_free(constraint
);
401 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
402 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
403 bmap
= isl_basic_map_cow(bmap
);
404 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
405 if (isl_constraint_is_equality(constraint
)) {
406 k
= isl_basic_map_alloc_equality(bmap
);
412 k
= isl_basic_map_alloc_inequality(bmap
);
417 total
= isl_basic_map_total_dim(bmap
);
418 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
419 isl_constraint_free(constraint
);
422 isl_constraint_free(constraint
);
423 isl_basic_map_free(bmap
);
427 struct isl_basic_set
*isl_basic_set_from_constraint(
428 struct isl_constraint
*constraint
)
433 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
434 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
436 isl_constraint_free(constraint
);
440 int isl_basic_set_has_defining_equality(
441 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
442 struct isl_constraint
**c
)
450 offset
= basic_set_offset(bset
, type
);
451 total
= isl_basic_set_total_dim(bset
);
452 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
453 for (i
= 0; i
< bset
->n_eq
; ++i
)
454 if (!isl_int_is_zero(bset
->eq
[i
][offset
+ pos
]) &&
455 isl_seq_first_non_zero(bset
->eq
[i
]+offset
+pos
+1,
456 1+total
-offset
-pos
-1) == -1) {
457 *c
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
464 int isl_basic_set_has_defining_inequalities(
465 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
466 struct isl_constraint
**lower
,
467 struct isl_constraint
**upper
)
473 isl_int
**lower_line
, **upper_line
;
477 offset
= basic_set_offset(bset
, type
);
478 total
= isl_basic_set_total_dim(bset
);
479 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
481 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
482 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
484 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
486 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
488 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
489 1+total
-offset
-pos
-1) != -1)
491 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
492 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
495 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
496 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
499 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
500 lower_line
= &bset
->ineq
[i
];
501 upper_line
= &bset
->ineq
[j
];
503 lower_line
= &bset
->ineq
[j
];
504 upper_line
= &bset
->ineq
[i
];
506 *lower
= isl_basic_set_constraint(
507 isl_basic_set_copy(bset
), lower_line
);
508 *upper
= isl_basic_set_constraint(
509 isl_basic_set_copy(bset
), upper_line
);