Bug Summary

File:tools/polly/lib/External/isl/isl_aff.c
Warning:line 5466, column 3
Value stored to 'o_src' is never read

Annotated Source Code

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