1 #include <isl_constraint.h>
3 #include "isl_map_private.h"
5 static unsigned n(struct isl_constraint
*c
, enum isl_dim_type type
)
7 return isl_basic_map_dim(c
->bmap
, type
);
10 static unsigned offset(struct isl_constraint
*c
, enum isl_dim_type type
)
12 struct isl_dim
*dim
= c
->bmap
->dim
;
14 case isl_dim_param
: return 1;
15 case isl_dim_in
: return 1 + dim
->nparam
;
16 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
17 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
21 static unsigned basic_set_offset(struct isl_basic_set
*bset
,
22 enum isl_dim_type type
)
24 struct isl_dim
*dim
= bset
->dim
;
26 case isl_dim_param
: return 1;
27 case isl_dim_in
: return 1 + dim
->nparam
;
28 case isl_dim_out
: return 1 + dim
->nparam
+ dim
->n_in
;
29 case isl_dim_div
: return 1 + dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
33 struct isl_constraint
*isl_basic_map_constraint(struct isl_basic_map
*bmap
,
36 struct isl_constraint
*constraint
;
41 constraint
= isl_alloc_type(bmap
->ctx
, struct isl_constraint
);
45 constraint
->ctx
= bmap
->ctx
;
46 isl_ctx_ref(constraint
->ctx
);
48 constraint
->bmap
= bmap
;
49 constraint
->line
= line
;
53 isl_basic_map_free(bmap
);
57 struct isl_constraint
*isl_basic_set_constraint(struct isl_basic_set
*bset
,
60 return isl_basic_map_constraint((struct isl_basic_map
*)bset
, line
);
63 struct isl_constraint
*isl_equality_alloc(struct isl_dim
*dim
)
65 struct isl_basic_map
*bmap
;
70 bmap
= isl_basic_map_alloc_dim(dim
, 0, 1, 0);
74 isl_basic_map_alloc_equality(bmap
);
75 isl_seq_clr(bmap
->eq
[0], 1 + isl_basic_map_total_dim(bmap
));
76 return isl_basic_map_constraint(bmap
, &bmap
->eq
[0]);
79 struct isl_constraint
*isl_inequality_alloc(struct isl_dim
*dim
)
81 struct isl_basic_map
*bmap
;
86 bmap
= isl_basic_map_alloc_dim(dim
, 0, 0, 1);
90 isl_basic_map_alloc_inequality(bmap
);
91 isl_seq_clr(bmap
->ineq
[0], 1 + isl_basic_map_total_dim(bmap
));
92 return isl_basic_map_constraint(bmap
, &bmap
->ineq
[0]);
95 struct isl_constraint
*isl_constraint_dup(struct isl_constraint
*c
)
100 return isl_basic_map_constraint(isl_basic_map_copy(c
->bmap
), c
->line
);
103 struct isl_constraint
*isl_constraint_cow(struct isl_constraint
*c
)
111 return isl_constraint_dup(c
);
114 struct isl_constraint
*isl_constraint_copy(struct isl_constraint
*constraint
)
123 void isl_constraint_free(struct isl_constraint
*c
)
131 isl_basic_map_free(c
->bmap
);
132 isl_ctx_deref(c
->ctx
);
136 struct isl_constraint
*isl_basic_set_first_constraint(
137 struct isl_basic_set
*bset
)
143 return isl_basic_set_constraint(bset
, &bset
->eq
[0]);
145 if (bset
->n_ineq
> 0)
146 return isl_basic_set_constraint(bset
, &bset
->ineq
[0]);
148 isl_basic_set_free(bset
);
152 struct isl_constraint
*isl_constraint_next(struct isl_constraint
*c
)
154 c
= isl_constraint_cow(c
);
155 if (c
->line
>= c
->bmap
->eq
) {
157 if (c
->line
< c
->bmap
->eq
+ c
->bmap
->n_eq
)
159 c
->line
= c
->bmap
->ineq
;
162 if (c
->line
< c
->bmap
->ineq
+ c
->bmap
->n_ineq
)
164 isl_constraint_free(c
);
168 int isl_constraint_is_equal(struct isl_constraint
*constraint1
,
169 struct isl_constraint
*constraint2
)
171 if (!constraint1
|| !constraint2
)
173 return constraint1
->bmap
== constraint2
->bmap
&&
174 constraint1
->line
== constraint2
->line
;
177 struct isl_basic_map
*isl_basic_map_add_constraint(
178 struct isl_basic_map
*bmap
, struct isl_constraint
*constraint
)
180 if (!bmap
|| !constraint
)
183 isl_assert(constraint
->ctx
,
184 isl_dim_equal(bmap
->dim
, constraint
->bmap
->dim
), goto error
);
186 bmap
= isl_basic_map_intersect(bmap
,
187 isl_basic_map_from_constraint(constraint
));
190 isl_basic_map_free(bmap
);
191 isl_constraint_free(constraint
);
195 struct isl_basic_set
*isl_basic_set_add_constraint(
196 struct isl_basic_set
*bset
, struct isl_constraint
*constraint
)
198 return (struct isl_basic_set
*)
199 isl_basic_map_add_constraint((struct isl_basic_map
*)bset
,
203 struct isl_constraint
*isl_constraint_add_div(struct isl_constraint
*constraint
,
204 struct isl_div
*div
, int *pos
)
206 if (!constraint
|| !div
)
209 isl_assert(constraint
->ctx
,
210 isl_dim_equal(div
->bmap
->dim
, constraint
->bmap
->dim
), goto error
);
211 isl_assert(constraint
->ctx
,
212 constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1, goto error
);
214 constraint
->bmap
= isl_basic_map_cow(constraint
->bmap
);
215 constraint
->bmap
= isl_basic_map_extend_dim(constraint
->bmap
,
216 isl_dim_copy(constraint
->bmap
->dim
), 1, 0, 0);
217 if (!constraint
->bmap
)
219 constraint
->line
= &constraint
->bmap
->eq
[0];
220 *pos
= isl_basic_map_alloc_div(constraint
->bmap
);
223 isl_seq_cpy(constraint
->bmap
->div
[*pos
], div
->line
[0],
224 1 + 1 + isl_basic_map_total_dim(constraint
->bmap
));
228 isl_constraint_free(constraint
);
233 int isl_constraint_dim(struct isl_constraint
*constraint
,
234 enum isl_dim_type type
)
238 return n(constraint
, type
);
241 void isl_constraint_get_constant(struct isl_constraint
*constraint
, isl_int
*v
)
245 isl_int_set(*v
, constraint
->line
[0][0]);
248 void isl_constraint_get_coefficient(struct isl_constraint
*constraint
,
249 enum isl_dim_type type
, int pos
, isl_int
*v
)
254 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
255 isl_int_set(*v
, constraint
->line
[0][offset(constraint
, type
) + pos
]);
258 struct isl_div
*isl_constraint_div(struct isl_constraint
*constraint
, int pos
)
263 isl_assert(constraint
->ctx
, pos
< n(constraint
, isl_dim_div
),
265 return isl_basic_map_div(isl_basic_map_copy(constraint
->bmap
), pos
);
268 void isl_constraint_set_constant(struct isl_constraint
*constraint
, isl_int v
)
272 isl_int_set(constraint
->line
[0][0], v
);
275 void isl_constraint_set_coefficient(struct isl_constraint
*constraint
,
276 enum isl_dim_type type
, int pos
, isl_int v
)
281 isl_assert(constraint
->ctx
, pos
< n(constraint
, type
), return);
282 isl_int_set(constraint
->line
[0][offset(constraint
, type
) + pos
], v
);
285 void isl_constraint_clear(struct isl_constraint
*constraint
)
291 total
= isl_basic_map_total_dim(constraint
->bmap
);
292 isl_seq_clr(constraint
->line
[0], 1 + total
);
295 struct isl_constraint
*isl_constraint_negate(struct isl_constraint
*constraint
)
302 isl_assert(constraint
->ctx
, !isl_constraint_is_equality(constraint
),
304 isl_assert(constraint
->ctx
, constraint
->bmap
->ref
== 1, goto error
);
305 total
= isl_basic_map_total_dim(constraint
->bmap
);
306 isl_seq_neg(constraint
->line
[0], constraint
->line
[0], 1 + total
);
307 isl_int_sub_ui(constraint
->line
[0][0], constraint
->line
[0][0], 1);
308 ISL_F_CLR(constraint
->bmap
, ISL_BASIC_MAP_NORMALIZED
);
311 isl_constraint_free(constraint
);
315 int isl_constraint_is_equality(struct isl_constraint
*constraint
)
319 return constraint
->line
>= constraint
->bmap
->eq
;
322 __isl_give isl_basic_map
*isl_basic_map_from_constraint(
323 __isl_take isl_constraint
*constraint
)
326 struct isl_basic_map
*bmap
;
333 if (constraint
->bmap
->n_eq
+ constraint
->bmap
->n_ineq
== 1) {
334 bmap
= isl_basic_map_copy(constraint
->bmap
);
335 isl_constraint_free(constraint
);
339 bmap
= isl_basic_map_universe_like(constraint
->bmap
);
340 bmap
= isl_basic_map_align_divs(bmap
, constraint
->bmap
);
341 bmap
= isl_basic_map_cow(bmap
);
342 bmap
= isl_basic_map_extend_constraints(bmap
, 1, 1);
343 if (isl_constraint_is_equality(constraint
)) {
344 k
= isl_basic_map_alloc_equality(bmap
);
350 k
= isl_basic_map_alloc_inequality(bmap
);
355 total
= isl_basic_map_total_dim(bmap
);
356 isl_seq_cpy(c
, constraint
->line
[0], 1 + total
);
357 isl_constraint_free(constraint
);
360 isl_constraint_free(constraint
);
361 isl_basic_map_free(bmap
);
365 struct isl_basic_set
*isl_basic_set_from_constraint(
366 struct isl_constraint
*constraint
)
371 isl_assert(constraint
->ctx
,n(constraint
, isl_dim_in
) == 0, goto error
);
372 return (isl_basic_set
*)isl_basic_map_from_constraint(constraint
);
374 isl_constraint_free(constraint
);
378 int isl_basic_set_has_defining_equality(
379 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
380 struct isl_constraint
**c
)
388 offset
= basic_set_offset(bset
, type
);
389 total
= isl_basic_set_total_dim(bset
);
390 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
391 for (i
= 0; i
< bset
->n_eq
; ++i
)
392 if (!isl_int_is_zero(bset
->eq
[i
][offset
+ pos
]) &&
393 isl_seq_first_non_zero(bset
->eq
[i
]+offset
+pos
+1,
394 1+total
-offset
-pos
-1) == -1) {
395 *c
= isl_basic_set_constraint(isl_basic_set_copy(bset
),
402 int isl_basic_set_has_defining_inequalities(
403 struct isl_basic_set
*bset
, enum isl_dim_type type
, int pos
,
404 struct isl_constraint
**lower
,
405 struct isl_constraint
**upper
)
411 isl_int
**lower_line
, **upper_line
;
415 offset
= basic_set_offset(bset
, type
);
416 total
= isl_basic_set_total_dim(bset
);
417 isl_assert(bset
->ctx
, pos
< isl_basic_set_dim(bset
, type
), return -1);
419 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
420 if (isl_int_is_zero(bset
->ineq
[i
][offset
+ pos
]))
422 if (isl_int_is_one(bset
->ineq
[i
][offset
+ pos
]))
424 if (isl_int_is_negone(bset
->ineq
[i
][offset
+ pos
]))
426 if (isl_seq_first_non_zero(bset
->ineq
[i
]+offset
+pos
+1,
427 1+total
-offset
-pos
-1) != -1)
429 for (j
= i
+ 1; j
< bset
->n_ineq
; ++j
) {
430 if (!isl_seq_is_neg(bset
->ineq
[i
]+1, bset
->ineq
[j
]+1,
433 isl_int_add(m
, bset
->ineq
[i
][0], bset
->ineq
[j
][0]);
434 if (isl_int_abs_ge(m
, bset
->ineq
[i
][offset
+pos
]))
437 if (isl_int_is_pos(bset
->ineq
[i
][offset
+pos
])) {
438 lower_line
= &bset
->ineq
[i
];
439 upper_line
= &bset
->ineq
[j
];
441 lower_line
= &bset
->ineq
[j
];
442 upper_line
= &bset
->ineq
[i
];
444 *lower
= isl_basic_set_constraint(
445 isl_basic_set_copy(bset
), lower_line
);
446 *upper
= isl_basic_set_constraint(
447 isl_basic_set_copy(bset
), upper_line
);