1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 *
5 * Use of this software is governed by the MIT license
6 *
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
11 */
12
13 #include <isl_map_private.h>
14 #include <isl_constraint_private.h>
15 #include <isl_space_private.h>
16 #include <isl_seq.h>
17 #include <isl_aff_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
21
22 #include <bset_to_bmap.c>
23 #include <bset_from_bmap.c>
24
25 #undef EL_BASE
26 #define EL_BASE constraint
27
28 #include <isl_list_templ.c>
29
isl_constraint_get_ctx(__isl_keep isl_constraint * c)30 isl_ctx *isl_constraint_get_ctx(__isl_keep isl_constraint *c)
31 {
32 return c ? isl_local_space_get_ctx(c->ls) : NULL;
33 }
34
n(__isl_keep isl_constraint * c,enum isl_dim_type type)35 static isl_size n(__isl_keep isl_constraint *c, enum isl_dim_type type)
36 {
37 return isl_local_space_dim(c->ls, type);
38 }
39
offset(__isl_keep isl_constraint * c,enum isl_dim_type type)40 static unsigned offset(__isl_keep isl_constraint *c, enum isl_dim_type type)
41 {
42 return isl_local_space_offset(c->ls, type);
43 }
44
isl_constraint_alloc_vec(int eq,__isl_take isl_local_space * ls,__isl_take isl_vec * v)45 __isl_give isl_constraint *isl_constraint_alloc_vec(int eq,
46 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
47 {
48 isl_constraint *constraint;
49
50 if (!ls || !v)
51 goto error;
52
53 constraint = isl_alloc_type(isl_vec_get_ctx(v), isl_constraint);
54 if (!constraint)
55 goto error;
56
57 constraint->ref = 1;
58 constraint->eq = eq;
59 constraint->ls = ls;
60 constraint->v = v;
61
62 return constraint;
63 error:
64 isl_local_space_free(ls);
65 isl_vec_free(v);
66 return NULL;
67 }
68
isl_constraint_alloc(int eq,__isl_take isl_local_space * ls)69 __isl_give isl_constraint *isl_constraint_alloc(int eq,
70 __isl_take isl_local_space *ls)
71 {
72 isl_size dim;
73 isl_ctx *ctx;
74 isl_vec *v;
75
76 dim = isl_local_space_dim(ls, isl_dim_all);
77 if (dim < 0)
78 ls = isl_local_space_free(ls);
79 if (!ls)
80 return NULL;
81
82 ctx = isl_local_space_get_ctx(ls);
83 v = isl_vec_alloc(ctx, 1 + dim);
84 v = isl_vec_clr(v);
85 return isl_constraint_alloc_vec(eq, ls, v);
86 }
87
isl_basic_map_constraint(__isl_take isl_basic_map * bmap,isl_int ** line)88 __isl_give isl_constraint *isl_basic_map_constraint(
89 __isl_take isl_basic_map *bmap, isl_int **line)
90 {
91 int eq;
92 isl_size dim;
93 isl_ctx *ctx;
94 isl_vec *v;
95 isl_local_space *ls = NULL;
96 isl_constraint *constraint;
97
98 if (!bmap || !line)
99 goto error;
100
101 eq = line >= bmap->eq;
102
103 ctx = isl_basic_map_get_ctx(bmap);
104 ls = isl_basic_map_get_local_space(bmap);
105 dim = isl_local_space_dim(ls, isl_dim_all);
106 if (dim < 0)
107 goto error;
108 v = isl_vec_alloc(ctx, 1 + dim);
109 if (!v)
110 goto error;
111 isl_seq_cpy(v->el, line[0], v->size);
112 constraint = isl_constraint_alloc_vec(eq, ls, v);
113
114 isl_basic_map_free(bmap);
115 return constraint;
116 error:
117 isl_local_space_free(ls);
118 isl_basic_map_free(bmap);
119 return NULL;
120 }
121
isl_basic_set_constraint(__isl_take isl_basic_set * bset,isl_int ** line)122 __isl_give isl_constraint *isl_basic_set_constraint(
123 __isl_take isl_basic_set *bset, isl_int **line)
124 {
125 return isl_basic_map_constraint(bset_to_bmap(bset), line);
126 }
127
isl_constraint_alloc_equality(__isl_take isl_local_space * ls)128 __isl_give isl_constraint *isl_constraint_alloc_equality(
129 __isl_take isl_local_space *ls)
130 {
131 return isl_constraint_alloc(1, ls);
132 }
133
isl_constraint_alloc_inequality(__isl_take isl_local_space * ls)134 __isl_give isl_constraint *isl_constraint_alloc_inequality(
135 __isl_take isl_local_space *ls)
136 {
137 return isl_constraint_alloc(0, ls);
138 }
139
isl_constraint_dup(__isl_keep isl_constraint * c)140 __isl_give isl_constraint *isl_constraint_dup(__isl_keep isl_constraint *c)
141 {
142 if (!c)
143 return NULL;
144
145 return isl_constraint_alloc_vec(c->eq, isl_local_space_copy(c->ls),
146 isl_vec_copy(c->v));
147 }
148
isl_constraint_cow(__isl_take isl_constraint * c)149 __isl_give isl_constraint *isl_constraint_cow(__isl_take isl_constraint *c)
150 {
151 if (!c)
152 return NULL;
153
154 if (c->ref == 1)
155 return c;
156 c->ref--;
157 return isl_constraint_dup(c);
158 }
159
isl_constraint_copy(__isl_keep isl_constraint * constraint)160 __isl_give isl_constraint *isl_constraint_copy(
161 __isl_keep isl_constraint *constraint)
162 {
163 if (!constraint)
164 return NULL;
165
166 constraint->ref++;
167 return constraint;
168 }
169
isl_constraint_free(__isl_take isl_constraint * c)170 __isl_null isl_constraint *isl_constraint_free(__isl_take isl_constraint *c)
171 {
172 if (!c)
173 return NULL;
174
175 if (--c->ref > 0)
176 return NULL;
177
178 isl_local_space_free(c->ls);
179 isl_vec_free(c->v);
180 free(c);
181
182 return NULL;
183 }
184
185 /* Return the number of constraints in "bmap", i.e., the
186 * number of times isl_basic_map_foreach_constraint will
187 * call the callback.
188 */
isl_basic_map_n_constraint(__isl_keep isl_basic_map * bmap)189 isl_size isl_basic_map_n_constraint(__isl_keep isl_basic_map *bmap)
190 {
191 if (!bmap)
192 return isl_size_error;
193
194 return bmap->n_eq + bmap->n_ineq;
195 }
196
197 /* Return the number of constraints in "bset", i.e., the
198 * number of times isl_basic_set_foreach_constraint will
199 * call the callback.
200 */
isl_basic_set_n_constraint(__isl_keep isl_basic_set * bset)201 isl_size isl_basic_set_n_constraint(__isl_keep isl_basic_set *bset)
202 {
203 return isl_basic_map_n_constraint(bset);
204 }
205
isl_basic_map_foreach_constraint(__isl_keep isl_basic_map * bmap,isl_stat (* fn)(__isl_take isl_constraint * c,void * user),void * user)206 isl_stat isl_basic_map_foreach_constraint(__isl_keep isl_basic_map *bmap,
207 isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
208 {
209 int i;
210 struct isl_constraint *c;
211
212 if (!bmap)
213 return isl_stat_error;
214
215 isl_assert(bmap->ctx, ISL_F_ISSET(bmap, ISL_BASIC_MAP_FINAL),
216 return isl_stat_error);
217
218 for (i = 0; i < bmap->n_eq; ++i) {
219 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
220 &bmap->eq[i]);
221 if (!c)
222 return isl_stat_error;
223 if (fn(c, user) < 0)
224 return isl_stat_error;
225 }
226
227 for (i = 0; i < bmap->n_ineq; ++i) {
228 c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
229 &bmap->ineq[i]);
230 if (!c)
231 return isl_stat_error;
232 if (fn(c, user) < 0)
233 return isl_stat_error;
234 }
235
236 return isl_stat_ok;
237 }
238
isl_basic_set_foreach_constraint(__isl_keep isl_basic_set * bset,isl_stat (* fn)(__isl_take isl_constraint * c,void * user),void * user)239 isl_stat isl_basic_set_foreach_constraint(__isl_keep isl_basic_set *bset,
240 isl_stat (*fn)(__isl_take isl_constraint *c, void *user), void *user)
241 {
242 return isl_basic_map_foreach_constraint(bset_to_bmap(bset), fn, user);
243 }
244
245 /* Add the constraint to the list that "user" points to, if it is not
246 * a div constraint.
247 */
collect_constraint(__isl_take isl_constraint * constraint,void * user)248 static isl_stat collect_constraint(__isl_take isl_constraint *constraint,
249 void *user)
250 {
251 isl_constraint_list **list = user;
252 isl_bool is_div;
253
254 is_div = isl_constraint_is_div_constraint(constraint);
255 if (is_div < 0 || is_div)
256 isl_constraint_free(constraint);
257 else
258 *list = isl_constraint_list_add(*list, constraint);
259
260 return is_div < 0 ? isl_stat_error : isl_stat_ok;
261 }
262
263 /* Return a list of constraints that, when combined, are equivalent
264 * to "bmap". The input is required to have only known divs.
265 *
266 * There is no need to include the div constraints as they are
267 * implied by the div expressions.
268 */
isl_basic_map_get_constraint_list(__isl_keep isl_basic_map * bmap)269 __isl_give isl_constraint_list *isl_basic_map_get_constraint_list(
270 __isl_keep isl_basic_map *bmap)
271 {
272 isl_size n;
273 isl_bool known;
274 isl_ctx *ctx;
275 isl_constraint_list *list;
276
277 known = isl_basic_map_divs_known(bmap);
278 if (known < 0)
279 return NULL;
280 ctx = isl_basic_map_get_ctx(bmap);
281 if (!known)
282 isl_die(ctx, isl_error_invalid,
283 "input involves unknown divs", return NULL);
284
285 n = isl_basic_map_n_constraint(bmap);
286 if (n < 0)
287 return NULL;
288 list = isl_constraint_list_alloc(ctx, n);
289 if (isl_basic_map_foreach_constraint(bmap,
290 &collect_constraint, &list) < 0)
291 list = isl_constraint_list_free(list);
292
293 return list;
294 }
295
296 /* Return a list of constraints that, when combined, are equivalent
297 * to "bset". The input is required to have only known divs.
298 */
isl_basic_set_get_constraint_list(__isl_keep isl_basic_set * bset)299 __isl_give isl_constraint_list *isl_basic_set_get_constraint_list(
300 __isl_keep isl_basic_set *bset)
301 {
302 return isl_basic_map_get_constraint_list(bset);
303 }
304
isl_constraint_is_equal(__isl_keep isl_constraint * constraint1,__isl_keep isl_constraint * constraint2)305 int isl_constraint_is_equal(__isl_keep isl_constraint *constraint1,
306 __isl_keep isl_constraint *constraint2)
307 {
308 int equal;
309
310 if (!constraint1 || !constraint2)
311 return 0;
312 if (constraint1->eq != constraint2->eq)
313 return 0;
314 equal = isl_local_space_is_equal(constraint1->ls, constraint2->ls);
315 if (equal < 0 || !equal)
316 return equal;
317 return isl_vec_is_equal(constraint1->v, constraint2->v);
318 }
319
isl_basic_map_add_constraint(__isl_take isl_basic_map * bmap,__isl_take isl_constraint * constraint)320 __isl_give isl_basic_map *isl_basic_map_add_constraint(
321 __isl_take isl_basic_map *bmap, __isl_take isl_constraint *constraint)
322 {
323 isl_ctx *ctx;
324 isl_space *space;
325 int equal_space;
326
327 if (!bmap || !constraint)
328 goto error;
329
330 ctx = isl_constraint_get_ctx(constraint);
331 space = isl_constraint_get_space(constraint);
332 equal_space = isl_space_is_equal(bmap->dim, space);
333 isl_space_free(space);
334 isl_assert(ctx, equal_space, goto error);
335
336 bmap = isl_basic_map_intersect(bmap,
337 isl_basic_map_from_constraint(constraint));
338 return bmap;
339 error:
340 isl_basic_map_free(bmap);
341 isl_constraint_free(constraint);
342 return NULL;
343 }
344
isl_basic_set_add_constraint(__isl_take isl_basic_set * bset,__isl_take isl_constraint * constraint)345 __isl_give isl_basic_set *isl_basic_set_add_constraint(
346 __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
347 {
348 return bset_from_bmap(isl_basic_map_add_constraint(bset_to_bmap(bset),
349 constraint));
350 }
351
isl_map_add_constraint(__isl_take isl_map * map,__isl_take isl_constraint * constraint)352 __isl_give isl_map *isl_map_add_constraint(__isl_take isl_map *map,
353 __isl_take isl_constraint *constraint)
354 {
355 isl_basic_map *bmap;
356
357 bmap = isl_basic_map_from_constraint(constraint);
358 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
359
360 return map;
361 }
362
isl_set_add_constraint(__isl_take isl_set * set,__isl_take isl_constraint * constraint)363 __isl_give isl_set *isl_set_add_constraint(__isl_take isl_set *set,
364 __isl_take isl_constraint *constraint)
365 {
366 return isl_map_add_constraint(set, constraint);
367 }
368
369 /* Return the space of "constraint".
370 */
isl_constraint_peek_space(__isl_keep isl_constraint * constraint)371 static __isl_keep isl_space *isl_constraint_peek_space(
372 __isl_keep isl_constraint *constraint)
373 {
374 return constraint ? isl_local_space_peek_space(constraint->ls) : NULL;
375 }
376
isl_constraint_get_space(__isl_keep isl_constraint * constraint)377 __isl_give isl_space *isl_constraint_get_space(
378 __isl_keep isl_constraint *constraint)
379 {
380 return constraint ? isl_local_space_get_space(constraint->ls) : NULL;
381 }
382
isl_constraint_get_local_space(__isl_keep isl_constraint * constraint)383 __isl_give isl_local_space *isl_constraint_get_local_space(
384 __isl_keep isl_constraint *constraint)
385 {
386 return constraint ? isl_local_space_copy(constraint->ls) : NULL;
387 }
388
isl_constraint_dim(__isl_keep isl_constraint * constraint,enum isl_dim_type type)389 isl_size isl_constraint_dim(__isl_keep isl_constraint *constraint,
390 enum isl_dim_type type)
391 {
392 if (!constraint)
393 return isl_size_error;
394 return n(constraint, type);
395 }
396
397 #undef TYPE
398 #define TYPE isl_constraint
399 static
400 #include "check_type_range_templ.c"
401
isl_constraint_involves_dims(__isl_keep isl_constraint * constraint,enum isl_dim_type type,unsigned first,unsigned n)402 isl_bool isl_constraint_involves_dims(__isl_keep isl_constraint *constraint,
403 enum isl_dim_type type, unsigned first, unsigned n)
404 {
405 int i;
406 int *active = NULL;
407 isl_bool involves = isl_bool_false;
408
409 if (!constraint)
410 return isl_bool_error;
411 if (n == 0)
412 return isl_bool_false;
413
414 if (isl_constraint_check_range(constraint, type, first, n) < 0)
415 return isl_bool_error;
416
417 active = isl_local_space_get_active(constraint->ls,
418 constraint->v->el + 1);
419 if (!active)
420 goto error;
421
422 first += isl_local_space_offset(constraint->ls, type) - 1;
423 for (i = 0; i < n; ++i)
424 if (active[first + i]) {
425 involves = isl_bool_true;
426 break;
427 }
428
429 free(active);
430
431 return involves;
432 error:
433 free(active);
434 return isl_bool_error;
435 }
436
437 /* Does the given constraint represent a lower bound on the given
438 * dimension?
439 */
isl_constraint_is_lower_bound(__isl_keep isl_constraint * constraint,enum isl_dim_type type,unsigned pos)440 isl_bool isl_constraint_is_lower_bound(__isl_keep isl_constraint *constraint,
441 enum isl_dim_type type, unsigned pos)
442 {
443 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
444 return isl_bool_error;
445
446 pos += isl_local_space_offset(constraint->ls, type);
447 return isl_bool_ok(isl_int_is_pos(constraint->v->el[pos]));
448 }
449
450 /* Does the given constraint represent an upper bound on the given
451 * dimension?
452 */
isl_constraint_is_upper_bound(__isl_keep isl_constraint * constraint,enum isl_dim_type type,unsigned pos)453 isl_bool isl_constraint_is_upper_bound(__isl_keep isl_constraint *constraint,
454 enum isl_dim_type type, unsigned pos)
455 {
456 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
457 return isl_bool_error;
458
459 pos += isl_local_space_offset(constraint->ls, type);
460 return isl_bool_ok(isl_int_is_neg(constraint->v->el[pos]));
461 }
462
isl_constraint_get_dim_name(__isl_keep isl_constraint * constraint,enum isl_dim_type type,unsigned pos)463 const char *isl_constraint_get_dim_name(__isl_keep isl_constraint *constraint,
464 enum isl_dim_type type, unsigned pos)
465 {
466 return constraint ?
467 isl_local_space_get_dim_name(constraint->ls, type, pos) : NULL;
468 }
469
isl_constraint_get_constant(__isl_keep isl_constraint * constraint,isl_int * v)470 void isl_constraint_get_constant(__isl_keep isl_constraint *constraint,
471 isl_int *v)
472 {
473 if (!constraint)
474 return;
475 isl_int_set(*v, constraint->v->el[0]);
476 }
477
478 /* Return the constant term of "constraint".
479 */
isl_constraint_get_constant_val(__isl_keep isl_constraint * constraint)480 __isl_give isl_val *isl_constraint_get_constant_val(
481 __isl_keep isl_constraint *constraint)
482 {
483 isl_ctx *ctx;
484
485 if (!constraint)
486 return NULL;
487
488 ctx = isl_constraint_get_ctx(constraint);
489 return isl_val_int_from_isl_int(ctx, constraint->v->el[0]);
490 }
491
isl_constraint_get_coefficient(__isl_keep isl_constraint * constraint,enum isl_dim_type type,int pos,isl_int * v)492 void isl_constraint_get_coefficient(__isl_keep isl_constraint *constraint,
493 enum isl_dim_type type, int pos, isl_int *v)
494 {
495 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
496 return;
497
498 pos += isl_local_space_offset(constraint->ls, type);
499 isl_int_set(*v, constraint->v->el[pos]);
500 }
501
502 /* Return the coefficient of the variable of type "type" at position "pos"
503 * of "constraint".
504 */
isl_constraint_get_coefficient_val(__isl_keep isl_constraint * constraint,enum isl_dim_type type,int pos)505 __isl_give isl_val *isl_constraint_get_coefficient_val(
506 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
507 {
508 isl_ctx *ctx;
509
510 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
511 return NULL;
512
513 ctx = isl_constraint_get_ctx(constraint);
514 pos += isl_local_space_offset(constraint->ls, type);
515 return isl_val_int_from_isl_int(ctx, constraint->v->el[pos]);
516 }
517
isl_constraint_get_div(__isl_keep isl_constraint * constraint,int pos)518 __isl_give isl_aff *isl_constraint_get_div(__isl_keep isl_constraint *constraint,
519 int pos)
520 {
521 if (!constraint)
522 return NULL;
523
524 return isl_local_space_get_div(constraint->ls, pos);
525 }
526
isl_constraint_set_constant(__isl_take isl_constraint * constraint,isl_int v)527 __isl_give isl_constraint *isl_constraint_set_constant(
528 __isl_take isl_constraint *constraint, isl_int v)
529 {
530 constraint = isl_constraint_cow(constraint);
531 if (!constraint)
532 return NULL;
533
534 constraint->v = isl_vec_cow(constraint->v);
535 if (!constraint->v)
536 return isl_constraint_free(constraint);
537
538 isl_int_set(constraint->v->el[0], v);
539 return constraint;
540 }
541
542 /* Replace the constant term of "constraint" by "v".
543 */
isl_constraint_set_constant_val(__isl_take isl_constraint * constraint,__isl_take isl_val * v)544 __isl_give isl_constraint *isl_constraint_set_constant_val(
545 __isl_take isl_constraint *constraint, __isl_take isl_val *v)
546 {
547 constraint = isl_constraint_cow(constraint);
548 if (!constraint || !v)
549 goto error;
550 if (!isl_val_is_int(v))
551 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
552 "expecting integer value", goto error);
553 constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
554 if (!constraint->v)
555 constraint = isl_constraint_free(constraint);
556 return constraint;
557 error:
558 isl_val_free(v);
559 return isl_constraint_free(constraint);
560 }
561
isl_constraint_set_constant_si(__isl_take isl_constraint * constraint,int v)562 __isl_give isl_constraint *isl_constraint_set_constant_si(
563 __isl_take isl_constraint *constraint, int v)
564 {
565 constraint = isl_constraint_cow(constraint);
566 if (!constraint)
567 return NULL;
568
569 constraint->v = isl_vec_cow(constraint->v);
570 if (!constraint->v)
571 return isl_constraint_free(constraint);
572
573 isl_int_set_si(constraint->v->el[0], v);
574 return constraint;
575 }
576
isl_constraint_set_coefficient(__isl_take isl_constraint * constraint,enum isl_dim_type type,int pos,isl_int v)577 __isl_give isl_constraint *isl_constraint_set_coefficient(
578 __isl_take isl_constraint *constraint,
579 enum isl_dim_type type, int pos, isl_int v)
580 {
581 constraint = isl_constraint_cow(constraint);
582 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
583 return isl_constraint_free(constraint);
584
585 constraint->v = isl_vec_cow(constraint->v);
586 if (!constraint->v)
587 return isl_constraint_free(constraint);
588
589 pos += isl_local_space_offset(constraint->ls, type);
590 isl_int_set(constraint->v->el[pos], v);
591
592 return constraint;
593 }
594
595 /* Replace the coefficient of the variable of type "type" at position "pos"
596 * of "constraint" by "v".
597 */
isl_constraint_set_coefficient_val(__isl_take isl_constraint * constraint,enum isl_dim_type type,int pos,__isl_take isl_val * v)598 __isl_give isl_constraint *isl_constraint_set_coefficient_val(
599 __isl_take isl_constraint *constraint,
600 enum isl_dim_type type, int pos, __isl_take isl_val *v)
601 {
602 constraint = isl_constraint_cow(constraint);
603 if (!constraint || !v)
604 goto error;
605 if (!isl_val_is_int(v))
606 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
607 "expecting integer value", goto error);
608 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
609 goto error;
610
611 pos += isl_local_space_offset(constraint->ls, type);
612 constraint->v = isl_vec_set_element_val(constraint->v, pos, v);
613 if (!constraint->v)
614 constraint = isl_constraint_free(constraint);
615 return constraint;
616 error:
617 isl_val_free(v);
618 return isl_constraint_free(constraint);
619 }
620
isl_constraint_set_coefficient_si(__isl_take isl_constraint * constraint,enum isl_dim_type type,int pos,int v)621 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
622 __isl_take isl_constraint *constraint,
623 enum isl_dim_type type, int pos, int v)
624 {
625 constraint = isl_constraint_cow(constraint);
626 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
627 return isl_constraint_free(constraint);
628
629 constraint->v = isl_vec_cow(constraint->v);
630 if (!constraint->v)
631 return isl_constraint_free(constraint);
632
633 pos += isl_local_space_offset(constraint->ls, type);
634 isl_int_set_si(constraint->v->el[pos], v);
635
636 return constraint;
637 }
638
isl_constraint_negate(__isl_take isl_constraint * constraint)639 __isl_give isl_constraint *isl_constraint_negate(
640 __isl_take isl_constraint *constraint)
641 {
642 isl_ctx *ctx;
643
644 constraint = isl_constraint_cow(constraint);
645 if (!constraint)
646 return NULL;
647
648 ctx = isl_constraint_get_ctx(constraint);
649 if (isl_constraint_is_equality(constraint))
650 isl_die(ctx, isl_error_invalid, "cannot negate equality",
651 return isl_constraint_free(constraint));
652 constraint->v = isl_vec_neg(constraint->v);
653 constraint->v = isl_vec_cow(constraint->v);
654 if (!constraint->v)
655 return isl_constraint_free(constraint);
656 isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
657 return constraint;
658 }
659
isl_constraint_is_equality(struct isl_constraint * constraint)660 isl_bool isl_constraint_is_equality(struct isl_constraint *constraint)
661 {
662 if (!constraint)
663 return isl_bool_error;
664 return isl_bool_ok(constraint->eq);
665 }
666
isl_constraint_is_div_constraint(__isl_keep isl_constraint * constraint)667 isl_bool isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
668 {
669 int i;
670 isl_size n_div;
671
672 if (!constraint)
673 return isl_bool_error;
674 if (isl_constraint_is_equality(constraint))
675 return isl_bool_false;
676 n_div = isl_constraint_dim(constraint, isl_dim_div);
677 if (n_div < 0)
678 return isl_bool_error;
679 for (i = 0; i < n_div; ++i) {
680 isl_bool is_div;
681 is_div = isl_local_space_is_div_constraint(constraint->ls,
682 constraint->v->el, i);
683 if (is_div < 0 || is_div)
684 return is_div;
685 }
686
687 return isl_bool_false;
688 }
689
690 /* Is "constraint" an equality that corresponds to integer division "div"?
691 *
692 * That is, given an integer division of the form
693 *
694 * a = floor((f + c)/m)
695 *
696 * is the equality of the form
697 *
698 * -f + m d + c' = 0
699 * ?
700 * Note that the constant term is not checked explicitly, but given
701 * that this is a valid equality constraint, the constant c' necessarily
702 * has a value close to -c.
703 */
isl_constraint_is_div_equality(__isl_keep isl_constraint * constraint,unsigned div)704 isl_bool isl_constraint_is_div_equality(__isl_keep isl_constraint *constraint,
705 unsigned div)
706 {
707 isl_bool equality;
708
709 equality = isl_constraint_is_equality(constraint);
710 if (equality < 0 || !equality)
711 return equality;
712 return isl_local_space_is_div_equality(constraint->ls,
713 constraint->v->el, div);
714 }
715
716 /* We manually set ISL_BASIC_SET_FINAL instead of calling
717 * isl_basic_map_finalize because we want to keep the position
718 * of the divs and we therefore do not want to throw away redundant divs.
719 * This is arguably a bit fragile.
720 */
isl_basic_map_from_constraint(__isl_take isl_constraint * constraint)721 __isl_give isl_basic_map *isl_basic_map_from_constraint(
722 __isl_take isl_constraint *constraint)
723 {
724 int k;
725 isl_local_space *ls;
726 struct isl_basic_map *bmap;
727 isl_int *c;
728 isl_size total;
729
730 if (!constraint)
731 return NULL;
732
733 ls = isl_local_space_copy(constraint->ls);
734 bmap = isl_basic_map_from_local_space(ls);
735 bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
736 if (isl_constraint_is_equality(constraint)) {
737 k = isl_basic_map_alloc_equality(bmap);
738 if (k < 0)
739 goto error;
740 c = bmap->eq[k];
741 }
742 else {
743 k = isl_basic_map_alloc_inequality(bmap);
744 if (k < 0)
745 goto error;
746 c = bmap->ineq[k];
747 }
748 total = isl_basic_map_dim(bmap, isl_dim_all);
749 if (total < 0)
750 goto error;
751 isl_seq_cpy(c, constraint->v->el, 1 + total);
752 isl_constraint_free(constraint);
753 if (bmap)
754 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
755 return bmap;
756 error:
757 isl_constraint_free(constraint);
758 isl_basic_map_free(bmap);
759 return NULL;
760 }
761
isl_basic_set_from_constraint(__isl_take isl_constraint * constraint)762 __isl_give isl_basic_set *isl_basic_set_from_constraint(
763 __isl_take isl_constraint *constraint)
764 {
765 isl_space *space;
766
767 space = isl_constraint_peek_space(constraint);
768 if (isl_space_check_is_set(space) < 0)
769 goto error;
770 return bset_from_bmap(isl_basic_map_from_constraint(constraint));
771 error:
772 isl_constraint_free(constraint);
773 return NULL;
774 }
775
776 /* Is the variable of "type" at position "pos" of "bmap" defined
777 * in terms of earlier dimensions through an equality?
778 *
779 * If so, and if c is not NULL, then return a copy of this equality in *c.
780 */
isl_basic_map_has_defining_equality(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,int pos,__isl_give isl_constraint ** c)781 isl_bool isl_basic_map_has_defining_equality(
782 __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
783 __isl_give isl_constraint **c)
784 {
785 int i;
786 unsigned offset;
787 isl_size total;
788
789 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
790 return isl_bool_error;
791 offset = isl_basic_map_offset(bmap, type);
792 total = isl_basic_map_dim(bmap, isl_dim_all);
793 if (total < 0)
794 return isl_bool_error;
795 for (i = 0; i < bmap->n_eq; ++i) {
796 if (isl_int_is_zero(bmap->eq[i][offset + pos]) ||
797 isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
798 1+total-offset-pos-1) != -1)
799 continue;
800 if (c)
801 *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
802 &bmap->eq[i]);
803 return isl_bool_true;
804 }
805 return isl_bool_false;
806 }
807
808 /* Is the variable of "type" at position "pos" of "bset" defined
809 * in terms of earlier dimensions through an equality?
810 *
811 * If so, and if c is not NULL, then return a copy of this equality in *c.
812 */
isl_basic_set_has_defining_equality(__isl_keep isl_basic_set * bset,enum isl_dim_type type,int pos,__isl_give isl_constraint ** c)813 isl_bool isl_basic_set_has_defining_equality(
814 __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
815 __isl_give isl_constraint **c)
816 {
817 return isl_basic_map_has_defining_equality(bset_to_bmap(bset),
818 type, pos, c);
819 }
820
isl_basic_set_has_defining_inequalities(struct isl_basic_set * bset,enum isl_dim_type type,int pos,struct isl_constraint ** lower,struct isl_constraint ** upper)821 isl_bool isl_basic_set_has_defining_inequalities(
822 struct isl_basic_set *bset, enum isl_dim_type type, int pos,
823 struct isl_constraint **lower,
824 struct isl_constraint **upper)
825 {
826 int i, j;
827 unsigned offset;
828 isl_size total;
829 isl_int m;
830 isl_int **lower_line, **upper_line;
831
832 if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
833 return isl_bool_error;
834 offset = isl_basic_set_offset(bset, type);
835 total = isl_basic_set_dim(bset, isl_dim_all);
836 if (total < 0)
837 return isl_bool_error;
838 isl_int_init(m);
839 for (i = 0; i < bset->n_ineq; ++i) {
840 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
841 continue;
842 if (isl_int_is_one(bset->ineq[i][offset + pos]))
843 continue;
844 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
845 continue;
846 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
847 1+total-offset-pos-1) != -1)
848 continue;
849 for (j = i + 1; j < bset->n_ineq; ++j) {
850 if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
851 total))
852 continue;
853 isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
854 if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
855 continue;
856
857 if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
858 lower_line = &bset->ineq[i];
859 upper_line = &bset->ineq[j];
860 } else {
861 lower_line = &bset->ineq[j];
862 upper_line = &bset->ineq[i];
863 }
864 *lower = isl_basic_set_constraint(
865 isl_basic_set_copy(bset), lower_line);
866 *upper = isl_basic_set_constraint(
867 isl_basic_set_copy(bset), upper_line);
868 isl_int_clear(m);
869 return isl_bool_true;
870 }
871 }
872 *lower = NULL;
873 *upper = NULL;
874 isl_int_clear(m);
875 return isl_bool_false;
876 }
877
878 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
879 * (in "a" and "b"), add a constraint to "bset" that ensures that the
880 * bound implied by "a" is (strictly) larger than the bound implied by "b".
881 *
882 * If both constraints imply lower bounds, then this means that "a" is
883 * active in the result.
884 * If both constraints imply upper bounds, then this means that "b" is
885 * active in the result.
886 */
add_larger_bound_constraint(__isl_take isl_basic_set * bset,isl_int * a,isl_int * b,unsigned abs_pos,int strict)887 static __isl_give isl_basic_set *add_larger_bound_constraint(
888 __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
889 unsigned abs_pos, int strict)
890 {
891 int k;
892 isl_int t;
893 isl_size total;
894
895 total = isl_basic_set_dim(bset, isl_dim_all);
896 if (total < 0)
897 return isl_basic_set_free(bset);
898 k = isl_basic_set_alloc_inequality(bset);
899 if (k < 0)
900 goto error;
901
902 isl_int_init(t);
903 isl_int_neg(t, b[1 + abs_pos]);
904
905 isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
906 isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
907 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
908 total - abs_pos);
909
910 if (strict)
911 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
912
913 isl_int_clear(t);
914
915 return bset;
916 error:
917 isl_basic_set_free(bset);
918 return NULL;
919 }
920
921 /* Add constraints to "context" that ensure that "u" is the smallest
922 * (and therefore active) upper bound on "abs_pos" in "bset" and return
923 * the resulting basic set.
924 */
set_smallest_upper_bound(__isl_keep isl_basic_set * context,__isl_keep isl_basic_set * bset,unsigned abs_pos,int n_upper,int u)925 static __isl_give isl_basic_set *set_smallest_upper_bound(
926 __isl_keep isl_basic_set *context,
927 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
928 {
929 int j;
930
931 context = isl_basic_set_copy(context);
932 context = isl_basic_set_cow(context);
933
934 context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
935
936 for (j = 0; j < bset->n_ineq; ++j) {
937 if (j == u)
938 continue;
939 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
940 continue;
941 context = add_larger_bound_constraint(context,
942 bset->ineq[j], bset->ineq[u], abs_pos, j > u);
943 }
944
945 context = isl_basic_set_simplify(context);
946 context = isl_basic_set_finalize(context);
947
948 return context;
949 }
950
951 /* Add constraints to "context" that ensure that "u" is the largest
952 * (and therefore active) upper bound on "abs_pos" in "bset" and return
953 * the resulting basic set.
954 */
set_largest_lower_bound(__isl_keep isl_basic_set * context,__isl_keep isl_basic_set * bset,unsigned abs_pos,int n_lower,int l)955 static __isl_give isl_basic_set *set_largest_lower_bound(
956 __isl_keep isl_basic_set *context,
957 __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
958 {
959 int j;
960
961 context = isl_basic_set_copy(context);
962 context = isl_basic_set_cow(context);
963
964 context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
965
966 for (j = 0; j < bset->n_ineq; ++j) {
967 if (j == l)
968 continue;
969 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
970 continue;
971 context = add_larger_bound_constraint(context,
972 bset->ineq[l], bset->ineq[j], abs_pos, j > l);
973 }
974
975 context = isl_basic_set_simplify(context);
976 context = isl_basic_set_finalize(context);
977
978 return context;
979 }
980
foreach_upper_bound(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned abs_pos,__isl_take isl_basic_set * context,int n_upper,isl_stat (* fn)(__isl_take isl_constraint * lower,__isl_take isl_constraint * upper,__isl_take isl_basic_set * bset,void * user),void * user)981 static isl_stat foreach_upper_bound(__isl_keep isl_basic_set *bset,
982 enum isl_dim_type type, unsigned abs_pos,
983 __isl_take isl_basic_set *context, int n_upper,
984 isl_stat (*fn)(__isl_take isl_constraint *lower,
985 __isl_take isl_constraint *upper,
986 __isl_take isl_basic_set *bset, void *user), void *user)
987 {
988 isl_basic_set *context_i;
989 isl_constraint *upper = NULL;
990 int i;
991
992 for (i = 0; i < bset->n_ineq; ++i) {
993 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
994 continue;
995
996 context_i = set_smallest_upper_bound(context, bset,
997 abs_pos, n_upper, i);
998 if (isl_basic_set_is_empty(context_i)) {
999 isl_basic_set_free(context_i);
1000 continue;
1001 }
1002 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1003 &bset->ineq[i]);
1004 if (!upper || !context_i)
1005 goto error;
1006 if (fn(NULL, upper, context_i, user) < 0)
1007 break;
1008 }
1009
1010 isl_basic_set_free(context);
1011
1012 if (i < bset->n_ineq)
1013 return isl_stat_error;
1014
1015 return isl_stat_ok;
1016 error:
1017 isl_constraint_free(upper);
1018 isl_basic_set_free(context_i);
1019 isl_basic_set_free(context);
1020 return isl_stat_error;
1021 }
1022
foreach_lower_bound(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned abs_pos,__isl_take isl_basic_set * context,int n_lower,isl_stat (* fn)(__isl_take isl_constraint * lower,__isl_take isl_constraint * upper,__isl_take isl_basic_set * bset,void * user),void * user)1023 static isl_stat foreach_lower_bound(__isl_keep isl_basic_set *bset,
1024 enum isl_dim_type type, unsigned abs_pos,
1025 __isl_take isl_basic_set *context, int n_lower,
1026 isl_stat (*fn)(__isl_take isl_constraint *lower,
1027 __isl_take isl_constraint *upper,
1028 __isl_take isl_basic_set *bset, void *user), void *user)
1029 {
1030 isl_basic_set *context_i;
1031 isl_constraint *lower = NULL;
1032 int i;
1033
1034 for (i = 0; i < bset->n_ineq; ++i) {
1035 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
1036 continue;
1037
1038 context_i = set_largest_lower_bound(context, bset,
1039 abs_pos, n_lower, i);
1040 if (isl_basic_set_is_empty(context_i)) {
1041 isl_basic_set_free(context_i);
1042 continue;
1043 }
1044 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1045 &bset->ineq[i]);
1046 if (!lower || !context_i)
1047 goto error;
1048 if (fn(lower, NULL, context_i, user) < 0)
1049 break;
1050 }
1051
1052 isl_basic_set_free(context);
1053
1054 if (i < bset->n_ineq)
1055 return isl_stat_error;
1056
1057 return isl_stat_ok;
1058 error:
1059 isl_constraint_free(lower);
1060 isl_basic_set_free(context_i);
1061 isl_basic_set_free(context);
1062 return isl_stat_error;
1063 }
1064
foreach_bound_pair(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned abs_pos,__isl_take isl_basic_set * context,int n_lower,int n_upper,isl_stat (* fn)(__isl_take isl_constraint * lower,__isl_take isl_constraint * upper,__isl_take isl_basic_set * bset,void * user),void * user)1065 static isl_stat foreach_bound_pair(__isl_keep isl_basic_set *bset,
1066 enum isl_dim_type type, unsigned abs_pos,
1067 __isl_take isl_basic_set *context, int n_lower, int n_upper,
1068 isl_stat (*fn)(__isl_take isl_constraint *lower,
1069 __isl_take isl_constraint *upper,
1070 __isl_take isl_basic_set *bset, void *user), void *user)
1071 {
1072 isl_basic_set *context_i, *context_j;
1073 isl_constraint *lower = NULL;
1074 isl_constraint *upper = NULL;
1075 int i, j;
1076
1077 for (i = 0; i < bset->n_ineq; ++i) {
1078 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1079 continue;
1080
1081 context_i = set_largest_lower_bound(context, bset,
1082 abs_pos, n_lower, i);
1083 if (isl_basic_set_is_empty(context_i)) {
1084 isl_basic_set_free(context_i);
1085 continue;
1086 }
1087
1088 for (j = 0; j < bset->n_ineq; ++j) {
1089 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
1090 continue;
1091
1092 context_j = set_smallest_upper_bound(context_i, bset,
1093 abs_pos, n_upper, j);
1094 context_j = isl_basic_set_extend_constraints(context_j,
1095 0, 1);
1096 context_j = add_larger_bound_constraint(context_j,
1097 bset->ineq[i], bset->ineq[j], abs_pos, 0);
1098 context_j = isl_basic_set_simplify(context_j);
1099 context_j = isl_basic_set_finalize(context_j);
1100 if (isl_basic_set_is_empty(context_j)) {
1101 isl_basic_set_free(context_j);
1102 continue;
1103 }
1104 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1105 &bset->ineq[i]);
1106 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1107 &bset->ineq[j]);
1108 if (!lower || !upper || !context_j)
1109 goto error;
1110 if (fn(lower, upper, context_j, user) < 0)
1111 break;
1112 }
1113
1114 isl_basic_set_free(context_i);
1115
1116 if (j < bset->n_ineq)
1117 break;
1118 }
1119
1120 isl_basic_set_free(context);
1121
1122 if (i < bset->n_ineq)
1123 return isl_stat_error;
1124
1125 return isl_stat_ok;
1126 error:
1127 isl_constraint_free(lower);
1128 isl_constraint_free(upper);
1129 isl_basic_set_free(context_i);
1130 isl_basic_set_free(context_j);
1131 isl_basic_set_free(context);
1132 return isl_stat_error;
1133 }
1134
1135 /* For each pair of lower and upper bounds on the variable "pos"
1136 * of type "type", call "fn" with these lower and upper bounds and the
1137 * set of constraints on the remaining variables where these bounds
1138 * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1139 *
1140 * If the designated variable is equal to an affine combination of the
1141 * other variables then fn is called with both lower and upper
1142 * set to the corresponding equality.
1143 *
1144 * If there is no lower (or upper) bound, then NULL is passed
1145 * as the corresponding bound.
1146 *
1147 * We first check if the variable is involved in any equality.
1148 * If not, we count the number of lower and upper bounds and
1149 * act accordingly.
1150 */
isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned pos,isl_stat (* fn)(__isl_take isl_constraint * lower,__isl_take isl_constraint * upper,__isl_take isl_basic_set * bset,void * user),void * user)1151 isl_stat isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1152 enum isl_dim_type type, unsigned pos,
1153 isl_stat (*fn)(__isl_take isl_constraint *lower,
1154 __isl_take isl_constraint *upper,
1155 __isl_take isl_basic_set *bset, void *user), void *user)
1156 {
1157 int i;
1158 isl_constraint *lower = NULL;
1159 isl_constraint *upper = NULL;
1160 isl_basic_set *context = NULL;
1161 unsigned abs_pos;
1162 int n_lower, n_upper;
1163 isl_size off;
1164
1165 if (isl_basic_set_check_range(bset, type, pos, 1) < 0)
1166 return isl_stat_error;
1167 isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1168 return isl_stat_error);
1169
1170 off = isl_basic_set_var_offset(bset, type);
1171 if (off < 0)
1172 return isl_stat_error;
1173 abs_pos = off + pos;
1174
1175 for (i = 0; i < bset->n_eq; ++i) {
1176 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1177 continue;
1178
1179 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1180 &bset->eq[i]);
1181 upper = isl_constraint_copy(lower);
1182 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1183 type, pos, 1);
1184 if (!lower || !upper || !context)
1185 goto error;
1186 return fn(lower, upper, context, user);
1187 }
1188
1189 n_lower = 0;
1190 n_upper = 0;
1191 for (i = 0; i < bset->n_ineq; ++i) {
1192 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1193 n_lower++;
1194 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1195 n_upper++;
1196 }
1197
1198 context = isl_basic_set_copy(bset);
1199 context = isl_basic_set_cow(context);
1200 if (!context)
1201 goto error;
1202 for (i = context->n_ineq - 1; i >= 0; --i)
1203 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1204 isl_basic_set_drop_inequality(context, i);
1205
1206 context = isl_basic_set_drop(context, type, pos, 1);
1207 if (!n_lower && !n_upper)
1208 return fn(NULL, NULL, context, user);
1209 if (!n_lower)
1210 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1211 fn, user);
1212 if (!n_upper)
1213 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1214 fn, user);
1215 return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1216 fn, user);
1217 error:
1218 isl_constraint_free(lower);
1219 isl_constraint_free(upper);
1220 isl_basic_set_free(context);
1221 return isl_stat_error;
1222 }
1223
isl_constraint_get_bound(__isl_keep isl_constraint * constraint,enum isl_dim_type type,int pos)1224 __isl_give isl_aff *isl_constraint_get_bound(
1225 __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1226 {
1227 isl_space *space;
1228 isl_aff *aff;
1229 isl_ctx *ctx;
1230
1231 if (isl_constraint_check_range(constraint, type, pos, 1) < 0)
1232 return NULL;
1233 space = isl_constraint_peek_space(constraint);
1234 if (isl_space_check_is_set(space) < 0)
1235 return NULL;
1236
1237 ctx = isl_constraint_get_ctx(constraint);
1238 pos += offset(constraint, type);
1239 if (isl_int_is_zero(constraint->v->el[pos]))
1240 isl_die(ctx, isl_error_invalid,
1241 "constraint does not define a bound on given dimension",
1242 return NULL);
1243
1244 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1245 if (!aff)
1246 return NULL;
1247
1248 if (isl_int_is_neg(constraint->v->el[pos]))
1249 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1250 else
1251 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1252 isl_int_set_si(aff->v->el[1 + pos], 0);
1253 isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1254 aff = isl_aff_normalize(aff);
1255
1256 return aff;
1257 }
1258
1259 /* For an inequality constraint
1260 *
1261 * f >= 0
1262 *
1263 * or an equality constraint
1264 *
1265 * f = 0
1266 *
1267 * return the affine expression f.
1268 */
isl_constraint_get_aff(__isl_keep isl_constraint * constraint)1269 __isl_give isl_aff *isl_constraint_get_aff(
1270 __isl_keep isl_constraint *constraint)
1271 {
1272 isl_aff *aff;
1273
1274 if (!constraint)
1275 return NULL;
1276
1277 aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1278 if (!aff)
1279 return NULL;
1280
1281 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1282 isl_int_set_si(aff->v->el[0], 1);
1283
1284 return aff;
1285 }
1286
1287 /* Construct an inequality (eq = 0) or equality (eq = 1) constraint from "aff".
1288 * In particular, construct aff >= 0 or aff = 0.
1289 *
1290 * The denominator of "aff" can be ignored.
1291 */
isl_constraint_alloc_aff(int eq,__isl_take isl_aff * aff)1292 static __isl_give isl_constraint *isl_constraint_alloc_aff(int eq,
1293 __isl_take isl_aff *aff)
1294 {
1295 isl_local_space *ls;
1296 isl_vec *v;
1297
1298 if (!aff)
1299 return NULL;
1300 ls = isl_aff_get_domain_local_space(aff);
1301 v = isl_vec_drop_els(isl_vec_copy(aff->v), 0, 1);
1302 isl_aff_free(aff);
1303
1304 return isl_constraint_alloc_vec(eq, ls, v);
1305 }
1306
1307 /* Construct an equality constraint equating the given affine expression
1308 * to zero.
1309 */
isl_equality_from_aff(__isl_take isl_aff * aff)1310 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1311 {
1312 return isl_constraint_alloc_aff(1, aff);
1313 }
1314
1315 /* Construct an inequality constraint enforcing the given affine expression
1316 * to be non-negative.
1317 */
isl_inequality_from_aff(__isl_take isl_aff * aff)1318 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1319 {
1320 return isl_constraint_alloc_aff(0, aff);
1321 }
1322
1323 /* Compare two isl_constraints.
1324 *
1325 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1326 * than "c2" and 0 if they are equal.
1327 *
1328 * The order is fairly arbitrary. We do consider constraints that only involve
1329 * earlier dimensions as "smaller".
1330 */
isl_constraint_plain_cmp(__isl_keep isl_constraint * c1,__isl_keep isl_constraint * c2)1331 int isl_constraint_plain_cmp(__isl_keep isl_constraint *c1,
1332 __isl_keep isl_constraint *c2)
1333 {
1334 int cmp;
1335 int last1, last2;
1336
1337 if (c1 == c2)
1338 return 0;
1339 if (!c1)
1340 return -1;
1341 if (!c2)
1342 return 1;
1343 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1344 if (cmp != 0)
1345 return cmp;
1346
1347 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1348 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1349 if (last1 != last2)
1350 return last1 - last2;
1351
1352 return isl_seq_cmp(c1->v->el, c2->v->el, c1->v->size);
1353 }
1354
1355 /* Compare two constraints based on their final (non-zero) coefficients.
1356 * In particular, the constraint that involves later variables or
1357 * that has a larger coefficient for a shared latest variable
1358 * is considered "greater" than the other constraint.
1359 *
1360 * Return -1 if "c1" is "smaller" than "c2", 1 if "c1" is "greater"
1361 * than "c2" and 0 if they are equal.
1362 *
1363 * If the constraints live in different local spaces, then we cannot
1364 * really compare the constraints so we compare the local spaces instead.
1365 */
isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint * c1,__isl_keep isl_constraint * c2)1366 int isl_constraint_cmp_last_non_zero(__isl_keep isl_constraint *c1,
1367 __isl_keep isl_constraint *c2)
1368 {
1369 int cmp;
1370 int last1, last2;
1371
1372 if (c1 == c2)
1373 return 0;
1374 if (!c1)
1375 return -1;
1376 if (!c2)
1377 return 1;
1378 cmp = isl_local_space_cmp(c1->ls, c2->ls);
1379 if (cmp != 0)
1380 return cmp;
1381
1382 last1 = isl_seq_last_non_zero(c1->v->el + 1, c1->v->size - 1);
1383 last2 = isl_seq_last_non_zero(c2->v->el + 1, c1->v->size - 1);
1384 if (last1 != last2)
1385 return last1 - last2;
1386 if (last1 == -1)
1387 return 0;
1388 return isl_int_abs_cmp(c1->v->el[1 + last1], c2->v->el[1 + last2]);
1389 }
1390