Bug Summary

File:tools/polly/lib/External/isl/isl_aff.c
Warning:line 2358, column 11
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
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)
2
Assuming 'aff' is non-null
3
Taking false branch
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)
16
Taking false branch
283 return NULL((void*)0);
284
285 if (--aff->ref > 0)
17
Taking false branch
286 return NULL((void*)0);
287
288 isl_local_space_free(aff->ls);
289 isl_vec_free(aff->v);
290
291 free(aff);
18
Memory is released
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)
10
Taking false branch
1234 return NULL((void*)0);
1235 if (isl_aff_is_nan(aff))
11
Taking false branch
1236 return aff;
1237 aff = isl_aff_cow(aff);
1238 if (!aff)
12
Taking false branch
1239 return NULL((void*)0);
1240 aff->v = isl_vec_cow(aff->v);
1241 if (!aff->v)
13
Assuming the condition is true
14
Taking true branch
1242 return isl_aff_free(aff);
15
Calling 'isl_aff_free'
19
Returning; memory was released via 1st parameter
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));
9
Calling 'isl_aff_neg'
20
Returning; memory was released via 1st parameter
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);
8
Calling 'isl_aff_sub'
21
Returning; memory was released via 2nd parameter
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);
7
Calling 'isl_aff_gt_basic_set'
22
Returning; memory was released via 2nd parameter
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));
6
Calling 'isl_aff_lt_basic_set'
23
Returning; memory was released via 1st parameter
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),
1
Calling 'isl_aff_copy'
4
Returning from 'isl_aff_copy'
5
Calling 'isl_aff_lt_set'
24
Returning; memory was released via 1st parameter
2357 isl_aff_copy(aff2));
2358 set_gt = isl_aff_gt_set(aff1, aff2);
25
Use of memory after it is freed
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_spac