add isl_aff_set_constant_val
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  * Copyright 2011      Sven Verdoolaege
4  * Copyright 2012-2013 Ecole Normale Superieure
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10  * 91893 Orsay, France
11  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12  */
13
14 #include <isl_ctx_private.h>
15 #define ISL_DIM_H
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl_aff_private.h>
19 #include <isl_space_private.h>
20 #include <isl_local_space_private.h>
21 #include <isl_mat_private.h>
22 #include <isl/constraint.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl_val_private.h>
26 #include <isl_config.h>
27
28 #undef BASE
29 #define BASE aff
30
31 #include <isl_list_templ.c>
32
33 #undef BASE
34 #define BASE pw_aff
35
36 #include <isl_list_templ.c>
37
38 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
39         __isl_take isl_vec *v)
40 {
41         isl_aff *aff;
42
43         if (!ls || !v)
44                 goto error;
45
46         aff = isl_calloc_type(v->ctx, struct isl_aff);
47         if (!aff)
48                 goto error;
49
50         aff->ref = 1;
51         aff->ls = ls;
52         aff->v = v;
53
54         return aff;
55 error:
56         isl_local_space_free(ls);
57         isl_vec_free(v);
58         return NULL;
59 }
60
61 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
62 {
63         isl_ctx *ctx;
64         isl_vec *v;
65         unsigned total;
66
67         if (!ls)
68                 return NULL;
69
70         ctx = isl_local_space_get_ctx(ls);
71         if (!isl_local_space_divs_known(ls))
72                 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
73                         goto error);
74         if (!isl_local_space_is_set(ls))
75                 isl_die(ctx, isl_error_invalid,
76                         "domain of affine expression should be a set",
77                         goto error);
78
79         total = isl_local_space_dim(ls, isl_dim_all);
80         v = isl_vec_alloc(ctx, 1 + 1 + total);
81         return isl_aff_alloc_vec(ls, v);
82 error:
83         isl_local_space_free(ls);
84         return NULL;
85 }
86
87 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
88 {
89         isl_aff *aff;
90
91         aff = isl_aff_alloc(ls);
92         if (!aff)
93                 return NULL;
94
95         isl_int_set_si(aff->v->el[0], 1);
96         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
97
98         return aff;
99 }
100
101 /* Return a piecewise affine expression defined on the specified domain
102  * that is equal to zero.
103  */
104 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
105 {
106         return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
107 }
108
109 /* Return an affine expression that is equal to the specified dimension
110  * in "ls".
111  */
112 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
113         enum isl_dim_type type, unsigned pos)
114 {
115         isl_space *space;
116         isl_aff *aff;
117
118         if (!ls)
119                 return NULL;
120
121         space = isl_local_space_get_space(ls);
122         if (!space)
123                 goto error;
124         if (isl_space_is_map(space))
125                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
126                         "expecting (parameter) set space", goto error);
127         if (pos >= isl_local_space_dim(ls, type))
128                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
129                         "position out of bounds", goto error);
130
131         isl_space_free(space);
132         aff = isl_aff_alloc(ls);
133         if (!aff)
134                 return NULL;
135
136         pos += isl_local_space_offset(aff->ls, type);
137
138         isl_int_set_si(aff->v->el[0], 1);
139         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
140         isl_int_set_si(aff->v->el[1 + pos], 1);
141
142         return aff;
143 error:
144         isl_local_space_free(ls);
145         isl_space_free(space);
146         return NULL;
147 }
148
149 /* Return a piecewise affine expression that is equal to
150  * the specified dimension in "ls".
151  */
152 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
153         enum isl_dim_type type, unsigned pos)
154 {
155         return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
156 }
157
158 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
159 {
160         if (!aff)
161                 return NULL;
162
163         aff->ref++;
164         return aff;
165 }
166
167 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
168 {
169         if (!aff)
170                 return NULL;
171
172         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
173                                  isl_vec_copy(aff->v));
174 }
175
176 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
177 {
178         if (!aff)
179                 return NULL;
180
181         if (aff->ref == 1)
182                 return aff;
183         aff->ref--;
184         return isl_aff_dup(aff);
185 }
186
187 void *isl_aff_free(__isl_take isl_aff *aff)
188 {
189         if (!aff)
190                 return NULL;
191
192         if (--aff->ref > 0)
193                 return NULL;
194
195         isl_local_space_free(aff->ls);
196         isl_vec_free(aff->v);
197
198         free(aff);
199
200         return NULL;
201 }
202
203 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
204 {
205         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
206 }
207
208 /* Externally, an isl_aff has a map space, but internally, the
209  * ls field corresponds to the domain of that space.
210  */
211 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
212 {
213         if (!aff)
214                 return 0;
215         if (type == isl_dim_out)
216                 return 1;
217         if (type == isl_dim_in)
218                 type = isl_dim_set;
219         return isl_local_space_dim(aff->ls, type);
220 }
221
222 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
223 {
224         return aff ? isl_local_space_get_space(aff->ls) : NULL;
225 }
226
227 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
228 {
229         isl_space *space;
230         if (!aff)
231                 return NULL;
232         space = isl_local_space_get_space(aff->ls);
233         space = isl_space_from_domain(space);
234         space = isl_space_add_dims(space, isl_dim_out, 1);
235         return space;
236 }
237
238 __isl_give isl_local_space *isl_aff_get_domain_local_space(
239         __isl_keep isl_aff *aff)
240 {
241         return aff ? isl_local_space_copy(aff->ls) : NULL;
242 }
243
244 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
245 {
246         isl_local_space *ls;
247         if (!aff)
248                 return NULL;
249         ls = isl_local_space_copy(aff->ls);
250         ls = isl_local_space_from_domain(ls);
251         ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
252         return ls;
253 }
254
255 /* Externally, an isl_aff has a map space, but internally, the
256  * ls field corresponds to the domain of that space.
257  */
258 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
259         enum isl_dim_type type, unsigned pos)
260 {
261         if (!aff)
262                 return NULL;
263         if (type == isl_dim_out)
264                 return NULL;
265         if (type == isl_dim_in)
266                 type = isl_dim_set;
267         return isl_local_space_get_dim_name(aff->ls, type, pos);
268 }
269
270 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
271         __isl_take isl_space *dim)
272 {
273         aff = isl_aff_cow(aff);
274         if (!aff || !dim)
275                 goto error;
276
277         aff->ls = isl_local_space_reset_space(aff->ls, dim);
278         if (!aff->ls)
279                 return isl_aff_free(aff);
280
281         return aff;
282 error:
283         isl_aff_free(aff);
284         isl_space_free(dim);
285         return NULL;
286 }
287
288 /* Reset the space of "aff".  This function is called from isl_pw_templ.c
289  * and doesn't know if the space of an element object is represented
290  * directly or through its domain.  It therefore passes along both.
291  */
292 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
293         __isl_take isl_space *space, __isl_take isl_space *domain)
294 {
295         isl_space_free(space);
296         return isl_aff_reset_domain_space(aff, domain);
297 }
298
299 /* Reorder the coefficients of the affine expression based
300  * on the given reodering.
301  * The reordering r is assumed to have been extended with the local
302  * variables.
303  */
304 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
305         __isl_take isl_reordering *r, int n_div)
306 {
307         isl_vec *res;
308         int i;
309
310         if (!vec || !r)
311                 goto error;
312
313         res = isl_vec_alloc(vec->ctx,
314                             2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
315         isl_seq_cpy(res->el, vec->el, 2);
316         isl_seq_clr(res->el + 2, res->size - 2);
317         for (i = 0; i < r->len; ++i)
318                 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
319
320         isl_reordering_free(r);
321         isl_vec_free(vec);
322         return res;
323 error:
324         isl_vec_free(vec);
325         isl_reordering_free(r);
326         return NULL;
327 }
328
329 /* Reorder the dimensions of the domain of "aff" according
330  * to the given reordering.
331  */
332 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
333         __isl_take isl_reordering *r)
334 {
335         aff = isl_aff_cow(aff);
336         if (!aff)
337                 goto error;
338
339         r = isl_reordering_extend(r, aff->ls->div->n_row);
340         aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
341                                 aff->ls->div->n_row);
342         aff->ls = isl_local_space_realign(aff->ls, r);
343
344         if (!aff->v || !aff->ls)
345                 return isl_aff_free(aff);
346
347         return aff;
348 error:
349         isl_aff_free(aff);
350         isl_reordering_free(r);
351         return NULL;
352 }
353
354 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
355         __isl_take isl_space *model)
356 {
357         if (!aff || !model)
358                 goto error;
359
360         if (!isl_space_match(aff->ls->dim, isl_dim_param,
361                              model, isl_dim_param)) {
362                 isl_reordering *exp;
363
364                 model = isl_space_drop_dims(model, isl_dim_in,
365                                         0, isl_space_dim(model, isl_dim_in));
366                 model = isl_space_drop_dims(model, isl_dim_out,
367                                         0, isl_space_dim(model, isl_dim_out));
368                 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
369                 exp = isl_reordering_extend_space(exp,
370                                         isl_aff_get_domain_space(aff));
371                 aff = isl_aff_realign_domain(aff, exp);
372         }
373
374         isl_space_free(model);
375         return aff;
376 error:
377         isl_space_free(model);
378         isl_aff_free(aff);
379         return NULL;
380 }
381
382 int isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
383 {
384         if (!aff)
385                 return -1;
386
387         return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
388 }
389
390 int isl_aff_plain_is_equal(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
391 {
392         int equal;
393
394         if (!aff1 || !aff2)
395                 return -1;
396
397         equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
398         if (equal < 0 || !equal)
399                 return equal;
400
401         return isl_vec_is_equal(aff1->v, aff2->v);
402 }
403
404 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
405 {
406         if (!aff)
407                 return -1;
408         isl_int_set(*v, aff->v->el[0]);
409         return 0;
410 }
411
412 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
413 {
414         if (!aff)
415                 return -1;
416         isl_int_set(*v, aff->v->el[1]);
417         return 0;
418 }
419
420 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
421         enum isl_dim_type type, int pos, isl_int *v)
422 {
423         if (!aff)
424                 return -1;
425
426         if (type == isl_dim_out)
427                 isl_die(aff->v->ctx, isl_error_invalid,
428                         "output/set dimension does not have a coefficient",
429                         return -1);
430         if (type == isl_dim_in)
431                 type = isl_dim_set;
432
433         if (pos >= isl_local_space_dim(aff->ls, type))
434                 isl_die(aff->v->ctx, isl_error_invalid,
435                         "position out of bounds", return -1);
436
437         pos += isl_local_space_offset(aff->ls, type);
438         isl_int_set(*v, aff->v->el[1 + pos]);
439
440         return 0;
441 }
442
443 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
444 {
445         aff = isl_aff_cow(aff);
446         if (!aff)
447                 return NULL;
448
449         aff->v = isl_vec_cow(aff->v);
450         if (!aff->v)
451                 return isl_aff_free(aff);
452
453         isl_int_set(aff->v->el[0], v);
454
455         return aff;
456 }
457
458 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
459 {
460         aff = isl_aff_cow(aff);
461         if (!aff)
462                 return NULL;
463
464         aff->v = isl_vec_cow(aff->v);
465         if (!aff->v)
466                 return isl_aff_free(aff);
467
468         isl_int_set(aff->v->el[1], v);
469
470         return aff;
471 }
472
473 /* Replace the constant term of "aff" by "v".
474  */
475 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
476         __isl_take isl_val *v)
477 {
478         if (!aff || !v)
479                 goto error;
480
481         if (!isl_val_is_rat(v))
482                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
483                         "expecting rational value", goto error);
484
485         if (isl_int_eq(aff->v->el[1], v->n) &&
486             isl_int_eq(aff->v->el[0], v->d)) {
487                 isl_val_free(v);
488                 return aff;
489         }
490
491         aff = isl_aff_cow(aff);
492         if (!aff)
493                 goto error;
494         aff->v = isl_vec_cow(aff->v);
495         if (!aff->v)
496                 goto error;
497
498         if (isl_int_eq(aff->v->el[0], v->d)) {
499                 isl_int_set(aff->v->el[1], v->n);
500         } else if (isl_int_is_one(v->d)) {
501                 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
502         } else {
503                 isl_seq_scale(aff->v->el + 1,
504                                 aff->v->el + 1, v->d, aff->v->size - 1);
505                 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
506                 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
507                 aff->v = isl_vec_normalize(aff->v);
508                 if (!aff->v)
509                         goto error;
510         }
511
512         isl_val_free(v);
513         return aff;
514 error:
515         isl_aff_free(aff);
516         isl_val_free(v);
517         return NULL;
518 }
519
520 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
521 {
522         if (isl_int_is_zero(v))
523                 return aff;
524
525         aff = isl_aff_cow(aff);
526         if (!aff)
527                 return NULL;
528
529         aff->v = isl_vec_cow(aff->v);
530         if (!aff->v)
531                 return isl_aff_free(aff);
532
533         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
534
535         return aff;
536 }
537
538 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
539 {
540         isl_int t;
541
542         isl_int_init(t);
543         isl_int_set_si(t, v);
544         aff = isl_aff_add_constant(aff, t);
545         isl_int_clear(t);
546
547         return aff;
548 }
549
550 /* Add "v" to the numerator of the constant term of "aff".
551  */
552 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
553 {
554         if (isl_int_is_zero(v))
555                 return aff;
556
557         aff = isl_aff_cow(aff);
558         if (!aff)
559                 return NULL;
560
561         aff->v = isl_vec_cow(aff->v);
562         if (!aff->v)
563                 return isl_aff_free(aff);
564
565         isl_int_add(aff->v->el[1], aff->v->el[1], v);
566
567         return aff;
568 }
569
570 /* Add "v" to the numerator of the constant term of "aff".
571  */
572 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
573 {
574         isl_int t;
575
576         if (v == 0)
577                 return aff;
578
579         isl_int_init(t);
580         isl_int_set_si(t, v);
581         aff = isl_aff_add_constant_num(aff, t);
582         isl_int_clear(t);
583
584         return aff;
585 }
586
587 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
588 {
589         aff = isl_aff_cow(aff);
590         if (!aff)
591                 return NULL;
592
593         aff->v = isl_vec_cow(aff->v);
594         if (!aff->v)
595                 return isl_aff_free(aff);
596
597         isl_int_set_si(aff->v->el[1], v);
598
599         return aff;
600 }
601
602 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
603         enum isl_dim_type type, int pos, isl_int v)
604 {
605         if (!aff)
606                 return NULL;
607
608         if (type == isl_dim_out)
609                 isl_die(aff->v->ctx, isl_error_invalid,
610                         "output/set dimension does not have a coefficient",
611                         return isl_aff_free(aff));
612         if (type == isl_dim_in)
613                 type = isl_dim_set;
614
615         if (pos >= isl_local_space_dim(aff->ls, type))
616                 isl_die(aff->v->ctx, isl_error_invalid,
617                         "position out of bounds", return isl_aff_free(aff));
618
619         aff = isl_aff_cow(aff);
620         if (!aff)
621                 return NULL;
622
623         aff->v = isl_vec_cow(aff->v);
624         if (!aff->v)
625                 return isl_aff_free(aff);
626
627         pos += isl_local_space_offset(aff->ls, type);
628         isl_int_set(aff->v->el[1 + pos], v);
629
630         return aff;
631 }
632
633 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
634         enum isl_dim_type type, int pos, int v)
635 {
636         if (!aff)
637                 return NULL;
638
639         if (type == isl_dim_out)
640                 isl_die(aff->v->ctx, isl_error_invalid,
641                         "output/set dimension does not have a coefficient",
642                         return isl_aff_free(aff));
643         if (type == isl_dim_in)
644                 type = isl_dim_set;
645
646         if (pos >= isl_local_space_dim(aff->ls, type))
647                 isl_die(aff->v->ctx, isl_error_invalid,
648                         "position out of bounds", return isl_aff_free(aff));
649
650         aff = isl_aff_cow(aff);
651         if (!aff)
652                 return NULL;
653
654         aff->v = isl_vec_cow(aff->v);
655         if (!aff->v)
656                 return isl_aff_free(aff);
657
658         pos += isl_local_space_offset(aff->ls, type);
659         isl_int_set_si(aff->v->el[1 + pos], v);
660
661         return aff;
662 }
663
664 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
665         enum isl_dim_type type, int pos, isl_int v)
666 {
667         if (!aff)
668                 return NULL;
669
670         if (type == isl_dim_out)
671                 isl_die(aff->v->ctx, isl_error_invalid,
672                         "output/set dimension does not have a coefficient",
673                         return isl_aff_free(aff));
674         if (type == isl_dim_in)
675                 type = isl_dim_set;
676
677         if (pos >= isl_local_space_dim(aff->ls, type))
678                 isl_die(aff->v->ctx, isl_error_invalid,
679                         "position out of bounds", return isl_aff_free(aff));
680
681         aff = isl_aff_cow(aff);
682         if (!aff)
683                 return NULL;
684
685         aff->v = isl_vec_cow(aff->v);
686         if (!aff->v)
687                 return isl_aff_free(aff);
688
689         pos += isl_local_space_offset(aff->ls, type);
690         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
691
692         return aff;
693 }
694
695 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
696         enum isl_dim_type type, int pos, int v)
697 {
698         isl_int t;
699
700         isl_int_init(t);
701         isl_int_set_si(t, v);
702         aff = isl_aff_add_coefficient(aff, type, pos, t);
703         isl_int_clear(t);
704
705         return aff;
706 }
707
708 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
709 {
710         if (!aff)
711                 return NULL;
712
713         return isl_local_space_get_div(aff->ls, pos);
714 }
715
716 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
717 {
718         aff = isl_aff_cow(aff);
719         if (!aff)
720                 return NULL;
721         aff->v = isl_vec_cow(aff->v);
722         if (!aff->v)
723                 return isl_aff_free(aff);
724
725         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
726
727         return aff;
728 }
729
730 /* Remove divs from the local space that do not appear in the affine
731  * expression.
732  * We currently only remove divs at the end.
733  * Some intermediate divs may also not appear directly in the affine
734  * expression, but we would also need to check that no other divs are
735  * defined in terms of them.
736  */
737 __isl_give isl_aff *isl_aff_remove_unused_divs( __isl_take isl_aff *aff)
738 {
739         int pos;
740         int off;
741         int n;
742
743         if (!aff)
744                 return NULL;
745
746         n = isl_local_space_dim(aff->ls, isl_dim_div);
747         off = isl_local_space_offset(aff->ls, isl_dim_div);
748
749         pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
750         if (pos == n)
751                 return aff;
752
753         aff = isl_aff_cow(aff);
754         if (!aff)
755                 return NULL;
756
757         aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
758         aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
759         if (!aff->ls || !aff->v)
760                 return isl_aff_free(aff);
761
762         return aff;
763 }
764
765 /* Given two affine expressions "p" of length p_len (including the
766  * denominator and the constant term) and "subs" of length subs_len,
767  * plug in "subs" for the variable at position "pos".
768  * The variables of "subs" and "p" are assumed to match up to subs_len,
769  * but "p" may have additional variables.
770  * "v" is an initialized isl_int that can be used internally.
771  *
772  * In particular, if "p" represents the expression
773  *
774  *      (a i + g)/m
775  *
776  * with i the variable at position "pos" and "subs" represents the expression
777  *
778  *      f/d
779  *
780  * then the result represents the expression
781  *
782  *      (a f + d g)/(m d)
783  *
784  */
785 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
786         int p_len, int subs_len, isl_int v)
787 {
788         isl_int_set(v, p[1 + pos]);
789         isl_int_set_si(p[1 + pos], 0);
790         isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
791         isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
792         isl_int_mul(p[0], p[0], subs[0]);
793 }
794
795 /* Look for any divs in the aff->ls with a denominator equal to one
796  * and plug them into the affine expression and any subsequent divs
797  * that may reference the div.
798  */
799 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
800 {
801         int i, n;
802         int len;
803         isl_int v;
804         isl_vec *vec;
805         isl_local_space *ls;
806         unsigned pos;
807
808         if (!aff)
809                 return NULL;
810
811         n = isl_local_space_dim(aff->ls, isl_dim_div);
812         len = aff->v->size;
813         for (i = 0; i < n; ++i) {
814                 if (!isl_int_is_one(aff->ls->div->row[i][0]))
815                         continue;
816                 ls = isl_local_space_copy(aff->ls);
817                 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
818                                 aff->ls->div->row[i], len, i + 1, n - (i + 1));
819                 vec = isl_vec_copy(aff->v);
820                 vec = isl_vec_cow(vec);
821                 if (!ls || !vec)
822                         goto error;
823
824                 isl_int_init(v);
825
826                 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
827                 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
828                                         len, len, v);
829
830                 isl_int_clear(v);
831
832                 isl_vec_free(aff->v);
833                 aff->v = vec;
834                 isl_local_space_free(aff->ls);
835                 aff->ls = ls;
836         }
837
838         return aff;
839 error:
840         isl_vec_free(vec);
841         isl_local_space_free(ls);
842         return isl_aff_free(aff);
843 }
844
845 /* Look for any divs j that appear with a unit coefficient inside
846  * the definitions of other divs i and plug them into the definitions
847  * of the divs i.
848  *
849  * In particular, an expression of the form
850  *
851  *      floor((f(..) + floor(g(..)/n))/m)
852  *
853  * is simplified to
854  *
855  *      floor((n * f(..) + g(..))/(n * m))
856  *
857  * This simplification is correct because we can move the expression
858  * f(..) into the inner floor in the original expression to obtain
859  *
860  *      floor(floor((n * f(..) + g(..))/n)/m)
861  *
862  * from which we can derive the simplified expression.
863  */
864 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
865 {
866         int i, j, n;
867         int off;
868
869         if (!aff)
870                 return NULL;
871
872         n = isl_local_space_dim(aff->ls, isl_dim_div);
873         off = isl_local_space_offset(aff->ls, isl_dim_div);
874         for (i = 1; i < n; ++i) {
875                 for (j = 0; j < i; ++j) {
876                         if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
877                                 continue;
878                         aff->ls = isl_local_space_substitute_seq(aff->ls,
879                                 isl_dim_div, j, aff->ls->div->row[j],
880                                 aff->v->size, i, 1);
881                         if (!aff->ls)
882                                 return isl_aff_free(aff);
883                 }
884         }
885
886         return aff;
887 }
888
889 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
890  *
891  * Even though this function is only called on isl_affs with a single
892  * reference, we are careful to only change aff->v and aff->ls together.
893  */
894 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
895 {
896         unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
897         isl_local_space *ls;
898         isl_vec *v;
899
900         ls = isl_local_space_copy(aff->ls);
901         ls = isl_local_space_swap_div(ls, a, b);
902         v = isl_vec_copy(aff->v);
903         v = isl_vec_cow(v);
904         if (!ls || !v)
905                 goto error;
906
907         isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
908         isl_vec_free(aff->v);
909         aff->v = v;
910         isl_local_space_free(aff->ls);
911         aff->ls = ls;
912
913         return aff;
914 error:
915         isl_vec_free(v);
916         isl_local_space_free(ls);
917         return isl_aff_free(aff);
918 }
919
920 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
921  *
922  * We currently do not actually remove div "b", but simply add its
923  * coefficient to that of "a" and then zero it out.
924  */
925 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
926 {
927         unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
928
929         if (isl_int_is_zero(aff->v->el[1 + off + b]))
930                 return aff;
931
932         aff->v = isl_vec_cow(aff->v);
933         if (!aff->v)
934                 return isl_aff_free(aff);
935
936         isl_int_add(aff->v->el[1 + off + a],
937                     aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
938         isl_int_set_si(aff->v->el[1 + off + b], 0);
939
940         return aff;
941 }
942
943 /* Sort the divs in the local space of "aff" according to
944  * the comparison function "cmp_row" in isl_local_space.c,
945  * combining the coefficients of identical divs.
946  *
947  * Reordering divs does not change the semantics of "aff",
948  * so there is no need to call isl_aff_cow.
949  * Moreover, this function is currently only called on isl_affs
950  * with a single reference.
951  */
952 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
953 {
954         int i, j, n;
955         unsigned off;
956
957         if (!aff)
958                 return NULL;
959
960         off = isl_local_space_offset(aff->ls, isl_dim_div);
961         n = isl_aff_dim(aff, isl_dim_div);
962         for (i = 1; i < n; ++i) {
963                 for (j = i - 1; j >= 0; --j) {
964                         int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
965                         if (cmp < 0)
966                                 break;
967                         if (cmp == 0)
968                                 aff = merge_divs(aff, j, j + 1);
969                         else
970                                 aff = swap_div(aff, j, j + 1);
971                         if (!aff)
972                                 return NULL;
973                 }
974         }
975
976         return aff;
977 }
978
979 /* Normalize the representation of "aff".
980  *
981  * This function should only be called of "new" isl_affs, i.e.,
982  * with only a single reference.  We therefore do not need to
983  * worry about affecting other instances.
984  */
985 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
986 {
987         if (!aff)
988                 return NULL;
989         aff->v = isl_vec_normalize(aff->v);
990         if (!aff->v)
991                 return isl_aff_free(aff);
992         aff = plug_in_integral_divs(aff);
993         aff = plug_in_unit_divs(aff);
994         aff = sort_divs(aff);
995         aff = isl_aff_remove_unused_divs(aff);
996         return aff;
997 }
998
999 /* Given f, return floor(f).
1000  * If f is an integer expression, then just return f.
1001  * If f is a constant, then return the constant floor(f).
1002  * Otherwise, if f = g/m, write g = q m + r,
1003  * create a new div d = [r/m] and return the expression q + d.
1004  * The coefficients in r are taken to lie between -m/2 and m/2.
1005  */
1006 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1007 {
1008         int i;
1009         int size;
1010         isl_ctx *ctx;
1011         isl_vec *div;
1012
1013         if (!aff)
1014                 return NULL;
1015
1016         if (isl_int_is_one(aff->v->el[0]))
1017                 return aff;
1018
1019         aff = isl_aff_cow(aff);
1020         if (!aff)
1021                 return NULL;
1022
1023         aff->v = isl_vec_cow(aff->v);
1024         if (!aff->v)
1025                 return isl_aff_free(aff);
1026
1027         if (isl_aff_is_cst(aff)) {
1028                 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1029                 isl_int_set_si(aff->v->el[0], 1);
1030                 return aff;
1031         }
1032
1033         div = isl_vec_copy(aff->v);
1034         div = isl_vec_cow(div);
1035         if (!div)
1036                 return isl_aff_free(aff);
1037
1038         ctx = isl_aff_get_ctx(aff);
1039         isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1040         for (i = 1; i < aff->v->size; ++i) {
1041                 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1042                 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1043                 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1044                         isl_int_sub(div->el[i], div->el[i], div->el[0]);
1045                         isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1046                 }
1047         }
1048
1049         aff->ls = isl_local_space_add_div(aff->ls, div);
1050         if (!aff->ls)
1051                 return isl_aff_free(aff);
1052
1053         size = aff->v->size;
1054         aff->v = isl_vec_extend(aff->v, size + 1);
1055         if (!aff->v)
1056                 return isl_aff_free(aff);
1057         isl_int_set_si(aff->v->el[0], 1);
1058         isl_int_set_si(aff->v->el[size], 1);
1059
1060         aff = isl_aff_normalize(aff);
1061
1062         return aff;
1063 }
1064
1065 /* Compute
1066  *
1067  *      aff mod m = aff - m * floor(aff/m)
1068  */
1069 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1070 {
1071         isl_aff *res;
1072
1073         res = isl_aff_copy(aff);
1074         aff = isl_aff_scale_down(aff, m);
1075         aff = isl_aff_floor(aff);
1076         aff = isl_aff_scale(aff, m);
1077         res = isl_aff_sub(res, aff);
1078
1079         return res;
1080 }
1081
1082 /* Compute
1083  *
1084  *      pwaff mod m = pwaff - m * floor(pwaff/m)
1085  */
1086 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1087 {
1088         isl_pw_aff *res;
1089
1090         res = isl_pw_aff_copy(pwaff);
1091         pwaff = isl_pw_aff_scale_down(pwaff, m);
1092         pwaff = isl_pw_aff_floor(pwaff);
1093         pwaff = isl_pw_aff_scale(pwaff, m);
1094         res = isl_pw_aff_sub(res, pwaff);
1095
1096         return res;
1097 }
1098
1099 /* Given f, return ceil(f).
1100  * If f is an integer expression, then just return f.
1101  * Otherwise, let f be the expression
1102  *
1103  *      e/m
1104  *
1105  * then return
1106  *
1107  *      floor((e + m - 1)/m)
1108  */
1109 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1110 {
1111         if (!aff)
1112                 return NULL;
1113
1114         if (isl_int_is_one(aff->v->el[0]))
1115                 return aff;
1116
1117         aff = isl_aff_cow(aff);
1118         if (!aff)
1119                 return NULL;
1120         aff->v = isl_vec_cow(aff->v);
1121         if (!aff->v)
1122                 return isl_aff_free(aff);
1123
1124         isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1125         isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1126         aff = isl_aff_floor(aff);
1127
1128         return aff;
1129 }
1130
1131 /* Apply the expansion computed by isl_merge_divs.
1132  * The expansion itself is given by "exp" while the resulting
1133  * list of divs is given by "div".
1134  */
1135 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1136         __isl_take isl_mat *div, int *exp)
1137 {
1138         int i, j;
1139         int old_n_div;
1140         int new_n_div;
1141         int offset;
1142
1143         aff = isl_aff_cow(aff);
1144         if (!aff || !div)
1145                 goto error;
1146
1147         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1148         new_n_div = isl_mat_rows(div);
1149         if (new_n_div < old_n_div)
1150                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1151                         "not an expansion", goto error);
1152
1153         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1154         if (!aff->v)
1155                 goto error;
1156
1157         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1158         j = old_n_div - 1;
1159         for (i = new_n_div - 1; i >= 0; --i) {
1160                 if (j >= 0 && exp[j] == i) {
1161                         if (i != j)
1162                                 isl_int_swap(aff->v->el[offset + i],
1163                                              aff->v->el[offset + j]);
1164                         j--;
1165                 } else
1166                         isl_int_set_si(aff->v->el[offset + i], 0);
1167         }
1168
1169         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1170         if (!aff->ls)
1171                 goto error;
1172         isl_mat_free(div);
1173         return aff;
1174 error:
1175         isl_aff_free(aff);
1176         isl_mat_free(div);
1177         return NULL;
1178 }
1179
1180 /* Add two affine expressions that live in the same local space.
1181  */
1182 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1183         __isl_take isl_aff *aff2)
1184 {
1185         isl_int gcd, f;
1186
1187         aff1 = isl_aff_cow(aff1);
1188         if (!aff1 || !aff2)
1189                 goto error;
1190
1191         aff1->v = isl_vec_cow(aff1->v);
1192         if (!aff1->v)
1193                 goto error;
1194
1195         isl_int_init(gcd);
1196         isl_int_init(f);
1197         isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1198         isl_int_divexact(f, aff2->v->el[0], gcd);
1199         isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1200         isl_int_divexact(f, aff1->v->el[0], gcd);
1201         isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1202         isl_int_divexact(f, aff2->v->el[0], gcd);
1203         isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1204         isl_int_clear(f);
1205         isl_int_clear(gcd);
1206
1207         isl_aff_free(aff2);
1208         return aff1;
1209 error:
1210         isl_aff_free(aff1);
1211         isl_aff_free(aff2);
1212         return NULL;
1213 }
1214
1215 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1216         __isl_take isl_aff *aff2)
1217 {
1218         isl_ctx *ctx;
1219         int *exp1 = NULL;
1220         int *exp2 = NULL;
1221         isl_mat *div;
1222
1223         if (!aff1 || !aff2)
1224                 goto error;
1225
1226         ctx = isl_aff_get_ctx(aff1);
1227         if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1228                 isl_die(ctx, isl_error_invalid,
1229                         "spaces don't match", goto error);
1230
1231         if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
1232                 return add_expanded(aff1, aff2);
1233
1234         exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
1235         exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
1236         if (!exp1 || !exp2)
1237                 goto error;
1238
1239         div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1240         aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1241         aff2 = isl_aff_expand_divs(aff2, div, exp2);
1242         free(exp1);
1243         free(exp2);
1244
1245         return add_expanded(aff1, aff2);
1246 error:
1247         free(exp1);
1248         free(exp2);
1249         isl_aff_free(aff1);
1250         isl_aff_free(aff2);
1251         return NULL;
1252 }
1253
1254 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1255         __isl_take isl_aff *aff2)
1256 {
1257         return isl_aff_add(aff1, isl_aff_neg(aff2));
1258 }
1259
1260 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1261 {
1262         isl_int gcd;
1263
1264         if (isl_int_is_one(f))
1265                 return aff;
1266
1267         aff = isl_aff_cow(aff);
1268         if (!aff)
1269                 return NULL;
1270         aff->v = isl_vec_cow(aff->v);
1271         if (!aff->v)
1272                 return isl_aff_free(aff);
1273
1274         if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1275                 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1276                 return aff;
1277         }
1278
1279         isl_int_init(gcd);
1280         isl_int_gcd(gcd, aff->v->el[0], f);
1281         isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1282         isl_int_divexact(gcd, f, gcd);
1283         isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1284         isl_int_clear(gcd);
1285
1286         return aff;
1287 }
1288
1289 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1290 {
1291         isl_int gcd;
1292
1293         if (isl_int_is_one(f))
1294                 return aff;
1295
1296         aff = isl_aff_cow(aff);
1297         if (!aff)
1298                 return NULL;
1299
1300         if (isl_int_is_zero(f))
1301                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1302                         "cannot scale down by zero", return isl_aff_free(aff));
1303
1304         aff->v = isl_vec_cow(aff->v);
1305         if (!aff->v)
1306                 return isl_aff_free(aff);
1307
1308         isl_int_init(gcd);
1309         isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1310         isl_int_gcd(gcd, gcd, f);
1311         isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1312         isl_int_divexact(gcd, f, gcd);
1313         isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1314         isl_int_clear(gcd);
1315
1316         return aff;
1317 }
1318
1319 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1320 {
1321         isl_int v;
1322
1323         if (f == 1)
1324                 return aff;
1325
1326         isl_int_init(v);
1327         isl_int_set_ui(v, f);
1328         aff = isl_aff_scale_down(aff, v);
1329         isl_int_clear(v);
1330
1331         return aff;
1332 }
1333
1334 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1335         enum isl_dim_type type, unsigned pos, const char *s)
1336 {
1337         aff = isl_aff_cow(aff);
1338         if (!aff)
1339                 return NULL;
1340         if (type == isl_dim_out)
1341                 isl_die(aff->v->ctx, isl_error_invalid,
1342                         "cannot set name of output/set dimension",
1343                         return isl_aff_free(aff));
1344         if (type == isl_dim_in)
1345                 type = isl_dim_set;
1346         aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1347         if (!aff->ls)
1348                 return isl_aff_free(aff);
1349
1350         return aff;
1351 }
1352
1353 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1354         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1355 {
1356         aff = isl_aff_cow(aff);
1357         if (!aff)
1358                 return isl_id_free(id);
1359         if (type == isl_dim_out)
1360                 isl_die(aff->v->ctx, isl_error_invalid,
1361                         "cannot set name of output/set dimension",
1362                         goto error);
1363         if (type == isl_dim_in)
1364                 type = isl_dim_set;
1365         aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1366         if (!aff->ls)
1367                 return isl_aff_free(aff);
1368
1369         return aff;
1370 error:
1371         isl_id_free(id);
1372         isl_aff_free(aff);
1373         return NULL;
1374 }
1375
1376 /* Exploit the equalities in "eq" to simplify the affine expression
1377  * and the expressions of the integer divisions in the local space.
1378  * The integer divisions in this local space are assumed to appear
1379  * as regular dimensions in "eq".
1380  */
1381 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1382         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1383 {
1384         int i, j;
1385         unsigned total;
1386         unsigned n_div;
1387
1388         if (!eq)
1389                 goto error;
1390         if (eq->n_eq == 0) {
1391                 isl_basic_set_free(eq);
1392                 return aff;
1393         }
1394
1395         aff = isl_aff_cow(aff);
1396         if (!aff)
1397                 goto error;
1398
1399         aff->ls = isl_local_space_substitute_equalities(aff->ls,
1400                                                         isl_basic_set_copy(eq));
1401         aff->v = isl_vec_cow(aff->v);
1402         if (!aff->ls || !aff->v)
1403                 goto error;
1404
1405         total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1406         n_div = eq->n_div;
1407         for (i = 0; i < eq->n_eq; ++i) {
1408                 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1409                 if (j < 0 || j == 0 || j >= total)
1410                         continue;
1411
1412                 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1413                                 &aff->v->el[0]);
1414         }
1415
1416         isl_basic_set_free(eq);
1417         aff = isl_aff_normalize(aff);
1418         return aff;
1419 error:
1420         isl_basic_set_free(eq);
1421         isl_aff_free(aff);
1422         return NULL;
1423 }
1424
1425 /* Exploit the equalities in "eq" to simplify the affine expression
1426  * and the expressions of the integer divisions in the local space.
1427  */
1428 static __isl_give isl_aff *isl_aff_substitute_equalities(
1429         __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1430 {
1431         int n_div;
1432
1433         if (!aff || !eq)
1434                 goto error;
1435         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1436         if (n_div > 0)
1437                 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1438         return isl_aff_substitute_equalities_lifted(aff, eq);
1439 error:
1440         isl_basic_set_free(eq);
1441         isl_aff_free(aff);
1442         return NULL;
1443 }
1444
1445 /* Look for equalities among the variables shared by context and aff
1446  * and the integer divisions of aff, if any.
1447  * The equalities are then used to eliminate coefficients and/or integer
1448  * divisions from aff.
1449  */
1450 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
1451         __isl_take isl_set *context)
1452 {
1453         isl_basic_set *hull;
1454         int n_div;
1455
1456         if (!aff)
1457                 goto error;
1458         n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1459         if (n_div > 0) {
1460                 isl_basic_set *bset;
1461                 isl_local_space *ls;
1462                 context = isl_set_add_dims(context, isl_dim_set, n_div);
1463                 ls = isl_aff_get_domain_local_space(aff);
1464                 bset = isl_basic_set_from_local_space(ls);
1465                 bset = isl_basic_set_lift(bset);
1466                 bset = isl_basic_set_flatten(bset);
1467                 context = isl_set_intersect(context,
1468                                             isl_set_from_basic_set(bset));
1469         }
1470
1471         hull = isl_set_affine_hull(context);
1472         return isl_aff_substitute_equalities_lifted(aff, hull);
1473 error:
1474         isl_aff_free(aff);
1475         isl_set_free(context);
1476         return NULL;
1477 }
1478
1479 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
1480         __isl_take isl_set *context)
1481 {
1482         isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
1483         dom_context = isl_set_intersect_params(dom_context, context);
1484         return isl_aff_gist(aff, dom_context);
1485 }
1486
1487 /* Return a basic set containing those elements in the space
1488  * of aff where it is non-negative.
1489  * If "rational" is set, then return a rational basic set.
1490  */
1491 static __isl_give isl_basic_set *aff_nonneg_basic_set(
1492         __isl_take isl_aff *aff, int rational)
1493 {
1494         isl_constraint *ineq;
1495         isl_basic_set *bset;
1496
1497         ineq = isl_inequality_from_aff(aff);
1498
1499         bset = isl_basic_set_from_constraint(ineq);
1500         if (rational)
1501                 bset = isl_basic_set_set_rational(bset);
1502         bset = isl_basic_set_simplify(bset);
1503         return bset;
1504 }
1505
1506 /* Return a basic set containing those elements in the space
1507  * of aff where it is non-negative.
1508  */
1509 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
1510 {
1511         return aff_nonneg_basic_set(aff, 0);
1512 }
1513
1514 /* Return a basic set containing those elements in the domain space
1515  * of aff where it is negative.
1516  */
1517 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
1518 {
1519         aff = isl_aff_neg(aff);
1520         aff = isl_aff_add_constant_num_si(aff, -1);
1521         return isl_aff_nonneg_basic_set(aff);
1522 }
1523
1524 /* Return a basic set containing those elements in the space
1525  * of aff where it is zero.
1526  * If "rational" is set, then return a rational basic set.
1527  */
1528 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
1529         int rational)
1530 {
1531         isl_constraint *ineq;
1532         isl_basic_set *bset;
1533
1534         ineq = isl_equality_from_aff(aff);
1535
1536         bset = isl_basic_set_from_constraint(ineq);
1537         if (rational)
1538                 bset = isl_basic_set_set_rational(bset);
1539         bset = isl_basic_set_simplify(bset);
1540         return bset;
1541 }
1542
1543 /* Return a basic set containing those elements in the space
1544  * of aff where it is zero.
1545  */
1546 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
1547 {
1548         return aff_zero_basic_set(aff, 0);
1549 }
1550
1551 /* Return a basic set containing those elements in the shared space
1552  * of aff1 and aff2 where aff1 is greater than or equal to aff2.
1553  */
1554 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
1555         __isl_take isl_aff *aff2)
1556 {
1557         aff1 = isl_aff_sub(aff1, aff2);
1558
1559         return isl_aff_nonneg_basic_set(aff1);
1560 }
1561
1562 /* Return a basic set containing those elements in the shared space
1563  * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
1564  */
1565 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
1566         __isl_take isl_aff *aff2)
1567 {
1568         return isl_aff_ge_basic_set(aff2, aff1);
1569 }
1570
1571 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
1572         __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
1573 {
1574         aff1 = isl_aff_add(aff1, aff2);
1575         aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
1576         return aff1;
1577 }
1578
1579 int isl_aff_is_empty(__isl_keep isl_aff *aff)
1580 {
1581         if (!aff)
1582                 return -1;
1583
1584         return 0;
1585 }
1586
1587 /* Check whether the given affine expression has non-zero coefficient
1588  * for any dimension in the given range or if any of these dimensions
1589  * appear with non-zero coefficients in any of the integer divisions
1590  * involved in the affine expression.
1591  */
1592 int isl_aff_involves_dims(__isl_keep isl_aff *aff,
1593         enum isl_dim_type type, unsigned first, unsigned n)
1594 {
1595         int i;
1596         isl_ctx *ctx;
1597         int *active = NULL;
1598         int involves = 0;
1599
1600         if (!aff)
1601                 return -1;
1602         if (n == 0)
1603                 return 0;
1604
1605         ctx = isl_aff_get_ctx(aff);
1606         if (first + n > isl_aff_dim(aff, type))
1607                 isl_die(ctx, isl_error_invalid,
1608                         "range out of bounds", return -1);
1609
1610         active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
1611         if (!active)
1612                 goto error;
1613
1614         first += isl_local_space_offset(aff->ls, type) - 1;
1615         for (i = 0; i < n; ++i)
1616                 if (active[first + i]) {
1617                         involves = 1;
1618                         break;
1619                 }
1620
1621         free(active);
1622
1623         return involves;
1624 error:
1625         free(active);
1626         return -1;
1627 }
1628
1629 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
1630         enum isl_dim_type type, unsigned first, unsigned n)
1631 {
1632         isl_ctx *ctx;
1633
1634         if (!aff)
1635                 return NULL;
1636         if (type == isl_dim_out)
1637                 isl_die(aff->v->ctx, isl_error_invalid,
1638                         "cannot drop output/set dimension",
1639                         return isl_aff_free(aff));
1640         if (type == isl_dim_in)
1641                 type = isl_dim_set;
1642         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1643                 return aff;
1644
1645         ctx = isl_aff_get_ctx(aff);
1646         if (first + n > isl_local_space_dim(aff->ls, type))
1647                 isl_die(ctx, isl_error_invalid, "range out of bounds",
1648                         return isl_aff_free(aff));
1649
1650         aff = isl_aff_cow(aff);
1651         if (!aff)
1652                 return NULL;
1653
1654         aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
1655         if (!aff->ls)
1656                 return isl_aff_free(aff);
1657
1658         first += 1 + isl_local_space_offset(aff->ls, type);
1659         aff->v = isl_vec_drop_els(aff->v, first, n);
1660         if (!aff->v)
1661                 return isl_aff_free(aff);
1662
1663         return aff;
1664 }
1665
1666 /* Project the domain of the affine expression onto its parameter space.
1667  * The affine expression may not involve any of the domain dimensions.
1668  */
1669 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
1670 {
1671         isl_space *space;
1672         unsigned n;
1673         int involves;
1674
1675         n = isl_aff_dim(aff, isl_dim_in);
1676         involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
1677         if (involves < 0)
1678                 return isl_aff_free(aff);
1679         if (involves)
1680                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1681                     "affine expression involves some of the domain dimensions",
1682                     return isl_aff_free(aff));
1683         aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
1684         space = isl_aff_get_domain_space(aff);
1685         space = isl_space_params(space);
1686         aff = isl_aff_reset_domain_space(aff, space);
1687         return aff;
1688 }
1689
1690 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
1691         enum isl_dim_type type, unsigned first, unsigned n)
1692 {
1693         isl_ctx *ctx;
1694
1695         if (!aff)
1696                 return NULL;
1697         if (type == isl_dim_out)
1698                 isl_die(aff->v->ctx, isl_error_invalid,
1699                         "cannot insert output/set dimensions",
1700                         return isl_aff_free(aff));
1701         if (type == isl_dim_in)
1702                 type = isl_dim_set;
1703         if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
1704                 return aff;
1705
1706         ctx = isl_aff_get_ctx(aff);
1707         if (first > isl_local_space_dim(aff->ls, type))
1708                 isl_die(ctx, isl_error_invalid, "position out of bounds",
1709                         return isl_aff_free(aff));
1710
1711         aff = isl_aff_cow(aff);
1712         if (!aff)
1713                 return NULL;
1714
1715         aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
1716         if (!aff->ls)
1717                 return isl_aff_free(aff);
1718
1719         first += 1 + isl_local_space_offset(aff->ls, type);
1720         aff->v = isl_vec_insert_zero_els(aff->v, first, n);
1721         if (!aff->v)
1722                 return isl_aff_free(aff);
1723
1724         return aff;
1725 }
1726
1727 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
1728         enum isl_dim_type type, unsigned n)
1729 {
1730         unsigned pos;
1731
1732         pos = isl_aff_dim(aff, type);
1733
1734         return isl_aff_insert_dims(aff, type, pos, n);
1735 }
1736
1737 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
1738         enum isl_dim_type type, unsigned n)
1739 {
1740         unsigned pos;
1741
1742         pos = isl_pw_aff_dim(pwaff, type);
1743
1744         return isl_pw_aff_insert_dims(pwaff, type, pos, n);
1745 }
1746
1747 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
1748 {
1749         isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
1750         return isl_pw_aff_alloc(dom, aff);
1751 }
1752
1753 #undef PW
1754 #define PW isl_pw_aff
1755 #undef EL
1756 #define EL isl_aff
1757 #undef EL_IS_ZERO
1758 #define EL_IS_ZERO is_empty
1759 #undef ZERO
1760 #define ZERO empty
1761 #undef IS_ZERO
1762 #define IS_ZERO is_empty
1763 #undef FIELD
1764 #define FIELD aff
1765 #undef DEFAULT_IS_ZERO
1766 #define DEFAULT_IS_ZERO 0
1767
1768 #define NO_EVAL
1769 #define NO_OPT
1770 #define NO_MOVE_DIMS
1771 #define NO_LIFT
1772 #define NO_MORPH
1773
1774 #include <isl_pw_templ.c>
1775
1776 static __isl_give isl_set *align_params_pw_pw_set_and(
1777         __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
1778         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
1779                                     __isl_take isl_pw_aff *pwaff2))
1780 {
1781         if (!pwaff1 || !pwaff2)
1782                 goto error;
1783         if (isl_space_match(pwaff1->dim, isl_dim_param,
1784                           pwaff2->dim, isl_dim_param))
1785                 return fn(pwaff1, pwaff2);
1786         if (!isl_space_has_named_params(pwaff1->dim) ||
1787             !isl_space_has_named_params(pwaff2->dim))
1788                 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
1789                         "unaligned unnamed parameters", goto error);
1790         pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
1791         pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
1792         return fn(pwaff1, pwaff2);
1793 error:
1794         isl_pw_aff_free(pwaff1);
1795         isl_pw_aff_free(pwaff2);
1796         return NULL;
1797 }
1798
1799 /* Compute a piecewise quasi-affine expression with a domain that
1800  * is the union of those of pwaff1 and pwaff2 and such that on each
1801  * cell, the quasi-affine expression is the better (according to cmp)
1802  * of those of pwaff1 and pwaff2.  If only one of pwaff1 or pwaff2
1803  * is defined on a given cell, then the associated expression
1804  * is the defined one.
1805  */
1806 static __isl_give isl_pw_aff *pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1807         __isl_take isl_pw_aff *pwaff2,
1808         __isl_give isl_basic_set *(*cmp)(__isl_take isl_aff *aff1,
1809                                         __isl_take isl_aff *aff2))
1810 {
1811         int i, j, n;
1812         isl_pw_aff *res;
1813         isl_ctx *ctx;
1814         isl_set *set;
1815
1816         if (!pwaff1 || !pwaff2)
1817                 goto error;
1818
1819         ctx = isl_space_get_ctx(pwaff1->dim);
1820         if (!isl_space_is_equal(pwaff1->dim, pwaff2->dim))
1821                 isl_die(ctx, isl_error_invalid,
1822                         "arguments should live in same space", goto error);
1823
1824         if (isl_pw_aff_is_empty(pwaff1)) {
1825                 isl_pw_aff_free(pwaff1);
1826                 return pwaff2;
1827         }
1828
1829         if (isl_pw_aff_is_empty(pwaff2)) {
1830                 isl_pw_aff_free(pwaff2);
1831                 return pwaff1;
1832         }
1833
1834         n = 2 * (pwaff1->n + 1) * (pwaff2->n + 1);
1835         res = isl_pw_aff_alloc_size(isl_space_copy(pwaff1->dim), n);
1836
1837         for (i = 0; i < pwaff1->n; ++i) {
1838                 set = isl_set_copy(pwaff1->p[i].set);
1839                 for (j = 0; j < pwaff2->n; ++j) {
1840                         struct isl_set *common;
1841                         isl_set *better;
1842
1843                         common = isl_set_intersect(
1844                                         isl_set_copy(pwaff1->p[i].set),
1845                                         isl_set_copy(pwaff2->p[j].set));
1846                         better = isl_set_from_basic_set(cmp(
1847                                         isl_aff_copy(pwaff2->p[j].aff),
1848                                         isl_aff_copy(pwaff1->p[i].aff)));
1849                         better = isl_set_intersect(common, better);
1850                         if (isl_set_plain_is_empty(better)) {
1851                                 isl_set_free(better);
1852                                 continue;
1853                         }
1854                         set = isl_set_subtract(set, isl_set_copy(better));
1855
1856                         res = isl_pw_aff_add_piece(res, better,
1857                                                 isl_aff_copy(pwaff2->p[j].aff));
1858                 }
1859                 res = isl_pw_aff_add_piece(res, set,
1860                                                 isl_aff_copy(pwaff1->p[i].aff));
1861         }
1862
1863         for (j = 0; j < pwaff2->n; ++j) {
1864                 set = isl_set_copy(pwaff2->p[j].set);
1865                 for (i = 0; i < pwaff1->n; ++i)
1866                         set = isl_set_subtract(set,
1867                                         isl_set_copy(pwaff1->p[i].set));
1868                 res = isl_pw_aff_add_piece(res, set,
1869                                                 isl_aff_copy(pwaff2->p[j].aff));
1870         }
1871
1872         isl_pw_aff_free(pwaff1);
1873         isl_pw_aff_free(pwaff2);
1874
1875         return res;
1876 error:
1877         isl_pw_aff_free(pwaff1);
1878         isl_pw_aff_free(pwaff2);
1879         return NULL;
1880 }
1881
1882 /* Compute a piecewise quasi-affine expression with a domain that
1883  * is the union of those of pwaff1 and pwaff2 and such that on each
1884  * cell, the quasi-affine expression is the maximum of those of pwaff1
1885  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1886  * cell, then the associated expression is the defined one.
1887  */
1888 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1889         __isl_take isl_pw_aff *pwaff2)
1890 {
1891         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_ge_basic_set);
1892 }
1893
1894 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
1895         __isl_take isl_pw_aff *pwaff2)
1896 {
1897         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1898                                                         &pw_aff_union_max);
1899 }
1900
1901 /* Compute a piecewise quasi-affine expression with a domain that
1902  * is the union of those of pwaff1 and pwaff2 and such that on each
1903  * cell, the quasi-affine expression is the minimum of those of pwaff1
1904  * and pwaff2.  If only one of pwaff1 or pwaff2 is defined on a given
1905  * cell, then the associated expression is the defined one.
1906  */
1907 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1908         __isl_take isl_pw_aff *pwaff2)
1909 {
1910         return pw_aff_union_opt(pwaff1, pwaff2, &isl_aff_le_basic_set);
1911 }
1912
1913 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
1914         __isl_take isl_pw_aff *pwaff2)
1915 {
1916         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
1917                                                         &pw_aff_union_min);
1918 }
1919
1920 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
1921         __isl_take isl_pw_aff *pwaff2, int max)
1922 {
1923         if (max)
1924                 return isl_pw_aff_union_max(pwaff1, pwaff2);
1925         else
1926                 return isl_pw_aff_union_min(pwaff1, pwaff2);
1927 }
1928
1929 /* Construct a map with as domain the domain of pwaff and
1930  * one-dimensional range corresponding to the affine expressions.
1931  */
1932 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1933 {
1934         int i;
1935         isl_space *dim;
1936         isl_map *map;
1937
1938         if (!pwaff)
1939                 return NULL;
1940
1941         dim = isl_pw_aff_get_space(pwaff);
1942         map = isl_map_empty(dim);
1943
1944         for (i = 0; i < pwaff->n; ++i) {
1945                 isl_basic_map *bmap;
1946                 isl_map *map_i;
1947
1948                 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
1949                 map_i = isl_map_from_basic_map(bmap);
1950                 map_i = isl_map_intersect_domain(map_i,
1951                                                 isl_set_copy(pwaff->p[i].set));
1952                 map = isl_map_union_disjoint(map, map_i);
1953         }
1954
1955         isl_pw_aff_free(pwaff);
1956
1957         return map;
1958 }
1959
1960 /* Construct a map with as domain the domain of pwaff and
1961  * one-dimensional range corresponding to the affine expressions.
1962  */
1963 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1964 {
1965         if (!pwaff)
1966                 return NULL;
1967         if (isl_space_is_set(pwaff->dim))
1968                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1969                         "space of input is not a map",
1970                         return isl_pw_aff_free(pwaff));
1971         return map_from_pw_aff(pwaff);
1972 }
1973
1974 /* Construct a one-dimensional set with as parameter domain
1975  * the domain of pwaff and the single set dimension
1976  * corresponding to the affine expressions.
1977  */
1978 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
1979 {
1980         if (!pwaff)
1981                 return NULL;
1982         if (!isl_space_is_set(pwaff->dim))
1983                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
1984                         "space of input is not a set",
1985                         return isl_pw_aff_free(pwaff));
1986         return map_from_pw_aff(pwaff);
1987 }
1988
1989 /* Return a set containing those elements in the domain
1990  * of pwaff where it is non-negative.
1991  */
1992 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
1993 {
1994         int i;
1995         isl_set *set;
1996
1997         if (!pwaff)
1998                 return NULL;
1999
2000         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2001
2002         for (i = 0; i < pwaff->n; ++i) {
2003                 isl_basic_set *bset;
2004                 isl_set *set_i;
2005                 int rational;
2006
2007                 rational = isl_set_has_rational(pwaff->p[i].set);
2008                 bset = aff_nonneg_basic_set(isl_aff_copy(pwaff->p[i].aff),
2009                                                 rational);
2010                 set_i = isl_set_from_basic_set(bset);
2011                 set_i = isl_set_intersect(set_i, isl_set_copy(pwaff->p[i].set));
2012                 set = isl_set_union_disjoint(set, set_i);
2013         }
2014
2015         isl_pw_aff_free(pwaff);
2016
2017         return set;
2018 }
2019
2020 /* Return a set containing those elements in the domain
2021  * of pwaff where it is zero (if complement is 0) or not zero
2022  * (if complement is 1).
2023  */
2024 static __isl_give isl_set *pw_aff_zero_set(__isl_take isl_pw_aff *pwaff,
2025         int complement)
2026 {
2027         int i;
2028         isl_set *set;
2029
2030         if (!pwaff)
2031                 return NULL;
2032
2033         set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2034
2035         for (i = 0; i < pwaff->n; ++i) {
2036                 isl_basic_set *bset;
2037                 isl_set *set_i, *zero;
2038                 int rational;
2039
2040                 rational = isl_set_has_rational(pwaff->p[i].set);
2041                 bset = aff_zero_basic_set(isl_aff_copy(pwaff->p[i].aff),
2042                                                 rational);
2043                 zero = isl_set_from_basic_set(bset);
2044                 set_i = isl_set_copy(pwaff->p[i].set);
2045                 if (complement)
2046                         set_i = isl_set_subtract(set_i, zero);
2047                 else
2048                         set_i = isl_set_intersect(set_i, zero);
2049                 set = isl_set_union_disjoint(set, set_i);
2050         }
2051
2052         isl_pw_aff_free(pwaff);
2053
2054         return set;
2055 }
2056
2057 /* Return a set containing those elements in the domain
2058  * of pwaff where it is zero.
2059  */
2060 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2061 {
2062         return pw_aff_zero_set(pwaff, 0);
2063 }
2064
2065 /* Return a set containing those elements in the domain
2066  * of pwaff where it is not zero.
2067  */
2068 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2069 {
2070         return pw_aff_zero_set(pwaff, 1);
2071 }
2072
2073 /* Return a set containing those elements in the shared domain
2074  * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2075  *
2076  * We compute the difference on the shared domain and then construct
2077  * the set of values where this difference is non-negative.
2078  * If strict is set, we first subtract 1 from the difference.
2079  * If equal is set, we only return the elements where pwaff1 and pwaff2
2080  * are equal.
2081  */
2082 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2083         __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2084 {
2085         isl_set *set1, *set2;
2086
2087         set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2088         set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2089         set1 = isl_set_intersect(set1, set2);
2090         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2091         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2092         pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2093
2094         if (strict) {
2095                 isl_space *dim = isl_set_get_space(set1);
2096                 isl_aff *aff;
2097                 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2098                 aff = isl_aff_add_constant_si(aff, -1);
2099                 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2100         } else
2101                 isl_set_free(set1);
2102
2103         if (equal)
2104                 return isl_pw_aff_zero_set(pwaff1);
2105         return isl_pw_aff_nonneg_set(pwaff1);
2106 }
2107
2108 /* Return a set containing those elements in the shared domain
2109  * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2110  */
2111 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2112         __isl_take isl_pw_aff *pwaff2)
2113 {
2114         return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2115 }
2116
2117 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2118         __isl_take isl_pw_aff *pwaff2)
2119 {
2120         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2121 }
2122
2123 /* Return a set containing those elements in the shared domain
2124  * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2125  */
2126 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2127         __isl_take isl_pw_aff *pwaff2)
2128 {
2129         return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2130 }
2131
2132 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2133         __isl_take isl_pw_aff *pwaff2)
2134 {
2135         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2136 }
2137
2138 /* Return a set containing those elements in the shared domain
2139  * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2140  */
2141 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2142         __isl_take isl_pw_aff *pwaff2)
2143 {
2144         return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2145 }
2146
2147 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2148         __isl_take isl_pw_aff *pwaff2)
2149 {
2150         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2151 }
2152
2153 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2154         __isl_take isl_pw_aff *pwaff2)
2155 {
2156         return isl_pw_aff_ge_set(pwaff2, pwaff1);
2157 }
2158
2159 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2160         __isl_take isl_pw_aff *pwaff2)
2161 {
2162         return isl_pw_aff_gt_set(pwaff2, pwaff1);
2163 }
2164
2165 /* Return a set containing those elements in the shared domain
2166  * of the elements of list1 and list2 where each element in list1
2167  * has the relation specified by "fn" with each element in list2.
2168  */
2169 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2170         __isl_take isl_pw_aff_list *list2,
2171         __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2172                                     __isl_take isl_pw_aff *pwaff2))
2173 {
2174         int i, j;
2175         isl_ctx *ctx;
2176         isl_set *set;
2177
2178         if (!list1 || !list2)
2179                 goto error;
2180
2181         ctx = isl_pw_aff_list_get_ctx(list1);
2182         if (list1->n < 1 || list2->n < 1)
2183                 isl_die(ctx, isl_error_invalid,
2184                         "list should contain at least one element", goto error);
2185
2186         set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
2187         for (i = 0; i < list1->n; ++i)
2188                 for (j = 0; j < list2->n; ++j) {
2189                         isl_set *set_ij;
2190
2191                         set_ij = fn(isl_pw_aff_copy(list1->p[i]),
2192                                     isl_pw_aff_copy(list2->p[j]));
2193                         set = isl_set_intersect(set, set_ij);
2194                 }
2195
2196         isl_pw_aff_list_free(list1);
2197         isl_pw_aff_list_free(list2);
2198         return set;
2199 error:
2200         isl_pw_aff_list_free(list1);
2201         isl_pw_aff_list_free(list2);
2202         return NULL;
2203 }
2204
2205 /* Return a set containing those elements in the shared domain
2206  * of the elements of list1 and list2 where each element in list1
2207  * is equal to each element in list2.
2208  */
2209 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
2210         __isl_take isl_pw_aff_list *list2)
2211 {
2212         return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
2213 }
2214
2215 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
2216         __isl_take isl_pw_aff_list *list2)
2217 {
2218         return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
2219 }
2220
2221 /* Return a set containing those elements in the shared domain
2222  * of the elements of list1 and list2 where each element in list1
2223  * is less than or equal to each element in list2.
2224  */
2225 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
2226         __isl_take isl_pw_aff_list *list2)
2227 {
2228         return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
2229 }
2230
2231 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
2232         __isl_take isl_pw_aff_list *list2)
2233 {
2234         return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
2235 }
2236
2237 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
2238         __isl_take isl_pw_aff_list *list2)
2239 {
2240         return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
2241 }
2242
2243 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
2244         __isl_take isl_pw_aff_list *list2)
2245 {
2246         return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
2247 }
2248
2249
2250 /* Return a set containing those elements in the shared domain
2251  * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
2252  */
2253 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2254         __isl_take isl_pw_aff *pwaff2)
2255 {
2256         isl_set *set_lt, *set_gt;
2257
2258         set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
2259                                    isl_pw_aff_copy(pwaff2));
2260         set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
2261         return isl_set_union_disjoint(set_lt, set_gt);
2262 }
2263
2264 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
2265         __isl_take isl_pw_aff *pwaff2)
2266 {
2267         return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
2268 }
2269
2270 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
2271         isl_int v)
2272 {
2273         int i;
2274
2275         if (isl_int_is_one(v))
2276                 return pwaff;
2277         if (!isl_int_is_pos(v))
2278                 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2279                         "factor needs to be positive",
2280                         return isl_pw_aff_free(pwaff));
2281         pwaff = isl_pw_aff_cow(pwaff);
2282         if (!pwaff)
2283                 return NULL;
2284         if (pwaff->n == 0)
2285                 return pwaff;
2286
2287         for (i = 0; i < pwaff->n; ++i) {
2288                 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
2289                 if (!pwaff->p[i].aff)
2290                         return isl_pw_aff_free(pwaff);
2291         }
2292
2293         return pwaff;
2294 }
2295
2296 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
2297 {
2298         int i;
2299
2300         pwaff = isl_pw_aff_cow(pwaff);
2301         if (!pwaff)
2302                 return NULL;
2303         if (pwaff->n == 0)
2304                 return pwaff;
2305
2306         for (i = 0; i < pwaff->n; ++i) {
2307                 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
2308                 if (!pwaff->p[i].aff)
2309                         return isl_pw_aff_free(pwaff);
2310         }
2311
2312         return pwaff;
2313 }
2314
2315 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
2316 {
2317         int i;
2318
2319         pwaff = isl_pw_aff_cow(pwaff);
2320         if (!pwaff)
2321                 return NULL;
2322         if (pwaff->n == 0)
2323                 return pwaff;
2324
2325         for (i = 0; i < pwaff->n; ++i) {
2326                 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
2327                 if (!pwaff->p[i].aff)
2328                         return isl_pw_aff_free(pwaff);
2329         }
2330
2331         return pwaff;
2332 }
2333
2334 /* Assuming that "cond1" and "cond2" are disjoint,
2335  * return an affine expression that is equal to pwaff1 on cond1
2336  * and to pwaff2 on cond2.
2337  */
2338 static __isl_give isl_pw_aff *isl_pw_aff_select(
2339         __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
2340         __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
2341 {
2342         pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
2343         pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
2344
2345         return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
2346 }
2347
2348 /* Return an affine expression that is equal to pwaff_true for elements
2349  * where "cond" is non-zero and to pwaff_false for elements where "cond"
2350  * is zero.
2351  * That is, return cond ? pwaff_true : pwaff_false;
2352  */
2353 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
2354         __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
2355 {
2356         isl_set *cond_true, *cond_false;
2357
2358         cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
2359         cond_false = isl_pw_aff_zero_set(cond);
2360         return isl_pw_aff_select(cond_true, pwaff_true,
2361                                  cond_false, pwaff_false);
2362 }
2363
2364 int isl_aff_is_cst(__isl_keep isl_aff *aff)
2365 {
2366         if (!aff)
2367                 return -1;
2368
2369         return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
2370 }
2371
2372 /* Check whether pwaff is a piecewise constant.
2373  */
2374 int isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
2375 {
2376         int i;
2377
2378         if (!pwaff)
2379                 return -1;
2380
2381         for (i = 0; i < pwaff->n; ++i) {
2382                 int is_cst = isl_aff_is_cst(pwaff->p[i].aff);
2383                 if (is_cst < 0 || !is_cst)
2384                         return is_cst;
2385         }
2386
2387         return 1;
2388 }
2389
2390 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
2391         __isl_take isl_aff *aff2)
2392 {
2393         if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
2394                 return isl_aff_mul(aff2, aff1);
2395
2396         if (!isl_aff_is_cst(aff2))
2397                 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
2398                         "at least one affine expression should be constant",
2399                         goto error);
2400
2401         aff1 = isl_aff_cow(aff1);
2402         if (!aff1 || !aff2)
2403                 goto error;
2404
2405         aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
2406         aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
2407
2408         isl_aff_free(aff2);
2409         return aff1;
2410 error:
2411         isl_aff_free(aff1);
2412         isl_aff_free(aff2);
2413         return NULL;
2414 }
2415
2416 /* Divide "aff1" by "aff2", assuming "aff2" is a piecewise constant.
2417  */
2418 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
2419         __isl_take isl_aff *aff2)
2420 {
2421         int is_cst;
2422         int neg;
2423
2424         is_cst = isl_aff_is_cst(aff2);
2425         if (is_cst < 0)
2426                 goto error;
2427         if (!is_cst)
2428                 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
2429                         "second argument should be a constant", goto error);
2430
2431         if (!aff2)
2432                 goto error;
2433
2434         neg = isl_int_is_neg(aff2->v->el[1]);
2435         if (neg) {
2436                 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2437                 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2438         }
2439
2440         aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
2441         aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
2442
2443         if (neg) {
2444                 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
2445                 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
2446         }
2447
2448         isl_aff_free(aff2);
2449         return aff1;
2450 error:
2451         isl_aff_free(aff1);
2452         isl_aff_free(aff2);
2453         return NULL;
2454 }
2455
2456 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2457         __isl_take isl_pw_aff *pwaff2)
2458 {
2459         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
2460 }
2461
2462 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
2463         __isl_take isl_pw_aff *pwaff2)
2464 {
2465         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
2466 }
2467
2468 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
2469         __isl_take isl_pw_aff *pwaff2)
2470 {
2471         return isl_pw_aff_union_add_(pwaff1, pwaff2);
2472 }
2473
2474 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2475         __isl_take isl_pw_aff *pwaff2)
2476 {
2477         return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
2478 }
2479
2480 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
2481         __isl_take isl_pw_aff *pwaff2)
2482 {
2483         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
2484 }
2485
2486 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
2487         __isl_take isl_pw_aff *pa2)
2488 {
2489         return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
2490 }
2491
2492 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
2493  */
2494 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
2495         __isl_take isl_pw_aff *pa2)
2496 {
2497         int is_cst;
2498
2499         is_cst = isl_pw_aff_is_cst(pa2);
2500         if (is_cst < 0)
2501                 goto error;
2502         if (!is_cst)
2503                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2504                         "second argument should be a piecewise constant",
2505                         goto error);
2506         return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
2507 error:
2508         isl_pw_aff_free(pa1);
2509         isl_pw_aff_free(pa2);
2510         return NULL;
2511 }
2512
2513 /* Compute the quotient of the integer division of "pa1" by "pa2"
2514  * with rounding towards zero.
2515  * "pa2" is assumed to be a piecewise constant.
2516  *
2517  * In particular, return
2518  *
2519  *      pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
2520  *
2521  */
2522 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
2523         __isl_take isl_pw_aff *pa2)
2524 {
2525         int is_cst;
2526         isl_set *cond;
2527         isl_pw_aff *f, *c;
2528
2529         is_cst = isl_pw_aff_is_cst(pa2);
2530         if (is_cst < 0)
2531                 goto error;
2532         if (!is_cst)
2533                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2534                         "second argument should be a piecewise constant",
2535                         goto error);
2536
2537         pa1 = isl_pw_aff_div(pa1, pa2);
2538
2539         cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
2540         f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
2541         c = isl_pw_aff_ceil(pa1);
2542         return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
2543 error:
2544         isl_pw_aff_free(pa1);
2545         isl_pw_aff_free(pa2);
2546         return NULL;
2547 }
2548
2549 /* Compute the remainder of the integer division of "pa1" by "pa2"
2550  * with rounding towards zero.
2551  * "pa2" is assumed to be a piecewise constant.
2552  *
2553  * In particular, return
2554  *
2555  *      pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
2556  *
2557  */
2558 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
2559         __isl_take isl_pw_aff *pa2)
2560 {
2561         int is_cst;
2562         isl_pw_aff *res;
2563
2564         is_cst = isl_pw_aff_is_cst(pa2);
2565         if (is_cst < 0)
2566                 goto error;
2567         if (!is_cst)
2568                 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
2569                         "second argument should be a piecewise constant",
2570                         goto error);
2571         res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
2572         res = isl_pw_aff_mul(pa2, res);
2573         res = isl_pw_aff_sub(pa1, res);
2574         return res;
2575 error:
2576         isl_pw_aff_free(pa1);
2577         isl_pw_aff_free(pa2);
2578         return NULL;
2579 }
2580
2581 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2582         __isl_take isl_pw_aff *pwaff2)
2583 {
2584         isl_set *le;
2585         isl_set *dom;
2586
2587         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2588                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2589         le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
2590                                 isl_pw_aff_copy(pwaff2));
2591         dom = isl_set_subtract(dom, isl_set_copy(le));
2592         return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
2593 }
2594
2595 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
2596         __isl_take isl_pw_aff *pwaff2)
2597 {
2598         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
2599 }
2600
2601 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2602         __isl_take isl_pw_aff *pwaff2)
2603 {
2604         isl_set *ge;
2605         isl_set *dom;
2606
2607         dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
2608                                 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
2609         ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
2610                                 isl_pw_aff_copy(pwaff2));
2611         dom = isl_set_subtract(dom, isl_set_copy(ge));
2612         return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
2613 }
2614
2615 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
2616         __isl_take isl_pw_aff *pwaff2)
2617 {
2618         return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
2619 }
2620
2621 static __isl_give isl_pw_aff *pw_aff_list_reduce(
2622         __isl_take isl_pw_aff_list *list,
2623         __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
2624                                         __isl_take isl_pw_aff *pwaff2))
2625 {
2626         int i;
2627         isl_ctx *ctx;
2628         isl_pw_aff *res;
2629
2630         if (!list)
2631                 return NULL;
2632
2633         ctx = isl_pw_aff_list_get_ctx(list);
2634         if (list->n < 1)
2635                 isl_die(ctx, isl_error_invalid,
2636                         "list should contain at least one element",
2637                         return isl_pw_aff_list_free(list));
2638
2639         res = isl_pw_aff_copy(list->p[0]);
2640         for (i = 1; i < list->n; ++i)
2641                 res = fn(res, isl_pw_aff_copy(list->p[i]));
2642
2643         isl_pw_aff_list_free(list);
2644         return res;
2645 }
2646
2647 /* Return an isl_pw_aff that maps each element in the intersection of the
2648  * domains of the elements of list to the minimal corresponding affine
2649  * expression.
2650  */
2651 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
2652 {
2653         return pw_aff_list_reduce(list, &isl_pw_aff_min);
2654 }
2655
2656 /* Return an isl_pw_aff that maps each element in the intersection of the
2657  * domains of the elements of list to the maximal corresponding affine
2658  * expression.
2659  */
2660 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
2661 {
2662         return pw_aff_list_reduce(list, &isl_pw_aff_max);
2663 }
2664
2665 /* Mark the domains of "pwaff" as rational.
2666  */
2667 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
2668 {
2669         int i;
2670
2671         pwaff = isl_pw_aff_cow(pwaff);
2672         if (!pwaff)
2673                 return NULL;
2674         if (pwaff->n == 0)
2675                 return pwaff;
2676
2677         for (i = 0; i < pwaff->n; ++i) {
2678                 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
2679                 if (!pwaff->p[i].set)
2680                         return isl_pw_aff_free(pwaff);
2681         }
2682
2683         return pwaff;
2684 }
2685
2686 /* Mark the domains of the elements of "list" as rational.
2687  */
2688 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
2689         __isl_take isl_pw_aff_list *list)
2690 {
2691         int i, n;
2692
2693         if (!list)
2694                 return NULL;
2695         if (list->n == 0)
2696                 return list;
2697
2698         n = list->n;
2699         for (i = 0; i < n; ++i) {
2700                 isl_pw_aff *pa;
2701
2702                 pa = isl_pw_aff_list_get_pw_aff(list, i);
2703                 pa = isl_pw_aff_set_rational(pa);
2704                 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
2705         }
2706
2707         return list;
2708 }
2709
2710 /* Check that the domain space of "aff" matches "space".
2711  *
2712  * Return 0 on success and -1 on error.
2713  */
2714 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
2715         __isl_keep isl_space *space)
2716 {
2717         isl_space *aff_space;
2718         int match;
2719
2720         if (!aff || !space)
2721                 return -1;
2722
2723         aff_space = isl_aff_get_domain_space(aff);
2724
2725         match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
2726         if (match < 0)
2727                 goto error;
2728         if (!match)
2729                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2730                         "parameters don't match", goto error);
2731         match = isl_space_tuple_match(space, isl_dim_in,
2732                                         aff_space, isl_dim_set);
2733         if (match < 0)
2734                 goto error;
2735         if (!match)
2736                 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2737                         "domains don't match", goto error);
2738         isl_space_free(aff_space);
2739         return 0;
2740 error:
2741         isl_space_free(aff_space);
2742         return -1;
2743 }
2744
2745 #undef BASE
2746 #define BASE aff
2747
2748 #include <isl_multi_templ.c>
2749
2750 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
2751  * domain.
2752  */
2753 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
2754         __isl_take isl_multi_aff *ma)
2755 {
2756         isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
2757         return isl_pw_multi_aff_alloc(dom, ma);
2758 }
2759
2760 /* Create a piecewise multi-affine expression in the given space that maps each
2761  * input dimension to the corresponding output dimension.
2762  */
2763 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
2764         __isl_take isl_space *space)
2765 {
2766         return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
2767 }
2768
2769 __isl_give isl_multi_aff *isl_multi_aff_add(__isl_take isl_multi_aff *maff1,
2770         __isl_take isl_multi_aff *maff2)
2771 {
2772         return isl_multi_aff_bin_op(maff1, maff2, &isl_aff_add);
2773 }
2774
2775 /* Subtract "ma2" from "ma1" and return the result.
2776  */
2777 __isl_give isl_multi_aff *isl_multi_aff_sub(__isl_take isl_multi_aff *ma1,
2778         __isl_take isl_multi_aff *ma2)
2779 {
2780         return isl_multi_aff_bin_op(ma1, ma2, &isl_aff_sub);
2781 }
2782
2783 /* Given two multi-affine expressions A -> B and C -> D,
2784  * construct a multi-affine expression [A -> C] -> [B -> D].
2785  */
2786 __isl_give isl_multi_aff *isl_multi_aff_product(
2787         __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
2788 {
2789         int i;
2790         isl_aff *aff;
2791         isl_space *space;
2792         isl_multi_aff *res;
2793         int in1, in2, out1, out2;
2794
2795         in1 = isl_multi_aff_dim(ma1, isl_dim_in);
2796         in2 = isl_multi_aff_dim(ma2, isl_dim_in);
2797         out1 = isl_multi_aff_dim(ma1, isl_dim_out);
2798         out2 = isl_multi_aff_dim(ma2, isl_dim_out);
2799         space = isl_space_product(isl_multi_aff_get_space(ma1),
2800                                   isl_multi_aff_get_space(ma2));
2801         res = isl_multi_aff_alloc(isl_space_copy(space));
2802         space = isl_space_domain(space);
2803
2804         for (i = 0; i < out1; ++i) {
2805                 aff = isl_multi_aff_get_aff(ma1, i);
2806                 aff = isl_aff_insert_dims(aff, isl_dim_in, in1, in2);
2807                 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2808                 res = isl_multi_aff_set_aff(res, i, aff);
2809         }
2810
2811         for (i = 0; i < out2; ++i) {
2812                 aff = isl_multi_aff_get_aff(ma2, i);
2813                 aff = isl_aff_insert_dims(aff, isl_dim_in, 0, in1);
2814                 aff = isl_aff_reset_domain_space(aff, isl_space_copy(space));
2815                 res = isl_multi_aff_set_aff(res, out1 + i, aff);
2816         }
2817
2818         isl_space_free(space);
2819         isl_multi_aff_free(ma1);
2820         isl_multi_aff_free(ma2);
2821         return res;
2822 }
2823
2824 /* Exploit the equalities in "eq" to simplify the affine expressions.
2825  */
2826 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
2827         __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
2828 {
2829         int i;
2830
2831         maff = isl_multi_aff_cow(maff);
2832         if (!maff || !eq)
2833                 goto error;
2834
2835         for (i = 0; i < maff->n; ++i) {
2836                 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
2837                                                     isl_basic_set_copy(eq));
2838                 if (!maff->p[i])
2839                         goto error;
2840         }
2841
2842         isl_basic_set_free(eq);
2843         return maff;
2844 error:
2845         isl_basic_set_free(eq);
2846         isl_multi_aff_free(maff);
2847         return NULL;
2848 }
2849
2850 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
2851         isl_int f)
2852 {
2853         int i;
2854
2855         maff = isl_multi_aff_cow(maff);
2856         if (!maff)
2857                 return NULL;
2858
2859         for (i = 0; i < maff->n; ++i) {
2860                 maff->p[i] = isl_aff_scale(maff->p[i], f);
2861                 if (!maff->p[i])
2862                         return isl_multi_aff_free(maff);
2863         }
2864
2865         return maff;
2866 }
2867
2868 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
2869         __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
2870 {
2871         maff1 = isl_multi_aff_add(maff1, maff2);
2872         maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
2873         return maff1;
2874 }
2875
2876 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
2877 {
2878         if (!maff)
2879                 return -1;
2880
2881         return 0;
2882 }
2883
2884 int isl_multi_aff_plain_is_equal(__isl_keep isl_multi_aff *maff1,
2885         __isl_keep isl_multi_aff *maff2)
2886 {
2887         int i;
2888         int equal;
2889
2890         if (!maff1 || !maff2)
2891                 return -1;
2892         if (maff1->n != maff2->n)
2893                 return 0;
2894         equal = isl_space_is_equal(maff1->space, maff2->space);
2895         if (equal < 0 || !equal)
2896                 return equal;
2897
2898         for (i = 0; i < maff1->n; ++i) {
2899                 equal = isl_aff_plain_is_equal(maff1->p[i], maff2->p[i]);
2900                 if (equal < 0 || !equal)
2901                         return equal;
2902         }
2903
2904         return 1;
2905 }
2906
2907 /* Return the set of domain elements where "ma1" is lexicographically
2908  * smaller than or equal to "ma2".
2909  */
2910 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
2911         __isl_take isl_multi_aff *ma2)
2912 {
2913         return isl_multi_aff_lex_ge_set(ma2, ma1);
2914 }
2915
2916 /* Return the set of domain elements where "ma1" is lexicographically
2917  * greater than or equal to "ma2".
2918  */
2919 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
2920         __isl_take isl_multi_aff *ma2)
2921 {
2922         isl_space *space;
2923         isl_map *map1, *map2;
2924         isl_map *map, *ge;
2925
2926         map1 = isl_map_from_multi_aff(ma1);
2927         map2 = isl_map_from_multi_aff(ma2);
2928         map = isl_map_range_product(map1, map2);
2929         space = isl_space_range(isl_map_get_space(map));
2930         space = isl_space_domain(isl_space_unwrap(space));
2931         ge = isl_map_lex_ge(space);
2932         map = isl_map_intersect_range(map, isl_map_wrap(ge));
2933
2934         return isl_map_domain(map);
2935 }
2936
2937 #undef PW
2938 #define PW isl_pw_multi_aff
2939 #undef EL
2940 #define EL isl_multi_aff
2941 #undef EL_IS_ZERO
2942 #define EL_IS_ZERO is_empty
2943 #undef ZERO
2944 #define ZERO empty
2945 #undef IS_ZERO
2946 #define IS_ZERO is_empty
2947 #undef FIELD
2948 #define FIELD maff
2949 #undef DEFAULT_IS_ZERO
2950 #define DEFAULT_IS_ZERO 0
2951
2952 #define NO_NEG
2953 #define NO_EVAL
2954 #define NO_OPT
2955 #define NO_INVOLVES_DIMS
2956 #define NO_MOVE_DIMS
2957 #define NO_INSERT_DIMS
2958 #define NO_LIFT
2959 #define NO_MORPH
2960
2961 #include <isl_pw_templ.c>
2962
2963 #undef UNION
2964 #define UNION isl_union_pw_multi_aff
2965 #undef PART
2966 #define PART isl_pw_multi_aff
2967 #undef PARTS
2968 #define PARTS pw_multi_aff
2969 #define ALIGN_DOMAIN
2970
2971 #define NO_EVAL
2972
2973 #include <isl_union_templ.c>
2974
2975 /* Given a function "cmp" that returns the set of elements where
2976  * "ma1" is "better" than "ma2", return the intersection of this
2977  * set with "dom1" and "dom2".
2978  */
2979 static __isl_give isl_set *shared_and_better(__isl_keep isl_set *dom1,
2980         __isl_keep isl_set *dom2, __isl_keep isl_multi_aff *ma1,
2981         __isl_keep isl_multi_aff *ma2,
2982         __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
2983                                     __isl_take isl_multi_aff *ma2))
2984 {
2985         isl_set *common;
2986         isl_set *better;
2987         int is_empty;
2988
2989         common = isl_set_intersect(isl_set_copy(dom1), isl_set_copy(dom2));
2990         is_empty = isl_set_plain_is_empty(common);
2991         if (is_empty >= 0 && is_empty)
2992                 return common;
2993         if (is_empty < 0)
2994                 return isl_set_free(common);
2995         better = cmp(isl_multi_aff_copy(ma1), isl_multi_aff_copy(ma2));
2996         better = isl_set_intersect(common, better);
2997
2998         return better;
2999 }
3000
3001 /* Given a function "cmp" that returns the set of elements where
3002  * "ma1" is "better" than "ma2", return a piecewise multi affine
3003  * expression defined on the union of the definition domains
3004  * of "pma1" and "pma2" that maps to the "best" of "pma1" and
3005  * "pma2" on each cell.  If only one of the two input functions
3006  * is defined on a given cell, then it is considered the best.
3007  */
3008 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_opt(
3009         __isl_take isl_pw_multi_aff *pma1,
3010         __isl_take isl_pw_multi_aff *pma2,
3011         __isl_give isl_set *(*cmp)(__isl_take isl_multi_aff *ma1,
3012                                     __isl_take isl_multi_aff *ma2))
3013 {
3014         int i, j, n;
3015         isl_pw_multi_aff *res = NULL;
3016         isl_ctx *ctx;
3017         isl_set *set = NULL;
3018
3019         if (!pma1 || !pma2)
3020                 goto error;
3021
3022         ctx = isl_space_get_ctx(pma1->dim);
3023         if (!isl_space_is_equal(pma1->dim, pma2->dim))
3024                 isl_die(ctx, isl_error_invalid,
3025                         "arguments should live in the same space", goto error);
3026
3027         if (isl_pw_multi_aff_is_empty(pma1)) {
3028                 isl_pw_multi_aff_free(pma1);
3029                 return pma2;
3030         }
3031
3032         if (isl_pw_multi_aff_is_empty(pma2)) {
3033                 isl_pw_multi_aff_free(pma2);
3034                 return pma1;
3035         }
3036
3037         n = 2 * (pma1->n + 1) * (pma2->n + 1);
3038         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma1->dim), n);
3039
3040         for (i = 0; i < pma1->n; ++i) {
3041                 set = isl_set_copy(pma1->p[i].set);
3042                 for (j = 0; j < pma2->n; ++j) {
3043                         isl_set *better;
3044                         int is_empty;
3045
3046                         better = shared_and_better(pma2->p[j].set,
3047                                         pma1->p[i].set, pma2->p[j].maff,
3048                                         pma1->p[i].maff, cmp);
3049                         is_empty = isl_set_plain_is_empty(better);
3050                         if (is_empty < 0 || is_empty) {
3051                                 isl_set_free(better);
3052                                 if (is_empty < 0)
3053                                         goto error;
3054                                 continue;
3055                         }
3056                         set = isl_set_subtract(set, isl_set_copy(better));
3057
3058                         res = isl_pw_multi_aff_add_piece(res, better,
3059                                         isl_multi_aff_copy(pma2->p[j].maff));
3060                 }
3061                 res = isl_pw_multi_aff_add_piece(res, set,
3062                                         isl_multi_aff_copy(pma1->p[i].maff));
3063         }
3064
3065         for (j = 0; j < pma2->n; ++j) {
3066                 set = isl_set_copy(pma2->p[j].set);
3067                 for (i = 0; i < pma1->n; ++i)
3068                         set = isl_set_subtract(set,
3069                                         isl_set_copy(pma1->p[i].set));
3070                 res = isl_pw_multi_aff_add_piece(res, set,
3071                                         isl_multi_aff_copy(pma2->p[j].maff));
3072         }
3073
3074         isl_pw_multi_aff_free(pma1);
3075         isl_pw_multi_aff_free(pma2);
3076
3077         return res;
3078 error:
3079         isl_pw_multi_aff_free(pma1);
3080         isl_pw_multi_aff_free(pma2);
3081         isl_set_free(set);
3082         return isl_pw_multi_aff_free(res);
3083 }
3084
3085 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
3086         __isl_take isl_pw_multi_aff *pma1,
3087         __isl_take isl_pw_multi_aff *pma2)
3088 {
3089         return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_ge_set);
3090 }
3091
3092 /* Given two piecewise multi affine expressions, return a piecewise
3093  * multi-affine expression defined on the union of the definition domains
3094  * of the inputs that is equal to the lexicographic maximum of the two
3095  * inputs on each cell.  If only one of the two inputs is defined on
3096  * a given cell, then it is considered to be the maximum.
3097  */
3098 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
3099         __isl_take isl_pw_multi_aff *pma1,
3100         __isl_take isl_pw_multi_aff *pma2)
3101 {
3102         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3103                                                     &pw_multi_aff_union_lexmax);
3104 }
3105
3106 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
3107         __isl_take isl_pw_multi_aff *pma1,
3108         __isl_take isl_pw_multi_aff *pma2)
3109 {
3110         return pw_multi_aff_union_opt(pma1, pma2, &isl_multi_aff_lex_le_set);
3111 }
3112
3113 /* Given two piecewise multi affine expressions, return a piecewise
3114  * multi-affine expression defined on the union of the definition domains
3115  * of the inputs that is equal to the lexicographic minimum of the two
3116  * inputs on each cell.  If only one of the two inputs is defined on
3117  * a given cell, then it is considered to be the minimum.
3118  */
3119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
3120         __isl_take isl_pw_multi_aff *pma1,
3121         __isl_take isl_pw_multi_aff *pma2)
3122 {
3123         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3124                                                     &pw_multi_aff_union_lexmin);
3125 }
3126
3127 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
3128         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3129 {
3130         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3131                                                 &isl_multi_aff_add);
3132 }
3133
3134 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
3135         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3136 {
3137         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3138                                                 &pw_multi_aff_add);
3139 }
3140
3141 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
3142         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3143 {
3144         return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
3145                                                 &isl_multi_aff_sub);
3146 }
3147
3148 /* Subtract "pma2" from "pma1" and return the result.
3149  */
3150 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
3151         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3152 {
3153         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3154                                                 &pw_multi_aff_sub);
3155 }
3156
3157 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
3158         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3159 {
3160         return isl_pw_multi_aff_union_add_(pma1, pma2);
3161 }
3162
3163 /* Given two piecewise multi-affine expressions A -> B and C -> D,
3164  * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
3165  */
3166 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
3167         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3168 {
3169         int i, j, n;
3170         isl_space *space;
3171         isl_pw_multi_aff *res;
3172
3173         if (!pma1 || !pma2)
3174                 goto error;
3175
3176         n = pma1->n * pma2->n;
3177         space = isl_space_product(isl_space_copy(pma1->dim),
3178                                   isl_space_copy(pma2->dim));
3179         res = isl_pw_multi_aff_alloc_size(space, n);
3180
3181         for (i = 0; i < pma1->n; ++i) {
3182                 for (j = 0; j < pma2->n; ++j) {
3183                         isl_set *domain;
3184                         isl_multi_aff *ma;
3185
3186                         domain = isl_set_product(isl_set_copy(pma1->p[i].set),
3187                                                  isl_set_copy(pma2->p[j].set));
3188                         ma = isl_multi_aff_product(
3189                                         isl_multi_aff_copy(pma1->p[i].maff),
3190                                         isl_multi_aff_copy(pma2->p[i].maff));
3191                         res = isl_pw_multi_aff_add_piece(res, domain, ma);
3192                 }
3193         }
3194
3195         isl_pw_multi_aff_free(pma1);
3196         isl_pw_multi_aff_free(pma2);
3197         return res;
3198 error:
3199         isl_pw_multi_aff_free(pma1);
3200         isl_pw_multi_aff_free(pma2);
3201         return NULL;
3202 }
3203
3204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
3205         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
3206 {
3207         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
3208                                                 &pw_multi_aff_product);
3209 }
3210
3211 /* Construct a map mapping the domain of the piecewise multi-affine expression
3212  * to its range, with each dimension in the range equated to the
3213  * corresponding affine expression on its cell.
3214  */
3215 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3216 {
3217         int i;
3218         isl_map *map;
3219
3220         if (!pma)
3221                 return NULL;
3222
3223         map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
3224
3225         for (i = 0; i < pma->n; ++i) {
3226                 isl_multi_aff *maff;
3227                 isl_basic_map *bmap;
3228                 isl_map *map_i;
3229
3230                 maff = isl_multi_aff_copy(pma->p[i].maff);
3231                 bmap = isl_basic_map_from_multi_aff(maff);
3232                 map_i = isl_map_from_basic_map(bmap);
3233                 map_i = isl_map_intersect_domain(map_i,
3234                                                 isl_set_copy(pma->p[i].set));
3235                 map = isl_map_union_disjoint(map, map_i);
3236         }
3237
3238         isl_pw_multi_aff_free(pma);
3239         return map;
3240 }
3241
3242 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
3243 {
3244         if (!pma)
3245                 return NULL;
3246
3247         if (!isl_space_is_set(pma->dim))
3248                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
3249                         "isl_pw_multi_aff cannot be converted into an isl_set",
3250                         return isl_pw_multi_aff_free(pma));
3251
3252         return isl_map_from_pw_multi_aff(pma);
3253 }
3254
3255 /* Given a basic map with a single output dimension that is defined
3256  * in terms of the parameters and input dimensions using an equality,
3257  * extract an isl_aff that expresses the output dimension in terms
3258  * of the parameters and input dimensions.
3259  *
3260  * Since some applications expect the result of isl_pw_multi_aff_from_map
3261  * to only contain integer affine expressions, we compute the floor
3262  * of the expression before returning.
3263  *
3264  * This function shares some similarities with
3265  * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
3266  */
3267 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
3268         __isl_take isl_basic_map *bmap)
3269 {
3270         int i;
3271         unsigned offset;
3272         unsigned total;
3273         isl_local_space *ls;
3274         isl_aff *aff;
3275
3276         if (!bmap)
3277                 return NULL;
3278         if (isl_basic_map_dim(bmap, isl_dim_out) != 1)
3279                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3280                         "basic map should have a single output dimension",
3281                         goto error);
3282         offset = isl_basic_map_offset(bmap, isl_dim_out);
3283         total = isl_basic_map_total_dim(bmap);
3284         for (i = 0; i < bmap->n_eq; ++i) {
3285                 if (isl_int_is_zero(bmap->eq[i][offset]))
3286                         continue;
3287                 if (isl_seq_first_non_zero(bmap->eq[i] + offset + 1,
3288                                            1 + total - (offset + 1)) != -1)
3289                         continue;
3290                 break;
3291         }
3292         if (i >= bmap->n_eq)
3293                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3294                         "unable to find suitable equality", goto error);
3295         ls = isl_basic_map_get_local_space(bmap);
3296         aff = isl_aff_alloc(isl_local_space_domain(ls));
3297         if (!aff)
3298                 goto error;
3299         if (isl_int_is_neg(bmap->eq[i][offset]))
3300                 isl_seq_cpy(aff->v->el + 1, bmap->eq[i], offset);
3301         else
3302                 isl_seq_neg(aff->v->el + 1, bmap->eq[i], offset);
3303         isl_seq_clr(aff->v->el + 1 + offset, aff->v->size - (1 + offset));
3304         isl_int_abs(aff->v->el[0], bmap->eq[i][offset]);
3305         isl_basic_map_free(bmap);
3306
3307         aff = isl_aff_remove_unused_divs(aff);
3308         aff = isl_aff_floor(aff);
3309         return aff;
3310 error:
3311         isl_basic_map_free(bmap);
3312         return NULL;
3313 }
3314
3315 /* Given a basic map where each output dimension is defined
3316  * in terms of the parameters and input dimensions using an equality,
3317  * extract an isl_multi_aff that expresses the output dimensions in terms
3318  * of the parameters and input dimensions.
3319  */
3320 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
3321         __isl_take isl_basic_map *bmap)
3322 {
3323         int i;
3324         unsigned n_out;
3325         isl_multi_aff *ma;
3326
3327         if (!bmap)
3328                 return NULL;
3329
3330         ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
3331         n_out = isl_basic_map_dim(bmap, isl_dim_out);
3332
3333         for (i = 0; i < n_out; ++i) {
3334                 isl_basic_map *bmap_i;
3335                 isl_aff *aff;
3336
3337                 bmap_i = isl_basic_map_copy(bmap);
3338                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out,
3339                                                         i + 1, n_out - (1 + i));
3340                 bmap_i = isl_basic_map_project_out(bmap_i, isl_dim_out, 0, i);
3341                 aff = extract_isl_aff_from_basic_map(bmap_i);
3342                 ma = isl_multi_aff_set_aff(ma, i, aff);
3343         }
3344
3345         isl_basic_map_free(bmap);
3346
3347         return ma;
3348 }
3349
3350 /* Create an isl_pw_multi_aff that is equivalent to
3351  * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
3352  * The given basic map is such that each output dimension is defined
3353  * in terms of the parameters and input dimensions using an equality.
3354  */
3355 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
3356         __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
3357 {
3358         isl_multi_aff *ma;
3359
3360         ma = extract_isl_multi_aff_from_basic_map(bmap);
3361         return isl_pw_multi_aff_alloc(domain, ma);
3362 }
3363
3364 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3365  * This obviously only works if the input "map" is single-valued.
3366  * If so, we compute the lexicographic minimum of the image in the form
3367  * of an isl_pw_multi_aff.  Since the image is unique, it is equal
3368  * to its lexicographic minimum.
3369  * If the input is not single-valued, we produce an error.
3370  */
3371 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
3372         __isl_take isl_map *map)
3373 {
3374         int i;
3375         int sv;
3376         isl_pw_multi_aff *pma;
3377
3378         sv = isl_map_is_single_valued(map);
3379         if (sv < 0)
3380                 goto error;
3381         if (!sv)
3382                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
3383                         "map is not single-valued", goto error);
3384         map = isl_map_make_disjoint(map);
3385         if (!map)
3386                 return NULL;
3387
3388         pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
3389
3390         for (i = 0; i < map->n; ++i) {
3391                 isl_pw_multi_aff *pma_i;
3392                 isl_basic_map *bmap;
3393                 bmap = isl_basic_map_copy(map->p[i]);
3394                 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
3395                 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
3396         }
3397
3398         isl_map_free(map);
3399         return pma;
3400 error:
3401         isl_map_free(map);
3402         return NULL;
3403 }
3404
3405 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3406  * taking into account that the output dimension at position "d"
3407  * can be represented as
3408  *
3409  *      x = floor((e(...) + c1) / m)
3410  *
3411  * given that constraint "i" is of the form
3412  *
3413  *      e(...) + c1 - m x >= 0
3414  *
3415  *
3416  * Let "map" be of the form
3417  *
3418  *      A -> B
3419  *
3420  * We construct a mapping
3421  *
3422  *      A -> [A -> x = floor(...)]
3423  *
3424  * apply that to the map, obtaining
3425  *
3426  *      [A -> x = floor(...)] -> B
3427  *
3428  * and equate dimension "d" to x.
3429  * We then compute a isl_pw_multi_aff representation of the resulting map
3430  * and plug in the mapping above.
3431  */
3432 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
3433         __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
3434 {
3435         isl_ctx *ctx;
3436         isl_space *space;
3437         isl_local_space *ls;
3438         isl_multi_aff *ma;
3439         isl_aff *aff;
3440         isl_vec *v;
3441         isl_map *insert;
3442         int offset;
3443         int n;
3444         int n_in;
3445         isl_pw_multi_aff *pma;
3446         int is_set;
3447
3448         is_set = isl_map_is_set(map);
3449
3450         offset = isl_basic_map_offset(hull, isl_dim_out);
3451         ctx = isl_map_get_ctx(map);
3452         space = isl_space_domain(isl_map_get_space(map));
3453         n_in = isl_space_dim(space, isl_dim_set);
3454         n = isl_space_dim(space, isl_dim_all);
3455
3456         v = isl_vec_alloc(ctx, 1 + 1 + n);
3457         if (v) {
3458                 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
3459                 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
3460         }
3461         isl_basic_map_free(hull);
3462
3463         ls = isl_local_space_from_space(isl_space_copy(space));
3464         aff = isl_aff_alloc_vec(ls, v);
3465         aff = isl_aff_floor(aff);
3466         if (is_set) {
3467                 isl_space_free(space);
3468                 ma = isl_multi_aff_from_aff(aff);
3469         } else {
3470                 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
3471                 ma = isl_multi_aff_range_product(ma,
3472                                                 isl_multi_aff_from_aff(aff));
3473         }
3474
3475         insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
3476         map = isl_map_apply_domain(map, insert);
3477         map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
3478         pma = isl_pw_multi_aff_from_map(map);
3479         pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
3480
3481         return pma;
3482 }
3483
3484 /* Is constraint "c" of the form
3485  *
3486  *      e(...) + c1 - m x >= 0
3487  *
3488  * or
3489  *
3490  *      -e(...) + c2 + m x >= 0
3491  *
3492  * where m > 1 and e only depends on parameters and input dimemnsions?
3493  *
3494  * "offset" is the offset of the output dimensions
3495  * "pos" is the position of output dimension x.
3496  */
3497 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
3498 {
3499         if (isl_int_is_zero(c[offset + d]))
3500                 return 0;
3501         if (isl_int_is_one(c[offset + d]))
3502                 return 0;
3503         if (isl_int_is_negone(c[offset + d]))
3504                 return 0;
3505         if (isl_seq_first_non_zero(c + offset, d) != -1)
3506                 return 0;
3507         if (isl_seq_first_non_zero(c + offset + d + 1,
3508                                     total - (offset + d + 1)) != -1)
3509                 return 0;
3510         return 1;
3511 }
3512
3513 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3514  *
3515  * As a special case, we first check if there is any pair of constraints,
3516  * shared by all the basic maps in "map" that force a given dimension
3517  * to be equal to the floor of some affine combination of the input dimensions.
3518  *
3519  * In particular, if we can find two constraints
3520  *
3521  *      e(...) + c1 - m x >= 0          i.e.,           m x <= e(...) + c1
3522  *
3523  * and
3524  *
3525  *      -e(...) + c2 + m x >= 0         i.e.,           m x >= e(...) - c2
3526  *
3527  * where m > 1 and e only depends on parameters and input dimemnsions,
3528  * and such that
3529  *
3530  *      c1 + c2 < m                     i.e.,           -c2 >= c1 - (m - 1)
3531  *
3532  * then we know that we can take
3533  *
3534  *      x = floor((e(...) + c1) / m)
3535  *
3536  * without having to perform any computation.
3537  *
3538  * Note that we know that
3539  *
3540  *      c1 + c2 >= 1
3541  *
3542  * If c1 + c2 were 0, then we would have detected an equality during
3543  * simplification.  If c1 + c2 were negative, then we would have detected
3544  * a contradiction.
3545  */
3546 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
3547         __isl_take isl_map *map)
3548 {
3549         int d, dim;
3550         int i, j, n;
3551         int offset, total;
3552         isl_int sum;
3553         isl_basic_map *hull;
3554
3555         hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
3556         if (!hull)
3557                 goto error;
3558
3559         isl_int_init(sum);
3560         dim = isl_map_dim(map, isl_dim_out);
3561         offset = isl_basic_map_offset(hull, isl_dim_out);
3562         total = 1 + isl_basic_map_total_dim(hull);
3563         n = hull->n_ineq;
3564         for (d = 0; d < dim; ++d) {
3565                 for (i = 0; i < n; ++i) {
3566                         if (!is_potential_div_constraint(hull->ineq[i],
3567                                                         offset, d, total))
3568                                 continue;
3569                         for (j = i + 1; j < n; ++j) {
3570                                 if (!isl_seq_is_neg(hull->ineq[i] + 1,
3571                                                 hull->ineq[j] + 1, total - 1))
3572                                         continue;
3573                                 isl_int_add(sum, hull->ineq[i][0],
3574                                                 hull->ineq[j][0]);
3575                                 if (isl_int_abs_lt(sum,
3576                                                     hull->ineq[i][offset + d]))
3577                                         break;
3578
3579                         }
3580                         if (j >= n)
3581                                 continue;
3582                         isl_int_clear(sum);
3583                         if (isl_int_is_pos(hull->ineq[j][offset + d]))
3584                                 j = i;
3585                         return pw_multi_aff_from_map_div(map, hull, d, j);
3586                 }
3587         }
3588         isl_int_clear(sum);
3589         isl_basic_map_free(hull);
3590         return pw_multi_aff_from_map_base(map);
3591 error:
3592         isl_map_free(map);
3593         isl_basic_map_free(hull);
3594         return NULL;
3595 }
3596
3597 /* Given an affine expression
3598  *
3599  *      [A -> B] -> f(A,B)
3600  *
3601  * construct an isl_multi_aff
3602  *
3603  *      [A -> B] -> B'
3604  *
3605  * such that dimension "d" in B' is set to "aff" and the remaining
3606  * dimensions are set equal to the corresponding dimensions in B.
3607  * "n_in" is the dimension of the space A.
3608  * "n_out" is the dimension of the space B.
3609  *
3610  * If "is_set" is set, then the affine expression is of the form
3611  *
3612  *      [B] -> f(B)
3613  *
3614  * and we construct an isl_multi_aff
3615  *
3616  *      B -> B'
3617  */
3618 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
3619         unsigned n_in, unsigned n_out, int is_set)
3620 {
3621         int i;
3622         isl_multi_aff *ma;
3623         isl_space *space, *space2;
3624         isl_local_space *ls;
3625
3626         space = isl_aff_get_domain_space(aff);
3627         ls = isl_local_space_from_space(isl_space_copy(space));
3628         space2 = isl_space_copy(space);
3629         if (!is_set)
3630                 space2 = isl_space_range(isl_space_unwrap(space2));
3631         space = isl_space_map_from_domain_and_range(space, space2);
3632         ma = isl_multi_aff_alloc(space);
3633         ma = isl_multi_aff_set_aff(ma, d, aff);
3634
3635         for (i = 0; i < n_out; ++i) {
3636                 if (i == d)
3637                         continue;
3638                 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3639                                                 isl_dim_set, n_in + i);
3640                 ma = isl_multi_aff_set_aff(ma, i, aff);
3641         }
3642
3643         isl_local_space_free(ls);
3644
3645         return ma;
3646 }
3647
3648 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
3649  * taking into account that the dimension at position "d" can be written as
3650  *
3651  *      x = m a + f(..)                                         (1)
3652  *
3653  * where m is equal to "gcd".
3654  * "i" is the index of the equality in "hull" that defines f(..).
3655  * In particular, the equality is of the form
3656  *
3657  *      f(..) - x + m g(existentials) = 0
3658  *
3659  * or
3660  *
3661  *      -f(..) + x + m g(existentials) = 0
3662  *
3663  * We basically plug (1) into "map", resulting in a map with "a"
3664  * in the range instead of "x".  The corresponding isl_pw_multi_aff
3665  * defining "a" is then plugged back into (1) to obtain a definition fro "x".
3666  *
3667  * Specifically, given the input map
3668  *
3669  *      A -> B
3670  *
3671  * We first wrap it into a set
3672  *
3673  *      [A -> B]
3674  *
3675  * and define (1) on top of the corresponding space, resulting in "aff".
3676  * We use this to create an isl_multi_aff that maps the output position "d"
3677  * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
3678  * We plug this into the wrapped map, unwrap the result and compute the
3679  * corresponding isl_pw_multi_aff.
3680  * The result is an expression
3681  *
3682  *      A -> T(A)
3683  *
3684  * We adjust that to
3685  *
3686  *      A -> [A -> T(A)]
3687  *
3688  * so that we can plug that into "aff", after extending the latter to
3689  * a mapping
3690  *
3691  *      [A -> B] -> B'
3692  *
3693  *
3694  * If "map" is actually a set, then there is no "A" space, meaning
3695  * that we do not need to perform any wrapping, and that the result
3696  * of the recursive call is of the form
3697  *
3698  *      [T]
3699  *
3700  * which is plugged into a mapping of the form
3701  *
3702  *      B -> B'
3703  */
3704 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
3705         __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
3706         isl_int gcd)
3707 {
3708         isl_set *set;
3709         isl_space *space;
3710         isl_local_space *ls;
3711         isl_aff *aff;
3712         isl_multi_aff *ma;
3713         isl_pw_multi_aff *pma, *id;
3714         unsigned n_in;
3715         unsigned o_out;
3716         unsigned n_out;
3717         int is_set;
3718
3719         is_set = isl_map_is_set(map);
3720
3721         n_in = isl_basic_map_dim(hull, isl_dim_in);
3722         n_out = isl_basic_map_dim(hull, isl_dim_out);
3723         o_out = isl_basic_map_offset(hull, isl_dim_out);
3724
3725         if (is_set)
3726                 set = map;
3727         else
3728                 set = isl_map_wrap(map);
3729         space = isl_space_map_from_set(isl_set_get_space(set));
3730         ma = isl_multi_aff_identity(space);
3731         ls = isl_local_space_from_space(isl_set_get_space(set));
3732         aff = isl_aff_alloc(ls);
3733         if (aff) {
3734                 isl_int_set_si(aff->v->el[0], 1);
3735                 if (isl_int_is_one(hull->eq[i][o_out + d]))
3736                         isl_seq_neg(aff->v->el + 1, hull->eq[i],
3737                                     aff->v->size - 1);
3738                 else
3739                         isl_seq_cpy(aff->v->el + 1, hull->eq[i],
3740                                     aff->v->size - 1);
3741                 isl_int_set(aff->v->el[1 + o_out + d], gcd);
3742         }
3743         ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
3744         set = isl_set_preimage_multi_aff(set, ma);
3745
3746         ma = range_map(aff, d, n_in, n_out, is_set);
3747
3748         if (is_set)
3749                 map = set;
3750         else
3751                 map = isl_set_unwrap(set);
3752         pma = isl_pw_multi_aff_from_map(set);
3753
3754         if (!is_set) {
3755                 space = isl_pw_multi_aff_get_domain_space(pma);
3756                 space = isl_space_map_from_set(space);
3757                 id = isl_pw_multi_aff_identity(space);
3758                 pma = isl_pw_multi_aff_range_product(id, pma);
3759         }
3760         id = isl_pw_multi_aff_from_multi_aff(ma);
3761         pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
3762
3763         isl_basic_map_free(hull);
3764         return pma;
3765 }
3766
3767 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
3768  *
3769  * As a special case, we first check if all output dimensions are uniquely
3770  * defined in terms of the parameters and input dimensions over the entire
3771  * domain.  If so, we extract the desired isl_pw_multi_aff directly
3772  * from the affine hull of "map" and its domain.
3773  *
3774  * Otherwise, we check if any of the output dimensions is "strided".
3775  * That is, we check if can be written as
3776  *
3777  *      x = m a + f(..)
3778  *
3779  * with m greater than 1, a some combination of existentiall quantified
3780  * variables and f and expression in the parameters and input dimensions.
3781  * If so, we remove the stride in pw_multi_aff_from_map_stride.
3782  *
3783  * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
3784  * special case.
3785  */
3786 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
3787 {
3788         int i, j;
3789         int sv;
3790         isl_basic_map *hull;
3791         unsigned n_out;
3792         unsigned o_out;
3793         unsigned n_div;
3794         unsigned o_div;
3795         isl_int gcd;
3796
3797         if (!map)
3798                 return NULL;
3799
3800         hull = isl_map_affine_hull(isl_map_copy(map));
3801         sv = isl_basic_map_plain_is_single_valued(hull);
3802         if (sv >= 0 && sv)
3803                 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
3804         if (sv < 0)
3805                 hull = isl_basic_map_free(hull);
3806         if (!hull)
3807                 goto error;
3808
3809         n_div = isl_basic_map_dim(hull, isl_dim_div);
3810         o_div = isl_basic_map_offset(hull, isl_dim_div);
3811
3812         if (n_div == 0) {
3813                 isl_basic_map_free(hull);
3814                 return pw_multi_aff_from_map_check_div(map);
3815         }
3816
3817         isl_int_init(gcd);
3818
3819         n_out = isl_basic_map_dim(hull, isl_dim_out);
3820         o_out = isl_basic_map_offset(hull, isl_dim_out);
3821
3822         for (i = 0; i < n_out; ++i) {
3823                 for (j = 0; j < hull->n_eq; ++j) {
3824                         isl_int *eq = hull->eq[j];
3825                         isl_pw_multi_aff *res;
3826
3827                         if (!isl_int_is_one(eq[o_out + i]) &&
3828                             !isl_int_is_negone(eq[o_out + i]))
3829                                 continue;
3830                         if (isl_seq_first_non_zero(eq + o_out, i) != -1)
3831                                 continue;
3832                         if (isl_seq_first_non_zero(eq + o_out + i + 1,
3833                                                     n_out - (i + 1)) != -1)
3834                                 continue;
3835                         isl_seq_gcd(eq + o_div, n_div, &gcd);
3836                         if (isl_int_is_zero(gcd))
3837                                 continue;
3838                         if (isl_int_is_one(gcd))
3839                                 continue;
3840
3841                         res = pw_multi_aff_from_map_stride(map, hull,
3842                                                                 i, j, gcd);
3843                         isl_int_clear(gcd);
3844                         return res;
3845                 }
3846         }
3847
3848         isl_int_clear(gcd);
3849         isl_basic_map_free(hull);
3850         return pw_multi_aff_from_map_check_div(map);
3851 error:
3852         isl_map_free(map);
3853         return NULL;
3854 }
3855
3856 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
3857 {
3858         return isl_pw_multi_aff_from_map(set);
3859 }
3860
3861 /* Convert "map" into an isl_pw_multi_aff (if possible) and
3862  * add it to *user.
3863  */
3864 static int pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
3865 {
3866         isl_union_pw_multi_aff **upma = user;
3867         isl_pw_multi_aff *pma;
3868
3869         pma = isl_pw_multi_aff_from_map(map);
3870         *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
3871
3872         return *upma ? 0 : -1;
3873 }
3874
3875 /* Try and create an isl_union_pw_multi_aff that is equivalent
3876  * to the given isl_union_map.
3877  * The isl_union_map is required to be single-valued in each space.
3878  * Otherwise, an error is produced.
3879  */
3880 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
3881         __isl_take isl_union_map *umap)
3882 {
3883         isl_space *space;
3884         isl_union_pw_multi_aff *upma;
3885
3886         space = isl_union_map_get_space(umap);
3887         upma = isl_union_pw_multi_aff_empty(space);
3888         if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
3889                 upma = isl_union_pw_multi_aff_free(upma);
3890         isl_union_map_free(umap);
3891
3892         return upma;
3893 }
3894
3895 /* Try and create an isl_union_pw_multi_aff that is equivalent
3896  * to the given isl_union_set.
3897  * The isl_union_set is required to be a singleton in each space.
3898  * Otherwise, an error is produced.
3899  */
3900 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
3901         __isl_take isl_union_set *uset)
3902 {
3903         return isl_union_pw_multi_aff_from_union_map(uset);
3904 }
3905
3906 /* Return the piecewise affine expression "set ? 1 : 0".
3907  */
3908 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
3909 {
3910         isl_pw_aff *pa;
3911         isl_space *space = isl_set_get_space(set);
3912         isl_local_space *ls = isl_local_space_from_space(space);
3913         isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
3914         isl_aff *one = isl_aff_zero_on_domain(ls);
3915
3916         one = isl_aff_add_constant_si(one, 1);
3917         pa = isl_pw_aff_alloc(isl_set_copy(set), one);
3918         set = isl_set_complement(set);
3919         pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
3920
3921         return pa;
3922 }
3923
3924 /* Plug in "subs" for dimension "type", "pos" of "aff".
3925  *
3926  * Let i be the dimension to replace and let "subs" be of the form
3927  *
3928  *      f/d
3929  *
3930  * and "aff" of the form
3931  *
3932  *      (a i + g)/m
3933  *
3934  * The result is
3935  *
3936  *      (a f + d g')/(m d)
3937  *
3938  * where g' is the result of plugging in "subs" in each of the integer
3939  * divisions in g.
3940  */
3941 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
3942         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
3943 {
3944         isl_ctx *ctx;
3945         isl_int v;
3946
3947         aff = isl_aff_cow(aff);
3948         if (!aff || !subs)
3949                 return isl_aff_free(aff);
3950
3951         ctx = isl_aff_get_ctx(aff);
3952         if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
3953                 isl_die(ctx, isl_error_invalid,
3954                         "spaces don't match", return isl_aff_free(aff));
3955         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
3956                 isl_die(ctx, isl_error_unsupported,
3957                         "cannot handle divs yet", return isl_aff_free(aff));
3958
3959         aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
3960         if (!aff->ls)
3961                 return isl_aff_free(aff);
3962
3963         aff->v = isl_vec_cow(aff->v);
3964         if (!aff->v)
3965                 return isl_aff_free(aff);
3966
3967         pos += isl_local_space_offset(aff->ls, type);
3968
3969         isl_int_init(v);
3970         isl_seq_substitute(aff->v->el, pos, subs->v->el,
3971                             aff->v->size, subs->v->size, v);
3972         isl_int_clear(v);
3973
3974         return aff;
3975 }
3976
3977 /* Plug in "subs" for dimension "type", "pos" in each of the affine
3978  * expressions in "maff".
3979  */
3980 __isl_give isl_multi_aff *isl_multi_aff_substitute(
3981         __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
3982         __isl_keep isl_aff *subs)
3983 {
3984         int i;
3985
3986         maff = isl_multi_aff_cow(maff);
3987         if (!maff || !subs)
3988                 return isl_multi_aff_free(maff);
3989
3990         if (type == isl_dim_in)
3991                 type = isl_dim_set;
3992
3993         for (i = 0; i < maff->n; ++i) {
3994                 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
3995                 if (!maff->p[i])
3996                         return isl_multi_aff_free(maff);
3997         }
3998
3999         return maff;
4000 }
4001
4002 /* Plug in "subs" for dimension "type", "pos" of "pma".
4003  *
4004  * pma is of the form
4005  *
4006  *      A_i(v) -> M_i(v)
4007  *
4008  * while subs is of the form
4009  *
4010  *      v' = B_j(v) -> S_j
4011  *
4012  * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
4013  * has a contribution in the result, in particular
4014  *
4015  *      C_ij(S_j) -> M_i(S_j)
4016  *
4017  * Note that plugging in S_j in C_ij may also result in an empty set
4018  * and this contribution should simply be discarded.
4019  */
4020 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
4021         __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
4022         __isl_keep isl_pw_aff *subs)
4023 {
4024         int i, j, n;
4025         isl_pw_multi_aff *res;
4026
4027         if (!pma || !subs)
4028                 return isl_pw_multi_aff_free(pma);
4029
4030         n = pma->n * subs->n;
4031         res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
4032
4033         for (i = 0; i < pma->n; ++i) {
4034                 for (j = 0; j < subs->n; ++j) {
4035                         isl_set *common;
4036                         isl_multi_aff *res_ij;
4037                         int empty;
4038
4039                         common = isl_set_intersect(
4040                                         isl_set_copy(pma->p[i].set),
4041                                         isl_set_copy(subs->p[j].set));
4042                         common = isl_set_substitute(common,
4043                                         type, pos, subs->p[j].aff);
4044                         empty = isl_set_plain_is_empty(common);
4045                         if (empty < 0 || empty) {
4046                                 isl_set_free(common);
4047                                 if (empty < 0)
4048                                         goto error;
4049                                 continue;
4050                         }
4051
4052                         res_ij = isl_multi_aff_substitute(
4053                                         isl_multi_aff_copy(pma->p[i].maff),
4054                                         type, pos, subs->p[j].aff);
4055
4056                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4057                 }
4058         }
4059
4060         isl_pw_multi_aff_free(pma);
4061         return res;
4062 error:
4063         isl_pw_multi_aff_free(pma);
4064         isl_pw_multi_aff_free(res);
4065         return NULL;
4066 }
4067
4068 /* Compute the preimage of a range of dimensions in the affine expression "src"
4069  * under "ma" and put the result in "dst".  The number of dimensions in "src"
4070  * that precede the range is given by "n_before".  The number of dimensions
4071  * in the range is given by the number of output dimensions of "ma".
4072  * The number of dimensions that follow the range is given by "n_after".
4073  * If "has_denom" is set (to one),
4074  * then "src" and "dst" have an extra initial denominator.
4075  * "n_div_ma" is the number of existentials in "ma"
4076  * "n_div_bset" is the number of existentials in "src"
4077  * The resulting "dst" (which is assumed to have been allocated by
4078  * the caller) contains coefficients for both sets of existentials,
4079  * first those in "ma" and then those in "src".
4080  * f, c1, c2 and g are temporary objects that have been initialized
4081  * by the caller.
4082  *
4083  * Let src represent the expression
4084  *
4085  *      (a(p) + f_u u + b v + f_w w + c(divs))/d
4086  *
4087  * and let ma represent the expressions
4088  *
4089  *      v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
4090  *
4091  * We start out with the following expression for dst:
4092  *
4093  *      (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
4094  *
4095  * with the multiplication factor f initially equal to 1
4096  * and f \sum_i b_i v_i kept separately.
4097  * For each x_i that we substitute, we multiply the numerator
4098  * (and denominator) of dst by c_1 = m_i and add the numerator
4099  * of the x_i expression multiplied by c_2 = f b_i,
4100  * after removing the common factors of c_1 and c_2.
4101  * The multiplication factor f also needs to be multiplied by c_1
4102  * for the next x_j, j > i.
4103  */
4104 void isl_seq_preimage(isl_int *dst, isl_int *src,
4105         __isl_keep isl_multi_aff *ma, int n_before, int n_after,
4106         int n_div_ma, int n_div_bmap,
4107         isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
4108 {
4109         int i;
4110         int n_param, n_in, n_out;
4111         int o_dst, o_src;
4112
4113         n_param = isl_multi_aff_dim(ma, isl_dim_param);
4114         n_in = isl_multi_aff_dim(ma, isl_dim_in);
4115         n_out = isl_multi_aff_dim(ma, isl_dim_out);
4116
4117         isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
4118         o_dst = o_src = has_denom + 1 + n_param + n_before;
4119         isl_seq_clr(dst + o_dst, n_in);
4120         o_dst += n_in;
4121         o_src += n_out;
4122         isl_seq_cpy(dst + o_dst, src + o_src, n_after);
4123         o_dst += n_after;
4124         o_src += n_after;
4125         isl_seq_clr(dst + o_dst, n_div_ma);
4126         o_dst += n_div_ma;
4127         isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
4128
4129         isl_int_set_si(f, 1);
4130
4131         for (i = 0; i < n_out; ++i) {
4132                 int offset = has_denom + 1 + n_param + n_before + i;
4133
4134                 if (isl_int_is_zero(src[offset]))
4135                         continue;
4136                 isl_int_set(c1, ma->p[i]->v->el[0]);
4137                 isl_int_mul(c2, f, src[offset]);
4138                 isl_int_gcd(g, c1, c2);
4139                 isl_int_divexact(c1, c1, g);
4140                 isl_int_divexact(c2, c2, g);
4141
4142                 isl_int_mul(f, f, c1);
4143                 o_dst = has_denom;
4144                 o_src = 1;
4145                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4146                                 c2, ma->p[i]->v->el + o_src, 1 + n_param);
4147                 o_dst += 1 + n_param;
4148                 o_src += 1 + n_param;
4149                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
4150                 o_dst += n_before;
4151                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4152                                 c2, ma->p[i]->v->el + o_src, n_in);
4153                 o_dst += n_in;
4154                 o_src += n_in;
4155                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
4156                 o_dst += n_after;
4157                 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
4158                                 c2, ma->p[i]->v->el + o_src, n_div_ma);
4159                 o_dst += n_div_ma;
4160                 o_src += n_div_ma;
4161                 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
4162                 if (has_denom)
4163                         isl_int_mul(dst[0], dst[0], c1);
4164         }
4165 }
4166
4167 /* Compute the pullback of "aff" by the function represented by "ma".
4168  * In other words, plug in "ma" in "aff".  The result is an affine expression
4169  * defined over the domain space of "ma".
4170  *
4171  * If "aff" is represented by
4172  *
4173  *      (a(p) + b x + c(divs))/d
4174  *
4175  * and ma is represented by
4176  *
4177  *      x = D(p) + F(y) + G(divs')
4178  *
4179  * then the result is
4180  *
4181  *      (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
4182  *
4183  * The divs in the local space of the input are similarly adjusted
4184  * through a call to isl_local_space_preimage_multi_aff.
4185  */
4186 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
4187         __isl_take isl_multi_aff *ma)
4188 {
4189         isl_aff *res = NULL;
4190         isl_local_space *ls;
4191         int n_div_aff, n_div_ma;
4192         isl_int f, c1, c2, g;
4193
4194         ma = isl_multi_aff_align_divs(ma);
4195         if (!aff || !ma)
4196                 goto error;
4197
4198         n_div_aff = isl_aff_dim(aff, isl_dim_div);
4199         n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
4200
4201         ls = isl_aff_get_domain_local_space(aff);
4202         ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
4203         res = isl_aff_alloc(ls);
4204         if (!res)
4205                 goto error;
4206
4207         isl_int_init(f);
4208         isl_int_init(c1);
4209         isl_int_init(c2);
4210         isl_int_init(g);
4211
4212         isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
4213                         f, c1, c2, g, 1);
4214
4215         isl_int_clear(f);
4216         isl_int_clear(c1);
4217         isl_int_clear(c2);
4218         isl_int_clear(g);
4219
4220         isl_aff_free(aff);
4221         isl_multi_aff_free(ma);
4222         res = isl_aff_normalize(res);
4223         return res;
4224 error:
4225         isl_aff_free(aff);
4226         isl_multi_aff_free(ma);
4227         isl_aff_free(res);
4228         return NULL;
4229 }
4230
4231 /* Compute the pullback of "ma1" by the function represented by "ma2".
4232  * In other words, plug in "ma2" in "ma1".
4233  */
4234 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
4235         __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
4236 {
4237         int i;
4238         isl_space *space = NULL;
4239
4240         ma2 = isl_multi_aff_align_divs(ma2);
4241         ma1 = isl_multi_aff_cow(ma1);
4242         if (!ma1 || !ma2)
4243                 goto error;
4244
4245         space = isl_space_join(isl_multi_aff_get_space(ma2),
4246                                 isl_multi_aff_get_space(ma1));
4247
4248         for (i = 0; i < ma1->n; ++i) {
4249                 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
4250                                                     isl_multi_aff_copy(ma2));
4251                 if (!ma1->p[i])
4252                         goto error;
4253         }
4254
4255         ma1 = isl_multi_aff_reset_space(ma1, space);
4256         isl_multi_aff_free(ma2);
4257         return ma1;
4258 error:
4259         isl_space_free(space);
4260         isl_multi_aff_free(ma2);
4261         isl_multi_aff_free(ma1);
4262         return NULL;
4263 }
4264
4265 /* Extend the local space of "dst" to include the divs
4266  * in the local space of "src".
4267  */
4268 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
4269         __isl_keep isl_aff *src)
4270 {
4271         isl_ctx *ctx;
4272         int *exp1 = NULL;
4273         int *exp2 = NULL;
4274         isl_mat *div;
4275
4276         if (!src || !dst)
4277                 return isl_aff_free(dst);
4278
4279         ctx = isl_aff_get_ctx(src);
4280         if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
4281                 isl_die(ctx, isl_error_invalid,
4282                         "spaces don't match", goto error);
4283
4284         if (src->ls->div->n_row == 0)
4285                 return dst;
4286
4287         exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
4288         exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
4289         if (!exp1 || !exp2)
4290                 goto error;
4291
4292         div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
4293         dst = isl_aff_expand_divs(dst, div, exp2);
4294         free(exp1);
4295         free(exp2);
4296
4297         return dst;
4298 error:
4299         free(exp1);
4300         free(exp2);
4301         return isl_aff_free(dst);
4302 }
4303
4304 /* Adjust the local spaces of the affine expressions in "maff"
4305  * such that they all have the save divs.
4306  */
4307 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
4308         __isl_take isl_multi_aff *maff)
4309 {
4310         int i;
4311
4312         if (!maff)
4313                 return NULL;
4314         if (maff->n == 0)
4315                 return maff;
4316         maff = isl_multi_aff_cow(maff);
4317         if (!maff)
4318                 return NULL;
4319
4320         for (i = 1; i < maff->n; ++i)
4321                 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
4322         for (i = 1; i < maff->n; ++i) {
4323                 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
4324                 if (!maff->p[i])
4325                         return isl_multi_aff_free(maff);
4326         }
4327
4328         return maff;
4329 }
4330
4331 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
4332 {
4333         aff = isl_aff_cow(aff);
4334         if (!aff)
4335                 return NULL;
4336
4337         aff->ls = isl_local_space_lift(aff->ls);
4338         if (!aff->ls)
4339                 return isl_aff_free(aff);
4340
4341         return aff;
4342 }
4343
4344 /* Lift "maff" to a space with extra dimensions such that the result
4345  * has no more existentially quantified variables.
4346  * If "ls" is not NULL, then *ls is assigned the local space that lies
4347  * at the basis of the lifting applied to "maff".
4348  */
4349 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
4350         __isl_give isl_local_space **ls)
4351 {
4352         int i;
4353         isl_space *space;
4354         unsigned n_div;
4355
4356         if (ls)
4357                 *ls = NULL;
4358
4359         if (!maff)
4360                 return NULL;
4361
4362         if (maff->n == 0) {
4363                 if (ls) {
4364                         isl_space *space = isl_multi_aff_get_domain_space(maff);
4365                         *ls = isl_local_space_from_space(space);
4366                         if (!*ls)
4367                                 return isl_multi_aff_free(maff);
4368                 }
4369                 return maff;
4370         }
4371
4372         maff = isl_multi_aff_cow(maff);
4373         maff = isl_multi_aff_align_divs(maff);
4374         if (!maff)
4375                 return NULL;
4376
4377         n_div = isl_aff_dim(maff->p[0], isl_dim_div);
4378         space = isl_multi_aff_get_space(maff);
4379         space = isl_space_lift(isl_space_domain(space), n_div);
4380         space = isl_space_extend_domain_with_range(space,
4381                                                 isl_multi_aff_get_space(maff));
4382         if (!space)
4383                 return isl_multi_aff_free(maff);
4384         isl_space_free(maff->space);
4385         maff->space = space;
4386
4387         if (ls) {
4388                 *ls = isl_aff_get_domain_local_space(maff->p[0]);
4389                 if (!*ls)
4390                         return isl_multi_aff_free(maff);
4391         }
4392
4393         for (i = 0; i < maff->n; ++i) {
4394                 maff->p[i] = isl_aff_lift(maff->p[i]);
4395                 if (!maff->p[i])
4396                         goto error;
4397         }
4398
4399         return maff;
4400 error:
4401         if (ls)
4402                 isl_local_space_free(*ls);
4403         return isl_multi_aff_free(maff);
4404 }
4405
4406
4407 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
4408  */
4409 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
4410         __isl_keep isl_pw_multi_aff *pma, int pos)
4411 {
4412         int i;
4413         int n_out;
4414         isl_space *space;
4415         isl_pw_aff *pa;
4416
4417         if (!pma)
4418                 return NULL;
4419
4420         n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
4421         if (pos < 0 || pos >= n_out)
4422                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4423                         "index out of bounds", return NULL);
4424
4425         space = isl_pw_multi_aff_get_space(pma);
4426         space = isl_space_drop_dims(space, isl_dim_out,
4427                                     pos + 1, n_out - pos - 1);
4428         space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
4429
4430         pa = isl_pw_aff_alloc_size(space, pma->n);
4431         for (i = 0; i < pma->n; ++i) {
4432                 isl_aff *aff;
4433                 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
4434                 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
4435         }
4436
4437         return pa;
4438 }
4439
4440 /* Return an isl_pw_multi_aff with the given "set" as domain and
4441  * an unnamed zero-dimensional range.
4442  */
4443 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
4444         __isl_take isl_set *set)
4445 {
4446         isl_multi_aff *ma;
4447         isl_space *space;
4448
4449         space = isl_set_get_space(set);
4450         space = isl_space_from_domain(space);
4451         ma = isl_multi_aff_zero(space);
4452         return isl_pw_multi_aff_alloc(set, ma);
4453 }
4454
4455 /* Add an isl_pw_multi_aff with the given "set" as domain and
4456  * an unnamed zero-dimensional range to *user.
4457  */
4458 static int add_pw_multi_aff_from_domain(__isl_take isl_set *set, void *user)
4459 {
4460         isl_union_pw_multi_aff **upma = user;
4461         isl_pw_multi_aff *pma;
4462
4463         pma = isl_pw_multi_aff_from_domain(set);
4464         *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
4465
4466         return 0;
4467 }
4468
4469 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
4470  * an unnamed zero-dimensional range.
4471  */
4472 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
4473         __isl_take isl_union_set *uset)
4474 {
4475         isl_space *space;
4476         isl_union_pw_multi_aff *upma;
4477
4478         if (!uset)
4479                 return NULL;
4480
4481         space = isl_union_set_get_space(uset);
4482         upma = isl_union_pw_multi_aff_empty(space);
4483
4484         if (isl_union_set_foreach_set(uset,
4485                                     &add_pw_multi_aff_from_domain, &upma) < 0)
4486                 goto error;
4487
4488         isl_union_set_free(uset);
4489         return upma;
4490 error:
4491         isl_union_set_free(uset);
4492         isl_union_pw_multi_aff_free(upma);
4493         return NULL;
4494 }
4495
4496 /* Convert "pma" to an isl_map and add it to *umap.
4497  */
4498 static int map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma, void *user)
4499 {
4500         isl_union_map **umap = user;
4501         isl_map *map;
4502
4503         map = isl_map_from_pw_multi_aff(pma);
4504         *umap = isl_union_map_add_map(*umap, map);
4505
4506         return 0;
4507 }
4508
4509 /* Construct a union map mapping the domain of the union
4510  * piecewise multi-affine expression to its range, with each dimension
4511  * in the range equated to the corresponding affine expression on its cell.
4512  */
4513 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
4514         __isl_take isl_union_pw_multi_aff *upma)
4515 {
4516         isl_space *space;
4517         isl_union_map *umap;
4518
4519         if (!upma)
4520                 return NULL;
4521
4522         space = isl_union_pw_multi_aff_get_space(upma);
4523         umap = isl_union_map_empty(space);
4524
4525         if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
4526                                         &map_from_pw_multi_aff, &umap) < 0)
4527                 goto error;
4528
4529         isl_union_pw_multi_aff_free(upma);
4530         return umap;
4531 error:
4532         isl_union_pw_multi_aff_free(upma);
4533         isl_union_map_free(umap);
4534         return NULL;
4535 }
4536
4537 /* Local data for bin_entry and the callback "fn".
4538  */
4539 struct isl_union_pw_multi_aff_bin_data {
4540         isl_union_pw_multi_aff *upma2;
4541         isl_union_pw_multi_aff *res;
4542         isl_pw_multi_aff *pma;
4543         int (*fn)(void **entry, void *user);
4544 };
4545
4546 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
4547  * and call data->fn for each isl_pw_multi_aff in data->upma2.
4548  */
4549 static int bin_entry(void **entry, void *user)
4550 {
4551         struct isl_union_pw_multi_aff_bin_data *data = user;
4552         isl_pw_multi_aff *pma = *entry;
4553
4554         data->pma = pma;
4555         if (isl_hash_table_foreach(data->upma2->dim->ctx, &data->upma2->table,
4556                                    data->fn, data) < 0)
4557                 return -1;
4558
4559         return 0;
4560 }
4561
4562 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
4563  * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
4564  * passed as user field) and the isl_pw_multi_aff from upma2 is available
4565  * as *entry.  The callback should adjust data->res if desired.
4566  */
4567 static __isl_give isl_union_pw_multi_aff *bin_op(
4568         __isl_take isl_union_pw_multi_aff *upma1,
4569         __isl_take isl_union_pw_multi_aff *upma2,
4570         int (*fn)(void **entry, void *user))
4571 {
4572         isl_space *space;
4573         struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
4574
4575         space = isl_union_pw_multi_aff_get_space(upma2);
4576         upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
4577         space = isl_union_pw_multi_aff_get_space(upma1);
4578         upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
4579
4580         if (!upma1 || !upma2)
4581                 goto error;
4582
4583         data.upma2 = upma2;
4584         data.res = isl_union_pw_multi_aff_alloc(isl_space_copy(upma1->dim),
4585                                        upma1->table.n);
4586         if (isl_hash_table_foreach(upma1->dim->ctx, &upma1->table,
4587                                    &bin_entry, &data) < 0)
4588                 goto error;
4589
4590         isl_union_pw_multi_aff_free(upma1);
4591         isl_union_pw_multi_aff_free(upma2);
4592         return data.res;
4593 error:
4594         isl_union_pw_multi_aff_free(upma1);
4595         isl_union_pw_multi_aff_free(upma2);
4596         isl_union_pw_multi_aff_free(data.res);
4597         return NULL;
4598 }
4599
4600 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4601  * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4602  */
4603 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
4604         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4605 {
4606         isl_space *space;
4607
4608         space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4609                                         isl_pw_multi_aff_get_space(pma2));
4610         return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4611                                             &isl_multi_aff_range_product);
4612 }
4613
4614 /* Given two isl_pw_multi_affs A -> B and C -> D,
4615  * construct an isl_pw_multi_aff (A * C) -> [B -> D].
4616  */
4617 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
4618         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4619 {
4620         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4621                                             &pw_multi_aff_range_product);
4622 }
4623
4624 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
4625  * construct an isl_pw_multi_aff (A * C) -> (B, D).
4626  */
4627 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
4628         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4629 {
4630         isl_space *space;
4631
4632         space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
4633                                         isl_pw_multi_aff_get_space(pma2));
4634         space = isl_space_flatten_range(space);
4635         return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
4636                                             &isl_multi_aff_flat_range_product);
4637 }
4638
4639 /* Given two isl_pw_multi_affs A -> B and C -> D,
4640  * construct an isl_pw_multi_aff (A * C) -> (B, D).
4641  */
4642 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
4643         __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4644 {
4645         return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4646                                             &pw_multi_aff_flat_range_product);
4647 }
4648
4649 /* If data->pma and *entry have the same domain space, then compute
4650  * their flat range product and the result to data->res.
4651  */
4652 static int flat_range_product_entry(void **entry, void *user)
4653 {
4654         struct isl_union_pw_multi_aff_bin_data *data = user;
4655         isl_pw_multi_aff *pma2 = *entry;
4656
4657         if (!isl_space_tuple_match(data->pma->dim, isl_dim_in,
4658                                  pma2->dim, isl_dim_in))
4659                 return 0;
4660
4661         pma2 = isl_pw_multi_aff_flat_range_product(
4662                                         isl_pw_multi_aff_copy(data->pma),
4663                                         isl_pw_multi_aff_copy(pma2));
4664
4665         data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
4666
4667         return 0;
4668 }
4669
4670 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
4671  * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
4672  */
4673 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
4674         __isl_take isl_union_pw_multi_aff *upma1,
4675         __isl_take isl_union_pw_multi_aff *upma2)
4676 {
4677         return bin_op(upma1, upma2, &flat_range_product_entry);
4678 }
4679
4680 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4681  * The parameters are assumed to have been aligned.
4682  *
4683  * The implementation essentially performs an isl_pw_*_on_shared_domain,
4684  * except that it works on two different isl_pw_* types.
4685  */
4686 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
4687         __isl_take isl_pw_multi_aff *pma, unsigned pos,
4688         __isl_take isl_pw_aff *pa)
4689 {
4690         int i, j, n;
4691         isl_pw_multi_aff *res = NULL;
4692
4693         if (!pma || !pa)
4694                 goto error;
4695
4696         if (!isl_space_tuple_match(pma->dim, isl_dim_in, pa->dim, isl_dim_in))
4697                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4698                         "domains don't match", goto error);
4699         if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
4700                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4701                         "index out of bounds", goto error);
4702
4703         n = pma->n * pa->n;
4704         res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
4705
4706         for (i = 0; i < pma->n; ++i) {
4707                 for (j = 0; j < pa->n; ++j) {
4708                         isl_set *common;
4709                         isl_multi_aff *res_ij;
4710                         int empty;
4711
4712                         common = isl_set_intersect(isl_set_copy(pma->p[i].set),
4713                                                    isl_set_copy(pa->p[j].set));
4714                         empty = isl_set_plain_is_empty(common);
4715                         if (empty < 0 || empty) {
4716                                 isl_set_free(common);
4717                                 if (empty < 0)
4718                                         goto error;
4719                                 continue;
4720                         }
4721
4722                         res_ij = isl_multi_aff_set_aff(
4723                                         isl_multi_aff_copy(pma->p[i].maff), pos,
4724                                         isl_aff_copy(pa->p[j].aff));
4725                         res_ij = isl_multi_aff_gist(res_ij,
4726                                         isl_set_copy(common));
4727
4728                         res = isl_pw_multi_aff_add_piece(res, common, res_ij);
4729                 }
4730         }
4731
4732         isl_pw_multi_aff_free(pma);
4733         isl_pw_aff_free(pa);
4734         return res;
4735 error:
4736         isl_pw_multi_aff_free(pma);
4737         isl_pw_aff_free(pa);
4738         return isl_pw_multi_aff_free(res);
4739 }
4740
4741 /* Replace the affine expressions at position "pos" in "pma" by "pa".
4742  */
4743 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
4744         __isl_take isl_pw_multi_aff *pma, unsigned pos,
4745         __isl_take isl_pw_aff *pa)
4746 {
4747         if (!pma || !pa)
4748                 goto error;
4749         if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
4750                 return pw_multi_aff_set_pw_aff(pma, pos, pa);
4751         if (!isl_space_has_named_params(pma->dim) ||
4752             !isl_space_has_named_params(pa->dim))
4753                 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4754                         "unaligned unnamed parameters", goto error);
4755         pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
4756         pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
4757         return pw_multi_aff_set_pw_aff(pma, pos, pa);
4758 error:
4759         isl_pw_multi_aff_free(pma);
4760         isl_pw_aff_free(pa);
4761         return NULL;
4762 }
4763
4764 /* Check that the domain space of "pa" matches "space".
4765  *
4766  * Return 0 on success and -1 on error.
4767  */
4768 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
4769         __isl_keep isl_space *space)
4770 {
4771         isl_space *pa_space;
4772         int match;
4773
4774         if (!pa || !space)
4775                 return -1;
4776
4777         pa_space = isl_pw_aff_get_space(pa);
4778
4779         match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
4780         if (match < 0)
4781                 goto error;
4782         if (!match)
4783                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4784                         "parameters don't match", goto error);
4785         match = isl_space_tuple_match(space, isl_dim_in, pa_space, isl_dim_in);
4786         if (match < 0)
4787                 goto error;
4788         if (!match)
4789                 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
4790                         "domains don't match", goto error);
4791         isl_space_free(pa_space);
4792         return 0;
4793 error:
4794         isl_space_free(pa_space);
4795         return -1;
4796 }
4797
4798 #undef BASE
4799 #define BASE pw_aff
4800
4801 #include <isl_multi_templ.c>
4802
4803 /* Scale the first elements of "ma" by the corresponding elements of "vec".
4804  */
4805 __isl_give isl_multi_aff *isl_multi_aff_scale_vec(__isl_take isl_multi_aff *ma,
4806         __isl_take isl_vec *vec)
4807 {
4808         int i, n;
4809         isl_int v;
4810
4811         if (!ma || !vec)
4812                 goto error;
4813
4814         n = isl_multi_aff_dim(ma, isl_dim_out);
4815         if (isl_vec_size(vec) < n)
4816                 n = isl_vec_size(vec);
4817
4818         isl_int_init(v);
4819         for (i = 0; i < n; ++i) {
4820                 isl_aff *aff;
4821
4822                 isl_vec_get_element(vec, i, &v);
4823
4824                 aff = isl_multi_aff_get_aff(ma, i);
4825                 aff = isl_aff_scale(aff, v);
4826                 ma = isl_multi_aff_set_aff(ma, i, aff);
4827         }
4828         isl_int_clear(v);
4829
4830         isl_vec_free(vec);
4831         return ma;
4832 error:
4833         isl_vec_free(vec);
4834         isl_multi_aff_free(ma);
4835         return NULL;
4836 }
4837
4838 /* Scale the first elements of "pma" by the corresponding elements of "vec".
4839  */
4840 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_vec(
4841         __isl_take isl_pw_multi_aff *pma, __isl_take isl_vec *v)
4842 {
4843         int i;
4844
4845         pma = isl_pw_multi_aff_cow(pma);
4846         if (!pma || !v)
4847                 goto error;
4848
4849         for (i = 0; i < pma->n; ++i) {
4850                 pma->p[i].maff = isl_multi_aff_scale_vec(pma->p[i].maff,
4851                                                         isl_vec_copy(v));
4852                 if (!pma->p[i].maff)
4853                         goto error;
4854         }
4855
4856         isl_vec_free(v);
4857         return pma;
4858 error:
4859         isl_vec_free(v);
4860         isl_pw_multi_aff_free(pma);
4861         return NULL;
4862 }
4863
4864 /* This function is called for each entry of an isl_union_pw_multi_aff.
4865  * Replace the entry by the result of applying isl_pw_multi_aff_scale_vec
4866  * to the original entry with the isl_vec in "user" as extra argument.
4867  */
4868 static int union_pw_multi_aff_scale_vec_entry(void **entry, void *user)
4869 {
4870         isl_pw_multi_aff **pma = (isl_pw_multi_aff **) entry;
4871         isl_vec *v = user;
4872
4873         *pma = isl_pw_multi_aff_scale_vec(*pma, isl_vec_copy(v));
4874         if (!*pma)
4875                 return -1;
4876
4877         return 0;
4878 }
4879
4880 /* Scale the first elements of "upma" by the corresponding elements of "vec".
4881  */
4882 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_vec(
4883         __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_vec *v)
4884 {
4885         upma = isl_union_pw_multi_aff_cow(upma);
4886         if (!upma || !v)
4887                 goto error;
4888
4889         if (isl_hash_table_foreach(upma->dim->ctx, &upma->table,
4890                                    &union_pw_multi_aff_scale_vec_entry, v) < 0)
4891                 goto error;
4892
4893         isl_vec_free(v);
4894         return upma;
4895 error:
4896         isl_vec_free(v);
4897         isl_union_pw_multi_aff_free(upma);
4898         return NULL;
4899 }