Bug Summary

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