Bug Summary

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

Annotated Source Code

Press '?' to see keyboard shortcuts

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

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

1/*
2 * Copyright 2010 INRIA Saclay
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
10
11#define ISL_DIM_H
12#include <isl_map_private.h>
13#include <isl_union_map_private.h>
14#include <isl_polynomial_private.h>
15#include <isl_point_private.h>
16#include <isl_space_private.h>
17#include <isl_lp_private.h>
18#include <isl_seq.h>
19#include <isl_mat_private.h>
20#include <isl_val_private.h>
21#include <isl_vec_private.h>
22#include <isl_config.h>
23
24enum isl_fold isl_fold_type_negate(enum isl_fold type)
25{
26 switch (type) {
27 case isl_fold_min:
28 return isl_fold_max;
29 case isl_fold_max:
30 return isl_fold_min;
31 case isl_fold_list:
32 return isl_fold_list;
33 }
34
35 isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort())do { isl_handle_error(((void*)0), isl_error_internal, "unhandled isl_fold type"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 35); abort(); } while (0)
;
36}
37
38static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
39 enum isl_fold type, __isl_take isl_space *dim, int n)
40{
41 isl_qpolynomial_fold *fold;
42
43 if (!dim)
44 goto error;
45
46 isl_assert(dim->ctx, n >= 0, goto error)do { if (n >= 0) break; do { isl_handle_error(dim->ctx,
isl_error_unknown, "Assertion \"" "n >= 0" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 46); goto error; } while (0); } while (0)
;
47 fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,((struct isl_qpolynomial_fold *)isl_calloc_or_die(dim->ctx
, 1, sizeof(struct isl_qpolynomial_fold) + (n - 1) * sizeof(struct
isl_qpolynomial *)))
48 sizeof(struct isl_qpolynomial_fold) +((struct isl_qpolynomial_fold *)isl_calloc_or_die(dim->ctx
, 1, sizeof(struct isl_qpolynomial_fold) + (n - 1) * sizeof(struct
isl_qpolynomial *)))
49 (n - 1) * sizeof(struct isl_qpolynomial *))((struct isl_qpolynomial_fold *)isl_calloc_or_die(dim->ctx
, 1, sizeof(struct isl_qpolynomial_fold) + (n - 1) * sizeof(struct
isl_qpolynomial *)))
;
50 if (!fold)
51 goto error;
52
53 fold->ref = 1;
54 fold->size = n;
55 fold->n = 0;
56 fold->type = type;
57 fold->dim = dim;
58
59 return fold;
60error:
61 isl_space_free(dim);
62 return NULL((void*)0);
63}
64
65isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
66{
67 return fold ? fold->dim->ctx : NULL((void*)0);
68}
69
70__isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
71 __isl_keep isl_qpolynomial_fold *fold)
72{
73 return fold ? isl_space_copy(fold->dim) : NULL((void*)0);
74}
75
76__isl_give isl_space *isl_qpolynomial_fold_get_space(
77 __isl_keep isl_qpolynomial_fold *fold)
78{
79 isl_space *space;
80 if (!fold)
81 return NULL((void*)0);
82 space = isl_space_copy(fold->dim);
83 space = isl_space_from_domain(space);
84 space = isl_space_add_dims(space, isl_dim_out, 1);
85 return space;
86}
87
88__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
89 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
90{
91 int i;
92
93 fold = isl_qpolynomial_fold_cow(fold);
94 if (!fold || !dim)
95 goto error;
96
97 for (i = 0; i < fold->n; ++i) {
98 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
99 isl_space_copy(dim));
100 if (!fold->qp[i])
101 goto error;
102 }
103
104 isl_space_free(fold->dim);
105 fold->dim = dim;
106
107 return fold;
108error:
109 isl_qpolynomial_fold_free(fold);
110 isl_space_free(dim);
111 return NULL((void*)0);
112}
113
114/* Reset the space of "fold". This function is called from isl_pw_templ.c
115 * and doesn't know if the space of an element object is represented
116 * directly or through its domain. It therefore passes along both.
117 */
118__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_space_and_domain(
119 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *space,
120 __isl_take isl_space *domain)
121{
122 isl_space_free(space);
123 return isl_qpolynomial_fold_reset_domain_space(fold, domain);
124}
125
126int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
127 enum isl_dim_type type, unsigned first, unsigned n)
128{
129 int i;
130
131 if (!fold)
132 return -1;
133 if (fold->n == 0 || n == 0)
134 return 0;
135
136 for (i = 0; i < fold->n; ++i) {
137 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
138 type, first, n);
139 if (involves < 0 || involves)
140 return involves;
141 }
142 return 0;
143}
144
145__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
146 __isl_take isl_qpolynomial_fold *fold,
147 enum isl_dim_type type, unsigned pos, const char *s)
148{
149 int i;
150
151 fold = isl_qpolynomial_fold_cow(fold);
152 if (!fold)
153 return NULL((void*)0);
154 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
155 if (!fold->dim)
156 goto error;
157
158 for (i = 0; i < fold->n; ++i) {
159 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
160 type, pos, s);
161 if (!fold->qp[i])
162 goto error;
163 }
164
165 return fold;
166error:
167 isl_qpolynomial_fold_free(fold);
168 return NULL((void*)0);
169}
170
171/* Given a dimension type for an isl_qpolynomial_fold,
172 * return the corresponding type for the domain.
173 */
174static enum isl_dim_type domain_type(enum isl_dim_type type)
175{
176 if (type == isl_dim_in)
177 return isl_dim_set;
178 return type;
179}
180
181__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
182 __isl_take isl_qpolynomial_fold *fold,
183 enum isl_dim_type type, unsigned first, unsigned n)
184{
185 int i;
186 enum isl_dim_type set_type;
187
188 if (!fold)
189 return NULL((void*)0);
190 if (n == 0)
191 return fold;
192
193 set_type = domain_type(type);
194
195 fold = isl_qpolynomial_fold_cow(fold);
196 if (!fold)
197 return NULL((void*)0);
198 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
199 if (!fold->dim)
200 goto error;
201
202 for (i = 0; i < fold->n; ++i) {
203 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
204 type, first, n);
205 if (!fold->qp[i])
206 goto error;
207 }
208
209 return fold;
210error:
211 isl_qpolynomial_fold_free(fold);
212 return NULL((void*)0);
213}
214
215__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
216 __isl_take isl_qpolynomial_fold *fold,
217 enum isl_dim_type type, unsigned first, unsigned n)
218{
219 int i;
220
221 if (!fold)
222 return NULL((void*)0);
223 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
224 return fold;
225
226 fold = isl_qpolynomial_fold_cow(fold);
227 if (!fold)
228 return NULL((void*)0);
229 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
230 if (!fold->dim)
231 goto error;
232
233 for (i = 0; i < fold->n; ++i) {
234 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
235 type, first, n);
236 if (!fold->qp[i])
237 goto error;
238 }
239
240 return fold;
241error:
242 isl_qpolynomial_fold_free(fold);
243 return NULL((void*)0);
244}
245
246/* Determine the sign of the constant quasipolynomial "qp".
247 *
248 * Return
249 * -1 if qp <= 0
250 * 1 if qp >= 0
251 * 0 if unknown
252 *
253 * For qp == 0, we can return either -1 or 1. In practice, we return 1.
254 * For qp == NaN, the sign is undefined, so we return 0.
255 */
256static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
257{
258 struct isl_upoly_cst *cst;
259
260 if (isl_qpolynomial_is_nan(qp))
261 return 0;
262
263 cst = isl_upoly_as_cst(qp->upoly);
264 if (!cst)
265 return 0;
266
267 return isl_int_sgn(cst->n)isl_sioimath_sgn(*(cst->n)) < 0 ? -1 : 1;
268}
269
270static int isl_qpolynomial_aff_sign(__isl_keep isl_setisl_map *set,
271 __isl_keep isl_qpolynomial *qp)
272{
273 enum isl_lp_result res;
274 isl_vec *aff;
275 isl_int opt;
276 int sgn = 0;
277
278 aff = isl_qpolynomial_extract_affine(qp);
279 if (!aff)
280 return 0;
281
282 isl_int_init(opt)isl_sioimath_init((opt));
283
284 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
285 &opt, NULL((void*)0), NULL((void*)0));
286 if (res == isl_lp_error)
287 goto done;
288 if (res == isl_lp_empty ||
289 (res == isl_lp_ok && !isl_int_is_neg(opt)(isl_sioimath_sgn(*(opt)) < 0))) {
290 sgn = 1;
291 goto done;
292 }
293
294 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
295 &opt, NULL((void*)0), NULL((void*)0));
296 if (res == isl_lp_ok && !isl_int_is_pos(opt)(isl_sioimath_sgn(*(opt)) > 0))
297 sgn = -1;
298
299done:
300 isl_int_clear(opt)isl_sioimath_clear((opt));
301 isl_vec_free(aff);
302 return sgn;
303}
304
305/* Determine, if possible, the sign of the quasipolynomial "qp" on
306 * the domain "set".
307 *
308 * If qp is a constant, then the problem is trivial.
309 * If qp is linear, then we check if the minimum of the corresponding
310 * affine constraint is non-negative or if the maximum is non-positive.
311 *
312 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
313 * in "set". If so, we write qp(v,v') as
314 *
315 * q(v,v') * (v - l) + r(v')
316 *
317 * if q(v,v') and r(v') have the same known sign, then the original
318 * quasipolynomial has the same sign as well.
319 *
320 * Return
321 * -1 if qp <= 0
322 * 1 if qp >= 0
323 * 0 if unknown
324 */
325static int isl_qpolynomial_sign(__isl_keep isl_setisl_map *set,
326 __isl_keep isl_qpolynomial *qp)
327{
328 int d;
329 int i;
330 int is;
331 struct isl_upoly_rec *rec;
332 isl_vec *v;
333 isl_int l;
334 enum isl_lp_result res;
335 int sgn = 0;
336
337 is = isl_qpolynomial_is_cst(qp, NULL((void*)0), NULL((void*)0));
338 if (is < 0)
339 return 0;
340 if (is)
341 return isl_qpolynomial_cst_sign(qp);
342
343 is = isl_qpolynomial_is_affine(qp);
344 if (is < 0)
345 return 0;
346 if (is)
347 return isl_qpolynomial_aff_sign(set, qp);
348
349 if (qp->div->n_row > 0)
350 return 0;
351
352 rec = isl_upoly_as_rec(qp->upoly);
353 if (!rec)
354 return 0;
355
356 d = isl_space_dim(qp->dim, isl_dim_all);
357 v = isl_vec_alloc(set->ctx, 2 + d);
358 if (!v)
359 return 0;
360
361 isl_seq_clr(v->el + 1, 1 + d);
362 isl_int_set_si(v->el[0], 1)isl_sioimath_set_si((v->el[0]), 1);
363 isl_int_set_si(v->el[2 + qp->upoly->var], 1)isl_sioimath_set_si((v->el[2 + qp->upoly->var]), 1);
364
365 isl_int_init(l)isl_sioimath_init((l));
366
367 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL((void*)0), NULL((void*)0));
368 if (res == isl_lp_ok) {
369 isl_qpolynomial *min;
370 isl_qpolynomial *base;
371 isl_qpolynomial *r, *q;
372 isl_qpolynomial *t;
373
374 min = isl_qpolynomial_cst_on_domain(isl_space_copy(qp->dim), l);
375 base = isl_qpolynomial_var_pow_on_domain(isl_space_copy(qp->dim),
376 qp->upoly->var, 1);
377
378 r = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
379 isl_upoly_copy(rec->p[rec->n - 1]));
380 q = isl_qpolynomial_copy(r);
381
382 for (i = rec->n - 2; i >= 0; --i) {
383 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
384 t = isl_qpolynomial_alloc(isl_space_copy(qp->dim), 0,
385 isl_upoly_copy(rec->p[i]));
386 r = isl_qpolynomial_add(r, t);
387 if (i == 0)
388 break;
389 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
390 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
391 }
392
393 if (isl_qpolynomial_is_zero(q))
394 sgn = isl_qpolynomial_sign(set, r);
395 else if (isl_qpolynomial_is_zero(r))
396 sgn = isl_qpolynomial_sign(set, q);
397 else {
398 int sgn_q, sgn_r;
399 sgn_r = isl_qpolynomial_sign(set, r);
400 sgn_q = isl_qpolynomial_sign(set, q);
401 if (sgn_r == sgn_q)
402 sgn = sgn_r;
403 }
404
405 isl_qpolynomial_free(min);
406 isl_qpolynomial_free(base);
407 isl_qpolynomial_free(q);
408 isl_qpolynomial_free(r);
409 }
410
411 isl_int_clear(l)isl_sioimath_clear((l));
412
413 isl_vec_free(v);
414
415 return sgn;
416}
417
418/* Combine "fold1" and "fold2" into a single reduction, eliminating
419 * those elements of one reduction that are already covered by the other
420 * reduction on "set".
421 *
422 * If "fold1" or "fold2" is an empty reduction, then return
423 * the other reduction.
424 * If "fold1" or "fold2" is a NaN, then return this NaN.
425 */
426__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
427 __isl_keep isl_setisl_map *set,
428 __isl_take isl_qpolynomial_fold *fold1,
429 __isl_take isl_qpolynomial_fold *fold2)
430{
431 int i, j;
432 int n1;
433 struct isl_qpolynomial_fold *res = NULL((void*)0);
434 int better;
435
436 if (!fold1 || !fold2)
437 goto error;
438
439 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error)do { if (fold1->type == fold2->type) break; do { isl_handle_error
(fold1->dim->ctx, isl_error_unknown, "Assertion \"" "fold1->type == fold2->type"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 439); goto error; } while (0); } while (0)
;
440 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),do { if (isl_space_is_equal(fold1->dim, fold2->dim)) break
; do { isl_handle_error(fold1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(fold1->dim, fold2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 441); goto error; } while (0); } while (0)
441 goto error)do { if (isl_space_is_equal(fold1->dim, fold2->dim)) break
; do { isl_handle_error(fold1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(fold1->dim, fold2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 441); goto error; } while (0); } while (0)
;
442
443 better = fold1->type == isl_fold_max ? -1 : 1;
444
445 if (isl_qpolynomial_fold_is_empty(fold1) ||
446 isl_qpolynomial_fold_is_nan(fold2)) {
447 isl_qpolynomial_fold_free(fold1);
448 return fold2;
449 }
450
451 if (isl_qpolynomial_fold_is_empty(fold2) ||
452 isl_qpolynomial_fold_is_nan(fold1)) {
453 isl_qpolynomial_fold_free(fold2);
454 return fold1;
455 }
456
457 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
458 fold1->n + fold2->n);
459 if (!res)
460 goto error;
461
462 for (i = 0; i < fold1->n; ++i) {
463 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
464 if (!res->qp[res->n])
465 goto error;
466 res->n++;
467 }
468 n1 = res->n;
469
470 for (i = 0; i < fold2->n; ++i) {
471 for (j = n1 - 1; j >= 0; --j) {
472 isl_qpolynomial *d;
473 int sgn, equal;
474 equal = isl_qpolynomial_plain_is_equal(res->qp[j],
475 fold2->qp[i]);
476 if (equal < 0)
477 goto error;
478 if (equal)
479 break;
480 d = isl_qpolynomial_sub(
481 isl_qpolynomial_copy(res->qp[j]),
482 isl_qpolynomial_copy(fold2->qp[i]));
483 sgn = isl_qpolynomial_sign(set, d);
484 isl_qpolynomial_free(d);
485 if (sgn == 0)
486 continue;
487 if (sgn != better)
488 break;
489 isl_qpolynomial_free(res->qp[j]);
490 if (j != n1 - 1)
491 res->qp[j] = res->qp[n1 - 1];
492 n1--;
493 if (n1 != res->n - 1)
494 res->qp[n1] = res->qp[res->n - 1];
495 res->n--;
496 }
497 if (j >= 0)
498 continue;
499 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
500 if (!res->qp[res->n])
501 goto error;
502 res->n++;
503 }
504
505 isl_qpolynomial_fold_free(fold1);
506 isl_qpolynomial_fold_free(fold2);
507
508 return res;
509error:
510 isl_qpolynomial_fold_free(res);
511 isl_qpolynomial_fold_free(fold1);
512 isl_qpolynomial_fold_free(fold2);
513 return NULL((void*)0);
514}
515
516__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
517 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
518{
519 int i;
520
521 if (!fold || !qp)
522 goto error;
523
524 if (isl_qpolynomial_is_zero(qp)) {
525 isl_qpolynomial_free(qp);
526 return fold;
527 }
528
529 fold = isl_qpolynomial_fold_cow(fold);
530 if (!fold)
531 goto error;
532
533 for (i = 0; i < fold->n; ++i) {
534 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
535 isl_qpolynomial_copy(qp));
536 if (!fold->qp[i])
537 goto error;
538 }
539
540 isl_qpolynomial_free(qp);
541 return fold;
542error:
543 isl_qpolynomial_fold_free(fold);
544 isl_qpolynomial_free(qp);
545 return NULL((void*)0);
546}
547
548__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
549 __isl_keep isl_setisl_map *dom,
550 __isl_take isl_qpolynomial_fold *fold1,
551 __isl_take isl_qpolynomial_fold *fold2)
552{
553 int i;
554 isl_qpolynomial_fold *res = NULL((void*)0);
555
556 if (!fold1 || !fold2)
557 goto error;
558
559 if (isl_qpolynomial_fold_is_empty(fold1)) {
560 isl_qpolynomial_fold_free(fold1);
561 return fold2;
562 }
563
564 if (isl_qpolynomial_fold_is_empty(fold2)) {
565 isl_qpolynomial_fold_free(fold2);
566 return fold1;
567 }
568
569 if (fold1->n == 1 && fold2->n != 1)
570 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
571
572 if (fold2->n == 1) {
573 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
574 isl_qpolynomial_copy(fold2->qp[0]));
575 isl_qpolynomial_fold_free(fold2);
576 return res;
577 }
578
579 res = isl_qpolynomial_fold_add_qpolynomial(
580 isl_qpolynomial_fold_copy(fold1),
581 isl_qpolynomial_copy(fold2->qp[0]));
582
583 for (i = 1; i < fold2->n; ++i) {
584 isl_qpolynomial_fold *res_i;
585 res_i = isl_qpolynomial_fold_add_qpolynomial(
586 isl_qpolynomial_fold_copy(fold1),
587 isl_qpolynomial_copy(fold2->qp[i]));
588 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
589 }
590
591 isl_qpolynomial_fold_free(fold1);
592 isl_qpolynomial_fold_free(fold2);
593 return res;
594error:
595 isl_qpolynomial_fold_free(res);
596 isl_qpolynomial_fold_free(fold1);
597 isl_qpolynomial_fold_free(fold2);
598 return NULL((void*)0);
599}
600
601__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
602 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_setisl_basic_map *eq)
603{
604 int i;
605
606 if (!fold || !eq)
607 goto error;
608
609 fold = isl_qpolynomial_fold_cow(fold);
610 if (!fold)
611 return NULL((void*)0);
612
613 for (i = 0; i < fold->n; ++i) {
614 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
615 isl_basic_set_copy(eq));
616 if (!fold->qp[i])
617 goto error;
618 }
619
620 isl_basic_set_free(eq);
621 return fold;
622error:
623 isl_basic_set_free(eq);
624 isl_qpolynomial_fold_free(fold);
625 return NULL((void*)0);
626}
627
628__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
629 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_setisl_map *context)
630{
631 int i;
632
633 if (!fold || !context)
634 goto error;
635
636 fold = isl_qpolynomial_fold_cow(fold);
637 if (!fold)
638 return NULL((void*)0);
639
640 for (i = 0; i < fold->n; ++i) {
641 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
642 isl_set_copy(context));
643 if (!fold->qp[i])
644 goto error;
645 }
646
647 isl_set_free(context);
648 return fold;
649error:
650 isl_set_free(context);
651 isl_qpolynomial_fold_free(fold);
652 return NULL((void*)0);
653}
654
655__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
656 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_setisl_map *context)
657{
658 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
659 isl_setisl_map *dom_context = isl_set_universe(space);
660 dom_context = isl_set_intersect_params(dom_context, context);
661 return isl_qpolynomial_fold_gist(fold, dom_context);
662}
663
664#define isl_qpolynomial_fold_involves_nanisl_qpolynomial_fold_is_nan isl_qpolynomial_fold_is_nan
665
666#define HAS_TYPE
667
668#undef PWisl_pw_qpolynomial_fold
669#define PWisl_pw_qpolynomial_fold isl_pw_qpolynomial_fold
670#undef ELisl_qpolynomial_fold
671#define ELisl_qpolynomial_fold isl_qpolynomial_fold
672#undef EL_IS_ZEROis_empty
673#define EL_IS_ZEROis_empty is_empty
674#undef ZEROzero
675#define ZEROzero zero
676#undef IS_ZEROis_zero
677#define IS_ZEROis_zero is_zero
678#undef FIELDfold
679#define FIELDfold fold
680#undef DEFAULT_IS_ZERO1
681#define DEFAULT_IS_ZERO1 1
682
683#define NO_NEG
684#define NO_SUB
685#define NO_PULLBACK
686
687#include <isl_pw_templ.c>
688
689#undef UNIONisl_union_pw_qpolynomial_fold
690#define UNIONisl_union_pw_qpolynomial_fold isl_union_pw_qpolynomial_fold
691#undef PARTisl_pw_qpolynomial_fold
692#define PARTisl_pw_qpolynomial_fold isl_pw_qpolynomial_fold
693#undef PARTSpw_qpolynomial_fold
694#define PARTSpw_qpolynomial_fold pw_qpolynomial_fold
695
696#define NO_SUB
697
698#include <isl_union_single.c>
699#include <isl_union_eval.c>
700
701__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
702 __isl_take isl_space *dim)
703{
704 return qpolynomial_fold_alloc(type, dim, 0);
705}
706
707__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
708 enum isl_fold type, __isl_take isl_qpolynomial *qp)
709{
710 isl_qpolynomial_fold *fold;
711
712 if (!qp)
713 return NULL((void*)0);
714
715 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
716 if (!fold)
717 goto error;
718
719 fold->qp[0] = qp;
720 fold->n++;
721
722 return fold;
723error:
724 isl_qpolynomial_fold_free(fold);
725 isl_qpolynomial_free(qp);
726 return NULL((void*)0);
727}
728
729__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
730 __isl_keep isl_qpolynomial_fold *fold)
731{
732 if (!fold)
733 return NULL((void*)0);
734
735 fold->ref++;
736 return fold;
737}
738
739__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
740 __isl_keep isl_qpolynomial_fold *fold)
741{
742 int i;
743 isl_qpolynomial_fold *dup;
744
745 if (!fold)
746 return NULL((void*)0);
747 dup = qpolynomial_fold_alloc(fold->type,
748 isl_space_copy(fold->dim), fold->n);
749 if (!dup)
750 return NULL((void*)0);
751
752 dup->n = fold->n;
753 for (i = 0; i < fold->n; ++i) {
754 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
755 if (!dup->qp[i])
756 goto error;
757 }
758
759 return dup;
760error:
761 isl_qpolynomial_fold_free(dup);
762 return NULL((void*)0);
763}
764
765__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
766 __isl_take isl_qpolynomial_fold *fold)
767{
768 if (!fold)
769 return NULL((void*)0);
770
771 if (fold->ref == 1)
772 return fold;
773 fold->ref--;
774 return isl_qpolynomial_fold_dup(fold);
775}
776
777void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
778{
779 int i;
780
781 if (!fold)
782 return;
783 if (--fold->ref > 0)
784 return;
785
786 for (i = 0; i < fold->n; ++i)
787 isl_qpolynomial_free(fold->qp[i]);
788 isl_space_free(fold->dim);
789 free(fold);
790}
791
792int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
793{
794 if (!fold)
795 return -1;
796
797 return fold->n == 0;
798}
799
800/* Does "fold" represent max(NaN) or min(NaN)?
801 */
802isl_bool isl_qpolynomial_fold_is_nan(__isl_keep isl_qpolynomial_fold *fold)
803{
804 if (!fold)
805 return isl_bool_error;
806 if (fold->n != 1)
807 return isl_bool_false;
808 return isl_qpolynomial_is_nan(fold->qp[0]);
809}
810
811__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
812 __isl_take isl_qpolynomial_fold *fold1,
813 __isl_take isl_qpolynomial_fold *fold2)
814{
815 int i;
816 struct isl_qpolynomial_fold *res = NULL((void*)0);
817
818 if (!fold1 || !fold2)
819 goto error;
820
821 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error)do { if (fold1->type == fold2->type) break; do { isl_handle_error
(fold1->dim->ctx, isl_error_unknown, "Assertion \"" "fold1->type == fold2->type"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 821); goto error; } while (0); } while (0)
;
822 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),do { if (isl_space_is_equal(fold1->dim, fold2->dim)) break
; do { isl_handle_error(fold1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(fold1->dim, fold2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 823); goto error; } while (0); } while (0)
823 goto error)do { if (isl_space_is_equal(fold1->dim, fold2->dim)) break
; do { isl_handle_error(fold1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(fold1->dim, fold2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 823); goto error; } while (0); } while (0)
;
824
825 if (isl_qpolynomial_fold_is_empty(fold1)) {
826 isl_qpolynomial_fold_free(fold1);
827 return fold2;
828 }
829
830 if (isl_qpolynomial_fold_is_empty(fold2)) {
831 isl_qpolynomial_fold_free(fold2);
832 return fold1;
833 }
834
835 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
836 fold1->n + fold2->n);
837 if (!res)
838 goto error;
839
840 for (i = 0; i < fold1->n; ++i) {
841 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
842 if (!res->qp[res->n])
843 goto error;
844 res->n++;
845 }
846
847 for (i = 0; i < fold2->n; ++i) {
848 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
849 if (!res->qp[res->n])
850 goto error;
851 res->n++;
852 }
853
854 isl_qpolynomial_fold_free(fold1);
855 isl_qpolynomial_fold_free(fold2);
856
857 return res;
858error:
859 isl_qpolynomial_fold_free(res);
860 isl_qpolynomial_fold_free(fold1);
861 isl_qpolynomial_fold_free(fold2);
862 return NULL((void*)0);
863}
864
865__isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
866 __isl_take isl_pw_qpolynomial_fold *pw1,
867 __isl_take isl_pw_qpolynomial_fold *pw2)
868{
869 int i, j, n;
870 struct isl_pw_qpolynomial_fold *res;
871 isl_setisl_map *set;
872
873 if (!pw1 || !pw2)
16
Taking false branch
874 goto error;
875
876 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error)do { if (isl_space_is_equal(pw1->dim, pw2->dim)) break;
do { isl_handle_error(pw1->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(pw1->dim, pw2->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 876); goto error; } while (0); } while (0)
;
877
878 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
879 isl_pw_qpolynomial_fold_free(pw1);
880 return pw2;
881 }
882
883 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
884 isl_pw_qpolynomial_fold_free(pw2);
885 return pw1;
886 }
887
888 if (pw1->type != pw2->type)
889 isl_die(pw1->dim->ctx, isl_error_invalid,do { isl_handle_error(pw1->dim->ctx, isl_error_invalid,
"fold types don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 890); goto error; } while (0)
890 "fold types don't match", goto error)do { isl_handle_error(pw1->dim->ctx, isl_error_invalid,
"fold types don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 890); goto error; } while (0)
;
891
892 n = (pw1->n + 1) * (pw2->n + 1);
893 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
894 pw1->type, n);
895
896 for (i = 0; i < pw1->n; ++i) {
897 set = isl_set_copy(pw1->p[i].set);
898 for (j = 0; j < pw2->n; ++j) {
899 struct isl_setisl_map *common;
900 isl_qpolynomial_fold *sum;
901 set = isl_set_subtract(set,
902 isl_set_copy(pw2->p[j].set));
903 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
904 isl_set_copy(pw2->p[j].set));
905 if (isl_set_plain_is_empty(common)) {
906 isl_set_free(common);
907 continue;
908 }
909
910 sum = isl_qpolynomial_fold_fold_on_domain(common,
911 isl_qpolynomial_fold_copy(pw1->p[i].fold),
912 isl_qpolynomial_fold_copy(pw2->p[j].fold));
913
914 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
915 }
916 res = isl_pw_qpolynomial_fold_add_piece(res, set,
917 isl_qpolynomial_fold_copy(pw1->p[i].fold));
918 }
919
920 for (j = 0; j < pw2->n; ++j) {
921 set = isl_set_copy(pw2->p[j].set);
922 for (i = 0; i < pw1->n; ++i)
923 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
924 res = isl_pw_qpolynomial_fold_add_piece(res, set,
925 isl_qpolynomial_fold_copy(pw2->p[j].fold));
926 }
927
928 isl_pw_qpolynomial_fold_free(pw1);
929 isl_pw_qpolynomial_fold_free(pw2);
930
931 return res;
932error:
933 isl_pw_qpolynomial_fold_free(pw1);
934 isl_pw_qpolynomial_fold_free(pw2);
17
Calling 'isl_pw_qpolynomial_fold_free'
24
Returning; memory was released via 1st parameter
935 return NULL((void*)0);
936}
937
938__isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
939 __isl_take isl_union_pw_qpolynomial_fold *u,
940 __isl_take isl_pw_qpolynomial_fold *part)
941{
942 struct isl_hash_table_entry *entry;
943
944 u = isl_union_pw_qpolynomial_fold_cow(u);
945
946 if (!part || !u)
8
Assuming 'part' is non-null
9
Assuming 'u' is non-null
10
Taking false branch
947 goto error;
948 if (isl_space_check_equal_params(part->dim, u->space) < 0)
11
Assuming the condition is false
12
Taking false branch
949 goto error;
950
951 entry = isl_union_pw_qpolynomial_fold_find_part_entry(u, part->dim, 1);
952 if (!entry)
13
Taking false branch
953 goto error;
954
955 if (!entry->data)
14
Taking false branch
956 entry->data = part;
957 else {
958 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
15
Calling 'isl_pw_qpolynomial_fold_fold'
25
Returning; memory was released via 2nd parameter
959 isl_pw_qpolynomial_fold_copy(part));
960 if (!entry->data)
26
Taking true branch
961 goto error;
27
Control jumps to line 967
962 isl_pw_qpolynomial_fold_free(part);
963 }
964
965 return u;
966error:
967 isl_pw_qpolynomial_fold_free(part);
28
Use of memory after it is freed
968 isl_union_pw_qpolynomial_fold_free(u);
969 return NULL((void*)0);
970}
971
972static isl_stat fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
973{
974 isl_union_pw_qpolynomial_fold **u;
975 u = (isl_union_pw_qpolynomial_fold **)user;
976
977 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
978
979 return isl_stat_ok;
980}
981
982__isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
983 __isl_take isl_union_pw_qpolynomial_fold *u1,
984 __isl_take isl_union_pw_qpolynomial_fold *u2)
985{
986 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
987
988 if (!u1 || !u2)
989 goto error;
990
991 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
992 &fold_part, &u1) < 0)
993 goto error;
994
995 isl_union_pw_qpolynomial_fold_free(u2);
996
997 return u1;
998error:
999 isl_union_pw_qpolynomial_fold_free(u1);
1000 isl_union_pw_qpolynomial_fold_free(u2);
1001 return NULL((void*)0);
1002}
1003
1004__isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
1005 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
1006{
1007 int i;
1008 isl_pw_qpolynomial_fold *pwf;
1009
1010 if (!pwqp)
1011 return NULL((void*)0);
1012
1013 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
1014 type, pwqp->n);
1015
1016 for (i = 0; i < pwqp->n; ++i)
1017 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
1018 isl_set_copy(pwqp->p[i].set),
1019 isl_qpolynomial_fold_alloc(type,
1020 isl_qpolynomial_copy(pwqp->p[i].qp)));
1021
1022 isl_pw_qpolynomial_free(pwqp);
1023
1024 return pwf;
1025}
1026
1027__isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
1028 __isl_take isl_pw_qpolynomial_fold *pwf1,
1029 __isl_take isl_pw_qpolynomial_fold *pwf2)
1030{
1031 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
1032}
1033
1034/* Compare two quasi-polynomial reductions.
1035 *
1036 * Return -1 if "fold1" is "smaller" than "fold2", 1 if "fold1" is "greater"
1037 * than "fold2" and 0 if they are equal.
1038 */
1039int isl_qpolynomial_fold_plain_cmp(__isl_keep isl_qpolynomial_fold *fold1,
1040 __isl_keep isl_qpolynomial_fold *fold2)
1041{
1042 int i;
1043
1044 if (fold1 == fold2)
1045 return 0;
1046 if (!fold1)
1047 return -1;
1048 if (!fold2)
1049 return 1;
1050
1051 if (fold1->n != fold2->n)
1052 return fold1->n - fold2->n;
1053
1054 for (i = 0; i < fold1->n; ++i) {
1055 int cmp;
1056
1057 cmp = isl_qpolynomial_plain_cmp(fold1->qp[i], fold2->qp[i]);
1058 if (cmp != 0)
1059 return cmp;
1060 }
1061
1062 return 0;
1063}
1064
1065int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
1066 __isl_keep isl_qpolynomial_fold *fold2)
1067{
1068 int i;
1069
1070 if (!fold1 || !fold2)
1071 return -1;
1072
1073 if (fold1->n != fold2->n)
1074 return 0;
1075
1076 /* We probably want to sort the qps first... */
1077 for (i = 0; i < fold1->n; ++i) {
1078 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1079 if (eq < 0 || !eq)
1080 return eq;
1081 }
1082
1083 return 1;
1084}
1085
1086__isl_give isl_val *isl_qpolynomial_fold_eval(
1087 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1088{
1089 isl_ctx *ctx;
1090 isl_val *v;
1091
1092 if (!fold || !pnt)
1093 goto error;
1094 ctx = isl_point_get_ctx(pnt);
1095 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error)do { if (isl_space_is_equal(pnt->dim, fold->dim)) break
; do { isl_handle_error(pnt->dim->ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_equal(pnt->dim, fold->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1095); goto error; } while (0); } while (0)
;
1096 isl_assert(pnt->dim->ctx,do { if (fold->type == isl_fold_max || fold->type == isl_fold_min
) break; do { isl_handle_error(pnt->dim->ctx, isl_error_unknown
, "Assertion \"" "fold->type == isl_fold_max || fold->type == isl_fold_min"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1098); goto error; } while (0); } while (0)
1097 fold->type == isl_fold_max || fold->type == isl_fold_min,do { if (fold->type == isl_fold_max || fold->type == isl_fold_min
) break; do { isl_handle_error(pnt->dim->ctx, isl_error_unknown
, "Assertion \"" "fold->type == isl_fold_max || fold->type == isl_fold_min"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1098); goto error; } while (0); } while (0)
1098 goto error)do { if (fold->type == isl_fold_max || fold->type == isl_fold_min
) break; do { isl_handle_error(pnt->dim->ctx, isl_error_unknown
, "Assertion \"" "fold->type == isl_fold_max || fold->type == isl_fold_min"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1098); goto error; } while (0); } while (0)
;
1099
1100 if (fold->n == 0)
1101 v = isl_val_zero(ctx);
1102 else {
1103 int i;
1104 v = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1105 isl_point_copy(pnt));
1106 for (i = 1; i < fold->n; ++i) {
1107 isl_val *v_i;
1108 v_i = isl_qpolynomial_eval(
1109 isl_qpolynomial_copy(fold->qp[i]),
1110 isl_point_copy(pnt));
1111 if (fold->type == isl_fold_max)
1112 v = isl_val_max(v, v_i);
1113 else
1114 v = isl_val_min(v, v_i);
1115 }
1116 }
1117 isl_qpolynomial_fold_free(fold);
1118 isl_point_free(pnt);
1119
1120 return v;
1121error:
1122 isl_qpolynomial_fold_free(fold);
1123 isl_point_free(pnt);
1124 return NULL((void*)0);
1125}
1126
1127size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1128{
1129 int i;
1130 size_t n = 0;
1131
1132 for (i = 0; i < pwf->n; ++i)
1133 n += pwf->p[i].fold->n;
1134
1135 return n;
1136}
1137
1138__isl_give isl_val *isl_qpolynomial_fold_opt_on_domain(
1139 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_setisl_map *set, int max)
1140{
1141 int i;
1142 isl_val *opt;
1143
1144 if (!set || !fold)
1145 goto error;
1146
1147 if (fold->n == 0) {
1148 opt = isl_val_zero(isl_set_get_ctx(set));
1149 isl_set_free(set);
1150 isl_qpolynomial_fold_free(fold);
1151 return opt;
1152 }
1153
1154 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1155 isl_set_copy(set), max);
1156 for (i = 1; i < fold->n; ++i) {
1157 isl_val *opt_i;
1158 opt_i = isl_qpolynomial_opt_on_domain(
1159 isl_qpolynomial_copy(fold->qp[i]),
1160 isl_set_copy(set), max);
1161 if (max)
1162 opt = isl_val_max(opt, opt_i);
1163 else
1164 opt = isl_val_min(opt, opt_i);
1165 }
1166
1167 isl_set_free(set);
1168 isl_qpolynomial_fold_free(fold);
1169
1170 return opt;
1171error:
1172 isl_set_free(set);
1173 isl_qpolynomial_fold_free(fold);
1174 return NULL((void*)0);
1175}
1176
1177/* Check whether for each quasi-polynomial in "fold2" there is
1178 * a quasi-polynomial in "fold1" that dominates it on "set".
1179 */
1180static int qpolynomial_fold_covers_on_domain(__isl_keep isl_setisl_map *set,
1181 __isl_keep isl_qpolynomial_fold *fold1,
1182 __isl_keep isl_qpolynomial_fold *fold2)
1183{
1184 int i, j;
1185 int covers;
1186
1187 if (!set || !fold1 || !fold2)
1188 return -1;
1189
1190 covers = fold1->type == isl_fold_max ? 1 : -1;
1191
1192 for (i = 0; i < fold2->n; ++i) {
1193 for (j = 0; j < fold1->n; ++j) {
1194 isl_qpolynomial *d;
1195 int sgn;
1196
1197 d = isl_qpolynomial_sub(
1198 isl_qpolynomial_copy(fold1->qp[j]),
1199 isl_qpolynomial_copy(fold2->qp[i]));
1200 sgn = isl_qpolynomial_sign(set, d);
1201 isl_qpolynomial_free(d);
1202 if (sgn == covers)
1203 break;
1204 }
1205 if (j >= fold1->n)
1206 return 0;
1207 }
1208
1209 return 1;
1210}
1211
1212/* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1213 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1214 * that of pwf2.
1215 */
1216int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1217 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1218{
1219 int i, j;
1220 isl_setisl_map *dom1, *dom2;
1221 int is_subset;
1222
1223 if (!pwf1 || !pwf2)
1224 return -1;
1225
1226 if (pwf2->n == 0)
1227 return 1;
1228 if (pwf1->n == 0)
1229 return 0;
1230
1231 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1232 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1233 is_subset = isl_set_is_subset(dom2, dom1);
1234 isl_set_free(dom1);
1235 isl_set_free(dom2);
1236
1237 if (is_subset < 0 || !is_subset)
1238 return is_subset;
1239
1240 for (i = 0; i < pwf2->n; ++i) {
1241 for (j = 0; j < pwf1->n; ++j) {
1242 int is_empty;
1243 isl_setisl_map *common;
1244 int covers;
1245
1246 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1247 isl_set_copy(pwf2->p[i].set));
1248 is_empty = isl_set_is_empty(common);
1249 if (is_empty < 0 || is_empty) {
1250 isl_set_free(common);
1251 if (is_empty < 0)
1252 return -1;
1253 continue;
1254 }
1255 covers = qpolynomial_fold_covers_on_domain(common,
1256 pwf1->p[j].fold, pwf2->p[i].fold);
1257 isl_set_free(common);
1258 if (covers < 0 || !covers)
1259 return covers;
1260 }
1261 }
1262
1263 return 1;
1264}
1265
1266__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1267 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1268{
1269 int i;
1270 isl_ctx *ctx;
1271
1272 if (!fold || !morph)
1273 goto error;
1274
1275 ctx = fold->dim->ctx;
1276 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error)do { if (isl_space_is_equal(fold->dim, morph->dom->dim
)) break; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(fold->dim, morph->dom->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1276); goto error; } while (0); } while (0)
;
1277
1278 fold = isl_qpolynomial_fold_cow(fold);
1279 if (!fold)
1280 goto error;
1281
1282 isl_space_free(fold->dim);
1283 fold->dim = isl_space_copy(morph->ran->dim);
1284 if (!fold->dim)
1285 goto error;
1286
1287 for (i = 0; i < fold->n; ++i) {
1288 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1289 isl_morph_copy(morph));
1290 if (!fold->qp[i])
1291 goto error;
1292 }
1293
1294 isl_morph_free(morph);
1295
1296 return fold;
1297error:
1298 isl_qpolynomial_fold_free(fold);
1299 isl_morph_free(morph);
1300 return NULL((void*)0);
1301}
1302
1303enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1304{
1305 if (!fold)
1306 return isl_fold_list;
1307 return fold->type;
1308}
1309
1310enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1311 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1312{
1313 if (!upwf)
1314 return isl_fold_list;
1315 return upwf->type;
1316}
1317
1318__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1319 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1320{
1321 int i;
1322
1323 if (!fold || !dim)
1324 goto error;
1325
1326 if (isl_space_is_equal(fold->dim, dim)) {
1327 isl_space_free(dim);
1328 return fold;
1329 }
1330
1331 fold = isl_qpolynomial_fold_cow(fold);
1332 if (!fold)
1333 goto error;
1334
1335 isl_space_free(fold->dim);
1336 fold->dim = isl_space_copy(dim);
1337 if (!fold->dim)
1338 goto error;
1339
1340 for (i = 0; i < fold->n; ++i) {
1341 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1342 isl_space_copy(dim));
1343 if (!fold->qp[i])
1344 goto error;
1345 }
1346
1347 isl_space_free(dim);
1348
1349 return fold;
1350error:
1351 isl_qpolynomial_fold_free(fold);
1352 isl_space_free(dim);
1353 return NULL((void*)0);
1354}
1355
1356isl_stat isl_qpolynomial_fold_foreach_qpolynomial(
1357 __isl_keep isl_qpolynomial_fold *fold,
1358 isl_stat (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1359{
1360 int i;
1361
1362 if (!fold)
1363 return isl_stat_error;
1364
1365 for (i = 0; i < fold->n; ++i)
1366 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1367 return isl_stat_error;
1368
1369 return isl_stat_ok;
1370}
1371
1372__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1373 __isl_take isl_qpolynomial_fold *fold,
1374 enum isl_dim_type dst_type, unsigned dst_pos,
1375 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1376{
1377 int i;
1378 enum isl_dim_type set_src_type, set_dst_type;
1379
1380 if (n == 0)
1381 return fold;
1382
1383 fold = isl_qpolynomial_fold_cow(fold);
1384 if (!fold)
1385 return NULL((void*)0);
1386
1387 set_src_type = domain_type(src_type);
1388 set_dst_type = domain_type(dst_type);
1389
1390 fold->dim = isl_space_move_dims(fold->dim, set_dst_type, dst_pos,
1391 set_src_type, src_pos, n);
1392 if (!fold->dim)
1393 goto error;
1394
1395 for (i = 0; i < fold->n; ++i) {
1396 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1397 dst_type, dst_pos, src_type, src_pos, n);
1398 if (!fold->qp[i])
1399 goto error;
1400 }
1401
1402 return fold;
1403error:
1404 isl_qpolynomial_fold_free(fold);
1405 return NULL((void*)0);
1406}
1407
1408/* For each 0 <= i < "n", replace variable "first" + i of type "type"
1409 * in fold->qp[k] by subs[i].
1410 */
1411__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1412 __isl_take isl_qpolynomial_fold *fold,
1413 enum isl_dim_type type, unsigned first, unsigned n,
1414 __isl_keep isl_qpolynomial **subs)
1415{
1416 int i;
1417
1418 if (n == 0)
1419 return fold;
1420
1421 fold = isl_qpolynomial_fold_cow(fold);
1422 if (!fold)
1423 return NULL((void*)0);
1424
1425 for (i = 0; i < fold->n; ++i) {
1426 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1427 type, first, n, subs);
1428 if (!fold->qp[i])
1429 goto error;
1430 }
1431
1432 return fold;
1433error:
1434 isl_qpolynomial_fold_free(fold);
1435 return NULL((void*)0);
1436}
1437
1438static isl_stat add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1439{
1440 isl_pw_qpolynomial_fold *pwf;
1441 isl_union_pw_qpolynomial_fold **upwf;
1442 struct isl_hash_table_entry *entry;
1443
1444 upwf = (isl_union_pw_qpolynomial_fold **)user;
1445
1446 entry = isl_union_pw_qpolynomial_fold_find_part_entry(*upwf,
1447 pwqp->dim, 1);
1448 if (!entry)
1449 goto error;
1450
1451 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1452 if (!entry->data)
1453 entry->data = pwf;
1454 else {
1455 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1456 if (!entry->data)
1457 return isl_stat_error;
1458 if (isl_pw_qpolynomial_fold_is_zero(entry->data))
1459 *upwf = isl_union_pw_qpolynomial_fold_remove_part_entry(
1460 *upwf, entry);
1461 }
1462
1463 return isl_stat_ok;
1464error:
1465 isl_pw_qpolynomial_free(pwqp);
1466 return isl_stat_error;
1467}
1468
1469__isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1470 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1471 __isl_take isl_union_pw_qpolynomial *upwqp)
1472{
1473 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1474 isl_union_pw_qpolynomial_get_space(upwqp));
1475 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1476 isl_union_pw_qpolynomial_fold_get_space(upwf));
1477
1478 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1479 if (!upwf || !upwqp)
1480 goto error;
1481
1482 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1483 &upwf) < 0)
1484 goto error;
1485
1486 isl_union_pw_qpolynomial_free(upwqp);
1487
1488 return upwf;
1489error:
1490 isl_union_pw_qpolynomial_fold_free(upwf);
1491 isl_union_pw_qpolynomial_free(upwqp);
1492 return NULL((void*)0);
1493}
1494
1495static isl_bool join_compatible(__isl_keep isl_space *space1,
1496 __isl_keep isl_space *space2)
1497{
1498 isl_bool m;
1499 m = isl_space_has_equal_params(space1, space2);
1500 if (m < 0 || !m)
1501 return m;
1502 return isl_space_tuple_is_equal(space1, isl_dim_out,
1503 space2, isl_dim_in);
1504}
1505
1506/* Compute the intersection of the range of the map and the domain
1507 * of the piecewise quasipolynomial reduction and then compute a bound
1508 * on the associated quasipolynomial reduction over all elements
1509 * in this intersection.
1510 *
1511 * We first introduce some unconstrained dimensions in the
1512 * piecewise quasipolynomial, intersect the resulting domain
1513 * with the wrapped map and the compute the sum.
1514 */
1515__isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1516 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1517 int *tight)
1518{
1519 isl_ctx *ctx;
1520 isl_setisl_map *dom;
1521 isl_space *map_dim;
1522 isl_space *pwf_dim;
1523 unsigned n_in;
1524 isl_bool ok;
1525
1526 ctx = isl_map_get_ctx(map);
1527 if (!ctx)
1528 goto error;
1529
1530 map_dim = isl_map_get_space(map);
1531 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1532 ok = join_compatible(map_dim, pwf_dim);
1533 isl_space_free(map_dim);
1534 isl_space_free(pwf_dim);
1535 if (ok < 0)
1536 goto error;
1537 if (!ok)
1538 isl_die(ctx, isl_error_invalid, "incompatible dimensions",do { isl_handle_error(ctx, isl_error_invalid, "incompatible dimensions"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1539); goto error; } while (0)
1539 goto error)do { isl_handle_error(ctx, isl_error_invalid, "incompatible dimensions"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1539); goto error; } while (0)
;
1540
1541 n_in = isl_map_dim(map, isl_dim_in);
1542 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1543
1544 dom = isl_map_wrap(map);
1545 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1546 isl_set_get_space(dom));
1547
1548 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1549 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1550
1551 return pwf;
1552error:
1553 isl_map_free(map);
1554 isl_pw_qpolynomial_fold_free(pwf);
1555 return NULL((void*)0);
1556}
1557
1558__isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1559 __isl_take isl_setisl_map *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1560 int *tight)
1561{
1562 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1563}
1564
1565struct isl_apply_fold_data {
1566 isl_union_pw_qpolynomial_fold *upwf;
1567 isl_union_pw_qpolynomial_fold *res;
1568 isl_map *map;
1569 int tight;
1570};
1571
1572static isl_stat pw_qpolynomial_fold_apply(
1573 __isl_take isl_pw_qpolynomial_fold *pwf, void *user)
1574{
1575 isl_space *map_dim;
1576 isl_space *pwf_dim;
1577 struct isl_apply_fold_data *data = user;
1578 isl_bool ok;
1579
1580 map_dim = isl_map_get_space(data->map);
1581 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1582 ok = join_compatible(map_dim, pwf_dim);
1583 isl_space_free(map_dim);
1584 isl_space_free(pwf_dim);
1585
1586 if (ok < 0)
1
Assuming 'ok' is >= 0
2
Taking false branch
1587 return isl_stat_error;
1588 if (ok) {
3
Assuming 'ok' is not equal to 0
4
Taking true branch
1589 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1590 pwf, data->tight ? &data->tight : NULL((void*)0));
5
Assuming the condition is false
6
'?' condition is false
1591 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
7
Calling 'isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold'
1592 data->res, pwf);
1593 } else
1594 isl_pw_qpolynomial_fold_free(pwf);
1595
1596 return isl_stat_ok;
1597}
1598
1599static isl_stat map_apply(__isl_take isl_map *map, void *user)
1600{
1601 struct isl_apply_fold_data *data = user;
1602 isl_stat r;
1603
1604 data->map = map;
1605 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1606 data->upwf, &pw_qpolynomial_fold_apply, data);
1607
1608 isl_map_free(map);
1609 return r;
1610}
1611
1612__isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1613 __isl_take isl_union_map *umap,
1614 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1615{
1616 isl_space *dim;
1617 enum isl_fold type;
1618 struct isl_apply_fold_data data;
1619
1620 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1621 isl_union_map_get_space(umap));
1622 umap = isl_union_map_align_params(umap,
1623 isl_union_pw_qpolynomial_fold_get_space(upwf));
1624
1625 data.upwf = upwf;
1626 data.tight = tight ? 1 : 0;
1627 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1628 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1629 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1630 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1631 goto error;
1632
1633 isl_union_map_free(umap);
1634 isl_union_pw_qpolynomial_fold_free(upwf);
1635
1636 if (tight)
1637 *tight = data.tight;
1638
1639 return data.res;
1640error:
1641 isl_union_map_free(umap);
1642 isl_union_pw_qpolynomial_fold_free(upwf);
1643 isl_union_pw_qpolynomial_fold_free(data.res);
1644 return NULL((void*)0);
1645}
1646
1647__isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1648 __isl_take isl_union_setisl_union_map *uset,
1649 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1650{
1651 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1652}
1653
1654/* Reorder the dimension of "fold" according to the given reordering.
1655 */
1656__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1657 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1658{
1659 int i;
1660
1661 fold = isl_qpolynomial_fold_cow(fold);
1662 if (!fold || !r)
1663 goto error;
1664
1665 for (i = 0; i < fold->n; ++i) {
1666 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1667 isl_reordering_copy(r));
1668 if (!fold->qp[i])
1669 goto error;
1670 }
1671
1672 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1673 isl_space_copy(r->dim));
1674
1675 isl_reordering_free(r);
1676
1677 return fold;
1678error:
1679 isl_qpolynomial_fold_free(fold);
1680 isl_reordering_free(r);
1681 return NULL((void*)0);
1682}
1683
1684__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1685 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1686{
1687 int i;
1688
1689 if (isl_int_is_one(v)(isl_sioimath_cmp_si(*(v), 1) == 0))
1690 return fold;
1691 if (fold && isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0)) {
1692 isl_qpolynomial_fold *zero;
1693 isl_space *dim = isl_space_copy(fold->dim);
1694 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1695 isl_qpolynomial_fold_free(fold);
1696 return zero;
1697 }
1698
1699 fold = isl_qpolynomial_fold_cow(fold);
1700 if (!fold)
1701 return NULL((void*)0);
1702
1703 if (isl_int_is_neg(v)(isl_sioimath_sgn(*(v)) < 0))
1704 fold->type = isl_fold_type_negate(fold->type);
1705 for (i = 0; i < fold->n; ++i) {
1706 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1707 if (!fold->qp[i])
1708 goto error;
1709 }
1710
1711 return fold;
1712error:
1713 isl_qpolynomial_fold_free(fold);
1714 return NULL((void*)0);
1715}
1716
1717__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1718 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1719{
1720 return isl_qpolynomial_fold_mul_isl_int(fold, v);
1721}
1722
1723/* Multiply "fold" by "v".
1724 */
1725__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_val(
1726 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1727{
1728 int i;
1729
1730 if (!fold || !v)
1731 goto error;
1732
1733 if (isl_val_is_one(v)) {
1734 isl_val_free(v);
1735 return fold;
1736 }
1737 if (isl_val_is_zero(v)) {
1738 isl_qpolynomial_fold *zero;
1739 isl_space *space = isl_qpolynomial_fold_get_domain_space(fold);
1740 zero = isl_qpolynomial_fold_empty(fold->type, space);
1741 isl_qpolynomial_fold_free(fold);
1742 isl_val_free(v);
1743 return zero;
1744 }
1745 if (!isl_val_is_rat(v))
1746 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1747); goto error; } while (0)
1747 "expecting rational factor", goto error)do { isl_handle_error(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1747); goto error; } while (0)
;
1748
1749 fold = isl_qpolynomial_fold_cow(fold);
1750 if (!fold)
1751 goto error;
1752
1753 if (isl_val_is_neg(v))
1754 fold->type = isl_fold_type_negate(fold->type);
1755 for (i = 0; i < fold->n; ++i) {
1756 fold->qp[i] = isl_qpolynomial_scale_val(fold->qp[i],
1757 isl_val_copy(v));
1758 if (!fold->qp[i])
1759 goto error;
1760 }
1761
1762 isl_val_free(v);
1763 return fold;
1764error:
1765 isl_val_free(v);
1766 isl_qpolynomial_fold_free(fold);
1767 return NULL((void*)0);
1768}
1769
1770/* Divide "fold" by "v".
1771 */
1772__isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale_down_val(
1773 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_val *v)
1774{
1775 if (!fold || !v)
1776 goto error;
1777
1778 if (isl_val_is_one(v)) {
1779 isl_val_free(v);
1780 return fold;
1781 }
1782 if (!isl_val_is_rat(v))
1783 isl_die(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid,do { isl_handle_error(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1784); goto error; } while (0)
1784 "expecting rational factor", goto error)do { isl_handle_error(isl_qpolynomial_fold_get_ctx(fold), isl_error_invalid
, "expecting rational factor", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1784); goto error; } while (0)
;
1785 if (isl_val_is_zero(v))
1786 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1787); goto error; } while (0)
1787 "cannot scale down by zero", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_fold.c"
, 1787); goto error; } while (0)
;
1788
1789 return isl_qpolynomial_fold_scale_val(fold, isl_val_inv(v));
1790error:
1791 isl_val_free(v);
1792 isl_qpolynomial_fold_free(fold);
1793 return NULL((void*)0);
1794}

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

