Bug Summary

File:polly/lib/External/isl/isl_aff.c
Warning:line 2521, column 2
Value stored to 'ctx' is never read

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-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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.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-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/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 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018 Cerebras Systems
8 *
9 * Use of this software is governed by the MIT license
10 *
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
18 */
19
20#include <isl_ctx_private.h>
21#include <isl_map_private.h>
22#include <isl_union_map_private.h>
23#include <isl_aff_private.h>
24#include <isl_space_private.h>
25#include <isl_local_space_private.h>
26#include <isl_vec_private.h>
27#include <isl_mat_private.h>
28#include <isl_id_private.h>
29#include <isl/constraint.h>
30#include <isl_seq.h>
31#include <isl/set.h>
32#include <isl_val_private.h>
33#include <isl_point_private.h>
34#include <isl_config.h>
35
36#undef EL_BASEunion_pw_aff
37#define EL_BASEunion_pw_aff aff
38
39#include <isl_list_templ.c>
40
41#undef EL_BASEunion_pw_aff
42#define EL_BASEunion_pw_aff pw_aff
43
44#include <isl_list_templ.c>
45
46#undef EL_BASEunion_pw_aff
47#define EL_BASEunion_pw_aff pw_multi_aff
48
49#include <isl_list_templ.c>
50
51#undef EL_BASEunion_pw_aff
52#define EL_BASEunion_pw_aff union_pw_aff
53
54#include <isl_list_templ.c>
55
56#undef EL_BASEunion_pw_aff
57#define EL_BASEunion_pw_aff union_pw_multi_aff
58
59#include <isl_list_templ.c>
60
61__isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
62 __isl_take isl_vec *v)
63{
64 isl_aff *aff;
65
66 if (!ls || !v)
67 goto error;
68
69 aff = isl_calloc_type(v->ctx, struct isl_aff)((struct isl_aff *)isl_calloc_or_die(v->ctx, 1, sizeof(struct
isl_aff)))
;
70 if (!aff)
71 goto error;
72
73 aff->ref = 1;
74 aff->ls = ls;
75 aff->v = v;
76
77 return aff;
78error:
79 isl_local_space_free(ls);
80 isl_vec_free(v);
81 return NULL((void*)0);
82}
83
84__isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
85{
86 isl_ctx *ctx;
87 isl_vec *v;
88 isl_size total;
89
90 if (!ls)
91 return NULL((void*)0);
92
93 ctx = isl_local_space_get_ctx(ls);
94 if (!isl_local_space_divs_known(ls))
95 isl_die(ctx, isl_error_invalid, "local space has unknown divs",do { isl_handle_error(ctx, isl_error_invalid, "local space has unknown divs"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 96); goto error; } while (0)
96 goto error)do { isl_handle_error(ctx, isl_error_invalid, "local space has unknown divs"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 96); goto error; } while (0)
;
97 if (!isl_local_space_is_set(ls))
98 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 100); goto error; } while (0)
99 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 100); goto error; } while (0)
100 goto error)do { isl_handle_error(ctx, isl_error_invalid, "domain of affine expression should be a set"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 100); goto error; } while (0)
;
101
102 total = isl_local_space_dim(ls, isl_dim_all);
103 if (total < 0)
104 goto error;
105 v = isl_vec_alloc(ctx, 1 + 1 + total);
106 return isl_aff_alloc_vec(ls, v);
107error:
108 isl_local_space_free(ls);
109 return NULL((void*)0);
110}
111
112__isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
113{
114 isl_aff *aff;
115
116 aff = isl_aff_alloc(ls);
117 if (!aff)
118 return NULL((void*)0);
119
120 isl_int_set_si(aff->v->el[0], 1)isl_sioimath_set_si((aff->v->el[0]), 1);
121 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
122
123 return aff;
124}
125
126/* Return a piecewise affine expression defined on the specified domain
127 * that is equal to zero.
128 */
129__isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
130{
131 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
132}
133
134/* Return an affine expression defined on the specified domain
135 * that represents NaN.
136 */
137__isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
138{
139 isl_aff *aff;
140
141 aff = isl_aff_alloc(ls);
142 if (!aff)
143 return NULL((void*)0);
144
145 isl_seq_clr(aff->v->el, aff->v->size);
146
147 return aff;
148}
149
150/* Return a piecewise affine expression defined on the specified domain
151 * that represents NaN.
152 */
153__isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
154{
155 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
156}
157
158/* Return an affine expression that is equal to "val" on
159 * domain local space "ls".
160 */
161__isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
162 __isl_take isl_val *val)
163{
164 isl_aff *aff;
165
166 if (!ls || !val)
167 goto error;
168 if (!isl_val_is_rat(val))
169 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 170); goto error; } while (0)
170 "expecting rational value", goto error)do { isl_handle_error(isl_val_get_ctx(val), isl_error_invalid
, "expecting rational value", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 170); goto error; } while (0)
;
171
172 aff = isl_aff_alloc(isl_local_space_copy(ls));
173 if (!aff)
174 goto error;
175
176 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
177 isl_int_set(aff->v->el[1], val->n)isl_sioimath_set((aff->v->el[1]), *(val->n));
178 isl_int_set(aff->v->el[0], val->d)isl_sioimath_set((aff->v->el[0]), *(val->d));
179
180 isl_local_space_free(ls);
181 isl_val_free(val);
182 return aff;
183error:
184 isl_local_space_free(ls);
185 isl_val_free(val);
186 return NULL((void*)0);
187}
188
189/* Return an affine expression that is equal to "val" on domain space "space".
190 */
191__isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
192 __isl_take isl_val *val)
193{
194 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
195}
196
197/* Return an affine expression that is equal to the specified dimension
198 * in "ls".
199 */
200__isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
201 enum isl_dim_type type, unsigned pos)
202{
203 isl_space *space;
204 isl_aff *aff;
205
206 if (!ls)
207 return NULL((void*)0);
208
209 space = isl_local_space_get_space(ls);
210 if (!space)
211 goto error;
212 if (isl_space_is_map(space))
213 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 214); goto error; } while (0)
214 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 214); goto error; } while (0)
;
215 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
216 goto error;
217
218 isl_space_free(space);
219 aff = isl_aff_alloc(ls);
220 if (!aff)
221 return NULL((void*)0);
222
223 pos += isl_local_space_offset(aff->ls, type);
224
225 isl_int_set_si(aff->v->el[0], 1)isl_sioimath_set_si((aff->v->el[0]), 1);
226 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
227 isl_int_set_si(aff->v->el[1 + pos], 1)isl_sioimath_set_si((aff->v->el[1 + pos]), 1);
228
229 return aff;
230error:
231 isl_local_space_free(ls);
232 isl_space_free(space);
233 return NULL((void*)0);
234}
235
236/* Return a piecewise affine expression that is equal to
237 * the specified dimension in "ls".
238 */
239__isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
240 enum isl_dim_type type, unsigned pos)
241{
242 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
243}
244
245/* Return an affine expression that is equal to the parameter
246 * in the domain space "space" with identifier "id".
247 */
248__isl_give isl_aff *isl_aff_param_on_domain_space_id(
249 __isl_take isl_space *space, __isl_take isl_id *id)
250{
251 int pos;
252 isl_local_space *ls;
253
254 if (!space || !id)
255 goto error;
256 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
257 if (pos < 0)
258 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 259); goto error; } while (0)
259 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 259); goto error; } while (0)
;
260 isl_id_free(id);
261 ls = isl_local_space_from_space(space);
262 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
263error:
264 isl_space_free(space);
265 isl_id_free(id);
266 return NULL((void*)0);
267}
268
269__isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
270{
271 if (!aff)
272 return NULL((void*)0);
273
274 aff->ref++;
275 return aff;
276}
277
278__isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
279{
280 if (!aff)
281 return NULL((void*)0);
282
283 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
284 isl_vec_copy(aff->v));
285}
286
287__isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
288{
289 if (!aff)
290 return NULL((void*)0);
291
292 if (aff->ref == 1)
293 return aff;
294 aff->ref--;
295 return isl_aff_dup(aff);
296}
297
298__isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
299{
300 if (!aff)
301 return NULL((void*)0);
302
303 if (--aff->ref > 0)
304 return NULL((void*)0);
305
306 isl_local_space_free(aff->ls);
307 isl_vec_free(aff->v);
308
309 free(aff);
310
311 return NULL((void*)0);
312}
313
314isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
315{
316 return aff ? isl_local_space_get_ctx(aff->ls) : NULL((void*)0);
317}
318
319/* Return a hash value that digests "aff".
320 */
321uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
322{
323 uint32_t hash, ls_hash, v_hash;
324
325 if (!aff)
326 return 0;
327
328 hash = isl_hash_init()(2166136261u);
329 ls_hash = isl_local_space_get_hash(aff->ls);
330 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)
;
331 v_hash = isl_vec_get_hash(aff->v);
332 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)
;
333
334 return hash;
335}
336
337/* Return the domain local space of "aff".
338 */
339static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
340 __isl_keep isl_aff *aff)
341{
342 return aff ? aff->ls : NULL((void*)0);
343}
344
345/* Return the number of variables of the given type in the domain of "aff".
346 */
347isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
348{
349 isl_local_space *ls;
350
351 ls = isl_aff_peek_domain_local_space(aff);
352 return isl_local_space_dim(ls, type);
353}
354
355/* Externally, an isl_aff has a map space, but internally, the
356 * ls field corresponds to the domain of that space.
357 */
358isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
359{
360 if (!aff)
361 return isl_size_error((int) -1);
362 if (type == isl_dim_out)
363 return 1;
364 if (type == isl_dim_in)
365 type = isl_dim_set;
366 return isl_aff_domain_dim(aff, type);
367}
368
369/* Return the offset of the first coefficient of type "type" in
370 * the domain of "aff".
371 */
372isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
373{
374 isl_local_space *ls;
375
376 ls = isl_aff_peek_domain_local_space(aff);
377 return isl_local_space_offset(ls, type);
378}
379
380/* Return the position of the dimension of the given type and name
381 * in "aff".
382 * Return -1 if no such dimension can be found.
383 */
384int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
385 const char *name)
386{
387 if (!aff)
388 return -1;
389 if (type == isl_dim_out)
390 return -1;
391 if (type == isl_dim_in)
392 type = isl_dim_set;
393 return isl_local_space_find_dim_by_name(aff->ls, type, name);
394}
395
396/* Return the domain space of "aff".
397 */
398static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
399{
400 return aff ? isl_local_space_peek_space(aff->ls) : NULL((void*)0);
401}
402
403__isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
404{
405 return isl_space_copy(isl_aff_peek_domain_space(aff));
406}
407
408__isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
409{
410 isl_space *space;
411 if (!aff)
412 return NULL((void*)0);
413 space = isl_local_space_get_space(aff->ls);
414 space = isl_space_from_domain(space);
415 space = isl_space_add_dims(space, isl_dim_out, 1);
416 return space;
417}
418
419/* Return a copy of the domain space of "aff".
420 */
421__isl_give isl_local_space *isl_aff_get_domain_local_space(
422 __isl_keep isl_aff *aff)
423{
424 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
425}
426
427__isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
428{
429 isl_local_space *ls;
430 if (!aff)
431 return NULL((void*)0);
432 ls = isl_local_space_copy(aff->ls);
433 ls = isl_local_space_from_domain(ls);
434 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
435 return ls;
436}
437
438/* Return the local space of the domain of "aff".
439 * This may be either a copy or the local space itself
440 * if there is only one reference to "aff".
441 * This allows the local space to be modified inplace
442 * if both the expression and its local space have only a single reference.
443 * The caller is not allowed to modify "aff" between this call and
444 * a subsequent call to isl_aff_restore_domain_local_space.
445 * The only exception is that isl_aff_free can be called instead.
446 */
447__isl_give isl_local_space *isl_aff_take_domain_local_space(
448 __isl_keep isl_aff *aff)
449{
450 isl_local_space *ls;
451
452 if (!aff)
453 return NULL((void*)0);
454 if (aff->ref != 1)
455 return isl_aff_get_domain_local_space(aff);
456 ls = aff->ls;
457 aff->ls = NULL((void*)0);
458 return ls;
459}
460
461/* Set the local space of the domain of "aff" to "ls",
462 * where the local space of "aff" may be missing
463 * due to a preceding call to isl_aff_take_domain_local_space.
464 * However, in this case, "aff" only has a single reference and
465 * then the call to isl_aff_cow has no effect.
466 */
467__isl_give isl_aff *isl_aff_restore_domain_local_space(
468 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
469{
470 if (!aff || !ls)
471 goto error;
472
473 if (aff->ls == ls) {
474 isl_local_space_free(ls);
475 return aff;
476 }
477
478 aff = isl_aff_cow(aff);
479 if (!aff)
480 goto error;
481 isl_local_space_free(aff->ls);
482 aff->ls = ls;
483
484 return aff;
485error:
486 isl_aff_free(aff);
487 isl_local_space_free(ls);
488 return NULL((void*)0);
489}
490
491/* Externally, an isl_aff has a map space, but internally, the
492 * ls field corresponds to the domain of that space.
493 */
494const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
495 enum isl_dim_type type, unsigned pos)
496{
497 if (!aff)
498 return NULL((void*)0);
499 if (type == isl_dim_out)
500 return NULL((void*)0);
501 if (type == isl_dim_in)
502 type = isl_dim_set;
503 return isl_local_space_get_dim_name(aff->ls, type, pos);
504}
505
506__isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
507 __isl_take isl_space *dim)
508{
509 aff = isl_aff_cow(aff);
510 if (!aff || !dim)
511 goto error;
512
513 aff->ls = isl_local_space_reset_space(aff->ls, dim);
514 if (!aff->ls)
515 return isl_aff_free(aff);
516
517 return aff;
518error:
519 isl_aff_free(aff);
520 isl_space_free(dim);
521 return NULL((void*)0);
522}
523
524/* Reset the space of "aff". This function is called from isl_pw_templ.c
525 * and doesn't know if the space of an element object is represented
526 * directly or through its domain. It therefore passes along both.
527 */
528__isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
529 __isl_take isl_space *space, __isl_take isl_space *domain)
530{
531 isl_space_free(space);
532 return isl_aff_reset_domain_space(aff, domain);
533}
534
535/* Reorder the coefficients of the affine expression based
536 * on the given reordering.
537 * The reordering r is assumed to have been extended with the local
538 * variables.
539 */
540static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
541 __isl_take isl_reordering *r, int n_div)
542{
543 isl_space *space;
544 isl_vec *res;
545 isl_size dim;
546 int i;
547
548 if (!vec || !r)
549 goto error;
550
551 space = isl_reordering_peek_space(r);
552 dim = isl_space_dim(space, isl_dim_all);
553 if (dim < 0)
554 goto error;
555 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
556 if (!res)
557 goto error;
558 isl_seq_cpy(res->el, vec->el, 2);
559 isl_seq_clr(res->el + 2, res->size - 2);
560 for (i = 0; i < r->len; ++i)
561 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]))
;
562
563 isl_reordering_free(r);
564 isl_vec_free(vec);
565 return res;
566error:
567 isl_vec_free(vec);
568 isl_reordering_free(r);
569 return NULL((void*)0);
570}
571
572/* Reorder the dimensions of the domain of "aff" according
573 * to the given reordering.
574 */
575__isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
576 __isl_take isl_reordering *r)
577{
578 aff = isl_aff_cow(aff);
579 if (!aff)
580 goto error;
581
582 r = isl_reordering_extend(r, aff->ls->div->n_row);
583 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
584 aff->ls->div->n_row);
585 aff->ls = isl_local_space_realign(aff->ls, r);
586
587 if (!aff->v || !aff->ls)
588 return isl_aff_free(aff);
589
590 return aff;
591error:
592 isl_aff_free(aff);
593 isl_reordering_free(r);
594 return NULL((void*)0);
595}
596
597__isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
598 __isl_take isl_space *model)
599{
600 isl_bool equal_params;
601
602 if (!aff || !model)
603 goto error;
604
605 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
606 if (equal_params < 0)
607 goto error;
608 if (!equal_params) {
609 isl_reordering *exp;
610
611 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
612 exp = isl_reordering_extend_space(exp,
613 isl_aff_get_domain_space(aff));
614 aff = isl_aff_realign_domain(aff, exp);
615 }
616
617 isl_space_free(model);
618 return aff;
619error:
620 isl_space_free(model);
621 isl_aff_free(aff);
622 return NULL((void*)0);
623}
624
625/* Given an affine function "aff" defined over a parameter domain,
626 * convert it to a function defined over a domain corresponding
627 * to "domain".
628 * Any parameters with identifiers in "domain" are reinterpreted
629 * as the corresponding domain dimensions.
630 */
631__isl_give isl_aff *isl_aff_unbind_params_insert_domain(
632 __isl_take isl_aff *aff, __isl_take isl_multi_id *domain)
633{
634 isl_bool is_params;
635 isl_space *space;
636 isl_reordering *r;
637
638 space = isl_aff_peek_domain_space(aff);
639 is_params = isl_space_is_params(space);
640 if (is_params < 0)
641 domain = isl_multi_id_free(domain);
642 else if (!is_params)
643 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting function with parameter domain", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 645); domain = isl_multi_id_free(domain); } while (0)
644 "expecting function with parameter domain",do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting function with parameter domain", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 645); domain = isl_multi_id_free(domain); } while (0)
645 domain = isl_multi_id_free(domain))do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting function with parameter domain", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 645); domain = isl_multi_id_free(domain); } while (0)
;
646 r = isl_reordering_unbind_params_insert_domain(space, domain);
647 isl_multi_id_free(domain);
648
649 return isl_aff_realign_domain(aff, r);
650}
651
652/* Is "aff" obviously equal to zero?
653 *
654 * If the denominator is zero, then "aff" is not equal to zero.
655 */
656isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
657{
658 int pos;
659
660 if (!aff)
661 return isl_bool_error;
662
663 if (isl_int_is_zero(aff->v->el[0])(isl_sioimath_sgn(*(aff->v->el[0])) == 0))
664 return isl_bool_false;
665 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
666 return isl_bool_ok(pos < 0);
667}
668
669/* Does "aff" represent NaN?
670 */
671isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
672{
673 if (!aff)
674 return isl_bool_error;
675
676 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
677}
678
679/* Are "aff1" and "aff2" obviously equal?
680 *
681 * NaN is not equal to anything, not even to another NaN.
682 */
683isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
684 __isl_keep isl_aff *aff2)
685{
686 isl_bool equal;
687
688 if (!aff1 || !aff2)
689 return isl_bool_error;
690
691 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
692 return isl_bool_false;
693
694 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
695 if (equal < 0 || !equal)
696 return equal;
697
698 return isl_vec_is_equal(aff1->v, aff2->v);
699}
700
701/* Return the common denominator of "aff" in "v".
702 *
703 * We cannot return anything meaningful in case of a NaN.
704 */
705isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
706{
707 if (!aff)
708 return isl_stat_error;
709 if (isl_aff_is_nan(aff))
710 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 711); return isl_stat_error; } while (0)
711 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 711); return isl_stat_error; } while (0)
;
712 isl_int_set(*v, aff->v->el[0])isl_sioimath_set((*v), *(aff->v->el[0]));
713 return isl_stat_ok;
714}
715
716/* Return the common denominator of "aff".
717 */
718__isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
719{
720 isl_ctx *ctx;
721
722 if (!aff)
723 return NULL((void*)0);
724
725 ctx = isl_aff_get_ctx(aff);
726 if (isl_aff_is_nan(aff))
727 return isl_val_nan(ctx);
728 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
729}
730
731/* Return the constant term of "aff".
732 */
733__isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
734{
735 isl_ctx *ctx;
736 isl_val *v;
737
738 if (!aff)
739 return NULL((void*)0);
740
741 ctx = isl_aff_get_ctx(aff);
742 if (isl_aff_is_nan(aff))
743 return isl_val_nan(ctx);
744 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
745 return isl_val_normalize(v);
746}
747
748/* Return the coefficient of the variable of type "type" at position "pos"
749 * of "aff".
750 */
751__isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
752 enum isl_dim_type type, int pos)
753{
754 isl_ctx *ctx;
755 isl_val *v;
756
757 if (!aff)
758 return NULL((void*)0);
759
760 ctx = isl_aff_get_ctx(aff);
761 if (type == isl_dim_out)
762 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 764); return ((void*)0); } while (0)
763 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 764); return ((void*)0); } while (0)
764 return NULL)do { isl_handle_error(ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 764); return ((void*)0); } while (0)
;
765 if (type == isl_dim_in)
766 type = isl_dim_set;
767
768 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
769 return NULL((void*)0);
770
771 if (isl_aff_is_nan(aff))
772 return isl_val_nan(ctx);
773 pos += isl_local_space_offset(aff->ls, type);
774 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
775 return isl_val_normalize(v);
776}
777
778/* Return the sign of the coefficient of the variable of type "type"
779 * at position "pos" of "aff".
780 */
781int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
782 int pos)
783{
784 isl_ctx *ctx;
785
786 if (!aff)
787 return 0;
788
789 ctx = isl_aff_get_ctx(aff);
790 if (type == isl_dim_out)
791 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 793); return 0; } while (0)
792 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 793); return 0; } while (0)
793 return 0)do { isl_handle_error(ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 793); return 0; } while (0)
;
794 if (type == isl_dim_in)
795 type = isl_dim_set;
796
797 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
798 return 0;
799
800 pos += isl_local_space_offset(aff->ls, type);
801 return isl_int_sgn(aff->v->el[1 + pos])isl_sioimath_sgn(*(aff->v->el[1 + pos]));
802}
803
804/* Replace the numerator of the constant term of "aff" by "v".
805 *
806 * A NaN is unaffected by this operation.
807 */
808__isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
809{
810 if (!aff)
811 return NULL((void*)0);
812 if (isl_aff_is_nan(aff))
813 return aff;
814 aff = isl_aff_cow(aff);
815 if (!aff)
816 return NULL((void*)0);
817
818 aff->v = isl_vec_cow(aff->v);
819 if (!aff->v)
820 return isl_aff_free(aff);
821
822 isl_int_set(aff->v->el[1], v)isl_sioimath_set((aff->v->el[1]), *(v));
823
824 return aff;
825}
826
827/* Replace the constant term of "aff" by "v".
828 *
829 * A NaN is unaffected by this operation.
830 */
831__isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
832 __isl_take isl_val *v)
833{
834 if (!aff || !v)
835 goto error;
836
837 if (isl_aff_is_nan(aff)) {
838 isl_val_free(v);
839 return aff;
840 }
841
842 if (!isl_val_is_rat(v))
843 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 844); goto error; } while (0)
844 "expecting rational value", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational value", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 844); goto error; } while (0)
;
845
846 if (isl_int_eq(aff->v->el[1], v->n)(isl_sioimath_cmp(*(aff->v->el[1]), *(v->n)) == 0) &&
847 isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
848 isl_val_free(v);
849 return aff;
850 }
851
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 goto error;
855 aff->v = isl_vec_cow(aff->v);
856 if (!aff->v)
857 goto error;
858
859 if (isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
860 isl_int_set(aff->v->el[1], v->n)isl_sioimath_set((aff->v->el[1]), *(v->n));
861 } else if (isl_int_is_one(v->d)(isl_sioimath_cmp_si(*(v->d), 1) == 0)) {
862 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))
;
863 } else {
864 isl_seq_scale(aff->v->el + 1,
865 aff->v->el + 1, v->d, aff->v->size - 1);
866 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))
;
867 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))
;
868 aff->v = isl_vec_normalize(aff->v);
869 if (!aff->v)
870 goto error;
871 }
872
873 isl_val_free(v);
874 return aff;
875error:
876 isl_aff_free(aff);
877 isl_val_free(v);
878 return NULL((void*)0);
879}
880
881/* Add "v" to the constant term of "aff".
882 *
883 * A NaN is unaffected by this operation.
884 */
885__isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
886{
887 if (isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0))
888 return aff;
889
890 if (!aff)
891 return NULL((void*)0);
892 if (isl_aff_is_nan(aff))
893 return aff;
894 aff = isl_aff_cow(aff);
895 if (!aff)
896 return NULL((void*)0);
897
898 aff->v = isl_vec_cow(aff->v);
899 if (!aff->v)
900 return isl_aff_free(aff);
901
902 isl_int_addmul(aff->v->el[1], aff->v->el[0], v)isl_sioimath_addmul((aff->v->el[1]), *(aff->v->el
[0]), *(v))
;
903
904 return aff;
905}
906
907/* Add "v" to the constant term of "aff".
908 *
909 * A NaN is unaffected by this operation.
910 */
911__isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
912 __isl_take isl_val *v)
913{
914 if (!aff || !v)
915 goto error;
916
917 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
918 isl_val_free(v);
919 return aff;
920 }
921
922 if (!isl_val_is_rat(v))
923 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 924); goto error; } while (0)
924 "expecting rational value", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational value", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 924); goto error; } while (0)
;
925
926 aff = isl_aff_cow(aff);
927 if (!aff)
928 goto error;
929
930 aff->v = isl_vec_cow(aff->v);
931 if (!aff->v)
932 goto error;
933
934 if (isl_int_is_one(v->d)(isl_sioimath_cmp_si(*(v->d), 1) == 0)) {
935 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))
;
936 } else if (isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
937 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))
;
938 aff->v = isl_vec_normalize(aff->v);
939 if (!aff->v)
940 goto error;
941 } else {
942 isl_seq_scale(aff->v->el + 1,
943 aff->v->el + 1, v->d, aff->v->size - 1);
944 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))
;
945 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))
;
946 aff->v = isl_vec_normalize(aff->v);
947 if (!aff->v)
948 goto error;
949 }
950
951 isl_val_free(v);
952 return aff;
953error:
954 isl_aff_free(aff);
955 isl_val_free(v);
956 return NULL((void*)0);
957}
958
959__isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
960{
961 isl_int t;
962
963 isl_int_init(t)isl_sioimath_init((t));
964 isl_int_set_si(t, v)isl_sioimath_set_si((t), v);
965 aff = isl_aff_add_constant(aff, t);
966 isl_int_clear(t)isl_sioimath_clear((t));
967
968 return aff;
969}
970
971/* Add "v" to the numerator of the constant term of "aff".
972 *
973 * A NaN is unaffected by this operation.
974 */
975__isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
976{
977 if (isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0))
978 return aff;
979
980 if (!aff)
981 return NULL((void*)0);
982 if (isl_aff_is_nan(aff))
983 return aff;
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 return NULL((void*)0);
987
988 aff->v = isl_vec_cow(aff->v);
989 if (!aff->v)
990 return isl_aff_free(aff);
991
992 isl_int_add(aff->v->el[1], aff->v->el[1], v)isl_sioimath_add((aff->v->el[1]), *(aff->v->el[1]
), *(v))
;
993
994 return aff;
995}
996
997/* Add "v" to the numerator of the constant term of "aff".
998 *
999 * A NaN is unaffected by this operation.
1000 */
1001__isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1002{
1003 isl_int t;
1004
1005 if (v == 0)
1006 return aff;
1007
1008 isl_int_init(t)isl_sioimath_init((t));
1009 isl_int_set_si(t, v)isl_sioimath_set_si((t), v);
1010 aff = isl_aff_add_constant_num(aff, t);
1011 isl_int_clear(t)isl_sioimath_clear((t));
1012
1013 return aff;
1014}
1015
1016/* Replace the numerator of the constant term of "aff" by "v".
1017 *
1018 * A NaN is unaffected by this operation.
1019 */
1020__isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1021{
1022 if (!aff)
1023 return NULL((void*)0);
1024 if (isl_aff_is_nan(aff))
1025 return aff;
1026 aff = isl_aff_cow(aff);
1027 if (!aff)
1028 return NULL((void*)0);
1029
1030 aff->v = isl_vec_cow(aff->v);
1031 if (!aff->v)
1032 return isl_aff_free(aff);
1033
1034 isl_int_set_si(aff->v->el[1], v)isl_sioimath_set_si((aff->v->el[1]), v);
1035
1036 return aff;
1037}
1038
1039/* Replace the numerator of the coefficient of the variable of type "type"
1040 * at position "pos" of "aff" by "v".
1041 *
1042 * A NaN is unaffected by this operation.
1043 */
1044__isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1045 enum isl_dim_type type, int pos, isl_int v)
1046{
1047 if (!aff)
1048 return NULL((void*)0);
1049
1050 if (type == isl_dim_out)
1051 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1053); return isl_aff_free(aff); } while (0)
1052 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1053); return isl_aff_free(aff); } while (0)
1053 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1053); return isl_aff_free(aff); } while (0)
;
1054 if (type == isl_dim_in)
1055 type = isl_dim_set;
1056
1057 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1058 return isl_aff_free(aff);
1059
1060 if (isl_aff_is_nan(aff))
1061 return aff;
1062 aff = isl_aff_cow(aff);
1063 if (!aff)
1064 return NULL((void*)0);
1065
1066 aff->v = isl_vec_cow(aff->v);
1067 if (!aff->v)
1068 return isl_aff_free(aff);
1069
1070 pos += isl_local_space_offset(aff->ls, type);
1071 isl_int_set(aff->v->el[1 + pos], v)isl_sioimath_set((aff->v->el[1 + pos]), *(v));
1072
1073 return aff;
1074}
1075
1076/* Replace the numerator of the coefficient of the variable of type "type"
1077 * at position "pos" of "aff" by "v".
1078 *
1079 * A NaN is unaffected by this operation.
1080 */
1081__isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1082 enum isl_dim_type type, int pos, int v)
1083{
1084 if (!aff)
1085 return NULL((void*)0);
1086
1087 if (type == isl_dim_out)
1088 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1090); return isl_aff_free(aff); } while (0)
1089 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1090); return isl_aff_free(aff); } while (0)
1090 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1090); return isl_aff_free(aff); } while (0)
;
1091 if (type == isl_dim_in)
1092 type = isl_dim_set;
1093
1094 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1095 return isl_aff_free(aff);
1096
1097 if (isl_aff_is_nan(aff))
1098 return aff;
1099 pos += isl_local_space_offset(aff->ls, type);
1100 if (isl_int_cmp_si(aff->v->el[1 + pos], v)isl_sioimath_cmp_si(*(aff->v->el[1 + pos]), v) == 0)
1101 return aff;
1102
1103 aff = isl_aff_cow(aff);
1104 if (!aff)
1105 return NULL((void*)0);
1106
1107 aff->v = isl_vec_cow(aff->v);
1108 if (!aff->v)
1109 return isl_aff_free(aff);
1110
1111 isl_int_set_si(aff->v->el[1 + pos], v)isl_sioimath_set_si((aff->v->el[1 + pos]), v);
1112
1113 return aff;
1114}
1115
1116/* Replace the coefficient of the variable of type "type" at position "pos"
1117 * of "aff" by "v".
1118 *
1119 * A NaN is unaffected by this operation.
1120 */
1121__isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1122 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1123{
1124 if (!aff || !v)
1125 goto error;
1126
1127 if (type == isl_dim_out)
1128 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1130); goto error; } while (0)
1129 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1130); goto error; } while (0)
1130 goto error)do { isl_handle_error(aff->v->ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1130); goto error; } while (0)
;
1131 if (type == isl_dim_in)
1132 type = isl_dim_set;
1133
1134 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1135 return isl_aff_free(aff);
1136
1137 if (isl_aff_is_nan(aff)) {
1138 isl_val_free(v);
1139 return aff;
1140 }
1141 if (!isl_val_is_rat(v))
1142 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1143); goto error; } while (0)
1143 "expecting rational value", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational value", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1143); goto error; } while (0)
;
1144
1145 pos += isl_local_space_offset(aff->ls, type);
1146 if (isl_int_eq(aff->v->el[1 + pos], v->n)(isl_sioimath_cmp(*(aff->v->el[1 + pos]), *(v->n)) ==
0)
&&
1147 isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
1148 isl_val_free(v);
1149 return aff;
1150 }
1151
1152 aff = isl_aff_cow(aff);
1153 if (!aff)
1154 goto error;
1155 aff->v = isl_vec_cow(aff->v);
1156 if (!aff->v)
1157 goto error;
1158
1159 if (isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
1160 isl_int_set(aff->v->el[1 + pos], v->n)isl_sioimath_set((aff->v->el[1 + pos]), *(v->n));
1161 } else if (isl_int_is_one(v->d)(isl_sioimath_cmp_si(*(v->d), 1) == 0)) {
1162 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))
;
1163 } else {
1164 isl_seq_scale(aff->v->el + 1,
1165 aff->v->el + 1, v->d, aff->v->size - 1);
1166 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))
;
1167 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))
;
1168 aff->v = isl_vec_normalize(aff->v);
1169 if (!aff->v)
1170 goto error;
1171 }
1172
1173 isl_val_free(v);
1174 return aff;
1175error:
1176 isl_aff_free(aff);
1177 isl_val_free(v);
1178 return NULL((void*)0);
1179}
1180
1181/* Add "v" to the coefficient of the variable of type "type"
1182 * at position "pos" of "aff".
1183 *
1184 * A NaN is unaffected by this operation.
1185 */
1186__isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1187 enum isl_dim_type type, int pos, isl_int v)
1188{
1189 if (!aff)
1190 return NULL((void*)0);
1191
1192 if (type == isl_dim_out)
1193 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1195); return isl_aff_free(aff); } while (0)
1194 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1195); return isl_aff_free(aff); } while (0)
1195 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1195); return isl_aff_free(aff); } while (0)
;
1196 if (type == isl_dim_in)
1197 type = isl_dim_set;
1198
1199 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1200 return isl_aff_free(aff);
1201
1202 if (isl_aff_is_nan(aff))
1203 return aff;
1204 aff = isl_aff_cow(aff);
1205 if (!aff)
1206 return NULL((void*)0);
1207
1208 aff->v = isl_vec_cow(aff->v);
1209 if (!aff->v)
1210 return isl_aff_free(aff);
1211
1212 pos += isl_local_space_offset(aff->ls, type);
1213 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))
;
1214
1215 return aff;
1216}
1217
1218/* Add "v" to the coefficient of the variable of type "type"
1219 * at position "pos" of "aff".
1220 *
1221 * A NaN is unaffected by this operation.
1222 */
1223__isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1224 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1225{
1226 if (!aff || !v)
1227 goto error;
1228
1229 if (isl_val_is_zero(v)) {
1230 isl_val_free(v);
1231 return aff;
1232 }
1233
1234 if (type == isl_dim_out)
1235 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1237); goto error; } while (0)
1236 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1237); goto error; } while (0)
1237 goto error)do { isl_handle_error(aff->v->ctx, isl_error_invalid, "output/set dimension does not have a coefficient"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1237); goto error; } while (0)
;
1238 if (type == isl_dim_in)
1239 type = isl_dim_set;
1240
1241 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1242 goto error;
1243
1244 if (isl_aff_is_nan(aff)) {
1245 isl_val_free(v);
1246 return aff;
1247 }
1248 if (!isl_val_is_rat(v))
1249 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1250); goto error; } while (0)
1250 "expecting rational value", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational value", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1250); goto error; } while (0)
;
1251
1252 aff = isl_aff_cow(aff);
1253 if (!aff)
1254 goto error;
1255
1256 aff->v = isl_vec_cow(aff->v);
1257 if (!aff->v)
1258 goto error;
1259
1260 pos += isl_local_space_offset(aff->ls, type);
1261 if (isl_int_is_one(v->d)(isl_sioimath_cmp_si(*(v->d), 1) == 0)) {
1262 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))
;
1263 } else if (isl_int_eq(aff->v->el[0], v->d)(isl_sioimath_cmp(*(aff->v->el[0]), *(v->d)) == 0)) {
1264 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))
;
1265 aff->v = isl_vec_normalize(aff->v);
1266 if (!aff->v)
1267 goto error;
1268 } else {
1269 isl_seq_scale(aff->v->el + 1,
1270 aff->v->el + 1, v->d, aff->v->size - 1);
1271 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))
;
1272 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))
;
1273 aff->v = isl_vec_normalize(aff->v);
1274 if (!aff->v)
1275 goto error;
1276 }
1277
1278 isl_val_free(v);
1279 return aff;
1280error:
1281 isl_aff_free(aff);
1282 isl_val_free(v);
1283 return NULL((void*)0);
1284}
1285
1286__isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1287 enum isl_dim_type type, int pos, int v)
1288{
1289 isl_int t;
1290
1291 isl_int_init(t)isl_sioimath_init((t));
1292 isl_int_set_si(t, v)isl_sioimath_set_si((t), v);
1293 aff = isl_aff_add_coefficient(aff, type, pos, t);
1294 isl_int_clear(t)isl_sioimath_clear((t));
1295
1296 return aff;
1297}
1298
1299__isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1300{
1301 if (!aff)
1302 return NULL((void*)0);
1303
1304 return isl_local_space_get_div(aff->ls, pos);
1305}
1306
1307/* Return the negation of "aff".
1308 *
1309 * As a special case, -NaN = NaN.
1310 */
1311__isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1312{
1313 if (!aff)
1314 return NULL((void*)0);
1315 if (isl_aff_is_nan(aff))
1316 return aff;
1317 aff = isl_aff_cow(aff);
1318 if (!aff)
1319 return NULL((void*)0);
1320 aff->v = isl_vec_cow(aff->v);
1321 if (!aff->v)
1322 return isl_aff_free(aff);
1323
1324 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1325
1326 return aff;
1327}
1328
1329/* Remove divs from the local space that do not appear in the affine
1330 * expression.
1331 * We currently only remove divs at the end.
1332 * Some intermediate divs may also not appear directly in the affine
1333 * expression, but we would also need to check that no other divs are
1334 * defined in terms of them.
1335 */
1336__isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1337{
1338 int pos;
1339 isl_size off;
1340 isl_size n;
1341
1342 n = isl_aff_domain_dim(aff, isl_dim_div);
1343 off = isl_aff_domain_offset(aff, isl_dim_div);
1344 if (n < 0 || off < 0)
1345 return isl_aff_free(aff);
1346
1347 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1348 if (pos == n)
1349 return aff;
1350
1351 aff = isl_aff_cow(aff);
1352 if (!aff)
1353 return NULL((void*)0);
1354
1355 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1356 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1357 if (!aff->ls || !aff->v)
1358 return isl_aff_free(aff);
1359
1360 return aff;
1361}
1362
1363/* Look for any divs in the aff->ls with a denominator equal to one
1364 * and plug them into the affine expression and any subsequent divs
1365 * that may reference the div.
1366 */
1367static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1368{
1369 int i;
1370 isl_size n;
1371 int len;
1372 isl_int v;
1373 isl_vec *vec;
1374 isl_local_space *ls;
1375 isl_size off;
1376
1377 n = isl_aff_domain_dim(aff, isl_dim_div);
1378 off = isl_aff_domain_offset(aff, isl_dim_div);
1379 if (n < 0 || off < 0)
1380 return isl_aff_free(aff);
1381 len = aff->v->size;
1382 for (i = 0; i < n; ++i) {
1383 if (!isl_int_is_one(aff->ls->div->row[i][0])(isl_sioimath_cmp_si(*(aff->ls->div->row[i][0]), 1) ==
0)
)
1384 continue;
1385 ls = isl_local_space_copy(aff->ls);
1386 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1387 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1388 vec = isl_vec_copy(aff->v);
1389 vec = isl_vec_cow(vec);
1390 if (!ls || !vec)
1391 goto error;
1392
1393 isl_int_init(v)isl_sioimath_init((v));
1394
1395 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1396 len, len, v);
1397
1398 isl_int_clear(v)isl_sioimath_clear((v));
1399
1400 isl_vec_free(aff->v);
1401 aff->v = vec;
1402 isl_local_space_free(aff->ls);
1403 aff->ls = ls;
1404 }
1405
1406 return aff;
1407error:
1408 isl_vec_free(vec);
1409 isl_local_space_free(ls);
1410 return isl_aff_free(aff);
1411}
1412
1413/* Look for any divs j that appear with a unit coefficient inside
1414 * the definitions of other divs i and plug them into the definitions
1415 * of the divs i.
1416 *
1417 * In particular, an expression of the form
1418 *
1419 * floor((f(..) + floor(g(..)/n))/m)
1420 *
1421 * is simplified to
1422 *
1423 * floor((n * f(..) + g(..))/(n * m))
1424 *
1425 * This simplification is correct because we can move the expression
1426 * f(..) into the inner floor in the original expression to obtain
1427 *
1428 * floor(floor((n * f(..) + g(..))/n)/m)
1429 *
1430 * from which we can derive the simplified expression.
1431 */
1432static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1433{
1434 int i, j;
1435 isl_size n;
1436 isl_size off;
1437
1438 n = isl_aff_domain_dim(aff, isl_dim_div);
1439 off = isl_aff_domain_offset(aff, isl_dim_div);
1440 if (n < 0 || off < 0)
1441 return isl_aff_free(aff);
1442 for (i = 1; i < n; ++i) {
1443 for (j = 0; j < i; ++j) {
1444 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)
)
1445 continue;
1446 aff->ls = isl_local_space_substitute_seq(aff->ls,
1447 isl_dim_div, j, aff->ls->div->row[j],
1448 aff->v->size, i, 1);
1449 if (!aff->ls)
1450 return isl_aff_free(aff);
1451 }
1452 }
1453
1454 return aff;
1455}
1456
1457/* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1458 *
1459 * Even though this function is only called on isl_affs with a single
1460 * reference, we are careful to only change aff->v and aff->ls together.
1461 */
1462static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1463{
1464 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1465 isl_local_space *ls;
1466 isl_vec *v;
1467
1468 if (off < 0)
1469 return isl_aff_free(aff);
1470
1471 ls = isl_local_space_copy(aff->ls);
1472 ls = isl_local_space_swap_div(ls, a, b);
1473 v = isl_vec_copy(aff->v);
1474 v = isl_vec_cow(v);
1475 if (!ls || !v)
1476 goto error;
1477
1478 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]))
;
1479 isl_vec_free(aff->v);
1480 aff->v = v;
1481 isl_local_space_free(aff->ls);
1482 aff->ls = ls;
1483
1484 return aff;
1485error:
1486 isl_vec_free(v);
1487 isl_local_space_free(ls);
1488 return isl_aff_free(aff);
1489}
1490
1491/* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1492 *
1493 * We currently do not actually remove div "b", but simply add its
1494 * coefficient to that of "a" and then zero it out.
1495 */
1496static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1497{
1498 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1499
1500 if (off < 0)
1501 return isl_aff_free(aff);
1502
1503 if (isl_int_is_zero(aff->v->el[1 + off + b])(isl_sioimath_sgn(*(aff->v->el[1 + off + b])) == 0))
1504 return aff;
1505
1506 aff->v = isl_vec_cow(aff->v);
1507 if (!aff->v)
1508 return isl_aff_free(aff);
1509
1510 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]))
1511 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]))
;
1512 isl_int_set_si(aff->v->el[1 + off + b], 0)isl_sioimath_set_si((aff->v->el[1 + off + b]), 0);
1513
1514 return aff;
1515}
1516
1517/* Sort the divs in the local space of "aff" according to
1518 * the comparison function "cmp_row" in isl_local_space.c,
1519 * combining the coefficients of identical divs.
1520 *
1521 * Reordering divs does not change the semantics of "aff",
1522 * so there is no need to call isl_aff_cow.
1523 * Moreover, this function is currently only called on isl_affs
1524 * with a single reference.
1525 */
1526static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1527{
1528 isl_size n;
1529 int i, j;
1530
1531 n = isl_aff_dim(aff, isl_dim_div);
1532 if (n < 0)
1533 return isl_aff_free(aff);
1534 for (i = 1; i < n; ++i) {
1535 for (j = i - 1; j >= 0; --j) {
1536 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1537 if (cmp < 0)
1538 break;
1539 if (cmp == 0)
1540 aff = merge_divs(aff, j, j + 1);
1541 else
1542 aff = swap_div(aff, j, j + 1);
1543 if (!aff)
1544 return NULL((void*)0);
1545 }
1546 }
1547
1548 return aff;
1549}
1550
1551/* Normalize the representation of "aff".
1552 *
1553 * This function should only be called of "new" isl_affs, i.e.,
1554 * with only a single reference. We therefore do not need to
1555 * worry about affecting other instances.
1556 */
1557__isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1558{
1559 if (!aff)
1560 return NULL((void*)0);
1561 aff->v = isl_vec_normalize(aff->v);
1562 if (!aff->v)
1563 return isl_aff_free(aff);
1564 aff = plug_in_integral_divs(aff);
1565 aff = plug_in_unit_divs(aff);
1566 aff = sort_divs(aff);
1567 aff = isl_aff_remove_unused_divs(aff);
1568 return aff;
1569}
1570
1571/* Given f, return floor(f).
1572 * If f is an integer expression, then just return f.
1573 * If f is a constant, then return the constant floor(f).
1574 * Otherwise, if f = g/m, write g = q m + r,
1575 * create a new div d = [r/m] and return the expression q + d.
1576 * The coefficients in r are taken to lie between -m/2 and m/2.
1577 *
1578 * reduce_div_coefficients performs the same normalization.
1579 *
1580 * As a special case, floor(NaN) = NaN.
1581 */
1582__isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1583{
1584 int i;
1585 int size;
1586 isl_ctx *ctx;
1587 isl_vec *div;
1588
1589 if (!aff)
1590 return NULL((void*)0);
1591
1592 if (isl_aff_is_nan(aff))
1593 return aff;
1594 if (isl_int_is_one(aff->v->el[0])(isl_sioimath_cmp_si(*(aff->v->el[0]), 1) == 0))
1595 return aff;
1596
1597 aff = isl_aff_cow(aff);
1598 if (!aff)
1599 return NULL((void*)0);
1600
1601 aff->v = isl_vec_cow(aff->v);
1602 if (!aff->v)
1603 return isl_aff_free(aff);
1604
1605 if (isl_aff_is_cst(aff)) {
1606 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]))
;
1607 isl_int_set_si(aff->v->el[0], 1)isl_sioimath_set_si((aff->v->el[0]), 1);
1608 return aff;
1609 }
1610
1611 div = isl_vec_copy(aff->v);
1612 div = isl_vec_cow(div);
1613 if (!div)
1614 return isl_aff_free(aff);
1615
1616 ctx = isl_aff_get_ctx(aff);
1617 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))
;
1618 for (i = 1; i < aff->v->size; ++i) {
1619 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]))
;
1620 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]))
;
1621 if (isl_int_gt(div->el[i], aff->v->el[0])(isl_sioimath_cmp(*(div->el[i]), *(aff->v->el[0])) >
0)
) {
1622 isl_int_sub(div->el[i], div->el[i], div->el[0])isl_sioimath_sub((div->el[i]), *(div->el[i]), *(div->
el[0]))
;
1623 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)
;
1624 }
1625 }
1626
1627 aff->ls = isl_local_space_add_div(aff->ls, div);
1628 if (!aff->ls)
1629 return isl_aff_free(aff);
1630
1631 size = aff->v->size;
1632 aff->v = isl_vec_extend(aff->v, size + 1);
1633 if (!aff->v)
1634 return isl_aff_free(aff);
1635 isl_int_set_si(aff->v->el[0], 1)isl_sioimath_set_si((aff->v->el[0]), 1);
1636 isl_int_set_si(aff->v->el[size], 1)isl_sioimath_set_si((aff->v->el[size]), 1);
1637
1638 aff = isl_aff_normalize(aff);
1639
1640 return aff;
1641}
1642
1643/* Compute
1644 *
1645 * aff mod m = aff - m * floor(aff/m)
1646 *
1647 * with m an integer value.
1648 */
1649__isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1650 __isl_take isl_val *m)
1651{
1652 isl_aff *res;
1653
1654 if (!aff || !m)
1655 goto error;
1656
1657 if (!isl_val_is_int(m))
1658 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1659); goto error; } while (0)
1659 "expecting integer modulo", goto error)do { isl_handle_error(isl_val_get_ctx(m), isl_error_invalid, "expecting integer modulo"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1659); goto error; } while (0)
;
1660
1661 res = isl_aff_copy(aff);
1662 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1663 aff = isl_aff_floor(aff);
1664 aff = isl_aff_scale_val(aff, m);
1665 res = isl_aff_sub(res, aff);
1666
1667 return res;
1668error:
1669 isl_aff_free(aff);
1670 isl_val_free(m);
1671 return NULL((void*)0);
1672}
1673
1674/* Compute
1675 *
1676 * pwaff mod m = pwaff - m * floor(pwaff/m)
1677 */
1678__isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1679{
1680 isl_pw_aff *res;
1681
1682 res = isl_pw_aff_copy(pwaff);
1683 pwaff = isl_pw_aff_scale_down(pwaff, m);
1684 pwaff = isl_pw_aff_floor(pwaff);
1685 pwaff = isl_pw_aff_scale(pwaff, m);
1686 res = isl_pw_aff_sub(res, pwaff);
1687
1688 return res;
1689}
1690
1691/* Compute
1692 *
1693 * pa mod m = pa - m * floor(pa/m)
1694 *
1695 * with m an integer value.
1696 */
1697__isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1698 __isl_take isl_val *m)
1699{
1700 if (!pa || !m)
1701 goto error;
1702 if (!isl_val_is_int(m))
1703 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1704); goto error; } while (0)
1704 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1704); goto error; } while (0)
;
1705 pa = isl_pw_aff_mod(pa, m->n);
1706 isl_val_free(m);
1707 return pa;
1708error:
1709 isl_pw_aff_free(pa);
1710 isl_val_free(m);
1711 return NULL((void*)0);
1712}
1713
1714/* Given f, return ceil(f).
1715 * If f is an integer expression, then just return f.
1716 * Otherwise, let f be the expression
1717 *
1718 * e/m
1719 *
1720 * then return
1721 *
1722 * floor((e + m - 1)/m)
1723 *
1724 * As a special case, ceil(NaN) = NaN.
1725 */
1726__isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1727{
1728 if (!aff)
1729 return NULL((void*)0);
1730
1731 if (isl_aff_is_nan(aff))
1732 return aff;
1733 if (isl_int_is_one(aff->v->el[0])(isl_sioimath_cmp_si(*(aff->v->el[0]), 1) == 0))
1734 return aff;
1735
1736 aff = isl_aff_cow(aff);
1737 if (!aff)
1738 return NULL((void*)0);
1739 aff->v = isl_vec_cow(aff->v);
1740 if (!aff->v)
1741 return isl_aff_free(aff);
1742
1743 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]))
;
1744 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)
;
1745 aff = isl_aff_floor(aff);
1746
1747 return aff;
1748}
1749
1750/* Apply the expansion computed by isl_merge_divs.
1751 * The expansion itself is given by "exp" while the resulting
1752 * list of divs is given by "div".
1753 */
1754__isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1755 __isl_take isl_mat *div, int *exp)
1756{
1757 isl_size old_n_div;
1758 isl_size new_n_div;
1759 isl_size offset;
1760
1761 aff = isl_aff_cow(aff);
1762
1763 offset = isl_aff_domain_offset(aff, isl_dim_div);
1764 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1765 new_n_div = isl_mat_rows(div);
1766 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1767 goto error;
1768
1769 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1770 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1771 if (!aff->v || !aff->ls)
1772 return isl_aff_free(aff);
1773 return aff;
1774error:
1775 isl_aff_free(aff);
1776 isl_mat_free(div);
1777 return NULL((void*)0);
1778}
1779
1780/* Add two affine expressions that live in the same local space.
1781 */
1782static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1783 __isl_take isl_aff *aff2)
1784{
1785 isl_int gcd, f;
1786
1787 aff1 = isl_aff_cow(aff1);
1788 if (!aff1 || !aff2)
1789 goto error;
1790
1791 aff1->v = isl_vec_cow(aff1->v);
1792 if (!aff1->v)
1793 goto error;
1794
1795 isl_int_init(gcd)isl_sioimath_init((gcd));
1796 isl_int_init(f)isl_sioimath_init((f));
1797 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0])isl_sioimath_gcd((gcd), *(aff1->v->el[0]), *(aff2->v
->el[0]))
;
1798 isl_int_divexact(f, aff2->v->el[0], gcd)isl_sioimath_tdiv_q((f), *(aff2->v->el[0]), *(gcd));
1799 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1800 isl_int_divexact(f, aff1->v->el[0], gcd)isl_sioimath_tdiv_q((f), *(aff1->v->el[0]), *(gcd));
1801 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1802 isl_int_divexact(f, aff2->v->el[0], gcd)isl_sioimath_tdiv_q((f), *(aff2->v->el[0]), *(gcd));
1803 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f)isl_sioimath_mul((aff1->v->el[0]), *(aff1->v->el[
0]), *(f))
;
1804 isl_int_clear(f)isl_sioimath_clear((f));
1805 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1806
1807 isl_aff_free(aff2);
1808 aff1 = isl_aff_normalize(aff1);
1809 return aff1;
1810error:
1811 isl_aff_free(aff1);
1812 isl_aff_free(aff2);
1813 return NULL((void*)0);
1814}
1815
1816/* Return the sum of "aff1" and "aff2".
1817 *
1818 * If either of the two is NaN, then the result is NaN.
1819 */
1820__isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1821 __isl_take isl_aff *aff2)
1822{
1823 isl_ctx *ctx;
1824 int *exp1 = NULL((void*)0);
1825 int *exp2 = NULL((void*)0);
1826 isl_mat *div;
1827 isl_size n_div1, n_div2;
1828
1829 if (!aff1 || !aff2)
1830 goto error;
1831
1832 ctx = isl_aff_get_ctx(aff1);
1833 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1834 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "spaces don't match"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1835); goto error; } while (0)
1835 "spaces don't match", goto error)do { isl_handle_error(ctx, isl_error_invalid, "spaces don't match"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1835); goto error; } while (0)
;
1836
1837 if (isl_aff_is_nan(aff1)) {
1838 isl_aff_free(aff2);
1839 return aff1;
1840 }
1841 if (isl_aff_is_nan(aff2)) {
1842 isl_aff_free(aff1);
1843 return aff2;
1844 }
1845
1846 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1847 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1848 if (n_div1 < 0 || n_div2 < 0)
1849 goto error;
1850 if (n_div1 == 0 && n_div2 == 0)
1851 return add_expanded(aff1, aff2);
1852
1853 exp1 = isl_alloc_array(ctx, int, n_div1)((int *)isl_malloc_or_die(ctx, (n_div1)*sizeof(int)));
1854 exp2 = isl_alloc_array(ctx, int, n_div2)((int *)isl_malloc_or_die(ctx, (n_div2)*sizeof(int)));
1855 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1856 goto error;
1857
1858 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1859 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1860 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1861 free(exp1);
1862 free(exp2);
1863
1864 return add_expanded(aff1, aff2);
1865error:
1866 free(exp1);
1867 free(exp2);
1868 isl_aff_free(aff1);
1869 isl_aff_free(aff2);
1870 return NULL((void*)0);
1871}
1872
1873__isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1874 __isl_take isl_aff *aff2)
1875{
1876 return isl_aff_add(aff1, isl_aff_neg(aff2));
1877}
1878
1879/* Return the result of scaling "aff" by a factor of "f".
1880 *
1881 * As a special case, f * NaN = NaN.
1882 */
1883__isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1884{
1885 isl_int gcd;
1886
1887 if (!aff)
1888 return NULL((void*)0);
1889 if (isl_aff_is_nan(aff))
1890 return aff;
1891
1892 if (isl_int_is_one(f)(isl_sioimath_cmp_si(*(f), 1) == 0))
1893 return aff;
1894
1895 aff = isl_aff_cow(aff);
1896 if (!aff)
1897 return NULL((void*)0);
1898 aff->v = isl_vec_cow(aff->v);
1899 if (!aff->v)
1900 return isl_aff_free(aff);
1901
1902 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))) {
1903 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))
;
1904 return aff;
1905 }
1906
1907 isl_int_init(gcd)isl_sioimath_init((gcd));
1908 isl_int_gcd(gcd, aff->v->el[0], f)isl_sioimath_gcd((gcd), *(aff->v->el[0]), *(f));
1909 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))
;
1910 isl_int_divexact(gcd, f, gcd)isl_sioimath_tdiv_q((gcd), *(f), *(gcd));
1911 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1912 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1913
1914 return aff;
1915}
1916
1917/* Multiple "aff" by "v".
1918 */
1919__isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1920 __isl_take isl_val *v)
1921{
1922 if (!aff || !v)
1923 goto error;
1924
1925 if (isl_val_is_one(v)) {
1926 isl_val_free(v);
1927 return aff;
1928 }
1929
1930 if (!isl_val_is_rat(v))
1931 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1932); goto error; } while (0)
1932 "expecting rational factor", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1932); goto error; } while (0)
;
1933
1934 aff = isl_aff_scale(aff, v->n);
1935 aff = isl_aff_scale_down(aff, v->d);
1936
1937 isl_val_free(v);
1938 return aff;
1939error:
1940 isl_aff_free(aff);
1941 isl_val_free(v);
1942 return NULL((void*)0);
1943}
1944
1945/* Return the result of scaling "aff" down by a factor of "f".
1946 *
1947 * As a special case, NaN/f = NaN.
1948 */
1949__isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1950{
1951 isl_int gcd;
1952
1953 if (!aff)
1954 return NULL((void*)0);
1955 if (isl_aff_is_nan(aff))
1956 return aff;
1957
1958 if (isl_int_is_one(f)(isl_sioimath_cmp_si(*(f), 1) == 0))
1959 return aff;
1960
1961 aff = isl_aff_cow(aff);
1962 if (!aff)
1963 return NULL((void*)0);
1964
1965 if (isl_int_is_zero(f)(isl_sioimath_sgn(*(f)) == 0))
1966 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1967); return isl_aff_free(aff); } while (0)
1967 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1967); return isl_aff_free(aff); } while (0)
;
1968
1969 aff->v = isl_vec_cow(aff->v);
1970 if (!aff->v)
1971 return isl_aff_free(aff);
1972
1973 isl_int_init(gcd)isl_sioimath_init((gcd));
1974 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1975 isl_int_gcd(gcd, gcd, f)isl_sioimath_gcd((gcd), *(gcd), *(f));
1976 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1977 isl_int_divexact(gcd, f, gcd)isl_sioimath_tdiv_q((gcd), *(f), *(gcd));
1978 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd)isl_sioimath_mul((aff->v->el[0]), *(aff->v->el[0]
), *(gcd))
;
1979 isl_int_clear(gcd)isl_sioimath_clear((gcd));
1980
1981 return aff;
1982}
1983
1984/* Divide "aff" by "v".
1985 */
1986__isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1987 __isl_take isl_val *v)
1988{
1989 if (!aff || !v)
1990 goto error;
1991
1992 if (isl_val_is_one(v)) {
1993 isl_val_free(v);
1994 return aff;
1995 }
1996
1997 if (!isl_val_is_rat(v))
1998 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1999); goto error; } while (0)
1999 "expecting rational factor", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 1999); goto error; } while (0)
;
2000 if (!isl_val_is_pos(v))
2001 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2002); goto error; } while (0)
2002 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2002); goto error; } while (0)
;
2003
2004 aff = isl_aff_scale(aff, v->d);
2005 aff = isl_aff_scale_down(aff, v->n);
2006
2007 isl_val_free(v);
2008 return aff;
2009error:
2010 isl_aff_free(aff);
2011 isl_val_free(v);
2012 return NULL((void*)0);
2013}
2014
2015__isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2016{
2017 isl_int v;
2018
2019 if (f == 1)
2020 return aff;
2021
2022 isl_int_init(v)isl_sioimath_init((v));
2023 isl_int_set_ui(v, f)isl_sioimath_set_ui((v), f);
2024 aff = isl_aff_scale_down(aff, v);
2025 isl_int_clear(v)isl_sioimath_clear((v));
2026
2027 return aff;
2028}
2029
2030__isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2031 enum isl_dim_type type, unsigned pos, const char *s)
2032{
2033 aff = isl_aff_cow(aff);
2034 if (!aff)
2035 return NULL((void*)0);
2036 if (type == isl_dim_out)
2037 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2039); return isl_aff_free(aff); } while (0)
2038 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2039); return isl_aff_free(aff); } while (0)
2039 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2039); return isl_aff_free(aff); } while (0)
;
2040 if (type == isl_dim_in)
2041 type = isl_dim_set;
2042 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2043 if (!aff->ls)
2044 return isl_aff_free(aff);
2045
2046 return aff;
2047}
2048
2049__isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2050 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2051{
2052 aff = isl_aff_cow(aff);
2053 if (!aff)
2054 goto error;
2055 if (type == isl_dim_out)
2056 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2058); goto error; } while (0)
2057 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2058); goto error; } while (0)
2058 goto error)do { isl_handle_error(aff->v->ctx, isl_error_invalid, "cannot set name of output/set dimension"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2058); goto error; } while (0)
;
2059 if (type == isl_dim_in)
2060 type = isl_dim_set;
2061 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2062 if (!aff->ls)
2063 return isl_aff_free(aff);
2064
2065 return aff;
2066error:
2067 isl_id_free(id);
2068 isl_aff_free(aff);
2069 return NULL((void*)0);
2070}
2071
2072/* Replace the identifier of the input tuple of "aff" by "id".
2073 * type is currently required to be equal to isl_dim_in
2074 */
2075__isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2076 enum isl_dim_type type, __isl_take isl_id *id)
2077{
2078 aff = isl_aff_cow(aff);
2079 if (!aff)
2080 goto error;
2081 if (type != isl_dim_in)
2082 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2083); goto error; } while (0)
2083 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2083); goto error; } while (0)
;
2084 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2085 if (!aff->ls)
2086 return isl_aff_free(aff);
2087
2088 return aff;
2089error:
2090 isl_id_free(id);
2091 isl_aff_free(aff);
2092 return NULL((void*)0);
2093}
2094
2095/* Exploit the equalities in "eq" to simplify the affine expression
2096 * and the expressions of the integer divisions in the local space.
2097 * The integer divisions in this local space are assumed to appear
2098 * as regular dimensions in "eq".
2099 */
2100static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2101 __isl_take isl_aff *aff, __isl_take isl_basic_setisl_basic_map *eq)
2102{
2103 int i, j;
2104 unsigned o_div;
2105 unsigned n_div;
2106
2107 if (!eq)
2108 goto error;
2109 if (eq->n_eq == 0) {
2110 isl_basic_set_free(eq);
2111 return aff;
2112 }
2113
2114 aff = isl_aff_cow(aff);
2115 if (!aff)
2116 goto error;
2117
2118 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2119 isl_basic_set_copy(eq));
2120 aff->v = isl_vec_cow(aff->v);
2121 if (!aff->ls || !aff->v)
2122 goto error;
2123
2124 o_div = isl_basic_set_offset(eq, isl_dim_div);
2125 n_div = eq->n_div;
2126 for (i = 0; i < eq->n_eq; ++i) {
2127 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2128 if (j < 0 || j == 0 || j >= o_div)
2129 continue;
2130
2131 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2132 &aff->v->el[0]);
2133 }
2134
2135 isl_basic_set_free(eq);
2136 aff = isl_aff_normalize(aff);
2137 return aff;
2138error:
2139 isl_basic_set_free(eq);
2140 isl_aff_free(aff);
2141 return NULL((void*)0);
2142}
2143
2144/* Exploit the equalities in "eq" to simplify the affine expression
2145 * and the expressions of the integer divisions in the local space.
2146 */
2147__isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2148 __isl_take isl_basic_setisl_basic_map *eq)
2149{
2150 isl_size n_div;
2151
2152 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2153 if (n_div < 0)
2154 goto error;
2155 if (n_div > 0)
2156 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2157 return isl_aff_substitute_equalities_lifted(aff, eq);
2158error:
2159 isl_basic_set_free(eq);
2160 isl_aff_free(aff);
2161 return NULL((void*)0);
2162}
2163
2164/* Look for equalities among the variables shared by context and aff
2165 * and the integer divisions of aff, if any.
2166 * The equalities are then used to eliminate coefficients and/or integer
2167 * divisions from aff.
2168 */
2169__isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2170 __isl_take isl_setisl_map *context)
2171{
2172 isl_local_space *ls;
2173 isl_basic_setisl_basic_map *hull;
2174
2175 ls = isl_aff_get_domain_local_space(aff);
2176 context = isl_local_space_lift_set(ls, context);
2177
2178 hull = isl_set_affine_hull(context);
2179 return isl_aff_substitute_equalities_lifted(aff, hull);
2180}
2181
2182__isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2183 __isl_take isl_setisl_map *context)
2184{
2185 isl_setisl_map *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2186 dom_context = isl_set_intersect_params(dom_context, context);
2187 return isl_aff_gist(aff, dom_context);
2188}
2189
2190/* Return a basic set containing those elements in the space
2191 * of aff where it is positive. "rational" should not be set.
2192 *
2193 * If "aff" is NaN, then it is not positive.
2194 */
2195static __isl_give isl_basic_setisl_basic_map *aff_pos_basic_set(__isl_take isl_aff *aff,
2196 int rational, void *user)
2197{
2198 isl_constraint *ineq;
2199 isl_basic_setisl_basic_map *bset;
2200 isl_val *c;
2201
2202 if (!aff)
2203 return NULL((void*)0);
2204 if (isl_aff_is_nan(aff)) {
2205 isl_space *space = isl_aff_get_domain_space(aff);
2206 isl_aff_free(aff);
2207 return isl_basic_set_empty(space);
2208 }
2209 if (rational)
2210 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2211); goto error; } while (0)
2211 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2211); goto error; } while (0)
;
2212
2213 ineq = isl_inequality_from_aff(aff);
2214 c = isl_constraint_get_constant_val(ineq);
2215 c = isl_val_sub_ui(c, 1);
2216 ineq = isl_constraint_set_constant_val(ineq, c);
2217
2218 bset = isl_basic_set_from_constraint(ineq);
2219 bset = isl_basic_set_simplify(bset);
2220 return bset;
2221error:
2222 isl_aff_free(aff);
2223 return NULL((void*)0);
2224}
2225
2226/* Return a basic set containing those elements in the space
2227 * of aff where it is non-negative.
2228 * If "rational" is set, then return a rational basic set.
2229 *
2230 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2231 */
2232static __isl_give isl_basic_setisl_basic_map *aff_nonneg_basic_set(
2233 __isl_take isl_aff *aff, int rational, void *user)
2234{
2235 isl_constraint *ineq;
2236 isl_basic_setisl_basic_map *bset;
2237
2238 if (!aff)
2239 return NULL((void*)0);
2240 if (isl_aff_is_nan(aff)) {
2241 isl_space *space = isl_aff_get_domain_space(aff);
2242 isl_aff_free(aff);
2243 return isl_basic_set_empty(space);
2244 }
2245
2246 ineq = isl_inequality_from_aff(aff);
2247
2248 bset = isl_basic_set_from_constraint(ineq);
2249 if (rational)
2250 bset = isl_basic_set_set_rational(bset);
2251 bset = isl_basic_set_simplify(bset);
2252 return bset;
2253}
2254
2255/* Return a basic set containing those elements in the space
2256 * of aff where it is non-negative.
2257 */
2258__isl_give isl_basic_setisl_basic_map *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2259{
2260 return aff_nonneg_basic_set(aff, 0, NULL((void*)0));
2261}
2262
2263/* Return a basic set containing those elements in the domain space
2264 * of "aff" where it is positive.
2265 */
2266__isl_give isl_basic_setisl_basic_map *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2267{
2268 aff = isl_aff_add_constant_num_si(aff, -1);
2269 return isl_aff_nonneg_basic_set(aff);
2270}
2271
2272/* Return a basic set containing those elements in the domain space
2273 * of aff where it is negative.
2274 */
2275__isl_give isl_basic_setisl_basic_map *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2276{
2277 aff = isl_aff_neg(aff);
2278 return isl_aff_pos_basic_set(aff);
2279}
2280
2281/* Return a basic set containing those elements in the space
2282 * of aff where it is zero.
2283 * If "rational" is set, then return a rational basic set.
2284 *
2285 * If "aff" is NaN, then it is not zero.
2286 */
2287static __isl_give isl_basic_setisl_basic_map *aff_zero_basic_set(__isl_take isl_aff *aff,
2288 int rational, void *user)
2289{
2290 isl_constraint *ineq;
2291 isl_basic_setisl_basic_map *bset;
2292
2293 if (!aff)
2294 return NULL((void*)0);
2295 if (isl_aff_is_nan(aff)) {
2296 isl_space *space = isl_aff_get_domain_space(aff);
2297 isl_aff_free(aff);
2298 return isl_basic_set_empty(space);
2299 }
2300
2301 ineq = isl_equality_from_aff(aff);
2302
2303 bset = isl_basic_set_from_constraint(ineq);
2304 if (rational)
2305 bset = isl_basic_set_set_rational(bset);
2306 bset = isl_basic_set_simplify(bset);
2307 return bset;
2308}
2309
2310/* Return a basic set containing those elements in the space
2311 * of aff where it is zero.
2312 */
2313__isl_give isl_basic_setisl_basic_map *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2314{
2315 return aff_zero_basic_set(aff, 0, NULL((void*)0));
2316}
2317
2318/* Return a basic set containing those elements in the shared space
2319 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2320 */
2321__isl_give isl_basic_setisl_basic_map *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2322 __isl_take isl_aff *aff2)
2323{
2324 aff1 = isl_aff_sub(aff1, aff2);
2325
2326 return isl_aff_nonneg_basic_set(aff1);
2327}
2328
2329/* Return a basic set containing those elements in the shared domain space
2330 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2331 */
2332__isl_give isl_basic_setisl_basic_map *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2333 __isl_take isl_aff *aff2)
2334{
2335 aff1 = isl_aff_sub(aff1, aff2);
2336
2337 return isl_aff_pos_basic_set(aff1);
2338}
2339
2340/* Return a set containing those elements in the shared space
2341 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2342 */
2343__isl_give isl_setisl_map *isl_aff_ge_set(__isl_take isl_aff *aff1,
2344 __isl_take isl_aff *aff2)
2345{
2346 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2347}
2348
2349/* Return a set containing those elements in the shared domain space
2350 * of aff1 and aff2 where aff1 is greater than aff2.
2351 *
2352 * If either of the two inputs is NaN, then the result is empty,
2353 * as comparisons with NaN always return false.
2354 */
2355__isl_give isl_setisl_map *isl_aff_gt_set(__isl_take isl_aff *aff1,
2356 __isl_take isl_aff *aff2)
2357{
2358 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2359}
2360
2361/* Return a basic set containing those elements in the shared space
2362 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2363 */
2364__isl_give isl_basic_setisl_basic_map *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2365 __isl_take isl_aff *aff2)
2366{
2367 return isl_aff_ge_basic_set(aff2, aff1);
2368}
2369
2370/* Return a basic set containing those elements in the shared domain space
2371 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2372 */
2373__isl_give isl_basic_setisl_basic_map *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2374 __isl_take isl_aff *aff2)
2375{
2376 return isl_aff_gt_basic_set(aff2, aff1);
2377}
2378
2379/* Return a set containing those elements in the shared space
2380 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2381 */
2382__isl_give isl_setisl_map *isl_aff_le_set(__isl_take isl_aff *aff1,
2383 __isl_take isl_aff *aff2)
2384{
2385 return isl_aff_ge_set(aff2, aff1);
2386}
2387
2388/* Return a set containing those elements in the shared domain space
2389 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2390 */
2391__isl_give isl_setisl_map *isl_aff_lt_set(__isl_take isl_aff *aff1,
2392 __isl_take isl_aff *aff2)
2393{
2394 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2395}
2396
2397/* Return a basic set containing those elements in the shared space
2398 * of aff1 and aff2 where aff1 and aff2 are equal.
2399 */
2400__isl_give isl_basic_setisl_basic_map *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2401 __isl_take isl_aff *aff2)
2402{
2403 aff1 = isl_aff_sub(aff1, aff2);
2404
2405 return isl_aff_zero_basic_set(aff1);
2406}
2407
2408/* Return a set containing those elements in the shared space
2409 * of aff1 and aff2 where aff1 and aff2 are equal.
2410 */
2411__isl_give isl_setisl_map *isl_aff_eq_set(__isl_take isl_aff *aff1,
2412 __isl_take isl_aff *aff2)
2413{
2414 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2415}
2416
2417/* Return a set containing those elements in the shared domain space
2418 * of aff1 and aff2 where aff1 and aff2 are not equal.
2419 *
2420 * If either of the two inputs is NaN, then the result is empty,
2421 * as comparisons with NaN always return false.
2422 */
2423__isl_give isl_setisl_map *isl_aff_ne_set(__isl_take isl_aff *aff1,
2424 __isl_take isl_aff *aff2)
2425{
2426 isl_setisl_map *set_lt, *set_gt;
2427
2428 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2429 isl_aff_copy(aff2));
2430 set_gt = isl_aff_gt_set(aff1, aff2);
2431 return isl_set_union_disjoint(set_lt, set_gt);
2432}
2433
2434__isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_setisl_map *dom,
2435 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2436{
2437 aff1 = isl_aff_add(aff1, aff2);
2438 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2439 return aff1;
2440}
2441
2442isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2443{
2444 if (!aff)
2445 return isl_bool_error;
2446
2447 return isl_bool_false;
2448}
2449
2450#undef TYPEisl_multi_union_pw_aff
2451#define TYPEisl_multi_union_pw_aff isl_aff
2452static
2453#include "check_type_range_templ.c"
2454
2455/* Check whether the given affine expression has non-zero coefficient
2456 * for any dimension in the given range or if any of these dimensions
2457 * appear with non-zero coefficients in any of the integer divisions
2458 * involved in the affine expression.
2459 */
2460isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2461 enum isl_dim_type type, unsigned first, unsigned n)
2462{
2463 int i;
2464 int *active = NULL((void*)0);
2465 isl_bool involves = isl_bool_false;
2466
2467 if (!aff)
2468 return isl_bool_error;
2469 if (n == 0)
2470 return isl_bool_false;
2471 if (isl_aff_check_range(aff, type, first, n) < 0)
2472 return isl_bool_error;
2473
2474 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2475 if (!active)
2476 goto error;
2477
2478 first += isl_local_space_offset(aff->ls, type) - 1;
2479 for (i = 0; i < n; ++i)
2480 if (active[first + i]) {
2481 involves = isl_bool_true;
2482 break;
2483 }
2484
2485 free(active);
2486
2487 return involves;
2488error:
2489 free(active);
2490 return isl_bool_error;
2491}
2492
2493/* Does "aff" involve any local variables, i.e., integer divisions?
2494 */
2495isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2496{
2497 isl_size n;
2498
2499 n = isl_aff_dim(aff, isl_dim_div);
2500 if (n < 0)
2501 return isl_bool_error;
2502 return isl_aff_involves_dims(aff, isl_dim_div, 0, n);
2503}
2504
2505__isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2506 enum isl_dim_type type, unsigned first, unsigned n)
2507{
2508 isl_ctx *ctx;
2509
2510 if (!aff)
2511 return NULL((void*)0);
2512 if (type == isl_dim_out)
2513 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2515); return isl_aff_free(aff); } while (0)
2514 "cannot drop output/set dimension",do { isl_handle_error(aff->v->ctx, isl_error_invalid, "cannot drop output/set dimension"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2515); return isl_aff_free(aff); } while (0)
2515 return isl_aff_free(aff))do { isl_handle_error(aff->v->ctx, isl_error_invalid, "cannot drop output/set dimension"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2515); return isl_aff_free(aff); } while (0)
;
2516 if (type == isl_dim_in)
2517 type = isl_dim_set;
2518 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2519 return aff;
2520
2521 ctx = isl_aff_get_ctx(aff);
Value stored to 'ctx' is never read
2522 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2523 return isl_aff_free(aff);
2524
2525 aff = isl_aff_cow(aff);
2526 if (!aff)
2527 return NULL((void*)0);
2528
2529 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2530 if (!aff->ls)
2531 return isl_aff_free(aff);
2532
2533 first += 1 + isl_local_space_offset(aff->ls, type);
2534 aff->v = isl_vec_drop_els(aff->v, first, n);
2535 if (!aff->v)
2536 return isl_aff_free(aff);
2537
2538 return aff;
2539}
2540
2541/* Is the domain of "aff" a product?
2542 */
2543static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2544{
2545 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2546}
2547
2548#undef TYPEisl_multi_union_pw_aff
2549#define TYPEisl_multi_union_pw_aff isl_aff
2550#include <isl_domain_factor_templ.c>
2551
2552/* Project the domain of the affine expression onto its parameter space.
2553 * The affine expression may not involve any of the domain dimensions.
2554 */
2555__isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2556{
2557 isl_space *space;
2558 isl_size n;
2559
2560 n = isl_aff_dim(aff, isl_dim_in);
2561 if (n < 0)
2562 return isl_aff_free(aff);
2563 aff = isl_aff_drop_domain(aff, 0, n);
2564 space = isl_aff_get_domain_space(aff);
2565 space = isl_space_params(space);
2566 aff = isl_aff_reset_domain_space(aff, space);
2567 return aff;
2568}
2569
2570/* Convert an affine expression defined over a parameter domain
2571 * into one that is defined over a zero-dimensional set.
2572 */
2573__isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2574{
2575 isl_local_space *ls;
2576
2577 ls = isl_aff_take_domain_local_space(aff);
2578 ls = isl_local_space_set_from_params(ls);
2579 aff = isl_aff_restore_domain_local_space(aff, ls);
2580
2581 return aff;
2582}
2583
2584__isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2585 enum isl_dim_type type, unsigned first, unsigned n)
2586{
2587 isl_ctx *ctx;
2588
2589 if (!aff)
2590 return NULL((void*)0);
2591 if (type == isl_dim_out)
2592 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2594); return isl_aff_free(aff); } while (0)
2593 "cannot insert output/set dimensions",do { isl_handle_error(aff->v->ctx, isl_error_invalid, "cannot insert output/set dimensions"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2594); return isl_aff_free(aff); } while (0)
2594 return isl_aff_free(aff))do { isl_handle_error(aff->v->ctx, isl_error_invalid, "cannot insert output/set dimensions"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2594); return isl_aff_free(aff); } while (0)
;
2595 if (type == isl_dim_in)
2596 type = isl_dim_set;
2597 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2598 return aff;
2599
2600 ctx = isl_aff_get_ctx(aff);
2601 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2602 return isl_aff_free(aff);
2603
2604 aff = isl_aff_cow(aff);
2605 if (!aff)
2606 return NULL((void*)0);
2607
2608 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2609 if (!aff->ls)
2610 return isl_aff_free(aff);
2611
2612 first += 1 + isl_local_space_offset(aff->ls, type);
2613 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2614 if (!aff->v)
2615 return isl_aff_free(aff);
2616
2617 return aff;
2618}
2619
2620__isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2621 enum isl_dim_type type, unsigned n)
2622{
2623 isl_size pos;
2624
2625 pos = isl_aff_dim(aff, type);
2626 if (pos < 0)
2627 return isl_aff_free(aff);
2628
2629 return isl_aff_insert_dims(aff, type, pos, n);
2630}
2631
2632__isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2633 enum isl_dim_type type, unsigned n)
2634{
2635 isl_size pos;
2636
2637 pos = isl_pw_aff_dim(pwaff, type);
2638 if (pos < 0)
2639 return isl_pw_aff_free(pwaff);
2640
2641 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2642}
2643
2644/* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2645 * to dimensions of "dst_type" at "dst_pos".
2646 *
2647 * We only support moving input dimensions to parameters and vice versa.
2648 */
2649__isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2650 enum isl_dim_type dst_type, unsigned dst_pos,
2651 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2652{
2653 unsigned g_dst_pos;
2654 unsigned g_src_pos;
2655 isl_size src_off, dst_off;
2656
2657 if (!aff)
2658 return NULL((void*)0);
2659 if (n == 0 &&
2660 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2661 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2662 return aff;
2663
2664 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2665 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2667); return isl_aff_free(aff); } while (0)
2666 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2667); return isl_aff_free(aff); } while (0)
2667 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2667); return isl_aff_free(aff); } while (0)
;
2668 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2669 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2670); return isl_aff_free(aff); } while (0)
2670 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2670); return isl_aff_free(aff); } while (0)
;
2671 if (dst_type == isl_dim_in)
2672 dst_type = isl_dim_set;
2673 if (src_type == isl_dim_in)
2674 src_type = isl_dim_set;
2675
2676 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2677 return isl_aff_free(aff);
2678 if (dst_type == src_type)
2679 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2681); return isl_aff_free(aff); } while (0)
2680 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2681); return isl_aff_free(aff); } while (0)
2681 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2681); return isl_aff_free(aff); } while (0)
;
2682
2683 aff = isl_aff_cow(aff);
2684 src_off = isl_aff_domain_offset(aff, src_type);
2685 dst_off = isl_aff_domain_offset(aff, dst_type);
2686 if (src_off < 0 || dst_off < 0)
2687 return isl_aff_free(aff);
2688
2689 g_src_pos = 1 + src_off + src_pos;
2690 g_dst_pos = 1 + dst_off + dst_pos;
2691 if (dst_type > src_type)
2692 g_dst_pos -= n;
2693
2694 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2695 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2696 src_type, src_pos, n);
2697 if (!aff->v || !aff->ls)
2698 return isl_aff_free(aff);
2699
2700 aff = sort_divs(aff);
2701
2702 return aff;
2703}
2704
2705/* Return a zero isl_aff in the given space.
2706 *
2707 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2708 * interface over all piecewise types.
2709 */
2710static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2711{
2712 isl_local_space *ls;
2713
2714 ls = isl_local_space_from_space(isl_space_domain(space));
2715 return isl_aff_zero_on_domain(ls);
2716}
2717
2718#define isl_aff_involves_nanisl_aff_is_nan isl_aff_is_nan
2719
2720#undef PWisl_pw_multi_aff
2721#define PWisl_pw_multi_aff isl_pw_aff
2722#undef BASEunion_pw_aff
2723#define BASEunion_pw_aff aff
2724#undef EL_IS_ZEROis_empty
2725#define EL_IS_ZEROis_empty is_empty
2726#undef ZEROempty
2727#define ZEROempty empty
2728#undef IS_ZEROis_empty
2729#define IS_ZEROis_empty is_empty
2730#undef FIELDmaff
2731#define FIELDmaff aff
2732#undef DEFAULT_IS_ZERO0
2733#define DEFAULT_IS_ZERO0 0
2734
2735#include <isl_pw_templ.c>
2736#include <isl_pw_add_constant_val_templ.c>
2737#include <isl_pw_bind_domain_templ.c>
2738#include <isl_pw_eval.c>
2739#include <isl_pw_hash.c>
2740#include <isl_pw_insert_dims_templ.c>
2741#include <isl_pw_move_dims_templ.c>
2742#include <isl_pw_neg_templ.c>
2743#include <isl_pw_pullback_templ.c>
2744#include <isl_pw_sub_templ.c>
2745#include <isl_pw_union_opt.c>
2746
2747#undef BASEunion_pw_aff
2748#define BASEunion_pw_aff pw_aff
2749
2750#include <isl_union_single.c>
2751#include <isl_union_neg.c>
2752
2753/* Compute a piecewise quasi-affine expression with a domain that
2754 * is the union of those of pwaff1 and pwaff2 and such that on each
2755 * cell, the quasi-affine expression is the maximum of those of pwaff1
2756 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2757 * cell, then the associated expression is the defined one.
2758 */
2759__isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2760 __isl_take isl_pw_aff *pwaff2)
2761{
2762 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2763 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2764}
2765
2766/* Compute a piecewise quasi-affine expression with a domain that
2767 * is the union of those of pwaff1 and pwaff2 and such that on each
2768 * cell, the quasi-affine expression is the minimum of those of pwaff1
2769 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2770 * cell, then the associated expression is the defined one.
2771 */
2772__isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2773 __isl_take isl_pw_aff *pwaff2)
2774{
2775 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2776 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2777}
2778
2779__isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2780 __isl_take isl_pw_aff *pwaff2, int max)
2781{
2782 if (max)
2783 return isl_pw_aff_union_max(pwaff1, pwaff2);
2784 else
2785 return isl_pw_aff_union_min(pwaff1, pwaff2);
2786}
2787
2788/* Is the domain of "pa" a product?
2789 */
2790static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2791{
2792 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2793}
2794
2795#undef TYPEisl_multi_union_pw_aff
2796#define TYPEisl_multi_union_pw_aff isl_pw_aff
2797#include <isl_domain_factor_templ.c>
2798
2799/* Return a set containing those elements in the domain
2800 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2801 * does not satisfy "fn" (if complement is 1).
2802 *
2803 * The pieces with a NaN never belong to the result since
2804 * NaN does not satisfy any property.
2805 */
2806static __isl_give isl_setisl_map *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2807 __isl_give isl_basic_setisl_basic_map *(*fn)(__isl_take isl_aff *aff, int rational,
2808 void *user),
2809 int complement, void *user)
2810{
2811 int i;
2812 isl_setisl_map *set;
2813
2814 if (!pwaff)
2815 return NULL((void*)0);
2816
2817 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2818
2819 for (i = 0; i < pwaff->n; ++i) {
2820 isl_basic_setisl_basic_map *bset;
2821 isl_setisl_map *set_i, *locus;
2822 isl_bool rational;
2823
2824 if (isl_aff_is_nan(pwaff->p[i].aff))
2825 continue;
2826
2827 rational = isl_set_has_rational(pwaff->p[i].set);
2828 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2829 locus = isl_set_from_basic_set(bset);
2830 set_i = isl_set_copy(pwaff->p[i].set);
2831 if (complement)
2832 set_i = isl_set_subtract(set_i, locus);
2833 else
2834 set_i = isl_set_intersect(set_i, locus);
2835 set = isl_set_union_disjoint(set, set_i);
2836 }
2837
2838 isl_pw_aff_free(pwaff);
2839
2840 return set;
2841}
2842
2843/* Return a set containing those elements in the domain
2844 * of "pa" where it is positive.
2845 */
2846__isl_give isl_setisl_map *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2847{
2848 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL((void*)0));
2849}
2850
2851/* Return a set containing those elements in the domain
2852 * of pwaff where it is non-negative.
2853 */
2854__isl_give isl_setisl_map *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2855{
2856 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL((void*)0));
2857}
2858
2859/* Return a set containing those elements in the domain
2860 * of pwaff where it is zero.
2861 */
2862__isl_give isl_setisl_map *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2863{
2864 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL((void*)0));
2865}
2866
2867/* Return a set containing those elements in the domain
2868 * of pwaff where it is not zero.
2869 */
2870__isl_give isl_setisl_map *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2871{
2872 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL((void*)0));
2873}
2874
2875/* Bind the affine function "aff" to the parameter "id",
2876 * returning the elements in the domain where the affine expression
2877 * is equal to the parameter.
2878 */
2879__isl_give isl_basic_setisl_basic_map *isl_aff_bind_id(__isl_take isl_aff *aff,
2880 __isl_take isl_id *id)
2881{
2882 isl_space *space;
2883 isl_aff *aff_id;
2884
2885 space = isl_aff_get_domain_space(aff);
2886 space = isl_space_add_param_id(space, isl_id_copy(id));
2887
2888 aff = isl_aff_align_params(aff, isl_space_copy(space));
2889 aff_id = isl_aff_param_on_domain_space_id(space, id);
2890
2891 return isl_aff_eq_basic_set(aff, aff_id);
2892}
2893
2894/* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2895 * "rational" should not be set.
2896 */
2897static __isl_give isl_basic_setisl_basic_map *aff_bind_id(__isl_take isl_aff *aff,
2898 int rational, void *user)
2899{
2900 isl_id *id = user;
2901
2902 if (!aff)
2903 return NULL((void*)0);
2904 if (rational)
2905 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_unsupported
, "rational binding not supported", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2906); goto error; } while (0)
2906 "rational binding not supported", goto error)do { isl_handle_error(isl_aff_get_ctx(aff), isl_error_unsupported
, "rational binding not supported", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 2906); goto error; } while (0)
;
2907 return isl_aff_bind_id(aff, isl_id_copy(id));
2908error:
2909 isl_aff_free(aff);
2910 return NULL((void*)0);
2911}
2912
2913/* Bind the piecewise affine function "pa" to the parameter "id",
2914 * returning the elements in the domain where the expression
2915 * is equal to the parameter.
2916 */
2917__isl_give isl_setisl_map *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2918 __isl_take isl_id *id)
2919{
2920 isl_setisl_map *bound;
2921
2922 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2923 isl_id_free(id);
2924
2925 return bound;
2926}
2927
2928/* Return a set containing those elements in the shared domain
2929 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2930 *
2931 * We compute the difference on the shared domain and then construct
2932 * the set of values where this difference is non-negative.
2933 * If strict is set, we first subtract 1 from the difference.
2934 * If equal is set, we only return the elements where pwaff1 and pwaff2
2935 * are equal.
2936 */
2937static __isl_give isl_setisl_map *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2938 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2939{
2940 isl_setisl_map *set1, *set2;
2941
2942 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2943 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2944 set1 = isl_set_intersect(set1, set2);
2945 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2946 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2947 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2948
2949 if (strict) {
2950 isl_space *space = isl_set_get_space(set1);
2951 isl_aff *aff;
2952 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2953 aff = isl_aff_add_constant_si(aff, -1);
2954 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2955 } else
2956 isl_set_free(set1);
2957
2958 if (equal)
2959 return isl_pw_aff_zero_set(pwaff1);
2960 return isl_pw_aff_nonneg_set(pwaff1);
2961}
2962
2963/* Return a set containing those elements in the shared domain
2964 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2965 */
2966__isl_give isl_setisl_map *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2967 __isl_take isl_pw_aff *pwaff2)
2968{
2969 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2970 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2971}
2972
2973/* Return a set containing those elements in the shared domain
2974 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2975 */
2976__isl_give isl_setisl_map *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2978{
2979 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2980 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2981}
2982
2983/* Return a set containing those elements in the shared domain
2984 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2985 */
2986__isl_give isl_setisl_map *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2988{
2989 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2990 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2991}
2992
2993__isl_give isl_setisl_map *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2)
2995{
2996 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2997}
2998
2999__isl_give isl_setisl_map *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3000 __isl_take isl_pw_aff *pwaff2)
3001{
3002 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3003}
3004
3005/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function values are ordered in the same way as "order",
3007 * which returns a set in the shared domain of its two arguments.
3008 *
3009 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3010 * We first pull back the two functions such that they are defined on
3011 * the domain [A -> B]. Then we apply "order", resulting in a set
3012 * in the space [A -> B]. Finally, we unwrap this set to obtain
3013 * a map in the space A -> B.
3014 */
3015static __isl_give isl_map *isl_pw_aff_order_map(
3016 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3017 __isl_give isl_setisl_map *(*order)(__isl_take isl_pw_aff *pa1,
3018 __isl_take isl_pw_aff *pa2))
3019{
3020 isl_space *space1, *space2;
3021 isl_multi_aff *ma;
3022 isl_setisl_map *set;
3023
3024 isl_pw_aff_align_params_bin(&pa1, &pa2);
3025 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3026 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3027 space1 = isl_space_map_from_domain_and_range(space1, space2);
3028 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3029 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3030 ma = isl_multi_aff_range_map(space1);
3031 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3032 set = order(pa1, pa2);
3033
3034 return isl_set_unwrap(set);
3035}
3036
3037/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3039 */
3040__isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3041 __isl_take isl_pw_aff *pa2)
3042{
3043 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3044}
3045
3046/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than or equal to
3048 * the function value of "pa2".
3049 */
3050__isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2)
3052{
3053 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3054}
3055
3056/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3058 */
3059__isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3060 __isl_take isl_pw_aff *pa2)
3061{
3062 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3063}
3064
3065/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than or equal to
3067 * the function value of "pa2".
3068 */
3069__isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3070 __isl_take isl_pw_aff *pa2)
3071{
3072 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3073}
3074
3075/* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3077 * of "pa2".
3078 */
3079__isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3080 __isl_take isl_pw_aff *pa2)
3081{
3082 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3083}
3084
3085/* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3088 */
3089static __isl_give isl_setisl_map *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3090 __isl_take isl_pw_aff_list *list2,
3091 __isl_give isl_setisl_map *(*fn)(__isl_take isl_pw_aff *pwaff1,
3092 __isl_take isl_pw_aff *pwaff2))
3093{
3094 int i, j;
3095 isl_ctx *ctx;
3096 isl_setisl_map *set;
3097
3098 if (!list1 || !list2)
3099 goto error;
3100
3101 ctx = isl_pw_aff_list_get_ctx(list1);
3102 if (list1->n < 1 || list2->n < 1)
3103 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "list should contain at least one element"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3104); goto error; } while (0)
3104 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3104); goto error; } while (0)
;
3105
3106 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3107 for (i = 0; i < list1->n; ++i)
3108 for (j = 0; j < list2->n; ++j) {
3109 isl_setisl_map *set_ij;
3110
3111 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3112 isl_pw_aff_copy(list2->p[j]));
3113 set = isl_set_intersect(set, set_ij);
3114 }
3115
3116 isl_pw_aff_list_free(list1);
3117 isl_pw_aff_list_free(list2);
3118 return set;
3119error:
3120 isl_pw_aff_list_free(list1);
3121 isl_pw_aff_list_free(list2);
3122 return NULL((void*)0);
3123}
3124
3125/* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is equal to each element in list2.
3128 */
3129__isl_give isl_setisl_map *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3131{
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3133}
3134
3135__isl_give isl_setisl_map *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2)
3137{
3138 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3139}
3140
3141/* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3144 */
3145__isl_give isl_setisl_map *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3147{
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3149}
3150
3151__isl_give isl_setisl_map *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3153{
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3155}
3156
3157__isl_give isl_setisl_map *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3159{
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3161}
3162
3163__isl_give isl_setisl_map *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3164 __isl_take isl_pw_aff_list *list2)
3165{
3166 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3167}
3168
3169
3170/* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3172 */
3173__isl_give isl_setisl_map *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3174 __isl_take isl_pw_aff *pwaff2)
3175{
3176 isl_setisl_map *set_lt, *set_gt;
3177
3178 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3179 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3180 isl_pw_aff_copy(pwaff2));
3181 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3182 return isl_set_union_disjoint(set_lt, set_gt);
3183}
3184
3185__isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3186 isl_int v)
3187{
3188 int i;
3189
3190 if (isl_int_is_one(v)(isl_sioimath_cmp_si(*(v), 1) == 0))
3191 return pwaff;
3192 if (!isl_int_is_pos(v)(isl_sioimath_sgn(*(v)) > 0))
3193 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3195); return isl_pw_aff_free(pwaff); } while (0)
3194 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3195); return isl_pw_aff_free(pwaff); } while (0)
3195 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3195); return isl_pw_aff_free(pwaff); } while (0)
;
3196 pwaff = isl_pw_aff_cow(pwaff);
3197 if (!pwaff)
3198 return NULL((void*)0);
3199 if (pwaff->n == 0)
3200 return pwaff;
3201
3202 for (i = 0; i < pwaff->n; ++i) {
3203 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3204 if (!pwaff->p[i].aff)
3205 return isl_pw_aff_free(pwaff);
3206 }
3207
3208 return pwaff;
3209}
3210
3211__isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3212{
3213 int i;
3214
3215 pwaff = isl_pw_aff_cow(pwaff);
3216 if (!pwaff)
3217 return NULL((void*)0);
3218 if (pwaff->n == 0)
3219 return pwaff;
3220
3221 for (i = 0; i < pwaff->n; ++i) {
3222 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3223 if (!pwaff->p[i].aff)
3224 return isl_pw_aff_free(pwaff);
3225 }
3226
3227 return pwaff;
3228}
3229
3230__isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3231{
3232 int i;
3233
3234 pwaff = isl_pw_aff_cow(pwaff);
3235 if (!pwaff)
3236 return NULL((void*)0);
3237 if (pwaff->n == 0)
3238 return pwaff;
3239
3240 for (i = 0; i < pwaff->n; ++i) {
3241 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3242 if (!pwaff->p[i].aff)
3243 return isl_pw_aff_free(pwaff);
3244 }
3245
3246 return pwaff;
3247}
3248
3249/* Assuming that "cond1" and "cond2" are disjoint,
3250 * return an affine expression that is equal to pwaff1 on cond1
3251 * and to pwaff2 on cond2.
3252 */
3253static __isl_give isl_pw_aff *isl_pw_aff_select(
3254 __isl_take isl_setisl_map *cond1, __isl_take isl_pw_aff *pwaff1,
3255 __isl_take isl_setisl_map *cond2, __isl_take isl_pw_aff *pwaff2)
3256{
3257 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3258 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3259
3260 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3261}
3262
3263/* Return an affine expression that is equal to pwaff_true for elements
3264 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3265 * is zero.
3266 * That is, return cond ? pwaff_true : pwaff_false;
3267 *
3268 * If "cond" involves and NaN, then we conservatively return a NaN
3269 * on its entire domain. In principle, we could consider the pieces
3270 * where it is NaN separately from those where it is not.
3271 *
3272 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3273 * then only use the domain of "cond" to restrict the domain.
3274 */
3275__isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3276 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3277{
3278 isl_setisl_map *cond_true, *cond_false;
3279 isl_bool equal;
3280
3281 if (!cond)
3282 goto error;
3283 if (isl_pw_aff_involves_nan(cond)) {
3284 isl_space *space = isl_pw_aff_get_domain_space(cond);
3285 isl_local_space *ls = isl_local_space_from_space(space);
3286 isl_pw_aff_free(cond);
3287 isl_pw_aff_free(pwaff_true);
3288 isl_pw_aff_free(pwaff_false);
3289 return isl_pw_aff_nan_on_domain(ls);
3290 }
3291
3292 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3293 isl_pw_aff_get_space(pwaff_false));
3294 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3295 isl_pw_aff_get_space(pwaff_true));
3296 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3297 if (equal < 0)
3298 goto error;
3299 if (equal) {
3300 isl_setisl_map *dom;
3301
3302 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3303 isl_pw_aff_free(pwaff_false);
3304 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3305 }
3306
3307 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3308 cond_false = isl_pw_aff_zero_set(cond);
3309 return isl_pw_aff_select(cond_true, pwaff_true,
3310 cond_false, pwaff_false);
3311error:
3312 isl_pw_aff_free(cond);
3313 isl_pw_aff_free(pwaff_true);
3314 isl_pw_aff_free(pwaff_false);
3315 return NULL((void*)0);
3316}
3317
3318isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3319{
3320 int pos;
3321
3322 if (!aff)
3323 return isl_bool_error;
3324
3325 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3326 return isl_bool_ok(pos == -1);
3327}
3328
3329/* Check whether pwaff is a piecewise constant.
3330 */
3331isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3332{
3333 int i;
3334
3335 if (!pwaff)
3336 return isl_bool_error;
3337
3338 for (i = 0; i < pwaff->n; ++i) {
3339 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3340 if (is_cst < 0 || !is_cst)
3341 return is_cst;
3342 }
3343
3344 return isl_bool_true;
3345}
3346
3347/* Return the product of "aff1" and "aff2".
3348 *
3349 * If either of the two is NaN, then the result is NaN.
3350 *
3351 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3352 */
3353__isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3354 __isl_take isl_aff *aff2)
3355{
3356 if (!aff1 || !aff2)
3357 goto error;
3358
3359 if (isl_aff_is_nan(aff1)) {
3360 isl_aff_free(aff2);
3361 return aff1;
3362 }
3363 if (isl_aff_is_nan(aff2)) {
3364 isl_aff_free(aff1);
3365 return aff2;
3366 }
3367
3368 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3369 return isl_aff_mul(aff2, aff1);
3370
3371 if (!isl_aff_is_cst(aff2))
3372 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,do { isl_handle_error(isl_aff_get_ctx(aff1), isl_error_invalid
, "at least one affine expression should be constant", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3374); goto error; } while (0)
3373 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3374); goto error; } while (0)
3374 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3374); goto error; } while (0)
;
3375
3376 aff1 = isl_aff_cow(aff1);
3377 if (!aff1 || !aff2)
3378 goto error;
3379
3380 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3381 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3382
3383 isl_aff_free(aff2);
3384 return aff1;
3385error:
3386 isl_aff_free(aff1);
3387 isl_aff_free(aff2);
3388 return NULL((void*)0);
3389}
3390
3391/* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3392 *
3393 * If either of the two is NaN, then the result is NaN.
3394 */
3395__isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3396 __isl_take isl_aff *aff2)
3397{
3398 int is_cst;
3399 int neg;
3400
3401 if (!aff1 || !aff2)
3402 goto error;
3403
3404 if (isl_aff_is_nan(aff1)) {
3405 isl_aff_free(aff2);
3406 return aff1;
3407 }
3408 if (isl_aff_is_nan(aff2)) {
3409 isl_aff_free(aff1);
3410 return aff2;
3411 }
3412
3413 is_cst = isl_aff_is_cst(aff2);
3414 if (is_cst < 0)
3415 goto error;
3416 if (!is_cst)
3417 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3418); goto error; } while (0)
3418 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3418); goto error; } while (0)
;
3419
3420 if (!aff2)
3421 goto error;
3422
3423 neg = isl_int_is_neg(aff2->v->el[1])(isl_sioimath_sgn(*(aff2->v->el[1])) < 0);
3424 if (neg) {
3425 isl_int_neg(aff2->v->el[0], aff2->v->el[0])isl_sioimath_neg((aff2->v->el[0]), *(aff2->v->el[
0]))
;
3426 isl_int_neg(aff2->v->el[1], aff2->v->el[1])isl_sioimath_neg((aff2->v->el[1]), *(aff2->v->el[
1]))
;
3427 }
3428
3429 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3430 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3431
3432 if (neg) {
3433 isl_int_neg(aff2->v->el[0], aff2->v->el[0])isl_sioimath_neg((aff2->v->el[0]), *(aff2->v->el[
0]))
;
3434 isl_int_neg(aff2->v->el[1], aff2->v->el[1])isl_sioimath_neg((aff2->v->el[1]), *(aff2->v->el[
1]))
;
3435 }
3436
3437 isl_aff_free(aff2);
3438 return aff1;
3439error:
3440 isl_aff_free(aff1);
3441 isl_aff_free(aff2);
3442 return NULL((void*)0);
3443}
3444
3445__isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3446 __isl_take isl_pw_aff *pwaff2)
3447{
3448 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3449 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3450}
3451
3452__isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3454{
3455 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3456}
3457
3458__isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3459 __isl_take isl_pw_aff *pwaff2)
3460{
3461 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3462 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3463}
3464
3465/* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3466 */
3467__isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3468 __isl_take isl_pw_aff *pa2)
3469{
3470 int is_cst;
3471
3472 is_cst = isl_pw_aff_is_cst(pa2);
3473 if (is_cst < 0)
3474 goto error;
3475 if (!is_cst)
3476 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3478); goto error; } while (0)
3477 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3478); goto error; } while (0)
3478 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3478); goto error; } while (0)
;
3479 isl_pw_aff_align_params_bin(&pa1, &pa2);
3480 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3481error:
3482 isl_pw_aff_free(pa1);
3483 isl_pw_aff_free(pa2);
3484 return NULL((void*)0);
3485}
3486
3487/* Compute the quotient of the integer division of "pa1" by "pa2"
3488 * with rounding towards zero.
3489 * "pa2" is assumed to be a piecewise constant.
3490 *
3491 * In particular, return
3492 *
3493 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3494 *
3495 */
3496__isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3497 __isl_take isl_pw_aff *pa2)
3498{
3499 int is_cst;
3500 isl_setisl_map *cond;
3501 isl_pw_aff *f, *c;
3502
3503 is_cst = isl_pw_aff_is_cst(pa2);
3504 if (is_cst < 0)
3505 goto error;
3506 if (!is_cst)
3507 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3509); goto error; } while (0)
3508 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3509); goto error; } while (0)
3509 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3509); goto error; } while (0)
;
3510
3511 pa1 = isl_pw_aff_div(pa1, pa2);
3512
3513 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3514 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3515 c = isl_pw_aff_ceil(pa1);
3516 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3517error:
3518 isl_pw_aff_free(pa1);
3519 isl_pw_aff_free(pa2);
3520 return NULL((void*)0);
3521}
3522
3523/* Compute the remainder of the integer division of "pa1" by "pa2"
3524 * with rounding towards zero.
3525 * "pa2" is assumed to be a piecewise constant.
3526 *
3527 * In particular, return
3528 *
3529 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3530 *
3531 */
3532__isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3533 __isl_take isl_pw_aff *pa2)
3534{
3535 int is_cst;
3536 isl_pw_aff *res;
3537
3538 is_cst = isl_pw_aff_is_cst(pa2);
3539 if (is_cst < 0)
3540 goto error;
3541 if (!is_cst)
3542 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3544); goto error; } while (0)
3543 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3544); goto error; } while (0)
3544 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3544); goto error; } while (0)
;
3545 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3546 res = isl_pw_aff_mul(pa2, res);
3547 res = isl_pw_aff_sub(pa1, res);
3548 return res;
3549error:
3550 isl_pw_aff_free(pa1);
3551 isl_pw_aff_free(pa2);
3552 return NULL((void*)0);
3553}
3554
3555/* Does either of "pa1" or "pa2" involve any NaN2?
3556 */
3557static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3558 __isl_keep isl_pw_aff *pa2)
3559{
3560 isl_bool has_nan;
3561
3562 has_nan = isl_pw_aff_involves_nan(pa1);
3563 if (has_nan < 0 || has_nan)
3564 return has_nan;
3565 return isl_pw_aff_involves_nan(pa2);
3566}
3567
3568/* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3569 * by a NaN on their shared domain.
3570 *
3571 * In principle, the result could be refined to only being NaN
3572 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3573 */
3574static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3575 __isl_take isl_pw_aff *pa2)
3576{
3577 isl_local_space *ls;
3578 isl_setisl_map *dom;
3579 isl_pw_aff *pa;
3580
3581 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3582 ls = isl_local_space_from_space(isl_set_get_space(dom));
3583 pa = isl_pw_aff_nan_on_domain(ls);
3584 pa = isl_pw_aff_intersect_domain(pa, dom);
3585
3586 return pa;
3587}
3588
3589static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3590 __isl_take isl_pw_aff *pwaff2)
3591{
3592 isl_setisl_map *le;
3593 isl_setisl_map *dom;
3594
3595 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3596 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3597 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3598 isl_pw_aff_copy(pwaff2));
3599 dom = isl_set_subtract(dom, isl_set_copy(le));
3600 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3601}
3602
3603static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3604 __isl_take isl_pw_aff *pwaff2)
3605{
3606 isl_setisl_map *ge;
3607 isl_setisl_map *dom;
3608
3609 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3610 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3611 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3612 isl_pw_aff_copy(pwaff2));
3613 dom = isl_set_subtract(dom, isl_set_copy(ge));
3614 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3615}
3616
3617/* Return an expression for the minimum (if "max" is not set) or
3618 * the maximum (if "max" is set) of "pa1" and "pa2".
3619 * If either expression involves any NaN, then return a NaN
3620 * on the shared domain as result.
3621 */
3622static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3623 __isl_take isl_pw_aff *pa2, int max)
3624{
3625 isl_bool has_nan;
3626
3627 has_nan = either_involves_nan(pa1, pa2);
3628 if (has_nan < 0)
3629 pa1 = isl_pw_aff_free(pa1);
3630 else if (has_nan)
3631 return replace_by_nan(pa1, pa2);
3632
3633 isl_pw_aff_align_params_bin(&pa1, &pa2);
3634 if (max)
3635 return pw_aff_max(pa1, pa2);
3636 else
3637 return pw_aff_min(pa1, pa2);
3638}
3639
3640/* Return an expression for the minimum of "pwaff1" and "pwaff2".
3641 */
3642__isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3643 __isl_take isl_pw_aff *pwaff2)
3644{
3645 return pw_aff_min_max(pwaff1, pwaff2, 0);
3646}
3647
3648/* Return an expression for the maximum of "pwaff1" and "pwaff2".
3649 */
3650__isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3651 __isl_take isl_pw_aff *pwaff2)
3652{
3653 return pw_aff_min_max(pwaff1, pwaff2, 1);
3654}
3655
3656static __isl_give isl_pw_aff *pw_aff_list_reduce(
3657 __isl_take isl_pw_aff_list *list,
3658 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3659 __isl_take isl_pw_aff *pwaff2))
3660{
3661 int i;
3662 isl_ctx *ctx;
3663 isl_pw_aff *res;
3664
3665 if (!list)
3666 return NULL((void*)0);
3667
3668 ctx = isl_pw_aff_list_get_ctx(list);
3669 if (list->n < 1)
3670 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3671); goto error; } while (0)
3671 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3671); goto error; } while (0)
;
3672
3673 res = isl_pw_aff_copy(list->p[0]);
3674 for (i = 1; i < list->n; ++i)
3675 res = fn(res, isl_pw_aff_copy(list->p[i]));
3676
3677 isl_pw_aff_list_free(list);
3678 return res;
3679error:
3680 isl_pw_aff_list_free(list);
3681 return NULL((void*)0);
3682}
3683
3684/* Return an isl_pw_aff that maps each element in the intersection of the
3685 * domains of the elements of list to the minimal corresponding affine
3686 * expression.
3687 */
3688__isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3689{
3690 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3691}
3692
3693/* Return an isl_pw_aff that maps each element in the intersection of the
3694 * domains of the elements of list to the maximal corresponding affine
3695 * expression.
3696 */
3697__isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3698{
3699 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3700}
3701
3702/* Mark the domains of "pwaff" as rational.
3703 */
3704__isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3705{
3706 int i;
3707
3708 pwaff = isl_pw_aff_cow(pwaff);
3709 if (!pwaff)
3710 return NULL((void*)0);
3711 if (pwaff->n == 0)
3712 return pwaff;
3713
3714 for (i = 0; i < pwaff->n; ++i) {
3715 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3716 if (!pwaff->p[i].set)
3717 return isl_pw_aff_free(pwaff);
3718 }
3719
3720 return pwaff;
3721}
3722
3723/* Mark the domains of the elements of "list" as rational.
3724 */
3725__isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3726 __isl_take isl_pw_aff_list *list)
3727{
3728 int i, n;
3729
3730 if (!list)
3731 return NULL((void*)0);
3732 if (list->n == 0)
3733 return list;
3734
3735 n = list->n;
3736 for (i = 0; i < n; ++i) {
3737 isl_pw_aff *pa;
3738
3739 pa = isl_pw_aff_list_get_pw_aff(list, i);
3740 pa = isl_pw_aff_set_rational(pa);
3741 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3742 }
3743
3744 return list;
3745}
3746
3747/* Do the parameters of "aff" match those of "space"?
3748 */
3749isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3750 __isl_keep isl_space *space)
3751{
3752 isl_space *aff_space;
3753 isl_bool match;
3754
3755 if (!aff || !space)
3756 return isl_bool_error;
3757
3758 aff_space = isl_aff_get_domain_space(aff);
3759
3760 match = isl_space_has_equal_params(space, aff_space);
3761
3762 isl_space_free(aff_space);
3763 return match;
3764}
3765
3766/* Check that the domain space of "aff" matches "space".
3767 */
3768isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3769 __isl_keep isl_space *space)
3770{
3771 isl_space *aff_space;
3772 isl_bool match;
3773
3774 if (!aff || !space)
3775 return isl_stat_error;
3776
3777 aff_space = isl_aff_get_domain_space(aff);
3778
3779 match = isl_space_has_equal_params(space, aff_space);
3780 if (match < 0)
3781 goto error;
3782 if (!match)
3783 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3784); goto error; } while (0)
3784 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3784); goto error; } while (0)
;
3785 match = isl_space_tuple_is_equal(space, isl_dim_in,
3786 aff_space, isl_dim_set);
3787 if (match < 0)
3788 goto error;
3789 if (!match)
3790 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3791); goto error; } while (0)
3791 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3791); goto error; } while (0)
;
3792 isl_space_free(aff_space);
3793 return isl_stat_ok;
3794error:
3795 isl_space_free(aff_space);
3796 return isl_stat_error;
3797}
3798
3799/* Return the shared (universe) domain of the elements of "ma".
3800 *
3801 * Since an isl_multi_aff (and an isl_aff) is always total,
3802 * the domain is always the universe set in its domain space.
3803 * This is a helper function for use in the generic isl_multi_*_bind.
3804 */
3805static __isl_give isl_basic_setisl_basic_map *isl_multi_aff_domain(
3806 __isl_take isl_multi_aff *ma)
3807{
3808 isl_space *space;
3809
3810 space = isl_multi_aff_get_space(ma);
3811 isl_multi_aff_free(ma);
3812
3813 return isl_basic_set_universe(isl_space_domain(space));
3814}
3815
3816#undef BASEunion_pw_aff
3817#define BASEunion_pw_aff aff
3818
3819#include <isl_multi_no_explicit_domain.c>
3820#include <isl_multi_templ.c>
3821#include <isl_multi_add_constant_templ.c>
3822#include <isl_multi_apply_set.c>
3823#include <isl_multi_arith_templ.c>
3824#include <isl_multi_bind_domain_templ.c>
3825#include <isl_multi_cmp.c>
3826#include <isl_multi_dim_id_templ.c>
3827#include <isl_multi_dims.c>
3828#include <isl_multi_floor.c>
3829#include <isl_multi_from_base_templ.c>
3830#include <isl_multi_identity_templ.c>
3831#include <isl_multi_locals_templ.c>
3832#include <isl_multi_move_dims_templ.c>
3833#include <isl_multi_nan_templ.c>
3834#include <isl_multi_product_templ.c>
3835#include <isl_multi_splice_templ.c>
3836#include <isl_multi_tuple_id_templ.c>
3837#include <isl_multi_zero_templ.c>
3838
3839#undef DOMBASEunion_set
3840#define DOMBASEunion_set set
3841#include <isl_multi_gist.c>
3842
3843#undef DOMBASEunion_set
3844#define DOMBASEunion_set basic_set
3845#include <isl_multi_bind_templ.c>
3846
3847/* Construct an isl_multi_aff living in "space" that corresponds
3848 * to the affine transformation matrix "mat".
3849 */
3850__isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3851 __isl_take isl_space *space, __isl_take isl_mat *mat)
3852{
3853 isl_ctx *ctx;
3854 isl_local_space *ls = NULL((void*)0);
3855 isl_multi_aff *ma = NULL((void*)0);
3856 isl_size n_row, n_col, n_out, total;
3857 int i;
3858
3859 if (!space || !mat)
3860 goto error;
3861
3862 ctx = isl_mat_get_ctx(mat);
3863
3864 n_row = isl_mat_rows(mat);
3865 n_col = isl_mat_cols(mat);
3866 n_out = isl_space_dim(space, isl_dim_out);
3867 total = isl_space_dim(space, isl_dim_all);
3868 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3869 goto error;
3870 if (n_row < 1)
3871 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "insufficient number of rows"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3872); goto error; } while (0)
3872 "insufficient number of rows", goto error)do { isl_handle_error(ctx, isl_error_invalid, "insufficient number of rows"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3872); goto error; } while (0)
;
3873 if (n_col < 1)
3874 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "insufficient number of columns"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3875); goto error; } while (0)
3875 "insufficient number of columns", goto error)do { isl_handle_error(ctx, isl_error_invalid, "insufficient number of columns"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3875); goto error; } while (0)
;
3876 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3877 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "dimension mismatch"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3878); goto error; } while (0)
3878 "dimension mismatch", goto error)do { isl_handle_error(ctx, isl_error_invalid, "dimension mismatch"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3878); goto error; } while (0)
;
3879
3880 ma = isl_multi_aff_zero(isl_space_copy(space));
3881 ls = isl_local_space_from_space(isl_space_domain(space));
3882
3883 for (i = 0; i < n_row - 1; ++i) {
3884 isl_vec *v;
3885 isl_aff *aff;
3886
3887 v = isl_vec_alloc(ctx, 1 + n_col);
3888 if (!v)
3889 goto error;
3890 isl_int_set(v->el[0], mat->row[0][0])isl_sioimath_set((v->el[0]), *(mat->row[0][0]));
3891 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3892 v = isl_vec_normalize(v);
3893 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3894 ma = isl_multi_aff_set_aff(ma, i, aff);
3895 }
3896
3897 isl_local_space_free(ls);
3898 isl_mat_free(mat);
3899 return ma;
3900error:
3901 isl_local_space_free(ls);
3902 isl_mat_free(mat);
3903 isl_multi_aff_free(ma);
3904 return NULL((void*)0);
3905}
3906
3907/* Return the constant terms of the affine expressions of "ma".
3908 */
3909__isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3910 __isl_keep isl_multi_aff *ma)
3911{
3912 int i;
3913 isl_size n;
3914 isl_space *space;
3915 isl_multi_val *mv;
3916
3917 n = isl_multi_aff_size(ma);
3918 if (n < 0)
3919 return NULL((void*)0);
3920 space = isl_space_range(isl_multi_aff_get_space(ma));
3921 space = isl_space_drop_all_params(space);
3922 mv = isl_multi_val_zero(space);
3923
3924 for (i = 0; i < n; ++i) {
3925 isl_aff *aff;
3926 isl_val *val;
3927
3928 aff = isl_multi_aff_get_at(ma, i);
3929 val = isl_aff_get_constant_val(aff);
3930 isl_aff_free(aff);
3931 mv = isl_multi_val_set_at(mv, i, val);
3932 }
3933
3934 return mv;
3935}
3936
3937/* Remove any internal structure of the domain of "ma".
3938 * If there is any such internal structure in the input,
3939 * then the name of the corresponding space is also removed.
3940 */
3941__isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3942 __isl_take isl_multi_aff *ma)
3943{
3944 isl_space *space;
3945
3946 if (!ma)
3947 return NULL((void*)0);
3948
3949 if (!ma->space->nested[0])
3950 return ma;
3951
3952 space = isl_multi_aff_get_space(ma);
3953 space = isl_space_flatten_domain(space);
3954 ma = isl_multi_aff_reset_space(ma, space);
3955
3956 return ma;
3957}
3958
3959/* Given a map space, return an isl_multi_aff that maps a wrapped copy
3960 * of the space to its domain.
3961 */
3962__isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3963{
3964 int i;
3965 isl_size n_in;
3966 isl_local_space *ls;
3967 isl_multi_aff *ma;
3968
3969 if (!space)
3970 return NULL((void*)0);
3971 if (!isl_space_is_map(space))
3972 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3973); goto error; } while (0)
3973 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 3973); goto error; } while (0)
;
3974
3975 n_in = isl_space_dim(space, isl_dim_in);
3976 if (n_in < 0)
3977 goto error;
3978 space = isl_space_domain_map(space);
3979
3980 ma = isl_multi_aff_alloc(isl_space_copy(space));
3981 if (n_in == 0) {
3982 isl_space_free(space);
3983 return ma;
3984 }
3985
3986 space = isl_space_domain(space);
3987 ls = isl_local_space_from_space(space);
3988 for (i = 0; i < n_in; ++i) {
3989 isl_aff *aff;
3990
3991 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3992 isl_dim_set, i);
3993 ma = isl_multi_aff_set_aff(ma, i, aff);
3994 }
3995 isl_local_space_free(ls);
3996 return ma;
3997error:
3998 isl_space_free(space);
3999 return NULL((void*)0);
4000}
4001
4002/* Given a map space, return an isl_multi_aff that maps a wrapped copy
4003 * of the space to its range.
4004 */
4005__isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4006{
4007 int i;
4008 isl_size n_in, n_out;
4009 isl_local_space *ls;
4010 isl_multi_aff *ma;
4011
4012 if (!space)
4013 return NULL((void*)0);
4014 if (!isl_space_is_map(space))
4015 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4016); goto error; } while (0)
4016 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4016); goto error; } while (0)
;
4017
4018 n_in = isl_space_dim(space, isl_dim_in);
4019 n_out = isl_space_dim(space, isl_dim_out);
4020 if (n_in < 0 || n_out < 0)
4021 goto error;
4022 space = isl_space_range_map(space);
4023
4024 ma = isl_multi_aff_alloc(isl_space_copy(space));
4025 if (n_out == 0) {
4026 isl_space_free(space);
4027 return ma;
4028 }
4029
4030 space = isl_space_domain(space);
4031 ls = isl_local_space_from_space(space);
4032 for (i = 0; i < n_out; ++i) {
4033 isl_aff *aff;
4034
4035 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4036 isl_dim_set, n_in + i);
4037 ma = isl_multi_aff_set_aff(ma, i, aff);
4038 }
4039 isl_local_space_free(ls);
4040 return ma;
4041error:
4042 isl_space_free(space);
4043 return NULL((void*)0);
4044}
4045
4046/* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4047 * of the space to its range.
4048 */
4049__isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4050 __isl_take isl_space *space)
4051{
4052 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4053}
4054
4055/* Given the space of a set and a range of set dimensions,
4056 * construct an isl_multi_aff that projects out those dimensions.
4057 */
4058__isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4059 __isl_take isl_space *space, enum isl_dim_type type,
4060 unsigned first, unsigned n)
4061{
4062 int i;
4063 isl_size dim;
4064 isl_local_space *ls;
4065 isl_multi_aff *ma;
4066
4067 if (!space)
4068 return NULL((void*)0);
4069 if (!isl_space_is_set(space))
4070 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4071); goto error; } while (0)
4071 "expecting set space", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_unsupported
, "expecting set space", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4071); goto error; } while (0)
;
4072 if (type != isl_dim_set)
4073 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4074); goto error; } while (0)
4074 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4074); goto error; } while (0)
;
4075 if (isl_space_check_range(space, type, first, n) < 0)
4076 goto error;
4077
4078 dim = isl_space_dim(space, isl_dim_set);
4079 if (dim < 0)
4080 goto error;
4081
4082 space = isl_space_from_domain(space);
4083 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4084
4085 if (dim == n)
4086 return isl_multi_aff_alloc(space);
4087
4088 ma = isl_multi_aff_alloc(isl_space_copy(space));
4089 space = isl_space_domain(space);
4090 ls = isl_local_space_from_space(space);
4091
4092 for (i = 0; i < first; ++i) {
4093 isl_aff *aff;
4094
4095 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4096 isl_dim_set, i);
4097 ma = isl_multi_aff_set_aff(ma, i, aff);
4098 }
4099
4100 for (i = 0; i < dim - (first + n); ++i) {
4101 isl_aff *aff;
4102
4103 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4104 isl_dim_set, first + n + i);
4105 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4106 }
4107
4108 isl_local_space_free(ls);
4109 return ma;
4110error:
4111 isl_space_free(space);
4112 return NULL((void*)0);
4113}
4114
4115/* Given the space of a set and a range of set dimensions,
4116 * construct an isl_pw_multi_aff that projects out those dimensions.
4117 */
4118__isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4119 __isl_take isl_space *space, enum isl_dim_type type,
4120 unsigned first, unsigned n)
4121{
4122 isl_multi_aff *ma;
4123
4124 ma = isl_multi_aff_project_out_map(space, type, first, n);
4125 return isl_pw_multi_aff_from_multi_aff(ma);
4126}
4127
4128/* Create a piecewise multi-affine expression in the given space that maps each
4129 * input dimension to the corresponding output dimension.
4130 */
4131__isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4132 __isl_take isl_space *space)
4133{
4134 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4135}
4136
4137/* Exploit the equalities in "eq" to simplify the affine expressions.
4138 */
4139static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4140 __isl_take isl_multi_aff *maff, __isl_take isl_basic_setisl_basic_map *eq)
4141{
4142 int i;
4143
4144 maff = isl_multi_aff_cow(maff);
4145 if (!maff || !eq)
4146 goto error;
4147
4148 for (i = 0; i < maff->n; ++i) {
4149 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4150 isl_basic_set_copy(eq));
4151 if (!maff->u.p[i])
4152 goto error;
4153 }
4154
4155 isl_basic_set_free(eq);
4156 return maff;
4157error:
4158 isl_basic_set_free(eq);
4159 isl_multi_aff_free(maff);
4160 return NULL((void*)0);
4161}
4162
4163__isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4164 isl_int f)
4165{
4166 int i;
4167
4168 maff = isl_multi_aff_cow(maff);
4169 if (!maff)
4170 return NULL((void*)0);
4171
4172 for (i = 0; i < maff->n; ++i) {
4173 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4174 if (!maff->u.p[i])
4175 return isl_multi_aff_free(maff);
4176 }
4177
4178 return maff;
4179}
4180
4181__isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_setisl_map *dom,
4182 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4183{
4184 maff1 = isl_multi_aff_add(maff1, maff2);
4185 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4186 return maff1;
4187}
4188
4189isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4190{
4191 if (!maff)
4192 return isl_bool_error;
4193
4194 return isl_bool_false;
4195}
4196
4197/* Return the set of domain elements where "ma1" is lexicographically
4198 * smaller than or equal to "ma2".
4199 */
4200__isl_give isl_setisl_map *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4201 __isl_take isl_multi_aff *ma2)
4202{
4203 return isl_multi_aff_lex_ge_set(ma2, ma1);
4204}
4205
4206/* Return the set of domain elements where "ma1" is lexicographically
4207 * smaller than "ma2".
4208 */
4209__isl_give isl_setisl_map *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4210 __isl_take isl_multi_aff *ma2)
4211{
4212 return isl_multi_aff_lex_gt_set(ma2, ma1);
4213}
4214
4215/* Return the set of domain elements where "ma1" and "ma2"
4216 * satisfy "order".
4217 */
4218static __isl_give isl_setisl_map *isl_multi_aff_order_set(
4219 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4220 __isl_give isl_map *order(__isl_take isl_space *set_space))
4221{
4222 isl_space *space;
4223 isl_map *map1, *map2;
4224 isl_map *map, *ge;
4225
4226 map1 = isl_map_from_multi_aff_internal(ma1);
4227 map2 = isl_map_from_multi_aff_internal(ma2);
4228 map = isl_map_range_product(map1, map2);
4229 space = isl_space_range(isl_map_get_space(map));
4230 space = isl_space_domain(isl_space_unwrap(space));
4231 ge = order(space);
4232 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4233
4234 return isl_map_domain(map);
4235}
4236
4237/* Return the set of domain elements where "ma1" is lexicographically
4238 * greater than or equal to "ma2".
4239 */
4240__isl_give isl_setisl_map *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4241 __isl_take isl_multi_aff *ma2)
4242{
4243 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4244}
4245
4246/* Return the set of domain elements where "ma1" is lexicographically
4247 * greater than "ma2".
4248 */
4249__isl_give isl_setisl_map *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4250 __isl_take isl_multi_aff *ma2)
4251{
4252 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4253}
4254
4255#define isl_multi_aff_zero_in_spaceisl_multi_aff_zero isl_multi_aff_zero
4256
4257#undef PWisl_pw_multi_aff
4258#define PWisl_pw_multi_aff isl_pw_multi_aff
4259#undef BASEunion_pw_aff
4260#define BASEunion_pw_aff multi_aff
4261#undef EL_IS_ZEROis_empty
4262#define EL_IS_ZEROis_empty is_empty
4263#undef ZEROempty
4264#define ZEROempty empty
4265#undef IS_ZEROis_empty
4266#define IS_ZEROis_empty is_empty
4267#undef FIELDmaff
4268#define FIELDmaff maff
4269#undef DEFAULT_IS_ZERO0
4270#define DEFAULT_IS_ZERO0 0
4271
4272#include <isl_pw_templ.c>
4273#include <isl_pw_add_constant_multi_val_templ.c>
4274#include <isl_pw_add_constant_val_templ.c>
4275#include <isl_pw_bind_domain_templ.c>
4276#include <isl_pw_move_dims_templ.c>
4277#include <isl_pw_neg_templ.c>
4278#include <isl_pw_pullback_templ.c>
4279#include <isl_pw_union_opt.c>
4280
4281#undef BASEunion_pw_aff
4282#define BASEunion_pw_aff pw_multi_aff
4283
4284#include <isl_union_multi.c>
4285#include <isl_union_neg.c>
4286
4287/* Generic function for extracting a factor from a product "pma".
4288 * "check_space" checks that the space is that of the right kind of product.
4289 * "space_factor" extracts the factor from the space.
4290 * "multi_aff_factor" extracts the factor from the constituent functions.
4291 */
4292static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4293 __isl_take isl_pw_multi_aff *pma,
4294 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4295 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4296 __isl_give isl_multi_aff *(*multi_aff_factor)(
4297 __isl_take isl_multi_aff *ma))
4298{
4299 int i;
4300 isl_space *space;
4301
4302 if (check_space(pma) < 0)
4303 return isl_pw_multi_aff_free(pma);
4304
4305 space = isl_pw_multi_aff_take_space(pma);
4306 space = space_factor(space);
4307
4308 for (i = 0; pma && i < pma->n; ++i) {
4309 isl_multi_aff *ma;
4310
4311 ma = isl_pw_multi_aff_take_base_at(pma, i);
4312 ma = multi_aff_factor(ma);
4313 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4314 }
4315
4316 pma = isl_pw_multi_aff_restore_space(pma, space);
4317
4318 return pma;
4319}
4320
4321/* Is the range of "pma" a wrapped relation?
4322 */
4323static isl_bool isl_pw_multi_aff_range_is_wrapping(
4324 __isl_keep isl_pw_multi_aff *pma)
4325{
4326 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4327}
4328
4329/* Check that the range of "pma" is a product.
4330 */
4331static isl_stat pw_multi_aff_check_range_product(
4332 __isl_keep isl_pw_multi_aff *pma)
4333{
4334 isl_bool wraps;
4335
4336 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4337 if (wraps < 0)
4338 return isl_stat_error;
4339 if (!wraps)
4340 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
, "range is not a product", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4341); return isl_stat_error; } while (0)
4341 "range is not a product", return isl_stat_error)do { isl_handle_error(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid
, "range is not a product", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4341); return isl_stat_error; } while (0)
;
4342 return isl_stat_ok;
4343}
4344
4345/* Given a function A -> [B -> C], extract the function A -> B.
4346 */
4347__isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4348 __isl_take isl_pw_multi_aff *pma)
4349{
4350 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4351 &isl_space_range_factor_domain,
4352 &isl_multi_aff_range_factor_domain);
4353}
4354
4355/* Given a function A -> [B -> C], extract the function A -> C.
4356 */
4357__isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4358 __isl_take isl_pw_multi_aff *pma)
4359{
4360 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4361 &isl_space_range_factor_range,
4362 &isl_multi_aff_range_factor_range);
4363}
4364
4365/* Given two piecewise multi affine expressions, return a piecewise
4366 * multi-affine expression defined on the union of the definition domains
4367 * of the inputs that is equal to the lexicographic maximum of the two
4368 * inputs on each cell. If only one of the two inputs is defined on
4369 * a given cell, then it is considered to be the maximum.
4370 */
4371__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4372 __isl_take isl_pw_multi_aff *pma1,
4373 __isl_take isl_pw_multi_aff *pma2)
4374{
4375 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4376 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4377 &isl_multi_aff_lex_ge_set);
4378}
4379
4380/* Given two piecewise multi affine expressions, return a piecewise
4381 * multi-affine expression defined on the union of the definition domains
4382 * of the inputs that is equal to the lexicographic minimum of the two
4383 * inputs on each cell. If only one of the two inputs is defined on
4384 * a given cell, then it is considered to be the minimum.
4385 */
4386__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4387 __isl_take isl_pw_multi_aff *pma1,
4388 __isl_take isl_pw_multi_aff *pma2)
4389{
4390 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4391 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4392 &isl_multi_aff_lex_le_set);
4393}
4394
4395__isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4396 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4397{
4398 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4399 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4400 &isl_multi_aff_add);
4401}
4402
4403/* Subtract "pma2" from "pma1" and return the result.
4404 */
4405__isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4406 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4407{
4408 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4409 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4410 &isl_multi_aff_sub);
4411}
4412
4413__isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4414 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4415{
4416 return isl_pw_multi_aff_union_add_(pma1, pma2);
4417}
4418
4419/* Compute the sum of "upa1" and "upa2" on the union of their domains,
4420 * with the actual sum on the shared domain and
4421 * the defined expression on the symmetric difference of the domains.
4422 */
4423__isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4424 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4425{
4426 return isl_union_pw_aff_union_add_(upa1, upa2);
4427}
4428
4429/* Compute the sum of "upma1" and "upma2" on the union of their domains,
4430 * with the actual sum on the shared domain and
4431 * the defined expression on the symmetric difference of the domains.
4432 */
4433__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4434 __isl_take isl_union_pw_multi_aff *upma1,
4435 __isl_take isl_union_pw_multi_aff *upma2)
4436{
4437 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4438}
4439
4440/* Given two piecewise multi-affine expressions A -> B and C -> D,
4441 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4442 */
4443__isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4444 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4445{
4446 int i, j, n;
4447 isl_space *space;
4448 isl_pw_multi_aff *res;
4449
4450 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4451 goto error;
4452
4453 n = pma1->n * pma2->n;
4454 space = isl_space_product(isl_space_copy(pma1->dim),
4455 isl_space_copy(pma2->dim));
4456 res = isl_pw_multi_aff_alloc_size(space, n);
4457
4458 for (i = 0; i < pma1->n; ++i) {
4459 for (j = 0; j < pma2->n; ++j) {
4460 isl_setisl_map *domain;
4461 isl_multi_aff *ma;
4462
4463 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4464 isl_set_copy(pma2->p[j].set));
4465 ma = isl_multi_aff_product(
4466 isl_multi_aff_copy(pma1->p[i].maff),
4467 isl_multi_aff_copy(pma2->p[j].maff));
4468 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4469 }
4470 }
4471
4472 isl_pw_multi_aff_free(pma1);
4473 isl_pw_multi_aff_free(pma2);
4474 return res;
4475error:
4476 isl_pw_multi_aff_free(pma1);
4477 isl_pw_multi_aff_free(pma2);
4478 return NULL((void*)0);
4479}
4480
4481/* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4482 * denominator "denom".
4483 * "denom" is allowed to be negative, in which case the actual denominator
4484 * is -denom and the expressions are added instead.
4485 */
4486static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4487 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4488{
4489 int i, first;
4490 int sign;
4491 isl_int d;
4492
4493 first = isl_seq_first_non_zero(c, n);
4494 if (first == -1)
4495 return aff;
4496
4497 sign = isl_int_sgn(denom)isl_sioimath_sgn(*(denom));
4498 isl_int_init(d)isl_sioimath_init((d));
4499 isl_int_abs(d, denom)isl_sioimath_abs((d), *(denom));
4500 for (i = first; i < n; ++i) {
4501 isl_aff *aff_i;
4502
4503 if (isl_int_is_zero(c[i])(isl_sioimath_sgn(*(c[i])) == 0))
4504 continue;
4505 aff_i = isl_multi_aff_get_aff(ma, i);
4506 aff_i = isl_aff_scale(aff_i, c[i]);
4507 aff_i = isl_aff_scale_down(aff_i, d);
4508 if (sign >= 0)
4509 aff = isl_aff_sub(aff, aff_i);
4510 else
4511 aff = isl_aff_add(aff, aff_i);
4512 }
4513 isl_int_clear(d)isl_sioimath_clear((d));
4514
4515 return aff;
4516}
4517
4518/* Extract an affine expression that expresses the output dimension "pos"
4519 * of "bmap" in terms of the parameters and input dimensions from
4520 * equality "eq".
4521 * Note that this expression may involve integer divisions defined
4522 * in terms of parameters and input dimensions.
4523 * The equality may also involve references to earlier (but not later)
4524 * output dimensions. These are replaced by the corresponding elements
4525 * in "ma".
4526 *
4527 * If the equality is of the form
4528 *
4529 * f(i) + h(j) + a x + g(i) = 0,
4530 *
4531 * with f(i) a linear combinations of the parameters and input dimensions,
4532 * g(i) a linear combination of integer divisions defined in terms of the same
4533 * and h(j) a linear combinations of earlier output dimensions,
4534 * then the affine expression is
4535 *
4536 * (-f(i) - g(i))/a - h(j)/a
4537 *
4538 * If the equality is of the form
4539 *
4540 * f(i) + h(j) - a x + g(i) = 0,
4541 *
4542 * then the affine expression is
4543 *
4544 * (f(i) + g(i))/a - h(j)/(-a)
4545 *
4546 *
4547 * If "div" refers to an integer division (i.e., it is smaller than
4548 * the number of integer divisions), then the equality constraint
4549 * does involve an integer division (the one at position "div") that
4550 * is defined in terms of output dimensions. However, this integer
4551 * division can be eliminated by exploiting a pair of constraints
4552 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4553 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4554 * -l + x >= 0.
4555 * In particular, let
4556 *
4557 * x = e(i) + m floor(...)
4558 *
4559 * with e(i) the expression derived above and floor(...) the integer
4560 * division involving output dimensions.
4561 * From
4562 *
4563 * l <= x <= l + n,
4564 *
4565 * we have
4566 *
4567 * 0 <= x - l <= n
4568 *
4569 * This means
4570 *
4571 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4572 * = (e(i) - l) mod m
4573 *
4574 * Therefore,
4575 *
4576 * x - l = (e(i) - l) mod m
4577 *
4578 * or
4579 *
4580 * x = ((e(i) - l) mod m) + l
4581 *
4582 * The variable "shift" below contains the expression -l, which may
4583 * also involve a linear combination of earlier output dimensions.
4584 */
4585static __isl_give isl_aff *extract_aff_from_equality(
4586 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4587 __isl_keep isl_multi_aff *ma)
4588{
4589 unsigned o_out;
4590 isl_size n_div, n_out;
4591 isl_ctx *ctx;
4592 isl_local_space *ls;
4593 isl_aff *aff, *shift;
4594 isl_val *mod;
4595
4596 ctx = isl_basic_map_get_ctx(bmap);
4597 ls = isl_basic_map_get_local_space(bmap);
4598 ls = isl_local_space_domain(ls);
4599 aff = isl_aff_alloc(isl_local_space_copy(ls));
4600 if (!aff)
4601 goto error;
4602 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4603 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4604 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4605 if (n_out < 0 || n_div < 0)
4606 goto error;
4607 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])(isl_sioimath_sgn(*(bmap->eq[eq][o_out + pos])) < 0)) {
4608 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4609 isl_seq_cpy(aff->v->el + 1 + o_out,
4610 bmap->eq[eq] + o_out + n_out, n_div);
4611 } else {
4612 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4613 isl_seq_neg(aff->v->el + 1 + o_out,
4614 bmap->eq[eq] + o_out + n_out, n_div);
4615 }
4616 if (div < n_div)
4617 isl_int_set_si(aff->v->el[1 + o_out + div], 0)isl_sioimath_set_si((aff->v->el[1 + o_out + div]), 0);
4618 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]))
;
4619 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4620 bmap->eq[eq][o_out + pos]);
4621 if (div < n_div) {
4622 shift = isl_aff_alloc(isl_local_space_copy(ls));
4623 if (!shift)
4624 goto error;
4625 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4626 isl_seq_cpy(shift->v->el + 1 + o_out,
4627 bmap->ineq[ineq] + o_out + n_out, n_div);
4628 isl_int_set_si(shift->v->el[0], 1)isl_sioimath_set_si((shift->v->el[0]), 1);
4629 shift = subtract_initial(shift, ma, pos,
4630 bmap->ineq[ineq] + o_out, ctx->negone);
4631 aff = isl_aff_add(aff, isl_aff_copy(shift));
4632 mod = isl_val_int_from_isl_int(ctx,
4633 bmap->eq[eq][o_out + n_out + div]);
4634 mod = isl_val_abs(mod);
4635 aff = isl_aff_mod_val(aff, mod);
4636 aff = isl_aff_sub(aff, shift);
4637 }
4638
4639 isl_local_space_free(ls);
4640 return aff;
4641error:
4642 isl_local_space_free(ls);
4643 isl_aff_free(aff);
4644 return NULL((void*)0);
4645}
4646
4647/* Given a basic map with output dimensions defined
4648 * in terms of the parameters input dimensions and earlier
4649 * output dimensions using an equality (and possibly a pair on inequalities),
4650 * extract an isl_aff that expresses output dimension "pos" in terms
4651 * of the parameters and input dimensions.
4652 * Note that this expression may involve integer divisions defined
4653 * in terms of parameters and input dimensions.
4654 * "ma" contains the expressions corresponding to earlier output dimensions.
4655 *
4656 * This function shares some similarities with
4657 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4658 */
4659static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4660 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4661{
4662 int eq, div, ineq;
4663 isl_aff *aff;
4664
4665 if (!bmap)
4666 return NULL((void*)0);
4667 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4668 if (eq >= bmap->n_eq)
4669 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4670); return ((void*)0); } while (0)
4670 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4670); return ((void*)0); } while (0)
;
4671 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4672
4673 aff = isl_aff_remove_unused_divs(aff);
4674 return aff;
4675}
4676
4677/* Given a basic map where each output dimension is defined
4678 * in terms of the parameters and input dimensions using an equality,
4679 * extract an isl_multi_aff that expresses the output dimensions in terms
4680 * of the parameters and input dimensions.
4681 */
4682static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4683 __isl_take isl_basic_map *bmap)
4684{
4685 int i;
4686 isl_size n_out;
4687 isl_multi_aff *ma;
4688
4689 if (!bmap)
4690 return NULL((void*)0);
4691
4692 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4693 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4694 if (n_out < 0)
4695 ma = isl_multi_aff_free(ma);
4696
4697 for (i = 0; i < n_out; ++i) {
4698 isl_aff *aff;
4699
4700 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4701 ma = isl_multi_aff_set_aff(ma, i, aff);
4702 }
4703
4704 isl_basic_map_free(bmap);
4705
4706 return ma;
4707}
4708
4709/* Given a basic set where each set dimension is defined
4710 * in terms of the parameters using an equality,
4711 * extract an isl_multi_aff that expresses the set dimensions in terms
4712 * of the parameters.
4713 */
4714__isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4715 __isl_take isl_basic_setisl_basic_map *bset)
4716{
4717 return extract_isl_multi_aff_from_basic_map(bset);
4718}
4719
4720/* Create an isl_pw_multi_aff that is equivalent to
4721 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4722 * The given basic map is such that each output dimension is defined
4723 * in terms of the parameters and input dimensions using an equality.
4724 *
4725 * Since some applications expect the result of isl_pw_multi_aff_from_map
4726 * to only contain integer affine expressions, we compute the floor
4727 * of the expression before returning.
4728 *
4729 * Remove all constraints involving local variables without
4730 * an explicit representation (resulting in the removal of those
4731 * local variables) prior to the actual extraction to ensure
4732 * that the local spaces in which the resulting affine expressions
4733 * are created do not contain any unknown local variables.
4734 * Removing such constraints is safe because constraints involving
4735 * unknown local variables are not used to determine whether
4736 * a basic map is obviously single-valued.
4737 */
4738static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4739 __isl_take isl_setisl_map *domain, __isl_take isl_basic_map *bmap)
4740{
4741 isl_multi_aff *ma;
4742
4743 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4744 ma = extract_isl_multi_aff_from_basic_map(bmap);
4745 ma = isl_multi_aff_floor(ma);
4746 return isl_pw_multi_aff_alloc(domain, ma);
4747}
4748
4749/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4750 * This obviously only works if the input "map" is single-valued.
4751 * If so, we compute the lexicographic minimum of the image in the form
4752 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4753 * to its lexicographic minimum.
4754 * If the input is not single-valued, we produce an error.
4755 */
4756static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4757 __isl_take isl_map *map)
4758{
4759 int i;
4760 int sv;
4761 isl_pw_multi_aff *pma;
4762
4763 sv = isl_map_is_single_valued(map);
4764 if (sv < 0)
4765 goto error;
4766 if (!sv)
4767 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4768); goto error; } while (0)
4768 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 4768); goto error; } while (0)
;
4769 map = isl_map_make_disjoint(map);
4770 if (!map)
4771 return NULL((void*)0);
4772
4773 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4774
4775 for (i = 0; i < map->n; ++i) {
4776 isl_pw_multi_aff *pma_i;
4777 isl_basic_map *bmap;
4778 bmap = isl_basic_map_copy(map->p[i]);
4779 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4780 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4781 }
4782
4783 isl_map_free(map);
4784 return pma;
4785error:
4786 isl_map_free(map);
4787 return NULL((void*)0);
4788}
4789
4790/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4791 * taking into account that the output dimension at position "d"
4792 * can be represented as
4793 *
4794 * x = floor((e(...) + c1) / m)
4795 *
4796 * given that constraint "i" is of the form
4797 *
4798 * e(...) + c1 - m x >= 0
4799 *
4800 *
4801 * Let "map" be of the form
4802 *
4803 * A -> B
4804 *
4805 * We construct a mapping
4806 *
4807 * A -> [A -> x = floor(...)]
4808 *
4809 * apply that to the map, obtaining
4810 *
4811 * [A -> x = floor(...)] -> B
4812 *
4813 * and equate dimension "d" to x.
4814 * We then compute a isl_pw_multi_aff representation of the resulting map
4815 * and plug in the mapping above.
4816 */
4817static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4818 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4819{
4820 isl_ctx *ctx;
4821 isl_space *space = NULL((void*)0);
4822 isl_local_space *ls;
4823 isl_multi_aff *ma;
4824 isl_aff *aff;
4825 isl_vec *v;
4826 isl_map *insert;
4827 int offset;
4828 isl_size n;
4829 isl_size n_in;
4830 isl_pw_multi_aff *pma;
4831 isl_bool is_set;
4832
4833 is_set = isl_map_is_set(map);
4834 if (is_set < 0)
4835 goto error;
4836
4837 offset = isl_basic_map_offset(hull, isl_dim_out);
4838 ctx = isl_map_get_ctx(map);
4839 space = isl_space_domain(isl_map_get_space(map));
4840 n_in = isl_space_dim(space, isl_dim_set);
4841 n = isl_space_dim(space, isl_dim_all);
4842 if (n_in < 0 || n < 0)
4843 goto error;
4844
4845 v = isl_vec_alloc(ctx, 1 + 1 + n);
4846 if (v) {
4847 isl_int_neg(v->el[0], hull->ineq[i][offset + d])isl_sioimath_neg((v->el[0]), *(hull->ineq[i][offset + d
]))
;
4848 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4849 }
4850 isl_basic_map_free(hull);
4851
4852 ls = isl_local_space_from_space(isl_space_copy(space));
4853 aff = isl_aff_alloc_vec(ls, v);
4854 aff = isl_aff_floor(aff);
4855 if (is_set) {
4856 isl_space_free(space);
4857 ma = isl_multi_aff_from_aff(aff);
4858 } else {
4859 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4860 ma = isl_multi_aff_range_product(ma,
4861 isl_multi_aff_from_aff(aff));
4862 }
4863
4864 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4865 map = isl_map_apply_domain(map, insert);
4866 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4867 pma = isl_pw_multi_aff_from_map(map);
4868 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4869
4870 return pma;
4871error:
4872 isl_space_free(space);
4873 isl_map_free(map);
4874 isl_basic_map_free(hull);
4875 return NULL((void*)0);
4876}
4877
4878/* Is constraint "c" of the form
4879 *
4880 * e(...) + c1 - m x >= 0
4881 *
4882 * or
4883 *
4884 * -e(...) + c2 + m x >= 0
4885 *
4886 * where m > 1 and e only depends on parameters and input dimemnsions?
4887 *
4888 * "offset" is the offset of the output dimensions
4889 * "pos" is the position of output dimension x.
4890 */
4891static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4892{
4893 if (isl_int_is_zero(c[offset + d])(isl_sioimath_sgn(*(c[offset + d])) == 0))
4894 return 0;
4895 if (isl_int_is_one(c[offset + d])(isl_sioimath_cmp_si(*(c[offset + d]), 1) == 0))
4896 return 0;
4897 if (isl_int_is_negone(c[offset + d])(isl_sioimath_cmp_si(*(c[offset + d]), -1) == 0))
4898 return 0;
4899 if (isl_seq_first_non_zero(c + offset, d) != -1)
4900 return 0;
4901 if (isl_seq_first_non_zero(c + offset + d + 1,
4902 total - (offset + d + 1)) != -1)
4903 return 0;
4904 return 1;
4905}
4906
4907/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4908 *
4909 * As a special case, we first check if there is any pair of constraints,
4910 * shared by all the basic maps in "map" that force a given dimension
4911 * to be equal to the floor of some affine combination of the input dimensions.
4912 *
4913 * In particular, if we can find two constraints
4914 *
4915 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4916 *
4917 * and
4918 *
4919 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4920 *
4921 * where m > 1 and e only depends on parameters and input dimemnsions,
4922 * and such that
4923 *
4924 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4925 *
4926 * then we know that we can take
4927 *
4928 * x = floor((e(...) + c1) / m)
4929 *
4930 * without having to perform any computation.
4931 *
4932 * Note that we know that
4933 *
4934 * c1 + c2 >= 1
4935 *
4936 * If c1 + c2 were 0, then we would have detected an equality during
4937 * simplification. If c1 + c2 were negative, then we would have detected
4938 * a contradiction.
4939 */
4940static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4941 __isl_take isl_map *map)
4942{
4943 int d;
4944 isl_size dim;
4945 int i, j, n;
4946 int offset;
4947 isl_size total;
4948 isl_int sum;
4949 isl_basic_map *hull;
4950
4951 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4952 dim = isl_map_dim(map, isl_dim_out);
4953 total = isl_basic_map_dim(hull, isl_dim_all);
4954 if (dim < 0 || total < 0)
4955 goto error;
4956
4957 isl_int_init(sum)isl_sioimath_init((sum));
4958 offset = isl_basic_map_offset(hull, isl_dim_out);
4959 n = hull->n_ineq;
4960 for (d = 0; d < dim; ++d) {
4961 for (i = 0; i < n; ++i) {
4962 if (!is_potential_div_constraint(hull->ineq[i],
4963 offset, d, 1 + total))
4964 continue;
4965 for (j = i + 1; j < n; ++j) {
4966 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4967 hull->ineq[j] + 1, total))
4968 continue;
4969 isl_int_add(sum, hull->ineq[i][0],isl_sioimath_add((sum), *(hull->ineq[i][0]), *(hull->ineq
[j][0]))
4970 hull->ineq[j][0])isl_sioimath_add((sum), *(hull->ineq[i][0]), *(hull->ineq
[j][0]))
;
4971 if (isl_int_abs_lt(sum,(isl_sioimath_abs_cmp(*(sum), *(hull->ineq[i][offset + d])
) < 0)
4972 hull->ineq[i][offset + d])(isl_sioimath_abs_cmp(*(sum), *(hull->ineq[i][offset + d])
) < 0)
)
4973 break;
4974
4975 }
4976 if (j >= n)
4977 continue;
4978 isl_int_clear(sum)isl_sioimath_clear((sum));
4979 if (isl_int_is_pos(hull->ineq[j][offset + d])(isl_sioimath_sgn(*(hull->ineq[j][offset + d])) > 0))
4980 j = i;
4981 return pw_multi_aff_from_map_div(map, hull, d, j);
4982 }
4983 }
4984 isl_int_clear(sum)isl_sioimath_clear((sum));
4985 isl_basic_map_free(hull);
4986 return pw_multi_aff_from_map_base(map);
4987error:
4988 isl_map_free(map);
4989 isl_basic_map_free(hull);
4990 return NULL((void*)0);
4991}
4992
4993/* Given an affine expression
4994 *
4995 * [A -> B] -> f(A,B)
4996 *
4997 * construct an isl_multi_aff
4998 *
4999 * [A -> B] -> B'
5000 *
5001 * such that dimension "d" in B' is set to "aff" and the remaining
5002 * dimensions are set equal to the corresponding dimensions in B.
5003 * "n_in" is the dimension of the space A.
5004 * "n_out" is the dimension of the space B.
5005 *
5006 * If "is_set" is set, then the affine expression is of the form
5007 *
5008 * [B] -> f(B)
5009 *
5010 * and we construct an isl_multi_aff
5011 *
5012 * B -> B'
5013 */
5014static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5015 unsigned n_in, unsigned n_out, int is_set)
5016{
5017 int i;
5018 isl_multi_aff *ma;
5019 isl_space *space, *space2;
5020 isl_local_space *ls;
5021
5022 space = isl_aff_get_domain_space(aff);
5023 ls = isl_local_space_from_space(isl_space_copy(space));
5024 space2 = isl_space_copy(space);
5025 if (!is_set)
5026 space2 = isl_space_range(isl_space_unwrap(space2));
5027 space = isl_space_map_from_domain_and_range(space, space2);
5028 ma = isl_multi_aff_alloc(space);
5029 ma = isl_multi_aff_set_aff(ma, d, aff);
5030
5031 for (i = 0; i < n_out; ++i) {
5032 if (i == d)
5033 continue;
5034 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5035 isl_dim_set, n_in + i);
5036 ma = isl_multi_aff_set_aff(ma, i, aff);
5037 }
5038
5039 isl_local_space_free(ls);
5040
5041 return ma;
5042}
5043
5044/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5045 * taking into account that the dimension at position "d" can be written as
5046 *
5047 * x = m a + f(..) (1)
5048 *
5049 * where m is equal to "gcd".
5050 * "i" is the index of the equality in "hull" that defines f(..).
5051 * In particular, the equality is of the form
5052 *
5053 * f(..) - x + m g(existentials) = 0
5054 *
5055 * or
5056 *
5057 * -f(..) + x + m g(existentials) = 0
5058 *
5059 * We basically plug (1) into "map", resulting in a map with "a"
5060 * in the range instead of "x". The corresponding isl_pw_multi_aff
5061 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5062 *
5063 * Specifically, given the input map
5064 *
5065 * A -> B
5066 *
5067 * We first wrap it into a set
5068 *
5069 * [A -> B]
5070 *
5071 * and define (1) on top of the corresponding space, resulting in "aff".
5072 * We use this to create an isl_multi_aff that maps the output position "d"
5073 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5074 * We plug this into the wrapped map, unwrap the result and compute the
5075 * corresponding isl_pw_multi_aff.
5076 * The result is an expression
5077 *
5078 * A -> T(A)
5079 *
5080 * We adjust that to
5081 *
5082 * A -> [A -> T(A)]
5083 *
5084 * so that we can plug that into "aff", after extending the latter to
5085 * a mapping
5086 *
5087 * [A -> B] -> B'
5088 *
5089 *
5090 * If "map" is actually a set, then there is no "A" space, meaning
5091 * that we do not need to perform any wrapping, and that the result
5092 * of the recursive call is of the form
5093 *
5094 * [T]
5095 *
5096 * which is plugged into a mapping of the form
5097 *
5098 * B -> B'
5099 */
5100static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5101 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5102 isl_int gcd)
5103{
5104 isl_setisl_map *set;
5105 isl_space *space;
5106 isl_local_space *ls;
5107 isl_aff *aff;
5108 isl_multi_aff *ma;
5109 isl_pw_multi_aff *pma, *id;
5110 isl_size n_in;
5111 unsigned o_out;
5112 isl_size n_out;
5113 isl_bool is_set;
5114
5115 is_set = isl_map_is_set(map);
5116 if (is_set < 0)
5117 goto error;
5118
5119 n_in = isl_basic_map_dim(hull, isl_dim_in);
5120 n_out = isl_basic_map_dim(hull, isl_dim_out);
5121 if (n_in < 0 || n_out < 0)
5122 goto error;
5123 o_out = isl_basic_map_offset(hull, isl_dim_out);
5124
5125 if (is_set)
5126 set = map;
5127 else
5128 set = isl_map_wrap(map);
5129 space = isl_space_map_from_set(isl_set_get_space(set));
5130 ma = isl_multi_aff_identity(space);
5131 ls = isl_local_space_from_space(isl_set_get_space(set));
5132 aff = isl_aff_alloc(ls);
5133 if (aff) {
5134 isl_int_set_si(aff->v->el[0], 1)isl_sioimath_set_si((aff->v->el[0]), 1);
5135 if (isl_int_is_one(hull->eq[i][o_out + d])(isl_sioimath_cmp_si(*(hull->eq[i][o_out + d]), 1) == 0))
5136 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5137 aff->v->size - 1);
5138 else
5139 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5140 aff->v->size - 1);
5141 isl_int_set(aff->v->el[1 + o_out + d], gcd)isl_sioimath_set((aff->v->el[1 + o_out + d]), *(gcd));
5142 }
5143 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5144 set = isl_set_preimage_multi_aff(set, ma);
5145
5146 ma = range_map(aff, d, n_in, n_out, is_set);
5147
5148 if (is_set)
5149 map = set;
5150 else
5151 map = isl_set_unwrap(set);
5152 pma = isl_pw_multi_aff_from_map(map);
5153
5154 if (!is_set) {
5155 space = isl_pw_multi_aff_get_domain_space(pma);
5156 space = isl_space_map_from_set(space);
5157 id = isl_pw_multi_aff_identity(space);
5158 pma = isl_pw_multi_aff_range_product(id, pma);
5159 }
5160 id = isl_pw_multi_aff_from_multi_aff(ma);
5161 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5162
5163 isl_basic_map_free(hull);
5164 return pma;
5165error:
5166 isl_map_free(map);
5167 isl_basic_map_free(hull);
5168 return NULL((void*)0);
5169}
5170
5171/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5172 * "hull" contains the equalities valid for "map".
5173 *
5174 * Check if any of the output dimensions is "strided".
5175 * That is, we check if it can be written as
5176 *
5177 * x = m a + f(..)
5178 *
5179 * with m greater than 1, a some combination of existentially quantified
5180 * variables and f an expression in the parameters and input dimensions.
5181 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5182 *
5183 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5184 * special case.
5185 */
5186static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5187 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5188{
5189 int i, j;
5190 isl_size n_out;
5191 unsigned o_out;
5192 isl_size n_div;
5193 unsigned o_div;
5194 isl_int gcd;
5195
5196 n_div = isl_basic_map_dim(hull, isl_dim_div);
5197 n_out = isl_basic_map_dim(hull, isl_dim_out);
5198 if (n_div < 0 || n_out < 0)
5199 goto error;
5200
5201 if (n_div == 0) {
5202 isl_basic_map_free(hull);
5203 return pw_multi_aff_from_map_check_div(map);
5204 }
5205
5206 isl_int_init(gcd)isl_sioimath_init((gcd));
5207
5208 o_div = isl_basic_map_offset(hull, isl_dim_div);
5209 o_out = isl_basic_map_offset(hull, isl_dim_out);
5210
5211 for (i = 0; i < n_out; ++i) {
5212 for (j = 0; j < hull->n_eq; ++j) {
5213 isl_int *eq = hull->eq[j];
5214 isl_pw_multi_aff *res;
5215
5216 if (!isl_int_is_one(eq[o_out + i])(isl_sioimath_cmp_si(*(eq[o_out + i]), 1) == 0) &&
5217 !isl_int_is_negone(eq[o_out + i])(isl_sioimath_cmp_si(*(eq[o_out + i]), -1) == 0))
5218 continue;
5219 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5220 continue;
5221 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5222 n_out - (i + 1)) != -1)
5223 continue;
5224 isl_seq_gcd(eq + o_div, n_div, &gcd);
5225 if (isl_int_is_zero(gcd)(isl_sioimath_sgn(*(gcd)) == 0))
5226 continue;
5227 if (isl_int_is_one(gcd)(isl_sioimath_cmp_si(*(gcd), 1) == 0))
5228 continue;
5229
5230 res = pw_multi_aff_from_map_stride(map, hull,
5231 i, j, gcd);
5232 isl_int_clear(gcd)isl_sioimath_clear((gcd));
5233 return res;
5234 }
5235 }
5236
5237 isl_int_clear(gcd)isl_sioimath_clear((gcd));
5238 isl_basic_map_free(hull);
5239 return pw_multi_aff_from_map_check_div(map);
5240error:
5241 isl_map_free(map);
5242 isl_basic_map_free(hull);
5243 return NULL((void*)0);
5244}
5245
5246/* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5247 *
5248 * As a special case, we first check if all output dimensions are uniquely
5249 * defined in terms of the parameters and input dimensions over the entire
5250 * domain. If so, we extract the desired isl_pw_multi_aff directly
5251 * from the affine hull of "map" and its domain.
5252 *
5253 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5254 * special cases.
5255 */
5256__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5257{
5258 isl_bool sv;
5259 isl_size n;
5260 isl_basic_map *hull;
5261
5262 n = isl_map_n_basic_map(map);
5263 if (n < 0)
5264 goto error;
5265
5266 if (n == 1) {
5267 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5268 hull = isl_basic_map_plain_affine_hull(hull);
5269 sv = isl_basic_map_plain_is_single_valued(hull);
5270 if (sv >= 0 && sv)
5271 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5272 hull);
5273 isl_basic_map_free(hull);
5274 }
5275 map = isl_map_detect_equalities(map);
5276 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5277 sv = isl_basic_map_plain_is_single_valued(hull);
5278 if (sv >= 0 && sv)
5279 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5280 if (sv >= 0)
5281 return pw_multi_aff_from_map_check_strides(map, hull);
5282 isl_basic_map_free(hull);
5283error:
5284 isl_map_free(map);
5285 return NULL((void*)0);
5286}
5287
5288__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_setisl_map *set)
5289{
5290 return isl_pw_multi_aff_from_map(set);
5291}
5292
5293/* Convert "map" into an isl_pw_multi_aff (if possible) and
5294 * add it to *user.
5295 */
5296static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5297{
5298 isl_union_pw_multi_aff **upma = user;
5299 isl_pw_multi_aff *pma;
5300
5301 pma = isl_pw_multi_aff_from_map(map);
5302 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5303
5304 return *upma ? isl_stat_ok : isl_stat_error;
5305}
5306
5307/* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5308 * domain.
5309 */
5310__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5311 __isl_take isl_aff *aff)
5312{
5313 isl_multi_aff *ma;
5314 isl_pw_multi_aff *pma;
5315
5316 ma = isl_multi_aff_from_aff(aff);
5317 pma = isl_pw_multi_aff_from_multi_aff(ma);
5318 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5319}
5320
5321/* Try and create an isl_union_pw_multi_aff that is equivalent
5322 * to the given isl_union_map.
5323 * The isl_union_map is required to be single-valued in each space.
5324 * Otherwise, an error is produced.
5325 */
5326__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5327 __isl_take isl_union_map *umap)
5328{
5329 isl_space *space;
5330 isl_union_pw_multi_aff *upma;
5331
5332 space = isl_union_map_get_space(umap);
5333 upma = isl_union_pw_multi_aff_empty(space);
5334 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5335 upma = isl_union_pw_multi_aff_free(upma);
5336 isl_union_map_free(umap);
5337
5338 return upma;
5339}
5340
5341/* Try and create an isl_union_pw_multi_aff that is equivalent
5342 * to the given isl_union_set.
5343 * The isl_union_set is required to be a singleton in each space.
5344 * Otherwise, an error is produced.
5345 */
5346__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5347 __isl_take isl_union_setisl_union_map *uset)
5348{
5349 return isl_union_pw_multi_aff_from_union_map(uset);
5350}
5351
5352/* Return the piecewise affine expression "set ? 1 : 0".
5353 */
5354__isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_setisl_map *set)
5355{
5356 isl_pw_aff *pa;
5357 isl_space *space = isl_set_get_space(set);
5358 isl_local_space *ls = isl_local_space_from_space(space);
5359 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5360 isl_aff *one = isl_aff_zero_on_domain(ls);
5361
5362 one = isl_aff_add_constant_si(one, 1);
5363 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5364 set = isl_set_complement(set);
5365 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5366
5367 return pa;
5368}
5369
5370/* Plug in "subs" for dimension "type", "pos" of "aff".
5371 *
5372 * Let i be the dimension to replace and let "subs" be of the form
5373 *
5374 * f/d
5375 *
5376 * and "aff" of the form
5377 *
5378 * (a i + g)/m
5379 *
5380 * The result is
5381 *
5382 * (a f + d g')/(m d)
5383 *
5384 * where g' is the result of plugging in "subs" in each of the integer
5385 * divisions in g.
5386 */
5387__isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5388 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5389{
5390 isl_ctx *ctx;
5391 isl_int v;
5392 isl_size n_div;
5393
5394 aff = isl_aff_cow(aff);
5395 if (!aff || !subs)
5396 return isl_aff_free(aff);
5397
5398 ctx = isl_aff_get_ctx(aff);
5399 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5400 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "spaces don't match"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5401); return isl_aff_free(aff); } while (0)
5401 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5401); return isl_aff_free(aff); } while (0)
;
5402 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5403 if (n_div < 0)
5404 return isl_aff_free(aff);
5405 if (n_div != 0)
5406 isl_die(ctx, isl_error_unsupported,do { isl_handle_error(ctx, isl_error_unsupported, "cannot handle divs yet"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5407); return isl_aff_free(aff); } while (0)
5407 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5407); return isl_aff_free(aff); } while (0)
;
5408
5409 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5410 if (!aff->ls)
5411 return isl_aff_free(aff);
5412
5413 aff->v = isl_vec_cow(aff->v);
5414 if (!aff->v)
5415 return isl_aff_free(aff);
5416
5417 pos += isl_local_space_offset(aff->ls, type);
5418
5419 isl_int_init(v)isl_sioimath_init((v));
5420 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5421 aff->v->size, subs->v->size, v);
5422 isl_int_clear(v)isl_sioimath_clear((v));
5423
5424 return aff;
5425}
5426
5427/* Plug in "subs" for dimension "type", "pos" in each of the affine
5428 * expressions in "maff".
5429 */
5430__isl_give isl_multi_aff *isl_multi_aff_substitute(
5431 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5432 __isl_keep isl_aff *subs)
5433{
5434 int i;
5435
5436 maff = isl_multi_aff_cow(maff);
5437 if (!maff || !subs)
5438 return isl_multi_aff_free(maff);
5439
5440 if (type == isl_dim_in)
5441 type = isl_dim_set;
5442
5443 for (i = 0; i < maff->n; ++i) {
5444 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5445 type, pos, subs);
5446 if (!maff->u.p[i])
5447 return isl_multi_aff_free(maff);
5448 }
5449
5450 return maff;
5451}
5452
5453/* Plug in "subs" for dimension "type", "pos" of "pma".
5454 *
5455 * pma is of the form
5456 *
5457 * A_i(v) -> M_i(v)
5458 *
5459 * while subs is of the form
5460 *
5461 * v' = B_j(v) -> S_j
5462 *
5463 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5464 * has a contribution in the result, in particular
5465 *
5466 * C_ij(S_j) -> M_i(S_j)
5467 *
5468 * Note that plugging in S_j in C_ij may also result in an empty set
5469 * and this contribution should simply be discarded.
5470 */
5471__isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5472 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5473 __isl_keep isl_pw_aff *subs)
5474{
5475 int i, j, n;
5476 isl_pw_multi_aff *res;
5477
5478 if (!pma || !subs)
5479 return isl_pw_multi_aff_free(pma);
5480
5481 n = pma->n * subs->n;
5482 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5483
5484 for (i = 0; i < pma->n; ++i) {
5485 for (j = 0; j < subs->n; ++j) {
5486 isl_setisl_map *common;
5487 isl_multi_aff *res_ij;
5488 int empty;
5489
5490 common = isl_set_intersect(
5491 isl_set_copy(pma->p[i].set),
5492 isl_set_copy(subs->p[j].set));
5493 common = isl_set_substitute(common,
5494 type, pos, subs->p[j].aff);
5495 empty = isl_set_plain_is_empty(common);
5496 if (empty < 0 || empty) {
5497 isl_set_free(common);
5498 if (empty < 0)
5499 goto error;
5500 continue;
5501 }
5502
5503 res_ij = isl_multi_aff_substitute(
5504 isl_multi_aff_copy(pma->p[i].maff),
5505 type, pos, subs->p[j].aff);
5506
5507 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5508 }
5509 }
5510
5511 isl_pw_multi_aff_free(pma);
5512 return res;
5513error:
5514 isl_pw_multi_aff_free(pma);
5515 isl_pw_multi_aff_free(res);
5516 return NULL((void*)0);
5517}
5518
5519/* Compute the preimage of a range of dimensions in the affine expression "src"
5520 * under "ma" and put the result in "dst". The number of dimensions in "src"
5521 * that precede the range is given by "n_before". The number of dimensions
5522 * in the range is given by the number of output dimensions of "ma".
5523 * The number of dimensions that follow the range is given by "n_after".
5524 * If "has_denom" is set (to one),
5525 * then "src" and "dst" have an extra initial denominator.
5526 * "n_div_ma" is the number of existentials in "ma"
5527 * "n_div_bset" is the number of existentials in "src"
5528 * The resulting "dst" (which is assumed to have been allocated by
5529 * the caller) contains coefficients for both sets of existentials,
5530 * first those in "ma" and then those in "src".
5531 * f, c1, c2 and g are temporary objects that have been initialized
5532 * by the caller.
5533 *
5534 * Let src represent the expression
5535 *
5536 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5537 *
5538 * and let ma represent the expressions
5539 *
5540 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5541 *
5542 * We start out with the following expression for dst:
5543 *
5544 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5545 *
5546 * with the multiplication factor f initially equal to 1
5547 * and f \sum_i b_i v_i kept separately.
5548 * For each x_i that we substitute, we multiply the numerator
5549 * (and denominator) of dst by c_1 = m_i and add the numerator
5550 * of the x_i expression multiplied by c_2 = f b_i,
5551 * after removing the common factors of c_1 and c_2.
5552 * The multiplication factor f also needs to be multiplied by c_1
5553 * for the next x_j, j > i.
5554 */
5555isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5556 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5557 int n_div_ma, int n_div_bmap,
5558 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5559{
5560 int i;
5561 isl_size n_param, n_in, n_out;
5562 int o_dst, o_src;
5563
5564 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5565 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5566 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5567 if (n_param < 0 || n_in < 0 || n_out < 0)
5568 return isl_stat_error;
5569
5570 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5571 o_dst = o_src = has_denom + 1 + n_param + n_before;
5572 isl_seq_clr(dst + o_dst, n_in);
5573 o_dst += n_in;
5574 o_src += n_out;
5575 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5576 o_dst += n_after;
5577 o_src += n_after;
5578 isl_seq_clr(dst + o_dst, n_div_ma);
5579 o_dst += n_div_ma;
5580 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5581
5582 isl_int_set_si(f, 1)isl_sioimath_set_si((f), 1);
5583
5584 for (i = 0; i < n_out; ++i) {
5585 int offset = has_denom + 1 + n_param + n_before + i;
5586
5587 if (isl_int_is_zero(src[offset])(isl_sioimath_sgn(*(src[offset])) == 0))
5588 continue;
5589 isl_int_set(c1, ma->u.p[i]->v->el[0])isl_sioimath_set((c1), *(ma->u.p[i]->v->el[0]));
5590 isl_int_mul(c2, f, src[offset])isl_sioimath_mul((c2), *(f), *(src[offset]));
5591 isl_int_gcd(g, c1, c2)isl_sioimath_gcd((g), *(c1), *(c2));
5592 isl_int_divexact(c1, c1, g)isl_sioimath_tdiv_q((c1), *(c1), *(g));
5593 isl_int_divexact(c2, c2, g)isl_sioimath_tdiv_q((c2), *(c2), *(g));
5594
5595 isl_int_mul(f, f, c1)isl_sioimath_mul((f), *(f), *(c1));
5596 o_dst = has_denom;
5597 o_src = 1;
5598 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5599 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5600 o_dst += 1 + n_param;
5601 o_src += 1 + n_param;
5602 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5603 o_dst += n_before;
5604 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5605 c2, ma->u.p[i]->v->el + o_src, n_in);
5606 o_dst += n_in;
5607 o_src += n_in;
5608 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5609 o_dst += n_after;
5610 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5611 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5612 o_dst += n_div_ma;
5613 o_src += n_div_ma;
5614 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5615 if (has_denom)
5616 isl_int_mul(dst[0], dst[0], c1)isl_sioimath_mul((dst[0]), *(dst[0]), *(c1));
5617 }
5618
5619 return isl_stat_ok;
5620}
5621
5622/* Compute the pullback of "aff" by the function represented by "ma".
5623 * In other words, plug in "ma" in "aff". The result is an affine expression
5624 * defined over the domain space of "ma".
5625 *
5626 * If "aff" is represented by
5627 *
5628 * (a(p) + b x + c(divs))/d
5629 *
5630 * and ma is represented by
5631 *
5632 * x = D(p) + F(y) + G(divs')
5633 *
5634 * then the result is
5635 *
5636 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5637 *
5638 * The divs in the local space of the input are similarly adjusted
5639 * through a call to isl_local_space_preimage_multi_aff.
5640 */
5641__isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5642 __isl_take isl_multi_aff *ma)
5643{
5644 isl_aff *res = NULL((void*)0);
5645 isl_local_space *ls;
5646 isl_size n_div_aff, n_div_ma;
5647 isl_int f, c1, c2, g;
5648
5649 ma = isl_multi_aff_align_divs(ma);
5650 if (!aff || !ma)
5651 goto error;
5652
5653 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5654 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5655 if (n_div_aff < 0 || n_div_ma < 0)
5656 goto error;
5657
5658 ls = isl_aff_get_domain_local_space(aff);
5659 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5660 res = isl_aff_alloc(ls);
5661 if (!res)
5662 goto error;
5663
5664 isl_int_init(f)isl_sioimath_init((f));
5665 isl_int_init(c1)isl_sioimath_init((c1));
5666 isl_int_init(c2)isl_sioimath_init((c2));
5667 isl_int_init(g)isl_sioimath_init((g));
5668
5669 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5670 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5671 res = isl_aff_free(res);
5672
5673 isl_int_clear(f)isl_sioimath_clear((f));
5674 isl_int_clear(c1)isl_sioimath_clear((c1));
5675 isl_int_clear(c2)isl_sioimath_clear((c2));
5676 isl_int_clear(g)isl_sioimath_clear((g));
5677
5678 isl_aff_free(aff);
5679 isl_multi_aff_free(ma);
5680 res = isl_aff_normalize(res);
5681 return res;
5682error:
5683 isl_aff_free(aff);
5684 isl_multi_aff_free(ma);
5685 isl_aff_free(res);
5686 return NULL((void*)0);
5687}
5688
5689/* Compute the pullback of "aff1" by the function represented by "aff2".
5690 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5691 * defined over the domain space of "aff1".
5692 *
5693 * The domain of "aff1" should match the range of "aff2", which means
5694 * that it should be single-dimensional.
5695 */
5696__isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5697 __isl_take isl_aff *aff2)
5698{
5699 isl_multi_aff *ma;
5700
5701 ma = isl_multi_aff_from_aff(aff2);
5702 return isl_aff_pullback_multi_aff(aff1, ma);
5703}
5704
5705/* Compute the pullback of "ma1" by the function represented by "ma2".
5706 * In other words, plug in "ma2" in "ma1".
5707 */
5708__isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5709 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5710{
5711 int i;
5712 isl_space *space = NULL((void*)0);
5713
5714 isl_multi_aff_align_params_bin(&ma1, &ma2);
5715 ma2 = isl_multi_aff_align_divs(ma2);
5716 ma1 = isl_multi_aff_cow(ma1);
5717 if (!ma1 || !ma2)
5718 goto error;
5719
5720 space = isl_space_join(isl_multi_aff_get_space(ma2),
5721 isl_multi_aff_get_space(ma1));
5722
5723 for (i = 0; i < ma1->n; ++i) {
5724 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5725 isl_multi_aff_copy(ma2));
5726 if (!ma1->u.p[i])
5727 goto error;
5728 }
5729
5730 ma1 = isl_multi_aff_reset_space(ma1, space);
5731 isl_multi_aff_free(ma2);
5732 return ma1;
5733error:
5734 isl_space_free(space);
5735 isl_multi_aff_free(ma2);
5736 isl_multi_aff_free(ma1);
5737 return NULL((void*)0);
5738}
5739
5740/* Extend the local space of "dst" to include the divs
5741 * in the local space of "src".
5742 *
5743 * If "src" does not have any divs or if the local spaces of "dst" and
5744 * "src" are the same, then no extension is required.
5745 */
5746__isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5747 __isl_keep isl_aff *src)
5748{
5749 isl_ctx *ctx;
5750 isl_size src_n_div, dst_n_div;
5751 int *exp1 = NULL((void*)0);
5752 int *exp2 = NULL((void*)0);
5753 isl_bool equal;
5754 isl_mat *div;
5755
5756 if (!src || !dst)
5757 return isl_aff_free(dst);
5758
5759 ctx = isl_aff_get_ctx(src);
5760 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5761 if (equal < 0)
5762 return isl_aff_free(dst);
5763 if (!equal)
5764 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "spaces don't match"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5765); goto error; } while (0)
5765 "spaces don't match", goto error)do { isl_handle_error(ctx, isl_error_invalid, "spaces don't match"
, "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 5765); goto error; } while (0)
;
5766
5767 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5768 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5769 if (src_n_div == 0)
5770 return dst;
5771 equal = isl_local_space_is_equal(src->ls, dst->ls);
5772 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5773 return isl_aff_free(dst);
5774 if (equal)
5775 return dst;
5776
5777 exp1 = isl_alloc_array(ctx, int, src_n_div)((int *)isl_malloc_or_die(ctx, (src_n_div)*sizeof(int)));
5778 exp2 = isl_alloc_array(ctx, int, dst_n_div)((int *)isl_malloc_or_die(ctx, (dst_n_div)*sizeof(int)));
5779 if (!exp1 || (dst_n_div && !exp2))
5780 goto error;
5781
5782 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5783 dst = isl_aff_expand_divs(dst, div, exp2);
5784 free(exp1);
5785 free(exp2);
5786
5787 return dst;
5788error:
5789 free(exp1);
5790 free(exp2);
5791 return isl_aff_free(dst);
5792}
5793
5794/* Adjust the local spaces of the affine expressions in "maff"
5795 * such that they all have the save divs.
5796 */
5797__isl_give isl_multi_aff *isl_multi_aff_align_divs(
5798 __isl_take isl_multi_aff *maff)
5799{
5800 int i;
5801
5802 if (!maff)
5803 return NULL((void*)0);
5804 if (maff->n == 0)
5805 return maff;
5806 maff = isl_multi_aff_cow(maff);
5807 if (!maff)
5808 return NULL((void*)0);
5809
5810 for (i = 1; i < maff->n; ++i)
5811 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5812 for (i = 1; i < maff->n; ++i) {
5813 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5814 if (!maff->u.p[i])
5815 return isl_multi_aff_free(maff);
5816 }
5817
5818 return maff;
5819}
5820
5821__isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5822{
5823 aff = isl_aff_cow(aff);
5824 if (!aff)
5825 return NULL((void*)0);
5826
5827 aff->ls = isl_local_space_lift(aff->ls);
5828 if (!aff->ls)
5829 return isl_aff_free(aff);
5830
5831 return aff;
5832}
5833
5834/* Lift "maff" to a space with extra dimensions such that the result
5835 * has no more existentially quantified variables.
5836 * If "ls" is not NULL, then *ls is assigned the local space that lies
5837 * at the basis of the lifting applied to "maff".
5838 */
5839__isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5840 __isl_give isl_local_space **ls)
5841{
5842 int i;
5843 isl_space *space;
5844 isl_size n_div;
5845
5846 if (ls)
5847 *ls = NULL((void*)0);
5848
5849 if (!maff)
5850 return NULL((void*)0);
5851
5852 if (maff->n == 0) {
5853 if (ls) {
5854 isl_space *space = isl_multi_aff_get_domain_space(maff);
5855 *ls = isl_local_space_from_space(space);
5856 if (!*ls)
5857 return isl_multi_aff_free(maff);
5858 }
5859 return maff;
5860 }
5861
5862 maff = isl_multi_aff_cow(maff);
5863 maff = isl_multi_aff_align_divs(maff);
5864 if (!maff)
5865 return NULL((void*)0);
5866
5867 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5868 if (n_div < 0)
5869 return isl_multi_aff_free(maff);
5870 space = isl_multi_aff_get_space(maff);
5871 space = isl_space_lift(isl_space_domain(space), n_div);
5872 space = isl_space_extend_domain_with_range(space,
5873 isl_multi_aff_get_space(maff));
5874 if (!space)
5875 return isl_multi_aff_free(maff);
5876 isl_space_free(maff->space);
5877 maff->space = space;
5878
5879 if (ls) {
5880 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5881 if (!*ls)
5882 return isl_multi_aff_free(maff);
5883 }
5884
5885 for (i = 0; i < maff->n; ++i) {
5886 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5887 if (!maff->u.p[i])
5888 goto error;
5889 }
5890
5891 return maff;
5892error:
5893 if (ls)
5894 isl_local_space_free(*ls);
5895 return isl_multi_aff_free(maff);
5896}
5897
5898#undef TYPEisl_multi_union_pw_aff
5899#define TYPEisl_multi_union_pw_aff isl_pw_multi_aff
5900static
5901#include "check_type_range_templ.c"
5902
5903/* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5904 */
5905__isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5906 __isl_keep isl_pw_multi_aff *pma, int pos)
5907{
5908 int i;
5909 isl_size n_out;
5910 isl_space *space;
5911 isl_pw_aff *pa;
5912
5913 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5914 return NULL((void*)0);
5915
5916 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5917 if (n_out < 0)
5918 return NULL((void*)0);
5919
5920 space = isl_pw_multi_aff_get_space(pma);
5921 space = isl_space_drop_dims(space, isl_dim_out,
5922 pos + 1, n_out - pos - 1);
5923 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5924
5925 pa = isl_pw_aff_alloc_size(space, pma->n);
5926 for (i = 0; i < pma->n; ++i) {
5927 isl_aff *aff;
5928 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5929 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5930 }
5931
5932 return pa;
5933}
5934
5935/* Return an isl_pw_multi_aff with the given "set" as domain and
5936 * an unnamed zero-dimensional range.
5937 */
5938__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5939 __isl_take isl_setisl_map *set)
5940{
5941 isl_multi_aff *ma;
5942 isl_space *space;
5943
5944 space = isl_set_get_space(set);
5945 space = isl_space_from_domain(space);
5946 ma = isl_multi_aff_zero(space);
5947 return isl_pw_multi_aff_alloc(set, ma);
5948}
5949
5950/* Add an isl_pw_multi_aff with the given "set" as domain and
5951 * an unnamed zero-dimensional range to *user.
5952 */
5953static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_setisl_map *set,
5954 void *user)
5955{
5956 isl_union_pw_multi_aff **upma = user;
5957 isl_pw_multi_aff *pma;
5958
5959 pma = isl_pw_multi_aff_from_domain(set);
5960 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5961
5962 return isl_stat_ok;
5963}
5964
5965/* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5966 * an unnamed zero-dimensional range.
5967 */
5968__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5969 __isl_take isl_union_setisl_union_map *uset)
5970{
5971 isl_space *space;
5972 isl_union_pw_multi_aff *upma;
5973
5974 if (!uset)
5975 return NULL((void*)0);
5976
5977 space = isl_union_set_get_space(uset);
5978 upma = isl_union_pw_multi_aff_empty(space);
5979
5980 if (isl_union_set_foreach_set(uset,
5981 &add_pw_multi_aff_from_domain, &upma) < 0)
5982 goto error;
5983
5984 isl_union_set_free(uset);
5985 return upma;
5986error:
5987 isl_union_set_free(uset);
5988 isl_union_pw_multi_aff_free(upma);
5989 return NULL((void*)0);
5990}
5991
5992/* Local data for bin_entry and the callback "fn".
5993 */
5994struct isl_union_pw_multi_aff_bin_data {
5995 isl_union_pw_multi_aff *upma2;
5996 isl_union_pw_multi_aff *res;
5997 isl_pw_multi_aff *pma;
5998 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5999};
6000
6001/* Given an isl_pw_multi_aff from upma1, store it in data->pma
6002 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6003 */
6004static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6005{
6006 struct isl_union_pw_multi_aff_bin_data *data = user;
6007 isl_stat r;
6008
6009 data->pma = pma;
6010 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6011 data->fn, data);
6012 isl_pw_multi_aff_free(pma);
6013
6014 return r;
6015}
6016
6017/* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6018 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6019 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6020 * as *entry. The callback should adjust data->res if desired.
6021 */
6022static __isl_give isl_union_pw_multi_aff *bin_op(
6023 __isl_take isl_union_pw_multi_aff *upma1,
6024 __isl_take isl_union_pw_multi_aff *upma2,
6025 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6026{
6027 isl_space *space;
6028 struct isl_union_pw_multi_aff_bin_data data = { NULL((void*)0), NULL((void*)0), NULL((void*)0), fn };
6029
6030 space = isl_union_pw_multi_aff_get_space(upma2);
6031 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6032 space = isl_union_pw_multi_aff_get_space(upma1);
6033 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6034
6035 if (!upma1 || !upma2)
6036 goto error;
6037
6038 data.upma2 = upma2;
6039 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6040 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6041 &bin_entry, &data) < 0)
6042 goto error;
6043
6044 isl_union_pw_multi_aff_free(upma1);
6045 isl_union_pw_multi_aff_free(upma2);
6046 return data.res;
6047error:
6048 isl_union_pw_multi_aff_free(upma1);
6049 isl_union_pw_multi_aff_free(upma2);
6050 isl_union_pw_multi_aff_free(data.res);
6051 return NULL((void*)0);
6052}
6053
6054/* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6055 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6056 */
6057__isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6058 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6059{
6060 isl_space *space;
6061
6062 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6063 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6064 isl_pw_multi_aff_get_space(pma2));
6065 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6066 &isl_multi_aff_range_product);
6067}
6068
6069/* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6070 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6071 */
6072__isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6073 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6074{
6075 isl_space *space;
6076
6077 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6078 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6079 isl_pw_multi_aff_get_space(pma2));
6080 space = isl_space_flatten_range(space);
6081 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6082 &isl_multi_aff_flat_range_product);
6083}
6084
6085/* If data->pma and "pma2" have the same domain space, then compute
6086 * their flat range product and the result to data->res.
6087 */
6088static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6089 void *user)
6090{
6091 struct isl_union_pw_multi_aff_bin_data *data = user;
6092
6093 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6094 pma2->dim, isl_dim_in)) {
6095 isl_pw_multi_aff_free(pma2);
6096 return isl_stat_ok;
6097 }
6098
6099 pma2 = isl_pw_multi_aff_flat_range_product(
6100 isl_pw_multi_aff_copy(data->pma), pma2);
6101
6102 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6103
6104 return isl_stat_ok;
6105}
6106
6107/* Given two isl_union_pw_multi_affs A -> B and C -> D,
6108 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6109 */
6110__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6111 __isl_take isl_union_pw_multi_aff *upma1,
6112 __isl_take isl_union_pw_multi_aff *upma2)
6113{
6114 return bin_op(upma1, upma2, &flat_range_product_entry);
6115}
6116
6117/* Replace the affine expressions at position "pos" in "pma" by "pa".
6118 * The parameters are assumed to have been aligned.
6119 *
6120 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6121 * except that it works on two different isl_pw_* types.
6122 */
6123static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6124 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6125 __isl_take isl_pw_aff *pa)
6126{
6127 int i, j, n;
6128 isl_pw_multi_aff *res = NULL((void*)0);
6129
6130 if (!pma || !pa)
6131 goto error;
6132
6133 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6134 pa->dim, isl_dim_in))
6135 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6136); goto error; } while (0)
6136 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6136); goto error; } while (0)
;
6137 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6138 goto error;
6139
6140 n = pma->n * pa->n;
6141 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6142
6143 for (i = 0; i < pma->n; ++i) {
6144 for (j = 0; j < pa->n; ++j) {
6145 isl_setisl_map *common;
6146 isl_multi_aff *res_ij;
6147 int empty;
6148
6149 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6150 isl_set_copy(pa->p[j].set));
6151 empty = isl_set_plain_is_empty(common);
6152 if (empty < 0 || empty) {
6153 isl_set_free(common);
6154 if (empty < 0)
6155 goto error;
6156 continue;
6157 }
6158
6159 res_ij = isl_multi_aff_set_aff(
6160 isl_multi_aff_copy(pma->p[i].maff), pos,
6161 isl_aff_copy(pa->p[j].aff));
6162 res_ij = isl_multi_aff_gist(res_ij,
6163 isl_set_copy(common));
6164
6165 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6166 }
6167 }
6168
6169 isl_pw_multi_aff_free(pma);
6170 isl_pw_aff_free(pa);
6171 return res;
6172error:
6173 isl_pw_multi_aff_free(pma);
6174 isl_pw_aff_free(pa);
6175 return isl_pw_multi_aff_free(res);
6176}
6177
6178/* Replace the affine expressions at position "pos" in "pma" by "pa".
6179 */
6180__isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6181 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6182 __isl_take isl_pw_aff *pa)
6183{
6184 isl_bool equal_params;
6185
6186 if (!pma || !pa)
6187 goto error;
6188 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6189 if (equal_params < 0)
6190 goto error;
6191 if (equal_params)
6192 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6193 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6194 isl_pw_aff_check_named_params(pa) < 0)
6195 goto error;
6196 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6197 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6198 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6199error:
6200 isl_pw_multi_aff_free(pma);
6201 isl_pw_aff_free(pa);
6202 return NULL((void*)0);
6203}
6204
6205/* Do the parameters of "pa" match those of "space"?
6206 */
6207isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6208 __isl_keep isl_space *space)
6209{
6210 isl_space *pa_space;
6211 isl_bool match;
6212
6213 if (!pa || !space)
6214 return isl_bool_error;
6215
6216 pa_space = isl_pw_aff_get_space(pa);
6217
6218 match = isl_space_has_equal_params(space, pa_space);
6219
6220 isl_space_free(pa_space);
6221 return match;
6222}
6223
6224/* Check that the domain space of "pa" matches "space".
6225 */
6226isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6227 __isl_keep isl_space *space)
6228{
6229 isl_space *pa_space;
6230 isl_bool match;
6231
6232 if (!pa || !space)
6233 return isl_stat_error;
6234
6235 pa_space = isl_pw_aff_get_space(pa);
6236
6237 match = isl_space_has_equal_params(space, pa_space);
6238 if (match < 0)
6239 goto error;
6240 if (!match)
6241 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6242); goto error; } while (0)
6242 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6242); goto error; } while (0)
;
6243 match = isl_space_tuple_is_equal(space, isl_dim_in,
6244 pa_space, isl_dim_in);
6245 if (match < 0)
6246 goto error;
6247 if (!match)
6248 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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6249); goto error; } while (0)
6249 "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-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6249); goto error; } while (0)
;
6250 isl_space_free(pa_space);
6251 return isl_stat_ok;
6252error:
6253 isl_space_free(pa_space);
6254 return isl_stat_error;
6255}
6256
6257#undef BASEunion_pw_aff
6258#define BASEunion_pw_aff pw_aff
6259#undef DOMBASEunion_set
6260#define DOMBASEunion_set set
6261
6262#include <isl_multi_explicit_domain.c>
6263#include <isl_multi_pw_aff_explicit_domain.c>
6264#include <isl_multi_templ.c>
6265#include <isl_multi_add_constant_templ.c>
6266#include <isl_multi_apply_set.c>
6267#include <isl_multi_arith_templ.c>
6268#include <isl_multi_bind_templ.c>
6269#include <isl_multi_bind_domain_templ.c>
6270#include <isl_multi_coalesce.c>
6271#include <isl_multi_domain_templ.c>
6272#include <isl_multi_dim_id_templ.c>
6273#include <isl_multi_dims.c>
6274#include <isl_multi_from_base_templ.c>
6275#include <isl_multi_gist.c>
6276#include <isl_multi_hash.c>
6277#include <isl_multi_identity_templ.c>
6278#include <isl_multi_align_set.c>
6279#include <isl_multi_intersect.c>
6280#include <isl_multi_move_dims_templ.c>
6281#include <isl_multi_nan_templ.c>
6282#include <isl_multi_param_templ.c>
6283#include <isl_multi_product_templ.c>
6284#include <isl_multi_splice_templ.c>
6285#include <isl_multi_tuple_id_templ.c>
6286#include <isl_multi_zero_templ.c>
6287
6288/* Are all elements of "mpa" piecewise constants?
6289 */
6290isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6291{
6292 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6293}
6294
6295/* Does "mpa" have a non-trivial explicit domain?
6296 *
6297 * The explicit domain, if present, is trivial if it represents
6298 * an (obviously) universe set.
6299 */
6300isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6301 __isl_keep isl_multi_pw_aff *mpa)
6302{
6303 if (!mpa)
6304 return isl_bool_error;
6305 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6306 return isl_bool_false;
6307 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6308}
6309
6310/* Scale the elements of "pma" by the corresponding elements of "mv".
6311 */
6312__isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6313 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6314{
6315 int i;
6316 isl_bool equal_params;
6317
6318 pma = isl_pw_multi_aff_cow(pma);
6319 if (!pma || !mv)
6320 goto error;
6321 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6322 mv->space, isl_dim_set))
6323 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,do { isl_handle_error(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6324); goto error; } while (0)
6324 "spaces don't match", goto error)do { isl_handle_error(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_aff.c"
, 6324); goto error; } while (0)
;
6325 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6326 if (equal_params < 0)
6327 goto error;
6328 if (!equal_params) {
6329 pma = isl_pw_multi_aff_align_params(pma,
6330 isl_multi_val_get_space(mv));
6331 mv = isl_multi_val_align_params(mv,
6332 isl_pw_multi_aff_get_space(pma));
6333 if (!pma || !mv)
6334 goto error;
6335 }
6336
6337 for (i = 0; i < pma->n; ++i) {
6338 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6339 isl_multi_val_copy(mv));
6340 if (!pma->p[i].maff)
6341 goto error;
6342 }
6343
6344 isl_multi_val_free(mv);
6345 return pma;
6346error:
6347 isl_multi_val_free(mv);
6348 isl_pw_multi_aff_free(pma);
6349 return NULL((void*)0);
6350}
6351
6352/* This function is called for each entry of an isl_union_pw_multi_aff.
6353 * If the space of the entry matches that of data->mv,
6354 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6355 * Otherwise, return an empty isl_pw_multi_aff.
6356 */
6357static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6358 __isl_take isl_pw_multi_aff *pma, void *user)
6359{
6360 isl_multi_val *mv = user;
6361
6362 if (!pma)
6363 return NULL((void*)0);
6364 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6365 mv->space, isl_dim_set)) {
6366 isl_space *space = isl_pw_multi_aff_get_space(pma);
6367 isl_pw_multi_aff_free(pma);
6368 return isl_pw_multi_aff_empty(space);
6369 }
6370
6371 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6372}
6373
6374/* Scale the elements of "upma" by the corresponding elements of "mv",
6375 * for those entries that match the space of "mv".
6376 */
6377__isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6378 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6379{
6380 struct isl_union_pw_multi_aff_transform_control control = {
6381 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6382 .fn_user = mv,
6383 };
6384
6385 upma = isl_union_pw_multi_aff_align_params(upma,
6386 isl_multi_val_get_space(mv));
6387 mv = isl_multi_val_align_params(mv,
6388 isl_union_pw_multi_aff_get_space(upma));
6389 if (!upma || !mv)
6390 goto error;
6391
6392 return isl_union_pw_multi_aff_transform(upma, &control);
6393
6394 isl_multi_val_free(mv);
6395 return upma;
6396error:
6397 isl_multi_val_free(mv);
6398 isl_union_pw_multi_aff_free(upma);
6399 return NULL((void*)0);
6400}
6401
6402/* Construct and return a piecewise multi affine expression
6403 * in the given space with value zero in each of the output dimensions and
6404 * a universe domain.
6405 */
6406__isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6407{
6408 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6409}
6410
6411/* Construct and return a piecewise multi affine expression
6412 * that is equal to the given piecewise affine expression.
6413 */
6414__isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6415 __isl_take isl_pw_aff *pa)
6416{
6417 int i;
6418 isl_space *space;
6419 isl_pw_multi_aff *pma;
6420
6421 if (!pa)
6422 return NULL((void*)0);