Bug Summary

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