Bug Summary

File:polly/lib/External/isl/isl_map.c
Warning:line 10456, column 55
Although the value stored to 'pos' is used in the enclosing expression, the value is never actually read from 'pos'

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