Bug Summary

File:tools/polly/lib/External/isl/isl_aff.c
Warning:line 8995, column 2
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name isl_aff.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/include -I /usr/include/jsoncpp -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/include -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=gnu99 -fconst-strings -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-7~svn329677=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c /build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_aff.c

/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_aff.c

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

/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c

1/*
2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
4 *
5 * Use of this software is governed by the MIT license
6 *
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 */
12
13__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,cow)isl_union_pw_multi_aff_cow(__isl_take UNIONisl_union_pw_multi_aff *u);
14
15isl_ctx *FN(UNION,get_ctx)isl_union_pw_multi_aff_get_ctx(__isl_keep UNIONisl_union_pw_multi_aff *u)
16{
17 return u ? u->space->ctx : NULL((void*)0);
18}
19
20__isl_give isl_space *FN(UNION,get_space)isl_union_pw_multi_aff_get_space(__isl_keep UNIONisl_union_pw_multi_aff *u)
21{
22 if (!u)
2
Assuming 'u' is non-null
3
Taking false branch
23 return NULL((void*)0);
24 return isl_space_copy(u->space);
25}
26
27/* Return the number of parameters of "u", where "type"
28 * is required to be set to isl_dim_param.
29 */
30unsigned FN(UNION,dim)isl_union_pw_multi_aff_dim(__isl_keep UNIONisl_union_pw_multi_aff *u, enum isl_dim_type type)
31{
32 if (!u)
33 return 0;
34
35 if (type != isl_dim_param)
36 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 37); return 0; } while (0)
37 "can only reference parameters", return 0)do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 37); return 0; } while (0)
;
38
39 return isl_space_dim(u->space, type);
40}
41
42/* Return the position of the parameter with the given name
43 * in "u".
44 * Return -1 if no such dimension can be found.
45 */
46int FN(UNION,find_dim_by_name)isl_union_pw_multi_aff_find_dim_by_name(__isl_keep UNIONisl_union_pw_multi_aff *u, enum isl_dim_type type,
47 const char *name)
48{
49 if (!u)
50 return -1;
51 return isl_space_find_dim_by_name(u->space, type, name);
52}
53
54#ifdef HAS_TYPE
55static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,alloc)isl_union_pw_multi_aff_alloc(__isl_take isl_space *dim,
56 enum isl_fold type, int size)
57#else
58static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,alloc)isl_union_pw_multi_aff_alloc(__isl_take isl_space *dim, int size)
59#endif
60{
61 UNIONisl_union_pw_multi_aff *u;
62
63 dim = isl_space_params(dim);
64 if (!dim)
65 return NULL((void*)0);
66
67 u = isl_calloc_type(dim->ctx, UNION)((isl_union_pw_multi_aff *)isl_calloc_or_die(dim->ctx, 1, sizeof
(isl_union_pw_multi_aff)))
;
68 if (!u)
69 goto error;
70
71 u->ref = 1;
72#ifdef HAS_TYPE
73 u->type = type;
74#endif
75 u->space = dim;
76 if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
77 return FN(UNION,free)isl_union_pw_multi_aff_free(u);
78
79 return u;
80error:
81 isl_space_free(dim);
82 return NULL((void*)0);
83}
84
85#ifdef HAS_TYPE
86__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,ZERO)isl_union_pw_multi_aff_empty(__isl_take isl_space *dim, enum isl_fold type)
87{
88 return FN(UNION,alloc)isl_union_pw_multi_aff_alloc(dim, type, 16);
89}
90#else
91__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,ZERO)isl_union_pw_multi_aff_empty(__isl_take isl_space *dim)
92{
93 return FN(UNION,alloc)isl_union_pw_multi_aff_alloc(dim, 16);
94}
95#endif
96
97__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,copy)isl_union_pw_multi_aff_copy(__isl_keep UNIONisl_union_pw_multi_aff *u)
98{
99 if (!u)
100 return NULL((void*)0);
101
102 u->ref++;
103 return u;
104}
105
106/* Extract the element of "u" living in "space" (ignoring parameters).
107 *
108 * Return the ZERO element if "u" does not contain any element
109 * living in "space".
110 */
111__isl_give PARTisl_pw_multi_aff *FN(FN(UNION,extract),PARTS)isl_union_pw_multi_aff_extract_pw_multi_aff(__isl_keep UNIONisl_union_pw_multi_aff *u,
112 __isl_take isl_space *space)
113{
114 struct isl_hash_table_entry *entry;
115 isl_bool equal_params;
116
117 if (!u || !space)
118 goto error;
119 equal_params = isl_space_has_equal_params(u->space, space);
120 if (equal_params < 0)
121 goto error;
122 if (!equal_params) {
123 space = isl_space_drop_dims(space, isl_dim_param,
124 0, isl_space_dim(space, isl_dim_param));
125 space = isl_space_align_params(space,
126 FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
127 if (!space)
128 goto error;
129 }
130
131 entry = FN(UNION,find_part_entry)isl_union_pw_multi_aff_find_part_entry(u, space, 0);
132 if (!entry)
133 goto error;
134 if (entry == isl_hash_table_entry_none)
135#ifdef HAS_TYPE
136 return FN(PART,ZERO)isl_pw_multi_aff_empty(space, u->type);
137#else
138 return FN(PART,ZERO)isl_pw_multi_aff_empty(space);
139#endif
140 isl_space_free(space);
141 return FN(PART,copy)isl_pw_multi_aff_copy(entry->data);
142error:
143 isl_space_free(space);
144 return NULL((void*)0);
145}
146
147/* Add "part" to "u".
148 * If "disjoint" is set, then "u" is not allowed to already have
149 * a part that is defined over a domain that overlaps with the domain
150 * of "part".
151 * Otherwise, compute the union sum of "part" and the part in "u"
152 * defined on the same space.
153 */
154static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,add_part_generic)isl_union_pw_multi_aff_add_part_generic(__isl_take UNIONisl_union_pw_multi_aff *u,
155 __isl_take PARTisl_pw_multi_aff *part, int disjoint)
156{
157 int empty;
158 struct isl_hash_table_entry *entry;
159
160 if (!part)
161 goto error;
162
163 empty = FN(PART,IS_ZERO)isl_pw_multi_aff_is_empty(part);
164 if (empty < 0)
165 goto error;
166 if (empty) {
167 FN(PART,free)isl_pw_multi_aff_free(part);
168 return u;
169 }
170
171 u = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u, FN(PART,get_space)isl_pw_multi_aff_get_space(part));
172 part = FN(PART,align_params)isl_pw_multi_aff_align_params(part, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
173
174 u = FN(UNION,cow)isl_union_pw_multi_aff_cow(u);
175
176 if (!u)
177 goto error;
178
179 if (FN(UNION,check_disjoint_domain_other)isl_union_pw_multi_aff_check_disjoint_domain_other(u, part) < 0)
180 goto error;
181 entry = FN(UNION,find_part_entry)isl_union_pw_multi_aff_find_part_entry(u, part->dim, 1);
182 if (!entry)
183 goto error;
184
185 if (!entry->data)
186 entry->data = part;
187 else {
188 if (disjoint &&
189 FN(UNION,check_disjoint_domain)isl_union_pw_multi_aff_check_disjoint_domain(entry->data, part) < 0)
190 goto error;
191 entry->data = FN(PART,union_add_)isl_pw_multi_aff_union_add_(entry->data,
192 FN(PART,copy)isl_pw_multi_aff_copy(part));
193 if (!entry->data)
194 goto error;
195 empty = FN(PART,IS_ZERO)isl_pw_multi_aff_is_empty(part);
196 if (empty < 0)
197 goto error;
198 if (empty)
199 u = FN(UNION,remove_part_entry)isl_union_pw_multi_aff_remove_part_entry(u, entry);
200 FN(PART,free)isl_pw_multi_aff_free(part);
201 }
202
203 return u;
204error:
205 FN(PART,free)isl_pw_multi_aff_free(part);
206 FN(UNION,free)isl_union_pw_multi_aff_free(u);
207 return NULL((void*)0);
208}
209
210/* Add "part" to "u", where "u" is assumed not to already have
211 * a part that is defined on the same space as "part".
212 */
213__isl_give UNIONisl_union_pw_multi_aff *FN(FN(UNION,add),PARTS)isl_union_pw_multi_aff_add_pw_multi_aff(__isl_take UNIONisl_union_pw_multi_aff *u,
214 __isl_take PARTisl_pw_multi_aff *part)
215{
216 return FN(UNION,add_part_generic)isl_union_pw_multi_aff_add_part_generic(u, part, 1);
217}
218
219#ifdef HAS_TYPE
220/* Allocate a UNION with the same type and the same size as "u" and
221 * with space "space".
222 */
223static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,alloc_same_size_on_space)isl_union_pw_multi_aff_alloc_same_size_on_space(__isl_keep UNIONisl_union_pw_multi_aff *u,
224 __isl_take isl_space *space)
225{
226 if (!u)
227 goto error;
228 return FN(UNION,alloc)isl_union_pw_multi_aff_alloc(space, u->type, u->table.n);
229error:
230 isl_space_free(space);
231 return NULL((void*)0);
232}
233#else
234/* Allocate a UNION with the same size as "u" and with space "space".
235 */
236static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,alloc_same_size_on_space)isl_union_pw_multi_aff_alloc_same_size_on_space(__isl_keep UNIONisl_union_pw_multi_aff *u,
237 __isl_take isl_space *space)
238{
239 if (!u)
240 goto error;
241 return FN(UNION,alloc)isl_union_pw_multi_aff_alloc(space, u->table.n);
242error:
243 isl_space_free(space);
244 return NULL((void*)0);
245}
246#endif
247
248/* Allocate a UNION with the same space, the same type (if any) and
249 * the same size as "u".
250 */
251static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,alloc_same_size)isl_union_pw_multi_aff_alloc_same_size(__isl_keep UNIONisl_union_pw_multi_aff *u)
252{
253 return FN(UNION,alloc_same_size_on_space)isl_union_pw_multi_aff_alloc_same_size_on_space(u, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
254}
255
256/* Internal data structure for isl_union_*_transform_space.
257 * "fn' is applied to each entry in the input.
258 * "res" collects the results.
259 */
260S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data
261{
262 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *part, void *user);
263 void *user;
264
265 UNIONisl_union_pw_multi_aff *res;
266};
267
268/* Apply data->fn to "part" and add the result to data->res.
269 */
270static isl_stat FN(UNION,transform_entry)isl_union_pw_multi_aff_transform_entry(__isl_take PARTisl_pw_multi_aff *part, void *user)
271{
272 S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data *data = (S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data *)user;
273
274 part = data->fn(part, data->user);
275 data->res = FN(FN(UNION,add),PARTS)isl_union_pw_multi_aff_add_pw_multi_aff(data->res, part);
276 if (!data->res)
277 return isl_stat_error;
278
279 return isl_stat_ok;
280}
281
282/* Return a UNION living in "space" that is obtained by applying "fn"
283 * to each of the entries in "u".
284 */
285static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(__isl_take UNIONisl_union_pw_multi_aff *u,
286 isl_space *space,
287 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *part, void *user), void *user)
288{
289 S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data data = { fn, user };
290
291 data.res = FN(UNION,alloc_same_size_on_space)isl_union_pw_multi_aff_alloc_same_size_on_space(u, space);
292 if (FN(FN(UNION,foreach),PARTS)isl_union_pw_multi_aff_foreach_pw_multi_aff(u,
293 &FN(UNION,transform_entry)isl_union_pw_multi_aff_transform_entry, &data) < 0)
294 data.res = FN(UNION,free)isl_union_pw_multi_aff_free(data.res);
295 FN(UNION,free)isl_union_pw_multi_aff_free(u);
296 return data.res;
297}
298
299/* Return a UNION that lives in the same space as "u" and that is obtained
300 * by applying "fn" to each of the entries in "u".
301 */
302static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,transform)isl_union_pw_multi_aff_transform(__isl_take UNIONisl_union_pw_multi_aff *u,
303 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *part, void *user), void *user)
304{
305 return FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(u, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u), fn, user);
306}
307
308/* Apply data->fn to *part and store the result back into *part.
309 */
310static isl_stat FN(UNION,transform_inplace_entry)isl_union_pw_multi_aff_transform_inplace_entry(void **part, void *user)
311{
312 S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data *data = (S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data *) user;
313
314 *part = data->fn(*part, data->user);
315 if (!*part)
316 return isl_stat_error;
317 return isl_stat_ok;
318}
319
320/* Update "u" by applying "fn" to each entry.
321 * This operation is assumed not to change the number of entries nor
322 * the spaces of the entries.
323 *
324 * If there is only one reference to "u", then change "u" inplace.
325 * Otherwise, create a new UNION from "u" and discard the original.
326 */
327static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,transform_inplace)isl_union_pw_multi_aff_transform_inplace(__isl_take UNIONisl_union_pw_multi_aff *u,
328 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *part, void *user), void *user)
329{
330 isl_bool single_ref;
331
332 single_ref = FN(UNION,has_single_reference)isl_union_pw_multi_aff_has_single_reference(u);
333 if (single_ref < 0)
334 return FN(UNION,free)isl_union_pw_multi_aff_free(u);
335 if (single_ref) {
336 S(UNION,transform_data)struct isl_union_pw_multi_aff_transform_data data = { fn, user };
337 if (FN(UNION,foreach_inplace)isl_union_pw_multi_aff_foreach_inplace(u,
338 &FN(UNION,transform_inplace_entry)isl_union_pw_multi_aff_transform_inplace_entry, &data) < 0)
339 return FN(UNION,free)isl_union_pw_multi_aff_free(u);
340 return u;
341 }
342 return FN(UNION,transform)isl_union_pw_multi_aff_transform(u, fn, user);
343}
344
345/* An isl_union_*_transform callback for use in isl_union_*_dup
346 * that simply returns "part".
347 */
348static __isl_give PARTisl_pw_multi_aff *FN(UNION,copy_part)isl_union_pw_multi_aff_copy_part(__isl_take PARTisl_pw_multi_aff *part, void *user)
349{
350 return part;
351}
352
353__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,dup)isl_union_pw_multi_aff_dup(__isl_keep UNIONisl_union_pw_multi_aff *u)
354{
355 u = FN(UNION,copy)isl_union_pw_multi_aff_copy(u);
356 return FN(UNION,transform)isl_union_pw_multi_aff_transform(u, &FN(UNION,copy_part)isl_union_pw_multi_aff_copy_part, NULL((void*)0));
357}
358
359__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,cow)isl_union_pw_multi_aff_cow(__isl_take UNIONisl_union_pw_multi_aff *u)
360{
361 if (!u)
362 return NULL((void*)0);
363
364 if (u->ref == 1)
365 return u;
366 u->ref--;
367 return FN(UNION,dup)isl_union_pw_multi_aff_dup(u);
368}
369
370__isl_null UNIONisl_union_pw_multi_aff *FN(UNION,free)isl_union_pw_multi_aff_free(__isl_take UNIONisl_union_pw_multi_aff *u)
371{
372 if (!u)
13
Taking false branch
373 return NULL((void*)0);
374
375 if (--u->ref > 0)
14
Assuming the condition is false
15
Taking false branch
376 return NULL((void*)0);
377
378 isl_hash_table_foreach(u->space->ctx, &u->table,
379 &FN(UNION,free_u_entry)isl_union_pw_multi_aff_free_u_entry, NULL((void*)0));
380 isl_hash_table_clear(&u->table);
381 isl_space_free(u->space);
382 free(u);
16
Memory is released
383 return NULL((void*)0);
384}
385
386static __isl_give PARTisl_pw_multi_aff *FN(UNION,align_entry)isl_union_pw_multi_aff_align_entry(__isl_take PARTisl_pw_multi_aff *part, void *user)
387{
388 isl_reordering *exp = user;
389
390 exp = isl_reordering_extend_space(isl_reordering_copy(exp),
391 FN(PART,get_domain_space)isl_pw_multi_aff_get_domain_space(part));
392 return FN(PART,realign_domain)isl_pw_multi_aff_realign_domain(part, exp);
393}
394
395/* Reorder the parameters of "u" according to the given reordering.
396 */
397static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,realign_domain)isl_union_pw_multi_aff_realign_domain(__isl_take UNIONisl_union_pw_multi_aff *u,
398 __isl_take isl_reordering *r)
399{
400 isl_space *space;
401
402 if (!u || !r)
403 goto error;
404
405 space = isl_space_copy(r->dim);
406 u = FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(u, space, &FN(UNION,align_entry)isl_union_pw_multi_aff_align_entry, r);
407 isl_reordering_free(r);
408 return u;
409error:
410 FN(UNION,free)isl_union_pw_multi_aff_free(u);
411 isl_reordering_free(r);
412 return NULL((void*)0);
413}
414
415/* Align the parameters of "u" to those of "model".
416 */
417__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,align_params)isl_union_pw_multi_aff_align_params(__isl_take UNIONisl_union_pw_multi_aff *u,
418 __isl_take isl_space *model)
419{
420 isl_bool equal_params;
421 isl_reordering *r;
422
423 if (!u || !model)
10
Taking true branch
424 goto error;
11
Control jumps to line 440
425
426 equal_params = isl_space_has_equal_params(u->space, model);
427 if (equal_params < 0)
428 goto error;
429 if (equal_params) {
430 isl_space_free(model);
431 return u;
432 }
433
434 model = isl_space_params(model);
435 r = isl_parameter_alignment_reordering(u->space, model);
436 isl_space_free(model);
437
438 return FN(UNION,realign_domain)isl_union_pw_multi_aff_realign_domain(u, r);
439error:
440 isl_space_free(model);
441 FN(UNION,free)isl_union_pw_multi_aff_free(u);
12
Within the expansion of the macro 'FN':
a
Calling 'isl_union_pw_multi_aff_free'
b
Returning; memory was released via 1st parameter
442 return NULL((void*)0);
443}
444
445/* Add "part" to *u, taking the union sum if "u" already has
446 * a part defined on the same space as "part".
447 */
448static isl_stat FN(UNION,union_add_part)isl_union_pw_multi_aff_union_add_part(__isl_take PARTisl_pw_multi_aff *part, void *user)
449{
450 UNIONisl_union_pw_multi_aff **u = (UNIONisl_union_pw_multi_aff **)user;
451
452 *u = FN(UNION,add_part_generic)isl_union_pw_multi_aff_add_part_generic(*u, part, 0);
453
454 return isl_stat_ok;
455}
456
457/* Compute the sum of "u1" and "u2" on the union of their domains,
458 * with the actual sum on the shared domain and
459 * the defined expression on the symmetric difference of the domains.
460 *
461 * This is an internal function that is exposed under different
462 * names depending on whether the base expressions have a zero default
463 * value.
464 * If they do, then this function is called "add".
465 * Otherwise, it is called "union_add".
466 */
467static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,union_add_)isl_union_pw_multi_aff_union_add_(__isl_take UNIONisl_union_pw_multi_aff *u1,
468 __isl_take UNIONisl_union_pw_multi_aff *u2)
469{
470 u1 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u1, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u2));
471 u2 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u2, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u1));
472
473 u1 = FN(UNION,cow)isl_union_pw_multi_aff_cow(u1);
474
475 if (!u1 || !u2)
476 goto error;
477
478 if (FN(FN(UNION,foreach),PARTS)isl_union_pw_multi_aff_foreach_pw_multi_aff(u2, &FN(UNION,union_add_part)isl_union_pw_multi_aff_union_add_part, &u1) < 0)
479 goto error;
480
481 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
482
483 return u1;
484error:
485 FN(UNION,free)isl_union_pw_multi_aff_free(u1);
486 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
487 return NULL((void*)0);
488}
489
490__isl_give UNIONisl_union_pw_multi_aff *FN(FN(UNION,from),PARTS)isl_union_pw_multi_aff_from_pw_multi_aff(__isl_take PARTisl_pw_multi_aff *part)
491{
492 isl_space *dim;
493 UNIONisl_union_pw_multi_aff *u;
494
495 if (!part)
496 return NULL((void*)0);
497
498 dim = FN(PART,get_space)isl_pw_multi_aff_get_space(part);
499 dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
500 dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
501#ifdef HAS_TYPE
502 u = FN(UNION,ZERO)isl_union_pw_multi_aff_empty(dim, part->type);
503#else
504 u = FN(UNION,ZERO)isl_union_pw_multi_aff_empty(dim);
505#endif
506 u = FN(FN(UNION,add),PARTS)isl_union_pw_multi_aff_add_pw_multi_aff(u, part);
507
508 return u;
509}
510
511S(UNION,match_bin_data)struct isl_union_pw_multi_aff_match_bin_data {
512 UNIONisl_union_pw_multi_aff *u2;
513 UNIONisl_union_pw_multi_aff *res;
514 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *, __isl_take PARTisl_pw_multi_aff *);
515};
516
517/* Check if data->u2 has an element living in the same space as "part".
518 * If so, call data->fn on the two elements and add the result to
519 * data->res.
520 */
521static isl_stat FN(UNION,match_bin_entry)isl_union_pw_multi_aff_match_bin_entry(__isl_take PARTisl_pw_multi_aff *part, void *user)
522{
523 S(UNION,match_bin_data)struct isl_union_pw_multi_aff_match_bin_data *data = user;
524 struct isl_hash_table_entry *entry2;
525 isl_space *space;
526 PARTisl_pw_multi_aff *part2;
527
528 space = FN(PART,get_space)isl_pw_multi_aff_get_space(part);
529 entry2 = FN(UNION,find_part_entry)isl_union_pw_multi_aff_find_part_entry(data->u2, space, 0);
530 isl_space_free(space);
531 if (!entry2)
532 goto error;
533 if (entry2 == isl_hash_table_entry_none) {
534 FN(PART,free)isl_pw_multi_aff_free(part);
535 return isl_stat_ok;
536 }
537
538 part2 = entry2->data;
539 if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
540 part2->dim, isl_dim_out))
541 isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(data->
u2), isl_error_invalid, "entries should have the same range space"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 543); goto error; } while (0)
542 "entries should have the same range space",do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(data->
u2), isl_error_invalid, "entries should have the same range space"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 543); goto error; } while (0)
543 goto error)do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(data->
u2), isl_error_invalid, "entries should have the same range space"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 543); goto error; } while (0)
;
544
545 part = data->fn(part, FN(PART, copy)isl_pw_multi_aff_copy(entry2->data));
546
547 data->res = FN(FN(UNION,add),PARTS)isl_union_pw_multi_aff_add_pw_multi_aff(data->res, part);
548 if (!data->res)
549 return isl_stat_error;
550
551 return isl_stat_ok;
552error:
553 FN(PART,free)isl_pw_multi_aff_free(part);
554 return isl_stat_error;
555}
556
557/* This function is currently only used from isl_polynomial.c
558 * and not from isl_fold.c.
559 */
560static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,match_bin_op)isl_union_pw_multi_aff_match_bin_op(__isl_take UNIONisl_union_pw_multi_aff *u1,
561 __isl_take UNIONisl_union_pw_multi_aff *u2,
562 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *, __isl_take PARTisl_pw_multi_aff *))
563 __attribute__ ((unused));
564/* For each pair of elements in "u1" and "u2" living in the same space,
565 * call "fn" and collect the results.
566 */
567static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,match_bin_op)isl_union_pw_multi_aff_match_bin_op(__isl_take UNIONisl_union_pw_multi_aff *u1,
568 __isl_take UNIONisl_union_pw_multi_aff *u2,
569 __isl_give PARTisl_pw_multi_aff *(*fn)(__isl_take PARTisl_pw_multi_aff *, __isl_take PARTisl_pw_multi_aff *))
570{
571 S(UNION,match_bin_data)struct isl_union_pw_multi_aff_match_bin_data data = { NULL((void*)0), NULL((void*)0), fn };
572
573 u1 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u1, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u2));
574 u2 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u2, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u1));
575
576 if (!u1 || !u2)
577 goto error;
578
579 data.u2 = u2;
580 data.res = FN(UNION,alloc_same_size)isl_union_pw_multi_aff_alloc_same_size(u1);
581 if (FN(FN(UNION,foreach),PARTS)isl_union_pw_multi_aff_foreach_pw_multi_aff(u1,
582 &FN(UNION,match_bin_entry)isl_union_pw_multi_aff_match_bin_entry, &data) < 0)
583 goto error;
584
585 FN(UNION,free)isl_union_pw_multi_aff_free(u1);
586 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
587 return data.res;
588error:
589 FN(UNION,free)isl_union_pw_multi_aff_free(u1);
590 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
591 FN(UNION,free)isl_union_pw_multi_aff_free(data.res);
592 return NULL((void*)0);
593}
594
595/* Compute the sum of "u1" and "u2".
596 *
597 * If the base expressions have a default zero value, then the sum
598 * is computed on the union of the domains of "u1" and "u2".
599 * Otherwise, it is computed on their shared domains.
600 */
601__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,add)isl_union_pw_multi_aff_add(__isl_take UNIONisl_union_pw_multi_aff *u1, __isl_take UNIONisl_union_pw_multi_aff *u2)
602{
603#if DEFAULT_IS_ZERO0
604 return FN(UNION,union_add_)isl_union_pw_multi_aff_union_add_(u1, u2);
605#else
606 return FN(UNION,match_bin_op)isl_union_pw_multi_aff_match_bin_op(u1, u2, &FN(PART,add)isl_pw_multi_aff_add);
607#endif
608}
609
610#ifndef NO_SUB
611/* Subtract "u2" from "u1" and return the result.
612 */
613__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,sub)isl_union_pw_multi_aff_sub(__isl_take UNIONisl_union_pw_multi_aff *u1, __isl_take UNIONisl_union_pw_multi_aff *u2)
614{
615 return FN(UNION,match_bin_op)isl_union_pw_multi_aff_match_bin_op(u1, u2, &FN(PART,sub)isl_pw_multi_aff_sub);
616}
617#endif
618
619S(UNION,any_set_data)struct isl_union_pw_multi_aff_any_set_data {
620 isl_setisl_map *set;
621 __isl_give PWisl_pw_multi_aff *(*fn)(__isl_take PWisl_pw_multi_aff*, __isl_take isl_setisl_map*);
622};
623
624static __isl_give PARTisl_pw_multi_aff *FN(UNION,any_set_entry)isl_union_pw_multi_aff_any_set_entry(__isl_take PARTisl_pw_multi_aff *part,
625 void *user)
626{
627 S(UNION,any_set_data)struct isl_union_pw_multi_aff_any_set_data *data = user;
628
629 return data->fn(part, isl_set_copy(data->set));
630}
631
632/* Update each element of "u" by calling "fn" on the element and "set".
633 */
634static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,any_set_op)isl_union_pw_multi_aff_any_set_op(__isl_take UNIONisl_union_pw_multi_aff *u,
635 __isl_take isl_setisl_map *set,
636 __isl_give PWisl_pw_multi_aff *(*fn)(__isl_take PWisl_pw_multi_aff*, __isl_take isl_setisl_map*))
637{
638 S(UNION,any_set_data)struct isl_union_pw_multi_aff_any_set_data data = { NULL((void*)0), fn };
639
640 u = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u, isl_set_get_space(set));
641 set = isl_set_align_params(set, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
642
643 if (!u || !set)
644 goto error;
645
646 data.set = set;
647 u = FN(UNION,transform)isl_union_pw_multi_aff_transform(u, &FN(UNION,any_set_entry)isl_union_pw_multi_aff_any_set_entry, &data);
648 isl_set_free(set);
649 return u;
650error:
651 FN(UNION,free)isl_union_pw_multi_aff_free(u);
652 isl_set_free(set);
653 return NULL((void*)0);
654}
655
656/* Intersect the domain of "u" with the parameter domain "context".
657 */
658__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,intersect_params)isl_union_pw_multi_aff_intersect_params(__isl_take UNIONisl_union_pw_multi_aff *u,
659 __isl_take isl_setisl_map *set)
660{
661 return FN(UNION,any_set_op)isl_union_pw_multi_aff_any_set_op(u, set, &FN(PW,intersect_params)isl_pw_multi_aff_intersect_params);
662}
663
664/* Compute the gist of the domain of "u" with respect to
665 * the parameter domain "context".
666 */
667__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,gist_params)isl_union_pw_multi_aff_gist_params(__isl_take UNIONisl_union_pw_multi_aff *u,
668 __isl_take isl_setisl_map *set)
669{
670 return FN(UNION,any_set_op)isl_union_pw_multi_aff_any_set_op(u, set, &FN(PW,gist_params)isl_pw_multi_aff_gist_params);
671}
672
673S(UNION,match_domain_data)struct isl_union_pw_multi_aff_match_domain_data {
674 isl_union_setisl_union_map *uset;
675 UNIONisl_union_pw_multi_aff *res;
676 __isl_give PWisl_pw_multi_aff *(*fn)(__isl_take PWisl_pw_multi_aff*, __isl_take isl_setisl_map*);
677};
678
679static int FN(UNION,set_has_dim)isl_union_pw_multi_aff_set_has_dim(const void *entry, const void *val)
680{
681 isl_setisl_map *set = (isl_setisl_map *)entry;
682 isl_space *dim = (isl_space *)val;
683
684 return isl_space_is_equal(set->dim, dim);
685}
686
687/* Find the set in data->uset that lives in the same space as the domain
688 * of "part", apply data->fn to *entry and this set (if any), and add
689 * the result to data->res.
690 */
691static isl_stat FN(UNION,match_domain_entry)isl_union_pw_multi_aff_match_domain_entry(__isl_take PARTisl_pw_multi_aff *part, void *user)
692{
693 S(UNION,match_domain_data)struct isl_union_pw_multi_aff_match_domain_data *data = user;
694 uint32_t hash;
695 struct isl_hash_table_entry *entry2;
696 isl_space *space;
697
698 space = FN(PART,get_domain_space)isl_pw_multi_aff_get_domain_space(part);
699 hash = isl_space_get_hash(space);
700 entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
701 hash, &FN(UNION,set_has_dim)isl_union_pw_multi_aff_set_has_dim, space, 0);
702 isl_space_free(space);
703 if (!entry2) {
704 FN(PART,free)isl_pw_multi_aff_free(part);
705 return isl_stat_ok;
706 }
707
708 part = data->fn(part, isl_set_copy(entry2->data));
709
710 data->res = FN(FN(UNION,add),PARTS)isl_union_pw_multi_aff_add_pw_multi_aff(data->res, part);
711 if (!data->res)
712 return isl_stat_error;
713
714 return isl_stat_ok;
715}
716
717/* Apply fn to each pair of PW in u and set in uset such that
718 * the set lives in the same space as the domain of PW
719 * and collect the results.
720 */
721static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,match_domain_op)isl_union_pw_multi_aff_match_domain_op(__isl_take UNIONisl_union_pw_multi_aff *u,
722 __isl_take isl_union_setisl_union_map *uset,
723 __isl_give PWisl_pw_multi_aff *(*fn)(__isl_take PWisl_pw_multi_aff*, __isl_take isl_setisl_map*))
724{
725 S(UNION,match_domain_data)struct isl_union_pw_multi_aff_match_domain_data data = { NULL((void*)0), NULL((void*)0), fn };
726
727 u = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u, isl_union_set_get_space(uset));
728 uset = isl_union_set_align_params(uset, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
729
730 if (!u || !uset)
731 goto error;
732
733 data.uset = uset;
734 data.res = FN(UNION,alloc_same_size)isl_union_pw_multi_aff_alloc_same_size(u);
735 if (FN(FN(UNION,foreach),PARTS)isl_union_pw_multi_aff_foreach_pw_multi_aff(u,
736 &FN(UNION,match_domain_entry)isl_union_pw_multi_aff_match_domain_entry, &data) < 0)
737 goto error;
738
739 FN(UNION,free)isl_union_pw_multi_aff_free(u);
740 isl_union_set_free(uset);
741 return data.res;
742error:
743 FN(UNION,free)isl_union_pw_multi_aff_free(u);
744 isl_union_set_free(uset);
745 FN(UNION,free)isl_union_pw_multi_aff_free(data.res);
746 return NULL((void*)0);
747}
748
749/* Intersect the domain of "u" with "uset".
750 * If "uset" is a parameters domain, then intersect the parameter
751 * domain of "u" with this set.
752 */
753__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,intersect_domain)isl_union_pw_multi_aff_intersect_domain(__isl_take UNIONisl_union_pw_multi_aff *u,
754 __isl_take isl_union_setisl_union_map *uset)
755{
756 if (isl_union_set_is_params(uset))
757 return FN(UNION,intersect_params)isl_union_pw_multi_aff_intersect_params(u,
758 isl_set_from_union_set(uset));
759 return FN(UNION,match_domain_op)isl_union_pw_multi_aff_match_domain_op(u, uset, &FN(PW,intersect_domain)isl_pw_multi_aff_intersect_domain);
760}
761
762/* Take the set (which may be empty) in data->uset that lives
763 * in the same space as the domain of "pw", subtract it from the domain
764 * of "part" and return the result.
765 */
766static __isl_give PARTisl_pw_multi_aff *FN(UNION,subtract_domain_entry)isl_union_pw_multi_aff_subtract_domain_entry(__isl_take PARTisl_pw_multi_aff *part,
767 void *user)
768{
769 isl_union_setisl_union_map *uset = user;
770 isl_space *space;
771 isl_setisl_map *set;
772
773 space = FN(PART,get_domain_space)isl_pw_multi_aff_get_domain_space(part);
774 set = isl_union_set_extract_set(uset, space);
775 return FN(PART,subtract_domain)isl_pw_multi_aff_subtract_domain(part, set);
776}
777
778/* Subtract "uset' from the domain of "u".
779 */
780__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,subtract_domain)isl_union_pw_multi_aff_subtract_domain(__isl_take UNIONisl_union_pw_multi_aff *u,
781 __isl_take isl_union_setisl_union_map *uset)
782{
783 u = FN(UNION,transform)isl_union_pw_multi_aff_transform(u, &FN(UNION,subtract_domain_entry)isl_union_pw_multi_aff_subtract_domain_entry, uset);
784 isl_union_set_free(uset);
785 return u;
786}
787
788__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,gist)isl_union_pw_multi_aff_gist(__isl_take UNIONisl_union_pw_multi_aff *u,
789 __isl_take isl_union_setisl_union_map *uset)
790{
791 if (isl_union_set_is_params(uset))
792 return FN(UNION,gist_params)isl_union_pw_multi_aff_gist_params(u, isl_set_from_union_set(uset));
793 return FN(UNION,match_domain_op)isl_union_pw_multi_aff_match_domain_op(u, uset, &FN(PW,gist)isl_pw_multi_aff_gist);
794}
795
796/* Coalesce an entry in a UNION. Coalescing is performed in-place.
797 * Since the UNION may have several references, the entry is only
798 * replaced if the coalescing is successful.
799 */
800static isl_stat FN(UNION,coalesce_entry)isl_union_pw_multi_aff_coalesce_entry(void **entry, void *user)
801{
802 PARTisl_pw_multi_aff **part_p = (PARTisl_pw_multi_aff **) entry;
803 PARTisl_pw_multi_aff *part;
804
805 part = FN(PART,copy)isl_pw_multi_aff_copy(*part_p);
806 part = FN(PW,coalesce)isl_pw_multi_aff_coalesce(part);
807 if (!part)
808 return isl_stat_error;
809 FN(PART,free)isl_pw_multi_aff_free(*part_p);
810 *part_p = part;
811
812 return isl_stat_ok;
813}
814
815__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,coalesce)isl_union_pw_multi_aff_coalesce(__isl_take UNIONisl_union_pw_multi_aff *u)
816{
817 if (FN(UNION,foreach_inplace)isl_union_pw_multi_aff_foreach_inplace(u, &FN(UNION,coalesce_entry)isl_union_pw_multi_aff_coalesce_entry, NULL((void*)0)) < 0)
818 goto error;
819
820 return u;
821error:
822 FN(UNION,free)isl_union_pw_multi_aff_free(u);
823 return NULL((void*)0);
824}
825
826static isl_stat FN(UNION,domain_entry)isl_union_pw_multi_aff_domain_entry(__isl_take PARTisl_pw_multi_aff *part, void *user)
827{
828 isl_union_setisl_union_map **uset = (isl_union_setisl_union_map **)user;
829
830 *uset = isl_union_set_add_set(*uset, FN(PART,domain)isl_pw_multi_aff_domain(part));
831
832 return isl_stat_ok;
833}
834
835__isl_give isl_union_setisl_union_map *FN(UNION,domain)isl_union_pw_multi_aff_domain(__isl_take UNIONisl_union_pw_multi_aff *u)
836{
837 isl_union_setisl_union_map *uset;
838
839 uset = isl_union_set_empty(FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u));
840 if (FN(FN(UNION,foreach),PARTS)isl_union_pw_multi_aff_foreach_pw_multi_aff(u, &FN(UNION,domain_entry)isl_union_pw_multi_aff_domain_entry, &uset) < 0)
841 goto error;
842
843 FN(UNION,free)isl_union_pw_multi_aff_free(u);
844
845 return uset;
846error:
847 isl_union_set_free(uset);
848 FN(UNION,free)isl_union_pw_multi_aff_free(u);
849 return NULL((void*)0);
850}
851
852#ifdef HAS_TYPE
853/* Negate the type of "u".
854 */
855static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,negate_type)isl_union_pw_multi_aff_negate_type(__isl_take UNIONisl_union_pw_multi_aff *u)
856{
857 u = FN(UNION,cow)isl_union_pw_multi_aff_cow(u);
858 if (!u)
859 return NULL((void*)0);
860 u->type = isl_fold_type_negate(u->type);
861 return u;
862}
863#else
864/* Negate the type of "u".
865 * Since "u" does not have a type, do nothing.
866 */
867static __isl_give UNIONisl_union_pw_multi_aff *FN(UNION,negate_type)isl_union_pw_multi_aff_negate_type(__isl_take UNIONisl_union_pw_multi_aff *u)
868{
869 return u;
870}
871#endif
872
873/* Multiply "part" by the isl_val "user" and return the result.
874 */
875static __isl_give PARTisl_pw_multi_aff *FN(UNION,scale_val_entry)isl_union_pw_multi_aff_scale_val_entry(__isl_take PARTisl_pw_multi_aff *part,
876 void *user)
877{
878 isl_val *v = user;
879
880 return FN(PART,scale_val)isl_pw_multi_aff_scale_val(part, isl_val_copy(v));
881}
882
883/* Multiply "u" by "v" and return the result.
884 */
885__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,scale_val)isl_union_pw_multi_aff_scale_val(__isl_take UNIONisl_union_pw_multi_aff *u,
886 __isl_take isl_val *v)
887{
888 if (!u || !v)
889 goto error;
890 if (isl_val_is_one(v)) {
891 isl_val_free(v);
892 return u;
893 }
894
895 if (DEFAULT_IS_ZERO0 && u && isl_val_is_zero(v)) {
896 UNIONisl_union_pw_multi_aff *zero;
897 isl_space *space = FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u);
898#ifdef HAS_TYPE
899 zero = FN(UNION,ZERO)isl_union_pw_multi_aff_empty(space, u->type);
900#else
901 zero = FN(UNION,ZERO)isl_union_pw_multi_aff_empty(space);
902#endif
903 FN(UNION,free)isl_union_pw_multi_aff_free(u);
904 isl_val_free(v);
905 return zero;
906 }
907
908 if (!isl_val_is_rat(v))
909 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 910); goto error; } while (0)
910 "expecting rational factor", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 910); goto error; } while (0)
;
911
912 u = FN(UNION,transform_inplace)isl_union_pw_multi_aff_transform_inplace(u, &FN(UNION,scale_val_entry)isl_union_pw_multi_aff_scale_val_entry, v);
913 if (isl_val_is_neg(v))
914 u = FN(UNION,negate_type)isl_union_pw_multi_aff_negate_type(u);
915
916 isl_val_free(v);
917 return u;
918error:
919 isl_val_free(v);
920 FN(UNION,free)isl_union_pw_multi_aff_free(u);
921 return NULL((void*)0);
922}
923
924/* Divide "part" by the isl_val "user" and return the result.
925 */
926static __isl_give PARTisl_pw_multi_aff *FN(UNION,scale_down_val_entry)isl_union_pw_multi_aff_scale_down_val_entry(__isl_take PARTisl_pw_multi_aff *part,
927 void *user)
928{
929 isl_val *v = user;
930
931 return FN(PART,scale_down_val)isl_pw_multi_aff_scale_down_val(part, isl_val_copy(v));
932}
933
934/* Divide "u" by "v" and return the result.
935 */
936__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,scale_down_val)isl_union_pw_multi_aff_scale_down_val(__isl_take UNIONisl_union_pw_multi_aff *u,
937 __isl_take isl_val *v)
938{
939 if (!u || !v)
940 goto error;
941 if (isl_val_is_one(v)) {
942 isl_val_free(v);
943 return u;
944 }
945
946 if (!isl_val_is_rat(v))
947 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 948); goto error; } while (0)
948 "expecting rational factor", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 948); goto error; } while (0)
;
949 if (isl_val_is_zero(v))
950 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 951); goto error; } while (0)
951 "cannot scale down by zero", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 951); goto error; } while (0)
;
952
953 u = FN(UNION,transform_inplace)isl_union_pw_multi_aff_transform_inplace(u, &FN(UNION,scale_down_val_entry)isl_union_pw_multi_aff_scale_down_val_entry, v);
954 if (isl_val_is_neg(v))
955 u = FN(UNION,negate_type)isl_union_pw_multi_aff_negate_type(u);
956
957 isl_val_free(v);
958 return u;
959error:
960 isl_val_free(v);
961 FN(UNION,free)isl_union_pw_multi_aff_free(u);
962 return NULL((void*)0);
963}
964
965S(UNION,plain_is_equal_data)struct isl_union_pw_multi_aff_plain_is_equal_data
966{
967 UNIONisl_union_pw_multi_aff *u2;
968 isl_bool is_equal;
969};
970
971static isl_stat FN(UNION,plain_is_equal_entry)isl_union_pw_multi_aff_plain_is_equal_entry(void **entry, void *user)
972{
973 S(UNION,plain_is_equal_data)struct isl_union_pw_multi_aff_plain_is_equal_data *data = user;
974 struct isl_hash_table_entry *entry2;
975 PWisl_pw_multi_aff *pw = *entry;
976
977 entry2 = FN(UNION,find_part_entry)isl_union_pw_multi_aff_find_part_entry(data->u2, pw->dim, 0);
978 if (!entry2 || entry2 == isl_hash_table_entry_none) {
979 if (!entry2)
980 data->is_equal = isl_bool_error;
981 else
982 data->is_equal = isl_bool_false;
983 return isl_stat_error;
984 }
985
986 data->is_equal = FN(PW,plain_is_equal)isl_pw_multi_aff_plain_is_equal(pw, entry2->data);
987 if (data->is_equal < 0 || !data->is_equal)
988 return isl_stat_error;
989
990 return isl_stat_ok;
991}
992
993isl_bool FN(UNION,plain_is_equal)isl_union_pw_multi_aff_plain_is_equal(__isl_keep UNIONisl_union_pw_multi_aff *u1, __isl_keep UNIONisl_union_pw_multi_aff *u2)
994{
995 S(UNION,plain_is_equal_data)struct isl_union_pw_multi_aff_plain_is_equal_data data = { NULL((void*)0), isl_bool_true };
996 int n1, n2;
997
998 if (!u1 || !u2)
999 return isl_bool_error;
1000 if (u1 == u2)
1001 return isl_bool_true;
1002 if (u1->table.n != u2->table.n)
1003 return isl_bool_false;
1004 n1 = FN(FN(UNION,n),PARTS)isl_union_pw_multi_aff_n_pw_multi_aff(u1);
1005 n2 = FN(FN(UNION,n),PARTS)isl_union_pw_multi_aff_n_pw_multi_aff(u2);
1006 if (n1 < 0 || n2 < 0)
1007 return isl_bool_error;
1008 if (n1 != n2)
1009 return isl_bool_false;
1010
1011 u1 = FN(UNION,copy)isl_union_pw_multi_aff_copy(u1);
1012 u2 = FN(UNION,copy)isl_union_pw_multi_aff_copy(u2);
1013 u1 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u1, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u2));
1014 u2 = FN(UNION,align_params)isl_union_pw_multi_aff_align_params(u2, FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u1));
1015 if (!u1 || !u2)
1016 goto error;
1017
1018 data.u2 = u2;
1019 if (FN(UNION,foreach_inplace)isl_union_pw_multi_aff_foreach_inplace(u1,
1020 &FN(UNION,plain_is_equal_entry)isl_union_pw_multi_aff_plain_is_equal_entry, &data) < 0 &&
1021 data.is_equal)
1022 goto error;
1023
1024 FN(UNION,free)isl_union_pw_multi_aff_free(u1);
1025 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
1026
1027 return data.is_equal;
1028error:
1029 FN(UNION,free)isl_union_pw_multi_aff_free(u1);
1030 FN(UNION,free)isl_union_pw_multi_aff_free(u2);
1031 return isl_bool_error;
1032}
1033
1034/* Check whether the element that "entry" points to involves any NaNs and
1035 * store the result in *nan.
1036 * Abort as soon as one such element has been found.
1037 */
1038static isl_stat FN(UNION,involves_nan_entry)isl_union_pw_multi_aff_involves_nan_entry(void **entry, void *user)
1039{
1040 isl_bool *nan = user;
1041 PWisl_pw_multi_aff *pw = *entry;
1042
1043 *nan = FN(PW,involves_nan)isl_pw_multi_aff_involves_nan(pw);
1044 if (*nan < 0 || !nan)
1045 return isl_stat_error;
1046
1047 return isl_stat_ok;
1048}
1049
1050/* Does "u" involve any NaNs?
1051 */
1052isl_bool FN(UNION,involves_nan)isl_union_pw_multi_aff_involves_nan(__isl_keep UNIONisl_union_pw_multi_aff *u)
1053{
1054 isl_bool nan = isl_bool_false;
1055
1056 if (!u)
1057 return isl_bool_error;
1058
1059 if (FN(UNION,foreach_inplace)isl_union_pw_multi_aff_foreach_inplace(u,
1060 &FN(UNION,involves_nan_entry)isl_union_pw_multi_aff_involves_nan_entry, &nan) < 0 &&
1061 !nan)
1062 return isl_bool_error;
1063
1064 return nan;
1065}
1066
1067/* Internal data structure for isl_union_*_drop_dims.
1068 * type, first and n are passed to isl_*_drop_dims.
1069 */
1070S(UNION,drop_dims_data)struct isl_union_pw_multi_aff_drop_dims_data {
1071 enum isl_dim_type type;
1072 unsigned first;
1073 unsigned n;
1074};
1075
1076/* Drop the parameters specified by "data" from "part" and return the result.
1077 */
1078static __isl_give PARTisl_pw_multi_aff *FN(UNION,drop_dims_entry)isl_union_pw_multi_aff_drop_dims_entry(__isl_take PARTisl_pw_multi_aff *part,
1079 void *user)
1080{
1081 S(UNION,drop_dims_data)struct isl_union_pw_multi_aff_drop_dims_data *data = user;
1082
1083 return FN(PART,drop_dims)isl_pw_multi_aff_drop_dims(part, data->type, data->first, data->n);
1084}
1085
1086/* Drop the specified parameters from "u".
1087 * That is, type is required to be isl_dim_param.
1088 */
1089__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,drop_dims)isl_union_pw_multi_aff_drop_dims( __isl_take UNIONisl_union_pw_multi_aff *u,
1090 enum isl_dim_type type, unsigned first, unsigned n)
1091{
1092 isl_space *space;
1093 S(UNION,drop_dims_data)struct isl_union_pw_multi_aff_drop_dims_data data = { type, first, n };
1094
1095 if (!u)
1096 return NULL((void*)0);
1097
1098 if (type != isl_dim_param)
1099 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1101); return isl_union_pw_multi_aff_free(u); } while (0)
1100 "can only project out parameters",do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1101); return isl_union_pw_multi_aff_free(u); } while (0)
1101 return FN(UNION,free)(u))do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1101); return isl_union_pw_multi_aff_free(u); } while (0)
;
1102
1103 space = FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u);
1104 space = isl_space_drop_dims(space, type, first, n);
1105 return FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(u, space, &FN(UNION,drop_dims_entry)isl_union_pw_multi_aff_drop_dims_entry,
1106 &data);
1107}
1108
1109/* Internal data structure for isl_union_*_set_dim_name.
1110 * pos is the position of the parameter that needs to be renamed.
1111 * s is the new name.
1112 */
1113S(UNION,set_dim_name_data)struct isl_union_pw_multi_aff_set_dim_name_data {
1114 unsigned pos;
1115 const char *s;
1116};
1117
1118/* Change the name of the parameter at position data->pos of "part" to data->s
1119 * and return the result.
1120 */
1121static __isl_give PARTisl_pw_multi_aff *FN(UNION,set_dim_name_entry)isl_union_pw_multi_aff_set_dim_name_entry(__isl_take PARTisl_pw_multi_aff *part,
1122 void *user)
1123{
1124 S(UNION,set_dim_name_data)struct isl_union_pw_multi_aff_set_dim_name_data *data = user;
1125
1126 return FN(PART,set_dim_name)isl_pw_multi_aff_set_dim_name(part, isl_dim_param, data->pos, data->s);
1127}
1128
1129/* Change the name of the parameter at position "pos" to "s".
1130 * That is, type is required to be isl_dim_param.
1131 */
1132__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,set_dim_name)isl_union_pw_multi_aff_set_dim_name(__isl_take UNIONisl_union_pw_multi_aff *u,
1133 enum isl_dim_type type, unsigned pos, const char *s)
1134{
1135 S(UNION,set_dim_name_data)struct isl_union_pw_multi_aff_set_dim_name_data data = { pos, s };
1136 isl_space *space;
1137
1138 if (!u)
1139 return NULL((void*)0);
1140
1141 if (type != isl_dim_param)
1142 isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only set parameter names", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1144); return isl_union_pw_multi_aff_free(u); } while (0)
1143 "can only set parameter names",do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only set parameter names", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1144); return isl_union_pw_multi_aff_free(u); } while (0)
1144 return FN(UNION,free)(u))do { isl_handle_error(isl_union_pw_multi_aff_get_ctx(u), isl_error_invalid
, "can only set parameter names", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_union_templ.c"
, 1144); return isl_union_pw_multi_aff_free(u); } while (0)
;
1145
1146 space = FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u);
1147 space = isl_space_set_dim_name(space, type, pos, s);
1148 return FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(u, space,
1149 &FN(UNION,set_dim_name_entry)isl_union_pw_multi_aff_set_dim_name_entry, &data);
1150}
1151
1152/* Reset the user pointer on all identifiers of parameters and tuples
1153 * of the space of "part" and return the result.
1154 */
1155static __isl_give PARTisl_pw_multi_aff *FN(UNION,reset_user_entry)isl_union_pw_multi_aff_reset_user_entry(__isl_take PARTisl_pw_multi_aff *part,
1156 void *user)
1157{
1158 return FN(PART,reset_user)isl_pw_multi_aff_reset_user(part);
1159}
1160
1161/* Reset the user pointer on all identifiers of parameters and tuples
1162 * of the spaces of "u".
1163 */
1164__isl_give UNIONisl_union_pw_multi_aff *FN(UNION,reset_user)isl_union_pw_multi_aff_reset_user(__isl_take UNIONisl_union_pw_multi_aff *u)
1165{
1166 isl_space *space;
1167
1168 space = FN(UNION,get_space)isl_union_pw_multi_aff_get_space(u);
1169 space = isl_space_reset_user(space);
1170 return FN(UNION,transform_space)isl_union_pw_multi_aff_transform_space(u, space, &FN(UNION,reset_user_entry)isl_union_pw_multi_aff_reset_user_entry,
1171 NULL((void*)0));
1172}