Bug Summary

File:polly/lib/External/isl/isl_union_map.c
Warning:line 373, column 25
Access to field 'ctx' results in a dereference of a null pointer (loaded from field 'dim')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name isl_union_map.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-11/lib/clang/11.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/pet/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/ppcg/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/ppcg -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/imath -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External/isl/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/include -I /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/llvm/include -U NDEBUG -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-11/lib/clang/11.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=gnu99 -fconst-strings -fdebug-compilation-dir /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/build-llvm/tools/polly/lib/External -fdebug-prefix-map=/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347=. -ferror-limit 19 -fmessage-length 0 -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-03-09-184146-41876-1 -x c /build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c
1/*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016-2017 Sven Verdoolaege
6 *
7 * Use of this software is governed by the MIT license
8 *
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
13 * B.P. 105 - 78153 Le Chesnay, France
14 */
15
16#include <isl_map_private.h>
17#include <isl_union_map_private.h>
18#include <isl/ctx.h>
19#include <isl/hash.h>
20#include <isl_aff_private.h>
21#include <isl/map.h>
22#include <isl/set.h>
23#include <isl_space_private.h>
24#include <isl/union_set.h>
25#include <isl_maybe_map.h>
26#include <isl_id_private.h>
27
28#include <bset_from_bmap.c>
29#include <set_to_map.c>
30#include <set_from_map.c>
31#include <uset_to_umap.c>
32#include <uset_from_umap.c>
33#include <set_list_from_map_list_inl.c>
34
35/* Return the number of parameters of "umap", where "type"
36 * is required to be set to isl_dim_param.
37 */
38isl_size isl_union_map_dim(__isl_keep isl_union_map *umap,
39 enum isl_dim_type type)
40{
41 if (!umap)
42 return isl_size_error((int) -1);
43
44 if (type != isl_dim_param)
45 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 46); return ((int) -1); } while (0)
46 "can only reference parameters", return isl_size_error)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 46); return ((int) -1); } while (0)
;
47
48 return isl_space_dim(umap->dim, type);
49}
50
51/* Return the number of parameters of "uset", where "type"
52 * is required to be set to isl_dim_param.
53 */
54isl_size isl_union_set_dim(__isl_keep isl_union_setisl_union_map *uset,
55 enum isl_dim_type type)
56{
57 return isl_union_map_dim(uset, type);
58}
59
60/* Return the id of the specified dimension.
61 */
62__isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
63 enum isl_dim_type type, unsigned pos)
64{
65 if (!umap)
66 return NULL((void*)0);
67
68 if (type != isl_dim_param)
69 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 70); return ((void*)0); } while (0)
70 "can only reference parameters", return NULL)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 70); return ((void*)0); } while (0)
;
71
72 return isl_space_get_dim_id(umap->dim, type, pos);
73}
74
75/* Is this union set a parameter domain?
76 */
77isl_bool isl_union_set_is_params(__isl_keep isl_union_setisl_union_map *uset)
78{
79 isl_setisl_map *set;
80 isl_bool params;
81
82 if (!uset)
83 return isl_bool_error;
84 if (uset->table.n != 1)
85 return isl_bool_false;
86
87 set = isl_set_from_union_set(isl_union_set_copy(uset));
88 params = isl_set_is_params(set);
89 isl_set_free(set);
90 return params;
91}
92
93/* Is this union map actually a parameter domain?
94 * Users should never call this function. Outside of isl,
95 * a union map can never be a parameter domain.
96 */
97isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
98{
99 return isl_union_set_is_params(uset_from_umap(umap));
100}
101
102static __isl_give isl_union_map *isl_union_map_alloc(
103 __isl_take isl_space *space, int size)
104{
105 isl_union_map *umap;
106
107 space = isl_space_params(space);
108 if (!space)
109 return NULL((void*)0);
110
111 umap = isl_calloc_type(space->ctx, isl_union_map)((isl_union_map *)isl_calloc_or_die(space->ctx, 1, sizeof(
isl_union_map)))
;
112 if (!umap) {
113 isl_space_free(space);
114 return NULL((void*)0);
115 }
116
117 umap->ref = 1;
118 umap->dim = space;
119 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
120 return isl_union_map_free(umap);
121
122 return umap;
123}
124
125/* Create an empty union map without specifying any parameters.
126 */
127__isl_give isl_union_map *isl_union_map_empty_ctx(isl_ctx *ctx)
128{
129 return isl_union_map_empty_space(isl_space_unit(ctx));
130}
131
132__isl_give isl_union_map *isl_union_map_empty_space(__isl_take isl_space *space)
133{
134 return isl_union_map_alloc(space, 16);
135}
136
137/* This is an alternative name for the function above.
138 */
139__isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
140{
141 return isl_union_map_empty_space(space);
142}
143
144/* Create an empty union set without specifying any parameters.
145 */
146__isl_give isl_union_setisl_union_map *isl_union_set_empty_ctx(isl_ctx *ctx)
147{
148 return uset_from_umap(isl_union_map_empty_ctx(ctx));
149}
150
151__isl_give isl_union_setisl_union_map *isl_union_set_empty_space(__isl_take isl_space *space)
152{
153 return uset_from_umap(isl_union_map_empty_space(space));
154}
155
156/* This is an alternative name for the function above.
157 */
158__isl_give isl_union_setisl_union_map *isl_union_set_empty(__isl_take isl_space *space)
159{
160 return isl_union_set_empty_space(space);
161}
162
163isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
164{
165 return umap ? umap->dim->ctx : NULL((void*)0);
166}
167
168isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_setisl_union_map *uset)
169{
170 return uset ? uset->dim->ctx : NULL((void*)0);
171}
172
173/* Return the space of "umap".
174 */
175__isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
176{
177 return umap ? umap->dim : NULL((void*)0);
178}
179
180/* Return the space of "uset".
181 */
182__isl_keep isl_space *isl_union_set_peek_space(__isl_keep isl_union_setisl_union_map *uset)
183{
184 return isl_union_map_peek_space(uset_to_umap(uset));
185}
186
187__isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
188{
189 return isl_space_copy(isl_union_map_peek_space(umap));
190}
191
192/* Return the position of the parameter with the given name
193 * in "umap".
194 * Return -1 if no such dimension can be found.
195 */
196int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
197 enum isl_dim_type type, const char *name)
198{
199 if (!umap)
200 return -1;
201 return isl_space_find_dim_by_name(umap->dim, type, name);
202}
203
204__isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_setisl_union_map *uset)
205{
206 return isl_union_map_get_space(uset);
207}
208
209static isl_stat free_umap_entry(void **entry, void *user)
210{
211 isl_map *map = *entry;
212 isl_map_free(map);
213 return isl_stat_ok;
214}
215
216static isl_stat add_map(__isl_take isl_map *map, void *user)
217{
218 isl_union_map **umap = (isl_union_map **)user;
219
220 *umap = isl_union_map_add_map(*umap, map);
221
222 return isl_stat_ok;
223}
224
225__isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
226{
227 isl_union_map *dup;
228
229 if (!umap)
230 return NULL((void*)0);
231
232 dup = isl_union_map_empty(isl_space_copy(umap->dim));
233 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
234 goto error;
235 return dup;
236error:
237 isl_union_map_free(dup);
238 return NULL((void*)0);
239}
240
241__isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
242{
243 if (!umap)
3
Assuming 'umap' is non-null
4
Taking false branch
244 return NULL((void*)0);
245
246 if (umap->ref == 1)
5
Assuming field 'ref' is equal to 1
6
Taking true branch
247 return umap;
7
Returning without writing to 'umap->dim', which participates in a condition later
8
Returning without writing to 'umap->dim'
9
Returning pointer (loaded from 'umap'), which participates in a condition later
248 umap->ref--;
249 return isl_union_map_dup(umap);
250}
251
252struct isl_union_align {
253 isl_reordering *exp;
254 isl_union_map *res;
255};
256
257static isl_stat align_entry(void **entry, void *user)
258{
259 isl_map *map = *entry;
260 isl_reordering *exp;
261 struct isl_union_align *data = user;
262
263 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
264 isl_map_get_space(map));
265
266 data->res = isl_union_map_add_map(data->res,
267 isl_map_realign(isl_map_copy(map), exp));
268
269 return isl_stat_ok;
270}
271
272/* Align the parameters of umap along those of model.
273 * The result has the parameters of model first, in the same order
274 * as they appear in model, followed by any remaining parameters of
275 * umap that do not appear in model.
276 */
277__isl_give isl_union_map *isl_union_map_align_params(
278 __isl_take isl_union_map *umap, __isl_take isl_space *model)
279{
280 struct isl_union_align data = { NULL((void*)0), NULL((void*)0) };
281 isl_bool equal_params;
282
283 if (!umap || !model)
284 goto error;
285
286 equal_params = isl_space_has_equal_params(umap->dim, model);
287 if (equal_params < 0)
288 goto error;
289 if (equal_params) {
290 isl_space_free(model);
291 return umap;
292 }
293
294 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
295 if (!data.exp)
296 goto error;
297
298 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
299 umap->table.n);
300 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
301 &align_entry, &data) < 0)
302 goto error;
303
304 isl_reordering_free(data.exp);
305 isl_union_map_free(umap);
306 isl_space_free(model);
307 return data.res;
308error:
309 isl_reordering_free(data.exp);
310 isl_union_map_free(umap);
311 isl_union_map_free(data.res);
312 isl_space_free(model);
313 return NULL((void*)0);
314}
315
316__isl_give isl_union_setisl_union_map *isl_union_set_align_params(
317 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_space *model)
318{
319 return isl_union_map_align_params(uset, model);
320}
321
322__isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
323 __isl_take isl_union_map *umap2)
324{
325 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
326 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
327
328 umap1 = isl_union_map_cow(umap1);
329
330 if (!umap1 || !umap2)
331 goto error;
332
333 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
334 goto error;
335
336 isl_union_map_free(umap2);
337
338 return umap1;
339error:
340 isl_union_map_free(umap1);
341 isl_union_map_free(umap2);
342 return NULL((void*)0);
343}
344
345__isl_give isl_union_setisl_union_map *isl_union_set_union(__isl_take isl_union_setisl_union_map *uset1,
346 __isl_take isl_union_setisl_union_map *uset2)
347{
348 return isl_union_map_union(uset1, uset2);
349}
350
351__isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
352{
353 if (!umap)
354 return NULL((void*)0);
355
356 umap->ref++;
357 return umap;
358}
359
360__isl_give isl_union_setisl_union_map *isl_union_set_copy(__isl_keep isl_union_setisl_union_map *uset)
361{
362 return isl_union_map_copy(uset);
363}
364
365__isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
366{
367 if (!umap
16.1
'umap' is non-null
)
17
Taking false branch
368 return NULL((void*)0);
369
370 if (--umap->ref > 0)
18
Taking false branch
371 return NULL((void*)0);
372
373 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
19
Access to field 'ctx' results in a dereference of a null pointer (loaded from field 'dim')
374 &free_umap_entry, NULL((void*)0));
375 isl_hash_table_clear(&umap->table);
376 isl_space_free(umap->dim);
377 free(umap);
378 return NULL((void*)0);
379}
380
381__isl_null isl_union_setisl_union_map *isl_union_set_free(__isl_take isl_union_setisl_union_map *uset)
382{
383 return isl_union_map_free(uset);
384}
385
386/* Do "umap" and "space" have the same parameters?
387 */
388isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
389 __isl_keep isl_space *space)
390{
391 isl_space *umap_space;
392
393 umap_space = isl_union_map_peek_space(umap);
394 return isl_space_has_equal_params(umap_space, space);
395}
396
397/* Do "uset" and "space" have the same parameters?
398 */
399isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_setisl_union_map *uset,
400 __isl_keep isl_space *space)
401{
402 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
403}
404
405static isl_bool has_space(const void *entry, const void *val)
406{
407 isl_map *map = (isl_map *)entry;
408 isl_space *space = (isl_space *) val;
409
410 return isl_space_is_equal(map->dim, space);
411}
412
413__isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
414 __isl_take isl_map *map)
415{
416 uint32_t hash;
417 struct isl_hash_table_entry *entry;
418 isl_bool aligned;
419
420 if (!map || !umap)
421 goto error;
422
423 if (isl_map_plain_is_empty(map)) {
424 isl_map_free(map);
425 return umap;
426 }
427
428 aligned = isl_map_space_has_equal_params(map, umap->dim);
429 if (aligned < 0)
430 goto error;
431 if (!aligned) {
432 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
433 map = isl_map_align_params(map, isl_union_map_get_space(umap));
434 }
435
436 umap = isl_union_map_cow(umap);
437
438 if (!map || !umap)
439 goto error;
440
441 hash = isl_space_get_hash(map->dim);
442 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
443 &has_space, map->dim, 1);
444 if (!entry)
445 goto error;
446
447 if (!entry->data)
448 entry->data = map;
449 else {
450 entry->data = isl_map_union(entry->data, isl_map_copy(map));
451 if (!entry->data)
452 goto error;
453 isl_map_free(map);
454 }
455
456 return umap;
457error:
458 isl_map_free(map);
459 isl_union_map_free(umap);
460 return NULL((void*)0);
461}
462
463__isl_give isl_union_setisl_union_map *isl_union_set_add_set(__isl_take isl_union_setisl_union_map *uset,
464 __isl_take isl_setisl_map *set)
465{
466 return isl_union_map_add_map(uset, set_to_map(set));
467}
468
469__isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
470{
471 isl_space *dim;
472 isl_union_map *umap;
473
474 if (!map)
475 return NULL((void*)0);
476
477 dim = isl_map_get_space(map);
478 dim = isl_space_params(dim);
479 umap = isl_union_map_empty(dim);
480 umap = isl_union_map_add_map(umap, map);
481
482 return umap;
483}
484
485__isl_give isl_union_setisl_union_map *isl_union_set_from_set(__isl_take isl_setisl_map *set)
486{
487 return isl_union_map_from_map(set_to_map(set));
488}
489
490__isl_give isl_union_map *isl_union_map_from_basic_map(
491 __isl_take isl_basic_map *bmap)
492{
493 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
494}
495
496__isl_give isl_union_setisl_union_map *isl_union_set_from_basic_set(
497 __isl_take isl_basic_setisl_basic_map *bset)
498{
499 return isl_union_map_from_basic_map(bset);
500}
501
502struct isl_union_map_foreach_data
503{
504 isl_stat (*fn)(__isl_take isl_map *map, void *user);
505 void *user;
506};
507
508static isl_stat call_on_copy(void **entry, void *user)
509{
510 isl_map *map = *entry;
511 struct isl_union_map_foreach_data *data;
512 data = (struct isl_union_map_foreach_data *)user;
513
514 return data->fn(isl_map_copy(map), data->user);
515}
516
517isl_size isl_union_map_n_map(__isl_keep isl_union_map *umap)
518{
519 return umap ? umap->table.n : isl_size_error((int) -1);
520}
521
522isl_size isl_union_set_n_set(__isl_keep isl_union_setisl_union_map *uset)
523{
524 return uset ? uset->table.n : isl_size_error((int) -1);
525}
526
527isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
528 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
529{
530 struct isl_union_map_foreach_data data = { fn, user };
531
532 if (!umap)
533 return isl_stat_error;
534
535 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
536 &call_on_copy, &data);
537}
538
539/* Internal data structure for isl_union_map_every_map.
540 *
541 * "test" is the user-specified callback function.
542 * "user" is the user-specified callback function argument.
543 *
544 * "failed" is initialized to 0 and set to 1 if "test" fails
545 * on any map.
546 */
547struct isl_union_map_every_data {
548 isl_bool (*test)(__isl_keep isl_map *map, void *user);
549 void *user;
550 int failed;
551};
552
553/* Call data->test on "map".
554 * If this fails, then set data->failed and abort.
555 */
556static isl_stat call_every(void **entry, void *user)
557{
558 isl_map *map = *entry;
559 struct isl_union_map_every_data *data = user;
560 isl_bool r;
561
562 r = data->test(map, data->user);
563 if (r < 0)
564 return isl_stat_error;
565 if (r)
566 return isl_stat_ok;
567 data->failed = 1;
568 return isl_stat_error;
569}
570
571/* Does "test" succeed on every map in "umap"?
572 */
573isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
574 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
575{
576 struct isl_union_map_every_data data = { test, user, 0 };
577 isl_stat r;
578
579 if (!umap)
580 return isl_bool_error;
581
582 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
583 &call_every, &data);
584 if (r >= 0)
585 return isl_bool_true;
586 if (data.failed)
587 return isl_bool_false;
588 return isl_bool_error;
589}
590
591/* Add "map" to "list".
592 */
593static isl_stat add_list_map(__isl_take isl_map *map, void *user)
594{
595 isl_map_list **list = user;
596
597 *list = isl_map_list_add(*list, map);
598
599 if (!*list)
600 return isl_stat_error;
601 return isl_stat_ok;
602}
603
604/* Return the maps in "umap" as a list.
605 *
606 * First construct a list of the appropriate size and then add all the
607 * elements.
608 */
609__isl_give isl_map_list *isl_union_map_get_map_list(
610 __isl_keep isl_union_map *umap)
611{
612 isl_size n_maps;
613 isl_ctx *ctx;
614 isl_map_list *list;
615
616 n_maps = isl_union_map_n_map(umap);
617 if (n_maps < 0)
618 return NULL((void*)0);
619 ctx = isl_union_map_get_ctx(umap);
620 list = isl_map_list_alloc(ctx, n_maps);
621
622 if (isl_union_map_foreach_map(umap, &add_list_map, &list) < 0)
623 list = isl_map_list_free(list);
624
625 return list;
626}
627
628/* Return the sets in "uset" as a list.
629 */
630__isl_give isl_set_listisl_map_list *isl_union_set_get_set_list(
631 __isl_keep isl_union_setisl_union_map *uset)
632{
633 return set_list_from_map_list(
634 isl_union_map_get_map_list(uset_to_umap(uset)));
635}
636
637/* Can "umap" be converted to an isl_map?
638 * That is, does it contain elements in exactly one space?
639 */
640isl_bool isl_union_map_isa_map(__isl_keep isl_union_map *umap)
641{
642 isl_size n;
643
644 n = isl_union_map_n_map(umap);
645 if (n < 0)
646 return isl_bool_error;
647 return isl_bool_ok(n == 1);
648}
649
650/* Can "uset" be converted to an isl_set?
651 * That is, does it contain elements in exactly one space?
652 */
653isl_bool isl_union_set_isa_set(__isl_keep isl_union_setisl_union_map *uset)
654{
655 return isl_union_map_isa_map(uset_to_umap(uset));
656}
657
658static isl_stat copy_map(void **entry, void *user)
659{
660 isl_map *map = *entry;
661 isl_map **map_p = user;
662
663 *map_p = isl_map_copy(map);
664
665 return isl_stat_error;
666}
667
668__isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
669{
670 isl_bool is_map;
671 isl_ctx *ctx;
672 isl_map *map = NULL((void*)0);
673
674 is_map = isl_union_map_isa_map(umap);
675 if (is_map < 0)
676 goto error;
677 ctx = isl_union_map_get_ctx(umap);
678 if (!is_map)
679 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 681); goto error; } while (0)
680 "union map needs to contain elements in exactly "do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 681); goto error; } while (0)
681 "one space", goto error)do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 681); goto error; } while (0)
;
682
683 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
684
685 isl_union_map_free(umap);
686
687 return map;
688error:
689 isl_union_map_free(umap);
690 return NULL((void*)0);
691}
692
693__isl_give isl_setisl_map *isl_set_from_union_set(__isl_take isl_union_setisl_union_map *uset)
694{
695 return isl_map_from_union_map(uset);
696}
697
698/* Extract the map in "umap" that lives in the given space (ignoring
699 * parameters).
700 */
701__isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
702 __isl_take isl_space *space)
703{
704 uint32_t hash;
705 struct isl_hash_table_entry *entry;
706
707 space = isl_space_drop_all_params(space);
708 space = isl_space_align_params(space, isl_union_map_get_space(umap));
709 if (!umap || !space)
710 goto error;
711
712 hash = isl_space_get_hash(space);
713 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
714 &has_space, space, 0);
715 if (!entry)
716 goto error;
717 if (entry == isl_hash_table_entry_none)
718 return isl_map_empty(space);
719 isl_space_free(space);
720 return isl_map_copy(entry->data);
721error:
722 isl_space_free(space);
723 return NULL((void*)0);
724}
725
726__isl_give isl_setisl_map *isl_union_set_extract_set(__isl_keep isl_union_setisl_union_map *uset,
727 __isl_take isl_space *space)
728{
729 return set_from_map(isl_union_map_extract_map(uset, space));
730}
731
732/* Check if umap contains a map in the given space.
733 */
734isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
735 __isl_keep isl_space *space)
736{
737 uint32_t hash;
738 struct isl_hash_table_entry *entry;
739
740 if (!umap || !space)
741 return isl_bool_error;
742
743 hash = isl_space_get_hash(space);
744 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
745 &has_space, space, 0);
746 if (!entry)
747 return isl_bool_error;
748 return isl_bool_ok(entry != isl_hash_table_entry_none);
749}
750
751isl_bool isl_union_set_contains(__isl_keep isl_union_setisl_union_map *uset,
752 __isl_keep isl_space *space)
753{
754 return isl_union_map_contains(uset, space);
755}
756
757isl_stat isl_union_set_foreach_set(__isl_keep isl_union_setisl_union_map *uset,
758 isl_stat (*fn)(__isl_take isl_setisl_map *set, void *user), void *user)
759{
760 return isl_union_map_foreach_map(uset,
761 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
762}
763
764/* Internal data structure for isl_union_set_every_set.
765 *
766 * "test" is the user-specified callback function.
767 * "user" is the user-specified callback function argument.
768 */
769struct isl_test_set_from_map_data {
770 isl_bool (*test)(__isl_keep isl_setisl_map *set, void *user);
771 void *user;
772};
773
774/* Call data->test on "map", which is part of an isl_union_set and
775 * therefore known to be an isl_set.
776 */
777static isl_bool test_set_from_map(__isl_keep isl_map *map, void *user)
778{
779 struct isl_test_set_from_map_data *data = user;
780
781 return data->test(set_from_map(map), data->user);
782}
783
784/* Does "test" succeed on every set in "uset"?
785 */
786isl_bool isl_union_set_every_set(__isl_keep isl_union_setisl_union_map *uset,
787 isl_bool (*test)(__isl_keep isl_setisl_map *set, void *user), void *user)
788{
789 struct isl_test_set_from_map_data data = { test, user };
790
791 return isl_union_map_every_map(uset_to_umap(uset),
792 &test_set_from_map, &data);
793}
794
795struct isl_union_set_foreach_point_data {
796 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
797 void *user;
798};
799
800static isl_stat foreach_point(__isl_take isl_setisl_map *set, void *user)
801{
802 struct isl_union_set_foreach_point_data *data = user;
803 isl_stat r;
804
805 r = isl_set_foreach_point(set, data->fn, data->user);
806 isl_set_free(set);
807
808 return r;
809}
810
811isl_stat isl_union_set_foreach_point(__isl_keep isl_union_setisl_union_map *uset,
812 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
813{
814 struct isl_union_set_foreach_point_data data = { fn, user };
815 return isl_union_set_foreach_set(uset, &foreach_point, &data);
816}
817
818/* Data structure that specifies how gen_bin_op should
819 * construct results from the inputs.
820 *
821 * If "subtract" is set, then a map in the first input is copied to the result
822 * if there is no corresponding map in the second input.
823 * Otherwise, a map in the first input with no corresponding map
824 * in the second input is ignored.
825 * If "filter" is not NULL, then it specifies which maps in the first
826 * input may have a matching map in the second input.
827 * In particular, it makes sure that "match_space" can be called
828 * on the space of the map.
829 * "match_space" specifies how to transform the space of a map
830 * in the first input to the space of the corresponding map
831 * in the second input.
832 * "fn_map" specifies how the matching maps, one from each input,
833 * should be combined to form a map in the result.
834 */
835struct isl_bin_op_control {
836 int subtract;
837 isl_bool (*filter)(__isl_keep isl_map *map);
838 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
839 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
840 __isl_take isl_map *map2);
841};
842
843/* Internal data structure for gen_bin_op.
844 * "control" specifies how the maps in the result should be constructed.
845 * "umap2" is a pointer to the second argument.
846 * "res" collects the results.
847 */
848struct isl_union_map_gen_bin_data {
849 struct isl_bin_op_control *control;
850 isl_union_map *umap2;
851 isl_union_map *res;
852};
853
854/* Add a copy of "map" to "res" and return the result.
855 */
856static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
857 __isl_keep isl_map *map)
858{
859 return isl_union_map_add_map(res, isl_map_copy(map));
860}
861
862/* Combine "map1" and "map2", add the result to "res" and return the result.
863 * Check whether the result is empty before adding it to "res".
864 */
865static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
866 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
867 struct isl_union_map_gen_bin_data *data)
868{
869 isl_bool empty;
870 isl_map *map;
871
872 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
873 empty = isl_map_is_empty(map);
874 if (empty < 0 || empty) {
875 isl_map_free(map);
876 if (empty < 0)
877 return isl_union_map_free(res);
878 return res;
879 }
880 return isl_union_map_add_map(res, map);
881}
882
883/* Dummy match_space function that simply returns the input space.
884 */
885static __isl_give isl_space *identity(__isl_take isl_space *space)
886{
887 return space;
888}
889
890/* Look for the map in data->umap2 that corresponds to "map", if any.
891 * Return (isl_bool_true, matching map) if there is one,
892 * (isl_bool_false, NULL) if there is no matching map and
893 * (isl_bool_error, NULL) on error.
894 *
895 * If not NULL, then data->control->filter specifies whether "map"
896 * can have any matching map. If so,
897 * data->control->match_space specifies which map in data->umap2
898 * corresponds to "map".
899 */
900static __isl_keep isl_maybe_isl_map bin_try_get_match(
901 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
902{
903 uint32_t hash;
904 struct isl_hash_table_entry *entry2;
905 isl_space *space;
906 isl_maybe_isl_map res = { isl_bool_error, NULL((void*)0) };
907
908 if (data->control->filter) {
909 res.valid = data->control->filter(map);
910 if (res.valid < 0 || !res.valid)
911 return res;
912 res.valid = isl_bool_error;
913 }
914
915 space = isl_map_get_space(map);
916 if (data->control->match_space != &identity)
917 space = data->control->match_space(space);
918 if (!space)
919 return res;
920 hash = isl_space_get_hash(space);
921 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
922 &data->umap2->table, hash,
923 &has_space, space, 0);
924 isl_space_free(space);
925 if (entry2)
926 res.valid = isl_bool_ok(entry2 != isl_hash_table_entry_none);
927 if (res.valid >= 0 && res.valid)
928 res.value = entry2->data;
929
930 return res;
931}
932
933/* isl_hash_table_foreach callback for gen_bin_op.
934 * Look for the map in data->umap2 that corresponds
935 * to the map that "entry" points to, apply the binary operation and
936 * add the result to data->res.
937 *
938 * If no corresponding map can be found, then the effect depends
939 * on data->control->subtract. If it is set, then the current map
940 * is added directly to the result. Otherwise, it is ignored.
941 */
942static isl_stat gen_bin_entry(void **entry, void *user)
943{
944 struct isl_union_map_gen_bin_data *data = user;
945 isl_map *map = *entry;
946 isl_maybe_isl_map m;
947
948 m = bin_try_get_match(data, map);
949 if (m.valid < 0)
950 return isl_stat_error;
951 if (!m.valid && !data->control->subtract)
952 return isl_stat_ok;
953
954 if (!m.valid)
955 data->res = bin_add_map(data->res, map);
956 else
957 data->res = bin_add_pair(data->res, map, m.value, data);
958 if (!data->res)
959 return isl_stat_error;
960
961 return isl_stat_ok;
962}
963
964/* Apply a binary operation to "umap1" and "umap2" based on "control".
965 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
966 * in gen_bin_entry.
967 */
968static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
969 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
970{
971 struct isl_union_map_gen_bin_data data = { control, NULL((void*)0), NULL((void*)0) };
972
973 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
974 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
975
976 if (!umap1 || !umap2)
977 goto error;
978
979 data.umap2 = umap2;
980 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
981 umap1->table.n);
982 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
983 &gen_bin_entry, &data) < 0)
984 goto error;
985
986 isl_union_map_free(umap1);
987 isl_union_map_free(umap2);
988 return data.res;
989error:
990 isl_union_map_free(umap1);
991 isl_union_map_free(umap2);
992 isl_union_map_free(data.res);
993 return NULL((void*)0);
994}
995
996__isl_give isl_union_map *isl_union_map_subtract(
997 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
998{
999 struct isl_bin_op_control control = {
1000 .subtract = 1,
1001 .match_space = &identity,
1002 .fn_map = &isl_map_subtract,
1003 };
1004
1005 return gen_bin_op(umap1, umap2, &control);
1006}
1007
1008__isl_give isl_union_setisl_union_map *isl_union_set_subtract(
1009 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1010{
1011 return isl_union_map_subtract(uset1, uset2);
1012}
1013
1014struct isl_union_map_gen_bin_set_data {
1015 isl_setisl_map *set;
1016 isl_union_map *res;
1017};
1018
1019static isl_stat intersect_params_entry(void **entry, void *user)
1020{
1021 struct isl_union_map_gen_bin_set_data *data = user;
1022 isl_map *map = *entry;
1023 int empty;
1024
1025 map = isl_map_copy(map);
1026 map = isl_map_intersect_params(map, isl_set_copy(data->set));
1027
1028 empty = isl_map_is_empty(map);
1029 if (empty < 0) {
1030 isl_map_free(map);
1031 return isl_stat_error;
1032 }
1033
1034 data->res = isl_union_map_add_map(data->res, map);
1035
1036 return isl_stat_ok;
1037}
1038
1039static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
1040 __isl_take isl_setisl_map *set, isl_stat (*fn)(void **, void *))
1041{
1042 struct isl_union_map_gen_bin_set_data data = { NULL((void*)0), NULL((void*)0) };
1043
1044 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
1045 set = isl_set_align_params(set, isl_union_map_get_space(umap));
1046
1047 if (!umap || !set)
1048 goto error;
1049
1050 data.set = set;
1051 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
1052 umap->table.n);
1053 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1054 fn, &data) < 0)
1055 goto error;
1056
1057 isl_union_map_free(umap);
1058 isl_set_free(set);
1059 return data.res;
1060error:
1061 isl_union_map_free(umap);
1062 isl_set_free(set);
1063 isl_union_map_free(data.res);
1064 return NULL((void*)0);
1065}
1066
1067/* Intersect "umap" with the parameter domain "set".
1068 *
1069 * If "set" does not have any constraints, then we can return immediately.
1070 */
1071__isl_give isl_union_map *isl_union_map_intersect_params(
1072 __isl_take isl_union_map *umap, __isl_take isl_setisl_map *set)
1073{
1074 int is_universe;
1075
1076 is_universe = isl_set_plain_is_universe(set);
1077 if (is_universe < 0)
1078 goto error;
1079 if (is_universe) {
1080 isl_set_free(set);
1081 return umap;
1082 }
1083
1084 return gen_bin_set_op(umap, set, &intersect_params_entry);
1085error:
1086 isl_union_map_free(umap);
1087 isl_set_free(set);
1088 return NULL((void*)0);
1089}
1090
1091__isl_give isl_union_setisl_union_map *isl_union_set_intersect_params(
1092 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_setisl_map *set)
1093{
1094 return isl_union_map_intersect_params(uset, set);
1095}
1096
1097static __isl_give isl_union_map *union_map_intersect_params(
1098 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1099{
1100 return isl_union_map_intersect_params(umap,
1101 isl_set_from_union_set(uset));
1102}
1103
1104static __isl_give isl_union_map *union_map_gist_params(
1105 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1106{
1107 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
1108}
1109
1110struct isl_union_map_match_bin_data {
1111 isl_union_map *umap2;
1112 isl_union_map *res;
1113 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
1114};
1115
1116static isl_stat match_bin_entry(void **entry, void *user)
1117{
1118 struct isl_union_map_match_bin_data *data = user;
1119 uint32_t hash;
1120 struct isl_hash_table_entry *entry2;
1121 isl_map *map = *entry;
1122 int empty;
1123
1124 hash = isl_space_get_hash(map->dim);
1125 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1126 hash, &has_space, map->dim, 0);
1127 if (!entry2)
1128 return isl_stat_error;
1129 if (entry2 == isl_hash_table_entry_none)
1130 return isl_stat_ok;
1131
1132 map = isl_map_copy(map);
1133 map = data->fn(map, isl_map_copy(entry2->data));
1134
1135 empty = isl_map_is_empty(map);
1136 if (empty < 0) {
1137 isl_map_free(map);
1138 return isl_stat_error;
1139 }
1140 if (empty) {
1141 isl_map_free(map);
1142 return isl_stat_ok;
1143 }
1144
1145 data->res = isl_union_map_add_map(data->res, map);
1146
1147 return isl_stat_ok;
1148}
1149
1150static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1151 __isl_take isl_union_map *umap2,
1152 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1153{
1154 struct isl_union_map_match_bin_data data = { NULL((void*)0), NULL((void*)0), fn };
1155
1156 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1157 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1158
1159 if (!umap1 || !umap2)
1160 goto error;
1161
1162 data.umap2 = umap2;
1163 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1164 umap1->table.n);
1165 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1166 &match_bin_entry, &data) < 0)
1167 goto error;
1168
1169 isl_union_map_free(umap1);
1170 isl_union_map_free(umap2);
1171 return data.res;
1172error:
1173 isl_union_map_free(umap1);
1174 isl_union_map_free(umap2);
1175 isl_union_map_free(data.res);
1176 return NULL((void*)0);
1177}
1178
1179__isl_give isl_union_map *isl_union_map_intersect(
1180 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1181{
1182 return match_bin_op(umap1, umap2, &isl_map_intersect);
1183}
1184
1185/* Compute the intersection of the two union_sets.
1186 * As a special case, if exactly one of the two union_sets
1187 * is a parameter domain, then intersect the parameter domain
1188 * of the other one with this set.
1189 */
1190__isl_give isl_union_setisl_union_map *isl_union_set_intersect(
1191 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1192{
1193 int p1, p2;
1194
1195 p1 = isl_union_set_is_params(uset1);
1196 p2 = isl_union_set_is_params(uset2);
1197 if (p1 < 0 || p2 < 0)
1198 goto error;
1199 if (!p1 && p2)
1200 return union_map_intersect_params(uset1, uset2);
1201 if (p1 && !p2)
1202 return union_map_intersect_params(uset2, uset1);
1203 return isl_union_map_intersect(uset1, uset2);
1204error:
1205 isl_union_set_free(uset1);
1206 isl_union_set_free(uset2);
1207 return NULL((void*)0);
1208}
1209
1210static isl_stat gist_params_entry(void **entry, void *user)
1211{
1212 struct isl_union_map_gen_bin_set_data *data = user;
1213 isl_map *map = *entry;
1214 int empty;
1215
1216 map = isl_map_copy(map);
1217 map = isl_map_gist_params(map, isl_set_copy(data->set));
1218
1219 empty = isl_map_is_empty(map);
1220 if (empty < 0) {
1221 isl_map_free(map);
1222 return isl_stat_error;
1223 }
1224
1225 data->res = isl_union_map_add_map(data->res, map);
1226
1227 return isl_stat_ok;
1228}
1229
1230__isl_give isl_union_map *isl_union_map_gist_params(
1231 __isl_take isl_union_map *umap, __isl_take isl_setisl_map *set)
1232{
1233 return gen_bin_set_op(umap, set, &gist_params_entry);
1234}
1235
1236__isl_give isl_union_setisl_union_map *isl_union_set_gist_params(
1237 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_setisl_map *set)
1238{
1239 return isl_union_map_gist_params(uset, set);
1240}
1241
1242__isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1243 __isl_take isl_union_map *context)
1244{
1245 return match_bin_op(umap, context, &isl_map_gist);
1246}
1247
1248__isl_give isl_union_setisl_union_map *isl_union_set_gist(__isl_take isl_union_setisl_union_map *uset,
1249 __isl_take isl_union_setisl_union_map *context)
1250{
1251 if (isl_union_set_is_params(context))
1252 return union_map_gist_params(uset, context);
1253 return isl_union_map_gist(uset, context);
1254}
1255
1256/* For each map in "umap", remove the constraints in the corresponding map
1257 * of "context".
1258 * Each map in "context" is assumed to consist of a single disjunct and
1259 * to have explicit representations for all local variables.
1260 */
1261__isl_give isl_union_map *isl_union_map_plain_gist(
1262 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1263{
1264 return match_bin_op(umap, context, &isl_map_plain_gist);
1265}
1266
1267/* For each set in "uset", remove the constraints in the corresponding set
1268 * of "context".
1269 * Each set in "context" is assumed to consist of a single disjunct and
1270 * to have explicit representations for all local variables.
1271 */
1272__isl_give isl_union_setisl_union_map *isl_union_set_plain_gist(
1273 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_union_setisl_union_map *context)
1274{
1275 return isl_union_map_plain_gist(uset, context);
1276}
1277
1278static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1279 __isl_take isl_map *set2)
1280{
1281 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1282}
1283
1284static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1285 __isl_take isl_map *set2)
1286{
1287 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1288}
1289
1290__isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1291 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1292{
1293 return match_bin_op(uset1, uset2, &lex_lt_set);
1294}
1295
1296__isl_give isl_union_map *isl_union_set_lex_le_union_set(
1297 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1298{
1299 return match_bin_op(uset1, uset2, &lex_le_set);
1300}
1301
1302__isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1303 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1304{
1305 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1306}
1307
1308__isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1309 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
1310{
1311 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1312}
1313
1314__isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1315 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1316{
1317 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1318}
1319
1320__isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1321 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1322{
1323 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1324}
1325
1326/* Intersect the domain of "umap" with "uset".
1327 */
1328static __isl_give isl_union_map *union_map_intersect_domain(
1329 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1330{
1331 struct isl_bin_op_control control = {
1332 .match_space = &isl_space_domain,
1333 .fn_map = &isl_map_intersect_domain,
1334 };
1335
1336 return gen_bin_op(umap, uset, &control);
1337}
1338
1339/* Intersect the domain of "umap" with "uset".
1340 * If "uset" is a parameters domain, then intersect the parameter
1341 * domain of "umap" with this set.
1342 */
1343__isl_give isl_union_map *isl_union_map_intersect_domain(
1344 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1345{
1346 if (isl_union_set_is_params(uset))
1347 return union_map_intersect_params(umap, uset);
1348 else
1349 return union_map_intersect_domain(umap, uset);
1350}
1351
1352/* Remove the elements of "uset" from the domain of "umap".
1353 */
1354__isl_give isl_union_map *isl_union_map_subtract_domain(
1355 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *dom)
1356{
1357 struct isl_bin_op_control control = {
1358 .subtract = 1,
1359 .match_space = &isl_space_domain,
1360 .fn_map = &isl_map_subtract_domain,
1361 };
1362
1363 return gen_bin_op(umap, dom, &control);
1364}
1365
1366/* Remove the elements of "uset" from the range of "umap".
1367 */
1368__isl_give isl_union_map *isl_union_map_subtract_range(
1369 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *dom)
1370{
1371 struct isl_bin_op_control control = {
1372 .subtract = 1,
1373 .match_space = &isl_space_range,
1374 .fn_map = &isl_map_subtract_range,
1375 };
1376
1377 return gen_bin_op(umap, dom, &control);
1378}
1379
1380/* Compute the gist of "umap" with respect to the domain "uset".
1381 */
1382static __isl_give isl_union_map *union_map_gist_domain(
1383 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1384{
1385 struct isl_bin_op_control control = {
1386 .match_space = &isl_space_domain,
1387 .fn_map = &isl_map_gist_domain,
1388 };
1389
1390 return gen_bin_op(umap, uset, &control);
1391}
1392
1393/* Compute the gist of "umap" with respect to the domain "uset".
1394 * If "uset" is a parameters domain, then compute the gist
1395 * with respect to this parameter domain.
1396 */
1397__isl_give isl_union_map *isl_union_map_gist_domain(
1398 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1399{
1400 if (isl_union_set_is_params(uset))
1401 return union_map_gist_params(umap, uset);
1402 else
1403 return union_map_gist_domain(umap, uset);
1404}
1405
1406/* Compute the gist of "umap" with respect to the range "uset".
1407 */
1408__isl_give isl_union_map *isl_union_map_gist_range(
1409 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1410{
1411 struct isl_bin_op_control control = {
1412 .match_space = &isl_space_range,
1413 .fn_map = &isl_map_gist_range,
1414 };
1415
1416 return gen_bin_op(umap, uset, &control);
1417}
1418
1419__isl_give isl_union_map *isl_union_map_intersect_range(
1420 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1421{
1422 struct isl_bin_op_control control = {
1423 .match_space = &isl_space_range,
1424 .fn_map = &isl_map_intersect_range,
1425 };
1426
1427 return gen_bin_op(umap, uset, &control);
1428}
1429
1430/* Intersect each map in "umap" in a space [A -> B] -> C
1431 * with the corresponding map in "factor" in the space B -> C and
1432 * collect the results.
1433 */
1434__isl_give isl_union_map *isl_union_map_intersect_domain_factor_range(
1435 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1436{
1437 struct isl_bin_op_control control = {
1438 .filter = &isl_map_domain_is_wrapping,
1439 .match_space = &isl_space_domain_factor_range,
1440 .fn_map = &isl_map_intersect_domain_factor_range,
1441 };
1442
1443 return gen_bin_op(umap, factor, &control);
1444}
1445
1446/* Intersect each map in "umap" in a space A -> [B -> C]
1447 * with the corresponding map in "factor" in the space A -> B and
1448 * collect the results.
1449 */
1450__isl_give isl_union_map *isl_union_map_intersect_range_factor_domain(
1451 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1452{
1453 struct isl_bin_op_control control = {
1454 .filter = &isl_map_range_is_wrapping,
1455 .match_space = &isl_space_range_factor_domain,
1456 .fn_map = &isl_map_intersect_range_factor_domain,
1457 };
1458
1459 return gen_bin_op(umap, factor, &control);
1460}
1461
1462/* Intersect each map in "umap" in a space A -> [B -> C]
1463 * with the corresponding map in "factor" in the space A -> C and
1464 * collect the results.
1465 */
1466__isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1467 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1468{
1469 struct isl_bin_op_control control = {
1470 .filter = &isl_map_range_is_wrapping,
1471 .match_space = &isl_space_range_factor_range,
1472 .fn_map = &isl_map_intersect_range_factor_range,
1473 };
1474
1475 return gen_bin_op(umap, factor, &control);
1476}
1477
1478struct isl_union_map_bin_data {
1479 isl_union_map *umap2;
1480 isl_union_map *res;
1481 isl_map *map;
1482 isl_stat (*fn)(void **entry, void *user);
1483};
1484
1485static isl_stat apply_range_entry(void **entry, void *user)
1486{
1487 struct isl_union_map_bin_data *data = user;
1488 isl_map *map2 = *entry;
1489 isl_bool empty;
1490
1491 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1492 map2->dim, isl_dim_in))
1493 return isl_stat_ok;
1494
1495 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1496
1497 empty = isl_map_is_empty(map2);
1498 if (empty < 0) {
1499 isl_map_free(map2);
1500 return isl_stat_error;
1501 }
1502 if (empty) {
1503 isl_map_free(map2);
1504 return isl_stat_ok;
1505 }
1506
1507 data->res = isl_union_map_add_map(data->res, map2);
1508
1509 return isl_stat_ok;
1510}
1511
1512static isl_stat bin_entry(void **entry, void *user)
1513{
1514 struct isl_union_map_bin_data *data = user;
1515 isl_map *map = *entry;
1516
1517 data->map = map;
1518 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1519 data->fn, data) < 0)
1520 return isl_stat_error;
1521
1522 return isl_stat_ok;
1523}
1524
1525static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1526 __isl_take isl_union_map *umap2,
1527 isl_stat (*fn)(void **entry, void *user))
1528{
1529 struct isl_union_map_bin_data data = { NULL((void*)0), NULL((void*)0), NULL((void*)0), fn };
1530
1531 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1532 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1533
1534 if (!umap1 || !umap2)
1535 goto error;
1536
1537 data.umap2 = umap2;
1538 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1539 umap1->table.n);
1540 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1541 &bin_entry, &data) < 0)
1542 goto error;
1543
1544 isl_union_map_free(umap1);
1545 isl_union_map_free(umap2);
1546 return data.res;
1547error:
1548 isl_union_map_free(umap1);
1549 isl_union_map_free(umap2);
1550 isl_union_map_free(data.res);
1551 return NULL((void*)0);
1552}
1553
1554__isl_give isl_union_map *isl_union_map_apply_range(
1555 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1556{
1557 return bin_op(umap1, umap2, &apply_range_entry);
1558}
1559
1560__isl_give isl_union_map *isl_union_map_apply_domain(
1561 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1562{
1563 umap1 = isl_union_map_reverse(umap1);
1564 umap1 = isl_union_map_apply_range(umap1, umap2);
1565 return isl_union_map_reverse(umap1);
1566}
1567
1568__isl_give isl_union_setisl_union_map *isl_union_set_apply(
1569 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_union_map *umap)
1570{
1571 return isl_union_map_apply_range(uset, umap);
1572}
1573
1574static isl_stat map_lex_lt_entry(void **entry, void *user)
1575{
1576 struct isl_union_map_bin_data *data = user;
1577 isl_map *map2 = *entry;
1578
1579 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1580 map2->dim, isl_dim_out))
1581 return isl_stat_ok;
1582
1583 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1584
1585 data->res = isl_union_map_add_map(data->res, map2);
1586
1587 return isl_stat_ok;
1588}
1589
1590__isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1591 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1592{
1593 return bin_op(umap1, umap2, &map_lex_lt_entry);
1594}
1595
1596static isl_stat map_lex_le_entry(void **entry, void *user)
1597{
1598 struct isl_union_map_bin_data *data = user;
1599 isl_map *map2 = *entry;
1600
1601 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1602 map2->dim, isl_dim_out))
1603 return isl_stat_ok;
1604
1605 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1606
1607 data->res = isl_union_map_add_map(data->res, map2);
1608
1609 return isl_stat_ok;
1610}
1611
1612__isl_give isl_union_map *isl_union_map_lex_le_union_map(
1613 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1614{
1615 return bin_op(umap1, umap2, &map_lex_le_entry);
1616}
1617
1618static isl_stat product_entry(void **entry, void *user)
1619{
1620 struct isl_union_map_bin_data *data = user;
1621 isl_map *map2 = *entry;
1622
1623 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1624
1625 data->res = isl_union_map_add_map(data->res, map2);
1626
1627 return isl_stat_ok;
1628}
1629
1630__isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1631 __isl_take isl_union_map *umap2)
1632{
1633 return bin_op(umap1, umap2, &product_entry);
1634}
1635
1636static isl_stat set_product_entry(void **entry, void *user)
1637{
1638 struct isl_union_map_bin_data *data = user;
1639 isl_setisl_map *set2 = *entry;
1640
1641 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1642
1643 data->res = isl_union_set_add_set(data->res, set2);
1644
1645 return isl_stat_ok;
1646}
1647
1648__isl_give isl_union_setisl_union_map *isl_union_set_product(__isl_take isl_union_setisl_union_map *uset1,
1649 __isl_take isl_union_setisl_union_map *uset2)
1650{
1651 return bin_op(uset1, uset2, &set_product_entry);
1652}
1653
1654static isl_stat domain_product_entry(void **entry, void *user)
1655{
1656 struct isl_union_map_bin_data *data = user;
1657 isl_map *map2 = *entry;
1658
1659 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1660 map2->dim, isl_dim_out))
1661 return isl_stat_ok;
1662
1663 map2 = isl_map_domain_product(isl_map_copy(data->map),
1664 isl_map_copy(map2));
1665
1666 data->res = isl_union_map_add_map(data->res, map2);
1667
1668 return isl_stat_ok;
1669}
1670
1671/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1672 */
1673__isl_give isl_union_map *isl_union_map_domain_product(
1674 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1675{
1676 return bin_op(umap1, umap2, &domain_product_entry);
1677}
1678
1679static isl_stat range_product_entry(void **entry, void *user)
1680{
1681 struct isl_union_map_bin_data *data = user;
1682 isl_map *map2 = *entry;
1683
1684 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1685 map2->dim, isl_dim_in))
1686 return isl_stat_ok;
1687
1688 map2 = isl_map_range_product(isl_map_copy(data->map),
1689 isl_map_copy(map2));
1690
1691 data->res = isl_union_map_add_map(data->res, map2);
1692
1693 return isl_stat_ok;
1694}
1695
1696__isl_give isl_union_map *isl_union_map_range_product(
1697 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1698{
1699 return bin_op(umap1, umap2, &range_product_entry);
1700}
1701
1702/* If data->map A -> B and "map2" C -> D have the same range space,
1703 * then add (A, C) -> (B * D) to data->res.
1704 */
1705static isl_stat flat_domain_product_entry(void **entry, void *user)
1706{
1707 struct isl_union_map_bin_data *data = user;
1708 isl_map *map2 = *entry;
1709
1710 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1711 map2->dim, isl_dim_out))
1712 return isl_stat_ok;
1713
1714 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1715 isl_map_copy(map2));
1716
1717 data->res = isl_union_map_add_map(data->res, map2);
1718
1719 return isl_stat_ok;
1720}
1721
1722/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1723 */
1724__isl_give isl_union_map *isl_union_map_flat_domain_product(
1725 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1726{
1727 return bin_op(umap1, umap2, &flat_domain_product_entry);
1728}
1729
1730static isl_stat flat_range_product_entry(void **entry, void *user)
1731{
1732 struct isl_union_map_bin_data *data = user;
1733 isl_map *map2 = *entry;
1734
1735 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1736 map2->dim, isl_dim_in))
1737 return isl_stat_ok;
1738
1739 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1740 isl_map_copy(map2));
1741
1742 data->res = isl_union_map_add_map(data->res, map2);
1743
1744 return isl_stat_ok;
1745}
1746
1747__isl_give isl_union_map *isl_union_map_flat_range_product(
1748 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1749{
1750 return bin_op(umap1, umap2, &flat_range_product_entry);
1751}
1752
1753/* Data structure that specifies how un_op should modify
1754 * the maps in the union map.
1755 *
1756 * If "inplace" is set, then the maps in the input union map
1757 * are modified in place. This means that "fn_map" should not
1758 * change the meaning of the map or that the union map only
1759 * has a single reference.
1760 * If "total" is set, then all maps need to be modified and
1761 * the results need to live in the same space.
1762 * Otherwise, a new union map is constructed to store the results.
1763 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1764 * are taken into account. "filter_user" is passed as the second argument
1765 * to "filter". No filter can be set if "inplace" or
1766 * "total" is set.
1767 * Exactly one of "fn_map" or "fn_map2" should be set, specifying
1768 * how the maps (selected by "filter") should be transformed.
1769 * If "fn_map2" is set, then "fn_map2_user" is passed as the second argument.
1770 */
1771struct isl_un_op_control {
1772 int inplace;
1773 int total;
1774 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1775 void *filter_user;
1776 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1777 __isl_give isl_map *(*fn_map2)(__isl_take isl_map *map, void *user);
1778 void *fn_map2_user;
1779};
1780
1781/* Data structure for wrapping the data for un_op_filter_drop_user.
1782 * "filter" is the function that is being wrapped.
1783 */
1784struct isl_un_op_drop_user_data {
1785 isl_bool (*filter)(__isl_keep isl_map *map);
1786};
1787
1788/* Wrapper for isl_un_op_control filters that do not require
1789 * a second argument.
1790 * Simply call data->filter without the second argument.
1791 */
1792static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1793{
1794 struct isl_un_op_drop_user_data *data = user;
1795 return data->filter(map);
1796}
1797
1798/* Internal data structure for "un_op".
1799 * "control" specifies how the maps in the union map should be modified.
1800 * "res" collects the results.
1801 */
1802struct isl_union_map_un_data {
1803 struct isl_un_op_control *control;
1804 isl_union_map *res;
1805};
1806
1807/* isl_hash_table_foreach callback for un_op.
1808 * Handle the map that "entry" points to.
1809 *
1810 * If control->filter is set, then check if this map satisfies the filter.
1811 * If so (or if control->filter is not set), modify the map
1812 * by calling control->fn_map or control->fn_map2 and
1813 * either add the result to data->res or
1814 * replace the original entry by the result (if control->inplace is set).
1815 */
1816static isl_stat un_entry(void **entry, void *user)
1817{
1818 struct isl_union_map_un_data *data = user;
1819 struct isl_un_op_control *control = data->control;
1820 isl_map *map = *entry;
1821
1822 if (control->filter) {
1823 isl_bool ok;
1824
1825 ok = control->filter(map, control->filter_user);
1826 if (ok < 0)
1827 return isl_stat_error;
1828 if (!ok)
1829 return isl_stat_ok;
1830 }
1831
1832 map = isl_map_copy(map);
1833 if (control->fn_map2 != NULL((void*)0))
1834 map = control->fn_map2(map, control->fn_map2_user);
1835 else
1836 map = control->fn_map(map);
1837 if (!map)
1838 return isl_stat_error;
1839 if (control->inplace) {
1840 isl_map_free(*entry);
1841 *entry = map;
1842 } else {
1843 data->res = isl_union_map_add_map(data->res, map);
1844 if (!data->res)
1845 return isl_stat_error;
1846 }
1847
1848 return isl_stat_ok;
1849}
1850
1851/* Modify the maps in "umap" based on "control".
1852 * If control->inplace is set, then modify the maps in "umap" in-place.
1853 * Otherwise, create a new union map to hold the results.
1854 * If control->total is set, then perform an inplace computation
1855 * if "umap" is only referenced once. Otherwise, create a new union map
1856 * to store the results.
1857 */
1858static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1859 struct isl_un_op_control *control)
1860{
1861 struct isl_union_map_un_data data = { control };
1862
1863 if (!umap)
1864 return NULL((void*)0);
1865 if (!!control->fn_map == !!control->fn_map2)
1866 isl_die(isl_union_map_get_ctx(umap), isl_error_internal,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_internal
, "exactly one mapping function should be specified", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1868); return isl_union_map_free(umap); } while (0)
1867 "exactly one mapping function should be specified",do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_internal
, "exactly one mapping function should be specified", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1868); return isl_union_map_free(umap); } while (0)
1868 return isl_union_map_free(umap))do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_internal
, "exactly one mapping function should be specified", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1868); return isl_union_map_free(umap); } while (0)
;
1869 if ((control->inplace || control->total) && control->filter)
1870 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1872); return isl_union_map_free(umap); } while (0)
1871 "inplace/total modification cannot be filtered",do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1872); return isl_union_map_free(umap); } while (0)
1872 return isl_union_map_free(umap))do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 1872); return isl_union_map_free(umap); } while (0)
;
1873
1874 if (control->total && umap->ref == 1)
1875 control->inplace = 1;
1876 if (control->inplace) {
1877 data.res = umap;
1878 } else {
1879 isl_space *space;
1880
1881 space = isl_union_map_get_space(umap);
1882 data.res = isl_union_map_alloc(space, umap->table.n);
1883 }
1884 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1885 &umap->table, &un_entry, &data) < 0)
1886 data.res = isl_union_map_free(data.res);
1887
1888 if (control->inplace)
1889 return data.res;
1890 isl_union_map_free(umap);
1891 return data.res;
1892}
1893
1894__isl_give isl_union_map *isl_union_map_from_range(
1895 __isl_take isl_union_setisl_union_map *uset)
1896{
1897 struct isl_un_op_control control = {
1898 .fn_map = &isl_map_from_range,
1899 };
1900 return un_op(uset, &control);
1901}
1902
1903__isl_give isl_union_map *isl_union_map_from_domain(
1904 __isl_take isl_union_setisl_union_map *uset)
1905{
1906 return isl_union_map_reverse(isl_union_map_from_range(uset));
1907}
1908
1909__isl_give isl_union_map *isl_union_map_from_domain_and_range(
1910 __isl_take isl_union_setisl_union_map *domain, __isl_take isl_union_setisl_union_map *range)
1911{
1912 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1913 isl_union_map_from_range(range));
1914}
1915
1916/* Modify the maps in "umap" by applying "fn" on them.
1917 * "fn" should apply to all maps in "umap" and should not modify the space.
1918 */
1919static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1920 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1921{
1922 struct isl_un_op_control control = {
1923 .total = 1,
1924 .fn_map = fn,
1925 };
1926
1927 return un_op(umap, &control);
1928}
1929
1930/* Compute the affine hull of "map" and return the result as an isl_map.
1931 */
1932static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1933{
1934 return isl_map_from_basic_map(isl_map_affine_hull(map));
1935}
1936
1937__isl_give isl_union_map *isl_union_map_affine_hull(
1938 __isl_take isl_union_map *umap)
1939{
1940 return total(umap, &isl_map_affine_hull_map);
1941}
1942
1943__isl_give isl_union_setisl_union_map *isl_union_set_affine_hull(
1944 __isl_take isl_union_setisl_union_map *uset)
1945{
1946 return isl_union_map_affine_hull(uset);
1947}
1948
1949/* Wrapper around isl_set_combined_lineality_space
1950 * that returns the combined lineality space in the form of an isl_set
1951 * instead of an isl_basic_set.
1952 */
1953static __isl_give isl_setisl_map *combined_lineality_space(__isl_take isl_setisl_map *set)
1954{
1955 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1956}
1957
1958/* For each set in "uset", compute the (linear) hull
1959 * of the lineality spaces of its basic sets and
1960 * collect and return the results.
1961 */
1962__isl_give isl_union_setisl_union_map *isl_union_set_combined_lineality_space(
1963 __isl_take isl_union_setisl_union_map *uset)
1964{
1965 struct isl_un_op_control control = {
1966 .fn_map = &combined_lineality_space,
1967 };
1968 return un_op(uset, &control);
1969}
1970
1971/* Compute the polyhedral hull of "map" and return the result as an isl_map.
1972 */
1973static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1974{
1975 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1976}
1977
1978__isl_give isl_union_map *isl_union_map_polyhedral_hull(
1979 __isl_take isl_union_map *umap)
1980{
1981 return total(umap, &isl_map_polyhedral_hull_map);
1982}
1983
1984__isl_give isl_union_setisl_union_map *isl_union_set_polyhedral_hull(
1985 __isl_take isl_union_setisl_union_map *uset)
1986{
1987 return isl_union_map_polyhedral_hull(uset);
1988}
1989
1990/* Compute a superset of the convex hull of "map" that is described
1991 * by only translates of the constraints in the constituents of "map" and
1992 * return the result as an isl_map.
1993 */
1994static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1995{
1996 return isl_map_from_basic_map(isl_map_simple_hull(map));
1997}
1998
1999__isl_give isl_union_map *isl_union_map_simple_hull(
2000 __isl_take isl_union_map *umap)
2001{
2002 return total(umap, &isl_map_simple_hull_map);
2003}
2004
2005__isl_give isl_union_setisl_union_map *isl_union_set_simple_hull(
2006 __isl_take isl_union_setisl_union_map *uset)
2007{
2008 return isl_union_map_simple_hull(uset);
2009}
2010
2011static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
2012 __isl_give isl_map *(*fn)(__isl_take isl_map *))
2013{
2014 struct isl_un_op_control control = {
2015 .inplace = 1,
2016 .fn_map = fn,
2017 };
2018
2019 return un_op(umap, &control);
2020}
2021
2022/* Remove redundant constraints in each of the basic maps of "umap".
2023 * Since removing redundant constraints does not change the meaning
2024 * or the space, the operation can be performed in-place.
2025 */
2026__isl_give isl_union_map *isl_union_map_remove_redundancies(
2027 __isl_take isl_union_map *umap)
2028{
2029 return inplace(umap, &isl_map_remove_redundancies);
2030}
2031
2032/* Remove redundant constraints in each of the basic sets of "uset".
2033 */
2034__isl_give isl_union_setisl_union_map *isl_union_set_remove_redundancies(
2035 __isl_take isl_union_setisl_union_map *uset)
2036{
2037 return isl_union_map_remove_redundancies(uset);
2038}
2039
2040__isl_give isl_union_map *isl_union_map_coalesce(
2041 __isl_take isl_union_map *umap)
2042{
2043 return inplace(umap, &isl_map_coalesce);
2044}
2045
2046__isl_give isl_union_setisl_union_map *isl_union_set_coalesce(
2047 __isl_take isl_union_setisl_union_map *uset)
2048{
2049 return isl_union_map_coalesce(uset);
2050}
2051
2052__isl_give isl_union_map *isl_union_map_detect_equalities(
2053 __isl_take isl_union_map *umap)
2054{
2055 return inplace(umap, &isl_map_detect_equalities);
2056}
2057
2058__isl_give isl_union_setisl_union_map *isl_union_set_detect_equalities(
2059 __isl_take isl_union_setisl_union_map *uset)
2060{
2061 return isl_union_map_detect_equalities(uset);
2062}
2063
2064__isl_give isl_union_map *isl_union_map_compute_divs(
2065 __isl_take isl_union_map *umap)
2066{
2067 return inplace(umap, &isl_map_compute_divs);
2068}
2069
2070__isl_give isl_union_setisl_union_map *isl_union_set_compute_divs(
2071 __isl_take isl_union_setisl_union_map *uset)
2072{
2073 return isl_union_map_compute_divs(uset);
2074}
2075
2076__isl_give isl_union_map *isl_union_map_lexmin(
2077 __isl_take isl_union_map *umap)
2078{
2079 return total(umap, &isl_map_lexmin);
2080}
2081
2082__isl_give isl_union_setisl_union_map *isl_union_set_lexmin(
2083 __isl_take isl_union_setisl_union_map *uset)
2084{
2085 return isl_union_map_lexmin(uset);
2086}
2087
2088__isl_give isl_union_map *isl_union_map_lexmax(
2089 __isl_take isl_union_map *umap)
2090{
2091 return total(umap, &isl_map_lexmax);
2092}
2093
2094__isl_give isl_union_setisl_union_map *isl_union_set_lexmax(
2095 __isl_take isl_union_setisl_union_map *uset)
2096{
2097 return isl_union_map_lexmax(uset);
2098}
2099
2100/* Return the universe in the space of "map".
2101 */
2102static __isl_give isl_map *universe(__isl_take isl_map *map)
2103{
2104 isl_space *space;
2105
2106 space = isl_map_get_space(map);
2107 isl_map_free(map);
2108 return isl_map_universe(space);
2109}
2110
2111__isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
2112{
2113 struct isl_un_op_control control = {
2114 .fn_map = &universe,
2115 };
2116 return un_op(umap, &control);
2117}
2118
2119__isl_give isl_union_setisl_union_map *isl_union_set_universe(__isl_take isl_union_setisl_union_map *uset)
2120{
2121 return isl_union_map_universe(uset);
2122}
2123
2124__isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
2125{
2126 struct isl_un_op_control control = {
2127 .fn_map = &isl_map_reverse,
2128 };
2129 return un_op(umap, &control);
2130}
2131
2132/* Given a union map, take the maps of the form A -> (B -> C) and
2133 * return the union of the corresponding maps A -> (C -> B).
2134 */
2135__isl_give isl_union_map *isl_union_map_range_reverse(
2136 __isl_take isl_union_map *umap)
2137{
2138 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2139 struct isl_un_op_control control = {
2140 .filter = &un_op_filter_drop_user,
2141 .filter_user = &data,
2142 .fn_map = &isl_map_range_reverse,
2143 };
2144 return un_op(umap, &control);
2145}
2146
2147/* Compute the parameter domain of the given union map.
2148 */
2149__isl_give isl_setisl_map *isl_union_map_params(__isl_take isl_union_map *umap)
2150{
2151 struct isl_un_op_control control = {
2152 .fn_map = &isl_map_params,
2153 };
2154 int empty;
2155
2156 empty = isl_union_map_is_empty(umap);
2157 if (empty < 0)
2158 goto error;
2159 if (empty) {
2160 isl_space *space;
2161 space = isl_union_map_get_space(umap);
2162 isl_union_map_free(umap);
2163 return isl_set_empty(space);
2164 }
2165 return isl_set_from_union_set(un_op(umap, &control));
2166error:
2167 isl_union_map_free(umap);
2168 return NULL((void*)0);
2169}
2170
2171/* Compute the parameter domain of the given union set.
2172 */
2173__isl_give isl_setisl_map *isl_union_set_params(__isl_take isl_union_setisl_union_map *uset)
2174{
2175 return isl_union_map_params(uset);
2176}
2177
2178__isl_give isl_union_setisl_union_map *isl_union_map_domain(__isl_take isl_union_map *umap)
2179{
2180 struct isl_un_op_control control = {
2181 .fn_map = &isl_map_domain,
2182 };
2183 return un_op(umap, &control);
2184}
2185
2186__isl_give isl_union_setisl_union_map *isl_union_map_range(__isl_take isl_union_map *umap)
2187{
2188 struct isl_un_op_control control = {
2189 .fn_map = &isl_map_range,
2190 };
2191 return un_op(umap, &control);
2192}
2193
2194__isl_give isl_union_map *isl_union_map_domain_map(
2195 __isl_take isl_union_map *umap)
2196{
2197 struct isl_un_op_control control = {
2198 .fn_map = &isl_map_domain_map,
2199 };
2200 return un_op(umap, &control);
2201}
2202
2203/* Construct an isl_pw_multi_aff that maps "map" to its domain and
2204 * add the result to "res".
2205 */
2206static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2207{
2208 isl_union_pw_multi_aff **res = user;
2209 isl_multi_aff *ma;
2210 isl_pw_multi_aff *pma;
2211
2212 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2213 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2214 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2215
2216 return *res ? isl_stat_ok : isl_stat_error;
2217
2218}
2219
2220/* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2221 * to its domain.
2222 */
2223__isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2224 __isl_take isl_union_map *umap)
2225{
2226 isl_union_pw_multi_aff *res;
2227
2228 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2229 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2230 res = isl_union_pw_multi_aff_free(res);
2231
2232 isl_union_map_free(umap);
2233 return res;
2234}
2235
2236__isl_give isl_union_map *isl_union_map_range_map(
2237 __isl_take isl_union_map *umap)
2238{
2239 struct isl_un_op_control control = {
2240 .fn_map = &isl_map_range_map,
2241 };
2242 return un_op(umap, &control);
2243}
2244
2245/* Given a collection of wrapped maps of the form A[B -> C],
2246 * return the collection of maps A[B -> C] -> B.
2247 */
2248__isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2249 __isl_take isl_union_setisl_union_map *uset)
2250{
2251 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2252 struct isl_un_op_control control = {
2253 .filter = &un_op_filter_drop_user,
2254 .filter_user = &data,
2255 .fn_map = &isl_set_wrapped_domain_map,
2256 };
2257 return un_op(uset, &control);
2258}
2259
2260/* Does "map" relate elements from the same space?
2261 */
2262static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2263{
2264 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2265 map->dim, isl_dim_out);
2266}
2267
2268__isl_give isl_union_setisl_union_map *isl_union_map_deltas(__isl_take isl_union_map *umap)
2269{
2270 struct isl_un_op_control control = {
2271 .filter = &equal_tuples,
2272 .fn_map = &isl_map_deltas,
2273 };
2274 return un_op(umap, &control);
2275}
2276
2277__isl_give isl_union_map *isl_union_map_deltas_map(
2278 __isl_take isl_union_map *umap)
2279{
2280 struct isl_un_op_control control = {
2281 .filter = &equal_tuples,
2282 .fn_map = &isl_map_deltas_map,
2283 };
2284 return un_op(umap, &control);
2285}
2286
2287__isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_setisl_union_map *uset)
2288{
2289 struct isl_un_op_control control = {
2290 .fn_map = &isl_set_identity,
2291 };
2292 return un_op(uset, &control);
2293}
2294
2295/* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2296 */
2297static isl_stat identity_upma(__isl_take isl_setisl_map *set, void *user)
2298{
2299 isl_union_pw_multi_aff **res = user;
2300 isl_space *space;
2301 isl_pw_multi_aff *pma;
2302
2303 space = isl_space_map_from_set(isl_set_get_space(set));
2304 pma = isl_pw_multi_aff_identity(space);
2305 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2306 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2307
2308 return *res ? isl_stat_ok : isl_stat_error;
2309}
2310
2311/* Return an identity function on "uset" in the form
2312 * of an isl_union_pw_multi_aff.
2313 */
2314__isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2315 __isl_take isl_union_setisl_union_map *uset)
2316{
2317 isl_union_pw_multi_aff *res;
2318
2319 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2320 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2321 res = isl_union_pw_multi_aff_free(res);
2322
2323 isl_union_set_free(uset);
2324 return res;
2325}
2326
2327/* For each map in "umap" of the form [A -> B] -> C,
2328 * construct the map A -> C and collect the results.
2329 */
2330__isl_give isl_union_map *isl_union_map_domain_factor_domain(
2331 __isl_take isl_union_map *umap)
2332{
2333 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2334 struct isl_un_op_control control = {
2335 .filter = &un_op_filter_drop_user,
2336 .filter_user = &data,
2337 .fn_map = &isl_map_domain_factor_domain,
2338 };
2339 return un_op(umap, &control);
2340}
2341
2342/* For each map in "umap" of the form [A -> B] -> C,
2343 * construct the map B -> C and collect the results.
2344 */
2345__isl_give isl_union_map *isl_union_map_domain_factor_range(
2346 __isl_take isl_union_map *umap)
2347{
2348 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2349 struct isl_un_op_control control = {
2350 .filter = &un_op_filter_drop_user,
2351 .filter_user = &data,
2352 .fn_map = &isl_map_domain_factor_range,
2353 };
2354 return un_op(umap, &control);
2355}
2356
2357/* For each map in "umap" of the form A -> [B -> C],
2358 * construct the map A -> B and collect the results.
2359 */
2360__isl_give isl_union_map *isl_union_map_range_factor_domain(
2361 __isl_take isl_union_map *umap)
2362{
2363 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2364 struct isl_un_op_control control = {
2365 .filter = &un_op_filter_drop_user,
2366 .filter_user = &data,
2367 .fn_map = &isl_map_range_factor_domain,
2368 };
2369 return un_op(umap, &control);
2370}
2371
2372/* For each map in "umap" of the form A -> [B -> C],
2373 * construct the map A -> C and collect the results.
2374 */
2375__isl_give isl_union_map *isl_union_map_range_factor_range(
2376 __isl_take isl_union_map *umap)
2377{
2378 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2379 struct isl_un_op_control control = {
2380 .filter = &un_op_filter_drop_user,
2381 .filter_user = &data,
2382 .fn_map = &isl_map_range_factor_range,
2383 };
2384 return un_op(umap, &control);
2385}
2386
2387/* For each map in "umap" of the form [A -> B] -> [C -> D],
2388 * construct the map A -> C and collect the results.
2389 */
2390__isl_give isl_union_map *isl_union_map_factor_domain(
2391 __isl_take isl_union_map *umap)
2392{
2393 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2394 struct isl_un_op_control control = {
2395 .filter = &un_op_filter_drop_user,
2396 .filter_user = &data,
2397 .fn_map = &isl_map_factor_domain,
2398 };
2399 return un_op(umap, &control);
2400}
2401
2402/* For each map in "umap" of the form [A -> B] -> [C -> D],
2403 * construct the map B -> D and collect the results.
2404 */
2405__isl_give isl_union_map *isl_union_map_factor_range(
2406 __isl_take isl_union_map *umap)
2407{
2408 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2409 struct isl_un_op_control control = {
2410 .filter = &un_op_filter_drop_user,
2411 .filter_user = &data,
2412 .fn_map = &isl_map_factor_range,
2413 };
2414 return un_op(umap, &control);
2415}
2416
2417__isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_setisl_union_map *uset)
2418{
2419 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2420 struct isl_un_op_control control = {
2421 .filter = &un_op_filter_drop_user,
2422 .filter_user = &data,
2423 .fn_map = &isl_set_unwrap,
2424 };
2425 return un_op(uset, &control);
2426}
2427
2428__isl_give isl_union_setisl_union_map *isl_union_map_wrap(__isl_take isl_union_map *umap)
2429{
2430 struct isl_un_op_control control = {
2431 .fn_map = &isl_map_wrap,
2432 };
2433 return un_op(umap, &control);
2434}
2435
2436struct isl_union_map_is_subset_data {
2437 isl_union_map *umap2;
2438 isl_bool is_subset;
2439};
2440
2441static isl_stat is_subset_entry(void **entry, void *user)
2442{
2443 struct isl_union_map_is_subset_data *data = user;
2444 uint32_t hash;
2445 struct isl_hash_table_entry *entry2;
2446 isl_map *map = *entry;
2447
2448 hash = isl_space_get_hash(map->dim);
2449 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2450 hash, &has_space, map->dim, 0);
2451 if (!entry2)
2452 return isl_stat_error;
2453 if (entry2 == isl_hash_table_entry_none) {
2454 int empty = isl_map_is_empty(map);
2455 if (empty < 0)
2456 return isl_stat_error;
2457 if (empty)
2458 return isl_stat_ok;
2459 data->is_subset = isl_bool_false;
2460 return isl_stat_error;
2461 }
2462
2463 data->is_subset = isl_map_is_subset(map, entry2->data);
2464 if (data->is_subset < 0 || !data->is_subset)
2465 return isl_stat_error;
2466
2467 return isl_stat_ok;
2468}
2469
2470isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2471 __isl_keep isl_union_map *umap2)
2472{
2473 struct isl_union_map_is_subset_data data = { NULL((void*)0), isl_bool_true };
2474
2475 umap1 = isl_union_map_copy(umap1);
2476 umap2 = isl_union_map_copy(umap2);
2477 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2478 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2479
2480 if (!umap1 || !umap2)
2481 goto error;
2482
2483 data.umap2 = umap2;
2484 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2485 &is_subset_entry, &data) < 0 &&
2486 data.is_subset)
2487 goto error;
2488
2489 isl_union_map_free(umap1);
2490 isl_union_map_free(umap2);
2491
2492 return data.is_subset;
2493error:
2494 isl_union_map_free(umap1);
2495 isl_union_map_free(umap2);
2496 return isl_bool_error;
2497}
2498
2499isl_bool isl_union_set_is_subset(__isl_keep isl_union_setisl_union_map *uset1,
2500 __isl_keep isl_union_setisl_union_map *uset2)
2501{
2502 return isl_union_map_is_subset(uset1, uset2);
2503}
2504
2505isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2506 __isl_keep isl_union_map *umap2)
2507{
2508 isl_bool is_subset;
2509
2510 if (!umap1 || !umap2)
2511 return isl_bool_error;
2512 is_subset = isl_union_map_is_subset(umap1, umap2);
2513 if (is_subset != isl_bool_true)
2514 return is_subset;
2515 is_subset = isl_union_map_is_subset(umap2, umap1);
2516 return is_subset;
2517}
2518
2519isl_bool isl_union_set_is_equal(__isl_keep isl_union_setisl_union_map *uset1,
2520 __isl_keep isl_union_setisl_union_map *uset2)
2521{
2522 return isl_union_map_is_equal(uset1, uset2);
2523}
2524
2525isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2526 __isl_keep isl_union_map *umap2)
2527{
2528 isl_bool is_subset;
2529
2530 if (!umap1 || !umap2)
2531 return isl_bool_error;
2532 is_subset = isl_union_map_is_subset(umap1, umap2);
2533 if (is_subset != isl_bool_true)
2534 return is_subset;
2535 is_subset = isl_union_map_is_subset(umap2, umap1);
2536 return isl_bool_not(is_subset);
2537}
2538
2539isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_setisl_union_map *uset1,
2540 __isl_keep isl_union_setisl_union_map *uset2)
2541{
2542 return isl_union_map_is_strict_subset(uset1, uset2);
2543}
2544
2545/* Internal data structure for isl_union_map_is_disjoint.
2546 * umap2 is the union map with which we are comparing.
2547 * is_disjoint is initialized to 1 and is set to 0 as soon
2548 * as the union maps turn out not to be disjoint.
2549 */
2550struct isl_union_map_is_disjoint_data {
2551 isl_union_map *umap2;
2552 isl_bool is_disjoint;
2553};
2554
2555/* Check if "map" is disjoint from data->umap2 and abort
2556 * the search if it is not.
2557 */
2558static isl_stat is_disjoint_entry(void **entry, void *user)
2559{
2560 struct isl_union_map_is_disjoint_data *data = user;
2561 uint32_t hash;
2562 struct isl_hash_table_entry *entry2;
2563 isl_map *map = *entry;
2564
2565 hash = isl_space_get_hash(map->dim);
2566 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2567 hash, &has_space, map->dim, 0);
2568 if (!entry2)
2569 return isl_stat_error;
2570 if (entry2 == isl_hash_table_entry_none)
2571 return isl_stat_ok;
2572
2573 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2574 if (data->is_disjoint < 0 || !data->is_disjoint)
2575 return isl_stat_error;
2576
2577 return isl_stat_ok;
2578}
2579
2580/* Are "umap1" and "umap2" disjoint?
2581 */
2582isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2583 __isl_keep isl_union_map *umap2)
2584{
2585 struct isl_union_map_is_disjoint_data data = { NULL((void*)0), isl_bool_true };
2586
2587 umap1 = isl_union_map_copy(umap1);
2588 umap2 = isl_union_map_copy(umap2);
2589 umap1 = isl_union_map_align_params(umap1,
2590 isl_union_map_get_space(umap2));
2591 umap2 = isl_union_map_align_params(umap2,
2592 isl_union_map_get_space(umap1));
2593
2594 if (!umap1 || !umap2)
2595 goto error;
2596
2597 data.umap2 = umap2;
2598 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2599 &is_disjoint_entry, &data) < 0 &&
2600 data.is_disjoint)
2601 goto error;
2602
2603 isl_union_map_free(umap1);
2604 isl_union_map_free(umap2);
2605
2606 return data.is_disjoint;
2607error:
2608 isl_union_map_free(umap1);
2609 isl_union_map_free(umap2);
2610 return isl_bool_error;
2611}
2612
2613/* Are "uset1" and "uset2" disjoint?
2614 */
2615isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_setisl_union_map *uset1,
2616 __isl_keep isl_union_setisl_union_map *uset2)
2617{
2618 return isl_union_map_is_disjoint(uset1, uset2);
2619}
2620
2621static isl_stat sample_entry(void **entry, void *user)
2622{
2623 isl_basic_map **sample = (isl_basic_map **)user;
2624 isl_map *map = *entry;
2625
2626 *sample = isl_map_sample(isl_map_copy(map));
2627 if (!*sample)
2628 return isl_stat_error;
2629 if (!isl_basic_map_plain_is_empty(*sample))
2630 return isl_stat_error;
2631 return isl_stat_ok;
2632}
2633
2634__isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2635{
2636 isl_basic_map *sample = NULL((void*)0);
2637
2638 if (!umap)
2639 return NULL((void*)0);
2640
2641 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2642 &sample_entry, &sample) < 0 &&
2643 !sample)
2644 goto error;
2645
2646 if (!sample)
2647 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2648
2649 isl_union_map_free(umap);
2650
2651 return sample;
2652error:
2653 isl_union_map_free(umap);
2654 return NULL((void*)0);
2655}
2656
2657__isl_give isl_basic_setisl_basic_map *isl_union_set_sample(__isl_take isl_union_setisl_union_map *uset)
2658{
2659 return bset_from_bmap(isl_union_map_sample(uset));
2660}
2661
2662/* Return an element in "uset" in the form of an isl_point.
2663 * Return a void isl_point if "uset" is empty.
2664 */
2665__isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_setisl_union_map *uset)
2666{
2667 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2668}
2669
2670struct isl_forall_data {
2671 isl_bool res;
2672 isl_bool (*fn)(__isl_keep isl_map *map);
2673};
2674
2675static isl_stat forall_entry(void **entry, void *user)
2676{
2677 struct isl_forall_data *data = user;
2678 isl_map *map = *entry;
2679
2680 data->res = data->fn(map);
2681 if (data->res < 0)
2682 return isl_stat_error;
2683
2684 if (!data->res)
2685 return isl_stat_error;
2686
2687 return isl_stat_ok;
2688}
2689
2690static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2691 isl_bool (*fn)(__isl_keep isl_map *map))
2692{
2693 struct isl_forall_data data = { isl_bool_true, fn };
2694
2695 if (!umap)
2696 return isl_bool_error;
2697
2698 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2699 &forall_entry, &data) < 0 && data.res)
2700 return isl_bool_error;
2701
2702 return data.res;
2703}
2704
2705struct isl_forall_user_data {
2706 isl_bool res;
2707 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2708 void *user;
2709};
2710
2711static isl_stat forall_user_entry(void **entry, void *user)
2712{
2713 struct isl_forall_user_data *data = user;
2714 isl_map *map = *entry;
2715
2716 data->res = data->fn(map, data->user);
2717 if (data->res < 0)
2718 return isl_stat_error;
2719
2720 if (!data->res)
2721 return isl_stat_error;
2722
2723 return isl_stat_ok;
2724}
2725
2726/* Check if fn(map, user) returns true for all maps "map" in umap.
2727 */
2728static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2729 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2730{
2731 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2732
2733 if (!umap)
2734 return isl_bool_error;
2735
2736 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2737 &forall_user_entry, &data) < 0 && data.res)
2738 return isl_bool_error;
2739
2740 return data.res;
2741}
2742
2743/* Is "umap" obviously empty?
2744 */
2745isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2746{
2747 isl_size n;
2748
2749 n = isl_union_map_n_map(umap);
2750 if (n < 0)
2751 return isl_bool_error;
2752 return n == 0;
2753}
2754
2755isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2756{
2757 return union_map_forall(umap, &isl_map_is_empty);
2758}
2759
2760isl_bool isl_union_set_is_empty(__isl_keep isl_union_setisl_union_map *uset)
2761{
2762 return isl_union_map_is_empty(uset);
2763}
2764
2765static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2766{
2767 isl_bool is_subset;
2768 isl_space *dim;
2769 isl_map *id;
2770
2771 if (!map)
2772 return isl_bool_error;
2773
2774 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2775 map->dim, isl_dim_out))
2776 return isl_bool_false;
2777
2778 dim = isl_map_get_space(map);
2779 id = isl_map_identity(dim);
2780
2781 is_subset = isl_map_is_subset(map, id);
2782
2783 isl_map_free(id);
2784
2785 return is_subset;
2786}
2787
2788/* Given an isl_union_map that consists of a single map, check
2789 * if it is single-valued.
2790 */
2791static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2792{
2793 isl_map *map;
2794 isl_bool sv;
2795
2796 umap = isl_union_map_copy(umap);
2797 map = isl_map_from_union_map(umap);
2798 sv = isl_map_is_single_valued(map);
2799 isl_map_free(map);
2800
2801 return sv;
2802}
2803
2804/* Internal data structure for single_valued_on_domain.
2805 *
2806 * "umap" is the union map to be tested.
2807 * "sv" is set to 1 as long as "umap" may still be single-valued.
2808 */
2809struct isl_union_map_is_sv_data {
2810 isl_union_map *umap;
2811 isl_bool sv;
2812};
2813
2814/* Check if the data->umap is single-valued on "set".
2815 *
2816 * If data->umap consists of a single map on "set", then test it
2817 * as an isl_map.
2818 *
2819 * Otherwise, compute
2820 *
2821 * M \circ M^-1
2822 *
2823 * check if the result is a subset of the identity mapping and
2824 * store the result in data->sv.
2825 *
2826 * Terminate as soon as data->umap has been determined not to
2827 * be single-valued.
2828 */
2829static isl_stat single_valued_on_domain(__isl_take isl_setisl_map *set, void *user)
2830{
2831 struct isl_union_map_is_sv_data *data = user;
2832 isl_union_map *umap, *test;
2833 isl_size n;
2834
2835 umap = isl_union_map_copy(data->umap);
2836 umap = isl_union_map_intersect_domain(umap,
2837 isl_union_set_from_set(set));
2838
2839 n = isl_union_map_n_map(umap);
2840 if (n < 0) {
2841 data->sv = isl_bool_error;
2842 } else if (n == 1) {
2843 data->sv = single_map_is_single_valued(umap);
2844 isl_union_map_free(umap);
2845 } else {
2846 test = isl_union_map_reverse(isl_union_map_copy(umap));
2847 test = isl_union_map_apply_range(test, umap);
2848
2849 data->sv = union_map_forall(test, &is_subset_of_identity);
2850
2851 isl_union_map_free(test);
2852 }
2853
2854 if (data->sv < 0 || !data->sv)
2855 return isl_stat_error;
2856 return isl_stat_ok;
2857}
2858
2859/* Check if the given map is single-valued.
2860 *
2861 * If the union map consists of a single map, then test it as an isl_map.
2862 * Otherwise, check if the union map is single-valued on each of its
2863 * domain spaces.
2864 */
2865isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2866{
2867 isl_union_map *universe;
2868 isl_union_setisl_union_map *domain;
2869 struct isl_union_map_is_sv_data data;
2870 isl_size n;
2871
2872 n = isl_union_map_n_map(umap);
2873 if (n < 0)
2874 return isl_bool_error;
2875 if (n == 1)
2876 return single_map_is_single_valued(umap);
2877
2878 universe = isl_union_map_universe(isl_union_map_copy(umap));
2879 domain = isl_union_map_domain(universe);
2880
2881 data.sv = isl_bool_true;
2882 data.umap = umap;
2883 if (isl_union_set_foreach_set(domain,
2884 &single_valued_on_domain, &data) < 0 && data.sv)
2885 data.sv = isl_bool_error;
2886 isl_union_set_free(domain);
2887
2888 return data.sv;
2889}
2890
2891isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2892{
2893 isl_bool in;
2894
2895 umap = isl_union_map_copy(umap);
2896 umap = isl_union_map_reverse(umap);
2897 in = isl_union_map_is_single_valued(umap);
2898 isl_union_map_free(umap);
2899
2900 return in;
2901}
2902
2903/* Is "map" obviously not an identity relation because
2904 * it maps elements from one space to another space?
2905 * Update *non_identity accordingly.
2906 *
2907 * In particular, if the domain and range spaces are the same,
2908 * then the map is not considered to obviously not be an identity relation.
2909 * Otherwise, the map is considered to obviously not be an identity relation
2910 * if it is is non-empty.
2911 *
2912 * If "map" is determined to obviously not be an identity relation,
2913 * then the search is aborted.
2914 */
2915static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2916{
2917 isl_bool *non_identity = user;
2918 isl_bool equal;
2919 isl_space *space;
2920
2921 space = isl_map_get_space(map);
2922 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2923 if (equal >= 0 && !equal)
2924 *non_identity = isl_bool_not(isl_map_is_empty(map));
2925 else
2926 *non_identity = isl_bool_not(equal);
2927 isl_space_free(space);
2928 isl_map_free(map);
2929
2930 if (*non_identity < 0 || *non_identity)
2931 return isl_stat_error;
2932
2933 return isl_stat_ok;
2934}
2935
2936/* Is "umap" obviously not an identity relation because
2937 * it maps elements from one space to another space?
2938 *
2939 * As soon as a map has been found that maps elements to a different space,
2940 * non_identity is changed and the search is aborted.
2941 */
2942static isl_bool isl_union_map_plain_is_not_identity(
2943 __isl_keep isl_union_map *umap)
2944{
2945 isl_bool non_identity;
2946
2947 non_identity = isl_bool_false;
2948 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2949 &non_identity) < 0 &&
2950 non_identity == isl_bool_false)
2951 return isl_bool_error;
2952
2953 return non_identity;
2954}
2955
2956/* Does "map" only map elements to themselves?
2957 * Update *identity accordingly.
2958 *
2959 * If "map" is determined not to be an identity relation,
2960 * then the search is aborted.
2961 */
2962static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2963{
2964 isl_bool *identity = user;
2965
2966 *identity = isl_map_is_identity(map);
2967 isl_map_free(map);
2968
2969 if (*identity < 0 || !*identity)
2970 return isl_stat_error;
2971
2972 return isl_stat_ok;
2973}
2974
2975/* Does "umap" only map elements to themselves?
2976 *
2977 * First check if there are any maps that map elements to different spaces.
2978 * If not, then check that all the maps (between identical spaces)
2979 * are identity relations.
2980 */
2981isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2982{
2983 isl_bool non_identity;
2984 isl_bool identity;
2985
2986 non_identity = isl_union_map_plain_is_not_identity(umap);
2987 if (non_identity < 0 || non_identity)
2988 return isl_bool_not(non_identity);
2989
2990 identity = isl_bool_true;
2991 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2992 identity == isl_bool_true)
2993 return isl_bool_error;
2994
2995 return identity;
2996}
2997
2998/* Represents a map that has a fixed value (v) for one of its
2999 * range dimensions.
3000 * The map in this structure is not reference counted, so it
3001 * is only valid while the isl_union_map from which it was
3002 * obtained is still alive.
3003 */
3004struct isl_fixed_map {
3005 isl_int v;
3006 isl_map *map;
3007};
3008
3009static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
3010 int n)
3011{
3012 int i;
3013 struct isl_fixed_map *v;
3014
3015 v = isl_calloc_array(ctx, struct isl_fixed_map, n)((struct isl_fixed_map *)isl_calloc_or_die(ctx, n, sizeof(struct
isl_fixed_map)))
;
3016 if (!v)
3017 return NULL((void*)0);
3018 for (i = 0; i < n; ++i)
3019 isl_int_init(v[i].v)isl_sioimath_init((v[i].v));
3020 return v;
3021}
3022
3023static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
3024{
3025 int i;
3026
3027 if (!v)
3028 return;
3029 for (i = 0; i < n; ++i)
3030 isl_int_clear(v[i].v)isl_sioimath_clear((v[i].v));
3031 free(v);
3032}
3033
3034/* Compare the "v" field of two isl_fixed_map structs.
3035 */
3036static int qsort_fixed_map_cmp(const void *p1, const void *p2)
3037{
3038 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
3039 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
3040
3041 return isl_int_cmp(e1->v, e2->v)isl_sioimath_cmp(*(e1->v), *(e2->v));
3042}
3043
3044/* Internal data structure used while checking whether all maps
3045 * in a union_map have a fixed value for a given output dimension.
3046 * v is the list of maps, with the fixed value for the dimension
3047 * n is the number of maps considered so far
3048 * pos is the output dimension under investigation
3049 */
3050struct isl_fixed_dim_data {
3051 struct isl_fixed_map *v;
3052 int n;
3053 int pos;
3054};
3055
3056static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
3057{
3058 struct isl_fixed_dim_data *data = user;
3059
3060 data->v[data->n].map = map;
3061 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
3062 &data->v[data->n++].v);
3063}
3064
3065static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3066 int first, int n_range);
3067
3068/* Given a list of the maps, with their fixed values at output dimension "pos",
3069 * check whether the ranges of the maps form an obvious partition.
3070 *
3071 * We first sort the maps according to their fixed values.
3072 * If all maps have a different value, then we know the ranges form
3073 * a partition.
3074 * Otherwise, we collect the maps with the same fixed value and
3075 * check whether each such collection is obviously injective
3076 * based on later dimensions.
3077 */
3078static int separates(struct isl_fixed_map *v, int n,
3079 __isl_take isl_space *dim, int pos, int n_range)
3080{
3081 int i;
3082
3083 if (!v)
3084 goto error;
3085
3086 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
3087
3088 for (i = 0; i + 1 < n; ++i) {
3089 int j, k;
3090 isl_union_map *part;
3091 int injective;
3092
3093 for (j = i + 1; j < n; ++j)
3094 if (isl_int_ne(v[i].v, v[j].v)(isl_sioimath_cmp(*(v[i].v), *(v[j].v)) != 0))
3095 break;
3096
3097 if (j == i + 1)
3098 continue;
3099
3100 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
3101 for (k = i; k < j; ++k)
3102 part = isl_union_map_add_map(part,
3103 isl_map_copy(v[k].map));
3104
3105 injective = plain_injective_on_range(part, pos + 1, n_range);
3106 if (injective < 0)
3107 goto error;
3108 if (!injective)
3109 break;
3110
3111 i = j - 1;
3112 }
3113
3114 isl_space_free(dim);
3115 free_isl_fixed_map_array(v, n);
3116 return i + 1 >= n;
3117error:
3118 isl_space_free(dim);
3119 free_isl_fixed_map_array(v, n);
3120 return -1;
3121}
3122
3123/* Check whether the maps in umap have obviously distinct ranges.
3124 * In particular, check for an output dimension in the range
3125 * [first,n_range) for which all maps have a fixed value
3126 * and then check if these values, possibly along with fixed values
3127 * at later dimensions, entail distinct ranges.
3128 */
3129static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
3130 int first, int n_range)
3131{
3132 isl_ctx *ctx;
3133 isl_size n;
3134 struct isl_fixed_dim_data data = { NULL((void*)0) };
3135
3136 ctx = isl_union_map_get_ctx(umap);
3137
3138 n = isl_union_map_n_map(umap);
3139 if (n < 0)
3140 goto error;
3141
3142 if (n <= 1) {
3143 isl_union_map_free(umap);
3144 return isl_bool_true;
3145 }
3146
3147 if (first >= n_range) {
3148 isl_union_map_free(umap);
3149 return isl_bool_false;
3150 }
3151
3152 data.v = alloc_isl_fixed_map_array(ctx, n);
3153 if (!data.v)
3154 goto error;
3155
3156 for (data.pos = first; data.pos < n_range; ++data.pos) {
3157 isl_bool fixed;
3158 int injective;
3159 isl_space *dim;
3160
3161 data.n = 0;
3162 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
3163 if (fixed < 0)
3164 goto error;
3165 if (!fixed)
3166 continue;
3167 dim = isl_union_map_get_space(umap);
3168 injective = separates(data.v, n, dim, data.pos, n_range);
3169 isl_union_map_free(umap);
3170 return injective;
3171 }
3172
3173 free_isl_fixed_map_array(data.v, n);
3174 isl_union_map_free(umap);
3175
3176 return isl_bool_false;
3177error:
3178 free_isl_fixed_map_array(data.v, n);
3179 isl_union_map_free(umap);
3180 return isl_bool_error;
3181}
3182
3183/* Check whether the maps in umap that map to subsets of "ran"
3184 * have obviously distinct ranges.
3185 */
3186static isl_bool plain_injective_on_range_wrap(__isl_keep isl_setisl_map *ran,
3187 void *user)
3188{
3189 isl_size dim;
3190 isl_union_map *umap = user;
3191
3192 dim = isl_set_dim(ran, isl_dim_set);
3193 if (dim < 0)
3194 return isl_bool_error;
3195
3196 umap = isl_union_map_copy(umap);
3197 umap = isl_union_map_intersect_range(umap,
3198 isl_union_set_from_set(isl_set_copy(ran)));
3199 return plain_injective_on_range(umap, 0, dim);
3200}
3201
3202/* Check if the given union_map is obviously injective.
3203 *
3204 * In particular, we first check if all individual maps are obviously
3205 * injective and then check if all the ranges of these maps are
3206 * obviously disjoint.
3207 */
3208isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
3209{
3210 isl_bool in;
3211 isl_union_map *univ;
3212 isl_union_setisl_union_map *ran;
3213
3214 in = union_map_forall(umap, &isl_map_plain_is_injective);
3215 if (in < 0)
3216 return isl_bool_error;
3217 if (!in)
3218 return isl_bool_false;
3219
3220 univ = isl_union_map_universe(isl_union_map_copy(umap));
3221 ran = isl_union_map_range(univ);
3222
3223 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3224
3225 isl_union_set_free(ran);
3226
3227 return in;
3228}
3229
3230isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3231{
3232 isl_bool sv;
3233
3234 sv = isl_union_map_is_single_valued(umap);
3235 if (sv < 0 || !sv)
3236 return sv;
3237
3238 return isl_union_map_is_injective(umap);
3239}
3240
3241__isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3242{
3243 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3244 struct isl_un_op_control control = {
3245 .filter = &un_op_filter_drop_user,
3246 .filter_user = &data,
3247 .fn_map = &isl_map_zip,
3248 };
3249 return un_op(umap, &control);
3250}
3251
3252/* Given a union map, take the maps of the form A -> (B -> C) and
3253 * return the union of the corresponding maps (A -> B) -> C.
3254 */
3255__isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3256{
3257 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3258 struct isl_un_op_control control = {
3259 .filter = &un_op_filter_drop_user,
3260 .filter_user = &data,
3261 .fn_map = &isl_map_uncurry,
3262 };
3263 return un_op(umap, &control);
3264}
3265
3266/* Given a union map, take the maps of the form (A -> B) -> C and
3267 * return the union of the corresponding maps A -> (B -> C).
3268 */
3269__isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3270{
3271 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3272 struct isl_un_op_control control = {
3273 .filter = &un_op_filter_drop_user,
3274 .filter_user = &data,
3275 .fn_map = &isl_map_curry,
3276 };
3277 return un_op(umap, &control);
3278}
3279
3280/* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3281 * return the union of the corresponding maps A -> (B -> (C -> D)).
3282 */
3283__isl_give isl_union_map *isl_union_map_range_curry(
3284 __isl_take isl_union_map *umap)
3285{
3286 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3287 struct isl_un_op_control control = {
3288 .filter = &un_op_filter_drop_user,
3289 .filter_user = &data,
3290 .fn_map = &isl_map_range_curry,
3291 };
3292 return un_op(umap, &control);
3293}
3294
3295__isl_give isl_union_setisl_union_map *isl_union_set_lift(__isl_take isl_union_setisl_union_map *uset)
3296{
3297 struct isl_un_op_control control = {
3298 .fn_map = &isl_set_lift,
3299 };
3300 return un_op(uset, &control);
3301}
3302
3303static isl_stat coefficients_entry(void **entry, void *user)
3304{
3305 isl_setisl_map *set = *entry;
3306 isl_union_setisl_union_map **res = user;
3307
3308 set = isl_set_copy(set);
3309 set = isl_set_from_basic_set(isl_set_coefficients(set));
3310 *res = isl_union_set_add_set(*res, set);
3311
3312 return isl_stat_ok;
3313}
3314
3315__isl_give isl_union_setisl_union_map *isl_union_set_coefficients(
3316 __isl_take isl_union_setisl_union_map *uset)
3317{
3318 isl_ctx *ctx;
3319 isl_space *dim;
3320 isl_union_setisl_union_map *res;
3321
3322 if (!uset)
3323 return NULL((void*)0);
3324
3325 ctx = isl_union_set_get_ctx(uset);
3326 dim = isl_space_set_alloc(ctx, 0, 0);
3327 res = isl_union_map_alloc(dim, uset->table.n);
3328 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3329 &coefficients_entry, &res) < 0)
3330 goto error;
3331
3332 isl_union_set_free(uset);
3333 return res;
3334error:
3335 isl_union_set_free(uset);
3336 isl_union_set_free(res);
3337 return NULL((void*)0);
3338}
3339
3340static isl_stat solutions_entry(void **entry, void *user)
3341{
3342 isl_setisl_map *set = *entry;
3343 isl_union_setisl_union_map **res = user;
3344
3345 set = isl_set_copy(set);
3346 set = isl_set_from_basic_set(isl_set_solutions(set));
3347 if (!*res)
3348 *res = isl_union_set_from_set(set);
3349 else
3350 *res = isl_union_set_add_set(*res, set);
3351
3352 if (!*res)
3353 return isl_stat_error;
3354
3355 return isl_stat_ok;
3356}
3357
3358__isl_give isl_union_setisl_union_map *isl_union_set_solutions(
3359 __isl_take isl_union_setisl_union_map *uset)
3360{
3361 isl_union_setisl_union_map *res = NULL((void*)0);
3362
3363 if (!uset)
3364 return NULL((void*)0);
3365
3366 if (uset->table.n == 0) {
3367 res = isl_union_set_empty(isl_union_set_get_space(uset));
3368 isl_union_set_free(uset);
3369 return res;
3370 }
3371
3372 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3373 &solutions_entry, &res) < 0)
3374 goto error;
3375
3376 isl_union_set_free(uset);
3377 return res;
3378error:
3379 isl_union_set_free(uset);
3380 isl_union_set_free(res);
3381 return NULL((void*)0);
3382}
3383
3384/* Is the domain space of "map" equal to "space"?
3385 */
3386static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3387{
3388 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3389 space, isl_dim_out);
3390}
3391
3392/* Is the range space of "map" equal to "space"?
3393 */
3394static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3395{
3396 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3397 space, isl_dim_out);
3398}
3399
3400/* Is the set space of "map" equal to "space"?
3401 */
3402static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3403{
3404 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3405 space, isl_dim_out);
3406}
3407
3408/* Internal data structure for preimage_pw_multi_aff.
3409 *
3410 * "pma" is the function under which the preimage should be taken.
3411 * "space" is the space of "pma".
3412 * "res" collects the results.
3413 * "fn" computes the preimage for a given map.
3414 * "match" returns true if "fn" can be called.
3415 */
3416struct isl_union_map_preimage_data {
3417 isl_space *space;
3418 isl_pw_multi_aff *pma;
3419 isl_union_map *res;
3420 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3421 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3422 __isl_take isl_pw_multi_aff *pma);
3423};
3424
3425/* Call data->fn to compute the preimage of the domain or range of *entry
3426 * under the function represented by data->pma, provided the domain/range
3427 * space of *entry matches the target space of data->pma
3428 * (as given by data->match), and add the result to data->res.
3429 */
3430static isl_stat preimage_entry(void **entry, void *user)
3431{
3432 int m;
3433 isl_map *map = *entry;
3434 struct isl_union_map_preimage_data *data = user;
3435 isl_bool empty;
3436
3437 m = data->match(map, data->space);
3438 if (m < 0)
3439 return isl_stat_error;
3440 if (!m)
3441 return isl_stat_ok;
3442
3443 map = isl_map_copy(map);
3444 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3445
3446 empty = isl_map_is_empty(map);
3447 if (empty < 0 || empty) {
3448 isl_map_free(map);
3449 return empty < 0 ? isl_stat_error : isl_stat_ok;
3450 }
3451
3452 data->res = isl_union_map_add_map(data->res, map);
3453
3454 return isl_stat_ok;
3455}
3456
3457/* Compute the preimage of the domain or range of "umap" under the function
3458 * represented by "pma".
3459 * In other words, plug in "pma" in the domain or range of "umap".
3460 * The function "fn" performs the actual preimage computation on a map,
3461 * while "match" determines to which maps the function should be applied.
3462 */
3463static __isl_give isl_union_map *preimage_pw_multi_aff(
3464 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3465 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3466 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3467 __isl_take isl_pw_multi_aff *pma))
3468{
3469 isl_ctx *ctx;
3470 isl_space *space;
3471 struct isl_union_map_preimage_data data;
3472
3473 umap = isl_union_map_align_params(umap,
3474 isl_pw_multi_aff_get_space(pma));
3475 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3476
3477 if (!umap || !pma)
3478 goto error;
3479
3480 ctx = isl_union_map_get_ctx(umap);
3481 space = isl_union_map_get_space(umap);
3482 data.space = isl_pw_multi_aff_get_space(pma);
3483 data.pma = pma;
3484 data.res = isl_union_map_alloc(space, umap->table.n);
3485 data.match = match;
3486 data.fn = fn;
3487 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3488 &data) < 0)
3489 data.res = isl_union_map_free(data.res);
3490
3491 isl_space_free(data.space);
3492 isl_union_map_free(umap);
3493 isl_pw_multi_aff_free(pma);
3494 return data.res;
3495error:
3496 isl_union_map_free(umap);
3497 isl_pw_multi_aff_free(pma);
3498 return NULL((void*)0);
3499}
3500
3501/* Compute the preimage of the domain of "umap" under the function
3502 * represented by "pma".
3503 * In other words, plug in "pma" in the domain of "umap".
3504 * The result contains maps that live in the same spaces as the maps of "umap"
3505 * with domain space equal to the target space of "pma",
3506 * except that the domain has been replaced by the domain space of "pma".
3507 */
3508__isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3509 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3510{
3511 return preimage_pw_multi_aff(umap, pma, &domain_match,
3512 &isl_map_preimage_domain_pw_multi_aff);
3513}
3514
3515/* Compute the preimage of the range of "umap" under the function
3516 * represented by "pma".
3517 * In other words, plug in "pma" in the range of "umap".
3518 * The result contains maps that live in the same spaces as the maps of "umap"
3519 * with range space equal to the target space of "pma",
3520 * except that the range has been replaced by the domain space of "pma".
3521 */
3522__isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3523 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3524{
3525 return preimage_pw_multi_aff(umap, pma, &range_match,
3526 &isl_map_preimage_range_pw_multi_aff);
3527}
3528
3529/* Compute the preimage of "uset" under the function represented by "pma".
3530 * In other words, plug in "pma" in "uset".
3531 * The result contains sets that live in the same spaces as the sets of "uset"
3532 * with space equal to the target space of "pma",
3533 * except that the space has been replaced by the domain space of "pma".
3534 */
3535__isl_give isl_union_setisl_union_map *isl_union_set_preimage_pw_multi_aff(
3536 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_pw_multi_aff *pma)
3537{
3538 return preimage_pw_multi_aff(uset, pma, &set_match,
3539 &isl_set_preimage_pw_multi_aff);
3540}
3541
3542/* Compute the preimage of the domain of "umap" under the function
3543 * represented by "ma".
3544 * In other words, plug in "ma" in the domain of "umap".
3545 * The result contains maps that live in the same spaces as the maps of "umap"
3546 * with domain space equal to the target space of "ma",
3547 * except that the domain has been replaced by the domain space of "ma".
3548 */
3549__isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3550 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3551{
3552 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3553 isl_pw_multi_aff_from_multi_aff(ma));
3554}
3555
3556/* Compute the preimage of the range of "umap" under the function
3557 * represented by "ma".
3558 * In other words, plug in "ma" in the range of "umap".
3559 * The result contains maps that live in the same spaces as the maps of "umap"
3560 * with range space equal to the target space of "ma",
3561 * except that the range has been replaced by the domain space of "ma".
3562 */
3563__isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3564 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3565{
3566 return isl_union_map_preimage_range_pw_multi_aff(umap,
3567 isl_pw_multi_aff_from_multi_aff(ma));
3568}
3569
3570/* Compute the preimage of "uset" under the function represented by "ma".
3571 * In other words, plug in "ma" in "uset".
3572 * The result contains sets that live in the same spaces as the sets of "uset"
3573 * with space equal to the target space of "ma",
3574 * except that the space has been replaced by the domain space of "ma".
3575 */
3576__isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3577 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_multi_aff *ma)
3578{
3579 return isl_union_set_preimage_pw_multi_aff(uset,
3580 isl_pw_multi_aff_from_multi_aff(ma));
3581}
3582
3583/* Internal data structure for preimage_multi_pw_aff.
3584 *
3585 * "mpa" is the function under which the preimage should be taken.
3586 * "space" is the space of "mpa".
3587 * "res" collects the results.
3588 * "fn" computes the preimage for a given map.
3589 * "match" returns true if "fn" can be called.
3590 */
3591struct isl_union_map_preimage_mpa_data {
3592 isl_space *space;
3593 isl_multi_pw_aff *mpa;
3594 isl_union_map *res;
3595 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3596 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3597 __isl_take isl_multi_pw_aff *mpa);
3598};
3599
3600/* Call data->fn to compute the preimage of the domain or range of *entry
3601 * under the function represented by data->mpa, provided the domain/range
3602 * space of *entry matches the target space of data->mpa
3603 * (as given by data->match), and add the result to data->res.
3604 */
3605static isl_stat preimage_mpa_entry(void **entry, void *user)
3606{
3607 int m;
3608 isl_map *map = *entry;
3609 struct isl_union_map_preimage_mpa_data *data = user;
3610 isl_bool empty;
3611
3612 m = data->match(map, data->space);
3613 if (m < 0)
3614 return isl_stat_error;
3615 if (!m)
3616 return isl_stat_ok;
3617
3618 map = isl_map_copy(map);
3619 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3620
3621 empty = isl_map_is_empty(map);
3622 if (empty < 0 || empty) {
3623 isl_map_free(map);
3624 return empty < 0 ? isl_stat_error : isl_stat_ok;
3625 }
3626
3627 data->res = isl_union_map_add_map(data->res, map);
3628
3629 return isl_stat_ok;
3630}
3631
3632/* Compute the preimage of the domain or range of "umap" under the function
3633 * represented by "mpa".
3634 * In other words, plug in "mpa" in the domain or range of "umap".
3635 * The function "fn" performs the actual preimage computation on a map,
3636 * while "match" determines to which maps the function should be applied.
3637 */
3638static __isl_give isl_union_map *preimage_multi_pw_aff(
3639 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3640 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3641 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3642 __isl_take isl_multi_pw_aff *mpa))
3643{
3644 isl_ctx *ctx;
3645 isl_space *space;
3646 struct isl_union_map_preimage_mpa_data data;
3647
3648 umap = isl_union_map_align_params(umap,
3649 isl_multi_pw_aff_get_space(mpa));
3650 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3651
3652 if (!umap || !mpa)
3653 goto error;
3654
3655 ctx = isl_union_map_get_ctx(umap);
3656 space = isl_union_map_get_space(umap);
3657 data.space = isl_multi_pw_aff_get_space(mpa);
3658 data.mpa = mpa;
3659 data.res = isl_union_map_alloc(space, umap->table.n);
3660 data.match = match;
3661 data.fn = fn;
3662 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3663 &data) < 0)
3664 data.res = isl_union_map_free(data.res);
3665
3666 isl_space_free(data.space);
3667 isl_union_map_free(umap);
3668 isl_multi_pw_aff_free(mpa);
3669 return data.res;
3670error:
3671 isl_union_map_free(umap);
3672 isl_multi_pw_aff_free(mpa);
3673 return NULL((void*)0);
3674}
3675
3676/* Compute the preimage of the domain of "umap" under the function
3677 * represented by "mpa".
3678 * In other words, plug in "mpa" in the domain of "umap".
3679 * The result contains maps that live in the same spaces as the maps of "umap"
3680 * with domain space equal to the target space of "mpa",
3681 * except that the domain has been replaced by the domain space of "mpa".
3682 */
3683__isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3684 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3685{
3686 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3687 &isl_map_preimage_domain_multi_pw_aff);
3688}
3689
3690/* Internal data structure for preimage_upma.
3691 *
3692 * "umap" is the map of which the preimage should be computed.
3693 * "res" collects the results.
3694 * "fn" computes the preimage for a given piecewise multi-affine function.
3695 */
3696struct isl_union_map_preimage_upma_data {
3697 isl_union_map *umap;
3698 isl_union_map *res;
3699 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3700 __isl_take isl_pw_multi_aff *pma);
3701};
3702
3703/* Call data->fn to compute the preimage of the domain or range of data->umap
3704 * under the function represented by pma and add the result to data->res.
3705 */
3706static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3707{
3708 struct isl_union_map_preimage_upma_data *data = user;
3709 isl_union_map *umap;
3710
3711 umap = isl_union_map_copy(data->umap);
3712 umap = data->fn(umap, pma);
3713 data->res = isl_union_map_union(data->res, umap);
3714
3715 return data->res ? isl_stat_ok : isl_stat_error;
3716}
3717
3718/* Compute the preimage of the domain or range of "umap" under the function
3719 * represented by "upma".
3720 * In other words, plug in "upma" in the domain or range of "umap".
3721 * The function "fn" performs the actual preimage computation
3722 * on a piecewise multi-affine function.
3723 */
3724static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3725 __isl_take isl_union_map *umap,
3726 __isl_take isl_union_pw_multi_aff *upma,
3727 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3728 __isl_take isl_pw_multi_aff *pma))
3729{
3730 struct isl_union_map_preimage_upma_data data;
3731
3732 data.umap = umap;
3733 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3734 data.fn = fn;
3735 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3736 &preimage_upma, &data) < 0)
3737 data.res = isl_union_map_free(data.res);
3738
3739 isl_union_map_free(umap);
3740 isl_union_pw_multi_aff_free(upma);
3741
3742 return data.res;
3743}
3744
3745/* Compute the preimage of the domain of "umap" under the function
3746 * represented by "upma".
3747 * In other words, plug in "upma" in the domain of "umap".
3748 * The result contains maps that live in the same spaces as the maps of "umap"
3749 * with domain space equal to one of the target spaces of "upma",
3750 * except that the domain has been replaced by one of the domain spaces that
3751 * correspond to that target space of "upma".
3752 */
3753__isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3754 __isl_take isl_union_map *umap,
3755 __isl_take isl_union_pw_multi_aff *upma)
3756{
3757 return preimage_union_pw_multi_aff(umap, upma,
3758 &isl_union_map_preimage_domain_pw_multi_aff);
3759}
3760
3761/* Compute the preimage of the range of "umap" under the function
3762 * represented by "upma".
3763 * In other words, plug in "upma" in the range of "umap".
3764 * The result contains maps that live in the same spaces as the maps of "umap"
3765 * with range space equal to one of the target spaces of "upma",
3766 * except that the range has been replaced by one of the domain spaces that
3767 * correspond to that target space of "upma".
3768 */
3769__isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3770 __isl_take isl_union_map *umap,
3771 __isl_take isl_union_pw_multi_aff *upma)
3772{
3773 return preimage_union_pw_multi_aff(umap, upma,
3774 &isl_union_map_preimage_range_pw_multi_aff);
3775}
3776
3777/* Compute the preimage of "uset" under the function represented by "upma".
3778 * In other words, plug in "upma" in the range of "uset".
3779 * The result contains sets that live in the same spaces as the sets of "uset"
3780 * with space equal to one of the target spaces of "upma",
3781 * except that the space has been replaced by one of the domain spaces that
3782 * correspond to that target space of "upma".
3783 */
3784__isl_give isl_union_setisl_union_map *isl_union_set_preimage_union_pw_multi_aff(
3785 __isl_take isl_union_setisl_union_map *uset,
3786 __isl_take isl_union_pw_multi_aff *upma)
3787{
3788 return preimage_union_pw_multi_aff(uset, upma,
3789 &isl_union_set_preimage_pw_multi_aff);
3790}
3791
3792/* Reset the user pointer on all identifiers of parameters and tuples
3793 * of the spaces of "umap".
3794 */
3795__isl_give isl_union_map *isl_union_map_reset_user(
3796 __isl_take isl_union_map *umap)
3797{
3798 umap = isl_union_map_cow(umap);
2
Calling 'isl_union_map_cow'
10
Returning from 'isl_union_map_cow'
3799 if (!umap
10.1
'umap' is non-null
)
11
Taking false branch
3800 return NULL((void*)0);
3801 umap->dim = isl_space_reset_user(umap->dim);
12
Value assigned to field 'dim'
3802 if (!umap->dim)
13
Assuming field 'dim' is null
14
Assuming pointer value is null
15
Taking true branch
3803 return isl_union_map_free(umap);
16
Calling 'isl_union_map_free'
3804 return total(umap, &isl_map_reset_user);
3805}
3806
3807/* Reset the user pointer on all identifiers of parameters and tuples
3808 * of the spaces of "uset".
3809 */
3810__isl_give isl_union_setisl_union_map *isl_union_set_reset_user(
3811 __isl_take isl_union_setisl_union_map *uset)
3812{
3813 return isl_union_map_reset_user(uset);
1
Calling 'isl_union_map_reset_user'
3814}
3815
3816/* Remove all existentially quantified variables and integer divisions
3817 * from "umap" using Fourier-Motzkin elimination.
3818 */
3819__isl_give isl_union_map *isl_union_map_remove_divs(
3820 __isl_take isl_union_map *umap)
3821{
3822 return total(umap, &isl_map_remove_divs);
3823}
3824
3825/* Remove all existentially quantified variables and integer divisions
3826 * from "uset" using Fourier-Motzkin elimination.
3827 */
3828__isl_give isl_union_setisl_union_map *isl_union_set_remove_divs(
3829 __isl_take isl_union_setisl_union_map *uset)
3830{
3831 return isl_union_map_remove_divs(uset);
3832}
3833
3834/* Internal data structure for isl_union_map_project_out.
3835 * "type", "first" and "n" are the arguments for the isl_map_project_out
3836 * call.
3837 * "res" collects the results.
3838 */
3839struct isl_union_map_project_out_data {
3840 enum isl_dim_type type;
3841 unsigned first;
3842 unsigned n;
3843
3844 isl_union_map *res;
3845};
3846
3847/* Turn the data->n dimensions of type data->type, starting at data->first
3848 * into existentially quantified variables and add the result to data->res.
3849 */
3850static isl_stat project_out(__isl_take isl_map *map, void *user)
3851{
3852 struct isl_union_map_project_out_data *data = user;
3853
3854 map = isl_map_project_out(map, data->type, data->first, data->n);
3855 data->res = isl_union_map_add_map(data->res, map);
3856
3857 return isl_stat_ok;
3858}
3859
3860/* Turn the "n" dimensions of type "type", starting at "first"
3861 * into existentially quantified variables.
3862 * Since the space of an isl_union_map only contains parameters,
3863 * type is required to be equal to isl_dim_param.
3864 */
3865__isl_give isl_union_map *isl_union_map_project_out(
3866 __isl_take isl_union_map *umap,
3867 enum isl_dim_type type, unsigned first, unsigned n)
3868{
3869 isl_space *space;
3870 struct isl_union_map_project_out_data data = { type, first, n };
3871
3872 if (!umap)
3873 return NULL((void*)0);
3874
3875 if (type != isl_dim_param)
3876 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 3878); return isl_union_map_free(umap); } while (0)
3877 "can only project out parameters",do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 3878); return isl_union_map_free(umap); } while (0)
3878 return isl_union_map_free(umap))do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 3878); return isl_union_map_free(umap); } while (0)
;
3879
3880 space = isl_union_map_get_space(umap);
3881 space = isl_space_drop_dims(space, type, first, n);
3882 data.res = isl_union_map_empty(space);
3883 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3884 data.res = isl_union_map_free(data.res);
3885
3886 isl_union_map_free(umap);
3887
3888 return data.res;
3889}
3890
3891#undef TYPEisl_union_map
3892#define TYPEisl_union_map isl_union_map
3893#include "isl_project_out_all_params_templ.c"
3894
3895/* Turn the "n" dimensions of type "type", starting at "first"
3896 * into existentially quantified variables.
3897 * Since the space of an isl_union_set only contains parameters,
3898 * "type" is required to be equal to isl_dim_param.
3899 */
3900__isl_give isl_union_setisl_union_map *isl_union_set_project_out(
3901 __isl_take isl_union_setisl_union_map *uset,
3902 enum isl_dim_type type, unsigned first, unsigned n)
3903{
3904 return isl_union_map_project_out(uset, type, first, n);
3905}
3906
3907/* Project out all parameters from "uset" by existentially quantifying
3908 * over them.
3909 */
3910__isl_give isl_union_setisl_union_map *isl_union_set_project_out_all_params(
3911 __isl_take isl_union_setisl_union_map *uset)
3912{
3913 return uset_from_umap(
3914 isl_union_map_project_out_all_params(uset_to_umap(uset)));
3915}
3916
3917/* Internal data structure for isl_union_map_involves_dims.
3918 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3919 */
3920struct isl_union_map_involves_dims_data {
3921 unsigned first;
3922 unsigned n;
3923};
3924
3925/* Does "map" _not_ involve the data->n parameters starting at data->first?
3926 */
3927static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3928{
3929 struct isl_union_map_involves_dims_data *data = user;
3930 isl_bool involves;
3931
3932 involves = isl_map_involves_dims(map,
3933 isl_dim_param, data->first, data->n);
3934 return isl_bool_not(involves);
3935}
3936
3937/* Does "umap" involve any of the n parameters starting at first?
3938 * "type" is required to be set to isl_dim_param.
3939 *
3940 * "umap" involves any of those parameters if any of its maps
3941 * involve the parameters. In other words, "umap" does not
3942 * involve any of the parameters if all its maps to not
3943 * involve the parameters.
3944 */
3945isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3946 enum isl_dim_type type, unsigned first, unsigned n)
3947{
3948 struct isl_union_map_involves_dims_data data = { first, n };
3949 isl_bool excludes;
3950
3951 if (type != isl_dim_param)
3952 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 3953); return isl_bool_error; } while (0)
3953 "can only reference parameters", return isl_bool_error)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 3953); return isl_bool_error; } while (0)
;
3954
3955 excludes = union_map_forall_user(umap, &map_excludes, &data);
3956
3957 return isl_bool_not(excludes);
3958}
3959
3960/* Internal data structure for isl_union_map_reset_range_space.
3961 * "range" is the space from which to set the range space.
3962 * "res" collects the results.
3963 */
3964struct isl_union_map_reset_range_space_data {
3965 isl_space *range;
3966 isl_union_map *res;
3967};
3968
3969/* Replace the range space of "map" by the range space of data->range and
3970 * add the result to data->res.
3971 */
3972static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3973{
3974 struct isl_union_map_reset_range_space_data *data = user;
3975 isl_space *space;
3976
3977 space = isl_map_get_space(map);
3978 space = isl_space_domain(space);
3979 space = isl_space_extend_domain_with_range(space,
3980 isl_space_copy(data->range));
3981 map = isl_map_reset_space(map, space);
3982 data->res = isl_union_map_add_map(data->res, map);
3983
3984 return data->res ? isl_stat_ok : isl_stat_error;
3985}
3986
3987/* Replace the range space of all the maps in "umap" by
3988 * the range space of "space".
3989 *
3990 * This assumes that all maps have the same output dimension.
3991 * This function should therefore not be made publicly available.
3992 *
3993 * Since the spaces of the maps change, so do their hash value.
3994 * We therefore need to create a new isl_union_map.
3995 */
3996__isl_give isl_union_map *isl_union_map_reset_range_space(
3997 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3998{
3999 struct isl_union_map_reset_range_space_data data = { space };
4000
4001 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4002 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
4003 data.res = isl_union_map_free(data.res);
4004
4005 isl_space_free(space);
4006 isl_union_map_free(umap);
4007 return data.res;
4008}
4009
4010/* Check that "umap" and "space" have the same number of parameters.
4011 */
4012static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
4013 __isl_keep isl_space *space)
4014{
4015 isl_size dim1, dim2;
4016
4017 dim1 = isl_union_map_dim(umap, isl_dim_param);
4018 dim2 = isl_space_dim(space, isl_dim_param);
4019 if (dim1 < 0 || dim2 < 0)
4020 return isl_stat_error;
4021 if (dim1 == dim2)
4022 return isl_stat_ok;
4023 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "number of parameters does not match", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 4024); return isl_stat_error; } while (0)
4024 "number of parameters does not match", return isl_stat_error)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "number of parameters does not match", "/build/llvm-toolchain-snapshot-11~++20200309111110+2c36c23f347/polly/lib/External/isl/isl_union_map.c"
, 4024); return isl_stat_error; } while (0)
;
4025}
4026
4027/* Internal data structure for isl_union_map_reset_equal_dim_space.
4028 * "space" is the target space.
4029 * "res" collects the results.
4030 */
4031struct isl_union_map_reset_params_data {
4032 isl_space *space;
4033 isl_union_map *res;
4034};
4035
4036/* Replace the parameters of "map" by those of data->space and
4037 * add the result to data->res.
4038 */
4039static isl_stat reset_params(__isl_take isl_map *map, void *user)
4040{
4041 struct isl_union_map_reset_params_data *data = user;
4042 isl_space *space;
4043
4044 space = isl_map_get_space(map);
4045 space = isl_space_replace_params(space, data->space);
4046 map = isl_map_reset_equal_dim_space(map, space);
4047 data->res = isl_union_map_add_map(data->res, map);
4048
4049 return data->res ? isl_stat_ok : isl_stat_error;
4050}
4051
4052/* Replace the space of "umap" by "space", without modifying
4053 * the dimension of "umap", i.e., the number of parameters of "umap".
4054 *
4055 * Since the hash values of the maps in the union map depend
4056 * on the parameters, a new union map needs to be constructed.
4057 */
4058__isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
4059 __isl_take isl_union_map *umap, __isl_take isl_space *space)
4060{
4061 struct isl_union_map_reset_params_data data = { space };
4062 isl_bool equal;
4063 isl_space *umap_space;
4064
4065 umap_space = isl_union_map_peek_space(umap);
4066 equal = isl_space_is_equal(umap_space, space);
4067 if (equal < 0)
4068 goto error;
4069 if (equal) {
4070 isl_space_free(space);
4071 return umap;
4072 }
4073 if (check_union_map_space_equal_dim(umap, space) < 0)
4074 goto error;
4075
4076 data.res = isl_union_map_empty(isl_space_copy(space));
4077 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
4078 data.res = isl_union_map_free(data.res);
4079
4080 isl_space_free(space);
4081 isl_union_map_free(umap);
4082 return data.res;
4083error:
4084 isl_union_map_free(umap);
4085 isl_space_free(space);
4086 return NULL((void*)0);
4087}
4088
4089/* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
4090 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
4091 * "order" is applied to the extracted isl_multi_pw_affs that correspond
4092 * to the domain and the range of each map.
4093 * "res" collects the results.
4094 */
4095struct isl_union_order_at_data {
4096 isl_multi_union_pw_aff *mupa;
4097 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4098 __isl_take isl_multi_pw_aff *mpa2);
4099 isl_union_map *res;
4100};
4101
4102/* Intersect "map" with the result of applying data->order to
4103 * the functions in data->mupa that apply to the domain and the range
4104 * of "map" and add the result to data->res.
4105 */
4106static isl_stat order_at(__isl_take isl_map *map, void *user)
4107{
4108 struct isl_union_order_at_data *data = user;
4109 isl_space *space;
4110 isl_multi_pw_aff *mpa1, *mpa2;
4111 isl_map *order;
4112
4113 space = isl_space_domain(isl_map_get_space(map));
4114 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4115 space = isl_space_range(isl_map_get_space(map));
4116 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
4117 order = data->order(mpa1, mpa2);
4118 map = isl_map_intersect(map, order);
4119 data->res = isl_union_map_add_map(data->res, map);
4120
4121 return data->res ? isl_stat_ok : isl_stat_error;
4122}
4123
4124/* If "mupa" has a non-trivial explicit domain, then intersect
4125 * domain and range of "umap" with this explicit domain.
4126 * If the explicit domain only describes constraints on the parameters,
4127 * then the intersection only needs to be performed once.
4128 */
4129static __isl_give isl_union_map *intersect_explicit_domain(
4130 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
4131{
4132 isl_bool non_trivial, is_params;
4133 isl_union_setisl_union_map *dom;
4134
4135 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
4136 if (non_trivial < 0)
4137 return isl_union_map_free(umap);
4138 if (!non_trivial)
4139 return umap;
4140 mupa = isl_multi_union_pw_aff_copy(mupa);
4141 dom = isl_multi_union_pw_aff_domain(mupa);
4142 is_params = isl_union_set_is_params(dom);
4143 if (is_params < 0) {
4144 isl_union_set_free(dom);
4145 return isl_union_map_free(umap);
4146 }
4147 if (is_params) {
4148 isl_setisl_map *set;
4149
4150 set = isl_union_set_params(dom);
4151 umap = isl_union_map_intersect_params(umap, set);
4152 return umap;
4153 }
4154 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
4155 umap = isl_union_map_intersect_range(umap, dom);
4156 return umap;
4157}
4158
4159/* Intersect each map in "umap" with the result of calling "order"
4160 * on the functions is "mupa" that apply to the domain and the range
4161 * of the map.
4162 */
4163static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
4164 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
4165 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
4166 __isl_take isl_multi_pw_aff *mpa2))
4167{
4168 struct isl_union_order_at_data data;
4169
4170 umap = isl_union_map_align_params(umap,
4171 isl_multi_union_pw_aff_get_space(mupa));
4172 mupa = isl_multi_union_pw_aff_align_params(mupa,
4173 isl_union_map_get_space(umap));
4174 umap = intersect_explicit_domain(umap, mupa);
4175 data.mupa = mupa;
4176 data.order = order;
4177 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
4178 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
4179 data.res = isl_union_map_free(data.res);
4180
4181 isl_multi_union_pw_aff_free(mupa);
4182 isl_union_map_free(umap);
4183 return data.res;
4184}
4185
4186/* Return the subset of "umap" where the domain and the range
4187 * have equal "mupa" values.
4188 */
4189__isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
4190 __isl_take isl_union_map *umap,
4191 __isl_take isl_multi_union_pw_aff *mupa)
4192{
4193 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4194 &isl_multi_pw_aff_eq_map);
4195}
4196
4197/* Return the subset of "umap" where the domain has a lexicographically
4198 * smaller "mupa" value than the range.
4199 */
4200__isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
4201 __isl_take isl_union_map *umap,
4202 __isl_take isl_multi_union_pw_aff *mupa)
4203{
4204 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4205 &isl_multi_pw_aff_lex_lt_map);
4206}
4207
4208/* Return the subset of "umap" where the domain has a lexicographically
4209 * greater "mupa" value than the range.
4210 */
4211__isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
4212 __isl_take isl_union_map *umap,
4213 __isl_take isl_multi_union_pw_aff *mupa)
4214{
4215 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4216 &isl_multi_pw_aff_lex_gt_map);
4217}
4218
4219/* Return the union of the elements in the list "list".
4220 */
4221__isl_give isl_union_setisl_union_map *isl_union_set_list_union(
4222 __isl_take isl_union_set_listisl_union_map_list *list)
4223{
4224 int i;
4225 isl_size n;
4226 isl_ctx *ctx;
4227 isl_space *space;
4228 isl_union_setisl_union_map *res;
4229
4230 n = isl_union_set_list_n_union_set(list);
4231 if (n < 0)
4232 goto error;
4233
4234 ctx = isl_union_set_list_get_ctx(list);
4235 space = isl_space_params_alloc(ctx, 0);
4236 res = isl_union_set_empty(space);
4237
4238 for (i = 0; i < n; ++i) {
4239 isl_union_setisl_union_map *uset_i;
4240
4241 uset_i = isl_union_set_list_get_union_set(list, i);
4242 res = isl_union_set_union(res, uset_i);
4243 }
4244
4245 isl_union_set_list_free(list);
4246 return res;
4247error:
4248 isl_union_set_list_free(list);
4249 return NULL((void*)0);
4250}
4251
4252/* Update *hash with the hash value of "map".
4253 */
4254static isl_stat add_hash(__isl_take isl_map *map, void *user)
4255{
4256 uint32_t *hash = user;
4257 uint32_t map_hash;
4258
4259 map_hash = isl_map_get_hash(map);
4260 isl_hash_hash(*hash, map_hash)do { do { *hash *= 16777619; *hash ^= (map_hash) & 0xFF; }
while(0); do { *hash *= 16777619; *hash ^= ((map_hash) >>
8) & 0xFF; } while(0); do { *hash *= 16777619; *hash ^= (
(map_hash) >> 16) & 0xFF; } while(0); do { *hash *=
16777619; *hash ^= ((map_hash) >> 24) & 0xFF; } while
(0); } while(0)
;
4261
4262 isl_map_free(map);
4263 return isl_stat_ok;
4264}
4265
4266/* Return a hash value that digests "umap".
4267 */
4268uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4269{
4270 uint32_t hash;
4271
4272 if (!umap)
4273 return 0;
4274
4275 hash = isl_hash_init()(2166136261u);
4276 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4277 return 0;
4278
4279 return hash;
4280}
4281
4282/* Return a hash value that digests "uset".
4283 */
4284uint32_t isl_union_set_get_hash(__isl_keep isl_union_setisl_union_map *uset)
4285{
4286 return isl_union_map_get_hash(uset);
4287}
4288
4289/* Add the number of basic sets in "set" to "n".
4290 */
4291static isl_stat add_n(__isl_take isl_setisl_map *set, void *user)
4292{
4293 int *n = user;
4294 isl_size set_n;
4295
4296 set_n = isl_set_n_basic_set(set);
4297 *n += set_n;
4298 isl_set_free(set);
4299
4300 return set_n < 0 ? isl_stat_error : isl_stat_ok;
4301}
4302
4303/* Return the total number of basic sets in "uset".
4304 */
4305int isl_union_set_n_basic_set(__isl_keep isl_union_setisl_union_map *uset)
4306{
4307 int n = 0;
4308
4309 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4310 return -1;
4311
4312 return n;
4313}
4314
4315/* Add the basic sets in "set" to "list".
4316 */
4317static isl_stat add_list(__isl_take isl_setisl_map *set, void *user)
4318{
4319 isl_basic_set_listisl_basic_map_list **list = user;
4320 isl_basic_set_listisl_basic_map_list *list_i;
4321
4322 list_i = isl_set_get_basic_set_list(set);
4323 *list = isl_basic_set_list_concat(*list, list_i);
4324 isl_set_free(set);
4325
4326 if (!*list)
4327 return isl_stat_error;
4328 return isl_stat_ok;
4329}
4330
4331/* Return a list containing all the basic sets in "uset".
4332 *
4333 * First construct a list of the appropriate size and
4334 * then add all the elements.
4335 */
4336__isl_give isl_basic_set_listisl_basic_map_list *isl_union_set_get_basic_set_list(
4337 __isl_keep isl_union_setisl_union_map *uset)
4338{
4339 int n;
4340 isl_ctx *ctx;
4341 isl_basic_set_listisl_basic_map_list *list;
4342
4343 if (!uset)
4344 return NULL((void*)0);
4345 ctx = isl_union_set_get_ctx(uset);
4346 n = isl_union_set_n_basic_set(uset);
4347 if (n < 0)
4348 return NULL((void*)0);
4349 list = isl_basic_set_list_alloc(ctx, n);
4350 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4351 list = isl_basic_set_list_free(list);
4352
4353 return list;
4354}
4355
4356/* Internal data structure for isl_union_map_remove_map_if.
4357 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4358 */
4359struct isl_union_map_remove_map_if_data {
4360 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4361 void *user;
4362};
4363
4364/* isl_un_op_control filter that negates the result of data->fn
4365 * called on "map".
4366 */
4367static isl_bool not(__isl_keep isl_map *map, void *user)
4368{
4369 struct isl_union_map_remove_map_if_data *data = user;
4370
4371 return isl_bool_not(data->fn(map, data->user));
4372}
4373
4374/* Dummy isl_un_op_control transformation callback that
4375 * simply returns the input.
4376 */
4377static __isl_give isl_map *map_id(__isl_take isl_map *map)
4378{
4379 return map;
4380}
4381
4382/* Call "fn" on every map in "umap" and remove those maps
4383 * for which the callback returns true.
4384 *
4385 * Use un_op to keep only those maps that are not filtered out,
4386 * applying an identity transformation on them.
4387 */
4388__isl_give isl_union_map *isl_union_map_remove_map_if(
4389 __isl_take isl_union_map *umap,
4390 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4391{
4392 struct isl_union_map_remove_map_if_data data = { fn, user };
4393 struct isl_un_op_control control = {
4394 .filter = &not,
4395 .filter_user = &data,
4396 .fn_map = &map_id,
4397 };
4398 return un_op(umap, &control);
4399}
4400
4401/* Does "map" have "space" as range (ignoring parameters)?
4402 */
4403static isl_bool has_range_space(__isl_keep isl_map *map, void *user)
4404{
4405 isl_space *space = user;
4406
4407 return isl_space_has_range_tuples(space, isl_map_peek_space(map));
4408}
4409
4410/* Wrapper around isl_map_bind_range for use as a un_op callback.
4411 */
4412static __isl_give isl_map *bind_range(__isl_take isl_map *map, void *user)
4413{
4414 isl_multi_id *tuple = user;
4415
4416 return isl_map_bind_range(map, isl_multi_id_copy(tuple));
4417}
4418
4419/* Bind the output dimensions of "umap" to parameters with identifiers
4420 * specified by "tuple", living in the range space of "umap",
4421 * for those maps that have a matching range space.
4422 */
4423__isl_give isl_union_setisl_union_map *isl_union_map_bind_range(
4424 __isl_take isl_union_map *umap, __isl_take isl_multi_id *tuple)
4425{
4426 struct isl_un_op_control control = {
4427 .filter = &has_range_space,
4428 .filter_user = isl_multi_id_peek_space(tuple),
4429 .fn_map2 = &bind_range,
4430 .fn_map2_user = tuple,
4431 };
4432 isl_union_setisl_union_map *bound;
4433
4434 bound = uset_from_umap(un_op(umap, &control));
4435 isl_multi_id_free(tuple);
4436 return bound;
4437}