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