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