Bug Summary

File:tools/polly/lib/External/isl/isl_map.c
Warning:line 5763, column 2
Value stored to 'n_out' is never read

Annotated Source Code

Press '?' to see keyboard shortcuts

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