1/*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 *
6 * Use of this software is governed by the MIT license
7 *
8 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
9 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * 91893 Orsay, France
11 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 */
13
14#include <isl/aff.h>
15#include <isl_sort.h>
16#include <isl_val_private.h>
17
18#include <isl_pw_macro.h>
19
20#ifdef HAS_TYPE
21__isl_give PWisl_pw_qpolynomial_fold *FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(__isl_take isl_space *dim,
22 enum isl_fold type, int n)
23#else
24__isl_give PWisl_pw_qpolynomial_fold *FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(__isl_take isl_space *dim, int n)
25#endif
26{
27 isl_ctx *ctx;
28 struct PWisl_pw_qpolynomial_fold *pw;
29
30 if (!dim)
31 return NULL((void*)0);
32 ctx = isl_space_get_ctx(dim);
33 isl_assert(ctx, n >= 0, goto error)do { if (n >= 0) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "n >= 0" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 33); goto error; } while (0); } while (0)
;
34 pw = isl_alloc(ctx, struct PW,((struct isl_pw_qpolynomial_fold *)isl_malloc_or_die(ctx, sizeof
(struct isl_pw_qpolynomial_fold) + (n - 1) * sizeof(struct isl_pw_qpolynomial_fold_piece
)))
35 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)))((struct isl_pw_qpolynomial_fold *)isl_malloc_or_die(ctx, sizeof
(struct isl_pw_qpolynomial_fold) + (n - 1) * sizeof(struct isl_pw_qpolynomial_fold_piece
)))
;
36 if (!pw)
37 goto error;
38
39 pw->ref = 1;
40#ifdef HAS_TYPE
41 pw->type = type;
42#endif
43 pw->size = n;
44 pw->n = 0;
45 pw->dim = dim;
46 return pw;
47error:
48 isl_space_free(dim);
49 return NULL((void*)0);
50}
51
52#ifdef HAS_TYPE
53__isl_give PWisl_pw_qpolynomial_fold *FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(__isl_take isl_space *dim, enum isl_fold type)
54{
55 return FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(dim, type, 0);
56}
57#else
58__isl_give PWisl_pw_qpolynomial_fold *FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(__isl_take isl_space *dim)
59{
60 return FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(dim, 0);
61}
62#endif
63
64__isl_give PWisl_pw_qpolynomial_fold *FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(__isl_take PWisl_pw_qpolynomial_fold *pw,
65 __isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el)
66{
67 isl_ctx *ctx;
68 isl_space *el_dim = NULL((void*)0);
69
70 if (!pw || !set || !el)
71 goto error;
72
73 if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)isl_qpolynomial_fold_is_empty(el)) {
74 isl_set_free(set);
75 FN(EL,free)isl_qpolynomial_fold_free(el);
76 return pw;
77 }
78
79 ctx = isl_set_get_ctx(set);
80#ifdef HAS_TYPE
81 if (pw->type != el->type)
82 isl_die(ctx, isl_error_invalid, "fold types don't match",do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 83); goto error; } while (0)
83 goto error)do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 83); goto error; } while (0)
;
84#endif
85 el_dim = FN(EL,get_space(el))isl_qpolynomial_fold_get_space(el);
86 isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error)do { if (isl_space_is_equal(pw->dim, el_dim)) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "isl_space_is_equal(pw->dim, el_dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 86); goto error; } while (0); } while (0)
;
87 isl_assert(ctx, pw->n < pw->size, goto error)do { if (pw->n < pw->size) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "pw->n < pw->size"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 87); goto error; } while (0); } while (0)
;
88
89 pw->p[pw->n].set = set;
90 pw->p[pw->n].FIELDfold = el;
91 pw->n++;
92
93 isl_space_free(el_dim);
94 return pw;
95error:
96 isl_space_free(el_dim);
97 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
98 isl_set_free(set);
99 FN(EL,free)isl_qpolynomial_fold_free(el);
100 return NULL((void*)0);
101}
102
103/* Does the space of "set" correspond to that of the domain of "el".
104 */
105static isl_bool FN(PW,compatible_domain)isl_pw_qpolynomial_fold_compatible_domain(__isl_keep ELisl_qpolynomial_fold *el,
106 __isl_keep isl_setisl_map *set)
107{
108 isl_bool ok;
109 isl_space *el_space, *set_space;
110
111 if (!set || !el)
112 return isl_bool_error;
113 set_space = isl_set_get_space(set);
114 el_space = FN(EL,get_space)isl_qpolynomial_fold_get_space(el);
115 ok = isl_space_is_domain_internal(set_space, el_space);
116 isl_space_free(el_space);
117 isl_space_free(set_space);
118 return ok;
119}
120
121/* Check that the space of "set" corresponds to that of the domain of "el".
122 */
123static isl_stat FN(PW,check_compatible_domain)isl_pw_qpolynomial_fold_check_compatible_domain(__isl_keep ELisl_qpolynomial_fold *el,
124 __isl_keep isl_setisl_map *set)
125{
126 isl_bool ok;
127
128 ok = FN(PW,compatible_domain)isl_pw_qpolynomial_fold_compatible_domain(el, set);
129 if (ok < 0)
130 return isl_stat_error;
131 if (!ok)
132 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "incompatible spaces", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 133); return isl_stat_error; } while (0)
133 "incompatible spaces", return isl_stat_error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "incompatible spaces", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 133); return isl_stat_error; } while (0)
;
134
135 return isl_stat_ok;
136}
137
138#ifdef HAS_TYPE
139__isl_give PWisl_pw_qpolynomial_fold *FN(PW,alloc)isl_pw_qpolynomial_fold_alloc(enum isl_fold type,
140 __isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el)
141#else
142__isl_give PWisl_pw_qpolynomial_fold *FN(PW,alloc)isl_pw_qpolynomial_fold_alloc(__isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el)
143#endif
144{
145 PWisl_pw_qpolynomial_fold *pw;
146
147 if (FN(PW,check_compatible_domain)isl_pw_qpolynomial_fold_check_compatible_domain(el, set) < 0)
148 goto error;
149
150#ifdef HAS_TYPE
151 pw = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(FN(EL,get_space)isl_qpolynomial_fold_get_space(el), type, 1);
152#else
153 pw = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(FN(EL,get_space)isl_qpolynomial_fold_get_space(el), 1);
154#endif
155
156 return FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(pw, set, el);
157error:
158 isl_set_free(set);
159 FN(EL,free)isl_qpolynomial_fold_free(el);
160 return NULL((void*)0);
161}
162
163__isl_give PWisl_pw_qpolynomial_fold *FN(PW,dup)isl_pw_qpolynomial_fold_dup(__isl_keep PWisl_pw_qpolynomial_fold *pw)
164{
165 int i;
166 PWisl_pw_qpolynomial_fold *dup;
167
168 if (!pw)
169 return NULL((void*)0);
170
171#ifdef HAS_TYPE
172 dup = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw->dim), pw->type, pw->n);
173#else
174 dup = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw->dim), pw->n);
175#endif
176 if (!dup)
177 return NULL((void*)0);
178
179 for (i = 0; i < pw->n; ++i)
180 dup = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(dup, isl_set_copy(pw->p[i].set),
181 FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold));
182
183 return dup;
184}
185
186__isl_give PWisl_pw_qpolynomial_fold *FN(PW,cow)isl_pw_qpolynomial_fold_cow(__isl_take PWisl_pw_qpolynomial_fold *pw)
187{
188 if (!pw)
189 return NULL((void*)0);
190
191 if (pw->ref == 1)
192 return pw;
193 pw->ref--;
194 return FN(PW,dup)isl_pw_qpolynomial_fold_dup(pw);
195}
196
197__isl_give PWisl_pw_qpolynomial_fold *FN(PW,copy)isl_pw_qpolynomial_fold_copy(__isl_keep PWisl_pw_qpolynomial_fold *pw)
198{
199 if (!pw)
200 return NULL((void*)0);
201
202 pw->ref++;
203 return pw;
204}
205
206__isl_null PWisl_pw_qpolynomial_fold *FN(PW,free)isl_pw_qpolynomial_fold_free(__isl_take PWisl_pw_qpolynomial_fold *pw)
207{
208 int i;
209
210 if (!pw)
18
Taking false branch
211 return NULL((void*)0);
212 if (--pw->ref > 0)
19
Assuming the condition is false
20
Taking false branch
213 return NULL((void*)0);
214
215 for (i = 0; i < pw->n; ++i) {
21
Assuming the condition is false
22
Loop condition is false. Execution continues on line 219
216 isl_set_free(pw->p[i].set);
217 FN(EL,free)isl_qpolynomial_fold_free(pw->p[i].FIELDfold);
218 }
219 isl_space_free(pw->dim);
220 free(pw);
23
Memory is released
221
222 return NULL((void*)0);
223}
224
225const char *FN(PW,get_dim_name)isl_pw_qpolynomial_fold_get_dim_name(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
226 unsigned pos)
227{
228 return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL((void*)0);
229}
230
231isl_bool FN(PW,has_dim_id)isl_pw_qpolynomial_fold_has_dim_id(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
232 unsigned pos)
233{
234 return pw ? isl_space_has_dim_id(pw->dim, type, pos) : isl_bool_error;
235}
236
237__isl_give isl_id *FN(PW,get_dim_id)isl_pw_qpolynomial_fold_get_dim_id(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
238 unsigned pos)
239{
240 return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL((void*)0);
241}
242
243isl_bool FN(PW,has_tuple_name)isl_pw_qpolynomial_fold_has_tuple_name(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
244{
245 return pw ? isl_space_has_tuple_name(pw->dim, type) : isl_bool_error;
246}
247
248const char *FN(PW,get_tuple_name)isl_pw_qpolynomial_fold_get_tuple_name(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
249{
250 return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL((void*)0);
251}
252
253isl_bool FN(PW,has_tuple_id)isl_pw_qpolynomial_fold_has_tuple_id(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
254{
255 return pw ? isl_space_has_tuple_id(pw->dim, type) : isl_bool_error;
256}
257
258__isl_give isl_id *FN(PW,get_tuple_id)isl_pw_qpolynomial_fold_get_tuple_id(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
259{
260 return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL((void*)0);
261}
262
263isl_bool FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(__isl_keep PWisl_pw_qpolynomial_fold *pw)
264{
265 if (!pw)
266 return isl_bool_error;
267
268 return pw->n == 0;
269}
270
271#ifndef NO_REALIGN
272__isl_give PWisl_pw_qpolynomial_fold *FN(PW,realign_domain)isl_pw_qpolynomial_fold_realign_domain(__isl_take PWisl_pw_qpolynomial_fold *pw,
273 __isl_take isl_reordering *exp)
274{
275 int i;
276
277 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
278 if (!pw || !exp)
279 goto error;
280
281 for (i = 0; i < pw->n; ++i) {
282 pw->p[i].set = isl_set_realign(pw->p[i].set,
283 isl_reordering_copy(exp));
284 if (!pw->p[i].set)
285 goto error;
286 pw->p[i].FIELDfold = FN(EL,realign_domain)isl_qpolynomial_fold_realign_domain(pw->p[i].FIELDfold,
287 isl_reordering_copy(exp));
288 if (!pw->p[i].FIELDfold)
289 goto error;
290 }
291
292 pw = FN(PW,reset_domain_space)isl_pw_qpolynomial_fold_reset_domain_space(pw, isl_space_copy(exp->dim));
293
294 isl_reordering_free(exp);
295 return pw;
296error:
297 isl_reordering_free(exp);
298 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
299 return NULL((void*)0);
300}
301
302/* Align the parameters of "pw" to those of "model".
303 */
304__isl_give PWisl_pw_qpolynomial_fold *FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_space *model)
305{
306 isl_ctx *ctx;
307 isl_bool equal_params;
308
309 if (!pw || !model)
310 goto error;
311
312 ctx = isl_space_get_ctx(model);
313 if (!isl_space_has_named_params(model))
314 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "model has unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 315); goto error; } while (0)
315 "model has unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "model has unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 315); goto error; } while (0)
;
316 if (!isl_space_has_named_params(pw->dim))
317 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "input has unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 318); goto error; } while (0)
318 "input has unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "input has unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 318); goto error; } while (0)
;
319 equal_params = isl_space_has_equal_params(pw->dim, model);
320 if (equal_params < 0)
321 goto error;
322 if (!equal_params) {
323 isl_reordering *exp;
324
325 model = isl_space_drop_dims(model, isl_dim_in,
326 0, isl_space_dim(model, isl_dim_in));
327 model = isl_space_drop_dims(model, isl_dim_out,
328 0, isl_space_dim(model, isl_dim_out));
329 exp = isl_parameter_alignment_reordering(pw->dim, model);
330 exp = isl_reordering_extend_space(exp,
331 FN(PW,get_domain_space)isl_pw_qpolynomial_fold_get_domain_space(pw));
332 pw = FN(PW,realign_domain)isl_pw_qpolynomial_fold_realign_domain(pw, exp);
333 }
334
335 isl_space_free(model);
336 return pw;
337error:
338 isl_space_free(model);
339 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
340 return NULL((void*)0);
341}
342
343static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,align_params_pw_pw_and)isl_pw_qpolynomial_fold_align_params_pw_pw_and(__isl_take PWisl_pw_qpolynomial_fold *pw1,
344 __isl_take PWisl_pw_qpolynomial_fold *pw2,
345 __isl_give PWisl_pw_qpolynomial_fold *(*fn)(__isl_take PWisl_pw_qpolynomial_fold *pw1, __isl_take PWisl_pw_qpolynomial_fold *pw2))
346{
347 isl_ctx *ctx;
348 isl_bool equal_params;
349
350 if (!pw1 || !pw2)
351 goto error;
352 equal_params = isl_space_has_equal_params(pw1->dim, pw2->dim);
353 if (equal_params < 0)
354 goto error;
355 if (equal_params)
356 return fn(pw1, pw2);
357 ctx = FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw1);
358 if (!isl_space_has_named_params(pw1->dim) ||
359 !isl_space_has_named_params(pw2->dim))
360 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 361); goto error; } while (0)
361 "unaligned unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 361); goto error; } while (0)
;
362 pw1 = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw1, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw2));
363 pw2 = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw2, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw1));
364 return fn(pw1, pw2);
365error:
366 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
367 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
368 return NULL((void*)0);
369}
370
371static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(__isl_take PWisl_pw_qpolynomial_fold *pw,
372 __isl_take isl_setisl_map *set,
373 __isl_give PWisl_pw_qpolynomial_fold *(*fn)(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_setisl_map *set))
374{
375 isl_ctx *ctx;
376 isl_bool aligned;
377
378 if (!pw || !set)
379 goto error;
380 aligned = isl_set_space_has_equal_params(set, pw->dim);
381 if (aligned < 0)
382 goto error;
383 if (aligned)
384 return fn(pw, set);
385 ctx = FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw);
386 if (!isl_space_has_named_params(pw->dim) ||
387 !isl_space_has_named_params(set->dim))
388 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 389); goto error; } while (0)
389 "unaligned unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 389); goto error; } while (0)
;
390 pw = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw, isl_set_get_space(set));
391 set = isl_set_align_params(set, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
392 return fn(pw, set);
393error:
394 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
395 isl_set_free(set);
396 return NULL((void*)0);
397}
398#endif
399
400static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,union_add_aligned)isl_pw_qpolynomial_fold_union_add_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw1,
401 __isl_take PWisl_pw_qpolynomial_fold *pw2)
402{
403 int i, j, n;
404 struct PWisl_pw_qpolynomial_fold *res;
405 isl_ctx *ctx;
406 isl_setisl_map *set;
407
408 if (!pw1 || !pw2)
409 goto error;
410
411 ctx = isl_space_get_ctx(pw1->dim);
412#ifdef HAS_TYPE
413 if (pw1->type != pw2->type)
414 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 415); goto error; } while (0)
415 "fold types don't match", goto error)do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 415); goto error; } while (0)
;
416#endif
417 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error)do { if (isl_space_is_equal(pw1->dim, pw2->dim)) break;
do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(pw1->dim, pw2->dim)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 417); goto error; } while (0); } while (0)
;
418
419 if (FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(pw1)) {
420 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
421 return pw2;
422 }
423
424 if (FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(pw2)) {
425 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
426 return pw1;
427 }
428
429 n = (pw1->n + 1) * (pw2->n + 1);
430#ifdef HAS_TYPE
431 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim), pw1->type, n);
432#else
433 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim), n);
434#endif
435
436 for (i = 0; i < pw1->n; ++i) {
437 set = isl_set_copy(pw1->p[i].set);
438 for (j = 0; j < pw2->n; ++j) {
439 struct isl_setisl_map *common;
440 ELisl_qpolynomial_fold *sum;
441 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
442 isl_set_copy(pw2->p[j].set));
443 if (isl_set_plain_is_empty(common)) {
444 isl_set_free(common);
445 continue;
446 }
447 set = isl_set_subtract(set,
448 isl_set_copy(pw2->p[j].set));
449
450 sum = FN(EL,add_on_domain)isl_qpolynomial_fold_add_on_domain(common,
451 FN(EL,copy)isl_qpolynomial_fold_copy(pw1->p[i].FIELDfold),
452 FN(EL,copy)isl_qpolynomial_fold_copy(pw2->p[j].FIELDfold));
453
454 res = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(res, common, sum);
455 }
456 res = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(res, set, FN(EL,copy)isl_qpolynomial_fold_copy(pw1->p[i].FIELDfold));
457 }
458
459 for (j = 0; j < pw2->n; ++j) {
460 set = isl_set_copy(pw2->p[j].set);
461 for (i = 0; i < pw1->n; ++i)
462 set = isl_set_subtract(set,
463 isl_set_copy(pw1->p[i].set));
464 res = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(res, set, FN(EL,copy)isl_qpolynomial_fold_copy(pw2->p[j].FIELDfold));
465 }
466
467 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
468 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
469
470 return res;
471error:
472 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
473 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
474 return NULL((void*)0);
475}
476
477/* Private version of "union_add". For isl_pw_qpolynomial and
478 * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
479 */
480static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,union_add_)isl_pw_qpolynomial_fold_union_add_(__isl_take PWisl_pw_qpolynomial_fold *pw1, __isl_take PWisl_pw_qpolynomial_fold *pw2)
481{
482 return FN(PW,align_params_pw_pw_and)isl_pw_qpolynomial_fold_align_params_pw_pw_and(pw1, pw2,
483 &FN(PW,union_add_aligned)isl_pw_qpolynomial_fold_union_add_aligned);
484}
485
486/* Make sure "pw" has room for at least "n" more pieces.
487 *
488 * If there is only one reference to pw, we extend it in place.
489 * Otherwise, we create a new PW and copy the pieces.
490 */
491static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,grow)isl_pw_qpolynomial_fold_grow(__isl_take PWisl_pw_qpolynomial_fold *pw, int n)
492{
493 int i;
494 isl_ctx *ctx;
495 PWisl_pw_qpolynomial_fold *res;
496
497 if (!pw)
498 return NULL((void*)0);
499 if (pw->n + n <= pw->size)
500 return pw;
501 ctx = FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw);
502 n += pw->n;
503 if (pw->ref == 1) {
504 res = isl_realloc(ctx, pw, struct PW,((struct isl_pw_qpolynomial_fold *)isl_realloc_or_die(ctx, pw
, sizeof(struct isl_pw_qpolynomial_fold) + (n - 1) * sizeof(struct
isl_pw_qpolynomial_fold_piece)))
505 sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)))((struct isl_pw_qpolynomial_fold *)isl_realloc_or_die(ctx, pw
, sizeof(struct isl_pw_qpolynomial_fold) + (n - 1) * sizeof(struct
isl_pw_qpolynomial_fold_piece)))
;
506 if (!res)
507 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
508 res->size = n;
509 return res;
510 }
511#ifdef HAS_TYPE
512 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw->dim), pw->type, n);
513#else
514 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw->dim), n);
515#endif
516 if (!res)
517 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
518 for (i = 0; i < pw->n; ++i)
519 res = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(res, isl_set_copy(pw->p[i].set),
520 FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold));
521 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
522 return res;
523}
524
525static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,add_disjoint_aligned)isl_pw_qpolynomial_fold_add_disjoint_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw1,
526 __isl_take PWisl_pw_qpolynomial_fold *pw2)
527{
528 int i;
529 isl_ctx *ctx;
530
531 if (!pw1 || !pw2)
532 goto error;
533
534 if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
535 return FN(PW,add_disjoint_aligned)isl_pw_qpolynomial_fold_add_disjoint_aligned(pw2, pw1);
536
537 ctx = isl_space_get_ctx(pw1->dim);
538#ifdef HAS_TYPE
539 if (pw1->type != pw2->type)
540 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 541); goto error; } while (0)
541 "fold types don't match", goto error)do { isl_handle_error(ctx, isl_error_invalid, "fold types don't match"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 541); goto error; } while (0)
;
542#endif
543 isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error)do { if (isl_space_is_equal(pw1->dim, pw2->dim)) break;
do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(pw1->dim, pw2->dim)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 543); goto error; } while (0); } while (0)
;
544
545 if (FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(pw1)) {
546 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
547 return pw2;
548 }
549
550 if (FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(pw2)) {
551 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
552 return pw1;
553 }
554
555 pw1 = FN(PW,grow)isl_pw_qpolynomial_fold_grow(pw1, pw2->n);
556 if (!pw1)
557 goto error;
558
559 for (i = 0; i < pw2->n; ++i)
560 pw1 = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(pw1,
561 isl_set_copy(pw2->p[i].set),
562 FN(EL,copy)isl_qpolynomial_fold_copy(pw2->p[i].FIELDfold));
563
564 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
565
566 return pw1;
567error:
568 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
569 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
570 return NULL((void*)0);
571}
572
573__isl_give PWisl_pw_qpolynomial_fold *FN(PW,add_disjoint)isl_pw_qpolynomial_fold_add_disjoint(__isl_take PWisl_pw_qpolynomial_fold *pw1, __isl_take PWisl_pw_qpolynomial_fold *pw2)
574{
575 return FN(PW,align_params_pw_pw_and)isl_pw_qpolynomial_fold_align_params_pw_pw_and(pw1, pw2,
576 &FN(PW,add_disjoint_aligned)isl_pw_qpolynomial_fold_add_disjoint_aligned);
577}
578
579/* This function is currently only used from isl_aff.c
580 */
581static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,on_shared_domain_in)isl_pw_qpolynomial_fold_on_shared_domain_in(__isl_take PWisl_pw_qpolynomial_fold *pw1,
582 __isl_take PWisl_pw_qpolynomial_fold *pw2, __isl_take isl_space *space,
583 __isl_give ELisl_qpolynomial_fold *(*fn)(__isl_take ELisl_qpolynomial_fold *el1, __isl_take ELisl_qpolynomial_fold *el2))
584 __attribute__ ((unused));
585
586/* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
587 * The result of "fn" (and therefore also of this function) lives in "space".
588 */
589static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,on_shared_domain_in)isl_pw_qpolynomial_fold_on_shared_domain_in(__isl_take PWisl_pw_qpolynomial_fold *pw1,
590 __isl_take PWisl_pw_qpolynomial_fold *pw2, __isl_take isl_space *space,
591 __isl_give ELisl_qpolynomial_fold *(*fn)(__isl_take ELisl_qpolynomial_fold *el1, __isl_take ELisl_qpolynomial_fold *el2))
592{
593 int i, j, n;
594 PWisl_pw_qpolynomial_fold *res = NULL((void*)0);
595
596 if (!pw1 || !pw2)
597 goto error;
598
599 n = pw1->n * pw2->n;
600#ifdef HAS_TYPE
601 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(space), pw1->type, n);
602#else
603 res = FN(PW,alloc_size)isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(space), n);
604#endif
605
606 for (i = 0; i < pw1->n; ++i) {
607 for (j = 0; j < pw2->n; ++j) {
608 isl_setisl_map *common;
609 ELisl_qpolynomial_fold *res_ij;
610 int empty;
611
612 common = isl_set_intersect(
613 isl_set_copy(pw1->p[i].set),
614 isl_set_copy(pw2->p[j].set));
615 empty = isl_set_plain_is_empty(common);
616 if (empty < 0 || empty) {
617 isl_set_free(common);
618 if (empty < 0)
619 goto error;
620 continue;
621 }
622
623 res_ij = fn(FN(EL,copy)isl_qpolynomial_fold_copy(pw1->p[i].FIELDfold),
624 FN(EL,copy)isl_qpolynomial_fold_copy(pw2->p[j].FIELDfold));
625 res_ij = FN(EL,gist)isl_qpolynomial_fold_gist(res_ij, isl_set_copy(common));
626
627 res = FN(PW,add_piece)isl_pw_qpolynomial_fold_add_piece(res, common, res_ij);
628 }
629 }
630
631 isl_space_free(space);
632 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
633 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
634 return res;
635error:
636 isl_space_free(space);
637 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
638 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
639 FN(PW,free)isl_pw_qpolynomial_fold_free(res);
640 return NULL((void*)0);
641}
642
643/* This function is currently only used from isl_aff.c
644 */
645static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,on_shared_domain)isl_pw_qpolynomial_fold_on_shared_domain(__isl_take PWisl_pw_qpolynomial_fold *pw1,
646 __isl_take PWisl_pw_qpolynomial_fold *pw2,
647 __isl_give ELisl_qpolynomial_fold *(*fn)(__isl_take ELisl_qpolynomial_fold *el1, __isl_take ELisl_qpolynomial_fold *el2))
648 __attribute__ ((unused));
649
650/* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
651 * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
652 */
653static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,on_shared_domain)isl_pw_qpolynomial_fold_on_shared_domain(__isl_take PWisl_pw_qpolynomial_fold *pw1,
654 __isl_take PWisl_pw_qpolynomial_fold *pw2,
655 __isl_give ELisl_qpolynomial_fold *(*fn)(__isl_take ELisl_qpolynomial_fold *el1, __isl_take ELisl_qpolynomial_fold *el2))
656{
657 isl_space *space;
658
659 if (!pw1 || !pw2)
660 goto error;
661
662 space = isl_space_copy(pw1->dim);
663 return FN(PW,on_shared_domain_in)isl_pw_qpolynomial_fold_on_shared_domain_in(pw1, pw2, space, fn);
664error:
665 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
666 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
667 return NULL((void*)0);
668}
669
670#ifndef NO_NEG
671__isl_give PWisl_pw_qpolynomial_fold *FN(PW,neg)isl_pw_qpolynomial_fold_neg(__isl_take PWisl_pw_qpolynomial_fold *pw)
672{
673 int i;
674
675 if (!pw)
676 return NULL((void*)0);
677
678 if (FN(PW,IS_ZERO)isl_pw_qpolynomial_fold_is_zero(pw))
679 return pw;
680
681 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
682 if (!pw)
683 return NULL((void*)0);
684
685 for (i = 0; i < pw->n; ++i) {
686 pw->p[i].FIELDfold = FN(EL,neg)isl_qpolynomial_fold_neg(pw->p[i].FIELDfold);
687 if (!pw->p[i].FIELDfold)
688 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
689 }
690
691 return pw;
692}
693#endif
694
695#ifndef NO_SUB
696__isl_give PWisl_pw_qpolynomial_fold *FN(PW,sub)isl_pw_qpolynomial_fold_sub(__isl_take PWisl_pw_qpolynomial_fold *pw1, __isl_take PWisl_pw_qpolynomial_fold *pw2)
697{
698 return FN(PW,add)isl_pw_qpolynomial_fold_add(pw1, FN(PW,neg)isl_pw_qpolynomial_fold_neg(pw2));
699}
700#endif
701
702#ifndef NO_EVAL
703__isl_give isl_val *FN(PW,eval)isl_pw_qpolynomial_fold_eval(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_point *pnt)
704{
705 int i;
706 int found = 0;
707 isl_ctx *ctx;
708 isl_space *pnt_dim = NULL((void*)0);
709 isl_val *v;
710
711 if (!pw || !pnt)
712 goto error;
713 ctx = isl_point_get_ctx(pnt);
714 pnt_dim = isl_point_get_space(pnt);
715 isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),do { if (isl_space_is_domain_internal(pnt_dim, pw->dim)) break
; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"isl_space_is_domain_internal(pnt_dim, pw->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 716); goto error; } while (0); } while (0)
716 goto error)do { if (isl_space_is_domain_internal(pnt_dim, pw->dim)) break
; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"isl_space_is_domain_internal(pnt_dim, pw->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 716); goto error; } while (0); } while (0)
;
717
718 for (i = 0; i < pw->n; ++i) {
719 found = isl_set_contains_point(pw->p[i].set, pnt);
720 if (found < 0)
721 goto error;
722 if (found)
723 break;
724 }
725 if (found)
726 v = FN(EL,eval)isl_qpolynomial_fold_eval(FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold),
727 isl_point_copy(pnt));
728 else
729 v = isl_val_zero(ctx);
730 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
731 isl_space_free(pnt_dim);
732 isl_point_free(pnt);
733 return v;
734error:
735 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
736 isl_space_free(pnt_dim);
737 isl_point_free(pnt);
738 return NULL((void*)0);
739}
740#endif
741
742/* Return the parameter domain of "pw".
743 */
744__isl_give isl_setisl_map *FN(PW,params)isl_pw_qpolynomial_fold_params(__isl_take PWisl_pw_qpolynomial_fold *pw)
745{
746 return isl_set_params(FN(PW,domain)isl_pw_qpolynomial_fold_domain(pw));
747}
748
749__isl_give isl_setisl_map *FN(PW,domain)isl_pw_qpolynomial_fold_domain(__isl_take PWisl_pw_qpolynomial_fold *pw)
750{
751 int i;
752 isl_setisl_map *dom;
753
754 if (!pw)
755 return NULL((void*)0);
756
757 dom = isl_set_empty(FN(PW,get_domain_space)isl_pw_qpolynomial_fold_get_domain_space(pw));
758 for (i = 0; i < pw->n; ++i)
759 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
760
761 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
762
763 return dom;
764}
765
766/* Exploit the equalities in the domain of piece "i" of "pw"
767 * to simplify the associated function.
768 * If the domain of piece "i" is empty, then remove it entirely,
769 * replacing it with the final piece.
770 */
771static int FN(PW,exploit_equalities_and_remove_if_empty)isl_pw_qpolynomial_fold_exploit_equalities_and_remove_if_empty(__isl_keep PWisl_pw_qpolynomial_fold *pw,
772 int i)
773{
774 isl_basic_setisl_basic_map *aff;
775 int empty = isl_set_plain_is_empty(pw->p[i].set);
776
777 if (empty < 0)
778 return -1;
779 if (empty) {
780 isl_set_free(pw->p[i].set);
781 FN(EL,free)isl_qpolynomial_fold_free(pw->p[i].FIELDfold);
782 if (i != pw->n - 1)
783 pw->p[i] = pw->p[pw->n - 1];
784 pw->n--;
785
786 return 0;
787 }
788
789 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
790 pw->p[i].FIELDfold = FN(EL,substitute_equalities)isl_qpolynomial_fold_substitute_equalities(pw->p[i].FIELDfold, aff);
791 if (!pw->p[i].FIELDfold)
792 return -1;
793
794 return 0;
795}
796
797/* Convert a piecewise expression defined over a parameter domain
798 * into one that is defined over a zero-dimensional set.
799 */
800__isl_give PWisl_pw_qpolynomial_fold *FN(PW,from_range)isl_pw_qpolynomial_fold_from_range(__isl_take PWisl_pw_qpolynomial_fold *pw)
801{
802 isl_space *space;
803
804 if (!pw)
805 return NULL((void*)0);
806 if (!isl_space_is_set(pw->dim))
807 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "not living in a set space", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 808); return isl_pw_qpolynomial_fold_free(pw); } while (0)
808 "not living in a set space", return FN(PW,free)(pw))do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "not living in a set space", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 808); return isl_pw_qpolynomial_fold_free(pw); } while (0)
;
809
810 space = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
811 space = isl_space_from_range(space);
812 pw = FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, space);
813
814 return pw;
815}
816
817/* Fix the value of the given parameter or domain dimension of "pw"
818 * to be equal to "value".
819 */
820__isl_give PWisl_pw_qpolynomial_fold *FN(PW,fix_si)isl_pw_qpolynomial_fold_fix_si(__isl_take PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
821 unsigned pos, int value)
822{
823 int i;
824
825 if (!pw)
826 return NULL((void*)0);
827
828 if (type == isl_dim_out)
829 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "cannot fix output dimension", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 830); return isl_pw_qpolynomial_fold_free(pw); } while (0)
830 "cannot fix output dimension", return FN(PW,free)(pw))do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "cannot fix output dimension", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 830); return isl_pw_qpolynomial_fold_free(pw); } while (0)
;
831
832 if (pw->n == 0)
833 return pw;
834
835 if (type == isl_dim_in)
836 type = isl_dim_set;
837
838 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
839 if (!pw)
840 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
841
842 for (i = pw->n - 1; i >= 0; --i) {
843 pw->p[i].set = isl_set_fix_si(pw->p[i].set, type, pos, value);
844 if (FN(PW,exploit_equalities_and_remove_if_empty)isl_pw_qpolynomial_fold_exploit_equalities_and_remove_if_empty(pw, i) < 0)
845 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
846 }
847
848 return pw;
849}
850
851/* Restrict the domain of "pw" by combining each cell
852 * with "set" through a call to "fn", where "fn" may be
853 * isl_set_intersect, isl_set_intersect_params or isl_set_subtract.
854 */
855static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,restrict_domain_aligned)isl_pw_qpolynomial_fold_restrict_domain_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
856 __isl_take isl_setisl_map *set,
857 __isl_give isl_setisl_map *(*fn)(__isl_take isl_setisl_map *set1,
858 __isl_take isl_setisl_map *set2))
859{
860 int i;
861
862 if (!pw || !set)
863 goto error;
864
865 if (pw->n == 0) {
866 isl_set_free(set);
867 return pw;
868 }
869
870 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
871 if (!pw)
872 goto error;
873
874 for (i = pw->n - 1; i >= 0; --i) {
875 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
876 if (FN(PW,exploit_equalities_and_remove_if_empty)isl_pw_qpolynomial_fold_exploit_equalities_and_remove_if_empty(pw, i) < 0)
877 goto error;
878 }
879
880 isl_set_free(set);
881 return pw;
882error:
883 isl_set_free(set);
884 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
885 return NULL((void*)0);
886}
887
888static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,intersect_domain_aligned)isl_pw_qpolynomial_fold_intersect_domain_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
889 __isl_take isl_setisl_map *set)
890{
891 return FN(PW,restrict_domain_aligned)isl_pw_qpolynomial_fold_restrict_domain_aligned(pw, set, &isl_set_intersect);
892}
893
894__isl_give PWisl_pw_qpolynomial_fold *FN(PW,intersect_domain)isl_pw_qpolynomial_fold_intersect_domain(__isl_take PWisl_pw_qpolynomial_fold *pw,
895 __isl_take isl_setisl_map *context)
896{
897 return FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(pw, context,
898 &FN(PW,intersect_domain_aligned)isl_pw_qpolynomial_fold_intersect_domain_aligned);
899}
900
901static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,intersect_params_aligned)isl_pw_qpolynomial_fold_intersect_params_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
902 __isl_take isl_setisl_map *set)
903{
904 return FN(PW,restrict_domain_aligned)isl_pw_qpolynomial_fold_restrict_domain_aligned(pw, set,
905 &isl_set_intersect_params);
906}
907
908/* Intersect the domain of "pw" with the parameter domain "context".
909 */
910__isl_give PWisl_pw_qpolynomial_fold *FN(PW,intersect_params)isl_pw_qpolynomial_fold_intersect_params(__isl_take PWisl_pw_qpolynomial_fold *pw,
911 __isl_take isl_setisl_map *context)
912{
913 return FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(pw, context,
914 &FN(PW,intersect_params_aligned)isl_pw_qpolynomial_fold_intersect_params_aligned);
915}
916
917/* Subtract "domain' from the domain of "pw", assuming their
918 * parameters have been aligned.
919 */
920static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,subtract_domain_aligned)isl_pw_qpolynomial_fold_subtract_domain_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
921 __isl_take isl_setisl_map *domain)
922{
923 return FN(PW,restrict_domain_aligned)isl_pw_qpolynomial_fold_restrict_domain_aligned(pw, domain, &isl_set_subtract);
924}
925
926/* Subtract "domain' from the domain of "pw".
927 */
928__isl_give PWisl_pw_qpolynomial_fold *FN(PW,subtract_domain)isl_pw_qpolynomial_fold_subtract_domain(__isl_take PWisl_pw_qpolynomial_fold *pw,
929 __isl_take isl_setisl_map *domain)
930{
931 return FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(pw, domain,
932 &FN(PW,subtract_domain_aligned)isl_pw_qpolynomial_fold_subtract_domain_aligned);
933}
934
935/* Compute the gist of "pw" with respect to the domain constraints
936 * of "context" for the case where the domain of the last element
937 * of "pw" is equal to "context".
938 * Call "fn_el" to compute the gist of this element, replace
939 * its domain by the universe and drop all other elements
940 * as their domains are necessarily disjoint from "context".
941 */
942static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist_last)isl_pw_qpolynomial_fold_gist_last(__isl_take PWisl_pw_qpolynomial_fold *pw,
943 __isl_take isl_setisl_map *context,
944 __isl_give ELisl_qpolynomial_fold *(*fn_el)(__isl_take ELisl_qpolynomial_fold *el, __isl_take isl_setisl_map *set))
945{
946 int i;
947 isl_space *space;
948
949 for (i = 0; i < pw->n - 1; ++i) {
950 isl_set_free(pw->p[i].set);
951 FN(EL,free)isl_qpolynomial_fold_free(pw->p[i].FIELDfold);
952 }
953 pw->p[0].FIELDfold = pw->p[pw->n - 1].FIELDfold;
954 pw->p[0].set = pw->p[pw->n - 1].set;
955 pw->n = 1;
956
957 space = isl_set_get_space(context);
958 pw->p[0].FIELDfold = fn_el(pw->p[0].FIELDfold, context);
959 context = isl_set_universe(space);
960 isl_set_free(pw->p[0].set);
961 pw->p[0].set = context;
962
963 if (!pw->p[0].FIELDfold || !pw->p[0].set)
964 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
965
966 return pw;
967}
968
969/* Compute the gist of "pw" with respect to the domain constraints
970 * of "context". Call "fn_el" to compute the gist of the elements
971 * and "fn_dom" to compute the gist of the domains.
972 *
973 * If the piecewise expression is empty or the context is the universe,
974 * then nothing can be simplified.
975 */
976static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist_aligned)isl_pw_qpolynomial_fold_gist_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
977 __isl_take isl_setisl_map *context,
978 __isl_give ELisl_qpolynomial_fold *(*fn_el)(__isl_take ELisl_qpolynomial_fold *el,
979 __isl_take isl_setisl_map *set),
980 __isl_give isl_setisl_map *(*fn_dom)(__isl_take isl_setisl_map *set,
981 __isl_take isl_basic_setisl_basic_map *bset))
982{
983 int i;
984 int is_universe;
985 isl_bool aligned;
986 isl_basic_setisl_basic_map *hull = NULL((void*)0);
987
988 if (!pw || !context)
989 goto error;
990
991 if (pw->n == 0) {
992 isl_set_free(context);
993 return pw;
994 }
995
996 is_universe = isl_set_plain_is_universe(context);
997 if (is_universe < 0)
998 goto error;
999 if (is_universe) {
1000 isl_set_free(context);
1001 return pw;
1002 }
1003
1004 aligned = isl_set_space_has_equal_params(context, pw->dim);
1005 if (aligned < 0)
1006 goto error;
1007 if (!aligned) {
1008 pw = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw, isl_set_get_space(context));
1009 context = isl_set_align_params(context, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
1010 }
1011
1012 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1013 if (!pw)
1014 goto error;
1015
1016 if (pw->n == 1) {
1017 int equal;
1018
1019 equal = isl_set_plain_is_equal(pw->p[0].set, context);
1020 if (equal < 0)
1021 goto error;
1022 if (equal)
1023 return FN(PW,gist_last)isl_pw_qpolynomial_fold_gist_last(pw, context, fn_el);
1024 }
1025
1026 context = isl_set_compute_divs(context);
1027 hull = isl_set_simple_hull(isl_set_copy(context));
1028
1029 for (i = pw->n - 1; i >= 0; --i) {
1030 isl_setisl_map *set_i;
1031 int empty;
1032
1033 if (i == pw->n - 1) {
1034 int equal;
1035 equal = isl_set_plain_is_equal(pw->p[i].set, context);
1036 if (equal < 0)
1037 goto error;
1038 if (equal) {
1039 isl_basic_set_free(hull);
1040 return FN(PW,gist_last)isl_pw_qpolynomial_fold_gist_last(pw, context, fn_el);
1041 }
1042 }
1043 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
1044 isl_set_copy(context));
1045 empty = isl_set_plain_is_empty(set_i);
1046 pw->p[i].FIELDfold = fn_el(pw->p[i].FIELDfold, set_i);
1047 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
1048 if (empty < 0 || !pw->p[i].FIELDfold || !pw->p[i].set)
1049 goto error;
1050 if (empty) {
1051 isl_set_free(pw->p[i].set);
1052 FN(EL,free)isl_qpolynomial_fold_free(pw->p[i].FIELDfold);
1053 if (i != pw->n - 1)
1054 pw->p[i] = pw->p[pw->n - 1];
1055 pw->n--;
1056 }
1057 }
1058
1059 isl_basic_set_free(hull);
1060 isl_set_free(context);
1061
1062 return pw;
1063error:
1064 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1065 isl_basic_set_free(hull);
1066 isl_set_free(context);
1067 return NULL((void*)0);
1068}
1069
1070static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist_domain_aligned)isl_pw_qpolynomial_fold_gist_domain_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
1071 __isl_take isl_setisl_map *set)
1072{
1073 return FN(PW,gist_aligned)isl_pw_qpolynomial_fold_gist_aligned(pw, set, &FN(EL,gist)isl_qpolynomial_fold_gist,
1074 &isl_set_gist_basic_set);
1075}
1076
1077__isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist)isl_pw_qpolynomial_fold_gist(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_setisl_map *context)
1078{
1079 return FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(pw, context,
1080 &FN(PW,gist_domain_aligned)isl_pw_qpolynomial_fold_gist_domain_aligned);
1081}
1082
1083static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist_params_aligned)isl_pw_qpolynomial_fold_gist_params_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
1084 __isl_take isl_setisl_map *set)
1085{
1086 return FN(PW,gist_aligned)isl_pw_qpolynomial_fold_gist_aligned(pw, set, &FN(EL,gist_params)isl_qpolynomial_fold_gist_params,
1087 &isl_set_gist_params_basic_set);
1088}
1089
1090__isl_give PWisl_pw_qpolynomial_fold *FN(PW,gist_params)isl_pw_qpolynomial_fold_gist_params(__isl_take PWisl_pw_qpolynomial_fold *pw,
1091 __isl_take isl_setisl_map *context)
1092{
1093 return FN(PW,align_params_pw_set_and)isl_pw_qpolynomial_fold_align_params_pw_set_and(pw, context,
1094 &FN(PW,gist_params_aligned)isl_pw_qpolynomial_fold_gist_params_aligned);
1095}
1096
1097/* Return -1 if the piece "p1" should be sorted before "p2"
1098 * and 1 if it should be sorted after "p2".
1099 * Return 0 if they do not need to be sorted in a specific order.
1100 *
1101 * The two pieces are compared on the basis of their function value expressions.
1102 */
1103static int FN(PW,sort_field_cmp)isl_pw_qpolynomial_fold_sort_field_cmp(const void *p1, const void *p2, void *arg)
1104{
1105 struct FN(PW,piece)isl_pw_qpolynomial_fold_piece const *pc1 = p1;
1106 struct FN(PW,piece)isl_pw_qpolynomial_fold_piece const *pc2 = p2;
1107
1108 return FN(EL,plain_cmp)isl_qpolynomial_fold_plain_cmp(pc1->FIELDfold, pc2->FIELDfold);
1109}
1110
1111/* Sort the pieces of "pw" according to their function value
1112 * expressions and then combine pairs of adjacent pieces with
1113 * the same such expression.
1114 *
1115 * The sorting is performed in place because it does not
1116 * change the meaning of "pw", but care needs to be
1117 * taken not to change any possible other copies of "pw"
1118 * in case anything goes wrong.
1119 */
1120__isl_give PWisl_pw_qpolynomial_fold *FN(PW,sort)isl_pw_qpolynomial_fold_sort(__isl_take PWisl_pw_qpolynomial_fold *pw)
1121{
1122 int i, j;
1123 isl_setisl_map *set;
1124
1125 if (!pw)
1126 return NULL((void*)0);
1127 if (pw->n <= 1)
1128 return pw;
1129 if (isl_sort(pw->p, pw->n, sizeof(pw->p[0]),
1130 &FN(PW,sort_field_cmp)isl_pw_qpolynomial_fold_sort_field_cmp, NULL((void*)0)) < 0)
1131 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1132 for (i = pw->n - 1; i >= 1; --i) {
1133 if (!FN(EL,plain_is_equal)isl_qpolynomial_fold_plain_is_equal(pw->p[i - 1].FIELDfold, pw->p[i].FIELDfold))
1134 continue;
1135 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1136 isl_set_copy(pw->p[i].set));
1137 if (!set)
1138 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1139 isl_set_free(pw->p[i].set);
1140 FN(EL,free)isl_qpolynomial_fold_free(pw->p[i].FIELDfold);
1141 isl_set_free(pw->p[i - 1].set);
1142 pw->p[i - 1].set = set;
1143 for (j = i + 1; j < pw->n; ++j)
1144 pw->p[j - 1] = pw->p[j];
1145 pw->n--;
1146 }
1147
1148 return pw;
1149}
1150
1151/* Coalesce the domains of "pw".
1152 *
1153 * Prior to the actual coalescing, first sort the pieces such that
1154 * pieces with the same function value expression are combined
1155 * into a single piece, the combined domain of which can then
1156 * be coalesced.
1157 */
1158__isl_give PWisl_pw_qpolynomial_fold *FN(PW,coalesce)isl_pw_qpolynomial_fold_coalesce(__isl_take PWisl_pw_qpolynomial_fold *pw)
1159{
1160 int i;
1161
1162 pw = FN(PW,sort)isl_pw_qpolynomial_fold_sort(pw);
1163 if (!pw)
1164 return NULL((void*)0);
1165
1166 for (i = 0; i < pw->n; ++i) {
1167 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
1168 if (!pw->p[i].set)
1169 goto error;
1170 }
1171
1172 return pw;
1173error:
1174 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1175 return NULL((void*)0);
1176}
1177
1178isl_ctx *FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(__isl_keep PWisl_pw_qpolynomial_fold *pw)
1179{
1180 return pw ? isl_space_get_ctx(pw->dim) : NULL((void*)0);
1181}
1182
1183#ifndef NO_INVOLVES_DIMS
1184isl_bool FN(PW,involves_dims)isl_pw_qpolynomial_fold_involves_dims(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
1185 unsigned first, unsigned n)
1186{
1187 int i;
1188 enum isl_dim_type set_type;
1189
1190 if (!pw)
1191 return isl_bool_error;
1192 if (pw->n == 0 || n == 0)
1193 return isl_bool_false;
1194
1195 set_type = type == isl_dim_in ? isl_dim_set : type;
1196
1197 for (i = 0; i < pw->n; ++i) {
1198 isl_bool involves = FN(EL,involves_dims)isl_qpolynomial_fold_involves_dims(pw->p[i].FIELDfold,
1199 type, first, n);
1200 if (involves < 0 || involves)
1201 return involves;
1202 involves = isl_set_involves_dims(pw->p[i].set,
1203 set_type, first, n);
1204 if (involves < 0 || involves)
1205 return involves;
1206 }
1207 return isl_bool_false;
1208}
1209#endif
1210
1211__isl_give PWisl_pw_qpolynomial_fold *FN(PW,set_dim_name)isl_pw_qpolynomial_fold_set_dim_name(__isl_take PWisl_pw_qpolynomial_fold *pw,
1212 enum isl_dim_type type, unsigned pos, const char *s)
1213{
1214 int i;
1215 enum isl_dim_type set_type;
1216
1217 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1218 if (!pw)
1219 return NULL((void*)0);
1220
1221 set_type = type == isl_dim_in ? isl_dim_set : type;
1222
1223 pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
1224 if (!pw->dim)
1225 goto error;
1226
1227 for (i = 0; i < pw->n; ++i) {
1228 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
1229 set_type, pos, s);
1230 if (!pw->p[i].set)
1231 goto error;
1232 pw->p[i].FIELDfold = FN(EL,set_dim_name)isl_qpolynomial_fold_set_dim_name(pw->p[i].FIELDfold, type, pos, s);
1233 if (!pw->p[i].FIELDfold)
1234 goto error;
1235 }
1236
1237 return pw;
1238error:
1239 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1240 return NULL((void*)0);
1241}
1242
1243#ifndef NO_DROP_DIMS
1244__isl_give PWisl_pw_qpolynomial_fold *FN(PW,drop_dims)isl_pw_qpolynomial_fold_drop_dims(__isl_take PWisl_pw_qpolynomial_fold *pw,
1245 enum isl_dim_type type, unsigned first, unsigned n)
1246{
1247 int i;
1248 enum isl_dim_type set_type;
1249
1250 if (!pw)
1251 return NULL((void*)0);
1252 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1253 return pw;
1254
1255 set_type = type == isl_dim_in ? isl_dim_set : type;
1256
1257 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1258 if (!pw)
1259 return NULL((void*)0);
1260 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1261 if (!pw->dim)
1262 goto error;
1263 for (i = 0; i < pw->n; ++i) {
1264 pw->p[i].FIELDfold = FN(EL,drop_dims)isl_qpolynomial_fold_drop_dims(pw->p[i].FIELDfold, type, first, n);
1265 if (!pw->p[i].FIELDfold)
1266 goto error;
1267 if (type == isl_dim_out)
1268 continue;
1269 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
1270 if (!pw->p[i].set)
1271 goto error;
1272 }
1273
1274 return pw;
1275error:
1276 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1277 return NULL((void*)0);
1278}
1279
1280/* This function is very similar to drop_dims.
1281 * The only difference is that the cells may still involve
1282 * the specified dimensions. They are removed using
1283 * isl_set_project_out instead of isl_set_drop.
1284 */
1285__isl_give PWisl_pw_qpolynomial_fold *FN(PW,project_out)isl_pw_qpolynomial_fold_project_out(__isl_take PWisl_pw_qpolynomial_fold *pw,
1286 enum isl_dim_type type, unsigned first, unsigned n)
1287{
1288 int i;
1289 enum isl_dim_type set_type;
1290
1291 if (!pw)
1292 return NULL((void*)0);
1293 if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1294 return pw;
1295
1296 set_type = type == isl_dim_in ? isl_dim_set : type;
1297
1298 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1299 if (!pw)
1300 return NULL((void*)0);
1301 pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1302 if (!pw->dim)
1303 goto error;
1304 for (i = 0; i < pw->n; ++i) {
1305 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1306 set_type, first, n);
1307 if (!pw->p[i].set)
1308 goto error;
1309 pw->p[i].FIELDfold = FN(EL,drop_dims)isl_qpolynomial_fold_drop_dims(pw->p[i].FIELDfold, type, first, n);
1310 if (!pw->p[i].FIELDfold)
1311 goto error;
1312 }
1313
1314 return pw;
1315error:
1316 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1317 return NULL((void*)0);
1318}
1319
1320/* Project the domain of pw onto its parameter space.
1321 */
1322__isl_give PWisl_pw_qpolynomial_fold *FN(PW,project_domain_on_params)isl_pw_qpolynomial_fold_project_domain_on_params(__isl_take PWisl_pw_qpolynomial_fold *pw)
1323{
1324 isl_space *space;
1325 unsigned n;
1326
1327 n = FN(PW,dim)isl_pw_qpolynomial_fold_dim(pw, isl_dim_in);
1328 pw = FN(PW,project_out)isl_pw_qpolynomial_fold_project_out(pw, isl_dim_in, 0, n);
1329 space = FN(PW,get_domain_space)isl_pw_qpolynomial_fold_get_domain_space(pw);
1330 space = isl_space_params(space);
1331 pw = FN(PW,reset_domain_space)isl_pw_qpolynomial_fold_reset_domain_space(pw, space);
1332 return pw;
1333}
1334#endif
1335
1336#ifndef NO_INSERT_DIMS
1337__isl_give PWisl_pw_qpolynomial_fold *FN(PW,insert_dims)isl_pw_qpolynomial_fold_insert_dims(__isl_take PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
1338 unsigned first, unsigned n)
1339{
1340 int i;
1341 enum isl_dim_type set_type;
1342
1343 if (!pw)
1344 return NULL((void*)0);
1345 if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1346 return pw;
1347
1348 set_type = type == isl_dim_in ? isl_dim_set : type;
1349
1350 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1351 if (!pw)
1352 return NULL((void*)0);
1353
1354 pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1355 if (!pw->dim)
1356 goto error;
1357
1358 for (i = 0; i < pw->n; ++i) {
1359 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1360 set_type, first, n);
1361 if (!pw->p[i].set)
1362 goto error;
1363 pw->p[i].FIELDfold = FN(EL,insert_dims)isl_qpolynomial_fold_insert_dims(pw->p[i].FIELDfold,
1364 type, first, n);
1365 if (!pw->p[i].FIELDfold)
1366 goto error;
1367 }
1368
1369 return pw;
1370error:
1371 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1372 return NULL((void*)0);
1373}
1374#endif
1375
1376__isl_give PWisl_pw_qpolynomial_fold *FN(PW,fix_dim)isl_pw_qpolynomial_fold_fix_dim(__isl_take PWisl_pw_qpolynomial_fold *pw,
1377 enum isl_dim_type type, unsigned pos, isl_int v)
1378{
1379 int i;
1380
1381 if (!pw)
1382 return NULL((void*)0);
1383
1384 if (type == isl_dim_in)
1385 type = isl_dim_set;
1386
1387 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1388 if (!pw)
1389 return NULL((void*)0);
1390 for (i = 0; i < pw->n; ++i) {
1391 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1392 if (FN(PW,exploit_equalities_and_remove_if_empty)isl_pw_qpolynomial_fold_exploit_equalities_and_remove_if_empty(pw, i) < 0)
1393 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1394 }
1395
1396 return pw;
1397}
1398
1399/* Fix the value of the variable at position "pos" of type "type" of "pw"
1400 * to be equal to "v".
1401 */
1402__isl_give PWisl_pw_qpolynomial_fold *FN(PW,fix_val)isl_pw_qpolynomial_fold_fix_val(__isl_take PWisl_pw_qpolynomial_fold *pw,
1403 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
1404{
1405 if (!v)
1406 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1407 if (!isl_val_is_int(v))
1408 isl_die(FN(PW,get_ctx)(pw), isl_error_invalid,do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1409); goto error; } while (0)
1409 "expecting integer value", goto error)do { isl_handle_error(isl_pw_qpolynomial_fold_get_ctx(pw), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1409); goto error; } while (0)
;
1410
1411 pw = FN(PW,fix_dim)isl_pw_qpolynomial_fold_fix_dim(pw, type, pos, v->n);
1412 isl_val_free(v);
1413
1414 return pw;
1415error:
1416 isl_val_free(v);
1417 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1418}
1419
1420unsigned FN(PW,dim)isl_pw_qpolynomial_fold_dim(__isl_keep PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
1421{
1422 return pw ? isl_space_dim(pw->dim, type) : 0;
1423}
1424
1425__isl_give PWisl_pw_qpolynomial_fold *FN(PW,split_dims)isl_pw_qpolynomial_fold_split_dims(__isl_take PWisl_pw_qpolynomial_fold *pw,
1426 enum isl_dim_type type, unsigned first, unsigned n)
1427{
1428 int i;
1429
1430 if (!pw)
1431 return NULL((void*)0);
1432 if (n == 0)
1433 return pw;
1434
1435 if (type == isl_dim_in)
1436 type = isl_dim_set;
1437
1438 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1439 if (!pw)
1440 return NULL((void*)0);
1441 if (!pw->dim)
1442 goto error;
1443 for (i = 0; i < pw->n; ++i) {
1444 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1445 if (!pw->p[i].set)
1446 goto error;
1447 }
1448
1449 return pw;
1450error:
1451 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1452 return NULL((void*)0);
1453}
1454
1455#ifndef NO_OPT
1456/* Compute the maximal value attained by the piecewise quasipolynomial
1457 * on its domain or zero if the domain is empty.
1458 * In the worst case, the domain is scanned completely,
1459 * so the domain is assumed to be bounded.
1460 */
1461__isl_give isl_val *FN(PW,opt)isl_pw_qpolynomial_fold_opt(__isl_take PWisl_pw_qpolynomial_fold *pw, int max)
1462{
1463 int i;
1464 isl_val *opt;
1465
1466 if (!pw)
1467 return NULL((void*)0);
1468
1469 if (pw->n == 0) {
1470 opt = isl_val_zero(FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw));
1471 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1472 return opt;
1473 }
1474
1475 opt = FN(EL,opt_on_domain)isl_qpolynomial_fold_opt_on_domain(FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[0].FIELDfold),
1476 isl_set_copy(pw->p[0].set), max);
1477 for (i = 1; i < pw->n; ++i) {
1478 isl_val *opt_i;
1479 opt_i = FN(EL,opt_on_domain)isl_qpolynomial_fold_opt_on_domain(FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold),
1480 isl_set_copy(pw->p[i].set), max);
1481 if (max)
1482 opt = isl_val_max(opt, opt_i);
1483 else
1484 opt = isl_val_min(opt, opt_i);
1485 }
1486
1487 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1488 return opt;
1489}
1490
1491__isl_give isl_val *FN(PW,max)isl_pw_qpolynomial_fold_max(__isl_take PWisl_pw_qpolynomial_fold *pw)
1492{
1493 return FN(PW,opt)isl_pw_qpolynomial_fold_opt(pw, 1);
1494}
1495
1496__isl_give isl_val *FN(PW,min)isl_pw_qpolynomial_fold_min(__isl_take PWisl_pw_qpolynomial_fold *pw)
1497{
1498 return FN(PW,opt)isl_pw_qpolynomial_fold_opt(pw, 0);
1499}
1500#endif
1501
1502/* Return the space of "pw".
1503 */
1504__isl_keep isl_space *FN(PW,peek_space)isl_pw_qpolynomial_fold_peek_space(__isl_keep PWisl_pw_qpolynomial_fold *pw)
1505{
1506 return pw ? pw->dim : NULL((void*)0);
1507}
1508
1509__isl_give isl_space *FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(__isl_keep PWisl_pw_qpolynomial_fold *pw)
1510{
1511 return isl_space_copy(FN(PW,peek_space)isl_pw_qpolynomial_fold_peek_space(pw));
1512}
1513
1514__isl_give isl_space *FN(PW,get_domain_space)isl_pw_qpolynomial_fold_get_domain_space(__isl_keep PWisl_pw_qpolynomial_fold *pw)
1515{
1516 return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL((void*)0);
1517}
1518
1519/* Return the position of the dimension of the given type and name
1520 * in "pw".
1521 * Return -1 if no such dimension can be found.
1522 */
1523int FN(PW,find_dim_by_name)isl_pw_qpolynomial_fold_find_dim_by_name(__isl_keep PWisl_pw_qpolynomial_fold *pw,
1524 enum isl_dim_type type, const char *name)
1525{
1526 if (!pw)
1527 return -1;
1528 return isl_space_find_dim_by_name(pw->dim, type, name);
1529}
1530
1531#ifndef NO_RESET_DIM
1532/* Reset the space of "pw". Since we don't know if the elements
1533 * represent the spaces themselves or their domains, we pass along
1534 * both when we call their reset_space_and_domain.
1535 */
1536static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,reset_space_and_domain)isl_pw_qpolynomial_fold_reset_space_and_domain(__isl_take PWisl_pw_qpolynomial_fold *pw,
1537 __isl_take isl_space *space, __isl_take isl_space *domain)
1538{
1539 int i;
1540
1541 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1542 if (!pw || !space || !domain)
1543 goto error;
1544
1545 for (i = 0; i < pw->n; ++i) {
1546 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1547 isl_space_copy(domain));
1548 if (!pw->p[i].set)
1549 goto error;
1550 pw->p[i].FIELDfold = FN(EL,reset_space_and_domain)isl_qpolynomial_fold_reset_space_and_domain(pw->p[i].FIELDfold,
1551 isl_space_copy(space), isl_space_copy(domain));
1552 if (!pw->p[i].FIELDfold)
1553 goto error;
1554 }
1555
1556 isl_space_free(domain);
1557
1558 isl_space_free(pw->dim);
1559 pw->dim = space;
1560
1561 return pw;
1562error:
1563 isl_space_free(domain);
1564 isl_space_free(space);
1565 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1566 return NULL((void*)0);
1567}
1568
1569__isl_give PWisl_pw_qpolynomial_fold *FN(PW,reset_domain_space)isl_pw_qpolynomial_fold_reset_domain_space(__isl_take PWisl_pw_qpolynomial_fold *pw,
1570 __isl_take isl_space *domain)
1571{
1572 isl_space *space;
1573
1574 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1575 FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
1576 return FN(PW,reset_space_and_domain)isl_pw_qpolynomial_fold_reset_space_and_domain(pw, space, domain);
1577}
1578
1579__isl_give PWisl_pw_qpolynomial_fold *FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_space *dim)
1580{
1581 isl_space *domain;
1582
1583 domain = isl_space_domain(isl_space_copy(dim));
1584 return FN(PW,reset_space_and_domain)isl_pw_qpolynomial_fold_reset_space_and_domain(pw, dim, domain);
1585}
1586
1587__isl_give PWisl_pw_qpolynomial_fold *FN(PW,set_tuple_id)isl_pw_qpolynomial_fold_set_tuple_id(__isl_take PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type,
1588 __isl_take isl_id *id)
1589{
1590 isl_space *space;
1591
1592 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1593 if (!pw)
1594 goto error;
1595
1596 space = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
1597 space = isl_space_set_tuple_id(space, type, id);
1598
1599 return FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, space);
1600error:
1601 isl_id_free(id);
1602 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1603}
1604
1605/* Drop the id on the specified tuple.
1606 */
1607__isl_give PWisl_pw_qpolynomial_fold *FN(PW,reset_tuple_id)isl_pw_qpolynomial_fold_reset_tuple_id(__isl_take PWisl_pw_qpolynomial_fold *pw, enum isl_dim_type type)
1608{
1609 isl_space *space;
1610
1611 if (!pw)
1612 return NULL((void*)0);
1613 if (!FN(PW,has_tuple_id)isl_pw_qpolynomial_fold_has_tuple_id(pw, type))
1614 return pw;
1615
1616 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1617 if (!pw)
1618 return NULL((void*)0);
1619
1620 space = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
1621 space = isl_space_reset_tuple_id(space, type);
1622
1623 return FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, space);
1624}
1625
1626__isl_give PWisl_pw_qpolynomial_fold *FN(PW,set_dim_id)isl_pw_qpolynomial_fold_set_dim_id(__isl_take PWisl_pw_qpolynomial_fold *pw,
1627 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1628{
1629 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1630 if (!pw)
1631 goto error;
1632 pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1633 return FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, isl_space_copy(pw->dim));
1634error:
1635 isl_id_free(id);
1636 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1637}
1638#endif
1639
1640/* Reset the user pointer on all identifiers of parameters and tuples
1641 * of the space of "pw".
1642 */
1643__isl_give PWisl_pw_qpolynomial_fold *FN(PW,reset_user)isl_pw_qpolynomial_fold_reset_user(__isl_take PWisl_pw_qpolynomial_fold *pw)
1644{
1645 isl_space *space;
1646
1647 space = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
1648 space = isl_space_reset_user(space);
1649
1650 return FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, space);
1651}
1652
1653isl_bool FN(PW,has_equal_space)isl_pw_qpolynomial_fold_has_equal_space(__isl_keep PWisl_pw_qpolynomial_fold *pw1, __isl_keep PWisl_pw_qpolynomial_fold *pw2)
1654{
1655 if (!pw1 || !pw2)
1656 return isl_bool_error;
1657
1658 return isl_space_is_equal(pw1->dim, pw2->dim);
1659}
1660
1661#ifndef NO_MORPH
1662__isl_give PWisl_pw_qpolynomial_fold *FN(PW,morph_domain)isl_pw_qpolynomial_fold_morph_domain(__isl_take PWisl_pw_qpolynomial_fold *pw,
1663 __isl_take isl_morph *morph)
1664{
1665 int i;
1666 isl_ctx *ctx;
1667
1668 if (!pw || !morph)
1669 goto error;
1670
1671 ctx = isl_space_get_ctx(pw->dim);
1672 isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),do { if (isl_space_is_domain_internal(morph->dom->dim, pw
->dim)) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_domain_internal(morph->dom->dim, pw->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1673); goto error; } while (0); } while (0)
1673 goto error)do { if (isl_space_is_domain_internal(morph->dom->dim, pw
->dim)) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "isl_space_is_domain_internal(morph->dom->dim, pw->dim)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1673); goto error; } while (0); } while (0)
;
1674
1675 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1676 if (!pw)
1677 goto error;
1678 pw->dim = isl_space_extend_domain_with_range(
1679 isl_space_copy(morph->ran->dim), pw->dim);
1680 if (!pw->dim)
1681 goto error;
1682
1683 for (i = 0; i < pw->n; ++i) {
1684 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1685 if (!pw->p[i].set)
1686 goto error;
1687 pw->p[i].FIELDfold = FN(EL,morph_domain)isl_qpolynomial_fold_morph_domain(pw->p[i].FIELDfold,
1688 isl_morph_copy(morph));
1689 if (!pw->p[i].FIELDfold)
1690 goto error;
1691 }
1692
1693 isl_morph_free(morph);
1694
1695 return pw;
1696error:
1697 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1698 isl_morph_free(morph);
1699 return NULL((void*)0);
1700}
1701#endif
1702
1703int FN(PW,n_piece)isl_pw_qpolynomial_fold_n_piece(__isl_keep PWisl_pw_qpolynomial_fold *pw)
1704{
1705 return pw ? pw->n : 0;
1706}
1707
1708isl_stat FN(PW,foreach_piece)isl_pw_qpolynomial_fold_foreach_piece(__isl_keep PWisl_pw_qpolynomial_fold *pw,
1709 isl_stat (*fn)(__isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el, void *user),
1710 void *user)
1711{
1712 int i;
1713
1714 if (!pw)
1715 return isl_stat_error;
1716
1717 for (i = 0; i < pw->n; ++i)
1718 if (fn(isl_set_copy(pw->p[i].set),
1719 FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold), user) < 0)
1720 return isl_stat_error;
1721
1722 return isl_stat_ok;
1723}
1724
1725#ifndef NO_LIFT
1726static isl_bool any_divs(__isl_keep isl_setisl_map *set)
1727{
1728 int i;
1729
1730 if (!set)
1731 return isl_bool_error;
1732
1733 for (i = 0; i < set->n; ++i)
1734 if (set->p[i]->n_div > 0)
1735 return isl_bool_true;
1736
1737 return isl_bool_false;
1738}
1739
1740static isl_stat foreach_lifted_subset(__isl_take isl_setisl_map *set,
1741 __isl_take ELisl_qpolynomial_fold *el,
1742 isl_stat (*fn)(__isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el,
1743 void *user), void *user)
1744{
1745 int i;
1746
1747 if (!set || !el)
1748 goto error;
1749
1750 for (i = 0; i < set->n; ++i) {
1751 isl_setisl_map *lift;
1752 ELisl_qpolynomial_fold *copy;
1753
1754 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1755 lift = isl_set_lift(lift);
1756
1757 copy = FN(EL,copy)isl_qpolynomial_fold_copy(el);
1758 copy = FN(EL,lift)isl_qpolynomial_fold_lift(copy, isl_set_get_space(lift));
1759
1760 if (fn(lift, copy, user) < 0)
1761 goto error;
1762 }
1763
1764 isl_set_free(set);
1765 FN(EL,free)isl_qpolynomial_fold_free(el);
1766
1767 return isl_stat_ok;
1768error:
1769 isl_set_free(set);
1770 FN(EL,free)isl_qpolynomial_fold_free(el);
1771 return isl_stat_error;
1772}
1773
1774isl_stat FN(PW,foreach_lifted_piece)isl_pw_qpolynomial_fold_foreach_lifted_piece(__isl_keep PWisl_pw_qpolynomial_fold *pw,
1775 isl_stat (*fn)(__isl_take isl_setisl_map *set, __isl_take ELisl_qpolynomial_fold *el,
1776 void *user), void *user)
1777{
1778 int i;
1779
1780 if (!pw)
1781 return isl_stat_error;
1782
1783 for (i = 0; i < pw->n; ++i) {
1784 isl_bool any;
1785 isl_setisl_map *set;
1786 ELisl_qpolynomial_fold *el;
1787
1788 any = any_divs(pw->p[i].set);
1789 if (any < 0)
1790 return isl_stat_error;
1791 set = isl_set_copy(pw->p[i].set);
1792 el = FN(EL,copy)isl_qpolynomial_fold_copy(pw->p[i].FIELDfold);
1793 if (!any) {
1794 if (fn(set, el, user) < 0)
1795 return isl_stat_error;
1796 continue;
1797 }
1798 if (foreach_lifted_subset(set, el, fn, user) < 0)
1799 return isl_stat_error;
1800 }
1801
1802 return isl_stat_ok;
1803}
1804#endif
1805
1806#ifndef NO_MOVE_DIMS
1807__isl_give PWisl_pw_qpolynomial_fold *FN(PW,move_dims)isl_pw_qpolynomial_fold_move_dims(__isl_take PWisl_pw_qpolynomial_fold *pw,
1808 enum isl_dim_type dst_type, unsigned dst_pos,
1809 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1810{
1811 int i;
1812
1813 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1814 if (!pw)
1815 return NULL((void*)0);
1816
1817 pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1818 if (!pw->dim)
1819 goto error;
1820
1821 for (i = 0; i < pw->n; ++i) {
1822 pw->p[i].FIELDfold = FN(EL,move_dims)isl_qpolynomial_fold_move_dims(pw->p[i].FIELDfold,
1823 dst_type, dst_pos, src_type, src_pos, n);
1824 if (!pw->p[i].FIELDfold)
1825 goto error;
1826 }
1827
1828 if (dst_type == isl_dim_in)
1829 dst_type = isl_dim_set;
1830 if (src_type == isl_dim_in)
1831 src_type = isl_dim_set;
1832
1833 for (i = 0; i < pw->n; ++i) {
1834 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1835 dst_type, dst_pos,
1836 src_type, src_pos, n);
1837 if (!pw->p[i].set)
1838 goto error;
1839 }
1840
1841 return pw;
1842error:
1843 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1844 return NULL((void*)0);
1845}
1846#endif
1847
1848__isl_give PWisl_pw_qpolynomial_fold *FN(PW,mul_isl_int)isl_pw_qpolynomial_fold_mul_isl_int(__isl_take PWisl_pw_qpolynomial_fold *pw, isl_int v)
1849{
1850 int i;
1851
1852 if (isl_int_is_one(v)(isl_sioimath_cmp_si(*(v), 1) == 0))
1853 return pw;
1854 if (pw && DEFAULT_IS_ZERO1 && isl_int_is_zero(v)(isl_sioimath_sgn(*(v)) == 0)) {
1855 PWisl_pw_qpolynomial_fold *zero;
1856 isl_space *dim = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
1857#ifdef HAS_TYPE
1858 zero = FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(dim, pw->type);
1859#else
1860 zero = FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(dim);
1861#endif
1862 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1863 return zero;
1864 }
1865 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1866 if (!pw)
1867 return NULL((void*)0);
1868 if (pw->n == 0)
1869 return pw;
1870
1871#ifdef HAS_TYPE
1872 if (isl_int_is_neg(v)(isl_sioimath_sgn(*(v)) < 0))
1873 pw->type = isl_fold_type_negate(pw->type);
1874#endif
1875 for (i = 0; i < pw->n; ++i) {
1876 pw->p[i].FIELDfold = FN(EL,scale)isl_qpolynomial_fold_scale(pw->p[i].FIELDfold, v);
1877 if (!pw->p[i].FIELDfold)
1878 goto error;
1879 }
1880
1881 return pw;
1882error:
1883 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1884 return NULL((void*)0);
1885}
1886
1887/* Multiply the pieces of "pw" by "v" and return the result.
1888 */
1889__isl_give PWisl_pw_qpolynomial_fold *FN(PW,scale_val)isl_pw_qpolynomial_fold_scale_val(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_val *v)
1890{
1891 int i;
1892
1893 if (!pw || !v)
1894 goto error;
1895
1896 if (isl_val_is_one(v)) {
1897 isl_val_free(v);
1898 return pw;
1899 }
1900 if (pw && DEFAULT_IS_ZERO1 && isl_val_is_zero(v)) {
1901 PWisl_pw_qpolynomial_fold *zero;
1902 isl_space *space = FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw);
1903#ifdef HAS_TYPE
1904 zero = FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(space, pw->type);
1905#else
1906 zero = FN(PW,ZERO)isl_pw_qpolynomial_fold_zero(space);
1907#endif
1908 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1909 isl_val_free(v);
1910 return zero;
1911 }
1912 if (pw->n == 0) {
1913 isl_val_free(v);
1914 return pw;
1915 }
1916 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1917 if (!pw)
1918 goto error;
1919
1920#ifdef HAS_TYPE
1921 if (isl_val_is_neg(v))
1922 pw->type = isl_fold_type_negate(pw->type);
1923#endif
1924 for (i = 0; i < pw->n; ++i) {
1925 pw->p[i].FIELDfold = FN(EL,scale_val)isl_qpolynomial_fold_scale_val(pw->p[i].FIELDfold,
1926 isl_val_copy(v));
1927 if (!pw->p[i].FIELDfold)
1928 goto error;
1929 }
1930
1931 isl_val_free(v);
1932 return pw;
1933error:
1934 isl_val_free(v);
1935 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1936 return NULL((void*)0);
1937}
1938
1939/* Divide the pieces of "pw" by "v" and return the result.
1940 */
1941__isl_give PWisl_pw_qpolynomial_fold *FN(PW,scale_down_val)isl_pw_qpolynomial_fold_scale_down_val(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_val *v)
1942{
1943 int i;
1944
1945 if (!pw || !v)
1946 goto error;
1947
1948 if (isl_val_is_one(v)) {
1949 isl_val_free(v);
1950 return pw;
1951 }
1952
1953 if (!isl_val_is_rat(v))
1954 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1955); goto error; } while (0)
1955 "expecting rational factor", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "expecting rational factor"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1955); goto error; } while (0)
;
1956 if (isl_val_is_zero(v))
1957 isl_die(isl_val_get_ctx(v), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1958); goto error; } while (0)
1958 "cannot scale down by zero", goto error)do { isl_handle_error(isl_val_get_ctx(v), isl_error_invalid, "cannot scale down by zero"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 1958); goto error; } while (0)
;
1959
1960 if (pw->n == 0) {
1961 isl_val_free(v);
1962 return pw;
1963 }
1964 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
1965 if (!pw)
1966 goto error;
1967
1968#ifdef HAS_TYPE
1969 if (isl_val_is_neg(v))
1970 pw->type = isl_fold_type_negate(pw->type);
1971#endif
1972 for (i = 0; i < pw->n; ++i) {
1973 pw->p[i].FIELDfold = FN(EL,scale_down_val)isl_qpolynomial_fold_scale_down_val(pw->p[i].FIELDfold,
1974 isl_val_copy(v));
1975 if (!pw->p[i].FIELDfold)
1976 goto error;
1977 }
1978
1979 isl_val_free(v);
1980 return pw;
1981error:
1982 isl_val_free(v);
1983 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
1984 return NULL((void*)0);
1985}
1986
1987__isl_give PWisl_pw_qpolynomial_fold *FN(PW,scale)isl_pw_qpolynomial_fold_scale(__isl_take PWisl_pw_qpolynomial_fold *pw, isl_int v)
1988{
1989 return FN(PW,mul_isl_int)isl_pw_qpolynomial_fold_mul_isl_int(pw, v);
1990}
1991
1992/* Apply some normalization to "pw".
1993 * In particular, sort the pieces according to their function value
1994 * expressions, combining pairs of adjacent pieces with
1995 * the same such expression, and then normalize the domains of the pieces.
1996 *
1997 * We normalize in place, but if anything goes wrong we need
1998 * to return NULL, so we need to make sure we don't change the
1999 * meaning of any possible other copies of "pw".
2000 */
2001__isl_give PWisl_pw_qpolynomial_fold *FN(PW,normalize)isl_pw_qpolynomial_fold_normalize(__isl_take PWisl_pw_qpolynomial_fold *pw)
2002{
2003 int i;
2004 isl_setisl_map *set;
2005
2006 pw = FN(PW,sort)isl_pw_qpolynomial_fold_sort(pw);
2007 if (!pw)
2008 return NULL((void*)0);
2009 for (i = 0; i < pw->n; ++i) {
2010 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
2011 if (!set)
2012 return FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2013 isl_set_free(pw->p[i].set);
2014 pw->p[i].set = set;
2015 }
2016
2017 return pw;
2018}
2019
2020/* Is pw1 obviously equal to pw2?
2021 * That is, do they have obviously identical cells and obviously identical
2022 * elements on each cell?
2023 *
2024 * If "pw1" or "pw2" contain any NaNs, then they are considered
2025 * not to be the same. A NaN is not equal to anything, not even
2026 * to another NaN.
2027 */
2028isl_bool FN(PW,plain_is_equal)isl_pw_qpolynomial_fold_plain_is_equal(__isl_keep PWisl_pw_qpolynomial_fold *pw1, __isl_keep PWisl_pw_qpolynomial_fold *pw2)
2029{
2030 int i;
2031 isl_bool equal, has_nan;
2032
2033 if (!pw1 || !pw2)
2034 return isl_bool_error;
2035
2036 has_nan = FN(PW,involves_nan)isl_pw_qpolynomial_fold_involves_nan(pw1);
2037 if (has_nan >= 0 && !has_nan)
2038 has_nan = FN(PW,involves_nan)isl_pw_qpolynomial_fold_involves_nan(pw2);
2039 if (has_nan < 0 || has_nan)
2040 return isl_bool_not(has_nan);
2041
2042 if (pw1 == pw2)
2043 return isl_bool_true;
2044 if (!isl_space_is_equal(pw1->dim, pw2->dim))
2045 return isl_bool_false;
2046
2047 pw1 = FN(PW,copy)isl_pw_qpolynomial_fold_copy(pw1);
2048 pw2 = FN(PW,copy)isl_pw_qpolynomial_fold_copy(pw2);
2049 pw1 = FN(PW,normalize)isl_pw_qpolynomial_fold_normalize(pw1);
2050 pw2 = FN(PW,normalize)isl_pw_qpolynomial_fold_normalize(pw2);
2051 if (!pw1 || !pw2)
2052 goto error;
2053
2054 equal = pw1->n == pw2->n;
2055 for (i = 0; equal && i < pw1->n; ++i) {
2056 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
2057 if (equal < 0)
2058 goto error;
2059 if (!equal)
2060 break;
2061 equal = FN(EL,plain_is_equal)isl_qpolynomial_fold_plain_is_equal(pw1->p[i].FIELDfold, pw2->p[i].FIELDfold);
2062 if (equal < 0)
2063 goto error;
2064 }
2065
2066 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
2067 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
2068 return equal;
2069error:
2070 FN(PW,free)isl_pw_qpolynomial_fold_free(pw1);
2071 FN(PW,free)isl_pw_qpolynomial_fold_free(pw2);
2072 return isl_bool_error;
2073}
2074
2075/* Does "pw" involve any NaNs?
2076 */
2077isl_bool FN(PW,involves_nan)isl_pw_qpolynomial_fold_involves_nan(__isl_keep PWisl_pw_qpolynomial_fold *pw)
2078{
2079 int i;
2080
2081 if (!pw)
2082 return isl_bool_error;
2083 if (pw->n == 0)
2084 return isl_bool_false;
2085
2086 for (i = 0; i < pw->n; ++i) {
2087 isl_bool has_nan = FN(EL,involves_nan)isl_qpolynomial_fold_is_nan(pw->p[i].FIELDfold);
2088 if (has_nan < 0 || has_nan)
2089 return has_nan;
2090 }
2091
2092 return isl_bool_false;
2093}
2094
2095#ifndef NO_PULLBACK
2096static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,align_params_pw_multi_aff_and)isl_pw_qpolynomial_fold_align_params_pw_multi_aff_and(__isl_take PWisl_pw_qpolynomial_fold *pw,
2097 __isl_take isl_multi_aff *ma,
2098 __isl_give PWisl_pw_qpolynomial_fold *(*fn)(__isl_take PWisl_pw_qpolynomial_fold *pw, __isl_take isl_multi_aff *ma))
2099{
2100 isl_ctx *ctx;
2101 isl_bool equal_params;
2102 isl_space *ma_space;
2103
2104 ma_space = isl_multi_aff_get_space(ma);
2105 if (!pw || !ma || !ma_space)
2106 goto error;
2107 equal_params = isl_space_has_equal_params(pw->dim, ma_space);
2108 if (equal_params < 0)
2109 goto error;
2110 if (equal_params) {
2111 isl_space_free(ma_space);
2112 return fn(pw, ma);
2113 }
2114 ctx = FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw);
2115 if (!isl_space_has_named_params(pw->dim) ||
2116 !isl_space_has_named_params(ma_space))
2117 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 2118); goto error; } while (0)
2118 "unaligned unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 2118); goto error; } while (0)
;
2119 pw = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw, ma_space);
2120 ma = isl_multi_aff_align_params(ma, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
2121 return fn(pw, ma);
2122error:
2123 isl_space_free(ma_space);
2124 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2125 isl_multi_aff_free(ma);
2126 return NULL((void*)0);
2127}
2128
2129static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,align_params_pw_pw_multi_aff_and)isl_pw_qpolynomial_fold_align_params_pw_pw_multi_aff_and(__isl_take PWisl_pw_qpolynomial_fold *pw,
2130 __isl_take isl_pw_multi_aff *pma,
2131 __isl_give PWisl_pw_qpolynomial_fold *(*fn)(__isl_take PWisl_pw_qpolynomial_fold *pw,
2132 __isl_take isl_pw_multi_aff *ma))
2133{
2134 isl_ctx *ctx;
2135 isl_bool equal_params;
2136 isl_space *pma_space;
2137
2138 pma_space = isl_pw_multi_aff_get_space(pma);
2139 if (!pw || !pma || !pma_space)
2140 goto error;
2141 equal_params = isl_space_has_equal_params(pw->dim, pma_space);
2142 if (equal_params < 0)
2143 goto error;
2144 if (equal_params) {
2145 isl_space_free(pma_space);
2146 return fn(pw, pma);
2147 }
2148 ctx = FN(PW,get_ctx)isl_pw_qpolynomial_fold_get_ctx(pw);
2149 if (!isl_space_has_named_params(pw->dim) ||
2150 !isl_space_has_named_params(pma_space))
2151 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 2152); goto error; } while (0)
2152 "unaligned unnamed parameters", goto error)do { isl_handle_error(ctx, isl_error_invalid, "unaligned unnamed parameters"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_pw_templ.c"
, 2152); goto error; } while (0)
;
2153 pw = FN(PW,align_params)isl_pw_qpolynomial_fold_align_params(pw, pma_space);
2154 pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
2155 return fn(pw, pma);
2156error:
2157 isl_space_free(pma_space);
2158 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2159 isl_pw_multi_aff_free(pma);
2160 return NULL((void*)0);
2161}
2162
2163/* Compute the pullback of "pw" by the function represented by "ma".
2164 * In other words, plug in "ma" in "pw".
2165 */
2166static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,pullback_multi_aff_aligned)isl_pw_qpolynomial_fold_pullback_multi_aff_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
2167 __isl_take isl_multi_aff *ma)
2168{
2169 int i;
2170 isl_space *space = NULL((void*)0);
2171
2172 ma = isl_multi_aff_align_divs(ma);
2173 pw = FN(PW,cow)isl_pw_qpolynomial_fold_cow(pw);
2174 if (!pw || !ma)
2175 goto error;
2176
2177 space = isl_space_join(isl_multi_aff_get_space(ma),
2178 FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
2179
2180 for (i = 0; i < pw->n; ++i) {
2181 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
2182 isl_multi_aff_copy(ma));
2183 if (!pw->p[i].set)
2184 goto error;
2185 pw->p[i].FIELDfold = FN(EL,pullback_multi_aff)isl_qpolynomial_fold_pullback_multi_aff(pw->p[i].FIELDfold,
2186 isl_multi_aff_copy(ma));
2187 if (!pw->p[i].FIELDfold)
2188 goto error;
2189 }
2190
2191 pw = FN(PW,reset_space)isl_pw_qpolynomial_fold_reset_space(pw, space);
2192 isl_multi_aff_free(ma);
2193 return pw;
2194error:
2195 isl_space_free(space);
2196 isl_multi_aff_free(ma);
2197 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2198 return NULL((void*)0);
2199}
2200
2201__isl_give PWisl_pw_qpolynomial_fold *FN(PW,pullback_multi_aff)isl_pw_qpolynomial_fold_pullback_multi_aff(__isl_take PWisl_pw_qpolynomial_fold *pw,
2202 __isl_take isl_multi_aff *ma)
2203{
2204 return FN(PW,align_params_pw_multi_aff_and)isl_pw_qpolynomial_fold_align_params_pw_multi_aff_and(pw, ma,
2205 &FN(PW,pullback_multi_aff_aligned)isl_pw_qpolynomial_fold_pullback_multi_aff_aligned);
2206}
2207
2208/* Compute the pullback of "pw" by the function represented by "pma".
2209 * In other words, plug in "pma" in "pw".
2210 */
2211static __isl_give PWisl_pw_qpolynomial_fold *FN(PW,pullback_pw_multi_aff_aligned)isl_pw_qpolynomial_fold_pullback_pw_multi_aff_aligned(__isl_take PWisl_pw_qpolynomial_fold *pw,
2212 __isl_take isl_pw_multi_aff *pma)
2213{
2214 int i;
2215 PWisl_pw_qpolynomial_fold *res;
2216
2217 if (!pma)
2218 goto error;
2219
2220 if (pma->n == 0) {
2221 isl_space *space;
2222 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
2223 FN(PW,get_space)isl_pw_qpolynomial_fold_get_space(pw));
2224 isl_pw_multi_aff_free(pma);
2225 res = FN(PW,empty)isl_pw_qpolynomial_fold_empty(space);
2226 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2227 return res;
2228 }
2229
2230 res = FN(PW,pullback_multi_aff)isl_pw_qpolynomial_fold_pullback_multi_aff(FN(PW,copy)isl_pw_qpolynomial_fold_copy(pw),
2231 isl_multi_aff_copy(pma->p[0].maff));
2232 res = FN(PW,intersect_domain)isl_pw_qpolynomial_fold_intersect_domain(res, isl_set_copy(pma->p[0].set));
2233
2234 for (i = 1; i < pma->n; ++i) {
2235 PWisl_pw_qpolynomial_fold *res_i;
2236
2237 res_i = FN(PW,pullback_multi_aff)isl_pw_qpolynomial_fold_pullback_multi_aff(FN(PW,copy)isl_pw_qpolynomial_fold_copy(pw),
2238 isl_multi_aff_copy(pma->p[i].maff));
2239 res_i = FN(PW,intersect_domain)isl_pw_qpolynomial_fold_intersect_domain(res_i,
2240 isl_set_copy(pma->p[i].set));
2241 res = FN(PW,add_disjoint)isl_pw_qpolynomial_fold_add_disjoint(res, res_i);
2242 }
2243
2244 isl_pw_multi_aff_free(pma);
2245 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2246 return res;
2247error:
2248 isl_pw_multi_aff_free(pma);
2249 FN(PW,free)isl_pw_qpolynomial_fold_free(pw);
2250 return NULL((void*)0);
2251}
2252
2253__isl_give PWisl_pw_qpolynomial_fold *FN(PW,pullback_pw_multi_aff)isl_pw_qpolynomial_fold_pullback_pw_multi_aff(__isl_take PWisl_pw_qpolynomial_fold *pw,
2254 __isl_take isl_pw_multi_aff *pma)
2255{
2256 return FN(PW,align_params_pw_pw_multi_aff_and)isl_pw_qpolynomial_fold_align_params_pw_pw_multi_aff_and(pw, pma,
2257 &FN(PW,pullback_pw_multi_aff_aligned)isl_pw_qpolynomial_fold_pullback_pw_multi_aff_aligned);
2258}
2259#endif