1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 *
9 * Use of this software is governed by the MIT license
10 *
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
18 */
19
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
35
36 #undef EL_BASE
37 #define EL_BASE aff
38
39 #include <isl_list_templ.c>
40
41 #undef EL_BASE
42 #define EL_BASE pw_aff
43
44 #include <isl_list_templ.c>
45
46 #undef EL_BASE
47 #define EL_BASE pw_multi_aff
48
49 #include <isl_list_templ.c>
50
51 #undef EL_BASE
52 #define EL_BASE union_pw_aff
53
54 #include <isl_list_templ.c>
55
56 #undef EL_BASE
57 #define EL_BASE union_pw_multi_aff
58
59 #include <isl_list_templ.c>
60
isl_aff_alloc_vec(__isl_take isl_local_space * ls,__isl_take isl_vec * v)61 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
62 __isl_take isl_vec *v)
63 {
64 isl_aff *aff;
65
66 if (!ls || !v)
67 goto error;
68
69 aff = isl_calloc_type(v->ctx, struct isl_aff);
70 if (!aff)
71 goto error;
72
73 aff->ref = 1;
74 aff->ls = ls;
75 aff->v = v;
76
77 return aff;
78 error:
79 isl_local_space_free(ls);
80 isl_vec_free(v);
81 return NULL;
82 }
83
isl_aff_alloc(__isl_take isl_local_space * ls)84 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
85 {
86 isl_ctx *ctx;
87 isl_vec *v;
88 isl_size total;
89
90 if (!ls)
91 return NULL;
92
93 ctx = isl_local_space_get_ctx(ls);
94 if (!isl_local_space_divs_known(ls))
95 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
96 goto error);
97 if (!isl_local_space_is_set(ls))
98 isl_die(ctx, isl_error_invalid,
99 "domain of affine expression should be a set",
100 goto error);
101
102 total = isl_local_space_dim(ls, isl_dim_all);
103 if (total < 0)
104 goto error;
105 v = isl_vec_alloc(ctx, 1 + 1 + total);
106 return isl_aff_alloc_vec(ls, v);
107 error:
108 isl_local_space_free(ls);
109 return NULL;
110 }
111
isl_aff_copy(__isl_keep isl_aff * aff)112 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
113 {
114 if (!aff)
115 return NULL;
116
117 aff->ref++;
118 return aff;
119 }
120
isl_aff_dup(__isl_keep isl_aff * aff)121 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
122 {
123 if (!aff)
124 return NULL;
125
126 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
127 isl_vec_copy(aff->v));
128 }
129
isl_aff_cow(__isl_take isl_aff * aff)130 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
131 {
132 if (!aff)
133 return NULL;
134
135 if (aff->ref == 1)
136 return aff;
137 aff->ref--;
138 return isl_aff_dup(aff);
139 }
140
isl_aff_zero_on_domain(__isl_take isl_local_space * ls)141 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
142 {
143 isl_aff *aff;
144
145 aff = isl_aff_alloc(ls);
146 if (!aff)
147 return NULL;
148
149 isl_int_set_si(aff->v->el[0], 1);
150 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
151
152 return aff;
153 }
154
155 /* Return an affine expression that is equal to zero on domain space "space".
156 */
isl_aff_zero_on_domain_space(__isl_take isl_space * space)157 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
158 {
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
160 }
161
162 /* Return a piecewise affine expression defined on the specified domain
163 * that is equal to zero.
164 */
isl_pw_aff_zero_on_domain(__isl_take isl_local_space * ls)165 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
166 {
167 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
168 }
169
170 /* Change "aff" into a NaN.
171 *
172 * Note that this function gets called from isl_aff_nan_on_domain,
173 * so "aff" may not have been initialized yet.
174 */
isl_aff_set_nan(__isl_take isl_aff * aff)175 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
176 {
177 aff = isl_aff_cow(aff);
178 if (!aff)
179 return NULL;
180
181 aff->v = isl_vec_clr(aff->v);
182 if (!aff->v)
183 return isl_aff_free(aff);
184
185 return aff;
186 }
187
188 /* Return an affine expression defined on the specified domain
189 * that represents NaN.
190 */
isl_aff_nan_on_domain(__isl_take isl_local_space * ls)191 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
192 {
193 isl_aff *aff;
194
195 aff = isl_aff_alloc(ls);
196 return isl_aff_set_nan(aff);
197 }
198
199 /* Return an affine expression defined on the specified domain space
200 * that represents NaN.
201 */
isl_aff_nan_on_domain_space(__isl_take isl_space * space)202 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
203 {
204 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
205 }
206
207 /* Return a piecewise affine expression defined on the specified domain space
208 * that represents NaN.
209 */
isl_pw_aff_nan_on_domain_space(__isl_take isl_space * space)210 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
211 __isl_take isl_space *space)
212 {
213 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
214 }
215
216 /* Return a piecewise affine expression defined on the specified domain
217 * that represents NaN.
218 */
isl_pw_aff_nan_on_domain(__isl_take isl_local_space * ls)219 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
220 {
221 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
222 }
223
224 /* Return an affine expression that is equal to "val" on
225 * domain local space "ls".
226 */
isl_aff_val_on_domain(__isl_take isl_local_space * ls,__isl_take isl_val * val)227 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
228 __isl_take isl_val *val)
229 {
230 isl_aff *aff;
231
232 if (!ls || !val)
233 goto error;
234 if (!isl_val_is_rat(val))
235 isl_die(isl_val_get_ctx(val), isl_error_invalid,
236 "expecting rational value", goto error);
237
238 aff = isl_aff_alloc(isl_local_space_copy(ls));
239 if (!aff)
240 goto error;
241
242 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
243 isl_int_set(aff->v->el[1], val->n);
244 isl_int_set(aff->v->el[0], val->d);
245
246 isl_local_space_free(ls);
247 isl_val_free(val);
248 return aff;
249 error:
250 isl_local_space_free(ls);
251 isl_val_free(val);
252 return NULL;
253 }
254
255 /* Return an affine expression that is equal to "val" on domain space "space".
256 */
isl_aff_val_on_domain_space(__isl_take isl_space * space,__isl_take isl_val * val)257 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
258 __isl_take isl_val *val)
259 {
260 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
261 }
262
263 /* Return an affine expression that is equal to the specified dimension
264 * in "ls".
265 */
isl_aff_var_on_domain(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos)266 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
267 enum isl_dim_type type, unsigned pos)
268 {
269 isl_space *space;
270 isl_aff *aff;
271
272 if (!ls)
273 return NULL;
274
275 space = isl_local_space_get_space(ls);
276 if (!space)
277 goto error;
278 if (isl_space_is_map(space))
279 isl_die(isl_space_get_ctx(space), isl_error_invalid,
280 "expecting (parameter) set space", goto error);
281 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
282 goto error;
283
284 isl_space_free(space);
285 aff = isl_aff_alloc(ls);
286 if (!aff)
287 return NULL;
288
289 pos += isl_local_space_offset(aff->ls, type);
290
291 isl_int_set_si(aff->v->el[0], 1);
292 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
293 isl_int_set_si(aff->v->el[1 + pos], 1);
294
295 return aff;
296 error:
297 isl_local_space_free(ls);
298 isl_space_free(space);
299 return NULL;
300 }
301
302 /* Return a piecewise affine expression that is equal to
303 * the specified dimension in "ls".
304 */
isl_pw_aff_var_on_domain(__isl_take isl_local_space * ls,enum isl_dim_type type,unsigned pos)305 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
306 enum isl_dim_type type, unsigned pos)
307 {
308 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
309 }
310
311 /* Return an affine expression that is equal to the parameter
312 * in the domain space "space" with identifier "id".
313 */
isl_aff_param_on_domain_space_id(__isl_take isl_space * space,__isl_take isl_id * id)314 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
315 __isl_take isl_space *space, __isl_take isl_id *id)
316 {
317 int pos;
318 isl_local_space *ls;
319
320 if (!space || !id)
321 goto error;
322 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
323 if (pos < 0)
324 isl_die(isl_space_get_ctx(space), isl_error_invalid,
325 "parameter not found in space", goto error);
326 isl_id_free(id);
327 ls = isl_local_space_from_space(space);
328 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
329 error:
330 isl_space_free(space);
331 isl_id_free(id);
332 return NULL;
333 }
334
isl_aff_free(__isl_take isl_aff * aff)335 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
336 {
337 if (!aff)
338 return NULL;
339
340 if (--aff->ref > 0)
341 return NULL;
342
343 isl_local_space_free(aff->ls);
344 isl_vec_free(aff->v);
345
346 free(aff);
347
348 return NULL;
349 }
350
isl_aff_get_ctx(__isl_keep isl_aff * aff)351 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
352 {
353 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
354 }
355
356 /* Return a hash value that digests "aff".
357 */
isl_aff_get_hash(__isl_keep isl_aff * aff)358 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
359 {
360 uint32_t hash, ls_hash, v_hash;
361
362 if (!aff)
363 return 0;
364
365 hash = isl_hash_init();
366 ls_hash = isl_local_space_get_hash(aff->ls);
367 isl_hash_hash(hash, ls_hash);
368 v_hash = isl_vec_get_hash(aff->v);
369 isl_hash_hash(hash, v_hash);
370
371 return hash;
372 }
373
374 /* Return the domain local space of "aff".
375 */
isl_aff_peek_domain_local_space(__isl_keep isl_aff * aff)376 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
377 __isl_keep isl_aff *aff)
378 {
379 return aff ? aff->ls : NULL;
380 }
381
382 /* Return the number of variables of the given type in the domain of "aff".
383 */
isl_aff_domain_dim(__isl_keep isl_aff * aff,enum isl_dim_type type)384 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
385 {
386 isl_local_space *ls;
387
388 ls = isl_aff_peek_domain_local_space(aff);
389 return isl_local_space_dim(ls, type);
390 }
391
392 /* Externally, an isl_aff has a map space, but internally, the
393 * ls field corresponds to the domain of that space.
394 */
isl_aff_dim(__isl_keep isl_aff * aff,enum isl_dim_type type)395 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
396 {
397 if (!aff)
398 return isl_size_error;
399 if (type == isl_dim_out)
400 return 1;
401 if (type == isl_dim_in)
402 type = isl_dim_set;
403 return isl_aff_domain_dim(aff, type);
404 }
405
406 /* Return the offset of the first coefficient of type "type" in
407 * the domain of "aff".
408 */
isl_aff_domain_offset(__isl_keep isl_aff * aff,enum isl_dim_type type)409 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
410 {
411 isl_local_space *ls;
412
413 ls = isl_aff_peek_domain_local_space(aff);
414 return isl_local_space_offset(ls, type);
415 }
416
417 /* Return the position of the dimension of the given type and name
418 * in "aff".
419 * Return -1 if no such dimension can be found.
420 */
isl_aff_find_dim_by_name(__isl_keep isl_aff * aff,enum isl_dim_type type,const char * name)421 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
422 const char *name)
423 {
424 if (!aff)
425 return -1;
426 if (type == isl_dim_out)
427 return -1;
428 if (type == isl_dim_in)
429 type = isl_dim_set;
430 return isl_local_space_find_dim_by_name(aff->ls, type, name);
431 }
432
433 /* Return the domain space of "aff".
434 */
isl_aff_peek_domain_space(__isl_keep isl_aff * aff)435 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
436 {
437 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
438 }
439
isl_aff_get_domain_space(__isl_keep isl_aff * aff)440 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
441 {
442 return isl_space_copy(isl_aff_peek_domain_space(aff));
443 }
444
isl_aff_get_space(__isl_keep isl_aff * aff)445 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
446 {
447 isl_space *space;
448 if (!aff)
449 return NULL;
450 space = isl_local_space_get_space(aff->ls);
451 space = isl_space_from_domain(space);
452 space = isl_space_add_dims(space, isl_dim_out, 1);
453 return space;
454 }
455
456 /* Return a copy of the domain space of "aff".
457 */
isl_aff_get_domain_local_space(__isl_keep isl_aff * aff)458 __isl_give isl_local_space *isl_aff_get_domain_local_space(
459 __isl_keep isl_aff *aff)
460 {
461 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
462 }
463
isl_aff_get_local_space(__isl_keep isl_aff * aff)464 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
465 {
466 isl_local_space *ls;
467 if (!aff)
468 return NULL;
469 ls = isl_local_space_copy(aff->ls);
470 ls = isl_local_space_from_domain(ls);
471 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
472 return ls;
473 }
474
475 /* Return the local space of the domain of "aff".
476 * This may be either a copy or the local space itself
477 * if there is only one reference to "aff".
478 * This allows the local space to be modified inplace
479 * if both the expression and its local space have only a single reference.
480 * The caller is not allowed to modify "aff" between this call and
481 * a subsequent call to isl_aff_restore_domain_local_space.
482 * The only exception is that isl_aff_free can be called instead.
483 */
isl_aff_take_domain_local_space(__isl_keep isl_aff * aff)484 __isl_give isl_local_space *isl_aff_take_domain_local_space(
485 __isl_keep isl_aff *aff)
486 {
487 isl_local_space *ls;
488
489 if (!aff)
490 return NULL;
491 if (aff->ref != 1)
492 return isl_aff_get_domain_local_space(aff);
493 ls = aff->ls;
494 aff->ls = NULL;
495 return ls;
496 }
497
498 /* Set the local space of the domain of "aff" to "ls",
499 * where the local space of "aff" may be missing
500 * due to a preceding call to isl_aff_take_domain_local_space.
501 * However, in this case, "aff" only has a single reference and
502 * then the call to isl_aff_cow has no effect.
503 */
isl_aff_restore_domain_local_space(__isl_keep isl_aff * aff,__isl_take isl_local_space * ls)504 __isl_give isl_aff *isl_aff_restore_domain_local_space(
505 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
506 {
507 if (!aff || !ls)
508 goto error;
509
510 if (aff->ls == ls) {
511 isl_local_space_free(ls);
512 return aff;
513 }
514
515 aff = isl_aff_cow(aff);
516 if (!aff)
517 goto error;
518 isl_local_space_free(aff->ls);
519 aff->ls = ls;
520
521 return aff;
522 error:
523 isl_aff_free(aff);
524 isl_local_space_free(ls);
525 return NULL;
526 }
527
528 /* Externally, an isl_aff has a map space, but internally, the
529 * ls field corresponds to the domain of that space.
530 */
isl_aff_get_dim_name(__isl_keep isl_aff * aff,enum isl_dim_type type,unsigned pos)531 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
532 enum isl_dim_type type, unsigned pos)
533 {
534 if (!aff)
535 return NULL;
536 if (type == isl_dim_out)
537 return NULL;
538 if (type == isl_dim_in)
539 type = isl_dim_set;
540 return isl_local_space_get_dim_name(aff->ls, type, pos);
541 }
542
isl_aff_reset_domain_space(__isl_take isl_aff * aff,__isl_take isl_space * space)543 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
544 __isl_take isl_space *space)
545 {
546 aff = isl_aff_cow(aff);
547 if (!aff || !space)
548 goto error;
549
550 aff->ls = isl_local_space_reset_space(aff->ls, space);
551 if (!aff->ls)
552 return isl_aff_free(aff);
553
554 return aff;
555 error:
556 isl_aff_free(aff);
557 isl_space_free(space);
558 return NULL;
559 }
560
561 /* Reset the space of "aff". This function is called from isl_pw_templ.c
562 * and doesn't know if the space of an element object is represented
563 * directly or through its domain. It therefore passes along both.
564 */
isl_aff_reset_space_and_domain(__isl_take isl_aff * aff,__isl_take isl_space * space,__isl_take isl_space * domain)565 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
566 __isl_take isl_space *space, __isl_take isl_space *domain)
567 {
568 isl_space_free(space);
569 return isl_aff_reset_domain_space(aff, domain);
570 }
571
572 /* Reorder the coefficients of the affine expression based
573 * on the given reordering.
574 * The reordering r is assumed to have been extended with the local
575 * variables.
576 */
vec_reorder(__isl_take isl_vec * vec,__isl_take isl_reordering * r,int n_div)577 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
578 __isl_take isl_reordering *r, int n_div)
579 {
580 isl_space *space;
581 isl_vec *res;
582 isl_size dim;
583 int i;
584
585 if (!vec || !r)
586 goto error;
587
588 space = isl_reordering_peek_space(r);
589 dim = isl_space_dim(space, isl_dim_all);
590 if (dim < 0)
591 goto error;
592 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
593 if (!res)
594 goto error;
595 isl_seq_cpy(res->el, vec->el, 2);
596 isl_seq_clr(res->el + 2, res->size - 2);
597 for (i = 0; i < r->len; ++i)
598 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
599
600 isl_reordering_free(r);
601 isl_vec_free(vec);
602 return res;
603 error:
604 isl_vec_free(vec);
605 isl_reordering_free(r);
606 return NULL;
607 }
608
609 /* Reorder the dimensions of the domain of "aff" according
610 * to the given reordering.
611 */
isl_aff_realign_domain(__isl_take isl_aff * aff,__isl_take isl_reordering * r)612 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
613 __isl_take isl_reordering *r)
614 {
615 aff = isl_aff_cow(aff);
616 if (!aff)
617 goto error;
618
619 r = isl_reordering_extend(r, aff->ls->div->n_row);
620 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
621 aff->ls->div->n_row);
622 aff->ls = isl_local_space_realign(aff->ls, r);
623
624 if (!aff->v || !aff->ls)
625 return isl_aff_free(aff);
626
627 return aff;
628 error:
629 isl_aff_free(aff);
630 isl_reordering_free(r);
631 return NULL;
632 }
633
isl_aff_align_params(__isl_take isl_aff * aff,__isl_take isl_space * model)634 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
635 __isl_take isl_space *model)
636 {
637 isl_bool equal_params;
638
639 if (!aff || !model)
640 goto error;
641
642 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
643 if (equal_params < 0)
644 goto error;
645 if (!equal_params) {
646 isl_reordering *exp;
647
648 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
649 exp = isl_reordering_extend_space(exp,
650 isl_aff_get_domain_space(aff));
651 aff = isl_aff_realign_domain(aff, exp);
652 }
653
654 isl_space_free(model);
655 return aff;
656 error:
657 isl_space_free(model);
658 isl_aff_free(aff);
659 return NULL;
660 }
661
662 #undef TYPE
663 #define TYPE isl_aff
664 #include "isl_unbind_params_templ.c"
665
666 /* Is "aff" obviously equal to zero?
667 *
668 * If the denominator is zero, then "aff" is not equal to zero.
669 */
isl_aff_plain_is_zero(__isl_keep isl_aff * aff)670 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
671 {
672 int pos;
673
674 if (!aff)
675 return isl_bool_error;
676
677 if (isl_int_is_zero(aff->v->el[0]))
678 return isl_bool_false;
679 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
680 return isl_bool_ok(pos < 0);
681 }
682
683 /* Does "aff" represent NaN?
684 */
isl_aff_is_nan(__isl_keep isl_aff * aff)685 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
686 {
687 if (!aff)
688 return isl_bool_error;
689
690 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
691 }
692
693 /* Are "aff1" and "aff2" obviously equal?
694 *
695 * NaN is not equal to anything, not even to another NaN.
696 */
isl_aff_plain_is_equal(__isl_keep isl_aff * aff1,__isl_keep isl_aff * aff2)697 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
698 __isl_keep isl_aff *aff2)
699 {
700 isl_bool equal;
701
702 if (!aff1 || !aff2)
703 return isl_bool_error;
704
705 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
706 return isl_bool_false;
707
708 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
709 if (equal < 0 || !equal)
710 return equal;
711
712 return isl_vec_is_equal(aff1->v, aff2->v);
713 }
714
715 /* Return the common denominator of "aff" in "v".
716 *
717 * We cannot return anything meaningful in case of a NaN.
718 */
isl_aff_get_denominator(__isl_keep isl_aff * aff,isl_int * v)719 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
720 {
721 if (!aff)
722 return isl_stat_error;
723 if (isl_aff_is_nan(aff))
724 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
725 "cannot get denominator of NaN", return isl_stat_error);
726 isl_int_set(*v, aff->v->el[0]);
727 return isl_stat_ok;
728 }
729
730 /* Return the common denominator of "aff".
731 */
isl_aff_get_denominator_val(__isl_keep isl_aff * aff)732 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
733 {
734 isl_ctx *ctx;
735
736 if (!aff)
737 return NULL;
738
739 ctx = isl_aff_get_ctx(aff);
740 if (isl_aff_is_nan(aff))
741 return isl_val_nan(ctx);
742 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
743 }
744
745 /* Return the constant term of "aff".
746 */
isl_aff_get_constant_val(__isl_keep isl_aff * aff)747 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
748 {
749 isl_ctx *ctx;
750 isl_val *v;
751
752 if (!aff)
753 return NULL;
754
755 ctx = isl_aff_get_ctx(aff);
756 if (isl_aff_is_nan(aff))
757 return isl_val_nan(ctx);
758 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
759 return isl_val_normalize(v);
760 }
761
762 /* Return the coefficient of the variable of type "type" at position "pos"
763 * of "aff".
764 */
isl_aff_get_coefficient_val(__isl_keep isl_aff * aff,enum isl_dim_type type,int pos)765 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
766 enum isl_dim_type type, int pos)
767 {
768 isl_ctx *ctx;
769 isl_val *v;
770
771 if (!aff)
772 return NULL;
773
774 ctx = isl_aff_get_ctx(aff);
775 if (type == isl_dim_out)
776 isl_die(ctx, isl_error_invalid,
777 "output/set dimension does not have a coefficient",
778 return NULL);
779 if (type == isl_dim_in)
780 type = isl_dim_set;
781
782 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
783 return NULL;
784
785 if (isl_aff_is_nan(aff))
786 return isl_val_nan(ctx);
787 pos += isl_local_space_offset(aff->ls, type);
788 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
789 return isl_val_normalize(v);
790 }
791
792 /* Return the sign of the coefficient of the variable of type "type"
793 * at position "pos" of "aff".
794 */
isl_aff_coefficient_sgn(__isl_keep isl_aff * aff,enum isl_dim_type type,int pos)795 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
796 int pos)
797 {
798 isl_ctx *ctx;
799
800 if (!aff)
801 return 0;
802
803 ctx = isl_aff_get_ctx(aff);
804 if (type == isl_dim_out)
805 isl_die(ctx, isl_error_invalid,
806 "output/set dimension does not have a coefficient",
807 return 0);
808 if (type == isl_dim_in)
809 type = isl_dim_set;
810
811 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
812 return 0;
813
814 pos += isl_local_space_offset(aff->ls, type);
815 return isl_int_sgn(aff->v->el[1 + pos]);
816 }
817
818 /* Replace the numerator of the constant term of "aff" by "v".
819 *
820 * A NaN is unaffected by this operation.
821 */
isl_aff_set_constant(__isl_take isl_aff * aff,isl_int v)822 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
823 {
824 if (!aff)
825 return NULL;
826 if (isl_aff_is_nan(aff))
827 return aff;
828 aff = isl_aff_cow(aff);
829 if (!aff)
830 return NULL;
831
832 aff->v = isl_vec_cow(aff->v);
833 if (!aff->v)
834 return isl_aff_free(aff);
835
836 isl_int_set(aff->v->el[1], v);
837
838 return aff;
839 }
840
841 /* Replace the constant term of "aff" by "v".
842 *
843 * A NaN is unaffected by this operation.
844 */
isl_aff_set_constant_val(__isl_take isl_aff * aff,__isl_take isl_val * v)845 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
846 __isl_take isl_val *v)
847 {
848 if (!aff || !v)
849 goto error;
850
851 if (isl_aff_is_nan(aff)) {
852 isl_val_free(v);
853 return aff;
854 }
855
856 if (!isl_val_is_rat(v))
857 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
858 "expecting rational value", goto error);
859
860 if (isl_int_eq(aff->v->el[1], v->n) &&
861 isl_int_eq(aff->v->el[0], v->d)) {
862 isl_val_free(v);
863 return aff;
864 }
865
866 aff = isl_aff_cow(aff);
867 if (!aff)
868 goto error;
869 aff->v = isl_vec_cow(aff->v);
870 if (!aff->v)
871 goto error;
872
873 if (isl_int_eq(aff->v->el[0], v->d)) {
874 isl_int_set(aff->v->el[1], v->n);
875 } else if (isl_int_is_one(v->d)) {
876 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
877 } else {
878 isl_seq_scale(aff->v->el + 1,
879 aff->v->el + 1, v->d, aff->v->size - 1);
880 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
881 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
882 aff->v = isl_vec_normalize(aff->v);
883 if (!aff->v)
884 goto error;
885 }
886
887 isl_val_free(v);
888 return aff;
889 error:
890 isl_aff_free(aff);
891 isl_val_free(v);
892 return NULL;
893 }
894
895 /* Add "v" to the constant term of "aff".
896 *
897 * A NaN is unaffected by this operation.
898 */
isl_aff_add_constant(__isl_take isl_aff * aff,isl_int v)899 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
900 {
901 if (isl_int_is_zero(v))
902 return aff;
903
904 if (!aff)
905 return NULL;
906 if (isl_aff_is_nan(aff))
907 return aff;
908 aff = isl_aff_cow(aff);
909 if (!aff)
910 return NULL;
911
912 aff->v = isl_vec_cow(aff->v);
913 if (!aff->v)
914 return isl_aff_free(aff);
915
916 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
917
918 return aff;
919 }
920
921 /* Add "v" to the constant term of "aff",
922 * in case "aff" is a rational expression.
923 */
isl_aff_add_rat_constant_val(__isl_take isl_aff * aff,__isl_take isl_val * v)924 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
925 __isl_take isl_val *v)
926 {
927 aff = isl_aff_cow(aff);
928 if (!aff)
929 goto error;
930
931 aff->v = isl_vec_cow(aff->v);
932 if (!aff->v)
933 goto error;
934
935 if (isl_int_is_one(v->d)) {
936 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
937 } else if (isl_int_eq(aff->v->el[0], v->d)) {
938 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
939 aff->v = isl_vec_normalize(aff->v);
940 if (!aff->v)
941 goto error;
942 } else {
943 isl_seq_scale(aff->v->el + 1,
944 aff->v->el + 1, v->d, aff->v->size - 1);
945 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
946 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
947 aff->v = isl_vec_normalize(aff->v);
948 if (!aff->v)
949 goto error;
950 }
951
952 isl_val_free(v);
953 return aff;
954 error:
955 isl_aff_free(aff);
956 isl_val_free(v);
957 return NULL;
958 }
959
960 /* Return the first argument and free the second.
961 */
pick_free(__isl_take isl_aff * aff,__isl_take isl_val * v)962 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
963 __isl_take isl_val *v)
964 {
965 isl_val_free(v);
966 return aff;
967 }
968
969 /* Replace the first argument by NaN and free the second argument.
970 */
set_nan_free_val(__isl_take isl_aff * aff,__isl_take isl_val * v)971 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
972 __isl_take isl_val *v)
973 {
974 isl_val_free(v);
975 return isl_aff_set_nan(aff);
976 }
977
978 /* Add "v" to the constant term of "aff".
979 *
980 * A NaN is unaffected by this operation.
981 * Conversely, adding a NaN turns "aff" into a NaN.
982 */
isl_aff_add_constant_val(__isl_take isl_aff * aff,__isl_take isl_val * v)983 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
984 __isl_take isl_val *v)
985 {
986 isl_bool is_nan, is_zero, is_rat;
987
988 is_nan = isl_aff_is_nan(aff);
989 is_zero = isl_val_is_zero(v);
990 if (is_nan < 0 || is_zero < 0)
991 goto error;
992 if (is_nan || is_zero)
993 return pick_free(aff, v);
994
995 is_nan = isl_val_is_nan(v);
996 is_rat = isl_val_is_rat(v);
997 if (is_nan < 0 || is_rat < 0)
998 goto error;
999 if (is_nan)
1000 return set_nan_free_val(aff, v);
1001 if (!is_rat)
1002 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1003 "expecting rational value or NaN", goto error);
1004
1005 return isl_aff_add_rat_constant_val(aff, v);
1006 error:
1007 isl_aff_free(aff);
1008 isl_val_free(v);
1009 return NULL;
1010 }
1011
isl_aff_add_constant_si(__isl_take isl_aff * aff,int v)1012 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1013 {
1014 isl_int t;
1015
1016 isl_int_init(t);
1017 isl_int_set_si(t, v);
1018 aff = isl_aff_add_constant(aff, t);
1019 isl_int_clear(t);
1020
1021 return aff;
1022 }
1023
1024 /* Add "v" to the numerator of the constant term of "aff".
1025 *
1026 * A NaN is unaffected by this operation.
1027 */
isl_aff_add_constant_num(__isl_take isl_aff * aff,isl_int v)1028 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1029 {
1030 if (isl_int_is_zero(v))
1031 return aff;
1032
1033 if (!aff)
1034 return NULL;
1035 if (isl_aff_is_nan(aff))
1036 return aff;
1037 aff = isl_aff_cow(aff);
1038 if (!aff)
1039 return NULL;
1040
1041 aff->v = isl_vec_cow(aff->v);
1042 if (!aff->v)
1043 return isl_aff_free(aff);
1044
1045 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1046
1047 return aff;
1048 }
1049
1050 /* Add "v" to the numerator of the constant term of "aff".
1051 *
1052 * A NaN is unaffected by this operation.
1053 */
isl_aff_add_constant_num_si(__isl_take isl_aff * aff,int v)1054 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1055 {
1056 isl_int t;
1057
1058 if (v == 0)
1059 return aff;
1060
1061 isl_int_init(t);
1062 isl_int_set_si(t, v);
1063 aff = isl_aff_add_constant_num(aff, t);
1064 isl_int_clear(t);
1065
1066 return aff;
1067 }
1068
1069 /* Replace the numerator of the constant term of "aff" by "v".
1070 *
1071 * A NaN is unaffected by this operation.
1072 */
isl_aff_set_constant_si(__isl_take isl_aff * aff,int v)1073 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1074 {
1075 if (!aff)
1076 return NULL;
1077 if (isl_aff_is_nan(aff))
1078 return aff;
1079 aff = isl_aff_cow(aff);
1080 if (!aff)
1081 return NULL;
1082
1083 aff->v = isl_vec_cow(aff->v);
1084 if (!aff->v)
1085 return isl_aff_free(aff);
1086
1087 isl_int_set_si(aff->v->el[1], v);
1088
1089 return aff;
1090 }
1091
1092 /* Replace the numerator of the coefficient of the variable of type "type"
1093 * at position "pos" of "aff" by "v".
1094 *
1095 * A NaN is unaffected by this operation.
1096 */
isl_aff_set_coefficient(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,isl_int v)1097 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1098 enum isl_dim_type type, int pos, isl_int v)
1099 {
1100 if (!aff)
1101 return NULL;
1102
1103 if (type == isl_dim_out)
1104 isl_die(aff->v->ctx, isl_error_invalid,
1105 "output/set dimension does not have a coefficient",
1106 return isl_aff_free(aff));
1107 if (type == isl_dim_in)
1108 type = isl_dim_set;
1109
1110 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1111 return isl_aff_free(aff);
1112
1113 if (isl_aff_is_nan(aff))
1114 return aff;
1115 aff = isl_aff_cow(aff);
1116 if (!aff)
1117 return NULL;
1118
1119 aff->v = isl_vec_cow(aff->v);
1120 if (!aff->v)
1121 return isl_aff_free(aff);
1122
1123 pos += isl_local_space_offset(aff->ls, type);
1124 isl_int_set(aff->v->el[1 + pos], v);
1125
1126 return aff;
1127 }
1128
1129 /* Replace the numerator of the coefficient of the variable of type "type"
1130 * at position "pos" of "aff" by "v".
1131 *
1132 * A NaN is unaffected by this operation.
1133 */
isl_aff_set_coefficient_si(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,int v)1134 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1135 enum isl_dim_type type, int pos, int v)
1136 {
1137 if (!aff)
1138 return NULL;
1139
1140 if (type == isl_dim_out)
1141 isl_die(aff->v->ctx, isl_error_invalid,
1142 "output/set dimension does not have a coefficient",
1143 return isl_aff_free(aff));
1144 if (type == isl_dim_in)
1145 type = isl_dim_set;
1146
1147 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1148 return isl_aff_free(aff);
1149
1150 if (isl_aff_is_nan(aff))
1151 return aff;
1152 pos += isl_local_space_offset(aff->ls, type);
1153 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1154 return aff;
1155
1156 aff = isl_aff_cow(aff);
1157 if (!aff)
1158 return NULL;
1159
1160 aff->v = isl_vec_cow(aff->v);
1161 if (!aff->v)
1162 return isl_aff_free(aff);
1163
1164 isl_int_set_si(aff->v->el[1 + pos], v);
1165
1166 return aff;
1167 }
1168
1169 /* Replace the coefficient of the variable of type "type" at position "pos"
1170 * of "aff" by "v".
1171 *
1172 * A NaN is unaffected by this operation.
1173 */
isl_aff_set_coefficient_val(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,__isl_take isl_val * v)1174 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1175 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1176 {
1177 if (!aff || !v)
1178 goto error;
1179
1180 if (type == isl_dim_out)
1181 isl_die(aff->v->ctx, isl_error_invalid,
1182 "output/set dimension does not have a coefficient",
1183 goto error);
1184 if (type == isl_dim_in)
1185 type = isl_dim_set;
1186
1187 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1188 return isl_aff_free(aff);
1189
1190 if (isl_aff_is_nan(aff)) {
1191 isl_val_free(v);
1192 return aff;
1193 }
1194 if (!isl_val_is_rat(v))
1195 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1196 "expecting rational value", goto error);
1197
1198 pos += isl_local_space_offset(aff->ls, type);
1199 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1200 isl_int_eq(aff->v->el[0], v->d)) {
1201 isl_val_free(v);
1202 return aff;
1203 }
1204
1205 aff = isl_aff_cow(aff);
1206 if (!aff)
1207 goto error;
1208 aff->v = isl_vec_cow(aff->v);
1209 if (!aff->v)
1210 goto error;
1211
1212 if (isl_int_eq(aff->v->el[0], v->d)) {
1213 isl_int_set(aff->v->el[1 + pos], v->n);
1214 } else if (isl_int_is_one(v->d)) {
1215 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1216 } else {
1217 isl_seq_scale(aff->v->el + 1,
1218 aff->v->el + 1, v->d, aff->v->size - 1);
1219 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1220 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1221 aff->v = isl_vec_normalize(aff->v);
1222 if (!aff->v)
1223 goto error;
1224 }
1225
1226 isl_val_free(v);
1227 return aff;
1228 error:
1229 isl_aff_free(aff);
1230 isl_val_free(v);
1231 return NULL;
1232 }
1233
1234 /* Add "v" to the coefficient of the variable of type "type"
1235 * at position "pos" of "aff".
1236 *
1237 * A NaN is unaffected by this operation.
1238 */
isl_aff_add_coefficient(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,isl_int v)1239 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1240 enum isl_dim_type type, int pos, isl_int v)
1241 {
1242 if (!aff)
1243 return NULL;
1244
1245 if (type == isl_dim_out)
1246 isl_die(aff->v->ctx, isl_error_invalid,
1247 "output/set dimension does not have a coefficient",
1248 return isl_aff_free(aff));
1249 if (type == isl_dim_in)
1250 type = isl_dim_set;
1251
1252 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1253 return isl_aff_free(aff);
1254
1255 if (isl_aff_is_nan(aff))
1256 return aff;
1257 aff = isl_aff_cow(aff);
1258 if (!aff)
1259 return NULL;
1260
1261 aff->v = isl_vec_cow(aff->v);
1262 if (!aff->v)
1263 return isl_aff_free(aff);
1264
1265 pos += isl_local_space_offset(aff->ls, type);
1266 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1267
1268 return aff;
1269 }
1270
1271 /* Add "v" to the coefficient of the variable of type "type"
1272 * at position "pos" of "aff".
1273 *
1274 * A NaN is unaffected by this operation.
1275 */
isl_aff_add_coefficient_val(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,__isl_take isl_val * v)1276 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1277 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1278 {
1279 if (!aff || !v)
1280 goto error;
1281
1282 if (isl_val_is_zero(v)) {
1283 isl_val_free(v);
1284 return aff;
1285 }
1286
1287 if (type == isl_dim_out)
1288 isl_die(aff->v->ctx, isl_error_invalid,
1289 "output/set dimension does not have a coefficient",
1290 goto error);
1291 if (type == isl_dim_in)
1292 type = isl_dim_set;
1293
1294 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1295 goto error;
1296
1297 if (isl_aff_is_nan(aff)) {
1298 isl_val_free(v);
1299 return aff;
1300 }
1301 if (!isl_val_is_rat(v))
1302 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1303 "expecting rational value", goto error);
1304
1305 aff = isl_aff_cow(aff);
1306 if (!aff)
1307 goto error;
1308
1309 aff->v = isl_vec_cow(aff->v);
1310 if (!aff->v)
1311 goto error;
1312
1313 pos += isl_local_space_offset(aff->ls, type);
1314 if (isl_int_is_one(v->d)) {
1315 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1316 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1317 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1318 aff->v = isl_vec_normalize(aff->v);
1319 if (!aff->v)
1320 goto error;
1321 } else {
1322 isl_seq_scale(aff->v->el + 1,
1323 aff->v->el + 1, v->d, aff->v->size - 1);
1324 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1325 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1326 aff->v = isl_vec_normalize(aff->v);
1327 if (!aff->v)
1328 goto error;
1329 }
1330
1331 isl_val_free(v);
1332 return aff;
1333 error:
1334 isl_aff_free(aff);
1335 isl_val_free(v);
1336 return NULL;
1337 }
1338
isl_aff_add_coefficient_si(__isl_take isl_aff * aff,enum isl_dim_type type,int pos,int v)1339 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1340 enum isl_dim_type type, int pos, int v)
1341 {
1342 isl_int t;
1343
1344 isl_int_init(t);
1345 isl_int_set_si(t, v);
1346 aff = isl_aff_add_coefficient(aff, type, pos, t);
1347 isl_int_clear(t);
1348
1349 return aff;
1350 }
1351
isl_aff_get_div(__isl_keep isl_aff * aff,int pos)1352 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1353 {
1354 if (!aff)
1355 return NULL;
1356
1357 return isl_local_space_get_div(aff->ls, pos);
1358 }
1359
1360 /* Return the negation of "aff".
1361 *
1362 * As a special case, -NaN = NaN.
1363 */
isl_aff_neg(__isl_take isl_aff * aff)1364 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1365 {
1366 if (!aff)
1367 return NULL;
1368 if (isl_aff_is_nan(aff))
1369 return aff;
1370 aff = isl_aff_cow(aff);
1371 if (!aff)
1372 return NULL;
1373 aff->v = isl_vec_cow(aff->v);
1374 if (!aff->v)
1375 return isl_aff_free(aff);
1376
1377 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1378
1379 return aff;
1380 }
1381
1382 /* Remove divs from the local space that do not appear in the affine
1383 * expression.
1384 * We currently only remove divs at the end.
1385 * Some intermediate divs may also not appear directly in the affine
1386 * expression, but we would also need to check that no other divs are
1387 * defined in terms of them.
1388 */
isl_aff_remove_unused_divs(__isl_take isl_aff * aff)1389 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1390 {
1391 int pos;
1392 isl_size off;
1393 isl_size n;
1394
1395 n = isl_aff_domain_dim(aff, isl_dim_div);
1396 off = isl_aff_domain_offset(aff, isl_dim_div);
1397 if (n < 0 || off < 0)
1398 return isl_aff_free(aff);
1399
1400 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1401 if (pos == n)
1402 return aff;
1403
1404 aff = isl_aff_cow(aff);
1405 if (!aff)
1406 return NULL;
1407
1408 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1409 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1410 if (!aff->ls || !aff->v)
1411 return isl_aff_free(aff);
1412
1413 return aff;
1414 }
1415
1416 /* Look for any divs in the aff->ls with a denominator equal to one
1417 * and plug them into the affine expression and any subsequent divs
1418 * that may reference the div.
1419 */
plug_in_integral_divs(__isl_take isl_aff * aff)1420 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1421 {
1422 int i;
1423 isl_size n;
1424 int len;
1425 isl_int v;
1426 isl_vec *vec;
1427 isl_local_space *ls;
1428 isl_size off;
1429
1430 n = isl_aff_domain_dim(aff, isl_dim_div);
1431 off = isl_aff_domain_offset(aff, isl_dim_div);
1432 if (n < 0 || off < 0)
1433 return isl_aff_free(aff);
1434 len = aff->v->size;
1435 for (i = 0; i < n; ++i) {
1436 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1437 continue;
1438 ls = isl_local_space_copy(aff->ls);
1439 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1440 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1441 vec = isl_vec_copy(aff->v);
1442 vec = isl_vec_cow(vec);
1443 if (!ls || !vec)
1444 goto error;
1445
1446 isl_int_init(v);
1447
1448 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1449 len, len, v);
1450
1451 isl_int_clear(v);
1452
1453 isl_vec_free(aff->v);
1454 aff->v = vec;
1455 isl_local_space_free(aff->ls);
1456 aff->ls = ls;
1457 }
1458
1459 return aff;
1460 error:
1461 isl_vec_free(vec);
1462 isl_local_space_free(ls);
1463 return isl_aff_free(aff);
1464 }
1465
1466 /* Look for any divs j that appear with a unit coefficient inside
1467 * the definitions of other divs i and plug them into the definitions
1468 * of the divs i.
1469 *
1470 * In particular, an expression of the form
1471 *
1472 * floor((f(..) + floor(g(..)/n))/m)
1473 *
1474 * is simplified to
1475 *
1476 * floor((n * f(..) + g(..))/(n * m))
1477 *
1478 * This simplification is correct because we can move the expression
1479 * f(..) into the inner floor in the original expression to obtain
1480 *
1481 * floor(floor((n * f(..) + g(..))/n)/m)
1482 *
1483 * from which we can derive the simplified expression.
1484 */
plug_in_unit_divs(__isl_take isl_aff * aff)1485 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1486 {
1487 int i, j;
1488 isl_size n;
1489 isl_size off;
1490
1491 n = isl_aff_domain_dim(aff, isl_dim_div);
1492 off = isl_aff_domain_offset(aff, isl_dim_div);
1493 if (n < 0 || off < 0)
1494 return isl_aff_free(aff);
1495 for (i = 1; i < n; ++i) {
1496 for (j = 0; j < i; ++j) {
1497 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1498 continue;
1499 aff->ls = isl_local_space_substitute_seq(aff->ls,
1500 isl_dim_div, j, aff->ls->div->row[j],
1501 aff->v->size, i, 1);
1502 if (!aff->ls)
1503 return isl_aff_free(aff);
1504 }
1505 }
1506
1507 return aff;
1508 }
1509
1510 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1511 *
1512 * Even though this function is only called on isl_affs with a single
1513 * reference, we are careful to only change aff->v and aff->ls together.
1514 */
swap_div(__isl_take isl_aff * aff,int a,int b)1515 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1516 {
1517 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1518 isl_local_space *ls;
1519 isl_vec *v;
1520
1521 if (off < 0)
1522 return isl_aff_free(aff);
1523
1524 ls = isl_local_space_copy(aff->ls);
1525 ls = isl_local_space_swap_div(ls, a, b);
1526 v = isl_vec_copy(aff->v);
1527 v = isl_vec_cow(v);
1528 if (!ls || !v)
1529 goto error;
1530
1531 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1532 isl_vec_free(aff->v);
1533 aff->v = v;
1534 isl_local_space_free(aff->ls);
1535 aff->ls = ls;
1536
1537 return aff;
1538 error:
1539 isl_vec_free(v);
1540 isl_local_space_free(ls);
1541 return isl_aff_free(aff);
1542 }
1543
1544 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1545 *
1546 * We currently do not actually remove div "b", but simply add its
1547 * coefficient to that of "a" and then zero it out.
1548 */
merge_divs(__isl_take isl_aff * aff,int a,int b)1549 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1550 {
1551 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1552
1553 if (off < 0)
1554 return isl_aff_free(aff);
1555
1556 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1557 return aff;
1558
1559 aff->v = isl_vec_cow(aff->v);
1560 if (!aff->v)
1561 return isl_aff_free(aff);
1562
1563 isl_int_add(aff->v->el[1 + off + a],
1564 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1565 isl_int_set_si(aff->v->el[1 + off + b], 0);
1566
1567 return aff;
1568 }
1569
1570 /* Sort the divs in the local space of "aff" according to
1571 * the comparison function "cmp_row" in isl_local_space.c,
1572 * combining the coefficients of identical divs.
1573 *
1574 * Reordering divs does not change the semantics of "aff",
1575 * so there is no need to call isl_aff_cow.
1576 * Moreover, this function is currently only called on isl_affs
1577 * with a single reference.
1578 */
sort_divs(__isl_take isl_aff * aff)1579 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1580 {
1581 isl_size n;
1582 int i, j;
1583
1584 n = isl_aff_dim(aff, isl_dim_div);
1585 if (n < 0)
1586 return isl_aff_free(aff);
1587 for (i = 1; i < n; ++i) {
1588 for (j = i - 1; j >= 0; --j) {
1589 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1590 if (cmp < 0)
1591 break;
1592 if (cmp == 0)
1593 aff = merge_divs(aff, j, j + 1);
1594 else
1595 aff = swap_div(aff, j, j + 1);
1596 if (!aff)
1597 return NULL;
1598 }
1599 }
1600
1601 return aff;
1602 }
1603
1604 /* Normalize the representation of "aff".
1605 *
1606 * This function should only be called on "new" isl_affs, i.e.,
1607 * with only a single reference. We therefore do not need to
1608 * worry about affecting other instances.
1609 */
isl_aff_normalize(__isl_take isl_aff * aff)1610 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1611 {
1612 if (!aff)
1613 return NULL;
1614 aff->v = isl_vec_normalize(aff->v);
1615 if (!aff->v)
1616 return isl_aff_free(aff);
1617 aff = plug_in_integral_divs(aff);
1618 aff = plug_in_unit_divs(aff);
1619 aff = sort_divs(aff);
1620 aff = isl_aff_remove_unused_divs(aff);
1621 return aff;
1622 }
1623
1624 /* Given f, return floor(f).
1625 * If f is an integer expression, then just return f.
1626 * If f is a constant, then return the constant floor(f).
1627 * Otherwise, if f = g/m, write g = q m + r,
1628 * create a new div d = [r/m] and return the expression q + d.
1629 * The coefficients in r are taken to lie between -m/2 and m/2.
1630 *
1631 * reduce_div_coefficients performs the same normalization.
1632 *
1633 * As a special case, floor(NaN) = NaN.
1634 */
isl_aff_floor(__isl_take isl_aff * aff)1635 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1636 {
1637 int i;
1638 int size;
1639 isl_ctx *ctx;
1640 isl_vec *div;
1641
1642 if (!aff)
1643 return NULL;
1644
1645 if (isl_aff_is_nan(aff))
1646 return aff;
1647 if (isl_int_is_one(aff->v->el[0]))
1648 return aff;
1649
1650 aff = isl_aff_cow(aff);
1651 if (!aff)
1652 return NULL;
1653
1654 aff->v = isl_vec_cow(aff->v);
1655 if (!aff->v)
1656 return isl_aff_free(aff);
1657
1658 if (isl_aff_is_cst(aff)) {
1659 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1660 isl_int_set_si(aff->v->el[0], 1);
1661 return aff;
1662 }
1663
1664 div = isl_vec_copy(aff->v);
1665 div = isl_vec_cow(div);
1666 if (!div)
1667 return isl_aff_free(aff);
1668
1669 ctx = isl_aff_get_ctx(aff);
1670 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1671 for (i = 1; i < aff->v->size; ++i) {
1672 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1673 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1674 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1675 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1676 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1677 }
1678 }
1679
1680 aff->ls = isl_local_space_add_div(aff->ls, div);
1681 if (!aff->ls)
1682 return isl_aff_free(aff);
1683
1684 size = aff->v->size;
1685 aff->v = isl_vec_extend(aff->v, size + 1);
1686 if (!aff->v)
1687 return isl_aff_free(aff);
1688 isl_int_set_si(aff->v->el[0], 1);
1689 isl_int_set_si(aff->v->el[size], 1);
1690
1691 aff = isl_aff_normalize(aff);
1692
1693 return aff;
1694 }
1695
1696 /* Compute
1697 *
1698 * aff mod m = aff - m * floor(aff/m)
1699 *
1700 * with m an integer value.
1701 */
isl_aff_mod_val(__isl_take isl_aff * aff,__isl_take isl_val * m)1702 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1703 __isl_take isl_val *m)
1704 {
1705 isl_aff *res;
1706
1707 if (!aff || !m)
1708 goto error;
1709
1710 if (!isl_val_is_int(m))
1711 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1712 "expecting integer modulo", goto error);
1713
1714 res = isl_aff_copy(aff);
1715 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1716 aff = isl_aff_floor(aff);
1717 aff = isl_aff_scale_val(aff, m);
1718 res = isl_aff_sub(res, aff);
1719
1720 return res;
1721 error:
1722 isl_aff_free(aff);
1723 isl_val_free(m);
1724 return NULL;
1725 }
1726
1727 /* Compute
1728 *
1729 * pwaff mod m = pwaff - m * floor(pwaff/m)
1730 */
isl_pw_aff_mod(__isl_take isl_pw_aff * pwaff,isl_int m)1731 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1732 {
1733 isl_pw_aff *res;
1734
1735 res = isl_pw_aff_copy(pwaff);
1736 pwaff = isl_pw_aff_scale_down(pwaff, m);
1737 pwaff = isl_pw_aff_floor(pwaff);
1738 pwaff = isl_pw_aff_scale(pwaff, m);
1739 res = isl_pw_aff_sub(res, pwaff);
1740
1741 return res;
1742 }
1743
1744 /* Compute
1745 *
1746 * pa mod m = pa - m * floor(pa/m)
1747 *
1748 * with m an integer value.
1749 */
isl_pw_aff_mod_val(__isl_take isl_pw_aff * pa,__isl_take isl_val * m)1750 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1751 __isl_take isl_val *m)
1752 {
1753 if (!pa || !m)
1754 goto error;
1755 if (!isl_val_is_int(m))
1756 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1757 "expecting integer modulo", goto error);
1758 pa = isl_pw_aff_mod(pa, m->n);
1759 isl_val_free(m);
1760 return pa;
1761 error:
1762 isl_pw_aff_free(pa);
1763 isl_val_free(m);
1764 return NULL;
1765 }
1766
1767 /* Given f, return ceil(f).
1768 * If f is an integer expression, then just return f.
1769 * Otherwise, let f be the expression
1770 *
1771 * e/m
1772 *
1773 * then return
1774 *
1775 * floor((e + m - 1)/m)
1776 *
1777 * As a special case, ceil(NaN) = NaN.
1778 */
isl_aff_ceil(__isl_take isl_aff * aff)1779 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1780 {
1781 if (!aff)
1782 return NULL;
1783
1784 if (isl_aff_is_nan(aff))
1785 return aff;
1786 if (isl_int_is_one(aff->v->el[0]))
1787 return aff;
1788
1789 aff = isl_aff_cow(aff);
1790 if (!aff)
1791 return NULL;
1792 aff->v = isl_vec_cow(aff->v);
1793 if (!aff->v)
1794 return isl_aff_free(aff);
1795
1796 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1797 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1798 aff = isl_aff_floor(aff);
1799
1800 return aff;
1801 }
1802
1803 /* Apply the expansion computed by isl_merge_divs.
1804 * The expansion itself is given by "exp" while the resulting
1805 * list of divs is given by "div".
1806 */
isl_aff_expand_divs(__isl_take isl_aff * aff,__isl_take isl_mat * div,int * exp)1807 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1808 __isl_take isl_mat *div, int *exp)
1809 {
1810 isl_size old_n_div;
1811 isl_size new_n_div;
1812 isl_size offset;
1813
1814 aff = isl_aff_cow(aff);
1815
1816 offset = isl_aff_domain_offset(aff, isl_dim_div);
1817 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1818 new_n_div = isl_mat_rows(div);
1819 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1820 goto error;
1821
1822 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1823 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1824 if (!aff->v || !aff->ls)
1825 return isl_aff_free(aff);
1826 return aff;
1827 error:
1828 isl_aff_free(aff);
1829 isl_mat_free(div);
1830 return NULL;
1831 }
1832
1833 /* Add two affine expressions that live in the same local space.
1834 */
add_expanded(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)1835 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1836 __isl_take isl_aff *aff2)
1837 {
1838 isl_int gcd, f;
1839
1840 aff1 = isl_aff_cow(aff1);
1841 if (!aff1 || !aff2)
1842 goto error;
1843
1844 aff1->v = isl_vec_cow(aff1->v);
1845 if (!aff1->v)
1846 goto error;
1847
1848 isl_int_init(gcd);
1849 isl_int_init(f);
1850 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1851 isl_int_divexact(f, aff2->v->el[0], gcd);
1852 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1853 isl_int_divexact(f, aff1->v->el[0], gcd);
1854 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1855 isl_int_divexact(f, aff2->v->el[0], gcd);
1856 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1857 isl_int_clear(f);
1858 isl_int_clear(gcd);
1859
1860 isl_aff_free(aff2);
1861 aff1 = isl_aff_normalize(aff1);
1862 return aff1;
1863 error:
1864 isl_aff_free(aff1);
1865 isl_aff_free(aff2);
1866 return NULL;
1867 }
1868
1869 /* Replace one of the arguments by a NaN and free the other one.
1870 */
set_nan_free(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)1871 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1872 __isl_take isl_aff *aff2)
1873 {
1874 isl_aff_free(aff2);
1875 return isl_aff_set_nan(aff1);
1876 }
1877
1878 /* Return the sum of "aff1" and "aff2".
1879 *
1880 * If either of the two is NaN, then the result is NaN.
1881 */
isl_aff_add(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)1882 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1883 __isl_take isl_aff *aff2)
1884 {
1885 isl_ctx *ctx;
1886 int *exp1 = NULL;
1887 int *exp2 = NULL;
1888 isl_mat *div;
1889 isl_size n_div1, n_div2;
1890
1891 if (!aff1 || !aff2)
1892 goto error;
1893
1894 ctx = isl_aff_get_ctx(aff1);
1895 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1896 isl_die(ctx, isl_error_invalid,
1897 "spaces don't match", goto error);
1898
1899 if (isl_aff_is_nan(aff1)) {
1900 isl_aff_free(aff2);
1901 return aff1;
1902 }
1903 if (isl_aff_is_nan(aff2)) {
1904 isl_aff_free(aff1);
1905 return aff2;
1906 }
1907
1908 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1909 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1910 if (n_div1 < 0 || n_div2 < 0)
1911 goto error;
1912 if (n_div1 == 0 && n_div2 == 0)
1913 return add_expanded(aff1, aff2);
1914
1915 exp1 = isl_alloc_array(ctx, int, n_div1);
1916 exp2 = isl_alloc_array(ctx, int, n_div2);
1917 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1918 goto error;
1919
1920 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1921 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1922 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1923 free(exp1);
1924 free(exp2);
1925
1926 return add_expanded(aff1, aff2);
1927 error:
1928 free(exp1);
1929 free(exp2);
1930 isl_aff_free(aff1);
1931 isl_aff_free(aff2);
1932 return NULL;
1933 }
1934
isl_aff_sub(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)1935 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1936 __isl_take isl_aff *aff2)
1937 {
1938 return isl_aff_add(aff1, isl_aff_neg(aff2));
1939 }
1940
1941 /* Return the result of scaling "aff" by a factor of "f".
1942 *
1943 * As a special case, f * NaN = NaN.
1944 */
isl_aff_scale(__isl_take isl_aff * aff,isl_int f)1945 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1946 {
1947 isl_int gcd;
1948
1949 if (!aff)
1950 return NULL;
1951 if (isl_aff_is_nan(aff))
1952 return aff;
1953
1954 if (isl_int_is_one(f))
1955 return aff;
1956
1957 aff = isl_aff_cow(aff);
1958 if (!aff)
1959 return NULL;
1960 aff->v = isl_vec_cow(aff->v);
1961 if (!aff->v)
1962 return isl_aff_free(aff);
1963
1964 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1965 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1966 return aff;
1967 }
1968
1969 isl_int_init(gcd);
1970 isl_int_gcd(gcd, aff->v->el[0], f);
1971 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1972 isl_int_divexact(gcd, f, gcd);
1973 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1974 isl_int_clear(gcd);
1975
1976 return aff;
1977 }
1978
1979 /* Multiple "aff" by "v".
1980 */
isl_aff_scale_val(__isl_take isl_aff * aff,__isl_take isl_val * v)1981 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1982 __isl_take isl_val *v)
1983 {
1984 if (!aff || !v)
1985 goto error;
1986
1987 if (isl_val_is_one(v)) {
1988 isl_val_free(v);
1989 return aff;
1990 }
1991
1992 if (!isl_val_is_rat(v))
1993 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1994 "expecting rational factor", goto error);
1995
1996 aff = isl_aff_scale(aff, v->n);
1997 aff = isl_aff_scale_down(aff, v->d);
1998
1999 isl_val_free(v);
2000 return aff;
2001 error:
2002 isl_aff_free(aff);
2003 isl_val_free(v);
2004 return NULL;
2005 }
2006
2007 /* Return the result of scaling "aff" down by a factor of "f".
2008 *
2009 * As a special case, NaN/f = NaN.
2010 */
isl_aff_scale_down(__isl_take isl_aff * aff,isl_int f)2011 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2012 {
2013 isl_int gcd;
2014
2015 if (!aff)
2016 return NULL;
2017 if (isl_aff_is_nan(aff))
2018 return aff;
2019
2020 if (isl_int_is_one(f))
2021 return aff;
2022
2023 aff = isl_aff_cow(aff);
2024 if (!aff)
2025 return NULL;
2026
2027 if (isl_int_is_zero(f))
2028 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2029 "cannot scale down by zero", return isl_aff_free(aff));
2030
2031 aff->v = isl_vec_cow(aff->v);
2032 if (!aff->v)
2033 return isl_aff_free(aff);
2034
2035 isl_int_init(gcd);
2036 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2037 isl_int_gcd(gcd, gcd, f);
2038 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2039 isl_int_divexact(gcd, f, gcd);
2040 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2041 isl_int_clear(gcd);
2042
2043 return aff;
2044 }
2045
2046 /* Divide "aff" by "v".
2047 */
isl_aff_scale_down_val(__isl_take isl_aff * aff,__isl_take isl_val * v)2048 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2049 __isl_take isl_val *v)
2050 {
2051 if (!aff || !v)
2052 goto error;
2053
2054 if (isl_val_is_one(v)) {
2055 isl_val_free(v);
2056 return aff;
2057 }
2058
2059 if (!isl_val_is_rat(v))
2060 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2061 "expecting rational factor", goto error);
2062 if (!isl_val_is_pos(v))
2063 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2064 "factor needs to be positive", goto error);
2065
2066 aff = isl_aff_scale(aff, v->d);
2067 aff = isl_aff_scale_down(aff, v->n);
2068
2069 isl_val_free(v);
2070 return aff;
2071 error:
2072 isl_aff_free(aff);
2073 isl_val_free(v);
2074 return NULL;
2075 }
2076
isl_aff_scale_down_ui(__isl_take isl_aff * aff,unsigned f)2077 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2078 {
2079 isl_int v;
2080
2081 if (f == 1)
2082 return aff;
2083
2084 isl_int_init(v);
2085 isl_int_set_ui(v, f);
2086 aff = isl_aff_scale_down(aff, v);
2087 isl_int_clear(v);
2088
2089 return aff;
2090 }
2091
isl_aff_set_dim_name(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned pos,const char * s)2092 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2093 enum isl_dim_type type, unsigned pos, const char *s)
2094 {
2095 aff = isl_aff_cow(aff);
2096 if (!aff)
2097 return NULL;
2098 if (type == isl_dim_out)
2099 isl_die(aff->v->ctx, isl_error_invalid,
2100 "cannot set name of output/set dimension",
2101 return isl_aff_free(aff));
2102 if (type == isl_dim_in)
2103 type = isl_dim_set;
2104 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2105 if (!aff->ls)
2106 return isl_aff_free(aff);
2107
2108 return aff;
2109 }
2110
isl_aff_set_dim_id(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned pos,__isl_take isl_id * id)2111 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2112 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2113 {
2114 aff = isl_aff_cow(aff);
2115 if (!aff)
2116 goto error;
2117 if (type == isl_dim_out)
2118 isl_die(aff->v->ctx, isl_error_invalid,
2119 "cannot set name of output/set dimension",
2120 goto error);
2121 if (type == isl_dim_in)
2122 type = isl_dim_set;
2123 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2124 if (!aff->ls)
2125 return isl_aff_free(aff);
2126
2127 return aff;
2128 error:
2129 isl_id_free(id);
2130 isl_aff_free(aff);
2131 return NULL;
2132 }
2133
2134 /* Replace the identifier of the input tuple of "aff" by "id".
2135 * type is currently required to be equal to isl_dim_in
2136 */
isl_aff_set_tuple_id(__isl_take isl_aff * aff,enum isl_dim_type type,__isl_take isl_id * id)2137 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2138 enum isl_dim_type type, __isl_take isl_id *id)
2139 {
2140 aff = isl_aff_cow(aff);
2141 if (!aff)
2142 goto error;
2143 if (type != isl_dim_in)
2144 isl_die(aff->v->ctx, isl_error_invalid,
2145 "cannot only set id of input tuple", goto error);
2146 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2147 if (!aff->ls)
2148 return isl_aff_free(aff);
2149
2150 return aff;
2151 error:
2152 isl_id_free(id);
2153 isl_aff_free(aff);
2154 return NULL;
2155 }
2156
2157 /* Exploit the equalities in "eq" to simplify the affine expression
2158 * and the expressions of the integer divisions in the local space.
2159 * The integer divisions in this local space are assumed to appear
2160 * as regular dimensions in "eq".
2161 */
isl_aff_substitute_equalities_lifted(__isl_take isl_aff * aff,__isl_take isl_basic_set * eq)2162 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2163 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2164 {
2165 int i, j;
2166 unsigned o_div;
2167 unsigned n_div;
2168
2169 if (!eq)
2170 goto error;
2171 if (eq->n_eq == 0) {
2172 isl_basic_set_free(eq);
2173 return aff;
2174 }
2175
2176 aff = isl_aff_cow(aff);
2177 if (!aff)
2178 goto error;
2179
2180 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2181 isl_basic_set_copy(eq));
2182 aff->v = isl_vec_cow(aff->v);
2183 if (!aff->ls || !aff->v)
2184 goto error;
2185
2186 o_div = isl_basic_set_offset(eq, isl_dim_div);
2187 n_div = eq->n_div;
2188 for (i = 0; i < eq->n_eq; ++i) {
2189 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2190 if (j < 0 || j == 0 || j >= o_div)
2191 continue;
2192
2193 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2194 &aff->v->el[0]);
2195 }
2196
2197 isl_basic_set_free(eq);
2198 aff = isl_aff_normalize(aff);
2199 return aff;
2200 error:
2201 isl_basic_set_free(eq);
2202 isl_aff_free(aff);
2203 return NULL;
2204 }
2205
2206 /* Exploit the equalities in "eq" to simplify the affine expression
2207 * and the expressions of the integer divisions in the local space.
2208 */
isl_aff_substitute_equalities(__isl_take isl_aff * aff,__isl_take isl_basic_set * eq)2209 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2210 __isl_take isl_basic_set *eq)
2211 {
2212 isl_size n_div;
2213
2214 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2215 if (n_div < 0)
2216 goto error;
2217 if (n_div > 0)
2218 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2219 return isl_aff_substitute_equalities_lifted(aff, eq);
2220 error:
2221 isl_basic_set_free(eq);
2222 isl_aff_free(aff);
2223 return NULL;
2224 }
2225
2226 /* Look for equalities among the variables shared by context and aff
2227 * and the integer divisions of aff, if any.
2228 * The equalities are then used to eliminate coefficients and/or integer
2229 * divisions from aff.
2230 */
isl_aff_gist(__isl_take isl_aff * aff,__isl_take isl_set * context)2231 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2232 __isl_take isl_set *context)
2233 {
2234 isl_local_space *ls;
2235 isl_basic_set *hull;
2236
2237 ls = isl_aff_get_domain_local_space(aff);
2238 context = isl_local_space_lift_set(ls, context);
2239
2240 hull = isl_set_affine_hull(context);
2241 return isl_aff_substitute_equalities_lifted(aff, hull);
2242 }
2243
isl_aff_gist_params(__isl_take isl_aff * aff,__isl_take isl_set * context)2244 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2245 __isl_take isl_set *context)
2246 {
2247 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2248 dom_context = isl_set_intersect_params(dom_context, context);
2249 return isl_aff_gist(aff, dom_context);
2250 }
2251
2252 /* Return a basic set containing those elements in the space
2253 * of aff where it is positive. "rational" should not be set.
2254 *
2255 * If "aff" is NaN, then it is not positive.
2256 */
aff_pos_basic_set(__isl_take isl_aff * aff,int rational,void * user)2257 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2258 int rational, void *user)
2259 {
2260 isl_constraint *ineq;
2261 isl_basic_set *bset;
2262 isl_val *c;
2263
2264 if (!aff)
2265 return NULL;
2266 if (isl_aff_is_nan(aff)) {
2267 isl_space *space = isl_aff_get_domain_space(aff);
2268 isl_aff_free(aff);
2269 return isl_basic_set_empty(space);
2270 }
2271 if (rational)
2272 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2273 "rational sets not supported", goto error);
2274
2275 ineq = isl_inequality_from_aff(aff);
2276 c = isl_constraint_get_constant_val(ineq);
2277 c = isl_val_sub_ui(c, 1);
2278 ineq = isl_constraint_set_constant_val(ineq, c);
2279
2280 bset = isl_basic_set_from_constraint(ineq);
2281 bset = isl_basic_set_simplify(bset);
2282 return bset;
2283 error:
2284 isl_aff_free(aff);
2285 return NULL;
2286 }
2287
2288 /* Return a basic set containing those elements in the space
2289 * of aff where it is non-negative.
2290 * If "rational" is set, then return a rational basic set.
2291 *
2292 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2293 */
aff_nonneg_basic_set(__isl_take isl_aff * aff,int rational,void * user)2294 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2295 __isl_take isl_aff *aff, int rational, void *user)
2296 {
2297 isl_constraint *ineq;
2298 isl_basic_set *bset;
2299
2300 if (!aff)
2301 return NULL;
2302 if (isl_aff_is_nan(aff)) {
2303 isl_space *space = isl_aff_get_domain_space(aff);
2304 isl_aff_free(aff);
2305 return isl_basic_set_empty(space);
2306 }
2307
2308 ineq = isl_inequality_from_aff(aff);
2309
2310 bset = isl_basic_set_from_constraint(ineq);
2311 if (rational)
2312 bset = isl_basic_set_set_rational(bset);
2313 bset = isl_basic_set_simplify(bset);
2314 return bset;
2315 }
2316
2317 /* Return a basic set containing those elements in the space
2318 * of aff where it is non-negative.
2319 */
isl_aff_nonneg_basic_set(__isl_take isl_aff * aff)2320 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2321 {
2322 return aff_nonneg_basic_set(aff, 0, NULL);
2323 }
2324
2325 /* Return a basic set containing those elements in the domain space
2326 * of "aff" where it is positive.
2327 */
isl_aff_pos_basic_set(__isl_take isl_aff * aff)2328 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2329 {
2330 aff = isl_aff_add_constant_num_si(aff, -1);
2331 return isl_aff_nonneg_basic_set(aff);
2332 }
2333
2334 /* Return a basic set containing those elements in the domain space
2335 * of aff where it is negative.
2336 */
isl_aff_neg_basic_set(__isl_take isl_aff * aff)2337 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2338 {
2339 aff = isl_aff_neg(aff);
2340 return isl_aff_pos_basic_set(aff);
2341 }
2342
2343 /* Return a basic set containing those elements in the space
2344 * of aff where it is zero.
2345 * If "rational" is set, then return a rational basic set.
2346 *
2347 * If "aff" is NaN, then it is not zero.
2348 */
aff_zero_basic_set(__isl_take isl_aff * aff,int rational,void * user)2349 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2350 int rational, void *user)
2351 {
2352 isl_constraint *ineq;
2353 isl_basic_set *bset;
2354
2355 if (!aff)
2356 return NULL;
2357 if (isl_aff_is_nan(aff)) {
2358 isl_space *space = isl_aff_get_domain_space(aff);
2359 isl_aff_free(aff);
2360 return isl_basic_set_empty(space);
2361 }
2362
2363 ineq = isl_equality_from_aff(aff);
2364
2365 bset = isl_basic_set_from_constraint(ineq);
2366 if (rational)
2367 bset = isl_basic_set_set_rational(bset);
2368 bset = isl_basic_set_simplify(bset);
2369 return bset;
2370 }
2371
2372 /* Return a basic set containing those elements in the space
2373 * of aff where it is zero.
2374 */
isl_aff_zero_basic_set(__isl_take isl_aff * aff)2375 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2376 {
2377 return aff_zero_basic_set(aff, 0, NULL);
2378 }
2379
2380 /* Return a basic set containing those elements in the shared space
2381 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2382 */
isl_aff_ge_basic_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2383 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2384 __isl_take isl_aff *aff2)
2385 {
2386 aff1 = isl_aff_sub(aff1, aff2);
2387
2388 return isl_aff_nonneg_basic_set(aff1);
2389 }
2390
2391 /* Return a basic set containing those elements in the shared domain space
2392 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2393 */
isl_aff_gt_basic_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2394 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2395 __isl_take isl_aff *aff2)
2396 {
2397 aff1 = isl_aff_sub(aff1, aff2);
2398
2399 return isl_aff_pos_basic_set(aff1);
2400 }
2401
2402 /* Return a set containing those elements in the shared space
2403 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2404 */
isl_aff_ge_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2405 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2406 __isl_take isl_aff *aff2)
2407 {
2408 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2409 }
2410
2411 /* Return a set containing those elements in the shared domain space
2412 * of aff1 and aff2 where aff1 is greater than aff2.
2413 *
2414 * If either of the two inputs is NaN, then the result is empty,
2415 * as comparisons with NaN always return false.
2416 */
isl_aff_gt_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2417 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2418 __isl_take isl_aff *aff2)
2419 {
2420 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2421 }
2422
2423 /* Return a basic set containing those elements in the shared space
2424 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2425 */
isl_aff_le_basic_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2426 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2427 __isl_take isl_aff *aff2)
2428 {
2429 return isl_aff_ge_basic_set(aff2, aff1);
2430 }
2431
2432 /* Return a basic set containing those elements in the shared domain space
2433 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2434 */
isl_aff_lt_basic_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2435 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2436 __isl_take isl_aff *aff2)
2437 {
2438 return isl_aff_gt_basic_set(aff2, aff1);
2439 }
2440
2441 /* Return a set containing those elements in the shared space
2442 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2443 */
isl_aff_le_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2444 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2445 __isl_take isl_aff *aff2)
2446 {
2447 return isl_aff_ge_set(aff2, aff1);
2448 }
2449
2450 /* Return a set containing those elements in the shared domain space
2451 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2452 */
isl_aff_lt_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2453 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2454 __isl_take isl_aff *aff2)
2455 {
2456 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2457 }
2458
2459 /* Return a basic set containing those elements in the shared space
2460 * of aff1 and aff2 where aff1 and aff2 are equal.
2461 */
isl_aff_eq_basic_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2462 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2463 __isl_take isl_aff *aff2)
2464 {
2465 aff1 = isl_aff_sub(aff1, aff2);
2466
2467 return isl_aff_zero_basic_set(aff1);
2468 }
2469
2470 /* Return a set containing those elements in the shared space
2471 * of aff1 and aff2 where aff1 and aff2 are equal.
2472 */
isl_aff_eq_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2473 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2474 __isl_take isl_aff *aff2)
2475 {
2476 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2477 }
2478
2479 /* Return a set containing those elements in the shared domain space
2480 * of aff1 and aff2 where aff1 and aff2 are not equal.
2481 *
2482 * If either of the two inputs is NaN, then the result is empty,
2483 * as comparisons with NaN always return false.
2484 */
isl_aff_ne_set(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2485 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2486 __isl_take isl_aff *aff2)
2487 {
2488 isl_set *set_lt, *set_gt;
2489
2490 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2491 isl_aff_copy(aff2));
2492 set_gt = isl_aff_gt_set(aff1, aff2);
2493 return isl_set_union_disjoint(set_lt, set_gt);
2494 }
2495
isl_aff_add_on_domain(__isl_keep isl_set * dom,__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)2496 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2497 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2498 {
2499 aff1 = isl_aff_add(aff1, aff2);
2500 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2501 return aff1;
2502 }
2503
isl_aff_is_empty(__isl_keep isl_aff * aff)2504 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2505 {
2506 if (!aff)
2507 return isl_bool_error;
2508
2509 return isl_bool_false;
2510 }
2511
2512 #undef TYPE
2513 #define TYPE isl_aff
2514 static
2515 #include "check_type_range_templ.c"
2516
2517 /* Check whether the given affine expression has non-zero coefficient
2518 * for any dimension in the given range or if any of these dimensions
2519 * appear with non-zero coefficients in any of the integer divisions
2520 * involved in the affine expression.
2521 */
isl_aff_involves_dims(__isl_keep isl_aff * aff,enum isl_dim_type type,unsigned first,unsigned n)2522 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2523 enum isl_dim_type type, unsigned first, unsigned n)
2524 {
2525 int i;
2526 int *active = NULL;
2527 isl_bool involves = isl_bool_false;
2528
2529 if (!aff)
2530 return isl_bool_error;
2531 if (n == 0)
2532 return isl_bool_false;
2533 if (isl_aff_check_range(aff, type, first, n) < 0)
2534 return isl_bool_error;
2535
2536 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2537 if (!active)
2538 goto error;
2539
2540 first += isl_local_space_offset(aff->ls, type) - 1;
2541 for (i = 0; i < n; ++i)
2542 if (active[first + i]) {
2543 involves = isl_bool_true;
2544 break;
2545 }
2546
2547 free(active);
2548
2549 return involves;
2550 error:
2551 free(active);
2552 return isl_bool_error;
2553 }
2554
2555 /* Does "aff" involve any local variables, i.e., integer divisions?
2556 */
isl_aff_involves_locals(__isl_keep isl_aff * aff)2557 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2558 {
2559 isl_size n;
2560
2561 n = isl_aff_dim(aff, isl_dim_div);
2562 if (n < 0)
2563 return isl_bool_error;
2564 return isl_bool_ok(n > 0);
2565 }
2566
isl_aff_drop_dims(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned first,unsigned n)2567 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2568 enum isl_dim_type type, unsigned first, unsigned n)
2569 {
2570 isl_ctx *ctx;
2571
2572 if (!aff)
2573 return NULL;
2574 if (type == isl_dim_out)
2575 isl_die(aff->v->ctx, isl_error_invalid,
2576 "cannot drop output/set dimension",
2577 return isl_aff_free(aff));
2578 if (type == isl_dim_in)
2579 type = isl_dim_set;
2580 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2581 return aff;
2582
2583 ctx = isl_aff_get_ctx(aff);
2584 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2585 return isl_aff_free(aff);
2586
2587 aff = isl_aff_cow(aff);
2588 if (!aff)
2589 return NULL;
2590
2591 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2592 if (!aff->ls)
2593 return isl_aff_free(aff);
2594
2595 first += 1 + isl_local_space_offset(aff->ls, type);
2596 aff->v = isl_vec_drop_els(aff->v, first, n);
2597 if (!aff->v)
2598 return isl_aff_free(aff);
2599
2600 return aff;
2601 }
2602
2603 /* Is the domain of "aff" a product?
2604 */
isl_aff_domain_is_product(__isl_keep isl_aff * aff)2605 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2606 {
2607 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2608 }
2609
2610 #undef TYPE
2611 #define TYPE isl_aff
2612 #include <isl_domain_factor_templ.c>
2613
2614 /* Project the domain of the affine expression onto its parameter space.
2615 * The affine expression may not involve any of the domain dimensions.
2616 */
isl_aff_project_domain_on_params(__isl_take isl_aff * aff)2617 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2618 {
2619 isl_space *space;
2620 isl_size n;
2621
2622 n = isl_aff_dim(aff, isl_dim_in);
2623 if (n < 0)
2624 return isl_aff_free(aff);
2625 aff = isl_aff_drop_domain(aff, 0, n);
2626 space = isl_aff_get_domain_space(aff);
2627 space = isl_space_params(space);
2628 aff = isl_aff_reset_domain_space(aff, space);
2629 return aff;
2630 }
2631
2632 /* Convert an affine expression defined over a parameter domain
2633 * into one that is defined over a zero-dimensional set.
2634 */
isl_aff_from_range(__isl_take isl_aff * aff)2635 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2636 {
2637 isl_local_space *ls;
2638
2639 ls = isl_aff_take_domain_local_space(aff);
2640 ls = isl_local_space_set_from_params(ls);
2641 aff = isl_aff_restore_domain_local_space(aff, ls);
2642
2643 return aff;
2644 }
2645
isl_aff_insert_dims(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned first,unsigned n)2646 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2647 enum isl_dim_type type, unsigned first, unsigned n)
2648 {
2649 isl_ctx *ctx;
2650
2651 if (!aff)
2652 return NULL;
2653 if (type == isl_dim_out)
2654 isl_die(aff->v->ctx, isl_error_invalid,
2655 "cannot insert output/set dimensions",
2656 return isl_aff_free(aff));
2657 if (type == isl_dim_in)
2658 type = isl_dim_set;
2659 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2660 return aff;
2661
2662 ctx = isl_aff_get_ctx(aff);
2663 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2664 return isl_aff_free(aff);
2665
2666 aff = isl_aff_cow(aff);
2667 if (!aff)
2668 return NULL;
2669
2670 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2671 if (!aff->ls)
2672 return isl_aff_free(aff);
2673
2674 first += 1 + isl_local_space_offset(aff->ls, type);
2675 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2676 if (!aff->v)
2677 return isl_aff_free(aff);
2678
2679 return aff;
2680 }
2681
isl_aff_add_dims(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned n)2682 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2683 enum isl_dim_type type, unsigned n)
2684 {
2685 isl_size pos;
2686
2687 pos = isl_aff_dim(aff, type);
2688 if (pos < 0)
2689 return isl_aff_free(aff);
2690
2691 return isl_aff_insert_dims(aff, type, pos, n);
2692 }
2693
2694 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2695 * to dimensions of "dst_type" at "dst_pos".
2696 *
2697 * We only support moving input dimensions to parameters and vice versa.
2698 */
isl_aff_move_dims(__isl_take isl_aff * aff,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)2699 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2700 enum isl_dim_type dst_type, unsigned dst_pos,
2701 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2702 {
2703 unsigned g_dst_pos;
2704 unsigned g_src_pos;
2705 isl_size src_off, dst_off;
2706
2707 if (!aff)
2708 return NULL;
2709 if (n == 0 &&
2710 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2711 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2712 return aff;
2713
2714 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2715 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2716 "cannot move output/set dimension",
2717 return isl_aff_free(aff));
2718 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2719 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2720 "cannot move divs", return isl_aff_free(aff));
2721 if (dst_type == isl_dim_in)
2722 dst_type = isl_dim_set;
2723 if (src_type == isl_dim_in)
2724 src_type = isl_dim_set;
2725
2726 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2727 return isl_aff_free(aff);
2728 if (dst_type == src_type)
2729 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2730 "moving dims within the same type not supported",
2731 return isl_aff_free(aff));
2732
2733 aff = isl_aff_cow(aff);
2734 src_off = isl_aff_domain_offset(aff, src_type);
2735 dst_off = isl_aff_domain_offset(aff, dst_type);
2736 if (src_off < 0 || dst_off < 0)
2737 return isl_aff_free(aff);
2738
2739 g_src_pos = 1 + src_off + src_pos;
2740 g_dst_pos = 1 + dst_off + dst_pos;
2741 if (dst_type > src_type)
2742 g_dst_pos -= n;
2743
2744 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2745 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2746 src_type, src_pos, n);
2747 if (!aff->v || !aff->ls)
2748 return isl_aff_free(aff);
2749
2750 aff = sort_divs(aff);
2751
2752 return aff;
2753 }
2754
2755 /* Return a zero isl_aff in the given space.
2756 *
2757 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2758 * interface over all piecewise types.
2759 */
isl_aff_zero_in_space(__isl_take isl_space * space)2760 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2761 {
2762 isl_local_space *ls;
2763
2764 ls = isl_local_space_from_space(isl_space_domain(space));
2765 return isl_aff_zero_on_domain(ls);
2766 }
2767
2768 #define isl_aff_involves_nan isl_aff_is_nan
2769
2770 #undef PW
2771 #define PW isl_pw_aff
2772 #undef BASE
2773 #define BASE aff
2774 #undef EL_IS_ZERO
2775 #define EL_IS_ZERO is_empty
2776 #undef ZERO
2777 #define ZERO empty
2778 #undef IS_ZERO
2779 #define IS_ZERO is_empty
2780 #undef FIELD
2781 #define FIELD aff
2782 #undef DEFAULT_IS_ZERO
2783 #define DEFAULT_IS_ZERO 0
2784
2785 #include <isl_pw_templ.c>
2786 #include <isl_pw_add_constant_val_templ.c>
2787 #include <isl_pw_bind_domain_templ.c>
2788 #include <isl_pw_eval.c>
2789 #include <isl_pw_hash.c>
2790 #include <isl_pw_insert_dims_templ.c>
2791 #include <isl_pw_insert_domain_templ.c>
2792 #include <isl_pw_move_dims_templ.c>
2793 #include <isl_pw_neg_templ.c>
2794 #include <isl_pw_pullback_templ.c>
2795 #include <isl_pw_sub_templ.c>
2796 #include <isl_pw_union_opt.c>
2797
2798 #undef BASE
2799 #define BASE pw_aff
2800
2801 #include <isl_union_single.c>
2802 #include <isl_union_neg.c>
2803
2804 #undef BASE
2805 #define BASE aff
2806
2807 #include <isl_union_pw_templ.c>
2808
2809 /* Compute a piecewise quasi-affine expression with a domain that
2810 * is the union of those of pwaff1 and pwaff2 and such that on each
2811 * cell, the quasi-affine expression is the maximum of those of pwaff1
2812 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2813 * cell, then the associated expression is the defined one.
2814 */
isl_pw_aff_union_max(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)2815 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2816 __isl_take isl_pw_aff *pwaff2)
2817 {
2818 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2819 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2820 }
2821
2822 /* Compute a piecewise quasi-affine expression with a domain that
2823 * is the union of those of pwaff1 and pwaff2 and such that on each
2824 * cell, the quasi-affine expression is the minimum of those of pwaff1
2825 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2826 * cell, then the associated expression is the defined one.
2827 */
isl_pw_aff_union_min(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)2828 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2829 __isl_take isl_pw_aff *pwaff2)
2830 {
2831 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2832 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2833 }
2834
isl_pw_aff_union_opt(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2,int max)2835 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2836 __isl_take isl_pw_aff *pwaff2, int max)
2837 {
2838 if (max)
2839 return isl_pw_aff_union_max(pwaff1, pwaff2);
2840 else
2841 return isl_pw_aff_union_min(pwaff1, pwaff2);
2842 }
2843
2844 /* Is the domain of "pa" a product?
2845 */
isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff * pa)2846 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2847 {
2848 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2849 }
2850
2851 #undef TYPE
2852 #define TYPE isl_pw_aff
2853 #include <isl_domain_factor_templ.c>
2854
2855 /* Return a set containing those elements in the domain
2856 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2857 * does not satisfy "fn" (if complement is 1).
2858 *
2859 * The pieces with a NaN never belong to the result since
2860 * NaN does not satisfy any property.
2861 */
pw_aff_locus(__isl_take isl_pw_aff * pwaff,__isl_give isl_basic_set * (* fn)(__isl_take isl_aff * aff,int rational,void * user),int complement,void * user)2862 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2863 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2864 void *user),
2865 int complement, void *user)
2866 {
2867 int i;
2868 isl_set *set;
2869
2870 if (!pwaff)
2871 return NULL;
2872
2873 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2874
2875 for (i = 0; i < pwaff->n; ++i) {
2876 isl_basic_set *bset;
2877 isl_set *set_i, *locus;
2878 isl_bool rational;
2879
2880 if (isl_aff_is_nan(pwaff->p[i].aff))
2881 continue;
2882
2883 rational = isl_set_has_rational(pwaff->p[i].set);
2884 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2885 locus = isl_set_from_basic_set(bset);
2886 set_i = isl_set_copy(pwaff->p[i].set);
2887 if (complement)
2888 set_i = isl_set_subtract(set_i, locus);
2889 else
2890 set_i = isl_set_intersect(set_i, locus);
2891 set = isl_set_union_disjoint(set, set_i);
2892 }
2893
2894 isl_pw_aff_free(pwaff);
2895
2896 return set;
2897 }
2898
2899 /* Return a set containing those elements in the domain
2900 * of "pa" where it is positive.
2901 */
isl_pw_aff_pos_set(__isl_take isl_pw_aff * pa)2902 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2903 {
2904 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2905 }
2906
2907 /* Return a set containing those elements in the domain
2908 * of pwaff where it is non-negative.
2909 */
isl_pw_aff_nonneg_set(__isl_take isl_pw_aff * pwaff)2910 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2911 {
2912 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2913 }
2914
2915 /* Return a set containing those elements in the domain
2916 * of pwaff where it is zero.
2917 */
isl_pw_aff_zero_set(__isl_take isl_pw_aff * pwaff)2918 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2919 {
2920 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2921 }
2922
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is not zero.
2925 */
isl_pw_aff_non_zero_set(__isl_take isl_pw_aff * pwaff)2926 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2927 {
2928 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2929 }
2930
2931 /* Bind the affine function "aff" to the parameter "id",
2932 * returning the elements in the domain where the affine expression
2933 * is equal to the parameter.
2934 */
isl_aff_bind_id(__isl_take isl_aff * aff,__isl_take isl_id * id)2935 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2936 __isl_take isl_id *id)
2937 {
2938 isl_space *space;
2939 isl_aff *aff_id;
2940
2941 space = isl_aff_get_domain_space(aff);
2942 space = isl_space_add_param_id(space, isl_id_copy(id));
2943
2944 aff = isl_aff_align_params(aff, isl_space_copy(space));
2945 aff_id = isl_aff_param_on_domain_space_id(space, id);
2946
2947 return isl_aff_eq_basic_set(aff, aff_id);
2948 }
2949
2950 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2951 * "rational" should not be set.
2952 */
aff_bind_id(__isl_take isl_aff * aff,int rational,void * user)2953 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2954 int rational, void *user)
2955 {
2956 isl_id *id = user;
2957
2958 if (!aff)
2959 return NULL;
2960 if (rational)
2961 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2962 "rational binding not supported", goto error);
2963 return isl_aff_bind_id(aff, isl_id_copy(id));
2964 error:
2965 isl_aff_free(aff);
2966 return NULL;
2967 }
2968
2969 /* Bind the piecewise affine function "pa" to the parameter "id",
2970 * returning the elements in the domain where the expression
2971 * is equal to the parameter.
2972 */
isl_pw_aff_bind_id(__isl_take isl_pw_aff * pa,__isl_take isl_id * id)2973 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2974 __isl_take isl_id *id)
2975 {
2976 isl_set *bound;
2977
2978 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2979 isl_id_free(id);
2980
2981 return bound;
2982 }
2983
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2986 *
2987 * We compute the difference on the shared domain and then construct
2988 * the set of values where this difference is non-negative.
2989 * If strict is set, we first subtract 1 from the difference.
2990 * If equal is set, we only return the elements where pwaff1 and pwaff2
2991 * are equal.
2992 */
pw_aff_gte_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2,int strict,int equal)2993 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2995 {
2996 isl_set *set1, *set2;
2997
2998 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2999 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3000 set1 = isl_set_intersect(set1, set2);
3001 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3002 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3003 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3004
3005 if (strict) {
3006 isl_space *space = isl_set_get_space(set1);
3007 isl_aff *aff;
3008 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3009 aff = isl_aff_add_constant_si(aff, -1);
3010 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3011 } else
3012 isl_set_free(set1);
3013
3014 if (equal)
3015 return isl_pw_aff_zero_set(pwaff1);
3016 return isl_pw_aff_nonneg_set(pwaff1);
3017 }
3018
3019 /* Return a set containing those elements in the shared domain
3020 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3021 */
isl_pw_aff_eq_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3022 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3023 __isl_take isl_pw_aff *pwaff2)
3024 {
3025 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3026 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3027 }
3028
3029 /* Return a set containing those elements in the shared domain
3030 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3031 */
isl_pw_aff_ge_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3032 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3033 __isl_take isl_pw_aff *pwaff2)
3034 {
3035 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3036 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3037 }
3038
3039 /* Return a set containing those elements in the shared domain
3040 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3041 */
isl_pw_aff_gt_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3042 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3043 __isl_take isl_pw_aff *pwaff2)
3044 {
3045 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3046 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3047 }
3048
isl_pw_aff_le_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3049 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3050 __isl_take isl_pw_aff *pwaff2)
3051 {
3052 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3053 }
3054
isl_pw_aff_lt_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3055 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3056 __isl_take isl_pw_aff *pwaff2)
3057 {
3058 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3059 }
3060
3061 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3062 * where the function values are ordered in the same way as "order",
3063 * which returns a set in the shared domain of its two arguments.
3064 *
3065 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3066 * We first pull back the two functions such that they are defined on
3067 * the domain [A -> B]. Then we apply "order", resulting in a set
3068 * in the space [A -> B]. Finally, we unwrap this set to obtain
3069 * a map in the space A -> B.
3070 */
isl_pw_aff_order_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2,__isl_give isl_set * (* order)(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2))3071 static __isl_give isl_map *isl_pw_aff_order_map(
3072 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3073 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3074 __isl_take isl_pw_aff *pa2))
3075 {
3076 isl_space *space1, *space2;
3077 isl_multi_aff *ma;
3078 isl_set *set;
3079
3080 isl_pw_aff_align_params_bin(&pa1, &pa2);
3081 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3082 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3083 space1 = isl_space_map_from_domain_and_range(space1, space2);
3084 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3085 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3086 ma = isl_multi_aff_range_map(space1);
3087 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3088 set = order(pa1, pa2);
3089
3090 return isl_set_unwrap(set);
3091 }
3092
3093 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3094 * where the function values are equal.
3095 */
isl_pw_aff_eq_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3096 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3097 __isl_take isl_pw_aff *pa2)
3098 {
3099 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3100 }
3101
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function value of "pa1" is less than or equal to
3104 * the function value of "pa2".
3105 */
isl_pw_aff_le_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3106 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3107 __isl_take isl_pw_aff *pa2)
3108 {
3109 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3110 }
3111
3112 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3113 * where the function value of "pa1" is less than the function value of "pa2".
3114 */
isl_pw_aff_lt_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3115 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3116 __isl_take isl_pw_aff *pa2)
3117 {
3118 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3119 }
3120
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is greater than or equal to
3123 * the function value of "pa2".
3124 */
isl_pw_aff_ge_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3125 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3126 __isl_take isl_pw_aff *pa2)
3127 {
3128 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3129 }
3130
3131 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3132 * where the function value of "pa1" is greater than the function value
3133 * of "pa2".
3134 */
isl_pw_aff_gt_map(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3135 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3136 __isl_take isl_pw_aff *pa2)
3137 {
3138 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3139 }
3140
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * has the relation specified by "fn" with each element in list2.
3144 */
pw_aff_list_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2,__isl_give isl_set * (* fn)(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2))3145 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2,
3147 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3148 __isl_take isl_pw_aff *pwaff2))
3149 {
3150 int i, j;
3151 isl_ctx *ctx;
3152 isl_set *set;
3153
3154 if (!list1 || !list2)
3155 goto error;
3156
3157 ctx = isl_pw_aff_list_get_ctx(list1);
3158 if (list1->n < 1 || list2->n < 1)
3159 isl_die(ctx, isl_error_invalid,
3160 "list should contain at least one element", goto error);
3161
3162 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3163 for (i = 0; i < list1->n; ++i)
3164 for (j = 0; j < list2->n; ++j) {
3165 isl_set *set_ij;
3166
3167 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3168 isl_pw_aff_copy(list2->p[j]));
3169 set = isl_set_intersect(set, set_ij);
3170 }
3171
3172 isl_pw_aff_list_free(list1);
3173 isl_pw_aff_list_free(list2);
3174 return set;
3175 error:
3176 isl_pw_aff_list_free(list1);
3177 isl_pw_aff_list_free(list2);
3178 return NULL;
3179 }
3180
3181 /* Return a set containing those elements in the shared domain
3182 * of the elements of list1 and list2 where each element in list1
3183 * is equal to each element in list2.
3184 */
isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3185 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3186 __isl_take isl_pw_aff_list *list2)
3187 {
3188 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3189 }
3190
isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3191 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3192 __isl_take isl_pw_aff_list *list2)
3193 {
3194 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3195 }
3196
3197 /* Return a set containing those elements in the shared domain
3198 * of the elements of list1 and list2 where each element in list1
3199 * is less than or equal to each element in list2.
3200 */
isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3201 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3202 __isl_take isl_pw_aff_list *list2)
3203 {
3204 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3205 }
3206
isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3207 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3208 __isl_take isl_pw_aff_list *list2)
3209 {
3210 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3211 }
3212
isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3213 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3214 __isl_take isl_pw_aff_list *list2)
3215 {
3216 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3217 }
3218
isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list * list1,__isl_take isl_pw_aff_list * list2)3219 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3220 __isl_take isl_pw_aff_list *list2)
3221 {
3222 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3223 }
3224
3225
3226 /* Return a set containing those elements in the shared domain
3227 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3228 */
isl_pw_aff_ne_set(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3229 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3230 __isl_take isl_pw_aff *pwaff2)
3231 {
3232 isl_set *set_lt, *set_gt;
3233
3234 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3235 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3236 isl_pw_aff_copy(pwaff2));
3237 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3238 return isl_set_union_disjoint(set_lt, set_gt);
3239 }
3240
isl_pw_aff_scale_down(__isl_take isl_pw_aff * pwaff,isl_int v)3241 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3242 isl_int v)
3243 {
3244 int i;
3245
3246 if (isl_int_is_one(v))
3247 return pwaff;
3248 if (!isl_int_is_pos(v))
3249 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3250 "factor needs to be positive",
3251 return isl_pw_aff_free(pwaff));
3252 pwaff = isl_pw_aff_cow(pwaff);
3253 if (!pwaff)
3254 return NULL;
3255 if (pwaff->n == 0)
3256 return pwaff;
3257
3258 for (i = 0; i < pwaff->n; ++i) {
3259 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3260 if (!pwaff->p[i].aff)
3261 return isl_pw_aff_free(pwaff);
3262 }
3263
3264 return pwaff;
3265 }
3266
isl_pw_aff_floor(__isl_take isl_pw_aff * pwaff)3267 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3268 {
3269 int i;
3270
3271 pwaff = isl_pw_aff_cow(pwaff);
3272 if (!pwaff)
3273 return NULL;
3274 if (pwaff->n == 0)
3275 return pwaff;
3276
3277 for (i = 0; i < pwaff->n; ++i) {
3278 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3279 if (!pwaff->p[i].aff)
3280 return isl_pw_aff_free(pwaff);
3281 }
3282
3283 return pwaff;
3284 }
3285
isl_pw_aff_ceil(__isl_take isl_pw_aff * pwaff)3286 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3287 {
3288 int i;
3289
3290 pwaff = isl_pw_aff_cow(pwaff);
3291 if (!pwaff)
3292 return NULL;
3293 if (pwaff->n == 0)
3294 return pwaff;
3295
3296 for (i = 0; i < pwaff->n; ++i) {
3297 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3298 if (!pwaff->p[i].aff)
3299 return isl_pw_aff_free(pwaff);
3300 }
3301
3302 return pwaff;
3303 }
3304
3305 /* Assuming that "cond1" and "cond2" are disjoint,
3306 * return an affine expression that is equal to pwaff1 on cond1
3307 * and to pwaff2 on cond2.
3308 */
isl_pw_aff_select(__isl_take isl_set * cond1,__isl_take isl_pw_aff * pwaff1,__isl_take isl_set * cond2,__isl_take isl_pw_aff * pwaff2)3309 static __isl_give isl_pw_aff *isl_pw_aff_select(
3310 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3311 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3312 {
3313 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3314 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3315
3316 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3317 }
3318
3319 /* Return an affine expression that is equal to pwaff_true for elements
3320 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3321 * is zero.
3322 * That is, return cond ? pwaff_true : pwaff_false;
3323 *
3324 * If "cond" involves and NaN, then we conservatively return a NaN
3325 * on its entire domain. In principle, we could consider the pieces
3326 * where it is NaN separately from those where it is not.
3327 *
3328 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3329 * then only use the domain of "cond" to restrict the domain.
3330 */
isl_pw_aff_cond(__isl_take isl_pw_aff * cond,__isl_take isl_pw_aff * pwaff_true,__isl_take isl_pw_aff * pwaff_false)3331 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3332 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3333 {
3334 isl_set *cond_true, *cond_false;
3335 isl_bool equal;
3336
3337 if (!cond)
3338 goto error;
3339 if (isl_pw_aff_involves_nan(cond)) {
3340 isl_space *space = isl_pw_aff_get_domain_space(cond);
3341 isl_local_space *ls = isl_local_space_from_space(space);
3342 isl_pw_aff_free(cond);
3343 isl_pw_aff_free(pwaff_true);
3344 isl_pw_aff_free(pwaff_false);
3345 return isl_pw_aff_nan_on_domain(ls);
3346 }
3347
3348 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3349 isl_pw_aff_get_space(pwaff_false));
3350 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3351 isl_pw_aff_get_space(pwaff_true));
3352 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3353 if (equal < 0)
3354 goto error;
3355 if (equal) {
3356 isl_set *dom;
3357
3358 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3359 isl_pw_aff_free(pwaff_false);
3360 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3361 }
3362
3363 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3364 cond_false = isl_pw_aff_zero_set(cond);
3365 return isl_pw_aff_select(cond_true, pwaff_true,
3366 cond_false, pwaff_false);
3367 error:
3368 isl_pw_aff_free(cond);
3369 isl_pw_aff_free(pwaff_true);
3370 isl_pw_aff_free(pwaff_false);
3371 return NULL;
3372 }
3373
isl_aff_is_cst(__isl_keep isl_aff * aff)3374 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3375 {
3376 int pos;
3377
3378 if (!aff)
3379 return isl_bool_error;
3380
3381 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3382 return isl_bool_ok(pos == -1);
3383 }
3384
3385 /* Check whether pwaff is a piecewise constant.
3386 */
isl_pw_aff_is_cst(__isl_keep isl_pw_aff * pwaff)3387 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3388 {
3389 int i;
3390
3391 if (!pwaff)
3392 return isl_bool_error;
3393
3394 for (i = 0; i < pwaff->n; ++i) {
3395 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3396 if (is_cst < 0 || !is_cst)
3397 return is_cst;
3398 }
3399
3400 return isl_bool_true;
3401 }
3402
3403 /* Return the product of "aff1" and "aff2".
3404 *
3405 * If either of the two is NaN, then the result is NaN.
3406 *
3407 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3408 */
isl_aff_mul(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)3409 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3410 __isl_take isl_aff *aff2)
3411 {
3412 if (!aff1 || !aff2)
3413 goto error;
3414
3415 if (isl_aff_is_nan(aff1)) {
3416 isl_aff_free(aff2);
3417 return aff1;
3418 }
3419 if (isl_aff_is_nan(aff2)) {
3420 isl_aff_free(aff1);
3421 return aff2;
3422 }
3423
3424 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3425 return isl_aff_mul(aff2, aff1);
3426
3427 if (!isl_aff_is_cst(aff2))
3428 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3429 "at least one affine expression should be constant",
3430 goto error);
3431
3432 aff1 = isl_aff_cow(aff1);
3433 if (!aff1 || !aff2)
3434 goto error;
3435
3436 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3437 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3438
3439 isl_aff_free(aff2);
3440 return aff1;
3441 error:
3442 isl_aff_free(aff1);
3443 isl_aff_free(aff2);
3444 return NULL;
3445 }
3446
3447 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3448 *
3449 * If either of the two is NaN, then the result is NaN.
3450 * A division by zero also results in NaN.
3451 */
isl_aff_div(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)3452 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3453 __isl_take isl_aff *aff2)
3454 {
3455 isl_bool is_cst, is_zero;
3456 int neg;
3457
3458 if (!aff1 || !aff2)
3459 goto error;
3460
3461 if (isl_aff_is_nan(aff1)) {
3462 isl_aff_free(aff2);
3463 return aff1;
3464 }
3465 if (isl_aff_is_nan(aff2)) {
3466 isl_aff_free(aff1);
3467 return aff2;
3468 }
3469
3470 is_cst = isl_aff_is_cst(aff2);
3471 if (is_cst < 0)
3472 goto error;
3473 if (!is_cst)
3474 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3475 "second argument should be a constant", goto error);
3476 is_zero = isl_aff_plain_is_zero(aff2);
3477 if (is_zero < 0)
3478 goto error;
3479 if (is_zero)
3480 return set_nan_free(aff1, aff2);
3481
3482 neg = isl_int_is_neg(aff2->v->el[1]);
3483 if (neg) {
3484 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3485 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3486 }
3487
3488 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3489 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3490
3491 if (neg) {
3492 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3493 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3494 }
3495
3496 isl_aff_free(aff2);
3497 return aff1;
3498 error:
3499 isl_aff_free(aff1);
3500 isl_aff_free(aff2);
3501 return NULL;
3502 }
3503
isl_pw_aff_add(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3504 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3505 __isl_take isl_pw_aff *pwaff2)
3506 {
3507 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3508 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3509 }
3510
isl_pw_aff_union_add(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3511 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3512 __isl_take isl_pw_aff *pwaff2)
3513 {
3514 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3515 }
3516
isl_pw_aff_mul(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3517 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3518 __isl_take isl_pw_aff *pwaff2)
3519 {
3520 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3521 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3522 }
3523
3524 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3525 */
isl_pw_aff_div(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3526 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3527 __isl_take isl_pw_aff *pa2)
3528 {
3529 int is_cst;
3530
3531 is_cst = isl_pw_aff_is_cst(pa2);
3532 if (is_cst < 0)
3533 goto error;
3534 if (!is_cst)
3535 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3536 "second argument should be a piecewise constant",
3537 goto error);
3538 isl_pw_aff_align_params_bin(&pa1, &pa2);
3539 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3540 error:
3541 isl_pw_aff_free(pa1);
3542 isl_pw_aff_free(pa2);
3543 return NULL;
3544 }
3545
3546 /* Compute the quotient of the integer division of "pa1" by "pa2"
3547 * with rounding towards zero.
3548 * "pa2" is assumed to be a piecewise constant.
3549 *
3550 * In particular, return
3551 *
3552 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3553 *
3554 */
isl_pw_aff_tdiv_q(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3555 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3556 __isl_take isl_pw_aff *pa2)
3557 {
3558 int is_cst;
3559 isl_set *cond;
3560 isl_pw_aff *f, *c;
3561
3562 is_cst = isl_pw_aff_is_cst(pa2);
3563 if (is_cst < 0)
3564 goto error;
3565 if (!is_cst)
3566 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3567 "second argument should be a piecewise constant",
3568 goto error);
3569
3570 pa1 = isl_pw_aff_div(pa1, pa2);
3571
3572 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3573 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3574 c = isl_pw_aff_ceil(pa1);
3575 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3576 error:
3577 isl_pw_aff_free(pa1);
3578 isl_pw_aff_free(pa2);
3579 return NULL;
3580 }
3581
3582 /* Compute the remainder of the integer division of "pa1" by "pa2"
3583 * with rounding towards zero.
3584 * "pa2" is assumed to be a piecewise constant.
3585 *
3586 * In particular, return
3587 *
3588 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3589 *
3590 */
isl_pw_aff_tdiv_r(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3591 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3592 __isl_take isl_pw_aff *pa2)
3593 {
3594 int is_cst;
3595 isl_pw_aff *res;
3596
3597 is_cst = isl_pw_aff_is_cst(pa2);
3598 if (is_cst < 0)
3599 goto error;
3600 if (!is_cst)
3601 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3602 "second argument should be a piecewise constant",
3603 goto error);
3604 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3605 res = isl_pw_aff_mul(pa2, res);
3606 res = isl_pw_aff_sub(pa1, res);
3607 return res;
3608 error:
3609 isl_pw_aff_free(pa1);
3610 isl_pw_aff_free(pa2);
3611 return NULL;
3612 }
3613
3614 /* Does either of "pa1" or "pa2" involve any NaN2?
3615 */
either_involves_nan(__isl_keep isl_pw_aff * pa1,__isl_keep isl_pw_aff * pa2)3616 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3617 __isl_keep isl_pw_aff *pa2)
3618 {
3619 isl_bool has_nan;
3620
3621 has_nan = isl_pw_aff_involves_nan(pa1);
3622 if (has_nan < 0 || has_nan)
3623 return has_nan;
3624 return isl_pw_aff_involves_nan(pa2);
3625 }
3626
3627 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3628 * by a NaN on their shared domain.
3629 *
3630 * In principle, the result could be refined to only being NaN
3631 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3632 */
replace_by_nan(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2)3633 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3634 __isl_take isl_pw_aff *pa2)
3635 {
3636 isl_local_space *ls;
3637 isl_set *dom;
3638 isl_pw_aff *pa;
3639
3640 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3641 ls = isl_local_space_from_space(isl_set_get_space(dom));
3642 pa = isl_pw_aff_nan_on_domain(ls);
3643 pa = isl_pw_aff_intersect_domain(pa, dom);
3644
3645 return pa;
3646 }
3647
pw_aff_min(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3648 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3649 __isl_take isl_pw_aff *pwaff2)
3650 {
3651 isl_set *le;
3652 isl_set *dom;
3653
3654 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3655 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3656 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3657 isl_pw_aff_copy(pwaff2));
3658 dom = isl_set_subtract(dom, isl_set_copy(le));
3659 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3660 }
3661
pw_aff_max(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3662 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3663 __isl_take isl_pw_aff *pwaff2)
3664 {
3665 isl_set *ge;
3666 isl_set *dom;
3667
3668 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3669 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3670 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3671 isl_pw_aff_copy(pwaff2));
3672 dom = isl_set_subtract(dom, isl_set_copy(ge));
3673 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3674 }
3675
3676 /* Return an expression for the minimum (if "max" is not set) or
3677 * the maximum (if "max" is set) of "pa1" and "pa2".
3678 * If either expression involves any NaN, then return a NaN
3679 * on the shared domain as result.
3680 */
pw_aff_min_max(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2,int max)3681 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3682 __isl_take isl_pw_aff *pa2, int max)
3683 {
3684 isl_bool has_nan;
3685
3686 has_nan = either_involves_nan(pa1, pa2);
3687 if (has_nan < 0)
3688 pa1 = isl_pw_aff_free(pa1);
3689 else if (has_nan)
3690 return replace_by_nan(pa1, pa2);
3691
3692 isl_pw_aff_align_params_bin(&pa1, &pa2);
3693 if (max)
3694 return pw_aff_max(pa1, pa2);
3695 else
3696 return pw_aff_min(pa1, pa2);
3697 }
3698
3699 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3700 */
isl_pw_aff_min(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3701 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3702 __isl_take isl_pw_aff *pwaff2)
3703 {
3704 return pw_aff_min_max(pwaff1, pwaff2, 0);
3705 }
3706
3707 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3708 */
isl_pw_aff_max(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2)3709 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3710 __isl_take isl_pw_aff *pwaff2)
3711 {
3712 return pw_aff_min_max(pwaff1, pwaff2, 1);
3713 }
3714
pw_aff_list_reduce(__isl_take isl_pw_aff_list * list,__isl_give isl_pw_aff * (* fn)(__isl_take isl_pw_aff * pwaff1,__isl_take isl_pw_aff * pwaff2))3715 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3716 __isl_take isl_pw_aff_list *list,
3717 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3718 __isl_take isl_pw_aff *pwaff2))
3719 {
3720 int i;
3721 isl_ctx *ctx;
3722 isl_pw_aff *res;
3723
3724 if (!list)
3725 return NULL;
3726
3727 ctx = isl_pw_aff_list_get_ctx(list);
3728 if (list->n < 1)
3729 isl_die(ctx, isl_error_invalid,
3730 "list should contain at least one element", goto error);
3731
3732 res = isl_pw_aff_copy(list->p[0]);
3733 for (i = 1; i < list->n; ++i)
3734 res = fn(res, isl_pw_aff_copy(list->p[i]));
3735
3736 isl_pw_aff_list_free(list);
3737 return res;
3738 error:
3739 isl_pw_aff_list_free(list);
3740 return NULL;
3741 }
3742
3743 /* Return an isl_pw_aff that maps each element in the intersection of the
3744 * domains of the elements of list to the minimal corresponding affine
3745 * expression.
3746 */
isl_pw_aff_list_min(__isl_take isl_pw_aff_list * list)3747 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3748 {
3749 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3750 }
3751
3752 /* Return an isl_pw_aff that maps each element in the intersection of the
3753 * domains of the elements of list to the maximal corresponding affine
3754 * expression.
3755 */
isl_pw_aff_list_max(__isl_take isl_pw_aff_list * list)3756 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3757 {
3758 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3759 }
3760
3761 /* Mark the domains of "pwaff" as rational.
3762 */
isl_pw_aff_set_rational(__isl_take isl_pw_aff * pwaff)3763 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3764 {
3765 int i;
3766
3767 pwaff = isl_pw_aff_cow(pwaff);
3768 if (!pwaff)
3769 return NULL;
3770 if (pwaff->n == 0)
3771 return pwaff;
3772
3773 for (i = 0; i < pwaff->n; ++i) {
3774 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3775 if (!pwaff->p[i].set)
3776 return isl_pw_aff_free(pwaff);
3777 }
3778
3779 return pwaff;
3780 }
3781
3782 /* Mark the domains of the elements of "list" as rational.
3783 */
isl_pw_aff_list_set_rational(__isl_take isl_pw_aff_list * list)3784 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3785 __isl_take isl_pw_aff_list *list)
3786 {
3787 int i, n;
3788
3789 if (!list)
3790 return NULL;
3791 if (list->n == 0)
3792 return list;
3793
3794 n = list->n;
3795 for (i = 0; i < n; ++i) {
3796 isl_pw_aff *pa;
3797
3798 pa = isl_pw_aff_list_get_pw_aff(list, i);
3799 pa = isl_pw_aff_set_rational(pa);
3800 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3801 }
3802
3803 return list;
3804 }
3805
3806 /* Do the parameters of "aff" match those of "space"?
3807 */
isl_aff_matching_params(__isl_keep isl_aff * aff,__isl_keep isl_space * space)3808 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3809 __isl_keep isl_space *space)
3810 {
3811 isl_space *aff_space;
3812 isl_bool match;
3813
3814 if (!aff || !space)
3815 return isl_bool_error;
3816
3817 aff_space = isl_aff_get_domain_space(aff);
3818
3819 match = isl_space_has_equal_params(space, aff_space);
3820
3821 isl_space_free(aff_space);
3822 return match;
3823 }
3824
3825 /* Check that the domain space of "aff" matches "space".
3826 */
isl_aff_check_match_domain_space(__isl_keep isl_aff * aff,__isl_keep isl_space * space)3827 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3828 __isl_keep isl_space *space)
3829 {
3830 isl_space *aff_space;
3831 isl_bool match;
3832
3833 if (!aff || !space)
3834 return isl_stat_error;
3835
3836 aff_space = isl_aff_get_domain_space(aff);
3837
3838 match = isl_space_has_equal_params(space, aff_space);
3839 if (match < 0)
3840 goto error;
3841 if (!match)
3842 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3843 "parameters don't match", goto error);
3844 match = isl_space_tuple_is_equal(space, isl_dim_in,
3845 aff_space, isl_dim_set);
3846 if (match < 0)
3847 goto error;
3848 if (!match)
3849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3850 "domains don't match", goto error);
3851 isl_space_free(aff_space);
3852 return isl_stat_ok;
3853 error:
3854 isl_space_free(aff_space);
3855 return isl_stat_error;
3856 }
3857
3858 /* Return the shared (universe) domain of the elements of "ma".
3859 *
3860 * Since an isl_multi_aff (and an isl_aff) is always total,
3861 * the domain is always the universe set in its domain space.
3862 * This is a helper function for use in the generic isl_multi_*_bind.
3863 */
isl_multi_aff_domain(__isl_take isl_multi_aff * ma)3864 static __isl_give isl_basic_set *isl_multi_aff_domain(
3865 __isl_take isl_multi_aff *ma)
3866 {
3867 isl_space *space;
3868
3869 space = isl_multi_aff_get_space(ma);
3870 isl_multi_aff_free(ma);
3871
3872 return isl_basic_set_universe(isl_space_domain(space));
3873 }
3874
3875 #undef BASE
3876 #define BASE aff
3877
3878 #include <isl_multi_no_explicit_domain.c>
3879 #include <isl_multi_templ.c>
3880 #include <isl_multi_add_constant_templ.c>
3881 #include <isl_multi_apply_set.c>
3882 #include <isl_multi_arith_templ.c>
3883 #include <isl_multi_bind_domain_templ.c>
3884 #include <isl_multi_cmp.c>
3885 #include <isl_multi_dim_id_templ.c>
3886 #include <isl_multi_dims.c>
3887 #include <isl_multi_floor.c>
3888 #include <isl_multi_from_base_templ.c>
3889 #include <isl_multi_identity_templ.c>
3890 #include <isl_multi_insert_domain_templ.c>
3891 #include <isl_multi_locals_templ.c>
3892 #include <isl_multi_move_dims_templ.c>
3893 #include <isl_multi_nan_templ.c>
3894 #include <isl_multi_product_templ.c>
3895 #include <isl_multi_splice_templ.c>
3896 #include <isl_multi_tuple_id_templ.c>
3897 #include <isl_multi_unbind_params_templ.c>
3898 #include <isl_multi_zero_templ.c>
3899
3900 #undef DOMBASE
3901 #define DOMBASE set
3902 #include <isl_multi_gist.c>
3903
3904 #undef DOMBASE
3905 #define DOMBASE basic_set
3906 #include <isl_multi_bind_templ.c>
3907
3908 /* Construct an isl_multi_aff living in "space" that corresponds
3909 * to the affine transformation matrix "mat".
3910 */
isl_multi_aff_from_aff_mat(__isl_take isl_space * space,__isl_take isl_mat * mat)3911 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3912 __isl_take isl_space *space, __isl_take isl_mat *mat)
3913 {
3914 isl_ctx *ctx;
3915 isl_local_space *ls = NULL;
3916 isl_multi_aff *ma = NULL;
3917 isl_size n_row, n_col, n_out, total;
3918 int i;
3919
3920 if (!space || !mat)
3921 goto error;
3922
3923 ctx = isl_mat_get_ctx(mat);
3924
3925 n_row = isl_mat_rows(mat);
3926 n_col = isl_mat_cols(mat);
3927 n_out = isl_space_dim(space, isl_dim_out);
3928 total = isl_space_dim(space, isl_dim_all);
3929 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3930 goto error;
3931 if (n_row < 1)
3932 isl_die(ctx, isl_error_invalid,
3933 "insufficient number of rows", goto error);
3934 if (n_col < 1)
3935 isl_die(ctx, isl_error_invalid,
3936 "insufficient number of columns", goto error);
3937 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3938 isl_die(ctx, isl_error_invalid,
3939 "dimension mismatch", goto error);
3940
3941 ma = isl_multi_aff_zero(isl_space_copy(space));
3942 space = isl_space_domain(space);
3943 ls = isl_local_space_from_space(isl_space_copy(space));
3944
3945 for (i = 0; i < n_row - 1; ++i) {
3946 isl_vec *v;
3947 isl_aff *aff;
3948
3949 v = isl_vec_alloc(ctx, 1 + n_col);
3950 if (!v)
3951 goto error;
3952 isl_int_set(v->el[0], mat->row[0][0]);
3953 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3954 v = isl_vec_normalize(v);
3955 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3956 ma = isl_multi_aff_set_aff(ma, i, aff);
3957 }
3958
3959 isl_space_free(space);
3960 isl_local_space_free(ls);
3961 isl_mat_free(mat);
3962 return ma;
3963 error:
3964 isl_space_free(space);
3965 isl_local_space_free(ls);
3966 isl_mat_free(mat);
3967 isl_multi_aff_free(ma);
3968 return NULL;
3969 }
3970
3971 /* Return the constant terms of the affine expressions of "ma".
3972 */
isl_multi_aff_get_constant_multi_val(__isl_keep isl_multi_aff * ma)3973 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3974 __isl_keep isl_multi_aff *ma)
3975 {
3976 int i;
3977 isl_size n;
3978 isl_space *space;
3979 isl_multi_val *mv;
3980
3981 n = isl_multi_aff_size(ma);
3982 if (n < 0)
3983 return NULL;
3984 space = isl_space_range(isl_multi_aff_get_space(ma));
3985 space = isl_space_drop_all_params(space);
3986 mv = isl_multi_val_zero(space);
3987
3988 for (i = 0; i < n; ++i) {
3989 isl_aff *aff;
3990 isl_val *val;
3991
3992 aff = isl_multi_aff_get_at(ma, i);
3993 val = isl_aff_get_constant_val(aff);
3994 isl_aff_free(aff);
3995 mv = isl_multi_val_set_at(mv, i, val);
3996 }
3997
3998 return mv;
3999 }
4000
4001 /* Remove any internal structure of the domain of "ma".
4002 * If there is any such internal structure in the input,
4003 * then the name of the corresponding space is also removed.
4004 */
isl_multi_aff_flatten_domain(__isl_take isl_multi_aff * ma)4005 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4006 __isl_take isl_multi_aff *ma)
4007 {
4008 isl_space *space;
4009
4010 if (!ma)
4011 return NULL;
4012
4013 if (!ma->space->nested[0])
4014 return ma;
4015
4016 space = isl_multi_aff_get_space(ma);
4017 space = isl_space_flatten_domain(space);
4018 ma = isl_multi_aff_reset_space(ma, space);
4019
4020 return ma;
4021 }
4022
4023 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4024 * of the space to its domain.
4025 */
isl_multi_aff_domain_map(__isl_take isl_space * space)4026 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4027 {
4028 int i;
4029 isl_size n_in;
4030 isl_local_space *ls;
4031 isl_multi_aff *ma;
4032
4033 if (!space)
4034 return NULL;
4035 if (!isl_space_is_map(space))
4036 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4037 "not a map space", goto error);
4038
4039 n_in = isl_space_dim(space, isl_dim_in);
4040 if (n_in < 0)
4041 goto error;
4042 space = isl_space_domain_map(space);
4043
4044 ma = isl_multi_aff_alloc(isl_space_copy(space));
4045 if (n_in == 0) {
4046 isl_space_free(space);
4047 return ma;
4048 }
4049
4050 space = isl_space_domain(space);
4051 ls = isl_local_space_from_space(space);
4052 for (i = 0; i < n_in; ++i) {
4053 isl_aff *aff;
4054
4055 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4056 isl_dim_set, i);
4057 ma = isl_multi_aff_set_aff(ma, i, aff);
4058 }
4059 isl_local_space_free(ls);
4060 return ma;
4061 error:
4062 isl_space_free(space);
4063 return NULL;
4064 }
4065
4066 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4067 * of the space to its range.
4068 */
isl_multi_aff_range_map(__isl_take isl_space * space)4069 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4070 {
4071 int i;
4072 isl_size n_in, n_out;
4073 isl_local_space *ls;
4074 isl_multi_aff *ma;
4075
4076 if (!space)
4077 return NULL;
4078 if (!isl_space_is_map(space))
4079 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4080 "not a map space", goto error);
4081
4082 n_in = isl_space_dim(space, isl_dim_in);
4083 n_out = isl_space_dim(space, isl_dim_out);
4084 if (n_in < 0 || n_out < 0)
4085 goto error;
4086 space = isl_space_range_map(space);
4087
4088 ma = isl_multi_aff_alloc(isl_space_copy(space));
4089 if (n_out == 0) {
4090 isl_space_free(space);
4091 return ma;
4092 }
4093
4094 space = isl_space_domain(space);
4095 ls = isl_local_space_from_space(space);
4096 for (i = 0; i < n_out; ++i) {
4097 isl_aff *aff;
4098
4099 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4100 isl_dim_set, n_in + i);
4101 ma = isl_multi_aff_set_aff(ma, i, aff);
4102 }
4103 isl_local_space_free(ls);
4104 return ma;
4105 error:
4106 isl_space_free(space);
4107 return NULL;
4108 }
4109
4110 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4111 * of the space to its domain.
4112 */
isl_pw_multi_aff_domain_map(__isl_take isl_space * space)4113 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4114 __isl_take isl_space *space)
4115 {
4116 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4117 }
4118
4119 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4120 * of the space to its range.
4121 */
isl_pw_multi_aff_range_map(__isl_take isl_space * space)4122 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4123 __isl_take isl_space *space)
4124 {
4125 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4126 }
4127
4128 /* Given the space of a set and a range of set dimensions,
4129 * construct an isl_multi_aff that projects out those dimensions.
4130 */
isl_multi_aff_project_out_map(__isl_take isl_space * space,enum isl_dim_type type,unsigned first,unsigned n)4131 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4132 __isl_take isl_space *space, enum isl_dim_type type,
4133 unsigned first, unsigned n)
4134 {
4135 int i;
4136 isl_size dim;
4137 isl_local_space *ls;
4138 isl_multi_aff *ma;
4139
4140 if (!space)
4141 return NULL;
4142 if (!isl_space_is_set(space))
4143 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4144 "expecting set space", goto error);
4145 if (type != isl_dim_set)
4146 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4147 "only set dimensions can be projected out", goto error);
4148 if (isl_space_check_range(space, type, first, n) < 0)
4149 goto error;
4150
4151 dim = isl_space_dim(space, isl_dim_set);
4152 if (dim < 0)
4153 goto error;
4154
4155 space = isl_space_from_domain(space);
4156 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4157
4158 if (dim == n)
4159 return isl_multi_aff_alloc(space);
4160
4161 ma = isl_multi_aff_alloc(isl_space_copy(space));
4162 space = isl_space_domain(space);
4163 ls = isl_local_space_from_space(space);
4164
4165 for (i = 0; i < first; ++i) {
4166 isl_aff *aff;
4167
4168 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4169 isl_dim_set, i);
4170 ma = isl_multi_aff_set_aff(ma, i, aff);
4171 }
4172
4173 for (i = 0; i < dim - (first + n); ++i) {
4174 isl_aff *aff;
4175
4176 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4177 isl_dim_set, first + n + i);
4178 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4179 }
4180
4181 isl_local_space_free(ls);
4182 return ma;
4183 error:
4184 isl_space_free(space);
4185 return NULL;
4186 }
4187
4188 /* Given the space of a set and a range of set dimensions,
4189 * construct an isl_pw_multi_aff that projects out those dimensions.
4190 */
isl_pw_multi_aff_project_out_map(__isl_take isl_space * space,enum isl_dim_type type,unsigned first,unsigned n)4191 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4192 __isl_take isl_space *space, enum isl_dim_type type,
4193 unsigned first, unsigned n)
4194 {
4195 isl_multi_aff *ma;
4196
4197 ma = isl_multi_aff_project_out_map(space, type, first, n);
4198 return isl_pw_multi_aff_from_multi_aff(ma);
4199 }
4200
4201 /* Create a piecewise multi-affine expression in the given space that maps each
4202 * input dimension to the corresponding output dimension.
4203 */
isl_pw_multi_aff_identity(__isl_take isl_space * space)4204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4205 __isl_take isl_space *space)
4206 {
4207 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4208 }
4209
4210 /* Create a piecewise multi expression that maps elements in the given space
4211 * to themselves.
4212 */
isl_pw_multi_aff_identity_on_domain_space(__isl_take isl_space * space)4213 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4214 __isl_take isl_space *space)
4215 {
4216 isl_multi_aff *ma;
4217
4218 ma = isl_multi_aff_identity_on_domain_space(space);
4219 return isl_pw_multi_aff_from_multi_aff(ma);
4220 }
4221
4222 /* Exploit the equalities in "eq" to simplify the affine expressions.
4223 */
isl_multi_aff_substitute_equalities(__isl_take isl_multi_aff * maff,__isl_take isl_basic_set * eq)4224 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4225 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4226 {
4227 int i;
4228
4229 maff = isl_multi_aff_cow(maff);
4230 if (!maff || !eq)
4231 goto error;
4232
4233 for (i = 0; i < maff->n; ++i) {
4234 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4235 isl_basic_set_copy(eq));
4236 if (!maff->u.p[i])
4237 goto error;
4238 }
4239
4240 isl_basic_set_free(eq);
4241 return maff;
4242 error:
4243 isl_basic_set_free(eq);
4244 isl_multi_aff_free(maff);
4245 return NULL;
4246 }
4247
isl_multi_aff_scale(__isl_take isl_multi_aff * maff,isl_int f)4248 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4249 isl_int f)
4250 {
4251 int i;
4252
4253 maff = isl_multi_aff_cow(maff);
4254 if (!maff)
4255 return NULL;
4256
4257 for (i = 0; i < maff->n; ++i) {
4258 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4259 if (!maff->u.p[i])
4260 return isl_multi_aff_free(maff);
4261 }
4262
4263 return maff;
4264 }
4265
isl_multi_aff_add_on_domain(__isl_keep isl_set * dom,__isl_take isl_multi_aff * maff1,__isl_take isl_multi_aff * maff2)4266 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4267 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4268 {
4269 maff1 = isl_multi_aff_add(maff1, maff2);
4270 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4271 return maff1;
4272 }
4273
isl_multi_aff_is_empty(__isl_keep isl_multi_aff * maff)4274 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4275 {
4276 if (!maff)
4277 return isl_bool_error;
4278
4279 return isl_bool_false;
4280 }
4281
4282 /* Return the set of domain elements where "ma1" is lexicographically
4283 * smaller than or equal to "ma2".
4284 */
isl_multi_aff_lex_le_set(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2)4285 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4286 __isl_take isl_multi_aff *ma2)
4287 {
4288 return isl_multi_aff_lex_ge_set(ma2, ma1);
4289 }
4290
4291 /* Return the set of domain elements where "ma1" is lexicographically
4292 * smaller than "ma2".
4293 */
isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2)4294 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4295 __isl_take isl_multi_aff *ma2)
4296 {
4297 return isl_multi_aff_lex_gt_set(ma2, ma1);
4298 }
4299
4300 /* Return the set of domain elements where "ma1" is lexicographically
4301 * greater than to "ma2". If "equal" is set, then include the domain
4302 * elements where they are equal.
4303 * Do this for the case where there are no entries.
4304 * In this case, "ma1" cannot be greater than "ma2",
4305 * but it is (greater than or) equal to "ma2".
4306 */
isl_multi_aff_lex_gte_set_0d(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2,int equal)4307 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4308 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4309 {
4310 isl_space *space;
4311
4312 space = isl_multi_aff_get_domain_space(ma1);
4313
4314 isl_multi_aff_free(ma1);
4315 isl_multi_aff_free(ma2);
4316
4317 if (equal)
4318 return isl_set_universe(space);
4319 else
4320 return isl_set_empty(space);
4321 }
4322
4323 /* Return the set where entry "i" of "ma1" and "ma2"
4324 * satisfy the relation prescribed by "cmp".
4325 */
isl_multi_aff_order_at(__isl_keep isl_multi_aff * ma1,__isl_keep isl_multi_aff * ma2,int i,__isl_give isl_set * (* cmp)(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2))4326 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4327 __isl_keep isl_multi_aff *ma2, int i,
4328 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4329 __isl_take isl_aff *aff2))
4330 {
4331 isl_aff *aff1, *aff2;
4332
4333 aff1 = isl_multi_aff_get_at(ma1, i);
4334 aff2 = isl_multi_aff_get_at(ma2, i);
4335 return cmp(aff1, aff2);
4336 }
4337
4338 /* Return the set of domain elements where "ma1" is lexicographically
4339 * greater than to "ma2". If "equal" is set, then include the domain
4340 * elements where they are equal.
4341 *
4342 * In particular, for all but the final entry,
4343 * include the set of elements where this entry is strictly greater in "ma1"
4344 * and all previous entries are equal.
4345 * The final entry is also allowed to be equal in the two functions
4346 * if "equal" is set.
4347 *
4348 * The case where there are no entries is handled separately.
4349 */
isl_multi_aff_lex_gte_set(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2,int equal)4350 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4351 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4352 {
4353 int i;
4354 isl_size n;
4355 isl_space *space;
4356 isl_set *res;
4357 isl_set *equal_set;
4358 isl_set *gte;
4359
4360 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4361 goto error;
4362 n = isl_multi_aff_size(ma1);
4363 if (n < 0)
4364 goto error;
4365 if (n == 0)
4366 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4367
4368 space = isl_multi_aff_get_domain_space(ma1);
4369 res = isl_set_empty(isl_space_copy(space));
4370 equal_set = isl_set_universe(space);
4371
4372 for (i = 0; i + 1 < n; ++i) {
4373 isl_bool empty;
4374 isl_set *gt, *eq;
4375
4376 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4377 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4378 res = isl_set_union(res, gt);
4379 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4380 equal_set = isl_set_intersect(equal_set, eq);
4381
4382 empty = isl_set_is_empty(equal_set);
4383 if (empty >= 0 && empty)
4384 break;
4385 }
4386
4387 if (equal)
4388 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4389 else
4390 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4391 isl_multi_aff_free(ma1);
4392 isl_multi_aff_free(ma2);
4393
4394 gte = isl_set_intersect(gte, equal_set);
4395 return isl_set_union(res, gte);
4396 error:
4397 isl_multi_aff_free(ma1);
4398 isl_multi_aff_free(ma2);
4399 return NULL;
4400 }
4401
4402 /* Return the set of domain elements where "ma1" is lexicographically
4403 * greater than or equal to "ma2".
4404 */
isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2)4405 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4406 __isl_take isl_multi_aff *ma2)
4407 {
4408 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4409 }
4410
4411 /* Return the set of domain elements where "ma1" is lexicographically
4412 * greater than "ma2".
4413 */
isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2)4414 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4415 __isl_take isl_multi_aff *ma2)
4416 {
4417 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4418 }
4419
4420 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4421
4422 #undef PW
4423 #define PW isl_pw_multi_aff
4424 #undef BASE
4425 #define BASE multi_aff
4426 #undef EL_IS_ZERO
4427 #define EL_IS_ZERO is_empty
4428 #undef ZERO
4429 #define ZERO empty
4430 #undef IS_ZERO
4431 #define IS_ZERO is_empty
4432 #undef FIELD
4433 #define FIELD maff
4434 #undef DEFAULT_IS_ZERO
4435 #define DEFAULT_IS_ZERO 0
4436
4437 #include <isl_pw_templ.c>
4438 #include <isl_pw_add_constant_multi_val_templ.c>
4439 #include <isl_pw_add_constant_val_templ.c>
4440 #include <isl_pw_bind_domain_templ.c>
4441 #include <isl_pw_insert_dims_templ.c>
4442 #include <isl_pw_insert_domain_templ.c>
4443 #include <isl_pw_locals_templ.c>
4444 #include <isl_pw_move_dims_templ.c>
4445 #include <isl_pw_neg_templ.c>
4446 #include <isl_pw_pullback_templ.c>
4447 #include <isl_pw_union_opt.c>
4448
4449 #undef BASE
4450 #define BASE pw_multi_aff
4451
4452 #include <isl_union_multi.c>
4453 #include "isl_union_locals_templ.c"
4454 #include <isl_union_neg.c>
4455
4456 #undef BASE
4457 #define BASE multi_aff
4458
4459 #include <isl_union_pw_templ.c>
4460
4461 /* Generic function for extracting a factor from a product "pma".
4462 * "check_space" checks that the space is that of the right kind of product.
4463 * "space_factor" extracts the factor from the space.
4464 * "multi_aff_factor" extracts the factor from the constituent functions.
4465 */
pw_multi_aff_factor(__isl_take isl_pw_multi_aff * pma,isl_stat (* check_space)(__isl_keep isl_pw_multi_aff * pma),__isl_give isl_space * (* space_factor)(__isl_take isl_space * space),__isl_give isl_multi_aff * (* multi_aff_factor)(__isl_take isl_multi_aff * ma))4466 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4467 __isl_take isl_pw_multi_aff *pma,
4468 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4469 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4470 __isl_give isl_multi_aff *(*multi_aff_factor)(
4471 __isl_take isl_multi_aff *ma))
4472 {
4473 int i;
4474 isl_space *space;
4475
4476 if (check_space(pma) < 0)
4477 return isl_pw_multi_aff_free(pma);
4478
4479 space = isl_pw_multi_aff_take_space(pma);
4480 space = space_factor(space);
4481
4482 for (i = 0; pma && i < pma->n; ++i) {
4483 isl_multi_aff *ma;
4484
4485 ma = isl_pw_multi_aff_take_base_at(pma, i);
4486 ma = multi_aff_factor(ma);
4487 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4488 }
4489
4490 pma = isl_pw_multi_aff_restore_space(pma, space);
4491
4492 return pma;
4493 }
4494
4495 /* Is the range of "pma" a wrapped relation?
4496 */
isl_pw_multi_aff_range_is_wrapping(__isl_keep isl_pw_multi_aff * pma)4497 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4498 __isl_keep isl_pw_multi_aff *pma)
4499 {
4500 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4501 }
4502
4503 /* Check that the range of "pma" is a product.
4504 */
pw_multi_aff_check_range_product(__isl_keep isl_pw_multi_aff * pma)4505 static isl_stat pw_multi_aff_check_range_product(
4506 __isl_keep isl_pw_multi_aff *pma)
4507 {
4508 isl_bool wraps;
4509
4510 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4511 if (wraps < 0)
4512 return isl_stat_error;
4513 if (!wraps)
4514 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4515 "range is not a product", return isl_stat_error);
4516 return isl_stat_ok;
4517 }
4518
4519 /* Given a function A -> [B -> C], extract the function A -> B.
4520 */
isl_pw_multi_aff_range_factor_domain(__isl_take isl_pw_multi_aff * pma)4521 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4522 __isl_take isl_pw_multi_aff *pma)
4523 {
4524 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4525 &isl_space_range_factor_domain,
4526 &isl_multi_aff_range_factor_domain);
4527 }
4528
4529 /* Given a function A -> [B -> C], extract the function A -> C.
4530 */
isl_pw_multi_aff_range_factor_range(__isl_take isl_pw_multi_aff * pma)4531 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4532 __isl_take isl_pw_multi_aff *pma)
4533 {
4534 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4535 &isl_space_range_factor_range,
4536 &isl_multi_aff_range_factor_range);
4537 }
4538
4539 /* Given two piecewise multi affine expressions, return a piecewise
4540 * multi-affine expression defined on the union of the definition domains
4541 * of the inputs that is equal to the lexicographic maximum of the two
4542 * inputs on each cell. If only one of the two inputs is defined on
4543 * a given cell, then it is considered to be the maximum.
4544 */
isl_pw_multi_aff_union_lexmax(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4545 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4546 __isl_take isl_pw_multi_aff *pma1,
4547 __isl_take isl_pw_multi_aff *pma2)
4548 {
4549 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4550 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4551 &isl_multi_aff_lex_ge_set);
4552 }
4553
4554 /* Given two piecewise multi affine expressions, return a piecewise
4555 * multi-affine expression defined on the union of the definition domains
4556 * of the inputs that is equal to the lexicographic minimum of the two
4557 * inputs on each cell. If only one of the two inputs is defined on
4558 * a given cell, then it is considered to be the minimum.
4559 */
isl_pw_multi_aff_union_lexmin(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4560 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4561 __isl_take isl_pw_multi_aff *pma1,
4562 __isl_take isl_pw_multi_aff *pma2)
4563 {
4564 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4565 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4566 &isl_multi_aff_lex_le_set);
4567 }
4568
isl_pw_multi_aff_add(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4569 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4570 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4571 {
4572 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4573 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4574 &isl_multi_aff_add);
4575 }
4576
4577 /* Subtract "pma2" from "pma1" and return the result.
4578 */
isl_pw_multi_aff_sub(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4579 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4580 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4581 {
4582 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4583 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4584 &isl_multi_aff_sub);
4585 }
4586
isl_pw_multi_aff_union_add(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4587 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4588 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4589 {
4590 return isl_pw_multi_aff_union_add_(pma1, pma2);
4591 }
4592
4593 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4594 * with the actual sum on the shared domain and
4595 * the defined expression on the symmetric difference of the domains.
4596 */
isl_union_pw_aff_union_add(__isl_take isl_union_pw_aff * upa1,__isl_take isl_union_pw_aff * upa2)4597 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4598 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4599 {
4600 return isl_union_pw_aff_union_add_(upa1, upa2);
4601 }
4602
4603 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4604 * with the actual sum on the shared domain and
4605 * the defined expression on the symmetric difference of the domains.
4606 */
isl_union_pw_multi_aff_union_add(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)4607 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4608 __isl_take isl_union_pw_multi_aff *upma1,
4609 __isl_take isl_union_pw_multi_aff *upma2)
4610 {
4611 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4612 }
4613
4614 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4615 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4616 */
isl_pw_multi_aff_product(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)4617 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4618 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4619 {
4620 int i, j, n;
4621 isl_space *space;
4622 isl_pw_multi_aff *res;
4623
4624 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4625 goto error;
4626
4627 n = pma1->n * pma2->n;
4628 space = isl_space_product(isl_space_copy(pma1->dim),
4629 isl_space_copy(pma2->dim));
4630 res = isl_pw_multi_aff_alloc_size(space, n);
4631
4632 for (i = 0; i < pma1->n; ++i) {
4633 for (j = 0; j < pma2->n; ++j) {
4634 isl_set *domain;
4635 isl_multi_aff *ma;
4636
4637 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4638 isl_set_copy(pma2->p[j].set));
4639 ma = isl_multi_aff_product(
4640 isl_multi_aff_copy(pma1->p[i].maff),
4641 isl_multi_aff_copy(pma2->p[j].maff));
4642 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4643 }
4644 }
4645
4646 isl_pw_multi_aff_free(pma1);
4647 isl_pw_multi_aff_free(pma2);
4648 return res;
4649 error:
4650 isl_pw_multi_aff_free(pma1);
4651 isl_pw_multi_aff_free(pma2);
4652 return NULL;
4653 }
4654
4655 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4656 * denominator "denom".
4657 * "denom" is allowed to be negative, in which case the actual denominator
4658 * is -denom and the expressions are added instead.
4659 */
subtract_initial(__isl_take isl_aff * aff,__isl_keep isl_multi_aff * ma,int n,isl_int * c,isl_int denom)4660 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4661 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4662 {
4663 int i, first;
4664 int sign;
4665 isl_int d;
4666
4667 first = isl_seq_first_non_zero(c, n);
4668 if (first == -1)
4669 return aff;
4670
4671 sign = isl_int_sgn(denom);
4672 isl_int_init(d);
4673 isl_int_abs(d, denom);
4674 for (i = first; i < n; ++i) {
4675 isl_aff *aff_i;
4676
4677 if (isl_int_is_zero(c[i]))
4678 continue;
4679 aff_i = isl_multi_aff_get_aff(ma, i);
4680 aff_i = isl_aff_scale(aff_i, c[i]);
4681 aff_i = isl_aff_scale_down(aff_i, d);
4682 if (sign >= 0)
4683 aff = isl_aff_sub(aff, aff_i);
4684 else
4685 aff = isl_aff_add(aff, aff_i);
4686 }
4687 isl_int_clear(d);
4688
4689 return aff;
4690 }
4691
4692 /* Extract an affine expression that expresses the output dimension "pos"
4693 * of "bmap" in terms of the parameters and input dimensions from
4694 * equality "eq".
4695 * Note that this expression may involve integer divisions defined
4696 * in terms of parameters and input dimensions.
4697 * The equality may also involve references to earlier (but not later)
4698 * output dimensions. These are replaced by the corresponding elements
4699 * in "ma".
4700 *
4701 * If the equality is of the form
4702 *
4703 * f(i) + h(j) + a x + g(i) = 0,
4704 *
4705 * with f(i) a linear combinations of the parameters and input dimensions,
4706 * g(i) a linear combination of integer divisions defined in terms of the same
4707 * and h(j) a linear combinations of earlier output dimensions,
4708 * then the affine expression is
4709 *
4710 * (-f(i) - g(i))/a - h(j)/a
4711 *
4712 * If the equality is of the form
4713 *
4714 * f(i) + h(j) - a x + g(i) = 0,
4715 *
4716 * then the affine expression is
4717 *
4718 * (f(i) + g(i))/a - h(j)/(-a)
4719 *
4720 *
4721 * If "div" refers to an integer division (i.e., it is smaller than
4722 * the number of integer divisions), then the equality constraint
4723 * does involve an integer division (the one at position "div") that
4724 * is defined in terms of output dimensions. However, this integer
4725 * division can be eliminated by exploiting a pair of constraints
4726 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4727 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4728 * -l + x >= 0.
4729 * In particular, let
4730 *
4731 * x = e(i) + m floor(...)
4732 *
4733 * with e(i) the expression derived above and floor(...) the integer
4734 * division involving output dimensions.
4735 * From
4736 *
4737 * l <= x <= l + n,
4738 *
4739 * we have
4740 *
4741 * 0 <= x - l <= n
4742 *
4743 * This means
4744 *
4745 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4746 * = (e(i) - l) mod m
4747 *
4748 * Therefore,
4749 *
4750 * x - l = (e(i) - l) mod m
4751 *
4752 * or
4753 *
4754 * x = ((e(i) - l) mod m) + l
4755 *
4756 * The variable "shift" below contains the expression -l, which may
4757 * also involve a linear combination of earlier output dimensions.
4758 */
extract_aff_from_equality(__isl_keep isl_basic_map * bmap,int pos,int eq,int div,int ineq,__isl_keep isl_multi_aff * ma)4759 static __isl_give isl_aff *extract_aff_from_equality(
4760 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4761 __isl_keep isl_multi_aff *ma)
4762 {
4763 unsigned o_out;
4764 isl_size n_div, n_out;
4765 isl_ctx *ctx;
4766 isl_local_space *ls;
4767 isl_aff *aff, *shift;
4768 isl_val *mod;
4769
4770 ctx = isl_basic_map_get_ctx(bmap);
4771 ls = isl_basic_map_get_local_space(bmap);
4772 ls = isl_local_space_domain(ls);
4773 aff = isl_aff_alloc(isl_local_space_copy(ls));
4774 if (!aff)
4775 goto error;
4776 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4777 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4778 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4779 if (n_out < 0 || n_div < 0)
4780 goto error;
4781 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4782 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4783 isl_seq_cpy(aff->v->el + 1 + o_out,
4784 bmap->eq[eq] + o_out + n_out, n_div);
4785 } else {
4786 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4787 isl_seq_neg(aff->v->el + 1 + o_out,
4788 bmap->eq[eq] + o_out + n_out, n_div);
4789 }
4790 if (div < n_div)
4791 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4792 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4793 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4794 bmap->eq[eq][o_out + pos]);
4795 if (div < n_div) {
4796 shift = isl_aff_alloc(isl_local_space_copy(ls));
4797 if (!shift)
4798 goto error;
4799 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4800 isl_seq_cpy(shift->v->el + 1 + o_out,
4801 bmap->ineq[ineq] + o_out + n_out, n_div);
4802 isl_int_set_si(shift->v->el[0], 1);
4803 shift = subtract_initial(shift, ma, pos,
4804 bmap->ineq[ineq] + o_out, ctx->negone);
4805 aff = isl_aff_add(aff, isl_aff_copy(shift));
4806 mod = isl_val_int_from_isl_int(ctx,
4807 bmap->eq[eq][o_out + n_out + div]);
4808 mod = isl_val_abs(mod);
4809 aff = isl_aff_mod_val(aff, mod);
4810 aff = isl_aff_sub(aff, shift);
4811 }
4812
4813 isl_local_space_free(ls);
4814 return aff;
4815 error:
4816 isl_local_space_free(ls);
4817 isl_aff_free(aff);
4818 return NULL;
4819 }
4820
4821 /* Given a basic map with output dimensions defined
4822 * in terms of the parameters input dimensions and earlier
4823 * output dimensions using an equality (and possibly a pair on inequalities),
4824 * extract an isl_aff that expresses output dimension "pos" in terms
4825 * of the parameters and input dimensions.
4826 * Note that this expression may involve integer divisions defined
4827 * in terms of parameters and input dimensions.
4828 * "ma" contains the expressions corresponding to earlier output dimensions.
4829 *
4830 * This function shares some similarities with
4831 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4832 */
extract_isl_aff_from_basic_map(__isl_keep isl_basic_map * bmap,int pos,__isl_keep isl_multi_aff * ma)4833 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4834 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4835 {
4836 int eq, div, ineq;
4837 isl_aff *aff;
4838
4839 if (!bmap)
4840 return NULL;
4841 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4842 if (eq >= bmap->n_eq)
4843 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4844 "unable to find suitable equality", return NULL);
4845 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4846
4847 aff = isl_aff_remove_unused_divs(aff);
4848 return aff;
4849 }
4850
4851 /* Given a basic map where each output dimension is defined
4852 * in terms of the parameters and input dimensions using an equality,
4853 * extract an isl_multi_aff that expresses the output dimensions in terms
4854 * of the parameters and input dimensions.
4855 */
extract_isl_multi_aff_from_basic_map(__isl_take isl_basic_map * bmap)4856 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4857 __isl_take isl_basic_map *bmap)
4858 {
4859 int i;
4860 isl_size n_out;
4861 isl_multi_aff *ma;
4862
4863 if (!bmap)
4864 return NULL;
4865
4866 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4867 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4868 if (n_out < 0)
4869 ma = isl_multi_aff_free(ma);
4870
4871 for (i = 0; i < n_out; ++i) {
4872 isl_aff *aff;
4873
4874 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4875 ma = isl_multi_aff_set_aff(ma, i, aff);
4876 }
4877
4878 isl_basic_map_free(bmap);
4879
4880 return ma;
4881 }
4882
4883 /* Given a basic set where each set dimension is defined
4884 * in terms of the parameters using an equality,
4885 * extract an isl_multi_aff that expresses the set dimensions in terms
4886 * of the parameters.
4887 */
isl_multi_aff_from_basic_set_equalities(__isl_take isl_basic_set * bset)4888 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4889 __isl_take isl_basic_set *bset)
4890 {
4891 return extract_isl_multi_aff_from_basic_map(bset);
4892 }
4893
4894 /* Create an isl_pw_multi_aff that is equivalent to
4895 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4896 * The given basic map is such that each output dimension is defined
4897 * in terms of the parameters and input dimensions using an equality.
4898 *
4899 * Since some applications expect the result of isl_pw_multi_aff_from_map
4900 * to only contain integer affine expressions, we compute the floor
4901 * of the expression before returning.
4902 *
4903 * Remove all constraints involving local variables without
4904 * an explicit representation (resulting in the removal of those
4905 * local variables) prior to the actual extraction to ensure
4906 * that the local spaces in which the resulting affine expressions
4907 * are created do not contain any unknown local variables.
4908 * Removing such constraints is safe because constraints involving
4909 * unknown local variables are not used to determine whether
4910 * a basic map is obviously single-valued.
4911 */
plain_pw_multi_aff_from_map(__isl_take isl_set * domain,__isl_take isl_basic_map * bmap)4912 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4913 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4914 {
4915 isl_multi_aff *ma;
4916
4917 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4918 ma = extract_isl_multi_aff_from_basic_map(bmap);
4919 ma = isl_multi_aff_floor(ma);
4920 return isl_pw_multi_aff_alloc(domain, ma);
4921 }
4922
4923 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4924 * This obviously only works if the input "map" is single-valued.
4925 * If so, we compute the lexicographic minimum of the image in the form
4926 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4927 * to its lexicographic minimum.
4928 * If the input is not single-valued, we produce an error.
4929 */
pw_multi_aff_from_map_base(__isl_take isl_map * map)4930 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4931 __isl_take isl_map *map)
4932 {
4933 int i;
4934 int sv;
4935 isl_pw_multi_aff *pma;
4936
4937 sv = isl_map_is_single_valued(map);
4938 if (sv < 0)
4939 goto error;
4940 if (!sv)
4941 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4942 "map is not single-valued", goto error);
4943 map = isl_map_make_disjoint(map);
4944 if (!map)
4945 return NULL;
4946
4947 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4948
4949 for (i = 0; i < map->n; ++i) {
4950 isl_pw_multi_aff *pma_i;
4951 isl_basic_map *bmap;
4952 bmap = isl_basic_map_copy(map->p[i]);
4953 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4954 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4955 }
4956
4957 isl_map_free(map);
4958 return pma;
4959 error:
4960 isl_map_free(map);
4961 return NULL;
4962 }
4963
4964 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4965 * taking into account that the output dimension at position "d"
4966 * can be represented as
4967 *
4968 * x = floor((e(...) + c1) / m)
4969 *
4970 * given that constraint "i" is of the form
4971 *
4972 * e(...) + c1 - m x >= 0
4973 *
4974 *
4975 * Let "map" be of the form
4976 *
4977 * A -> B
4978 *
4979 * We construct a mapping
4980 *
4981 * A -> [A -> x = floor(...)]
4982 *
4983 * apply that to the map, obtaining
4984 *
4985 * [A -> x = floor(...)] -> B
4986 *
4987 * and equate dimension "d" to x.
4988 * We then compute a isl_pw_multi_aff representation of the resulting map
4989 * and plug in the mapping above.
4990 */
pw_multi_aff_from_map_div(__isl_take isl_map * map,__isl_take isl_basic_map * hull,int d,int i)4991 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4992 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4993 {
4994 isl_ctx *ctx;
4995 isl_space *space = NULL;
4996 isl_local_space *ls;
4997 isl_multi_aff *ma;
4998 isl_aff *aff;
4999 isl_vec *v;
5000 isl_map *insert;
5001 int offset;
5002 isl_size n;
5003 isl_size n_in;
5004 isl_pw_multi_aff *pma;
5005 isl_bool is_set;
5006
5007 is_set = isl_map_is_set(map);
5008 if (is_set < 0)
5009 goto error;
5010
5011 offset = isl_basic_map_offset(hull, isl_dim_out);
5012 ctx = isl_map_get_ctx(map);
5013 space = isl_space_domain(isl_map_get_space(map));
5014 n_in = isl_space_dim(space, isl_dim_set);
5015 n = isl_space_dim(space, isl_dim_all);
5016 if (n_in < 0 || n < 0)
5017 goto error;
5018
5019 v = isl_vec_alloc(ctx, 1 + 1 + n);
5020 if (v) {
5021 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5022 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5023 }
5024 isl_basic_map_free(hull);
5025
5026 ls = isl_local_space_from_space(isl_space_copy(space));
5027 aff = isl_aff_alloc_vec(ls, v);
5028 aff = isl_aff_floor(aff);
5029 if (is_set) {
5030 isl_space_free(space);
5031 ma = isl_multi_aff_from_aff(aff);
5032 } else {
5033 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5034 ma = isl_multi_aff_range_product(ma,
5035 isl_multi_aff_from_aff(aff));
5036 }
5037
5038 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5039 map = isl_map_apply_domain(map, insert);
5040 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5041 pma = isl_pw_multi_aff_from_map(map);
5042 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5043
5044 return pma;
5045 error:
5046 isl_space_free(space);
5047 isl_map_free(map);
5048 isl_basic_map_free(hull);
5049 return NULL;
5050 }
5051
5052 /* Is constraint "c" of the form
5053 *
5054 * e(...) + c1 - m x >= 0
5055 *
5056 * or
5057 *
5058 * -e(...) + c2 + m x >= 0
5059 *
5060 * where m > 1 and e only depends on parameters and input dimemnsions?
5061 *
5062 * "offset" is the offset of the output dimensions
5063 * "pos" is the position of output dimension x.
5064 */
is_potential_div_constraint(isl_int * c,int offset,int d,int total)5065 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5066 {
5067 if (isl_int_is_zero(c[offset + d]))
5068 return 0;
5069 if (isl_int_is_one(c[offset + d]))
5070 return 0;
5071 if (isl_int_is_negone(c[offset + d]))
5072 return 0;
5073 if (isl_seq_first_non_zero(c + offset, d) != -1)
5074 return 0;
5075 if (isl_seq_first_non_zero(c + offset + d + 1,
5076 total - (offset + d + 1)) != -1)
5077 return 0;
5078 return 1;
5079 }
5080
5081 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5082 *
5083 * As a special case, we first check if there is any pair of constraints,
5084 * shared by all the basic maps in "map" that force a given dimension
5085 * to be equal to the floor of some affine combination of the input dimensions.
5086 *
5087 * In particular, if we can find two constraints
5088 *
5089 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5090 *
5091 * and
5092 *
5093 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5094 *
5095 * where m > 1 and e only depends on parameters and input dimemnsions,
5096 * and such that
5097 *
5098 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5099 *
5100 * then we know that we can take
5101 *
5102 * x = floor((e(...) + c1) / m)
5103 *
5104 * without having to perform any computation.
5105 *
5106 * Note that we know that
5107 *
5108 * c1 + c2 >= 1
5109 *
5110 * If c1 + c2 were 0, then we would have detected an equality during
5111 * simplification. If c1 + c2 were negative, then we would have detected
5112 * a contradiction.
5113 */
pw_multi_aff_from_map_check_div(__isl_take isl_map * map)5114 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5115 __isl_take isl_map *map)
5116 {
5117 int d;
5118 isl_size dim;
5119 int i, j, n;
5120 int offset;
5121 isl_size total;
5122 isl_int sum;
5123 isl_basic_map *hull;
5124
5125 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5126 dim = isl_map_dim(map, isl_dim_out);
5127 total = isl_basic_map_dim(hull, isl_dim_all);
5128 if (dim < 0 || total < 0)
5129 goto error;
5130
5131 isl_int_init(sum);
5132 offset = isl_basic_map_offset(hull, isl_dim_out);
5133 n = hull->n_ineq;
5134 for (d = 0; d < dim; ++d) {
5135 for (i = 0; i < n; ++i) {
5136 if (!is_potential_div_constraint(hull->ineq[i],
5137 offset, d, 1 + total))
5138 continue;
5139 for (j = i + 1; j < n; ++j) {
5140 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5141 hull->ineq[j] + 1, total))
5142 continue;
5143 isl_int_add(sum, hull->ineq[i][0],
5144 hull->ineq[j][0]);
5145 if (isl_int_abs_lt(sum,
5146 hull->ineq[i][offset + d]))
5147 break;
5148
5149 }
5150 if (j >= n)
5151 continue;
5152 isl_int_clear(sum);
5153 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5154 j = i;
5155 return pw_multi_aff_from_map_div(map, hull, d, j);
5156 }
5157 }
5158 isl_int_clear(sum);
5159 isl_basic_map_free(hull);
5160 return pw_multi_aff_from_map_base(map);
5161 error:
5162 isl_map_free(map);
5163 isl_basic_map_free(hull);
5164 return NULL;
5165 }
5166
5167 /* Given an affine expression
5168 *
5169 * [A -> B] -> f(A,B)
5170 *
5171 * construct an isl_multi_aff
5172 *
5173 * [A -> B] -> B'
5174 *
5175 * such that dimension "d" in B' is set to "aff" and the remaining
5176 * dimensions are set equal to the corresponding dimensions in B.
5177 * "n_in" is the dimension of the space A.
5178 * "n_out" is the dimension of the space B.
5179 *
5180 * If "is_set" is set, then the affine expression is of the form
5181 *
5182 * [B] -> f(B)
5183 *
5184 * and we construct an isl_multi_aff
5185 *
5186 * B -> B'
5187 */
range_map(__isl_take isl_aff * aff,int d,unsigned n_in,unsigned n_out,int is_set)5188 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5189 unsigned n_in, unsigned n_out, int is_set)
5190 {
5191 int i;
5192 isl_multi_aff *ma;
5193 isl_space *space, *space2;
5194 isl_local_space *ls;
5195
5196 space = isl_aff_get_domain_space(aff);
5197 ls = isl_local_space_from_space(isl_space_copy(space));
5198 space2 = isl_space_copy(space);
5199 if (!is_set)
5200 space2 = isl_space_range(isl_space_unwrap(space2));
5201 space = isl_space_map_from_domain_and_range(space, space2);
5202 ma = isl_multi_aff_alloc(space);
5203 ma = isl_multi_aff_set_aff(ma, d, aff);
5204
5205 for (i = 0; i < n_out; ++i) {
5206 if (i == d)
5207 continue;
5208 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5209 isl_dim_set, n_in + i);
5210 ma = isl_multi_aff_set_aff(ma, i, aff);
5211 }
5212
5213 isl_local_space_free(ls);
5214
5215 return ma;
5216 }
5217
5218 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5219 * taking into account that the dimension at position "d" can be written as
5220 *
5221 * x = m a + f(..) (1)
5222 *
5223 * where m is equal to "gcd".
5224 * "i" is the index of the equality in "hull" that defines f(..).
5225 * In particular, the equality is of the form
5226 *
5227 * f(..) - x + m g(existentials) = 0
5228 *
5229 * or
5230 *
5231 * -f(..) + x + m g(existentials) = 0
5232 *
5233 * We basically plug (1) into "map", resulting in a map with "a"
5234 * in the range instead of "x". The corresponding isl_pw_multi_aff
5235 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5236 *
5237 * Specifically, given the input map
5238 *
5239 * A -> B
5240 *
5241 * We first wrap it into a set
5242 *
5243 * [A -> B]
5244 *
5245 * and define (1) on top of the corresponding space, resulting in "aff".
5246 * We use this to create an isl_multi_aff that maps the output position "d"
5247 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5248 * We plug this into the wrapped map, unwrap the result and compute the
5249 * corresponding isl_pw_multi_aff.
5250 * The result is an expression
5251 *
5252 * A -> T(A)
5253 *
5254 * We adjust that to
5255 *
5256 * A -> [A -> T(A)]
5257 *
5258 * so that we can plug that into "aff", after extending the latter to
5259 * a mapping
5260 *
5261 * [A -> B] -> B'
5262 *
5263 *
5264 * If "map" is actually a set, then there is no "A" space, meaning
5265 * that we do not need to perform any wrapping, and that the result
5266 * of the recursive call is of the form
5267 *
5268 * [T]
5269 *
5270 * which is plugged into a mapping of the form
5271 *
5272 * B -> B'
5273 */
pw_multi_aff_from_map_stride(__isl_take isl_map * map,__isl_take isl_basic_map * hull,int d,int i,isl_int gcd)5274 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5275 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5276 isl_int gcd)
5277 {
5278 isl_set *set;
5279 isl_space *space;
5280 isl_local_space *ls;
5281 isl_aff *aff;
5282 isl_multi_aff *ma;
5283 isl_pw_multi_aff *pma, *id;
5284 isl_size n_in;
5285 unsigned o_out;
5286 isl_size n_out;
5287 isl_bool is_set;
5288
5289 is_set = isl_map_is_set(map);
5290 if (is_set < 0)
5291 goto error;
5292
5293 n_in = isl_basic_map_dim(hull, isl_dim_in);
5294 n_out = isl_basic_map_dim(hull, isl_dim_out);
5295 if (n_in < 0 || n_out < 0)
5296 goto error;
5297 o_out = isl_basic_map_offset(hull, isl_dim_out);
5298
5299 if (is_set)
5300 set = map;
5301 else
5302 set = isl_map_wrap(map);
5303 space = isl_space_map_from_set(isl_set_get_space(set));
5304 ma = isl_multi_aff_identity(space);
5305 ls = isl_local_space_from_space(isl_set_get_space(set));
5306 aff = isl_aff_alloc(ls);
5307 if (aff) {
5308 isl_int_set_si(aff->v->el[0], 1);
5309 if (isl_int_is_one(hull->eq[i][o_out + d]))
5310 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5311 aff->v->size - 1);
5312 else
5313 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5314 aff->v->size - 1);
5315 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5316 }
5317 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5318 set = isl_set_preimage_multi_aff(set, ma);
5319
5320 ma = range_map(aff, d, n_in, n_out, is_set);
5321
5322 if (is_set)
5323 map = set;
5324 else
5325 map = isl_set_unwrap(set);
5326 pma = isl_pw_multi_aff_from_map(map);
5327
5328 if (!is_set) {
5329 space = isl_pw_multi_aff_get_domain_space(pma);
5330 space = isl_space_map_from_set(space);
5331 id = isl_pw_multi_aff_identity(space);
5332 pma = isl_pw_multi_aff_range_product(id, pma);
5333 }
5334 id = isl_pw_multi_aff_from_multi_aff(ma);
5335 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5336
5337 isl_basic_map_free(hull);
5338 return pma;
5339 error:
5340 isl_map_free(map);
5341 isl_basic_map_free(hull);
5342 return NULL;
5343 }
5344
5345 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5346 * "hull" contains the equalities valid for "map".
5347 *
5348 * Check if any of the output dimensions is "strided".
5349 * That is, we check if it can be written as
5350 *
5351 * x = m a + f(..)
5352 *
5353 * with m greater than 1, a some combination of existentially quantified
5354 * variables and f an expression in the parameters and input dimensions.
5355 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5356 *
5357 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5358 * special case.
5359 */
pw_multi_aff_from_map_check_strides(__isl_take isl_map * map,__isl_take isl_basic_map * hull)5360 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5361 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5362 {
5363 int i, j;
5364 isl_size n_out;
5365 unsigned o_out;
5366 isl_size n_div;
5367 unsigned o_div;
5368 isl_int gcd;
5369
5370 n_div = isl_basic_map_dim(hull, isl_dim_div);
5371 n_out = isl_basic_map_dim(hull, isl_dim_out);
5372 if (n_div < 0 || n_out < 0)
5373 goto error;
5374
5375 if (n_div == 0) {
5376 isl_basic_map_free(hull);
5377 return pw_multi_aff_from_map_check_div(map);
5378 }
5379
5380 isl_int_init(gcd);
5381
5382 o_div = isl_basic_map_offset(hull, isl_dim_div);
5383 o_out = isl_basic_map_offset(hull, isl_dim_out);
5384
5385 for (i = 0; i < n_out; ++i) {
5386 for (j = 0; j < hull->n_eq; ++j) {
5387 isl_int *eq = hull->eq[j];
5388 isl_pw_multi_aff *res;
5389
5390 if (!isl_int_is_one(eq[o_out + i]) &&
5391 !isl_int_is_negone(eq[o_out + i]))
5392 continue;
5393 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5394 continue;
5395 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5396 n_out - (i + 1)) != -1)
5397 continue;
5398 isl_seq_gcd(eq + o_div, n_div, &gcd);
5399 if (isl_int_is_zero(gcd))
5400 continue;
5401 if (isl_int_is_one(gcd))
5402 continue;
5403
5404 res = pw_multi_aff_from_map_stride(map, hull,
5405 i, j, gcd);
5406 isl_int_clear(gcd);
5407 return res;
5408 }
5409 }
5410
5411 isl_int_clear(gcd);
5412 isl_basic_map_free(hull);
5413 return pw_multi_aff_from_map_check_div(map);
5414 error:
5415 isl_map_free(map);
5416 isl_basic_map_free(hull);
5417 return NULL;
5418 }
5419
5420 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5421 *
5422 * As a special case, we first check if all output dimensions are uniquely
5423 * defined in terms of the parameters and input dimensions over the entire
5424 * domain. If so, we extract the desired isl_pw_multi_aff directly
5425 * from the affine hull of "map" and its domain.
5426 *
5427 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5428 * special cases.
5429 */
isl_pw_multi_aff_from_map(__isl_take isl_map * map)5430 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5431 {
5432 isl_bool sv;
5433 isl_size n;
5434 isl_basic_map *hull;
5435
5436 n = isl_map_n_basic_map(map);
5437 if (n < 0)
5438 goto error;
5439
5440 if (n == 1) {
5441 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5442 hull = isl_basic_map_plain_affine_hull(hull);
5443 sv = isl_basic_map_plain_is_single_valued(hull);
5444 if (sv >= 0 && sv)
5445 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5446 hull);
5447 isl_basic_map_free(hull);
5448 }
5449 map = isl_map_detect_equalities(map);
5450 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5451 sv = isl_basic_map_plain_is_single_valued(hull);
5452 if (sv >= 0 && sv)
5453 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5454 if (sv >= 0)
5455 return pw_multi_aff_from_map_check_strides(map, hull);
5456 isl_basic_map_free(hull);
5457 error:
5458 isl_map_free(map);
5459 return NULL;
5460 }
5461
isl_pw_multi_aff_from_set(__isl_take isl_set * set)5462 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5463 {
5464 return isl_pw_multi_aff_from_map(set);
5465 }
5466
5467 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5468 * add it to *user.
5469 */
pw_multi_aff_from_map(__isl_take isl_map * map,void * user)5470 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5471 {
5472 isl_union_pw_multi_aff **upma = user;
5473 isl_pw_multi_aff *pma;
5474
5475 pma = isl_pw_multi_aff_from_map(map);
5476 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5477
5478 return *upma ? isl_stat_ok : isl_stat_error;
5479 }
5480
5481 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5482 * domain.
5483 */
isl_union_pw_multi_aff_from_aff(__isl_take isl_aff * aff)5484 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5485 __isl_take isl_aff *aff)
5486 {
5487 isl_multi_aff *ma;
5488 isl_pw_multi_aff *pma;
5489
5490 ma = isl_multi_aff_from_aff(aff);
5491 pma = isl_pw_multi_aff_from_multi_aff(ma);
5492 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5493 }
5494
5495 /* Try and create an isl_union_pw_multi_aff that is equivalent
5496 * to the given isl_union_map.
5497 * The isl_union_map is required to be single-valued in each space.
5498 * Otherwise, an error is produced.
5499 */
isl_union_pw_multi_aff_from_union_map(__isl_take isl_union_map * umap)5500 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5501 __isl_take isl_union_map *umap)
5502 {
5503 isl_space *space;
5504 isl_union_pw_multi_aff *upma;
5505
5506 space = isl_union_map_get_space(umap);
5507 upma = isl_union_pw_multi_aff_empty(space);
5508 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5509 upma = isl_union_pw_multi_aff_free(upma);
5510 isl_union_map_free(umap);
5511
5512 return upma;
5513 }
5514
5515 /* Try and create an isl_union_pw_multi_aff that is equivalent
5516 * to the given isl_union_set.
5517 * The isl_union_set is required to be a singleton in each space.
5518 * Otherwise, an error is produced.
5519 */
isl_union_pw_multi_aff_from_union_set(__isl_take isl_union_set * uset)5520 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5521 __isl_take isl_union_set *uset)
5522 {
5523 return isl_union_pw_multi_aff_from_union_map(uset);
5524 }
5525
5526 /* Return the piecewise affine expression "set ? 1 : 0".
5527 */
isl_set_indicator_function(__isl_take isl_set * set)5528 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5529 {
5530 isl_pw_aff *pa;
5531 isl_space *space = isl_set_get_space(set);
5532 isl_local_space *ls = isl_local_space_from_space(space);
5533 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5534 isl_aff *one = isl_aff_zero_on_domain(ls);
5535
5536 one = isl_aff_add_constant_si(one, 1);
5537 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5538 set = isl_set_complement(set);
5539 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5540
5541 return pa;
5542 }
5543
5544 /* Plug in "subs" for dimension "type", "pos" of "aff".
5545 *
5546 * Let i be the dimension to replace and let "subs" be of the form
5547 *
5548 * f/d
5549 *
5550 * and "aff" of the form
5551 *
5552 * (a i + g)/m
5553 *
5554 * The result is
5555 *
5556 * (a f + d g')/(m d)
5557 *
5558 * where g' is the result of plugging in "subs" in each of the integer
5559 * divisions in g.
5560 */
isl_aff_substitute(__isl_take isl_aff * aff,enum isl_dim_type type,unsigned pos,__isl_keep isl_aff * subs)5561 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5562 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5563 {
5564 isl_ctx *ctx;
5565 isl_int v;
5566 isl_size n_div;
5567
5568 aff = isl_aff_cow(aff);
5569 if (!aff || !subs)
5570 return isl_aff_free(aff);
5571
5572 ctx = isl_aff_get_ctx(aff);
5573 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5574 isl_die(ctx, isl_error_invalid,
5575 "spaces don't match", return isl_aff_free(aff));
5576 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5577 if (n_div < 0)
5578 return isl_aff_free(aff);
5579 if (n_div != 0)
5580 isl_die(ctx, isl_error_unsupported,
5581 "cannot handle divs yet", return isl_aff_free(aff));
5582
5583 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5584 if (!aff->ls)
5585 return isl_aff_free(aff);
5586
5587 aff->v = isl_vec_cow(aff->v);
5588 if (!aff->v)
5589 return isl_aff_free(aff);
5590
5591 pos += isl_local_space_offset(aff->ls, type);
5592
5593 isl_int_init(v);
5594 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5595 aff->v->size, subs->v->size, v);
5596 isl_int_clear(v);
5597
5598 return aff;
5599 }
5600
5601 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5602 * expressions in "maff".
5603 */
isl_multi_aff_substitute(__isl_take isl_multi_aff * maff,enum isl_dim_type type,unsigned pos,__isl_keep isl_aff * subs)5604 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5605 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5606 __isl_keep isl_aff *subs)
5607 {
5608 int i;
5609
5610 maff = isl_multi_aff_cow(maff);
5611 if (!maff || !subs)
5612 return isl_multi_aff_free(maff);
5613
5614 if (type == isl_dim_in)
5615 type = isl_dim_set;
5616
5617 for (i = 0; i < maff->n; ++i) {
5618 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5619 type, pos, subs);
5620 if (!maff->u.p[i])
5621 return isl_multi_aff_free(maff);
5622 }
5623
5624 return maff;
5625 }
5626
5627 /* Plug in "subs" for dimension "type", "pos" of "pma".
5628 *
5629 * pma is of the form
5630 *
5631 * A_i(v) -> M_i(v)
5632 *
5633 * while subs is of the form
5634 *
5635 * v' = B_j(v) -> S_j
5636 *
5637 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5638 * has a contribution in the result, in particular
5639 *
5640 * C_ij(S_j) -> M_i(S_j)
5641 *
5642 * Note that plugging in S_j in C_ij may also result in an empty set
5643 * and this contribution should simply be discarded.
5644 */
isl_pw_multi_aff_substitute(__isl_take isl_pw_multi_aff * pma,enum isl_dim_type type,unsigned pos,__isl_keep isl_pw_aff * subs)5645 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5646 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5647 __isl_keep isl_pw_aff *subs)
5648 {
5649 int i, j, n;
5650 isl_pw_multi_aff *res;
5651
5652 if (!pma || !subs)
5653 return isl_pw_multi_aff_free(pma);
5654
5655 n = pma->n * subs->n;
5656 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5657
5658 for (i = 0; i < pma->n; ++i) {
5659 for (j = 0; j < subs->n; ++j) {
5660 isl_set *common;
5661 isl_multi_aff *res_ij;
5662 int empty;
5663
5664 common = isl_set_intersect(
5665 isl_set_copy(pma->p[i].set),
5666 isl_set_copy(subs->p[j].set));
5667 common = isl_set_substitute(common,
5668 type, pos, subs->p[j].aff);
5669 empty = isl_set_plain_is_empty(common);
5670 if (empty < 0 || empty) {
5671 isl_set_free(common);
5672 if (empty < 0)
5673 goto error;
5674 continue;
5675 }
5676
5677 res_ij = isl_multi_aff_substitute(
5678 isl_multi_aff_copy(pma->p[i].maff),
5679 type, pos, subs->p[j].aff);
5680
5681 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5682 }
5683 }
5684
5685 isl_pw_multi_aff_free(pma);
5686 return res;
5687 error:
5688 isl_pw_multi_aff_free(pma);
5689 isl_pw_multi_aff_free(res);
5690 return NULL;
5691 }
5692
5693 /* Compute the preimage of a range of dimensions in the affine expression "src"
5694 * under "ma" and put the result in "dst". The number of dimensions in "src"
5695 * that precede the range is given by "n_before". The number of dimensions
5696 * in the range is given by the number of output dimensions of "ma".
5697 * The number of dimensions that follow the range is given by "n_after".
5698 * If "has_denom" is set (to one),
5699 * then "src" and "dst" have an extra initial denominator.
5700 * "n_div_ma" is the number of existentials in "ma"
5701 * "n_div_bset" is the number of existentials in "src"
5702 * The resulting "dst" (which is assumed to have been allocated by
5703 * the caller) contains coefficients for both sets of existentials,
5704 * first those in "ma" and then those in "src".
5705 * f, c1, c2 and g are temporary objects that have been initialized
5706 * by the caller.
5707 *
5708 * Let src represent the expression
5709 *
5710 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5711 *
5712 * and let ma represent the expressions
5713 *
5714 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5715 *
5716 * We start out with the following expression for dst:
5717 *
5718 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5719 *
5720 * with the multiplication factor f initially equal to 1
5721 * and f \sum_i b_i v_i kept separately.
5722 * For each x_i that we substitute, we multiply the numerator
5723 * (and denominator) of dst by c_1 = m_i and add the numerator
5724 * of the x_i expression multiplied by c_2 = f b_i,
5725 * after removing the common factors of c_1 and c_2.
5726 * The multiplication factor f also needs to be multiplied by c_1
5727 * for the next x_j, j > i.
5728 */
isl_seq_preimage(isl_int * dst,isl_int * src,__isl_keep isl_multi_aff * ma,int n_before,int n_after,int n_div_ma,int n_div_bmap,isl_int f,isl_int c1,isl_int c2,isl_int g,int has_denom)5729 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5730 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5731 int n_div_ma, int n_div_bmap,
5732 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5733 {
5734 int i;
5735 isl_size n_param, n_in, n_out;
5736 int o_dst, o_src;
5737
5738 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5739 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5740 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5741 if (n_param < 0 || n_in < 0 || n_out < 0)
5742 return isl_stat_error;
5743
5744 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5745 o_dst = o_src = has_denom + 1 + n_param + n_before;
5746 isl_seq_clr(dst + o_dst, n_in);
5747 o_dst += n_in;
5748 o_src += n_out;
5749 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5750 o_dst += n_after;
5751 o_src += n_after;
5752 isl_seq_clr(dst + o_dst, n_div_ma);
5753 o_dst += n_div_ma;
5754 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5755
5756 isl_int_set_si(f, 1);
5757
5758 for (i = 0; i < n_out; ++i) {
5759 int offset = has_denom + 1 + n_param + n_before + i;
5760
5761 if (isl_int_is_zero(src[offset]))
5762 continue;
5763 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5764 isl_int_mul(c2, f, src[offset]);
5765 isl_int_gcd(g, c1, c2);
5766 isl_int_divexact(c1, c1, g);
5767 isl_int_divexact(c2, c2, g);
5768
5769 isl_int_mul(f, f, c1);
5770 o_dst = has_denom;
5771 o_src = 1;
5772 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5773 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5774 o_dst += 1 + n_param;
5775 o_src += 1 + n_param;
5776 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5777 o_dst += n_before;
5778 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5779 c2, ma->u.p[i]->v->el + o_src, n_in);
5780 o_dst += n_in;
5781 o_src += n_in;
5782 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5783 o_dst += n_after;
5784 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5785 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5786 o_dst += n_div_ma;
5787 o_src += n_div_ma;
5788 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5789 if (has_denom)
5790 isl_int_mul(dst[0], dst[0], c1);
5791 }
5792
5793 return isl_stat_ok;
5794 }
5795
5796 /* Compute the pullback of "aff" by the function represented by "ma".
5797 * In other words, plug in "ma" in "aff". The result is an affine expression
5798 * defined over the domain space of "ma".
5799 *
5800 * If "aff" is represented by
5801 *
5802 * (a(p) + b x + c(divs))/d
5803 *
5804 * and ma is represented by
5805 *
5806 * x = D(p) + F(y) + G(divs')
5807 *
5808 * then the result is
5809 *
5810 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5811 *
5812 * The divs in the local space of the input are similarly adjusted
5813 * through a call to isl_local_space_preimage_multi_aff.
5814 */
isl_aff_pullback_multi_aff(__isl_take isl_aff * aff,__isl_take isl_multi_aff * ma)5815 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5816 __isl_take isl_multi_aff *ma)
5817 {
5818 isl_aff *res = NULL;
5819 isl_local_space *ls;
5820 isl_size n_div_aff, n_div_ma;
5821 isl_int f, c1, c2, g;
5822
5823 ma = isl_multi_aff_align_divs(ma);
5824 if (!aff || !ma)
5825 goto error;
5826
5827 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5828 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5829 if (n_div_aff < 0 || n_div_ma < 0)
5830 goto error;
5831
5832 ls = isl_aff_get_domain_local_space(aff);
5833 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5834 res = isl_aff_alloc(ls);
5835 if (!res)
5836 goto error;
5837
5838 isl_int_init(f);
5839 isl_int_init(c1);
5840 isl_int_init(c2);
5841 isl_int_init(g);
5842
5843 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5844 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5845 res = isl_aff_free(res);
5846
5847 isl_int_clear(f);
5848 isl_int_clear(c1);
5849 isl_int_clear(c2);
5850 isl_int_clear(g);
5851
5852 isl_aff_free(aff);
5853 isl_multi_aff_free(ma);
5854 res = isl_aff_normalize(res);
5855 return res;
5856 error:
5857 isl_aff_free(aff);
5858 isl_multi_aff_free(ma);
5859 isl_aff_free(res);
5860 return NULL;
5861 }
5862
5863 /* Compute the pullback of "aff1" by the function represented by "aff2".
5864 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5865 * defined over the domain space of "aff1".
5866 *
5867 * The domain of "aff1" should match the range of "aff2", which means
5868 * that it should be single-dimensional.
5869 */
isl_aff_pullback_aff(__isl_take isl_aff * aff1,__isl_take isl_aff * aff2)5870 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5871 __isl_take isl_aff *aff2)
5872 {
5873 isl_multi_aff *ma;
5874
5875 ma = isl_multi_aff_from_aff(aff2);
5876 return isl_aff_pullback_multi_aff(aff1, ma);
5877 }
5878
5879 /* Compute the pullback of "ma1" by the function represented by "ma2".
5880 * In other words, plug in "ma2" in "ma1".
5881 */
isl_multi_aff_pullback_multi_aff(__isl_take isl_multi_aff * ma1,__isl_take isl_multi_aff * ma2)5882 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5883 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5884 {
5885 int i;
5886 isl_space *space = NULL;
5887
5888 isl_multi_aff_align_params_bin(&ma1, &ma2);
5889 ma2 = isl_multi_aff_align_divs(ma2);
5890 ma1 = isl_multi_aff_cow(ma1);
5891 if (!ma1 || !ma2)
5892 goto error;
5893
5894 space = isl_space_join(isl_multi_aff_get_space(ma2),
5895 isl_multi_aff_get_space(ma1));
5896
5897 for (i = 0; i < ma1->n; ++i) {
5898 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5899 isl_multi_aff_copy(ma2));
5900 if (!ma1->u.p[i])
5901 goto error;
5902 }
5903
5904 ma1 = isl_multi_aff_reset_space(ma1, space);
5905 isl_multi_aff_free(ma2);
5906 return ma1;
5907 error:
5908 isl_space_free(space);
5909 isl_multi_aff_free(ma2);
5910 isl_multi_aff_free(ma1);
5911 return NULL;
5912 }
5913
5914 /* Extend the local space of "dst" to include the divs
5915 * in the local space of "src".
5916 *
5917 * If "src" does not have any divs or if the local spaces of "dst" and
5918 * "src" are the same, then no extension is required.
5919 */
isl_aff_align_divs(__isl_take isl_aff * dst,__isl_keep isl_aff * src)5920 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5921 __isl_keep isl_aff *src)
5922 {
5923 isl_ctx *ctx;
5924 isl_size src_n_div, dst_n_div;
5925 int *exp1 = NULL;
5926 int *exp2 = NULL;
5927 isl_bool equal;
5928 isl_mat *div;
5929
5930 if (!src || !dst)
5931 return isl_aff_free(dst);
5932
5933 ctx = isl_aff_get_ctx(src);
5934 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5935 if (equal < 0)
5936 return isl_aff_free(dst);
5937 if (!equal)
5938 isl_die(ctx, isl_error_invalid,
5939 "spaces don't match", goto error);
5940
5941 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5942 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5943 if (src_n_div == 0)
5944 return dst;
5945 equal = isl_local_space_is_equal(src->ls, dst->ls);
5946 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5947 return isl_aff_free(dst);
5948 if (equal)
5949 return dst;
5950
5951 exp1 = isl_alloc_array(ctx, int, src_n_div);
5952 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5953 if (!exp1 || (dst_n_div && !exp2))
5954 goto error;
5955
5956 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5957 dst = isl_aff_expand_divs(dst, div, exp2);
5958 free(exp1);
5959 free(exp2);
5960
5961 return dst;
5962 error:
5963 free(exp1);
5964 free(exp2);
5965 return isl_aff_free(dst);
5966 }
5967
5968 /* Adjust the local spaces of the affine expressions in "maff"
5969 * such that they all have the save divs.
5970 */
isl_multi_aff_align_divs(__isl_take isl_multi_aff * maff)5971 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5972 __isl_take isl_multi_aff *maff)
5973 {
5974 int i;
5975
5976 if (!maff)
5977 return NULL;
5978 if (maff->n == 0)
5979 return maff;
5980 maff = isl_multi_aff_cow(maff);
5981 if (!maff)
5982 return NULL;
5983
5984 for (i = 1; i < maff->n; ++i)
5985 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5986 for (i = 1; i < maff->n; ++i) {
5987 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5988 if (!maff->u.p[i])
5989 return isl_multi_aff_free(maff);
5990 }
5991
5992 return maff;
5993 }
5994
isl_aff_lift(__isl_take isl_aff * aff)5995 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5996 {
5997 aff = isl_aff_cow(aff);
5998 if (!aff)
5999 return NULL;
6000
6001 aff->ls = isl_local_space_lift(aff->ls);
6002 if (!aff->ls)
6003 return isl_aff_free(aff);
6004
6005 return aff;
6006 }
6007
6008 /* Lift "maff" to a space with extra dimensions such that the result
6009 * has no more existentially quantified variables.
6010 * If "ls" is not NULL, then *ls is assigned the local space that lies
6011 * at the basis of the lifting applied to "maff".
6012 */
isl_multi_aff_lift(__isl_take isl_multi_aff * maff,__isl_give isl_local_space ** ls)6013 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6014 __isl_give isl_local_space **ls)
6015 {
6016 int i;
6017 isl_space *space;
6018 isl_size n_div;
6019
6020 if (ls)
6021 *ls = NULL;
6022
6023 if (!maff)
6024 return NULL;
6025
6026 if (maff->n == 0) {
6027 if (ls) {
6028 isl_space *space = isl_multi_aff_get_domain_space(maff);
6029 *ls = isl_local_space_from_space(space);
6030 if (!*ls)
6031 return isl_multi_aff_free(maff);
6032 }
6033 return maff;
6034 }
6035
6036 maff = isl_multi_aff_cow(maff);
6037 maff = isl_multi_aff_align_divs(maff);
6038 if (!maff)
6039 return NULL;
6040
6041 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6042 if (n_div < 0)
6043 return isl_multi_aff_free(maff);
6044 space = isl_multi_aff_get_space(maff);
6045 space = isl_space_lift(isl_space_domain(space), n_div);
6046 space = isl_space_extend_domain_with_range(space,
6047 isl_multi_aff_get_space(maff));
6048 if (!space)
6049 return isl_multi_aff_free(maff);
6050 isl_space_free(maff->space);
6051 maff->space = space;
6052
6053 if (ls) {
6054 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6055 if (!*ls)
6056 return isl_multi_aff_free(maff);
6057 }
6058
6059 for (i = 0; i < maff->n; ++i) {
6060 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6061 if (!maff->u.p[i])
6062 goto error;
6063 }
6064
6065 return maff;
6066 error:
6067 if (ls)
6068 isl_local_space_free(*ls);
6069 return isl_multi_aff_free(maff);
6070 }
6071
6072 #undef TYPE
6073 #define TYPE isl_pw_multi_aff
6074 static
6075 #include "check_type_range_templ.c"
6076
6077 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6078 */
isl_pw_multi_aff_get_pw_aff(__isl_keep isl_pw_multi_aff * pma,int pos)6079 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6080 __isl_keep isl_pw_multi_aff *pma, int pos)
6081 {
6082 int i;
6083 isl_size n_out;
6084 isl_space *space;
6085 isl_pw_aff *pa;
6086
6087 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6088 return NULL;
6089
6090 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6091 if (n_out < 0)
6092 return NULL;
6093
6094 space = isl_pw_multi_aff_get_space(pma);
6095 space = isl_space_drop_dims(space, isl_dim_out,
6096 pos + 1, n_out - pos - 1);
6097 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6098
6099 pa = isl_pw_aff_alloc_size(space, pma->n);
6100 for (i = 0; i < pma->n; ++i) {
6101 isl_aff *aff;
6102 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6103 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6104 }
6105
6106 return pa;
6107 }
6108
6109 /* Return an isl_pw_multi_aff with the given "set" as domain and
6110 * an unnamed zero-dimensional range.
6111 */
isl_pw_multi_aff_from_domain(__isl_take isl_set * set)6112 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6113 __isl_take isl_set *set)
6114 {
6115 isl_multi_aff *ma;
6116 isl_space *space;
6117
6118 space = isl_set_get_space(set);
6119 space = isl_space_from_domain(space);
6120 ma = isl_multi_aff_zero(space);
6121 return isl_pw_multi_aff_alloc(set, ma);
6122 }
6123
6124 /* Add an isl_pw_multi_aff with the given "set" as domain and
6125 * an unnamed zero-dimensional range to *user.
6126 */
add_pw_multi_aff_from_domain(__isl_take isl_set * set,void * user)6127 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6128 void *user)
6129 {
6130 isl_union_pw_multi_aff **upma = user;
6131 isl_pw_multi_aff *pma;
6132
6133 pma = isl_pw_multi_aff_from_domain(set);
6134 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6135
6136 return isl_stat_ok;
6137 }
6138
6139 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6140 * an unnamed zero-dimensional range.
6141 */
isl_union_pw_multi_aff_from_domain(__isl_take isl_union_set * uset)6142 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6143 __isl_take isl_union_set *uset)
6144 {
6145 isl_space *space;
6146 isl_union_pw_multi_aff *upma;
6147
6148 if (!uset)
6149 return NULL;
6150
6151 space = isl_union_set_get_space(uset);
6152 upma = isl_union_pw_multi_aff_empty(space);
6153
6154 if (isl_union_set_foreach_set(uset,
6155 &add_pw_multi_aff_from_domain, &upma) < 0)
6156 goto error;
6157
6158 isl_union_set_free(uset);
6159 return upma;
6160 error:
6161 isl_union_set_free(uset);
6162 isl_union_pw_multi_aff_free(upma);
6163 return NULL;
6164 }
6165
6166 /* Local data for bin_entry and the callback "fn".
6167 */
6168 struct isl_union_pw_multi_aff_bin_data {
6169 isl_union_pw_multi_aff *upma2;
6170 isl_union_pw_multi_aff *res;
6171 isl_pw_multi_aff *pma;
6172 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6173 };
6174
6175 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6176 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6177 */
bin_entry(__isl_take isl_pw_multi_aff * pma,void * user)6178 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6179 {
6180 struct isl_union_pw_multi_aff_bin_data *data = user;
6181 isl_stat r;
6182
6183 data->pma = pma;
6184 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6185 data->fn, data);
6186 isl_pw_multi_aff_free(pma);
6187
6188 return r;
6189 }
6190
6191 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6192 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6193 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6194 * as *entry. The callback should adjust data->res if desired.
6195 */
bin_op(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2,isl_stat (* fn)(__isl_take isl_pw_multi_aff * pma,void * user))6196 static __isl_give isl_union_pw_multi_aff *bin_op(
6197 __isl_take isl_union_pw_multi_aff *upma1,
6198 __isl_take isl_union_pw_multi_aff *upma2,
6199 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6200 {
6201 isl_space *space;
6202 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6203
6204 space = isl_union_pw_multi_aff_get_space(upma2);
6205 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6206 space = isl_union_pw_multi_aff_get_space(upma1);
6207 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6208
6209 if (!upma1 || !upma2)
6210 goto error;
6211
6212 data.upma2 = upma2;
6213 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6214 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6215 &bin_entry, &data) < 0)
6216 goto error;
6217
6218 isl_union_pw_multi_aff_free(upma1);
6219 isl_union_pw_multi_aff_free(upma2);
6220 return data.res;
6221 error:
6222 isl_union_pw_multi_aff_free(upma1);
6223 isl_union_pw_multi_aff_free(upma2);
6224 isl_union_pw_multi_aff_free(data.res);
6225 return NULL;
6226 }
6227
6228 /* Given two isl_pw_multi_affs A -> B and C -> D,
6229 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6230 */
isl_pw_multi_aff_range_product(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)6231 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6232 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6233 {
6234 isl_space *space;
6235
6236 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6237 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6238 isl_pw_multi_aff_get_space(pma2));
6239 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6240 &isl_multi_aff_range_product);
6241 }
6242
6243 /* Given two isl_pw_multi_affs A -> B and C -> D,
6244 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6245 */
isl_pw_multi_aff_flat_range_product(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)6246 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6247 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6248 {
6249 isl_space *space;
6250
6251 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6252 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6253 isl_pw_multi_aff_get_space(pma2));
6254 space = isl_space_flatten_range(space);
6255 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6256 &isl_multi_aff_flat_range_product);
6257 }
6258
6259 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6260 * to compute some form of range product and add the result to data->res.
6261 */
gen_range_product_entry(__isl_take isl_pw_multi_aff * pma2,__isl_give isl_pw_multi_aff * (* range_product)(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2),void * user)6262 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6263 __isl_give isl_pw_multi_aff *(*range_product)(
6264 __isl_take isl_pw_multi_aff *pma1,
6265 __isl_take isl_pw_multi_aff *pma2),
6266 void *user)
6267 {
6268 struct isl_union_pw_multi_aff_bin_data *data = user;
6269 isl_bool match;
6270 isl_space *space1, *space2;
6271
6272 space1 = isl_pw_multi_aff_peek_space(data->pma);
6273 space2 = isl_pw_multi_aff_peek_space(pma2);
6274 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6275 space2, isl_dim_in);
6276 if (match < 0 || !match) {
6277 isl_pw_multi_aff_free(pma2);
6278 return match < 0 ? isl_stat_error : isl_stat_ok;
6279 }
6280
6281 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6282
6283 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6284
6285 return isl_stat_ok;
6286 }
6287
6288 /* If data->pma and "pma2" have the same domain space, then compute
6289 * their flat range product and add the result to data->res.
6290 */
flat_range_product_entry(__isl_take isl_pw_multi_aff * pma2,void * user)6291 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6292 void *user)
6293 {
6294 return gen_range_product_entry(pma2,
6295 &isl_pw_multi_aff_flat_range_product, user);
6296 }
6297
6298 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6299 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6300 */
isl_union_pw_multi_aff_flat_range_product(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)6301 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6302 __isl_take isl_union_pw_multi_aff *upma1,
6303 __isl_take isl_union_pw_multi_aff *upma2)
6304 {
6305 return bin_op(upma1, upma2, &flat_range_product_entry);
6306 }
6307
6308 /* If data->pma and "pma2" have the same domain space, then compute
6309 * their range product and add the result to data->res.
6310 */
range_product_entry(__isl_take isl_pw_multi_aff * pma2,void * user)6311 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6312 void *user)
6313 {
6314 return gen_range_product_entry(pma2,
6315 &isl_pw_multi_aff_range_product, user);
6316 }
6317
6318 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6319 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6320 */
isl_union_pw_multi_aff_range_product(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)6321 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6322 __isl_take isl_union_pw_multi_aff *upma1,
6323 __isl_take isl_union_pw_multi_aff *upma2)
6324 {
6325 return bin_op(upma1, upma2, &range_product_entry);
6326 }
6327
6328 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6329 * The parameters are assumed to have been aligned.
6330 *
6331 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6332 * except that it works on two different isl_pw_* types.
6333 */
pw_multi_aff_set_pw_aff(__isl_take isl_pw_multi_aff * pma,unsigned pos,__isl_take isl_pw_aff * pa)6334 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6335 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6336 __isl_take isl_pw_aff *pa)
6337 {
6338 int i, j, n;
6339 isl_pw_multi_aff *res = NULL;
6340
6341 if (!pma || !pa)
6342 goto error;
6343
6344 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6345 pa->dim, isl_dim_in))
6346 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6347 "domains don't match", goto error);
6348 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6349 goto error;
6350
6351 n = pma->n * pa->n;
6352 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6353
6354 for (i = 0; i < pma->n; ++i) {
6355 for (j = 0; j < pa->n; ++j) {
6356 isl_set *common;
6357 isl_multi_aff *res_ij;
6358 int empty;
6359
6360 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6361 isl_set_copy(pa->p[j].set));
6362 empty = isl_set_plain_is_empty(common);
6363 if (empty < 0 || empty) {
6364 isl_set_free(common);
6365 if (empty < 0)
6366 goto error;
6367 continue;
6368 }
6369
6370 res_ij = isl_multi_aff_set_aff(
6371 isl_multi_aff_copy(pma->p[i].maff), pos,
6372 isl_aff_copy(pa->p[j].aff));
6373 res_ij = isl_multi_aff_gist(res_ij,
6374 isl_set_copy(common));
6375
6376 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6377 }
6378 }
6379
6380 isl_pw_multi_aff_free(pma);
6381 isl_pw_aff_free(pa);
6382 return res;
6383 error:
6384 isl_pw_multi_aff_free(pma);
6385 isl_pw_aff_free(pa);
6386 return isl_pw_multi_aff_free(res);
6387 }
6388
6389 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6390 */
isl_pw_multi_aff_set_pw_aff(__isl_take isl_pw_multi_aff * pma,unsigned pos,__isl_take isl_pw_aff * pa)6391 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6392 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6393 __isl_take isl_pw_aff *pa)
6394 {
6395 isl_bool equal_params;
6396
6397 if (!pma || !pa)
6398 goto error;
6399 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6400 if (equal_params < 0)
6401 goto error;
6402 if (equal_params)
6403 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6404 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6405 isl_pw_aff_check_named_params(pa) < 0)
6406 goto error;
6407 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6408 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6409 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6410 error:
6411 isl_pw_multi_aff_free(pma);
6412 isl_pw_aff_free(pa);
6413 return NULL;
6414 }
6415
6416 /* Do the parameters of "pa" match those of "space"?
6417 */
isl_pw_aff_matching_params(__isl_keep isl_pw_aff * pa,__isl_keep isl_space * space)6418 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6419 __isl_keep isl_space *space)
6420 {
6421 isl_space *pa_space;
6422 isl_bool match;
6423
6424 if (!pa || !space)
6425 return isl_bool_error;
6426
6427 pa_space = isl_pw_aff_get_space(pa);
6428
6429 match = isl_space_has_equal_params(space, pa_space);
6430
6431 isl_space_free(pa_space);
6432 return match;
6433 }
6434
6435 /* Check that the domain space of "pa" matches "space".
6436 */
isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff * pa,__isl_keep isl_space * space)6437 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6438 __isl_keep isl_space *space)
6439 {
6440 isl_space *pa_space;
6441 isl_bool match;
6442
6443 if (!pa || !space)
6444 return isl_stat_error;
6445
6446 pa_space = isl_pw_aff_get_space(pa);
6447
6448 match = isl_space_has_equal_params(space, pa_space);
6449 if (match < 0)
6450 goto error;
6451 if (!match)
6452 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6453 "parameters don't match", goto error);
6454 match = isl_space_tuple_is_equal(space, isl_dim_in,
6455 pa_space, isl_dim_in);
6456 if (match < 0)
6457 goto error;
6458 if (!match)
6459 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6460 "domains don't match", goto error);
6461 isl_space_free(pa_space);
6462 return isl_stat_ok;
6463 error:
6464 isl_space_free(pa_space);
6465 return isl_stat_error;
6466 }
6467
6468 #undef BASE
6469 #define BASE pw_aff
6470 #undef DOMBASE
6471 #define DOMBASE set
6472
6473 #include <isl_multi_explicit_domain.c>
6474 #include <isl_multi_pw_aff_explicit_domain.c>
6475 #include <isl_multi_templ.c>
6476 #include <isl_multi_add_constant_templ.c>
6477 #include <isl_multi_apply_set.c>
6478 #include <isl_multi_arith_templ.c>
6479 #include <isl_multi_bind_templ.c>
6480 #include <isl_multi_bind_domain_templ.c>
6481 #include <isl_multi_coalesce.c>
6482 #include <isl_multi_domain_templ.c>
6483 #include <isl_multi_dim_id_templ.c>
6484 #include <isl_multi_dims.c>
6485 #include <isl_multi_from_base_templ.c>
6486 #include <isl_multi_gist.c>
6487 #include <isl_multi_hash.c>
6488 #include <isl_multi_identity_templ.c>
6489 #include <isl_multi_align_set.c>
6490 #include <isl_multi_insert_domain_templ.c>
6491 #include <isl_multi_intersect.c>
6492 #include <isl_multi_min_max_templ.c>
6493 #include <isl_multi_move_dims_templ.c>
6494 #include <isl_multi_nan_templ.c>
6495 #include <isl_multi_param_templ.c>
6496 #include <isl_multi_product_templ.c>
6497 #include <isl_multi_splice_templ.c>
6498 #include <isl_multi_tuple_id_templ.c>
6499 #include <isl_multi_union_add_templ.c>
6500 #include <isl_multi_zero_templ.c>
6501 #include <isl_multi_unbind_params_templ.c>
6502
6503 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6504 * with this explicit domain.
6505 */
isl_map_intersect_multi_pw_aff_explicit_domain(__isl_take isl_map * map,__isl_keep isl_multi_pw_aff * mpa)6506 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6507 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6508 {
6509 isl_set *dom;
6510
6511 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6512 return map;
6513
6514 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6515 map = isl_map_intersect_domain(map, dom);
6516
6517 return map;
6518 }
6519
6520 /* Are all elements of "mpa" piecewise constants?
6521 */
isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff * mpa)6522 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6523 {
6524 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6525 }
6526
6527 /* Does "mpa" have a non-trivial explicit domain?
6528 *
6529 * The explicit domain, if present, is trivial if it represents
6530 * an (obviously) universe set.
6531 */
isl_multi_pw_aff_has_non_trivial_domain(__isl_keep isl_multi_pw_aff * mpa)6532 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6533 __isl_keep isl_multi_pw_aff *mpa)
6534 {
6535 if (!mpa)
6536 return isl_bool_error;
6537 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6538 return isl_bool_false;
6539 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6540 }
6541
6542 #undef BASE
6543 #define BASE set
6544
6545 #include "isl_opt_mpa_templ.c"
6546
6547 /* Compute the minima of the set dimensions as a function of the
6548 * parameters, but independently of the other set dimensions.
6549 */
isl_set_min_multi_pw_aff(__isl_take isl_set * set)6550 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6551 {
6552 return set_opt_mpa(set, &isl_set_dim_min);
6553 }
6554
6555 /* Compute the maxima of the set dimensions as a function of the
6556 * parameters, but independently of the other set dimensions.
6557 */
isl_set_max_multi_pw_aff(__isl_take isl_set * set)6558 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6559 {
6560 return set_opt_mpa(set, &isl_set_dim_max);
6561 }
6562
6563 #undef BASE
6564 #define BASE map
6565
6566 #include "isl_opt_mpa_templ.c"
6567
6568 /* Compute the minima of the output dimensions as a function of the
6569 * parameters and input dimensions, but independently of
6570 * the other output dimensions.
6571 */
isl_map_min_multi_pw_aff(__isl_take isl_map * map)6572 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6573 {
6574 return map_opt_mpa(map, &isl_map_dim_min);
6575 }
6576
6577 /* Compute the maxima of the output dimensions as a function of the
6578 * parameters and input dimensions, but independently of
6579 * the other output dimensions.
6580 */
isl_map_max_multi_pw_aff(__isl_take isl_map * map)6581 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6582 {
6583 return map_opt_mpa(map, &isl_map_dim_max);
6584 }
6585
6586 /* Scale the elements of "pma" by the corresponding elements of "mv".
6587 */
isl_pw_multi_aff_scale_multi_val(__isl_take isl_pw_multi_aff * pma,__isl_take isl_multi_val * mv)6588 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6589 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6590 {
6591 int i;
6592 isl_bool equal_params;
6593
6594 pma = isl_pw_multi_aff_cow(pma);
6595 if (!pma || !mv)
6596 goto error;
6597 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6598 mv->space, isl_dim_set))
6599 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6600 "spaces don't match", goto error);
6601 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6602 if (equal_params < 0)
6603 goto error;
6604 if (!equal_params) {
6605 pma = isl_pw_multi_aff_align_params(pma,
6606 isl_multi_val_get_space(mv));
6607 mv = isl_multi_val_align_params(mv,
6608 isl_pw_multi_aff_get_space(pma));
6609 if (!pma || !mv)
6610 goto error;
6611 }
6612
6613 for (i = 0; i < pma->n; ++i) {
6614 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6615 isl_multi_val_copy(mv));
6616 if (!pma->p[i].maff)
6617 goto error;
6618 }
6619
6620 isl_multi_val_free(mv);
6621 return pma;
6622 error:
6623 isl_multi_val_free(mv);
6624 isl_pw_multi_aff_free(pma);
6625 return NULL;
6626 }
6627
6628 /* This function is called for each entry of an isl_union_pw_multi_aff.
6629 * If the space of the entry matches that of data->mv,
6630 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6631 * Otherwise, return an empty isl_pw_multi_aff.
6632 */
union_pw_multi_aff_scale_multi_val_entry(__isl_take isl_pw_multi_aff * pma,void * user)6633 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6634 __isl_take isl_pw_multi_aff *pma, void *user)
6635 {
6636 isl_multi_val *mv = user;
6637
6638 if (!pma)
6639 return NULL;
6640 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6641 mv->space, isl_dim_set)) {
6642 isl_space *space = isl_pw_multi_aff_get_space(pma);
6643 isl_pw_multi_aff_free(pma);
6644 return isl_pw_multi_aff_empty(space);
6645 }
6646
6647 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6648 }
6649
6650 /* Scale the elements of "upma" by the corresponding elements of "mv",
6651 * for those entries that match the space of "mv".
6652 */
isl_union_pw_multi_aff_scale_multi_val(__isl_take isl_union_pw_multi_aff * upma,__isl_take isl_multi_val * mv)6653 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6654 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6655 {
6656 struct isl_union_pw_multi_aff_transform_control control = {
6657 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6658 .fn_user = mv,
6659 };
6660
6661 upma = isl_union_pw_multi_aff_align_params(upma,
6662 isl_multi_val_get_space(mv));
6663 mv = isl_multi_val_align_params(mv,
6664 isl_union_pw_multi_aff_get_space(upma));
6665 if (!upma || !mv)
6666 goto error;
6667
6668 return isl_union_pw_multi_aff_transform(upma, &control);
6669
6670 isl_multi_val_free(mv);
6671 return upma;
6672 error:
6673 isl_multi_val_free(mv);
6674 isl_union_pw_multi_aff_free(upma);
6675 return NULL;
6676 }
6677
6678 /* Construct and return a piecewise multi affine expression
6679 * in the given space with value zero in each of the output dimensions and
6680 * a universe domain.
6681 */
isl_pw_multi_aff_zero(__isl_take isl_space * space)6682 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6683 {
6684 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6685 }
6686
6687 /* Construct and return a piecewise multi affine expression
6688 * that is equal to the given piecewise affine expression.
6689 */
isl_pw_multi_aff_from_pw_aff(__isl_take isl_pw_aff * pa)6690 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6691 __isl_take isl_pw_aff *pa)
6692 {
6693 int i;
6694 isl_space *space;
6695 isl_pw_multi_aff *pma;
6696
6697 if (!pa)
6698 return NULL;
6699
6700 space = isl_pw_aff_get_space(pa);
6701 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6702
6703 for (i = 0; i < pa->n; ++i) {
6704 isl_set *set;
6705 isl_multi_aff *ma;
6706
6707 set = isl_set_copy(pa->p[i].set);
6708 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6709 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6710 }
6711
6712 isl_pw_aff_free(pa);
6713 return pma;
6714 }
6715
6716 /* Construct and return a piecewise multi affine expression
6717 * that is equal to the given multi piecewise affine expression
6718 * on the shared domain of the piecewise affine expressions,
6719 * in the special case of a 0D multi piecewise affine expression.
6720 *
6721 * Create a piecewise multi affine expression with the explicit domain of
6722 * the 0D multi piecewise affine expression as domain.
6723 */
isl_pw_multi_aff_from_multi_pw_aff_0D(__isl_take isl_multi_pw_aff * mpa)6724 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6725 __isl_take isl_multi_pw_aff *mpa)
6726 {
6727 isl_space *space;
6728 isl_set *dom;
6729 isl_multi_aff *ma;
6730
6731 space = isl_multi_pw_aff_get_space(mpa);
6732 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6733 isl_multi_pw_aff_free(mpa);
6734
6735 ma = isl_multi_aff_zero(space);
6736 return isl_pw_multi_aff_alloc(dom, ma);
6737 }
6738
6739 /* Construct and return a piecewise multi affine expression
6740 * that is equal to the given multi piecewise affine expression
6741 * on the shared domain of the piecewise affine expressions.
6742 */
isl_pw_multi_aff_from_multi_pw_aff(__isl_take isl_multi_pw_aff * mpa)6743 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6744 __isl_take isl_multi_pw_aff *mpa)
6745 {
6746 int i;
6747 isl_space *space;
6748 isl_pw_aff *pa;
6749 isl_pw_multi_aff *pma;
6750
6751 if (!mpa)
6752 return NULL;
6753
6754 if (mpa->n == 0)
6755 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6756
6757 space = isl_multi_pw_aff_get_space(mpa);
6758 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6759 pma = isl_pw_multi_aff_from_pw_aff(pa);
6760
6761 for (i = 1; i < mpa->n; ++i) {
6762 isl_pw_multi_aff *pma_i;
6763
6764 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6765 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6766 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6767 }
6768
6769 pma = isl_pw_multi_aff_reset_space(pma, space);
6770
6771 isl_multi_pw_aff_free(mpa);
6772 return pma;
6773 }
6774
6775 /* Convenience function that constructs an isl_multi_pw_aff
6776 * directly from an isl_aff.
6777 */
isl_multi_pw_aff_from_aff(__isl_take isl_aff * aff)6778 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6779 {
6780 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6781 }
6782
6783 /* Construct and return a multi piecewise affine expression
6784 * that is equal to the given multi affine expression.
6785 */
isl_multi_pw_aff_from_multi_aff(__isl_take isl_multi_aff * ma)6786 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6787 __isl_take isl_multi_aff *ma)
6788 {
6789 int i;
6790 isl_size n;
6791 isl_multi_pw_aff *mpa;
6792
6793 n = isl_multi_aff_dim(ma, isl_dim_out);
6794 if (n < 0)
6795 ma = isl_multi_aff_free(ma);
6796 if (!ma)
6797 return NULL;
6798
6799 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6800
6801 for (i = 0; i < n; ++i) {
6802 isl_pw_aff *pa;
6803
6804 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6805 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6806 }
6807
6808 isl_multi_aff_free(ma);
6809 return mpa;
6810 }
6811
6812 /* Construct and return a multi piecewise affine expression
6813 * that is equal to the given piecewise multi affine expression.
6814 *
6815 * If the resulting multi piecewise affine expression has
6816 * an explicit domain, then assign it the domain of the input.
6817 * In other cases, the domain is stored in the individual elements.
6818 */
isl_multi_pw_aff_from_pw_multi_aff(__isl_take isl_pw_multi_aff * pma)6819 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6820 __isl_take isl_pw_multi_aff *pma)
6821 {
6822 int i;
6823 isl_size n;
6824 isl_space *space;
6825 isl_multi_pw_aff *mpa;
6826
6827 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6828 if (n < 0)
6829 pma = isl_pw_multi_aff_free(pma);
6830 space = isl_pw_multi_aff_get_space(pma);
6831 mpa = isl_multi_pw_aff_alloc(space);
6832
6833 for (i = 0; i < n; ++i) {
6834 isl_pw_aff *pa;
6835
6836 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6837 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6838 }
6839 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6840 isl_set *dom;
6841
6842 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6843 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6844 }
6845
6846 isl_pw_multi_aff_free(pma);
6847 return mpa;
6848 }
6849
6850 /* Do "pa1" and "pa2" represent the same function?
6851 *
6852 * We first check if they are obviously equal.
6853 * If not, we convert them to maps and check if those are equal.
6854 *
6855 * If "pa1" or "pa2" contain any NaNs, then they are considered
6856 * not to be the same. A NaN is not equal to anything, not even
6857 * to another NaN.
6858 */
isl_pw_aff_is_equal(__isl_keep isl_pw_aff * pa1,__isl_keep isl_pw_aff * pa2)6859 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6860 __isl_keep isl_pw_aff *pa2)
6861 {
6862 isl_bool equal;
6863 isl_bool has_nan;
6864 isl_map *map1, *map2;
6865
6866 if (!pa1 || !pa2)
6867 return isl_bool_error;
6868
6869 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6870 if (equal < 0 || equal)
6871 return equal;
6872 has_nan = either_involves_nan(pa1, pa2);
6873 if (has_nan < 0)
6874 return isl_bool_error;
6875 if (has_nan)
6876 return isl_bool_false;
6877
6878 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6879 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6880 equal = isl_map_is_equal(map1, map2);
6881 isl_map_free(map1);
6882 isl_map_free(map2);
6883
6884 return equal;
6885 }
6886
6887 /* Do "mpa1" and "mpa2" represent the same function?
6888 *
6889 * Note that we cannot convert the entire isl_multi_pw_aff
6890 * to a map because the domains of the piecewise affine expressions
6891 * may not be the same.
6892 */
isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff * mpa1,__isl_keep isl_multi_pw_aff * mpa2)6893 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6894 __isl_keep isl_multi_pw_aff *mpa2)
6895 {
6896 int i;
6897 isl_bool equal, equal_params;
6898
6899 if (!mpa1 || !mpa2)
6900 return isl_bool_error;
6901
6902 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6903 if (equal_params < 0)
6904 return isl_bool_error;
6905 if (!equal_params) {
6906 if (!isl_space_has_named_params(mpa1->space))
6907 return isl_bool_false;
6908 if (!isl_space_has_named_params(mpa2->space))
6909 return isl_bool_false;
6910 mpa1 = isl_multi_pw_aff_copy(mpa1);
6911 mpa2 = isl_multi_pw_aff_copy(mpa2);
6912 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6913 isl_multi_pw_aff_get_space(mpa2));
6914 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6915 isl_multi_pw_aff_get_space(mpa1));
6916 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6917 isl_multi_pw_aff_free(mpa1);
6918 isl_multi_pw_aff_free(mpa2);
6919 return equal;
6920 }
6921
6922 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6923 if (equal < 0 || !equal)
6924 return equal;
6925
6926 for (i = 0; i < mpa1->n; ++i) {
6927 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6928 if (equal < 0 || !equal)
6929 return equal;
6930 }
6931
6932 return isl_bool_true;
6933 }
6934
6935 /* Do "pma1" and "pma2" represent the same function?
6936 *
6937 * First check if they are obviously equal.
6938 * If not, then convert them to maps and check if those are equal.
6939 *
6940 * If "pa1" or "pa2" contain any NaNs, then they are considered
6941 * not to be the same. A NaN is not equal to anything, not even
6942 * to another NaN.
6943 */
isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff * pma1,__isl_keep isl_pw_multi_aff * pma2)6944 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6945 __isl_keep isl_pw_multi_aff *pma2)
6946 {
6947 isl_bool equal;
6948 isl_bool has_nan;
6949 isl_map *map1, *map2;
6950
6951 if (!pma1 || !pma2)
6952 return isl_bool_error;
6953
6954 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6955 if (equal < 0 || equal)
6956 return equal;
6957 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6958 if (has_nan >= 0 && !has_nan)
6959 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6960 if (has_nan < 0 || has_nan)
6961 return isl_bool_not(has_nan);
6962
6963 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
6964 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
6965 equal = isl_map_is_equal(map1, map2);
6966 isl_map_free(map1);
6967 isl_map_free(map2);
6968
6969 return equal;
6970 }
6971
6972 /* Compute the pullback of "mpa" by the function represented by "ma".
6973 * In other words, plug in "ma" in "mpa".
6974 *
6975 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6976 *
6977 * If "mpa" has an explicit domain, then it is this domain
6978 * that needs to undergo a pullback, i.e., a preimage.
6979 */
isl_multi_pw_aff_pullback_multi_aff_aligned(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_multi_aff * ma)6980 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6981 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6982 {
6983 int i;
6984 isl_space *space = NULL;
6985
6986 mpa = isl_multi_pw_aff_cow(mpa);
6987 if (!mpa || !ma)
6988 goto error;
6989
6990 space = isl_space_join(isl_multi_aff_get_space(ma),
6991 isl_multi_pw_aff_get_space(mpa));
6992 if (!space)
6993 goto error;
6994
6995 for (i = 0; i < mpa->n; ++i) {
6996 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6997 isl_multi_aff_copy(ma));
6998 if (!mpa->u.p[i])
6999 goto error;
7000 }
7001 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7002 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7003 isl_multi_aff_copy(ma));
7004 if (!mpa->u.dom)
7005 goto error;
7006 }
7007
7008 isl_multi_aff_free(ma);
7009 isl_space_free(mpa->space);
7010 mpa->space = space;
7011 return mpa;
7012 error:
7013 isl_space_free(space);
7014 isl_multi_pw_aff_free(mpa);
7015 isl_multi_aff_free(ma);
7016 return NULL;
7017 }
7018
7019 /* Compute the pullback of "mpa" by the function represented by "ma".
7020 * In other words, plug in "ma" in "mpa".
7021 */
isl_multi_pw_aff_pullback_multi_aff(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_multi_aff * ma)7022 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7023 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7024 {
7025 isl_bool equal_params;
7026
7027 if (!mpa || !ma)
7028 goto error;
7029 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7030 if (equal_params < 0)
7031 goto error;
7032 if (equal_params)
7033 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7034 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7035 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7036 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7037 error:
7038 isl_multi_pw_aff_free(mpa);
7039 isl_multi_aff_free(ma);
7040 return NULL;
7041 }
7042
7043 /* Compute the pullback of "mpa" by the function represented by "pma".
7044 * In other words, plug in "pma" in "mpa".
7045 *
7046 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7047 *
7048 * If "mpa" has an explicit domain, then it is this domain
7049 * that needs to undergo a pullback, i.e., a preimage.
7050 */
7051 static __isl_give isl_multi_pw_aff *
isl_multi_pw_aff_pullback_pw_multi_aff_aligned(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_pw_multi_aff * pma)7052 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7053 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7054 {
7055 int i;
7056 isl_space *space = NULL;
7057
7058 mpa = isl_multi_pw_aff_cow(mpa);
7059 if (!mpa || !pma)
7060 goto error;
7061
7062 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7063 isl_multi_pw_aff_get_space(mpa));
7064
7065 for (i = 0; i < mpa->n; ++i) {
7066 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7067 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7068 if (!mpa->u.p[i])
7069 goto error;
7070 }
7071 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7072 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7073 isl_pw_multi_aff_copy(pma));
7074 if (!mpa->u.dom)
7075 goto error;
7076 }
7077
7078 isl_pw_multi_aff_free(pma);
7079 isl_space_free(mpa->space);
7080 mpa->space = space;
7081 return mpa;
7082 error:
7083 isl_space_free(space);
7084 isl_multi_pw_aff_free(mpa);
7085 isl_pw_multi_aff_free(pma);
7086 return NULL;
7087 }
7088
7089 /* Compute the pullback of "mpa" by the function represented by "pma".
7090 * In other words, plug in "pma" in "mpa".
7091 */
isl_multi_pw_aff_pullback_pw_multi_aff(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_pw_multi_aff * pma)7092 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7093 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7094 {
7095 isl_bool equal_params;
7096
7097 if (!mpa || !pma)
7098 goto error;
7099 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7100 if (equal_params < 0)
7101 goto error;
7102 if (equal_params)
7103 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7104 mpa = isl_multi_pw_aff_align_params(mpa,
7105 isl_pw_multi_aff_get_space(pma));
7106 pma = isl_pw_multi_aff_align_params(pma,
7107 isl_multi_pw_aff_get_space(mpa));
7108 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7109 error:
7110 isl_multi_pw_aff_free(mpa);
7111 isl_pw_multi_aff_free(pma);
7112 return NULL;
7113 }
7114
7115 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7116 * with the domain of "aff". The domain of the result is the same
7117 * as that of "mpa".
7118 * "mpa" and "aff" are assumed to have been aligned.
7119 *
7120 * We first extract the parametric constant from "aff", defined
7121 * over the correct domain.
7122 * Then we add the appropriate combinations of the members of "mpa".
7123 * Finally, we add the integer divisions through recursive calls.
7124 */
isl_multi_pw_aff_apply_aff_aligned(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_aff * aff)7125 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7126 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7127 {
7128 int i;
7129 isl_size n_in, n_div, n_mpa_in;
7130 isl_space *space;
7131 isl_val *v;
7132 isl_pw_aff *pa;
7133 isl_aff *tmp;
7134
7135 n_in = isl_aff_dim(aff, isl_dim_in);
7136 n_div = isl_aff_dim(aff, isl_dim_div);
7137 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7138 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7139 goto error;
7140
7141 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7142 tmp = isl_aff_copy(aff);
7143 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7144 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7145 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7146 tmp = isl_aff_reset_domain_space(tmp, space);
7147 pa = isl_pw_aff_from_aff(tmp);
7148
7149 for (i = 0; i < n_in; ++i) {
7150 isl_pw_aff *pa_i;
7151
7152 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7153 continue;
7154 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7155 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7156 pa_i = isl_pw_aff_scale_val(pa_i, v);
7157 pa = isl_pw_aff_add(pa, pa_i);
7158 }
7159
7160 for (i = 0; i < n_div; ++i) {
7161 isl_aff *div;
7162 isl_pw_aff *pa_i;
7163
7164 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7165 continue;
7166 div = isl_aff_get_div(aff, i);
7167 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7168 isl_multi_pw_aff_copy(mpa), div);
7169 pa_i = isl_pw_aff_floor(pa_i);
7170 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7171 pa_i = isl_pw_aff_scale_val(pa_i, v);
7172 pa = isl_pw_aff_add(pa, pa_i);
7173 }
7174
7175 isl_multi_pw_aff_free(mpa);
7176 isl_aff_free(aff);
7177
7178 return pa;
7179 error:
7180 isl_multi_pw_aff_free(mpa);
7181 isl_aff_free(aff);
7182 return NULL;
7183 }
7184
7185 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7186 * with the domain of "aff". The domain of the result is the same
7187 * as that of "mpa".
7188 */
isl_multi_pw_aff_apply_aff(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_aff * aff)7189 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7190 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7191 {
7192 isl_bool equal_params;
7193
7194 if (!aff || !mpa)
7195 goto error;
7196 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7197 if (equal_params < 0)
7198 goto error;
7199 if (equal_params)
7200 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7201
7202 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7203 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7204
7205 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7206 error:
7207 isl_aff_free(aff);
7208 isl_multi_pw_aff_free(mpa);
7209 return NULL;
7210 }
7211
7212 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7213 * with the domain of "pa". The domain of the result is the same
7214 * as that of "mpa".
7215 * "mpa" and "pa" are assumed to have been aligned.
7216 *
7217 * We consider each piece in turn. Note that the domains of the
7218 * pieces are assumed to be disjoint and they remain disjoint
7219 * after taking the preimage (over the same function).
7220 */
isl_multi_pw_aff_apply_pw_aff_aligned(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_pw_aff * pa)7221 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7222 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7223 {
7224 isl_space *space;
7225 isl_pw_aff *res;
7226 int i;
7227
7228 if (!mpa || !pa)
7229 goto error;
7230
7231 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7232 isl_pw_aff_get_space(pa));
7233 res = isl_pw_aff_empty(space);
7234
7235 for (i = 0; i < pa->n; ++i) {
7236 isl_pw_aff *pa_i;
7237 isl_set *domain;
7238
7239 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7240 isl_multi_pw_aff_copy(mpa),
7241 isl_aff_copy(pa->p[i].aff));
7242 domain = isl_set_copy(pa->p[i].set);
7243 domain = isl_set_preimage_multi_pw_aff(domain,
7244 isl_multi_pw_aff_copy(mpa));
7245 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7246 res = isl_pw_aff_add_disjoint(res, pa_i);
7247 }
7248
7249 isl_pw_aff_free(pa);
7250 isl_multi_pw_aff_free(mpa);
7251 return res;
7252 error:
7253 isl_pw_aff_free(pa);
7254 isl_multi_pw_aff_free(mpa);
7255 return NULL;
7256 }
7257
7258 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7259 * with the domain of "pa". The domain of the result is the same
7260 * as that of "mpa".
7261 */
isl_multi_pw_aff_apply_pw_aff(__isl_take isl_multi_pw_aff * mpa,__isl_take isl_pw_aff * pa)7262 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7263 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7264 {
7265 isl_bool equal_params;
7266
7267 if (!pa || !mpa)
7268 goto error;
7269 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7270 if (equal_params < 0)
7271 goto error;
7272 if (equal_params)
7273 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7274
7275 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7276 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7277
7278 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7279 error:
7280 isl_pw_aff_free(pa);
7281 isl_multi_pw_aff_free(mpa);
7282 return NULL;
7283 }
7284
7285 /* Compute the pullback of "pa" by the function represented by "mpa".
7286 * In other words, plug in "mpa" in "pa".
7287 * "pa" and "mpa" are assumed to have been aligned.
7288 *
7289 * The pullback is computed by applying "pa" to "mpa".
7290 */
isl_pw_aff_pullback_multi_pw_aff_aligned(__isl_take isl_pw_aff * pa,__isl_take isl_multi_pw_aff * mpa)7291 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7292 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7293 {
7294 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7295 }
7296
7297 /* Compute the pullback of "pa" by the function represented by "mpa".
7298 * In other words, plug in "mpa" in "pa".
7299 *
7300 * The pullback is computed by applying "pa" to "mpa".
7301 */
isl_pw_aff_pullback_multi_pw_aff(__isl_take isl_pw_aff * pa,__isl_take isl_multi_pw_aff * mpa)7302 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7303 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7304 {
7305 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7306 }
7307
7308 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7309 * In other words, plug in "mpa2" in "mpa1".
7310 *
7311 * We pullback each member of "mpa1" in turn.
7312 *
7313 * If "mpa1" has an explicit domain, then it is this domain
7314 * that needs to undergo a pullback instead, i.e., a preimage.
7315 */
isl_multi_pw_aff_pullback_multi_pw_aff(__isl_take isl_multi_pw_aff * mpa1,__isl_take isl_multi_pw_aff * mpa2)7316 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7317 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7318 {
7319 int i;
7320 isl_space *space = NULL;
7321
7322 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7323 mpa1 = isl_multi_pw_aff_cow(mpa1);
7324 if (!mpa1 || !mpa2)
7325 goto error;
7326
7327 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7328 isl_multi_pw_aff_get_space(mpa1));
7329
7330 for (i = 0; i < mpa1->n; ++i) {
7331 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7332 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7333 if (!mpa1->u.p[i])
7334 goto error;
7335 }
7336
7337 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7338 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7339 isl_multi_pw_aff_copy(mpa2));
7340 if (!mpa1->u.dom)
7341 goto error;
7342 }
7343 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7344
7345 isl_multi_pw_aff_free(mpa2);
7346 return mpa1;
7347 error:
7348 isl_space_free(space);
7349 isl_multi_pw_aff_free(mpa1);
7350 isl_multi_pw_aff_free(mpa2);
7351 return NULL;
7352 }
7353
7354 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7355 * of "mpa1" and "mpa2" live in the same space, construct map space
7356 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7357 * with this map space as extract argument.
7358 */
isl_multi_pw_aff_order_map(__isl_take isl_multi_pw_aff * mpa1,__isl_take isl_multi_pw_aff * mpa2,__isl_give isl_map * (* order)(__isl_keep isl_multi_pw_aff * mpa1,__isl_keep isl_multi_pw_aff * mpa2,__isl_take isl_space * space))7359 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7360 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7361 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7362 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7363 {
7364 int match;
7365 isl_space *space1, *space2;
7366 isl_map *res;
7367
7368 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7369 isl_multi_pw_aff_get_space(mpa2));
7370 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7371 isl_multi_pw_aff_get_space(mpa1));
7372 if (!mpa1 || !mpa2)
7373 goto error;
7374 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7375 mpa2->space, isl_dim_out);
7376 if (match < 0)
7377 goto error;
7378 if (!match)
7379 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7380 "range spaces don't match", goto error);
7381 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7382 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7383 space1 = isl_space_map_from_domain_and_range(space1, space2);
7384
7385 res = order(mpa1, mpa2, space1);
7386 isl_multi_pw_aff_free(mpa1);
7387 isl_multi_pw_aff_free(mpa2);
7388 return res;
7389 error:
7390 isl_multi_pw_aff_free(mpa1);
7391 isl_multi_pw_aff_free(mpa2);
7392 return NULL;
7393 }
7394
7395 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7396 * where the function values are equal. "space" is the space of the result.
7397 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7398 *
7399 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7400 * in the sequences are equal.
7401 */
isl_multi_pw_aff_eq_map_on_space(__isl_keep isl_multi_pw_aff * mpa1,__isl_keep isl_multi_pw_aff * mpa2,__isl_take isl_space * space)7402 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7403 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7404 __isl_take isl_space *space)
7405 {
7406 int i;
7407 isl_size n;
7408 isl_map *res;
7409
7410 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7411 if (n < 0)
7412 space = isl_space_free(space);
7413 res = isl_map_universe(space);
7414
7415 for (i = 0; i < n; ++i) {
7416 isl_pw_aff *pa1, *pa2;
7417 isl_map *map;
7418
7419 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7420 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7421 map = isl_pw_aff_eq_map(pa1, pa2);
7422 res = isl_map_intersect(res, map);
7423 }
7424
7425 return res;
7426 }
7427
7428 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7429 * where the function values are equal.
7430 */
isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff * mpa1,__isl_take isl_multi_pw_aff * mpa2)7431 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7432 __isl_take isl_multi_pw_aff *mpa2)
7433 {
7434 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7435 &isl_multi_pw_aff_eq_map_on_space);
7436 }
7437
7438 /* Intersect "map" with the result of applying "order"
7439 * on two copies of "mpa".
7440 */
isl_map_order_at_multi_pw_aff(__isl_take isl_map * map,__isl_take isl_multi_pw_aff * mpa,__isl_give isl_map * (* order)(__isl_take isl_multi_pw_aff * mpa1,__isl_take isl_multi_pw_aff * mpa2))7441 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7442 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7443 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7444 __isl_take isl_multi_pw_aff *mpa2))
7445 {
7446 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7447 }
7448
7449 /* Return the subset of "map" where the domain and the range
7450 * have equal "mpa" values.
7451 */
isl_map_eq_at_multi_pw_aff(__isl_take isl_map * map,__isl_take isl_multi_pw_aff * mpa)7452 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7453 __isl_take isl_multi_pw_aff *mpa)
7454 {
7455 return isl_map_order_at_multi_pw_aff(map, mpa,
7456 &isl_multi_pw_aff_eq_map);
7457 }
7458
7459 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7460 * where the function values of "mpa1" lexicographically satisfies
7461 * "strict_base"/"base" compared to that of "mpa2".
7462 * "space" is the space of the result.
7463 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7464 *
7465 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7466 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7467 * when compared to the i-th element of "mpa2" while all previous elements are
7468 * pairwise equal.
7469 * In particular, if i corresponds to the final elements
7470 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7471 * for other values of i.
7472 * If "base" is a strict order, then "base" and "strict_base" are the same.
7473 */
isl_multi_pw_aff_lex_map_on_space(__isl_keep isl_multi_pw_aff * mpa1,__isl_keep isl_multi_pw_aff * mpa2,__isl_give isl_map * (* strict_base)(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2),__isl_give isl_map * (* base)(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2),__isl_take isl_space * space)7474 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7475 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7476 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7477 __isl_take isl_pw_aff *pa2),
7478 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7479 __isl_take isl_pw_aff *pa2),
7480 __isl_take isl_space *space)
7481 {
7482 int i;
7483 isl_size n;
7484 isl_map *res, *rest;
7485
7486 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7487 if (n < 0)
7488 space = isl_space_free(space);
7489 res = isl_map_empty(isl_space_copy(space));
7490 rest = isl_map_universe(space);
7491
7492 for (i = 0; i < n; ++i) {
7493 int last;
7494 isl_pw_aff *pa1, *pa2;
7495 isl_map *map;
7496
7497 last = i == n - 1;
7498
7499 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7500 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7501 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7502 map = isl_map_intersect(map, isl_map_copy(rest));
7503 res = isl_map_union(res, map);
7504
7505 if (last)
7506 continue;
7507
7508 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7509 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7510 map = isl_pw_aff_eq_map(pa1, pa2);
7511 rest = isl_map_intersect(rest, map);
7512 }
7513
7514 isl_map_free(rest);
7515 return res;
7516 }
7517
7518 #undef ORDER
7519 #define ORDER le
7520 #undef STRICT_ORDER
7521 #define STRICT_ORDER lt
7522 #include "isl_aff_lex_templ.c"
7523
7524 #undef ORDER
7525 #define ORDER lt
7526 #undef STRICT_ORDER
7527 #define STRICT_ORDER lt
7528 #include "isl_aff_lex_templ.c"
7529
7530 #undef ORDER
7531 #define ORDER ge
7532 #undef STRICT_ORDER
7533 #define STRICT_ORDER gt
7534 #include "isl_aff_lex_templ.c"
7535
7536 #undef ORDER
7537 #define ORDER gt
7538 #undef STRICT_ORDER
7539 #define STRICT_ORDER gt
7540 #include "isl_aff_lex_templ.c"
7541
7542 /* Compare two isl_affs.
7543 *
7544 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7545 * than "aff2" and 0 if they are equal.
7546 *
7547 * The order is fairly arbitrary. We do consider expressions that only involve
7548 * earlier dimensions as "smaller".
7549 */
isl_aff_plain_cmp(__isl_keep isl_aff * aff1,__isl_keep isl_aff * aff2)7550 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7551 {
7552 int cmp;
7553 int last1, last2;
7554
7555 if (aff1 == aff2)
7556 return 0;
7557
7558 if (!aff1)
7559 return -1;
7560 if (!aff2)
7561 return 1;
7562
7563 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7564 if (cmp != 0)
7565 return cmp;
7566
7567 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7568 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7569 if (last1 != last2)
7570 return last1 - last2;
7571
7572 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7573 }
7574
7575 /* Compare two isl_pw_affs.
7576 *
7577 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7578 * than "pa2" and 0 if they are equal.
7579 *
7580 * The order is fairly arbitrary. We do consider expressions that only involve
7581 * earlier dimensions as "smaller".
7582 */
isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff * pa1,__isl_keep isl_pw_aff * pa2)7583 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7584 __isl_keep isl_pw_aff *pa2)
7585 {
7586 int i;
7587 int cmp;
7588
7589 if (pa1 == pa2)
7590 return 0;
7591
7592 if (!pa1)
7593 return -1;
7594 if (!pa2)
7595 return 1;
7596
7597 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7598 if (cmp != 0)
7599 return cmp;
7600
7601 if (pa1->n != pa2->n)
7602 return pa1->n - pa2->n;
7603
7604 for (i = 0; i < pa1->n; ++i) {
7605 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7606 if (cmp != 0)
7607 return cmp;
7608 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7609 if (cmp != 0)
7610 return cmp;
7611 }
7612
7613 return 0;
7614 }
7615
7616 /* Return a piecewise affine expression that is equal to "v" on "domain".
7617 */
isl_pw_aff_val_on_domain(__isl_take isl_set * domain,__isl_take isl_val * v)7618 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7619 __isl_take isl_val *v)
7620 {
7621 isl_space *space;
7622 isl_local_space *ls;
7623 isl_aff *aff;
7624
7625 space = isl_set_get_space(domain);
7626 ls = isl_local_space_from_space(space);
7627 aff = isl_aff_val_on_domain(ls, v);
7628
7629 return isl_pw_aff_alloc(domain, aff);
7630 }
7631
7632 /* Return a piecewise affine expression that is equal to the parameter
7633 * with identifier "id" on "domain".
7634 */
isl_pw_aff_param_on_domain_id(__isl_take isl_set * domain,__isl_take isl_id * id)7635 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7636 __isl_take isl_set *domain, __isl_take isl_id *id)
7637 {
7638 isl_space *space;
7639 isl_aff *aff;
7640
7641 space = isl_set_get_space(domain);
7642 space = isl_space_add_param_id(space, isl_id_copy(id));
7643 domain = isl_set_align_params(domain, isl_space_copy(space));
7644 aff = isl_aff_param_on_domain_space_id(space, id);
7645
7646 return isl_pw_aff_alloc(domain, aff);
7647 }
7648
7649 /* Return a multi affine expression that is equal to "mv" on domain
7650 * space "space".
7651 */
isl_multi_aff_multi_val_on_space(__isl_take isl_space * space,__isl_take isl_multi_val * mv)7652 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7653 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7654 {
7655 int i;
7656 isl_size n;
7657 isl_space *space2;
7658 isl_local_space *ls;
7659 isl_multi_aff *ma;
7660
7661 n = isl_multi_val_dim(mv, isl_dim_set);
7662 if (!space || n < 0)
7663 goto error;
7664
7665 space2 = isl_multi_val_get_space(mv);
7666 space2 = isl_space_align_params(space2, isl_space_copy(space));
7667 space = isl_space_align_params(space, isl_space_copy(space2));
7668 space = isl_space_map_from_domain_and_range(space, space2);
7669 ma = isl_multi_aff_alloc(isl_space_copy(space));
7670 ls = isl_local_space_from_space(isl_space_domain(space));
7671 for (i = 0; i < n; ++i) {
7672 isl_val *v;
7673 isl_aff *aff;
7674
7675 v = isl_multi_val_get_val(mv, i);
7676 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7677 ma = isl_multi_aff_set_aff(ma, i, aff);
7678 }
7679 isl_local_space_free(ls);
7680
7681 isl_multi_val_free(mv);
7682 return ma;
7683 error:
7684 isl_space_free(space);
7685 isl_multi_val_free(mv);
7686 return NULL;
7687 }
7688
7689 /* Return a piecewise multi-affine expression
7690 * that is equal to "mv" on "domain".
7691 */
isl_pw_multi_aff_multi_val_on_domain(__isl_take isl_set * domain,__isl_take isl_multi_val * mv)7692 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7693 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7694 {
7695 isl_space *space;
7696 isl_multi_aff *ma;
7697
7698 space = isl_set_get_space(domain);
7699 ma = isl_multi_aff_multi_val_on_space(space, mv);
7700
7701 return isl_pw_multi_aff_alloc(domain, ma);
7702 }
7703
7704 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7705 * mv is the value that should be attained on each domain set
7706 * res collects the results
7707 */
7708 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7709 isl_multi_val *mv;
7710 isl_union_pw_multi_aff *res;
7711 };
7712
7713 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7714 * and add it to data->res.
7715 */
pw_multi_aff_multi_val_on_domain(__isl_take isl_set * domain,void * user)7716 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7717 void *user)
7718 {
7719 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7720 isl_pw_multi_aff *pma;
7721 isl_multi_val *mv;
7722
7723 mv = isl_multi_val_copy(data->mv);
7724 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7725 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7726
7727 return data->res ? isl_stat_ok : isl_stat_error;
7728 }
7729
7730 /* Return a union piecewise multi-affine expression
7731 * that is equal to "mv" on "domain".
7732 */
isl_union_pw_multi_aff_multi_val_on_domain(__isl_take isl_union_set * domain,__isl_take isl_multi_val * mv)7733 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7734 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7735 {
7736 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7737 isl_space *space;
7738
7739 space = isl_union_set_get_space(domain);
7740 data.res = isl_union_pw_multi_aff_empty(space);
7741 data.mv = mv;
7742 if (isl_union_set_foreach_set(domain,
7743 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7744 data.res = isl_union_pw_multi_aff_free(data.res);
7745 isl_union_set_free(domain);
7746 isl_multi_val_free(mv);
7747 return data.res;
7748 }
7749
7750 /* Compute the pullback of data->pma by the function represented by "pma2",
7751 * provided the spaces match, and add the results to data->res.
7752 */
pullback_entry(__isl_take isl_pw_multi_aff * pma2,void * user)7753 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7754 {
7755 struct isl_union_pw_multi_aff_bin_data *data = user;
7756
7757 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7758 pma2->dim, isl_dim_out)) {
7759 isl_pw_multi_aff_free(pma2);
7760 return isl_stat_ok;
7761 }
7762
7763 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7764 isl_pw_multi_aff_copy(data->pma), pma2);
7765
7766 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7767 if (!data->res)
7768 return isl_stat_error;
7769
7770 return isl_stat_ok;
7771 }
7772
7773 /* Compute the pullback of "upma1" by the function represented by "upma2".
7774 */
7775 __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_pullback_union_pw_multi_aff(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)7776 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7777 __isl_take isl_union_pw_multi_aff *upma1,
7778 __isl_take isl_union_pw_multi_aff *upma2)
7779 {
7780 return bin_op(upma1, upma2, &pullback_entry);
7781 }
7782
7783 /* Apply "upma2" to "upma1".
7784 *
7785 * That is, compute the pullback of "upma2" by "upma1".
7786 */
7787 __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_apply_union_pw_multi_aff(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)7788 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7789 __isl_take isl_union_pw_multi_aff *upma1,
7790 __isl_take isl_union_pw_multi_aff *upma2)
7791 {
7792 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7793 }
7794
7795 #undef TYPE
7796 #define TYPE isl_pw_multi_aff
7797 static
7798 #include "isl_copy_tuple_id_templ.c"
7799
7800 /* Given a function "pma1" of the form A[B -> C] -> D and
7801 * a function "pma2" of the form E -> B,
7802 * replace the domain of the wrapped relation inside the domain of "pma1"
7803 * by the preimage with respect to "pma2".
7804 * In other words, plug in "pma2" in this nested domain.
7805 * The result is of the form A[E -> C] -> D.
7806 *
7807 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7808 * plug that into "pma1".
7809 */
7810 __isl_give isl_pw_multi_aff *
isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(__isl_take isl_pw_multi_aff * pma1,__isl_take isl_pw_multi_aff * pma2)7811 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7812 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7813 {
7814 isl_space *pma1_space, *pma2_space;
7815 isl_space *space;
7816 isl_pw_multi_aff *id;
7817
7818 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7819 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7820
7821 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7822 goto error;
7823 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7824 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7825 goto error;
7826
7827 space = isl_space_domain(isl_space_copy(pma1_space));
7828 space = isl_space_range(isl_space_unwrap(space));
7829 id = isl_pw_multi_aff_identity_on_domain_space(space);
7830 pma2 = isl_pw_multi_aff_product(pma2, id);
7831
7832 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7833 pma1_space, isl_dim_in);
7834 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7835 pma1_space, isl_dim_in);
7836
7837 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7838 error:
7839 isl_pw_multi_aff_free(pma1);
7840 isl_pw_multi_aff_free(pma2);
7841 return NULL;
7842 }
7843
7844 /* If data->pma and "pma2" are such that
7845 * data->pma is of the form A[B -> C] -> D and
7846 * "pma2" is of the form E -> B,
7847 * then replace the domain of the wrapped relation
7848 * inside the domain of data->pma by the preimage with respect to "pma2" and
7849 * add the result to data->res.
7850 */
preimage_domain_wrapped_domain_entry(__isl_take isl_pw_multi_aff * pma2,void * user)7851 static isl_stat preimage_domain_wrapped_domain_entry(
7852 __isl_take isl_pw_multi_aff *pma2, void *user)
7853 {
7854 struct isl_union_pw_multi_aff_bin_data *data = user;
7855 isl_space *pma1_space, *pma2_space;
7856 isl_bool match;
7857
7858 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7859 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7860
7861 match = isl_space_domain_is_wrapping(pma1_space);
7862 if (match >= 0 && match)
7863 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7864 isl_dim_in, pma2_space, isl_dim_out);
7865 if (match < 0 || !match) {
7866 isl_pw_multi_aff_free(pma2);
7867 return match < 0 ? isl_stat_error : isl_stat_ok;
7868 }
7869
7870 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7871 isl_pw_multi_aff_copy(data->pma), pma2);
7872
7873 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7874
7875 return isl_stat_non_null(data->res);
7876 }
7877
7878 /* For each pair of functions A[B -> C] -> D in "upma1" and
7879 * E -> B in "upma2",
7880 * replace the domain of the wrapped relation inside the domain of the first
7881 * by the preimage with respect to the second and collect the results.
7882 * In other words, plug in the second function in this nested domain.
7883 * The results are of the form A[E -> C] -> D.
7884 */
7885 __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(__isl_take isl_union_pw_multi_aff * upma1,__isl_take isl_union_pw_multi_aff * upma2)7886 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7887 __isl_take isl_union_pw_multi_aff *upma1,
7888 __isl_take isl_union_pw_multi_aff *upma2)
7889 {
7890 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
7891 }
7892
7893 /* Check that the domain space of "upa" matches "space".
7894 *
7895 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7896 * can in principle never fail since the space "space" is that
7897 * of the isl_multi_union_pw_aff and is a set space such that
7898 * there is no domain space to match.
7899 *
7900 * We check the parameters and double-check that "space" is
7901 * indeed that of a set.
7902 */
isl_union_pw_aff_check_match_domain_space(__isl_keep isl_union_pw_aff * upa,__isl_keep isl_space * space)7903 static isl_stat isl_union_pw_aff_check_match_domain_space(
7904 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7905 {
7906 isl_space *upa_space;
7907 isl_bool match;
7908
7909 if (!upa || !space)
7910 return isl_stat_error;
7911
7912 match = isl_space_is_set(space);
7913 if (match < 0)
7914 return isl_stat_error;
7915 if (!match)
7916 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7917 "expecting set space", return isl_stat_error);
7918
7919 upa_space = isl_union_pw_aff_get_space(upa);
7920 match = isl_space_has_equal_params(space, upa_space);
7921 if (match < 0)
7922 goto error;
7923 if (!match)
7924 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7925 "parameters don't match", goto error);
7926
7927 isl_space_free(upa_space);
7928 return isl_stat_ok;
7929 error:
7930 isl_space_free(upa_space);
7931 return isl_stat_error;
7932 }
7933
7934 /* Do the parameters of "upa" match those of "space"?
7935 */
isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff * upa,__isl_keep isl_space * space)7936 static isl_bool isl_union_pw_aff_matching_params(
7937 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7938 {
7939 isl_space *upa_space;
7940 isl_bool match;
7941
7942 if (!upa || !space)
7943 return isl_bool_error;
7944
7945 upa_space = isl_union_pw_aff_get_space(upa);
7946
7947 match = isl_space_has_equal_params(space, upa_space);
7948
7949 isl_space_free(upa_space);
7950 return match;
7951 }
7952
7953 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7954 * space represents the new parameters.
7955 * res collects the results.
7956 */
7957 struct isl_union_pw_aff_reset_params_data {
7958 isl_space *space;
7959 isl_union_pw_aff *res;
7960 };
7961
7962 /* Replace the parameters of "pa" by data->space and
7963 * add the result to data->res.
7964 */
reset_params(__isl_take isl_pw_aff * pa,void * user)7965 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7966 {
7967 struct isl_union_pw_aff_reset_params_data *data = user;
7968 isl_space *space;
7969
7970 space = isl_pw_aff_get_space(pa);
7971 space = isl_space_replace_params(space, data->space);
7972 pa = isl_pw_aff_reset_space(pa, space);
7973 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7974
7975 return data->res ? isl_stat_ok : isl_stat_error;
7976 }
7977
7978 /* Replace the domain space of "upa" by "space".
7979 * Since a union expression does not have a (single) domain space,
7980 * "space" is necessarily a parameter space.
7981 *
7982 * Since the order and the names of the parameters determine
7983 * the hash value, we need to create a new hash table.
7984 */
isl_union_pw_aff_reset_domain_space(__isl_take isl_union_pw_aff * upa,__isl_take isl_space * space)7985 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7986 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7987 {
7988 struct isl_union_pw_aff_reset_params_data data = { space };
7989 isl_bool match;
7990
7991 match = isl_union_pw_aff_matching_params(upa, space);
7992 if (match < 0)
7993 upa = isl_union_pw_aff_free(upa);
7994 else if (match) {
7995 isl_space_free(space);
7996 return upa;
7997 }
7998
7999 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8000 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8001 data.res = isl_union_pw_aff_free(data.res);
8002
8003 isl_union_pw_aff_free(upa);
8004 isl_space_free(space);
8005 return data.res;
8006 }
8007
8008 /* Return the floor of "pa".
8009 */
floor_entry(__isl_take isl_pw_aff * pa,void * user)8010 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8011 {
8012 return isl_pw_aff_floor(pa);
8013 }
8014
8015 /* Given f, return floor(f).
8016 */
isl_union_pw_aff_floor(__isl_take isl_union_pw_aff * upa)8017 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8018 __isl_take isl_union_pw_aff *upa)
8019 {
8020 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8021 }
8022
8023 /* Compute
8024 *
8025 * upa mod m = upa - m * floor(upa/m)
8026 *
8027 * with m an integer value.
8028 */
isl_union_pw_aff_mod_val(__isl_take isl_union_pw_aff * upa,__isl_take isl_val * m)8029 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8030 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8031 {
8032 isl_union_pw_aff *res;
8033
8034 if (!upa || !m)
8035 goto error;
8036
8037 if (!isl_val_is_int(m))
8038 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8039 "expecting integer modulo", goto error);
8040 if (!isl_val_is_pos(m))
8041 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8042 "expecting positive modulo", goto error);
8043
8044 res = isl_union_pw_aff_copy(upa);
8045 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8046 upa = isl_union_pw_aff_floor(upa);
8047 upa = isl_union_pw_aff_scale_val(upa, m);
8048 res = isl_union_pw_aff_sub(res, upa);
8049
8050 return res;
8051 error:
8052 isl_val_free(m);
8053 isl_union_pw_aff_free(upa);
8054 return NULL;
8055 }
8056
8057 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8058 * pos is the output position that needs to be extracted.
8059 * res collects the results.
8060 */
8061 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8062 int pos;
8063 isl_union_pw_aff *res;
8064 };
8065
8066 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8067 * (assuming it has such a dimension) and add it to data->res.
8068 */
get_union_pw_aff(__isl_take isl_pw_multi_aff * pma,void * user)8069 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8070 {
8071 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8072 isl_size n_out;
8073 isl_pw_aff *pa;
8074
8075 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8076 if (n_out < 0)
8077 return isl_stat_error;
8078 if (data->pos >= n_out) {
8079 isl_pw_multi_aff_free(pma);
8080 return isl_stat_ok;
8081 }
8082
8083 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8084 isl_pw_multi_aff_free(pma);
8085
8086 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8087
8088 return data->res ? isl_stat_ok : isl_stat_error;
8089 }
8090
8091 /* Extract an isl_union_pw_aff corresponding to
8092 * output dimension "pos" of "upma".
8093 */
isl_union_pw_multi_aff_get_union_pw_aff(__isl_keep isl_union_pw_multi_aff * upma,int pos)8094 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8095 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8096 {
8097 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8098 isl_space *space;
8099
8100 if (!upma)
8101 return NULL;
8102
8103 if (pos < 0)
8104 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8105 "cannot extract at negative position", return NULL);
8106
8107 space = isl_union_pw_multi_aff_get_space(upma);
8108 data.res = isl_union_pw_aff_empty(space);
8109 data.pos = pos;
8110 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8111 &get_union_pw_aff, &data) < 0)
8112 data.res = isl_union_pw_aff_free(data.res);
8113
8114 return data.res;
8115 }
8116
8117 /* Return a union piecewise affine expression
8118 * that is equal to "aff" on "domain".
8119 */
isl_union_pw_aff_aff_on_domain(__isl_take isl_union_set * domain,__isl_take isl_aff * aff)8120 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8121 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8122 {
8123 isl_pw_aff *pa;
8124
8125 pa = isl_pw_aff_from_aff(aff);
8126 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8127 }
8128
8129 /* Return a union piecewise affine expression
8130 * that is equal to the parameter identified by "id" on "domain".
8131 *
8132 * Make sure the parameter appears in the space passed to
8133 * isl_aff_param_on_domain_space_id.
8134 */
isl_union_pw_aff_param_on_domain_id(__isl_take isl_union_set * domain,__isl_take isl_id * id)8135 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8136 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8137 {
8138 isl_space *space;
8139 isl_aff *aff;
8140
8141 space = isl_union_set_get_space(domain);
8142 space = isl_space_add_param_id(space, isl_id_copy(id));
8143 aff = isl_aff_param_on_domain_space_id(space, id);
8144 return isl_union_pw_aff_aff_on_domain(domain, aff);
8145 }
8146
8147 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8148 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8149 * needs to attain.
8150 * "res" collects the results.
8151 */
8152 struct isl_union_pw_aff_pw_aff_on_domain_data {
8153 isl_pw_aff *pa;
8154 isl_union_pw_aff *res;
8155 };
8156
8157 /* Construct a piecewise affine expression that is equal to data->pa
8158 * on "domain" and add the result to data->res.
8159 */
pw_aff_on_domain(__isl_take isl_set * domain,void * user)8160 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8161 {
8162 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8163 isl_pw_aff *pa;
8164 isl_size dim;
8165
8166 pa = isl_pw_aff_copy(data->pa);
8167 dim = isl_set_dim(domain, isl_dim_set);
8168 if (dim < 0)
8169 pa = isl_pw_aff_free(pa);
8170 pa = isl_pw_aff_from_range(pa);
8171 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8172 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8173 pa = isl_pw_aff_intersect_domain(pa, domain);
8174 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8175
8176 return data->res ? isl_stat_ok : isl_stat_error;
8177 }
8178
8179 /* Return a union piecewise affine expression
8180 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8181 * have been aligned.
8182 *
8183 * Construct an isl_pw_aff on each of the sets in "domain" and
8184 * collect the results.
8185 */
isl_union_pw_aff_pw_aff_on_domain_aligned(__isl_take isl_union_set * domain,__isl_take isl_pw_aff * pa)8186 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8187 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8188 {
8189 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8190 isl_space *space;
8191
8192 space = isl_union_set_get_space(domain);
8193 data.res = isl_union_pw_aff_empty(space);
8194 data.pa = pa;
8195 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8196 data.res = isl_union_pw_aff_free(data.res);
8197 isl_union_set_free(domain);
8198 isl_pw_aff_free(pa);
8199 return data.res;
8200 }
8201
8202 /* Return a union piecewise affine expression
8203 * that is equal to "pa" on "domain".
8204 *
8205 * Check that "pa" is a parametric expression,
8206 * align the parameters if needed and call
8207 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8208 */
isl_union_pw_aff_pw_aff_on_domain(__isl_take isl_union_set * domain,__isl_take isl_pw_aff * pa)8209 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8210 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8211 {
8212 isl_bool is_set;
8213 isl_bool equal_params;
8214 isl_space *domain_space, *pa_space;
8215
8216 pa_space = isl_pw_aff_peek_space(pa);
8217 is_set = isl_space_is_set(pa_space);
8218 if (is_set < 0)
8219 goto error;
8220 if (!is_set)
8221 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8222 "expecting parametric expression", goto error);
8223
8224 domain_space = isl_union_set_get_space(domain);
8225 pa_space = isl_pw_aff_get_space(pa);
8226 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8227 if (equal_params >= 0 && !equal_params) {
8228 isl_space *space;
8229
8230 space = isl_space_align_params(domain_space, pa_space);
8231 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8232 domain = isl_union_set_align_params(domain, space);
8233 } else {
8234 isl_space_free(domain_space);
8235 isl_space_free(pa_space);
8236 }
8237
8238 if (equal_params < 0)
8239 goto error;
8240 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8241 error:
8242 isl_union_set_free(domain);
8243 isl_pw_aff_free(pa);
8244 return NULL;
8245 }
8246
8247 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8248 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8249 * "res" collects the results.
8250 */
8251 struct isl_union_pw_aff_val_on_domain_data {
8252 isl_val *v;
8253 isl_union_pw_aff *res;
8254 };
8255
8256 /* Construct a piecewise affine expression that is equal to data->v
8257 * on "domain" and add the result to data->res.
8258 */
pw_aff_val_on_domain(__isl_take isl_set * domain,void * user)8259 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8260 {
8261 struct isl_union_pw_aff_val_on_domain_data *data = user;
8262 isl_pw_aff *pa;
8263 isl_val *v;
8264
8265 v = isl_val_copy(data->v);
8266 pa = isl_pw_aff_val_on_domain(domain, v);
8267 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8268
8269 return data->res ? isl_stat_ok : isl_stat_error;
8270 }
8271
8272 /* Return a union piecewise affine expression
8273 * that is equal to "v" on "domain".
8274 *
8275 * Construct an isl_pw_aff on each of the sets in "domain" and
8276 * collect the results.
8277 */
isl_union_pw_aff_val_on_domain(__isl_take isl_union_set * domain,__isl_take isl_val * v)8278 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8279 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8280 {
8281 struct isl_union_pw_aff_val_on_domain_data data;
8282 isl_space *space;
8283
8284 space = isl_union_set_get_space(domain);
8285 data.res = isl_union_pw_aff_empty(space);
8286 data.v = v;
8287 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8288 data.res = isl_union_pw_aff_free(data.res);
8289 isl_union_set_free(domain);
8290 isl_val_free(v);
8291 return data.res;
8292 }
8293
8294 /* Construct a piecewise multi affine expression
8295 * that is equal to "pa" and add it to upma.
8296 */
pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff * pa,void * user)8297 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8298 void *user)
8299 {
8300 isl_union_pw_multi_aff **upma = user;
8301 isl_pw_multi_aff *pma;
8302
8303 pma = isl_pw_multi_aff_from_pw_aff(pa);
8304 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8305
8306 return *upma ? isl_stat_ok : isl_stat_error;
8307 }
8308
8309 /* Construct and return a union piecewise multi affine expression
8310 * that is equal to the given union piecewise affine expression.
8311 */
isl_union_pw_multi_aff_from_union_pw_aff(__isl_take isl_union_pw_aff * upa)8312 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8313 __isl_take isl_union_pw_aff *upa)
8314 {
8315 isl_space *space;
8316 isl_union_pw_multi_aff *upma;
8317
8318 if (!upa)
8319 return NULL;
8320
8321 space = isl_union_pw_aff_get_space(upa);
8322 upma = isl_union_pw_multi_aff_empty(space);
8323
8324 if (isl_union_pw_aff_foreach_pw_aff(upa,
8325 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8326 upma = isl_union_pw_multi_aff_free(upma);
8327
8328 isl_union_pw_aff_free(upa);
8329 return upma;
8330 }
8331
8332 /* Compute the set of elements in the domain of "pa" where it is zero and
8333 * add this set to "uset".
8334 */
zero_union_set(__isl_take isl_pw_aff * pa,void * user)8335 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8336 {
8337 isl_union_set **uset = (isl_union_set **)user;
8338
8339 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8340
8341 return *uset ? isl_stat_ok : isl_stat_error;
8342 }
8343
8344 /* Return a union set containing those elements in the domain
8345 * of "upa" where it is zero.
8346 */
isl_union_pw_aff_zero_union_set(__isl_take isl_union_pw_aff * upa)8347 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8348 __isl_take isl_union_pw_aff *upa)
8349 {
8350 isl_union_set *zero;
8351
8352 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8353 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8354 zero = isl_union_set_free(zero);
8355
8356 isl_union_pw_aff_free(upa);
8357 return zero;
8358 }
8359
8360 /* Internal data structure for isl_union_pw_aff_bind_id,
8361 * storing the parameter that needs to be bound and
8362 * the accumulated results.
8363 */
8364 struct isl_bind_id_data {
8365 isl_id *id;
8366 isl_union_set *bound;
8367 };
8368
8369 /* Bind the piecewise affine function "pa" to the parameter data->id,
8370 * adding the resulting elements in the domain where the expression
8371 * is equal to the parameter to data->bound.
8372 */
bind_id(__isl_take isl_pw_aff * pa,void * user)8373 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8374 {
8375 struct isl_bind_id_data *data = user;
8376 isl_set *bound;
8377
8378 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8379 data->bound = isl_union_set_add_set(data->bound, bound);
8380
8381 return data->bound ? isl_stat_ok : isl_stat_error;
8382 }
8383
8384 /* Bind the union piecewise affine function "upa" to the parameter "id",
8385 * returning the elements in the domain where the expression
8386 * is equal to the parameter.
8387 */
isl_union_pw_aff_bind_id(__isl_take isl_union_pw_aff * upa,__isl_take isl_id * id)8388 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8389 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8390 {
8391 struct isl_bind_id_data data = { id };
8392
8393 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8394 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8395 data.bound = isl_union_set_free(data.bound);
8396
8397 isl_union_pw_aff_free(upa);
8398 isl_id_free(id);
8399 return data.bound;
8400 }
8401
8402 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8403 * upma is the function that is plugged in.
8404 * pa is the current part of the function in which upma is plugged in.
8405 * res collects the results.
8406 */
8407 struct isl_union_pw_aff_pullback_upma_data {
8408 isl_union_pw_multi_aff *upma;
8409 isl_pw_aff *pa;
8410 isl_union_pw_aff *res;
8411 };
8412
8413 /* Check if "pma" can be plugged into data->pa.
8414 * If so, perform the pullback and add the result to data->res.
8415 */
pa_pb_pma(__isl_take isl_pw_multi_aff * pma,void * user)8416 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8417 {
8418 struct isl_union_pw_aff_pullback_upma_data *data = user;
8419 isl_pw_aff *pa;
8420
8421 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8422 pma->dim, isl_dim_out)) {
8423 isl_pw_multi_aff_free(pma);
8424 return isl_stat_ok;
8425 }
8426
8427 pa = isl_pw_aff_copy(data->pa);
8428 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8429
8430 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8431
8432 return data->res ? isl_stat_ok : isl_stat_error;
8433 }
8434
8435 /* Check if any of the elements of data->upma can be plugged into pa,
8436 * add if so add the result to data->res.
8437 */
upa_pb_upma(__isl_take isl_pw_aff * pa,void * user)8438 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8439 {
8440 struct isl_union_pw_aff_pullback_upma_data *data = user;
8441 isl_stat r;
8442
8443 data->pa = pa;
8444 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8445 &pa_pb_pma, data);
8446 isl_pw_aff_free(pa);
8447
8448 return r;
8449 }
8450
8451 /* Compute the pullback of "upa" by the function represented by "upma".
8452 * In other words, plug in "upma" in "upa". The result contains
8453 * expressions defined over the domain space of "upma".
8454 *
8455 * Run over all pairs of elements in "upa" and "upma", perform
8456 * the pullback when appropriate and collect the results.
8457 * If the hash value were based on the domain space rather than
8458 * the function space, then we could run through all elements
8459 * of "upma" and directly pick out the corresponding element of "upa".
8460 */
isl_union_pw_aff_pullback_union_pw_multi_aff(__isl_take isl_union_pw_aff * upa,__isl_take isl_union_pw_multi_aff * upma)8461 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8462 __isl_take isl_union_pw_aff *upa,
8463 __isl_take isl_union_pw_multi_aff *upma)
8464 {
8465 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8466 isl_space *space;
8467
8468 space = isl_union_pw_multi_aff_get_space(upma);
8469 upa = isl_union_pw_aff_align_params(upa, space);
8470 space = isl_union_pw_aff_get_space(upa);
8471 upma = isl_union_pw_multi_aff_align_params(upma, space);
8472
8473 if (!upa || !upma)
8474 goto error;
8475
8476 data.upma = upma;
8477 data.res = isl_union_pw_aff_alloc_same_size(upa);
8478 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8479 data.res = isl_union_pw_aff_free(data.res);
8480
8481 isl_union_pw_aff_free(upa);
8482 isl_union_pw_multi_aff_free(upma);
8483 return data.res;
8484 error:
8485 isl_union_pw_aff_free(upa);
8486 isl_union_pw_multi_aff_free(upma);
8487 return NULL;
8488 }
8489
8490 #undef BASE
8491 #define BASE union_pw_aff
8492 #undef DOMBASE
8493 #define DOMBASE union_set
8494
8495 #include <isl_multi_explicit_domain.c>
8496 #include <isl_multi_union_pw_aff_explicit_domain.c>
8497 #include <isl_multi_templ.c>
8498 #include <isl_multi_apply_set.c>
8499 #include <isl_multi_apply_union_set.c>
8500 #include <isl_multi_arith_templ.c>
8501 #include <isl_multi_bind_templ.c>
8502 #include <isl_multi_coalesce.c>
8503 #include <isl_multi_dim_id_templ.c>
8504 #include <isl_multi_floor.c>
8505 #include <isl_multi_from_base_templ.c>
8506 #include <isl_multi_gist.c>
8507 #include <isl_multi_align_set.c>
8508 #include <isl_multi_align_union_set.c>
8509 #include <isl_multi_intersect.c>
8510 #include <isl_multi_nan_templ.c>
8511 #include <isl_multi_tuple_id_templ.c>
8512 #include <isl_multi_union_add_templ.c>
8513
8514 /* Does "mupa" have a non-trivial explicit domain?
8515 *
8516 * The explicit domain, if present, is trivial if it represents
8517 * an (obviously) universe parameter set.
8518 */
isl_multi_union_pw_aff_has_non_trivial_domain(__isl_keep isl_multi_union_pw_aff * mupa)8519 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8520 __isl_keep isl_multi_union_pw_aff *mupa)
8521 {
8522 isl_bool is_params, trivial;
8523 isl_set *set;
8524
8525 if (!mupa)
8526 return isl_bool_error;
8527 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8528 return isl_bool_false;
8529 is_params = isl_union_set_is_params(mupa->u.dom);
8530 if (is_params < 0 || !is_params)
8531 return isl_bool_not(is_params);
8532 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8533 trivial = isl_set_plain_is_universe(set);
8534 isl_set_free(set);
8535 return isl_bool_not(trivial);
8536 }
8537
8538 /* Construct a multiple union piecewise affine expression
8539 * in the given space with value zero in each of the output dimensions.
8540 *
8541 * Since there is no canonical zero value for
8542 * a union piecewise affine expression, we can only construct
8543 * a zero-dimensional "zero" value.
8544 */
isl_multi_union_pw_aff_zero(__isl_take isl_space * space)8545 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8546 __isl_take isl_space *space)
8547 {
8548 isl_bool params;
8549 isl_size dim;
8550
8551 if (!space)
8552 return NULL;
8553
8554 params = isl_space_is_params(space);
8555 if (params < 0)
8556 goto error;
8557 if (params)
8558 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8559 "expecting proper set space", goto error);
8560 if (!isl_space_is_set(space))
8561 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8562 "expecting set space", goto error);
8563 dim = isl_space_dim(space, isl_dim_out);
8564 if (dim < 0)
8565 goto error;
8566 if (dim != 0)
8567 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8568 "expecting 0D space", goto error);
8569
8570 return isl_multi_union_pw_aff_alloc(space);
8571 error:
8572 isl_space_free(space);
8573 return NULL;
8574 }
8575
8576 /* Construct and return a multi union piecewise affine expression
8577 * that is equal to the given multi affine expression.
8578 */
isl_multi_union_pw_aff_from_multi_aff(__isl_take isl_multi_aff * ma)8579 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8580 __isl_take isl_multi_aff *ma)
8581 {
8582 isl_multi_pw_aff *mpa;
8583
8584 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8585 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8586 }
8587
8588 /* Construct and return a multi union piecewise affine expression
8589 * that is equal to the given multi piecewise affine expression.
8590 */
isl_multi_union_pw_aff_from_multi_pw_aff(__isl_take isl_multi_pw_aff * mpa)8591 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8592 __isl_take isl_multi_pw_aff *mpa)
8593 {
8594 int i;
8595 isl_size n;
8596 isl_space *space;
8597 isl_multi_union_pw_aff *mupa;
8598
8599 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8600 if (n < 0)
8601 mpa = isl_multi_pw_aff_free(mpa);
8602 if (!mpa)
8603 return NULL;
8604
8605 space = isl_multi_pw_aff_get_space(mpa);
8606 space = isl_space_range(space);
8607 mupa = isl_multi_union_pw_aff_alloc(space);
8608
8609 for (i = 0; i < n; ++i) {
8610 isl_pw_aff *pa;
8611 isl_union_pw_aff *upa;
8612
8613 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8614 upa = isl_union_pw_aff_from_pw_aff(pa);
8615 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8616 }
8617
8618 isl_multi_pw_aff_free(mpa);
8619
8620 return mupa;
8621 }
8622
8623 /* Extract the range space of "pma" and assign it to *space.
8624 * If *space has already been set (through a previous call to this function),
8625 * then check that the range space is the same.
8626 */
extract_space(__isl_take isl_pw_multi_aff * pma,void * user)8627 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8628 {
8629 isl_space **space = user;
8630 isl_space *pma_space;
8631 isl_bool equal;
8632
8633 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8634 isl_pw_multi_aff_free(pma);
8635
8636 if (!pma_space)
8637 return isl_stat_error;
8638 if (!*space) {
8639 *space = pma_space;
8640 return isl_stat_ok;
8641 }
8642
8643 equal = isl_space_is_equal(pma_space, *space);
8644 isl_space_free(pma_space);
8645
8646 if (equal < 0)
8647 return isl_stat_error;
8648 if (!equal)
8649 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8650 "range spaces not the same", return isl_stat_error);
8651 return isl_stat_ok;
8652 }
8653
8654 /* Construct and return a multi union piecewise affine expression
8655 * that is equal to the given union piecewise multi affine expression.
8656 *
8657 * In order to be able to perform the conversion, the input
8658 * needs to be non-empty and may only involve a single range space.
8659 *
8660 * If the resulting multi union piecewise affine expression has
8661 * an explicit domain, then assign it the domain of the input.
8662 * In other cases, the domain is stored in the individual elements.
8663 */
8664 __isl_give isl_multi_union_pw_aff *
isl_multi_union_pw_aff_from_union_pw_multi_aff(__isl_take isl_union_pw_multi_aff * upma)8665 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8666 __isl_take isl_union_pw_multi_aff *upma)
8667 {
8668 isl_space *space = NULL;
8669 isl_multi_union_pw_aff *mupa;
8670 int i;
8671 isl_size n;
8672
8673 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8674 if (n < 0)
8675 goto error;
8676 if (n == 0)
8677 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8678 "cannot extract range space from empty input",
8679 goto error);
8680 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8681 &space) < 0)
8682 goto error;
8683
8684 if (!space)
8685 goto error;
8686
8687 n = isl_space_dim(space, isl_dim_set);
8688 if (n < 0)
8689 space = isl_space_free(space);
8690 mupa = isl_multi_union_pw_aff_alloc(space);
8691
8692 for (i = 0; i < n; ++i) {
8693 isl_union_pw_aff *upa;
8694
8695 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8696 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8697 }
8698 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8699 isl_union_set *dom;
8700 isl_union_pw_multi_aff *copy;
8701
8702 copy = isl_union_pw_multi_aff_copy(upma);
8703 dom = isl_union_pw_multi_aff_domain(copy);
8704 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8705 }
8706
8707 isl_union_pw_multi_aff_free(upma);
8708 return mupa;
8709 error:
8710 isl_space_free(space);
8711 isl_union_pw_multi_aff_free(upma);
8712 return NULL;
8713 }
8714
8715 /* Try and create an isl_multi_union_pw_aff that is equivalent
8716 * to the given isl_union_map.
8717 * The isl_union_map is required to be single-valued in each space.
8718 * Moreover, it cannot be empty and all range spaces need to be the same.
8719 * Otherwise, an error is produced.
8720 */
isl_multi_union_pw_aff_from_union_map(__isl_take isl_union_map * umap)8721 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8722 __isl_take isl_union_map *umap)
8723 {
8724 isl_union_pw_multi_aff *upma;
8725
8726 upma = isl_union_pw_multi_aff_from_union_map(umap);
8727 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8728 }
8729
8730 /* Return a multiple union piecewise affine expression
8731 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8732 * have been aligned.
8733 *
8734 * If the resulting multi union piecewise affine expression has
8735 * an explicit domain, then assign it the input domain.
8736 * In other cases, the domain is stored in the individual elements.
8737 */
8738 static __isl_give isl_multi_union_pw_aff *
isl_multi_union_pw_aff_multi_val_on_domain_aligned(__isl_take isl_union_set * domain,__isl_take isl_multi_val * mv)8739 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8740 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8741 {
8742 int i;
8743 isl_size n;
8744 isl_space *space;
8745 isl_multi_union_pw_aff *mupa;
8746
8747 n = isl_multi_val_dim(mv, isl_dim_set);
8748 if (!domain || n < 0)
8749 goto error;
8750
8751 space = isl_multi_val_get_space(mv);
8752 mupa = isl_multi_union_pw_aff_alloc(space);
8753 for (i = 0; i < n; ++i) {
8754 isl_val *v;
8755 isl_union_pw_aff *upa;
8756
8757 v = isl_multi_val_get_val(mv, i);
8758 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8759 v);
8760 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8761 }
8762 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8763 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8764 isl_union_set_copy(domain));
8765
8766 isl_union_set_free(domain);
8767 isl_multi_val_free(mv);
8768 return mupa;
8769 error:
8770 isl_union_set_free(domain);
8771 isl_multi_val_free(mv);
8772 return NULL;
8773 }
8774
8775 /* Return a multiple union piecewise affine expression
8776 * that is equal to "mv" on "domain".
8777 */
isl_multi_union_pw_aff_multi_val_on_domain(__isl_take isl_union_set * domain,__isl_take isl_multi_val * mv)8778 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8779 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8780 {
8781 isl_bool equal_params;
8782
8783 if (!domain || !mv)
8784 goto error;
8785 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8786 if (equal_params < 0)
8787 goto error;
8788 if (equal_params)
8789 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8790 domain, mv);
8791 domain = isl_union_set_align_params(domain,
8792 isl_multi_val_get_space(mv));
8793 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8794 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8795 error:
8796 isl_union_set_free(domain);
8797 isl_multi_val_free(mv);
8798 return NULL;
8799 }
8800
8801 /* Return a multiple union piecewise affine expression
8802 * that is equal to "ma" on "domain".
8803 */
isl_multi_union_pw_aff_multi_aff_on_domain(__isl_take isl_union_set * domain,__isl_take isl_multi_aff * ma)8804 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8805 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8806 {
8807 isl_pw_multi_aff *pma;
8808
8809 pma = isl_pw_multi_aff_from_multi_aff(ma);
8810 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8811 }
8812
8813 /* Return a multiple union piecewise affine expression
8814 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8815 * have been aligned.
8816 *
8817 * If the resulting multi union piecewise affine expression has
8818 * an explicit domain, then assign it the input domain.
8819 * In other cases, the domain is stored in the individual elements.
8820 */
8821 static __isl_give isl_multi_union_pw_aff *
isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(__isl_take isl_union_set * domain,__isl_take isl_pw_multi_aff * pma)8822 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8823 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8824 {
8825 int i;
8826 isl_size n;
8827 isl_space *space;
8828 isl_multi_union_pw_aff *mupa;
8829
8830 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8831 if (!domain || n < 0)
8832 goto error;
8833 space = isl_pw_multi_aff_get_space(pma);
8834 mupa = isl_multi_union_pw_aff_alloc(space);
8835 for (i = 0; i < n; ++i) {
8836 isl_pw_aff *pa;
8837 isl_union_pw_aff *upa;
8838
8839 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8840 upa = isl_union_pw_aff_pw_aff_on_domain(
8841 isl_union_set_copy(domain), pa);
8842 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8843 }
8844 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8845 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8846 isl_union_set_copy(domain));
8847
8848 isl_union_set_free(domain);
8849 isl_pw_multi_aff_free(pma);
8850 return mupa;
8851 error:
8852 isl_union_set_free(domain);
8853 isl_pw_multi_aff_free(pma);
8854 return NULL;
8855 }
8856
8857 /* Return a multiple union piecewise affine expression
8858 * that is equal to "pma" on "domain".
8859 */
8860 __isl_give isl_multi_union_pw_aff *
isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set * domain,__isl_take isl_pw_multi_aff * pma)8861 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8862 __isl_take isl_pw_multi_aff *pma)
8863 {
8864 isl_bool equal_params;
8865 isl_space *space;
8866
8867 space = isl_pw_multi_aff_peek_space(pma);
8868 equal_params = isl_union_set_space_has_equal_params(domain, space);
8869 if (equal_params < 0)
8870 goto error;
8871 if (equal_params)
8872 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8873 domain, pma);
8874 domain = isl_union_set_align_params(domain,
8875 isl_pw_multi_aff_get_space(pma));
8876 pma = isl_pw_multi_aff_align_params(pma,
8877 isl_union_set_get_space(domain));
8878 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8879 pma);
8880 error:
8881 isl_union_set_free(domain);
8882 isl_pw_multi_aff_free(pma);
8883 return NULL;
8884 }
8885
8886 /* Return a union set containing those elements in the domains
8887 * of the elements of "mupa" where they are all zero.
8888 *
8889 * If there are no elements, then simply return the entire domain.
8890 */
isl_multi_union_pw_aff_zero_union_set(__isl_take isl_multi_union_pw_aff * mupa)8891 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8892 __isl_take isl_multi_union_pw_aff *mupa)
8893 {
8894 int i;
8895 isl_size n;
8896 isl_union_pw_aff *upa;
8897 isl_union_set *zero;
8898
8899 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8900 if (n < 0)
8901 mupa = isl_multi_union_pw_aff_free(mupa);
8902 if (!mupa)
8903 return NULL;
8904
8905 if (n == 0)
8906 return isl_multi_union_pw_aff_domain(mupa);
8907
8908 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8909 zero = isl_union_pw_aff_zero_union_set(upa);
8910
8911 for (i = 1; i < n; ++i) {
8912 isl_union_set *zero_i;
8913
8914 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8915 zero_i = isl_union_pw_aff_zero_union_set(upa);
8916
8917 zero = isl_union_set_intersect(zero, zero_i);
8918 }
8919
8920 isl_multi_union_pw_aff_free(mupa);
8921 return zero;
8922 }
8923
8924 /* Construct a union map mapping the shared domain
8925 * of the union piecewise affine expressions to the range of "mupa"
8926 * in the special case of a 0D multi union piecewise affine expression.
8927 *
8928 * Construct a map between the explicit domain of "mupa" and
8929 * the range space.
8930 * Note that this assumes that the domain consists of explicit elements.
8931 */
isl_union_map_from_multi_union_pw_aff_0D(__isl_take isl_multi_union_pw_aff * mupa)8932 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8933 __isl_take isl_multi_union_pw_aff *mupa)
8934 {
8935 isl_bool is_params;
8936 isl_space *space;
8937 isl_union_set *dom, *ran;
8938
8939 space = isl_multi_union_pw_aff_get_space(mupa);
8940 dom = isl_multi_union_pw_aff_domain(mupa);
8941 ran = isl_union_set_from_set(isl_set_universe(space));
8942
8943 is_params = isl_union_set_is_params(dom);
8944 if (is_params < 0)
8945 dom = isl_union_set_free(dom);
8946 else if (is_params)
8947 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8948 "cannot create union map from expression without "
8949 "explicit domain elements",
8950 dom = isl_union_set_free(dom));
8951
8952 return isl_union_map_from_domain_and_range(dom, ran);
8953 }
8954
8955 /* Construct a union map mapping the shared domain
8956 * of the union piecewise affine expressions to the range of "mupa"
8957 * with each dimension in the range equated to the
8958 * corresponding union piecewise affine expression.
8959 *
8960 * If the input is zero-dimensional, then construct a mapping
8961 * from its explicit domain.
8962 */
isl_union_map_from_multi_union_pw_aff(__isl_take isl_multi_union_pw_aff * mupa)8963 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8964 __isl_take isl_multi_union_pw_aff *mupa)
8965 {
8966 int i;
8967 isl_size n;
8968 isl_space *space;
8969 isl_union_map *umap;
8970 isl_union_pw_aff *upa;
8971
8972 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8973 if (n < 0)
8974 mupa = isl_multi_union_pw_aff_free(mupa);
8975 if (!mupa)
8976 return NULL;
8977
8978 if (n == 0)
8979 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8980
8981 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8982 umap = isl_union_map_from_union_pw_aff(upa);
8983
8984 for (i = 1; i < n; ++i) {
8985 isl_union_map *umap_i;
8986
8987 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8988 umap_i = isl_union_map_from_union_pw_aff(upa);
8989 umap = isl_union_map_flat_range_product(umap, umap_i);
8990 }
8991
8992 space = isl_multi_union_pw_aff_get_space(mupa);
8993 umap = isl_union_map_reset_range_space(umap, space);
8994
8995 isl_multi_union_pw_aff_free(mupa);
8996 return umap;
8997 }
8998
8999 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9000 * "range" is the space from which to set the range space.
9001 * "res" collects the results.
9002 */
9003 struct isl_union_pw_multi_aff_reset_range_space_data {
9004 isl_space *range;
9005 isl_union_pw_multi_aff *res;
9006 };
9007
9008 /* Replace the range space of "pma" by the range space of data->range and
9009 * add the result to data->res.
9010 */
reset_range_space(__isl_take isl_pw_multi_aff * pma,void * user)9011 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9012 {
9013 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9014 isl_space *space;
9015
9016 space = isl_pw_multi_aff_get_space(pma);
9017 space = isl_space_domain(space);
9018 space = isl_space_extend_domain_with_range(space,
9019 isl_space_copy(data->range));
9020 pma = isl_pw_multi_aff_reset_space(pma, space);
9021 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9022
9023 return data->res ? isl_stat_ok : isl_stat_error;
9024 }
9025
9026 /* Replace the range space of all the piecewise affine expressions in "upma" by
9027 * the range space of "space".
9028 *
9029 * This assumes that all these expressions have the same output dimension.
9030 *
9031 * Since the spaces of the expressions change, so do their hash values.
9032 * We therefore need to create a new isl_union_pw_multi_aff.
9033 * Note that the hash value is currently computed based on the entire
9034 * space even though there can only be a single expression with a given
9035 * domain space.
9036 */
9037 static __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_reset_range_space(__isl_take isl_union_pw_multi_aff * upma,__isl_take isl_space * space)9038 isl_union_pw_multi_aff_reset_range_space(
9039 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9040 {
9041 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9042 isl_space *space_upma;
9043
9044 space_upma = isl_union_pw_multi_aff_get_space(upma);
9045 data.res = isl_union_pw_multi_aff_empty(space_upma);
9046 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9047 &reset_range_space, &data) < 0)
9048 data.res = isl_union_pw_multi_aff_free(data.res);
9049
9050 isl_space_free(space);
9051 isl_union_pw_multi_aff_free(upma);
9052 return data.res;
9053 }
9054
9055 /* Construct and return a union piecewise multi affine expression
9056 * that is equal to the given multi union piecewise affine expression,
9057 * in the special case of a 0D multi union piecewise affine expression.
9058 *
9059 * Construct a union piecewise multi affine expression
9060 * on top of the explicit domain of the input.
9061 */
9062 __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(__isl_take isl_multi_union_pw_aff * mupa)9063 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9064 __isl_take isl_multi_union_pw_aff *mupa)
9065 {
9066 isl_space *space;
9067 isl_multi_val *mv;
9068 isl_union_set *domain;
9069
9070 space = isl_multi_union_pw_aff_get_space(mupa);
9071 mv = isl_multi_val_zero(space);
9072 domain = isl_multi_union_pw_aff_domain(mupa);
9073 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9074 }
9075
9076 /* Construct and return a union piecewise multi affine expression
9077 * that is equal to the given multi union piecewise affine expression.
9078 *
9079 * If the input is zero-dimensional, then
9080 * construct a union piecewise multi affine expression
9081 * on top of the explicit domain of the input.
9082 */
9083 __isl_give isl_union_pw_multi_aff *
isl_union_pw_multi_aff_from_multi_union_pw_aff(__isl_take isl_multi_union_pw_aff * mupa)9084 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9085 __isl_take isl_multi_union_pw_aff *mupa)
9086 {
9087 int i;
9088 isl_size n;
9089 isl_space *space;
9090 isl_union_pw_multi_aff *upma;
9091 isl_union_pw_aff *upa;
9092
9093 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9094 if (n < 0)
9095 mupa = isl_multi_union_pw_aff_free(mupa);
9096 if (!mupa)
9097 return NULL;
9098
9099 if (n == 0)
9100 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9101
9102 space = isl_multi_union_pw_aff_get_space(mupa);
9103 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9104 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9105
9106 for (i = 1; i < n; ++i) {
9107 isl_union_pw_multi_aff *upma_i;
9108
9109 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9110 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9111 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9112 }
9113
9114 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9115
9116 isl_multi_union_pw_aff_free(mupa);
9117 return upma;
9118 }
9119
9120 /* Intersect the range of "mupa" with "range",
9121 * in the special case where "mupa" is 0D.
9122 *
9123 * Intersect the domain of "mupa" with the constraints on the parameters
9124 * of "range".
9125 */
mupa_intersect_range_0D(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_set * range)9126 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9127 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9128 {
9129 range = isl_set_params(range);
9130 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9131 return mupa;
9132 }
9133
9134 /* Intersect the range of "mupa" with "range".
9135 * That is, keep only those domain elements that have a function value
9136 * in "range".
9137 */
isl_multi_union_pw_aff_intersect_range(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_set * range)9138 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9139 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9140 {
9141 isl_union_pw_multi_aff *upma;
9142 isl_union_set *domain;
9143 isl_space *space;
9144 isl_size n;
9145 int match;
9146
9147 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9148 if (n < 0 || !range)
9149 goto error;
9150
9151 space = isl_set_get_space(range);
9152 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9153 space, isl_dim_set);
9154 isl_space_free(space);
9155 if (match < 0)
9156 goto error;
9157 if (!match)
9158 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9159 "space don't match", goto error);
9160 if (n == 0)
9161 return mupa_intersect_range_0D(mupa, range);
9162
9163 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9164 isl_multi_union_pw_aff_copy(mupa));
9165 domain = isl_union_set_from_set(range);
9166 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9167 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9168
9169 return mupa;
9170 error:
9171 isl_multi_union_pw_aff_free(mupa);
9172 isl_set_free(range);
9173 return NULL;
9174 }
9175
9176 /* Return the shared domain of the elements of "mupa",
9177 * in the special case where "mupa" is zero-dimensional.
9178 *
9179 * Return the explicit domain of "mupa".
9180 * Note that this domain may be a parameter set, either
9181 * because "mupa" is meant to live in a set space or
9182 * because no explicit domain has been set.
9183 */
isl_multi_union_pw_aff_domain_0D(__isl_take isl_multi_union_pw_aff * mupa)9184 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9185 __isl_take isl_multi_union_pw_aff *mupa)
9186 {
9187 isl_union_set *dom;
9188
9189 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9190 isl_multi_union_pw_aff_free(mupa);
9191
9192 return dom;
9193 }
9194
9195 /* Return the shared domain of the elements of "mupa".
9196 *
9197 * If "mupa" is zero-dimensional, then return its explicit domain.
9198 */
isl_multi_union_pw_aff_domain(__isl_take isl_multi_union_pw_aff * mupa)9199 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9200 __isl_take isl_multi_union_pw_aff *mupa)
9201 {
9202 int i;
9203 isl_size n;
9204 isl_union_pw_aff *upa;
9205 isl_union_set *dom;
9206
9207 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9208 if (n < 0)
9209 mupa = isl_multi_union_pw_aff_free(mupa);
9210 if (!mupa)
9211 return NULL;
9212
9213 if (n == 0)
9214 return isl_multi_union_pw_aff_domain_0D(mupa);
9215
9216 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9217 dom = isl_union_pw_aff_domain(upa);
9218 for (i = 1; i < n; ++i) {
9219 isl_union_set *dom_i;
9220
9221 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9222 dom_i = isl_union_pw_aff_domain(upa);
9223 dom = isl_union_set_intersect(dom, dom_i);
9224 }
9225
9226 isl_multi_union_pw_aff_free(mupa);
9227 return dom;
9228 }
9229
9230 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9231 * In particular, the spaces have been aligned.
9232 * The result is defined over the shared domain of the elements of "mupa"
9233 *
9234 * We first extract the parametric constant part of "aff" and
9235 * define that over the shared domain.
9236 * Then we iterate over all input dimensions of "aff" and add the corresponding
9237 * multiples of the elements of "mupa".
9238 * Finally, we consider the integer divisions, calling the function
9239 * recursively to obtain an isl_union_pw_aff corresponding to the
9240 * integer division argument.
9241 */
multi_union_pw_aff_apply_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_aff * aff)9242 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9243 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9244 {
9245 int i;
9246 isl_size n_in, n_div;
9247 isl_union_pw_aff *upa;
9248 isl_union_set *uset;
9249 isl_val *v;
9250 isl_aff *cst;
9251
9252 n_in = isl_aff_dim(aff, isl_dim_in);
9253 n_div = isl_aff_dim(aff, isl_dim_div);
9254 if (n_in < 0 || n_div < 0)
9255 goto error;
9256
9257 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9258 cst = isl_aff_copy(aff);
9259 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9260 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9261 cst = isl_aff_project_domain_on_params(cst);
9262 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9263
9264 for (i = 0; i < n_in; ++i) {
9265 isl_union_pw_aff *upa_i;
9266
9267 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9268 continue;
9269 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9270 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9271 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9272 upa = isl_union_pw_aff_add(upa, upa_i);
9273 }
9274
9275 for (i = 0; i < n_div; ++i) {
9276 isl_aff *div;
9277 isl_union_pw_aff *upa_i;
9278
9279 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9280 continue;
9281 div = isl_aff_get_div(aff, i);
9282 upa_i = multi_union_pw_aff_apply_aff(
9283 isl_multi_union_pw_aff_copy(mupa), div);
9284 upa_i = isl_union_pw_aff_floor(upa_i);
9285 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9286 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9287 upa = isl_union_pw_aff_add(upa, upa_i);
9288 }
9289
9290 isl_multi_union_pw_aff_free(mupa);
9291 isl_aff_free(aff);
9292
9293 return upa;
9294 error:
9295 isl_multi_union_pw_aff_free(mupa);
9296 isl_aff_free(aff);
9297 return NULL;
9298 }
9299
9300 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9301 * with the domain of "aff".
9302 * Furthermore, the dimension of this space needs to be greater than zero.
9303 * The result is defined over the shared domain of the elements of "mupa"
9304 *
9305 * We perform these checks and then hand over control to
9306 * multi_union_pw_aff_apply_aff.
9307 */
isl_multi_union_pw_aff_apply_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_aff * aff)9308 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9309 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9310 {
9311 isl_size dim;
9312 isl_space *space1, *space2;
9313 isl_bool equal;
9314
9315 mupa = isl_multi_union_pw_aff_align_params(mupa,
9316 isl_aff_get_space(aff));
9317 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9318 if (!mupa || !aff)
9319 goto error;
9320
9321 space1 = isl_multi_union_pw_aff_get_space(mupa);
9322 space2 = isl_aff_get_domain_space(aff);
9323 equal = isl_space_is_equal(space1, space2);
9324 isl_space_free(space1);
9325 isl_space_free(space2);
9326 if (equal < 0)
9327 goto error;
9328 if (!equal)
9329 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9330 "spaces don't match", goto error);
9331 dim = isl_aff_dim(aff, isl_dim_in);
9332 if (dim < 0)
9333 goto error;
9334 if (dim == 0)
9335 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9336 "cannot determine domains", goto error);
9337
9338 return multi_union_pw_aff_apply_aff(mupa, aff);
9339 error:
9340 isl_multi_union_pw_aff_free(mupa);
9341 isl_aff_free(aff);
9342 return NULL;
9343 }
9344
9345 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9346 * The space of "mupa" is known to be compatible with the domain of "ma".
9347 *
9348 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9349 * on the domain of "mupa".
9350 */
mupa_apply_multi_aff_0D(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_multi_aff * ma)9351 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9352 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9353 {
9354 isl_union_set *dom;
9355
9356 dom = isl_multi_union_pw_aff_domain(mupa);
9357 ma = isl_multi_aff_project_domain_on_params(ma);
9358
9359 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9360 }
9361
9362 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9363 * with the domain of "ma".
9364 * The result is defined over the shared domain of the elements of "mupa"
9365 */
isl_multi_union_pw_aff_apply_multi_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_multi_aff * ma)9366 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9367 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9368 {
9369 isl_space *space1, *space2;
9370 isl_multi_union_pw_aff *res;
9371 isl_bool equal;
9372 int i;
9373 isl_size n_in, n_out;
9374
9375 mupa = isl_multi_union_pw_aff_align_params(mupa,
9376 isl_multi_aff_get_space(ma));
9377 ma = isl_multi_aff_align_params(ma,
9378 isl_multi_union_pw_aff_get_space(mupa));
9379 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9380 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9381 if (!mupa || n_in < 0 || n_out < 0)
9382 goto error;
9383
9384 space1 = isl_multi_union_pw_aff_get_space(mupa);
9385 space2 = isl_multi_aff_get_domain_space(ma);
9386 equal = isl_space_is_equal(space1, space2);
9387 isl_space_free(space1);
9388 isl_space_free(space2);
9389 if (equal < 0)
9390 goto error;
9391 if (!equal)
9392 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9393 "spaces don't match", goto error);
9394 if (n_in == 0)
9395 return mupa_apply_multi_aff_0D(mupa, ma);
9396
9397 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9398 res = isl_multi_union_pw_aff_alloc(space1);
9399
9400 for (i = 0; i < n_out; ++i) {
9401 isl_aff *aff;
9402 isl_union_pw_aff *upa;
9403
9404 aff = isl_multi_aff_get_aff(ma, i);
9405 upa = multi_union_pw_aff_apply_aff(
9406 isl_multi_union_pw_aff_copy(mupa), aff);
9407 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9408 }
9409
9410 isl_multi_aff_free(ma);
9411 isl_multi_union_pw_aff_free(mupa);
9412 return res;
9413 error:
9414 isl_multi_union_pw_aff_free(mupa);
9415 isl_multi_aff_free(ma);
9416 return NULL;
9417 }
9418
9419 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9420 * The space of "mupa" is known to be compatible with the domain of "pa".
9421 *
9422 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9423 * on the domain of "mupa".
9424 */
isl_multi_union_pw_aff_apply_pw_aff_0D(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_pw_aff * pa)9425 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9426 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9427 {
9428 isl_union_set *dom;
9429
9430 dom = isl_multi_union_pw_aff_domain(mupa);
9431 pa = isl_pw_aff_project_domain_on_params(pa);
9432
9433 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9434 }
9435
9436 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9437 * with the domain of "pa".
9438 * Furthermore, the dimension of this space needs to be greater than zero.
9439 * The result is defined over the shared domain of the elements of "mupa"
9440 */
isl_multi_union_pw_aff_apply_pw_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_pw_aff * pa)9441 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9442 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9443 {
9444 int i;
9445 isl_bool equal;
9446 isl_size n_in;
9447 isl_space *space, *space2;
9448 isl_union_pw_aff *upa;
9449
9450 mupa = isl_multi_union_pw_aff_align_params(mupa,
9451 isl_pw_aff_get_space(pa));
9452 pa = isl_pw_aff_align_params(pa,
9453 isl_multi_union_pw_aff_get_space(mupa));
9454 if (!mupa || !pa)
9455 goto error;
9456
9457 space = isl_multi_union_pw_aff_get_space(mupa);
9458 space2 = isl_pw_aff_get_domain_space(pa);
9459 equal = isl_space_is_equal(space, space2);
9460 isl_space_free(space);
9461 isl_space_free(space2);
9462 if (equal < 0)
9463 goto error;
9464 if (!equal)
9465 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9466 "spaces don't match", goto error);
9467 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9468 if (n_in < 0)
9469 goto error;
9470 if (n_in == 0)
9471 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9472
9473 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9474 upa = isl_union_pw_aff_empty(space);
9475
9476 for (i = 0; i < pa->n; ++i) {
9477 isl_aff *aff;
9478 isl_set *domain;
9479 isl_multi_union_pw_aff *mupa_i;
9480 isl_union_pw_aff *upa_i;
9481
9482 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9483 domain = isl_set_copy(pa->p[i].set);
9484 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9485 aff = isl_aff_copy(pa->p[i].aff);
9486 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9487 upa = isl_union_pw_aff_union_add(upa, upa_i);
9488 }
9489
9490 isl_multi_union_pw_aff_free(mupa);
9491 isl_pw_aff_free(pa);
9492 return upa;
9493 error:
9494 isl_multi_union_pw_aff_free(mupa);
9495 isl_pw_aff_free(pa);
9496 return NULL;
9497 }
9498
9499 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9500 * The space of "mupa" is known to be compatible with the domain of "pma".
9501 *
9502 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9503 * on the domain of "mupa".
9504 */
mupa_apply_pw_multi_aff_0D(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_pw_multi_aff * pma)9505 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9506 __isl_take isl_multi_union_pw_aff *mupa,
9507 __isl_take isl_pw_multi_aff *pma)
9508 {
9509 isl_union_set *dom;
9510
9511 dom = isl_multi_union_pw_aff_domain(mupa);
9512 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9513
9514 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9515 }
9516
9517 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9518 * with the domain of "pma".
9519 * The result is defined over the shared domain of the elements of "mupa"
9520 */
isl_multi_union_pw_aff_apply_pw_multi_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_pw_multi_aff * pma)9521 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9522 __isl_take isl_multi_union_pw_aff *mupa,
9523 __isl_take isl_pw_multi_aff *pma)
9524 {
9525 isl_space *space1, *space2;
9526 isl_multi_union_pw_aff *res;
9527 isl_bool equal;
9528 int i;
9529 isl_size n_in, n_out;
9530
9531 mupa = isl_multi_union_pw_aff_align_params(mupa,
9532 isl_pw_multi_aff_get_space(pma));
9533 pma = isl_pw_multi_aff_align_params(pma,
9534 isl_multi_union_pw_aff_get_space(mupa));
9535 if (!mupa || !pma)
9536 goto error;
9537
9538 space1 = isl_multi_union_pw_aff_get_space(mupa);
9539 space2 = isl_pw_multi_aff_get_domain_space(pma);
9540 equal = isl_space_is_equal(space1, space2);
9541 isl_space_free(space1);
9542 isl_space_free(space2);
9543 if (equal < 0)
9544 goto error;
9545 if (!equal)
9546 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9547 "spaces don't match", goto error);
9548 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9549 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9550 if (n_in < 0 || n_out < 0)
9551 goto error;
9552 if (n_in == 0)
9553 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9554
9555 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9556 res = isl_multi_union_pw_aff_alloc(space1);
9557
9558 for (i = 0; i < n_out; ++i) {
9559 isl_pw_aff *pa;
9560 isl_union_pw_aff *upa;
9561
9562 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9563 upa = isl_multi_union_pw_aff_apply_pw_aff(
9564 isl_multi_union_pw_aff_copy(mupa), pa);
9565 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9566 }
9567
9568 isl_pw_multi_aff_free(pma);
9569 isl_multi_union_pw_aff_free(mupa);
9570 return res;
9571 error:
9572 isl_multi_union_pw_aff_free(mupa);
9573 isl_pw_multi_aff_free(pma);
9574 return NULL;
9575 }
9576
9577 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9578 * If the explicit domain only keeps track of constraints on the parameters,
9579 * then only update those constraints.
9580 */
preimage_explicit_domain(__isl_take isl_multi_union_pw_aff * mupa,__isl_keep isl_union_pw_multi_aff * upma)9581 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9582 __isl_take isl_multi_union_pw_aff *mupa,
9583 __isl_keep isl_union_pw_multi_aff *upma)
9584 {
9585 isl_bool is_params;
9586
9587 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9588 return isl_multi_union_pw_aff_free(mupa);
9589
9590 mupa = isl_multi_union_pw_aff_cow(mupa);
9591 if (!mupa)
9592 return NULL;
9593
9594 is_params = isl_union_set_is_params(mupa->u.dom);
9595 if (is_params < 0)
9596 return isl_multi_union_pw_aff_free(mupa);
9597
9598 upma = isl_union_pw_multi_aff_copy(upma);
9599 if (is_params)
9600 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9601 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9602 else
9603 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9604 mupa->u.dom, upma);
9605 if (!mupa->u.dom)
9606 return isl_multi_union_pw_aff_free(mupa);
9607 return mupa;
9608 }
9609
9610 /* Compute the pullback of "mupa" by the function represented by "upma".
9611 * In other words, plug in "upma" in "mupa". The result contains
9612 * expressions defined over the domain space of "upma".
9613 *
9614 * Run over all elements of "mupa" and plug in "upma" in each of them.
9615 *
9616 * If "mupa" has an explicit domain, then it is this domain
9617 * that needs to undergo a pullback instead, i.e., a preimage.
9618 */
9619 __isl_give isl_multi_union_pw_aff *
isl_multi_union_pw_aff_pullback_union_pw_multi_aff(__isl_take isl_multi_union_pw_aff * mupa,__isl_take isl_union_pw_multi_aff * upma)9620 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9621 __isl_take isl_multi_union_pw_aff *mupa,
9622 __isl_take isl_union_pw_multi_aff *upma)
9623 {
9624 int i;
9625 isl_size n;
9626
9627 mupa = isl_multi_union_pw_aff_align_params(mupa,
9628 isl_union_pw_multi_aff_get_space(upma));
9629 upma = isl_union_pw_multi_aff_align_params(upma,
9630 isl_multi_union_pw_aff_get_space(mupa));
9631 mupa = isl_multi_union_pw_aff_cow(mupa);
9632 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9633 if (n < 0 || !upma)
9634 goto error;
9635
9636 for (i = 0; i < n; ++i) {
9637 isl_union_pw_aff *upa;
9638
9639 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9640 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9641 isl_union_pw_multi_aff_copy(upma));
9642 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9643 }
9644
9645 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9646 mupa = preimage_explicit_domain(mupa, upma);
9647
9648 isl_union_pw_multi_aff_free(upma);
9649 return mupa;
9650 error:
9651 isl_multi_union_pw_aff_free(mupa);
9652 isl_union_pw_multi_aff_free(upma);
9653 return NULL;
9654 }
9655
9656 /* Extract the sequence of elements in "mupa" with domain space "space"
9657 * (ignoring parameters).
9658 *
9659 * For the elements of "mupa" that are not defined on the specified space,
9660 * the corresponding element in the result is empty.
9661 */
isl_multi_union_pw_aff_extract_multi_pw_aff(__isl_keep isl_multi_union_pw_aff * mupa,__isl_take isl_space * space)9662 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9663 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9664 {
9665 int i;
9666 isl_size n;
9667 isl_space *space_mpa;
9668 isl_multi_pw_aff *mpa;
9669
9670 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9671 if (n < 0 || !space)
9672 goto error;
9673
9674 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9675 space = isl_space_replace_params(space, space_mpa);
9676 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9677 space_mpa);
9678 mpa = isl_multi_pw_aff_alloc(space_mpa);
9679
9680 space = isl_space_from_domain(space);
9681 space = isl_space_add_dims(space, isl_dim_out, 1);
9682 for (i = 0; i < n; ++i) {
9683 isl_union_pw_aff *upa;
9684 isl_pw_aff *pa;
9685
9686 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9687 pa = isl_union_pw_aff_extract_pw_aff(upa,
9688 isl_space_copy(space));
9689 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9690 isl_union_pw_aff_free(upa);
9691 }
9692
9693 isl_space_free(space);
9694 return mpa;
9695 error:
9696 isl_space_free(space);
9697 return NULL;
9698 }
9699
9700 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9701 * should modify the base expressions in the input.
9702 *
9703 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9704 * are taken into account.
9705 * "fn" is applied to each entry in the input.
9706 */
9707 struct isl_union_pw_multi_aff_un_op_control {
9708 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9709 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9710 };
9711
9712 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9713 * a second argument) for use as an isl_union_pw_multi_aff_transform
9714 * filter function (which does take a second argument).
9715 * Simply call control->filter without the second argument.
9716 */
isl_union_pw_multi_aff_un_op_filter_drop_user(__isl_take isl_pw_multi_aff * pma,void * user)9717 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9718 __isl_take isl_pw_multi_aff *pma, void *user)
9719 {
9720 struct isl_union_pw_multi_aff_un_op_control *control = user;
9721
9722 return control->filter(pma);
9723 }
9724
9725 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9726 * a second argument) for use as an isl_union_pw_multi_aff_transform
9727 * base function (which does take a second argument).
9728 * Simply call control->fn without the second argument.
9729 */
isl_union_pw_multi_aff_un_op_drop_user(__isl_take isl_pw_multi_aff * pma,void * user)9730 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9731 __isl_take isl_pw_multi_aff *pma, void *user)
9732 {
9733 struct isl_union_pw_multi_aff_un_op_control *control = user;
9734
9735 return control->fn(pma);
9736 }
9737
9738 /* Construct an isl_union_pw_multi_aff that is obtained by
9739 * modifying "upma" according to "control".
9740 *
9741 * isl_union_pw_multi_aff_transform performs essentially
9742 * the same operation, but takes a filter and a callback function
9743 * of a different form (with an extra argument).
9744 * Call isl_union_pw_multi_aff_transform with wrappers
9745 * that remove this extra argument.
9746 */
isl_union_pw_multi_aff_un_op(__isl_take isl_union_pw_multi_aff * upma,struct isl_union_pw_multi_aff_un_op_control * control)9747 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9748 __isl_take isl_union_pw_multi_aff *upma,
9749 struct isl_union_pw_multi_aff_un_op_control *control)
9750 {
9751 struct isl_union_pw_multi_aff_transform_control t_control = {
9752 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9753 .filter_user = control,
9754 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9755 .fn_user = control,
9756 };
9757
9758 return isl_union_pw_multi_aff_transform(upma, &t_control);
9759 }
9760
9761 /* For each function in "upma" of the form A -> [B -> C],
9762 * extract the function A -> B and collect the results.
9763 */
isl_union_pw_multi_aff_range_factor_domain(__isl_take isl_union_pw_multi_aff * upma)9764 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9765 __isl_take isl_union_pw_multi_aff *upma)
9766 {
9767 struct isl_union_pw_multi_aff_un_op_control control = {
9768 .filter = &isl_pw_multi_aff_range_is_wrapping,
9769 .fn = &isl_pw_multi_aff_range_factor_domain,
9770 };
9771 return isl_union_pw_multi_aff_un_op(upma, &control);
9772 }
9773
9774 /* For each function in "upma" of the form A -> [B -> C],
9775 * extract the function A -> C and collect the results.
9776 */
isl_union_pw_multi_aff_range_factor_range(__isl_take isl_union_pw_multi_aff * upma)9777 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9778 __isl_take isl_union_pw_multi_aff *upma)
9779 {
9780 struct isl_union_pw_multi_aff_un_op_control control = {
9781 .filter = &isl_pw_multi_aff_range_is_wrapping,
9782 .fn = &isl_pw_multi_aff_range_factor_range,
9783 };
9784 return isl_union_pw_multi_aff_un_op(upma, &control);
9785 }
9786
9787 /* Evaluate the affine function "aff" in the void point "pnt".
9788 * In particular, return the value NaN.
9789 */
eval_void(__isl_take isl_aff * aff,__isl_take isl_point * pnt)9790 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9791 __isl_take isl_point *pnt)
9792 {
9793 isl_ctx *ctx;
9794
9795 ctx = isl_point_get_ctx(pnt);
9796 isl_aff_free(aff);
9797 isl_point_free(pnt);
9798 return isl_val_nan(ctx);
9799 }
9800
9801 /* Evaluate the affine expression "aff"
9802 * in the coordinates (with denominator) "pnt".
9803 */
eval(__isl_keep isl_vec * aff,__isl_keep isl_vec * pnt)9804 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9805 __isl_keep isl_vec *pnt)
9806 {
9807 isl_int n, d;
9808 isl_ctx *ctx;
9809 isl_val *v;
9810
9811 if (!aff || !pnt)
9812 return NULL;
9813
9814 ctx = isl_vec_get_ctx(aff);
9815 isl_int_init(n);
9816 isl_int_init(d);
9817 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9818 isl_int_mul(d, aff->el[0], pnt->el[0]);
9819 v = isl_val_rat_from_isl_int(ctx, n, d);
9820 v = isl_val_normalize(v);
9821 isl_int_clear(n);
9822 isl_int_clear(d);
9823
9824 return v;
9825 }
9826
9827 /* Check that the domain space of "aff" is equal to "space".
9828 */
isl_aff_check_has_domain_space(__isl_keep isl_aff * aff,__isl_keep isl_space * space)9829 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9830 __isl_keep isl_space *space)
9831 {
9832 isl_bool ok;
9833
9834 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9835 if (ok < 0)
9836 return isl_stat_error;
9837 if (!ok)
9838 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9839 "incompatible spaces", return isl_stat_error);
9840 return isl_stat_ok;
9841 }
9842
9843 /* Evaluate the affine function "aff" in "pnt".
9844 */
isl_aff_eval(__isl_take isl_aff * aff,__isl_take isl_point * pnt)9845 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9846 __isl_take isl_point *pnt)
9847 {
9848 isl_bool is_void;
9849 isl_val *v;
9850 isl_local_space *ls;
9851
9852 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9853 goto error;
9854 is_void = isl_point_is_void(pnt);
9855 if (is_void < 0)
9856 goto error;
9857 if (is_void)
9858 return eval_void(aff, pnt);
9859
9860 ls = isl_aff_get_domain_local_space(aff);
9861 pnt = isl_local_space_lift_point(ls, pnt);
9862
9863 v = eval(aff->v, isl_point_peek_vec(pnt));
9864
9865 isl_aff_free(aff);
9866 isl_point_free(pnt);
9867
9868 return v;
9869 error:
9870 isl_aff_free(aff);
9871 isl_point_free(pnt);
9872 return NULL;
9873 }
9874