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_constraint_is_equal(struct isl_constraint
*constraint1
,
193 struct isl_constraint
*constraint2
)
195 if (!constraint1
|| !constraint2
)
197 return constraint1
->bmap
== constraint2
->bmap
&&
198 constraint1
->line
== constraint2
->line
;
201 struct isl_basic_map
*isl_basic_map_add_constraint(
202 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
204 if (!bmap
|| !constraint
)
207 isl_assert(constraint
->ctx
,
208 isl_dim_equal(bmap
->dim
, constraint
->bmap
->dim
), goto error
);
210 bmap
= isl_basic_map_intersect(bmap
,
211 isl_basic_map_from_constraint(constraint
));
214 isl_basic_map_free(bmap
);
215 isl_constraint_free(constraint
);
219 struct isl_basic_set
*isl_basic_set_add_constraint(
220 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
222 return (struct isl_basic_set
*)
223 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
227 struct isl_constraint
*isl_constraint_add_div(struct isl_constraint
*constraint
,
228 struct isl_div
*div
, int *pos
)
230 if (!constraint
|| !div
)
233 isl_assert(constraint
->ctx
,
234 isl_dim_equal(div
->bmap
->dim
, constraint
->bmap
->dim
), goto error
);
235 isl_assert(constraint
->ctx
,
236 constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1, goto error
);
238 constraint
->bmap
= isl_basic_map_cow(constraint
->bmap
);
239 constraint
->bmap
= isl_basic_map_extend_dim(constraint
->bmap
,
240 isl_dim_copy(constraint
->bmap
->dim
), 1, 0, 0);
241 if (!constraint
->bmap
)
243 constraint
->line
= &constraint
->bmap
->eq
[0];
244 *pos
= isl_basic_map_alloc_div(constraint
->bmap
);
247 isl_seq_cpy(constraint
->bmap
->div
[*pos
], div
->line
[0],
248 1 + 1 + isl_basic_map_total_dim(constraint
->bmap
));
252 isl_constraint_free(constraint
);
257 int isl_constraint_dim(struct isl_constraint
*constraint
,
258 enum isl_dim_type type
)
262 return n(constraint
, type
);
265 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
269 isl_int_set(*v
, constraint
->line
[0][0]);
272 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
273 enum isl_dim_type type
, int pos
, isl_int
*v
)
278 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
279 isl_int_set(*v
, constraint
->line
[0][offset(constraint
, type
) + pos
]);
282 struct isl_div
*isl_constraint_div(struct isl_constraint
*constraint
, int pos
)
287 isl_assert(constraint
->ctx
, pos
< n(constraint
, isl_dim_div
),
289 return isl_basic_map_div(isl_basic_map_copy(constraint
->bmap
), pos
);
292 void isl_constraint_set_constant(struct isl_constraint
*constraint
, isl_int v
)
296 isl_int_set(constraint
->line
[0][0], v
);
299 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
300 enum isl_dim_type type
, int pos
, isl_int v
)
305 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
306 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
309 void isl_constraint_clear(struct isl_constraint
*constraint
)
315 total
= isl_basic_map_total_dim(constraint
->bmap
);
316 isl_seq_clr(constraint
->line
[0], 1 + total
);
319 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
326 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
328 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
329 total
= isl_basic_map_total_dim(constraint
->bmap
);
330 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
331 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
332 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
335 isl_constraint_free(constraint
);
339 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
343 return constraint
->line
>= constraint
->bmap
->eq
;
346 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
347 __isl_take isl_constraint
*constraint
)
350 struct isl_basic_map
*bmap
;
357 if (constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1) {
358 bmap
= isl_basic_map_copy(constraint
->bmap
);
359 isl_constraint_free(constraint
);
363 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
364 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
365 bmap
= isl_basic_map_cow(bmap
);
366 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
367 if (isl_constraint_is_equality(constraint
)) {
368 k
= isl_basic_map_alloc_equality(bmap
);
374 k
= isl_basic_map_alloc_inequality(bmap
);
379 total
= isl_basic_map_total_dim(bmap
);
380 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
381 isl_constraint_free(constraint
);
384 isl_constraint_free(constraint
);
385 isl_basic_map_free(bmap
);
389 struct isl_basic_set
*isl_basic_set_from_constraint(
390 struct isl_constraint
*constraint
)
395 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
396 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
398 isl_constraint_free(constraint
);
402 int isl_basic_set_has_defining_equality(
403 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
404 struct isl_constraint
**c
)
412 offset
= basic_set_offset(bset
, type
);
413 total
= isl_basic_set_total_dim(bset
);
414 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
415 for (i
= 0; i
< bset
->n_eq
; ++i
)
416 if (!isl_int_is_zero(bset
->eq
[i
][offset
+ pos
]) &&
417 isl_seq_first_non_zero(bset
->eq
[i
]+offset
+pos
+1,
418 1+total
-offset
-pos
-1) == -1) {
419 *c
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
426 int isl_basic_set_has_defining_inequalities(
427 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
428 struct isl_constraint
**lower
,
429 struct isl_constraint
**upper
)
435 isl_int
**lower_line
, **upper_line
;
439 offset
= basic_set_offset(bset
, type
);
440 total
= isl_basic_set_total_dim(bset
);
441 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
443 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
444 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
446 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
448 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
450 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
451 1+total
-offset
-pos
-1) != -1)
453 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
454 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
457 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
458 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
461 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
462 lower_line
= &bset
->ineq
[i
];
463 upper_line
= &bset
->ineq
[j
];
465 lower_line
= &bset
->ineq
[j
];
466 upper_line
= &bset
->ineq
[i
];
468 *lower
= isl_basic_set_constraint(
469 isl_basic_set_copy(bset
), lower_line
);
470 *upper
= isl_basic_set_constraint(
471 isl_basic_set_copy(bset
), upper_line
);