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