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