82e8ef7941e7e6a9d4d73d90a220cb5394e971c2
[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 /* Replace the constant term of "constraint" by "v".
450  */
451 __isl_give isl_constraint *isl_constraint_set_constant_val(
452         __isl_take isl_constraint *constraint, __isl_take isl_val *v)
453 {
454         constraint = isl_constraint_cow(constraint);
455         if (!constraint || !v)
456                 goto error;
457         if (!isl_val_is_int(v))
458                 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
459                         "expecting integer value", goto error);
460         constraint->v = isl_vec_set_element_val(constraint->v, 0, v);
461         if (!constraint->v)
462                 constraint = isl_constraint_free(constraint);
463         return constraint;
464 error:
465         isl_val_free(v);
466         return isl_constraint_free(constraint);
467 }
468
469 __isl_give isl_constraint *isl_constraint_set_constant_si(
470         __isl_take isl_constraint *constraint, int v)
471 {
472         constraint = isl_constraint_cow(constraint);
473         if (!constraint)
474                 return NULL;
475
476         constraint->v = isl_vec_cow(constraint->v);
477         if (!constraint->v)
478                 return isl_constraint_free(constraint);
479
480         isl_int_set_si(constraint->v->el[0], v);
481         return constraint;
482 }
483
484 __isl_give isl_constraint *isl_constraint_set_coefficient(
485         __isl_take isl_constraint *constraint,
486         enum isl_dim_type type, int pos, isl_int v)
487 {
488         constraint = isl_constraint_cow(constraint);
489         if (!constraint)
490                 return NULL;
491
492         if (pos >= isl_local_space_dim(constraint->ls, type))
493                 isl_die(constraint->v->ctx, isl_error_invalid,
494                         "position out of bounds",
495                         return isl_constraint_free(constraint));
496
497         constraint = isl_constraint_cow(constraint);
498         if (!constraint)
499                 return NULL;
500
501         constraint->v = isl_vec_cow(constraint->v);
502         if (!constraint->v)
503                 return isl_constraint_free(constraint);
504
505         pos += isl_local_space_offset(constraint->ls, type);
506         isl_int_set(constraint->v->el[pos], v);
507
508         return constraint;
509 }
510
511 __isl_give isl_constraint *isl_constraint_set_coefficient_si(
512         __isl_take isl_constraint *constraint,
513         enum isl_dim_type type, int pos, int v)
514 {
515         constraint = isl_constraint_cow(constraint);
516         if (!constraint)
517                 return NULL;
518
519         if (pos >= isl_local_space_dim(constraint->ls, type))
520                 isl_die(constraint->v->ctx, isl_error_invalid,
521                         "position out of bounds",
522                         return isl_constraint_free(constraint));
523
524         constraint = isl_constraint_cow(constraint);
525         if (!constraint)
526                 return NULL;
527
528         constraint->v = isl_vec_cow(constraint->v);
529         if (!constraint->v)
530                 return isl_constraint_free(constraint);
531
532         pos += isl_local_space_offset(constraint->ls, type);
533         isl_int_set_si(constraint->v->el[pos], v);
534
535         return constraint;
536 }
537
538 /* Drop any constraint from "bset" that is identical to "constraint".
539  * In particular, this means that the local spaces of "bset" and
540  * "constraint" need to be the same.
541  *
542  * Since the given constraint may actually be a pointer into the bset,
543  * we have to be careful not to reorder the constraints as the user
544  * may be holding on to other constraints from the same bset.
545  * This should be cleaned up when the internal representation of
546  * isl_constraint is changed to use isl_aff.
547  */
548 __isl_give isl_basic_set *isl_basic_set_drop_constraint(
549         __isl_take isl_basic_set *bset, __isl_take isl_constraint *constraint)
550 {
551         int i;
552         unsigned n;
553         isl_int **row;
554         unsigned total;
555         isl_local_space *ls1;
556         int equal;
557
558         if (!bset || !constraint)
559                 goto error;
560
561         ls1 = isl_basic_set_get_local_space(bset);
562         equal = isl_local_space_is_equal(ls1, constraint->ls);
563         isl_local_space_free(ls1);
564         if (equal < 0)
565                 goto error;
566         if (!equal) {
567                 isl_constraint_free(constraint);
568                 return bset;
569         }
570
571         if (isl_constraint_is_equality(constraint)) {
572                 n = bset->n_eq;
573                 row = bset->eq;
574         } else {
575                 n = bset->n_ineq;
576                 row = bset->ineq;
577         }
578
579         total = isl_constraint_dim(constraint, isl_dim_all);
580         for (i = 0; i < n; ++i)
581                 if (isl_seq_eq(row[i], constraint->v->el, 1 + total))
582                         isl_seq_clr(row[i], 1 + total);
583                         
584         isl_constraint_free(constraint);
585         return bset;
586 error:
587         isl_constraint_free(constraint);
588         isl_basic_set_free(bset);
589         return NULL;
590 }
591
592 struct isl_constraint *isl_constraint_negate(struct isl_constraint *constraint)
593 {
594         isl_ctx *ctx;
595
596         constraint = isl_constraint_cow(constraint);
597         if (!constraint)
598                 return NULL;
599
600         ctx = isl_constraint_get_ctx(constraint);
601         if (isl_constraint_is_equality(constraint))
602                 isl_die(ctx, isl_error_invalid, "cannot negate equality",
603                         return isl_constraint_free(constraint));
604         constraint->v = isl_vec_neg(constraint->v);
605         constraint->v = isl_vec_cow(constraint->v);
606         if (!constraint->v)
607                 return isl_constraint_free(constraint);
608         isl_int_sub_ui(constraint->v->el[0], constraint->v->el[0], 1);
609         return constraint;
610 }
611
612 int isl_constraint_is_equality(struct isl_constraint *constraint)
613 {
614         if (!constraint)
615                 return -1;
616         return constraint->eq;
617 }
618
619 int isl_constraint_is_div_constraint(__isl_keep isl_constraint *constraint)
620 {
621         int i;
622         int n_div;
623
624         if (!constraint)
625                 return -1;
626         if (isl_constraint_is_equality(constraint))
627                 return 0;
628         n_div = isl_constraint_dim(constraint, isl_dim_div);
629         for (i = 0; i < n_div; ++i) {
630                 if (isl_local_space_is_div_constraint(constraint->ls,
631                                                         constraint->v->el, i))
632                         return 1;
633         }
634
635         return 0;
636 }
637
638 /* We manually set ISL_BASIC_SET_FINAL instead of calling
639  * isl_basic_map_finalize because we want to keep the position
640  * of the divs and we therefore do not want to throw away redundant divs.
641  * This is arguably a bit fragile.
642  */
643 __isl_give isl_basic_map *isl_basic_map_from_constraint(
644         __isl_take isl_constraint *constraint)
645 {
646         int k;
647         isl_local_space *ls;
648         struct isl_basic_map *bmap;
649         isl_int *c;
650         unsigned total;
651
652         if (!constraint)
653                 return NULL;
654
655         ls = isl_local_space_copy(constraint->ls);
656         bmap = isl_basic_map_from_local_space(ls);
657         bmap = isl_basic_map_extend_constraints(bmap, 1, 1);
658         if (isl_constraint_is_equality(constraint)) {
659                 k = isl_basic_map_alloc_equality(bmap);
660                 if (k < 0)
661                         goto error;
662                 c = bmap->eq[k];
663         }
664         else {
665                 k = isl_basic_map_alloc_inequality(bmap);
666                 if (k < 0)
667                         goto error;
668                 c = bmap->ineq[k];
669         }
670         total = isl_basic_map_total_dim(bmap);
671         isl_seq_cpy(c, constraint->v->el, 1 + total);
672         isl_constraint_free(constraint);
673         if (bmap)
674                 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
675         return bmap;
676 error:
677         isl_constraint_free(constraint);
678         isl_basic_map_free(bmap);
679         return NULL;
680 }
681
682 struct isl_basic_set *isl_basic_set_from_constraint(
683         struct isl_constraint *constraint)
684 {
685         if (!constraint)
686                 return NULL;
687
688         if (isl_constraint_dim(constraint, isl_dim_in) != 0)
689                 isl_die(isl_constraint_get_ctx(constraint), isl_error_invalid,
690                         "not a set constraint",
691                         return isl_constraint_free(constraint));
692         return (isl_basic_set *)isl_basic_map_from_constraint(constraint);
693 }
694
695 int isl_basic_map_has_defining_equality(
696         __isl_keep isl_basic_map *bmap, enum isl_dim_type type, int pos,
697         __isl_give isl_constraint **c)
698 {
699         int i;
700         unsigned offset;
701         unsigned total;
702
703         if (!bmap)
704                 return -1;
705         offset = basic_map_offset(bmap, type);
706         total = isl_basic_map_total_dim(bmap);
707         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
708         for (i = 0; i < bmap->n_eq; ++i)
709                 if (!isl_int_is_zero(bmap->eq[i][offset + pos]) &&
710                     isl_seq_first_non_zero(bmap->eq[i]+offset+pos+1,
711                                            1+total-offset-pos-1) == -1) {
712                         *c = isl_basic_map_constraint(isl_basic_map_copy(bmap),
713                                                                 &bmap->eq[i]);
714                         return 1;
715                 }
716         return 0;
717 }
718
719 int isl_basic_set_has_defining_equality(
720         __isl_keep isl_basic_set *bset, enum isl_dim_type type, int pos,
721         __isl_give isl_constraint **c)
722 {
723         return isl_basic_map_has_defining_equality((isl_basic_map *)bset,
724                                                     type, pos, c);
725 }
726
727 int isl_basic_set_has_defining_inequalities(
728         struct isl_basic_set *bset, enum isl_dim_type type, int pos,
729         struct isl_constraint **lower,
730         struct isl_constraint **upper)
731 {
732         int i, j;
733         unsigned offset;
734         unsigned total;
735         isl_int m;
736         isl_int **lower_line, **upper_line;
737
738         if (!bset)
739                 return -1;
740         offset = basic_set_offset(bset, type);
741         total = isl_basic_set_total_dim(bset);
742         isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
743         isl_int_init(m);
744         for (i = 0; i < bset->n_ineq; ++i) {
745                 if (isl_int_is_zero(bset->ineq[i][offset + pos]))
746                         continue;
747                 if (isl_int_is_one(bset->ineq[i][offset + pos]))
748                         continue;
749                 if (isl_int_is_negone(bset->ineq[i][offset + pos]))
750                         continue;
751                 if (isl_seq_first_non_zero(bset->ineq[i]+offset+pos+1,
752                                                 1+total-offset-pos-1) != -1)
753                         continue;
754                 for (j = i + 1; j < bset->n_ineq; ++j) {
755                         if (!isl_seq_is_neg(bset->ineq[i]+1, bset->ineq[j]+1,
756                                             total))
757                                 continue;
758                         isl_int_add(m, bset->ineq[i][0], bset->ineq[j][0]);
759                         if (isl_int_abs_ge(m, bset->ineq[i][offset+pos]))
760                                 continue;
761
762                         if (isl_int_is_pos(bset->ineq[i][offset+pos])) {
763                                 lower_line = &bset->ineq[i];
764                                 upper_line = &bset->ineq[j];
765                         } else {
766                                 lower_line = &bset->ineq[j];
767                                 upper_line = &bset->ineq[i];
768                         }
769                         *lower = isl_basic_set_constraint(
770                                         isl_basic_set_copy(bset), lower_line);
771                         *upper = isl_basic_set_constraint(
772                                         isl_basic_set_copy(bset), upper_line);
773                         isl_int_clear(m);
774                         return 1;
775                 }
776         }
777         *lower = NULL;
778         *upper = NULL;
779         isl_int_clear(m);
780         return 0;
781 }
782
783 /* Given two constraints "a" and "b" on the variable at position "abs_pos"
784  * (in "a" and "b"), add a constraint to "bset" that ensures that the
785  * bound implied by "a" is (strictly) larger than the bound implied by "b".
786  *
787  * If both constraints imply lower bounds, then this means that "a" is
788  * active in the result.
789  * If both constraints imply upper bounds, then this means that "b" is
790  * active in the result.
791  */
792 static __isl_give isl_basic_set *add_larger_bound_constraint(
793         __isl_take isl_basic_set *bset, isl_int *a, isl_int *b,
794         unsigned abs_pos, int strict)
795 {
796         int k;
797         isl_int t;
798         unsigned total;
799
800         k = isl_basic_set_alloc_inequality(bset);
801         if (k < 0)
802                 goto error;
803
804         total = isl_basic_set_dim(bset, isl_dim_all);
805
806         isl_int_init(t);
807         isl_int_neg(t, b[1 + abs_pos]);
808
809         isl_seq_combine(bset->ineq[k], t, a, a[1 + abs_pos], b, 1 + abs_pos);
810         isl_seq_combine(bset->ineq[k] + 1 + abs_pos,
811                 t, a + 1 + abs_pos + 1, a[1 + abs_pos], b + 1 + abs_pos + 1,
812                 total - abs_pos);
813
814         if (strict)
815                 isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
816
817         isl_int_clear(t);
818
819         return bset;
820 error:
821         isl_basic_set_free(bset);
822         return NULL;
823 }
824
825 /* Add constraints to "context" that ensure that "u" is the smallest
826  * (and therefore active) upper bound on "abs_pos" in "bset" and return
827  * the resulting basic set.
828  */
829 static __isl_give isl_basic_set *set_smallest_upper_bound(
830         __isl_keep isl_basic_set *context,
831         __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_upper, int u)
832 {
833         int j;
834
835         context = isl_basic_set_copy(context);
836         context = isl_basic_set_cow(context);
837
838         context = isl_basic_set_extend_constraints(context, 0, n_upper - 1);
839
840         for (j = 0; j < bset->n_ineq; ++j) {
841                 if (j == u)
842                         continue;
843                 if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
844                         continue;
845                 context = add_larger_bound_constraint(context,
846                         bset->ineq[j], bset->ineq[u], abs_pos, j > u);
847         }
848
849         context = isl_basic_set_simplify(context);
850         context = isl_basic_set_finalize(context);
851
852         return context;
853 }
854
855 /* Add constraints to "context" that ensure that "u" is the largest
856  * (and therefore active) upper bound on "abs_pos" in "bset" and return
857  * the resulting basic set.
858  */
859 static __isl_give isl_basic_set *set_largest_lower_bound(
860         __isl_keep isl_basic_set *context,
861         __isl_keep isl_basic_set *bset, unsigned abs_pos, int n_lower, int l)
862 {
863         int j;
864
865         context = isl_basic_set_copy(context);
866         context = isl_basic_set_cow(context);
867
868         context = isl_basic_set_extend_constraints(context, 0, n_lower - 1);
869
870         for (j = 0; j < bset->n_ineq; ++j) {
871                 if (j == l)
872                         continue;
873                 if (!isl_int_is_pos(bset->ineq[j][1 + abs_pos]))
874                         continue;
875                 context = add_larger_bound_constraint(context,
876                         bset->ineq[l], bset->ineq[j], abs_pos, j > l);
877         }
878
879         context = isl_basic_set_simplify(context);
880         context = isl_basic_set_finalize(context);
881
882         return context;
883 }
884
885 static int foreach_upper_bound(__isl_keep isl_basic_set *bset,
886         enum isl_dim_type type, unsigned abs_pos,
887         __isl_take isl_basic_set *context, int n_upper,
888         int (*fn)(__isl_take isl_constraint *lower,
889                   __isl_take isl_constraint *upper,
890                   __isl_take isl_basic_set *bset, void *user), void *user)
891 {
892         isl_basic_set *context_i;
893         isl_constraint *upper = NULL;
894         int i;
895
896         for (i = 0; i < bset->n_ineq; ++i) {
897                 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
898                         continue;
899
900                 context_i = set_smallest_upper_bound(context, bset,
901                                                         abs_pos, n_upper, i);
902                 if (isl_basic_set_is_empty(context_i)) {
903                         isl_basic_set_free(context_i);
904                         continue;
905                 }
906                 upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
907                                                 &bset->ineq[i]);
908                 if (!upper || !context_i)
909                         goto error;
910                 if (fn(NULL, upper, context_i, user) < 0)
911                         break;
912         }
913
914         isl_basic_set_free(context);
915
916         if (i < bset->n_ineq)
917                 return -1;
918
919         return 0;
920 error:
921         isl_constraint_free(upper);
922         isl_basic_set_free(context_i);
923         isl_basic_set_free(context);
924         return -1;
925 }
926
927 static int foreach_lower_bound(__isl_keep isl_basic_set *bset,
928         enum isl_dim_type type, unsigned abs_pos,
929         __isl_take isl_basic_set *context, int n_lower,
930         int (*fn)(__isl_take isl_constraint *lower,
931                   __isl_take isl_constraint *upper,
932                   __isl_take isl_basic_set *bset, void *user), void *user)
933 {
934         isl_basic_set *context_i;
935         isl_constraint *lower = NULL;
936         int i;
937
938         for (i = 0; i < bset->n_ineq; ++i) {
939                 if (isl_int_is_zero(bset->ineq[i][1 + abs_pos]))
940                         continue;
941
942                 context_i = set_largest_lower_bound(context, bset,
943                                                         abs_pos, n_lower, i);
944                 if (isl_basic_set_is_empty(context_i)) {
945                         isl_basic_set_free(context_i);
946                         continue;
947                 }
948                 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
949                                                 &bset->ineq[i]);
950                 if (!lower || !context_i)
951                         goto error;
952                 if (fn(lower, NULL, context_i, user) < 0)
953                         break;
954         }
955
956         isl_basic_set_free(context);
957
958         if (i < bset->n_ineq)
959                 return -1;
960
961         return 0;
962 error:
963         isl_constraint_free(lower);
964         isl_basic_set_free(context_i);
965         isl_basic_set_free(context);
966         return -1;
967 }
968
969 static int foreach_bound_pair(__isl_keep isl_basic_set *bset,
970         enum isl_dim_type type, unsigned abs_pos,
971         __isl_take isl_basic_set *context, int n_lower, int n_upper,
972         int (*fn)(__isl_take isl_constraint *lower,
973                   __isl_take isl_constraint *upper,
974                   __isl_take isl_basic_set *bset, void *user), void *user)
975 {
976         isl_basic_set *context_i, *context_j;
977         isl_constraint *lower = NULL;
978         isl_constraint *upper = NULL;
979         int i, j;
980
981         for (i = 0; i < bset->n_ineq; ++i) {
982                 if (!isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
983                         continue;
984
985                 context_i = set_largest_lower_bound(context, bset,
986                                                         abs_pos, n_lower, i);
987                 if (isl_basic_set_is_empty(context_i)) {
988                         isl_basic_set_free(context_i);
989                         continue;
990                 }
991
992                 for (j = 0; j < bset->n_ineq; ++j) {
993                         if (!isl_int_is_neg(bset->ineq[j][1 + abs_pos]))
994                                 continue;
995
996                         context_j = set_smallest_upper_bound(context_i, bset,
997                                                             abs_pos, n_upper, j);
998                         context_j = isl_basic_set_extend_constraints(context_j,
999                                                                         0, 1);
1000                         context_j = add_larger_bound_constraint(context_j,
1001                                 bset->ineq[i], bset->ineq[j], abs_pos, 0);
1002                         context_j = isl_basic_set_simplify(context_j);
1003                         context_j = isl_basic_set_finalize(context_j);
1004                         if (isl_basic_set_is_empty(context_j)) {
1005                                 isl_basic_set_free(context_j);
1006                                 continue;
1007                         }
1008                         lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1009                                                         &bset->ineq[i]);
1010                         upper = isl_basic_set_constraint(isl_basic_set_copy(bset),
1011                                                         &bset->ineq[j]);
1012                         if (!lower || !upper || !context_j)
1013                                 goto error;
1014                         if (fn(lower, upper, context_j, user) < 0)
1015                                 break;
1016                 }
1017
1018                 isl_basic_set_free(context_i);
1019
1020                 if (j < bset->n_ineq)
1021                         break;
1022         }
1023
1024         isl_basic_set_free(context);
1025
1026         if (i < bset->n_ineq)
1027                 return -1;
1028
1029         return 0;
1030 error:
1031         isl_constraint_free(lower);
1032         isl_constraint_free(upper);
1033         isl_basic_set_free(context_i);
1034         isl_basic_set_free(context_j);
1035         isl_basic_set_free(context);
1036         return -1;
1037 }
1038
1039 /* For each pair of lower and upper bounds on the variable "pos"
1040  * of type "type", call "fn" with these lower and upper bounds and the
1041  * set of constraints on the remaining variables where these bounds
1042  * are active, i.e., (stricly) larger/smaller than the other lower/upper bounds.
1043  *
1044  * If the designated variable is equal to an affine combination of the
1045  * other variables then fn is called with both lower and upper
1046  * set to the corresponding equality.
1047  *
1048  * If there is no lower (or upper) bound, then NULL is passed
1049  * as the corresponding bound.
1050  *
1051  * We first check if the variable is involved in any equality.
1052  * If not, we count the number of lower and upper bounds and
1053  * act accordingly.
1054  */
1055 int isl_basic_set_foreach_bound_pair(__isl_keep isl_basic_set *bset,
1056         enum isl_dim_type type, unsigned pos,
1057         int (*fn)(__isl_take isl_constraint *lower,
1058                   __isl_take isl_constraint *upper,
1059                   __isl_take isl_basic_set *bset, void *user), void *user)
1060 {
1061         int i;
1062         isl_constraint *lower = NULL;
1063         isl_constraint *upper = NULL;
1064         isl_basic_set *context = NULL;
1065         unsigned abs_pos;
1066         int n_lower, n_upper;
1067
1068         if (!bset)
1069                 return -1;
1070         isl_assert(bset->ctx, pos < isl_basic_set_dim(bset, type), return -1);
1071         isl_assert(bset->ctx, type == isl_dim_param || type == isl_dim_set,
1072                 return -1);
1073
1074         abs_pos = pos;
1075         if (type == isl_dim_set)
1076                 abs_pos += isl_basic_set_dim(bset, isl_dim_param);
1077
1078         for (i = 0; i < bset->n_eq; ++i) {
1079                 if (isl_int_is_zero(bset->eq[i][1 + abs_pos]))
1080                         continue;
1081
1082                 lower = isl_basic_set_constraint(isl_basic_set_copy(bset),
1083                                                 &bset->eq[i]);
1084                 upper = isl_constraint_copy(lower);
1085                 context = isl_basic_set_remove_dims(isl_basic_set_copy(bset),
1086                                         type, pos, 1);
1087                 if (!lower || !upper || !context)
1088                         goto error;
1089                 return fn(lower, upper, context, user);
1090         }
1091
1092         n_lower = 0;
1093         n_upper = 0;
1094         for (i = 0; i < bset->n_ineq; ++i) {
1095                 if (isl_int_is_pos(bset->ineq[i][1 + abs_pos]))
1096                         n_lower++;
1097                 else if (isl_int_is_neg(bset->ineq[i][1 + abs_pos]))
1098                         n_upper++;
1099         }
1100
1101         context = isl_basic_set_copy(bset);
1102         context = isl_basic_set_cow(context);
1103         if (!context)
1104                 goto error;
1105         for (i = context->n_ineq - 1; i >= 0; --i)
1106                 if (!isl_int_is_zero(context->ineq[i][1 + abs_pos]))
1107                         isl_basic_set_drop_inequality(context, i);
1108
1109         context = isl_basic_set_drop(context, type, pos, 1);
1110         if (!n_lower && !n_upper)
1111                 return fn(NULL, NULL, context, user);
1112         if (!n_lower)
1113                 return foreach_upper_bound(bset, type, abs_pos, context, n_upper,
1114                                                 fn, user);
1115         if (!n_upper)
1116                 return foreach_lower_bound(bset, type, abs_pos, context, n_lower,
1117                                                 fn, user);
1118         return foreach_bound_pair(bset, type, abs_pos, context, n_lower, n_upper,
1119                                         fn, user);
1120 error:
1121         isl_constraint_free(lower);
1122         isl_constraint_free(upper);
1123         isl_basic_set_free(context);
1124         return -1;
1125 }
1126
1127 __isl_give isl_aff *isl_constraint_get_bound(
1128         __isl_keep isl_constraint *constraint, enum isl_dim_type type, int pos)
1129 {
1130         isl_aff *aff;
1131         isl_ctx *ctx;
1132
1133         if (!constraint)
1134                 return NULL;
1135         ctx = isl_constraint_get_ctx(constraint);
1136         if (pos >= isl_constraint_dim(constraint, type))
1137                 isl_die(ctx, isl_error_invalid,
1138                         "index out of bounds", return NULL);
1139         if (isl_constraint_dim(constraint, isl_dim_in) != 0)
1140                 isl_die(ctx, isl_error_invalid,
1141                         "not a set constraint", return NULL);
1142
1143         pos += offset(constraint, type);
1144         if (isl_int_is_zero(constraint->v->el[pos]))
1145                 isl_die(ctx, isl_error_invalid,
1146                         "constraint does not define a bound on given dimension",
1147                         return NULL);
1148
1149         aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1150         if (!aff)
1151                 return NULL;
1152
1153         if (isl_int_is_neg(constraint->v->el[pos]))
1154                 isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1155         else
1156                 isl_seq_neg(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1157         isl_int_set_si(aff->v->el[1 + pos], 0);
1158         isl_int_abs(aff->v->el[0], constraint->v->el[pos]);
1159
1160         return aff;
1161 }
1162
1163 /* For an inequality constraint
1164  *
1165  *      f >= 0
1166  *
1167  * or an equality constraint
1168  *
1169  *      f = 0
1170  *
1171  * return the affine expression f.
1172  */
1173 __isl_give isl_aff *isl_constraint_get_aff(
1174         __isl_keep isl_constraint *constraint)
1175 {
1176         isl_aff *aff;
1177
1178         if (!constraint)
1179                 return NULL;
1180
1181         aff = isl_aff_alloc(isl_local_space_copy(constraint->ls));
1182         if (!aff)
1183                 return NULL;
1184
1185         isl_seq_cpy(aff->v->el + 1, constraint->v->el, aff->v->size - 1);
1186         isl_int_set_si(aff->v->el[0], 1);
1187
1188         return aff;
1189 }
1190
1191 /* Construct an equality constraint equating the given affine expression
1192  * to zero.
1193  */
1194 __isl_give isl_constraint *isl_equality_from_aff(__isl_take isl_aff *aff)
1195 {
1196         int k;
1197         isl_local_space *ls;
1198         isl_basic_set *bset;
1199
1200         if (!aff)
1201                 return NULL;
1202
1203         ls = isl_aff_get_domain_local_space(aff);
1204         bset = isl_basic_set_from_local_space(ls);
1205         bset = isl_basic_set_extend_constraints(bset, 1, 0);
1206         k = isl_basic_set_alloc_equality(bset);
1207         if (k < 0)
1208                 goto error;
1209
1210         isl_seq_cpy(bset->eq[k], aff->v->el + 1, aff->v->size - 1);
1211         isl_aff_free(aff);
1212
1213         return isl_basic_set_constraint(bset, &bset->eq[k]);
1214 error:
1215         isl_aff_free(aff);
1216         isl_basic_set_free(bset);
1217         return NULL;
1218 }
1219
1220 /* Construct an inequality constraint enforcing the given affine expression
1221  * to be non-negative.
1222  */
1223 __isl_give isl_constraint *isl_inequality_from_aff(__isl_take isl_aff *aff)
1224 {
1225         int k;
1226         isl_local_space *ls;
1227         isl_basic_set *bset;
1228
1229         if (!aff)
1230                 return NULL;
1231
1232         ls = isl_aff_get_domain_local_space(aff);
1233         bset = isl_basic_set_from_local_space(ls);
1234         bset = isl_basic_set_extend_constraints(bset, 0, 1);
1235         k = isl_basic_set_alloc_inequality(bset);
1236         if (k < 0)
1237                 goto error;
1238
1239         isl_seq_cpy(bset->ineq[k], aff->v->el + 1, aff->v->size - 1);
1240         isl_aff_free(aff);
1241
1242         return isl_basic_set_constraint(bset, &bset->ineq[k]);
1243 error:
1244         isl_aff_free(aff);
1245         isl_basic_set_free(bset);
1246         return NULL;
1247 }