Bug Summary

File:tools/polly/lib/External/isl/isl_map.c
Warning:line 7784, column 9
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_map.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_map.c
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 *
9 * Use of this software is governed by the MIT license
10 *
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
20 */
21
22#include <string.h>
23#include <isl_ctx_private.h>
24#include <isl_map_private.h>
25#include <isl_blk.h>
26#include <isl/constraint.h>
27#include "isl_space_private.h"
28#include "isl_equalities.h"
29#include <isl_lp_private.h>
30#include <isl_seq.h>
31#include <isl/set.h>
32#include <isl/map.h>
33#include <isl_reordering.h>
34#include "isl_sample.h"
35#include <isl_sort.h>
36#include "isl_tab.h"
37#include <isl/vec.h>
38#include <isl_mat_private.h>
39#include <isl_vec_private.h>
40#include <isl_dim_map.h>
41#include <isl_local_space_private.h>
42#include <isl_aff_private.h>
43#include <isl_options_private.h>
44#include <isl_morph.h>
45#include <isl_val_private.h>
46
47#include <bset_to_bmap.c>
48#include <bset_from_bmap.c>
49#include <set_to_map.c>
50#include <set_from_map.c>
51
52static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
53{
54 switch (type) {
55 case isl_dim_param: return dim->nparam;
56 case isl_dim_in: return dim->n_in;
57 case isl_dim_out: return dim->n_out;
58 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
59 default: return 0;
60 }
61}
62
63static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
64{
65 switch (type) {
66 case isl_dim_param: return 1;
67 case isl_dim_in: return 1 + dim->nparam;
68 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
69 default: return 0;
70 }
71}
72
73unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
74 enum isl_dim_type type)
75{
76 if (!bmap)
77 return 0;
78 switch (type) {
79 case isl_dim_cst: return 1;
80 case isl_dim_param:
81 case isl_dim_in:
82 case isl_dim_out: return isl_space_dim(bmap->dim, type);
83 case isl_dim_div: return bmap->n_div;
84 case isl_dim_all: return isl_basic_map_total_dim(bmap);
85 default: return 0;
86 }
87}
88
89/* Return the space of "map".
90 */
91__isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
92{
93 return map ? map->dim : NULL((void*)0);
94}
95
96unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
97{
98 return map ? n(map->dim, type) : 0;
99}
100
101unsigned isl_set_dim(__isl_keep isl_setisl_map *set, enum isl_dim_type type)
102{
103 return set ? n(set->dim, type) : 0;
104}
105
106unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
107 enum isl_dim_type type)
108{
109 isl_space *space;
110
111 if (!bmap)
112 return 0;
113
114 space = bmap->dim;
115 switch (type) {
116 case isl_dim_cst: return 0;
117 case isl_dim_param: return 1;
118 case isl_dim_in: return 1 + space->nparam;
119 case isl_dim_out: return 1 + space->nparam + space->n_in;
120 case isl_dim_div: return 1 + space->nparam + space->n_in +
121 space->n_out;
122 default: return 0;
123 }
124}
125
126unsigned isl_basic_set_offset(__isl_keep isl_basic_setisl_basic_map *bset,
127 enum isl_dim_type type)
128{
129 return isl_basic_map_offset(bset, type);
130}
131
132static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
133{
134 return pos(map->dim, type);
135}
136
137unsigned isl_basic_set_dim(__isl_keep isl_basic_setisl_basic_map *bset,
138 enum isl_dim_type type)
139{
140 return isl_basic_map_dim(bset, type);
141}
142
143unsigned isl_basic_set_n_dim(__isl_keep isl_basic_setisl_basic_map *bset)
144{
145 return isl_basic_set_dim(bset, isl_dim_set);
146}
147
148unsigned isl_basic_set_n_param(__isl_keep isl_basic_setisl_basic_map *bset)
149{
150 return isl_basic_set_dim(bset, isl_dim_param);
151}
152
153unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_setisl_basic_map *bset)
154{
155 if (!bset)
156 return 0;
157 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
158}
159
160unsigned isl_set_n_dim(__isl_keep isl_setisl_map *set)
161{
162 return isl_set_dim(set, isl_dim_set);
163}
164
165unsigned isl_set_n_param(__isl_keep isl_setisl_map *set)
166{
167 return isl_set_dim(set, isl_dim_param);
168}
169
170unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
171{
172 return bmap ? bmap->dim->n_in : 0;
173}
174
175unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
176{
177 return bmap ? bmap->dim->n_out : 0;
178}
179
180unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
181{
182 return bmap ? bmap->dim->nparam : 0;
183}
184
185unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
186{
187 return bmap ? bmap->n_div : 0;
188}
189
190unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
191{
192 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
193}
194
195unsigned isl_map_n_in(__isl_keep const isl_map *map)
196{
197 return map ? map->dim->n_in : 0;
198}
199
200unsigned isl_map_n_out(__isl_keep const isl_map *map)
201{
202 return map ? map->dim->n_out : 0;
203}
204
205unsigned isl_map_n_param(__isl_keep const isl_map *map)
206{
207 return map ? map->dim->nparam : 0;
208}
209
210/* Return the number of equality constraints in the description of "bmap".
211 * Return -1 on error.
212 */
213int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
214{
215 if (!bmap)
216 return -1;
217 return bmap->n_eq;
218}
219
220/* Return the number of equality constraints in the description of "bset".
221 * Return -1 on error.
222 */
223int isl_basic_set_n_equality(__isl_keep isl_basic_setisl_basic_map *bset)
224{
225 return isl_basic_map_n_equality(bset_to_bmap(bset));
226}
227
228/* Return the number of inequality constraints in the description of "bmap".
229 * Return -1 on error.
230 */
231int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
232{
233 if (!bmap)
234 return -1;
235 return bmap->n_ineq;
236}
237
238/* Return the number of inequality constraints in the description of "bset".
239 * Return -1 on error.
240 */
241int isl_basic_set_n_inequality(__isl_keep isl_basic_setisl_basic_map *bset)
242{
243 return isl_basic_map_n_inequality(bset_to_bmap(bset));
244}
245
246/* Do "bmap1" and "bmap2" have the same parameters?
247 */
248static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
249 __isl_keep isl_basic_map *bmap2)
250{
251 isl_space *space1, *space2;
252
253 space1 = isl_basic_map_peek_space(bmap1);
254 space2 = isl_basic_map_peek_space(bmap2);
255 return isl_space_has_equal_params(space1, space2);
256}
257
258/* Do "map1" and "map2" have the same parameters?
259 */
260isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
261 __isl_keep isl_map *map2)
262{
263 isl_space *space1, *space2;
264
265 space1 = isl_map_peek_space(map1);
266 space2 = isl_map_peek_space(map2);
267 return isl_space_has_equal_params(space1, space2);
268}
269
270/* Do "map" and "set" have the same parameters?
271 */
272static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
273 __isl_keep isl_setisl_map *set)
274{
275 return isl_map_has_equal_params(map, set_to_map(set));
276}
277
278isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
279 __isl_keep isl_setisl_map *set)
280{
281 isl_bool m;
282 if (!map || !set)
283 return isl_bool_error;
284 m = isl_map_has_equal_params(map, set_to_map(set));
285 if (m < 0 || !m)
286 return m;
287 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
288 set->dim, isl_dim_set);
289}
290
291isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
292 __isl_keep isl_basic_setisl_basic_map *bset)
293{
294 isl_bool m;
295 if (!bmap || !bset)
296 return isl_bool_error;
297 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
298 if (m < 0 || !m)
299 return m;
300 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
301 bset->dim, isl_dim_set);
302}
303
304isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
305 __isl_keep isl_setisl_map *set)
306{
307 isl_bool m;
308 if (!map || !set)
309 return isl_bool_error;
310 m = isl_map_has_equal_params(map, set_to_map(set));
311 if (m < 0 || !m)
312 return m;
313 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
314 set->dim, isl_dim_set);
315}
316
317isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
318 __isl_keep isl_basic_setisl_basic_map *bset)
319{
320 isl_bool m;
321 if (!bmap || !bset)
322 return isl_bool_error;
323 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
324 if (m < 0 || !m)
325 return m;
326 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
327 bset->dim, isl_dim_set);
328}
329
330isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
331{
332 return bmap ? bmap->ctx : NULL((void*)0);
333}
334
335isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_setisl_basic_map *bset)
336{
337 return bset ? bset->ctx : NULL((void*)0);
338}
339
340isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
341{
342 return map ? map->ctx : NULL((void*)0);
343}
344
345isl_ctx *isl_set_get_ctx(__isl_keep isl_setisl_map *set)
346{
347 return set ? set->ctx : NULL((void*)0);
348}
349
350/* Return the space of "bmap".
351 */
352__isl_keep isl_space *isl_basic_map_peek_space(
353 __isl_keep const isl_basic_map *bmap)
354{
355 return bmap ? bmap->dim : NULL((void*)0);
356}
357
358/* Return the space of "bset".
359 */
360__isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_setisl_basic_map *bset)
361{
362 return isl_basic_map_peek_space(bset_to_bmap(bset));
363}
364
365__isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
366{
367 return isl_space_copy(isl_basic_map_peek_space(bmap));
368}
369
370__isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_setisl_basic_map *bset)
371{
372 return isl_basic_map_get_space(bset_to_bmap(bset));
373}
374
375/* Extract the divs in "bmap" as a matrix.
376 */
377__isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
378{
379 int i;
380 isl_ctx *ctx;
381 isl_mat *div;
382 unsigned total;
383 unsigned cols;
384
385 if (!bmap)
386 return NULL((void*)0);
387
388 ctx = isl_basic_map_get_ctx(bmap);
389 total = isl_space_dim(bmap->dim, isl_dim_all);
390 cols = 1 + 1 + total + bmap->n_div;
391 div = isl_mat_alloc(ctx, bmap->n_div, cols);
392 if (!div)
393 return NULL((void*)0);
394
395 for (i = 0; i < bmap->n_div; ++i)
396 isl_seq_cpy(div->row[i], bmap->div[i], cols);
397
398 return div;
399}
400
401/* Extract the divs in "bset" as a matrix.
402 */
403__isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_setisl_basic_map *bset)
404{
405 return isl_basic_map_get_divs(bset);
406}
407
408__isl_give isl_local_space *isl_basic_map_get_local_space(
409 __isl_keep isl_basic_map *bmap)
410{
411 isl_mat *div;
412
413 if (!bmap)
414 return NULL((void*)0);
415
416 div = isl_basic_map_get_divs(bmap);
417 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
418}
419
420__isl_give isl_local_space *isl_basic_set_get_local_space(
421 __isl_keep isl_basic_setisl_basic_map *bset)
422{
423 return isl_basic_map_get_local_space(bset);
424}
425
426/* For each known div d = floor(f/m), add the constraints
427 *
428 * f - m d >= 0
429 * -(f-(m-1)) + m d >= 0
430 *
431 * Do not finalize the result.
432 */
433static __isl_give isl_basic_map *add_known_div_constraints(
434 __isl_take isl_basic_map *bmap)
435{
436 int i;
437 unsigned n_div;
438
439 if (!bmap)
440 return NULL((void*)0);
441 n_div = isl_basic_map_dim(bmap, isl_dim_div);
442 if (n_div == 0)
443 return bmap;
444 bmap = isl_basic_map_cow(bmap);
445 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
446 if (!bmap)
447 return NULL((void*)0);
448 for (i = 0; i < n_div; ++i) {
449 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
450 continue;
451 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
452 return isl_basic_map_free(bmap);
453 }
454
455 return bmap;
456}
457
458__isl_give isl_basic_map *isl_basic_map_from_local_space(
459 __isl_take isl_local_space *ls)
460{
461 int i;
462 int n_div;
463 isl_basic_map *bmap;
464
465 if (!ls)
466 return NULL((void*)0);
467
468 n_div = isl_local_space_dim(ls, isl_dim_div);
469 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
470 n_div, 0, 2 * n_div);
471
472 for (i = 0; i < n_div; ++i)
473 if (isl_basic_map_alloc_div(bmap) < 0)
474 goto error;
475
476 for (i = 0; i < n_div; ++i)
477 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
478 bmap = add_known_div_constraints(bmap);
479
480 isl_local_space_free(ls);
481 return bmap;
482error:
483 isl_local_space_free(ls);
484 isl_basic_map_free(bmap);
485 return NULL((void*)0);
486}
487
488__isl_give isl_basic_setisl_basic_map *isl_basic_set_from_local_space(
489 __isl_take isl_local_space *ls)
490{
491 return isl_basic_map_from_local_space(ls);
492}
493
494__isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
495{
496 return isl_space_copy(isl_map_peek_space(map));
497}
498
499__isl_give isl_space *isl_set_get_space(__isl_keep isl_setisl_map *set)
500{
501 if (!set)
502 return NULL((void*)0);
503 return isl_space_copy(set->dim);
504}
505
506__isl_give isl_basic_map *isl_basic_map_set_tuple_name(
507 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
508{
509 bmap = isl_basic_map_cow(bmap);
510 if (!bmap)
511 return NULL((void*)0);
512 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
513 if (!bmap->dim)
514 goto error;
515 bmap = isl_basic_map_finalize(bmap);
516 return bmap;
517error:
518 isl_basic_map_free(bmap);
519 return NULL((void*)0);
520}
521
522__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_tuple_name(
523 __isl_take isl_basic_setisl_basic_map *bset, const char *s)
524{
525 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
526}
527
528const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
529 enum isl_dim_type type)
530{
531 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL((void*)0);
532}
533
534__isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
535 enum isl_dim_type type, const char *s)
536{
537 int i;
538
539 map = isl_map_cow(map);
540 if (!map)
541 return NULL((void*)0);
542
543 map->dim = isl_space_set_tuple_name(map->dim, type, s);
544 if (!map->dim)
545 goto error;
546
547 for (i = 0; i < map->n; ++i) {
548 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
549 if (!map->p[i])
550 goto error;
551 }
552
553 return map;
554error:
555 isl_map_free(map);
556 return NULL((void*)0);
557}
558
559/* Replace the identifier of the tuple of type "type" by "id".
560 */
561__isl_give isl_basic_map *isl_basic_map_set_tuple_id(
562 __isl_take isl_basic_map *bmap,
563 enum isl_dim_type type, __isl_take isl_id *id)
564{
565 bmap = isl_basic_map_cow(bmap);
566 if (!bmap)
567 goto error;
568 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
569 if (!bmap->dim)
570 return isl_basic_map_free(bmap);
571 bmap = isl_basic_map_finalize(bmap);
572 return bmap;
573error:
574 isl_id_free(id);
575 return NULL((void*)0);
576}
577
578/* Replace the identifier of the tuple by "id".
579 */
580__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_tuple_id(
581 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_id *id)
582{
583 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
584}
585
586/* Does the input or output tuple have a name?
587 */
588isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
589{
590 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
591}
592
593const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
594 enum isl_dim_type type)
595{
596 return map ? isl_space_get_tuple_name(map->dim, type) : NULL((void*)0);
597}
598
599__isl_give isl_setisl_map *isl_set_set_tuple_name(__isl_take isl_setisl_map *set,
600 const char *s)
601{
602 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
603 isl_dim_set, s));
604}
605
606__isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
607 enum isl_dim_type type, __isl_take isl_id *id)
608{
609 map = isl_map_cow(map);
610 if (!map)
611 goto error;
612
613 map->dim = isl_space_set_tuple_id(map->dim, type, id);
614
615 return isl_map_reset_space(map, isl_space_copy(map->dim));
616error:
617 isl_id_free(id);
618 return NULL((void*)0);
619}
620
621__isl_give isl_setisl_map *isl_set_set_tuple_id(__isl_take isl_setisl_map *set,
622 __isl_take isl_id *id)
623{
624 return isl_map_set_tuple_id(set, isl_dim_set, id);
625}
626
627__isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
628 enum isl_dim_type type)
629{
630 map = isl_map_cow(map);
631 if (!map)
632 return NULL((void*)0);
633
634 map->dim = isl_space_reset_tuple_id(map->dim, type);
635
636 return isl_map_reset_space(map, isl_space_copy(map->dim));
637}
638
639__isl_give isl_setisl_map *isl_set_reset_tuple_id(__isl_take isl_setisl_map *set)
640{
641 return isl_map_reset_tuple_id(set, isl_dim_set);
642}
643
644isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
645{
646 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
647}
648
649__isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
650 enum isl_dim_type type)
651{
652 return map ? isl_space_get_tuple_id(map->dim, type) : NULL((void*)0);
653}
654
655isl_bool isl_set_has_tuple_id(__isl_keep isl_setisl_map *set)
656{
657 return isl_map_has_tuple_id(set, isl_dim_set);
658}
659
660__isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_setisl_map *set)
661{
662 return isl_map_get_tuple_id(set, isl_dim_set);
663}
664
665/* Does the set tuple have a name?
666 */
667isl_bool isl_set_has_tuple_name(__isl_keep isl_setisl_map *set)
668{
669 if (!set)
670 return isl_bool_error;
671 return isl_space_has_tuple_name(set->dim, isl_dim_set);
672}
673
674
675const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_setisl_basic_map *bset)
676{
677 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL((void*)0);
678}
679
680const char *isl_set_get_tuple_name(__isl_keep isl_setisl_map *set)
681{
682 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL((void*)0);
683}
684
685const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
686 enum isl_dim_type type, unsigned pos)
687{
688 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL((void*)0);
689}
690
691const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_setisl_basic_map *bset,
692 enum isl_dim_type type, unsigned pos)
693{
694 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL((void*)0);
695}
696
697/* Does the given dimension have a name?
698 */
699isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
700 enum isl_dim_type type, unsigned pos)
701{
702 if (!map)
703 return isl_bool_error;
704 return isl_space_has_dim_name(map->dim, type, pos);
705}
706
707const char *isl_map_get_dim_name(__isl_keep isl_map *map,
708 enum isl_dim_type type, unsigned pos)
709{
710 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL((void*)0);
711}
712
713const char *isl_set_get_dim_name(__isl_keep isl_setisl_map *set,
714 enum isl_dim_type type, unsigned pos)
715{
716 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL((void*)0);
717}
718
719/* Does the given dimension have a name?
720 */
721isl_bool isl_set_has_dim_name(__isl_keep isl_setisl_map *set,
722 enum isl_dim_type type, unsigned pos)
723{
724 if (!set)
725 return isl_bool_error;
726 return isl_space_has_dim_name(set->dim, type, pos);
727}
728
729__isl_give isl_basic_map *isl_basic_map_set_dim_name(
730 __isl_take isl_basic_map *bmap,
731 enum isl_dim_type type, unsigned pos, const char *s)
732{
733 bmap = isl_basic_map_cow(bmap);
734 if (!bmap)
735 return NULL((void*)0);
736 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
737 if (!bmap->dim)
738 goto error;
739 return isl_basic_map_finalize(bmap);
740error:
741 isl_basic_map_free(bmap);
742 return NULL((void*)0);
743}
744
745__isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
746 enum isl_dim_type type, unsigned pos, const char *s)
747{
748 int i;
749
750 map = isl_map_cow(map);
751 if (!map)
752 return NULL((void*)0);
753
754 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
755 if (!map->dim)
756 goto error;
757
758 for (i = 0; i < map->n; ++i) {
759 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
760 if (!map->p[i])
761 goto error;
762 }
763
764 return map;
765error:
766 isl_map_free(map);
767 return NULL((void*)0);
768}
769
770__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_dim_name(
771 __isl_take isl_basic_setisl_basic_map *bset,
772 enum isl_dim_type type, unsigned pos, const char *s)
773{
774 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
775 type, pos, s));
776}
777
778__isl_give isl_setisl_map *isl_set_set_dim_name(__isl_take isl_setisl_map *set,
779 enum isl_dim_type type, unsigned pos, const char *s)
780{
781 return set_from_map(isl_map_set_dim_name(set_to_map(set),
782 type, pos, s));
783}
784
785isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
786 enum isl_dim_type type, unsigned pos)
787{
788 if (!bmap)
789 return isl_bool_error;
790 return isl_space_has_dim_id(bmap->dim, type, pos);
791}
792
793__isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_setisl_basic_map *bset,
794 enum isl_dim_type type, unsigned pos)
795{
796 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL((void*)0);
797}
798
799isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
800 enum isl_dim_type type, unsigned pos)
801{
802 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
803}
804
805__isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
806 enum isl_dim_type type, unsigned pos)
807{
808 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL((void*)0);
809}
810
811isl_bool isl_set_has_dim_id(__isl_keep isl_setisl_map *set,
812 enum isl_dim_type type, unsigned pos)
813{
814 return isl_map_has_dim_id(set, type, pos);
815}
816
817__isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_setisl_map *set,
818 enum isl_dim_type type, unsigned pos)
819{
820 return isl_map_get_dim_id(set, type, pos);
821}
822
823__isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
824 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
825{
826 map = isl_map_cow(map);
827 if (!map)
828 goto error;
829
830 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
831
832 return isl_map_reset_space(map, isl_space_copy(map->dim));
833error:
834 isl_id_free(id);
835 return NULL((void*)0);
836}
837
838__isl_give isl_setisl_map *isl_set_set_dim_id(__isl_take isl_setisl_map *set,
839 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
840{
841 return isl_map_set_dim_id(set, type, pos, id);
842}
843
844int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
845 __isl_keep isl_id *id)
846{
847 if (!map)
848 return -1;
849 return isl_space_find_dim_by_id(map->dim, type, id);
850}
851
852int isl_set_find_dim_by_id(__isl_keep isl_setisl_map *set, enum isl_dim_type type,
853 __isl_keep isl_id *id)
854{
855 return isl_map_find_dim_by_id(set, type, id);
856}
857
858/* Return the position of the dimension of the given type and name
859 * in "bmap".
860 * Return -1 if no such dimension can be found.
861 */
862int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
863 enum isl_dim_type type, const char *name)
864{
865 if (!bmap)
866 return -1;
867 return isl_space_find_dim_by_name(bmap->dim, type, name);
868}
869
870int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
871 const char *name)
872{
873 if (!map)
874 return -1;
875 return isl_space_find_dim_by_name(map->dim, type, name);
876}
877
878int isl_set_find_dim_by_name(__isl_keep isl_setisl_map *set, enum isl_dim_type type,
879 const char *name)
880{
881 return isl_map_find_dim_by_name(set, type, name);
882}
883
884/* Check whether equality i of bset is a pure stride constraint
885 * on a single dimension, i.e., of the form
886 *
887 * v = k e
888 *
889 * with k a constant and e an existentially quantified variable.
890 */
891isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_setisl_basic_map *bset, int i)
892{
893 unsigned nparam;
894 unsigned d;
895 unsigned n_div;
896 int pos1;
897 int pos2;
898
899 if (!bset)
900 return isl_bool_error;
901
902 if (!isl_int_is_zero(bset->eq[i][0])(isl_sioimath_sgn(*(bset->eq[i][0])) == 0))
903 return isl_bool_false;
904
905 nparam = isl_basic_set_dim(bset, isl_dim_param);
906 d = isl_basic_set_dim(bset, isl_dim_set);
907 n_div = isl_basic_set_dim(bset, isl_dim_div);
908
909 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
910 return isl_bool_false;
911 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
912 if (pos1 == -1)
913 return isl_bool_false;
914 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
915 d - pos1 - 1) != -1)
916 return isl_bool_false;
917
918 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
919 if (pos2 == -1)
920 return isl_bool_false;
921 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
922 n_div - pos2 - 1) != -1)
923 return isl_bool_false;
924 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1])(isl_sioimath_cmp_si(*(bset->eq[i][1 + nparam + pos1]), 1)
== 0)
&&
925 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1])(isl_sioimath_cmp_si(*(bset->eq[i][1 + nparam + pos1]), -1
) == 0)
)
926 return isl_bool_false;
927
928 return isl_bool_true;
929}
930
931/* Reset the user pointer on all identifiers of parameters and tuples
932 * of the space of "map".
933 */
934__isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
935{
936 isl_space *space;
937
938 space = isl_map_get_space(map);
939 space = isl_space_reset_user(space);
940 map = isl_map_reset_space(map, space);
941
942 return map;
943}
944
945/* Reset the user pointer on all identifiers of parameters and tuples
946 * of the space of "set".
947 */
948__isl_give isl_setisl_map *isl_set_reset_user(__isl_take isl_setisl_map *set)
949{
950 return isl_map_reset_user(set);
951}
952
953isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
954{
955 if (!bmap)
956 return isl_bool_error;
957 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4))));
958}
959
960/* Has "map" been marked as a rational map?
961 * In particular, have all basic maps in "map" been marked this way?
962 * An empty map is not considered to be rational.
963 * Maps where only some of the basic maps are marked rational
964 * are not allowed.
965 */
966isl_bool isl_map_is_rational(__isl_keep isl_map *map)
967{
968 int i;
969 isl_bool rational;
970
971 if (!map)
972 return isl_bool_error;
973 if (map->n == 0)
974 return isl_bool_false;
975 rational = isl_basic_map_is_rational(map->p[0]);
976 if (rational < 0)
977 return rational;
978 for (i = 1; i < map->n; ++i) {
979 isl_bool rational_i;
980
981 rational_i = isl_basic_map_is_rational(map->p[i]);
982 if (rational_i < 0)
983 return rational_i;
984 if (rational != rational_i)
985 isl_die(isl_map_get_ctx(map), isl_error_unsupported,do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 987); return isl_bool_error; } while (0)
986 "mixed rational and integer basic maps "do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 987); return isl_bool_error; } while (0)
987 "not supported", return isl_bool_error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_unsupported
, "mixed rational and integer basic maps " "not supported", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 987); return isl_bool_error; } while (0)
;
988 }
989
990 return rational;
991}
992
993/* Has "set" been marked as a rational set?
994 * In particular, have all basic set in "set" been marked this way?
995 * An empty set is not considered to be rational.
996 * Sets where only some of the basic sets are marked rational
997 * are not allowed.
998 */
999isl_bool isl_set_is_rational(__isl_keep isl_setisl_map *set)
1000{
1001 return isl_map_is_rational(set);
1002}
1003
1004int isl_basic_set_is_rational(__isl_keep isl_basic_setisl_basic_map *bset)
1005{
1006 return isl_basic_map_is_rational(bset);
1007}
1008
1009/* Does "bmap" contain any rational points?
1010 *
1011 * If "bmap" has an equality for each dimension, equating the dimension
1012 * to an integer constant, then it has no rational points, even if it
1013 * is marked as rational.
1014 */
1015isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1016{
1017 isl_bool has_rational = isl_bool_true;
1018 unsigned total;
1019
1020 if (!bmap)
1021 return isl_bool_error;
1022 if (isl_basic_map_plain_is_empty(bmap))
1023 return isl_bool_false;
1024 if (!isl_basic_map_is_rational(bmap))
1025 return isl_bool_false;
1026 bmap = isl_basic_map_copy(bmap);
1027 bmap = isl_basic_map_implicit_equalities(bmap);
1028 if (!bmap)
1029 return isl_bool_error;
1030 total = isl_basic_map_total_dim(bmap);
1031 if (bmap->n_eq == total) {
1032 int i, j;
1033 for (i = 0; i < bmap->n_eq; ++i) {
1034 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1035 if (j < 0)
1036 break;
1037 if (!isl_int_is_one(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), 1) == 0) &&
1038 !isl_int_is_negone(bmap->eq[i][1 + j])(isl_sioimath_cmp_si(*(bmap->eq[i][1 + j]), -1) == 0))
1039 break;
1040 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1041 total - j - 1);
1042 if (j >= 0)
1043 break;
1044 }
1045 if (i == bmap->n_eq)
1046 has_rational = isl_bool_false;
1047 }
1048 isl_basic_map_free(bmap);
1049
1050 return has_rational;
1051}
1052
1053/* Does "map" contain any rational points?
1054 */
1055isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1056{
1057 int i;
1058 isl_bool has_rational;
1059
1060 if (!map)
1061 return isl_bool_error;
1062 for (i = 0; i < map->n; ++i) {
1063 has_rational = isl_basic_map_has_rational(map->p[i]);
1064 if (has_rational < 0 || has_rational)
1065 return has_rational;
1066 }
1067 return isl_bool_false;
1068}
1069
1070/* Does "set" contain any rational points?
1071 */
1072isl_bool isl_set_has_rational(__isl_keep isl_setisl_map *set)
1073{
1074 return isl_map_has_rational(set);
1075}
1076
1077/* Is this basic set a parameter domain?
1078 */
1079isl_bool isl_basic_set_is_params(__isl_keep isl_basic_setisl_basic_map *bset)
1080{
1081 if (!bset)
1082 return isl_bool_error;
1083 return isl_space_is_params(bset->dim);
1084}
1085
1086/* Is this set a parameter domain?
1087 */
1088isl_bool isl_set_is_params(__isl_keep isl_setisl_map *set)
1089{
1090 if (!set)
1091 return isl_bool_error;
1092 return isl_space_is_params(set->dim);
1093}
1094
1095/* Is this map actually a parameter domain?
1096 * Users should never call this function. Outside of isl,
1097 * a map can never be a parameter domain.
1098 */
1099isl_bool isl_map_is_params(__isl_keep isl_map *map)
1100{
1101 if (!map)
1102 return isl_bool_error;
1103 return isl_space_is_params(map->dim);
1104}
1105
1106static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1107 struct isl_basic_map *bmap, unsigned extra,
1108 unsigned n_eq, unsigned n_ineq)
1109{
1110 int i;
1111 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1112
1113 bmap->ctx = ctx;
1114 isl_ctx_ref(ctx);
1115
1116 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1117 if (isl_blk_is_error(bmap->block))
1118 goto error;
1119
1120 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq)((isl_int * *)isl_malloc_or_die(ctx, (n_ineq + n_eq)*sizeof(isl_int
*)))
;
1121 if ((n_ineq + n_eq) && !bmap->ineq)
1122 goto error;
1123
1124 if (extra == 0) {
1125 bmap->block2 = isl_blk_empty();
1126 bmap->div = NULL((void*)0);
1127 } else {
1128 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1129 if (isl_blk_is_error(bmap->block2))
1130 goto error;
1131
1132 bmap->div = isl_alloc_array(ctx, isl_int *, extra)((isl_int * *)isl_malloc_or_die(ctx, (extra)*sizeof(isl_int *
)))
;
1133 if (!bmap->div)
1134 goto error;
1135 }
1136
1137 for (i = 0; i < n_ineq + n_eq; ++i)
1138 bmap->ineq[i] = bmap->block.data + i * row_size;
1139
1140 for (i = 0; i < extra; ++i)
1141 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1142
1143 bmap->ref = 1;
1144 bmap->flags = 0;
1145 bmap->c_size = n_eq + n_ineq;
1146 bmap->eq = bmap->ineq + n_ineq;
1147 bmap->extra = extra;
1148 bmap->n_eq = 0;
1149 bmap->n_ineq = 0;
1150 bmap->n_div = 0;
1151 bmap->sample = NULL((void*)0);
1152
1153 return bmap;
1154error:
1155 isl_basic_map_free(bmap);
1156 return NULL((void*)0);
1157}
1158
1159struct isl_basic_setisl_basic_map *isl_basic_set_alloc(struct isl_ctx *ctx,
1160 unsigned nparam, unsigned dim, unsigned extra,
1161 unsigned n_eq, unsigned n_ineq)
1162{
1163 struct isl_basic_map *bmap;
1164 isl_space *space;
1165
1166 space = isl_space_set_alloc(ctx, nparam, dim);
1167 if (!space)
1168 return NULL((void*)0);
1169
1170 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1171 return bset_from_bmap(bmap);
1172}
1173
1174__isl_give isl_basic_setisl_basic_map *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1175 unsigned extra, unsigned n_eq, unsigned n_ineq)
1176{
1177 struct isl_basic_map *bmap;
1178 if (!dim)
1179 return NULL((void*)0);
1180 isl_assert(dim->ctx, dim->n_in == 0, goto error)do { if (dim->n_in == 0) break; do { isl_handle_error(dim->
ctx, isl_error_unknown, "Assertion \"" "dim->n_in == 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1180); goto error; } while (0); } while (0)
;
1181 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1182 return bset_from_bmap(bmap);
1183error:
1184 isl_space_free(dim);
1185 return NULL((void*)0);
1186}
1187
1188struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1189 unsigned extra, unsigned n_eq, unsigned n_ineq)
1190{
1191 struct isl_basic_map *bmap;
1192
1193 if (!dim)
1194 return NULL((void*)0);
1195 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map)((struct isl_basic_map *)isl_calloc_or_die(dim->ctx, 1, sizeof
(struct isl_basic_map)))
;
1196 if (!bmap)
1197 goto error;
1198 bmap->dim = dim;
1199
1200 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1201error:
1202 isl_space_free(dim);
1203 return NULL((void*)0);
1204}
1205
1206struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1207 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1208 unsigned n_eq, unsigned n_ineq)
1209{
1210 struct isl_basic_map *bmap;
1211 isl_space *dim;
1212
1213 dim = isl_space_alloc(ctx, nparam, in, out);
1214 if (!dim)
1215 return NULL((void*)0);
1216
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bmap;
1219}
1220
1221static void dup_constraints(
1222 struct isl_basic_map *dst, struct isl_basic_map *src)
1223{
1224 int i;
1225 unsigned total = isl_basic_map_total_dim(src);
1226
1227 for (i = 0; i < src->n_eq; ++i) {
1228 int j = isl_basic_map_alloc_equality(dst);
1229 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1230 }
1231
1232 for (i = 0; i < src->n_ineq; ++i) {
1233 int j = isl_basic_map_alloc_inequality(dst);
1234 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1235 }
1236
1237 for (i = 0; i < src->n_div; ++i) {
1238 int j = isl_basic_map_alloc_div(dst);
1239 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1240 }
1241 ISL_F_SET(dst, ISL_BASIC_SET_FINAL)(((dst)->flags) |= ((1 << 0)));
1242}
1243
1244__isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1245{
1246 struct isl_basic_map *dup;
1247
1248 if (!bmap)
1249 return NULL((void*)0);
1250 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1251 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1252 if (!dup)
1253 return NULL((void*)0);
1254 dup_constraints(dup, bmap);
1255 dup->flags = bmap->flags;
1256 dup->sample = isl_vec_copy(bmap->sample);
1257 return dup;
1258}
1259
1260struct isl_basic_setisl_basic_map *isl_basic_set_dup(struct isl_basic_setisl_basic_map *bset)
1261{
1262 struct isl_basic_map *dup;
1263
1264 dup = isl_basic_map_dup(bset_to_bmap(bset));
1265 return bset_from_bmap(dup);
1266}
1267
1268__isl_give isl_basic_setisl_basic_map *isl_basic_set_copy(__isl_keep isl_basic_setisl_basic_map *bset)
1269{
1270 if (!bset)
1271 return NULL((void*)0);
1272
1273 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)(!!(((bset)->flags) & ((1 << 0))))) {
1274 bset->ref++;
1275 return bset;
1276 }
1277 return isl_basic_set_dup(bset);
1278}
1279
1280__isl_give isl_setisl_map *isl_set_copy(__isl_keep isl_setisl_map *set)
1281{
1282 if (!set)
1283 return NULL((void*)0);
1284
1285 set->ref++;
1286 return set;
1287}
1288
1289__isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1290{
1291 if (!bmap)
21
Assuming 'bmap' is non-null
22
Taking false branch
1292 return NULL((void*)0);
1293
1294 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)(!!(((bmap)->flags) & ((1 << 0))))) {
23
Taking true branch
1295 bmap->ref++;
1296 return bmap;
1297 }
1298 bmap = isl_basic_map_dup(bmap);
1299 if (bmap)
1300 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) |= ((1 << 0)));
1301 return bmap;
1302}
1303
1304__isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1305{
1306 if (!map)
1307 return NULL((void*)0);
1308
1309 map->ref++;
1310 return map;
1311}
1312
1313__isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1314{
1315 if (!bmap)
29
Taking false branch
1316 return NULL((void*)0);
1317
1318 if (--bmap->ref > 0)
30
Assuming the condition is false
31
Taking false branch
1319 return NULL((void*)0);
1320
1321 isl_ctx_deref(bmap->ctx);
1322 free(bmap->div);
1323 isl_blk_free(bmap->ctx, bmap->block2);
1324 free(bmap->ineq);
1325 isl_blk_free(bmap->ctx, bmap->block);
1326 isl_vec_free(bmap->sample);
1327 isl_space_free(bmap->dim);
1328 free(bmap);
32
Memory is released
1329
1330 return NULL((void*)0);
1331}
1332
1333__isl_null isl_basic_setisl_basic_map *isl_basic_set_free(__isl_take isl_basic_setisl_basic_map *bset)
1334{
1335 return isl_basic_map_free(bset_to_bmap(bset));
1336}
1337
1338static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1339{
1340 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1341}
1342
1343/* Check that "map" has only named parameters, reporting an error
1344 * if it does not.
1345 */
1346isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1347{
1348 return isl_space_check_named_params(isl_map_peek_space(map));
1349}
1350
1351/* Check that "bmap1" and "bmap2" have the same parameters,
1352 * reporting an error if they do not.
1353 */
1354static isl_stat isl_basic_map_check_equal_params(
1355 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1356{
1357 isl_bool match;
1358
1359 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1360 if (match < 0)
1361 return isl_stat_error;
1362 if (!match)
1363 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "parameters don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1364); return isl_stat_error; } while (0)
1364 "parameters don't match", return isl_stat_error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "parameters don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1364); return isl_stat_error; } while (0)
;
1365 return isl_stat_ok;
1366}
1367
1368__isl_give isl_map *isl_map_align_params_map_map_and(
1369 __isl_take isl_map *map1, __isl_take isl_map *map2,
1370 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1371 __isl_take isl_map *map2))
1372{
1373 if (!map1 || !map2)
3
Assuming 'map1' is non-null
4
Taking false branch
1374 goto error;
1375 if (isl_map_has_equal_params(map1, map2))
5
Assuming the condition is true
6
Taking true branch
1376 return fn(map1, map2);
7
Calling 'map_intersect_domain'
1377 if (isl_map_check_named_params(map1) < 0)
1378 goto error;
1379 if (isl_map_check_named_params(map2) < 0)
1380 goto error;
1381 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1382 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1383 return fn(map1, map2);
1384error:
1385 isl_map_free(map1);
1386 isl_map_free(map2);
1387 return NULL((void*)0);
1388}
1389
1390isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1391 __isl_keep isl_map *map2,
1392 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1393{
1394 isl_bool r;
1395
1396 if (!map1 || !map2)
1397 return isl_bool_error;
1398 if (isl_map_has_equal_params(map1, map2))
1399 return fn(map1, map2);
1400 if (isl_map_check_named_params(map1) < 0)
1401 return isl_bool_error;
1402 if (isl_map_check_named_params(map2) < 0)
1403 return isl_bool_error;
1404 map1 = isl_map_copy(map1);
1405 map2 = isl_map_copy(map2);
1406 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1407 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1408 r = fn(map1, map2);
1409 isl_map_free(map1);
1410 isl_map_free(map2);
1411 return r;
1412}
1413
1414int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1415{
1416 struct isl_ctx *ctx;
1417 if (!bmap)
1418 return -1;
1419 ctx = bmap->ctx;
1420 isl_assert(ctx, room_for_con(bmap, 1), return -1)do { if (room_for_con(bmap, 1)) break; do { isl_handle_error(
ctx, isl_error_unknown, "Assertion \"" "room_for_con(bmap, 1)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1420); return -1; } while (0); } while (0)
;
1421 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,do { if ((bmap->eq - bmap->ineq) + bmap->n_eq <= bmap
->c_size) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "(bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1422); return -1; } while (0); } while (0)
1422 return -1)do { if ((bmap->eq - bmap->ineq) + bmap->n_eq <= bmap
->c_size) break; do { isl_handle_error(ctx, isl_error_unknown
, "Assertion \"" "(bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1422); return -1; } while (0); } while (0)
;
1423 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1424 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT)(((bmap)->flags) &= ~((1 << 2)));
1426 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1427 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1428 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1429 isl_int *t;
1430 int j = isl_basic_map_alloc_inequality(bmap);
1431 if (j < 0)
1432 return -1;
1433 t = bmap->ineq[j];
1434 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1435 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1436 bmap->eq[-1] = t;
1437 bmap->n_eq++;
1438 bmap->n_ineq--;
1439 bmap->eq--;
1440 return 0;
1441 }
1442 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1443 bmap->extra - bmap->n_div);
1444 return bmap->n_eq++;
1445}
1446
1447int isl_basic_set_alloc_equality(struct isl_basic_setisl_basic_map *bset)
1448{
1449 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1450}
1451
1452int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1453{
1454 if (!bmap)
1455 return -1;
1456 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1)do { if (n <= bmap->n_eq) break; do { isl_handle_error(
bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_eq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1456); return -1; } while (0); } while (0)
;
1457 bmap->n_eq -= n;
1458 return 0;
1459}
1460
1461int isl_basic_set_free_equality(struct isl_basic_setisl_basic_map *bset, unsigned n)
1462{
1463 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1464}
1465
1466int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1467{
1468 isl_int *t;
1469 if (!bmap)
1470 return -1;
1471 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1)do { if (pos < bmap->n_eq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_eq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1471); return -1; } while (0); } while (0)
;
1472
1473 if (pos != bmap->n_eq - 1) {
1474 t = bmap->eq[pos];
1475 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1476 bmap->eq[bmap->n_eq - 1] = t;
1477 }
1478 bmap->n_eq--;
1479 return 0;
1480}
1481
1482/* Turn inequality "pos" of "bmap" into an equality.
1483 *
1484 * In particular, we move the inequality in front of the equalities
1485 * and move the last inequality in the position of the moved inequality.
1486 * Note that isl_tab_make_equalities_explicit depends on this particular
1487 * change in the ordering of the constraints.
1488 */
1489void isl_basic_map_inequality_to_equality(
1490 struct isl_basic_map *bmap, unsigned pos)
1491{
1492 isl_int *t;
1493
1494 t = bmap->ineq[pos];
1495 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1496 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1497 bmap->eq[-1] = t;
1498 bmap->n_eq++;
1499 bmap->n_ineq--;
1500 bmap->eq--;
1501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1502 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1505}
1506
1507static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1508{
1509 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1510}
1511
1512int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1513{
1514 struct isl_ctx *ctx;
1515 if (!bmap)
1516 return -1;
1517 ctx = bmap->ctx;
1518 isl_assert(ctx, room_for_ineq(bmap, 1), return -1)do { if (room_for_ineq(bmap, 1)) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "room_for_ineq(bmap, 1)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1518); return -1; } while (0); } while (0)
;
1519 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT)(((bmap)->flags) &= ~((1 << 2)));
1520 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT)(((bmap)->flags) &= ~((1 << 3)));
1521 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1522 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES)(((bmap)->flags) &= ~((1 << 7)));
1523 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1524 1 + isl_basic_map_total_dim(bmap),
1525 bmap->extra - bmap->n_div);
1526 return bmap->n_ineq++;
1527}
1528
1529int isl_basic_set_alloc_inequality(__isl_keep isl_basic_setisl_basic_map *bset)
1530{
1531 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1532}
1533
1534int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1535{
1536 if (!bmap)
1537 return -1;
1538 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1)do { if (n <= bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1538); return -1; } while (0); } while (0)
;
1539 bmap->n_ineq -= n;
1540 return 0;
1541}
1542
1543int isl_basic_set_free_inequality(struct isl_basic_setisl_basic_map *bset, unsigned n)
1544{
1545 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1546}
1547
1548int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1549{
1550 isl_int *t;
1551 if (!bmap)
1552 return -1;
1553 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1)do { if (pos < bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1553); return -1; } while (0); } while (0)
;
1554
1555 if (pos != bmap->n_ineq - 1) {
1556 t = bmap->ineq[pos];
1557 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1558 bmap->ineq[bmap->n_ineq - 1] = t;
1559 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
1560 }
1561 bmap->n_ineq--;
1562 return 0;
1563}
1564
1565int isl_basic_set_drop_inequality(struct isl_basic_setisl_basic_map *bset, unsigned pos)
1566{
1567 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1568}
1569
1570__isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1571 isl_int *eq)
1572{
1573 int k;
1574
1575 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1576 if (!bmap)
1577 return NULL((void*)0);
1578 k = isl_basic_map_alloc_equality(bmap);
1579 if (k < 0)
1580 goto error;
1581 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1582 return bmap;
1583error:
1584 isl_basic_map_free(bmap);
1585 return NULL((void*)0);
1586}
1587
1588__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_eq(__isl_take isl_basic_setisl_basic_map *bset,
1589 isl_int *eq)
1590{
1591 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1592}
1593
1594__isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1595 isl_int *ineq)
1596{
1597 int k;
1598
1599 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1600 if (!bmap)
1601 return NULL((void*)0);
1602 k = isl_basic_map_alloc_inequality(bmap);
1603 if (k < 0)
1604 goto error;
1605 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1606 return bmap;
1607error:
1608 isl_basic_map_free(bmap);
1609 return NULL((void*)0);
1610}
1611
1612__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_ineq(__isl_take isl_basic_setisl_basic_map *bset,
1613 isl_int *ineq)
1614{
1615 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1616}
1617
1618int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1619{
1620 if (!bmap)
1621 return -1;
1622 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1)do { if (bmap->n_div < bmap->extra) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "bmap->n_div < bmap->extra"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1622); return -1; } while (0); } while (0)
;
1623 isl_seq_clr(bmap->div[bmap->n_div] +
1624 1 + 1 + isl_basic_map_total_dim(bmap),
1625 bmap->extra - bmap->n_div);
1626 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS)(((bmap)->flags) &= ~((1 << 6)));
1627 return bmap->n_div++;
1628}
1629
1630int isl_basic_set_alloc_div(struct isl_basic_setisl_basic_map *bset)
1631{
1632 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1633}
1634
1635/* Check that there are "n" dimensions of type "type" starting at "first"
1636 * in "bmap".
1637 */
1638static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1639 enum isl_dim_type type, unsigned first, unsigned n)
1640{
1641 unsigned dim;
1642
1643 if (!bmap)
1644 return isl_stat_error;
1645 dim = isl_basic_map_dim(bmap, type);
1646 if (first + n > dim || first + n < first)
1647 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1649); return isl_stat_error; } while (0)
1648 "position or range out of bounds",do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1649); return isl_stat_error; } while (0)
1649 return isl_stat_error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "position or range out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1649); return isl_stat_error; } while (0)
;
1650 return isl_stat_ok;
1651}
1652
1653/* Insert an extra integer division, prescribed by "div", to "bmap"
1654 * at (integer division) position "pos".
1655 *
1656 * The integer division is first added at the end and then moved
1657 * into the right position.
1658 */
1659__isl_give isl_basic_map *isl_basic_map_insert_div(
1660 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1661{
1662 int i, k;
1663
1664 bmap = isl_basic_map_cow(bmap);
1665 if (!bmap || !div)
1666 return isl_basic_map_free(bmap);
1667
1668 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1669 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "unexpected size", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1670); return isl_basic_map_free(bmap); } while (0)
1670 "unexpected size", return isl_basic_map_free(bmap))do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "unexpected size", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1670); return isl_basic_map_free(bmap); } while (0)
;
1671 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1672 return isl_basic_map_free(bmap);
1673
1674 bmap = isl_basic_map_extend_space(bmap,
1675 isl_basic_map_get_space(bmap), 1, 0, 2);
1676 k = isl_basic_map_alloc_div(bmap);
1677 if (k < 0)
1678 return isl_basic_map_free(bmap);
1679 isl_seq_cpy(bmap->div[k], div->el, div->size);
1680 isl_int_set_si(bmap->div[k][div->size], 0)isl_sioimath_set_si((bmap->div[k][div->size]), 0);
1681
1682 for (i = k; i > pos; --i)
1683 isl_basic_map_swap_div(bmap, i, i - 1);
1684
1685 return bmap;
1686}
1687
1688isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1689{
1690 if (!bmap)
1691 return isl_stat_error;
1692 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error)do { if (n <= bmap->n_div) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "n <= bmap->n_div"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1692); return isl_stat_error; } while (0); } while (0)
;
1693 bmap->n_div -= n;
1694 return isl_stat_ok;
1695}
1696
1697/* Copy constraint from src to dst, putting the vars of src at offset
1698 * dim_off in dst and the divs of src at offset div_off in dst.
1699 * If both sets are actually map, then dim_off applies to the input
1700 * variables.
1701 */
1702static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1703 struct isl_basic_map *src_map, isl_int *src,
1704 unsigned in_off, unsigned out_off, unsigned div_off)
1705{
1706 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1707 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1708 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1709 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1710 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1711 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1712 isl_int_set(dst[0], src[0])isl_sioimath_set((dst[0]), *(src[0]));
1713 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam)((dst_nparam < src_nparam) ? (dst_nparam) : (src_nparam)));
1714 if (dst_nparam > src_nparam)
1715 isl_seq_clr(dst+1+src_nparam,
1716 dst_nparam - src_nparam);
1717 isl_seq_clr(dst+1+dst_nparam, in_off);
1718 isl_seq_cpy(dst+1+dst_nparam+in_off,
1719 src+1+src_nparam,
1720 isl_min(dst_in-in_off, src_in)((dst_in-in_off < src_in) ? (dst_in-in_off) : (src_in)));
1721 if (dst_in-in_off > src_in)
1722 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1723 dst_in - in_off - src_in);
1724 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1725 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1726 src+1+src_nparam+src_in,
1727 isl_min(dst_out-out_off, src_out)((dst_out-out_off < src_out) ? (dst_out-out_off) : (src_out
))
);
1728 if (dst_out-out_off > src_out)
1729 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1730 dst_out - out_off - src_out);
1731 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1732 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1733 src+1+src_nparam+src_in+src_out,
1734 isl_min(dst_map->extra-div_off, src_map->n_div)((dst_map->extra-div_off < src_map->n_div) ? (dst_map
->extra-div_off) : (src_map->n_div))
);
1735 if (dst_map->n_div-div_off > src_map->n_div)
1736 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1737 div_off+src_map->n_div,
1738 dst_map->n_div - div_off - src_map->n_div);
1739}
1740
1741static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1742 struct isl_basic_map *src_map, isl_int *src,
1743 unsigned in_off, unsigned out_off, unsigned div_off)
1744{
1745 isl_int_set(dst[0], src[0])isl_sioimath_set((dst[0]), *(src[0]));
1746 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1747}
1748
1749static __isl_give isl_basic_map *add_constraints(
1750 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1751 unsigned i_pos, unsigned o_pos)
1752{
1753 int i;
1754 unsigned div_off;
1755
1756 if (!bmap1 || !bmap2)
1757 goto error;
1758
1759 div_off = bmap1->n_div;
1760
1761 for (i = 0; i < bmap2->n_eq; ++i) {
1762 int i1 = isl_basic_map_alloc_equality(bmap1);
1763 if (i1 < 0)
1764 goto error;
1765 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1766 i_pos, o_pos, div_off);
1767 }
1768
1769 for (i = 0; i < bmap2->n_ineq; ++i) {
1770 int i1 = isl_basic_map_alloc_inequality(bmap1);
1771 if (i1 < 0)
1772 goto error;
1773 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1774 i_pos, o_pos, div_off);
1775 }
1776
1777 for (i = 0; i < bmap2->n_div; ++i) {
1778 int i1 = isl_basic_map_alloc_div(bmap1);
1779 if (i1 < 0)
1780 goto error;
1781 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1782 i_pos, o_pos, div_off);
1783 }
1784
1785 isl_basic_map_free(bmap2);
1786
1787 return bmap1;
1788
1789error:
1790 isl_basic_map_free(bmap1);
1791 isl_basic_map_free(bmap2);
1792 return NULL((void*)0);
1793}
1794
1795struct isl_basic_setisl_basic_map *isl_basic_set_add_constraints(struct isl_basic_setisl_basic_map *bset1,
1796 struct isl_basic_setisl_basic_map *bset2, unsigned pos)
1797{
1798 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1799 bset_to_bmap(bset2), 0, pos));
1800}
1801
1802__isl_give isl_basic_map *isl_basic_map_extend_space(
1803 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1804 unsigned extra, unsigned n_eq, unsigned n_ineq)
1805{
1806 struct isl_basic_map *ext;
1807 unsigned flags;
1808 int dims_ok;
1809
1810 if (!dim)
1811 goto error;
1812
1813 if (!base)
1814 goto error;
1815
1816 dims_ok = isl_space_is_equal(base->dim, dim) &&
1817 base->extra >= base->n_div + extra;
1818
1819 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1820 room_for_ineq(base, n_ineq)) {
1821 isl_space_free(dim);
1822 return base;
1823 }
1824
1825 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error)do { if (base->dim->nparam <= dim->nparam) break;
do { isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->nparam <= dim->nparam" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1825); goto error; } while (0); } while (0)
;
1826 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error)do { if (base->dim->n_in <= dim->n_in) break; do {
isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->n_in <= dim->n_in" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1826); goto error; } while (0); } while (0)
;
1827 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error)do { if (base->dim->n_out <= dim->n_out) break; do
{ isl_handle_error(base->ctx, isl_error_unknown, "Assertion \""
"base->dim->n_out <= dim->n_out" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1827); goto error; } while (0); } while (0)
;
1828 extra += base->extra;
1829 n_eq += base->n_eq;
1830 n_ineq += base->n_ineq;
1831
1832 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1833 dim = NULL((void*)0);
1834 if (!ext)
1835 goto error;
1836
1837 if (dims_ok)
1838 ext->sample = isl_vec_copy(base->sample);
1839 flags = base->flags;
1840 ext = add_constraints(ext, base, 0, 0);
1841 if (ext) {
1842 ext->flags = flags;
1843 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL)(((ext)->flags) &= ~((1 << 0)));
1844 }
1845
1846 return ext;
1847
1848error:
1849 isl_space_free(dim);
1850 isl_basic_map_free(base);
1851 return NULL((void*)0);
1852}
1853
1854__isl_give isl_basic_setisl_basic_map *isl_basic_set_extend_space(
1855 __isl_take isl_basic_setisl_basic_map *base,
1856 __isl_take isl_space *dim, unsigned extra,
1857 unsigned n_eq, unsigned n_ineq)
1858{
1859 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1860 dim, extra, n_eq, n_ineq));
1861}
1862
1863struct isl_basic_map *isl_basic_map_extend_constraints(
1864 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1865{
1866 if (!base)
1867 return NULL((void*)0);
1868 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1869 0, n_eq, n_ineq);
1870}
1871
1872struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1873 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1874 unsigned n_eq, unsigned n_ineq)
1875{
1876 struct isl_basic_map *bmap;
1877 isl_space *dim;
1878
1879 if (!base)
1880 return NULL((void*)0);
1881 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1882 if (!dim)
1883 goto error;
1884
1885 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1886 return bmap;
1887error:
1888 isl_basic_map_free(base);
1889 return NULL((void*)0);
1890}
1891
1892struct isl_basic_setisl_basic_map *isl_basic_set_extend(struct isl_basic_setisl_basic_map *base,
1893 unsigned nparam, unsigned dim, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1895{
1896 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1897 nparam, 0, dim, extra, n_eq, n_ineq));
1898}
1899
1900struct isl_basic_setisl_basic_map *isl_basic_set_extend_constraints(
1901 struct isl_basic_setisl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1902{
1903 isl_basic_map *bmap = bset_to_bmap(base);
1904 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1905 return bset_from_bmap(bmap);
1906}
1907
1908__isl_give isl_basic_setisl_basic_map *isl_basic_set_cow(__isl_take isl_basic_setisl_basic_map *bset)
1909{
1910 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1911}
1912
1913__isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1914{
1915 if (!bmap)
1916 return NULL((void*)0);
1917
1918 if (bmap->ref > 1) {
1919 bmap->ref--;
1920 bmap = isl_basic_map_dup(bmap);
1921 }
1922 if (bmap) {
1923 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL)(((bmap)->flags) &= ~((1 << 0)));
1924 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS)(((bmap)->flags) &= ~((1 << 8)));
1925 }
1926 return bmap;
1927}
1928
1929/* Clear all cached information in "map", either because it is about
1930 * to be modified or because it is being freed.
1931 * Always return the same pointer that is passed in.
1932 * This is needed for the use in isl_map_free.
1933 */
1934static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1935{
1936 isl_basic_map_free(map->cached_simple_hull[0]);
1937 isl_basic_map_free(map->cached_simple_hull[1]);
1938 map->cached_simple_hull[0] = NULL((void*)0);
1939 map->cached_simple_hull[1] = NULL((void*)0);
1940 return map;
1941}
1942
1943__isl_give isl_setisl_map *isl_set_cow(__isl_take isl_setisl_map *set)
1944{
1945 return isl_map_cow(set);
1946}
1947
1948/* Return an isl_map that is equal to "map" and that has only
1949 * a single reference.
1950 *
1951 * If the original input already has only one reference, then
1952 * simply return it, but clear all cached information, since
1953 * it may be rendered invalid by the operations that will be
1954 * performed on the result.
1955 *
1956 * Otherwise, create a duplicate (without any cached information).
1957 */
1958__isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1959{
1960 if (!map)
1961 return NULL((void*)0);
1962
1963 if (map->ref == 1)
1964 return clear_caches(map);
1965 map->ref--;
1966 return isl_map_dup(map);
1967}
1968
1969static void swap_vars(struct isl_blk blk, isl_int *a,
1970 unsigned a_len, unsigned b_len)
1971{
1972 isl_seq_cpy(blk.data, a+a_len, b_len);
1973 isl_seq_cpy(blk.data+b_len, a, a_len);
1974 isl_seq_cpy(a, blk.data, b_len+a_len);
1975}
1976
1977static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1978 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1979{
1980 int i;
1981 struct isl_blk blk;
1982
1983 if (!bmap)
1984 goto error;
1985
1986 isl_assert(bmap->ctx,do { if (pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap
)) break; do { isl_handle_error(bmap->ctx, isl_error_unknown
, "Assertion \"" "pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1987); goto error; } while (0); } while (0)
1987 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error)do { if (pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap
)) break; do { isl_handle_error(bmap->ctx, isl_error_unknown
, "Assertion \"" "pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 1987); goto error; } while (0); } while (0)
;
1988
1989 if (n1 == 0 || n2 == 0)
1990 return bmap;
1991
1992 bmap = isl_basic_map_cow(bmap);
1993 if (!bmap)
1994 return NULL((void*)0);
1995
1996 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1997 if (isl_blk_is_error(blk))
1998 goto error;
1999
2000 for (i = 0; i < bmap->n_eq; ++i)
2001 swap_vars(blk,
2002 bmap->eq[i] + pos, n1, n2);
2003
2004 for (i = 0; i < bmap->n_ineq; ++i)
2005 swap_vars(blk,
2006 bmap->ineq[i] + pos, n1, n2);
2007
2008 for (i = 0; i < bmap->n_div; ++i)
2009 swap_vars(blk,
2010 bmap->div[i]+1 + pos, n1, n2);
2011
2012 isl_blk_free(bmap->ctx, blk);
2013
2014 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2015 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
2016 return isl_basic_map_finalize(bmap);
2017error:
2018 isl_basic_map_free(bmap);
2019 return NULL((void*)0);
2020}
2021
2022__isl_give isl_basic_map *isl_basic_map_set_to_empty(
2023 __isl_take isl_basic_map *bmap)
2024{
2025 int i = 0;
2026 unsigned total;
2027 if (!bmap)
2028 goto error;
2029 total = isl_basic_map_total_dim(bmap);
2030 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2031 return isl_basic_map_free(bmap);
2032 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2033 if (bmap->n_eq > 0)
2034 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2035 else {
2036 i = isl_basic_map_alloc_equality(bmap);
2037 if (i < 0)
2038 goto error;
2039 }
2040 isl_int_set_si(bmap->eq[i][0], 1)isl_sioimath_set_si((bmap->eq[i][0]), 1);
2041 isl_seq_clr(bmap->eq[i]+1, total);
2042 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY)(((bmap)->flags) |= ((1 << 1)));
2043 isl_vec_free(bmap->sample);
2044 bmap->sample = NULL((void*)0);
2045 return isl_basic_map_finalize(bmap);
2046error:
2047 isl_basic_map_free(bmap);
2048 return NULL((void*)0);
2049}
2050
2051__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_to_empty(
2052 __isl_take isl_basic_setisl_basic_map *bset)
2053{
2054 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2055}
2056
2057__isl_give isl_basic_map *isl_basic_map_set_rational(
2058 __isl_take isl_basic_map *bmap)
2059{
2060 if (!bmap)
2061 return NULL((void*)0);
2062
2063 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
2064 return bmap;
2065
2066 bmap = isl_basic_map_cow(bmap);
2067 if (!bmap)
2068 return NULL((void*)0);
2069
2070 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL)(((bmap)->flags) |= ((1 << 4)));
2071
2072 return isl_basic_map_finalize(bmap);
2073}
2074
2075__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_rational(
2076 __isl_take isl_basic_setisl_basic_map *bset)
2077{
2078 return isl_basic_map_set_rational(bset);
2079}
2080
2081__isl_give isl_basic_setisl_basic_map *isl_basic_set_set_integral(
2082 __isl_take isl_basic_setisl_basic_map *bset)
2083{
2084 if (!bset)
2085 return NULL((void*)0);
2086
2087 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL)(!!(((bset)->flags) & ((1 << 4)))))
2088 return bset;
2089
2090 bset = isl_basic_set_cow(bset);
2091 if (!bset)
2092 return NULL((void*)0);
2093
2094 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL)(((bset)->flags) &= ~((1 << 4)));
2095
2096 return isl_basic_set_finalize(bset);
2097}
2098
2099__isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2100{
2101 int i;
2102
2103 map = isl_map_cow(map);
2104 if (!map)
2105 return NULL((void*)0);
2106 for (i = 0; i < map->n; ++i) {
2107 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2108 if (!map->p[i])
2109 goto error;
2110 }
2111 return map;
2112error:
2113 isl_map_free(map);
2114 return NULL((void*)0);
2115}
2116
2117__isl_give isl_setisl_map *isl_set_set_rational(__isl_take isl_setisl_map *set)
2118{
2119 return isl_map_set_rational(set);
2120}
2121
2122/* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2123 * of "bmap").
2124 */
2125static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2126{
2127 isl_int *t = bmap->div[a];
2128 bmap->div[a] = bmap->div[b];
2129 bmap->div[b] = t;
2130}
2131
2132/* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2133 * div definitions accordingly.
2134 */
2135void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2136{
2137 int i;
2138 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2139
2140 swap_div(bmap, a, b);
2141
2142 for (i = 0; i < bmap->n_eq; ++i)
2143 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b])isl_sioimath_swap((bmap->eq[i][1+off+a]), (bmap->eq[i][
1+off+b]))
;
2144
2145 for (i = 0; i < bmap->n_ineq; ++i)
2146 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b])isl_sioimath_swap((bmap->ineq[i][1+off+a]), (bmap->ineq
[i][1+off+b]))
;
2147
2148 for (i = 0; i < bmap->n_div; ++i)
2149 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b])isl_sioimath_swap((bmap->div[i][1+1+off+a]), (bmap->div
[i][1+1+off+b]))
;
2150 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2151}
2152
2153/* Swap divs "a" and "b" in "bset" and adjust the constraints and
2154 * div definitions accordingly.
2155 */
2156void isl_basic_set_swap_div(__isl_keep isl_basic_setisl_basic_map *bset, int a, int b)
2157{
2158 isl_basic_map_swap_div(bset, a, b);
2159}
2160
2161static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2162{
2163 isl_seq_cpy(c, c + n, rem);
2164 isl_seq_clr(c + rem, n);
2165}
2166
2167/* Drop n dimensions starting at first.
2168 *
2169 * In principle, this frees up some extra variables as the number
2170 * of columns remains constant, but we would have to extend
2171 * the div array too as the number of rows in this array is assumed
2172 * to be equal to extra.
2173 */
2174__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_dims(
2175 __isl_take isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2176{
2177 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2178}
2179
2180/* Move "n" divs starting at "first" to the end of the list of divs.
2181 */
2182static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2183 unsigned first, unsigned n)
2184{
2185 isl_int **div;
2186 int i;
2187
2188 if (first + n == bmap->n_div)
2189 return bmap;
2190
2191 div = isl_alloc_array(bmap->ctx, isl_int *, n)((isl_int * *)isl_malloc_or_die(bmap->ctx, (n)*sizeof(isl_int
*)))
;
2192 if (!div)
2193 goto error;
2194 for (i = 0; i < n; ++i)
2195 div[i] = bmap->div[first + i];
2196 for (i = 0; i < bmap->n_div - first - n; ++i)
2197 bmap->div[first + i] = bmap->div[first + n + i];
2198 for (i = 0; i < n; ++i)
2199 bmap->div[bmap->n_div - n + i] = div[i];
2200 free(div);
2201 return bmap;
2202error:
2203 isl_basic_map_free(bmap);
2204 return NULL((void*)0);
2205}
2206
2207/* Drop "n" dimensions of type "type" starting at "first".
2208 *
2209 * In principle, this frees up some extra variables as the number
2210 * of columns remains constant, but we would have to extend
2211 * the div array too as the number of rows in this array is assumed
2212 * to be equal to extra.
2213 */
2214__isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2215 enum isl_dim_type type, unsigned first, unsigned n)
2216{
2217 int i;
2218 unsigned dim;
2219 unsigned offset;
2220 unsigned left;
2221
2222 if (!bmap)
2223 goto error;
2224
2225 dim = isl_basic_map_dim(bmap, type);
2226 isl_assert(bmap->ctx, first + n <= dim, goto error)do { if (first + n <= dim) break; do { isl_handle_error(bmap
->ctx, isl_error_unknown, "Assertion \"" "first + n <= dim"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2226); goto error; } while (0); } while (0)
;
2227
2228 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2229 return bmap;
2230
2231 bmap = isl_basic_map_cow(bmap);
2232 if (!bmap)
2233 return NULL((void*)0);
2234
2235 offset = isl_basic_map_offset(bmap, type) + first;
2236 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2237 for (i = 0; i < bmap->n_eq; ++i)
2238 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2239
2240 for (i = 0; i < bmap->n_ineq; ++i)
2241 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2242
2243 for (i = 0; i < bmap->n_div; ++i)
2244 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2245
2246 if (type == isl_dim_div) {
2247 bmap = move_divs_last(bmap, first, n);
2248 if (!bmap)
2249 goto error;
2250 if (isl_basic_map_free_div(bmap, n) < 0)
2251 return isl_basic_map_free(bmap);
2252 } else
2253 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2254 if (!bmap->dim)
2255 goto error;
2256
2257 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2258 bmap = isl_basic_map_simplify(bmap);
2259 return isl_basic_map_finalize(bmap);
2260error:
2261 isl_basic_map_free(bmap);
2262 return NULL((void*)0);
2263}
2264
2265__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop(__isl_take isl_basic_setisl_basic_map *bset,
2266 enum isl_dim_type type, unsigned first, unsigned n)
2267{
2268 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2269 type, first, n));
2270}
2271
2272__isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2273 enum isl_dim_type type, unsigned first, unsigned n)
2274{
2275 int i;
2276
2277 if (!map)
2278 goto error;
2279
2280 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2280); goto error; } while (0); } while (0)
;
2281
2282 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2283 return map;
2284 map = isl_map_cow(map);
2285 if (!map)
2286 goto error;
2287 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2288 if (!map->dim)
2289 goto error;
2290
2291 for (i = 0; i < map->n; ++i) {
2292 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2293 if (!map->p[i])
2294 goto error;
2295 }
2296 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
2297
2298 return map;
2299error:
2300 isl_map_free(map);
2301 return NULL((void*)0);
2302}
2303
2304__isl_give isl_setisl_map *isl_set_drop(__isl_take isl_setisl_map *set,
2305 enum isl_dim_type type, unsigned first, unsigned n)
2306{
2307 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2308}
2309
2310/*
2311 * We don't cow, as the div is assumed to be redundant.
2312 */
2313__isl_give isl_basic_map *isl_basic_map_drop_div(
2314 __isl_take isl_basic_map *bmap, unsigned div)
2315{
2316 int i;
2317 unsigned pos;
2318
2319 if (!bmap)
2320 goto error;
2321
2322 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2323
2324 isl_assert(bmap->ctx, div < bmap->n_div, goto error)do { if (div < bmap->n_div) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "div < bmap->n_div"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2324); goto error; } while (0); } while (0)
;
2325
2326 for (i = 0; i < bmap->n_eq; ++i)
2327 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2328
2329 for (i = 0; i < bmap->n_ineq; ++i) {
2330 if (!isl_int_is_zero(bmap->ineq[i][pos])(isl_sioimath_sgn(*(bmap->ineq[i][pos])) == 0)) {
2331 isl_basic_map_drop_inequality(bmap, i);
2332 --i;
2333 continue;
2334 }
2335 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2336 }
2337
2338 for (i = 0; i < bmap->n_div; ++i)
2339 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2340
2341 if (div != bmap->n_div - 1) {
2342 int j;
2343 isl_int *t = bmap->div[div];
2344
2345 for (j = div; j < bmap->n_div - 1; ++j)
2346 bmap->div[j] = bmap->div[j+1];
2347
2348 bmap->div[bmap->n_div - 1] = t;
2349 }
2350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2351 if (isl_basic_map_free_div(bmap, 1) < 0)
2352 return isl_basic_map_free(bmap);
2353
2354 return bmap;
2355error:
2356 isl_basic_map_free(bmap);
2357 return NULL((void*)0);
2358}
2359
2360/* Eliminate the specified n dimensions starting at first from the
2361 * constraints, without removing the dimensions from the space.
2362 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2363 */
2364__isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2365 enum isl_dim_type type, unsigned first, unsigned n)
2366{
2367 int i;
2368
2369 if (!map)
2370 return NULL((void*)0);
2371 if (n == 0)
2372 return map;
2373
2374 if (first + n > isl_map_dim(map, type) || first + n < first)
2375 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2376); goto error; } while (0)
2376 "index out of bounds", goto error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2376); goto error; } while (0)
;
2377
2378 map = isl_map_cow(map);
2379 if (!map)
2380 return NULL((void*)0);
2381
2382 for (i = 0; i < map->n; ++i) {
2383 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2384 if (!map->p[i])
2385 goto error;
2386 }
2387 return map;
2388error:
2389 isl_map_free(map);
2390 return NULL((void*)0);
2391}
2392
2393/* Eliminate the specified n dimensions starting at first from the
2394 * constraints, without removing the dimensions from the space.
2395 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2396 */
2397__isl_give isl_setisl_map *isl_set_eliminate(__isl_take isl_setisl_map *set,
2398 enum isl_dim_type type, unsigned first, unsigned n)
2399{
2400 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2401}
2402
2403/* Eliminate the specified n dimensions starting at first from the
2404 * constraints, without removing the dimensions from the space.
2405 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2406 */
2407__isl_give isl_setisl_map *isl_set_eliminate_dims(__isl_take isl_setisl_map *set,
2408 unsigned first, unsigned n)
2409{
2410 return isl_set_eliminate(set, isl_dim_set, first, n);
2411}
2412
2413__isl_give isl_basic_map *isl_basic_map_remove_divs(
2414 __isl_take isl_basic_map *bmap)
2415{
2416 if (!bmap)
2417 return NULL((void*)0);
2418 bmap = isl_basic_map_eliminate_vars(bmap,
2419 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2420 if (!bmap)
2421 return NULL((void*)0);
2422 bmap->n_div = 0;
2423 return isl_basic_map_finalize(bmap);
2424}
2425
2426__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs(
2427 __isl_take isl_basic_setisl_basic_map *bset)
2428{
2429 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2430}
2431
2432__isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2433{
2434 int i;
2435
2436 if (!map)
2437 return NULL((void*)0);
2438 if (map->n == 0)
2439 return map;
2440
2441 map = isl_map_cow(map);
2442 if (!map)
2443 return NULL((void*)0);
2444
2445 for (i = 0; i < map->n; ++i) {
2446 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2447 if (!map->p[i])
2448 goto error;
2449 }
2450 return map;
2451error:
2452 isl_map_free(map);
2453 return NULL((void*)0);
2454}
2455
2456__isl_give isl_setisl_map *isl_set_remove_divs(__isl_take isl_setisl_map *set)
2457{
2458 return isl_map_remove_divs(set);
2459}
2460
2461__isl_give isl_basic_map *isl_basic_map_remove_dims(
2462 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2463 unsigned first, unsigned n)
2464{
2465 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2466 return isl_basic_map_free(bmap);
2467 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2468 return bmap;
2469 bmap = isl_basic_map_eliminate_vars(bmap,
2470 isl_basic_map_offset(bmap, type) - 1 + first, n);
2471 if (!bmap)
2472 return bmap;
2473 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY)(!!(((bmap)->flags) & ((1 << 1)))) && type == isl_dim_div)
2474 return bmap;
2475 bmap = isl_basic_map_drop(bmap, type, first, n);
2476 return bmap;
2477}
2478
2479/* Return true if the definition of the given div (recursively) involves
2480 * any of the given variables.
2481 */
2482static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2483 unsigned first, unsigned n)
2484{
2485 int i;
2486 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2487
2488 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2489 return isl_bool_false;
2490 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2491 return isl_bool_true;
2492
2493 for (i = bmap->n_div - 1; i >= 0; --i) {
2494 isl_bool involves;
2495
2496 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
2497 continue;
2498 involves = div_involves_vars(bmap, i, first, n);
2499 if (involves < 0 || involves)
2500 return involves;
2501 }
2502
2503 return isl_bool_false;
2504}
2505
2506/* Try and add a lower and/or upper bound on "div" to "bmap"
2507 * based on inequality "i".
2508 * "total" is the total number of variables (excluding the divs).
2509 * "v" is a temporary object that can be used during the calculations.
2510 * If "lb" is set, then a lower bound should be constructed.
2511 * If "ub" is set, then an upper bound should be constructed.
2512 *
2513 * The calling function has already checked that the inequality does not
2514 * reference "div", but we still need to check that the inequality is
2515 * of the right form. We'll consider the case where we want to construct
2516 * a lower bound. The construction of upper bounds is similar.
2517 *
2518 * Let "div" be of the form
2519 *
2520 * q = floor((a + f(x))/d)
2521 *
2522 * We essentially check if constraint "i" is of the form
2523 *
2524 * b + f(x) >= 0
2525 *
2526 * so that we can use it to derive a lower bound on "div".
2527 * However, we allow a slightly more general form
2528 *
2529 * b + g(x) >= 0
2530 *
2531 * with the condition that the coefficients of g(x) - f(x) are all
2532 * divisible by d.
2533 * Rewriting this constraint as
2534 *
2535 * 0 >= -b - g(x)
2536 *
2537 * adding a + f(x) to both sides and dividing by d, we obtain
2538 *
2539 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2540 *
2541 * Taking the floor on both sides, we obtain
2542 *
2543 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2544 *
2545 * or
2546 *
2547 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2548 *
2549 * In the case of an upper bound, we construct the constraint
2550 *
2551 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2552 *
2553 */
2554static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2555 __isl_take isl_basic_map *bmap, int div, int i,
2556 unsigned total, isl_int v, int lb, int ub)
2557{
2558 int j;
2559
2560 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2561 if (lb) {
2562 isl_int_sub(v, bmap->ineq[i][1 + j],isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2563 bmap->div[div][1 + 1 + j])isl_sioimath_sub((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2564 lb = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2565 }
2566 if (ub) {
2567 isl_int_add(v, bmap->ineq[i][1 + j],isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
2568 bmap->div[div][1 + 1 + j])isl_sioimath_add((v), *(bmap->ineq[i][1 + j]), *(bmap->
div[div][1 + 1 + j]))
;
2569 ub = isl_int_is_divisible_by(v, bmap->div[div][0])isl_sioimath_is_divisible_by(*(v), *(bmap->div[div][0]));
2570 }
2571 }
2572 if (!lb && !ub)
2573 return bmap;
2574
2575 bmap = isl_basic_map_cow(bmap);
2576 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2577 if (lb) {
2578 int k = isl_basic_map_alloc_inequality(bmap);
2579 if (k < 0)
2580 goto error;
2581 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2582 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],isl_sioimath_sub((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
2583 bmap->div[div][1 + j])isl_sioimath_sub((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2584 isl_int_cdiv_q(bmap->ineq[k][j],isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2585 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_cdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2586 }
2587 isl_int_set_si(bmap->ineq[k][1 + total + div], 1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), 1);
2588 }
2589 if (ub) {
2590 int k = isl_basic_map_alloc_inequality(bmap);
2591 if (k < 0)
2592 goto error;
2593 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2594 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],isl_sioimath_add((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
2595 bmap->div[div][1 + j])isl_sioimath_add((bmap->ineq[k][j]), *(bmap->ineq[i][j]
), *(bmap->div[div][1 + j]))
;
2596 isl_int_fdiv_q(bmap->ineq[k][j],isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
2597 bmap->ineq[k][j], bmap->div[div][0])isl_sioimath_fdiv_q((bmap->ineq[k][j]), *(bmap->ineq[k]
[j]), *(bmap->div[div][0]))
;
2598 }
2599 isl_int_set_si(bmap->ineq[k][1 + total + div], -1)isl_sioimath_set_si((bmap->ineq[k][1 + total + div]), -1);
2600 }
2601
2602 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
2603 return bmap;
2604error:
2605 isl_basic_map_free(bmap);
2606 return NULL((void*)0);
2607}
2608
2609/* This function is called right before "div" is eliminated from "bmap"
2610 * using Fourier-Motzkin.
2611 * Look through the constraints of "bmap" for constraints on the argument
2612 * of the integer division and use them to construct constraints on the
2613 * integer division itself. These constraints can then be combined
2614 * during the Fourier-Motzkin elimination.
2615 * Note that it is only useful to introduce lower bounds on "div"
2616 * if "bmap" already contains upper bounds on "div" as the newly
2617 * introduce lower bounds can then be combined with the pre-existing
2618 * upper bounds. Similarly for upper bounds.
2619 * We therefore first check if "bmap" contains any lower and/or upper bounds
2620 * on "div".
2621 *
2622 * It is interesting to note that the introduction of these constraints
2623 * can indeed lead to more accurate results, even when compared to
2624 * deriving constraints on the argument of "div" from constraints on "div".
2625 * Consider, for example, the set
2626 *
2627 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2628 *
2629 * The second constraint can be rewritten as
2630 *
2631 * 2 * [(-i-2j+3)/4] + k >= 0
2632 *
2633 * from which we can derive
2634 *
2635 * -i - 2j + 3 >= -2k
2636 *
2637 * or
2638 *
2639 * i + 2j <= 3 + 2k
2640 *
2641 * Combined with the first constraint, we obtain
2642 *
2643 * -3 <= 3 + 2k or k >= -3
2644 *
2645 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2646 * the first constraint, we obtain
2647 *
2648 * [(i + 2j)/4] >= [-3/4] = -1
2649 *
2650 * Combining this constraint with the second constraint, we obtain
2651 *
2652 * k >= -2
2653 */
2654static __isl_give isl_basic_map *insert_bounds_on_div(
2655 __isl_take isl_basic_map *bmap, int div)
2656{
2657 int i;
2658 int check_lb, check_ub;
2659 isl_int v;
2660 unsigned total;
2661
2662 if (!bmap)
2663 return NULL((void*)0);
2664
2665 if (isl_int_is_zero(bmap->div[div][0])(isl_sioimath_sgn(*(bmap->div[div][0])) == 0))
2666 return bmap;
2667
2668 total = isl_space_dim(bmap->dim, isl_dim_all);
2669
2670 check_lb = 0;
2671 check_ub = 0;
2672 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2673 int s = isl_int_sgn(bmap->ineq[i][1 + total + div])isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div]));
2674 if (s > 0)
2675 check_ub = 1;
2676 if (s < 0)
2677 check_lb = 1;
2678 }
2679
2680 if (!check_lb && !check_ub)
2681 return bmap;
2682
2683 isl_int_init(v)isl_sioimath_init((v));
2684
2685 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2686 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div])(isl_sioimath_sgn(*(bmap->ineq[i][1 + total + div])) == 0))
2687 continue;
2688
2689 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2690 check_lb, check_ub);
2691 }
2692
2693 isl_int_clear(v)isl_sioimath_clear((v));
2694
2695 return bmap;
2696}
2697
2698/* Remove all divs (recursively) involving any of the given dimensions
2699 * in their definitions.
2700 */
2701__isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2702 __isl_take isl_basic_map *bmap,
2703 enum isl_dim_type type, unsigned first, unsigned n)
2704{
2705 int i;
2706
2707 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2708 return isl_basic_map_free(bmap);
2709 first += isl_basic_map_offset(bmap, type);
2710
2711 for (i = bmap->n_div - 1; i >= 0; --i) {
2712 isl_bool involves;
2713
2714 involves = div_involves_vars(bmap, i, first, n);
2715 if (involves < 0)
2716 return isl_basic_map_free(bmap);
2717 if (!involves)
2718 continue;
2719 bmap = insert_bounds_on_div(bmap, i);
2720 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2721 if (!bmap)
2722 return NULL((void*)0);
2723 i = bmap->n_div;
2724 }
2725
2726 return bmap;
2727}
2728
2729__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_divs_involving_dims(
2730 __isl_take isl_basic_setisl_basic_map *bset,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2732{
2733 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2734}
2735
2736__isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2737 enum isl_dim_type type, unsigned first, unsigned n)
2738{
2739 int i;
2740
2741 if (!map)
2742 return NULL((void*)0);
2743 if (map->n == 0)
2744 return map;
2745
2746 map = isl_map_cow(map);
2747 if (!map)
2748 return NULL((void*)0);
2749
2750 for (i = 0; i < map->n; ++i) {
2751 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2752 type, first, n);
2753 if (!map->p[i])
2754 goto error;
2755 }
2756 return map;
2757error:
2758 isl_map_free(map);
2759 return NULL((void*)0);
2760}
2761
2762__isl_give isl_setisl_map *isl_set_remove_divs_involving_dims(__isl_take isl_setisl_map *set,
2763 enum isl_dim_type type, unsigned first, unsigned n)
2764{
2765 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2766 type, first, n));
2767}
2768
2769/* Does the description of "bmap" depend on the specified dimensions?
2770 * We also check whether the dimensions appear in any of the div definitions.
2771 * In principle there is no need for this check. If the dimensions appear
2772 * in a div definition, they also appear in the defining constraints of that
2773 * div.
2774 */
2775isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2776 enum isl_dim_type type, unsigned first, unsigned n)
2777{
2778 int i;
2779
2780 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2781 return isl_bool_error;
2782
2783 first += isl_basic_map_offset(bmap, type);
2784 for (i = 0; i < bmap->n_eq; ++i)
2785 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2786 return isl_bool_true;
2787 for (i = 0; i < bmap->n_ineq; ++i)
2788 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2789 return isl_bool_true;
2790 for (i = 0; i < bmap->n_div; ++i) {
2791 if (isl_int_is_zero(bmap->div[i][0])(isl_sioimath_sgn(*(bmap->div[i][0])) == 0))
2792 continue;
2793 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2794 return isl_bool_true;
2795 }
2796
2797 return isl_bool_false;
2798}
2799
2800isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2801 enum isl_dim_type type, unsigned first, unsigned n)
2802{
2803 int i;
2804
2805 if (!map)
2806 return isl_bool_error;
2807
2808 if (first + n > isl_map_dim(map, type))
2809 isl_die(map->ctx, isl_error_invalid,do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2810); return isl_bool_error; } while (0)
2810 "index out of bounds", return isl_bool_error)do { isl_handle_error(map->ctx, isl_error_invalid, "index out of bounds"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2810); return isl_bool_error; } while (0)
;
2811
2812 for (i = 0; i < map->n; ++i) {
2813 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2814 type, first, n);
2815 if (involves < 0 || involves)
2816 return involves;
2817 }
2818
2819 return isl_bool_false;
2820}
2821
2822isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_setisl_basic_map *bset,
2823 enum isl_dim_type type, unsigned first, unsigned n)
2824{
2825 return isl_basic_map_involves_dims(bset, type, first, n);
2826}
2827
2828isl_bool isl_set_involves_dims(__isl_keep isl_setisl_map *set,
2829 enum isl_dim_type type, unsigned first, unsigned n)
2830{
2831 return isl_map_involves_dims(set, type, first, n);
2832}
2833
2834/* Drop all constraints in bmap that involve any of the dimensions
2835 * first to first+n-1.
2836 */
2837static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2838 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2839{
2840 int i;
2841
2842 if (n == 0)
2843 return bmap;
2844
2845 bmap = isl_basic_map_cow(bmap);
2846
2847 if (!bmap)
2848 return NULL((void*)0);
2849
2850 for (i = bmap->n_eq - 1; i >= 0; --i) {
2851 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2852 continue;
2853 isl_basic_map_drop_equality(bmap, i);
2854 }
2855
2856 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2857 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2858 continue;
2859 isl_basic_map_drop_inequality(bmap, i);
2860 }
2861
2862 bmap = isl_basic_map_add_known_div_constraints(bmap);
2863 return bmap;
2864}
2865
2866/* Drop all constraints in bset that involve any of the dimensions
2867 * first to first+n-1.
2868 */
2869__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving(
2870 __isl_take isl_basic_setisl_basic_map *bset, unsigned first, unsigned n)
2871{
2872 return isl_basic_map_drop_constraints_involving(bset, first, n);
2873}
2874
2875/* Drop all constraints in bmap that do not involve any of the dimensions
2876 * first to first + n - 1 of the given type.
2877 */
2878__isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2879 __isl_take isl_basic_map *bmap,
2880 enum isl_dim_type type, unsigned first, unsigned n)
2881{
2882 int i;
2883
2884 if (n == 0) {
2885 isl_space *space = isl_basic_map_get_space(bmap);
2886 isl_basic_map_free(bmap);
2887 return isl_basic_map_universe(space);
2888 }
2889 bmap = isl_basic_map_cow(bmap);
2890 if (!bmap)
2891 return NULL((void*)0);
2892
2893 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2894 return isl_basic_map_free(bmap);
2895
2896 first += isl_basic_map_offset(bmap, type) - 1;
2897
2898 for (i = bmap->n_eq - 1; i >= 0; --i) {
2899 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2900 continue;
2901 isl_basic_map_drop_equality(bmap, i);
2902 }
2903
2904 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2905 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2906 continue;
2907 isl_basic_map_drop_inequality(bmap, i);
2908 }
2909
2910 bmap = isl_basic_map_add_known_div_constraints(bmap);
2911 return bmap;
2912}
2913
2914/* Drop all constraints in bset that do not involve any of the dimensions
2915 * first to first + n - 1 of the given type.
2916 */
2917__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_not_involving_dims(
2918 __isl_take isl_basic_setisl_basic_map *bset,
2919 enum isl_dim_type type, unsigned first, unsigned n)
2920{
2921 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2922 type, first, n);
2923}
2924
2925/* Drop all constraints in bmap that involve any of the dimensions
2926 * first to first + n - 1 of the given type.
2927 */
2928__isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2929 __isl_take isl_basic_map *bmap,
2930 enum isl_dim_type type, unsigned first, unsigned n)
2931{
2932 if (!bmap)
2933 return NULL((void*)0);
2934 if (n == 0)
2935 return bmap;
2936
2937 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2938 return isl_basic_map_free(bmap);
2939
2940 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2941 first += isl_basic_map_offset(bmap, type) - 1;
2942 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2943}
2944
2945/* Drop all constraints in bset that involve any of the dimensions
2946 * first to first + n - 1 of the given type.
2947 */
2948__isl_give isl_basic_setisl_basic_map *isl_basic_set_drop_constraints_involving_dims(
2949 __isl_take isl_basic_setisl_basic_map *bset,
2950 enum isl_dim_type type, unsigned first, unsigned n)
2951{
2952 return isl_basic_map_drop_constraints_involving_dims(bset,
2953 type, first, n);
2954}
2955
2956/* Drop constraints from "map" by applying "drop" to each basic map.
2957 */
2958static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2959 enum isl_dim_type type, unsigned first, unsigned n,
2960 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2961 enum isl_dim_type type, unsigned first, unsigned n))
2962{
2963 int i;
2964 unsigned dim;
2965
2966 if (!map)
2967 return NULL((void*)0);
2968
2969 dim = isl_map_dim(map, type);
2970 if (first + n > dim || first + n < first)
2971 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2972); return isl_map_free(map); } while (0)
2972 "index out of bounds", return isl_map_free(map))do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 2972); return isl_map_free(map); } while (0)
;
2973
2974 map = isl_map_cow(map);
2975 if (!map)
2976 return NULL((void*)0);
2977
2978 for (i = 0; i < map->n; ++i) {
2979 map->p[i] = drop(map->p[i], type, first, n);
2980 if (!map->p[i])
2981 return isl_map_free(map);
2982 }
2983
2984 if (map->n > 1)
2985 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
2986
2987 return map;
2988}
2989
2990/* Drop all constraints in map that involve any of the dimensions
2991 * first to first + n - 1 of the given type.
2992 */
2993__isl_give isl_map *isl_map_drop_constraints_involving_dims(
2994 __isl_take isl_map *map,
2995 enum isl_dim_type type, unsigned first, unsigned n)
2996{
2997 if (n == 0)
2998 return map;
2999 return drop_constraints(map, type, first, n,
3000 &isl_basic_map_drop_constraints_involving_dims);
3001}
3002
3003/* Drop all constraints in "map" that do not involve any of the dimensions
3004 * first to first + n - 1 of the given type.
3005 */
3006__isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3007 __isl_take isl_map *map,
3008 enum isl_dim_type type, unsigned first, unsigned n)
3009{
3010 if (n == 0) {
3011 isl_space *space = isl_map_get_space(map);
3012 isl_map_free(map);
3013 return isl_map_universe(space);
3014 }
3015 return drop_constraints(map, type, first, n,
3016 &isl_basic_map_drop_constraints_not_involving_dims);
3017}
3018
3019/* Drop all constraints in set that involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3021 */
3022__isl_give isl_setisl_map *isl_set_drop_constraints_involving_dims(
3023 __isl_take isl_setisl_map *set,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3025{
3026 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3027}
3028
3029/* Drop all constraints in "set" that do not involve any of the dimensions
3030 * first to first + n - 1 of the given type.
3031 */
3032__isl_give isl_setisl_map *isl_set_drop_constraints_not_involving_dims(
3033 __isl_take isl_setisl_map *set,
3034 enum isl_dim_type type, unsigned first, unsigned n)
3035{
3036 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3037}
3038
3039/* Does local variable "div" of "bmap" have a complete explicit representation?
3040 * Having a complete explicit representation requires not only
3041 * an explicit representation, but also that all local variables
3042 * that appear in this explicit representation in turn have
3043 * a complete explicit representation.
3044 */
3045isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3046{
3047 int i;
3048 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3049 isl_bool marked;
3050
3051 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3052 if (marked < 0 || marked)
3053 return isl_bool_not(marked);
3054
3055 for (i = bmap->n_div - 1; i >= 0; --i) {
3056 isl_bool known;
3057
3058 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i])(isl_sioimath_sgn(*(bmap->div[div][1 + div_offset + i])) ==
0)
)
3059 continue;
3060 known = isl_basic_map_div_is_known(bmap, i);
3061 if (known < 0 || !known)
3062 return known;
3063 }
3064
3065 return isl_bool_true;
3066}
3067
3068/* Remove all divs that are unknown or defined in terms of unknown divs.
3069 */
3070__isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3071 __isl_take isl_basic_map *bmap)
3072{
3073 int i;
3074
3075 if (!bmap)
3076 return NULL((void*)0);
3077
3078 for (i = bmap->n_div - 1; i >= 0; --i) {
3079 if (isl_basic_map_div_is_known(bmap, i))
3080 continue;
3081 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3082 if (!bmap)
3083 return NULL((void*)0);
3084 i = bmap->n_div;
3085 }
3086
3087 return bmap;
3088}
3089
3090/* Remove all divs that are unknown or defined in terms of unknown divs.
3091 */
3092__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_unknown_divs(
3093 __isl_take isl_basic_setisl_basic_map *bset)
3094{
3095 return isl_basic_map_remove_unknown_divs(bset);
3096}
3097
3098__isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3099{
3100 int i;
3101
3102 if (!map)
3103 return NULL((void*)0);
3104 if (map->n == 0)
3105 return map;
3106
3107 map = isl_map_cow(map);
3108 if (!map)
3109 return NULL((void*)0);
3110
3111 for (i = 0; i < map->n; ++i) {
3112 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3113 if (!map->p[i])
3114 goto error;
3115 }
3116 return map;
3117error:
3118 isl_map_free(map);
3119 return NULL((void*)0);
3120}
3121
3122__isl_give isl_setisl_map *isl_set_remove_unknown_divs(__isl_take isl_setisl_map *set)
3123{
3124 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3125}
3126
3127__isl_give isl_basic_setisl_basic_map *isl_basic_set_remove_dims(
3128 __isl_take isl_basic_setisl_basic_map *bset,
3129 enum isl_dim_type type, unsigned first, unsigned n)
3130{
3131 isl_basic_map *bmap = bset_to_bmap(bset);
3132 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3133 return bset_from_bmap(bmap);
3134}
3135
3136__isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3137 enum isl_dim_type type, unsigned first, unsigned n)
3138{
3139 int i;
3140
3141 if (n == 0)
3142 return map;
3143
3144 map = isl_map_cow(map);
3145 if (!map)
3146 return NULL((void*)0);
3147 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3147); goto error; } while (0); } while (0)
;
3148
3149 for (i = 0; i < map->n; ++i) {
3150 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3151 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3152 if (!map->p[i])
3153 goto error;
3154 }
3155 map = isl_map_drop(map, type, first, n);
3156 return map;
3157error:
3158 isl_map_free(map);
3159 return NULL((void*)0);
3160}
3161
3162__isl_give isl_setisl_map *isl_set_remove_dims(__isl_take isl_setisl_map *bset,
3163 enum isl_dim_type type, unsigned first, unsigned n)
3164{
3165 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3166 type, first, n));
3167}
3168
3169/* Project out n inputs starting at first using Fourier-Motzkin */
3170struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3171 unsigned first, unsigned n)
3172{
3173 return isl_map_remove_dims(map, isl_dim_in, first, n);
3174}
3175
3176static void dump_term(struct isl_basic_map *bmap,
3177 isl_int c, int pos, FILE *out)
3178{
3179 const char *name;
3180 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3181 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3182 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3183 if (!pos)
3184 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3185 else {
3186 if (!isl_int_is_one(c)(isl_sioimath_cmp_si(*(c), 1) == 0))
3187 isl_int_print(out, c, 0)isl_sioimath_print(out, *(c), 0);
3188 if (pos < 1 + nparam) {
3189 name = isl_space_get_dim_name(bmap->dim,
3190 isl_dim_param, pos - 1);
3191 if (name)
3192 fprintf(out, "%s", name);
3193 else
3194 fprintf(out, "p%d", pos - 1);
3195 } else if (pos < 1 + nparam + in)
3196 fprintf(out, "i%d", pos - 1 - nparam);
3197 else if (pos < 1 + nparam + dim)
3198 fprintf(out, "o%d", pos - 1 - nparam - in);
3199 else
3200 fprintf(out, "e%d", pos - 1 - nparam - dim);
3201 }
3202}
3203
3204static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3205 int sign, FILE *out)
3206{
3207 int i;
3208 int first;
3209 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3210 isl_int v;
3211
3212 isl_int_init(v)isl_sioimath_init((v));
3213 for (i = 0, first = 1; i < len; ++i) {
3214 if (isl_int_sgn(c[i])isl_sioimath_sgn(*(c[i])) * sign <= 0)
3215 continue;
3216 if (!first)
3217 fprintf(out, " + ");
3218 first = 0;
3219 isl_int_abs(v, c[i])isl_sioimath_abs((v), *(c[i]));
3220 dump_term(bmap, v, i, out);
3221 }
3222 isl_int_clear(v)isl_sioimath_clear((v));
3223 if (first)
3224 fprintf(out, "0");
3225}
3226
3227static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3228 const char *op, FILE *out, int indent)
3229{
3230 int i;
3231
3232 fprintf(out, "%*s", indent, "");
3233
3234 dump_constraint_sign(bmap, c, 1, out);
3235 fprintf(out, " %s ", op);
3236 dump_constraint_sign(bmap, c, -1, out);
3237
3238 fprintf(out, "\n");
3239
3240 for (i = bmap->n_div; i < bmap->extra; ++i) {
3241 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i])(isl_sioimath_sgn(*(c[1+isl_space_dim(bmap->dim, isl_dim_all
)+i])) == 0)
)
3242 continue;
3243 fprintf(out, "%*s", indent, "");
3244 fprintf(out, "ERROR: unused div coefficient not zero\n");
3245 abort();
3246 }
3247}
3248
3249static void dump_constraints(struct isl_basic_map *bmap,
3250 isl_int **c, unsigned n,
3251 const char *op, FILE *out, int indent)
3252{
3253 int i;
3254
3255 for (i = 0; i < n; ++i)
3256 dump_constraint(bmap, c[i], op, out, indent);
3257}
3258
3259static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3260{
3261 int j;
3262 int first = 1;
3263 unsigned total = isl_basic_map_total_dim(bmap);
3264
3265 for (j = 0; j < 1 + total; ++j) {
3266 if (isl_int_is_zero(exp[j])(isl_sioimath_sgn(*(exp[j])) == 0))
3267 continue;
3268 if (!first && isl_int_is_pos(exp[j])(isl_sioimath_sgn(*(exp[j])) > 0))
3269 fprintf(out, "+");
3270 dump_term(bmap, exp[j], j, out);
3271 first = 0;
3272 }
3273}
3274
3275static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3276{
3277 int i;
3278
3279 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3280 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3281
3282 for (i = 0; i < bmap->n_div; ++i) {
3283 fprintf(out, "%*s", indent, "");
3284 fprintf(out, "e%d = [(", i);
3285 dump_affine(bmap, bmap->div[i]+1, out);
3286 fprintf(out, ")/");
3287 isl_int_print(out, bmap->div[i][0], 0)isl_sioimath_print(out, *(bmap->div[i][0]), 0);
3288 fprintf(out, "]\n");
3289 }
3290}
3291
3292void isl_basic_set_print_internal(struct isl_basic_setisl_basic_map *bset,
3293 FILE *out, int indent)
3294{
3295 if (!bset) {
3296 fprintf(out, "null basic set\n");
3297 return;
3298 }
3299
3300 fprintf(out, "%*s", indent, "");
3301 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3302 bset->ref, bset->dim->nparam, bset->dim->n_out,
3303 bset->extra, bset->flags);
3304 dump(bset_to_bmap(bset), out, indent);
3305}
3306
3307void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3308 FILE *out, int indent)
3309{
3310 if (!bmap) {
3311 fprintf(out, "null basic map\n");
3312 return;
3313 }
3314
3315 fprintf(out, "%*s", indent, "");
3316 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3317 "flags: %x, n_name: %d\n",
3318 bmap->ref,
3319 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3320 bmap->extra, bmap->flags, bmap->dim->n_id);
3321 dump(bmap, out, indent);
3322}
3323
3324int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3325{
3326 unsigned total;
3327 if (!bmap)
3328 return -1;
3329 total = isl_basic_map_total_dim(bmap);
3330 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1)do { if (pos < bmap->n_ineq) break; do { isl_handle_error
(bmap->ctx, isl_error_unknown, "Assertion \"" "pos < bmap->n_ineq"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3330); return -1; } while (0); } while (0)
;
3331 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3332 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1)isl_sioimath_sub_ui((bmap->ineq[pos][0]), *(bmap->ineq[
pos][0]), 1)
;
3333 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
3334 return 0;
3335}
3336
3337__isl_give isl_setisl_map *isl_set_alloc_space(__isl_take isl_space *space, int n,
3338 unsigned flags)
3339{
3340 if (!space)
3341 return NULL((void*)0);
3342 if (isl_space_dim(space, isl_dim_in) != 0)
3343 isl_die(isl_space_get_ctx(space), isl_error_invalid,do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "set cannot have input dimensions", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3344); goto error; } while (0)
3344 "set cannot have input dimensions", goto error)do { isl_handle_error(isl_space_get_ctx(space), isl_error_invalid
, "set cannot have input dimensions", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3344); goto error; } while (0)
;
3345 return isl_map_alloc_space(space, n, flags);
3346error:
3347 isl_space_free(space);
3348 return NULL((void*)0);
3349}
3350
3351/* Make sure "map" has room for at least "n" more basic maps.
3352 */
3353__isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3354{
3355 int i;
3356 struct isl_map *grown = NULL((void*)0);
3357
3358 if (!map)
3359 return NULL((void*)0);
3360 isl_assert(map->ctx, n >= 0, goto error)do { if (n >= 0) break; do { isl_handle_error(map->ctx,
isl_error_unknown, "Assertion \"" "n >= 0" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3360); goto error; } while (0); } while (0)
;
3361 if (map->n + n <= map->size)
3362 return map;
3363 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3364 if (!grown)
3365 goto error;
3366 for (i = 0; i < map->n; ++i) {
3367 grown->p[i] = isl_basic_map_copy(map->p[i]);
3368 if (!grown->p[i])
3369 goto error;
3370 grown->n++;
3371 }
3372 isl_map_free(map);
3373 return grown;
3374error:
3375 isl_map_free(grown);
3376 isl_map_free(map);
3377 return NULL((void*)0);
3378}
3379
3380/* Make sure "set" has room for at least "n" more basic sets.
3381 */
3382struct isl_setisl_map *isl_set_grow(struct isl_setisl_map *set, int n)
3383{
3384 return set_from_map(isl_map_grow(set_to_map(set), n));
3385}
3386
3387__isl_give isl_setisl_map *isl_set_from_basic_set(__isl_take isl_basic_setisl_basic_map *bset)
3388{
3389 return isl_map_from_basic_map(bset);
3390}
3391
3392__isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3393{
3394 struct isl_map *map;
3395
3396 if (!bmap)
3397 return NULL((void*)0);
3398
3399 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT(1 << 0));
3400 return isl_map_add_basic_map(map, bmap);
3401}
3402
3403__isl_give isl_setisl_map *isl_set_add_basic_set(__isl_take isl_setisl_map *set,
3404 __isl_take isl_basic_setisl_basic_map *bset)
3405{
3406 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3407 bset_to_bmap(bset)));
3408}
3409
3410__isl_null isl_setisl_map *isl_set_free(__isl_take isl_setisl_map *set)
3411{
3412 return isl_map_free(set);
3413}
3414
3415void isl_set_print_internal(struct isl_setisl_map *set, FILE *out, int indent)
3416{
3417 int i;
3418
3419 if (!set) {
3420 fprintf(out, "null set\n");
3421 return;
3422 }
3423
3424 fprintf(out, "%*s", indent, "");
3425 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3426 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3427 set->flags);
3428 for (i = 0; i < set->n; ++i) {
3429 fprintf(out, "%*s", indent, "");
3430 fprintf(out, "basic set %d:\n", i);
3431 isl_basic_set_print_internal(set->p[i], out, indent+4);
3432 }
3433}
3434
3435void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3436{
3437 int i;
3438
3439 if (!map) {
3440 fprintf(out, "null map\n");
3441 return;
3442 }
3443
3444 fprintf(out, "%*s", indent, "");
3445 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3446 "flags: %x, n_name: %d\n",
3447 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3448 map->dim->n_out, map->flags, map->dim->n_id);
3449 for (i = 0; i < map->n; ++i) {
3450 fprintf(out, "%*s", indent, "");
3451 fprintf(out, "basic map %d:\n", i);
3452 isl_basic_map_print_internal(map->p[i], out, indent+4);
3453 }
3454}
3455
3456__isl_give isl_basic_map *isl_basic_map_intersect_domain(
3457 __isl_take isl_basic_map *bmap, __isl_take isl_basic_setisl_basic_map *bset)
3458{
3459 struct isl_basic_map *bmap_domain;
3460
3461 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
26
Taking true branch
3462 goto error;
27
Control jumps to line 3479
3463
3464 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3465 isl_assert(bset->ctx,do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3466); goto error; } while (0); } while (0)
3466 isl_basic_map_compatible_domain(bmap, bset), goto error)do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3466); goto error; } while (0); } while (0)
;
3467
3468 bmap = isl_basic_map_cow(bmap);
3469 if (!bmap)
3470 goto error;
3471 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3472 bset->n_div, bset->n_eq, bset->n_ineq);
3473 bmap_domain = isl_basic_map_from_domain(bset);
3474 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3475
3476 bmap = isl_basic_map_simplify(bmap);
3477 return isl_basic_map_finalize(bmap);
3478error:
3479 isl_basic_map_free(bmap);
28
Calling 'isl_basic_map_free'
33
Returning; memory was released via 1st parameter
3480 isl_basic_set_free(bset);
3481 return NULL((void*)0);
3482}
3483
3484/* Check that the space of "bset" is the same as that of the range of "bmap".
3485 */
3486static isl_stat isl_basic_map_check_compatible_range(
3487 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_setisl_basic_map *bset)
3488{
3489 isl_bool ok;
3490
3491 ok = isl_basic_map_compatible_range(bmap, bset);
3492 if (ok < 0)
3493 return isl_stat_error;
3494 if (!ok)
3495 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "incompatible spaces", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3496); return isl_stat_error; } while (0)
3496 "incompatible spaces", return isl_stat_error)do { isl_handle_error(isl_basic_set_get_ctx(bset), isl_error_invalid
, "incompatible spaces", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3496); return isl_stat_error; } while (0)
;
3497
3498 return isl_stat_ok;
3499}
3500
3501__isl_give isl_basic_map *isl_basic_map_intersect_range(
3502 __isl_take isl_basic_map *bmap, __isl_take isl_basic_setisl_basic_map *bset)
3503{
3504 struct isl_basic_map *bmap_range;
3505
3506 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3507 goto error;
3508
3509 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3510 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3511 goto error;
3512
3513 if (isl_basic_set_plain_is_universe(bset)) {
3514 isl_basic_set_free(bset);
3515 return bmap;
3516 }
3517
3518 bmap = isl_basic_map_cow(bmap);
3519 if (!bmap)
3520 goto error;
3521 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3522 bset->n_div, bset->n_eq, bset->n_ineq);
3523 bmap_range = bset_to_bmap(bset);
3524 bmap = add_constraints(bmap, bmap_range, 0, 0);
3525
3526 bmap = isl_basic_map_simplify(bmap);
3527 return isl_basic_map_finalize(bmap);
3528error:
3529 isl_basic_map_free(bmap);
3530 isl_basic_set_free(bset);
3531 return NULL((void*)0);
3532}
3533
3534isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3535 __isl_keep isl_vec *vec)
3536{
3537 int i;
3538 unsigned total;
3539 isl_int s;
3540
3541 if (!bmap || !vec)
3542 return isl_bool_error;
3543
3544 total = 1 + isl_basic_map_total_dim(bmap);
3545 if (total != vec->size)
3546 return isl_bool_false;
3547
3548 isl_int_init(s)isl_sioimath_init((s));
3549
3550 for (i = 0; i < bmap->n_eq; ++i) {
3551 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3552 if (!isl_int_is_zero(s)(isl_sioimath_sgn(*(s)) == 0)) {
3553 isl_int_clear(s)isl_sioimath_clear((s));
3554 return isl_bool_false;
3555 }
3556 }
3557
3558 for (i = 0; i < bmap->n_ineq; ++i) {
3559 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3560 if (isl_int_is_neg(s)(isl_sioimath_sgn(*(s)) < 0)) {
3561 isl_int_clear(s)isl_sioimath_clear((s));
3562 return isl_bool_false;
3563 }
3564 }
3565
3566 isl_int_clear(s)isl_sioimath_clear((s));
3567
3568 return isl_bool_true;
3569}
3570
3571isl_bool isl_basic_set_contains(__isl_keep isl_basic_setisl_basic_map *bset,
3572 __isl_keep isl_vec *vec)
3573{
3574 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3575}
3576
3577__isl_give isl_basic_map *isl_basic_map_intersect(
3578 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3579{
3580 struct isl_vec *sample = NULL((void*)0);
3581
3582 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3583 goto error;
3584 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3585 isl_space_dim(bmap1->dim, isl_dim_param) &&
3586 isl_space_dim(bmap2->dim, isl_dim_all) !=
3587 isl_space_dim(bmap2->dim, isl_dim_param))
3588 return isl_basic_map_intersect(bmap2, bmap1);
3589
3590 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3591 isl_space_dim(bmap2->dim, isl_dim_param))
3592 isl_assert(bmap1->ctx,do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3593); goto error; } while (0); } while (0)
3593 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error)do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3593); goto error; } while (0); } while (0)
;
3594
3595 if (isl_basic_map_plain_is_empty(bmap1)) {
3596 isl_basic_map_free(bmap2);
3597 return bmap1;
3598 }
3599 if (isl_basic_map_plain_is_empty(bmap2)) {
3600 isl_basic_map_free(bmap1);
3601 return bmap2;
3602 }
3603
3604 if (bmap1->sample &&
3605 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3606 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3607 sample = isl_vec_copy(bmap1->sample);
3608 else if (bmap2->sample &&
3609 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3610 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3611 sample = isl_vec_copy(bmap2->sample);
3612
3613 bmap1 = isl_basic_map_cow(bmap1);
3614 if (!bmap1)
3615 goto error;
3616 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3617 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3618 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3619
3620 if (!bmap1)
3621 isl_vec_free(sample);
3622 else if (sample) {
3623 isl_vec_free(bmap1->sample);
3624 bmap1->sample = sample;
3625 }
3626
3627 bmap1 = isl_basic_map_simplify(bmap1);
3628 return isl_basic_map_finalize(bmap1);
3629error:
3630 if (sample)
3631 isl_vec_free(sample);
3632 isl_basic_map_free(bmap1);
3633 isl_basic_map_free(bmap2);
3634 return NULL((void*)0);
3635}
3636
3637struct isl_basic_setisl_basic_map *isl_basic_set_intersect(
3638 struct isl_basic_setisl_basic_map *bset1, struct isl_basic_setisl_basic_map *bset2)
3639{
3640 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3641 bset_to_bmap(bset2)));
3642}
3643
3644__isl_give isl_basic_setisl_basic_map *isl_basic_set_intersect_params(
3645 __isl_take isl_basic_setisl_basic_map *bset1, __isl_take isl_basic_setisl_basic_map *bset2)
3646{
3647 return isl_basic_set_intersect(bset1, bset2);
3648}
3649
3650/* Special case of isl_map_intersect, where both map1 and map2
3651 * are convex, without any divs and such that either map1 or map2
3652 * contains a single constraint. This constraint is then simply
3653 * added to the other map.
3654 */
3655static __isl_give isl_map *map_intersect_add_constraint(
3656 __isl_take isl_map *map1, __isl_take isl_map *map2)
3657{
3658 isl_assert(map1->ctx, map1->n == 1, goto error)do { if (map1->n == 1) break; do { isl_handle_error(map1->
ctx, isl_error_unknown, "Assertion \"" "map1->n == 1" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3658); goto error; } while (0); } while (0)
;
3659 isl_assert(map2->ctx, map1->n == 1, goto error)do { if (map1->n == 1) break; do { isl_handle_error(map2->
ctx, isl_error_unknown, "Assertion \"" "map1->n == 1" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3659); goto error; } while (0); } while (0)
;
3660 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error)do { if (map1->p[0]->n_div == 0) break; do { isl_handle_error
(map1->ctx, isl_error_unknown, "Assertion \"" "map1->p[0]->n_div == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3660); goto error; } while (0); } while (0)
;
3661 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error)do { if (map1->p[0]->n_div == 0) break; do { isl_handle_error
(map2->ctx, isl_error_unknown, "Assertion \"" "map1->p[0]->n_div == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3661); goto error; } while (0); } while (0)
;
3662
3663 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3664 return isl_map_intersect(map2, map1);
3665
3666 map1 = isl_map_cow(map1);
3667 if (!map1)
3668 goto error;
3669 if (isl_map_plain_is_empty(map1)) {
3670 isl_map_free(map2);
3671 return map1;
3672 }
3673 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3674 if (map2->p[0]->n_eq == 1)
3675 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3676 else
3677 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3678 map2->p[0]->ineq[0]);
3679
3680 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3681 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3682 if (!map1->p[0])
3683 goto error;
3684
3685 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3686 isl_basic_map_free(map1->p[0]);
3687 map1->n = 0;
3688 }
3689
3690 isl_map_free(map2);
3691
3692 return map1;
3693error:
3694 isl_map_free(map1);
3695 isl_map_free(map2);
3696 return NULL((void*)0);
3697}
3698
3699/* map2 may be either a parameter domain or a map living in the same
3700 * space as map1.
3701 */
3702static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3703 __isl_take isl_map *map2)
3704{
3705 unsigned flags = 0;
3706 isl_map *result;
3707 int i, j;
3708
3709 if (!map1 || !map2)
3710 goto error;
3711
3712 if ((isl_map_plain_is_empty(map1) ||
3713 isl_map_plain_is_universe(map2)) &&
3714 isl_space_is_equal(map1->dim, map2->dim)) {
3715 isl_map_free(map2);
3716 return map1;
3717 }
3718 if ((isl_map_plain_is_empty(map2) ||
3719 isl_map_plain_is_universe(map1)) &&
3720 isl_space_is_equal(map1->dim, map2->dim)) {
3721 isl_map_free(map1);
3722 return map2;
3723 }
3724
3725 if (map1->n == 1 && map2->n == 1 &&
3726 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3727 isl_space_is_equal(map1->dim, map2->dim) &&
3728 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3729 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3730 return map_intersect_add_constraint(map1, map2);
3731
3732 if (isl_space_dim(map2->dim, isl_dim_all) !=
3733 isl_space_dim(map2->dim, isl_dim_param))
3734 isl_assert(map1->ctx,do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3735); goto error; } while (0); } while (0)
3735 isl_space_is_equal(map1->dim, map2->dim), goto error)do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3735); goto error; } while (0); } while (0)
;
3736
3737 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT)(!!(((map1)->flags) & ((1 << 0)))) &&
3738 ISL_F_ISSET(map2, ISL_MAP_DISJOINT)(!!(((map2)->flags) & ((1 << 0)))))
3739 ISL_FL_SET(flags, ISL_MAP_DISJOINT)((flags) |= ((1 << 0)));
3740
3741 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3742 map1->n * map2->n, flags);
3743 if (!result)
3744 goto error;
3745 for (i = 0; i < map1->n; ++i)
3746 for (j = 0; j < map2->n; ++j) {
3747 struct isl_basic_map *part;
3748 part = isl_basic_map_intersect(
3749 isl_basic_map_copy(map1->p[i]),
3750 isl_basic_map_copy(map2->p[j]));
3751 if (isl_basic_map_is_empty(part) < 0)
3752 part = isl_basic_map_free(part);
3753 result = isl_map_add_basic_map(result, part);
3754 if (!result)
3755 goto error;
3756 }
3757 isl_map_free(map1);
3758 isl_map_free(map2);
3759 return result;
3760error:
3761 isl_map_free(map1);
3762 isl_map_free(map2);
3763 return NULL((void*)0);
3764}
3765
3766static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3767 __isl_take isl_map *map2)
3768{
3769 if (!map1 || !map2)
3770 goto error;
3771 if (!isl_space_is_equal(map1->dim, map2->dim))
3772 isl_die(isl_map_get_ctx(map1), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3773); goto error; } while (0)
3773 "spaces don't match", goto error)do { isl_handle_error(isl_map_get_ctx(map1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3773); goto error; } while (0)
;
3774 return map_intersect_internal(map1, map2);
3775error:
3776 isl_map_free(map1);
3777 isl_map_free(map2);
3778 return NULL((void*)0);
3779}
3780
3781__isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3782 __isl_take isl_map *map2)
3783{
3784 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3785}
3786
3787struct isl_setisl_map *isl_set_intersect(struct isl_setisl_map *set1, struct isl_setisl_map *set2)
3788{
3789 return set_from_map(isl_map_intersect(set_to_map(set1),
3790 set_to_map(set2)));
3791}
3792
3793/* map_intersect_internal accepts intersections
3794 * with parameter domains, so we can just call that function.
3795 */
3796static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3797 __isl_take isl_setisl_map *params)
3798{
3799 return map_intersect_internal(map, params);
3800}
3801
3802__isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3803 __isl_take isl_map *map2)
3804{
3805 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3806}
3807
3808__isl_give isl_setisl_map *isl_set_intersect_params(__isl_take isl_setisl_map *set,
3809 __isl_take isl_setisl_map *params)
3810{
3811 return isl_map_intersect_params(set, params);
3812}
3813
3814__isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3815{
3816 isl_space *space;
3817 unsigned pos, n1, n2;
3818
3819 if (!bmap)
3820 return NULL((void*)0);
3821 bmap = isl_basic_map_cow(bmap);
3822 if (!bmap)
3823 return NULL((void*)0);
3824 space = isl_space_reverse(isl_space_copy(bmap->dim));
3825 pos = isl_basic_map_offset(bmap, isl_dim_in);
3826 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3827 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3828 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3829 return isl_basic_map_reset_space(bmap, space);
3830}
3831
3832static __isl_give isl_basic_map *basic_map_space_reset(
3833 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3834{
3835 isl_space *space;
3836
3837 if (!bmap)
3838 return NULL((void*)0);
3839 if (!isl_space_is_named_or_nested(bmap->dim, type))
3840 return bmap;
3841
3842 space = isl_basic_map_get_space(bmap);
3843 space = isl_space_reset(space, type);
3844 bmap = isl_basic_map_reset_space(bmap, space);
3845 return bmap;
3846}
3847
3848__isl_give isl_basic_map *isl_basic_map_insert_dims(
3849 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3850 unsigned pos, unsigned n)
3851{
3852 isl_bool rational;
3853 isl_space *res_dim;
3854 struct isl_basic_map *res;
3855 struct isl_dim_map *dim_map;
3856 unsigned total, off;
3857 enum isl_dim_type t;
3858
3859 if (n == 0)
3860 return basic_map_space_reset(bmap, type);
3861
3862 if (!bmap)
3863 return NULL((void*)0);
3864
3865 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3866
3867 total = isl_basic_map_total_dim(bmap) + n;
3868 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3869 off = 0;
3870 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3871 if (t != type) {
3872 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3873 } else {
3874 unsigned size = isl_basic_map_dim(bmap, t);
3875 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3876 0, pos, off);
3877 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3878 pos, size - pos, off + pos + n);
3879 }
3880 off += isl_space_dim(res_dim, t);
3881 }
3882 isl_dim_map_div(dim_map, bmap, off);
3883
3884 res = isl_basic_map_alloc_space(res_dim,
3885 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3886 rational = isl_basic_map_is_rational(bmap);
3887 if (rational < 0)
3888 res = isl_basic_map_free(res);
3889 if (rational)
3890 res = isl_basic_map_set_rational(res);
3891 if (isl_basic_map_plain_is_empty(bmap)) {
3892 isl_basic_map_free(bmap);
3893 free(dim_map);
3894 return isl_basic_map_set_to_empty(res);
3895 }
3896 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3897 return isl_basic_map_finalize(res);
3898}
3899
3900__isl_give isl_basic_setisl_basic_map *isl_basic_set_insert_dims(
3901 __isl_take isl_basic_setisl_basic_map *bset,
3902 enum isl_dim_type type, unsigned pos, unsigned n)
3903{
3904 return isl_basic_map_insert_dims(bset, type, pos, n);
3905}
3906
3907__isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3908 enum isl_dim_type type, unsigned n)
3909{
3910 if (!bmap)
3911 return NULL((void*)0);
3912 return isl_basic_map_insert_dims(bmap, type,
3913 isl_basic_map_dim(bmap, type), n);
3914}
3915
3916__isl_give isl_basic_setisl_basic_map *isl_basic_set_add_dims(__isl_take isl_basic_setisl_basic_map *bset,
3917 enum isl_dim_type type, unsigned n)
3918{
3919 if (!bset)
3920 return NULL((void*)0);
3921 isl_assert(bset->ctx, type != isl_dim_in, goto error)do { if (type != isl_dim_in) break; do { isl_handle_error(bset
->ctx, isl_error_unknown, "Assertion \"" "type != isl_dim_in"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3921); goto error; } while (0); } while (0)
;
3922 return isl_basic_map_add_dims(bset, type, n);
3923error:
3924 isl_basic_set_free(bset);
3925 return NULL((void*)0);
3926}
3927
3928static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3929 enum isl_dim_type type)
3930{
3931 isl_space *space;
3932
3933 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3934 return map;
3935
3936 space = isl_map_get_space(map);
3937 space = isl_space_reset(space, type);
3938 map = isl_map_reset_space(map, space);
3939 return map;
3940}
3941
3942__isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3943 enum isl_dim_type type, unsigned pos, unsigned n)
3944{
3945 int i;
3946
3947 if (n == 0)
3948 return map_space_reset(map, type);
3949
3950 map = isl_map_cow(map);
3951 if (!map)
3952 return NULL((void*)0);
3953
3954 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3955 if (!map->dim)
3956 goto error;
3957
3958 for (i = 0; i < map->n; ++i) {
3959 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3960 if (!map->p[i])
3961 goto error;
3962 }
3963
3964 return map;
3965error:
3966 isl_map_free(map);
3967 return NULL((void*)0);
3968}
3969
3970__isl_give isl_setisl_map *isl_set_insert_dims(__isl_take isl_setisl_map *set,
3971 enum isl_dim_type type, unsigned pos, unsigned n)
3972{
3973 return isl_map_insert_dims(set, type, pos, n);
3974}
3975
3976__isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3977 enum isl_dim_type type, unsigned n)
3978{
3979 if (!map)
3980 return NULL((void*)0);
3981 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3982}
3983
3984__isl_give isl_setisl_map *isl_set_add_dims(__isl_take isl_setisl_map *set,
3985 enum isl_dim_type type, unsigned n)
3986{
3987 if (!set)
3988 return NULL((void*)0);
3989 isl_assert(set->ctx, type != isl_dim_in, goto error)do { if (type != isl_dim_in) break; do { isl_handle_error(set
->ctx, isl_error_unknown, "Assertion \"" "type != isl_dim_in"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 3989); goto error; } while (0); } while (0)
;
3990 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3991error:
3992 isl_set_free(set);
3993 return NULL((void*)0);
3994}
3995
3996__isl_give isl_basic_map *isl_basic_map_move_dims(
3997 __isl_take isl_basic_map *bmap,
3998 enum isl_dim_type dst_type, unsigned dst_pos,
3999 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4000{
4001 struct isl_dim_map *dim_map;
4002 struct isl_basic_map *res;
4003 enum isl_dim_type t;
4004 unsigned total, off;
4005
4006 if (!bmap)
4007 return NULL((void*)0);
4008 if (n == 0) {
4009 bmap = isl_basic_map_reset(bmap, src_type);
4010 bmap = isl_basic_map_reset(bmap, dst_type);
4011 return bmap;
4012 }
4013
4014 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4015 return isl_basic_map_free(bmap);
4016
4017 if (dst_type == src_type && dst_pos == src_pos)
4018 return bmap;
4019
4020 isl_assert(bmap->ctx, dst_type != src_type, goto error)do { if (dst_type != src_type) break; do { isl_handle_error(bmap
->ctx, isl_error_unknown, "Assertion \"" "dst_type != src_type"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4020); goto error; } while (0); } while (0)
;
4021
4022 if (pos(bmap->dim, dst_type) + dst_pos ==
4023 pos(bmap->dim, src_type) + src_pos +
4024 ((src_type < dst_type) ? n : 0)) {
4025 bmap = isl_basic_map_cow(bmap);
4026 if (!bmap)
4027 return NULL((void*)0);
4028
4029 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4030 src_type, src_pos, n);
4031 if (!bmap->dim)
4032 goto error;
4033
4034 bmap = isl_basic_map_finalize(bmap);
4035
4036 return bmap;
4037 }
4038
4039 total = isl_basic_map_total_dim(bmap);
4040 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4041
4042 off = 0;
4043 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4044 unsigned size = isl_space_dim(bmap->dim, t);
4045 if (t == dst_type) {
4046 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4047 0, dst_pos, off);
4048 off += dst_pos;
4049 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4050 src_pos, n, off);
4051 off += n;
4052 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4053 dst_pos, size - dst_pos, off);
4054 off += size - dst_pos;
4055 } else if (t == src_type) {
4056 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4057 0, src_pos, off);
4058 off += src_pos;
4059 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4060 src_pos + n, size - src_pos - n, off);
4061 off += size - src_pos - n;
4062 } else {
4063 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4064 off += size;
4065 }
4066 }
4067 isl_dim_map_div(dim_map, bmap, off);
4068
4069 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4070 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4071 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4072 if (!bmap)
4073 goto error;
4074
4075 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4076 src_type, src_pos, n);
4077 if (!bmap->dim)
4078 goto error;
4079
4080 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED)(((bmap)->flags) &= ~((1 << 5)));
4081 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4082 bmap = isl_basic_map_finalize(bmap);
4083
4084 return bmap;
4085error:
4086 isl_basic_map_free(bmap);
4087 return NULL((void*)0);
4088}
4089
4090__isl_give isl_basic_setisl_basic_map *isl_basic_set_move_dims(__isl_take isl_basic_setisl_basic_map *bset,
4091 enum isl_dim_type dst_type, unsigned dst_pos,
4092 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4093{
4094 isl_basic_map *bmap = bset_to_bmap(bset);
4095 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4096 src_type, src_pos, n);
4097 return bset_from_bmap(bmap);
4098}
4099
4100__isl_give isl_setisl_map *isl_set_move_dims(__isl_take isl_setisl_map *set,
4101 enum isl_dim_type dst_type, unsigned dst_pos,
4102 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4103{
4104 if (!set)
4105 return NULL((void*)0);
4106 isl_assert(set->ctx, dst_type != isl_dim_in, goto error)do { if (dst_type != isl_dim_in) break; do { isl_handle_error
(set->ctx, isl_error_unknown, "Assertion \"" "dst_type != isl_dim_in"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4106); goto error; } while (0); } while (0)
;
4107 return set_from_map(isl_map_move_dims(set_to_map(set),
4108 dst_type, dst_pos, src_type, src_pos, n));
4109error:
4110 isl_set_free(set);
4111 return NULL((void*)0);
4112}
4113
4114__isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4115 enum isl_dim_type dst_type, unsigned dst_pos,
4116 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4117{
4118 int i;
4119
4120 if (!map)
4121 return NULL((void*)0);
4122 if (n == 0) {
4123 map = isl_map_reset(map, src_type);
4124 map = isl_map_reset(map, dst_type);
4125 return map;
4126 }
4127
4128 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),do { if (src_pos + n <= isl_map_dim(map, src_type)) break;
do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"src_pos + n <= isl_map_dim(map, src_type)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4129); goto error; } while (0); } while (0)
4129 goto error)do { if (src_pos + n <= isl_map_dim(map, src_type)) break;
do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"src_pos + n <= isl_map_dim(map, src_type)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4129); goto error; } while (0); } while (0)
;
4130
4131 if (dst_type == src_type && dst_pos == src_pos)
4132 return map;
4133
4134 isl_assert(map->ctx, dst_type != src_type, goto error)do { if (dst_type != src_type) break; do { isl_handle_error(map
->ctx, isl_error_unknown, "Assertion \"" "dst_type != src_type"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4134); goto error; } while (0); } while (0)
;
4135
4136 map = isl_map_cow(map);
4137 if (!map)
4138 return NULL((void*)0);
4139
4140 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4141 if (!map->dim)
4142 goto error;
4143
4144 for (i = 0; i < map->n; ++i) {
4145 map->p[i] = isl_basic_map_move_dims(map->p[i],
4146 dst_type, dst_pos,
4147 src_type, src_pos, n);
4148 if (!map->p[i])
4149 goto error;
4150 }
4151
4152 return map;
4153error:
4154 isl_map_free(map);
4155 return NULL((void*)0);
4156}
4157
4158/* Move the specified dimensions to the last columns right before
4159 * the divs. Don't change the dimension specification of bmap.
4160 * That's the responsibility of the caller.
4161 */
4162static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4163 enum isl_dim_type type, unsigned first, unsigned n)
4164{
4165 struct isl_dim_map *dim_map;
4166 struct isl_basic_map *res;
4167 enum isl_dim_type t;
4168 unsigned total, off;
4169
4170 if (!bmap)
4171 return NULL((void*)0);
4172 if (pos(bmap->dim, type) + first + n ==
4173 1 + isl_space_dim(bmap->dim, isl_dim_all))
4174 return bmap;
4175
4176 total = isl_basic_map_total_dim(bmap);
4177 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4178
4179 off = 0;
4180 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4181 unsigned size = isl_space_dim(bmap->dim, t);
4182 if (t == type) {
4183 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4184 0, first, off);
4185 off += first;
4186 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4187 first, n, total - bmap->n_div - n);
4188 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4189 first + n, size - (first + n), off);
4190 off += size - (first + n);
4191 } else {
4192 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4193 off += size;
4194 }
4195 }
4196 isl_dim_map_div(dim_map, bmap, off + n);
4197
4198 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4199 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4200 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4201 return res;
4202}
4203
4204/* Insert "n" rows in the divs of "bmap".
4205 *
4206 * The number of columns is not changed, which means that the last
4207 * dimensions of "bmap" are being reintepreted as the new divs.
4208 * The space of "bmap" is not adjusted, however, which means
4209 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4210 * from the space of "bmap" is the responsibility of the caller.
4211 */
4212static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4213 int n)
4214{
4215 int i;
4216 size_t row_size;
4217 isl_int **new_div;
4218 isl_int *old;
4219
4220 bmap = isl_basic_map_cow(bmap);
4221 if (!bmap)
4222 return NULL((void*)0);
4223
4224 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4225 old = bmap->block2.data;
4226 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4227 (bmap->extra + n) * (1 + row_size));
4228 if (!bmap->block2.data)
4229 return isl_basic_map_free(bmap);
4230 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n)((isl_int * *)isl_malloc_or_die(bmap->ctx, (bmap->extra
+ n)*sizeof(isl_int *)))
;
4231 if (!new_div)
4232 return isl_basic_map_free(bmap);
4233 for (i = 0; i < n; ++i) {
4234 new_div[i] = bmap->block2.data +
4235 (bmap->extra + i) * (1 + row_size);
4236 isl_seq_clr(new_div[i], 1 + row_size);
4237 }
4238 for (i = 0; i < bmap->extra; ++i)
4239 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4240 free(bmap->div);
4241 bmap->div = new_div;
4242 bmap->n_div += n;
4243 bmap->extra += n;
4244
4245 return bmap;
4246}
4247
4248/* Drop constraints from "bmap" that only involve the variables
4249 * of "type" in the range [first, first + n] that are not related
4250 * to any of the variables outside that interval.
4251 * These constraints cannot influence the values for the variables
4252 * outside the interval, except in case they cause "bmap" to be empty.
4253 * Only drop the constraints if "bmap" is known to be non-empty.
4254 */
4255static __isl_give isl_basic_map *drop_irrelevant_constraints(
4256 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4257 unsigned first, unsigned n)
4258{
4259 int i;
4260 int *groups;
4261 unsigned dim, n_div;
4262 isl_bool non_empty;
4263
4264 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4265 if (non_empty < 0)
4266 return isl_basic_map_free(bmap);
4267 if (!non_empty)
4268 return bmap;
4269
4270 dim = isl_basic_map_dim(bmap, isl_dim_all);
4271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4272 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim)((int *)isl_calloc_or_die(isl_basic_map_get_ctx(bmap), dim, sizeof
(int)))
;
4273 if (!groups)
4274 return isl_basic_map_free(bmap);
4275 first += isl_basic_map_offset(bmap, type) - 1;
4276 for (i = 0; i < first; ++i)
4277 groups[i] = -1;
4278 for (i = first + n; i < dim - n_div; ++i)
4279 groups[i] = -1;
4280
4281 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4282
4283 return bmap;
4284}
4285
4286/* Turn the n dimensions of type type, starting at first
4287 * into existentially quantified variables.
4288 *
4289 * If a subset of the projected out variables are unrelated
4290 * to any of the variables that remain, then the constraints
4291 * involving this subset are simply dropped first.
4292 */
4293__isl_give isl_basic_map *isl_basic_map_project_out(
4294 __isl_take isl_basic_map *bmap,
4295 enum isl_dim_type type, unsigned first, unsigned n)
4296{
4297 isl_bool empty;
4298
4299 if (n == 0)
4300 return basic_map_space_reset(bmap, type);
4301 if (type == isl_dim_div)
4302 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4304); return isl_basic_map_free(bmap); } while (0)
4303 "cannot project out existentially quantified variables",do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4304); return isl_basic_map_free(bmap); } while (0)
4304 return isl_basic_map_free(bmap))do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "cannot project out existentially quantified variables", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4304); return isl_basic_map_free(bmap); } while (0)
;
4305
4306 empty = isl_basic_map_plain_is_empty(bmap);
4307 if (empty < 0)
4308 return isl_basic_map_free(bmap);
4309 if (empty)
4310 bmap = isl_basic_map_set_to_empty(bmap);
4311
4312 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4313 if (!bmap)
4314 return NULL((void*)0);
4315
4316 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)(!!(((bmap)->flags) & ((1 << 4)))))
4317 return isl_basic_map_remove_dims(bmap, type, first, n);
4318
4319 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4320 return isl_basic_map_free(bmap);
4321
4322 bmap = move_last(bmap, type, first, n);
4323 bmap = isl_basic_map_cow(bmap);
4324 bmap = insert_div_rows(bmap, n);
4325 if (!bmap)
4326 return NULL((void*)0);
4327
4328 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4329 if (!bmap->dim)
4330 goto error;
4331 bmap = isl_basic_map_simplify(bmap);
4332 bmap = isl_basic_map_drop_redundant_divs(bmap);
4333 return isl_basic_map_finalize(bmap);
4334error:
4335 isl_basic_map_free(bmap);
4336 return NULL((void*)0);
4337}
4338
4339/* Turn the n dimensions of type type, starting at first
4340 * into existentially quantified variables.
4341 */
4342struct isl_basic_setisl_basic_map *isl_basic_set_project_out(struct isl_basic_setisl_basic_map *bset,
4343 enum isl_dim_type type, unsigned first, unsigned n)
4344{
4345 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4346 type, first, n));
4347}
4348
4349/* Turn the n dimensions of type type, starting at first
4350 * into existentially quantified variables.
4351 */
4352__isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4353 enum isl_dim_type type, unsigned first, unsigned n)
4354{
4355 int i;
4356
4357 if (!map)
4358 return NULL((void*)0);
4359
4360 if (n == 0)
4361 return map_space_reset(map, type);
4362
4363 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error)do { if (first + n <= isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "first + n <= isl_map_dim(map, type)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4363); goto error; } while (0); } while (0)
;
4364
4365 map = isl_map_cow(map);
4366 if (!map)
4367 return NULL((void*)0);
4368
4369 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4370 if (!map->dim)
4371 goto error;
4372
4373 for (i = 0; i < map->n; ++i) {
4374 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4375 if (!map->p[i])
4376 goto error;
4377 }
4378
4379 return map;
4380error:
4381 isl_map_free(map);
4382 return NULL((void*)0);
4383}
4384
4385/* Turn the n dimensions of type type, starting at first
4386 * into existentially quantified variables.
4387 */
4388__isl_give isl_setisl_map *isl_set_project_out(__isl_take isl_setisl_map *set,
4389 enum isl_dim_type type, unsigned first, unsigned n)
4390{
4391 return set_from_map(isl_map_project_out(set_to_map(set),
4392 type, first, n));
4393}
4394
4395/* Return a map that projects the elements in "set" onto their
4396 * "n" set dimensions starting at "first".
4397 * "type" should be equal to isl_dim_set.
4398 */
4399__isl_give isl_map *isl_set_project_onto_map(__isl_take isl_setisl_map *set,
4400 enum isl_dim_type type, unsigned first, unsigned n)
4401{
4402 int i;
4403 int dim;
4404 isl_map *map;
4405
4406 if (!set)
4407 return NULL((void*)0);
4408 if (type != isl_dim_set)
4409 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "only set dimensions can be projected out", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4410); goto error; } while (0)
4410 "only set dimensions can be projected out", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "only set dimensions can be projected out", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4410); goto error; } while (0)
;
4411 dim = isl_set_dim(set, isl_dim_set);
4412 if (first + n > dim || first + n < first)
4413 isl_die(isl_set_get_ctx(set), isl_error_invalid,do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4414); goto error; } while (0)
4414 "index out of bounds", goto error)do { isl_handle_error(isl_set_get_ctx(set), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4414); goto error; } while (0)
;
4415
4416 map = isl_map_from_domain(set);
4417 map = isl_map_add_dims(map, isl_dim_out, n);
4418 for (i = 0; i < n; ++i)
4419 map = isl_map_equate(map, isl_dim_in, first + i,
4420 isl_dim_out, i);
4421 return map;
4422error:
4423 isl_set_free(set);
4424 return NULL((void*)0);
4425}
4426
4427static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4428{
4429 int i, j;
4430
4431 for (i = 0; i < n; ++i) {
4432 j = isl_basic_map_alloc_div(bmap);
4433 if (j < 0)
4434 goto error;
4435 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4436 }
4437 return bmap;
4438error:
4439 isl_basic_map_free(bmap);
4440 return NULL((void*)0);
4441}
4442
4443struct isl_basic_map *isl_basic_map_apply_range(
4444 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4445{
4446 isl_space *dim_result = NULL((void*)0);
4447 struct isl_basic_map *bmap;
4448 unsigned n_in, n_out, n, nparam, total, pos;
4449 struct isl_dim_map *dim_map1, *dim_map2;
4450
4451 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4452 goto error;
4453 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4454 bmap2->dim, isl_dim_in))
4455 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4456); goto error; } while (0)
4456 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4456); goto error; } while (0)
;
4457
4458 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4459 isl_space_copy(bmap2->dim));
4460
4461 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4462 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4463 n = isl_basic_map_dim(bmap1, isl_dim_out);
4464 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4465
4466 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4467 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4468 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4469 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4470 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4473 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4474 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4475 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4476 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4477
4478 bmap = isl_basic_map_alloc_space(dim_result,
4479 bmap1->n_div + bmap2->n_div + n,
4480 bmap1->n_eq + bmap2->n_eq,
4481 bmap1->n_ineq + bmap2->n_ineq);
4482 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4483 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4484 bmap = add_divs(bmap, n);
4485 bmap = isl_basic_map_simplify(bmap);
4486 bmap = isl_basic_map_drop_redundant_divs(bmap);
4487 return isl_basic_map_finalize(bmap);
4488error:
4489 isl_basic_map_free(bmap1);
4490 isl_basic_map_free(bmap2);
4491 return NULL((void*)0);
4492}
4493
4494struct isl_basic_setisl_basic_map *isl_basic_set_apply(
4495 struct isl_basic_setisl_basic_map *bset, struct isl_basic_map *bmap)
4496{
4497 if (!bset || !bmap)
4498 goto error;
4499
4500 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4501); goto error; } while (0); } while (0)
4501 goto error)do { if (isl_basic_map_compatible_domain(bmap, bset)) break; do
{ isl_handle_error(bset->ctx, isl_error_unknown, "Assertion \""
"isl_basic_map_compatible_domain(bmap, bset)" "\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4501); goto error; } while (0); } while (0)
;
4502
4503 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4504 bmap));
4505error:
4506 isl_basic_set_free(bset);
4507 isl_basic_map_free(bmap);
4508 return NULL((void*)0);
4509}
4510
4511struct isl_basic_map *isl_basic_map_apply_domain(
4512 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4513{
4514 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4515 goto error;
4516 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4517 bmap2->dim, isl_dim_in))
4518 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4519); goto error; } while (0)
4519 "spaces don't match", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap1), isl_error_invalid
, "spaces don't match", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4519); goto error; } while (0)
;
4520
4521 bmap1 = isl_basic_map_reverse(bmap1);
4522 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4523 return isl_basic_map_reverse(bmap1);
4524error:
4525 isl_basic_map_free(bmap1);
4526 isl_basic_map_free(bmap2);
4527 return NULL((void*)0);
4528}
4529
4530/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4531 * A \cap B -> f(A) + f(B)
4532 */
4533__isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4534 __isl_take isl_basic_map *bmap2)
4535{
4536 unsigned n_in, n_out, nparam, total, pos;
4537 struct isl_basic_map *bmap = NULL((void*)0);
4538 struct isl_dim_map *dim_map1, *dim_map2;
4539 int i;
4540
4541 if (!bmap1 || !bmap2)
4542 goto error;
4543
4544 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4545); goto error; } while (0); } while (0)
4545 goto error)do { if (isl_space_is_equal(bmap1->dim, bmap2->dim)) break
; do { isl_handle_error(bmap1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(bmap1->dim, bmap2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4545); goto error; } while (0); } while (0)
;
4546
4547 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4548 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4549 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4550
4551 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4552 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4553 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4554 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4555 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4558 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4559 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4560 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4561 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4562
4563 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4564 bmap1->n_div + bmap2->n_div + 2 * n_out,
4565 bmap1->n_eq + bmap2->n_eq + n_out,
4566 bmap1->n_ineq + bmap2->n_ineq);
4567 for (i = 0; i < n_out; ++i) {
4568 int j = isl_basic_map_alloc_equality(bmap);
4569 if (j < 0)
4570 goto error;
4571 isl_seq_clr(bmap->eq[j], 1+total);
4572 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1)isl_sioimath_set_si((bmap->eq[j][1+nparam+n_in+i]), -1);
4573 isl_int_set_si(bmap->eq[j][1+pos+i], 1)isl_sioimath_set_si((bmap->eq[j][1+pos+i]), 1);
4574 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1)isl_sioimath_set_si((bmap->eq[j][1+pos-n_out+i]), 1);
4575 }
4576 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4577 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4578 bmap = add_divs(bmap, 2 * n_out);
4579
4580 bmap = isl_basic_map_simplify(bmap);
4581 return isl_basic_map_finalize(bmap);
4582error:
4583 isl_basic_map_free(bmap);
4584 isl_basic_map_free(bmap1);
4585 isl_basic_map_free(bmap2);
4586 return NULL((void*)0);
4587}
4588
4589/* Given two maps A -> f(A) and B -> g(B), construct a map
4590 * A \cap B -> f(A) + f(B)
4591 */
4592__isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4593 __isl_take isl_map *map2)
4594{
4595 struct isl_map *result;
4596 int i, j;
4597
4598 if (!map1 || !map2)
4599 goto error;
4600
4601 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error)do { if (isl_space_is_equal(map1->dim, map2->dim)) break
; do { isl_handle_error(map1->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map1->dim, map2->dim)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4601); goto error; } while (0); } while (0)
;
4602
4603 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4604 map1->n * map2->n, 0);
4605 if (!result)
4606 goto error;
4607 for (i = 0; i < map1->n; ++i)
4608 for (j = 0; j < map2->n; ++j) {
4609 struct isl_basic_map *part;
4610 part = isl_basic_map_sum(
4611 isl_basic_map_copy(map1->p[i]),
4612 isl_basic_map_copy(map2->p[j]));
4613 if (isl_basic_map_is_empty(part))
4614 isl_basic_map_free(part);
4615 else
4616 result = isl_map_add_basic_map(result, part);
4617 if (!result)
4618 goto error;
4619 }
4620 isl_map_free(map1);
4621 isl_map_free(map2);
4622 return result;
4623error:
4624 isl_map_free(map1);
4625 isl_map_free(map2);
4626 return NULL((void*)0);
4627}
4628
4629__isl_give isl_setisl_map *isl_set_sum(__isl_take isl_setisl_map *set1,
4630 __isl_take isl_setisl_map *set2)
4631{
4632 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4633}
4634
4635/* Given a basic map A -> f(A), construct A -> -f(A).
4636 */
4637__isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4638{
4639 int i, j;
4640 unsigned off, n;
4641
4642 bmap = isl_basic_map_cow(bmap);
4643 if (!bmap)
4644 return NULL((void*)0);
4645
4646 n = isl_basic_map_dim(bmap, isl_dim_out);
4647 off = isl_basic_map_offset(bmap, isl_dim_out);
4648 for (i = 0; i < bmap->n_eq; ++i)
4649 for (j = 0; j < n; ++j)
4650 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j])isl_sioimath_neg((bmap->eq[i][off+j]), *(bmap->eq[i][off
+j]))
;
4651 for (i = 0; i < bmap->n_ineq; ++i)
4652 for (j = 0; j < n; ++j)
4653 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j])isl_sioimath_neg((bmap->ineq[i][off+j]), *(bmap->ineq[i
][off+j]))
;
4654 for (i = 0; i < bmap->n_div; ++i)
4655 for (j = 0; j < n; ++j)
4656 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j])isl_sioimath_neg((bmap->div[i][1+off+j]), *(bmap->div[i
][1+off+j]))
;
4657 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
4658 return isl_basic_map_finalize(bmap);
4659}
4660
4661__isl_give isl_basic_setisl_basic_map *isl_basic_set_neg(__isl_take isl_basic_setisl_basic_map *bset)
4662{
4663 return isl_basic_map_neg(bset);
4664}
4665
4666/* Given a map A -> f(A), construct A -> -f(A).
4667 */
4668__isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4669{
4670 int i;
4671
4672 map = isl_map_cow(map);
4673 if (!map)
4674 return NULL((void*)0);
4675
4676 for (i = 0; i < map->n; ++i) {
4677 map->p[i] = isl_basic_map_neg(map->p[i]);
4678 if (!map->p[i])
4679 goto error;
4680 }
4681
4682 return map;
4683error:
4684 isl_map_free(map);
4685 return NULL((void*)0);
4686}
4687
4688__isl_give isl_setisl_map *isl_set_neg(__isl_take isl_setisl_map *set)
4689{
4690 return set_from_map(isl_map_neg(set_to_map(set)));
4691}
4692
4693/* Given a basic map A -> f(A) and an integer d, construct a basic map
4694 * A -> floor(f(A)/d).
4695 */
4696__isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4697 isl_int d)
4698{
4699 unsigned n_in, n_out, nparam, total, pos;
4700 struct isl_basic_map *result = NULL((void*)0);
4701 struct isl_dim_map *dim_map;
4702 int i;
4703
4704 if (!bmap)
4705 return NULL((void*)0);
4706
4707 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4708 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4709 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4710
4711 total = nparam + n_in + n_out + bmap->n_div + n_out;
4712 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4713 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4714 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4715 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4716 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4717
4718 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4719 bmap->n_div + n_out,
4720 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4721 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4722 result = add_divs(result, n_out);
4723 for (i = 0; i < n_out; ++i) {
4724 int j;
4725 j = isl_basic_map_alloc_inequality(result);
4726 if (j < 0)
4727 goto error;
4728 isl_seq_clr(result->ineq[j], 1+total);
4729 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_neg((result->ineq[j][1+nparam+n_in+i]), *(d));
4730 isl_int_set_si(result->ineq[j][1+pos+i], 1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), 1);
4731 j = isl_basic_map_alloc_inequality(result);
4732 if (j < 0)
4733 goto error;
4734 isl_seq_clr(result->ineq[j], 1+total);
4735 isl_int_set(result->ineq[j][1+nparam+n_in+i], d)isl_sioimath_set((result->ineq[j][1+nparam+n_in+i]), *(d));
4736 isl_int_set_si(result->ineq[j][1+pos+i], -1)isl_sioimath_set_si((result->ineq[j][1+pos+i]), -1);
4737 isl_int_sub_ui(result->ineq[j][0], d, 1)isl_sioimath_sub_ui((result->ineq[j][0]), *(d), 1);
4738 }
4739
4740 result = isl_basic_map_simplify(result);
4741 return isl_basic_map_finalize(result);
4742error:
4743 isl_basic_map_free(result);
4744 return NULL((void*)0);
4745}
4746
4747/* Given a map A -> f(A) and an integer d, construct a map
4748 * A -> floor(f(A)/d).
4749 */
4750__isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4751{
4752 int i;
4753
4754 map = isl_map_cow(map);
4755 if (!map)
4756 return NULL((void*)0);
4757
4758 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
4759 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
4760 for (i = 0; i < map->n; ++i) {
4761 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4762 if (!map->p[i])
4763 goto error;
4764 }
4765
4766 return map;
4767error:
4768 isl_map_free(map);
4769 return NULL((void*)0);
4770}
4771
4772/* Given a map A -> f(A) and an integer d, construct a map
4773 * A -> floor(f(A)/d).
4774 */
4775__isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4776 __isl_take isl_val *d)
4777{
4778 if (!map || !d)
4779 goto error;
4780 if (!isl_val_is_int(d))
4781 isl_die(isl_val_get_ctx(d), isl_error_invalid,do { isl_handle_error(isl_val_get_ctx(d), isl_error_invalid, "expecting integer denominator"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4782); goto error; } while (0)
4782 "expecting integer denominator", goto error)do { isl_handle_error(isl_val_get_ctx(d), isl_error_invalid, "expecting integer denominator"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 4782); goto error; } while (0)
;
4783 map = isl_map_floordiv(map, d->n);
4784 isl_val_free(d);
4785 return map;
4786error:
4787 isl_map_free(map);
4788 isl_val_free(d);
4789 return NULL((void*)0);
4790}
4791
4792static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4793 unsigned pos)
4794{
4795 int i;
4796 unsigned nparam;
4797 unsigned n_in;
4798
4799 i = isl_basic_map_alloc_equality(bmap);
4800 if (i < 0)
4801 goto error;
4802 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4803 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4804 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4805 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->eq[i][1+nparam+pos]), -1);
4806 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->eq[i][1+nparam+n_in+pos]), 1);
4807 return isl_basic_map_finalize(bmap);
4808error:
4809 isl_basic_map_free(bmap);
4810 return NULL((void*)0);
4811}
4812
4813/* Add a constraint to "bmap" expressing i_pos < o_pos
4814 */
4815static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4816 unsigned pos)
4817{
4818 int i;
4819 unsigned nparam;
4820 unsigned n_in;
4821
4822 i = isl_basic_map_alloc_inequality(bmap);
4823 if (i < 0)
4824 goto error;
4825 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4826 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4827 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4828 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4829 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4830 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), 1);
4831 return isl_basic_map_finalize(bmap);
4832error:
4833 isl_basic_map_free(bmap);
4834 return NULL((void*)0);
4835}
4836
4837/* Add a constraint to "bmap" expressing i_pos <= o_pos
4838 */
4839static __isl_give isl_basic_map *var_less_or_equal(
4840 __isl_take isl_basic_map *bmap, unsigned pos)
4841{
4842 int i;
4843 unsigned nparam;
4844 unsigned n_in;
4845
4846 i = isl_basic_map_alloc_inequality(bmap);
4847 if (i < 0)
4848 goto error;
4849 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4850 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4851 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4852 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), -1);
4853 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), 1);
4854 return isl_basic_map_finalize(bmap);
4855error:
4856 isl_basic_map_free(bmap);
4857 return NULL((void*)0);
4858}
4859
4860/* Add a constraint to "bmap" expressing i_pos > o_pos
4861 */
4862static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4863 unsigned pos)
4864{
4865 int i;
4866 unsigned nparam;
4867 unsigned n_in;
4868
4869 i = isl_basic_map_alloc_inequality(bmap);
4870 if (i < 0)
4871 goto error;
4872 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4873 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4874 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4875 isl_int_set_si(bmap->ineq[i][0], -1)isl_sioimath_set_si((bmap->ineq[i][0]), -1);
4876 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4877 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), -1
)
;
4878 return isl_basic_map_finalize(bmap);
4879error:
4880 isl_basic_map_free(bmap);
4881 return NULL((void*)0);
4882}
4883
4884/* Add a constraint to "bmap" expressing i_pos >= o_pos
4885 */
4886static __isl_give isl_basic_map *var_more_or_equal(
4887 __isl_take isl_basic_map *bmap, unsigned pos)
4888{
4889 int i;
4890 unsigned nparam;
4891 unsigned n_in;
4892
4893 i = isl_basic_map_alloc_inequality(bmap);
4894 if (i < 0)
4895 goto error;
4896 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4897 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4898 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4899 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+pos]), 1);
4900 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1)isl_sioimath_set_si((bmap->ineq[i][1+nparam+n_in+pos]), -1
)
;
4901 return isl_basic_map_finalize(bmap);
4902error:
4903 isl_basic_map_free(bmap);
4904 return NULL((void*)0);
4905}
4906
4907__isl_give isl_basic_map *isl_basic_map_equal(
4908 __isl_take isl_space *dim, unsigned n_equal)
4909{
4910 int i;
4911 struct isl_basic_map *bmap;
4912 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4913 if (!bmap)
4914 return NULL((void*)0);
4915 for (i = 0; i < n_equal && bmap; ++i)
4916 bmap = var_equal(bmap, i);
4917 return isl_basic_map_finalize(bmap);
4918}
4919
4920/* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4921 */
4922__isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4923 unsigned pos)
4924{
4925 int i;
4926 struct isl_basic_map *bmap;
4927 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4928 if (!bmap)
4929 return NULL((void*)0);
4930 for (i = 0; i < pos && bmap; ++i)
4931 bmap = var_equal(bmap, i);
4932 if (bmap)
4933 bmap = var_less(bmap, pos);
4934 return isl_basic_map_finalize(bmap);
4935}
4936
4937/* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4938 */
4939__isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4940 __isl_take isl_space *dim, unsigned pos)
4941{
4942 int i;
4943 isl_basic_map *bmap;
4944
4945 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4946 for (i = 0; i < pos; ++i)
4947 bmap = var_equal(bmap, i);
4948 bmap = var_less_or_equal(bmap, pos);
4949 return isl_basic_map_finalize(bmap);
4950}
4951
4952/* Return a relation on "dim" expressing i_pos > o_pos
4953 */
4954__isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4955 unsigned pos)
4956{
4957 int i;
4958 struct isl_basic_map *bmap;
4959 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4960 if (!bmap)
4961 return NULL((void*)0);
4962 for (i = 0; i < pos && bmap; ++i)
4963 bmap = var_equal(bmap, i);
4964 if (bmap)
4965 bmap = var_more(bmap, pos);
4966 return isl_basic_map_finalize(bmap);
4967}
4968
4969/* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4970 */
4971__isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4972 __isl_take isl_space *dim, unsigned pos)
4973{
4974 int i;
4975 isl_basic_map *bmap;
4976
4977 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4978 for (i = 0; i < pos; ++i)
4979 bmap = var_equal(bmap, i);
4980 bmap = var_more_or_equal(bmap, pos);
4981 return isl_basic_map_finalize(bmap);
4982}
4983
4984static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4985 unsigned n, int equal)
4986{
4987 struct isl_map *map;
4988 int i;
4989
4990 if (n == 0 && equal)
4991 return isl_map_universe(dims);
4992
4993 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
4994
4995 for (i = 0; i + 1 < n; ++i)
4996 map = isl_map_add_basic_map(map,
4997 isl_basic_map_less_at(isl_space_copy(dims), i));
4998 if (n > 0) {
4999 if (equal)
5000 map = isl_map_add_basic_map(map,
5001 isl_basic_map_less_or_equal_at(dims, n - 1));
5002 else
5003 map = isl_map_add_basic_map(map,
5004 isl_basic_map_less_at(dims, n - 1));
5005 } else
5006 isl_space_free(dims);
5007
5008 return map;
5009}
5010
5011static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
5012{
5013 if (!dims)
5014 return NULL((void*)0);
5015 return map_lex_lte_first(dims, dims->n_out, equal);
5016}
5017
5018__isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5019{
5020 return map_lex_lte_first(dim, n, 0);
5021}
5022
5023__isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5024{
5025 return map_lex_lte_first(dim, n, 1);
5026}
5027
5028__isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5029{
5030 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5031}
5032
5033__isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5034{
5035 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5036}
5037
5038static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5039 unsigned n, int equal)
5040{
5041 struct isl_map *map;
5042 int i;
5043
5044 if (n == 0 && equal)
5045 return isl_map_universe(dims);
5046
5047 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT(1 << 0));
5048
5049 for (i = 0; i + 1 < n; ++i)
5050 map = isl_map_add_basic_map(map,
5051 isl_basic_map_more_at(isl_space_copy(dims), i));
5052 if (n > 0) {
5053 if (equal)
5054 map = isl_map_add_basic_map(map,
5055 isl_basic_map_more_or_equal_at(dims, n - 1));
5056 else
5057 map = isl_map_add_basic_map(map,
5058 isl_basic_map_more_at(dims, n - 1));
5059 } else
5060 isl_space_free(dims);
5061
5062 return map;
5063}
5064
5065static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5066{
5067 if (!dims)
5068 return NULL((void*)0);
5069 return map_lex_gte_first(dims, dims->n_out, equal);
5070}
5071
5072__isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5073{
5074 return map_lex_gte_first(dim, n, 0);
5075}
5076
5077__isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5078{
5079 return map_lex_gte_first(dim, n, 1);
5080}
5081
5082__isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5083{
5084 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5085}
5086
5087__isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5088{
5089 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5090}
5091
5092__isl_give isl_map *isl_set_lex_le_set(__isl_take isl_setisl_map *set1,
5093 __isl_take isl_setisl_map *set2)
5094{
5095 isl_map *map;
5096 map = isl_map_lex_le(isl_set_get_space(set1));
5097 map = isl_map_intersect_domain(map, set1);
5098 map = isl_map_intersect_range(map, set2);
5099 return map;
5100}
5101
5102__isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_setisl_map *set1,
5103 __isl_take isl_setisl_map *set2)
5104{
5105 isl_map *map;
5106 map = isl_map_lex_lt(isl_set_get_space(set1));
5107 map = isl_map_intersect_domain(map, set1);
5108 map = isl_map_intersect_range(map, set2);
5109 return map;
5110}
5111
5112__isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_setisl_map *set1,
5113 __isl_take isl_setisl_map *set2)
5114{
5115 isl_map *map;
5116 map = isl_map_lex_ge(isl_set_get_space(set1));
5117 map = isl_map_intersect_domain(map, set1);
5118 map = isl_map_intersect_range(map, set2);
5119 return map;
5120}
5121
5122__isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_setisl_map *set1,
5123 __isl_take isl_setisl_map *set2)
5124{
5125 isl_map *map;
5126 map = isl_map_lex_gt(isl_set_get_space(set1));
5127 map = isl_map_intersect_domain(map, set1);
5128 map = isl_map_intersect_range(map, set2);
5129 return map;
5130}
5131
5132__isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5133 __isl_take isl_map *map2)
5134{
5135 isl_map *map;
5136 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5137 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5138 map = isl_map_apply_range(map, isl_map_reverse(map2));
5139 return map;
5140}
5141
5142__isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5143 __isl_take isl_map *map2)
5144{
5145 isl_map *map;
5146 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5147 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5148 map = isl_map_apply_range(map, isl_map_reverse(map2));
5149 return map;
5150}
5151
5152__isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5153 __isl_take isl_map *map2)
5154{
5155 isl_map *map;
5156 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5157 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5158 map = isl_map_apply_range(map, isl_map_reverse(map2));
5159 return map;
5160}
5161
5162__isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5163 __isl_take isl_map *map2)
5164{
5165 isl_map *map;
5166 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5167 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5168 map = isl_map_apply_range(map, isl_map_reverse(map2));
5169 return map;
5170}
5171
5172/* For a div d = floor(f/m), add the constraint
5173 *
5174 * f - m d >= 0
5175 */
5176static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5177 unsigned pos, isl_int *div)
5178{
5179 int i;
5180 unsigned total = isl_basic_map_total_dim(bmap);
5181
5182 i = isl_basic_map_alloc_inequality(bmap);
5183 if (i < 0)
5184 return isl_stat_error;
5185 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5186 isl_int_neg(bmap->ineq[i][1 + pos], div[0])isl_sioimath_neg((bmap->ineq[i][1 + pos]), *(div[0]));
5187
5188 return isl_stat_ok;
5189}
5190
5191/* For a div d = floor(f/m), add the constraint
5192 *
5193 * -(f-(m-1)) + m d >= 0
5194 */
5195static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5196 unsigned pos, isl_int *div)
5197{
5198 int i;
5199 unsigned total = isl_basic_map_total_dim(bmap);
5200
5201 i = isl_basic_map_alloc_inequality(bmap);
5202 if (i < 0)
5203 return isl_stat_error;
5204 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5205 isl_int_set(bmap->ineq[i][1 + pos], div[0])isl_sioimath_set((bmap->ineq[i][1 + pos]), *(div[0]));
5206 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos])isl_sioimath_add((bmap->ineq[i][0]), *(bmap->ineq[i][0]
), *(bmap->ineq[i][1 + pos]))
;
5207 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1)isl_sioimath_sub_ui((bmap->ineq[i][0]), *(bmap->ineq[i]
[0]), 1)
;
5208
5209 return isl_stat_ok;
5210}
5211
5212/* For a div d = floor(f/m), add the constraints
5213 *
5214 * f - m d >= 0
5215 * -(f-(m-1)) + m d >= 0
5216 *
5217 * Note that the second constraint is the negation of
5218 *
5219 * f - m d >= m
5220 */
5221int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5222 unsigned pos, isl_int *div)
5223{
5224 if (add_upper_div_constraint(bmap, pos, div) < 0)
5225 return -1;
5226 if (add_lower_div_constraint(bmap, pos, div) < 0)
5227 return -1;
5228 return 0;
5229}
5230
5231int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_setisl_basic_map *bset,
5232 unsigned pos, isl_int *div)
5233{
5234 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5235 pos, div);
5236}
5237
5238int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5239{
5240 unsigned total = isl_basic_map_total_dim(bmap);
5241 unsigned div_pos = total - bmap->n_div + div;
5242
5243 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5244 bmap->div[div]);
5245}
5246
5247/* For each known div d = floor(f/m), add the constraints
5248 *
5249 * f - m d >= 0
5250 * -(f-(m-1)) + m d >= 0
5251 *
5252 * Remove duplicate constraints in case of some these div constraints
5253 * already appear in "bmap".
5254 */
5255__isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5256 __isl_take isl_basic_map *bmap)
5257{
5258 unsigned n_div;
5259
5260 if (!bmap)
5261 return NULL((void*)0);
5262 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5263 if (n_div == 0)
5264 return bmap;
5265
5266 bmap = add_known_div_constraints(bmap);
5267 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL((void*)0), 0);
5268 bmap = isl_basic_map_finalize(bmap);
5269 return bmap;
5270}
5271
5272/* Add the div constraint of sign "sign" for div "div" of "bmap".
5273 *
5274 * In particular, if this div is of the form d = floor(f/m),
5275 * then add the constraint
5276 *
5277 * f - m d >= 0
5278 *
5279 * if sign < 0 or the constraint
5280 *
5281 * -(f-(m-1)) + m d >= 0
5282 *
5283 * if sign > 0.
5284 */
5285int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5286 unsigned div, int sign)
5287{
5288 unsigned total;
5289 unsigned div_pos;
5290
5291 if (!bmap)
5292 return -1;
5293
5294 total = isl_basic_map_total_dim(bmap);
5295 div_pos = total - bmap->n_div + div;
5296
5297 if (sign < 0)
5298 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5299 else
5300 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5301}
5302
5303__isl_give isl_basic_setisl_basic_map *isl_basic_map_underlying_set(
5304 __isl_take isl_basic_map *bmap)
5305{
5306 if (!bmap)
5307 goto error;
5308 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5309 bmap->n_div == 0 &&
5310 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5311 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5312 return bset_from_bmap(bmap);
5313 bmap = isl_basic_map_cow(bmap);
5314 if (!bmap)
5315 goto error;
5316 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5317 if (!bmap->dim)
5318 goto error;
5319 bmap->extra -= bmap->n_div;
5320 bmap->n_div = 0;
5321 bmap = isl_basic_map_finalize(bmap);
5322 return bset_from_bmap(bmap);
5323error:
5324 isl_basic_map_free(bmap);
5325 return NULL((void*)0);
5326}
5327
5328__isl_give isl_basic_setisl_basic_map *isl_basic_set_underlying_set(
5329 __isl_take isl_basic_setisl_basic_map *bset)
5330{
5331 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5332}
5333
5334/* Replace each element in "list" by the result of applying
5335 * isl_basic_map_underlying_set to the element.
5336 */
5337__isl_give isl_basic_set_listisl_basic_map_list *isl_basic_map_list_underlying_set(
5338 __isl_take isl_basic_map_list *list)
5339{
5340 int i, n;
5341
5342 if (!list)
5343 return NULL((void*)0);
5344
5345 n = isl_basic_map_list_n_basic_map(list);
5346 for (i = 0; i < n; ++i) {
5347 isl_basic_map *bmap;
5348 isl_basic_setisl_basic_map *bset;
5349
5350 bmap = isl_basic_map_list_get_basic_map(list, i);
5351 bset = isl_basic_set_underlying_set(bmap);
5352 list = isl_basic_set_list_set_basic_set(list, i, bset);
5353 }
5354
5355 return list;
5356}
5357
5358__isl_give isl_basic_map *isl_basic_map_overlying_set(
5359 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_basic_map *like)
5360{
5361 struct isl_basic_map *bmap;
5362 struct isl_ctx *ctx;
5363 unsigned total;
5364 int i;
5365
5366 if (!bset || !like)
5367 goto error;
5368 ctx = bset->ctx;
5369 isl_assert(ctx, bset->n_div == 0, goto error)do { if (bset->n_div == 0) break; do { isl_handle_error(ctx
, isl_error_unknown, "Assertion \"" "bset->n_div == 0" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5369); goto error; } while (0); } while (0)
;
5370 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error)do { if (isl_basic_set_n_param(bset) == 0) break; do { isl_handle_error
(ctx, isl_error_unknown, "Assertion \"" "isl_basic_set_n_param(bset) == 0"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5370); goto error; } while (0); } while (0)
;
5371 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),do { if (bset->dim->n_out == isl_basic_map_total_dim(like
)) break; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"bset->dim->n_out == isl_basic_map_total_dim(like)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5372); goto error; } while (0); } while (0)
5372 goto error)do { if (bset->dim->n_out == isl_basic_map_total_dim(like
)) break; do { isl_handle_error(ctx, isl_error_unknown, "Assertion \""
"bset->dim->n_out == isl_basic_map_total_dim(like)" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5372); goto error; } while (0); } while (0)
;
5373 if (like->n_div == 0) {
5374 isl_space *space = isl_basic_map_get_space(like);
5375 isl_basic_map_free(like);
5376 return isl_basic_map_reset_space(bset, space);
5377 }
5378 bset = isl_basic_set_cow(bset);
5379 if (!bset)
5380 goto error;
5381 total = bset->dim->n_out + bset->extra;
5382 bmap = bset_to_bmap(bset);
5383 isl_space_free(bmap->dim);
5384 bmap->dim = isl_space_copy(like->dim);
5385 if (!bmap->dim)
5386 goto error;
5387 bmap->n_div = like->n_div;
5388 bmap->extra += like->n_div;
5389 if (bmap->extra) {
5390 unsigned ltotal;
5391 isl_int **div;
5392 ltotal = total - bmap->extra + like->extra;
5393 if (ltotal > total)
5394 ltotal = total;
5395 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5396 bmap->extra * (1 + 1 + total));
5397 if (isl_blk_is_error(bmap->block2))
5398 goto error;
5399 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra)((isl_int * *)isl_realloc_or_die(ctx, bmap->div, (bmap->
extra)*sizeof(isl_int *)))
;
5400 if (!div)
5401 goto error;
5402 bmap->div = div;
5403 for (i = 0; i < bmap->extra; ++i)
5404 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5405 for (i = 0; i < like->n_div; ++i) {
5406 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5407 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5408 }
5409 bmap = isl_basic_map_add_known_div_constraints(bmap);
5410 }
5411 isl_basic_map_free(like);
5412 bmap = isl_basic_map_simplify(bmap);
5413 bmap = isl_basic_map_finalize(bmap);
5414 return bmap;
5415error:
5416 isl_basic_map_free(like);
5417 isl_basic_set_free(bset);
5418 return NULL((void*)0);
5419}
5420
5421struct isl_basic_setisl_basic_map *isl_basic_set_from_underlying_set(
5422 struct isl_basic_setisl_basic_map *bset, struct isl_basic_setisl_basic_map *like)
5423{
5424 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5425 bset_to_bmap(like)));
5426}
5427
5428__isl_give isl_setisl_map *isl_map_underlying_set(__isl_take isl_map *map)
5429{
5430 int i;
5431
5432 map = isl_map_cow(map);
5433 if (!map)
5434 return NULL((void*)0);
5435 map->dim = isl_space_cow(map->dim);
5436 if (!map->dim)
5437 goto error;
5438
5439 for (i = 1; i < map->n; ++i)
5440 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,do { if (map->p[0]->n_div == map->p[i]->n_div) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"map->p[0]->n_div == map->p[i]->n_div" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5441); goto error; } while (0); } while (0)
5441 goto error)do { if (map->p[0]->n_div == map->p[i]->n_div) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"map->p[0]->n_div == map->p[i]->n_div" "\" failed"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5441); goto error; } while (0); } while (0)
;
5442 for (i = 0; i < map->n; ++i) {
5443 map->p[i] = bset_to_bmap(
5444 isl_basic_map_underlying_set(map->p[i]));
5445 if (!map->p[i])
5446 goto error;
5447 }
5448 if (map->n == 0)
5449 map->dim = isl_space_underlying(map->dim, 0);
5450 else {
5451 isl_space_free(map->dim);
5452 map->dim = isl_space_copy(map->p[0]->dim);
5453 }
5454 if (!map->dim)
5455 goto error;
5456 return set_from_map(map);
5457error:
5458 isl_map_free(map);
5459 return NULL((void*)0);
5460}
5461
5462/* Replace the space of "bmap" by "space".
5463 *
5464 * If the space of "bmap" is identical to "space" (including the identifiers
5465 * of the input and output dimensions), then simply return the original input.
5466 */
5467__isl_give isl_basic_map *isl_basic_map_reset_space(
5468 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5469{
5470 isl_bool equal;
5471 isl_space *bmap_space;
5472
5473 bmap_space = isl_basic_map_peek_space(bmap);
5474 equal = isl_space_is_equal(bmap_space, space);
5475 if (equal >= 0 && equal)
5476 equal = isl_space_has_equal_ids(bmap_space, space);
5477 if (equal < 0)
5478 goto error;
5479 if (equal) {
5480 isl_space_free(space);
5481 return bmap;
5482 }
5483 bmap = isl_basic_map_cow(bmap);
5484 if (!bmap || !space)
5485 goto error;
5486
5487 isl_space_free(bmap->dim);
5488 bmap->dim = space;
5489
5490 bmap = isl_basic_map_finalize(bmap);
5491
5492 return bmap;
5493error:
5494 isl_basic_map_free(bmap);
5495 isl_space_free(space);
5496 return NULL((void*)0);
5497}
5498
5499__isl_give isl_basic_setisl_basic_map *isl_basic_set_reset_space(
5500 __isl_take isl_basic_setisl_basic_map *bset, __isl_take isl_space *dim)
5501{
5502 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5503 dim));
5504}
5505
5506__isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5507 __isl_take isl_space *dim)
5508{
5509 int i;
5510
5511 map = isl_map_cow(map);
5512 if (!map || !dim)
5513 goto error;
5514
5515 for (i = 0; i < map->n; ++i) {
5516 map->p[i] = isl_basic_map_reset_space(map->p[i],
5517 isl_space_copy(dim));
5518 if (!map->p[i])
5519 goto error;
5520 }
5521 isl_space_free(map->dim);
5522 map->dim = dim;
5523
5524 return map;
5525error:
5526 isl_map_free(map);
5527 isl_space_free(dim);
5528 return NULL((void*)0);
5529}
5530
5531__isl_give isl_setisl_map *isl_set_reset_space(__isl_take isl_setisl_map *set,
5532 __isl_take isl_space *dim)
5533{
5534 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5535}
5536
5537/* Compute the parameter domain of the given basic set.
5538 */
5539__isl_give isl_basic_setisl_basic_map *isl_basic_set_params(__isl_take isl_basic_setisl_basic_map *bset)
5540{
5541 isl_bool is_params;
5542 isl_space *space;
5543 unsigned n;
5544
5545 is_params = isl_basic_set_is_params(bset);
5546 if (is_params < 0)
5547 return isl_basic_set_free(bset);
5548 if (is_params)
5549 return bset;
5550
5551 n = isl_basic_set_dim(bset, isl_dim_set);
5552 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5553 space = isl_basic_set_get_space(bset);
5554 space = isl_space_params(space);
5555 bset = isl_basic_set_reset_space(bset, space);
5556 return bset;
5557}
5558
5559/* Construct a zero-dimensional basic set with the given parameter domain.
5560 */
5561__isl_give isl_basic_setisl_basic_map *isl_basic_set_from_params(
5562 __isl_take isl_basic_setisl_basic_map *bset)
5563{
5564 isl_space *space;
5565 space = isl_basic_set_get_space(bset);
5566 space = isl_space_set_from_params(space);
5567 bset = isl_basic_set_reset_space(bset, space);
5568 return bset;
5569}
5570
5571/* Compute the parameter domain of the given set.
5572 */
5573__isl_give isl_setisl_map *isl_set_params(__isl_take isl_setisl_map *set)
5574{
5575 isl_space *space;
5576 unsigned n;
5577
5578 if (isl_set_is_params(set))
5579 return set;
5580
5581 n = isl_set_dim(set, isl_dim_set);
5582 set = isl_set_project_out(set, isl_dim_set, 0, n);
5583 space = isl_set_get_space(set);
5584 space = isl_space_params(space);
5585 set = isl_set_reset_space(set, space);
5586 return set;
5587}
5588
5589/* Construct a zero-dimensional set with the given parameter domain.
5590 */
5591__isl_give isl_setisl_map *isl_set_from_params(__isl_take isl_setisl_map *set)
5592{
5593 isl_space *space;
5594 space = isl_set_get_space(set);
5595 space = isl_space_set_from_params(space);
5596 set = isl_set_reset_space(set, space);
5597 return set;
5598}
5599
5600/* Compute the parameter domain of the given map.
5601 */
5602__isl_give isl_setisl_map *isl_map_params(__isl_take isl_map *map)
5603{
5604 isl_space *space;
5605 unsigned n;
5606
5607 n = isl_map_dim(map, isl_dim_in);
5608 map = isl_map_project_out(map, isl_dim_in, 0, n);
5609 n = isl_map_dim(map, isl_dim_out);
5610 map = isl_map_project_out(map, isl_dim_out, 0, n);
5611 space = isl_map_get_space(map);
5612 space = isl_space_params(space);
5613 map = isl_map_reset_space(map, space);
5614 return map;
5615}
5616
5617struct isl_basic_setisl_basic_map *isl_basic_map_domain(struct isl_basic_map *bmap)
5618{
5619 isl_space *space;
5620 unsigned n_out;
5621
5622 if (!bmap)
5623 return NULL((void*)0);
5624 space = isl_space_domain(isl_basic_map_get_space(bmap));
5625
5626 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5627 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5628
5629 return isl_basic_map_reset_space(bmap, space);
5630}
5631
5632isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5633{
5634 if (!bmap)
5635 return isl_bool_error;
5636 return isl_space_may_be_set(bmap->dim);
5637}
5638
5639/* Is this basic map actually a set?
5640 * Users should never call this function. Outside of isl,
5641 * the type should indicate whether something is a set or a map.
5642 */
5643isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5644{
5645 if (!bmap)
5646 return isl_bool_error;
5647 return isl_space_is_set(bmap->dim);
5648}
5649
5650struct isl_basic_setisl_basic_map *isl_basic_map_range(struct isl_basic_map *bmap)
5651{
5652 isl_bool is_set;
5653
5654 is_set = isl_basic_map_is_set(bmap);
5655 if (is_set < 0)
5656 goto error;
5657 if (is_set)
5658 return bmap;
5659 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5660error:
5661 isl_basic_map_free(bmap);
5662 return NULL((void*)0);
5663}
5664
5665__isl_give isl_basic_map *isl_basic_map_domain_map(
5666 __isl_take isl_basic_map *bmap)
5667{
5668 int i;
5669 isl_space *dim;
5670 isl_basic_map *domain;
5671 int nparam, n_in, n_out;
5672
5673 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5674 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5675 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5676
5677 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5678 domain = isl_basic_map_universe(dim);
5679
5680 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5681 bmap = isl_basic_map_apply_range(bmap, domain);
5682 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5683
5684 for (i = 0; i < n_in; ++i)
5685 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5686 isl_dim_out, i);
5687
5688 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5689 return isl_basic_map_finalize(bmap);
5690}
5691
5692__isl_give isl_basic_map *isl_basic_map_range_map(
5693 __isl_take isl_basic_map *bmap)
5694{
5695 int i;
5696 isl_space *dim;
5697 isl_basic_map *range;
5698 int nparam, n_in, n_out;
5699
5700 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5701 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5702 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5703
5704 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5705 range = isl_basic_map_universe(dim);
5706
5707 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5708 bmap = isl_basic_map_apply_range(bmap, range);
5709 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5710
5711 for (i = 0; i < n_out; ++i)
5712 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5713 isl_dim_out, i);
5714
5715 bmap = isl_basic_map_gauss(bmap, NULL((void*)0));
5716 return isl_basic_map_finalize(bmap);
5717}
5718
5719int isl_map_may_be_set(__isl_keep isl_map *map)
5720{
5721 if (!map)
5722 return -1;
5723 return isl_space_may_be_set(map->dim);
5724}
5725
5726/* Is this map actually a set?
5727 * Users should never call this function. Outside of isl,
5728 * the type should indicate whether something is a set or a map.
5729 */
5730isl_bool isl_map_is_set(__isl_keep isl_map *map)
5731{
5732 if (!map)
5733 return isl_bool_error;
5734 return isl_space_is_set(map->dim);
5735}
5736
5737__isl_give isl_setisl_map *isl_map_range(__isl_take isl_map *map)
5738{
5739 int i;
5740 isl_bool is_set;
5741 struct isl_setisl_map *set;
5742
5743 is_set = isl_map_is_set(map);
5744 if (is_set < 0)
5745 goto error;
5746 if (is_set)
5747 return set_from_map(map);
5748
5749 map = isl_map_cow(map);
5750 if (!map)
5751 goto error;
5752
5753 set = set_from_map(map);
5754 set->dim = isl_space_range(set->dim);
5755 if (!set->dim)
5756 goto error;
5757 for (i = 0; i < map->n; ++i) {
5758 set->p[i] = isl_basic_map_range(map->p[i]);
5759 if (!set->p[i])
5760 goto error;
5761 }
5762 ISL_F_CLR(set, ISL_MAP_DISJOINT)(((set)->flags) &= ~((1 << 0)));
5763 ISL_F_CLR(set, ISL_SET_NORMALIZED)(((set)->flags) &= ~((1 << 1)));
5764 return set;
5765error:
5766 isl_map_free(map);
5767 return NULL((void*)0);
5768}
5769
5770__isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5771{
5772 int i;
5773
5774 map = isl_map_cow(map);
5775 if (!map)
5776 return NULL((void*)0);
5777
5778 map->dim = isl_space_domain_map(map->dim);
5779 if (!map->dim)
5780 goto error;
5781 for (i = 0; i < map->n; ++i) {
5782 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5783 if (!map->p[i])
5784 goto error;
5785 }
5786 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5787 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5788 return map;
5789error:
5790 isl_map_free(map);
5791 return NULL((void*)0);
5792}
5793
5794__isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5795{
5796 int i;
5797 isl_space *range_dim;
5798
5799 map = isl_map_cow(map);
5800 if (!map)
5801 return NULL((void*)0);
5802
5803 range_dim = isl_space_range(isl_map_get_space(map));
5804 range_dim = isl_space_from_range(range_dim);
5805 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5806 map->dim = isl_space_join(map->dim, range_dim);
5807 if (!map->dim)
5808 goto error;
5809 for (i = 0; i < map->n; ++i) {
5810 map->p[i] = isl_basic_map_range_map(map->p[i]);
5811 if (!map->p[i])
5812 goto error;
5813 }
5814 ISL_F_CLR(map, ISL_MAP_DISJOINT)(((map)->flags) &= ~((1 << 0)));
5815 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
5816 return map;
5817error:
5818 isl_map_free(map);
5819 return NULL((void*)0);
5820}
5821
5822/* Given a wrapped map of the form A[B -> C],
5823 * return the map A[B -> C] -> B.
5824 */
5825__isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_setisl_map *set)
5826{
5827 isl_id *id;
5828 isl_map *map;
5829
5830 if (!set)
5831 return NULL((void*)0);
5832 if (!isl_set_has_tuple_id(set))
5833 return isl_map_domain_map(isl_set_unwrap(set));
5834
5835 id = isl_set_get_tuple_id(set);
5836 map = isl_map_domain_map(isl_set_unwrap(set));
5837 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5838
5839 return map;
5840}
5841
5842__isl_give isl_basic_map *isl_basic_map_from_domain(
5843 __isl_take isl_basic_setisl_basic_map *bset)
5844{
5845 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5846}
5847
5848__isl_give isl_basic_map *isl_basic_map_from_range(
5849 __isl_take isl_basic_setisl_basic_map *bset)
5850{
5851 isl_space *space;
5852 space = isl_basic_set_get_space(bset);
5853 space = isl_space_from_range(space);
5854 bset = isl_basic_set_reset_space(bset, space);
5855 return bset_to_bmap(bset);
5856}
5857
5858/* Create a relation with the given set as range.
5859 * The domain of the created relation is a zero-dimensional
5860 * flat anonymous space.
5861 */
5862__isl_give isl_map *isl_map_from_range(__isl_take isl_setisl_map *set)
5863{
5864 isl_space *space;
5865 space = isl_set_get_space(set);
5866 space = isl_space_from_range(space);
5867 set = isl_set_reset_space(set, space);
5868 return set_to_map(set);
5869}
5870
5871/* Create a relation with the given set as domain.
5872 * The range of the created relation is a zero-dimensional
5873 * flat anonymous space.
5874 */
5875__isl_give isl_map *isl_map_from_domain(__isl_take isl_setisl_map *set)
5876{
5877 return isl_map_reverse(isl_map_from_range(set));
5878}
5879
5880__isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5881 __isl_take isl_basic_setisl_basic_map *domain, __isl_take isl_basic_setisl_basic_map *range)
5882{
5883 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5884}
5885
5886__isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_setisl_map *domain,
5887 __isl_take isl_setisl_map *range)
5888{
5889 return isl_map_apply_range(isl_map_reverse(domain), range);
5890}
5891
5892/* Return a newly allocated isl_map with given space and flags and
5893 * room for "n" basic maps.
5894 * Make sure that all cached information is cleared.
5895 */
5896__isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5897 unsigned flags)
5898{
5899 struct isl_map *map;
5900
5901 if (!space)
5902 return NULL((void*)0);
5903 if (n < 0)
5904 isl_die(space->ctx, isl_error_internal,do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5905); goto error; } while (0)
5905 "negative number of basic maps", goto error)do { isl_handle_error(space->ctx, isl_error_internal, "negative number of basic maps"
, "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 5905); goto error; } while (0)
;
5906 map = isl_calloc(space->ctx, struct isl_map,((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
5907 sizeof(struct isl_map) +((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
5908 (n - 1) * sizeof(struct isl_basic_map *))((struct isl_map *)isl_calloc_or_die(space->ctx, 1, sizeof
(struct isl_map) + (n - 1) * sizeof(struct isl_basic_map *)))
;
5909 if (!map)
5910 goto error;
5911
5912 map->ctx = space->ctx;
5913 isl_ctx_ref(map->ctx);
5914 map->ref = 1;
5915 map->size = n;
5916 map->n = 0;
5917 map->dim = space;
5918 map->flags = flags;
5919 return map;
5920error:
5921 isl_space_free(space);
5922 return NULL((void*)0);
5923}
5924
5925__isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5926{
5927 struct isl_basic_map *bmap;
5928 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5929 bmap = isl_basic_map_set_to_empty(bmap);
5930 return bmap;
5931}
5932
5933__isl_give isl_basic_setisl_basic_map *isl_basic_set_empty(__isl_take isl_space *space)
5934{
5935 struct isl_basic_setisl_basic_map *bset;
5936 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5937 bset = isl_basic_set_set_to_empty(bset);
5938 return bset;
5939}
5940
5941__isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5942{
5943 struct isl_basic_map *bmap;
5944 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5945 bmap = isl_basic_map_finalize(bmap);
5946 return bmap;
5947}
5948
5949__isl_give isl_basic_setisl_basic_map *isl_basic_set_universe(__isl_take isl_space *space)
5950{
5951 struct isl_basic_setisl_basic_map *bset;
5952 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5953 bset = isl_basic_set_finalize(bset);
5954 return bset;
5955}
5956
5957__isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5958{
5959 int i;
5960 unsigned total = isl_space_dim(dim, isl_dim_all);
5961 isl_basic_map *bmap;
5962
5963 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5964 for (i = 0; i < total; ++i) {
5965 int k = isl_basic_map_alloc_inequality(bmap);
5966 if (k < 0)
5967 goto error;
5968 isl_seq_clr(bmap->ineq[k], 1 + total);
5969 isl_int_set_si(bmap->ineq[k][1 + i], 1)isl_sioimath_set_si((bmap->ineq[k][1 + i]), 1);
5970 }
5971 return bmap;
5972error:
5973 isl_basic_map_free(bmap);
5974 return NULL((void*)0);
5975}
5976
5977__isl_give isl_basic_setisl_basic_map *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5978{
5979 return isl_basic_map_nat_universe(dim);
5980}
5981
5982__isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5983{
5984 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5985}
5986
5987__isl_give isl_setisl_map *isl_set_nat_universe(__isl_take isl_space *dim)
5988{
5989 return isl_map_nat_universe(dim);
5990}
5991
5992__isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5993{
5994 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT(1 << 0));
5995}
5996
5997__isl_give isl_setisl_map *isl_set_empty(__isl_take isl_space *space)
5998{
5999 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT(1 << 0));
6000}
6001
6002__isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6003{
6004 struct isl_map *map;
6005 if (!space)
6006 return NULL((void*)0);
6007 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT(1 << 0));
6008 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6009 return map;
6010}
6011
6012__isl_give isl_setisl_map *isl_set_universe(__isl_take isl_space *space)
6013{
6014 struct isl_setisl_map *set;
6015 if (!space)
6016 return NULL((void*)0);
6017 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT(1 << 0));
6018 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6019 return set;
6020}
6021
6022struct isl_map *isl_map_dup(struct isl_map *map)
6023{
6024 int i;
6025 struct isl_map *dup;
6026
6027 if (!map)
6028 return NULL((void*)0);
6029 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6030 for (i = 0; i < map->n; ++i)
6031 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6032 return dup;
6033}
6034
6035__isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6036 __isl_take isl_basic_map *bmap)
6037{
6038 if (!bmap || !map)
6039 goto error;
6040 if (isl_basic_map_plain_is_empty(bmap)) {
6041 isl_basic_map_free(bmap);
6042 return map;
6043 }
6044 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error)do { if (isl_space_is_equal(map->dim, bmap->dim)) break
; do { isl_handle_error(map->ctx, isl_error_unknown, "Assertion \""
"isl_space_is_equal(map->dim, bmap->dim)" "\" failed",
"/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6044); goto error; } while (0); } while (0)
;
6045 isl_assert(map->ctx, map->n < map->size, goto error)do { if (map->n < map->size) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "map->n < map->size"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6045); goto error; } while (0); } while (0)
;
6046 map->p[map->n] = bmap;
6047 map->n++;
6048 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6049 return map;
6050error:
6051 if (map)
6052 isl_map_free(map);
6053 if (bmap)
6054 isl_basic_map_free(bmap);
6055 return NULL((void*)0);
6056}
6057
6058__isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6059{
6060 int i;
6061
6062 if (!map)
6063 return NULL((void*)0);
6064
6065 if (--map->ref > 0)
6066 return NULL((void*)0);
6067
6068 clear_caches(map);
6069 isl_ctx_deref(map->ctx);
6070 for (i = 0; i < map->n; ++i)
6071 isl_basic_map_free(map->p[i]);
6072 isl_space_free(map->dim);
6073 free(map);
6074
6075 return NULL((void*)0);
6076}
6077
6078static struct isl_basic_map *isl_basic_map_fix_pos_si(
6079 struct isl_basic_map *bmap, unsigned pos, int value)
6080{
6081 int j;
6082
6083 bmap = isl_basic_map_cow(bmap);
6084 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6085 j = isl_basic_map_alloc_equality(bmap);
6086 if (j < 0)
6087 goto error;
6088 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6089 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6090 isl_int_set_si(bmap->eq[j][0], value)isl_sioimath_set_si((bmap->eq[j][0]), value);
6091 bmap = isl_basic_map_simplify(bmap);
6092 return isl_basic_map_finalize(bmap);
6093error:
6094 isl_basic_map_free(bmap);
6095 return NULL((void*)0);
6096}
6097
6098static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6099 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6100{
6101 int j;
6102
6103 bmap = isl_basic_map_cow(bmap);
6104 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6105 j = isl_basic_map_alloc_equality(bmap);
6106 if (j < 0)
6107 goto error;
6108 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6109 isl_int_set_si(bmap->eq[j][pos], -1)isl_sioimath_set_si((bmap->eq[j][pos]), -1);
6110 isl_int_set(bmap->eq[j][0], value)isl_sioimath_set((bmap->eq[j][0]), *(value));
6111 bmap = isl_basic_map_simplify(bmap);
6112 return isl_basic_map_finalize(bmap);
6113error:
6114 isl_basic_map_free(bmap);
6115 return NULL((void*)0);
6116}
6117
6118__isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6119 enum isl_dim_type type, unsigned pos, int value)
6120{
6121 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6122 return isl_basic_map_free(bmap);
6123 return isl_basic_map_fix_pos_si(bmap,
6124 isl_basic_map_offset(bmap, type) + pos, value);
6125}
6126
6127__isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6128 enum isl_dim_type type, unsigned pos, isl_int value)
6129{
6130 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6131 return isl_basic_map_free(bmap);
6132 return isl_basic_map_fix_pos(bmap,
6133 isl_basic_map_offset(bmap, type) + pos, value);
6134}
6135
6136/* Fix the value of the variable at position "pos" of type "type" of "bmap"
6137 * to be equal to "v".
6138 */
6139__isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6140 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6141{
6142 if (!bmap || !v)
6143 goto error;
6144 if (!isl_val_is_int(v))
6145 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6146); goto error; } while (0)
6146 "expecting integer value", goto error)do { isl_handle_error(isl_basic_map_get_ctx(bmap), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6146); goto error; } while (0)
;
6147 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6148 goto error;
6149 pos += isl_basic_map_offset(bmap, type);
6150 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6151 isl_val_free(v);
6152 return bmap;
6153error:
6154 isl_basic_map_free(bmap);
6155 isl_val_free(v);
6156 return NULL((void*)0);
6157}
6158
6159/* Fix the value of the variable at position "pos" of type "type" of "bset"
6160 * to be equal to "v".
6161 */
6162__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix_val(__isl_take isl_basic_setisl_basic_map *bset,
6163 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6164{
6165 return isl_basic_map_fix_val(bset, type, pos, v);
6166}
6167
6168struct isl_basic_setisl_basic_map *isl_basic_set_fix_si(struct isl_basic_setisl_basic_map *bset,
6169 enum isl_dim_type type, unsigned pos, int value)
6170{
6171 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6172 type, pos, value));
6173}
6174
6175__isl_give isl_basic_setisl_basic_map *isl_basic_set_fix(__isl_take isl_basic_setisl_basic_map *bset,
6176 enum isl_dim_type type, unsigned pos, isl_int value)
6177{
6178 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6179 type, pos, value));
6180}
6181
6182struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6183 unsigned input, int value)
6184{
6185 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6186}
6187
6188struct isl_basic_setisl_basic_map *isl_basic_set_fix_dim_si(struct isl_basic_setisl_basic_map *bset,
6189 unsigned dim, int value)
6190{
6191 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6192 isl_dim_set, dim, value));
6193}
6194
6195static int remove_if_empty(__isl_keep isl_map *map, int i)
6196{
6197 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6198
6199 if (empty < 0)
6200 return -1;
6201 if (!empty)
6202 return 0;
6203
6204 isl_basic_map_free(map->p[i]);
6205 if (i != map->n - 1) {
6206 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6207 map->p[i] = map->p[map->n - 1];
6208 }
6209 map->n--;
6210
6211 return 0;
6212}
6213
6214/* Perform "fn" on each basic map of "map", where we may not be holding
6215 * the only reference to "map".
6216 * In particular, "fn" should be a semantics preserving operation
6217 * that we want to apply to all copies of "map". We therefore need
6218 * to be careful not to modify "map" in a way that breaks "map"
6219 * in case anything goes wrong.
6220 */
6221__isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6222 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6223{
6224 struct isl_basic_map *bmap;
6225 int i;
6226
6227 if (!map)
6228 return NULL((void*)0);
6229
6230 for (i = map->n - 1; i >= 0; --i) {
6231 bmap = isl_basic_map_copy(map->p[i]);
6232 bmap = fn(bmap);
6233 if (!bmap)
6234 goto error;
6235 isl_basic_map_free(map->p[i]);
6236 map->p[i] = bmap;
6237 if (remove_if_empty(map, i) < 0)
6238 goto error;
6239 }
6240
6241 return map;
6242error:
6243 isl_map_free(map);
6244 return NULL((void*)0);
6245}
6246
6247__isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6248 enum isl_dim_type type, unsigned pos, int value)
6249{
6250 int i;
6251
6252 map = isl_map_cow(map);
6253 if (!map)
6254 return NULL((void*)0);
6255
6256 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error)do { if (pos < isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "pos < isl_map_dim(map, type)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6256); goto error; } while (0); } while (0)
;
6257 for (i = map->n - 1; i >= 0; --i) {
6258 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6259 if (remove_if_empty(map, i) < 0)
6260 goto error;
6261 }
6262 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6263 return map;
6264error:
6265 isl_map_free(map);
6266 return NULL((void*)0);
6267}
6268
6269__isl_give isl_setisl_map *isl_set_fix_si(__isl_take isl_setisl_map *set,
6270 enum isl_dim_type type, unsigned pos, int value)
6271{
6272 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6273}
6274
6275__isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6276 enum isl_dim_type type, unsigned pos, isl_int value)
6277{
6278 int i;
6279
6280 map = isl_map_cow(map);
6281 if (!map)
6282 return NULL((void*)0);
6283
6284 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error)do { if (pos < isl_map_dim(map, type)) break; do { isl_handle_error
(map->ctx, isl_error_unknown, "Assertion \"" "pos < isl_map_dim(map, type)"
"\" failed", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6284); goto error; } while (0); } while (0)
;
6285 for (i = 0; i < map->n; ++i) {
6286 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6287 if (!map->p[i])
6288 goto error;
6289 }
6290 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6291 return map;
6292error:
6293 isl_map_free(map);
6294 return NULL((void*)0);
6295}
6296
6297__isl_give isl_setisl_map *isl_set_fix(__isl_take isl_setisl_map *set,
6298 enum isl_dim_type type, unsigned pos, isl_int value)
6299{
6300 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6301}
6302
6303/* Fix the value of the variable at position "pos" of type "type" of "map"
6304 * to be equal to "v".
6305 */
6306__isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6307 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6308{
6309 int i;
6310
6311 map = isl_map_cow(map);
6312 if (!map || !v)
6313 goto error;
6314
6315 if (!isl_val_is_int(v))
6316 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6317); goto error; } while (0)
6317 "expecting integer value", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "expecting integer value", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6317); goto error; } while (0)
;
6318 if (pos >= isl_map_dim(map, type))
6319 isl_die(isl_map_get_ctx(map), isl_error_invalid,do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6320); goto error; } while (0)
6320 "index out of bounds", goto error)do { isl_handle_error(isl_map_get_ctx(map), isl_error_invalid
, "index out of bounds", "/build/llvm-toolchain-snapshot-7~svn329677/tools/polly/lib/External/isl/isl_map.c"
, 6320); goto error; } while (0)
;
6321 for (i = map->n - 1; i >= 0; --i) {
6322 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6323 isl_val_copy(v));
6324 if (remove_if_empty(map, i) < 0)
6325 goto error;
6326 }
6327 ISL_F_CLR(map, ISL_MAP_NORMALIZED)(((map)->flags) &= ~((1 << 1)));
6328 isl_val_free(v);
6329 return map;
6330error:
6331 isl_map_free(map);
6332 isl_val_free(v);
6333 return NULL((void*)0);
6334}
6335
6336/* Fix the value of the variable at position "pos" of type "type" of "set"
6337 * to be equal to "v".
6338 */
6339__isl_give isl_set