1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
8 * Copyright 2018-2019 Cerebras Systems
9 *
10 * Use of this software is governed by the MIT license
11 *
12 * Written by Sven Verdoolaege, K.U.Leuven, Departement
13 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
18 * B.P. 105 - 78153 Le Chesnay, France
19 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
20 * CS 42112, 75589 Paris Cedex 12, France
21 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
22 */
23
24 #include <string.h>
25 #include <isl_ctx_private.h>
26 #include <isl_map_private.h>
27 #include <isl_blk.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include "isl_space_private.h"
31 #include "isl_equalities.h"
32 #include <isl_lp_private.h>
33 #include <isl_seq.h>
34 #include <isl/set.h>
35 #include <isl/map.h>
36 #include <isl_reordering.h>
37 #include "isl_sample.h"
38 #include <isl_sort.h>
39 #include "isl_tab.h"
40 #include <isl/vec.h>
41 #include <isl_mat_private.h>
42 #include <isl_vec_private.h>
43 #include <isl_dim_map.h>
44 #include <isl_local_space_private.h>
45 #include <isl_aff_private.h>
46 #include <isl_options_private.h>
47 #include <isl_morph.h>
48 #include <isl_val_private.h>
49 #include <isl_printer_private.h>
50
51 #include <bset_to_bmap.c>
52 #include <bset_from_bmap.c>
53 #include <set_to_map.c>
54 #include <set_from_map.c>
55
56 /* Treat "bset" as a basic map.
57 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
58 * this function performs a redundant cast.
59 */
const_bset_to_bmap(__isl_keep const isl_basic_set * bset)60 static __isl_keep const isl_basic_map *const_bset_to_bmap(
61 __isl_keep const isl_basic_set *bset)
62 {
63 return (const isl_basic_map *) bset;
64 }
65
66 #undef TYPE
67 #define TYPE isl_basic_map
68 #include "has_single_reference_templ.c"
69
pos(__isl_keep isl_space * space,enum isl_dim_type type)70 static unsigned pos(__isl_keep isl_space *space, enum isl_dim_type type)
71 {
72 switch (type) {
73 case isl_dim_param: return 1;
74 case isl_dim_in: return 1 + space->nparam;
75 case isl_dim_out: return 1 + space->nparam + space->n_in;
76 default: return 0;
77 }
78 }
79
isl_basic_map_dim(__isl_keep isl_basic_map * bmap,enum isl_dim_type type)80 isl_size isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
81 enum isl_dim_type type)
82 {
83 if (!bmap)
84 return isl_size_error;
85 switch (type) {
86 case isl_dim_cst: return 1;
87 case isl_dim_param:
88 case isl_dim_in:
89 case isl_dim_out: return isl_space_dim(bmap->dim, type);
90 case isl_dim_div: return bmap->n_div;
91 case isl_dim_all: return isl_basic_map_total_dim(bmap);
92 default: return 0;
93 }
94 }
95
96 /* Return the space of "map".
97 */
isl_map_peek_space(__isl_keep const isl_map * map)98 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
99 {
100 return map ? map->dim : NULL;
101 }
102
103 /* Return the space of "set".
104 */
isl_set_peek_space(__isl_keep isl_set * set)105 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
106 {
107 return isl_map_peek_space(set_to_map(set));
108 }
109
isl_map_dim(__isl_keep isl_map * map,enum isl_dim_type type)110 isl_size isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
111 {
112 return isl_space_dim(isl_map_peek_space(map), type);
113 }
114
isl_set_dim(__isl_keep isl_set * set,enum isl_dim_type type)115 isl_size isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
116 {
117 return isl_map_dim(set_to_map(set), type);
118 }
119
120 /* Return the position of the variables of the given type
121 * within the sequence of variables of "bmap".
122 */
isl_basic_map_var_offset(__isl_keep isl_basic_map * bmap,enum isl_dim_type type)123 isl_size isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
124 enum isl_dim_type type)
125 {
126 isl_space *space;
127
128 space = isl_basic_map_peek_space(bmap);
129 if (!space)
130 return isl_size_error;
131
132 switch (type) {
133 case isl_dim_param:
134 case isl_dim_in:
135 case isl_dim_out: return isl_space_offset(space, type);
136 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
137 case isl_dim_cst:
138 default:
139 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
140 "invalid dimension type", return isl_size_error);
141 }
142 }
143
144 /* Return the position of the variables of the given type
145 * within the sequence of variables of "bset".
146 */
isl_basic_set_var_offset(__isl_keep isl_basic_set * bset,enum isl_dim_type type)147 isl_size isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
148 enum isl_dim_type type)
149 {
150 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
151 }
152
153 /* Return the position of the coefficients of the variables of the given type
154 * within the sequence of coefficients of "bmap".
155 */
isl_basic_map_offset(__isl_keep isl_basic_map * bmap,enum isl_dim_type type)156 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
157 enum isl_dim_type type)
158 {
159 switch (type) {
160 case isl_dim_cst: return 0;
161 case isl_dim_param:
162 case isl_dim_in:
163 case isl_dim_out:
164 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
165 default: return 0;
166 }
167 }
168
isl_basic_set_offset(__isl_keep isl_basic_set * bset,enum isl_dim_type type)169 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
170 enum isl_dim_type type)
171 {
172 return isl_basic_map_offset(bset, type);
173 }
174
map_offset(__isl_keep isl_map * map,enum isl_dim_type type)175 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
176 {
177 return pos(map->dim, type);
178 }
179
isl_basic_set_dim(__isl_keep isl_basic_set * bset,enum isl_dim_type type)180 isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset,
181 enum isl_dim_type type)
182 {
183 return isl_basic_map_dim(bset, type);
184 }
185
isl_basic_set_n_dim(__isl_keep isl_basic_set * bset)186 isl_size isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
187 {
188 return isl_basic_set_dim(bset, isl_dim_set);
189 }
190
isl_basic_set_n_param(__isl_keep isl_basic_set * bset)191 isl_size isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
192 {
193 return isl_basic_set_dim(bset, isl_dim_param);
194 }
195
isl_basic_set_total_dim(__isl_keep const isl_basic_set * bset)196 isl_size isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
197 {
198 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
199 }
200
isl_set_n_dim(__isl_keep isl_set * set)201 isl_size isl_set_n_dim(__isl_keep isl_set *set)
202 {
203 return isl_set_dim(set, isl_dim_set);
204 }
205
isl_set_n_param(__isl_keep isl_set * set)206 isl_size isl_set_n_param(__isl_keep isl_set *set)
207 {
208 return isl_set_dim(set, isl_dim_param);
209 }
210
isl_basic_map_total_dim(__isl_keep const isl_basic_map * bmap)211 isl_size isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
212 {
213 isl_size dim;
214
215 if (!bmap)
216 return isl_size_error;
217 dim = isl_space_dim(bmap->dim, isl_dim_all);
218 if (dim < 0)
219 return isl_size_error;
220 return dim + bmap->n_div;
221 }
222
223 /* Return the number of equality constraints in the description of "bmap".
224 * Return isl_size_error on error.
225 */
isl_basic_map_n_equality(__isl_keep isl_basic_map * bmap)226 isl_size isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
227 {
228 if (!bmap)
229 return isl_size_error;
230 return bmap->n_eq;
231 }
232
233 /* Return the number of equality constraints in the description of "bset".
234 * Return isl_size_error on error.
235 */
isl_basic_set_n_equality(__isl_keep isl_basic_set * bset)236 isl_size isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
237 {
238 return isl_basic_map_n_equality(bset_to_bmap(bset));
239 }
240
241 /* Return the number of inequality constraints in the description of "bmap".
242 * Return isl_size_error on error.
243 */
isl_basic_map_n_inequality(__isl_keep isl_basic_map * bmap)244 isl_size isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
245 {
246 if (!bmap)
247 return isl_size_error;
248 return bmap->n_ineq;
249 }
250
251 /* Return the number of inequality constraints in the description of "bset".
252 * Return isl_size_error on error.
253 */
isl_basic_set_n_inequality(__isl_keep isl_basic_set * bset)254 isl_size isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
255 {
256 return isl_basic_map_n_inequality(bset_to_bmap(bset));
257 }
258
259 /* Do "bmap1" and "bmap2" have the same parameters?
260 */
isl_basic_map_has_equal_params(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)261 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
262 __isl_keep isl_basic_map *bmap2)
263 {
264 isl_space *space1, *space2;
265
266 space1 = isl_basic_map_peek_space(bmap1);
267 space2 = isl_basic_map_peek_space(bmap2);
268 return isl_space_has_equal_params(space1, space2);
269 }
270
271 /* Do "map1" and "map2" have the same parameters?
272 */
isl_map_has_equal_params(__isl_keep isl_map * map1,__isl_keep isl_map * map2)273 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
274 __isl_keep isl_map *map2)
275 {
276 isl_space *space1, *space2;
277
278 space1 = isl_map_peek_space(map1);
279 space2 = isl_map_peek_space(map2);
280 return isl_space_has_equal_params(space1, space2);
281 }
282
283 /* Do "map" and "set" have the same parameters?
284 */
isl_map_set_has_equal_params(__isl_keep isl_map * map,__isl_keep isl_set * set)285 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
286 __isl_keep isl_set *set)
287 {
288 return isl_map_has_equal_params(map, set_to_map(set));
289 }
290
291 /* Is the tuple of type "type" of "bmap" the same as the single tuple of "bset"?
292 */
isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,__isl_keep isl_basic_set * bset)293 static isl_bool isl_basic_map_set_tuple_is_equal(__isl_keep isl_basic_map *bmap,
294 enum isl_dim_type type, __isl_keep isl_basic_set *bset)
295 {
296 isl_space *bmap_space, *bset_space;
297
298 bmap_space = isl_basic_map_peek_space(bmap);
299 bset_space = isl_basic_set_peek_space(bset);
300 return isl_space_tuple_is_equal(bmap_space, type,
301 bset_space, isl_dim_set);
302 }
303
304 /* Is the tuple of type "type" of "map" the same as the single tuple of "set"?
305 */
isl_map_set_tuple_is_equal(__isl_keep isl_map * map,enum isl_dim_type type,__isl_keep isl_set * set)306 static isl_bool isl_map_set_tuple_is_equal(__isl_keep isl_map *map,
307 enum isl_dim_type type, __isl_keep isl_set *set)
308 {
309 return isl_map_tuple_is_equal(map, type, set_to_map(set), isl_dim_set);
310 }
311
isl_map_compatible_domain(__isl_keep isl_map * map,__isl_keep isl_set * set)312 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
313 __isl_keep isl_set *set)
314 {
315 isl_bool m;
316 if (!map || !set)
317 return isl_bool_error;
318 m = isl_map_has_equal_params(map, set_to_map(set));
319 if (m < 0 || !m)
320 return m;
321 return isl_map_set_tuple_is_equal(map, isl_dim_in, set);
322 }
323
isl_basic_map_compatible_domain(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * bset)324 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
325 __isl_keep isl_basic_set *bset)
326 {
327 isl_bool m;
328 if (!bmap || !bset)
329 return isl_bool_error;
330 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
331 if (m < 0 || !m)
332 return m;
333 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_in, bset);
334 }
335
isl_map_compatible_range(__isl_keep isl_map * map,__isl_keep isl_set * set)336 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
337 __isl_keep isl_set *set)
338 {
339 isl_bool m;
340 if (!map || !set)
341 return isl_bool_error;
342 m = isl_map_has_equal_params(map, set_to_map(set));
343 if (m < 0 || !m)
344 return m;
345 return isl_map_set_tuple_is_equal(map, isl_dim_out, set);
346 }
347
isl_basic_map_compatible_range(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * bset)348 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
349 __isl_keep isl_basic_set *bset)
350 {
351 isl_bool m;
352 if (!bmap || !bset)
353 return isl_bool_error;
354 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
355 if (m < 0 || !m)
356 return m;
357 return isl_basic_map_set_tuple_is_equal(bmap, isl_dim_out, bset);
358 }
359
isl_basic_map_get_ctx(__isl_keep isl_basic_map * bmap)360 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
361 {
362 return bmap ? bmap->ctx : NULL;
363 }
364
isl_basic_set_get_ctx(__isl_keep isl_basic_set * bset)365 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
366 {
367 return bset ? bset->ctx : NULL;
368 }
369
isl_map_get_ctx(__isl_keep isl_map * map)370 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
371 {
372 return map ? map->ctx : NULL;
373 }
374
isl_set_get_ctx(__isl_keep isl_set * set)375 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
376 {
377 return set ? set->ctx : NULL;
378 }
379
380 /* Return the space of "bmap".
381 */
isl_basic_map_peek_space(__isl_keep const isl_basic_map * bmap)382 __isl_keep isl_space *isl_basic_map_peek_space(
383 __isl_keep const isl_basic_map *bmap)
384 {
385 return bmap ? bmap->dim : NULL;
386 }
387
388 /* Return the space of "bset".
389 */
isl_basic_set_peek_space(__isl_keep isl_basic_set * bset)390 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
391 {
392 return isl_basic_map_peek_space(bset_to_bmap(bset));
393 }
394
isl_basic_map_get_space(__isl_keep isl_basic_map * bmap)395 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
396 {
397 return isl_space_copy(isl_basic_map_peek_space(bmap));
398 }
399
isl_basic_set_get_space(__isl_keep isl_basic_set * bset)400 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
401 {
402 return isl_basic_map_get_space(bset_to_bmap(bset));
403 }
404
405 /* Return the space of "bmap".
406 * This may be either a copy or the space itself
407 * if there is only one reference to "bmap".
408 * This allows the space to be modified inplace
409 * if both the basic map and its space have only a single reference.
410 * The caller is not allowed to modify "bmap" between this call and
411 * a subsequent call to isl_basic_map_restore_space.
412 * The only exception is that isl_basic_map_free can be called instead.
413 */
isl_basic_map_take_space(__isl_keep isl_basic_map * bmap)414 static __isl_give isl_space *isl_basic_map_take_space(
415 __isl_keep isl_basic_map *bmap)
416 {
417 isl_space *space;
418
419 if (!bmap)
420 return NULL;
421 if (bmap->ref != 1)
422 return isl_basic_map_get_space(bmap);
423 space = bmap->dim;
424 bmap->dim = NULL;
425 return space;
426 }
427
428 /* Set the space of "bmap" to "space", where the space of "bmap" may be missing
429 * due to a preceding call to isl_basic_map_take_space.
430 * However, in this case, "bmap" only has a single reference and
431 * then the call to isl_basic_map_cow has no effect.
432 */
isl_basic_map_restore_space(__isl_take isl_basic_map * bmap,__isl_take isl_space * space)433 static __isl_give isl_basic_map *isl_basic_map_restore_space(
434 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
435 {
436 if (!bmap || !space)
437 goto error;
438
439 if (bmap->dim == space) {
440 isl_space_free(space);
441 return bmap;
442 }
443
444 bmap = isl_basic_map_cow(bmap);
445 if (!bmap)
446 goto error;
447 isl_space_free(bmap->dim);
448 bmap->dim = space;
449
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 isl_space_free(space);
454 return NULL;
455 }
456
457 /* Extract the divs in "bmap" as a matrix.
458 */
isl_basic_map_get_divs(__isl_keep isl_basic_map * bmap)459 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
460 {
461 int i;
462 isl_ctx *ctx;
463 isl_mat *div;
464 isl_size v_div;
465 unsigned cols;
466
467 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
468 if (v_div < 0)
469 return NULL;
470
471 ctx = isl_basic_map_get_ctx(bmap);
472 cols = 1 + 1 + v_div + bmap->n_div;
473 div = isl_mat_alloc(ctx, bmap->n_div, cols);
474 if (!div)
475 return NULL;
476
477 for (i = 0; i < bmap->n_div; ++i)
478 isl_seq_cpy(div->row[i], bmap->div[i], cols);
479
480 return div;
481 }
482
483 /* Extract the divs in "bset" as a matrix.
484 */
isl_basic_set_get_divs(__isl_keep isl_basic_set * bset)485 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
486 {
487 return isl_basic_map_get_divs(bset);
488 }
489
isl_basic_map_get_local_space(__isl_keep isl_basic_map * bmap)490 __isl_give isl_local_space *isl_basic_map_get_local_space(
491 __isl_keep isl_basic_map *bmap)
492 {
493 isl_mat *div;
494
495 if (!bmap)
496 return NULL;
497
498 div = isl_basic_map_get_divs(bmap);
499 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
500 }
501
isl_basic_set_get_local_space(__isl_keep isl_basic_set * bset)502 __isl_give isl_local_space *isl_basic_set_get_local_space(
503 __isl_keep isl_basic_set *bset)
504 {
505 return isl_basic_map_get_local_space(bset);
506 }
507
508 /* For each known div d = floor(f/m), add the constraints
509 *
510 * f - m d >= 0
511 * -(f-(m-1)) + m d >= 0
512 *
513 * Do not finalize the result.
514 */
add_known_div_constraints(__isl_take isl_basic_map * bmap)515 static __isl_give isl_basic_map *add_known_div_constraints(
516 __isl_take isl_basic_map *bmap)
517 {
518 int i;
519 isl_size n_div;
520
521 n_div = isl_basic_map_dim(bmap, isl_dim_div);
522 if (n_div < 0)
523 return isl_basic_map_free(bmap);
524 if (n_div == 0)
525 return bmap;
526 bmap = isl_basic_map_cow(bmap);
527 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
528 if (!bmap)
529 return NULL;
530 for (i = 0; i < n_div; ++i) {
531 if (isl_int_is_zero(bmap->div[i][0]))
532 continue;
533 bmap = isl_basic_map_add_div_constraints(bmap, i);
534 }
535
536 return bmap;
537 }
538
isl_basic_map_from_local_space(__isl_take isl_local_space * ls)539 __isl_give isl_basic_map *isl_basic_map_from_local_space(
540 __isl_take isl_local_space *ls)
541 {
542 int i;
543 isl_size n_div;
544 isl_basic_map *bmap;
545
546 n_div = isl_local_space_dim(ls, isl_dim_div);
547 if (n_div < 0)
548 ls = isl_local_space_free(ls);
549 if (!ls)
550 return NULL;
551
552 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
553 n_div, 0, 2 * n_div);
554
555 for (i = 0; i < n_div; ++i)
556 if (isl_basic_map_alloc_div(bmap) < 0)
557 goto error;
558
559 for (i = 0; i < n_div; ++i)
560 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
561 bmap = add_known_div_constraints(bmap);
562
563 isl_local_space_free(ls);
564 return bmap;
565 error:
566 isl_local_space_free(ls);
567 isl_basic_map_free(bmap);
568 return NULL;
569 }
570
isl_basic_set_from_local_space(__isl_take isl_local_space * ls)571 __isl_give isl_basic_set *isl_basic_set_from_local_space(
572 __isl_take isl_local_space *ls)
573 {
574 return isl_basic_map_from_local_space(ls);
575 }
576
isl_map_get_space(__isl_keep isl_map * map)577 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
578 {
579 return isl_space_copy(isl_map_peek_space(map));
580 }
581
isl_set_get_space(__isl_keep isl_set * set)582 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
583 {
584 if (!set)
585 return NULL;
586 return isl_space_copy(set->dim);
587 }
588
589 /* Return the space of "map".
590 * This may be either a copy or the space itself
591 * if there is only one reference to "map".
592 * This allows the space to be modified inplace
593 * if both the map and its space have only a single reference.
594 * The caller is not allowed to modify "map" between this call and
595 * a subsequent call to isl_map_restore_space.
596 * The only exception is that isl_map_free can be called instead.
597 */
isl_map_take_space(__isl_keep isl_map * map)598 static __isl_give isl_space *isl_map_take_space(__isl_keep isl_map *map)
599 {
600 isl_space *space;
601
602 if (!map)
603 return NULL;
604 if (map->ref != 1)
605 return isl_map_get_space(map);
606 space = map->dim;
607 map->dim = NULL;
608 return space;
609 }
610
611 /* Set the space of "map" to "space", where the space of "map" may be missing
612 * due to a preceding call to isl_map_take_space.
613 * However, in this case, "map" only has a single reference and
614 * then the call to isl_map_cow has no effect.
615 */
isl_map_restore_space(__isl_take isl_map * map,__isl_take isl_space * space)616 static __isl_give isl_map *isl_map_restore_space(__isl_take isl_map *map,
617 __isl_take isl_space *space)
618 {
619 if (!map || !space)
620 goto error;
621
622 if (map->dim == space) {
623 isl_space_free(space);
624 return map;
625 }
626
627 map = isl_map_cow(map);
628 if (!map)
629 goto error;
630 isl_space_free(map->dim);
631 map->dim = space;
632
633 return map;
634 error:
635 isl_map_free(map);
636 isl_space_free(space);
637 return NULL;
638 }
639
isl_basic_map_set_tuple_name(__isl_take isl_basic_map * bmap,enum isl_dim_type type,const char * s)640 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
641 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
642 {
643 isl_space *space;
644
645 space = isl_basic_map_take_space(bmap);
646 space = isl_space_set_tuple_name(space, type, s);
647 bmap = isl_basic_map_restore_space(bmap, space);
648 bmap = isl_basic_map_finalize(bmap);
649 return bmap;
650 }
651
isl_basic_set_set_tuple_name(__isl_take isl_basic_set * bset,const char * s)652 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
653 __isl_take isl_basic_set *bset, const char *s)
654 {
655 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
656 }
657
isl_basic_map_get_tuple_name(__isl_keep isl_basic_map * bmap,enum isl_dim_type type)658 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
659 enum isl_dim_type type)
660 {
661 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
662 }
663
isl_map_set_tuple_name(__isl_take isl_map * map,enum isl_dim_type type,const char * s)664 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
665 enum isl_dim_type type, const char *s)
666 {
667 int i;
668 isl_space *space;
669
670 map = isl_map_cow(map);
671 if (!map)
672 return NULL;
673
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
676 if (!map->p[i])
677 goto error;
678 }
679
680 space = isl_map_take_space(map);
681 space = isl_space_set_tuple_name(space, type, s);
682 map = isl_map_restore_space(map, space);
683
684 return map;
685 error:
686 isl_map_free(map);
687 return NULL;
688 }
689
690 /* Replace the identifier of the tuple of type "type" by "id".
691 */
isl_basic_map_set_tuple_id(__isl_take isl_basic_map * bmap,enum isl_dim_type type,__isl_take isl_id * id)692 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
693 __isl_take isl_basic_map *bmap,
694 enum isl_dim_type type, __isl_take isl_id *id)
695 {
696 isl_space *space;
697
698 space = isl_basic_map_take_space(bmap);
699 space = isl_space_set_tuple_id(space, type, id);
700 bmap = isl_basic_map_restore_space(bmap, space);
701 bmap = isl_basic_map_finalize(bmap);
702 return bmap;
703 }
704
705 /* Replace the identifier of the tuple by "id".
706 */
isl_basic_set_set_tuple_id(__isl_take isl_basic_set * bset,__isl_take isl_id * id)707 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
708 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
709 {
710 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
711 }
712
713 /* Does the input or output tuple have a name?
714 */
isl_map_has_tuple_name(__isl_keep isl_map * map,enum isl_dim_type type)715 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
716 {
717 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
718 }
719
isl_map_get_tuple_name(__isl_keep isl_map * map,enum isl_dim_type type)720 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
721 enum isl_dim_type type)
722 {
723 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
724 }
725
isl_set_set_tuple_name(__isl_take isl_set * set,const char * s)726 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
727 const char *s)
728 {
729 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
730 isl_dim_set, s));
731 }
732
isl_map_set_tuple_id(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_id * id)733 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
734 enum isl_dim_type type, __isl_take isl_id *id)
735 {
736 isl_space *space;
737
738 space = isl_map_take_space(map);
739 space = isl_space_set_tuple_id(space, type, id);
740 map = isl_map_restore_space(map, space);
741
742 return isl_map_reset_space(map, isl_map_get_space(map));
743 }
744
745 /* Replace the identifier of the domain tuple of "map" by "id".
746 */
isl_map_set_domain_tuple_id(__isl_take isl_map * map,__isl_take isl_id * id)747 __isl_give isl_map *isl_map_set_domain_tuple_id(__isl_take isl_map *map,
748 __isl_take isl_id *id)
749 {
750 return isl_map_set_tuple_id(map, isl_dim_in, id);
751 }
752
753 /* Replace the identifier of the range tuple of "map" by "id".
754 */
isl_map_set_range_tuple_id(__isl_take isl_map * map,__isl_take isl_id * id)755 __isl_give isl_map *isl_map_set_range_tuple_id(__isl_take isl_map *map,
756 __isl_take isl_id *id)
757 {
758 return isl_map_set_tuple_id(map, isl_dim_out, id);
759 }
760
isl_set_set_tuple_id(__isl_take isl_set * set,__isl_take isl_id * id)761 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
762 __isl_take isl_id *id)
763 {
764 return isl_map_set_tuple_id(set, isl_dim_set, id);
765 }
766
isl_map_reset_tuple_id(__isl_take isl_map * map,enum isl_dim_type type)767 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
768 enum isl_dim_type type)
769 {
770 isl_space *space;
771
772 space = isl_map_take_space(map);
773 space = isl_space_reset_tuple_id(space, type);
774 map = isl_map_restore_space(map, space);
775
776 return isl_map_reset_space(map, isl_map_get_space(map));
777 }
778
isl_set_reset_tuple_id(__isl_take isl_set * set)779 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
780 {
781 return isl_map_reset_tuple_id(set, isl_dim_set);
782 }
783
isl_map_has_tuple_id(__isl_keep isl_map * map,enum isl_dim_type type)784 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
785 {
786 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
787 }
788
789 /* Does the domain tuple of "map" have an identifier?
790 */
isl_map_has_domain_tuple_id(__isl_keep isl_map * map)791 isl_bool isl_map_has_domain_tuple_id(__isl_keep isl_map *map)
792 {
793 return isl_map_has_tuple_id(map, isl_dim_in);
794 }
795
796 /* Does the range tuple of "map" have an identifier?
797 */
isl_map_has_range_tuple_id(__isl_keep isl_map * map)798 isl_bool isl_map_has_range_tuple_id(__isl_keep isl_map *map)
799 {
800 return isl_map_has_tuple_id(map, isl_dim_out);
801 }
802
isl_map_get_tuple_id(__isl_keep isl_map * map,enum isl_dim_type type)803 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
804 enum isl_dim_type type)
805 {
806 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
807 }
808
809 /* Return the identifier of the domain tuple of "map", assuming it has one.
810 */
isl_map_get_domain_tuple_id(__isl_keep isl_map * map)811 __isl_give isl_id *isl_map_get_domain_tuple_id(__isl_keep isl_map *map)
812 {
813 return isl_map_get_tuple_id(map, isl_dim_in);
814 }
815
816 /* Return the identifier of the range tuple of "map", assuming it has one.
817 */
isl_map_get_range_tuple_id(__isl_keep isl_map * map)818 __isl_give isl_id *isl_map_get_range_tuple_id(__isl_keep isl_map *map)
819 {
820 return isl_map_get_tuple_id(map, isl_dim_out);
821 }
822
isl_set_has_tuple_id(__isl_keep isl_set * set)823 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
824 {
825 return isl_map_has_tuple_id(set, isl_dim_set);
826 }
827
isl_set_get_tuple_id(__isl_keep isl_set * set)828 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
829 {
830 return isl_map_get_tuple_id(set, isl_dim_set);
831 }
832
833 /* Does the set tuple have a name?
834 */
isl_set_has_tuple_name(__isl_keep isl_set * set)835 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
836 {
837 if (!set)
838 return isl_bool_error;
839 return isl_space_has_tuple_name(set->dim, isl_dim_set);
840 }
841
842
isl_basic_set_get_tuple_name(__isl_keep isl_basic_set * bset)843 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
844 {
845 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
846 }
847
isl_set_get_tuple_name(__isl_keep isl_set * set)848 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
849 {
850 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
851 }
852
isl_basic_map_get_dim_name(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)853 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
854 enum isl_dim_type type, unsigned pos)
855 {
856 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
857 }
858
isl_basic_set_get_dim_name(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned pos)859 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
860 enum isl_dim_type type, unsigned pos)
861 {
862 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
863 }
864
865 /* Does the given dimension have a name?
866 */
isl_map_has_dim_name(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)867 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
868 enum isl_dim_type type, unsigned pos)
869 {
870 if (!map)
871 return isl_bool_error;
872 return isl_space_has_dim_name(map->dim, type, pos);
873 }
874
isl_map_get_dim_name(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)875 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
876 enum isl_dim_type type, unsigned pos)
877 {
878 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
879 }
880
isl_set_get_dim_name(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)881 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
882 enum isl_dim_type type, unsigned pos)
883 {
884 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
885 }
886
887 /* Does the given dimension have a name?
888 */
isl_set_has_dim_name(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)889 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
890 enum isl_dim_type type, unsigned pos)
891 {
892 if (!set)
893 return isl_bool_error;
894 return isl_space_has_dim_name(set->dim, type, pos);
895 }
896
isl_basic_map_set_dim_name(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,const char * s)897 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
898 __isl_take isl_basic_map *bmap,
899 enum isl_dim_type type, unsigned pos, const char *s)
900 {
901 isl_space *space;
902
903 space = isl_basic_map_take_space(bmap);
904 space = isl_space_set_dim_name(space, type, pos, s);
905 bmap = isl_basic_map_restore_space(bmap, space);
906 return isl_basic_map_finalize(bmap);
907 }
908
isl_map_set_dim_name(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,const char * s)909 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
910 enum isl_dim_type type, unsigned pos, const char *s)
911 {
912 int i;
913 isl_space *space;
914
915 map = isl_map_cow(map);
916 if (!map)
917 return NULL;
918
919 for (i = 0; i < map->n; ++i) {
920 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
921 if (!map->p[i])
922 goto error;
923 }
924
925 space = isl_map_take_space(map);
926 space = isl_space_set_dim_name(space, type, pos, s);
927 map = isl_map_restore_space(map, space);
928
929 return map;
930 error:
931 isl_map_free(map);
932 return NULL;
933 }
934
isl_basic_set_set_dim_name(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,const char * s)935 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
936 __isl_take isl_basic_set *bset,
937 enum isl_dim_type type, unsigned pos, const char *s)
938 {
939 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
940 type, pos, s));
941 }
942
isl_set_set_dim_name(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,const char * s)943 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
944 enum isl_dim_type type, unsigned pos, const char *s)
945 {
946 return set_from_map(isl_map_set_dim_name(set_to_map(set),
947 type, pos, s));
948 }
949
isl_basic_map_has_dim_id(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)950 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
951 enum isl_dim_type type, unsigned pos)
952 {
953 if (!bmap)
954 return isl_bool_error;
955 return isl_space_has_dim_id(bmap->dim, type, pos);
956 }
957
isl_basic_set_get_dim_id(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned pos)958 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
959 enum isl_dim_type type, unsigned pos)
960 {
961 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
962 }
963
isl_map_has_dim_id(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)964 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
965 enum isl_dim_type type, unsigned pos)
966 {
967 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
968 }
969
isl_map_get_dim_id(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)970 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
971 enum isl_dim_type type, unsigned pos)
972 {
973 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
974 }
975
isl_set_has_dim_id(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)976 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
977 enum isl_dim_type type, unsigned pos)
978 {
979 return isl_map_has_dim_id(set, type, pos);
980 }
981
isl_set_get_dim_id(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)982 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
983 enum isl_dim_type type, unsigned pos)
984 {
985 return isl_map_get_dim_id(set, type, pos);
986 }
987
isl_map_set_dim_id(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,__isl_take isl_id * id)988 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
989 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
990 {
991 isl_space *space;
992
993 space = isl_map_take_space(map);
994 space = isl_space_set_dim_id(space, type, pos, id);
995 map = isl_map_restore_space(map, space);
996
997 return isl_map_reset_space(map, isl_map_get_space(map));
998 }
999
isl_set_set_dim_id(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,__isl_take isl_id * id)1000 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
1001 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1002 {
1003 return isl_map_set_dim_id(set, type, pos, id);
1004 }
1005
isl_map_find_dim_by_id(__isl_keep isl_map * map,enum isl_dim_type type,__isl_keep isl_id * id)1006 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
1007 __isl_keep isl_id *id)
1008 {
1009 if (!map)
1010 return -1;
1011 return isl_space_find_dim_by_id(map->dim, type, id);
1012 }
1013
isl_set_find_dim_by_id(__isl_keep isl_set * set,enum isl_dim_type type,__isl_keep isl_id * id)1014 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
1015 __isl_keep isl_id *id)
1016 {
1017 return isl_map_find_dim_by_id(set, type, id);
1018 }
1019
1020 /* Return the position of the dimension of the given type and name
1021 * in "bmap".
1022 * Return -1 if no such dimension can be found.
1023 */
isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,const char * name)1024 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
1025 enum isl_dim_type type, const char *name)
1026 {
1027 if (!bmap)
1028 return -1;
1029 return isl_space_find_dim_by_name(bmap->dim, type, name);
1030 }
1031
isl_map_find_dim_by_name(__isl_keep isl_map * map,enum isl_dim_type type,const char * name)1032 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
1033 const char *name)
1034 {
1035 if (!map)
1036 return -1;
1037 return isl_space_find_dim_by_name(map->dim, type, name);
1038 }
1039
isl_set_find_dim_by_name(__isl_keep isl_set * set,enum isl_dim_type type,const char * name)1040 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
1041 const char *name)
1042 {
1043 return isl_map_find_dim_by_name(set, type, name);
1044 }
1045
1046 /* Check whether equality i of bset is a pure stride constraint
1047 * on a single dimension, i.e., of the form
1048 *
1049 * v = k e
1050 *
1051 * with k a constant and e an existentially quantified variable.
1052 */
isl_basic_set_eq_is_stride(__isl_keep isl_basic_set * bset,int i)1053 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
1054 {
1055 isl_size nparam;
1056 isl_size d;
1057 isl_size n_div;
1058 int pos1;
1059 int pos2;
1060
1061 nparam = isl_basic_set_dim(bset, isl_dim_param);
1062 d = isl_basic_set_dim(bset, isl_dim_set);
1063 n_div = isl_basic_set_dim(bset, isl_dim_div);
1064 if (nparam < 0 || d < 0 || n_div < 0)
1065 return isl_bool_error;
1066
1067 if (!isl_int_is_zero(bset->eq[i][0]))
1068 return isl_bool_false;
1069
1070 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
1071 return isl_bool_false;
1072 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
1073 if (pos1 == -1)
1074 return isl_bool_false;
1075 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
1076 d - pos1 - 1) != -1)
1077 return isl_bool_false;
1078
1079 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
1080 if (pos2 == -1)
1081 return isl_bool_false;
1082 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
1083 n_div - pos2 - 1) != -1)
1084 return isl_bool_false;
1085 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
1086 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
1087 return isl_bool_false;
1088
1089 return isl_bool_true;
1090 }
1091
1092 /* Reset the user pointer on all identifiers of parameters and tuples
1093 * of the space of "map".
1094 */
isl_map_reset_user(__isl_take isl_map * map)1095 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
1096 {
1097 isl_space *space;
1098
1099 space = isl_map_get_space(map);
1100 space = isl_space_reset_user(space);
1101 map = isl_map_reset_space(map, space);
1102
1103 return map;
1104 }
1105
1106 /* Reset the user pointer on all identifiers of parameters and tuples
1107 * of the space of "set".
1108 */
isl_set_reset_user(__isl_take isl_set * set)1109 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
1110 {
1111 return isl_map_reset_user(set);
1112 }
1113
isl_basic_map_is_rational(__isl_keep isl_basic_map * bmap)1114 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1115 {
1116 if (!bmap)
1117 return isl_bool_error;
1118 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1119 }
1120
1121 /* Has "map" been marked as a rational map?
1122 * In particular, have all basic maps in "map" been marked this way?
1123 * An empty map is not considered to be rational.
1124 * Maps where only some of the basic maps are marked rational
1125 * are not allowed.
1126 */
isl_map_is_rational(__isl_keep isl_map * map)1127 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1128 {
1129 int i;
1130 isl_bool rational;
1131
1132 if (!map)
1133 return isl_bool_error;
1134 if (map->n == 0)
1135 return isl_bool_false;
1136 rational = isl_basic_map_is_rational(map->p[0]);
1137 if (rational < 0)
1138 return rational;
1139 for (i = 1; i < map->n; ++i) {
1140 isl_bool rational_i;
1141
1142 rational_i = isl_basic_map_is_rational(map->p[i]);
1143 if (rational_i < 0)
1144 return rational_i;
1145 if (rational != rational_i)
1146 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1147 "mixed rational and integer basic maps "
1148 "not supported", return isl_bool_error);
1149 }
1150
1151 return rational;
1152 }
1153
1154 /* Has "set" been marked as a rational set?
1155 * In particular, have all basic set in "set" been marked this way?
1156 * An empty set is not considered to be rational.
1157 * Sets where only some of the basic sets are marked rational
1158 * are not allowed.
1159 */
isl_set_is_rational(__isl_keep isl_set * set)1160 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1161 {
1162 return isl_map_is_rational(set);
1163 }
1164
isl_basic_set_is_rational(__isl_keep isl_basic_set * bset)1165 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1166 {
1167 return isl_basic_map_is_rational(bset);
1168 }
1169
1170 /* Does "bmap" contain any rational points?
1171 *
1172 * If "bmap" has an equality for each dimension, equating the dimension
1173 * to an integer constant, then it has no rational points, even if it
1174 * is marked as rational.
1175 */
isl_basic_map_has_rational(__isl_keep isl_basic_map * bmap)1176 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1177 {
1178 isl_bool has_rational = isl_bool_true;
1179 isl_size total;
1180
1181 if (!bmap)
1182 return isl_bool_error;
1183 if (isl_basic_map_plain_is_empty(bmap))
1184 return isl_bool_false;
1185 if (!isl_basic_map_is_rational(bmap))
1186 return isl_bool_false;
1187 bmap = isl_basic_map_copy(bmap);
1188 bmap = isl_basic_map_implicit_equalities(bmap);
1189 total = isl_basic_map_dim(bmap, isl_dim_all);
1190 if (total < 0)
1191 return isl_bool_error;
1192 if (bmap->n_eq == total) {
1193 int i, j;
1194 for (i = 0; i < bmap->n_eq; ++i) {
1195 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1196 if (j < 0)
1197 break;
1198 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1199 !isl_int_is_negone(bmap->eq[i][1 + j]))
1200 break;
1201 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1202 total - j - 1);
1203 if (j >= 0)
1204 break;
1205 }
1206 if (i == bmap->n_eq)
1207 has_rational = isl_bool_false;
1208 }
1209 isl_basic_map_free(bmap);
1210
1211 return has_rational;
1212 }
1213
1214 /* Does "map" contain any rational points?
1215 */
isl_map_has_rational(__isl_keep isl_map * map)1216 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1217 {
1218 int i;
1219 isl_bool has_rational;
1220
1221 if (!map)
1222 return isl_bool_error;
1223 for (i = 0; i < map->n; ++i) {
1224 has_rational = isl_basic_map_has_rational(map->p[i]);
1225 if (has_rational < 0 || has_rational)
1226 return has_rational;
1227 }
1228 return isl_bool_false;
1229 }
1230
1231 /* Does "set" contain any rational points?
1232 */
isl_set_has_rational(__isl_keep isl_set * set)1233 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1234 {
1235 return isl_map_has_rational(set);
1236 }
1237
1238 /* Is this basic set a parameter domain?
1239 */
isl_basic_set_is_params(__isl_keep isl_basic_set * bset)1240 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1241 {
1242 if (!bset)
1243 return isl_bool_error;
1244 return isl_space_is_params(bset->dim);
1245 }
1246
1247 /* Is this set a parameter domain?
1248 */
isl_set_is_params(__isl_keep isl_set * set)1249 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1250 {
1251 if (!set)
1252 return isl_bool_error;
1253 return isl_space_is_params(set->dim);
1254 }
1255
1256 /* Is this map actually a parameter domain?
1257 * Users should never call this function. Outside of isl,
1258 * a map can never be a parameter domain.
1259 */
isl_map_is_params(__isl_keep isl_map * map)1260 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1261 {
1262 if (!map)
1263 return isl_bool_error;
1264 return isl_space_is_params(map->dim);
1265 }
1266
basic_map_init(isl_ctx * ctx,__isl_take isl_basic_map * bmap,unsigned extra,unsigned n_eq,unsigned n_ineq)1267 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1268 __isl_take isl_basic_map *bmap, unsigned extra,
1269 unsigned n_eq, unsigned n_ineq)
1270 {
1271 int i;
1272 isl_space *space = isl_basic_map_peek_space(bmap);
1273 isl_size n_var = isl_space_dim(space, isl_dim_all);
1274 size_t row_size = 1 + n_var + extra;
1275
1276 bmap->ctx = ctx;
1277 isl_ctx_ref(ctx);
1278
1279 if (n_var < 0)
1280 return isl_basic_map_free(bmap);
1281
1282 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1283 if (isl_blk_is_error(bmap->block))
1284 goto error;
1285
1286 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1287 if ((n_ineq + n_eq) && !bmap->ineq)
1288 goto error;
1289
1290 if (extra == 0) {
1291 bmap->block2 = isl_blk_empty();
1292 bmap->div = NULL;
1293 } else {
1294 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1295 if (isl_blk_is_error(bmap->block2))
1296 goto error;
1297
1298 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1299 if (!bmap->div)
1300 goto error;
1301 }
1302
1303 for (i = 0; i < n_ineq + n_eq; ++i)
1304 bmap->ineq[i] = bmap->block.data + i * row_size;
1305
1306 for (i = 0; i < extra; ++i)
1307 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1308
1309 bmap->ref = 1;
1310 bmap->flags = 0;
1311 bmap->c_size = n_eq + n_ineq;
1312 bmap->eq = bmap->ineq + n_ineq;
1313 bmap->extra = extra;
1314 bmap->n_eq = 0;
1315 bmap->n_ineq = 0;
1316 bmap->n_div = 0;
1317 bmap->sample = NULL;
1318
1319 return bmap;
1320 error:
1321 isl_basic_map_free(bmap);
1322 return NULL;
1323 }
1324
isl_basic_set_alloc(isl_ctx * ctx,unsigned nparam,unsigned dim,unsigned extra,unsigned n_eq,unsigned n_ineq)1325 __isl_give isl_basic_set *isl_basic_set_alloc(isl_ctx *ctx,
1326 unsigned nparam, unsigned dim, unsigned extra,
1327 unsigned n_eq, unsigned n_ineq)
1328 {
1329 struct isl_basic_map *bmap;
1330 isl_space *space;
1331
1332 space = isl_space_set_alloc(ctx, nparam, dim);
1333 if (!space)
1334 return NULL;
1335
1336 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1337 return bset_from_bmap(bmap);
1338 }
1339
isl_basic_set_alloc_space(__isl_take isl_space * space,unsigned extra,unsigned n_eq,unsigned n_ineq)1340 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *space,
1341 unsigned extra, unsigned n_eq, unsigned n_ineq)
1342 {
1343 struct isl_basic_map *bmap;
1344 if (!space)
1345 return NULL;
1346 isl_assert(space->ctx, space->n_in == 0, goto error);
1347 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1348 return bset_from_bmap(bmap);
1349 error:
1350 isl_space_free(space);
1351 return NULL;
1352 }
1353
isl_basic_map_alloc_space(__isl_take isl_space * space,unsigned extra,unsigned n_eq,unsigned n_ineq)1354 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1355 unsigned extra, unsigned n_eq, unsigned n_ineq)
1356 {
1357 struct isl_basic_map *bmap;
1358
1359 if (!space)
1360 return NULL;
1361 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1362 if (!bmap)
1363 goto error;
1364 bmap->dim = space;
1365
1366 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1367 error:
1368 isl_space_free(space);
1369 return NULL;
1370 }
1371
isl_basic_map_alloc(isl_ctx * ctx,unsigned nparam,unsigned in,unsigned out,unsigned extra,unsigned n_eq,unsigned n_ineq)1372 __isl_give isl_basic_map *isl_basic_map_alloc(isl_ctx *ctx,
1373 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1374 unsigned n_eq, unsigned n_ineq)
1375 {
1376 struct isl_basic_map *bmap;
1377 isl_space *space;
1378
1379 space = isl_space_alloc(ctx, nparam, in, out);
1380 if (!space)
1381 return NULL;
1382
1383 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1384 return bmap;
1385 }
1386
dup_constraints(__isl_take isl_basic_map * dst,__isl_keep isl_basic_map * src)1387 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1388 __isl_keep isl_basic_map *src)
1389 {
1390 int i;
1391 isl_size total = isl_basic_map_dim(src, isl_dim_all);
1392
1393 if (!dst || total < 0)
1394 return isl_basic_map_free(dst);
1395
1396 for (i = 0; i < src->n_eq; ++i) {
1397 int j = isl_basic_map_alloc_equality(dst);
1398 if (j < 0)
1399 return isl_basic_map_free(dst);
1400 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1401 }
1402
1403 for (i = 0; i < src->n_ineq; ++i) {
1404 int j = isl_basic_map_alloc_inequality(dst);
1405 if (j < 0)
1406 return isl_basic_map_free(dst);
1407 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1408 }
1409
1410 for (i = 0; i < src->n_div; ++i) {
1411 int j = isl_basic_map_alloc_div(dst);
1412 if (j < 0)
1413 return isl_basic_map_free(dst);
1414 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1415 }
1416 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1417 return dst;
1418 }
1419
isl_basic_map_dup(__isl_keep isl_basic_map * bmap)1420 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1421 {
1422 struct isl_basic_map *dup;
1423
1424 if (!bmap)
1425 return NULL;
1426 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1427 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1428 dup = dup_constraints(dup, bmap);
1429 if (!dup)
1430 return NULL;
1431 dup->flags = bmap->flags;
1432 dup->sample = isl_vec_copy(bmap->sample);
1433 return dup;
1434 }
1435
isl_basic_set_dup(__isl_keep isl_basic_set * bset)1436 __isl_give isl_basic_set *isl_basic_set_dup(__isl_keep isl_basic_set *bset)
1437 {
1438 struct isl_basic_map *dup;
1439
1440 dup = isl_basic_map_dup(bset_to_bmap(bset));
1441 return bset_from_bmap(dup);
1442 }
1443
isl_basic_set_copy(__isl_keep isl_basic_set * bset)1444 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1445 {
1446 if (!bset)
1447 return NULL;
1448
1449 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1450 bset->ref++;
1451 return bset;
1452 }
1453 return isl_basic_set_dup(bset);
1454 }
1455
isl_set_copy(__isl_keep isl_set * set)1456 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1457 {
1458 if (!set)
1459 return NULL;
1460
1461 set->ref++;
1462 return set;
1463 }
1464
isl_basic_map_copy(__isl_keep isl_basic_map * bmap)1465 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1466 {
1467 if (!bmap)
1468 return NULL;
1469
1470 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1471 bmap->ref++;
1472 return bmap;
1473 }
1474 bmap = isl_basic_map_dup(bmap);
1475 if (bmap)
1476 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1477 return bmap;
1478 }
1479
isl_map_copy(__isl_keep isl_map * map)1480 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1481 {
1482 if (!map)
1483 return NULL;
1484
1485 map->ref++;
1486 return map;
1487 }
1488
isl_basic_map_free(__isl_take isl_basic_map * bmap)1489 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1490 {
1491 if (!bmap)
1492 return NULL;
1493
1494 if (--bmap->ref > 0)
1495 return NULL;
1496
1497 isl_ctx_deref(bmap->ctx);
1498 free(bmap->div);
1499 isl_blk_free(bmap->ctx, bmap->block2);
1500 free(bmap->ineq);
1501 isl_blk_free(bmap->ctx, bmap->block);
1502 isl_vec_free(bmap->sample);
1503 isl_space_free(bmap->dim);
1504 free(bmap);
1505
1506 return NULL;
1507 }
1508
isl_basic_set_free(__isl_take isl_basic_set * bset)1509 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1510 {
1511 return isl_basic_map_free(bset_to_bmap(bset));
1512 }
1513
room_for_con(__isl_keep isl_basic_map * bmap,unsigned n)1514 static int room_for_con(__isl_keep isl_basic_map *bmap, unsigned n)
1515 {
1516 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1517 }
1518
1519 /* Check that "bset" does not involve any parameters.
1520 */
isl_basic_set_check_no_params(__isl_keep isl_basic_set * bset)1521 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1522 {
1523 isl_size nparam;
1524
1525 nparam = isl_basic_set_dim(bset, isl_dim_param);
1526 if (nparam < 0)
1527 return isl_stat_error;
1528 if (nparam != 0)
1529 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1530 "basic set should not have any parameters",
1531 return isl_stat_error);
1532 return isl_stat_ok;
1533 }
1534
1535 /* Check that "bset" does not involve any local variables.
1536 */
isl_basic_set_check_no_locals(__isl_keep isl_basic_set * bset)1537 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1538 {
1539 isl_size n_div;
1540
1541 n_div = isl_basic_set_dim(bset, isl_dim_div);
1542 if (n_div < 0)
1543 return isl_stat_error;
1544 if (n_div != 0)
1545 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1546 "basic set should not have any local variables",
1547 return isl_stat_error);
1548 return isl_stat_ok;
1549 }
1550
1551 #undef TYPE
1552 #define TYPE isl_map
1553
1554 #include "isl_check_named_params_templ.c"
1555
1556 #undef TYPE
1557 #define TYPE isl_basic_map
1558
1559 static
1560 #include "isl_check_named_params_templ.c"
1561
1562 /* Check that "bmap1" and "bmap2" have the same parameters,
1563 * reporting an error if they do not.
1564 */
isl_basic_map_check_equal_params(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)1565 static isl_stat isl_basic_map_check_equal_params(
1566 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1567 {
1568 isl_bool match;
1569
1570 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1571 if (match < 0)
1572 return isl_stat_error;
1573 if (!match)
1574 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1575 "parameters don't match", return isl_stat_error);
1576 return isl_stat_ok;
1577 }
1578
1579 #undef TYPE
1580 #define TYPE isl_map
1581
1582 #include "isl_align_params_bin_templ.c"
1583
1584 #undef SUFFIX
1585 #define SUFFIX set
1586 #undef ARG1
1587 #define ARG1 isl_map
1588 #undef ARG2
1589 #define ARG2 isl_set
1590
1591 #include "isl_align_params_templ.c"
1592
isl_map_align_params_map_map_and_test(__isl_keep isl_map * map1,__isl_keep isl_map * map2,isl_bool (* fn)(__isl_keep isl_map * map1,__isl_keep isl_map * map2))1593 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1594 __isl_keep isl_map *map2,
1595 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1596 {
1597 isl_bool r;
1598
1599 if (!map1 || !map2)
1600 return isl_bool_error;
1601 if (isl_map_has_equal_params(map1, map2))
1602 return fn(map1, map2);
1603 if (isl_map_check_named_params(map1) < 0)
1604 return isl_bool_error;
1605 if (isl_map_check_named_params(map2) < 0)
1606 return isl_bool_error;
1607 map1 = isl_map_copy(map1);
1608 map2 = isl_map_copy(map2);
1609 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1610 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1611 r = fn(map1, map2);
1612 isl_map_free(map1);
1613 isl_map_free(map2);
1614 return r;
1615 }
1616
isl_basic_map_alloc_equality(__isl_keep isl_basic_map * bmap)1617 int isl_basic_map_alloc_equality(__isl_keep isl_basic_map *bmap)
1618 {
1619 isl_size total;
1620 struct isl_ctx *ctx;
1621
1622 total = isl_basic_map_dim(bmap, isl_dim_all);
1623 if (total < 0)
1624 return -1;
1625 ctx = bmap->ctx;
1626 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1627 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1628 return -1);
1629 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1630 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1631 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1632 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1633 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1634 isl_int *t;
1635 int j = isl_basic_map_alloc_inequality(bmap);
1636 if (j < 0)
1637 return -1;
1638 t = bmap->ineq[j];
1639 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1640 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1641 bmap->eq[-1] = t;
1642 bmap->n_eq++;
1643 bmap->n_ineq--;
1644 bmap->eq--;
1645 return 0;
1646 }
1647 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + total,
1648 bmap->extra - bmap->n_div);
1649 return bmap->n_eq++;
1650 }
1651
isl_basic_set_alloc_equality(__isl_keep isl_basic_set * bset)1652 int isl_basic_set_alloc_equality(__isl_keep isl_basic_set *bset)
1653 {
1654 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1655 }
1656
isl_basic_map_free_equality(__isl_take isl_basic_map * bmap,unsigned n)1657 __isl_give isl_basic_map *isl_basic_map_free_equality(
1658 __isl_take isl_basic_map *bmap, unsigned n)
1659 {
1660 if (!bmap)
1661 return NULL;
1662 if (n > bmap->n_eq)
1663 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1664 "invalid number of equalities",
1665 isl_basic_map_free(bmap));
1666 bmap->n_eq -= n;
1667 return bmap;
1668 }
1669
isl_basic_set_free_equality(__isl_take isl_basic_set * bset,unsigned n)1670 __isl_give isl_basic_set *isl_basic_set_free_equality(
1671 __isl_take isl_basic_set *bset, unsigned n)
1672 {
1673 return bset_from_bmap(isl_basic_map_free_equality(bset_to_bmap(bset),
1674 n));
1675 }
1676
1677 /* Drop the equality constraint at position "pos",
1678 * preserving the order of the other equality constraints.
1679 */
isl_basic_map_drop_equality(__isl_keep isl_basic_map * bmap,unsigned pos)1680 int isl_basic_map_drop_equality(__isl_keep isl_basic_map *bmap, unsigned pos)
1681 {
1682 isl_int *t;
1683 int r;
1684
1685 if (!bmap)
1686 return -1;
1687 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1688
1689 t = bmap->eq[pos];
1690 bmap->n_eq--;
1691 for (r = pos; r < bmap->n_eq; ++r)
1692 bmap->eq[r] = bmap->eq[r + 1];
1693 bmap->eq[bmap->n_eq] = t;
1694
1695 return 0;
1696 }
1697
1698 /* Turn inequality "pos" of "bmap" into an equality.
1699 *
1700 * In particular, we move the inequality in front of the equalities
1701 * and move the last inequality in the position of the moved inequality.
1702 * Note that isl_tab_make_equalities_explicit depends on this particular
1703 * change in the ordering of the constraints.
1704 */
isl_basic_map_inequality_to_equality(__isl_keep isl_basic_map * bmap,unsigned pos)1705 void isl_basic_map_inequality_to_equality(
1706 __isl_keep isl_basic_map *bmap, unsigned pos)
1707 {
1708 isl_int *t;
1709
1710 t = bmap->ineq[pos];
1711 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1712 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1713 bmap->eq[-1] = t;
1714 bmap->n_eq++;
1715 bmap->n_ineq--;
1716 bmap->eq--;
1717 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1718 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1719 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1720 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1721 }
1722
room_for_ineq(__isl_keep isl_basic_map * bmap,unsigned n)1723 static int room_for_ineq(__isl_keep isl_basic_map *bmap, unsigned n)
1724 {
1725 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1726 }
1727
isl_basic_map_alloc_inequality(__isl_keep isl_basic_map * bmap)1728 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1729 {
1730 isl_size total;
1731 struct isl_ctx *ctx;
1732
1733 total = isl_basic_map_dim(bmap, isl_dim_all);
1734 if (total < 0)
1735 return -1;
1736 ctx = bmap->ctx;
1737 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1738 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1739 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1740 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1741 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1742 isl_seq_clr(bmap->ineq[bmap->n_ineq] + 1 + total,
1743 bmap->extra - bmap->n_div);
1744 return bmap->n_ineq++;
1745 }
1746
isl_basic_set_alloc_inequality(__isl_keep isl_basic_set * bset)1747 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1748 {
1749 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1750 }
1751
isl_basic_map_free_inequality(__isl_take isl_basic_map * bmap,unsigned n)1752 __isl_give isl_basic_map *isl_basic_map_free_inequality(
1753 __isl_take isl_basic_map *bmap, unsigned n)
1754 {
1755 if (!bmap)
1756 return NULL;
1757 if (n > bmap->n_ineq)
1758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1759 "invalid number of inequalities",
1760 return isl_basic_map_free(bmap));
1761 bmap->n_ineq -= n;
1762 return bmap;
1763 }
1764
isl_basic_set_free_inequality(__isl_take isl_basic_set * bset,unsigned n)1765 __isl_give isl_basic_set *isl_basic_set_free_inequality(
1766 __isl_take isl_basic_set *bset, unsigned n)
1767 {
1768 return bset_from_bmap(isl_basic_map_free_inequality(bset_to_bmap(bset),
1769 n));
1770 }
1771
isl_basic_map_drop_inequality(__isl_keep isl_basic_map * bmap,unsigned pos)1772 int isl_basic_map_drop_inequality(__isl_keep isl_basic_map *bmap, unsigned pos)
1773 {
1774 isl_int *t;
1775 if (!bmap)
1776 return -1;
1777 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1778
1779 if (pos != bmap->n_ineq - 1) {
1780 t = bmap->ineq[pos];
1781 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1782 bmap->ineq[bmap->n_ineq - 1] = t;
1783 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1784 }
1785 bmap->n_ineq--;
1786 return 0;
1787 }
1788
isl_basic_set_drop_inequality(__isl_keep isl_basic_set * bset,unsigned pos)1789 int isl_basic_set_drop_inequality(__isl_keep isl_basic_set *bset, unsigned pos)
1790 {
1791 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1792 }
1793
isl_basic_map_add_eq(__isl_take isl_basic_map * bmap,isl_int * eq)1794 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1795 isl_int *eq)
1796 {
1797 isl_bool empty;
1798 isl_size total;
1799 int k;
1800
1801 empty = isl_basic_map_plain_is_empty(bmap);
1802 if (empty < 0)
1803 return isl_basic_map_free(bmap);
1804 if (empty)
1805 return bmap;
1806
1807 bmap = isl_basic_map_cow(bmap);
1808 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1809 total = isl_basic_map_dim(bmap, isl_dim_all);
1810 if (total < 0)
1811 return isl_basic_map_free(bmap);
1812 k = isl_basic_map_alloc_equality(bmap);
1813 if (k < 0)
1814 goto error;
1815 isl_seq_cpy(bmap->eq[k], eq, 1 + total);
1816 return bmap;
1817 error:
1818 isl_basic_map_free(bmap);
1819 return NULL;
1820 }
1821
isl_basic_set_add_eq(__isl_take isl_basic_set * bset,isl_int * eq)1822 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1823 isl_int *eq)
1824 {
1825 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1826 }
1827
isl_basic_map_add_ineq(__isl_take isl_basic_map * bmap,isl_int * ineq)1828 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1829 isl_int *ineq)
1830 {
1831 isl_size total;
1832 int k;
1833
1834 bmap = isl_basic_map_cow(bmap);
1835 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1836 total = isl_basic_map_dim(bmap, isl_dim_all);
1837 if (total < 0)
1838 return isl_basic_map_free(bmap);
1839 k = isl_basic_map_alloc_inequality(bmap);
1840 if (k < 0)
1841 goto error;
1842 isl_seq_cpy(bmap->ineq[k], ineq, 1 + total);
1843 return bmap;
1844 error:
1845 isl_basic_map_free(bmap);
1846 return NULL;
1847 }
1848
isl_basic_set_add_ineq(__isl_take isl_basic_set * bset,isl_int * ineq)1849 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1850 isl_int *ineq)
1851 {
1852 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1853 }
1854
isl_basic_map_alloc_div(__isl_keep isl_basic_map * bmap)1855 int isl_basic_map_alloc_div(__isl_keep isl_basic_map *bmap)
1856 {
1857 isl_size total;
1858
1859 total = isl_basic_map_dim(bmap, isl_dim_all);
1860 if (total < 0)
1861 return -1;
1862 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1863 isl_seq_clr(bmap->div[bmap->n_div] + 1 + 1 + total,
1864 bmap->extra - bmap->n_div);
1865 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1866 return bmap->n_div++;
1867 }
1868
isl_basic_set_alloc_div(__isl_keep isl_basic_set * bset)1869 int isl_basic_set_alloc_div(__isl_keep isl_basic_set *bset)
1870 {
1871 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1872 }
1873
1874 #undef TYPE
1875 #define TYPE isl_basic_map
1876 #include "check_type_range_templ.c"
1877
1878 /* Check that there are "n" dimensions of type "type" starting at "first"
1879 * in "bset".
1880 */
isl_basic_set_check_range(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)1881 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1882 enum isl_dim_type type, unsigned first, unsigned n)
1883 {
1884 return isl_basic_map_check_range(bset_to_bmap(bset),
1885 type, first, n);
1886 }
1887
1888 /* Insert an extra integer division, prescribed by "div", to "bmap"
1889 * at (integer division) position "pos".
1890 *
1891 * The integer division is first added at the end and then moved
1892 * into the right position.
1893 */
isl_basic_map_insert_div(__isl_take isl_basic_map * bmap,int pos,__isl_keep isl_vec * div)1894 __isl_give isl_basic_map *isl_basic_map_insert_div(
1895 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1896 {
1897 int i, k;
1898 isl_size total;
1899
1900 bmap = isl_basic_map_cow(bmap);
1901 total = isl_basic_map_dim(bmap, isl_dim_all);
1902 if (total < 0 || !div)
1903 return isl_basic_map_free(bmap);
1904
1905 if (div->size != 1 + 1 + total)
1906 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1907 "unexpected size", return isl_basic_map_free(bmap));
1908 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1909 return isl_basic_map_free(bmap);
1910
1911 bmap = isl_basic_map_extend(bmap, 1, 0, 2);
1912 k = isl_basic_map_alloc_div(bmap);
1913 if (k < 0)
1914 return isl_basic_map_free(bmap);
1915 isl_seq_cpy(bmap->div[k], div->el, div->size);
1916 isl_int_set_si(bmap->div[k][div->size], 0);
1917
1918 for (i = k; i > pos; --i)
1919 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1920
1921 return bmap;
1922 }
1923
isl_basic_map_free_div(__isl_keep isl_basic_map * bmap,unsigned n)1924 isl_stat isl_basic_map_free_div(__isl_keep isl_basic_map *bmap, unsigned n)
1925 {
1926 if (!bmap)
1927 return isl_stat_error;
1928 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1929 bmap->n_div -= n;
1930 return isl_stat_ok;
1931 }
1932
add_constraints(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2,unsigned i_pos,unsigned o_pos)1933 static __isl_give isl_basic_map *add_constraints(
1934 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1935 unsigned i_pos, unsigned o_pos)
1936 {
1937 isl_size total, n_param, n_in, n_out, n_div;
1938 unsigned o_in, o_out;
1939 isl_ctx *ctx;
1940 isl_space *space;
1941 struct isl_dim_map *dim_map;
1942
1943 space = isl_basic_map_peek_space(bmap2);
1944 if (!bmap1 || !space)
1945 goto error;
1946
1947 total = isl_basic_map_dim(bmap1, isl_dim_all);
1948 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1949 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1950 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1951 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1952 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1953 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1954 if (total < 0 || n_param < 0 || n_in < 0 || n_out < 0 || n_div < 0)
1955 goto error;
1956 ctx = isl_basic_map_get_ctx(bmap1);
1957 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1958 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1959 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1960 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1961 isl_dim_map_div(dim_map, bmap2, total);
1962
1963 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1964 error:
1965 isl_basic_map_free(bmap1);
1966 isl_basic_map_free(bmap2);
1967 return NULL;
1968 }
1969
isl_basic_map_extend(__isl_take isl_basic_map * base,unsigned extra,unsigned n_eq,unsigned n_ineq)1970 __isl_give isl_basic_map *isl_basic_map_extend(__isl_take isl_basic_map *base,
1971 unsigned extra, unsigned n_eq, unsigned n_ineq)
1972 {
1973 isl_space *space;
1974 struct isl_basic_map *ext;
1975 unsigned flags;
1976 int dims_ok;
1977
1978 if (!base)
1979 goto error;
1980
1981 dims_ok = base->extra >= base->n_div + extra;
1982
1983 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1984 room_for_ineq(base, n_ineq))
1985 return base;
1986
1987 extra += base->extra;
1988 n_eq += base->n_eq;
1989 n_ineq += base->n_ineq;
1990
1991 space = isl_basic_map_get_space(base);
1992 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1993 if (!ext)
1994 goto error;
1995
1996 if (dims_ok)
1997 ext->sample = isl_vec_copy(base->sample);
1998 flags = base->flags;
1999 ext = add_constraints(ext, base, 0, 0);
2000 if (ext) {
2001 ext->flags = flags;
2002 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
2003 }
2004
2005 return ext;
2006
2007 error:
2008 isl_basic_map_free(base);
2009 return NULL;
2010 }
2011
isl_basic_set_extend(__isl_take isl_basic_set * base,unsigned extra,unsigned n_eq,unsigned n_ineq)2012 __isl_give isl_basic_set *isl_basic_set_extend(__isl_take isl_basic_set *base,
2013 unsigned extra, unsigned n_eq, unsigned n_ineq)
2014 {
2015 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
2016 extra, n_eq, n_ineq));
2017 }
2018
isl_basic_map_extend_constraints(__isl_take isl_basic_map * base,unsigned n_eq,unsigned n_ineq)2019 __isl_give isl_basic_map *isl_basic_map_extend_constraints(
2020 __isl_take isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
2021 {
2022 return isl_basic_map_extend(base, 0, n_eq, n_ineq);
2023 }
2024
isl_basic_set_extend_constraints(__isl_take isl_basic_set * base,unsigned n_eq,unsigned n_ineq)2025 __isl_give isl_basic_set *isl_basic_set_extend_constraints(
2026 __isl_take isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
2027 {
2028 isl_basic_map *bmap = bset_to_bmap(base);
2029 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
2030 return bset_from_bmap(bmap);
2031 }
2032
isl_basic_set_cow(__isl_take isl_basic_set * bset)2033 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
2034 {
2035 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
2036 }
2037
isl_basic_map_cow(__isl_take isl_basic_map * bmap)2038 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
2039 {
2040 if (!bmap)
2041 return NULL;
2042
2043 if (bmap->ref > 1) {
2044 bmap->ref--;
2045 bmap = isl_basic_map_dup(bmap);
2046 }
2047 if (bmap) {
2048 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
2049 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
2050 }
2051 return bmap;
2052 }
2053
2054 /* Clear all cached information in "map", either because it is about
2055 * to be modified or because it is being freed.
2056 * Always return the same pointer that is passed in.
2057 * This is needed for the use in isl_map_free.
2058 */
clear_caches(__isl_take isl_map * map)2059 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
2060 {
2061 isl_basic_map_free(map->cached_simple_hull[0]);
2062 isl_basic_map_free(map->cached_simple_hull[1]);
2063 map->cached_simple_hull[0] = NULL;
2064 map->cached_simple_hull[1] = NULL;
2065 return map;
2066 }
2067
isl_set_cow(__isl_take isl_set * set)2068 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
2069 {
2070 return isl_map_cow(set);
2071 }
2072
2073 /* Return an isl_map that is equal to "map" and that has only
2074 * a single reference.
2075 *
2076 * If the original input already has only one reference, then
2077 * simply return it, but clear all cached information, since
2078 * it may be rendered invalid by the operations that will be
2079 * performed on the result.
2080 *
2081 * Otherwise, create a duplicate (without any cached information).
2082 */
isl_map_cow(__isl_take isl_map * map)2083 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
2084 {
2085 if (!map)
2086 return NULL;
2087
2088 if (map->ref == 1)
2089 return clear_caches(map);
2090 map->ref--;
2091 return isl_map_dup(map);
2092 }
2093
swap_vars(struct isl_blk blk,isl_int * a,unsigned a_len,unsigned b_len)2094 static void swap_vars(struct isl_blk blk, isl_int *a,
2095 unsigned a_len, unsigned b_len)
2096 {
2097 isl_seq_cpy(blk.data, a+a_len, b_len);
2098 isl_seq_cpy(blk.data+b_len, a, a_len);
2099 isl_seq_cpy(a, blk.data, b_len+a_len);
2100 }
2101
isl_basic_map_swap_vars(__isl_take isl_basic_map * bmap,unsigned pos,unsigned n1,unsigned n2)2102 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2103 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2104 {
2105 int i;
2106 struct isl_blk blk;
2107
2108 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2109 goto error;
2110
2111 if (n1 == 0 || n2 == 0)
2112 return bmap;
2113
2114 bmap = isl_basic_map_cow(bmap);
2115 if (!bmap)
2116 return NULL;
2117
2118 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2119 if (isl_blk_is_error(blk))
2120 goto error;
2121
2122 for (i = 0; i < bmap->n_eq; ++i)
2123 swap_vars(blk,
2124 bmap->eq[i] + pos, n1, n2);
2125
2126 for (i = 0; i < bmap->n_ineq; ++i)
2127 swap_vars(blk,
2128 bmap->ineq[i] + pos, n1, n2);
2129
2130 for (i = 0; i < bmap->n_div; ++i)
2131 swap_vars(blk,
2132 bmap->div[i]+1 + pos, n1, n2);
2133
2134 isl_blk_free(bmap->ctx, blk);
2135
2136 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2137 bmap = isl_basic_map_gauss(bmap, NULL);
2138 return isl_basic_map_finalize(bmap);
2139 error:
2140 isl_basic_map_free(bmap);
2141 return NULL;
2142 }
2143
2144 /* The given basic map has turned out to be empty.
2145 * Explicitly mark it as such and change the representation
2146 * to a canonical representation of the empty basic map.
2147 * Since the basic map has conflicting constraints,
2148 * it must have at least one constraint, except perhaps
2149 * if it was already explicitly marked as being empty.
2150 * Do nothing in the latter case, i.e., if it has been marked empty and
2151 * has no constraints.
2152 */
isl_basic_map_set_to_empty(__isl_take isl_basic_map * bmap)2153 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2154 __isl_take isl_basic_map *bmap)
2155 {
2156 int i = 0;
2157 isl_bool empty;
2158 isl_size n;
2159 isl_size total;
2160
2161 n = isl_basic_map_n_constraint(bmap);
2162 empty = isl_basic_map_plain_is_empty(bmap);
2163 if (n < 0 || empty < 0)
2164 return isl_basic_map_free(bmap);
2165 if (n == 0 && empty)
2166 return bmap;
2167 total = isl_basic_map_dim(bmap, isl_dim_all);
2168 if (total < 0)
2169 return isl_basic_map_free(bmap);
2170 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2171 return isl_basic_map_free(bmap);
2172 bmap = isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2173 if (!bmap)
2174 return NULL;
2175 if (bmap->n_eq > 0) {
2176 bmap = isl_basic_map_free_equality(bmap, bmap->n_eq - 1);
2177 if (!bmap)
2178 return NULL;
2179 } else {
2180 i = isl_basic_map_alloc_equality(bmap);
2181 if (i < 0)
2182 goto error;
2183 }
2184 isl_int_set_si(bmap->eq[i][0], 1);
2185 isl_seq_clr(bmap->eq[i]+1, total);
2186 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2187 isl_vec_free(bmap->sample);
2188 bmap->sample = NULL;
2189 return isl_basic_map_finalize(bmap);
2190 error:
2191 isl_basic_map_free(bmap);
2192 return NULL;
2193 }
2194
isl_basic_set_set_to_empty(__isl_take isl_basic_set * bset)2195 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2196 __isl_take isl_basic_set *bset)
2197 {
2198 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2199 }
2200
isl_basic_map_set_rational(__isl_take isl_basic_map * bmap)2201 __isl_give isl_basic_map *isl_basic_map_set_rational(
2202 __isl_take isl_basic_map *bmap)
2203 {
2204 if (!bmap)
2205 return NULL;
2206
2207 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2208 return bmap;
2209
2210 bmap = isl_basic_map_cow(bmap);
2211 if (!bmap)
2212 return NULL;
2213
2214 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2215
2216 return isl_basic_map_finalize(bmap);
2217 }
2218
isl_basic_set_set_rational(__isl_take isl_basic_set * bset)2219 __isl_give isl_basic_set *isl_basic_set_set_rational(
2220 __isl_take isl_basic_set *bset)
2221 {
2222 return isl_basic_map_set_rational(bset);
2223 }
2224
isl_basic_set_set_integral(__isl_take isl_basic_set * bset)2225 __isl_give isl_basic_set *isl_basic_set_set_integral(
2226 __isl_take isl_basic_set *bset)
2227 {
2228 if (!bset)
2229 return NULL;
2230
2231 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2232 return bset;
2233
2234 bset = isl_basic_set_cow(bset);
2235 if (!bset)
2236 return NULL;
2237
2238 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2239
2240 return isl_basic_set_finalize(bset);
2241 }
2242
isl_map_set_rational(__isl_take isl_map * map)2243 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2244 {
2245 int i;
2246
2247 map = isl_map_cow(map);
2248 if (!map)
2249 return NULL;
2250 for (i = 0; i < map->n; ++i) {
2251 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2252 if (!map->p[i])
2253 goto error;
2254 }
2255 return map;
2256 error:
2257 isl_map_free(map);
2258 return NULL;
2259 }
2260
isl_set_set_rational(__isl_take isl_set * set)2261 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2262 {
2263 return isl_map_set_rational(set);
2264 }
2265
2266 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2267 * of "bmap").
2268 */
swap_div(__isl_keep isl_basic_map * bmap,int a,int b)2269 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2270 {
2271 isl_int *t = bmap->div[a];
2272 bmap->div[a] = bmap->div[b];
2273 bmap->div[b] = t;
2274 }
2275
2276 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2277 * div definitions accordingly.
2278 */
isl_basic_map_swap_div(__isl_take isl_basic_map * bmap,int a,int b)2279 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2280 int a, int b)
2281 {
2282 int i;
2283 isl_size off;
2284
2285 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2286 if (off < 0)
2287 return isl_basic_map_free(bmap);
2288
2289 swap_div(bmap, a, b);
2290
2291 for (i = 0; i < bmap->n_eq; ++i)
2292 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2293
2294 for (i = 0; i < bmap->n_ineq; ++i)
2295 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2296
2297 for (i = 0; i < bmap->n_div; ++i)
2298 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2299 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2300
2301 return bmap;
2302 }
2303
constraint_drop_vars(isl_int * c,unsigned n,unsigned rem)2304 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2305 {
2306 isl_seq_cpy(c, c + n, rem);
2307 isl_seq_clr(c + rem, n);
2308 }
2309
2310 /* Drop n dimensions starting at first.
2311 *
2312 * In principle, this frees up some extra variables as the number
2313 * of columns remains constant, but we would have to extend
2314 * the div array too as the number of rows in this array is assumed
2315 * to be equal to extra.
2316 */
isl_basic_set_drop_dims(__isl_take isl_basic_set * bset,unsigned first,unsigned n)2317 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2318 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2319 {
2320 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2321 }
2322
2323 /* Move "n" divs starting at "first" to the end of the list of divs.
2324 */
move_divs_last(__isl_take isl_basic_map * bmap,unsigned first,unsigned n)2325 static __isl_give isl_basic_map *move_divs_last(__isl_take isl_basic_map *bmap,
2326 unsigned first, unsigned n)
2327 {
2328 isl_int **div;
2329 int i;
2330
2331 if (first + n == bmap->n_div)
2332 return bmap;
2333
2334 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2335 if (!div)
2336 goto error;
2337 for (i = 0; i < n; ++i)
2338 div[i] = bmap->div[first + i];
2339 for (i = 0; i < bmap->n_div - first - n; ++i)
2340 bmap->div[first + i] = bmap->div[first + n + i];
2341 for (i = 0; i < n; ++i)
2342 bmap->div[bmap->n_div - n + i] = div[i];
2343 free(div);
2344 return bmap;
2345 error:
2346 isl_basic_map_free(bmap);
2347 return NULL;
2348 }
2349
2350 #undef TYPE
2351 #define TYPE isl_map
2352 static
2353 #include "check_type_range_templ.c"
2354
2355 /* Check that there are "n" dimensions of type "type" starting at "first"
2356 * in "set".
2357 */
isl_set_check_range(__isl_keep isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)2358 isl_stat isl_set_check_range(__isl_keep isl_set *set,
2359 enum isl_dim_type type, unsigned first, unsigned n)
2360 {
2361 return isl_map_check_range(set_to_map(set), type, first, n);
2362 }
2363
2364 /* Drop "n" dimensions of type "type" starting at "first".
2365 * Perform the core computation, without cowing or
2366 * simplifying and finalizing the result.
2367 *
2368 * In principle, this frees up some extra variables as the number
2369 * of columns remains constant, but we would have to extend
2370 * the div array too as the number of rows in this array is assumed
2371 * to be equal to extra.
2372 */
isl_basic_map_drop_core(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)2373 __isl_give isl_basic_map *isl_basic_map_drop_core(
2374 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2375 unsigned first, unsigned n)
2376 {
2377 int i;
2378 unsigned offset;
2379 unsigned left;
2380 isl_size total;
2381
2382 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2383 return isl_basic_map_free(bmap);
2384
2385 total = isl_basic_map_dim(bmap, isl_dim_all);
2386 if (total < 0)
2387 return isl_basic_map_free(bmap);
2388
2389 offset = isl_basic_map_offset(bmap, type) + first;
2390 left = total - (offset - 1) - n;
2391 for (i = 0; i < bmap->n_eq; ++i)
2392 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2393
2394 for (i = 0; i < bmap->n_ineq; ++i)
2395 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2396
2397 for (i = 0; i < bmap->n_div; ++i)
2398 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2399
2400 if (type == isl_dim_div) {
2401 bmap = move_divs_last(bmap, first, n);
2402 if (!bmap)
2403 return NULL;
2404 if (isl_basic_map_free_div(bmap, n) < 0)
2405 return isl_basic_map_free(bmap);
2406 } else
2407 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2408 if (!bmap->dim)
2409 return isl_basic_map_free(bmap);
2410
2411 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2412 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2413 return bmap;
2414 }
2415
2416 /* Drop "n" dimensions of type "type" starting at "first".
2417 *
2418 * In principle, this frees up some extra variables as the number
2419 * of columns remains constant, but we would have to extend
2420 * the div array too as the number of rows in this array is assumed
2421 * to be equal to extra.
2422 */
isl_basic_map_drop(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)2423 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2425 {
2426 if (!bmap)
2427 return NULL;
2428 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2429 return bmap;
2430
2431 bmap = isl_basic_map_cow(bmap);
2432 if (!bmap)
2433 return NULL;
2434
2435 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2436
2437 bmap = isl_basic_map_simplify(bmap);
2438 return isl_basic_map_finalize(bmap);
2439 }
2440
isl_basic_set_drop(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)2441 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2442 enum isl_dim_type type, unsigned first, unsigned n)
2443 {
2444 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2445 type, first, n));
2446 }
2447
2448 /* No longer consider "map" to be normalized.
2449 */
isl_map_unmark_normalized(__isl_take isl_map * map)2450 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2451 {
2452 if (!map)
2453 return NULL;
2454 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2455 return map;
2456 }
2457
isl_map_drop(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)2458 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2459 enum isl_dim_type type, unsigned first, unsigned n)
2460 {
2461 int i;
2462 isl_space *space;
2463
2464 if (isl_map_check_range(map, type, first, n) < 0)
2465 return isl_map_free(map);
2466
2467 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2468 return map;
2469 map = isl_map_cow(map);
2470 if (!map)
2471 goto error;
2472
2473 for (i = 0; i < map->n; ++i) {
2474 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2475 if (!map->p[i])
2476 goto error;
2477 }
2478 map = isl_map_unmark_normalized(map);
2479
2480 space = isl_map_take_space(map);
2481 space = isl_space_drop_dims(space, type, first, n);
2482 map = isl_map_restore_space(map, space);
2483
2484 return map;
2485 error:
2486 isl_map_free(map);
2487 return NULL;
2488 }
2489
isl_set_drop(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)2490 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2491 enum isl_dim_type type, unsigned first, unsigned n)
2492 {
2493 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2494 }
2495
2496 /* Drop the integer division at position "div", which is assumed
2497 * not to appear in any of the constraints or
2498 * in any of the other integer divisions.
2499 *
2500 * Since the integer division is redundant, there is no need to cow.
2501 */
isl_basic_map_drop_div(__isl_take isl_basic_map * bmap,unsigned div)2502 __isl_give isl_basic_map *isl_basic_map_drop_div(
2503 __isl_take isl_basic_map *bmap, unsigned div)
2504 {
2505 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2506 }
2507
2508 /* Eliminate the specified n dimensions starting at first from the
2509 * constraints, without removing the dimensions from the space.
2510 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2511 */
isl_map_eliminate(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)2512 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2513 enum isl_dim_type type, unsigned first, unsigned n)
2514 {
2515 int i;
2516
2517 if (n == 0)
2518 return map;
2519
2520 if (isl_map_check_range(map, type, first, n) < 0)
2521 return isl_map_free(map);
2522
2523 map = isl_map_cow(map);
2524 if (!map)
2525 return NULL;
2526
2527 for (i = 0; i < map->n; ++i) {
2528 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2529 if (!map->p[i])
2530 goto error;
2531 }
2532 return map;
2533 error:
2534 isl_map_free(map);
2535 return NULL;
2536 }
2537
2538 /* Eliminate the specified n dimensions starting at first from the
2539 * constraints, without removing the dimensions from the space.
2540 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2541 */
isl_set_eliminate(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)2542 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2543 enum isl_dim_type type, unsigned first, unsigned n)
2544 {
2545 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2546 }
2547
2548 /* Eliminate the specified n dimensions starting at first from the
2549 * constraints, without removing the dimensions from the space.
2550 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2551 */
isl_set_eliminate_dims(__isl_take isl_set * set,unsigned first,unsigned n)2552 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2553 unsigned first, unsigned n)
2554 {
2555 return isl_set_eliminate(set, isl_dim_set, first, n);
2556 }
2557
isl_basic_map_remove_divs(__isl_take isl_basic_map * bmap)2558 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2559 __isl_take isl_basic_map *bmap)
2560 {
2561 isl_size v_div;
2562
2563 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2564 if (v_div < 0)
2565 return isl_basic_map_free(bmap);
2566 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2567 if (!bmap)
2568 return NULL;
2569 bmap->n_div = 0;
2570 return isl_basic_map_finalize(bmap);
2571 }
2572
isl_basic_set_remove_divs(__isl_take isl_basic_set * bset)2573 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2574 __isl_take isl_basic_set *bset)
2575 {
2576 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2577 }
2578
isl_map_remove_divs(__isl_take isl_map * map)2579 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2580 {
2581 int i;
2582
2583 if (!map)
2584 return NULL;
2585 if (map->n == 0)
2586 return map;
2587
2588 map = isl_map_cow(map);
2589 if (!map)
2590 return NULL;
2591
2592 for (i = 0; i < map->n; ++i) {
2593 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2594 if (!map->p[i])
2595 goto error;
2596 }
2597 return map;
2598 error:
2599 isl_map_free(map);
2600 return NULL;
2601 }
2602
isl_set_remove_divs(__isl_take isl_set * set)2603 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2604 {
2605 return isl_map_remove_divs(set);
2606 }
2607
isl_basic_map_remove_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)2608 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2609 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2610 unsigned first, unsigned n)
2611 {
2612 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2613 return isl_basic_map_free(bmap);
2614 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2615 return bmap;
2616 bmap = isl_basic_map_eliminate_vars(bmap,
2617 isl_basic_map_offset(bmap, type) - 1 + first, n);
2618 if (!bmap)
2619 return bmap;
2620 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2621 return bmap;
2622 bmap = isl_basic_map_drop(bmap, type, first, n);
2623 return bmap;
2624 }
2625
2626 /* Return true if the definition of the given div (recursively) involves
2627 * any of the given variables.
2628 */
div_involves_vars(__isl_keep isl_basic_map * bmap,int div,unsigned first,unsigned n)2629 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2630 unsigned first, unsigned n)
2631 {
2632 int i;
2633 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2634
2635 if (isl_int_is_zero(bmap->div[div][0]))
2636 return isl_bool_false;
2637 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2638 return isl_bool_true;
2639
2640 for (i = bmap->n_div - 1; i >= 0; --i) {
2641 isl_bool involves;
2642
2643 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2644 continue;
2645 involves = div_involves_vars(bmap, i, first, n);
2646 if (involves < 0 || involves)
2647 return involves;
2648 }
2649
2650 return isl_bool_false;
2651 }
2652
2653 /* Try and add a lower and/or upper bound on "div" to "bmap"
2654 * based on inequality "i".
2655 * "total" is the total number of variables (excluding the divs).
2656 * "v" is a temporary object that can be used during the calculations.
2657 * If "lb" is set, then a lower bound should be constructed.
2658 * If "ub" is set, then an upper bound should be constructed.
2659 *
2660 * The calling function has already checked that the inequality does not
2661 * reference "div", but we still need to check that the inequality is
2662 * of the right form. We'll consider the case where we want to construct
2663 * a lower bound. The construction of upper bounds is similar.
2664 *
2665 * Let "div" be of the form
2666 *
2667 * q = floor((a + f(x))/d)
2668 *
2669 * We essentially check if constraint "i" is of the form
2670 *
2671 * b + f(x) >= 0
2672 *
2673 * so that we can use it to derive a lower bound on "div".
2674 * However, we allow a slightly more general form
2675 *
2676 * b + g(x) >= 0
2677 *
2678 * with the condition that the coefficients of g(x) - f(x) are all
2679 * divisible by d.
2680 * Rewriting this constraint as
2681 *
2682 * 0 >= -b - g(x)
2683 *
2684 * adding a + f(x) to both sides and dividing by d, we obtain
2685 *
2686 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2687 *
2688 * Taking the floor on both sides, we obtain
2689 *
2690 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2691 *
2692 * or
2693 *
2694 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2695 *
2696 * In the case of an upper bound, we construct the constraint
2697 *
2698 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2699 *
2700 */
insert_bounds_on_div_from_ineq(__isl_take isl_basic_map * bmap,int div,int i,unsigned total,isl_int v,int lb,int ub)2701 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2702 __isl_take isl_basic_map *bmap, int div, int i,
2703 unsigned total, isl_int v, int lb, int ub)
2704 {
2705 int j;
2706
2707 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2708 if (lb) {
2709 isl_int_sub(v, bmap->ineq[i][1 + j],
2710 bmap->div[div][1 + 1 + j]);
2711 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2712 }
2713 if (ub) {
2714 isl_int_add(v, bmap->ineq[i][1 + j],
2715 bmap->div[div][1 + 1 + j]);
2716 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2717 }
2718 }
2719 if (!lb && !ub)
2720 return bmap;
2721
2722 bmap = isl_basic_map_cow(bmap);
2723 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2724 if (lb) {
2725 int k = isl_basic_map_alloc_inequality(bmap);
2726 if (k < 0)
2727 goto error;
2728 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2729 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2730 bmap->div[div][1 + j]);
2731 isl_int_cdiv_q(bmap->ineq[k][j],
2732 bmap->ineq[k][j], bmap->div[div][0]);
2733 }
2734 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2735 }
2736 if (ub) {
2737 int k = isl_basic_map_alloc_inequality(bmap);
2738 if (k < 0)
2739 goto error;
2740 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2741 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2742 bmap->div[div][1 + j]);
2743 isl_int_fdiv_q(bmap->ineq[k][j],
2744 bmap->ineq[k][j], bmap->div[div][0]);
2745 }
2746 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2747 }
2748
2749 return bmap;
2750 error:
2751 isl_basic_map_free(bmap);
2752 return NULL;
2753 }
2754
2755 /* This function is called right before "div" is eliminated from "bmap"
2756 * using Fourier-Motzkin.
2757 * Look through the constraints of "bmap" for constraints on the argument
2758 * of the integer division and use them to construct constraints on the
2759 * integer division itself. These constraints can then be combined
2760 * during the Fourier-Motzkin elimination.
2761 * Note that it is only useful to introduce lower bounds on "div"
2762 * if "bmap" already contains upper bounds on "div" as the newly
2763 * introduce lower bounds can then be combined with the pre-existing
2764 * upper bounds. Similarly for upper bounds.
2765 * We therefore first check if "bmap" contains any lower and/or upper bounds
2766 * on "div".
2767 *
2768 * It is interesting to note that the introduction of these constraints
2769 * can indeed lead to more accurate results, even when compared to
2770 * deriving constraints on the argument of "div" from constraints on "div".
2771 * Consider, for example, the set
2772 *
2773 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2774 *
2775 * The second constraint can be rewritten as
2776 *
2777 * 2 * [(-i-2j+3)/4] + k >= 0
2778 *
2779 * from which we can derive
2780 *
2781 * -i - 2j + 3 >= -2k
2782 *
2783 * or
2784 *
2785 * i + 2j <= 3 + 2k
2786 *
2787 * Combined with the first constraint, we obtain
2788 *
2789 * -3 <= 3 + 2k or k >= -3
2790 *
2791 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2792 * the first constraint, we obtain
2793 *
2794 * [(i + 2j)/4] >= [-3/4] = -1
2795 *
2796 * Combining this constraint with the second constraint, we obtain
2797 *
2798 * k >= -2
2799 */
insert_bounds_on_div(__isl_take isl_basic_map * bmap,int div)2800 static __isl_give isl_basic_map *insert_bounds_on_div(
2801 __isl_take isl_basic_map *bmap, int div)
2802 {
2803 int i;
2804 int check_lb, check_ub;
2805 isl_int v;
2806 isl_size v_div;
2807
2808 if (!bmap)
2809 return NULL;
2810
2811 if (isl_int_is_zero(bmap->div[div][0]))
2812 return bmap;
2813
2814 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2815 if (v_div < 0)
2816 return isl_basic_map_free(bmap);
2817
2818 check_lb = 0;
2819 check_ub = 0;
2820 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2821 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2822 if (s > 0)
2823 check_ub = 1;
2824 if (s < 0)
2825 check_lb = 1;
2826 }
2827
2828 if (!check_lb && !check_ub)
2829 return bmap;
2830
2831 isl_int_init(v);
2832
2833 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2834 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2835 continue;
2836
2837 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2838 check_lb, check_ub);
2839 }
2840
2841 isl_int_clear(v);
2842
2843 return bmap;
2844 }
2845
2846 /* Remove all divs (recursively) involving any of the given dimensions
2847 * in their definitions.
2848 */
isl_basic_map_remove_divs_involving_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)2849 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2850 __isl_take isl_basic_map *bmap,
2851 enum isl_dim_type type, unsigned first, unsigned n)
2852 {
2853 int i;
2854
2855 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2856 return isl_basic_map_free(bmap);
2857 first += isl_basic_map_offset(bmap, type);
2858
2859 for (i = bmap->n_div - 1; i >= 0; --i) {
2860 isl_bool involves;
2861
2862 involves = div_involves_vars(bmap, i, first, n);
2863 if (involves < 0)
2864 return isl_basic_map_free(bmap);
2865 if (!involves)
2866 continue;
2867 bmap = insert_bounds_on_div(bmap, i);
2868 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2869 if (!bmap)
2870 return NULL;
2871 i = bmap->n_div;
2872 }
2873
2874 return bmap;
2875 }
2876
isl_basic_set_remove_divs_involving_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)2877 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2878 __isl_take isl_basic_set *bset,
2879 enum isl_dim_type type, unsigned first, unsigned n)
2880 {
2881 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2882 }
2883
isl_map_remove_divs_involving_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)2884 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2885 enum isl_dim_type type, unsigned first, unsigned n)
2886 {
2887 int i;
2888
2889 if (!map)
2890 return NULL;
2891 if (map->n == 0)
2892 return map;
2893
2894 map = isl_map_cow(map);
2895 if (!map)
2896 return NULL;
2897
2898 for (i = 0; i < map->n; ++i) {
2899 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2900 type, first, n);
2901 if (!map->p[i])
2902 goto error;
2903 }
2904 return map;
2905 error:
2906 isl_map_free(map);
2907 return NULL;
2908 }
2909
isl_set_remove_divs_involving_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)2910 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2911 enum isl_dim_type type, unsigned first, unsigned n)
2912 {
2913 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2914 type, first, n));
2915 }
2916
2917 /* Does the description of "bmap" depend on the specified dimensions?
2918 * We also check whether the dimensions appear in any of the div definitions.
2919 * In principle there is no need for this check. If the dimensions appear
2920 * in a div definition, they also appear in the defining constraints of that
2921 * div.
2922 */
isl_basic_map_involves_dims(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)2923 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2924 enum isl_dim_type type, unsigned first, unsigned n)
2925 {
2926 int i;
2927
2928 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2929 return isl_bool_error;
2930
2931 first += isl_basic_map_offset(bmap, type);
2932 for (i = 0; i < bmap->n_eq; ++i)
2933 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2934 return isl_bool_true;
2935 for (i = 0; i < bmap->n_ineq; ++i)
2936 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2937 return isl_bool_true;
2938 for (i = 0; i < bmap->n_div; ++i) {
2939 if (isl_int_is_zero(bmap->div[i][0]))
2940 continue;
2941 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2942 return isl_bool_true;
2943 }
2944
2945 return isl_bool_false;
2946 }
2947
isl_map_involves_dims(__isl_keep isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)2948 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2949 enum isl_dim_type type, unsigned first, unsigned n)
2950 {
2951 int i;
2952
2953 if (isl_map_check_range(map, type, first, n) < 0)
2954 return isl_bool_error;
2955
2956 for (i = 0; i < map->n; ++i) {
2957 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2958 type, first, n);
2959 if (involves < 0 || involves)
2960 return involves;
2961 }
2962
2963 return isl_bool_false;
2964 }
2965
isl_basic_set_involves_dims(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)2966 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2967 enum isl_dim_type type, unsigned first, unsigned n)
2968 {
2969 return isl_basic_map_involves_dims(bset, type, first, n);
2970 }
2971
isl_set_involves_dims(__isl_keep isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)2972 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2974 {
2975 return isl_map_involves_dims(set, type, first, n);
2976 }
2977
2978 /* Does "bset" involve any local variables, i.e., integer divisions?
2979 */
isl_basic_set_involves_locals(__isl_keep isl_basic_set * bset)2980 static isl_bool isl_basic_set_involves_locals(__isl_keep isl_basic_set *bset)
2981 {
2982 isl_size n;
2983
2984 n = isl_basic_set_dim(bset, isl_dim_div);
2985 if (n < 0)
2986 return isl_bool_error;
2987 return isl_bool_ok(n > 0);
2988 }
2989
2990 /* isl_set_every_basic_set callback that checks whether "bset"
2991 * is free of local variables.
2992 */
basic_set_no_locals(__isl_keep isl_basic_set * bset,void * user)2993 static isl_bool basic_set_no_locals(__isl_keep isl_basic_set *bset, void *user)
2994 {
2995 return isl_bool_not(isl_basic_set_involves_locals(bset));
2996 }
2997
2998 /* Does "set" involve any local variables, i.e., integer divisions?
2999 */
isl_set_involves_locals(__isl_keep isl_set * set)3000 isl_bool isl_set_involves_locals(__isl_keep isl_set *set)
3001 {
3002 isl_bool no_locals;
3003
3004 no_locals = isl_set_every_basic_set(set, &basic_set_no_locals, NULL);
3005 return isl_bool_not(no_locals);
3006 }
3007
3008 /* Drop all constraints in bmap that involve any of the dimensions
3009 * first to first+n-1.
3010 * This function only performs the actual removal of constraints.
3011 *
3012 * This function should not call finalize since it is used by
3013 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
3014 */
isl_basic_map_drop_constraints_involving(__isl_take isl_basic_map * bmap,unsigned first,unsigned n)3015 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
3016 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
3017 {
3018 int i;
3019
3020 if (n == 0)
3021 return bmap;
3022
3023 bmap = isl_basic_map_cow(bmap);
3024
3025 if (!bmap)
3026 return NULL;
3027
3028 for (i = bmap->n_eq - 1; i >= 0; --i) {
3029 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
3030 continue;
3031 if (isl_basic_map_drop_equality(bmap, i) < 0)
3032 return isl_basic_map_free(bmap);
3033 }
3034
3035 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3036 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
3037 continue;
3038 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3039 return isl_basic_map_free(bmap);
3040 }
3041
3042 return bmap;
3043 }
3044
3045 /* Drop all constraints in bset that involve any of the dimensions
3046 * first to first+n-1.
3047 * This function only performs the actual removal of constraints.
3048 */
isl_basic_set_drop_constraints_involving(__isl_take isl_basic_set * bset,unsigned first,unsigned n)3049 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
3050 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
3051 {
3052 return isl_basic_map_drop_constraints_involving(bset, first, n);
3053 }
3054
3055 /* Drop all constraints in bmap that do not involve any of the dimensions
3056 * first to first + n - 1 of the given type.
3057 */
isl_basic_map_drop_constraints_not_involving_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)3058 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
3059 __isl_take isl_basic_map *bmap,
3060 enum isl_dim_type type, unsigned first, unsigned n)
3061 {
3062 int i;
3063
3064 if (n == 0) {
3065 isl_space *space = isl_basic_map_get_space(bmap);
3066 isl_basic_map_free(bmap);
3067 return isl_basic_map_universe(space);
3068 }
3069 bmap = isl_basic_map_cow(bmap);
3070 if (!bmap)
3071 return NULL;
3072
3073 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3074 return isl_basic_map_free(bmap);
3075
3076 first += isl_basic_map_offset(bmap, type) - 1;
3077
3078 for (i = bmap->n_eq - 1; i >= 0; --i) {
3079 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
3080 continue;
3081 if (isl_basic_map_drop_equality(bmap, i) < 0)
3082 return isl_basic_map_free(bmap);
3083 }
3084
3085 for (i = bmap->n_ineq - 1; i >= 0; --i) {
3086 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
3087 continue;
3088 if (isl_basic_map_drop_inequality(bmap, i) < 0)
3089 return isl_basic_map_free(bmap);
3090 }
3091
3092 bmap = isl_basic_map_add_known_div_constraints(bmap);
3093 return bmap;
3094 }
3095
3096 /* Drop all constraints in bset that do not involve any of the dimensions
3097 * first to first + n - 1 of the given type.
3098 */
isl_basic_set_drop_constraints_not_involving_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)3099 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
3100 __isl_take isl_basic_set *bset,
3101 enum isl_dim_type type, unsigned first, unsigned n)
3102 {
3103 return isl_basic_map_drop_constraints_not_involving_dims(bset,
3104 type, first, n);
3105 }
3106
3107 /* Drop all constraints in bmap that involve any of the dimensions
3108 * first to first + n - 1 of the given type.
3109 */
isl_basic_map_drop_constraints_involving_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)3110 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
3111 __isl_take isl_basic_map *bmap,
3112 enum isl_dim_type type, unsigned first, unsigned n)
3113 {
3114 if (!bmap)
3115 return NULL;
3116 if (n == 0)
3117 return bmap;
3118
3119 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3120 return isl_basic_map_free(bmap);
3121
3122 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
3123 first += isl_basic_map_offset(bmap, type) - 1;
3124 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
3125 bmap = isl_basic_map_add_known_div_constraints(bmap);
3126 return bmap;
3127 }
3128
3129 /* Drop all constraints in bset that involve any of the dimensions
3130 * first to first + n - 1 of the given type.
3131 */
isl_basic_set_drop_constraints_involving_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)3132 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
3133 __isl_take isl_basic_set *bset,
3134 enum isl_dim_type type, unsigned first, unsigned n)
3135 {
3136 return isl_basic_map_drop_constraints_involving_dims(bset,
3137 type, first, n);
3138 }
3139
3140 /* Drop constraints from "map" by applying "drop" to each basic map.
3141 */
drop_constraints(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n,__isl_give isl_basic_map * (* drop)(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n))3142 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
3143 enum isl_dim_type type, unsigned first, unsigned n,
3144 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
3145 enum isl_dim_type type, unsigned first, unsigned n))
3146 {
3147 int i;
3148
3149 if (isl_map_check_range(map, type, first, n) < 0)
3150 return isl_map_free(map);
3151
3152 map = isl_map_cow(map);
3153 if (!map)
3154 return NULL;
3155
3156 for (i = 0; i < map->n; ++i) {
3157 map->p[i] = drop(map->p[i], type, first, n);
3158 if (!map->p[i])
3159 return isl_map_free(map);
3160 }
3161
3162 if (map->n > 1)
3163 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3164
3165 return map;
3166 }
3167
3168 /* Drop all constraints in map that involve any of the dimensions
3169 * first to first + n - 1 of the given type.
3170 */
isl_map_drop_constraints_involving_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)3171 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3172 __isl_take isl_map *map,
3173 enum isl_dim_type type, unsigned first, unsigned n)
3174 {
3175 if (n == 0)
3176 return map;
3177 return drop_constraints(map, type, first, n,
3178 &isl_basic_map_drop_constraints_involving_dims);
3179 }
3180
3181 /* Drop all constraints in "map" that do not involve any of the dimensions
3182 * first to first + n - 1 of the given type.
3183 */
isl_map_drop_constraints_not_involving_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)3184 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3185 __isl_take isl_map *map,
3186 enum isl_dim_type type, unsigned first, unsigned n)
3187 {
3188 if (n == 0) {
3189 isl_space *space = isl_map_get_space(map);
3190 isl_map_free(map);
3191 return isl_map_universe(space);
3192 }
3193 return drop_constraints(map, type, first, n,
3194 &isl_basic_map_drop_constraints_not_involving_dims);
3195 }
3196
3197 /* Drop all constraints in set that involve any of the dimensions
3198 * first to first + n - 1 of the given type.
3199 */
isl_set_drop_constraints_involving_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)3200 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3201 __isl_take isl_set *set,
3202 enum isl_dim_type type, unsigned first, unsigned n)
3203 {
3204 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3205 }
3206
3207 /* Drop all constraints in "set" that do not involve any of the dimensions
3208 * first to first + n - 1 of the given type.
3209 */
isl_set_drop_constraints_not_involving_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)3210 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3211 __isl_take isl_set *set,
3212 enum isl_dim_type type, unsigned first, unsigned n)
3213 {
3214 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3215 }
3216
3217 /* Does local variable "div" of "bmap" have a complete explicit representation?
3218 * Having a complete explicit representation requires not only
3219 * an explicit representation, but also that all local variables
3220 * that appear in this explicit representation in turn have
3221 * a complete explicit representation.
3222 */
isl_basic_map_div_is_known(__isl_keep isl_basic_map * bmap,int div)3223 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3224 {
3225 int i;
3226 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3227 isl_bool marked;
3228
3229 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3230 if (marked < 0 || marked)
3231 return isl_bool_not(marked);
3232
3233 for (i = bmap->n_div - 1; i >= 0; --i) {
3234 isl_bool known;
3235
3236 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3237 continue;
3238 known = isl_basic_map_div_is_known(bmap, i);
3239 if (known < 0 || !known)
3240 return known;
3241 }
3242
3243 return isl_bool_true;
3244 }
3245
3246 /* Remove all divs that are unknown or defined in terms of unknown divs.
3247 */
isl_basic_map_remove_unknown_divs(__isl_take isl_basic_map * bmap)3248 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3249 __isl_take isl_basic_map *bmap)
3250 {
3251 int i;
3252
3253 if (!bmap)
3254 return NULL;
3255
3256 for (i = bmap->n_div - 1; i >= 0; --i) {
3257 if (isl_basic_map_div_is_known(bmap, i))
3258 continue;
3259 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3260 if (!bmap)
3261 return NULL;
3262 i = bmap->n_div;
3263 }
3264
3265 return bmap;
3266 }
3267
3268 /* Remove all divs that are unknown or defined in terms of unknown divs.
3269 */
isl_basic_set_remove_unknown_divs(__isl_take isl_basic_set * bset)3270 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3271 __isl_take isl_basic_set *bset)
3272 {
3273 return isl_basic_map_remove_unknown_divs(bset);
3274 }
3275
isl_map_remove_unknown_divs(__isl_take isl_map * map)3276 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3277 {
3278 int i;
3279
3280 if (!map)
3281 return NULL;
3282 if (map->n == 0)
3283 return map;
3284
3285 map = isl_map_cow(map);
3286 if (!map)
3287 return NULL;
3288
3289 for (i = 0; i < map->n; ++i) {
3290 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3291 if (!map->p[i])
3292 goto error;
3293 }
3294 return map;
3295 error:
3296 isl_map_free(map);
3297 return NULL;
3298 }
3299
isl_set_remove_unknown_divs(__isl_take isl_set * set)3300 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3301 {
3302 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3303 }
3304
isl_basic_set_remove_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)3305 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3306 __isl_take isl_basic_set *bset,
3307 enum isl_dim_type type, unsigned first, unsigned n)
3308 {
3309 isl_basic_map *bmap = bset_to_bmap(bset);
3310 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3311 return bset_from_bmap(bmap);
3312 }
3313
isl_map_remove_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)3314 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3315 enum isl_dim_type type, unsigned first, unsigned n)
3316 {
3317 int i;
3318
3319 if (n == 0)
3320 return map;
3321
3322 map = isl_map_cow(map);
3323 if (isl_map_check_range(map, type, first, n) < 0)
3324 return isl_map_free(map);
3325
3326 for (i = 0; i < map->n; ++i) {
3327 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3328 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3329 if (!map->p[i])
3330 goto error;
3331 }
3332 map = isl_map_drop(map, type, first, n);
3333 return map;
3334 error:
3335 isl_map_free(map);
3336 return NULL;
3337 }
3338
isl_set_remove_dims(__isl_take isl_set * bset,enum isl_dim_type type,unsigned first,unsigned n)3339 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3340 enum isl_dim_type type, unsigned first, unsigned n)
3341 {
3342 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3343 type, first, n));
3344 }
3345
3346 /* Project out n inputs starting at first using Fourier-Motzkin */
isl_map_remove_inputs(__isl_take isl_map * map,unsigned first,unsigned n)3347 __isl_give isl_map *isl_map_remove_inputs(__isl_take isl_map *map,
3348 unsigned first, unsigned n)
3349 {
3350 return isl_map_remove_dims(map, isl_dim_in, first, n);
3351 }
3352
isl_basic_set_print_internal(__isl_keep isl_basic_set * bset,FILE * out,int indent)3353 void isl_basic_set_print_internal(__isl_keep isl_basic_set *bset,
3354 FILE *out, int indent)
3355 {
3356 isl_printer *p;
3357
3358 if (!bset) {
3359 fprintf(out, "null basic set\n");
3360 return;
3361 }
3362
3363 fprintf(out, "%*s", indent, "");
3364 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3365 bset->ref, bset->dim->nparam, bset->dim->n_out,
3366 bset->extra, bset->flags);
3367
3368 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3369 p = isl_printer_set_dump(p, 1);
3370 p = isl_printer_set_indent(p, indent);
3371 p = isl_printer_start_line(p);
3372 p = isl_printer_print_basic_set(p, bset);
3373 p = isl_printer_end_line(p);
3374 isl_printer_free(p);
3375 }
3376
isl_basic_map_print_internal(__isl_keep isl_basic_map * bmap,FILE * out,int indent)3377 void isl_basic_map_print_internal(__isl_keep isl_basic_map *bmap,
3378 FILE *out, int indent)
3379 {
3380 isl_printer *p;
3381
3382 if (!bmap) {
3383 fprintf(out, "null basic map\n");
3384 return;
3385 }
3386
3387 fprintf(out, "%*s", indent, "");
3388 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3389 "flags: %x, n_name: %d\n",
3390 bmap->ref,
3391 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3392 bmap->extra, bmap->flags, bmap->dim->n_id);
3393
3394 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3395 p = isl_printer_set_dump(p, 1);
3396 p = isl_printer_set_indent(p, indent);
3397 p = isl_printer_start_line(p);
3398 p = isl_printer_print_basic_map(p, bmap);
3399 p = isl_printer_end_line(p);
3400 isl_printer_free(p);
3401 }
3402
isl_inequality_negate(__isl_take isl_basic_map * bmap,unsigned pos)3403 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3404 unsigned pos)
3405 {
3406 isl_size total;
3407
3408 total = isl_basic_map_dim(bmap, isl_dim_all);
3409 if (total < 0)
3410 return isl_basic_map_free(bmap);
3411 if (pos >= bmap->n_ineq)
3412 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3413 "invalid position", return isl_basic_map_free(bmap));
3414 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3415 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3416 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3417 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3418 return bmap;
3419 }
3420
isl_set_alloc_space(__isl_take isl_space * space,int n,unsigned flags)3421 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3422 unsigned flags)
3423 {
3424 if (isl_space_check_is_set(space) < 0)
3425 goto error;
3426 return isl_map_alloc_space(space, n, flags);
3427 error:
3428 isl_space_free(space);
3429 return NULL;
3430 }
3431
3432 /* Make sure "map" has room for at least "n" more basic maps.
3433 */
isl_map_grow(__isl_take isl_map * map,int n)3434 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3435 {
3436 int i;
3437 struct isl_map *grown = NULL;
3438
3439 if (!map)
3440 return NULL;
3441 isl_assert(map->ctx, n >= 0, goto error);
3442 if (map->n + n <= map->size)
3443 return map;
3444 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3445 if (!grown)
3446 goto error;
3447 for (i = 0; i < map->n; ++i) {
3448 grown->p[i] = isl_basic_map_copy(map->p[i]);
3449 if (!grown->p[i])
3450 goto error;
3451 grown->n++;
3452 }
3453 isl_map_free(map);
3454 return grown;
3455 error:
3456 isl_map_free(grown);
3457 isl_map_free(map);
3458 return NULL;
3459 }
3460
3461 /* Make sure "set" has room for at least "n" more basic sets.
3462 */
isl_set_grow(__isl_take isl_set * set,int n)3463 __isl_give isl_set *isl_set_grow(__isl_take isl_set *set, int n)
3464 {
3465 return set_from_map(isl_map_grow(set_to_map(set), n));
3466 }
3467
isl_set_from_basic_set(__isl_take isl_basic_set * bset)3468 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3469 {
3470 return isl_map_from_basic_map(bset);
3471 }
3472
3473 /* This function performs the same operation as isl_set_from_basic_set,
3474 * but is considered as a function on an isl_basic_set when exported.
3475 */
isl_basic_set_to_set(__isl_take isl_basic_set * bset)3476 __isl_give isl_set *isl_basic_set_to_set(__isl_take isl_basic_set *bset)
3477 {
3478 return isl_set_from_basic_set(bset);
3479 }
3480
isl_map_from_basic_map(__isl_take isl_basic_map * bmap)3481 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3482 {
3483 struct isl_map *map;
3484
3485 if (!bmap)
3486 return NULL;
3487
3488 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3489 return isl_map_add_basic_map(map, bmap);
3490 }
3491
isl_set_add_basic_set(__isl_take isl_set * set,__isl_take isl_basic_set * bset)3492 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3493 __isl_take isl_basic_set *bset)
3494 {
3495 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3496 bset_to_bmap(bset)));
3497 }
3498
isl_set_free(__isl_take isl_set * set)3499 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3500 {
3501 return isl_map_free(set);
3502 }
3503
isl_set_print_internal(__isl_keep isl_set * set,FILE * out,int indent)3504 void isl_set_print_internal(__isl_keep isl_set *set, FILE *out, int indent)
3505 {
3506 int i;
3507
3508 if (!set) {
3509 fprintf(out, "null set\n");
3510 return;
3511 }
3512
3513 fprintf(out, "%*s", indent, "");
3514 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3515 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3516 set->flags);
3517 for (i = 0; i < set->n; ++i) {
3518 fprintf(out, "%*s", indent, "");
3519 fprintf(out, "basic set %d:\n", i);
3520 isl_basic_set_print_internal(set->p[i], out, indent+4);
3521 }
3522 }
3523
isl_map_print_internal(__isl_keep isl_map * map,FILE * out,int indent)3524 void isl_map_print_internal(__isl_keep isl_map *map, FILE *out, int indent)
3525 {
3526 int i;
3527
3528 if (!map) {
3529 fprintf(out, "null map\n");
3530 return;
3531 }
3532
3533 fprintf(out, "%*s", indent, "");
3534 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3535 "flags: %x, n_name: %d\n",
3536 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3537 map->dim->n_out, map->flags, map->dim->n_id);
3538 for (i = 0; i < map->n; ++i) {
3539 fprintf(out, "%*s", indent, "");
3540 fprintf(out, "basic map %d:\n", i);
3541 isl_basic_map_print_internal(map->p[i], out, indent+4);
3542 }
3543 }
3544
3545 /* Check that the space of "bset" is the same as that of the domain of "bmap".
3546 */
isl_basic_map_check_compatible_domain(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * bset)3547 static isl_stat isl_basic_map_check_compatible_domain(
3548 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3549 {
3550 isl_bool ok;
3551
3552 ok = isl_basic_map_compatible_domain(bmap, bset);
3553 if (ok < 0)
3554 return isl_stat_error;
3555 if (!ok)
3556 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3557 "incompatible spaces", return isl_stat_error);
3558
3559 return isl_stat_ok;
3560 }
3561
isl_basic_map_intersect_domain(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * bset)3562 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3563 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3564 {
3565 struct isl_basic_map *bmap_domain;
3566 isl_size dim;
3567
3568 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3569 goto error;
3570
3571 dim = isl_basic_set_dim(bset, isl_dim_set);
3572 if (dim < 0)
3573 goto error;
3574 if (dim != 0 &&
3575 isl_basic_map_check_compatible_domain(bmap, bset) < 0)
3576 goto error;
3577
3578 bmap = isl_basic_map_cow(bmap);
3579 if (!bmap)
3580 goto error;
3581 bmap = isl_basic_map_extend(bmap,
3582 bset->n_div, bset->n_eq, bset->n_ineq);
3583 bmap_domain = isl_basic_map_from_domain(bset);
3584 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3585
3586 bmap = isl_basic_map_simplify(bmap);
3587 return isl_basic_map_finalize(bmap);
3588 error:
3589 isl_basic_map_free(bmap);
3590 isl_basic_set_free(bset);
3591 return NULL;
3592 }
3593
3594 /* Check that the space of "bset" is the same as that of the range of "bmap".
3595 */
isl_basic_map_check_compatible_range(__isl_keep isl_basic_map * bmap,__isl_keep isl_basic_set * bset)3596 static isl_stat isl_basic_map_check_compatible_range(
3597 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3598 {
3599 isl_bool ok;
3600
3601 ok = isl_basic_map_compatible_range(bmap, bset);
3602 if (ok < 0)
3603 return isl_stat_error;
3604 if (!ok)
3605 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3606 "incompatible spaces", return isl_stat_error);
3607
3608 return isl_stat_ok;
3609 }
3610
isl_basic_map_intersect_range(__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * bset)3611 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3612 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3613 {
3614 struct isl_basic_map *bmap_range;
3615 isl_size dim;
3616
3617 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3618 goto error;
3619
3620 dim = isl_basic_set_dim(bset, isl_dim_set);
3621 if (dim < 0)
3622 goto error;
3623 if (dim != 0 && isl_basic_map_check_compatible_range(bmap, bset) < 0)
3624 goto error;
3625
3626 if (isl_basic_set_plain_is_universe(bset)) {
3627 isl_basic_set_free(bset);
3628 return bmap;
3629 }
3630
3631 bmap = isl_basic_map_cow(bmap);
3632 if (!bmap)
3633 goto error;
3634 bmap = isl_basic_map_extend(bmap,
3635 bset->n_div, bset->n_eq, bset->n_ineq);
3636 bmap_range = bset_to_bmap(bset);
3637 bmap = add_constraints(bmap, bmap_range, 0, 0);
3638
3639 bmap = isl_basic_map_simplify(bmap);
3640 return isl_basic_map_finalize(bmap);
3641 error:
3642 isl_basic_map_free(bmap);
3643 isl_basic_set_free(bset);
3644 return NULL;
3645 }
3646
isl_basic_map_contains(__isl_keep isl_basic_map * bmap,__isl_keep isl_vec * vec)3647 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3648 __isl_keep isl_vec *vec)
3649 {
3650 int i;
3651 isl_size total;
3652 isl_int s;
3653
3654 total = isl_basic_map_dim(bmap, isl_dim_all);
3655 if (total < 0 || !vec)
3656 return isl_bool_error;
3657
3658 if (1 + total != vec->size)
3659 return isl_bool_false;
3660
3661 isl_int_init(s);
3662
3663 for (i = 0; i < bmap->n_eq; ++i) {
3664 isl_seq_inner_product(vec->el, bmap->eq[i], 1 + total, &s);
3665 if (!isl_int_is_zero(s)) {
3666 isl_int_clear(s);
3667 return isl_bool_false;
3668 }
3669 }
3670
3671 for (i = 0; i < bmap->n_ineq; ++i) {
3672 isl_seq_inner_product(vec->el, bmap->ineq[i], 1 + total, &s);
3673 if (isl_int_is_neg(s)) {
3674 isl_int_clear(s);
3675 return isl_bool_false;
3676 }
3677 }
3678
3679 isl_int_clear(s);
3680
3681 return isl_bool_true;
3682 }
3683
isl_basic_set_contains(__isl_keep isl_basic_set * bset,__isl_keep isl_vec * vec)3684 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3685 __isl_keep isl_vec *vec)
3686 {
3687 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3688 }
3689
isl_basic_map_intersect(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)3690 __isl_give isl_basic_map *isl_basic_map_intersect(
3691 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3692 {
3693 struct isl_vec *sample = NULL;
3694 isl_space *space1, *space2;
3695 isl_size dim1, dim2, nparam1, nparam2;
3696
3697 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3698 goto error;
3699 space1 = isl_basic_map_peek_space(bmap1);
3700 space2 = isl_basic_map_peek_space(bmap2);
3701 dim1 = isl_space_dim(space1, isl_dim_all);
3702 dim2 = isl_space_dim(space2, isl_dim_all);
3703 nparam1 = isl_space_dim(space1, isl_dim_param);
3704 nparam2 = isl_space_dim(space2, isl_dim_param);
3705 if (dim1 < 0 || dim2 < 0 || nparam1 < 0 || nparam2 < 0)
3706 goto error;
3707 if (dim1 == nparam1 && dim2 != nparam2)
3708 return isl_basic_map_intersect(bmap2, bmap1);
3709
3710 if (dim2 != nparam2 &&
3711 isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
3712 goto error;
3713
3714 if (isl_basic_map_plain_is_empty(bmap1)) {
3715 isl_basic_map_free(bmap2);
3716 return bmap1;
3717 }
3718 if (isl_basic_map_plain_is_empty(bmap2)) {
3719 isl_basic_map_free(bmap1);
3720 return bmap2;
3721 }
3722
3723 if (bmap1->sample &&
3724 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3725 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3726 sample = isl_vec_copy(bmap1->sample);
3727 else if (bmap2->sample &&
3728 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3729 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3730 sample = isl_vec_copy(bmap2->sample);
3731
3732 bmap1 = isl_basic_map_cow(bmap1);
3733 if (!bmap1)
3734 goto error;
3735 bmap1 = isl_basic_map_extend(bmap1,
3736 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3737 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3738
3739 if (!bmap1)
3740 isl_vec_free(sample);
3741 else if (sample) {
3742 isl_vec_free(bmap1->sample);
3743 bmap1->sample = sample;
3744 }
3745
3746 bmap1 = isl_basic_map_simplify(bmap1);
3747 return isl_basic_map_finalize(bmap1);
3748 error:
3749 if (sample)
3750 isl_vec_free(sample);
3751 isl_basic_map_free(bmap1);
3752 isl_basic_map_free(bmap2);
3753 return NULL;
3754 }
3755
isl_basic_set_intersect(__isl_take isl_basic_set * bset1,__isl_take isl_basic_set * bset2)3756 __isl_give isl_basic_set *isl_basic_set_intersect(
3757 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3758 {
3759 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3760 bset_to_bmap(bset2)));
3761 }
3762
isl_basic_set_intersect_params(__isl_take isl_basic_set * bset1,__isl_take isl_basic_set * bset2)3763 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3764 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3765 {
3766 return isl_basic_set_intersect(bset1, bset2);
3767 }
3768
3769 /* Special case of isl_map_intersect, where both map1 and map2
3770 * are convex, without any divs and such that either map1 or map2
3771 * contains a single constraint. This constraint is then simply
3772 * added to the other map.
3773 */
map_intersect_add_constraint(__isl_take isl_map * map1,__isl_take isl_map * map2)3774 static __isl_give isl_map *map_intersect_add_constraint(
3775 __isl_take isl_map *map1, __isl_take isl_map *map2)
3776 {
3777 isl_assert(map1->ctx, map1->n == 1, goto error);
3778 isl_assert(map2->ctx, map1->n == 1, goto error);
3779 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3780 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3781
3782 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3783 return isl_map_intersect(map2, map1);
3784
3785 map1 = isl_map_cow(map1);
3786 if (!map1)
3787 goto error;
3788 if (isl_map_plain_is_empty(map1)) {
3789 isl_map_free(map2);
3790 return map1;
3791 }
3792 if (map2->p[0]->n_eq == 1)
3793 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3794 else
3795 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3796 map2->p[0]->ineq[0]);
3797
3798 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3799 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3800 if (!map1->p[0])
3801 goto error;
3802
3803 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3804 isl_basic_map_free(map1->p[0]);
3805 map1->n = 0;
3806 }
3807
3808 isl_map_free(map2);
3809
3810 map1 = isl_map_unmark_normalized(map1);
3811 return map1;
3812 error:
3813 isl_map_free(map1);
3814 isl_map_free(map2);
3815 return NULL;
3816 }
3817
3818 /* map2 may be either a parameter domain or a map living in the same
3819 * space as map1.
3820 */
map_intersect_internal(__isl_take isl_map * map1,__isl_take isl_map * map2)3821 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3822 __isl_take isl_map *map2)
3823 {
3824 unsigned flags = 0;
3825 isl_bool equal;
3826 isl_map *result;
3827 int i, j;
3828 isl_size dim2, nparam2;
3829
3830 if (!map1 || !map2)
3831 goto error;
3832
3833 if ((isl_map_plain_is_empty(map1) ||
3834 isl_map_plain_is_universe(map2)) &&
3835 isl_space_is_equal(map1->dim, map2->dim)) {
3836 isl_map_free(map2);
3837 return map1;
3838 }
3839 if ((isl_map_plain_is_empty(map2) ||
3840 isl_map_plain_is_universe(map1)) &&
3841 isl_space_is_equal(map1->dim, map2->dim)) {
3842 isl_map_free(map1);
3843 return map2;
3844 }
3845
3846 if (map1->n == 1 && map2->n == 1 &&
3847 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3848 isl_space_is_equal(map1->dim, map2->dim) &&
3849 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3850 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3851 return map_intersect_add_constraint(map1, map2);
3852
3853 equal = isl_map_plain_is_equal(map1, map2);
3854 if (equal < 0)
3855 goto error;
3856 if (equal) {
3857 isl_map_free(map2);
3858 return map1;
3859 }
3860
3861 dim2 = isl_map_dim(map2, isl_dim_all);
3862 nparam2 = isl_map_dim(map2, isl_dim_param);
3863 if (dim2 < 0 || nparam2 < 0)
3864 goto error;
3865 if (dim2 != nparam2)
3866 isl_assert(map1->ctx,
3867 isl_space_is_equal(map1->dim, map2->dim), goto error);
3868
3869 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3870 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3871 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3872
3873 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3874 map1->n * map2->n, flags);
3875 if (!result)
3876 goto error;
3877 for (i = 0; i < map1->n; ++i)
3878 for (j = 0; j < map2->n; ++j) {
3879 struct isl_basic_map *part;
3880 part = isl_basic_map_intersect(
3881 isl_basic_map_copy(map1->p[i]),
3882 isl_basic_map_copy(map2->p[j]));
3883 if (isl_basic_map_is_empty(part) < 0)
3884 part = isl_basic_map_free(part);
3885 result = isl_map_add_basic_map(result, part);
3886 if (!result)
3887 goto error;
3888 }
3889 isl_map_free(map1);
3890 isl_map_free(map2);
3891 return result;
3892 error:
3893 isl_map_free(map1);
3894 isl_map_free(map2);
3895 return NULL;
3896 }
3897
map_intersect(__isl_take isl_map * map1,__isl_take isl_map * map2)3898 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3899 __isl_take isl_map *map2)
3900 {
3901 if (isl_map_check_equal_space(map1, map2) < 0)
3902 goto error;
3903 return map_intersect_internal(map1, map2);
3904 error:
3905 isl_map_free(map1);
3906 isl_map_free(map2);
3907 return NULL;
3908 }
3909
isl_map_intersect(__isl_take isl_map * map1,__isl_take isl_map * map2)3910 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3911 __isl_take isl_map *map2)
3912 {
3913 isl_map_align_params_bin(&map1, &map2);
3914 return map_intersect(map1, map2);
3915 }
3916
isl_set_intersect(__isl_take isl_set * set1,__isl_take isl_set * set2)3917 __isl_give isl_set *isl_set_intersect(__isl_take isl_set *set1,
3918 __isl_take isl_set *set2)
3919 {
3920 return set_from_map(isl_map_intersect(set_to_map(set1),
3921 set_to_map(set2)));
3922 }
3923
3924 /* map_intersect_internal accepts intersections
3925 * with parameter domains, so we can just call that function.
3926 */
isl_map_intersect_params(__isl_take isl_map * map,__isl_take isl_set * params)3927 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map,
3928 __isl_take isl_set *params)
3929 {
3930 isl_map_align_params_set(&map, ¶ms);
3931 return map_intersect_internal(map, params);
3932 }
3933
isl_set_intersect_params(__isl_take isl_set * set,__isl_take isl_set * params)3934 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3935 __isl_take isl_set *params)
3936 {
3937 return isl_map_intersect_params(set, params);
3938 }
3939
isl_basic_map_reverse(__isl_take isl_basic_map * bmap)3940 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3941 {
3942 isl_space *space;
3943 unsigned pos;
3944 isl_size n1, n2;
3945
3946 if (!bmap)
3947 return NULL;
3948 bmap = isl_basic_map_cow(bmap);
3949 if (!bmap)
3950 return NULL;
3951 space = isl_space_reverse(isl_space_copy(bmap->dim));
3952 pos = isl_basic_map_offset(bmap, isl_dim_in);
3953 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3954 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3955 if (n1 < 0 || n2 < 0)
3956 bmap = isl_basic_map_free(bmap);
3957 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3958 return isl_basic_map_reset_space(bmap, space);
3959 }
3960
3961 /* Given a basic map A -> (B -> C), return the corresponding basic map
3962 * A -> (C -> B).
3963 */
isl_basic_map_range_reverse(__isl_take isl_basic_map * bmap)3964 static __isl_give isl_basic_map *isl_basic_map_range_reverse(
3965 __isl_take isl_basic_map *bmap)
3966 {
3967 isl_space *space;
3968 isl_size offset, n1, n2;
3969
3970 space = isl_basic_map_peek_space(bmap);
3971 if (isl_space_check_range_is_wrapping(space) < 0)
3972 return isl_basic_map_free(bmap);
3973 offset = isl_basic_map_var_offset(bmap, isl_dim_out);
3974 n1 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_in);
3975 n2 = isl_space_wrapped_dim(space, isl_dim_out, isl_dim_out);
3976 if (offset < 0 || n1 < 0 || n2 < 0)
3977 return isl_basic_map_free(bmap);
3978
3979 bmap = isl_basic_map_swap_vars(bmap, 1 + offset, n1, n2);
3980
3981 space = isl_basic_map_take_space(bmap);
3982 space = isl_space_range_reverse(space);
3983 bmap = isl_basic_map_restore_space(bmap, space);
3984
3985 return bmap;
3986 }
3987
basic_map_space_reset(__isl_take isl_basic_map * bmap,enum isl_dim_type type)3988 static __isl_give isl_basic_map *basic_map_space_reset(
3989 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3990 {
3991 isl_space *space;
3992
3993 if (!bmap)
3994 return NULL;
3995 if (!isl_space_is_named_or_nested(bmap->dim, type))
3996 return bmap;
3997
3998 space = isl_basic_map_get_space(bmap);
3999 space = isl_space_reset(space, type);
4000 bmap = isl_basic_map_reset_space(bmap, space);
4001 return bmap;
4002 }
4003
isl_basic_map_insert_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,unsigned n)4004 __isl_give isl_basic_map *isl_basic_map_insert_dims(
4005 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4006 unsigned pos, unsigned n)
4007 {
4008 isl_bool rational, is_empty;
4009 isl_space *res_space;
4010 struct isl_basic_map *res;
4011 struct isl_dim_map *dim_map;
4012 isl_size total;
4013 unsigned off;
4014 enum isl_dim_type t;
4015
4016 if (n == 0)
4017 return basic_map_space_reset(bmap, type);
4018
4019 is_empty = isl_basic_map_plain_is_empty(bmap);
4020 total = isl_basic_map_dim(bmap, isl_dim_all);
4021 if (is_empty < 0 || total < 0)
4022 return isl_basic_map_free(bmap);
4023 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
4024 type, pos, n);
4025 if (!res_space)
4026 return isl_basic_map_free(bmap);
4027 if (is_empty) {
4028 isl_basic_map_free(bmap);
4029 return isl_basic_map_empty(res_space);
4030 }
4031
4032 dim_map = isl_dim_map_alloc(bmap->ctx, total + n);
4033 off = 0;
4034 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4035 isl_size dim;
4036
4037 if (t != type) {
4038 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4039 } else {
4040 isl_size size = isl_basic_map_dim(bmap, t);
4041 if (size < 0)
4042 dim_map = isl_dim_map_free(dim_map);
4043 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4044 0, pos, off);
4045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4046 pos, size - pos, off + pos + n);
4047 }
4048 dim = isl_space_dim(res_space, t);
4049 if (dim < 0)
4050 dim_map = isl_dim_map_free(dim_map);
4051 off += dim;
4052 }
4053 isl_dim_map_div(dim_map, bmap, off);
4054
4055 res = isl_basic_map_alloc_space(res_space,
4056 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4057 rational = isl_basic_map_is_rational(bmap);
4058 if (rational < 0)
4059 res = isl_basic_map_free(res);
4060 if (rational)
4061 res = isl_basic_map_set_rational(res);
4062 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4063 return isl_basic_map_finalize(res);
4064 }
4065
isl_basic_set_insert_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,unsigned n)4066 __isl_give isl_basic_set *isl_basic_set_insert_dims(
4067 __isl_take isl_basic_set *bset,
4068 enum isl_dim_type type, unsigned pos, unsigned n)
4069 {
4070 return isl_basic_map_insert_dims(bset, type, pos, n);
4071 }
4072
isl_basic_map_add_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned n)4073 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
4074 enum isl_dim_type type, unsigned n)
4075 {
4076 isl_size dim;
4077
4078 dim = isl_basic_map_dim(bmap, type);
4079 if (dim < 0)
4080 return isl_basic_map_free(bmap);
4081 return isl_basic_map_insert_dims(bmap, type, dim, n);
4082 }
4083
isl_basic_set_add_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned n)4084 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
4085 enum isl_dim_type type, unsigned n)
4086 {
4087 if (!bset)
4088 return NULL;
4089 isl_assert(bset->ctx, type != isl_dim_in, goto error);
4090 return isl_basic_map_add_dims(bset, type, n);
4091 error:
4092 isl_basic_set_free(bset);
4093 return NULL;
4094 }
4095
map_space_reset(__isl_take isl_map * map,enum isl_dim_type type)4096 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
4097 enum isl_dim_type type)
4098 {
4099 isl_space *space;
4100
4101 if (!map || !isl_space_is_named_or_nested(map->dim, type))
4102 return map;
4103
4104 space = isl_map_get_space(map);
4105 space = isl_space_reset(space, type);
4106 map = isl_map_reset_space(map, space);
4107 return map;
4108 }
4109
isl_map_insert_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,unsigned n)4110 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
4111 enum isl_dim_type type, unsigned pos, unsigned n)
4112 {
4113 int i;
4114 isl_space *space;
4115
4116 if (n == 0)
4117 return map_space_reset(map, type);
4118
4119 map = isl_map_cow(map);
4120 if (!map)
4121 return NULL;
4122
4123 for (i = 0; i < map->n; ++i) {
4124 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
4125 if (!map->p[i])
4126 goto error;
4127 }
4128
4129 space = isl_map_take_space(map);
4130 space = isl_space_insert_dims(space, type, pos, n);
4131 map = isl_map_restore_space(map, space);
4132
4133 return map;
4134 error:
4135 isl_map_free(map);
4136 return NULL;
4137 }
4138
isl_set_insert_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,unsigned n)4139 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4140 enum isl_dim_type type, unsigned pos, unsigned n)
4141 {
4142 return isl_map_insert_dims(set, type, pos, n);
4143 }
4144
isl_map_add_dims(__isl_take isl_map * map,enum isl_dim_type type,unsigned n)4145 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4146 enum isl_dim_type type, unsigned n)
4147 {
4148 isl_size dim;
4149
4150 dim = isl_map_dim(map, type);
4151 if (dim < 0)
4152 return isl_map_free(map);
4153 return isl_map_insert_dims(map, type, dim, n);
4154 }
4155
isl_set_add_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned n)4156 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4157 enum isl_dim_type type, unsigned n)
4158 {
4159 if (!set)
4160 return NULL;
4161 isl_assert(set->ctx, type != isl_dim_in, goto error);
4162 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4163 error:
4164 isl_set_free(set);
4165 return NULL;
4166 }
4167
isl_basic_map_move_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)4168 __isl_give isl_basic_map *isl_basic_map_move_dims(
4169 __isl_take isl_basic_map *bmap,
4170 enum isl_dim_type dst_type, unsigned dst_pos,
4171 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4172 {
4173 isl_space *space;
4174 struct isl_dim_map *dim_map;
4175 struct isl_basic_map *res;
4176 enum isl_dim_type t;
4177 isl_size total;
4178 unsigned off;
4179
4180 if (!bmap)
4181 return NULL;
4182 if (n == 0) {
4183 bmap = isl_basic_map_reset(bmap, src_type);
4184 bmap = isl_basic_map_reset(bmap, dst_type);
4185 return bmap;
4186 }
4187
4188 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4189 return isl_basic_map_free(bmap);
4190
4191 if (dst_type == src_type && dst_pos == src_pos)
4192 return bmap;
4193
4194 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4195
4196 if (pos(bmap->dim, dst_type) + dst_pos ==
4197 pos(bmap->dim, src_type) + src_pos +
4198 ((src_type < dst_type) ? n : 0)) {
4199 space = isl_basic_map_take_space(bmap);
4200 space = isl_space_move_dims(space, dst_type, dst_pos,
4201 src_type, src_pos, n);
4202 bmap = isl_basic_map_restore_space(bmap, space);
4203 bmap = isl_basic_map_finalize(bmap);
4204
4205 return bmap;
4206 }
4207
4208 total = isl_basic_map_dim(bmap, isl_dim_all);
4209 if (total < 0)
4210 return isl_basic_map_free(bmap);
4211 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4212
4213 off = 0;
4214 space = isl_basic_map_peek_space(bmap);
4215 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4216 isl_size size = isl_space_dim(space, t);
4217 if (size < 0)
4218 dim_map = isl_dim_map_free(dim_map);
4219 if (t == dst_type) {
4220 isl_dim_map_dim_range(dim_map, space, t,
4221 0, dst_pos, off);
4222 off += dst_pos;
4223 isl_dim_map_dim_range(dim_map, space, src_type,
4224 src_pos, n, off);
4225 off += n;
4226 isl_dim_map_dim_range(dim_map, space, t,
4227 dst_pos, size - dst_pos, off);
4228 off += size - dst_pos;
4229 } else if (t == src_type) {
4230 isl_dim_map_dim_range(dim_map, space, t,
4231 0, src_pos, off);
4232 off += src_pos;
4233 isl_dim_map_dim_range(dim_map, space, t,
4234 src_pos + n, size - src_pos - n, off);
4235 off += size - src_pos - n;
4236 } else {
4237 isl_dim_map_dim(dim_map, space, t, off);
4238 off += size;
4239 }
4240 }
4241 isl_dim_map_div(dim_map, bmap, off);
4242
4243 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4244 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4245 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4246 space = isl_basic_map_take_space(bmap);
4247 space = isl_space_move_dims(space, dst_type, dst_pos,
4248 src_type, src_pos, n);
4249 bmap = isl_basic_map_restore_space(bmap, space);
4250 if (!bmap)
4251 goto error;
4252
4253 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4254 bmap = isl_basic_map_gauss(bmap, NULL);
4255 bmap = isl_basic_map_finalize(bmap);
4256
4257 return bmap;
4258 error:
4259 isl_basic_map_free(bmap);
4260 return NULL;
4261 }
4262
isl_basic_set_move_dims(__isl_take isl_basic_set * bset,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)4263 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4264 enum isl_dim_type dst_type, unsigned dst_pos,
4265 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4266 {
4267 isl_basic_map *bmap = bset_to_bmap(bset);
4268 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4269 src_type, src_pos, n);
4270 return bset_from_bmap(bmap);
4271 }
4272
isl_set_move_dims(__isl_take isl_set * set,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)4273 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4274 enum isl_dim_type dst_type, unsigned dst_pos,
4275 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4276 {
4277 if (!set)
4278 return NULL;
4279 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4280 return set_from_map(isl_map_move_dims(set_to_map(set),
4281 dst_type, dst_pos, src_type, src_pos, n));
4282 error:
4283 isl_set_free(set);
4284 return NULL;
4285 }
4286
isl_map_move_dims(__isl_take isl_map * map,enum isl_dim_type dst_type,unsigned dst_pos,enum isl_dim_type src_type,unsigned src_pos,unsigned n)4287 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4288 enum isl_dim_type dst_type, unsigned dst_pos,
4289 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4290 {
4291 int i;
4292 isl_space *space;
4293
4294 if (n == 0) {
4295 map = isl_map_reset(map, src_type);
4296 map = isl_map_reset(map, dst_type);
4297 return map;
4298 }
4299
4300 if (isl_map_check_range(map, src_type, src_pos, n))
4301 return isl_map_free(map);
4302
4303 if (dst_type == src_type && dst_pos == src_pos)
4304 return map;
4305
4306 isl_assert(map->ctx, dst_type != src_type, goto error);
4307
4308 map = isl_map_cow(map);
4309 if (!map)
4310 return NULL;
4311
4312 for (i = 0; i < map->n; ++i) {
4313 map->p[i] = isl_basic_map_move_dims(map->p[i],
4314 dst_type, dst_pos,
4315 src_type, src_pos, n);
4316 if (!map->p[i])
4317 goto error;
4318 }
4319
4320 space = isl_map_take_space(map);
4321 space = isl_space_move_dims(space, dst_type, dst_pos,
4322 src_type, src_pos, n);
4323 map = isl_map_restore_space(map, space);
4324
4325 return map;
4326 error:
4327 isl_map_free(map);
4328 return NULL;
4329 }
4330
4331 /* Move the specified dimensions to the last columns right before
4332 * the divs. Don't change the dimension specification of bmap.
4333 * That's the responsibility of the caller.
4334 */
move_last(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)4335 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4336 enum isl_dim_type type, unsigned first, unsigned n)
4337 {
4338 isl_space *space;
4339 struct isl_dim_map *dim_map;
4340 struct isl_basic_map *res;
4341 enum isl_dim_type t;
4342 isl_size total;
4343 unsigned off;
4344
4345 if (!bmap)
4346 return NULL;
4347 if (isl_basic_map_offset(bmap, type) + first + n ==
4348 isl_basic_map_offset(bmap, isl_dim_div))
4349 return bmap;
4350
4351 total = isl_basic_map_dim(bmap, isl_dim_all);
4352 if (total < 0)
4353 return isl_basic_map_free(bmap);
4354 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4355
4356 off = 0;
4357 space = isl_basic_map_peek_space(bmap);
4358 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4359 isl_size size = isl_space_dim(space, t);
4360 if (size < 0)
4361 dim_map = isl_dim_map_free(dim_map);
4362 if (t == type) {
4363 isl_dim_map_dim_range(dim_map, space, t,
4364 0, first, off);
4365 off += first;
4366 isl_dim_map_dim_range(dim_map, space, t,
4367 first, n, total - bmap->n_div - n);
4368 isl_dim_map_dim_range(dim_map, space, t,
4369 first + n, size - (first + n), off);
4370 off += size - (first + n);
4371 } else {
4372 isl_dim_map_dim(dim_map, space, t, off);
4373 off += size;
4374 }
4375 }
4376 isl_dim_map_div(dim_map, bmap, off + n);
4377
4378 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4379 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4380 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4381 return res;
4382 }
4383
4384 /* Insert "n" rows in the divs of "bmap".
4385 *
4386 * The number of columns is not changed, which means that the last
4387 * dimensions of "bmap" are being reintepreted as the new divs.
4388 * The space of "bmap" is not adjusted, however, which means
4389 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4390 * from the space of "bmap" is the responsibility of the caller.
4391 */
insert_div_rows(__isl_take isl_basic_map * bmap,int n)4392 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4393 int n)
4394 {
4395 int i;
4396 size_t row_size;
4397 isl_int **new_div;
4398 isl_int *old;
4399
4400 bmap = isl_basic_map_cow(bmap);
4401 if (!bmap)
4402 return NULL;
4403
4404 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4405 old = bmap->block2.data;
4406 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4407 (bmap->extra + n) * (1 + row_size));
4408 if (!bmap->block2.data)
4409 return isl_basic_map_free(bmap);
4410 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4411 if (!new_div)
4412 return isl_basic_map_free(bmap);
4413 for (i = 0; i < n; ++i) {
4414 new_div[i] = bmap->block2.data +
4415 (bmap->extra + i) * (1 + row_size);
4416 isl_seq_clr(new_div[i], 1 + row_size);
4417 }
4418 for (i = 0; i < bmap->extra; ++i)
4419 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4420 free(bmap->div);
4421 bmap->div = new_div;
4422 bmap->n_div += n;
4423 bmap->extra += n;
4424
4425 return bmap;
4426 }
4427
4428 /* Drop constraints from "bmap" that only involve the variables
4429 * of "type" in the range [first, first + n] that are not related
4430 * to any of the variables outside that interval.
4431 * These constraints cannot influence the values for the variables
4432 * outside the interval, except in case they cause "bmap" to be empty.
4433 * Only drop the constraints if "bmap" is known to be non-empty.
4434 */
drop_irrelevant_constraints(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)4435 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4436 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4437 unsigned first, unsigned n)
4438 {
4439 int i;
4440 int *groups;
4441 isl_size dim, n_div;
4442 isl_bool non_empty;
4443
4444 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4445 if (non_empty < 0)
4446 return isl_basic_map_free(bmap);
4447 if (!non_empty)
4448 return bmap;
4449
4450 dim = isl_basic_map_dim(bmap, isl_dim_all);
4451 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4452 if (dim < 0 || n_div < 0)
4453 return isl_basic_map_free(bmap);
4454 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4455 if (!groups)
4456 return isl_basic_map_free(bmap);
4457 first += isl_basic_map_offset(bmap, type) - 1;
4458 for (i = 0; i < first; ++i)
4459 groups[i] = -1;
4460 for (i = first + n; i < dim - n_div; ++i)
4461 groups[i] = -1;
4462
4463 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4464
4465 return bmap;
4466 }
4467
4468 /* Turn the n dimensions of type type, starting at first
4469 * into existentially quantified variables.
4470 *
4471 * If a subset of the projected out variables are unrelated
4472 * to any of the variables that remain, then the constraints
4473 * involving this subset are simply dropped first.
4474 */
isl_basic_map_project_out(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,unsigned n)4475 __isl_give isl_basic_map *isl_basic_map_project_out(
4476 __isl_take isl_basic_map *bmap,
4477 enum isl_dim_type type, unsigned first, unsigned n)
4478 {
4479 isl_bool empty;
4480 isl_space *space;
4481
4482 if (n == 0)
4483 return basic_map_space_reset(bmap, type);
4484 if (type == isl_dim_div)
4485 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4486 "cannot project out existentially quantified variables",
4487 return isl_basic_map_free(bmap));
4488
4489 empty = isl_basic_map_plain_is_empty(bmap);
4490 if (empty < 0)
4491 return isl_basic_map_free(bmap);
4492 if (empty)
4493 bmap = isl_basic_map_set_to_empty(bmap);
4494
4495 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4496 if (!bmap)
4497 return NULL;
4498
4499 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4500 return isl_basic_map_remove_dims(bmap, type, first, n);
4501
4502 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4503 return isl_basic_map_free(bmap);
4504
4505 bmap = move_last(bmap, type, first, n);
4506 bmap = isl_basic_map_cow(bmap);
4507 bmap = insert_div_rows(bmap, n);
4508
4509 space = isl_basic_map_take_space(bmap);
4510 space = isl_space_drop_dims(space, type, first, n);
4511 bmap = isl_basic_map_restore_space(bmap, space);
4512 bmap = isl_basic_map_simplify(bmap);
4513 bmap = isl_basic_map_drop_redundant_divs(bmap);
4514 return isl_basic_map_finalize(bmap);
4515 }
4516
4517 /* Turn the n dimensions of type type, starting at first
4518 * into existentially quantified variables.
4519 */
isl_basic_set_project_out(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n)4520 __isl_give isl_basic_set *isl_basic_set_project_out(
4521 __isl_take isl_basic_set *bset, enum isl_dim_type type,
4522 unsigned first, unsigned n)
4523 {
4524 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4525 type, first, n));
4526 }
4527
4528 /* Turn the n dimensions of type type, starting at first
4529 * into existentially quantified variables.
4530 */
isl_map_project_out(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)4531 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4532 enum isl_dim_type type, unsigned first, unsigned n)
4533 {
4534 int i;
4535 isl_space *space;
4536
4537 if (n == 0)
4538 return map_space_reset(map, type);
4539
4540 if (isl_map_check_range(map, type, first, n) < 0)
4541 return isl_map_free(map);
4542
4543 map = isl_map_cow(map);
4544 if (!map)
4545 return NULL;
4546
4547 for (i = 0; i < map->n; ++i) {
4548 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4549 if (!map->p[i])
4550 goto error;
4551 }
4552
4553 if (map->n > 1)
4554 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4555 map = isl_map_unmark_normalized(map);
4556
4557 space = isl_map_take_space(map);
4558 space = isl_space_drop_dims(space, type, first, n);
4559 map = isl_map_restore_space(map, space);
4560
4561 return map;
4562 error:
4563 isl_map_free(map);
4564 return NULL;
4565 }
4566
4567 #undef TYPE
4568 #define TYPE isl_map
4569 #include "isl_project_out_all_params_templ.c"
4570
4571 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4572 * into existentially quantified variables.
4573 */
isl_map_project_onto(__isl_take isl_map * map,enum isl_dim_type type,unsigned first,unsigned n)4574 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4575 enum isl_dim_type type, unsigned first, unsigned n)
4576 {
4577 isl_size dim;
4578
4579 dim = isl_map_dim(map, type);
4580 if (isl_map_check_range(map, type, first, n) < 0 || dim < 0)
4581 return isl_map_free(map);
4582 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4583 map = isl_map_project_out(map, type, 0, first);
4584 return map;
4585 }
4586
4587 /* Turn the n dimensions of type type, starting at first
4588 * into existentially quantified variables.
4589 */
isl_set_project_out(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)4590 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4591 enum isl_dim_type type, unsigned first, unsigned n)
4592 {
4593 return set_from_map(isl_map_project_out(set_to_map(set),
4594 type, first, n));
4595 }
4596
4597 /* If "set" involves a parameter with identifier "id",
4598 * then turn it into an existentially quantified variable.
4599 */
isl_set_project_out_param_id(__isl_take isl_set * set,__isl_take isl_id * id)4600 __isl_give isl_set *isl_set_project_out_param_id(__isl_take isl_set *set,
4601 __isl_take isl_id *id)
4602 {
4603 int pos;
4604
4605 if (!set || !id)
4606 goto error;
4607 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
4608 isl_id_free(id);
4609 if (pos < 0)
4610 return set;
4611 return isl_set_project_out(set, isl_dim_param, pos, 1);
4612 error:
4613 isl_set_free(set);
4614 isl_id_free(id);
4615 return NULL;
4616 }
4617
4618 /* If "set" involves any of the parameters with identifiers in "list",
4619 * then turn them into existentially quantified variables.
4620 */
isl_set_project_out_param_id_list(__isl_take isl_set * set,__isl_take isl_id_list * list)4621 __isl_give isl_set *isl_set_project_out_param_id_list(__isl_take isl_set *set,
4622 __isl_take isl_id_list *list)
4623 {
4624 int i;
4625 isl_size n;
4626
4627 n = isl_id_list_size(list);
4628 if (n < 0)
4629 goto error;
4630 for (i = 0; i < n; ++i) {
4631 isl_id *id;
4632
4633 id = isl_id_list_get_at(list, i);
4634 set = isl_set_project_out_param_id(set, id);
4635 }
4636
4637 isl_id_list_free(list);
4638 return set;
4639 error:
4640 isl_id_list_free(list);
4641 isl_set_free(set);
4642 return NULL;
4643 }
4644
4645 /* Project out all parameters from "set" by existentially quantifying
4646 * over them.
4647 */
isl_set_project_out_all_params(__isl_take isl_set * set)4648 __isl_give isl_set *isl_set_project_out_all_params(__isl_take isl_set *set)
4649 {
4650 return set_from_map(isl_map_project_out_all_params(set_to_map(set)));
4651 }
4652
4653 /* Return a map that projects the elements in "set" onto their
4654 * "n" set dimensions starting at "first".
4655 * "type" should be equal to isl_dim_set.
4656 */
isl_set_project_onto_map(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)4657 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4658 enum isl_dim_type type, unsigned first, unsigned n)
4659 {
4660 int i;
4661 isl_map *map;
4662
4663 if (type != isl_dim_set)
4664 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4665 "only set dimensions can be projected out", goto error);
4666 if (isl_set_check_range(set, type, first, n) < 0)
4667 return isl_set_free(set);
4668
4669 map = isl_map_from_domain(set);
4670 map = isl_map_add_dims(map, isl_dim_out, n);
4671 for (i = 0; i < n; ++i)
4672 map = isl_map_equate(map, isl_dim_in, first + i,
4673 isl_dim_out, i);
4674 return map;
4675 error:
4676 isl_set_free(set);
4677 return NULL;
4678 }
4679
add_divs(__isl_take isl_basic_map * bmap,unsigned n)4680 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4681 unsigned n)
4682 {
4683 int i, j;
4684 isl_size total;
4685
4686 total = isl_basic_map_dim(bmap, isl_dim_all);
4687 if (total < 0)
4688 return isl_basic_map_free(bmap);
4689 for (i = 0; i < n; ++i) {
4690 j = isl_basic_map_alloc_div(bmap);
4691 if (j < 0)
4692 goto error;
4693 isl_seq_clr(bmap->div[j], 1 + 1 + total);
4694 }
4695 return bmap;
4696 error:
4697 isl_basic_map_free(bmap);
4698 return NULL;
4699 }
4700
4701 /* Does "bmap2" apply to the range of "bmap1" (ignoring parameters)?
4702 */
isl_basic_map_applies_range(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)4703 isl_bool isl_basic_map_applies_range(__isl_keep isl_basic_map *bmap1,
4704 __isl_keep isl_basic_map *bmap2)
4705 {
4706 isl_space *space1, *space2;
4707
4708 space1 = isl_basic_map_peek_space(bmap1);
4709 space2 = isl_basic_map_peek_space(bmap2);
4710 return isl_space_tuple_is_equal(space1, isl_dim_out,
4711 space2, isl_dim_in);
4712 }
4713
4714 /* Check that "bmap2" applies to the range of "bmap1" (ignoring parameters).
4715 */
isl_basic_map_check_applies_range(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)4716 static isl_stat isl_basic_map_check_applies_range(
4717 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
4718 {
4719 isl_bool equal;
4720
4721 equal = isl_basic_map_applies_range(bmap1, bmap2);
4722 if (equal < 0)
4723 return isl_stat_error;
4724 if (!equal)
4725 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4726 "spaces don't match", return isl_stat_error);
4727 return isl_stat_ok;
4728 }
4729
isl_basic_map_apply_range(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)4730 __isl_give isl_basic_map *isl_basic_map_apply_range(
4731 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4732 {
4733 isl_space *space_result = NULL;
4734 struct isl_basic_map *bmap;
4735 isl_size n_in, n_out, n, nparam;
4736 unsigned total, pos;
4737 struct isl_dim_map *dim_map1, *dim_map2;
4738
4739 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4740 goto error;
4741 if (isl_basic_map_check_applies_range(bmap1, bmap2) < 0)
4742 goto error;
4743
4744 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4745 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4746 n = isl_basic_map_dim(bmap1, isl_dim_out);
4747 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4748 if (n_in < 0 || n_out < 0 || n < 0 || nparam < 0)
4749 goto error;
4750
4751 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4752 isl_basic_map_get_space(bmap2));
4753
4754 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4755 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4756 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4757 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4758 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4759 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4760 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4761 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4762 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4763 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4764 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4765
4766 bmap = isl_basic_map_alloc_space(space_result,
4767 bmap1->n_div + bmap2->n_div + n,
4768 bmap1->n_eq + bmap2->n_eq,
4769 bmap1->n_ineq + bmap2->n_ineq);
4770 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4771 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4772 bmap = add_divs(bmap, n);
4773 bmap = isl_basic_map_simplify(bmap);
4774 bmap = isl_basic_map_drop_redundant_divs(bmap);
4775 return isl_basic_map_finalize(bmap);
4776 error:
4777 isl_basic_map_free(bmap1);
4778 isl_basic_map_free(bmap2);
4779 return NULL;
4780 }
4781
isl_basic_set_apply(__isl_take isl_basic_set * bset,__isl_take isl_basic_map * bmap)4782 __isl_give isl_basic_set *isl_basic_set_apply(__isl_take isl_basic_set *bset,
4783 __isl_take isl_basic_map *bmap)
4784 {
4785 if (isl_basic_map_check_compatible_domain(bmap, bset) < 0)
4786 goto error;
4787
4788 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4789 bmap));
4790 error:
4791 isl_basic_set_free(bset);
4792 isl_basic_map_free(bmap);
4793 return NULL;
4794 }
4795
isl_basic_map_apply_domain(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)4796 __isl_give isl_basic_map *isl_basic_map_apply_domain(
4797 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
4798 {
4799 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4800 goto error;
4801 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4802 bmap2->dim, isl_dim_in))
4803 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4804 "spaces don't match", goto error);
4805
4806 bmap1 = isl_basic_map_reverse(bmap1);
4807 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4808 return isl_basic_map_reverse(bmap1);
4809 error:
4810 isl_basic_map_free(bmap1);
4811 isl_basic_map_free(bmap2);
4812 return NULL;
4813 }
4814
4815 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4816 * A \cap B -> f(A) + f(B)
4817 */
isl_basic_map_sum(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)4818 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4819 __isl_take isl_basic_map *bmap2)
4820 {
4821 isl_size n_in, n_out, nparam;
4822 unsigned total, pos;
4823 struct isl_basic_map *bmap = NULL;
4824 struct isl_dim_map *dim_map1, *dim_map2;
4825 int i;
4826
4827 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
4828 goto error;
4829
4830 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4831 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4832 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4833 if (nparam < 0 || n_in < 0 || n_out < 0)
4834 goto error;
4835
4836 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4837 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4838 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4839 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4840 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4843 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4844 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4847
4848 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4849 bmap1->n_div + bmap2->n_div + 2 * n_out,
4850 bmap1->n_eq + bmap2->n_eq + n_out,
4851 bmap1->n_ineq + bmap2->n_ineq);
4852 for (i = 0; i < n_out; ++i) {
4853 int j = isl_basic_map_alloc_equality(bmap);
4854 if (j < 0)
4855 goto error;
4856 isl_seq_clr(bmap->eq[j], 1+total);
4857 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4858 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4859 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4860 }
4861 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4862 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4863 bmap = add_divs(bmap, 2 * n_out);
4864
4865 bmap = isl_basic_map_simplify(bmap);
4866 return isl_basic_map_finalize(bmap);
4867 error:
4868 isl_basic_map_free(bmap);
4869 isl_basic_map_free(bmap1);
4870 isl_basic_map_free(bmap2);
4871 return NULL;
4872 }
4873
4874 /* Given two maps A -> f(A) and B -> g(B), construct a map
4875 * A \cap B -> f(A) + f(B)
4876 */
isl_map_sum(__isl_take isl_map * map1,__isl_take isl_map * map2)4877 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4878 __isl_take isl_map *map2)
4879 {
4880 struct isl_map *result;
4881 int i, j;
4882
4883 if (isl_map_check_equal_space(map1, map2) < 0)
4884 goto error;
4885
4886 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4887 map1->n * map2->n, 0);
4888 if (!result)
4889 goto error;
4890 for (i = 0; i < map1->n; ++i)
4891 for (j = 0; j < map2->n; ++j) {
4892 struct isl_basic_map *part;
4893 part = isl_basic_map_sum(
4894 isl_basic_map_copy(map1->p[i]),
4895 isl_basic_map_copy(map2->p[j]));
4896 if (isl_basic_map_is_empty(part))
4897 isl_basic_map_free(part);
4898 else
4899 result = isl_map_add_basic_map(result, part);
4900 if (!result)
4901 goto error;
4902 }
4903 isl_map_free(map1);
4904 isl_map_free(map2);
4905 return result;
4906 error:
4907 isl_map_free(map1);
4908 isl_map_free(map2);
4909 return NULL;
4910 }
4911
isl_set_sum(__isl_take isl_set * set1,__isl_take isl_set * set2)4912 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4913 __isl_take isl_set *set2)
4914 {
4915 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4916 }
4917
4918 /* Given a basic map A -> f(A), construct A -> -f(A).
4919 */
isl_basic_map_neg(__isl_take isl_basic_map * bmap)4920 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4921 {
4922 int i, j;
4923 unsigned off;
4924 isl_size n;
4925
4926 bmap = isl_basic_map_cow(bmap);
4927 n = isl_basic_map_dim(bmap, isl_dim_out);
4928 if (n < 0)
4929 return isl_basic_map_free(bmap);
4930
4931 off = isl_basic_map_offset(bmap, isl_dim_out);
4932 for (i = 0; i < bmap->n_eq; ++i)
4933 for (j = 0; j < n; ++j)
4934 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4935 for (i = 0; i < bmap->n_ineq; ++i)
4936 for (j = 0; j < n; ++j)
4937 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4938 for (i = 0; i < bmap->n_div; ++i)
4939 for (j = 0; j < n; ++j)
4940 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4941 bmap = isl_basic_map_gauss(bmap, NULL);
4942 return isl_basic_map_finalize(bmap);
4943 }
4944
isl_basic_set_neg(__isl_take isl_basic_set * bset)4945 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4946 {
4947 return isl_basic_map_neg(bset);
4948 }
4949
4950 /* Given a map A -> f(A), construct A -> -f(A).
4951 */
isl_map_neg(__isl_take isl_map * map)4952 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4953 {
4954 int i;
4955
4956 map = isl_map_cow(map);
4957 if (!map)
4958 return NULL;
4959
4960 for (i = 0; i < map->n; ++i) {
4961 map->p[i] = isl_basic_map_neg(map->p[i]);
4962 if (!map->p[i])
4963 goto error;
4964 }
4965
4966 return map;
4967 error:
4968 isl_map_free(map);
4969 return NULL;
4970 }
4971
isl_set_neg(__isl_take isl_set * set)4972 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4973 {
4974 return set_from_map(isl_map_neg(set_to_map(set)));
4975 }
4976
4977 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4978 * A -> floor(f(A)/d).
4979 */
isl_basic_map_floordiv(__isl_take isl_basic_map * bmap,isl_int d)4980 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4981 isl_int d)
4982 {
4983 isl_size n_in, n_out, nparam;
4984 unsigned total, pos;
4985 struct isl_basic_map *result = NULL;
4986 struct isl_dim_map *dim_map;
4987 int i;
4988
4989 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4990 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4991 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4992 if (nparam < 0 || n_in < 0 || n_out < 0)
4993 return isl_basic_map_free(bmap);
4994
4995 total = nparam + n_in + n_out + bmap->n_div + n_out;
4996 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4997 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4998 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4999 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
5000 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
5001
5002 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
5003 bmap->n_div + n_out,
5004 bmap->n_eq, bmap->n_ineq + 2 * n_out);
5005 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
5006 result = add_divs(result, n_out);
5007 for (i = 0; i < n_out; ++i) {
5008 int j;
5009 j = isl_basic_map_alloc_inequality(result);
5010 if (j < 0)
5011 goto error;
5012 isl_seq_clr(result->ineq[j], 1+total);
5013 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
5014 isl_int_set_si(result->ineq[j][1+pos+i], 1);
5015 j = isl_basic_map_alloc_inequality(result);
5016 if (j < 0)
5017 goto error;
5018 isl_seq_clr(result->ineq[j], 1+total);
5019 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
5020 isl_int_set_si(result->ineq[j][1+pos+i], -1);
5021 isl_int_sub_ui(result->ineq[j][0], d, 1);
5022 }
5023
5024 result = isl_basic_map_simplify(result);
5025 return isl_basic_map_finalize(result);
5026 error:
5027 isl_basic_map_free(result);
5028 return NULL;
5029 }
5030
5031 /* Given a map A -> f(A) and an integer d, construct a map
5032 * A -> floor(f(A)/d).
5033 */
isl_map_floordiv(__isl_take isl_map * map,isl_int d)5034 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
5035 {
5036 int i;
5037
5038 map = isl_map_cow(map);
5039 if (!map)
5040 return NULL;
5041
5042 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5043 for (i = 0; i < map->n; ++i) {
5044 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
5045 if (!map->p[i])
5046 goto error;
5047 }
5048 map = isl_map_unmark_normalized(map);
5049
5050 return map;
5051 error:
5052 isl_map_free(map);
5053 return NULL;
5054 }
5055
5056 /* Given a map A -> f(A) and an integer d, construct a map
5057 * A -> floor(f(A)/d).
5058 */
isl_map_floordiv_val(__isl_take isl_map * map,__isl_take isl_val * d)5059 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
5060 __isl_take isl_val *d)
5061 {
5062 if (!map || !d)
5063 goto error;
5064 if (!isl_val_is_int(d))
5065 isl_die(isl_val_get_ctx(d), isl_error_invalid,
5066 "expecting integer denominator", goto error);
5067 map = isl_map_floordiv(map, d->n);
5068 isl_val_free(d);
5069 return map;
5070 error:
5071 isl_map_free(map);
5072 isl_val_free(d);
5073 return NULL;
5074 }
5075
var_equal(__isl_take isl_basic_map * bmap,unsigned pos)5076 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
5077 unsigned pos)
5078 {
5079 int i;
5080 isl_size nparam;
5081 isl_size n_in;
5082 isl_size total;
5083
5084 total = isl_basic_map_dim(bmap, isl_dim_all);
5085 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5086 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5087 if (total < 0 || nparam < 0 || n_in < 0)
5088 return isl_basic_map_free(bmap);
5089 i = isl_basic_map_alloc_equality(bmap);
5090 if (i < 0)
5091 goto error;
5092 isl_seq_clr(bmap->eq[i], 1 + total);
5093 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
5094 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
5095 return isl_basic_map_finalize(bmap);
5096 error:
5097 isl_basic_map_free(bmap);
5098 return NULL;
5099 }
5100
5101 /* Add a constraint to "bmap" expressing i_pos < o_pos
5102 */
var_less(__isl_take isl_basic_map * bmap,unsigned pos)5103 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
5104 unsigned pos)
5105 {
5106 int i;
5107 isl_size nparam;
5108 isl_size n_in;
5109 isl_size total;
5110
5111 total = isl_basic_map_dim(bmap, isl_dim_all);
5112 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5113 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5114 if (total < 0 || nparam < 0 || n_in < 0)
5115 return isl_basic_map_free(bmap);
5116 i = isl_basic_map_alloc_inequality(bmap);
5117 if (i < 0)
5118 goto error;
5119 isl_seq_clr(bmap->ineq[i], 1 + total);
5120 isl_int_set_si(bmap->ineq[i][0], -1);
5121 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5122 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5123 return isl_basic_map_finalize(bmap);
5124 error:
5125 isl_basic_map_free(bmap);
5126 return NULL;
5127 }
5128
5129 /* Add a constraint to "bmap" expressing i_pos <= o_pos
5130 */
var_less_or_equal(__isl_take isl_basic_map * bmap,unsigned pos)5131 static __isl_give isl_basic_map *var_less_or_equal(
5132 __isl_take isl_basic_map *bmap, unsigned pos)
5133 {
5134 int i;
5135 isl_size nparam;
5136 isl_size n_in;
5137 isl_size total;
5138
5139 total = isl_basic_map_dim(bmap, isl_dim_all);
5140 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5141 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5142 if (total < 0 || nparam < 0 || n_in < 0)
5143 return isl_basic_map_free(bmap);
5144 i = isl_basic_map_alloc_inequality(bmap);
5145 if (i < 0)
5146 goto error;
5147 isl_seq_clr(bmap->ineq[i], 1 + total);
5148 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
5149 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
5150 return isl_basic_map_finalize(bmap);
5151 error:
5152 isl_basic_map_free(bmap);
5153 return NULL;
5154 }
5155
5156 /* Add a constraint to "bmap" expressing i_pos > o_pos
5157 */
var_more(__isl_take isl_basic_map * bmap,unsigned pos)5158 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
5159 unsigned pos)
5160 {
5161 int i;
5162 isl_size nparam;
5163 isl_size n_in;
5164 isl_size total;
5165
5166 total = isl_basic_map_dim(bmap, isl_dim_all);
5167 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5168 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5169 if (total < 0 || nparam < 0 || n_in < 0)
5170 return isl_basic_map_free(bmap);
5171 i = isl_basic_map_alloc_inequality(bmap);
5172 if (i < 0)
5173 goto error;
5174 isl_seq_clr(bmap->ineq[i], 1 + total);
5175 isl_int_set_si(bmap->ineq[i][0], -1);
5176 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5177 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5178 return isl_basic_map_finalize(bmap);
5179 error:
5180 isl_basic_map_free(bmap);
5181 return NULL;
5182 }
5183
5184 /* Add a constraint to "bmap" expressing i_pos >= o_pos
5185 */
var_more_or_equal(__isl_take isl_basic_map * bmap,unsigned pos)5186 static __isl_give isl_basic_map *var_more_or_equal(
5187 __isl_take isl_basic_map *bmap, unsigned pos)
5188 {
5189 int i;
5190 isl_size nparam;
5191 isl_size n_in;
5192 isl_size total;
5193
5194 total = isl_basic_map_dim(bmap, isl_dim_all);
5195 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5196 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5197 if (total < 0 || nparam < 0 || n_in < 0)
5198 return isl_basic_map_free(bmap);
5199 i = isl_basic_map_alloc_inequality(bmap);
5200 if (i < 0)
5201 goto error;
5202 isl_seq_clr(bmap->ineq[i], 1 + total);
5203 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
5204 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
5205 return isl_basic_map_finalize(bmap);
5206 error:
5207 isl_basic_map_free(bmap);
5208 return NULL;
5209 }
5210
isl_basic_map_equal(__isl_take isl_space * space,unsigned n_equal)5211 __isl_give isl_basic_map *isl_basic_map_equal(
5212 __isl_take isl_space *space, unsigned n_equal)
5213 {
5214 int i;
5215 struct isl_basic_map *bmap;
5216 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
5217 if (!bmap)
5218 return NULL;
5219 for (i = 0; i < n_equal && bmap; ++i)
5220 bmap = var_equal(bmap, i);
5221 return isl_basic_map_finalize(bmap);
5222 }
5223
5224 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
5225 */
isl_basic_map_less_at(__isl_take isl_space * space,unsigned pos)5226 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
5227 unsigned pos)
5228 {
5229 int i;
5230 struct isl_basic_map *bmap;
5231 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5232 if (!bmap)
5233 return NULL;
5234 for (i = 0; i < pos && bmap; ++i)
5235 bmap = var_equal(bmap, i);
5236 if (bmap)
5237 bmap = var_less(bmap, pos);
5238 return isl_basic_map_finalize(bmap);
5239 }
5240
5241 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
5242 */
isl_basic_map_less_or_equal_at(__isl_take isl_space * space,unsigned pos)5243 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
5244 __isl_take isl_space *space, unsigned pos)
5245 {
5246 int i;
5247 isl_basic_map *bmap;
5248
5249 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5250 for (i = 0; i < pos; ++i)
5251 bmap = var_equal(bmap, i);
5252 bmap = var_less_or_equal(bmap, pos);
5253 return isl_basic_map_finalize(bmap);
5254 }
5255
5256 /* Return a relation on "space" expressing i_pos > o_pos
5257 */
isl_basic_map_more_at(__isl_take isl_space * space,unsigned pos)5258 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5259 unsigned pos)
5260 {
5261 int i;
5262 struct isl_basic_map *bmap;
5263 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5264 if (!bmap)
5265 return NULL;
5266 for (i = 0; i < pos && bmap; ++i)
5267 bmap = var_equal(bmap, i);
5268 if (bmap)
5269 bmap = var_more(bmap, pos);
5270 return isl_basic_map_finalize(bmap);
5271 }
5272
5273 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5274 */
isl_basic_map_more_or_equal_at(__isl_take isl_space * space,unsigned pos)5275 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5276 __isl_take isl_space *space, unsigned pos)
5277 {
5278 int i;
5279 isl_basic_map *bmap;
5280
5281 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5282 for (i = 0; i < pos; ++i)
5283 bmap = var_equal(bmap, i);
5284 bmap = var_more_or_equal(bmap, pos);
5285 return isl_basic_map_finalize(bmap);
5286 }
5287
map_lex_lte_first(__isl_take isl_space * space,unsigned n,int equal)5288 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5289 unsigned n, int equal)
5290 {
5291 struct isl_map *map;
5292 int i;
5293
5294 if (n == 0 && equal)
5295 return isl_map_universe(space);
5296
5297 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5298
5299 for (i = 0; i + 1 < n; ++i)
5300 map = isl_map_add_basic_map(map,
5301 isl_basic_map_less_at(isl_space_copy(space), i));
5302 if (n > 0) {
5303 if (equal)
5304 map = isl_map_add_basic_map(map,
5305 isl_basic_map_less_or_equal_at(space, n - 1));
5306 else
5307 map = isl_map_add_basic_map(map,
5308 isl_basic_map_less_at(space, n - 1));
5309 } else
5310 isl_space_free(space);
5311
5312 return map;
5313 }
5314
map_lex_lte(__isl_take isl_space * space,int equal)5315 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5316 {
5317 if (!space)
5318 return NULL;
5319 return map_lex_lte_first(space, space->n_out, equal);
5320 }
5321
isl_map_lex_lt_first(__isl_take isl_space * space,unsigned n)5322 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *space,
5323 unsigned n)
5324 {
5325 return map_lex_lte_first(space, n, 0);
5326 }
5327
isl_map_lex_le_first(__isl_take isl_space * space,unsigned n)5328 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *space,
5329 unsigned n)
5330 {
5331 return map_lex_lte_first(space, n, 1);
5332 }
5333
isl_map_lex_lt(__isl_take isl_space * set_space)5334 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_space)
5335 {
5336 return map_lex_lte(isl_space_map_from_set(set_space), 0);
5337 }
5338
isl_map_lex_le(__isl_take isl_space * set_space)5339 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_space)
5340 {
5341 return map_lex_lte(isl_space_map_from_set(set_space), 1);
5342 }
5343
map_lex_gte_first(__isl_take isl_space * space,unsigned n,int equal)5344 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5345 unsigned n, int equal)
5346 {
5347 struct isl_map *map;
5348 int i;
5349
5350 if (n == 0 && equal)
5351 return isl_map_universe(space);
5352
5353 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5354
5355 for (i = 0; i + 1 < n; ++i)
5356 map = isl_map_add_basic_map(map,
5357 isl_basic_map_more_at(isl_space_copy(space), i));
5358 if (n > 0) {
5359 if (equal)
5360 map = isl_map_add_basic_map(map,
5361 isl_basic_map_more_or_equal_at(space, n - 1));
5362 else
5363 map = isl_map_add_basic_map(map,
5364 isl_basic_map_more_at(space, n - 1));
5365 } else
5366 isl_space_free(space);
5367
5368 return map;
5369 }
5370
map_lex_gte(__isl_take isl_space * space,int equal)5371 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5372 {
5373 if (!space)
5374 return NULL;
5375 return map_lex_gte_first(space, space->n_out, equal);
5376 }
5377
isl_map_lex_gt_first(__isl_take isl_space * space,unsigned n)5378 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *space,
5379 unsigned n)
5380 {
5381 return map_lex_gte_first(space, n, 0);
5382 }
5383
isl_map_lex_ge_first(__isl_take isl_space * space,unsigned n)5384 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *space,
5385 unsigned n)
5386 {
5387 return map_lex_gte_first(space, n, 1);
5388 }
5389
isl_map_lex_gt(__isl_take isl_space * set_space)5390 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_space)
5391 {
5392 return map_lex_gte(isl_space_map_from_set(set_space), 0);
5393 }
5394
isl_map_lex_ge(__isl_take isl_space * set_space)5395 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_space)
5396 {
5397 return map_lex_gte(isl_space_map_from_set(set_space), 1);
5398 }
5399
isl_set_lex_le_set(__isl_take isl_set * set1,__isl_take isl_set * set2)5400 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5401 __isl_take isl_set *set2)
5402 {
5403 isl_map *map;
5404 map = isl_map_lex_le(isl_set_get_space(set1));
5405 map = isl_map_intersect_domain(map, set1);
5406 map = isl_map_intersect_range(map, set2);
5407 return map;
5408 }
5409
isl_set_lex_lt_set(__isl_take isl_set * set1,__isl_take isl_set * set2)5410 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5411 __isl_take isl_set *set2)
5412 {
5413 isl_map *map;
5414 map = isl_map_lex_lt(isl_set_get_space(set1));
5415 map = isl_map_intersect_domain(map, set1);
5416 map = isl_map_intersect_range(map, set2);
5417 return map;
5418 }
5419
isl_set_lex_ge_set(__isl_take isl_set * set1,__isl_take isl_set * set2)5420 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5421 __isl_take isl_set *set2)
5422 {
5423 isl_map *map;
5424 map = isl_map_lex_ge(isl_set_get_space(set1));
5425 map = isl_map_intersect_domain(map, set1);
5426 map = isl_map_intersect_range(map, set2);
5427 return map;
5428 }
5429
isl_set_lex_gt_set(__isl_take isl_set * set1,__isl_take isl_set * set2)5430 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5431 __isl_take isl_set *set2)
5432 {
5433 isl_map *map;
5434 map = isl_map_lex_gt(isl_set_get_space(set1));
5435 map = isl_map_intersect_domain(map, set1);
5436 map = isl_map_intersect_range(map, set2);
5437 return map;
5438 }
5439
isl_map_lex_le_map(__isl_take isl_map * map1,__isl_take isl_map * map2)5440 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5441 __isl_take isl_map *map2)
5442 {
5443 isl_map *map;
5444 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5445 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5446 map = isl_map_apply_range(map, isl_map_reverse(map2));
5447 return map;
5448 }
5449
isl_map_lex_lt_map(__isl_take isl_map * map1,__isl_take isl_map * map2)5450 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5451 __isl_take isl_map *map2)
5452 {
5453 isl_map *map;
5454 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5455 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5456 map = isl_map_apply_range(map, isl_map_reverse(map2));
5457 return map;
5458 }
5459
isl_map_lex_ge_map(__isl_take isl_map * map1,__isl_take isl_map * map2)5460 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5461 __isl_take isl_map *map2)
5462 {
5463 isl_map *map;
5464 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5465 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5466 map = isl_map_apply_range(map, isl_map_reverse(map2));
5467 return map;
5468 }
5469
isl_map_lex_gt_map(__isl_take isl_map * map1,__isl_take isl_map * map2)5470 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5471 __isl_take isl_map *map2)
5472 {
5473 isl_map *map;
5474 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5475 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5476 map = isl_map_apply_range(map, isl_map_reverse(map2));
5477 return map;
5478 }
5479
5480 /* For the div d = floor(f/m) at position "div", add the constraint
5481 *
5482 * f - m d >= 0
5483 */
add_upper_div_constraint(__isl_take isl_basic_map * bmap,unsigned div)5484 static __isl_give isl_basic_map *add_upper_div_constraint(
5485 __isl_take isl_basic_map *bmap, unsigned div)
5486 {
5487 int i;
5488 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5489 isl_size n_div;
5490 unsigned pos;
5491
5492 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5493 if (v_div < 0 || n_div < 0)
5494 return isl_basic_map_free(bmap);
5495 pos = v_div + div;
5496 i = isl_basic_map_alloc_inequality(bmap);
5497 if (i < 0)
5498 return isl_basic_map_free(bmap);
5499 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5500 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5501
5502 return bmap;
5503 }
5504
5505 /* For the div d = floor(f/m) at position "div", add the constraint
5506 *
5507 * -(f-(m-1)) + m d >= 0
5508 */
add_lower_div_constraint(__isl_take isl_basic_map * bmap,unsigned div)5509 static __isl_give isl_basic_map *add_lower_div_constraint(
5510 __isl_take isl_basic_map *bmap, unsigned div)
5511 {
5512 int i;
5513 isl_size v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5514 isl_size n_div;
5515 unsigned pos;
5516
5517 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5518 if (v_div < 0 || n_div < 0)
5519 return isl_basic_map_free(bmap);
5520 pos = v_div + div;
5521 i = isl_basic_map_alloc_inequality(bmap);
5522 if (i < 0)
5523 return isl_basic_map_free(bmap);
5524 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5525 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5526 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5527 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5528
5529 return bmap;
5530 }
5531
5532 /* For the div d = floor(f/m) at position "pos", add the constraints
5533 *
5534 * f - m d >= 0
5535 * -(f-(m-1)) + m d >= 0
5536 *
5537 * Note that the second constraint is the negation of
5538 *
5539 * f - m d >= m
5540 */
isl_basic_map_add_div_constraints(__isl_take isl_basic_map * bmap,unsigned pos)5541 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5542 __isl_take isl_basic_map *bmap, unsigned pos)
5543 {
5544 bmap = add_upper_div_constraint(bmap, pos);
5545 bmap = add_lower_div_constraint(bmap, pos);
5546 return bmap;
5547 }
5548
5549 /* For each known div d = floor(f/m), add the constraints
5550 *
5551 * f - m d >= 0
5552 * -(f-(m-1)) + m d >= 0
5553 *
5554 * Remove duplicate constraints in case of some these div constraints
5555 * already appear in "bmap".
5556 */
isl_basic_map_add_known_div_constraints(__isl_take isl_basic_map * bmap)5557 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5558 __isl_take isl_basic_map *bmap)
5559 {
5560 isl_size n_div;
5561
5562 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5563 if (n_div < 0)
5564 return isl_basic_map_free(bmap);
5565 if (n_div == 0)
5566 return bmap;
5567
5568 bmap = add_known_div_constraints(bmap);
5569 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5570 bmap = isl_basic_map_finalize(bmap);
5571 return bmap;
5572 }
5573
5574 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5575 *
5576 * In particular, if this div is of the form d = floor(f/m),
5577 * then add the constraint
5578 *
5579 * f - m d >= 0
5580 *
5581 * if sign < 0 or the constraint
5582 *
5583 * -(f-(m-1)) + m d >= 0
5584 *
5585 * if sign > 0.
5586 */
isl_basic_map_add_div_constraint(__isl_take isl_basic_map * bmap,unsigned div,int sign)5587 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5588 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5589 {
5590 if (sign < 0)
5591 return add_upper_div_constraint(bmap, div);
5592 else
5593 return add_lower_div_constraint(bmap, div);
5594 }
5595
isl_basic_map_underlying_set(__isl_take isl_basic_map * bmap)5596 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5597 __isl_take isl_basic_map *bmap)
5598 {
5599 isl_space *space;
5600
5601 if (!bmap)
5602 goto error;
5603 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5604 bmap->n_div == 0 &&
5605 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5606 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5607 return bset_from_bmap(bmap);
5608 bmap = isl_basic_map_cow(bmap);
5609 if (!bmap)
5610 return NULL;
5611 space = isl_basic_map_take_space(bmap);
5612 space = isl_space_underlying(space, bmap->n_div);
5613 bmap = isl_basic_map_restore_space(bmap, space);
5614 if (!bmap)
5615 return NULL;
5616 bmap->extra -= bmap->n_div;
5617 bmap->n_div = 0;
5618 bmap = isl_basic_map_finalize(bmap);
5619 return bset_from_bmap(bmap);
5620 error:
5621 isl_basic_map_free(bmap);
5622 return NULL;
5623 }
5624
isl_basic_set_underlying_set(__isl_take isl_basic_set * bset)5625 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5626 __isl_take isl_basic_set *bset)
5627 {
5628 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5629 }
5630
5631 /* Replace each element in "list" by the result of applying
5632 * isl_basic_map_underlying_set to the element.
5633 */
isl_basic_map_list_underlying_set(__isl_take isl_basic_map_list * list)5634 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5635 __isl_take isl_basic_map_list *list)
5636 {
5637 int i;
5638 isl_size n;
5639
5640 n = isl_basic_map_list_n_basic_map(list);
5641 if (n < 0)
5642 goto error;
5643
5644 for (i = 0; i < n; ++i) {
5645 isl_basic_map *bmap;
5646 isl_basic_set *bset;
5647
5648 bmap = isl_basic_map_list_get_basic_map(list, i);
5649 bset = isl_basic_set_underlying_set(bmap);
5650 list = isl_basic_set_list_set_basic_set(list, i, bset);
5651 }
5652
5653 return list;
5654 error:
5655 isl_basic_map_list_free(list);
5656 return NULL;
5657 }
5658
isl_basic_map_overlying_set(__isl_take isl_basic_set * bset,__isl_take isl_basic_map * like)5659 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5660 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5661 {
5662 struct isl_basic_map *bmap;
5663 struct isl_ctx *ctx;
5664 isl_size dim, bmap_total;
5665 unsigned total;
5666 int i;
5667
5668 if (!bset || !like)
5669 goto error;
5670 ctx = bset->ctx;
5671 if (isl_basic_set_check_no_params(bset) < 0 ||
5672 isl_basic_set_check_no_locals(bset) < 0)
5673 goto error;
5674 dim = isl_basic_set_dim(bset, isl_dim_set);
5675 bmap_total = isl_basic_map_dim(like, isl_dim_all);
5676 if (dim < 0 || bmap_total < 0)
5677 goto error;
5678 isl_assert(ctx, dim == bmap_total, goto error);
5679 if (like->n_div == 0) {
5680 isl_space *space = isl_basic_map_get_space(like);
5681 isl_basic_map_free(like);
5682 return isl_basic_map_reset_space(bset, space);
5683 }
5684 bset = isl_basic_set_cow(bset);
5685 if (!bset)
5686 goto error;
5687 total = dim + bset->extra;
5688 bmap = bset_to_bmap(bset);
5689 isl_space_free(isl_basic_map_take_space(bmap));
5690 bmap = isl_basic_map_restore_space(bmap, isl_basic_map_get_space(like));
5691 if (!bmap)
5692 goto error;
5693 bmap->n_div = like->n_div;
5694 bmap->extra += like->n_div;
5695 if (bmap->extra) {
5696 unsigned ltotal;
5697 isl_int **div;
5698 ltotal = total - bmap->extra + like->extra;
5699 if (ltotal > total)
5700 ltotal = total;
5701 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5702 bmap->extra * (1 + 1 + total));
5703 if (isl_blk_is_error(bmap->block2))
5704 goto error;
5705 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5706 if (!div)
5707 goto error;
5708 bmap->div = div;
5709 for (i = 0; i < bmap->extra; ++i)
5710 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5711 for (i = 0; i < like->n_div; ++i) {
5712 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5713 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5714 }
5715 bmap = isl_basic_map_add_known_div_constraints(bmap);
5716 }
5717 isl_basic_map_free(like);
5718 bmap = isl_basic_map_simplify(bmap);
5719 bmap = isl_basic_map_finalize(bmap);
5720 return bmap;
5721 error:
5722 isl_basic_map_free(like);
5723 isl_basic_set_free(bset);
5724 return NULL;
5725 }
5726
isl_basic_set_from_underlying_set(__isl_take isl_basic_set * bset,__isl_take isl_basic_set * like)5727 __isl_give isl_basic_set *isl_basic_set_from_underlying_set(
5728 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *like)
5729 {
5730 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5731 bset_to_bmap(like)));
5732 }
5733
isl_map_underlying_set(__isl_take isl_map * map)5734 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5735 {
5736 int i;
5737
5738 map = isl_map_cow(map);
5739 if (!map)
5740 return NULL;
5741 map->dim = isl_space_cow(map->dim);
5742 if (!map->dim)
5743 goto error;
5744
5745 for (i = 1; i < map->n; ++i)
5746 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5747 goto error);
5748 for (i = 0; i < map->n; ++i) {
5749 map->p[i] = bset_to_bmap(
5750 isl_basic_map_underlying_set(map->p[i]));
5751 if (!map->p[i])
5752 goto error;
5753 }
5754 if (map->n == 0)
5755 map->dim = isl_space_underlying(map->dim, 0);
5756 else {
5757 isl_space_free(map->dim);
5758 map->dim = isl_space_copy(map->p[0]->dim);
5759 }
5760 if (!map->dim)
5761 goto error;
5762 return set_from_map(map);
5763 error:
5764 isl_map_free(map);
5765 return NULL;
5766 }
5767
5768 /* Replace the space of "bmap" by "space".
5769 *
5770 * If the space of "bmap" is identical to "space" (including the identifiers
5771 * of the input and output dimensions), then simply return the original input.
5772 */
isl_basic_map_reset_space(__isl_take isl_basic_map * bmap,__isl_take isl_space * space)5773 __isl_give isl_basic_map *isl_basic_map_reset_space(
5774 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5775 {
5776 isl_bool equal;
5777 isl_space *bmap_space;
5778
5779 bmap_space = isl_basic_map_peek_space(bmap);
5780 equal = isl_space_is_equal(bmap_space, space);
5781 if (equal >= 0 && equal)
5782 equal = isl_space_has_equal_ids(bmap_space, space);
5783 if (equal < 0)
5784 goto error;
5785 if (equal) {
5786 isl_space_free(space);
5787 return bmap;
5788 }
5789 isl_space_free(isl_basic_map_take_space(bmap));
5790 bmap = isl_basic_map_restore_space(bmap, space);
5791
5792 bmap = isl_basic_map_finalize(bmap);
5793
5794 return bmap;
5795 error:
5796 isl_basic_map_free(bmap);
5797 isl_space_free(space);
5798 return NULL;
5799 }
5800
isl_basic_set_reset_space(__isl_take isl_basic_set * bset,__isl_take isl_space * space)5801 __isl_give isl_basic_set *isl_basic_set_reset_space(
5802 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
5803 {
5804 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5805 space));
5806 }
5807
5808 /* Check that the total dimensions of "map" and "space" are the same.
5809 */
check_map_space_equal_total_dim(__isl_keep isl_map * map,__isl_keep isl_space * space)5810 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5811 __isl_keep isl_space *space)
5812 {
5813 isl_size dim1, dim2;
5814
5815 dim1 = isl_map_dim(map, isl_dim_all);
5816 dim2 = isl_space_dim(space, isl_dim_all);
5817 if (dim1 < 0 || dim2 < 0)
5818 return isl_stat_error;
5819 if (dim1 == dim2)
5820 return isl_stat_ok;
5821 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5822 "total dimensions do not match", return isl_stat_error);
5823 }
5824
isl_map_reset_space(__isl_take isl_map * map,__isl_take isl_space * space)5825 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5826 __isl_take isl_space *space)
5827 {
5828 int i;
5829
5830 map = isl_map_cow(map);
5831 if (!map || !space)
5832 goto error;
5833
5834 for (i = 0; i < map->n; ++i) {
5835 map->p[i] = isl_basic_map_reset_space(map->p[i],
5836 isl_space_copy(space));
5837 if (!map->p[i])
5838 goto error;
5839 }
5840 isl_space_free(isl_map_take_space(map));
5841 map = isl_map_restore_space(map, space);
5842
5843 return map;
5844 error:
5845 isl_map_free(map);
5846 isl_space_free(space);
5847 return NULL;
5848 }
5849
5850 /* Replace the space of "map" by "space", without modifying
5851 * the dimension of "map".
5852 *
5853 * If the space of "map" is identical to "space" (including the identifiers
5854 * of the input and output dimensions), then simply return the original input.
5855 */
isl_map_reset_equal_dim_space(__isl_take isl_map * map,__isl_take isl_space * space)5856 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5857 __isl_take isl_space *space)
5858 {
5859 isl_bool equal;
5860 isl_space *map_space;
5861
5862 map_space = isl_map_peek_space(map);
5863 equal = isl_space_is_equal(map_space, space);
5864 if (equal >= 0 && equal)
5865 equal = isl_space_has_equal_ids(map_space, space);
5866 if (equal < 0)
5867 goto error;
5868 if (equal) {
5869 isl_space_free(space);
5870 return map;
5871 }
5872 if (check_map_space_equal_total_dim(map, space) < 0)
5873 goto error;
5874 return isl_map_reset_space(map, space);
5875 error:
5876 isl_map_free(map);
5877 isl_space_free(space);
5878 return NULL;
5879 }
5880
isl_set_reset_space(__isl_take isl_set * set,__isl_take isl_space * space)5881 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5882 __isl_take isl_space *space)
5883 {
5884 return set_from_map(isl_map_reset_space(set_to_map(set), space));
5885 }
5886
5887 /* Compute the parameter domain of the given basic set.
5888 */
isl_basic_set_params(__isl_take isl_basic_set * bset)5889 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5890 {
5891 isl_bool is_params;
5892 isl_space *space;
5893 isl_size n;
5894
5895 is_params = isl_basic_set_is_params(bset);
5896 if (is_params < 0)
5897 return isl_basic_set_free(bset);
5898 if (is_params)
5899 return bset;
5900
5901 n = isl_basic_set_dim(bset, isl_dim_set);
5902 if (n < 0)
5903 return isl_basic_set_free(bset);
5904 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5905 space = isl_basic_set_get_space(bset);
5906 space = isl_space_params(space);
5907 bset = isl_basic_set_reset_space(bset, space);
5908 return bset;
5909 }
5910
5911 /* Construct a zero-dimensional basic set with the given parameter domain.
5912 */
isl_basic_set_from_params(__isl_take isl_basic_set * bset)5913 __isl_give isl_basic_set *isl_basic_set_from_params(
5914 __isl_take isl_basic_set *bset)
5915 {
5916 isl_space *space;
5917 space = isl_basic_set_get_space(bset);
5918 space = isl_space_set_from_params(space);
5919 bset = isl_basic_set_reset_space(bset, space);
5920 return bset;
5921 }
5922
5923 /* Compute the parameter domain of the given set.
5924 */
isl_set_params(__isl_take isl_set * set)5925 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5926 {
5927 return isl_map_params(set_to_map(set));
5928 }
5929
5930 /* Construct a zero-dimensional set with the given parameter domain.
5931 */
isl_set_from_params(__isl_take isl_set * set)5932 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5933 {
5934 isl_space *space;
5935 space = isl_set_get_space(set);
5936 space = isl_space_set_from_params(space);
5937 set = isl_set_reset_space(set, space);
5938 return set;
5939 }
5940
5941 /* Compute the parameter domain of the given map.
5942 */
isl_map_params(__isl_take isl_map * map)5943 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5944 {
5945 isl_space *space;
5946 isl_size n_in, n_out;
5947
5948 n_in = isl_map_dim(map, isl_dim_in);
5949 n_out = isl_map_dim(map, isl_dim_out);
5950 if (n_in < 0 || n_out < 0)
5951 return isl_map_free(map);
5952 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
5953 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
5954 space = isl_map_get_space(map);
5955 space = isl_space_params(space);
5956 map = isl_map_reset_space(map, space);
5957 return map;
5958 }
5959
isl_basic_map_domain(__isl_take isl_basic_map * bmap)5960 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5961 {
5962 isl_space *space;
5963 isl_size n_out;
5964
5965 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5966 if (n_out < 0)
5967 return isl_basic_map_free(bmap);
5968 space = isl_space_domain(isl_basic_map_get_space(bmap));
5969
5970 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5971
5972 return isl_basic_map_reset_space(bmap, space);
5973 }
5974
isl_basic_map_may_be_set(__isl_keep isl_basic_map * bmap)5975 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5976 {
5977 if (!bmap)
5978 return isl_bool_error;
5979 return isl_space_may_be_set(bmap->dim);
5980 }
5981
5982 /* Is this basic map actually a set?
5983 * Users should never call this function. Outside of isl,
5984 * the type should indicate whether something is a set or a map.
5985 */
isl_basic_map_is_set(__isl_keep isl_basic_map * bmap)5986 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5987 {
5988 if (!bmap)
5989 return isl_bool_error;
5990 return isl_space_is_set(bmap->dim);
5991 }
5992
isl_basic_map_range(__isl_take isl_basic_map * bmap)5993 __isl_give isl_basic_set *isl_basic_map_range(__isl_take isl_basic_map *bmap)
5994 {
5995 isl_bool is_set;
5996
5997 is_set = isl_basic_map_is_set(bmap);
5998 if (is_set < 0)
5999 goto error;
6000 if (is_set)
6001 return bmap;
6002 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
6003 error:
6004 isl_basic_map_free(bmap);
6005 return NULL;
6006 }
6007
isl_basic_map_domain_map(__isl_take isl_basic_map * bmap)6008 __isl_give isl_basic_map *isl_basic_map_domain_map(
6009 __isl_take isl_basic_map *bmap)
6010 {
6011 int i;
6012 isl_space *space;
6013 isl_basic_map *domain;
6014 isl_size nparam, n_in, n_out;
6015
6016 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6017 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6018 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6019 if (nparam < 0 || n_in < 0 || n_out < 0)
6020 return isl_basic_map_free(bmap);
6021
6022 space = isl_basic_map_get_space(bmap);
6023 space = isl_space_from_range(isl_space_domain(space));
6024 domain = isl_basic_map_universe(space);
6025
6026 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6027 bmap = isl_basic_map_apply_range(bmap, domain);
6028 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
6029
6030 for (i = 0; i < n_in; ++i)
6031 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
6032 isl_dim_out, i);
6033
6034 bmap = isl_basic_map_gauss(bmap, NULL);
6035 return isl_basic_map_finalize(bmap);
6036 }
6037
isl_basic_map_range_map(__isl_take isl_basic_map * bmap)6038 __isl_give isl_basic_map *isl_basic_map_range_map(
6039 __isl_take isl_basic_map *bmap)
6040 {
6041 int i;
6042 isl_space *space;
6043 isl_basic_map *range;
6044 isl_size nparam, n_in, n_out;
6045
6046 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6047 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6048 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6049 if (nparam < 0 || n_in < 0 || n_out < 0)
6050 return isl_basic_map_free(bmap);
6051
6052 space = isl_basic_map_get_space(bmap);
6053 space = isl_space_from_range(isl_space_range(space));
6054 range = isl_basic_map_universe(space);
6055
6056 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6057 bmap = isl_basic_map_apply_range(bmap, range);
6058 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
6059
6060 for (i = 0; i < n_out; ++i)
6061 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
6062 isl_dim_out, i);
6063
6064 bmap = isl_basic_map_gauss(bmap, NULL);
6065 return isl_basic_map_finalize(bmap);
6066 }
6067
isl_map_may_be_set(__isl_keep isl_map * map)6068 int isl_map_may_be_set(__isl_keep isl_map *map)
6069 {
6070 if (!map)
6071 return -1;
6072 return isl_space_may_be_set(map->dim);
6073 }
6074
6075 /* Is this map actually a set?
6076 * Users should never call this function. Outside of isl,
6077 * the type should indicate whether something is a set or a map.
6078 */
isl_map_is_set(__isl_keep isl_map * map)6079 isl_bool isl_map_is_set(__isl_keep isl_map *map)
6080 {
6081 if (!map)
6082 return isl_bool_error;
6083 return isl_space_is_set(map->dim);
6084 }
6085
isl_map_range(__isl_take isl_map * map)6086 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
6087 {
6088 isl_space *space;
6089 isl_size n_in;
6090
6091 n_in = isl_map_dim(map, isl_dim_in);
6092 if (n_in < 0)
6093 return set_from_map(isl_map_free(map));
6094 space = isl_space_range(isl_map_get_space(map));
6095
6096 map = isl_map_project_out(map, isl_dim_in, 0, n_in);
6097
6098 return set_from_map(isl_map_reset_space(map, space));
6099 }
6100
6101 /* Transform "map" by applying "fn_space" to its space and "fn_bmap"
6102 * to each of its basic maps.
6103 */
isl_map_transform(__isl_take isl_map * map,__isl_give isl_space * (* fn_space)(__isl_take isl_space * space),__isl_give isl_basic_map * (* fn_bmap)(__isl_take isl_basic_map * bmap))6104 static __isl_give isl_map *isl_map_transform(__isl_take isl_map *map,
6105 __isl_give isl_space *(*fn_space)(__isl_take isl_space *space),
6106 __isl_give isl_basic_map *(*fn_bmap)(__isl_take isl_basic_map *bmap))
6107 {
6108 int i;
6109 isl_space *space;
6110
6111 map = isl_map_cow(map);
6112 if (!map)
6113 return NULL;
6114
6115 for (i = 0; i < map->n; ++i) {
6116 map->p[i] = fn_bmap(map->p[i]);
6117 if (!map->p[i])
6118 return isl_map_free(map);
6119 }
6120 map = isl_map_unmark_normalized(map);
6121
6122 space = isl_map_take_space(map);
6123 space = fn_space(space);
6124 map = isl_map_restore_space(map, space);
6125
6126 return map;
6127 }
6128
isl_map_domain_map(__isl_take isl_map * map)6129 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
6130 {
6131 return isl_map_transform(map, &isl_space_domain_map,
6132 &isl_basic_map_domain_map);
6133 }
6134
isl_map_range_map(__isl_take isl_map * map)6135 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
6136 {
6137 return isl_map_transform(map, &isl_space_range_map,
6138 &isl_basic_map_range_map);
6139 }
6140
6141 /* Given a wrapped map of the form A[B -> C],
6142 * return the map A[B -> C] -> B.
6143 */
isl_set_wrapped_domain_map(__isl_take isl_set * set)6144 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
6145 {
6146 isl_id *id;
6147 isl_map *map;
6148
6149 if (!set)
6150 return NULL;
6151 if (!isl_set_has_tuple_id(set))
6152 return isl_map_domain_map(isl_set_unwrap(set));
6153
6154 id = isl_set_get_tuple_id(set);
6155 map = isl_map_domain_map(isl_set_unwrap(set));
6156 map = isl_map_set_tuple_id(map, isl_dim_in, id);
6157
6158 return map;
6159 }
6160
isl_basic_map_from_domain(__isl_take isl_basic_set * bset)6161 __isl_give isl_basic_map *isl_basic_map_from_domain(
6162 __isl_take isl_basic_set *bset)
6163 {
6164 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
6165 }
6166
isl_basic_map_from_range(__isl_take isl_basic_set * bset)6167 __isl_give isl_basic_map *isl_basic_map_from_range(
6168 __isl_take isl_basic_set *bset)
6169 {
6170 isl_space *space;
6171 space = isl_basic_set_get_space(bset);
6172 space = isl_space_from_range(space);
6173 bset = isl_basic_set_reset_space(bset, space);
6174 return bset_to_bmap(bset);
6175 }
6176
6177 /* Create a relation with the given set as range.
6178 * The domain of the created relation is a zero-dimensional
6179 * flat anonymous space.
6180 */
isl_map_from_range(__isl_take isl_set * set)6181 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
6182 {
6183 isl_space *space;
6184 space = isl_set_get_space(set);
6185 space = isl_space_from_range(space);
6186 set = isl_set_reset_space(set, space);
6187 return set_to_map(set);
6188 }
6189
6190 /* Create a relation with the given set as domain.
6191 * The range of the created relation is a zero-dimensional
6192 * flat anonymous space.
6193 */
isl_map_from_domain(__isl_take isl_set * set)6194 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
6195 {
6196 return isl_map_reverse(isl_map_from_range(set));
6197 }
6198
isl_basic_map_from_domain_and_range(__isl_take isl_basic_set * domain,__isl_take isl_basic_set * range)6199 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
6200 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
6201 {
6202 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
6203 }
6204
isl_map_from_domain_and_range(__isl_take isl_set * domain,__isl_take isl_set * range)6205 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
6206 __isl_take isl_set *range)
6207 {
6208 return isl_map_apply_range(isl_map_reverse(domain), range);
6209 }
6210
6211 /* Return a newly allocated isl_map with given space and flags and
6212 * room for "n" basic maps.
6213 * Make sure that all cached information is cleared.
6214 */
isl_map_alloc_space(__isl_take isl_space * space,int n,unsigned flags)6215 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
6216 unsigned flags)
6217 {
6218 struct isl_map *map;
6219
6220 if (!space)
6221 return NULL;
6222 if (n < 0)
6223 isl_die(space->ctx, isl_error_internal,
6224 "negative number of basic maps", goto error);
6225 map = isl_calloc(space->ctx, struct isl_map,
6226 sizeof(struct isl_map) +
6227 (n - 1) * sizeof(struct isl_basic_map *));
6228 if (!map)
6229 goto error;
6230
6231 map->ctx = space->ctx;
6232 isl_ctx_ref(map->ctx);
6233 map->ref = 1;
6234 map->size = n;
6235 map->n = 0;
6236 map->dim = space;
6237 map->flags = flags;
6238 return map;
6239 error:
6240 isl_space_free(space);
6241 return NULL;
6242 }
6243
isl_basic_map_empty(__isl_take isl_space * space)6244 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6245 {
6246 struct isl_basic_map *bmap;
6247 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6248 bmap = isl_basic_map_set_to_empty(bmap);
6249 return bmap;
6250 }
6251
isl_basic_set_empty(__isl_take isl_space * space)6252 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6253 {
6254 struct isl_basic_set *bset;
6255 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6256 bset = isl_basic_set_set_to_empty(bset);
6257 return bset;
6258 }
6259
isl_basic_map_universe(__isl_take isl_space * space)6260 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6261 {
6262 struct isl_basic_map *bmap;
6263 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6264 bmap = isl_basic_map_finalize(bmap);
6265 return bmap;
6266 }
6267
isl_basic_set_universe(__isl_take isl_space * space)6268 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6269 {
6270 struct isl_basic_set *bset;
6271 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6272 bset = isl_basic_set_finalize(bset);
6273 return bset;
6274 }
6275
isl_basic_map_nat_universe(__isl_take isl_space * space)6276 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6277 __isl_take isl_space *space)
6278 {
6279 int i;
6280 isl_size total = isl_space_dim(space, isl_dim_all);
6281 isl_basic_map *bmap;
6282
6283 if (total < 0)
6284 space = isl_space_free(space);
6285 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6286 for (i = 0; i < total; ++i) {
6287 int k = isl_basic_map_alloc_inequality(bmap);
6288 if (k < 0)
6289 goto error;
6290 isl_seq_clr(bmap->ineq[k], 1 + total);
6291 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6292 }
6293 return bmap;
6294 error:
6295 isl_basic_map_free(bmap);
6296 return NULL;
6297 }
6298
isl_basic_set_nat_universe(__isl_take isl_space * space)6299 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6300 __isl_take isl_space *space)
6301 {
6302 return isl_basic_map_nat_universe(space);
6303 }
6304
isl_map_nat_universe(__isl_take isl_space * space)6305 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *space)
6306 {
6307 return isl_map_from_basic_map(isl_basic_map_nat_universe(space));
6308 }
6309
isl_set_nat_universe(__isl_take isl_space * space)6310 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *space)
6311 {
6312 return isl_map_nat_universe(space);
6313 }
6314
isl_map_empty(__isl_take isl_space * space)6315 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6316 {
6317 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6318 }
6319
isl_set_empty(__isl_take isl_space * space)6320 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6321 {
6322 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6323 }
6324
isl_map_universe(__isl_take isl_space * space)6325 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6326 {
6327 struct isl_map *map;
6328 if (!space)
6329 return NULL;
6330 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6331 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6332 return map;
6333 }
6334
6335 /* This function performs the same operation as isl_map_universe,
6336 * but is considered as a function on an isl_space when exported.
6337 */
isl_space_universe_map(__isl_take isl_space * space)6338 __isl_give isl_map *isl_space_universe_map(__isl_take isl_space *space)
6339 {
6340 return isl_map_universe(space);
6341 }
6342
isl_set_universe(__isl_take isl_space * space)6343 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6344 {
6345 struct isl_set *set;
6346 if (!space)
6347 return NULL;
6348 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6349 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6350 return set;
6351 }
6352
6353 /* This function performs the same operation as isl_set_universe,
6354 * but is considered as a function on an isl_space when exported.
6355 */
isl_space_universe_set(__isl_take isl_space * space)6356 __isl_give isl_set *isl_space_universe_set(__isl_take isl_space *space)
6357 {
6358 return isl_set_universe(space);
6359 }
6360
isl_map_dup(__isl_keep isl_map * map)6361 __isl_give isl_map *isl_map_dup(__isl_keep isl_map *map)
6362 {
6363 int i;
6364 struct isl_map *dup;
6365
6366 if (!map)
6367 return NULL;
6368 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6369 for (i = 0; i < map->n; ++i)
6370 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6371 return dup;
6372 }
6373
isl_map_add_basic_map(__isl_take isl_map * map,__isl_take isl_basic_map * bmap)6374 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6375 __isl_take isl_basic_map *bmap)
6376 {
6377 if (!bmap || !map)
6378 goto error;
6379 if (isl_basic_map_plain_is_empty(bmap)) {
6380 isl_basic_map_free(bmap);
6381 return map;
6382 }
6383 if (isl_map_basic_map_check_equal_space(map, bmap) < 0)
6384 goto error;
6385 isl_assert(map->ctx, map->n < map->size, goto error);
6386 map->p[map->n] = bmap;
6387 map->n++;
6388 map = isl_map_unmark_normalized(map);
6389 return map;
6390 error:
6391 if (map)
6392 isl_map_free(map);
6393 if (bmap)
6394 isl_basic_map_free(bmap);
6395 return NULL;
6396 }
6397
isl_map_free(__isl_take isl_map * map)6398 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6399 {
6400 int i;
6401
6402 if (!map)
6403 return NULL;
6404
6405 if (--map->ref > 0)
6406 return NULL;
6407
6408 clear_caches(map);
6409 isl_ctx_deref(map->ctx);
6410 for (i = 0; i < map->n; ++i)
6411 isl_basic_map_free(map->p[i]);
6412 isl_space_free(map->dim);
6413 free(map);
6414
6415 return NULL;
6416 }
6417
isl_basic_map_fix_pos_si(__isl_take isl_basic_map * bmap,unsigned pos,int value)6418 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6419 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6420 {
6421 int j;
6422 isl_size total;
6423
6424 total = isl_basic_map_dim(bmap, isl_dim_all);
6425 if (total < 0)
6426 return isl_basic_map_free(bmap);
6427
6428 bmap = isl_basic_map_cow(bmap);
6429 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6430 j = isl_basic_map_alloc_equality(bmap);
6431 if (j < 0)
6432 goto error;
6433 isl_seq_clr(bmap->eq[j] + 1, total);
6434 isl_int_set_si(bmap->eq[j][pos], -1);
6435 isl_int_set_si(bmap->eq[j][0], value);
6436 bmap = isl_basic_map_simplify(bmap);
6437 return isl_basic_map_finalize(bmap);
6438 error:
6439 isl_basic_map_free(bmap);
6440 return NULL;
6441 }
6442
isl_basic_map_fix_pos(__isl_take isl_basic_map * bmap,unsigned pos,isl_int value)6443 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6444 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6445 {
6446 int j;
6447 isl_size total;
6448
6449 total = isl_basic_map_dim(bmap, isl_dim_all);
6450 if (total < 0)
6451 return isl_basic_map_free(bmap);
6452
6453 bmap = isl_basic_map_cow(bmap);
6454 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6455 j = isl_basic_map_alloc_equality(bmap);
6456 if (j < 0)
6457 goto error;
6458 isl_seq_clr(bmap->eq[j] + 1, total);
6459 isl_int_set_si(bmap->eq[j][pos], -1);
6460 isl_int_set(bmap->eq[j][0], value);
6461 bmap = isl_basic_map_simplify(bmap);
6462 return isl_basic_map_finalize(bmap);
6463 error:
6464 isl_basic_map_free(bmap);
6465 return NULL;
6466 }
6467
isl_basic_map_fix_si(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,int value)6468 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6469 enum isl_dim_type type, unsigned pos, int value)
6470 {
6471 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6472 return isl_basic_map_free(bmap);
6473 return isl_basic_map_fix_pos_si(bmap,
6474 isl_basic_map_offset(bmap, type) + pos, value);
6475 }
6476
isl_basic_map_fix(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,isl_int value)6477 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6478 enum isl_dim_type type, unsigned pos, isl_int value)
6479 {
6480 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6481 return isl_basic_map_free(bmap);
6482 return isl_basic_map_fix_pos(bmap,
6483 isl_basic_map_offset(bmap, type) + pos, value);
6484 }
6485
6486 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6487 * to be equal to "v".
6488 */
isl_basic_map_fix_val(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,__isl_take isl_val * v)6489 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6490 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6491 {
6492 if (!bmap || !v)
6493 goto error;
6494 if (!isl_val_is_int(v))
6495 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6496 "expecting integer value", goto error);
6497 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6498 goto error;
6499 pos += isl_basic_map_offset(bmap, type);
6500 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6501 isl_val_free(v);
6502 return bmap;
6503 error:
6504 isl_basic_map_free(bmap);
6505 isl_val_free(v);
6506 return NULL;
6507 }
6508
6509 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6510 * to be equal to "v".
6511 */
isl_basic_set_fix_val(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,__isl_take isl_val * v)6512 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6513 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6514 {
6515 return isl_basic_map_fix_val(bset, type, pos, v);
6516 }
6517
isl_basic_set_fix_si(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,int value)6518 __isl_give isl_basic_set *isl_basic_set_fix_si(__isl_take isl_basic_set *bset,
6519 enum isl_dim_type type, unsigned pos, int value)
6520 {
6521 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6522 type, pos, value));
6523 }
6524
isl_basic_set_fix(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,isl_int value)6525 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6526 enum isl_dim_type type, unsigned pos, isl_int value)
6527 {
6528 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6529 type, pos, value));
6530 }
6531
6532 /* Remove the basic map at position "i" from "map" if this basic map
6533 * is (obviously) empty.
6534 */
remove_if_empty(__isl_take isl_map * map,int i)6535 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6536 {
6537 isl_bool empty;
6538
6539 if (!map)
6540 return NULL;
6541
6542 empty = isl_basic_map_plain_is_empty(map->p[i]);
6543 if (empty < 0)
6544 return isl_map_free(map);
6545 if (!empty)
6546 return map;
6547
6548 isl_basic_map_free(map->p[i]);
6549 map->n--;
6550 if (i != map->n) {
6551 map->p[i] = map->p[map->n];
6552 map = isl_map_unmark_normalized(map);
6553
6554 }
6555
6556 return map;
6557 }
6558
6559 /* Perform "fn" on each basic map of "map", where we may not be holding
6560 * the only reference to "map".
6561 * In particular, "fn" should be a semantics preserving operation
6562 * that we want to apply to all copies of "map". We therefore need
6563 * to be careful not to modify "map" in a way that breaks "map"
6564 * in case anything goes wrong.
6565 */
isl_map_inline_foreach_basic_map(__isl_take isl_map * map,__isl_give isl_basic_map * (* fn)(__isl_take isl_basic_map * bmap))6566 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6567 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6568 {
6569 struct isl_basic_map *bmap;
6570 int i;
6571
6572 if (!map)
6573 return NULL;
6574
6575 for (i = map->n - 1; i >= 0; --i) {
6576 bmap = isl_basic_map_copy(map->p[i]);
6577 bmap = fn(bmap);
6578 if (!bmap)
6579 goto error;
6580 isl_basic_map_free(map->p[i]);
6581 map->p[i] = bmap;
6582 map = remove_if_empty(map, i);
6583 if (!map)
6584 return NULL;
6585 }
6586
6587 return map;
6588 error:
6589 isl_map_free(map);
6590 return NULL;
6591 }
6592
isl_map_fix_si(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,int value)6593 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6594 enum isl_dim_type type, unsigned pos, int value)
6595 {
6596 int i;
6597
6598 map = isl_map_cow(map);
6599 if (isl_map_check_range(map, type, pos, 1) < 0)
6600 return isl_map_free(map);
6601 for (i = map->n - 1; i >= 0; --i) {
6602 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6603 map = remove_if_empty(map, i);
6604 if (!map)
6605 return NULL;
6606 }
6607 map = isl_map_unmark_normalized(map);
6608 return map;
6609 }
6610
isl_set_fix_si(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,int value)6611 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6612 enum isl_dim_type type, unsigned pos, int value)
6613 {
6614 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6615 }
6616
isl_map_fix(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,isl_int value)6617 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6618 enum isl_dim_type type, unsigned pos, isl_int value)
6619 {
6620 int i;
6621
6622 map = isl_map_cow(map);
6623 if (isl_map_check_range(map, type, pos, 1) < 0)
6624 return isl_map_free(map);
6625 for (i = 0; i < map->n; ++i) {
6626 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6627 if (!map->p[i])
6628 goto error;
6629 }
6630 map = isl_map_unmark_normalized(map);
6631 return map;
6632 error:
6633 isl_map_free(map);
6634 return NULL;
6635 }
6636
isl_set_fix(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,isl_int value)6637 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6638 enum isl_dim_type type, unsigned pos, isl_int value)
6639 {
6640 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6641 }
6642
6643 /* Fix the value of the variable at position "pos" of type "type" of "map"
6644 * to be equal to "v".
6645 */
isl_map_fix_val(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,__isl_take isl_val * v)6646 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6647 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6648 {
6649 int i;
6650
6651 map = isl_map_cow(map);
6652 if (!map || !v)
6653 goto error;
6654
6655 if (!isl_val_is_int(v))
6656 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6657 "expecting integer value", goto error);
6658 if (isl_map_check_range(map, type, pos, 1) < 0)
6659 goto error;
6660 for (i = map->n - 1; i >= 0; --i) {
6661 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6662 isl_val_copy(v));
6663 map = remove_if_empty(map, i);
6664 if (!map)
6665 goto error;
6666 }
6667 map = isl_map_unmark_normalized(map);
6668 isl_val_free(v);
6669 return map;
6670 error:
6671 isl_map_free(map);
6672 isl_val_free(v);
6673 return NULL;
6674 }
6675
6676 /* Fix the value of the variable at position "pos" of type "type" of "set"
6677 * to be equal to "v".
6678 */
isl_set_fix_val(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,__isl_take isl_val * v)6679 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6680 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6681 {
6682 return isl_map_fix_val(set, type, pos, v);
6683 }
6684
isl_map_fix_input_si(__isl_take isl_map * map,unsigned input,int value)6685 __isl_give isl_map *isl_map_fix_input_si(__isl_take isl_map *map,
6686 unsigned input, int value)
6687 {
6688 return isl_map_fix_si(map, isl_dim_in, input, value);
6689 }
6690
isl_set_fix_dim_si(__isl_take isl_set * set,unsigned dim,int value)6691 __isl_give isl_set *isl_set_fix_dim_si(__isl_take isl_set *set, unsigned dim,
6692 int value)
6693 {
6694 return set_from_map(isl_map_fix_si(set_to_map(set),
6695 isl_dim_set, dim, value));
6696 }
6697
basic_map_bound_si(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,int value,int upper)6698 static __isl_give isl_basic_map *basic_map_bound_si(
6699 __isl_take isl_basic_map *bmap,
6700 enum isl_dim_type type, unsigned pos, int value, int upper)
6701 {
6702 int j;
6703 isl_size total;
6704
6705 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6706 return isl_basic_map_free(bmap);
6707 total = isl_basic_map_dim(bmap, isl_dim_all);
6708 if (total < 0)
6709 return isl_basic_map_free(bmap);
6710 pos += isl_basic_map_offset(bmap, type);
6711 bmap = isl_basic_map_cow(bmap);
6712 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6713 j = isl_basic_map_alloc_inequality(bmap);
6714 if (j < 0)
6715 goto error;
6716 isl_seq_clr(bmap->ineq[j], 1 + total);
6717 if (upper) {
6718 isl_int_set_si(bmap->ineq[j][pos], -1);
6719 isl_int_set_si(bmap->ineq[j][0], value);
6720 } else {
6721 isl_int_set_si(bmap->ineq[j][pos], 1);
6722 isl_int_set_si(bmap->ineq[j][0], -value);
6723 }
6724 bmap = isl_basic_map_simplify(bmap);
6725 return isl_basic_map_finalize(bmap);
6726 error:
6727 isl_basic_map_free(bmap);
6728 return NULL;
6729 }
6730
isl_basic_map_lower_bound_si(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,int value)6731 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6732 __isl_take isl_basic_map *bmap,
6733 enum isl_dim_type type, unsigned pos, int value)
6734 {
6735 return basic_map_bound_si(bmap, type, pos, value, 0);
6736 }
6737
6738 /* Constrain the values of the given dimension to be no greater than "value".
6739 */
isl_basic_map_upper_bound_si(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,int value)6740 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6741 __isl_take isl_basic_map *bmap,
6742 enum isl_dim_type type, unsigned pos, int value)
6743 {
6744 return basic_map_bound_si(bmap, type, pos, value, 1);
6745 }
6746
map_bound_si(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,int value,int upper)6747 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6748 enum isl_dim_type type, unsigned pos, int value, int upper)
6749 {
6750 int i;
6751
6752 map = isl_map_cow(map);
6753 if (isl_map_check_range(map, type, pos, 1) < 0)
6754 return isl_map_free(map);
6755 for (i = 0; i < map->n; ++i) {
6756 map->p[i] = basic_map_bound_si(map->p[i],
6757 type, pos, value, upper);
6758 if (!map->p[i])
6759 goto error;
6760 }
6761 map = isl_map_unmark_normalized(map);
6762 return map;
6763 error:
6764 isl_map_free(map);
6765 return NULL;
6766 }
6767
isl_map_lower_bound_si(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,int value)6768 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6769 enum isl_dim_type type, unsigned pos, int value)
6770 {
6771 return map_bound_si(map, type, pos, value, 0);
6772 }
6773
isl_map_upper_bound_si(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,int value)6774 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6775 enum isl_dim_type type, unsigned pos, int value)
6776 {
6777 return map_bound_si(map, type, pos, value, 1);
6778 }
6779
isl_set_lower_bound_si(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,int value)6780 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6781 enum isl_dim_type type, unsigned pos, int value)
6782 {
6783 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6784 type, pos, value));
6785 }
6786
isl_set_upper_bound_si(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,int value)6787 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6788 enum isl_dim_type type, unsigned pos, int value)
6789 {
6790 return isl_map_upper_bound_si(set, type, pos, value);
6791 }
6792
6793 /* Bound the given variable of "bmap" from below (or above is "upper"
6794 * is set) to "value".
6795 */
basic_map_bound(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,isl_int value,int upper)6796 static __isl_give isl_basic_map *basic_map_bound(
6797 __isl_take isl_basic_map *bmap,
6798 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6799 {
6800 int j;
6801 isl_size total;
6802
6803 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6804 return isl_basic_map_free(bmap);
6805 total = isl_basic_map_dim(bmap, isl_dim_all);
6806 if (total < 0)
6807 return isl_basic_map_free(bmap);
6808 pos += isl_basic_map_offset(bmap, type);
6809 bmap = isl_basic_map_cow(bmap);
6810 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6811 j = isl_basic_map_alloc_inequality(bmap);
6812 if (j < 0)
6813 goto error;
6814 isl_seq_clr(bmap->ineq[j], 1 + total);
6815 if (upper) {
6816 isl_int_set_si(bmap->ineq[j][pos], -1);
6817 isl_int_set(bmap->ineq[j][0], value);
6818 } else {
6819 isl_int_set_si(bmap->ineq[j][pos], 1);
6820 isl_int_neg(bmap->ineq[j][0], value);
6821 }
6822 bmap = isl_basic_map_simplify(bmap);
6823 return isl_basic_map_finalize(bmap);
6824 error:
6825 isl_basic_map_free(bmap);
6826 return NULL;
6827 }
6828
6829 /* Bound the given variable of "map" from below (or above is "upper"
6830 * is set) to "value".
6831 */
map_bound(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,isl_int value,int upper)6832 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6833 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6834 {
6835 int i;
6836
6837 map = isl_map_cow(map);
6838 if (isl_map_check_range(map, type, pos, 1) < 0)
6839 return isl_map_free(map);
6840 for (i = map->n - 1; i >= 0; --i) {
6841 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6842 map = remove_if_empty(map, i);
6843 if (!map)
6844 return NULL;
6845 }
6846 map = isl_map_unmark_normalized(map);
6847 return map;
6848 }
6849
isl_map_lower_bound(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,isl_int value)6850 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6851 enum isl_dim_type type, unsigned pos, isl_int value)
6852 {
6853 return map_bound(map, type, pos, value, 0);
6854 }
6855
isl_map_upper_bound(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,isl_int value)6856 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6857 enum isl_dim_type type, unsigned pos, isl_int value)
6858 {
6859 return map_bound(map, type, pos, value, 1);
6860 }
6861
isl_set_lower_bound(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,isl_int value)6862 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6863 enum isl_dim_type type, unsigned pos, isl_int value)
6864 {
6865 return isl_map_lower_bound(set, type, pos, value);
6866 }
6867
isl_set_upper_bound(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,isl_int value)6868 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6869 enum isl_dim_type type, unsigned pos, isl_int value)
6870 {
6871 return isl_map_upper_bound(set, type, pos, value);
6872 }
6873
6874 /* Force the values of the variable at position "pos" of type "type" of "map"
6875 * to be no smaller than "value".
6876 */
isl_map_lower_bound_val(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)6877 __isl_give isl_map *isl_map_lower_bound_val(__isl_take isl_map *map,
6878 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6879 {
6880 if (!value)
6881 goto error;
6882 if (!isl_val_is_int(value))
6883 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6884 "expecting integer value", goto error);
6885 map = isl_map_lower_bound(map, type, pos, value->n);
6886 isl_val_free(value);
6887 return map;
6888 error:
6889 isl_val_free(value);
6890 isl_map_free(map);
6891 return NULL;
6892 }
6893
6894 /* Force the values of the variable at position "pos" of type "type" of "set"
6895 * to be no smaller than "value".
6896 */
isl_set_lower_bound_val(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)6897 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6898 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6899 {
6900 isl_map *map;
6901
6902 map = set_to_map(set);
6903 return set_from_map(isl_map_lower_bound_val(map, type, pos, value));
6904 }
6905
6906 /* Force the values of the variable at position "pos" of type "type" of "map"
6907 * to be no greater than "value".
6908 */
isl_map_upper_bound_val(__isl_take isl_map * map,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)6909 __isl_give isl_map *isl_map_upper_bound_val(__isl_take isl_map *map,
6910 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6911 {
6912 if (!value)
6913 goto error;
6914 if (!isl_val_is_int(value))
6915 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6916 "expecting integer value", goto error);
6917 map = isl_map_upper_bound(map, type, pos, value->n);
6918 isl_val_free(value);
6919 return map;
6920 error:
6921 isl_val_free(value);
6922 isl_map_free(map);
6923 return NULL;
6924 }
6925
6926 /* Force the values of the variable at position "pos" of type "type" of "set"
6927 * to be no greater than "value".
6928 */
isl_set_upper_bound_val(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)6929 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6930 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6931 {
6932 isl_map *map;
6933
6934 map = set_to_map(set);
6935 return set_from_map(isl_map_upper_bound_val(map, type, pos, value));
6936 }
6937
6938 /* If "mv" has an explicit domain, then intersect the domain of "map"
6939 * with this explicit domain.
6940 *
6941 * An isl_multi_val object never has an explicit domain,
6942 * so simply return "map".
6943 */
isl_map_intersect_multi_val_explicit_domain(__isl_take isl_map * map,__isl_keep isl_multi_val * mv)6944 static __isl_give isl_map *isl_map_intersect_multi_val_explicit_domain(
6945 __isl_take isl_map *map, __isl_keep isl_multi_val *mv)
6946 {
6947 return map;
6948 }
6949
6950 #undef BASE
6951 #define BASE val
6952 #include "isl_map_bound_templ.c"
6953
6954 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6955 * for each set dimension, by treating the set as a map.
6956 */
set_bound_multi_val(__isl_take isl_set * set,__isl_take isl_multi_val * bound,__isl_give isl_map * map_bound (__isl_take isl_map * map,unsigned pos,__isl_take isl_val * value))6957 static __isl_give isl_set *set_bound_multi_val(__isl_take isl_set *set,
6958 __isl_take isl_multi_val *bound,
6959 __isl_give isl_map *map_bound(__isl_take isl_map *map,
6960 unsigned pos, __isl_take isl_val *value))
6961 {
6962 isl_map *map;
6963
6964 map = set_to_map(set);
6965 return set_from_map(map_bound_multi_val(map, bound, map_bound));
6966 }
6967
6968 #undef BASE
6969 #define BASE pw_aff
6970 #include "isl_map_bound_templ.c"
6971
6972 /* Apply "map_bound" to "set" with the corresponding value in "bound"
6973 * for each set dimension, by converting the set and the bound
6974 * to objects living in a map space.
6975 */
set_bound_multi_pw_aff(__isl_take isl_set * set,__isl_take isl_multi_pw_aff * bound,__isl_give isl_map * set_bound (__isl_take isl_map * map,unsigned pos,__isl_take TYPE * value))6976 static __isl_give isl_set *set_bound_multi_pw_aff(__isl_take isl_set *set,
6977 __isl_take isl_multi_pw_aff *bound,
6978 __isl_give isl_map *set_bound(__isl_take isl_map *map,
6979 unsigned pos, __isl_take TYPE *value))
6980 {
6981 isl_map *map;
6982
6983 map = isl_map_from_range(set);
6984 bound = isl_multi_pw_aff_from_range(bound);
6985 map = map_bound_multi_pw_aff(map, bound, set_bound);
6986 return isl_map_range(map);
6987 }
6988
6989 /* Wrapper around isl_map_lower_bound_val for use in map_bound_multi_val,
6990 * setting a bound on the given output dimension.
6991 */
map_lower_bound_val(__isl_take isl_map * map,unsigned pos,__isl_take isl_val * v)6992 static __isl_give isl_map *map_lower_bound_val(__isl_take isl_map *map,
6993 unsigned pos, __isl_take isl_val *v)
6994 {
6995 return isl_map_lower_bound_val(map, isl_dim_out, pos, v);
6996 }
6997
6998 /* Force the values of the set dimensions of "set"
6999 * to be no smaller than the corresponding values in "lower".
7000 */
isl_set_lower_bound_multi_val(__isl_take isl_set * set,__isl_take isl_multi_val * lower)7001 __isl_give isl_set *isl_set_lower_bound_multi_val(__isl_take isl_set *set,
7002 __isl_take isl_multi_val *lower)
7003 {
7004 return set_bound_multi_val(set, lower, &map_lower_bound_val);
7005 }
7006
7007 /* Wrapper around isl_map_upper_bound_val for use in map_bound_multi_val,
7008 * setting a bound on the given output dimension.
7009 */
map_upper_bound_val(__isl_take isl_map * map,unsigned pos,__isl_take isl_val * v)7010 static __isl_give isl_map *map_upper_bound_val(__isl_take isl_map *map,
7011 unsigned pos, __isl_take isl_val *v)
7012 {
7013 return isl_map_upper_bound_val(map, isl_dim_out, pos, v);
7014 }
7015
7016 /* Force the values of the set dimensions of "set"
7017 * to be no greater than the corresponding values in "upper".
7018 */
isl_set_upper_bound_multi_val(__isl_take isl_set * set,__isl_take isl_multi_val * upper)7019 __isl_give isl_set *isl_set_upper_bound_multi_val(__isl_take isl_set *set,
7020 __isl_take isl_multi_val *upper)
7021 {
7022 return set_bound_multi_val(set, upper, &map_upper_bound_val);
7023 }
7024
7025 /* Force the symbolic constant expression "bound"
7026 * to satisfy the relation "order" with respect to
7027 * the output variable at position "pos" of "map".
7028 *
7029 * Create an affine expression representing the output variable
7030 * in terms of the range and
7031 * compare it using "order" to "bound" (defined on the domain).
7032 * The result is a relation between elements in domain and range that
7033 * can be intersected with "map".
7034 */
map_bound_pw_aff(__isl_take isl_map * map,unsigned pos,__isl_take isl_pw_aff * bound,__isl_give isl_map * (* order)(__isl_take isl_pw_aff * pa1,__isl_take isl_pw_aff * pa2))7035 static __isl_give isl_map *map_bound_pw_aff(__isl_take isl_map *map,
7036 unsigned pos, __isl_take isl_pw_aff *bound,
7037 __isl_give isl_map *(*order)(__isl_take isl_pw_aff *pa1,
7038 __isl_take isl_pw_aff *pa2))
7039 {
7040 isl_space *space;
7041 isl_local_space *ls;
7042 isl_pw_aff *var;
7043
7044 space = isl_space_range(isl_map_get_space(map));
7045 ls = isl_local_space_from_space(space);
7046 var = isl_pw_aff_var_on_domain(ls, isl_dim_set, pos);
7047 map = isl_map_intersect(map, order(bound, var));
7048 return map;
7049 }
7050
7051 /* Force the values of the output variable at position "pos" of "map"
7052 * to be no smaller than the symbolic constant expression "lower".
7053 */
map_lower_bound_pw_aff(__isl_take isl_map * map,unsigned pos,__isl_take isl_pw_aff * lower)7054 static __isl_give isl_map *map_lower_bound_pw_aff(__isl_take isl_map *map,
7055 unsigned pos, __isl_take isl_pw_aff *lower)
7056 {
7057 return map_bound_pw_aff(map, pos, lower, &isl_pw_aff_le_map);
7058 }
7059
7060 /* Force the values of the output variable at position "pos" of "map"
7061 * to be no greater than the symbolic constant expression "upper".
7062 */
map_upper_bound_pw_aff(__isl_take isl_map * map,unsigned pos,__isl_take isl_pw_aff * upper)7063 static __isl_give isl_map *map_upper_bound_pw_aff(__isl_take isl_map *map,
7064 unsigned pos, __isl_take isl_pw_aff *upper)
7065 {
7066 return map_bound_pw_aff(map, pos, upper, &isl_pw_aff_ge_map);
7067 }
7068
7069 /* Force the values of the set dimensions of "set"
7070 * to be no smaller than the corresponding constant symbolic expressions
7071 * in "lower".
7072 */
isl_set_lower_bound_multi_pw_aff(__isl_take isl_set * set,__isl_take isl_multi_pw_aff * lower)7073 __isl_give isl_set *isl_set_lower_bound_multi_pw_aff(__isl_take isl_set *set,
7074 __isl_take isl_multi_pw_aff *lower)
7075 {
7076 return set_bound_multi_pw_aff(set, lower, &map_lower_bound_pw_aff);
7077 }
7078
7079 /* Force the values of the set dimensions of "set"
7080 * to be no greater than the corresponding constant symbolic expressions
7081 * in "upper".
7082 */
isl_set_upper_bound_multi_pw_aff(__isl_take isl_set * set,__isl_take isl_multi_pw_aff * upper)7083 __isl_give isl_set *isl_set_upper_bound_multi_pw_aff(__isl_take isl_set *set,
7084 __isl_take isl_multi_pw_aff *upper)
7085 {
7086 return set_bound_multi_pw_aff(set, upper, &map_upper_bound_pw_aff);
7087 }
7088
7089 /* Force the values of the output dimensions of "map"
7090 * to be no smaller than the corresponding constant symbolic expressions
7091 * in "lower".
7092 */
isl_map_lower_bound_multi_pw_aff(__isl_take isl_map * map,__isl_take isl_multi_pw_aff * lower)7093 __isl_give isl_map *isl_map_lower_bound_multi_pw_aff(__isl_take isl_map *map,
7094 __isl_take isl_multi_pw_aff *lower)
7095 {
7096 return map_bound_multi_pw_aff(map, lower, &map_lower_bound_pw_aff);
7097 }
7098
7099 /* Force the values of the output dimensions of "map"
7100 * to be no greater than the corresponding constant symbolic expressions
7101 * in "upper".
7102 */
isl_map_upper_bound_multi_pw_aff(__isl_take isl_map * map,__isl_take isl_multi_pw_aff * upper)7103 __isl_give isl_map *isl_map_upper_bound_multi_pw_aff(__isl_take isl_map *map,
7104 __isl_take isl_multi_pw_aff *upper)
7105 {
7106 return map_bound_multi_pw_aff(map, upper, &map_upper_bound_pw_aff);
7107 }
7108
7109 /* Bound the given variable of "bset" from below (or above is "upper"
7110 * is set) to "value".
7111 */
isl_basic_set_bound(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,isl_int value,int upper)7112 static __isl_give isl_basic_set *isl_basic_set_bound(
7113 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7114 isl_int value, int upper)
7115 {
7116 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
7117 type, pos, value, upper));
7118 }
7119
7120 /* Bound the given variable of "bset" from below (or above is "upper"
7121 * is set) to "value".
7122 */
isl_basic_set_bound_val(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value,int upper)7123 static __isl_give isl_basic_set *isl_basic_set_bound_val(
7124 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7125 __isl_take isl_val *value, int upper)
7126 {
7127 if (!value)
7128 goto error;
7129 if (!isl_val_is_int(value))
7130 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
7131 "expecting integer value", goto error);
7132 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
7133 isl_val_free(value);
7134 return bset;
7135 error:
7136 isl_val_free(value);
7137 isl_basic_set_free(bset);
7138 return NULL;
7139 }
7140
7141 /* Bound the given variable of "bset" from below to "value".
7142 */
isl_basic_set_lower_bound_val(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)7143 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
7144 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7145 __isl_take isl_val *value)
7146 {
7147 return isl_basic_set_bound_val(bset, type, pos, value, 0);
7148 }
7149
7150 /* Bound the given variable of "bset" from above to "value".
7151 */
isl_basic_set_upper_bound_val(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,__isl_take isl_val * value)7152 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
7153 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
7154 __isl_take isl_val *value)
7155 {
7156 return isl_basic_set_bound_val(bset, type, pos, value, 1);
7157 }
7158
isl_map_reverse(__isl_take isl_map * map)7159 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
7160 {
7161 return isl_map_transform(map, &isl_space_reverse,
7162 &isl_basic_map_reverse);
7163 }
7164
7165 /* Given a map A -> (B -> C), return the corresponding map A -> (C -> B).
7166 */
isl_map_range_reverse(__isl_take isl_map * map)7167 __isl_give isl_map *isl_map_range_reverse(__isl_take isl_map *map)
7168 {
7169 return isl_map_transform(map, &isl_space_range_reverse,
7170 &isl_basic_map_range_reverse);
7171 }
7172
7173 #undef TYPE
7174 #define TYPE isl_pw_multi_aff
7175 #undef SUFFIX
7176 #define SUFFIX _pw_multi_aff
7177 #undef EMPTY
7178 #define EMPTY isl_pw_multi_aff_empty
7179 #undef ADD
7180 #define ADD isl_pw_multi_aff_union_add
7181 #include "isl_map_lexopt_templ.c"
7182
7183 /* Given a map "map", compute the lexicographically minimal
7184 * (or maximal) image element for each domain element in dom,
7185 * in the form of an isl_pw_multi_aff.
7186 * If "empty" is not NULL, then set *empty to those elements in dom that
7187 * do not have an image element.
7188 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7189 * should be computed over the domain of "map". "empty" is also NULL
7190 * in this case.
7191 *
7192 * We first compute the lexicographically minimal or maximal element
7193 * in the first basic map. This results in a partial solution "res"
7194 * and a subset "todo" of dom that still need to be handled.
7195 * We then consider each of the remaining maps in "map" and successively
7196 * update both "res" and "todo".
7197 * If "empty" is NULL, then the todo sets are not needed and therefore
7198 * also not computed.
7199 */
isl_map_partial_lexopt_aligned_pw_multi_aff(__isl_take isl_map * map,__isl_take isl_set * dom,__isl_give isl_set ** empty,unsigned flags)7200 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
7201 __isl_take isl_map *map, __isl_take isl_set *dom,
7202 __isl_give isl_set **empty, unsigned flags)
7203 {
7204 int i;
7205 int full;
7206 isl_pw_multi_aff *res;
7207 isl_set *todo;
7208
7209 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7210 if (!map || (!full && !dom))
7211 goto error;
7212
7213 if (isl_map_plain_is_empty(map)) {
7214 if (empty)
7215 *empty = dom;
7216 else
7217 isl_set_free(dom);
7218 return isl_pw_multi_aff_from_map(map);
7219 }
7220
7221 res = basic_map_partial_lexopt_pw_multi_aff(
7222 isl_basic_map_copy(map->p[0]),
7223 isl_set_copy(dom), empty, flags);
7224
7225 if (empty)
7226 todo = *empty;
7227 for (i = 1; i < map->n; ++i) {
7228 isl_pw_multi_aff *res_i;
7229
7230 res_i = basic_map_partial_lexopt_pw_multi_aff(
7231 isl_basic_map_copy(map->p[i]),
7232 isl_set_copy(dom), empty, flags);
7233
7234 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
7235 res = isl_pw_multi_aff_union_lexmax(res, res_i);
7236 else
7237 res = isl_pw_multi_aff_union_lexmin(res, res_i);
7238
7239 if (empty)
7240 todo = isl_set_intersect(todo, *empty);
7241 }
7242
7243 isl_set_free(dom);
7244 isl_map_free(map);
7245
7246 if (empty)
7247 *empty = todo;
7248
7249 return res;
7250 error:
7251 if (empty)
7252 *empty = NULL;
7253 isl_set_free(dom);
7254 isl_map_free(map);
7255 return NULL;
7256 }
7257
7258 #undef TYPE
7259 #define TYPE isl_map
7260 #undef SUFFIX
7261 #define SUFFIX
7262 #undef EMPTY
7263 #define EMPTY isl_map_empty
7264 #undef ADD
7265 #define ADD isl_map_union_disjoint
7266 #include "isl_map_lexopt_templ.c"
7267
7268 /* Given a map "map", compute the lexicographically minimal
7269 * (or maximal) image element for each domain element in "dom",
7270 * in the form of an isl_map.
7271 * If "empty" is not NULL, then set *empty to those elements in "dom" that
7272 * do not have an image element.
7273 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
7274 * should be computed over the domain of "map". "empty" is also NULL
7275 * in this case.
7276 *
7277 * If the input consists of more than one disjunct, then first
7278 * compute the desired result in the form of an isl_pw_multi_aff and
7279 * then convert that into an isl_map.
7280 *
7281 * This function used to have an explicit implementation in terms
7282 * of isl_maps, but it would continually intersect the domains of
7283 * partial results with the complement of the domain of the next
7284 * partial solution, potentially leading to an explosion in the number
7285 * of disjuncts if there are several disjuncts in the input.
7286 * An even earlier implementation of this function would look for
7287 * better results in the domain of the partial result and for extra
7288 * results in the complement of this domain, which would lead to
7289 * even more splintering.
7290 */
isl_map_partial_lexopt_aligned(__isl_take isl_map * map,__isl_take isl_set * dom,__isl_give isl_set ** empty,unsigned flags)7291 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
7292 __isl_take isl_map *map, __isl_take isl_set *dom,
7293 __isl_give isl_set **empty, unsigned flags)
7294 {
7295 int full;
7296 struct isl_map *res;
7297 isl_pw_multi_aff *pma;
7298
7299 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
7300 if (!map || (!full && !dom))
7301 goto error;
7302
7303 if (isl_map_plain_is_empty(map)) {
7304 if (empty)
7305 *empty = dom;
7306 else
7307 isl_set_free(dom);
7308 return map;
7309 }
7310
7311 if (map->n == 1) {
7312 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
7313 dom, empty, flags);
7314 isl_map_free(map);
7315 return res;
7316 }
7317
7318 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
7319 flags);
7320 return isl_map_from_pw_multi_aff_internal(pma);
7321 error:
7322 if (empty)
7323 *empty = NULL;
7324 isl_set_free(dom);
7325 isl_map_free(map);
7326 return NULL;
7327 }
7328
isl_map_partial_lexmax(__isl_take isl_map * map,__isl_take isl_set * dom,__isl_give isl_set ** empty)7329 __isl_give isl_map *isl_map_partial_lexmax(
7330 __isl_take isl_map *map, __isl_take isl_set *dom,
7331 __isl_give isl_set **empty)
7332 {
7333 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
7334 }
7335
isl_map_partial_lexmin(__isl_take isl_map * map,__isl_take isl_set * dom,__isl_give isl_set ** empty)7336 __isl_give isl_map *isl_map_partial_lexmin(
7337 __isl_take isl_map *map, __isl_take isl_set *dom,
7338 __isl_give isl_set **empty)
7339 {
7340 return isl_map_partial_lexopt(map, dom, empty, 0);
7341 }
7342
isl_set_partial_lexmin(__isl_take isl_set * set,__isl_take isl_set * dom,__isl_give isl_set ** empty)7343 __isl_give isl_set *isl_set_partial_lexmin(
7344 __isl_take isl_set *set, __isl_take isl_set *dom,
7345 __isl_give isl_set **empty)
7346 {
7347 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
7348 dom, empty));
7349 }
7350
isl_set_partial_lexmax(__isl_take isl_set * set,__isl_take isl_set * dom,__isl_give isl_set ** empty)7351 __isl_give isl_set *isl_set_partial_lexmax(
7352 __isl_take isl_set *set, __isl_take isl_set *dom,
7353 __isl_give isl_set **empty)
7354 {
7355 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
7356 dom, empty));
7357 }
7358
7359 /* Compute the lexicographic minimum (or maximum if "flags" includes
7360 * ISL_OPT_MAX) of "bset" over its parametric domain.
7361 */
isl_basic_set_lexopt(__isl_take isl_basic_set * bset,unsigned flags)7362 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
7363 unsigned flags)
7364 {
7365 return isl_basic_map_lexopt(bset, flags);
7366 }
7367
isl_basic_map_lexmax(__isl_take isl_basic_map * bmap)7368 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
7369 {
7370 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
7371 }
7372
isl_basic_set_lexmin(__isl_take isl_basic_set * bset)7373 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
7374 {
7375 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
7376 }
7377
isl_basic_set_lexmax(__isl_take isl_basic_set * bset)7378 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
7379 {
7380 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
7381 }
7382
7383 /* Compute the lexicographic minimum of "bset" over its parametric domain
7384 * for the purpose of quantifier elimination.
7385 * That is, find an explicit representation for all the existentially
7386 * quantified variables in "bset" by computing their lexicographic
7387 * minimum.
7388 */
isl_basic_set_lexmin_compute_divs(__isl_take isl_basic_set * bset)7389 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
7390 __isl_take isl_basic_set *bset)
7391 {
7392 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
7393 }
7394
7395 /* Given a basic map with one output dimension, compute the minimum or
7396 * maximum of that dimension as an isl_pw_aff.
7397 *
7398 * Compute the optimum as a lexicographic optimum over the single
7399 * output dimension and extract the single isl_pw_aff from the result.
7400 */
basic_map_dim_opt(__isl_keep isl_basic_map * bmap,int max)7401 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
7402 int max)
7403 {
7404 isl_pw_multi_aff *pma;
7405 isl_pw_aff *pwaff;
7406
7407 bmap = isl_basic_map_copy(bmap);
7408 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
7409 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
7410 isl_pw_multi_aff_free(pma);
7411
7412 return pwaff;
7413 }
7414
7415 /* Compute the minimum or maximum of the given output dimension
7416 * as a function of the parameters and the input dimensions,
7417 * but independently of the other output dimensions.
7418 *
7419 * We first project out the other output dimension and then compute
7420 * the "lexicographic" maximum in each basic map, combining the results
7421 * using isl_pw_aff_union_max.
7422 */
map_dim_opt(__isl_take isl_map * map,int pos,int max)7423 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7424 int max)
7425 {
7426 int i;
7427 isl_pw_aff *pwaff;
7428 isl_size n_out;
7429
7430 n_out = isl_map_dim(map, isl_dim_out);
7431 if (n_out < 0)
7432 map = isl_map_free(map);
7433 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7434 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7435 if (!map)
7436 return NULL;
7437
7438 if (map->n == 0) {
7439 isl_space *space = isl_map_get_space(map);
7440 isl_map_free(map);
7441 return isl_pw_aff_empty(space);
7442 }
7443
7444 pwaff = basic_map_dim_opt(map->p[0], max);
7445 for (i = 1; i < map->n; ++i) {
7446 isl_pw_aff *pwaff_i;
7447
7448 pwaff_i = basic_map_dim_opt(map->p[i], max);
7449 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7450 }
7451
7452 isl_map_free(map);
7453
7454 return pwaff;
7455 }
7456
7457 /* Compute the minimum of the given output dimension as a function of the
7458 * parameters and input dimensions, but independently of
7459 * the other output dimensions.
7460 */
isl_map_dim_min(__isl_take isl_map * map,int pos)7461 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7462 {
7463 return map_dim_opt(map, pos, 0);
7464 }
7465
7466 /* Compute the maximum of the given output dimension as a function of the
7467 * parameters and input dimensions, but independently of
7468 * the other output dimensions.
7469 */
isl_map_dim_max(__isl_take isl_map * map,int pos)7470 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7471 {
7472 return map_dim_opt(map, pos, 1);
7473 }
7474
7475 /* Compute the minimum or maximum of the given set dimension
7476 * as a function of the parameters,
7477 * but independently of the other set dimensions.
7478 */
set_dim_opt(__isl_take isl_set * set,int pos,int max)7479 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7480 int max)
7481 {
7482 return map_dim_opt(set, pos, max);
7483 }
7484
7485 /* Compute the maximum of the given set dimension as a function of the
7486 * parameters, but independently of the other set dimensions.
7487 */
isl_set_dim_max(__isl_take isl_set * set,int pos)7488 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7489 {
7490 return set_dim_opt(set, pos, 1);
7491 }
7492
7493 /* Compute the minimum of the given set dimension as a function of the
7494 * parameters, but independently of the other set dimensions.
7495 */
isl_set_dim_min(__isl_take isl_set * set,int pos)7496 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7497 {
7498 return set_dim_opt(set, pos, 0);
7499 }
7500
7501 /* Apply a preimage specified by "mat" on the parameters of "bset".
7502 * bset is assumed to have only parameters and divs.
7503 */
basic_set_parameter_preimage(__isl_take isl_basic_set * bset,__isl_take isl_mat * mat)7504 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7505 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7506 {
7507 isl_size nparam;
7508
7509 nparam = isl_basic_set_dim(bset, isl_dim_param);
7510 if (nparam < 0 || !mat)
7511 goto error;
7512
7513 bset->dim = isl_space_cow(bset->dim);
7514 if (!bset->dim)
7515 goto error;
7516
7517 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7518
7519 bset->dim->nparam = 0;
7520 bset->dim->n_out = nparam;
7521 bset = isl_basic_set_preimage(bset, mat);
7522 if (bset) {
7523 bset->dim->nparam = bset->dim->n_out;
7524 bset->dim->n_out = 0;
7525 }
7526 return bset;
7527 error:
7528 isl_mat_free(mat);
7529 isl_basic_set_free(bset);
7530 return NULL;
7531 }
7532
7533 /* Apply a preimage specified by "mat" on the parameters of "set".
7534 * set is assumed to have only parameters and divs.
7535 */
set_parameter_preimage(__isl_take isl_set * set,__isl_take isl_mat * mat)7536 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7537 __isl_take isl_mat *mat)
7538 {
7539 isl_space *space;
7540 isl_size nparam;
7541
7542 nparam = isl_set_dim(set, isl_dim_param);
7543 if (nparam < 0 || !mat)
7544 goto error;
7545
7546 if (mat->n_row != 1 + nparam)
7547 isl_die(isl_set_get_ctx(set), isl_error_internal,
7548 "unexpected number of rows", goto error);
7549
7550 space = isl_set_get_space(set);
7551 space = isl_space_move_dims(space, isl_dim_set, 0,
7552 isl_dim_param, 0, nparam);
7553 set = isl_set_reset_space(set, space);
7554 set = isl_set_preimage(set, mat);
7555 nparam = isl_set_dim(set, isl_dim_out);
7556 if (nparam < 0)
7557 set = isl_set_free(set);
7558 space = isl_set_get_space(set);
7559 space = isl_space_move_dims(space, isl_dim_param, 0,
7560 isl_dim_out, 0, nparam);
7561 set = isl_set_reset_space(set, space);
7562 return set;
7563 error:
7564 isl_mat_free(mat);
7565 isl_set_free(set);
7566 return NULL;
7567 }
7568
7569 /* Intersect the basic set "bset" with the affine space specified by the
7570 * equalities in "eq".
7571 */
basic_set_append_equalities(__isl_take isl_basic_set * bset,__isl_take isl_mat * eq)7572 static __isl_give isl_basic_set *basic_set_append_equalities(
7573 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7574 {
7575 int i, k;
7576 unsigned len;
7577
7578 if (!bset || !eq)
7579 goto error;
7580
7581 bset = isl_basic_set_extend(bset, 0, eq->n_row, 0);
7582 if (!bset)
7583 goto error;
7584
7585 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7586 for (i = 0; i < eq->n_row; ++i) {
7587 k = isl_basic_set_alloc_equality(bset);
7588 if (k < 0)
7589 goto error;
7590 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7591 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7592 }
7593 isl_mat_free(eq);
7594
7595 bset = isl_basic_set_gauss(bset, NULL);
7596 bset = isl_basic_set_finalize(bset);
7597
7598 return bset;
7599 error:
7600 isl_mat_free(eq);
7601 isl_basic_set_free(bset);
7602 return NULL;
7603 }
7604
7605 /* Intersect the set "set" with the affine space specified by the
7606 * equalities in "eq".
7607 */
set_append_equalities(__isl_take isl_set * set,__isl_take isl_mat * eq)7608 static __isl_give isl_set *set_append_equalities(__isl_take isl_set *set,
7609 __isl_take isl_mat *eq)
7610 {
7611 int i;
7612
7613 if (!set || !eq)
7614 goto error;
7615
7616 for (i = 0; i < set->n; ++i) {
7617 set->p[i] = basic_set_append_equalities(set->p[i],
7618 isl_mat_copy(eq));
7619 if (!set->p[i])
7620 goto error;
7621 }
7622 isl_mat_free(eq);
7623 return set;
7624 error:
7625 isl_mat_free(eq);
7626 isl_set_free(set);
7627 return NULL;
7628 }
7629
7630 /* Given a basic set "bset" that only involves parameters and existentially
7631 * quantified variables, return the index of the first equality
7632 * that only involves parameters. If there is no such equality then
7633 * return bset->n_eq.
7634 *
7635 * This function assumes that isl_basic_set_gauss has been called on "bset".
7636 */
first_parameter_equality(__isl_keep isl_basic_set * bset)7637 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7638 {
7639 int i, j;
7640 isl_size nparam, n_div;
7641
7642 nparam = isl_basic_set_dim(bset, isl_dim_param);
7643 n_div = isl_basic_set_dim(bset, isl_dim_div);
7644 if (nparam < 0 || n_div < 0)
7645 return -1;
7646
7647 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7648 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7649 ++i;
7650 }
7651
7652 return i;
7653 }
7654
7655 /* Compute an explicit representation for the existentially quantified
7656 * variables in "bset" by computing the "minimal value" of the set
7657 * variables. Since there are no set variables, the computation of
7658 * the minimal value essentially computes an explicit representation
7659 * of the non-empty part(s) of "bset".
7660 *
7661 * The input only involves parameters and existentially quantified variables.
7662 * All equalities among parameters have been removed.
7663 *
7664 * Since the existentially quantified variables in the result are in general
7665 * going to be different from those in the input, we first replace
7666 * them by the minimal number of variables based on their equalities.
7667 * This should simplify the parametric integer programming.
7668 */
base_compute_divs(__isl_take isl_basic_set * bset)7669 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7670 {
7671 isl_morph *morph1, *morph2;
7672 isl_set *set;
7673 isl_size n;
7674
7675 if (!bset)
7676 return NULL;
7677 if (bset->n_eq == 0)
7678 return isl_basic_set_lexmin_compute_divs(bset);
7679
7680 morph1 = isl_basic_set_parameter_compression(bset);
7681 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7682 bset = isl_basic_set_lift(bset);
7683 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7684 bset = isl_morph_basic_set(morph2, bset);
7685 n = isl_basic_set_dim(bset, isl_dim_set);
7686 if (n < 0)
7687 bset = isl_basic_set_free(bset);
7688 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7689
7690 set = isl_basic_set_lexmin_compute_divs(bset);
7691
7692 set = isl_morph_set(isl_morph_inverse(morph1), set);
7693
7694 return set;
7695 }
7696
7697 /* Project the given basic set onto its parameter domain, possibly introducing
7698 * new, explicit, existential variables in the constraints.
7699 * The input has parameters and (possibly implicit) existential variables.
7700 * The output has the same parameters, but only
7701 * explicit existentially quantified variables.
7702 *
7703 * The actual projection is performed by pip, but pip doesn't seem
7704 * to like equalities very much, so we first remove the equalities
7705 * among the parameters by performing a variable compression on
7706 * the parameters. Afterward, an inverse transformation is performed
7707 * and the equalities among the parameters are inserted back in.
7708 *
7709 * The variable compression on the parameters may uncover additional
7710 * equalities that were only implicit before. We therefore check
7711 * if there are any new parameter equalities in the result and
7712 * if so recurse. The removal of parameter equalities is required
7713 * for the parameter compression performed by base_compute_divs.
7714 */
parameter_compute_divs(__isl_take isl_basic_set * bset)7715 static __isl_give isl_set *parameter_compute_divs(
7716 __isl_take isl_basic_set *bset)
7717 {
7718 int i;
7719 struct isl_mat *eq;
7720 struct isl_mat *T, *T2;
7721 struct isl_set *set;
7722 isl_size nparam;
7723
7724 bset = isl_basic_set_cow(bset);
7725 if (!bset)
7726 return NULL;
7727
7728 if (bset->n_eq == 0)
7729 return base_compute_divs(bset);
7730
7731 bset = isl_basic_set_gauss(bset, NULL);
7732 if (!bset)
7733 return NULL;
7734 if (isl_basic_set_plain_is_empty(bset))
7735 return isl_set_from_basic_set(bset);
7736
7737 i = first_parameter_equality(bset);
7738 if (i == bset->n_eq)
7739 return base_compute_divs(bset);
7740
7741 nparam = isl_basic_set_dim(bset, isl_dim_param);
7742 if (nparam < 0)
7743 return isl_set_from_basic_set(isl_basic_set_free(bset));
7744 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7745 0, 1 + nparam);
7746 eq = isl_mat_cow(eq);
7747 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7748 if (T && T->n_col == 0) {
7749 isl_mat_free(T);
7750 isl_mat_free(T2);
7751 isl_mat_free(eq);
7752 bset = isl_basic_set_set_to_empty(bset);
7753 return isl_set_from_basic_set(bset);
7754 }
7755 bset = basic_set_parameter_preimage(bset, T);
7756
7757 i = first_parameter_equality(bset);
7758 if (!bset)
7759 set = NULL;
7760 else if (i == bset->n_eq)
7761 set = base_compute_divs(bset);
7762 else
7763 set = parameter_compute_divs(bset);
7764 set = set_parameter_preimage(set, T2);
7765 set = set_append_equalities(set, eq);
7766 return set;
7767 }
7768
7769 /* Insert the divs from "ls" before those of "bmap".
7770 *
7771 * The number of columns is not changed, which means that the last
7772 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7773 * The caller is responsible for removing the same number of dimensions
7774 * from the space of "bmap".
7775 */
insert_divs_from_local_space(__isl_take isl_basic_map * bmap,__isl_keep isl_local_space * ls)7776 static __isl_give isl_basic_map *insert_divs_from_local_space(
7777 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7778 {
7779 int i;
7780 isl_size n_div;
7781 int old_n_div;
7782
7783 n_div = isl_local_space_dim(ls, isl_dim_div);
7784 if (n_div < 0)
7785 return isl_basic_map_free(bmap);
7786 if (n_div == 0)
7787 return bmap;
7788
7789 old_n_div = bmap->n_div;
7790 bmap = insert_div_rows(bmap, n_div);
7791 if (!bmap)
7792 return NULL;
7793
7794 for (i = 0; i < n_div; ++i) {
7795 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7796 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7797 }
7798
7799 return bmap;
7800 }
7801
7802 /* Replace the space of "bmap" by the space and divs of "ls".
7803 *
7804 * If "ls" has any divs, then we simplify the result since we may
7805 * have discovered some additional equalities that could simplify
7806 * the div expressions.
7807 */
basic_replace_space_by_local_space(__isl_take isl_basic_map * bmap,__isl_take isl_local_space * ls)7808 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7809 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7810 {
7811 isl_size n_div;
7812
7813 bmap = isl_basic_map_cow(bmap);
7814 n_div = isl_local_space_dim(ls, isl_dim_div);
7815 if (!bmap || n_div < 0)
7816 goto error;
7817
7818 bmap = insert_divs_from_local_space(bmap, ls);
7819 if (!bmap)
7820 goto error;
7821
7822 isl_space_free(bmap->dim);
7823 bmap->dim = isl_local_space_get_space(ls);
7824 if (!bmap->dim)
7825 goto error;
7826
7827 isl_local_space_free(ls);
7828 if (n_div > 0)
7829 bmap = isl_basic_map_simplify(bmap);
7830 bmap = isl_basic_map_finalize(bmap);
7831 return bmap;
7832 error:
7833 isl_basic_map_free(bmap);
7834 isl_local_space_free(ls);
7835 return NULL;
7836 }
7837
7838 /* Replace the space of "map" by the space and divs of "ls".
7839 */
replace_space_by_local_space(__isl_take isl_map * map,__isl_take isl_local_space * ls)7840 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7841 __isl_take isl_local_space *ls)
7842 {
7843 int i;
7844
7845 map = isl_map_cow(map);
7846 if (!map || !ls)
7847 goto error;
7848
7849 for (i = 0; i < map->n; ++i) {
7850 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7851 isl_local_space_copy(ls));
7852 if (!map->p[i])
7853 goto error;
7854 }
7855 isl_space_free(isl_map_take_space(map));
7856 map = isl_map_restore_space(map, isl_local_space_get_space(ls));
7857
7858 isl_local_space_free(ls);
7859 return map;
7860 error:
7861 isl_local_space_free(ls);
7862 isl_map_free(map);
7863 return NULL;
7864 }
7865
7866 /* Compute an explicit representation for the existentially
7867 * quantified variables for which do not know any explicit representation yet.
7868 *
7869 * We first sort the existentially quantified variables so that the
7870 * existentially quantified variables for which we already have an explicit
7871 * representation are placed before those for which we do not.
7872 * The input dimensions, the output dimensions and the existentially
7873 * quantified variables for which we already have an explicit
7874 * representation are then turned into parameters.
7875 * compute_divs returns a map with the same parameters and
7876 * no input or output dimensions and the dimension specification
7877 * is reset to that of the input, including the existentially quantified
7878 * variables for which we already had an explicit representation.
7879 */
compute_divs(__isl_take isl_basic_map * bmap)7880 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7881 {
7882 struct isl_basic_set *bset;
7883 struct isl_set *set;
7884 struct isl_map *map;
7885 isl_space *space;
7886 isl_local_space *ls;
7887 isl_size nparam;
7888 isl_size n_in;
7889 isl_size n_out;
7890 int n_known;
7891 int i;
7892
7893 bmap = isl_basic_map_sort_divs(bmap);
7894 bmap = isl_basic_map_cow(bmap);
7895 if (!bmap)
7896 return NULL;
7897
7898 n_known = isl_basic_map_first_unknown_div(bmap);
7899 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7900 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7901 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7902 if (n_known < 0 || nparam < 0 || n_in < 0 || n_out < 0)
7903 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7904
7905 space = isl_space_set_alloc(bmap->ctx,
7906 nparam + n_in + n_out + n_known, 0);
7907 if (!space)
7908 goto error;
7909
7910 ls = isl_basic_map_get_local_space(bmap);
7911 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7912 n_known, bmap->n_div - n_known);
7913 if (n_known > 0) {
7914 for (i = n_known; i < bmap->n_div; ++i)
7915 swap_div(bmap, i - n_known, i);
7916 bmap->n_div -= n_known;
7917 bmap->extra -= n_known;
7918 }
7919 bmap = isl_basic_map_reset_space(bmap, space);
7920 bset = bset_from_bmap(bmap);
7921
7922 set = parameter_compute_divs(bset);
7923 map = set_to_map(set);
7924 map = replace_space_by_local_space(map, ls);
7925
7926 return map;
7927 error:
7928 isl_basic_map_free(bmap);
7929 return NULL;
7930 }
7931
7932 /* Remove the explicit representation of local variable "div",
7933 * if there is any.
7934 */
isl_basic_map_mark_div_unknown(__isl_take isl_basic_map * bmap,int div)7935 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7936 __isl_take isl_basic_map *bmap, int div)
7937 {
7938 isl_bool unknown;
7939
7940 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7941 if (unknown < 0)
7942 return isl_basic_map_free(bmap);
7943 if (unknown)
7944 return bmap;
7945
7946 bmap = isl_basic_map_cow(bmap);
7947 if (!bmap)
7948 return NULL;
7949 isl_int_set_si(bmap->div[div][0], 0);
7950 return bmap;
7951 }
7952
7953 /* Is local variable "div" of "bmap" marked as not having an explicit
7954 * representation?
7955 * Note that even if "div" is not marked in this way and therefore
7956 * has an explicit representation, this representation may still
7957 * depend (indirectly) on other local variables that do not
7958 * have an explicit representation.
7959 */
isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map * bmap,int div)7960 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7961 int div)
7962 {
7963 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7964 return isl_bool_error;
7965 return isl_int_is_zero(bmap->div[div][0]);
7966 }
7967
7968 /* Return the position of the first local variable that does not
7969 * have an explicit representation.
7970 * Return the total number of local variables if they all have
7971 * an explicit representation.
7972 * Return -1 on error.
7973 */
isl_basic_map_first_unknown_div(__isl_keep isl_basic_map * bmap)7974 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7975 {
7976 int i;
7977
7978 if (!bmap)
7979 return -1;
7980
7981 for (i = 0; i < bmap->n_div; ++i) {
7982 if (!isl_basic_map_div_is_known(bmap, i))
7983 return i;
7984 }
7985 return bmap->n_div;
7986 }
7987
7988 /* Return the position of the first local variable that does not
7989 * have an explicit representation.
7990 * Return the total number of local variables if they all have
7991 * an explicit representation.
7992 * Return -1 on error.
7993 */
isl_basic_set_first_unknown_div(__isl_keep isl_basic_set * bset)7994 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7995 {
7996 return isl_basic_map_first_unknown_div(bset);
7997 }
7998
7999 /* Does "bmap" have an explicit representation for all local variables?
8000 */
isl_basic_map_divs_known(__isl_keep isl_basic_map * bmap)8001 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
8002 {
8003 int first;
8004 isl_size n;
8005
8006 n = isl_basic_map_dim(bmap, isl_dim_div);
8007 first = isl_basic_map_first_unknown_div(bmap);
8008 if (n < 0 || first < 0)
8009 return isl_bool_error;
8010 return first == n;
8011 }
8012
8013 /* Do all basic maps in "map" have an explicit representation
8014 * for all local variables?
8015 */
isl_map_divs_known(__isl_keep isl_map * map)8016 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
8017 {
8018 int i;
8019
8020 if (!map)
8021 return isl_bool_error;
8022
8023 for (i = 0; i < map->n; ++i) {
8024 int known = isl_basic_map_divs_known(map->p[i]);
8025 if (known <= 0)
8026 return known;
8027 }
8028
8029 return isl_bool_true;
8030 }
8031
8032 /* If bmap contains any unknown divs, then compute explicit
8033 * expressions for them. However, this computation may be
8034 * quite expensive, so first try to remove divs that aren't
8035 * strictly needed.
8036 */
isl_basic_map_compute_divs(__isl_take isl_basic_map * bmap)8037 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
8038 {
8039 int known;
8040 struct isl_map *map;
8041
8042 known = isl_basic_map_divs_known(bmap);
8043 if (known < 0)
8044 goto error;
8045 if (known)
8046 return isl_map_from_basic_map(bmap);
8047
8048 bmap = isl_basic_map_drop_redundant_divs(bmap);
8049
8050 known = isl_basic_map_divs_known(bmap);
8051 if (known < 0)
8052 goto error;
8053 if (known)
8054 return isl_map_from_basic_map(bmap);
8055
8056 map = compute_divs(bmap);
8057 return map;
8058 error:
8059 isl_basic_map_free(bmap);
8060 return NULL;
8061 }
8062
isl_map_compute_divs(__isl_take isl_map * map)8063 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
8064 {
8065 int i;
8066 int known;
8067 struct isl_map *res;
8068
8069 if (!map)
8070 return NULL;
8071 if (map->n == 0)
8072 return map;
8073
8074 known = isl_map_divs_known(map);
8075 if (known < 0) {
8076 isl_map_free(map);
8077 return NULL;
8078 }
8079 if (known)
8080 return map;
8081
8082 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
8083 for (i = 1 ; i < map->n; ++i) {
8084 struct isl_map *r2;
8085 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
8086 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
8087 res = isl_map_union_disjoint(res, r2);
8088 else
8089 res = isl_map_union(res, r2);
8090 }
8091 isl_map_free(map);
8092
8093 return res;
8094 }
8095
isl_basic_set_compute_divs(__isl_take isl_basic_set * bset)8096 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
8097 {
8098 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
8099 }
8100
isl_set_compute_divs(__isl_take isl_set * set)8101 __isl_give isl_set *isl_set_compute_divs(__isl_take isl_set *set)
8102 {
8103 return set_from_map(isl_map_compute_divs(set_to_map(set)));
8104 }
8105
isl_map_domain(__isl_take isl_map * map)8106 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
8107 {
8108 isl_space *space;
8109 isl_size n_out;
8110
8111 n_out = isl_map_dim(map, isl_dim_out);
8112 if (n_out < 0)
8113 return set_from_map(isl_map_free(map));
8114 space = isl_space_domain(isl_map_get_space(map));
8115
8116 map = isl_map_project_out(map, isl_dim_out, 0, n_out);
8117
8118 return set_from_map(isl_map_reset_space(map, space));
8119 }
8120
8121 /* Return the union of "map1" and "map2", where we assume for now that
8122 * "map1" and "map2" are disjoint. Note that the basic maps inside
8123 * "map1" or "map2" may not be disjoint from each other.
8124 * Also note that this function is also called from isl_map_union,
8125 * which takes care of handling the situation where "map1" and "map2"
8126 * may not be disjoint.
8127 *
8128 * If one of the inputs is empty, we can simply return the other input.
8129 * Similarly, if one of the inputs is universal, then it is equal to the union.
8130 */
map_union_disjoint(__isl_take isl_map * map1,__isl_take isl_map * map2)8131 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
8132 __isl_take isl_map *map2)
8133 {
8134 int i;
8135 unsigned flags = 0;
8136 struct isl_map *map = NULL;
8137 int is_universe;
8138
8139 if (isl_map_check_equal_space(map1, map2) < 0)
8140 goto error;
8141
8142 if (map1->n == 0) {
8143 isl_map_free(map1);
8144 return map2;
8145 }
8146 if (map2->n == 0) {
8147 isl_map_free(map2);
8148 return map1;
8149 }
8150
8151 is_universe = isl_map_plain_is_universe(map1);
8152 if (is_universe < 0)
8153 goto error;
8154 if (is_universe) {
8155 isl_map_free(map2);
8156 return map1;
8157 }
8158
8159 is_universe = isl_map_plain_is_universe(map2);
8160 if (is_universe < 0)
8161 goto error;
8162 if (is_universe) {
8163 isl_map_free(map1);
8164 return map2;
8165 }
8166
8167 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8168 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8169 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8170
8171 map = isl_map_alloc_space(isl_space_copy(map1->dim),
8172 map1->n + map2->n, flags);
8173 if (!map)
8174 goto error;
8175 for (i = 0; i < map1->n; ++i) {
8176 map = isl_map_add_basic_map(map,
8177 isl_basic_map_copy(map1->p[i]));
8178 if (!map)
8179 goto error;
8180 }
8181 for (i = 0; i < map2->n; ++i) {
8182 map = isl_map_add_basic_map(map,
8183 isl_basic_map_copy(map2->p[i]));
8184 if (!map)
8185 goto error;
8186 }
8187 isl_map_free(map1);
8188 isl_map_free(map2);
8189 return map;
8190 error:
8191 isl_map_free(map);
8192 isl_map_free(map1);
8193 isl_map_free(map2);
8194 return NULL;
8195 }
8196
8197 /* Return the union of "map1" and "map2", where "map1" and "map2" are
8198 * guaranteed to be disjoint by the caller.
8199 *
8200 * Note that this functions is called from within isl_map_make_disjoint,
8201 * so we have to be careful not to touch the constraints of the inputs
8202 * in any way.
8203 */
isl_map_union_disjoint(__isl_take isl_map * map1,__isl_take isl_map * map2)8204 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
8205 __isl_take isl_map *map2)
8206 {
8207 isl_map_align_params_bin(&map1, &map2);
8208 return map_union_disjoint(map1, map2);
8209 }
8210
8211 /* Return the union of "map1" and "map2", where "map1" and "map2" may
8212 * not be disjoint.
8213 *
8214 * We currently simply call map_union_disjoint, the internal operation
8215 * of which does not really depend on the inputs being disjoint.
8216 * If the result contains more than one basic map, then we clear
8217 * the disjoint flag since the result may contain basic maps from
8218 * both inputs and these are not guaranteed to be disjoint.
8219 *
8220 * As a special case, if "map1" and "map2" are obviously equal,
8221 * then we simply return "map1".
8222 */
isl_map_union(__isl_take isl_map * map1,__isl_take isl_map * map2)8223 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
8224 __isl_take isl_map *map2)
8225 {
8226 int equal;
8227
8228 if (isl_map_align_params_bin(&map1, &map2) < 0)
8229 goto error;
8230
8231 equal = isl_map_plain_is_equal(map1, map2);
8232 if (equal < 0)
8233 goto error;
8234 if (equal) {
8235 isl_map_free(map2);
8236 return map1;
8237 }
8238
8239 map1 = map_union_disjoint(map1, map2);
8240 if (!map1)
8241 return NULL;
8242 if (map1->n > 1)
8243 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
8244 return map1;
8245 error:
8246 isl_map_free(map1);
8247 isl_map_free(map2);
8248 return NULL;
8249 }
8250
isl_set_union_disjoint(__isl_take isl_set * set1,__isl_take isl_set * set2)8251 __isl_give isl_set *isl_set_union_disjoint(
8252 __isl_take isl_set *set1, __isl_take isl_set *set2)
8253 {
8254 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
8255 set_to_map(set2)));
8256 }
8257
isl_set_union(__isl_take isl_set * set1,__isl_take isl_set * set2)8258 __isl_give isl_set *isl_set_union(__isl_take isl_set *set1,
8259 __isl_take isl_set *set2)
8260 {
8261 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
8262 }
8263
8264 /* Apply "fn" to pairs of elements from "map" and "set" and collect
8265 * the results in a map living in "space".
8266 *
8267 * "map" and "set" are assumed to be compatible and non-NULL.
8268 */
map_intersect_set(__isl_take isl_map * map,__isl_take isl_space * space,__isl_take isl_set * set,__isl_give isl_basic_map * fn (__isl_take isl_basic_map * bmap,__isl_take isl_basic_set * bset))8269 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
8270 __isl_take isl_space *space, __isl_take isl_set *set,
8271 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
8272 __isl_take isl_basic_set *bset))
8273 {
8274 unsigned flags = 0;
8275 struct isl_map *result;
8276 int i, j;
8277
8278 if (isl_set_plain_is_universe(set)) {
8279 isl_set_free(set);
8280 return isl_map_reset_equal_dim_space(map, space);
8281 }
8282
8283 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
8284 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
8285 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8286
8287 result = isl_map_alloc_space(space, map->n * set->n, flags);
8288 for (i = 0; result && i < map->n; ++i)
8289 for (j = 0; j < set->n; ++j) {
8290 result = isl_map_add_basic_map(result,
8291 fn(isl_basic_map_copy(map->p[i]),
8292 isl_basic_set_copy(set->p[j])));
8293 if (!result)
8294 break;
8295 }
8296
8297 isl_map_free(map);
8298 isl_set_free(set);
8299 return result;
8300 }
8301
isl_map_intersect_range(__isl_take isl_map * map,__isl_take isl_set * set)8302 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
8303 __isl_take isl_set *set)
8304 {
8305 isl_bool ok;
8306 isl_space *space;
8307
8308 isl_map_align_params_set(&map, &set);
8309 ok = isl_map_compatible_range(map, set);
8310 if (ok < 0)
8311 goto error;
8312 if (!ok)
8313 isl_die(set->ctx, isl_error_invalid,
8314 "incompatible spaces", goto error);
8315
8316 space = isl_map_get_space(map);
8317 return map_intersect_set(map, space, set,
8318 &isl_basic_map_intersect_range);
8319 error:
8320 isl_map_free(map);
8321 isl_set_free(set);
8322 return NULL;
8323 }
8324
8325 /* Intersect the domain of "map" with "set".
8326 *
8327 * If the domain dimensions of "map" do not have any identifiers,
8328 * then copy them over from "set".
8329 */
isl_map_intersect_domain(__isl_take isl_map * map,__isl_take isl_set * set)8330 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
8331 __isl_take isl_set *set)
8332 {
8333 isl_bool ok;
8334 isl_space *space;
8335
8336 isl_map_align_params_set(&map, &set);
8337 ok = isl_map_compatible_domain(map, set);
8338 if (ok < 0)
8339 goto error;
8340 if (!ok)
8341 isl_die(set->ctx, isl_error_invalid,
8342 "incompatible spaces", goto error);
8343
8344 space = isl_map_get_space(map);
8345 space = isl_space_copy_ids_if_unset(space, isl_dim_in,
8346 isl_set_peek_space(set), isl_dim_set);
8347 return map_intersect_set(map, space, set,
8348 &isl_basic_map_intersect_domain);
8349 error:
8350 isl_map_free(map);
8351 isl_set_free(set);
8352 return NULL;
8353 }
8354
8355 #undef TYPE
8356 #define TYPE isl_map
8357 static
8358 #include "isl_copy_tuple_id_templ.c"
8359
8360 /* Data structure that specifies how isl_map_intersect_factor
8361 * should operate.
8362 *
8363 * "preserve_type" is the tuple where the factor differs from
8364 * the input map and of which the identifiers needs
8365 * to be preserved explicitly.
8366 * "other_factor" is used to extract the space of the other factor
8367 * from the space of the product ("map").
8368 * "product" is used to combine the given factor and a universe map
8369 * in the space returned by "other_factor" to produce a map
8370 * that lives in the same space as the input map.
8371 */
8372 struct isl_intersect_factor_control {
8373 enum isl_dim_type preserve_type;
8374 __isl_give isl_space *(*other_factor)(__isl_take isl_space *space);
8375 __isl_give isl_map *(*product)(__isl_take isl_map *factor,
8376 __isl_take isl_map *other);
8377 };
8378
8379 /* Given a map "map" in some product space and a map "factor"
8380 * living in some factor space, return the intersection.
8381 *
8382 * After aligning the parameters,
8383 * the map "factor" is first extended to a map living in the same space
8384 * as "map" and then a regular intersection is computed.
8385 *
8386 * Note that the extension is computed as a product, which is anonymous
8387 * by default. If "map" has an identifier on the corresponding tuple,
8388 * then this identifier needs to be set on the product
8389 * before the intersection is computed.
8390 */
isl_map_intersect_factor(__isl_take isl_map * map,__isl_take isl_map * factor,struct isl_intersect_factor_control * control)8391 static __isl_give isl_map *isl_map_intersect_factor(
8392 __isl_take isl_map *map, __isl_take isl_map *factor,
8393 struct isl_intersect_factor_control *control)
8394 {
8395 isl_bool equal;
8396 isl_space *space;
8397 isl_map *other, *product;
8398
8399 equal = isl_map_has_equal_params(map, factor);
8400 if (equal < 0)
8401 goto error;
8402 if (!equal) {
8403 map = isl_map_align_params(map, isl_map_get_space(factor));
8404 factor = isl_map_align_params(factor, isl_map_get_space(map));
8405 }
8406
8407 space = isl_map_get_space(map);
8408 other = isl_map_universe(control->other_factor(space));
8409 product = control->product(factor, other);
8410
8411 space = isl_map_peek_space(map);
8412 product = isl_map_copy_tuple_id(product, control->preserve_type,
8413 space, control->preserve_type);
8414 return map_intersect(map, product);
8415 error:
8416 isl_map_free(map);
8417 isl_map_free(factor);
8418 return NULL;
8419 }
8420
8421 /* Return the domain product of "map2" and "map1".
8422 */
isl_map_reverse_domain_product(__isl_take isl_map * map1,__isl_take isl_map * map2)8423 static __isl_give isl_map *isl_map_reverse_domain_product(
8424 __isl_take isl_map *map1, __isl_take isl_map *map2)
8425 {
8426 return isl_map_domain_product(map2, map1);
8427 }
8428
8429 /* Return the range product of "map2" and "map1".
8430 */
isl_map_reverse_range_product(__isl_take isl_map * map1,__isl_take isl_map * map2)8431 static __isl_give isl_map *isl_map_reverse_range_product(
8432 __isl_take isl_map *map1, __isl_take isl_map *map2)
8433 {
8434 return isl_map_range_product(map2, map1);
8435 }
8436
8437 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8438 * in the space A -> C, return the intersection.
8439 */
isl_map_intersect_domain_factor_domain(__isl_take isl_map * map,__isl_take isl_map * factor)8440 __isl_give isl_map *isl_map_intersect_domain_factor_domain(
8441 __isl_take isl_map *map, __isl_take isl_map *factor)
8442 {
8443 struct isl_intersect_factor_control control = {
8444 .preserve_type = isl_dim_in,
8445 .other_factor = isl_space_domain_factor_range,
8446 .product = isl_map_domain_product,
8447 };
8448
8449 return isl_map_intersect_factor(map, factor, &control);
8450 }
8451
8452 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
8453 * in the space B -> C, return the intersection.
8454 */
isl_map_intersect_domain_factor_range(__isl_take isl_map * map,__isl_take isl_map * factor)8455 __isl_give isl_map *isl_map_intersect_domain_factor_range(
8456 __isl_take isl_map *map, __isl_take isl_map *factor)
8457 {
8458 struct isl_intersect_factor_control control = {
8459 .preserve_type = isl_dim_in,
8460 .other_factor = isl_space_domain_factor_domain,
8461 .product = isl_map_reverse_domain_product,
8462 };
8463
8464 return isl_map_intersect_factor(map, factor, &control);
8465 }
8466
8467 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8468 * in the space A -> B, return the intersection.
8469 */
isl_map_intersect_range_factor_domain(__isl_take isl_map * map,__isl_take isl_map * factor)8470 __isl_give isl_map *isl_map_intersect_range_factor_domain(
8471 __isl_take isl_map *map, __isl_take isl_map *factor)
8472 {
8473 struct isl_intersect_factor_control control = {
8474 .preserve_type = isl_dim_out,
8475 .other_factor = isl_space_range_factor_range,
8476 .product = isl_map_range_product,
8477 };
8478
8479 return isl_map_intersect_factor(map, factor, &control);
8480 }
8481
8482 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8483 * in the space A -> C, return the intersection.
8484 */
isl_map_intersect_range_factor_range(__isl_take isl_map * map,__isl_take isl_map * factor)8485 __isl_give isl_map *isl_map_intersect_range_factor_range(
8486 __isl_take isl_map *map, __isl_take isl_map *factor)
8487 {
8488 struct isl_intersect_factor_control control = {
8489 .preserve_type = isl_dim_out,
8490 .other_factor = isl_space_range_factor_domain,
8491 .product = isl_map_reverse_range_product,
8492 };
8493
8494 return isl_map_intersect_factor(map, factor, &control);
8495 }
8496
8497 /* Given a set "set" in a space [A -> B] and a set "domain"
8498 * in the space A, return the intersection.
8499 *
8500 * The set "domain" is first extended to a set living in the space
8501 * [A -> B] and then a regular intersection is computed.
8502 */
isl_set_intersect_factor_domain(__isl_take isl_set * set,__isl_take isl_set * domain)8503 __isl_give isl_set *isl_set_intersect_factor_domain(__isl_take isl_set *set,
8504 __isl_take isl_set *domain)
8505 {
8506 struct isl_intersect_factor_control control = {
8507 .preserve_type = isl_dim_set,
8508 .other_factor = isl_space_factor_range,
8509 .product = isl_map_range_product,
8510 };
8511
8512 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8513 set_to_map(domain), &control));
8514 }
8515
8516 /* Given a set "set" in a space [A -> B] and a set "range"
8517 * in the space B, return the intersection.
8518 *
8519 * The set "range" is first extended to a set living in the space
8520 * [A -> B] and then a regular intersection is computed.
8521 */
isl_set_intersect_factor_range(__isl_take isl_set * set,__isl_take isl_set * range)8522 __isl_give isl_set *isl_set_intersect_factor_range(__isl_take isl_set *set,
8523 __isl_take isl_set *range)
8524 {
8525 struct isl_intersect_factor_control control = {
8526 .preserve_type = isl_dim_set,
8527 .other_factor = isl_space_factor_domain,
8528 .product = isl_map_reverse_range_product,
8529 };
8530
8531 return set_from_map(isl_map_intersect_factor(set_to_map(set),
8532 set_to_map(range), &control));
8533 }
8534
isl_map_apply_domain(__isl_take isl_map * map1,__isl_take isl_map * map2)8535 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8536 __isl_take isl_map *map2)
8537 {
8538 if (isl_map_align_params_bin(&map1, &map2) < 0)
8539 goto error;
8540 map1 = isl_map_reverse(map1);
8541 map1 = isl_map_apply_range(map1, map2);
8542 return isl_map_reverse(map1);
8543 error:
8544 isl_map_free(map1);
8545 isl_map_free(map2);
8546 return NULL;
8547 }
8548
isl_map_apply_range(__isl_take isl_map * map1,__isl_take isl_map * map2)8549 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8550 __isl_take isl_map *map2)
8551 {
8552 isl_space *space;
8553 struct isl_map *result;
8554 int i, j;
8555
8556 if (isl_map_align_params_bin(&map1, &map2) < 0)
8557 goto error;
8558
8559 space = isl_space_join(isl_space_copy(map1->dim),
8560 isl_space_copy(map2->dim));
8561
8562 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8563 if (!result)
8564 goto error;
8565 for (i = 0; i < map1->n; ++i)
8566 for (j = 0; j < map2->n; ++j) {
8567 result = isl_map_add_basic_map(result,
8568 isl_basic_map_apply_range(
8569 isl_basic_map_copy(map1->p[i]),
8570 isl_basic_map_copy(map2->p[j])));
8571 if (!result)
8572 goto error;
8573 }
8574 isl_map_free(map1);
8575 isl_map_free(map2);
8576 if (result && result->n <= 1)
8577 ISL_F_SET(result, ISL_MAP_DISJOINT);
8578 return result;
8579 error:
8580 isl_map_free(map1);
8581 isl_map_free(map2);
8582 return NULL;
8583 }
8584
8585 /* Is "bmap" a transformation, i.e.,
8586 * does it relate elements from the same space.
8587 */
isl_basic_map_is_transformation(__isl_keep isl_basic_map * bmap)8588 isl_bool isl_basic_map_is_transformation(__isl_keep isl_basic_map *bmap)
8589 {
8590 isl_space *space;
8591
8592 space = isl_basic_map_peek_space(bmap);
8593 return isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8594 }
8595
8596 /* Check that "bmap" is a transformation, i.e.,
8597 * that it relates elements from the same space.
8598 */
isl_basic_map_check_transformation(__isl_keep isl_basic_map * bmap)8599 static isl_stat isl_basic_map_check_transformation(
8600 __isl_keep isl_basic_map *bmap)
8601 {
8602 isl_bool equal;
8603
8604 equal = isl_basic_map_is_transformation(bmap);
8605 if (equal < 0)
8606 return isl_stat_error;
8607 if (!equal)
8608 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
8609 "domain and range don't match", return isl_stat_error);
8610 return isl_stat_ok;
8611 }
8612
8613 /*
8614 * returns range - domain
8615 */
isl_basic_map_deltas(__isl_take isl_basic_map * bmap)8616 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8617 {
8618 isl_space *target_space;
8619 struct isl_basic_set *bset;
8620 isl_size dim;
8621 isl_size nparam;
8622 isl_size total;
8623 int i;
8624
8625 if (isl_basic_map_check_transformation(bmap) < 0)
8626 return isl_basic_map_free(bmap);
8627 dim = isl_basic_map_dim(bmap, isl_dim_in);
8628 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8629 if (dim < 0 || nparam < 0)
8630 goto error;
8631 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8632 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8633 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8634 total = isl_basic_map_dim(bmap, isl_dim_all);
8635 if (total < 0)
8636 bmap = isl_basic_map_free(bmap);
8637 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8638 for (i = 0; i < dim; ++i) {
8639 int j = isl_basic_map_alloc_equality(bmap);
8640 if (j < 0) {
8641 bmap = isl_basic_map_free(bmap);
8642 break;
8643 }
8644 isl_seq_clr(bmap->eq[j], 1 + total);
8645 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8646 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8647 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8648 }
8649 bset = isl_basic_map_domain(bmap);
8650 bset = isl_basic_set_reset_space(bset, target_space);
8651 return bset;
8652 error:
8653 isl_basic_map_free(bmap);
8654 return NULL;
8655 }
8656
8657 /* Is the tuple of type "type1" of "map" the same as
8658 * the tuple of type "type2" of "space"?
8659 */
isl_map_space_tuple_is_equal(__isl_keep isl_map * map,enum isl_dim_type type1,__isl_keep isl_space * space,enum isl_dim_type type2)8660 isl_bool isl_map_space_tuple_is_equal(__isl_keep isl_map *map,
8661 enum isl_dim_type type1, __isl_keep isl_space *space,
8662 enum isl_dim_type type2)
8663 {
8664 isl_space *map_space;
8665
8666 map_space = isl_map_peek_space(map);
8667 return isl_space_tuple_is_equal(map_space, type1, space, type2);
8668 }
8669
8670 /* Is the tuple of type "type1" of "map1" the same as
8671 * the tuple of type "type2" of "map2"?
8672 */
isl_map_tuple_is_equal(__isl_keep isl_map * map1,enum isl_dim_type type1,__isl_keep isl_map * map2,enum isl_dim_type type2)8673 isl_bool isl_map_tuple_is_equal(__isl_keep isl_map *map1,
8674 enum isl_dim_type type1, __isl_keep isl_map *map2,
8675 enum isl_dim_type type2)
8676 {
8677 isl_space *space1, *space2;
8678
8679 space1 = isl_map_peek_space(map1);
8680 space2 = isl_map_peek_space(map2);
8681 return isl_space_tuple_is_equal(space1, type1, space2, type2);
8682 }
8683
8684 /* Is the space of "obj" equal to "space", ignoring parameters?
8685 */
isl_map_has_space_tuples(__isl_keep isl_map * map,__isl_keep isl_space * space)8686 isl_bool isl_map_has_space_tuples(__isl_keep isl_map *map,
8687 __isl_keep isl_space *space)
8688 {
8689 isl_space *map_space;
8690
8691 map_space = isl_map_peek_space(map);
8692 return isl_space_has_equal_tuples(map_space, space);
8693 }
8694
8695 /* Check that "map" is a transformation, i.e.,
8696 * that it relates elements from the same space.
8697 */
isl_map_check_transformation(__isl_keep isl_map * map)8698 isl_stat isl_map_check_transformation(__isl_keep isl_map *map)
8699 {
8700 isl_bool equal;
8701
8702 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
8703 if (equal < 0)
8704 return isl_stat_error;
8705 if (!equal)
8706 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8707 "domain and range don't match", return isl_stat_error);
8708 return isl_stat_ok;
8709 }
8710
8711 /*
8712 * returns range - domain
8713 */
isl_map_deltas(__isl_take isl_map * map)8714 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8715 {
8716 int i;
8717 isl_space *space;
8718 struct isl_set *result;
8719
8720 if (isl_map_check_transformation(map) < 0)
8721 goto error;
8722 space = isl_map_get_space(map);
8723 space = isl_space_domain(space);
8724 result = isl_set_alloc_space(space, map->n, 0);
8725 if (!result)
8726 goto error;
8727 for (i = 0; i < map->n; ++i)
8728 result = isl_set_add_basic_set(result,
8729 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8730 isl_map_free(map);
8731 return result;
8732 error:
8733 isl_map_free(map);
8734 return NULL;
8735 }
8736
8737 /*
8738 * returns [domain -> range] -> range - domain
8739 */
isl_basic_map_deltas_map(__isl_take isl_basic_map * bmap)8740 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8741 __isl_take isl_basic_map *bmap)
8742 {
8743 int i, k;
8744 isl_space *space;
8745 isl_basic_map *domain;
8746 isl_size nparam, n;
8747 isl_size total;
8748
8749 if (isl_basic_map_check_transformation(bmap) < 0)
8750 return isl_basic_map_free(bmap);
8751
8752 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8753 n = isl_basic_map_dim(bmap, isl_dim_in);
8754 if (nparam < 0 || n < 0)
8755 return isl_basic_map_free(bmap);
8756
8757 space = isl_basic_map_get_space(bmap);
8758 space = isl_space_from_range(isl_space_domain(space));
8759 domain = isl_basic_map_universe(space);
8760
8761 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8762 bmap = isl_basic_map_apply_range(bmap, domain);
8763 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8764
8765 total = isl_basic_map_dim(bmap, isl_dim_all);
8766 if (total < 0)
8767 return isl_basic_map_free(bmap);
8768
8769 for (i = 0; i < n; ++i) {
8770 k = isl_basic_map_alloc_equality(bmap);
8771 if (k < 0)
8772 goto error;
8773 isl_seq_clr(bmap->eq[k], 1 + total);
8774 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8775 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8776 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8777 }
8778
8779 bmap = isl_basic_map_gauss(bmap, NULL);
8780 return isl_basic_map_finalize(bmap);
8781 error:
8782 isl_basic_map_free(bmap);
8783 return NULL;
8784 }
8785
8786 /*
8787 * returns [domain -> range] -> range - domain
8788 */
isl_map_deltas_map(__isl_take isl_map * map)8789 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8790 {
8791 if (isl_map_check_transformation(map) < 0)
8792 return isl_map_free(map);
8793
8794 return isl_map_transform(map, &isl_space_range_map,
8795 &isl_basic_map_deltas_map);
8796 }
8797
8798 /* Return pairs of elements { x -> y } such that y - x is in "deltas".
8799 */
isl_set_translation(__isl_take isl_set * deltas)8800 __isl_give isl_map *isl_set_translation(__isl_take isl_set *deltas)
8801 {
8802 isl_space *space;
8803 isl_map *map;
8804
8805 space = isl_space_map_from_set(isl_set_get_space(deltas));
8806 map = isl_map_deltas_map(isl_map_universe(space));
8807 map = isl_map_intersect_range(map, deltas);
8808
8809 return isl_set_unwrap(isl_map_domain(map));
8810 }
8811
isl_basic_map_identity(__isl_take isl_space * space)8812 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8813 {
8814 isl_size n_in, n_out;
8815
8816 n_in = isl_space_dim(space, isl_dim_in);
8817 n_out = isl_space_dim(space, isl_dim_out);
8818 if (n_in < 0 || n_out < 0)
8819 goto error;
8820 if (n_in != n_out)
8821 isl_die(space->ctx, isl_error_invalid,
8822 "number of input and output dimensions needs to be "
8823 "the same", goto error);
8824 return isl_basic_map_equal(space, n_in);
8825 error:
8826 isl_space_free(space);
8827 return NULL;
8828 }
8829
isl_map_identity(__isl_take isl_space * space)8830 __isl_give isl_map *isl_map_identity(__isl_take isl_space *space)
8831 {
8832 return isl_map_from_basic_map(isl_basic_map_identity(space));
8833 }
8834
isl_set_identity(__isl_take isl_set * set)8835 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8836 {
8837 isl_space *space = isl_set_get_space(set);
8838 isl_map *id;
8839 id = isl_map_identity(isl_space_map_from_set(space));
8840 return isl_map_intersect_range(id, set);
8841 }
8842
8843 /* Construct a basic set with all set dimensions having only non-negative
8844 * values.
8845 */
isl_basic_set_positive_orthant(__isl_take isl_space * space)8846 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8847 __isl_take isl_space *space)
8848 {
8849 int i;
8850 isl_size nparam;
8851 isl_size dim;
8852 isl_size total;
8853 struct isl_basic_set *bset;
8854
8855 nparam = isl_space_dim(space, isl_dim_param);
8856 dim = isl_space_dim(space, isl_dim_set);
8857 total = isl_space_dim(space, isl_dim_all);
8858 if (nparam < 0 || dim < 0 || total < 0)
8859 space = isl_space_free(space);
8860 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8861 if (!bset)
8862 return NULL;
8863 for (i = 0; i < dim; ++i) {
8864 int k = isl_basic_set_alloc_inequality(bset);
8865 if (k < 0)
8866 goto error;
8867 isl_seq_clr(bset->ineq[k], 1 + total);
8868 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8869 }
8870 return bset;
8871 error:
8872 isl_basic_set_free(bset);
8873 return NULL;
8874 }
8875
8876 /* Construct the half-space x_pos >= 0.
8877 */
nonneg_halfspace(__isl_take isl_space * space,int pos)8878 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8879 int pos)
8880 {
8881 int k;
8882 isl_size total;
8883 isl_basic_set *nonneg;
8884
8885 total = isl_space_dim(space, isl_dim_all);
8886 if (total < 0)
8887 space = isl_space_free(space);
8888 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8889 k = isl_basic_set_alloc_inequality(nonneg);
8890 if (k < 0)
8891 goto error;
8892 isl_seq_clr(nonneg->ineq[k], 1 + total);
8893 isl_int_set_si(nonneg->ineq[k][pos], 1);
8894
8895 return isl_basic_set_finalize(nonneg);
8896 error:
8897 isl_basic_set_free(nonneg);
8898 return NULL;
8899 }
8900
8901 /* Construct the half-space x_pos <= -1.
8902 */
neg_halfspace(__isl_take isl_space * space,int pos)8903 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8904 int pos)
8905 {
8906 int k;
8907 isl_size total;
8908 isl_basic_set *neg;
8909
8910 total = isl_space_dim(space, isl_dim_all);
8911 if (total < 0)
8912 space = isl_space_free(space);
8913 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8914 k = isl_basic_set_alloc_inequality(neg);
8915 if (k < 0)
8916 goto error;
8917 isl_seq_clr(neg->ineq[k], 1 + total);
8918 isl_int_set_si(neg->ineq[k][0], -1);
8919 isl_int_set_si(neg->ineq[k][pos], -1);
8920
8921 return isl_basic_set_finalize(neg);
8922 error:
8923 isl_basic_set_free(neg);
8924 return NULL;
8925 }
8926
isl_set_split_dims(__isl_take isl_set * set,enum isl_dim_type type,unsigned first,unsigned n)8927 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8928 enum isl_dim_type type, unsigned first, unsigned n)
8929 {
8930 int i;
8931 unsigned offset;
8932 isl_basic_set *nonneg;
8933 isl_basic_set *neg;
8934
8935 if (n == 0)
8936 return set;
8937
8938 if (isl_set_check_range(set, type, first, n) < 0)
8939 return isl_set_free(set);
8940
8941 offset = pos(set->dim, type);
8942 for (i = 0; i < n; ++i) {
8943 nonneg = nonneg_halfspace(isl_set_get_space(set),
8944 offset + first + i);
8945 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8946
8947 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8948 }
8949
8950 return set;
8951 }
8952
foreach_orthant(__isl_take isl_set * set,int * signs,int first,int len,isl_stat (* fn)(__isl_take isl_set * orthant,int * signs,void * user),void * user)8953 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8954 int len,
8955 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8956 void *user)
8957 {
8958 isl_set *half;
8959
8960 if (!set)
8961 return isl_stat_error;
8962 if (isl_set_plain_is_empty(set)) {
8963 isl_set_free(set);
8964 return isl_stat_ok;
8965 }
8966 if (first == len)
8967 return fn(set, signs, user);
8968
8969 signs[first] = 1;
8970 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8971 1 + first));
8972 half = isl_set_intersect(half, isl_set_copy(set));
8973 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8974 goto error;
8975
8976 signs[first] = -1;
8977 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8978 1 + first));
8979 half = isl_set_intersect(half, set);
8980 return foreach_orthant(half, signs, first + 1, len, fn, user);
8981 error:
8982 isl_set_free(set);
8983 return isl_stat_error;
8984 }
8985
8986 /* Call "fn" on the intersections of "set" with each of the orthants
8987 * (except for obviously empty intersections). The orthant is identified
8988 * by the signs array, with each entry having value 1 or -1 according
8989 * to the sign of the corresponding variable.
8990 */
isl_set_foreach_orthant(__isl_keep isl_set * set,isl_stat (* fn)(__isl_take isl_set * orthant,int * signs,void * user),void * user)8991 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8992 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8993 void *user)
8994 {
8995 isl_size nparam;
8996 isl_size nvar;
8997 int *signs;
8998 isl_stat r;
8999
9000 if (!set)
9001 return isl_stat_error;
9002 if (isl_set_plain_is_empty(set))
9003 return isl_stat_ok;
9004
9005 nparam = isl_set_dim(set, isl_dim_param);
9006 nvar = isl_set_dim(set, isl_dim_set);
9007 if (nparam < 0 || nvar < 0)
9008 return isl_stat_error;
9009
9010 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
9011
9012 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
9013 fn, user);
9014
9015 free(signs);
9016
9017 return r;
9018 }
9019
isl_set_is_equal(__isl_keep isl_set * set1,__isl_keep isl_set * set2)9020 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9021 {
9022 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
9023 }
9024
isl_basic_map_is_subset(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)9025 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
9026 __isl_keep isl_basic_map *bmap2)
9027 {
9028 isl_bool is_subset;
9029 struct isl_map *map1;
9030 struct isl_map *map2;
9031
9032 if (!bmap1 || !bmap2)
9033 return isl_bool_error;
9034
9035 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
9036 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
9037
9038 is_subset = isl_map_is_subset(map1, map2);
9039
9040 isl_map_free(map1);
9041 isl_map_free(map2);
9042
9043 return is_subset;
9044 }
9045
isl_basic_set_is_subset(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2)9046 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
9047 __isl_keep isl_basic_set *bset2)
9048 {
9049 return isl_basic_map_is_subset(bset1, bset2);
9050 }
9051
isl_basic_map_is_equal(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)9052 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
9053 __isl_keep isl_basic_map *bmap2)
9054 {
9055 isl_bool is_subset;
9056
9057 if (!bmap1 || !bmap2)
9058 return isl_bool_error;
9059 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9060 if (is_subset != isl_bool_true)
9061 return is_subset;
9062 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9063 return is_subset;
9064 }
9065
isl_basic_set_is_equal(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2)9066 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
9067 __isl_keep isl_basic_set *bset2)
9068 {
9069 return isl_basic_map_is_equal(
9070 bset_to_bmap(bset1), bset_to_bmap(bset2));
9071 }
9072
isl_map_is_empty(__isl_keep isl_map * map)9073 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
9074 {
9075 int i;
9076 int is_empty;
9077
9078 if (!map)
9079 return isl_bool_error;
9080 for (i = 0; i < map->n; ++i) {
9081 is_empty = isl_basic_map_is_empty(map->p[i]);
9082 if (is_empty < 0)
9083 return isl_bool_error;
9084 if (!is_empty)
9085 return isl_bool_false;
9086 }
9087 return isl_bool_true;
9088 }
9089
isl_map_plain_is_empty(__isl_keep isl_map * map)9090 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
9091 {
9092 return map ? map->n == 0 : isl_bool_error;
9093 }
9094
isl_set_plain_is_empty(__isl_keep isl_set * set)9095 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
9096 {
9097 return set ? set->n == 0 : isl_bool_error;
9098 }
9099
isl_set_is_empty(__isl_keep isl_set * set)9100 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
9101 {
9102 return isl_map_is_empty(set_to_map(set));
9103 }
9104
9105 #undef TYPE
9106 #define TYPE isl_basic_map
9107
9108 static
9109 #include "isl_type_has_equal_space_bin_templ.c"
9110 #include "isl_type_check_equal_space_templ.c"
9111
9112 /* Check that "bset1" and "bset2" live in the same space,
9113 * reporting an error if they do not.
9114 */
isl_basic_set_check_equal_space(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2)9115 isl_stat isl_basic_set_check_equal_space(__isl_keep isl_basic_set *bset1,
9116 __isl_keep isl_basic_set *bset2)
9117 {
9118 return isl_basic_map_check_equal_space(bset_to_bmap(bset1),
9119 bset_to_bmap(bset1));
9120 }
9121
9122 #undef TYPE
9123 #define TYPE isl_map
9124
9125 #include "isl_type_has_equal_space_bin_templ.c"
9126 #include "isl_type_check_equal_space_templ.c"
9127 #include "isl_type_has_space_templ.c"
9128
isl_set_has_equal_space(__isl_keep isl_set * set1,__isl_keep isl_set * set2)9129 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
9130 __isl_keep isl_set *set2)
9131 {
9132 return isl_map_has_equal_space(set_to_map(set1), set_to_map(set2));
9133 }
9134
9135 #undef TYPE1
9136 #define TYPE1 isl_map
9137 #undef TYPE2
9138 #define TYPE2 isl_basic_map
9139 #undef TYPE_PAIR
9140 #define TYPE_PAIR isl_map_basic_map
9141
9142 static
9143 #include "isl_type_has_equal_space_templ.c"
9144 #include "isl_type_check_equal_space_templ.c"
9145
9146 /* Check that "set" and "bset" live in the same space,
9147 * reporting an error if they do not.
9148 */
isl_set_basic_set_check_equal_space(__isl_keep isl_set * set,__isl_keep isl_basic_set * bset)9149 isl_stat isl_set_basic_set_check_equal_space(__isl_keep isl_set *set,
9150 __isl_keep isl_basic_set *bset)
9151 {
9152 return isl_map_basic_map_check_equal_space(set_to_map(set),
9153 bset_to_bmap(bset));
9154 }
9155
map_is_equal(__isl_keep isl_map * map1,__isl_keep isl_map * map2)9156 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9157 {
9158 isl_bool is_subset;
9159
9160 if (!map1 || !map2)
9161 return isl_bool_error;
9162 is_subset = isl_map_is_subset(map1, map2);
9163 if (is_subset != isl_bool_true)
9164 return is_subset;
9165 is_subset = isl_map_is_subset(map2, map1);
9166 return is_subset;
9167 }
9168
9169 /* Is "map1" equal to "map2"?
9170 *
9171 * First check if they are obviously equal.
9172 * If not, then perform a more detailed analysis.
9173 */
isl_map_is_equal(__isl_keep isl_map * map1,__isl_keep isl_map * map2)9174 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9175 {
9176 isl_bool equal;
9177
9178 equal = isl_map_plain_is_equal(map1, map2);
9179 if (equal < 0 || equal)
9180 return equal;
9181 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
9182 }
9183
isl_basic_map_is_strict_subset(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)9184 isl_bool isl_basic_map_is_strict_subset(__isl_keep isl_basic_map *bmap1,
9185 __isl_keep isl_basic_map *bmap2)
9186 {
9187 isl_bool is_subset;
9188
9189 if (!bmap1 || !bmap2)
9190 return isl_bool_error;
9191 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
9192 if (is_subset != isl_bool_true)
9193 return is_subset;
9194 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
9195 return isl_bool_not(is_subset);
9196 }
9197
isl_map_is_strict_subset(__isl_keep isl_map * map1,__isl_keep isl_map * map2)9198 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
9199 __isl_keep isl_map *map2)
9200 {
9201 isl_bool is_subset;
9202
9203 if (!map1 || !map2)
9204 return isl_bool_error;
9205 is_subset = isl_map_is_subset(map1, map2);
9206 if (is_subset != isl_bool_true)
9207 return is_subset;
9208 is_subset = isl_map_is_subset(map2, map1);
9209 return isl_bool_not(is_subset);
9210 }
9211
isl_set_is_strict_subset(__isl_keep isl_set * set1,__isl_keep isl_set * set2)9212 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
9213 __isl_keep isl_set *set2)
9214 {
9215 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
9216 }
9217
9218 /* Is "bmap" obviously equal to the universe with the same space?
9219 *
9220 * That is, does it not have any constraints?
9221 */
isl_basic_map_plain_is_universe(__isl_keep isl_basic_map * bmap)9222 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
9223 {
9224 if (!bmap)
9225 return isl_bool_error;
9226 return bmap->n_eq == 0 && bmap->n_ineq == 0;
9227 }
9228
9229 /* Is "bset" obviously equal to the universe with the same space?
9230 */
isl_basic_set_plain_is_universe(__isl_keep isl_basic_set * bset)9231 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
9232 {
9233 return isl_basic_map_plain_is_universe(bset);
9234 }
9235
9236 /* If "c" does not involve any existentially quantified variables,
9237 * then set *univ to false and abort
9238 */
involves_divs(__isl_take isl_constraint * c,void * user)9239 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
9240 {
9241 isl_bool *univ = user;
9242 isl_size n;
9243
9244 n = isl_constraint_dim(c, isl_dim_div);
9245 if (n < 0)
9246 c = isl_constraint_free(c);
9247 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
9248 isl_constraint_free(c);
9249 if (*univ < 0 || !*univ)
9250 return isl_stat_error;
9251 return isl_stat_ok;
9252 }
9253
9254 /* Is "bmap" equal to the universe with the same space?
9255 *
9256 * First check if it is obviously equal to the universe.
9257 * If not and if there are any constraints not involving
9258 * existentially quantified variables, then it is certainly
9259 * not equal to the universe.
9260 * Otherwise, check if the universe is a subset of "bmap".
9261 */
isl_basic_map_is_universe(__isl_keep isl_basic_map * bmap)9262 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
9263 {
9264 isl_size n_div;
9265 isl_bool univ;
9266 isl_basic_map *test;
9267
9268 univ = isl_basic_map_plain_is_universe(bmap);
9269 if (univ < 0 || univ)
9270 return univ;
9271 n_div = isl_basic_map_dim(bmap, isl_dim_div);
9272 if (n_div < 0)
9273 return isl_bool_error;
9274 if (n_div == 0)
9275 return isl_bool_false;
9276 univ = isl_bool_true;
9277 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
9278 univ)
9279 return isl_bool_error;
9280 if (univ < 0 || !univ)
9281 return univ;
9282 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
9283 univ = isl_basic_map_is_subset(test, bmap);
9284 isl_basic_map_free(test);
9285 return univ;
9286 }
9287
9288 /* Is "bset" equal to the universe with the same space?
9289 */
isl_basic_set_is_universe(__isl_keep isl_basic_set * bset)9290 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
9291 {
9292 return isl_basic_map_is_universe(bset);
9293 }
9294
isl_map_plain_is_universe(__isl_keep isl_map * map)9295 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
9296 {
9297 int i;
9298
9299 if (!map)
9300 return isl_bool_error;
9301
9302 for (i = 0; i < map->n; ++i) {
9303 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
9304 if (r < 0 || r)
9305 return r;
9306 }
9307
9308 return isl_bool_false;
9309 }
9310
isl_set_plain_is_universe(__isl_keep isl_set * set)9311 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
9312 {
9313 return isl_map_plain_is_universe(set_to_map(set));
9314 }
9315
isl_basic_map_is_empty(__isl_keep isl_basic_map * bmap)9316 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
9317 {
9318 struct isl_basic_set *bset = NULL;
9319 struct isl_vec *sample = NULL;
9320 isl_bool empty, non_empty;
9321
9322 if (!bmap)
9323 return isl_bool_error;
9324
9325 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
9326 return isl_bool_true;
9327
9328 if (isl_basic_map_plain_is_universe(bmap))
9329 return isl_bool_false;
9330
9331 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
9332 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
9333 copy = isl_basic_map_remove_redundancies(copy);
9334 empty = isl_basic_map_plain_is_empty(copy);
9335 isl_basic_map_free(copy);
9336 return empty;
9337 }
9338
9339 non_empty = isl_basic_map_plain_is_non_empty(bmap);
9340 if (non_empty < 0)
9341 return isl_bool_error;
9342 if (non_empty)
9343 return isl_bool_false;
9344 isl_vec_free(bmap->sample);
9345 bmap->sample = NULL;
9346 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
9347 if (!bset)
9348 return isl_bool_error;
9349 sample = isl_basic_set_sample_vec(bset);
9350 if (!sample)
9351 return isl_bool_error;
9352 empty = sample->size == 0;
9353 isl_vec_free(bmap->sample);
9354 bmap->sample = sample;
9355 if (empty)
9356 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
9357
9358 return empty;
9359 }
9360
isl_basic_map_plain_is_empty(__isl_keep isl_basic_map * bmap)9361 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
9362 {
9363 if (!bmap)
9364 return isl_bool_error;
9365 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
9366 }
9367
isl_basic_set_plain_is_empty(__isl_keep isl_basic_set * bset)9368 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
9369 {
9370 if (!bset)
9371 return isl_bool_error;
9372 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
9373 }
9374
9375 /* Is "bmap" known to be non-empty?
9376 *
9377 * That is, is the cached sample still valid?
9378 */
isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map * bmap)9379 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
9380 {
9381 isl_size total;
9382
9383 if (!bmap)
9384 return isl_bool_error;
9385 if (!bmap->sample)
9386 return isl_bool_false;
9387 total = isl_basic_map_dim(bmap, isl_dim_all);
9388 if (total < 0)
9389 return isl_bool_error;
9390 if (bmap->sample->size != 1 + total)
9391 return isl_bool_false;
9392 return isl_basic_map_contains(bmap, bmap->sample);
9393 }
9394
isl_basic_set_is_empty(__isl_keep isl_basic_set * bset)9395 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
9396 {
9397 return isl_basic_map_is_empty(bset_to_bmap(bset));
9398 }
9399
isl_basic_map_union(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)9400 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
9401 __isl_take isl_basic_map *bmap2)
9402 {
9403 struct isl_map *map;
9404
9405 if (isl_basic_map_check_equal_space(bmap1, bmap2) < 0)
9406 goto error;
9407
9408 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
9409 if (!map)
9410 goto error;
9411 map = isl_map_add_basic_map(map, bmap1);
9412 map = isl_map_add_basic_map(map, bmap2);
9413 return map;
9414 error:
9415 isl_basic_map_free(bmap1);
9416 isl_basic_map_free(bmap2);
9417 return NULL;
9418 }
9419
isl_basic_set_union(__isl_take isl_basic_set * bset1,__isl_take isl_basic_set * bset2)9420 __isl_give isl_set *isl_basic_set_union(__isl_take isl_basic_set *bset1,
9421 __isl_take isl_basic_set *bset2)
9422 {
9423 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
9424 bset_to_bmap(bset2)));
9425 }
9426
9427 /* Order divs such that any div only depends on previous divs */
isl_basic_map_order_divs(__isl_take isl_basic_map * bmap)9428 __isl_give isl_basic_map *isl_basic_map_order_divs(
9429 __isl_take isl_basic_map *bmap)
9430 {
9431 int i;
9432 isl_size off;
9433
9434 off = isl_basic_map_var_offset(bmap, isl_dim_div);
9435 if (off < 0)
9436 return isl_basic_map_free(bmap);
9437
9438 for (i = 0; i < bmap->n_div; ++i) {
9439 int pos;
9440 if (isl_int_is_zero(bmap->div[i][0]))
9441 continue;
9442 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
9443 bmap->n_div-i);
9444 if (pos == -1)
9445 continue;
9446 if (pos == 0)
9447 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
9448 "integer division depends on itself",
9449 return isl_basic_map_free(bmap));
9450 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
9451 if (!bmap)
9452 return NULL;
9453 --i;
9454 }
9455 return bmap;
9456 }
9457
isl_map_order_divs(__isl_take isl_map * map)9458 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
9459 {
9460 int i;
9461
9462 if (!map)
9463 return 0;
9464
9465 for (i = 0; i < map->n; ++i) {
9466 map->p[i] = isl_basic_map_order_divs(map->p[i]);
9467 if (!map->p[i])
9468 goto error;
9469 }
9470
9471 return map;
9472 error:
9473 isl_map_free(map);
9474 return NULL;
9475 }
9476
9477 /* Sort the local variables of "bset".
9478 */
isl_basic_set_sort_divs(__isl_take isl_basic_set * bset)9479 __isl_give isl_basic_set *isl_basic_set_sort_divs(
9480 __isl_take isl_basic_set *bset)
9481 {
9482 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
9483 }
9484
9485 /* Apply the expansion computed by isl_merge_divs.
9486 * The expansion itself is given by "exp" while the resulting
9487 * list of divs is given by "div".
9488 *
9489 * Move the integer divisions of "bmap" into the right position
9490 * according to "exp" and then introduce the additional integer
9491 * divisions, adding div constraints.
9492 * The moving should be done first to avoid moving coefficients
9493 * in the definitions of the extra integer divisions.
9494 */
isl_basic_map_expand_divs(__isl_take isl_basic_map * bmap,__isl_take isl_mat * div,int * exp)9495 __isl_give isl_basic_map *isl_basic_map_expand_divs(
9496 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
9497 {
9498 int i, j;
9499 int n_div;
9500
9501 bmap = isl_basic_map_cow(bmap);
9502 if (!bmap || !div)
9503 goto error;
9504
9505 if (div->n_row < bmap->n_div)
9506 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
9507 "not an expansion", goto error);
9508
9509 n_div = bmap->n_div;
9510 bmap = isl_basic_map_extend(bmap, div->n_row - n_div, 0,
9511 2 * (div->n_row - n_div));
9512
9513 for (i = n_div; i < div->n_row; ++i)
9514 if (isl_basic_map_alloc_div(bmap) < 0)
9515 goto error;
9516
9517 for (j = n_div - 1; j >= 0; --j) {
9518 if (exp[j] == j)
9519 break;
9520 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
9521 if (!bmap)
9522 goto error;
9523 }
9524 j = 0;
9525 for (i = 0; i < div->n_row; ++i) {
9526 if (j < n_div && exp[j] == i) {
9527 j++;
9528 } else {
9529 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
9530 if (isl_basic_map_div_is_marked_unknown(bmap, i))
9531 continue;
9532 bmap = isl_basic_map_add_div_constraints(bmap, i);
9533 if (!bmap)
9534 goto error;
9535 }
9536 }
9537
9538 isl_mat_free(div);
9539 return bmap;
9540 error:
9541 isl_basic_map_free(bmap);
9542 isl_mat_free(div);
9543 return NULL;
9544 }
9545
9546 /* Apply the expansion computed by isl_merge_divs.
9547 * The expansion itself is given by "exp" while the resulting
9548 * list of divs is given by "div".
9549 */
isl_basic_set_expand_divs(__isl_take isl_basic_set * bset,__isl_take isl_mat * div,int * exp)9550 __isl_give isl_basic_set *isl_basic_set_expand_divs(
9551 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
9552 {
9553 return isl_basic_map_expand_divs(bset, div, exp);
9554 }
9555
9556 /* Look for a div in dst that corresponds to the div "div" in src.
9557 * The divs before "div" in src and dst are assumed to be the same.
9558 *
9559 * Return the position of the corresponding div in dst
9560 * if there is one. Otherwise, return a position beyond the integer divisions.
9561 * Return -1 on error.
9562 */
find_div(__isl_keep isl_basic_map * dst,__isl_keep isl_basic_map * src,unsigned div)9563 static int find_div(__isl_keep isl_basic_map *dst,
9564 __isl_keep isl_basic_map *src, unsigned div)
9565 {
9566 int i;
9567 isl_size n_div;
9568 isl_size v_div;
9569
9570 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9571 n_div = isl_basic_map_dim(dst, isl_dim_div);
9572 if (n_div < 0 || v_div < 0)
9573 return -1;
9574 isl_assert(dst->ctx, div <= n_div, return -1);
9575 for (i = div; i < n_div; ++i)
9576 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
9577 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
9578 n_div - div) == -1)
9579 return i;
9580 return n_div;
9581 }
9582
9583 /* Align the divs of "dst" to those of "src", adding divs from "src"
9584 * if needed. That is, make sure that the first src->n_div divs
9585 * of the result are equal to those of src.
9586 * The integer division of "src" are assumed to be ordered.
9587 *
9588 * The integer divisions are swapped into the right position
9589 * (possibly after adding them first). This may result
9590 * in the remaining integer divisions appearing in the wrong order,
9591 * i.e., with some integer division appearing before
9592 * some other integer division on which it depends.
9593 * The integer divisions therefore need to be ordered.
9594 * This will not affect the integer divisions aligned to those of "src",
9595 * since "src" is assumed to have ordered integer divisions.
9596 *
9597 * The result is not finalized as by design it will have redundant
9598 * divs if any divs from "src" were copied.
9599 */
isl_basic_map_align_divs(__isl_take isl_basic_map * dst,__isl_keep isl_basic_map * src)9600 __isl_give isl_basic_map *isl_basic_map_align_divs(
9601 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
9602 {
9603 int i;
9604 isl_bool known;
9605 int extended;
9606 isl_size v_div;
9607 isl_size dst_n_div;
9608
9609 if (!dst || !src)
9610 return isl_basic_map_free(dst);
9611
9612 if (src->n_div == 0)
9613 return dst;
9614
9615 known = isl_basic_map_divs_known(src);
9616 if (known < 0)
9617 return isl_basic_map_free(dst);
9618 if (!known)
9619 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
9620 "some src divs are unknown",
9621 return isl_basic_map_free(dst));
9622
9623 v_div = isl_basic_map_var_offset(src, isl_dim_div);
9624 if (v_div < 0)
9625 return isl_basic_map_free(dst);
9626
9627 extended = 0;
9628 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
9629 if (dst_n_div < 0)
9630 dst = isl_basic_map_free(dst);
9631 for (i = 0; i < src->n_div; ++i) {
9632 int j = find_div(dst, src, i);
9633 if (j < 0)
9634 dst = isl_basic_map_free(dst);
9635 if (j == dst_n_div) {
9636 if (!extended) {
9637 int extra = src->n_div - i;
9638 dst = isl_basic_map_cow(dst);
9639 if (!dst)
9640 return isl_basic_map_free(dst);
9641 dst = isl_basic_map_extend(dst,
9642 extra, 0, 2 * extra);
9643 extended = 1;
9644 }
9645 j = isl_basic_map_alloc_div(dst);
9646 if (j < 0)
9647 return isl_basic_map_free(dst);
9648 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
9649 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
9650 dst_n_div++;
9651 dst = isl_basic_map_add_div_constraints(dst, j);
9652 if (!dst)
9653 return isl_basic_map_free(dst);
9654 }
9655 if (j != i)
9656 dst = isl_basic_map_swap_div(dst, i, j);
9657 if (!dst)
9658 return isl_basic_map_free(dst);
9659 }
9660 return isl_basic_map_order_divs(dst);
9661 }
9662
isl_map_align_divs_internal(__isl_take isl_map * map)9663 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9664 {
9665 int i;
9666
9667 if (!map)
9668 return NULL;
9669 if (map->n == 0)
9670 return map;
9671 map = isl_map_compute_divs(map);
9672 map = isl_map_order_divs(map);
9673 map = isl_map_cow(map);
9674 if (!map)
9675 return NULL;
9676
9677 for (i = 1; i < map->n; ++i)
9678 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9679 for (i = 1; i < map->n; ++i) {
9680 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9681 if (!map->p[i])
9682 return isl_map_free(map);
9683 }
9684
9685 map = isl_map_unmark_normalized(map);
9686 return map;
9687 }
9688
isl_map_align_divs(__isl_take isl_map * map)9689 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9690 {
9691 return isl_map_align_divs_internal(map);
9692 }
9693
isl_set_align_divs(__isl_take isl_set * set)9694 __isl_give isl_set *isl_set_align_divs(__isl_take isl_set *set)
9695 {
9696 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9697 }
9698
9699 /* Align the divs of the basic maps in "map" to those
9700 * of the basic maps in "list", as well as to the other basic maps in "map".
9701 * The elements in "list" are assumed to have known divs.
9702 */
isl_map_align_divs_to_basic_map_list(__isl_take isl_map * map,__isl_keep isl_basic_map_list * list)9703 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9704 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9705 {
9706 int i;
9707 isl_size n;
9708
9709 n = isl_basic_map_list_n_basic_map(list);
9710 map = isl_map_compute_divs(map);
9711 map = isl_map_cow(map);
9712 if (!map || n < 0)
9713 return isl_map_free(map);
9714 if (map->n == 0)
9715 return map;
9716
9717 for (i = 0; i < n; ++i) {
9718 isl_basic_map *bmap;
9719
9720 bmap = isl_basic_map_list_get_basic_map(list, i);
9721 bmap = isl_basic_map_order_divs(bmap);
9722 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9723 isl_basic_map_free(bmap);
9724 }
9725 if (!map->p[0])
9726 return isl_map_free(map);
9727
9728 return isl_map_align_divs_internal(map);
9729 }
9730
9731 /* Align the divs of each element of "list" to those of "bmap".
9732 * Both "bmap" and the elements of "list" are assumed to have known divs.
9733 */
isl_basic_map_list_align_divs_to_basic_map(__isl_take isl_basic_map_list * list,__isl_keep isl_basic_map * bmap)9734 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9735 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9736 {
9737 int i;
9738 isl_size n;
9739
9740 n = isl_basic_map_list_n_basic_map(list);
9741 if (n < 0 || !bmap)
9742 return isl_basic_map_list_free(list);
9743
9744 for (i = 0; i < n; ++i) {
9745 isl_basic_map *bmap_i;
9746
9747 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9748 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9749 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9750 }
9751
9752 return list;
9753 }
9754
isl_set_apply(__isl_take isl_set * set,__isl_take isl_map * map)9755 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9756 __isl_take isl_map *map)
9757 {
9758 isl_bool ok;
9759
9760 isl_map_align_params_set(&map, &set);
9761 ok = isl_map_compatible_domain(map, set);
9762 if (ok < 0)
9763 goto error;
9764 if (!ok)
9765 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9766 "incompatible spaces", goto error);
9767 map = isl_map_intersect_domain(map, set);
9768 set = isl_map_range(map);
9769 return set;
9770 error:
9771 isl_set_free(set);
9772 isl_map_free(map);
9773 return NULL;
9774 }
9775
9776 /* There is no need to cow as removing empty parts doesn't change
9777 * the meaning of the set.
9778 */
isl_map_remove_empty_parts(__isl_take isl_map * map)9779 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9780 {
9781 int i;
9782
9783 if (!map)
9784 return NULL;
9785
9786 for (i = map->n - 1; i >= 0; --i)
9787 map = remove_if_empty(map, i);
9788
9789 return map;
9790 }
9791
isl_set_remove_empty_parts(__isl_take isl_set * set)9792 __isl_give isl_set *isl_set_remove_empty_parts(__isl_take isl_set *set)
9793 {
9794 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9795 }
9796
9797 /* Create a binary relation that maps the shared initial "pos" dimensions
9798 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9799 */
join_initial(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2,int pos)9800 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9801 __isl_keep isl_basic_set *bset2, int pos)
9802 {
9803 isl_basic_map *bmap1;
9804 isl_basic_map *bmap2;
9805
9806 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9807 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9808 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9809 isl_dim_out, 0, pos);
9810 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9811 isl_dim_out, 0, pos);
9812 return isl_basic_map_range_product(bmap1, bmap2);
9813 }
9814
9815 /* Given two basic sets bset1 and bset2, compute the maximal difference
9816 * between the values of dimension pos in bset1 and those in bset2
9817 * for any common value of the parameters and dimensions preceding pos.
9818 */
basic_set_maximal_difference_at(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2,int pos,isl_int * opt)9819 static enum isl_lp_result basic_set_maximal_difference_at(
9820 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9821 int pos, isl_int *opt)
9822 {
9823 isl_basic_map *bmap1;
9824 struct isl_ctx *ctx;
9825 struct isl_vec *obj;
9826 isl_size total;
9827 isl_size nparam;
9828 isl_size dim1;
9829 enum isl_lp_result res;
9830
9831 nparam = isl_basic_set_dim(bset1, isl_dim_param);
9832 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9833 if (nparam < 0 || dim1 < 0 || !bset2)
9834 return isl_lp_error;
9835
9836 bmap1 = join_initial(bset1, bset2, pos);
9837 total = isl_basic_map_dim(bmap1, isl_dim_all);
9838 if (total < 0)
9839 return isl_lp_error;
9840
9841 ctx = bmap1->ctx;
9842 obj = isl_vec_alloc(ctx, 1 + total);
9843 if (!obj)
9844 goto error;
9845 isl_seq_clr(obj->block.data, 1 + total);
9846 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9847 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9848 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9849 opt, NULL, NULL);
9850 isl_basic_map_free(bmap1);
9851 isl_vec_free(obj);
9852 return res;
9853 error:
9854 isl_basic_map_free(bmap1);
9855 return isl_lp_error;
9856 }
9857
9858 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9859 * for any common value of the parameters and dimensions preceding pos
9860 * in both basic sets, the values of dimension pos in bset1 are
9861 * smaller or larger than those in bset2.
9862 *
9863 * Returns
9864 * 1 if bset1 follows bset2
9865 * -1 if bset1 precedes bset2
9866 * 0 if bset1 and bset2 are incomparable
9867 * -2 if some error occurred.
9868 */
isl_basic_set_compare_at(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2,int pos)9869 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9870 __isl_keep isl_basic_set *bset2, int pos)
9871 {
9872 isl_int opt;
9873 enum isl_lp_result res;
9874 int cmp;
9875
9876 isl_int_init(opt);
9877
9878 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9879
9880 if (res == isl_lp_empty)
9881 cmp = 0;
9882 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9883 res == isl_lp_unbounded)
9884 cmp = 1;
9885 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9886 cmp = -1;
9887 else
9888 cmp = -2;
9889
9890 isl_int_clear(opt);
9891 return cmp;
9892 }
9893
9894 /* Given two basic sets bset1 and bset2, check whether
9895 * for any common value of the parameters and dimensions preceding pos
9896 * there is a value of dimension pos in bset1 that is larger
9897 * than a value of the same dimension in bset2.
9898 *
9899 * Return
9900 * 1 if there exists such a pair
9901 * 0 if there is no such pair, but there is a pair of equal values
9902 * -1 otherwise
9903 * -2 if some error occurred.
9904 */
isl_basic_set_follows_at(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2,int pos)9905 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9906 __isl_keep isl_basic_set *bset2, int pos)
9907 {
9908 isl_bool empty;
9909 isl_basic_map *bmap;
9910 isl_size dim1;
9911
9912 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9913 if (dim1 < 0)
9914 return -2;
9915 bmap = join_initial(bset1, bset2, pos);
9916 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9917 isl_dim_out, dim1 - pos);
9918 empty = isl_basic_map_is_empty(bmap);
9919 if (empty < 0)
9920 goto error;
9921 if (empty) {
9922 isl_basic_map_free(bmap);
9923 return -1;
9924 }
9925 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9926 isl_dim_out, dim1 - pos);
9927 empty = isl_basic_map_is_empty(bmap);
9928 if (empty < 0)
9929 goto error;
9930 isl_basic_map_free(bmap);
9931 if (empty)
9932 return 0;
9933 return 1;
9934 error:
9935 isl_basic_map_free(bmap);
9936 return -2;
9937 }
9938
9939 /* Given two sets set1 and set2, check whether
9940 * for any common value of the parameters and dimensions preceding pos
9941 * there is a value of dimension pos in set1 that is larger
9942 * than a value of the same dimension in set2.
9943 *
9944 * Return
9945 * 1 if there exists such a pair
9946 * 0 if there is no such pair, but there is a pair of equal values
9947 * -1 otherwise
9948 * -2 if some error occurred.
9949 */
isl_set_follows_at(__isl_keep isl_set * set1,__isl_keep isl_set * set2,int pos)9950 int isl_set_follows_at(__isl_keep isl_set *set1,
9951 __isl_keep isl_set *set2, int pos)
9952 {
9953 int i, j;
9954 int follows = -1;
9955
9956 if (!set1 || !set2)
9957 return -2;
9958
9959 for (i = 0; i < set1->n; ++i)
9960 for (j = 0; j < set2->n; ++j) {
9961 int f;
9962 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9963 if (f == 1 || f == -2)
9964 return f;
9965 if (f > follows)
9966 follows = f;
9967 }
9968
9969 return follows;
9970 }
9971
isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map * bmap,unsigned pos,isl_int * val)9972 static isl_bool isl_basic_map_plain_has_fixed_var(
9973 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9974 {
9975 int i;
9976 int d;
9977 isl_size total;
9978
9979 total = isl_basic_map_dim(bmap, isl_dim_all);
9980 if (total < 0)
9981 return isl_bool_error;
9982 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9983 for (; d+1 > pos; --d)
9984 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9985 break;
9986 if (d != pos)
9987 continue;
9988 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9989 return isl_bool_false;
9990 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9991 return isl_bool_false;
9992 if (!isl_int_is_one(bmap->eq[i][1+d]))
9993 return isl_bool_false;
9994 if (val)
9995 isl_int_neg(*val, bmap->eq[i][0]);
9996 return isl_bool_true;
9997 }
9998 return isl_bool_false;
9999 }
10000
isl_map_plain_has_fixed_var(__isl_keep isl_map * map,unsigned pos,isl_int * val)10001 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
10002 unsigned pos, isl_int *val)
10003 {
10004 int i;
10005 isl_int v;
10006 isl_int tmp;
10007 isl_bool fixed;
10008
10009 if (!map)
10010 return isl_bool_error;
10011 if (map->n == 0)
10012 return isl_bool_false;
10013 if (map->n == 1)
10014 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
10015 isl_int_init(v);
10016 isl_int_init(tmp);
10017 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
10018 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
10019 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
10020 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
10021 fixed = isl_bool_false;
10022 }
10023 if (val)
10024 isl_int_set(*val, v);
10025 isl_int_clear(tmp);
10026 isl_int_clear(v);
10027 return fixed;
10028 }
10029
isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set * bset,unsigned pos,isl_int * val)10030 static isl_bool isl_basic_set_plain_has_fixed_var(
10031 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
10032 {
10033 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
10034 pos, val);
10035 }
10036
isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,isl_int * val)10037 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
10038 enum isl_dim_type type, unsigned pos, isl_int *val)
10039 {
10040 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10041 return isl_bool_error;
10042 return isl_basic_map_plain_has_fixed_var(bmap,
10043 isl_basic_map_offset(bmap, type) - 1 + pos, val);
10044 }
10045
10046 /* If "bmap" obviously lies on a hyperplane where the given dimension
10047 * has a fixed value, then return that value.
10048 * Otherwise return NaN.
10049 */
isl_basic_map_plain_get_val_if_fixed(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)10050 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
10051 __isl_keep isl_basic_map *bmap,
10052 enum isl_dim_type type, unsigned pos)
10053 {
10054 isl_ctx *ctx;
10055 isl_val *v;
10056 isl_bool fixed;
10057
10058 if (!bmap)
10059 return NULL;
10060 ctx = isl_basic_map_get_ctx(bmap);
10061 v = isl_val_alloc(ctx);
10062 if (!v)
10063 return NULL;
10064 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
10065 if (fixed < 0)
10066 return isl_val_free(v);
10067 if (fixed) {
10068 isl_int_set_si(v->d, 1);
10069 return v;
10070 }
10071 isl_val_free(v);
10072 return isl_val_nan(ctx);
10073 }
10074
isl_map_plain_is_fixed(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos,isl_int * val)10075 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
10076 enum isl_dim_type type, unsigned pos, isl_int *val)
10077 {
10078 if (isl_map_check_range(map, type, pos, 1) < 0)
10079 return isl_bool_error;
10080 return isl_map_plain_has_fixed_var(map,
10081 map_offset(map, type) - 1 + pos, val);
10082 }
10083
10084 /* If "map" obviously lies on a hyperplane where the given dimension
10085 * has a fixed value, then return that value.
10086 * Otherwise return NaN.
10087 */
isl_map_plain_get_val_if_fixed(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)10088 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
10089 enum isl_dim_type type, unsigned pos)
10090 {
10091 isl_ctx *ctx;
10092 isl_val *v;
10093 isl_bool fixed;
10094
10095 if (!map)
10096 return NULL;
10097 ctx = isl_map_get_ctx(map);
10098 v = isl_val_alloc(ctx);
10099 if (!v)
10100 return NULL;
10101 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
10102 if (fixed < 0)
10103 return isl_val_free(v);
10104 if (fixed) {
10105 isl_int_set_si(v->d, 1);
10106 return v;
10107 }
10108 isl_val_free(v);
10109 return isl_val_nan(ctx);
10110 }
10111
10112 /* If "set" obviously lies on a hyperplane where the given dimension
10113 * has a fixed value, then return that value.
10114 * Otherwise return NaN.
10115 */
isl_set_plain_get_val_if_fixed(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)10116 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
10117 enum isl_dim_type type, unsigned pos)
10118 {
10119 return isl_map_plain_get_val_if_fixed(set, type, pos);
10120 }
10121
10122 /* Return a sequence of values in the same space as "set"
10123 * that are equal to the corresponding set dimensions of "set"
10124 * for those set dimensions that obviously lie on a hyperplane
10125 * where the dimension has a fixed value.
10126 * The other elements are set to NaN.
10127 */
isl_set_get_plain_multi_val_if_fixed(__isl_keep isl_set * set)10128 __isl_give isl_multi_val *isl_set_get_plain_multi_val_if_fixed(
10129 __isl_keep isl_set *set)
10130 {
10131 int i;
10132 isl_size n;
10133 isl_space *space;
10134 isl_multi_val *mv;
10135
10136 space = isl_space_drop_all_params(isl_set_get_space(set));
10137 mv = isl_multi_val_alloc(space);
10138 n = isl_multi_val_size(mv);
10139 if (n < 0)
10140 return isl_multi_val_free(mv);
10141
10142 for (i = 0; i < n; ++i) {
10143 isl_val *v;
10144
10145 v = isl_set_plain_get_val_if_fixed(set, isl_dim_set, i);
10146 mv = isl_multi_val_set_val(mv, i, v);
10147 }
10148
10149 return mv;
10150 }
10151
10152 /* Check if dimension dim has fixed value and if so and if val is not NULL,
10153 * then return this fixed value in *val.
10154 */
isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set * bset,unsigned dim,isl_int * val)10155 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
10156 unsigned dim, isl_int *val)
10157 {
10158 isl_size nparam;
10159
10160 nparam = isl_basic_set_dim(bset, isl_dim_param);
10161 if (nparam < 0)
10162 return isl_bool_error;
10163 return isl_basic_set_plain_has_fixed_var(bset, nparam + dim, val);
10164 }
10165
10166 /* Return -1 if the constraint "c1" should be sorted before "c2"
10167 * and 1 if it should be sorted after "c2".
10168 * Return 0 if the two constraints are the same (up to the constant term).
10169 *
10170 * In particular, if a constraint involves later variables than another
10171 * then it is sorted after this other constraint.
10172 * uset_gist depends on constraints without existentially quantified
10173 * variables sorting first.
10174 *
10175 * For constraints that have the same latest variable, those
10176 * with the same coefficient for this latest variable (first in absolute value
10177 * and then in actual value) are grouped together.
10178 * This is useful for detecting pairs of constraints that can
10179 * be chained in their printed representation.
10180 *
10181 * Finally, within a group, constraints are sorted according to
10182 * their coefficients (excluding the constant term).
10183 */
sort_constraint_cmp(const void * p1,const void * p2,void * arg)10184 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
10185 {
10186 isl_int **c1 = (isl_int **) p1;
10187 isl_int **c2 = (isl_int **) p2;
10188 int l1, l2;
10189 unsigned size = *(unsigned *) arg;
10190 int cmp;
10191
10192 l1 = isl_seq_last_non_zero(*c1 + 1, size);
10193 l2 = isl_seq_last_non_zero(*c2 + 1, size);
10194
10195 if (l1 != l2)
10196 return l1 - l2;
10197
10198 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10199 if (cmp != 0)
10200 return cmp;
10201 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
10202 if (cmp != 0)
10203 return -cmp;
10204
10205 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
10206 }
10207
10208 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
10209 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
10210 * and 0 if the two constraints are the same (up to the constant term).
10211 */
isl_basic_map_constraint_cmp(__isl_keep isl_basic_map * bmap,isl_int * c1,isl_int * c2)10212 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
10213 isl_int *c1, isl_int *c2)
10214 {
10215 isl_size total;
10216 unsigned size;
10217
10218 total = isl_basic_map_dim(bmap, isl_dim_all);
10219 if (total < 0)
10220 return -2;
10221 size = total;
10222 return sort_constraint_cmp(&c1, &c2, &size);
10223 }
10224
isl_basic_map_sort_constraints(__isl_take isl_basic_map * bmap)10225 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
10226 __isl_take isl_basic_map *bmap)
10227 {
10228 isl_size total;
10229 unsigned size;
10230
10231 if (!bmap)
10232 return NULL;
10233 if (bmap->n_ineq == 0)
10234 return bmap;
10235 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
10236 return bmap;
10237 total = isl_basic_map_dim(bmap, isl_dim_all);
10238 if (total < 0)
10239 return isl_basic_map_free(bmap);
10240 size = total;
10241 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
10242 &sort_constraint_cmp, &size) < 0)
10243 return isl_basic_map_free(bmap);
10244 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
10245 return bmap;
10246 }
10247
isl_basic_set_sort_constraints(__isl_take isl_basic_set * bset)10248 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
10249 __isl_take isl_basic_set *bset)
10250 {
10251 isl_basic_map *bmap = bset_to_bmap(bset);
10252 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
10253 }
10254
isl_basic_map_normalize(__isl_take isl_basic_map * bmap)10255 __isl_give isl_basic_map *isl_basic_map_normalize(
10256 __isl_take isl_basic_map *bmap)
10257 {
10258 bmap = isl_basic_map_remove_redundancies(bmap);
10259 bmap = isl_basic_map_sort_constraints(bmap);
10260 return bmap;
10261 }
isl_basic_map_plain_cmp(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)10262 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
10263 __isl_keep isl_basic_map *bmap2)
10264 {
10265 int i, cmp;
10266 isl_size total;
10267 isl_space *space1, *space2;
10268
10269 if (!bmap1 || !bmap2)
10270 return -1;
10271
10272 if (bmap1 == bmap2)
10273 return 0;
10274 space1 = isl_basic_map_peek_space(bmap1);
10275 space2 = isl_basic_map_peek_space(bmap2);
10276 cmp = isl_space_cmp(space1, space2);
10277 if (cmp)
10278 return cmp;
10279 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
10280 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
10281 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
10282 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
10283 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10284 return 0;
10285 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
10286 return 1;
10287 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
10288 return -1;
10289 if (bmap1->n_eq != bmap2->n_eq)
10290 return bmap1->n_eq - bmap2->n_eq;
10291 if (bmap1->n_ineq != bmap2->n_ineq)
10292 return bmap1->n_ineq - bmap2->n_ineq;
10293 if (bmap1->n_div != bmap2->n_div)
10294 return bmap1->n_div - bmap2->n_div;
10295 total = isl_basic_map_dim(bmap1, isl_dim_all);
10296 if (total < 0)
10297 return -1;
10298 for (i = 0; i < bmap1->n_eq; ++i) {
10299 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
10300 if (cmp)
10301 return cmp;
10302 }
10303 for (i = 0; i < bmap1->n_ineq; ++i) {
10304 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
10305 if (cmp)
10306 return cmp;
10307 }
10308 for (i = 0; i < bmap1->n_div; ++i) {
10309 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
10310 if (cmp)
10311 return cmp;
10312 }
10313 return 0;
10314 }
10315
isl_basic_set_plain_cmp(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2)10316 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
10317 __isl_keep isl_basic_set *bset2)
10318 {
10319 return isl_basic_map_plain_cmp(bset1, bset2);
10320 }
10321
isl_set_plain_cmp(__isl_keep isl_set * set1,__isl_keep isl_set * set2)10322 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
10323 {
10324 int i, cmp;
10325
10326 if (set1 == set2)
10327 return 0;
10328 if (set1->n != set2->n)
10329 return set1->n - set2->n;
10330
10331 for (i = 0; i < set1->n; ++i) {
10332 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
10333 if (cmp)
10334 return cmp;
10335 }
10336
10337 return 0;
10338 }
10339
isl_basic_map_plain_is_equal(__isl_keep isl_basic_map * bmap1,__isl_keep isl_basic_map * bmap2)10340 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
10341 __isl_keep isl_basic_map *bmap2)
10342 {
10343 if (!bmap1 || !bmap2)
10344 return isl_bool_error;
10345 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
10346 }
10347
isl_basic_set_plain_is_equal(__isl_keep isl_basic_set * bset1,__isl_keep isl_basic_set * bset2)10348 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
10349 __isl_keep isl_basic_set *bset2)
10350 {
10351 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
10352 bset_to_bmap(bset2));
10353 }
10354
qsort_bmap_cmp(const void * p1,const void * p2)10355 static int qsort_bmap_cmp(const void *p1, const void *p2)
10356 {
10357 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
10358 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
10359
10360 return isl_basic_map_plain_cmp(bmap1, bmap2);
10361 }
10362
10363 /* Sort the basic maps of "map" and remove duplicate basic maps.
10364 *
10365 * While removing basic maps, we make sure that the basic maps remain
10366 * sorted because isl_map_normalize expects the basic maps of the result
10367 * to be sorted.
10368 */
sort_and_remove_duplicates(__isl_take isl_map * map)10369 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
10370 {
10371 int i, j;
10372
10373 map = isl_map_remove_empty_parts(map);
10374 if (!map)
10375 return NULL;
10376 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
10377 for (i = map->n - 1; i >= 1; --i) {
10378 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
10379 continue;
10380 isl_basic_map_free(map->p[i-1]);
10381 for (j = i; j < map->n; ++j)
10382 map->p[j - 1] = map->p[j];
10383 map->n--;
10384 }
10385
10386 return map;
10387 }
10388
10389 /* Remove obvious duplicates among the basic maps of "map".
10390 *
10391 * Unlike isl_map_normalize, this function does not remove redundant
10392 * constraints and only removes duplicates that have exactly the same
10393 * constraints in the input. It does sort the constraints and
10394 * the basic maps to ease the detection of duplicates.
10395 *
10396 * If "map" has already been normalized or if the basic maps are
10397 * disjoint, then there can be no duplicates.
10398 */
isl_map_remove_obvious_duplicates(__isl_take isl_map * map)10399 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
10400 {
10401 int i;
10402 isl_basic_map *bmap;
10403
10404 if (!map)
10405 return NULL;
10406 if (map->n <= 1)
10407 return map;
10408 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
10409 return map;
10410 for (i = 0; i < map->n; ++i) {
10411 bmap = isl_basic_map_copy(map->p[i]);
10412 bmap = isl_basic_map_sort_constraints(bmap);
10413 if (!bmap)
10414 return isl_map_free(map);
10415 isl_basic_map_free(map->p[i]);
10416 map->p[i] = bmap;
10417 }
10418
10419 map = sort_and_remove_duplicates(map);
10420 return map;
10421 }
10422
10423 /* We normalize in place, but if anything goes wrong we need
10424 * to return NULL, so we need to make sure we don't change the
10425 * meaning of any possible other copies of map.
10426 */
isl_map_normalize(__isl_take isl_map * map)10427 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
10428 {
10429 int i;
10430 struct isl_basic_map *bmap;
10431
10432 if (!map)
10433 return NULL;
10434 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
10435 return map;
10436 for (i = 0; i < map->n; ++i) {
10437 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
10438 if (!bmap)
10439 goto error;
10440 isl_basic_map_free(map->p[i]);
10441 map->p[i] = bmap;
10442 }
10443
10444 map = sort_and_remove_duplicates(map);
10445 if (map)
10446 ISL_F_SET(map, ISL_MAP_NORMALIZED);
10447 return map;
10448 error:
10449 isl_map_free(map);
10450 return NULL;
10451 }
10452
isl_set_normalize(__isl_take isl_set * set)10453 __isl_give isl_set *isl_set_normalize(__isl_take isl_set *set)
10454 {
10455 return set_from_map(isl_map_normalize(set_to_map(set)));
10456 }
10457
isl_map_plain_is_equal(__isl_keep isl_map * map1,__isl_keep isl_map * map2)10458 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
10459 __isl_keep isl_map *map2)
10460 {
10461 int i;
10462 isl_bool equal;
10463
10464 if (!map1 || !map2)
10465 return isl_bool_error;
10466
10467 if (map1 == map2)
10468 return isl_bool_true;
10469 equal = isl_map_has_equal_space(map1, map2);
10470 if (equal < 0 || !equal)
10471 return equal;
10472
10473 map1 = isl_map_copy(map1);
10474 map2 = isl_map_copy(map2);
10475 map1 = isl_map_normalize(map1);
10476 map2 = isl_map_normalize(map2);
10477 if (!map1 || !map2)
10478 goto error;
10479 equal = map1->n == map2->n;
10480 for (i = 0; equal && i < map1->n; ++i) {
10481 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
10482 if (equal < 0)
10483 goto error;
10484 }
10485 isl_map_free(map1);
10486 isl_map_free(map2);
10487 return equal;
10488 error:
10489 isl_map_free(map1);
10490 isl_map_free(map2);
10491 return isl_bool_error;
10492 }
10493
isl_set_plain_is_equal(__isl_keep isl_set * set1,__isl_keep isl_set * set2)10494 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
10495 __isl_keep isl_set *set2)
10496 {
10497 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
10498 }
10499
10500 /* Return the basic maps in "map" as a list.
10501 */
isl_map_get_basic_map_list(__isl_keep isl_map * map)10502 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
10503 __isl_keep isl_map *map)
10504 {
10505 int i;
10506 isl_ctx *ctx;
10507 isl_basic_map_list *list;
10508
10509 if (!map)
10510 return NULL;
10511 ctx = isl_map_get_ctx(map);
10512 list = isl_basic_map_list_alloc(ctx, map->n);
10513
10514 for (i = 0; i < map->n; ++i) {
10515 isl_basic_map *bmap;
10516
10517 bmap = isl_basic_map_copy(map->p[i]);
10518 list = isl_basic_map_list_add(list, bmap);
10519 }
10520
10521 return list;
10522 }
10523
10524 /* Return the intersection of the elements in the non-empty list "list".
10525 * All elements are assumed to live in the same space.
10526 */
isl_basic_map_list_intersect(__isl_take isl_basic_map_list * list)10527 __isl_give isl_basic_map *isl_basic_map_list_intersect(
10528 __isl_take isl_basic_map_list *list)
10529 {
10530 int i;
10531 isl_size n;
10532 isl_basic_map *bmap;
10533
10534 n = isl_basic_map_list_n_basic_map(list);
10535 if (n < 0)
10536 goto error;
10537 if (n < 1)
10538 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
10539 "expecting non-empty list", goto error);
10540
10541 bmap = isl_basic_map_list_get_basic_map(list, 0);
10542 for (i = 1; i < n; ++i) {
10543 isl_basic_map *bmap_i;
10544
10545 bmap_i = isl_basic_map_list_get_basic_map(list, i);
10546 bmap = isl_basic_map_intersect(bmap, bmap_i);
10547 }
10548
10549 isl_basic_map_list_free(list);
10550 return bmap;
10551 error:
10552 isl_basic_map_list_free(list);
10553 return NULL;
10554 }
10555
10556 /* Return the intersection of the elements in the non-empty list "list".
10557 * All elements are assumed to live in the same space.
10558 */
isl_basic_set_list_intersect(__isl_take isl_basic_set_list * list)10559 __isl_give isl_basic_set *isl_basic_set_list_intersect(
10560 __isl_take isl_basic_set_list *list)
10561 {
10562 return isl_basic_map_list_intersect(list);
10563 }
10564
10565 /* Return the union of the elements of "list".
10566 * The list is required to have at least one element.
10567 */
isl_basic_set_list_union(__isl_take isl_basic_set_list * list)10568 __isl_give isl_set *isl_basic_set_list_union(
10569 __isl_take isl_basic_set_list *list)
10570 {
10571 int i;
10572 isl_size n;
10573 isl_space *space;
10574 isl_basic_set *bset;
10575 isl_set *set;
10576
10577 n = isl_basic_set_list_n_basic_set(list);
10578 if (n < 0)
10579 goto error;
10580 if (n < 1)
10581 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
10582 "expecting non-empty list", goto error);
10583
10584 bset = isl_basic_set_list_get_basic_set(list, 0);
10585 space = isl_basic_set_get_space(bset);
10586 isl_basic_set_free(bset);
10587
10588 set = isl_set_alloc_space(space, n, 0);
10589 for (i = 0; i < n; ++i) {
10590 bset = isl_basic_set_list_get_basic_set(list, i);
10591 set = isl_set_add_basic_set(set, bset);
10592 }
10593
10594 isl_basic_set_list_free(list);
10595 return set;
10596 error:
10597 isl_basic_set_list_free(list);
10598 return NULL;
10599 }
10600
10601 /* Return the union of the elements in the non-empty list "list".
10602 * All elements are assumed to live in the same space.
10603 */
isl_set_list_union(__isl_take isl_set_list * list)10604 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
10605 {
10606 int i;
10607 isl_size n;
10608 isl_set *set;
10609
10610 n = isl_set_list_n_set(list);
10611 if (n < 0)
10612 goto error;
10613 if (n < 1)
10614 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
10615 "expecting non-empty list", goto error);
10616
10617 set = isl_set_list_get_set(list, 0);
10618 for (i = 1; i < n; ++i) {
10619 isl_set *set_i;
10620
10621 set_i = isl_set_list_get_set(list, i);
10622 set = isl_set_union(set, set_i);
10623 }
10624
10625 isl_set_list_free(list);
10626 return set;
10627 error:
10628 isl_set_list_free(list);
10629 return NULL;
10630 }
10631
isl_basic_map_product(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)10632 __isl_give isl_basic_map *isl_basic_map_product(
10633 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10634 {
10635 isl_space *space_result = NULL;
10636 struct isl_basic_map *bmap;
10637 unsigned in1, in2, out1, out2, nparam, total, pos;
10638 struct isl_dim_map *dim_map1, *dim_map2;
10639
10640 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10641 goto error;
10642 space_result = isl_space_product(isl_space_copy(bmap1->dim),
10643 isl_space_copy(bmap2->dim));
10644
10645 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10646 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10647 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10648 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10649 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10650
10651 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
10652 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10653 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10654 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10655 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10656 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10657 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10658 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10659 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10660 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10661 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10662
10663 bmap = isl_basic_map_alloc_space(space_result,
10664 bmap1->n_div + bmap2->n_div,
10665 bmap1->n_eq + bmap2->n_eq,
10666 bmap1->n_ineq + bmap2->n_ineq);
10667 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10668 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10669 bmap = isl_basic_map_simplify(bmap);
10670 return isl_basic_map_finalize(bmap);
10671 error:
10672 isl_basic_map_free(bmap1);
10673 isl_basic_map_free(bmap2);
10674 return NULL;
10675 }
10676
isl_basic_map_flat_product(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)10677 __isl_give isl_basic_map *isl_basic_map_flat_product(
10678 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10679 {
10680 isl_basic_map *prod;
10681
10682 prod = isl_basic_map_product(bmap1, bmap2);
10683 prod = isl_basic_map_flatten(prod);
10684 return prod;
10685 }
10686
isl_basic_set_flat_product(__isl_take isl_basic_set * bset1,__isl_take isl_basic_set * bset2)10687 __isl_give isl_basic_set *isl_basic_set_flat_product(
10688 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10689 {
10690 return isl_basic_map_flat_range_product(bset1, bset2);
10691 }
10692
isl_basic_map_domain_product(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)10693 __isl_give isl_basic_map *isl_basic_map_domain_product(
10694 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10695 {
10696 isl_space *space1, *space2;
10697 isl_space *space_result = NULL;
10698 isl_basic_map *bmap;
10699 isl_size in1, in2, out, nparam;
10700 unsigned total, pos;
10701 struct isl_dim_map *dim_map1, *dim_map2;
10702
10703 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10704 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10705 out = isl_basic_map_dim(bmap1, isl_dim_out);
10706 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10707 if (in1 < 0 || in2 < 0 || out < 0 || nparam < 0)
10708 goto error;
10709
10710 space1 = isl_basic_map_get_space(bmap1);
10711 space2 = isl_basic_map_get_space(bmap2);
10712 space_result = isl_space_domain_product(space1, space2);
10713
10714 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10715 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10716 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10717 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10718 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10719 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10720 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10721 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10722 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10723 isl_dim_map_div(dim_map1, bmap1, pos += out);
10724 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10725
10726 bmap = isl_basic_map_alloc_space(space_result,
10727 bmap1->n_div + bmap2->n_div,
10728 bmap1->n_eq + bmap2->n_eq,
10729 bmap1->n_ineq + bmap2->n_ineq);
10730 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10731 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10732 bmap = isl_basic_map_simplify(bmap);
10733 return isl_basic_map_finalize(bmap);
10734 error:
10735 isl_basic_map_free(bmap1);
10736 isl_basic_map_free(bmap2);
10737 return NULL;
10738 }
10739
isl_basic_map_range_product(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)10740 __isl_give isl_basic_map *isl_basic_map_range_product(
10741 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10742 {
10743 isl_bool rational;
10744 isl_space *space_result = NULL;
10745 isl_basic_map *bmap;
10746 isl_size in, out1, out2, nparam;
10747 unsigned total, pos;
10748 struct isl_dim_map *dim_map1, *dim_map2;
10749
10750 rational = isl_basic_map_is_rational(bmap1);
10751 if (rational >= 0 && rational)
10752 rational = isl_basic_map_is_rational(bmap2);
10753 in = isl_basic_map_dim(bmap1, isl_dim_in);
10754 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10755 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10756 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10757 if (in < 0 || out1 < 0 || out2 < 0 || nparam < 0 || rational < 0)
10758 goto error;
10759
10760 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10761 goto error;
10762
10763 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10764 isl_space_copy(bmap2->dim));
10765
10766 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10767 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10768 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10769 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10770 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10771 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10772 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10773 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10774 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10775 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10776 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10777
10778 bmap = isl_basic_map_alloc_space(space_result,
10779 bmap1->n_div + bmap2->n_div,
10780 bmap1->n_eq + bmap2->n_eq,
10781 bmap1->n_ineq + bmap2->n_ineq);
10782 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10783 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10784 if (rational)
10785 bmap = isl_basic_map_set_rational(bmap);
10786 bmap = isl_basic_map_simplify(bmap);
10787 return isl_basic_map_finalize(bmap);
10788 error:
10789 isl_basic_map_free(bmap1);
10790 isl_basic_map_free(bmap2);
10791 return NULL;
10792 }
10793
isl_basic_map_flat_range_product(__isl_take isl_basic_map * bmap1,__isl_take isl_basic_map * bmap2)10794 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10795 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10796 {
10797 isl_basic_map *prod;
10798
10799 prod = isl_basic_map_range_product(bmap1, bmap2);
10800 prod = isl_basic_map_flatten_range(prod);
10801 return prod;
10802 }
10803
10804 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10805 * and collect the results.
10806 * The result live in the space obtained by calling "space_product"
10807 * on the spaces of "map1" and "map2".
10808 * If "remove_duplicates" is set then the result may contain duplicates
10809 * (even if the inputs do not) and so we try and remove the obvious
10810 * duplicates.
10811 */
map_product(__isl_take isl_map * map1,__isl_take isl_map * map2,__isl_give isl_space * (* space_product)(__isl_take isl_space * left,__isl_take isl_space * right),__isl_give isl_basic_map * (* basic_map_product)(__isl_take isl_basic_map * left,__isl_take isl_basic_map * right),int remove_duplicates)10812 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10813 __isl_take isl_map *map2,
10814 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10815 __isl_take isl_space *right),
10816 __isl_give isl_basic_map *(*basic_map_product)(
10817 __isl_take isl_basic_map *left,
10818 __isl_take isl_basic_map *right),
10819 int remove_duplicates)
10820 {
10821 unsigned flags = 0;
10822 struct isl_map *result;
10823 int i, j;
10824 isl_bool m;
10825
10826 m = isl_map_has_equal_params(map1, map2);
10827 if (m < 0)
10828 goto error;
10829 if (!m)
10830 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10831 "parameters don't match", goto error);
10832
10833 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10834 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10835 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10836
10837 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10838 isl_space_copy(map2->dim)),
10839 map1->n * map2->n, flags);
10840 if (!result)
10841 goto error;
10842 for (i = 0; i < map1->n; ++i)
10843 for (j = 0; j < map2->n; ++j) {
10844 struct isl_basic_map *part;
10845 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10846 isl_basic_map_copy(map2->p[j]));
10847 if (isl_basic_map_is_empty(part))
10848 isl_basic_map_free(part);
10849 else
10850 result = isl_map_add_basic_map(result, part);
10851 if (!result)
10852 goto error;
10853 }
10854 if (remove_duplicates)
10855 result = isl_map_remove_obvious_duplicates(result);
10856 isl_map_free(map1);
10857 isl_map_free(map2);
10858 return result;
10859 error:
10860 isl_map_free(map1);
10861 isl_map_free(map2);
10862 return NULL;
10863 }
10864
10865 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10866 */
isl_map_product(__isl_take isl_map * map1,__isl_take isl_map * map2)10867 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10868 __isl_take isl_map *map2)
10869 {
10870 isl_map_align_params_bin(&map1, &map2);
10871 return map_product(map1, map2, &isl_space_product,
10872 &isl_basic_map_product, 0);
10873 }
10874
10875 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10876 */
isl_map_flat_product(__isl_take isl_map * map1,__isl_take isl_map * map2)10877 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10878 __isl_take isl_map *map2)
10879 {
10880 isl_map *prod;
10881
10882 prod = isl_map_product(map1, map2);
10883 prod = isl_map_flatten(prod);
10884 return prod;
10885 }
10886
10887 /* Given two set A and B, construct its Cartesian product A x B.
10888 */
isl_set_product(__isl_take isl_set * set1,__isl_take isl_set * set2)10889 __isl_give isl_set *isl_set_product(__isl_take isl_set *set1,
10890 __isl_take isl_set *set2)
10891 {
10892 return isl_map_range_product(set1, set2);
10893 }
10894
isl_set_flat_product(__isl_take isl_set * set1,__isl_take isl_set * set2)10895 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10896 __isl_take isl_set *set2)
10897 {
10898 return isl_map_flat_range_product(set1, set2);
10899 }
10900
10901 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10902 */
isl_map_domain_product(__isl_take isl_map * map1,__isl_take isl_map * map2)10903 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10904 __isl_take isl_map *map2)
10905 {
10906 isl_map_align_params_bin(&map1, &map2);
10907 return map_product(map1, map2, &isl_space_domain_product,
10908 &isl_basic_map_domain_product, 1);
10909 }
10910
10911 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10912 */
isl_map_range_product(__isl_take isl_map * map1,__isl_take isl_map * map2)10913 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10914 __isl_take isl_map *map2)
10915 {
10916 isl_map_align_params_bin(&map1, &map2);
10917 return map_product(map1, map2, &isl_space_range_product,
10918 &isl_basic_map_range_product, 1);
10919 }
10920
10921 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10922 */
isl_map_factor_domain(__isl_take isl_map * map)10923 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10924 {
10925 isl_space *space;
10926 isl_size total1, keep1, total2, keep2;
10927
10928 total1 = isl_map_dim(map, isl_dim_in);
10929 total2 = isl_map_dim(map, isl_dim_out);
10930 if (total1 < 0 || total2 < 0)
10931 return isl_map_free(map);
10932 if (!isl_space_domain_is_wrapping(map->dim) ||
10933 !isl_space_range_is_wrapping(map->dim))
10934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10935 "not a product", return isl_map_free(map));
10936
10937 space = isl_map_get_space(map);
10938 space = isl_space_factor_domain(space);
10939 keep1 = isl_space_dim(space, isl_dim_in);
10940 keep2 = isl_space_dim(space, isl_dim_out);
10941 if (keep1 < 0 || keep2 < 0)
10942 map = isl_map_free(map);
10943 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10944 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10945 map = isl_map_reset_space(map, space);
10946
10947 return map;
10948 }
10949
10950 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10951 */
isl_map_factor_range(__isl_take isl_map * map)10952 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10953 {
10954 isl_space *space;
10955 isl_size total1, keep1, total2, keep2;
10956
10957 total1 = isl_map_dim(map, isl_dim_in);
10958 total2 = isl_map_dim(map, isl_dim_out);
10959 if (total1 < 0 || total2 < 0)
10960 return isl_map_free(map);
10961 if (!isl_space_domain_is_wrapping(map->dim) ||
10962 !isl_space_range_is_wrapping(map->dim))
10963 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10964 "not a product", return isl_map_free(map));
10965
10966 space = isl_map_get_space(map);
10967 space = isl_space_factor_range(space);
10968 keep1 = isl_space_dim(space, isl_dim_in);
10969 keep2 = isl_space_dim(space, isl_dim_out);
10970 if (keep1 < 0 || keep2 < 0)
10971 map = isl_map_free(map);
10972 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10973 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10974 map = isl_map_reset_space(map, space);
10975
10976 return map;
10977 }
10978
10979 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10980 */
isl_map_domain_factor_domain(__isl_take isl_map * map)10981 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10982 {
10983 isl_space *space;
10984 isl_size total, keep;
10985
10986 total = isl_map_dim(map, isl_dim_in);
10987 if (total < 0)
10988 return isl_map_free(map);
10989 if (!isl_space_domain_is_wrapping(map->dim))
10990 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10991 "domain is not a product", return isl_map_free(map));
10992
10993 space = isl_map_get_space(map);
10994 space = isl_space_domain_factor_domain(space);
10995 keep = isl_space_dim(space, isl_dim_in);
10996 if (keep < 0)
10997 map = isl_map_free(map);
10998 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10999 map = isl_map_reset_space(map, space);
11000
11001 return map;
11002 }
11003
11004 /* Given a map of the form [A -> B] -> C, return the map B -> C.
11005 */
isl_map_domain_factor_range(__isl_take isl_map * map)11006 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
11007 {
11008 isl_space *space;
11009 isl_size total, keep;
11010
11011 total = isl_map_dim(map, isl_dim_in);
11012 if (total < 0)
11013 return isl_map_free(map);
11014 if (!isl_space_domain_is_wrapping(map->dim))
11015 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11016 "domain is not a product", return isl_map_free(map));
11017
11018 space = isl_map_get_space(map);
11019 space = isl_space_domain_factor_range(space);
11020 keep = isl_space_dim(space, isl_dim_in);
11021 if (keep < 0)
11022 map = isl_map_free(map);
11023 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
11024 map = isl_map_reset_space(map, space);
11025
11026 return map;
11027 }
11028
11029 /* Given a map A -> [B -> C], extract the map A -> B.
11030 */
isl_map_range_factor_domain(__isl_take isl_map * map)11031 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
11032 {
11033 isl_space *space;
11034 isl_size total, keep;
11035
11036 total = isl_map_dim(map, isl_dim_out);
11037 if (total < 0)
11038 return isl_map_free(map);
11039 if (!isl_space_range_is_wrapping(map->dim))
11040 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11041 "range is not a product", return isl_map_free(map));
11042
11043 space = isl_map_get_space(map);
11044 space = isl_space_range_factor_domain(space);
11045 keep = isl_space_dim(space, isl_dim_out);
11046 if (keep < 0)
11047 map = isl_map_free(map);
11048 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
11049 map = isl_map_reset_space(map, space);
11050
11051 return map;
11052 }
11053
11054 /* Given a map A -> [B -> C], extract the map A -> C.
11055 */
isl_map_range_factor_range(__isl_take isl_map * map)11056 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
11057 {
11058 isl_space *space;
11059 isl_size total, keep;
11060
11061 total = isl_map_dim(map, isl_dim_out);
11062 if (total < 0)
11063 return isl_map_free(map);
11064 if (!isl_space_range_is_wrapping(map->dim))
11065 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11066 "range is not a product", return isl_map_free(map));
11067
11068 space = isl_map_get_space(map);
11069 space = isl_space_range_factor_range(space);
11070 keep = isl_space_dim(space, isl_dim_out);
11071 if (keep < 0)
11072 map = isl_map_free(map);
11073 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
11074 map = isl_map_reset_space(map, space);
11075
11076 return map;
11077 }
11078
11079 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
11080 */
isl_map_flat_domain_product(__isl_take isl_map * map1,__isl_take isl_map * map2)11081 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
11082 __isl_take isl_map *map2)
11083 {
11084 isl_map *prod;
11085
11086 prod = isl_map_domain_product(map1, map2);
11087 prod = isl_map_flatten_domain(prod);
11088 return prod;
11089 }
11090
11091 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
11092 */
isl_map_flat_range_product(__isl_take isl_map * map1,__isl_take isl_map * map2)11093 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
11094 __isl_take isl_map *map2)
11095 {
11096 isl_map *prod;
11097
11098 prod = isl_map_range_product(map1, map2);
11099 prod = isl_map_flatten_range(prod);
11100 return prod;
11101 }
11102
isl_basic_map_get_hash(__isl_keep isl_basic_map * bmap)11103 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
11104 {
11105 int i;
11106 uint32_t hash = isl_hash_init();
11107 isl_size total;
11108
11109 if (!bmap)
11110 return 0;
11111 bmap = isl_basic_map_copy(bmap);
11112 bmap = isl_basic_map_normalize(bmap);
11113 total = isl_basic_map_dim(bmap, isl_dim_all);
11114 if (total < 0)
11115 return 0;
11116 isl_hash_byte(hash, bmap->n_eq & 0xFF);
11117 for (i = 0; i < bmap->n_eq; ++i) {
11118 uint32_t c_hash;
11119 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
11120 isl_hash_hash(hash, c_hash);
11121 }
11122 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
11123 for (i = 0; i < bmap->n_ineq; ++i) {
11124 uint32_t c_hash;
11125 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
11126 isl_hash_hash(hash, c_hash);
11127 }
11128 isl_hash_byte(hash, bmap->n_div & 0xFF);
11129 for (i = 0; i < bmap->n_div; ++i) {
11130 uint32_t c_hash;
11131 if (isl_int_is_zero(bmap->div[i][0]))
11132 continue;
11133 isl_hash_byte(hash, i & 0xFF);
11134 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
11135 isl_hash_hash(hash, c_hash);
11136 }
11137 isl_basic_map_free(bmap);
11138 return hash;
11139 }
11140
isl_basic_set_get_hash(__isl_keep isl_basic_set * bset)11141 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
11142 {
11143 return isl_basic_map_get_hash(bset_to_bmap(bset));
11144 }
11145
isl_map_get_hash(__isl_keep isl_map * map)11146 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
11147 {
11148 int i;
11149 uint32_t hash;
11150
11151 if (!map)
11152 return 0;
11153 map = isl_map_copy(map);
11154 map = isl_map_normalize(map);
11155 if (!map)
11156 return 0;
11157
11158 hash = isl_hash_init();
11159 for (i = 0; i < map->n; ++i) {
11160 uint32_t bmap_hash;
11161 bmap_hash = isl_basic_map_get_hash(map->p[i]);
11162 isl_hash_hash(hash, bmap_hash);
11163 }
11164
11165 isl_map_free(map);
11166
11167 return hash;
11168 }
11169
isl_set_get_hash(__isl_keep isl_set * set)11170 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
11171 {
11172 return isl_map_get_hash(set_to_map(set));
11173 }
11174
11175 /* Return the number of basic maps in the (current) representation of "map".
11176 */
isl_map_n_basic_map(__isl_keep isl_map * map)11177 isl_size isl_map_n_basic_map(__isl_keep isl_map *map)
11178 {
11179 return map ? map->n : isl_size_error;
11180 }
11181
isl_set_n_basic_set(__isl_keep isl_set * set)11182 isl_size isl_set_n_basic_set(__isl_keep isl_set *set)
11183 {
11184 return set ? set->n : isl_size_error;
11185 }
11186
isl_map_foreach_basic_map(__isl_keep isl_map * map,isl_stat (* fn)(__isl_take isl_basic_map * bmap,void * user),void * user)11187 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
11188 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
11189 {
11190 int i;
11191
11192 if (!map)
11193 return isl_stat_error;
11194
11195 for (i = 0; i < map->n; ++i)
11196 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
11197 return isl_stat_error;
11198
11199 return isl_stat_ok;
11200 }
11201
isl_set_foreach_basic_set(__isl_keep isl_set * set,isl_stat (* fn)(__isl_take isl_basic_set * bset,void * user),void * user)11202 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
11203 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
11204 {
11205 int i;
11206
11207 if (!set)
11208 return isl_stat_error;
11209
11210 for (i = 0; i < set->n; ++i)
11211 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
11212 return isl_stat_error;
11213
11214 return isl_stat_ok;
11215 }
11216
11217 /* Does "test" succeed on every basic set in "set"?
11218 */
isl_set_every_basic_set(__isl_keep isl_set * set,isl_bool (* test)(__isl_keep isl_basic_set * bset,void * user),void * user)11219 isl_bool isl_set_every_basic_set(__isl_keep isl_set *set,
11220 isl_bool (*test)(__isl_keep isl_basic_set *bset, void *user),
11221 void *user)
11222 {
11223 int i;
11224
11225 if (!set)
11226 return isl_bool_error;
11227
11228 for (i = 0; i < set->n; ++i) {
11229 isl_bool r;
11230
11231 r = test(set->p[i], user);
11232 if (r < 0 || !r)
11233 return r;
11234 }
11235
11236 return isl_bool_true;
11237 }
11238
11239 /* Return a list of basic sets, the union of which is equal to "set".
11240 */
isl_set_get_basic_set_list(__isl_keep isl_set * set)11241 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
11242 __isl_keep isl_set *set)
11243 {
11244 int i;
11245 isl_basic_set_list *list;
11246
11247 if (!set)
11248 return NULL;
11249
11250 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
11251 for (i = 0; i < set->n; ++i) {
11252 isl_basic_set *bset;
11253
11254 bset = isl_basic_set_copy(set->p[i]);
11255 list = isl_basic_set_list_add(list, bset);
11256 }
11257
11258 return list;
11259 }
11260
isl_basic_set_lift(__isl_take isl_basic_set * bset)11261 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
11262 {
11263 isl_space *space;
11264
11265 if (!bset)
11266 return NULL;
11267
11268 bset = isl_basic_set_cow(bset);
11269 if (!bset)
11270 return NULL;
11271
11272 space = isl_basic_set_get_space(bset);
11273 space = isl_space_lift(space, bset->n_div);
11274 if (!space)
11275 goto error;
11276 isl_space_free(bset->dim);
11277 bset->dim = space;
11278 bset->extra -= bset->n_div;
11279 bset->n_div = 0;
11280
11281 bset = isl_basic_set_finalize(bset);
11282
11283 return bset;
11284 error:
11285 isl_basic_set_free(bset);
11286 return NULL;
11287 }
11288
isl_set_lift(__isl_take isl_set * set)11289 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
11290 {
11291 int i;
11292 isl_space *space;
11293 unsigned n_div;
11294
11295 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
11296
11297 if (!set)
11298 return NULL;
11299
11300 set = isl_set_cow(set);
11301 if (!set)
11302 return NULL;
11303
11304 n_div = set->p[0]->n_div;
11305 space = isl_set_get_space(set);
11306 space = isl_space_lift(space, n_div);
11307 if (!space)
11308 goto error;
11309 isl_space_free(set->dim);
11310 set->dim = space;
11311
11312 for (i = 0; i < set->n; ++i) {
11313 set->p[i] = isl_basic_set_lift(set->p[i]);
11314 if (!set->p[i])
11315 goto error;
11316 }
11317
11318 return set;
11319 error:
11320 isl_set_free(set);
11321 return NULL;
11322 }
11323
isl_basic_set_size(__isl_keep isl_basic_set * bset)11324 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
11325 {
11326 isl_size dim;
11327 int size = 0;
11328
11329 dim = isl_basic_set_dim(bset, isl_dim_all);
11330 if (dim < 0)
11331 return -1;
11332 size += bset->n_eq * (1 + dim);
11333 size += bset->n_ineq * (1 + dim);
11334 size += bset->n_div * (2 + dim);
11335
11336 return size;
11337 }
11338
isl_set_size(__isl_keep isl_set * set)11339 int isl_set_size(__isl_keep isl_set *set)
11340 {
11341 int i;
11342 int size = 0;
11343
11344 if (!set)
11345 return -1;
11346
11347 for (i = 0; i < set->n; ++i)
11348 size += isl_basic_set_size(set->p[i]);
11349
11350 return size;
11351 }
11352
11353 /* Check if there is any lower bound (if lower == 0) and/or upper
11354 * bound (if upper == 0) on the specified dim.
11355 */
basic_map_dim_is_bounded(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos,int lower,int upper)11356 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11357 enum isl_dim_type type, unsigned pos, int lower, int upper)
11358 {
11359 int i;
11360
11361 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
11362 return isl_bool_error;
11363
11364 pos += isl_basic_map_offset(bmap, type);
11365
11366 for (i = 0; i < bmap->n_div; ++i) {
11367 if (isl_int_is_zero(bmap->div[i][0]))
11368 continue;
11369 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
11370 return isl_bool_true;
11371 }
11372
11373 for (i = 0; i < bmap->n_eq; ++i)
11374 if (!isl_int_is_zero(bmap->eq[i][pos]))
11375 return isl_bool_true;
11376
11377 for (i = 0; i < bmap->n_ineq; ++i) {
11378 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
11379 if (sgn > 0)
11380 lower = 1;
11381 if (sgn < 0)
11382 upper = 1;
11383 }
11384
11385 return lower && upper;
11386 }
11387
isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)11388 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
11389 enum isl_dim_type type, unsigned pos)
11390 {
11391 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
11392 }
11393
isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)11394 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
11395 enum isl_dim_type type, unsigned pos)
11396 {
11397 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
11398 }
11399
isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos)11400 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
11401 enum isl_dim_type type, unsigned pos)
11402 {
11403 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
11404 }
11405
isl_map_dim_is_bounded(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos)11406 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
11407 enum isl_dim_type type, unsigned pos)
11408 {
11409 int i;
11410
11411 if (!map)
11412 return isl_bool_error;
11413
11414 for (i = 0; i < map->n; ++i) {
11415 isl_bool bounded;
11416 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
11417 if (bounded < 0 || !bounded)
11418 return bounded;
11419 }
11420
11421 return isl_bool_true;
11422 }
11423
11424 /* Return true if the specified dim is involved in both an upper bound
11425 * and a lower bound.
11426 */
isl_set_dim_is_bounded(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)11427 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
11428 enum isl_dim_type type, unsigned pos)
11429 {
11430 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
11431 }
11432
11433 /* Does "map" have a bound (according to "fn") for any of its basic maps?
11434 */
has_any_bound(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos,isl_bool (* fn)(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos))11435 static isl_bool has_any_bound(__isl_keep isl_map *map,
11436 enum isl_dim_type type, unsigned pos,
11437 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11438 enum isl_dim_type type, unsigned pos))
11439 {
11440 int i;
11441
11442 if (!map)
11443 return isl_bool_error;
11444
11445 for (i = 0; i < map->n; ++i) {
11446 isl_bool bounded;
11447 bounded = fn(map->p[i], type, pos);
11448 if (bounded < 0 || bounded)
11449 return bounded;
11450 }
11451
11452 return isl_bool_false;
11453 }
11454
11455 /* Return 1 if the specified dim is involved in any lower bound.
11456 */
isl_set_dim_has_any_lower_bound(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)11457 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
11458 enum isl_dim_type type, unsigned pos)
11459 {
11460 return has_any_bound(set, type, pos,
11461 &isl_basic_map_dim_has_lower_bound);
11462 }
11463
11464 /* Return 1 if the specified dim is involved in any upper bound.
11465 */
isl_set_dim_has_any_upper_bound(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)11466 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
11467 enum isl_dim_type type, unsigned pos)
11468 {
11469 return has_any_bound(set, type, pos,
11470 &isl_basic_map_dim_has_upper_bound);
11471 }
11472
11473 /* Does "map" have a bound (according to "fn") for all of its basic maps?
11474 */
has_bound(__isl_keep isl_map * map,enum isl_dim_type type,unsigned pos,isl_bool (* fn)(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,unsigned pos))11475 static isl_bool has_bound(__isl_keep isl_map *map,
11476 enum isl_dim_type type, unsigned pos,
11477 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
11478 enum isl_dim_type type, unsigned pos))
11479 {
11480 int i;
11481
11482 if (!map)
11483 return isl_bool_error;
11484
11485 for (i = 0; i < map->n; ++i) {
11486 isl_bool bounded;
11487 bounded = fn(map->p[i], type, pos);
11488 if (bounded < 0 || !bounded)
11489 return bounded;
11490 }
11491
11492 return isl_bool_true;
11493 }
11494
11495 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
11496 */
isl_set_dim_has_lower_bound(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)11497 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
11498 enum isl_dim_type type, unsigned pos)
11499 {
11500 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
11501 }
11502
11503 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
11504 */
isl_set_dim_has_upper_bound(__isl_keep isl_set * set,enum isl_dim_type type,unsigned pos)11505 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
11506 enum isl_dim_type type, unsigned pos)
11507 {
11508 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
11509 }
11510
11511 /* For each of the "n" variables starting at "first", determine
11512 * the sign of the variable and put the results in the first "n"
11513 * elements of the array "signs".
11514 * Sign
11515 * 1 means that the variable is non-negative
11516 * -1 means that the variable is non-positive
11517 * 0 means the variable attains both positive and negative values.
11518 */
isl_basic_set_vars_get_sign(__isl_keep isl_basic_set * bset,unsigned first,unsigned n,int * signs)11519 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
11520 unsigned first, unsigned n, int *signs)
11521 {
11522 isl_vec *bound = NULL;
11523 struct isl_tab *tab = NULL;
11524 struct isl_tab_undo *snap;
11525 int i;
11526 isl_size total;
11527
11528 total = isl_basic_set_dim(bset, isl_dim_all);
11529 if (total < 0 || !signs)
11530 return isl_stat_error;
11531
11532 bound = isl_vec_alloc(bset->ctx, 1 + total);
11533 tab = isl_tab_from_basic_set(bset, 0);
11534 if (!bound || !tab)
11535 goto error;
11536
11537 isl_seq_clr(bound->el, bound->size);
11538 isl_int_set_si(bound->el[0], -1);
11539
11540 snap = isl_tab_snap(tab);
11541 for (i = 0; i < n; ++i) {
11542 int empty;
11543
11544 isl_int_set_si(bound->el[1 + first + i], -1);
11545 if (isl_tab_add_ineq(tab, bound->el) < 0)
11546 goto error;
11547 empty = tab->empty;
11548 isl_int_set_si(bound->el[1 + first + i], 0);
11549 if (isl_tab_rollback(tab, snap) < 0)
11550 goto error;
11551
11552 if (empty) {
11553 signs[i] = 1;
11554 continue;
11555 }
11556
11557 isl_int_set_si(bound->el[1 + first + i], 1);
11558 if (isl_tab_add_ineq(tab, bound->el) < 0)
11559 goto error;
11560 empty = tab->empty;
11561 isl_int_set_si(bound->el[1 + first + i], 0);
11562 if (isl_tab_rollback(tab, snap) < 0)
11563 goto error;
11564
11565 signs[i] = empty ? -1 : 0;
11566 }
11567
11568 isl_tab_free(tab);
11569 isl_vec_free(bound);
11570 return isl_stat_ok;
11571 error:
11572 isl_tab_free(tab);
11573 isl_vec_free(bound);
11574 return isl_stat_error;
11575 }
11576
isl_basic_set_dims_get_sign(__isl_keep isl_basic_set * bset,enum isl_dim_type type,unsigned first,unsigned n,int * signs)11577 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
11578 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
11579 {
11580 if (!bset || !signs)
11581 return isl_stat_error;
11582 if (isl_basic_set_check_range(bset, type, first, n) < 0)
11583 return isl_stat_error;
11584
11585 first += pos(bset->dim, type) - 1;
11586 return isl_basic_set_vars_get_sign(bset, first, n, signs);
11587 }
11588
11589 /* Is it possible for the integer division "div" to depend (possibly
11590 * indirectly) on any output dimensions?
11591 *
11592 * If the div is undefined, then we conservatively assume that it
11593 * may depend on them.
11594 * Otherwise, we check if it actually depends on them or on any integer
11595 * divisions that may depend on them.
11596 */
div_may_involve_output(__isl_keep isl_basic_map * bmap,int div)11597 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
11598 {
11599 int i;
11600 isl_size n_out, n_div;
11601 unsigned o_out, o_div;
11602
11603 if (isl_int_is_zero(bmap->div[div][0]))
11604 return isl_bool_true;
11605
11606 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11607 if (n_out < 0)
11608 return isl_bool_error;
11609 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11610
11611 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
11612 return isl_bool_true;
11613
11614 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11615 if (n_div < 0)
11616 return isl_bool_error;
11617 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11618
11619 for (i = 0; i < n_div; ++i) {
11620 isl_bool may_involve;
11621
11622 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
11623 continue;
11624 may_involve = div_may_involve_output(bmap, i);
11625 if (may_involve < 0 || may_involve)
11626 return may_involve;
11627 }
11628
11629 return isl_bool_false;
11630 }
11631
11632 /* Return the first integer division of "bmap" in the range
11633 * [first, first + n[ that may depend on any output dimensions and
11634 * that has a non-zero coefficient in "c" (where the first coefficient
11635 * in "c" corresponds to integer division "first").
11636 */
first_div_may_involve_output(__isl_keep isl_basic_map * bmap,isl_int * c,int first,int n)11637 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
11638 isl_int *c, int first, int n)
11639 {
11640 int k;
11641
11642 if (!bmap)
11643 return -1;
11644
11645 for (k = first; k < first + n; ++k) {
11646 isl_bool may_involve;
11647
11648 if (isl_int_is_zero(c[k]))
11649 continue;
11650 may_involve = div_may_involve_output(bmap, k);
11651 if (may_involve < 0)
11652 return -1;
11653 if (may_involve)
11654 return k;
11655 }
11656
11657 return first + n;
11658 }
11659
11660 /* Look for a pair of inequality constraints in "bmap" of the form
11661 *
11662 * -l + i >= 0 or i >= l
11663 * and
11664 * n + l - i >= 0 or i <= l + n
11665 *
11666 * with n < "m" and i the output dimension at position "pos".
11667 * (Note that n >= 0 as otherwise the two constraints would conflict.)
11668 * Furthermore, "l" is only allowed to involve parameters, input dimensions
11669 * and earlier output dimensions, as well as integer divisions that do
11670 * not involve any of the output dimensions.
11671 *
11672 * Return the index of the first inequality constraint or bmap->n_ineq
11673 * if no such pair can be found.
11674 */
find_modulo_constraint_pair(__isl_keep isl_basic_map * bmap,int pos,isl_int m)11675 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
11676 int pos, isl_int m)
11677 {
11678 int i, j;
11679 isl_ctx *ctx;
11680 isl_size total;
11681 isl_size n_div, n_out;
11682 unsigned o_div, o_out;
11683 int less;
11684
11685 total = isl_basic_map_dim(bmap, isl_dim_all);
11686 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11687 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11688 if (total < 0 || n_out < 0 || n_div < 0)
11689 return -1;
11690
11691 ctx = isl_basic_map_get_ctx(bmap);
11692 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11693 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11694 for (i = 0; i < bmap->n_ineq; ++i) {
11695 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
11696 continue;
11697 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
11698 n_out - (pos + 1)) != -1)
11699 continue;
11700 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
11701 0, n_div) < n_div)
11702 continue;
11703 for (j = i + 1; j < bmap->n_ineq; ++j) {
11704 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
11705 ctx->one))
11706 continue;
11707 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11708 bmap->ineq[j] + 1, total))
11709 continue;
11710 break;
11711 }
11712 if (j >= bmap->n_ineq)
11713 continue;
11714 isl_int_add(bmap->ineq[i][0],
11715 bmap->ineq[i][0], bmap->ineq[j][0]);
11716 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11717 isl_int_sub(bmap->ineq[i][0],
11718 bmap->ineq[i][0], bmap->ineq[j][0]);
11719 if (!less)
11720 continue;
11721 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11722 return i;
11723 else
11724 return j;
11725 }
11726
11727 return bmap->n_ineq;
11728 }
11729
11730 /* Return the index of the equality of "bmap" that defines
11731 * the output dimension "pos" in terms of earlier dimensions.
11732 * The equality may also involve integer divisions, as long
11733 * as those integer divisions are defined in terms of
11734 * parameters or input dimensions.
11735 * In this case, *div is set to the number of integer divisions and
11736 * *ineq is set to the number of inequality constraints (provided
11737 * div and ineq are not NULL).
11738 *
11739 * The equality may also involve a single integer division involving
11740 * the output dimensions (typically only output dimension "pos") as
11741 * long as the coefficient of output dimension "pos" is 1 or -1 and
11742 * there is a pair of constraints i >= l and i <= l + n, with i referring
11743 * to output dimension "pos", l an expression involving only earlier
11744 * dimensions and n smaller than the coefficient of the integer division
11745 * in the equality. In this case, the output dimension can be defined
11746 * in terms of a modulo expression that does not involve the integer division.
11747 * *div is then set to this single integer division and
11748 * *ineq is set to the index of constraint i >= l.
11749 *
11750 * Return bmap->n_eq if there is no such equality.
11751 * Return -1 on error.
11752 */
isl_basic_map_output_defining_equality(__isl_keep isl_basic_map * bmap,int pos,int * div,int * ineq)11753 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11754 int pos, int *div, int *ineq)
11755 {
11756 int j, k, l;
11757 isl_size n_div, n_out;
11758 unsigned o_div, o_out;
11759
11760 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11761 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11762 if (n_out < 0 || n_div < 0)
11763 return -1;
11764
11765 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11766 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11767
11768 if (ineq)
11769 *ineq = bmap->n_ineq;
11770 if (div)
11771 *div = n_div;
11772 for (j = 0; j < bmap->n_eq; ++j) {
11773 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11774 continue;
11775 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11776 n_out - (pos + 1)) != -1)
11777 continue;
11778 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11779 0, n_div);
11780 if (k >= n_div)
11781 return j;
11782 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11783 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11784 continue;
11785 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11786 k + 1, n_div - (k+1)) < n_div)
11787 continue;
11788 l = find_modulo_constraint_pair(bmap, pos,
11789 bmap->eq[j][o_div + k]);
11790 if (l < 0)
11791 return -1;
11792 if (l >= bmap->n_ineq)
11793 continue;
11794 if (div)
11795 *div = k;
11796 if (ineq)
11797 *ineq = l;
11798 return j;
11799 }
11800
11801 return bmap->n_eq;
11802 }
11803
11804 /* Check if the given basic map is obviously single-valued.
11805 * In particular, for each output dimension, check that there is
11806 * an equality that defines the output dimension in terms of
11807 * earlier dimensions.
11808 */
isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map * bmap)11809 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11810 {
11811 int i;
11812 isl_size n_out;
11813
11814 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11815 if (n_out < 0)
11816 return isl_bool_error;
11817
11818 for (i = 0; i < n_out; ++i) {
11819 int eq;
11820
11821 eq = isl_basic_map_output_defining_equality(bmap, i,
11822 NULL, NULL);
11823 if (eq < 0)
11824 return isl_bool_error;
11825 if (eq >= bmap->n_eq)
11826 return isl_bool_false;
11827 }
11828
11829 return isl_bool_true;
11830 }
11831
11832 /* Check if the given basic map is single-valued.
11833 * We simply compute
11834 *
11835 * M \circ M^-1
11836 *
11837 * and check if the result is a subset of the identity mapping.
11838 */
isl_basic_map_is_single_valued(__isl_keep isl_basic_map * bmap)11839 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11840 {
11841 isl_space *space;
11842 isl_basic_map *test;
11843 isl_basic_map *id;
11844 isl_bool sv;
11845
11846 sv = isl_basic_map_plain_is_single_valued(bmap);
11847 if (sv < 0 || sv)
11848 return sv;
11849
11850 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11851 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11852
11853 space = isl_basic_map_get_space(bmap);
11854 space = isl_space_map_from_set(isl_space_range(space));
11855 id = isl_basic_map_identity(space);
11856
11857 sv = isl_basic_map_is_subset(test, id);
11858
11859 isl_basic_map_free(test);
11860 isl_basic_map_free(id);
11861
11862 return sv;
11863 }
11864
11865 /* Check if the given map is obviously single-valued.
11866 */
isl_map_plain_is_single_valued(__isl_keep isl_map * map)11867 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11868 {
11869 if (!map)
11870 return isl_bool_error;
11871 if (map->n == 0)
11872 return isl_bool_true;
11873 if (map->n >= 2)
11874 return isl_bool_false;
11875
11876 return isl_basic_map_plain_is_single_valued(map->p[0]);
11877 }
11878
11879 /* Check if the given map is single-valued.
11880 * We simply compute
11881 *
11882 * M \circ M^-1
11883 *
11884 * and check if the result is a subset of the identity mapping.
11885 */
isl_map_is_single_valued(__isl_keep isl_map * map)11886 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11887 {
11888 isl_space *space;
11889 isl_map *test;
11890 isl_map *id;
11891 isl_bool sv;
11892
11893 sv = isl_map_plain_is_single_valued(map);
11894 if (sv < 0 || sv)
11895 return sv;
11896
11897 test = isl_map_reverse(isl_map_copy(map));
11898 test = isl_map_apply_range(test, isl_map_copy(map));
11899
11900 space = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11901 id = isl_map_identity(space);
11902
11903 sv = isl_map_is_subset(test, id);
11904
11905 isl_map_free(test);
11906 isl_map_free(id);
11907
11908 return sv;
11909 }
11910
isl_map_is_injective(__isl_keep isl_map * map)11911 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11912 {
11913 isl_bool in;
11914
11915 map = isl_map_copy(map);
11916 map = isl_map_reverse(map);
11917 in = isl_map_is_single_valued(map);
11918 isl_map_free(map);
11919
11920 return in;
11921 }
11922
11923 /* Check if the given map is obviously injective.
11924 */
isl_map_plain_is_injective(__isl_keep isl_map * map)11925 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11926 {
11927 isl_bool in;
11928
11929 map = isl_map_copy(map);
11930 map = isl_map_reverse(map);
11931 in = isl_map_plain_is_single_valued(map);
11932 isl_map_free(map);
11933
11934 return in;
11935 }
11936
isl_map_is_bijective(__isl_keep isl_map * map)11937 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11938 {
11939 isl_bool sv;
11940
11941 sv = isl_map_is_single_valued(map);
11942 if (sv < 0 || !sv)
11943 return sv;
11944
11945 return isl_map_is_injective(map);
11946 }
11947
isl_set_is_singleton(__isl_keep isl_set * set)11948 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11949 {
11950 return isl_map_is_single_valued(set_to_map(set));
11951 }
11952
11953 /* Does "map" only map elements to themselves?
11954 *
11955 * If the domain and range spaces are different, then "map"
11956 * is considered not to be an identity relation, even if it is empty.
11957 * Otherwise, construct the maximal identity relation and
11958 * check whether "map" is a subset of this relation.
11959 */
isl_map_is_identity(__isl_keep isl_map * map)11960 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11961 {
11962 isl_map *id;
11963 isl_bool equal, is_identity;
11964
11965 equal = isl_map_tuple_is_equal(map, isl_dim_in, map, isl_dim_out);
11966 if (equal < 0 || !equal)
11967 return equal;
11968
11969 id = isl_map_identity(isl_map_get_space(map));
11970 is_identity = isl_map_is_subset(map, id);
11971 isl_map_free(id);
11972
11973 return is_identity;
11974 }
11975
isl_map_is_translation(__isl_keep isl_map * map)11976 int isl_map_is_translation(__isl_keep isl_map *map)
11977 {
11978 int ok;
11979 isl_set *delta;
11980
11981 delta = isl_map_deltas(isl_map_copy(map));
11982 ok = isl_set_is_singleton(delta);
11983 isl_set_free(delta);
11984
11985 return ok;
11986 }
11987
unique(isl_int * p,unsigned pos,unsigned len)11988 static int unique(isl_int *p, unsigned pos, unsigned len)
11989 {
11990 if (isl_seq_first_non_zero(p, pos) != -1)
11991 return 0;
11992 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11993 return 0;
11994 return 1;
11995 }
11996
isl_basic_set_is_box(__isl_keep isl_basic_set * bset)11997 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11998 {
11999 int i, j;
12000 isl_size nvar, n_div;
12001 unsigned ovar;
12002
12003 n_div = isl_basic_set_dim(bset, isl_dim_div);
12004 if (n_div < 0)
12005 return isl_bool_error;
12006 if (n_div != 0)
12007 return isl_bool_false;
12008
12009 nvar = isl_basic_set_dim(bset, isl_dim_set);
12010 if (nvar < 0)
12011 return isl_bool_error;
12012 ovar = isl_space_offset(bset->dim, isl_dim_set);
12013 for (j = 0; j < nvar; ++j) {
12014 int lower = 0, upper = 0;
12015 for (i = 0; i < bset->n_eq; ++i) {
12016 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
12017 continue;
12018 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
12019 return isl_bool_false;
12020 break;
12021 }
12022 if (i < bset->n_eq)
12023 continue;
12024 for (i = 0; i < bset->n_ineq; ++i) {
12025 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
12026 continue;
12027 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
12028 return isl_bool_false;
12029 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
12030 lower = 1;
12031 else
12032 upper = 1;
12033 }
12034 if (!lower || !upper)
12035 return isl_bool_false;
12036 }
12037
12038 return isl_bool_true;
12039 }
12040
isl_set_is_box(__isl_keep isl_set * set)12041 isl_bool isl_set_is_box(__isl_keep isl_set *set)
12042 {
12043 if (!set)
12044 return isl_bool_error;
12045 if (set->n != 1)
12046 return isl_bool_false;
12047
12048 return isl_basic_set_is_box(set->p[0]);
12049 }
12050
isl_basic_set_is_wrapping(__isl_keep isl_basic_set * bset)12051 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
12052 {
12053 if (!bset)
12054 return isl_bool_error;
12055
12056 return isl_space_is_wrapping(bset->dim);
12057 }
12058
isl_set_is_wrapping(__isl_keep isl_set * set)12059 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
12060 {
12061 if (!set)
12062 return isl_bool_error;
12063
12064 return isl_space_is_wrapping(set->dim);
12065 }
12066
12067 /* Modify the space of "map" through a call to "change".
12068 * If "can_change" is set (not NULL), then first call it to check
12069 * if the modification is allowed, printing the error message "cannot_change"
12070 * if it is not.
12071 */
isl_map_change_space(__isl_take isl_map * map,isl_bool (* can_change)(__isl_keep isl_map * map),const char * cannot_change,__isl_give isl_space * (* change)(__isl_take isl_space * space))12072 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
12073 isl_bool (*can_change)(__isl_keep isl_map *map),
12074 const char *cannot_change,
12075 __isl_give isl_space *(*change)(__isl_take isl_space *space))
12076 {
12077 isl_bool ok;
12078 isl_space *space;
12079
12080 if (!map)
12081 return NULL;
12082
12083 ok = can_change ? can_change(map) : isl_bool_true;
12084 if (ok < 0)
12085 return isl_map_free(map);
12086 if (!ok)
12087 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
12088 return isl_map_free(map));
12089
12090 space = change(isl_map_get_space(map));
12091 map = isl_map_reset_space(map, space);
12092
12093 return map;
12094 }
12095
12096 /* Is the domain of "map" a wrapped relation?
12097 */
isl_map_domain_is_wrapping(__isl_keep isl_map * map)12098 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
12099 {
12100 if (!map)
12101 return isl_bool_error;
12102
12103 return isl_space_domain_is_wrapping(map->dim);
12104 }
12105
12106 /* Does "map" have a wrapped relation in both domain and range?
12107 */
isl_map_is_product(__isl_keep isl_map * map)12108 isl_bool isl_map_is_product(__isl_keep isl_map *map)
12109 {
12110 return isl_space_is_product(isl_map_peek_space(map));
12111 }
12112
12113 /* Is the range of "map" a wrapped relation?
12114 */
isl_map_range_is_wrapping(__isl_keep isl_map * map)12115 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
12116 {
12117 if (!map)
12118 return isl_bool_error;
12119
12120 return isl_space_range_is_wrapping(map->dim);
12121 }
12122
isl_basic_map_wrap(__isl_take isl_basic_map * bmap)12123 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
12124 {
12125 isl_space *space;
12126
12127 space = isl_basic_map_take_space(bmap);
12128 space = isl_space_wrap(space);
12129 bmap = isl_basic_map_restore_space(bmap, space);
12130
12131 bmap = isl_basic_map_finalize(bmap);
12132
12133 return bset_from_bmap(bmap);
12134 }
12135
12136 /* Given a map A -> B, return the set (A -> B).
12137 */
isl_map_wrap(__isl_take isl_map * map)12138 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
12139 {
12140 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
12141 }
12142
isl_basic_set_unwrap(__isl_take isl_basic_set * bset)12143 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
12144 {
12145 bset = isl_basic_set_cow(bset);
12146 if (!bset)
12147 return NULL;
12148
12149 bset->dim = isl_space_unwrap(bset->dim);
12150 if (!bset->dim)
12151 goto error;
12152
12153 bset = isl_basic_set_finalize(bset);
12154
12155 return bset_to_bmap(bset);
12156 error:
12157 isl_basic_set_free(bset);
12158 return NULL;
12159 }
12160
12161 /* Given a set (A -> B), return the map A -> B.
12162 * Error out if "set" is not of the form (A -> B).
12163 */
isl_set_unwrap(__isl_take isl_set * set)12164 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
12165 {
12166 return isl_map_change_space(set, &isl_set_is_wrapping,
12167 "not a wrapping set", &isl_space_unwrap);
12168 }
12169
isl_basic_map_reset(__isl_take isl_basic_map * bmap,enum isl_dim_type type)12170 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
12171 enum isl_dim_type type)
12172 {
12173 isl_space *space;
12174
12175 space = isl_basic_map_take_space(bmap);
12176 space = isl_space_reset(space, type);
12177 bmap = isl_basic_map_restore_space(bmap, space);
12178
12179 bmap = isl_basic_map_mark_final(bmap);
12180
12181 return bmap;
12182 }
12183
isl_map_reset(__isl_take isl_map * map,enum isl_dim_type type)12184 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
12185 enum isl_dim_type type)
12186 {
12187 int i;
12188 isl_space *space;
12189
12190 if (!map)
12191 return NULL;
12192
12193 if (!isl_space_is_named_or_nested(map->dim, type))
12194 return map;
12195
12196 map = isl_map_cow(map);
12197 if (!map)
12198 return NULL;
12199
12200 for (i = 0; i < map->n; ++i) {
12201 map->p[i] = isl_basic_map_reset(map->p[i], type);
12202 if (!map->p[i])
12203 goto error;
12204 }
12205
12206 space = isl_map_take_space(map);
12207 space = isl_space_reset(space, type);
12208 map = isl_map_restore_space(map, space);
12209
12210 return map;
12211 error:
12212 isl_map_free(map);
12213 return NULL;
12214 }
12215
isl_basic_map_flatten(__isl_take isl_basic_map * bmap)12216 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
12217 {
12218 isl_space *space;
12219
12220 space = isl_basic_map_take_space(bmap);
12221 space = isl_space_flatten(space);
12222 bmap = isl_basic_map_restore_space(bmap, space);
12223
12224 bmap = isl_basic_map_mark_final(bmap);
12225
12226 return bmap;
12227 }
12228
isl_basic_set_flatten(__isl_take isl_basic_set * bset)12229 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
12230 {
12231 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
12232 }
12233
isl_basic_map_flatten_domain(__isl_take isl_basic_map * bmap)12234 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
12235 __isl_take isl_basic_map *bmap)
12236 {
12237 isl_space *space;
12238
12239 space = isl_basic_map_take_space(bmap);
12240 space = isl_space_flatten_domain(space);
12241 bmap = isl_basic_map_restore_space(bmap, space);
12242
12243 bmap = isl_basic_map_mark_final(bmap);
12244
12245 return bmap;
12246 }
12247
isl_basic_map_flatten_range(__isl_take isl_basic_map * bmap)12248 __isl_give isl_basic_map *isl_basic_map_flatten_range(
12249 __isl_take isl_basic_map *bmap)
12250 {
12251 isl_space *space;
12252
12253 space = isl_basic_map_take_space(bmap);
12254 space = isl_space_flatten_range(space);
12255 bmap = isl_basic_map_restore_space(bmap, space);
12256
12257 bmap = isl_basic_map_mark_final(bmap);
12258
12259 return bmap;
12260 }
12261
12262 /* Remove any internal structure from the spaces of domain and range of "map".
12263 */
isl_map_flatten(__isl_take isl_map * map)12264 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
12265 {
12266 if (!map)
12267 return NULL;
12268
12269 if (!map->dim->nested[0] && !map->dim->nested[1])
12270 return map;
12271
12272 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
12273 }
12274
isl_set_flatten(__isl_take isl_set * set)12275 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
12276 {
12277 return set_from_map(isl_map_flatten(set_to_map(set)));
12278 }
12279
isl_set_flatten_map(__isl_take isl_set * set)12280 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
12281 {
12282 isl_space *space, *flat_space;
12283 isl_map *map;
12284
12285 space = isl_set_get_space(set);
12286 flat_space = isl_space_flatten(isl_space_copy(space));
12287 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
12288 flat_space));
12289 map = isl_map_intersect_domain(map, set);
12290
12291 return map;
12292 }
12293
12294 /* Remove any internal structure from the space of the domain of "map".
12295 */
isl_map_flatten_domain(__isl_take isl_map * map)12296 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
12297 {
12298 if (!map)
12299 return NULL;
12300
12301 if (!map->dim->nested[0])
12302 return map;
12303
12304 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
12305 }
12306
12307 /* Remove any internal structure from the space of the range of "map".
12308 */
isl_map_flatten_range(__isl_take isl_map * map)12309 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
12310 {
12311 if (!map)
12312 return NULL;
12313
12314 if (!map->dim->nested[1])
12315 return map;
12316
12317 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
12318 }
12319
12320 /* Reorder the dimensions of "bmap" according to the given dim_map
12321 * and set the dimension specification to "space" and
12322 * perform Gaussian elimination on the result.
12323 */
isl_basic_map_realign(__isl_take isl_basic_map * bmap,__isl_take isl_space * space,__isl_take struct isl_dim_map * dim_map)12324 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
12325 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
12326 {
12327 isl_basic_map *res;
12328 unsigned flags;
12329 isl_size n_div;
12330
12331 n_div = isl_basic_map_dim(bmap, isl_dim_div);
12332 if (n_div < 0 || !space || !dim_map)
12333 goto error;
12334
12335 flags = bmap->flags;
12336 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
12337 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
12338 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
12339 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
12340 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
12341 if (res)
12342 res->flags = flags;
12343 res = isl_basic_map_gauss(res, NULL);
12344 res = isl_basic_map_finalize(res);
12345 return res;
12346 error:
12347 isl_dim_map_free(dim_map);
12348 isl_basic_map_free(bmap);
12349 isl_space_free(space);
12350 return NULL;
12351 }
12352
12353 /* Reorder the dimensions of "map" according to given reordering.
12354 */
isl_map_realign(__isl_take isl_map * map,__isl_take isl_reordering * r)12355 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
12356 __isl_take isl_reordering *r)
12357 {
12358 int i;
12359 struct isl_dim_map *dim_map;
12360
12361 map = isl_map_cow(map);
12362 dim_map = isl_dim_map_from_reordering(r);
12363 if (!map || !r || !dim_map)
12364 goto error;
12365
12366 for (i = 0; i < map->n; ++i) {
12367 struct isl_dim_map *dim_map_i;
12368 isl_space *space;
12369
12370 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
12371
12372 space = isl_reordering_get_space(r);
12373 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
12374
12375 if (!map->p[i])
12376 goto error;
12377 }
12378
12379 map = isl_map_reset_space(map, isl_reordering_get_space(r));
12380 map = isl_map_unmark_normalized(map);
12381
12382 isl_reordering_free(r);
12383 isl_dim_map_free(dim_map);
12384 return map;
12385 error:
12386 isl_dim_map_free(dim_map);
12387 isl_map_free(map);
12388 isl_reordering_free(r);
12389 return NULL;
12390 }
12391
isl_set_realign(__isl_take isl_set * set,__isl_take isl_reordering * r)12392 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
12393 __isl_take isl_reordering *r)
12394 {
12395 return set_from_map(isl_map_realign(set_to_map(set), r));
12396 }
12397
isl_map_align_params(__isl_take isl_map * map,__isl_take isl_space * model)12398 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
12399 __isl_take isl_space *model)
12400 {
12401 isl_ctx *ctx;
12402 isl_bool aligned;
12403
12404 if (!map || !model)
12405 goto error;
12406
12407 ctx = isl_space_get_ctx(model);
12408 if (!isl_space_has_named_params(model))
12409 isl_die(ctx, isl_error_invalid,
12410 "model has unnamed parameters", goto error);
12411 if (isl_map_check_named_params(map) < 0)
12412 goto error;
12413 aligned = isl_map_space_has_equal_params(map, model);
12414 if (aligned < 0)
12415 goto error;
12416 if (!aligned) {
12417 isl_reordering *exp;
12418
12419 exp = isl_parameter_alignment_reordering(map->dim, model);
12420 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
12421 map = isl_map_realign(map, exp);
12422 }
12423
12424 isl_space_free(model);
12425 return map;
12426 error:
12427 isl_space_free(model);
12428 isl_map_free(map);
12429 return NULL;
12430 }
12431
isl_set_align_params(__isl_take isl_set * set,__isl_take isl_space * model)12432 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
12433 __isl_take isl_space *model)
12434 {
12435 return isl_map_align_params(set, model);
12436 }
12437
12438 /* Align the parameters of "bmap" to those of "model", introducing
12439 * additional parameters if needed.
12440 */
isl_basic_map_align_params(__isl_take isl_basic_map * bmap,__isl_take isl_space * model)12441 __isl_give isl_basic_map *isl_basic_map_align_params(
12442 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
12443 {
12444 isl_ctx *ctx;
12445 isl_bool equal_params;
12446
12447 if (!bmap || !model)
12448 goto error;
12449
12450 ctx = isl_space_get_ctx(model);
12451 if (!isl_space_has_named_params(model))
12452 isl_die(ctx, isl_error_invalid,
12453 "model has unnamed parameters", goto error);
12454 if (isl_basic_map_check_named_params(bmap) < 0)
12455 goto error;
12456 equal_params = isl_space_has_equal_params(bmap->dim, model);
12457 if (equal_params < 0)
12458 goto error;
12459 if (!equal_params) {
12460 isl_reordering *exp;
12461 struct isl_dim_map *dim_map;
12462
12463 exp = isl_parameter_alignment_reordering(bmap->dim, model);
12464 exp = isl_reordering_extend_space(exp,
12465 isl_basic_map_get_space(bmap));
12466 dim_map = isl_dim_map_from_reordering(exp);
12467 bmap = isl_basic_map_realign(bmap,
12468 isl_reordering_get_space(exp),
12469 isl_dim_map_extend(dim_map, bmap));
12470 isl_reordering_free(exp);
12471 isl_dim_map_free(dim_map);
12472 }
12473
12474 isl_space_free(model);
12475 return bmap;
12476 error:
12477 isl_space_free(model);
12478 isl_basic_map_free(bmap);
12479 return NULL;
12480 }
12481
12482 /* Do "bset" and "space" have the same parameters?
12483 */
isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set * bset,__isl_keep isl_space * space)12484 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
12485 __isl_keep isl_space *space)
12486 {
12487 isl_space *bset_space;
12488
12489 bset_space = isl_basic_set_peek_space(bset);
12490 return isl_space_has_equal_params(bset_space, space);
12491 }
12492
12493 /* Do "map" and "space" have the same parameters?
12494 */
isl_map_space_has_equal_params(__isl_keep isl_map * map,__isl_keep isl_space * space)12495 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
12496 __isl_keep isl_space *space)
12497 {
12498 isl_space *map_space;
12499
12500 map_space = isl_map_peek_space(map);
12501 return isl_space_has_equal_params(map_space, space);
12502 }
12503
12504 /* Do "set" and "space" have the same parameters?
12505 */
isl_set_space_has_equal_params(__isl_keep isl_set * set,__isl_keep isl_space * space)12506 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
12507 __isl_keep isl_space *space)
12508 {
12509 return isl_map_space_has_equal_params(set_to_map(set), space);
12510 }
12511
12512 /* Align the parameters of "bset" to those of "model", introducing
12513 * additional parameters if needed.
12514 */
isl_basic_set_align_params(__isl_take isl_basic_set * bset,__isl_take isl_space * model)12515 __isl_give isl_basic_set *isl_basic_set_align_params(
12516 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
12517 {
12518 return isl_basic_map_align_params(bset, model);
12519 }
12520
12521 /* Drop all parameters not referenced by "map".
12522 */
isl_map_drop_unused_params(__isl_take isl_map * map)12523 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
12524 {
12525 int i;
12526 isl_size n;
12527
12528 n = isl_map_dim(map, isl_dim_param);
12529 if (isl_map_check_named_params(map) < 0 || n < 0)
12530 return isl_map_free(map);
12531
12532 for (i = n - 1; i >= 0; i--) {
12533 isl_bool involves;
12534
12535 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
12536 if (involves < 0)
12537 return isl_map_free(map);
12538 if (!involves)
12539 map = isl_map_project_out(map, isl_dim_param, i, 1);
12540 }
12541
12542 return map;
12543 }
12544
12545 /* Drop all parameters not referenced by "set".
12546 */
isl_set_drop_unused_params(__isl_take isl_set * set)12547 __isl_give isl_set *isl_set_drop_unused_params(
12548 __isl_take isl_set *set)
12549 {
12550 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
12551 }
12552
12553 /* Drop all parameters not referenced by "bmap".
12554 */
isl_basic_map_drop_unused_params(__isl_take isl_basic_map * bmap)12555 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
12556 __isl_take isl_basic_map *bmap)
12557 {
12558 isl_size nparam;
12559 int i;
12560
12561 nparam = isl_basic_map_dim(bmap, isl_dim_param);
12562 if (nparam < 0 || isl_basic_map_check_named_params(bmap) < 0)
12563 return isl_basic_map_free(bmap);
12564
12565 for (i = nparam - 1; i >= 0; i--) {
12566 isl_bool involves;
12567
12568 involves = isl_basic_map_involves_dims(bmap,
12569 isl_dim_param, i, 1);
12570 if (involves < 0)
12571 return isl_basic_map_free(bmap);
12572 if (!involves)
12573 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
12574 }
12575
12576 return bmap;
12577 }
12578
12579 /* Drop all parameters not referenced by "bset".
12580 */
isl_basic_set_drop_unused_params(__isl_take isl_basic_set * bset)12581 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
12582 __isl_take isl_basic_set *bset)
12583 {
12584 return bset_from_bmap(isl_basic_map_drop_unused_params(
12585 bset_to_bmap(bset)));
12586 }
12587
12588 /* Given a tuple of identifiers "tuple" in a space that corresponds
12589 * to that of "set", if any of those identifiers appear as parameters
12590 * in "set", then equate those parameters with the corresponding
12591 * set dimensions and project out the parameters.
12592 * The result therefore has no such parameters.
12593 */
equate_params(__isl_take isl_set * set,__isl_keep isl_multi_id * tuple)12594 static __isl_give isl_set *equate_params(__isl_take isl_set *set,
12595 __isl_keep isl_multi_id *tuple)
12596 {
12597 int i;
12598 isl_size n;
12599 isl_space *set_space, *tuple_space;
12600
12601 set_space = isl_set_peek_space(set);
12602 tuple_space = isl_multi_id_peek_space(tuple);
12603 if (isl_space_check_equal_tuples(tuple_space, set_space) < 0)
12604 return isl_set_free(set);
12605 n = isl_multi_id_size(tuple);
12606 if (n < 0)
12607 return isl_set_free(set);
12608 for (i = 0; i < n; ++i) {
12609 isl_id *id;
12610 int pos;
12611
12612 id = isl_multi_id_get_at(tuple, i);
12613 if (!id)
12614 return isl_set_free(set);
12615 pos = isl_set_find_dim_by_id(set, isl_dim_param, id);
12616 isl_id_free(id);
12617 if (pos < 0)
12618 continue;
12619 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
12620 set = isl_set_project_out(set, isl_dim_param, pos, 1);
12621 }
12622 return set;
12623 }
12624
12625 /* Bind the set dimensions of "set" to parameters with identifiers
12626 * specified by "tuple", living in the same space as "set".
12627 *
12628 * If no parameters with these identifiers appear in "set" already,
12629 * then the set dimensions are simply reinterpreted as parameters.
12630 * Otherwise, the parameters are first equated to the corresponding
12631 * set dimensions.
12632 */
isl_set_bind(__isl_take isl_set * set,__isl_take isl_multi_id * tuple)12633 __isl_give isl_set *isl_set_bind(__isl_take isl_set *set,
12634 __isl_take isl_multi_id *tuple)
12635 {
12636 isl_space *space;
12637
12638 set = equate_params(set, tuple);
12639 space = isl_set_get_space(set);
12640 space = isl_space_bind_set(space, tuple);
12641 isl_multi_id_free(tuple);
12642 set = isl_set_reset_space(set, space);
12643
12644 return set;
12645 }
12646
12647 /* Given a tuple of identifiers "tuple" in a space that corresponds
12648 * to the domain of "map", if any of those identifiers appear as parameters
12649 * in "map", then equate those parameters with the corresponding
12650 * input dimensions and project out the parameters.
12651 * The result therefore has no such parameters.
12652 */
map_equate_params(__isl_take isl_map * map,__isl_keep isl_multi_id * tuple)12653 static __isl_give isl_map *map_equate_params(__isl_take isl_map *map,
12654 __isl_keep isl_multi_id *tuple)
12655 {
12656 int i;
12657 isl_size n;
12658 isl_space *map_space, *tuple_space;
12659
12660 map_space = isl_map_peek_space(map);
12661 tuple_space = isl_multi_id_peek_space(tuple);
12662 if (isl_space_check_domain_tuples(tuple_space, map_space) < 0)
12663 return isl_map_free(map);
12664 n = isl_multi_id_size(tuple);
12665 if (n < 0)
12666 return isl_map_free(map);
12667 for (i = 0; i < n; ++i) {
12668 isl_id *id;
12669 int pos;
12670
12671 id = isl_multi_id_get_at(tuple, i);
12672 if (!id)
12673 return isl_map_free(map);
12674 pos = isl_map_find_dim_by_id(map, isl_dim_param, id);
12675 isl_id_free(id);
12676 if (pos < 0)
12677 continue;
12678 map = isl_map_equate(map, isl_dim_param, pos, isl_dim_in, i);
12679 map = isl_map_project_out(map, isl_dim_param, pos, 1);
12680 }
12681 return map;
12682 }
12683
12684 /* Bind the input dimensions of "map" to parameters with identifiers
12685 * specified by "tuple", living in the domain space of "map".
12686 *
12687 * If no parameters with these identifiers appear in "map" already,
12688 * then the input dimensions are simply reinterpreted as parameters.
12689 * Otherwise, the parameters are first equated to the corresponding
12690 * input dimensions.
12691 */
isl_map_bind_domain(__isl_take isl_map * map,__isl_take isl_multi_id * tuple)12692 __isl_give isl_set *isl_map_bind_domain(__isl_take isl_map *map,
12693 __isl_take isl_multi_id *tuple)
12694 {
12695 isl_space *space;
12696 isl_set *set;
12697
12698 map = map_equate_params(map, tuple);
12699 space = isl_map_get_space(map);
12700 space = isl_space_bind_map_domain(space, tuple);
12701 isl_multi_id_free(tuple);
12702 set = set_from_map(isl_map_reset_space(map, space));
12703
12704 return set;
12705 }
12706
12707 /* Bind the output dimensions of "map" to parameters with identifiers
12708 * specified by "tuple", living in the range space of "map".
12709 *
12710 * Since binding is more easily implemented on the domain,
12711 * bind the input dimensions of the inverse of "map".
12712 */
isl_map_bind_range(__isl_take isl_map * map,__isl_take isl_multi_id * tuple)12713 __isl_give isl_set *isl_map_bind_range(__isl_take isl_map *map,
12714 __isl_take isl_multi_id *tuple)
12715 {
12716 return isl_map_bind_domain(isl_map_reverse(map), tuple);
12717 }
12718
12719 /* Insert a domain corresponding to "tuple"
12720 * into the nullary or unary relation "set".
12721 * The result has an extra initial tuple and is therefore
12722 * either a unary or binary relation.
12723 * Any parameters with identifiers in "tuple" are reinterpreted
12724 * as the corresponding domain dimensions.
12725 */
unbind_params_insert_domain(__isl_take isl_set * set,__isl_take isl_multi_id * tuple)12726 static __isl_give isl_map *unbind_params_insert_domain(
12727 __isl_take isl_set *set, __isl_take isl_multi_id *tuple)
12728 {
12729 isl_space *space;
12730 isl_reordering *r;
12731
12732 space = isl_set_peek_space(set);
12733 r = isl_reordering_unbind_params_insert_domain(space, tuple);
12734 isl_multi_id_free(tuple);
12735
12736 return isl_map_realign(set_to_map(set), r);
12737 }
12738
12739 /* Construct a set with "tuple" as domain from the parameter domain "set".
12740 * Any parameters with identifiers in "tuple" are reinterpreted
12741 * as the corresponding set dimensions.
12742 */
isl_set_unbind_params(__isl_take isl_set * set,__isl_take isl_multi_id * tuple)12743 __isl_give isl_set *isl_set_unbind_params(__isl_take isl_set *set,
12744 __isl_take isl_multi_id *tuple)
12745 {
12746 isl_bool is_params;
12747
12748 is_params = isl_set_is_params(set);
12749 if (is_params < 0)
12750 set = isl_set_free(set);
12751 else if (!is_params)
12752 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12753 "expecting parameter domain", set = isl_set_free(set));
12754 return set_from_map(unbind_params_insert_domain(set, tuple));
12755 }
12756
12757 /* Check that "set" is a proper set, i.e., that it is not a parameter domain.
12758 */
isl_set_check_is_set(__isl_keep isl_set * set)12759 static isl_stat isl_set_check_is_set(__isl_keep isl_set *set)
12760 {
12761 isl_bool is_params;
12762
12763 is_params = isl_set_is_params(set);
12764 if (is_params < 0)
12765 return isl_stat_error;
12766 else if (is_params)
12767 isl_die(isl_set_get_ctx(set), isl_error_invalid,
12768 "expecting proper set", return isl_stat_error);
12769
12770 return isl_stat_ok;
12771 }
12772
12773 /* Construct a map with "domain" as domain and "set" as range.
12774 * Any parameters with identifiers in "domain" are reinterpreted
12775 * as the corresponding domain dimensions.
12776 */
isl_set_unbind_params_insert_domain(__isl_take isl_set * set,__isl_take isl_multi_id * domain)12777 __isl_give isl_map *isl_set_unbind_params_insert_domain(
12778 __isl_take isl_set *set, __isl_take isl_multi_id *domain)
12779 {
12780 if (isl_set_check_is_set(set) < 0)
12781 set = isl_set_free(set);
12782 return unbind_params_insert_domain(set, domain);
12783 }
12784
12785 /* Construct a map with "domain" as domain and "set" as range.
12786 */
isl_set_insert_domain(__isl_take isl_set * set,__isl_take isl_space * domain)12787 __isl_give isl_map *isl_set_insert_domain(__isl_take isl_set *set,
12788 __isl_take isl_space *domain)
12789 {
12790 isl_size dim;
12791 isl_space *space;
12792 isl_map *map;
12793
12794 if (isl_set_check_is_set(set) < 0 || isl_space_check_is_set(domain) < 0)
12795 domain = isl_space_free(domain);
12796 dim = isl_space_dim(domain, isl_dim_set);
12797 if (dim < 0)
12798 domain = isl_space_free(domain);
12799 space = isl_set_get_space(set);
12800 domain = isl_space_replace_params(domain, space);
12801 space = isl_space_map_from_domain_and_range(domain, space);
12802
12803 map = isl_map_from_range(set);
12804 map = isl_map_add_dims(map, isl_dim_in, dim);
12805 map = isl_map_reset_space(map, space);
12806
12807 return map;
12808 }
12809
isl_basic_map_equalities_matrix(__isl_keep isl_basic_map * bmap,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4,enum isl_dim_type c5)12810 __isl_give isl_mat *isl_basic_map_equalities_matrix(
12811 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12812 enum isl_dim_type c2, enum isl_dim_type c3,
12813 enum isl_dim_type c4, enum isl_dim_type c5)
12814 {
12815 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12816 struct isl_mat *mat;
12817 int i, j, k;
12818 int pos;
12819 isl_size total;
12820
12821 total = isl_basic_map_dim(bmap, isl_dim_all);
12822 if (total < 0)
12823 return NULL;
12824 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq, total + 1);
12825 if (!mat)
12826 return NULL;
12827 for (i = 0; i < bmap->n_eq; ++i)
12828 for (j = 0, pos = 0; j < 5; ++j) {
12829 int off = isl_basic_map_offset(bmap, c[j]);
12830 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12831 if (dim < 0)
12832 return isl_mat_free(mat);
12833 for (k = 0; k < dim; ++k) {
12834 isl_int_set(mat->row[i][pos],
12835 bmap->eq[i][off + k]);
12836 ++pos;
12837 }
12838 }
12839
12840 return mat;
12841 }
12842
isl_basic_map_inequalities_matrix(__isl_keep isl_basic_map * bmap,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4,enum isl_dim_type c5)12843 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
12844 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
12845 enum isl_dim_type c2, enum isl_dim_type c3,
12846 enum isl_dim_type c4, enum isl_dim_type c5)
12847 {
12848 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12849 struct isl_mat *mat;
12850 int i, j, k;
12851 int pos;
12852 isl_size total;
12853
12854 total = isl_basic_map_dim(bmap, isl_dim_all);
12855 if (total < 0)
12856 return NULL;
12857 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq, total + 1);
12858 if (!mat)
12859 return NULL;
12860 for (i = 0; i < bmap->n_ineq; ++i)
12861 for (j = 0, pos = 0; j < 5; ++j) {
12862 int off = isl_basic_map_offset(bmap, c[j]);
12863 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12864 if (dim < 0)
12865 return isl_mat_free(mat);
12866 for (k = 0; k < dim; ++k) {
12867 isl_int_set(mat->row[i][pos],
12868 bmap->ineq[i][off + k]);
12869 ++pos;
12870 }
12871 }
12872
12873 return mat;
12874 }
12875
isl_basic_map_from_constraint_matrices(__isl_take isl_space * space,__isl_take isl_mat * eq,__isl_take isl_mat * ineq,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4,enum isl_dim_type c5)12876 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
12877 __isl_take isl_space *space,
12878 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12879 enum isl_dim_type c2, enum isl_dim_type c3,
12880 enum isl_dim_type c4, enum isl_dim_type c5)
12881 {
12882 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
12883 isl_basic_map *bmap = NULL;
12884 isl_size dim;
12885 unsigned total;
12886 unsigned extra;
12887 int i, j, k, l;
12888 int pos;
12889
12890 dim = isl_space_dim(space, isl_dim_all);
12891 if (dim < 0 || !eq || !ineq)
12892 goto error;
12893
12894 if (eq->n_col != ineq->n_col)
12895 isl_die(space->ctx, isl_error_invalid,
12896 "equalities and inequalities matrices should have "
12897 "same number of columns", goto error);
12898
12899 total = 1 + dim;
12900
12901 if (eq->n_col < total)
12902 isl_die(space->ctx, isl_error_invalid,
12903 "number of columns too small", goto error);
12904
12905 extra = eq->n_col - total;
12906
12907 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12908 eq->n_row, ineq->n_row);
12909 if (!bmap)
12910 goto error;
12911 for (i = 0; i < extra; ++i) {
12912 k = isl_basic_map_alloc_div(bmap);
12913 if (k < 0)
12914 goto error;
12915 isl_int_set_si(bmap->div[k][0], 0);
12916 }
12917 for (i = 0; i < eq->n_row; ++i) {
12918 l = isl_basic_map_alloc_equality(bmap);
12919 if (l < 0)
12920 goto error;
12921 for (j = 0, pos = 0; j < 5; ++j) {
12922 int off = isl_basic_map_offset(bmap, c[j]);
12923 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12924 if (dim < 0)
12925 goto error;
12926 for (k = 0; k < dim; ++k) {
12927 isl_int_set(bmap->eq[l][off + k],
12928 eq->row[i][pos]);
12929 ++pos;
12930 }
12931 }
12932 }
12933 for (i = 0; i < ineq->n_row; ++i) {
12934 l = isl_basic_map_alloc_inequality(bmap);
12935 if (l < 0)
12936 goto error;
12937 for (j = 0, pos = 0; j < 5; ++j) {
12938 int off = isl_basic_map_offset(bmap, c[j]);
12939 isl_size dim = isl_basic_map_dim(bmap, c[j]);
12940 if (dim < 0)
12941 goto error;
12942 for (k = 0; k < dim; ++k) {
12943 isl_int_set(bmap->ineq[l][off + k],
12944 ineq->row[i][pos]);
12945 ++pos;
12946 }
12947 }
12948 }
12949
12950 isl_space_free(space);
12951 isl_mat_free(eq);
12952 isl_mat_free(ineq);
12953
12954 bmap = isl_basic_map_simplify(bmap);
12955 return isl_basic_map_finalize(bmap);
12956 error:
12957 isl_space_free(space);
12958 isl_mat_free(eq);
12959 isl_mat_free(ineq);
12960 isl_basic_map_free(bmap);
12961 return NULL;
12962 }
12963
isl_basic_set_equalities_matrix(__isl_keep isl_basic_set * bset,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4)12964 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12965 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12966 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12967 {
12968 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12969 c1, c2, c3, c4, isl_dim_in);
12970 }
12971
isl_basic_set_inequalities_matrix(__isl_keep isl_basic_set * bset,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4)12972 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12973 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12974 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12975 {
12976 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12977 c1, c2, c3, c4, isl_dim_in);
12978 }
12979
isl_basic_set_from_constraint_matrices(__isl_take isl_space * space,__isl_take isl_mat * eq,__isl_take isl_mat * ineq,enum isl_dim_type c1,enum isl_dim_type c2,enum isl_dim_type c3,enum isl_dim_type c4)12980 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12981 __isl_take isl_space *space,
12982 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12983 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12984 {
12985 isl_basic_map *bmap;
12986 bmap = isl_basic_map_from_constraint_matrices(space, eq, ineq,
12987 c1, c2, c3, c4, isl_dim_in);
12988 return bset_from_bmap(bmap);
12989 }
12990
isl_basic_map_can_zip(__isl_keep isl_basic_map * bmap)12991 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12992 {
12993 if (!bmap)
12994 return isl_bool_error;
12995
12996 return isl_space_can_zip(bmap->dim);
12997 }
12998
isl_map_can_zip(__isl_keep isl_map * map)12999 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
13000 {
13001 if (!map)
13002 return isl_bool_error;
13003
13004 return isl_space_can_zip(map->dim);
13005 }
13006
13007 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
13008 * (A -> C) -> (B -> D).
13009 */
isl_basic_map_zip(__isl_take isl_basic_map * bmap)13010 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
13011 {
13012 unsigned pos;
13013 isl_size n_in;
13014 isl_size n1;
13015 isl_size n2;
13016
13017 if (!bmap)
13018 return NULL;
13019
13020 if (!isl_basic_map_can_zip(bmap))
13021 isl_die(bmap->ctx, isl_error_invalid,
13022 "basic map cannot be zipped", goto error);
13023 n_in = isl_space_dim(bmap->dim->nested[0], isl_dim_in);
13024 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
13025 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
13026 if (n_in < 0 || n1 < 0 || n2 < 0)
13027 return isl_basic_map_free(bmap);
13028 pos = isl_basic_map_offset(bmap, isl_dim_in) + n_in;
13029 bmap = isl_basic_map_cow(bmap);
13030 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
13031 if (!bmap)
13032 return NULL;
13033 bmap->dim = isl_space_zip(bmap->dim);
13034 if (!bmap->dim)
13035 goto error;
13036 bmap = isl_basic_map_mark_final(bmap);
13037 return bmap;
13038 error:
13039 isl_basic_map_free(bmap);
13040 return NULL;
13041 }
13042
13043 /* Given a map (A -> B) -> (C -> D), return the corresponding map
13044 * (A -> C) -> (B -> D).
13045 */
isl_map_zip(__isl_take isl_map * map)13046 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
13047 {
13048 if (!map)
13049 return NULL;
13050
13051 if (!isl_map_can_zip(map))
13052 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
13053 goto error);
13054
13055 return isl_map_transform(map, &isl_space_zip, &isl_basic_map_zip);
13056 error:
13057 isl_map_free(map);
13058 return NULL;
13059 }
13060
13061 /* Can we apply isl_basic_map_curry to "bmap"?
13062 * That is, does it have a nested relation in its domain?
13063 */
isl_basic_map_can_curry(__isl_keep isl_basic_map * bmap)13064 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
13065 {
13066 if (!bmap)
13067 return isl_bool_error;
13068
13069 return isl_space_can_curry(bmap->dim);
13070 }
13071
13072 /* Can we apply isl_map_curry to "map"?
13073 * That is, does it have a nested relation in its domain?
13074 */
isl_map_can_curry(__isl_keep isl_map * map)13075 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
13076 {
13077 if (!map)
13078 return isl_bool_error;
13079
13080 return isl_space_can_curry(map->dim);
13081 }
13082
13083 /* Given a basic map (A -> B) -> C, return the corresponding basic map
13084 * A -> (B -> C).
13085 */
isl_basic_map_curry(__isl_take isl_basic_map * bmap)13086 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
13087 {
13088
13089 if (!bmap)
13090 return NULL;
13091
13092 if (!isl_basic_map_can_curry(bmap))
13093 isl_die(bmap->ctx, isl_error_invalid,
13094 "basic map cannot be curried", goto error);
13095 bmap = isl_basic_map_cow(bmap);
13096 if (!bmap)
13097 return NULL;
13098 bmap->dim = isl_space_curry(bmap->dim);
13099 if (!bmap->dim)
13100 goto error;
13101 bmap = isl_basic_map_mark_final(bmap);
13102 return bmap;
13103 error:
13104 isl_basic_map_free(bmap);
13105 return NULL;
13106 }
13107
13108 /* Given a map (A -> B) -> C, return the corresponding map
13109 * A -> (B -> C).
13110 */
isl_map_curry(__isl_take isl_map * map)13111 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
13112 {
13113 return isl_map_change_space(map, &isl_map_can_curry,
13114 "map cannot be curried", &isl_space_curry);
13115 }
13116
13117 /* Can isl_map_range_curry be applied to "map"?
13118 * That is, does it have a nested relation in its range,
13119 * the domain of which is itself a nested relation?
13120 */
isl_map_can_range_curry(__isl_keep isl_map * map)13121 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
13122 {
13123 if (!map)
13124 return isl_bool_error;
13125
13126 return isl_space_can_range_curry(map->dim);
13127 }
13128
13129 /* Given a map A -> ((B -> C) -> D), return the corresponding map
13130 * A -> (B -> (C -> D)).
13131 */
isl_map_range_curry(__isl_take isl_map * map)13132 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
13133 {
13134 return isl_map_change_space(map, &isl_map_can_range_curry,
13135 "map range cannot be curried",
13136 &isl_space_range_curry);
13137 }
13138
13139 /* Can we apply isl_basic_map_uncurry to "bmap"?
13140 * That is, does it have a nested relation in its domain?
13141 */
isl_basic_map_can_uncurry(__isl_keep isl_basic_map * bmap)13142 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
13143 {
13144 if (!bmap)
13145 return isl_bool_error;
13146
13147 return isl_space_can_uncurry(bmap->dim);
13148 }
13149
13150 /* Can we apply isl_map_uncurry to "map"?
13151 * That is, does it have a nested relation in its domain?
13152 */
isl_map_can_uncurry(__isl_keep isl_map * map)13153 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
13154 {
13155 if (!map)
13156 return isl_bool_error;
13157
13158 return isl_space_can_uncurry(map->dim);
13159 }
13160
13161 /* Given a basic map A -> (B -> C), return the corresponding basic map
13162 * (A -> B) -> C.
13163 */
isl_basic_map_uncurry(__isl_take isl_basic_map * bmap)13164 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
13165 {
13166
13167 if (!bmap)
13168 return NULL;
13169
13170 if (!isl_basic_map_can_uncurry(bmap))
13171 isl_die(bmap->ctx, isl_error_invalid,
13172 "basic map cannot be uncurried",
13173 return isl_basic_map_free(bmap));
13174 bmap = isl_basic_map_cow(bmap);
13175 if (!bmap)
13176 return NULL;
13177 bmap->dim = isl_space_uncurry(bmap->dim);
13178 if (!bmap->dim)
13179 return isl_basic_map_free(bmap);
13180 bmap = isl_basic_map_mark_final(bmap);
13181 return bmap;
13182 }
13183
13184 /* Given a map A -> (B -> C), return the corresponding map
13185 * (A -> B) -> C.
13186 */
isl_map_uncurry(__isl_take isl_map * map)13187 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
13188 {
13189 return isl_map_change_space(map, &isl_map_can_uncurry,
13190 "map cannot be uncurried", &isl_space_uncurry);
13191 }
13192
isl_set_equate(__isl_take isl_set * set,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13193 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
13194 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13195 {
13196 return isl_map_equate(set, type1, pos1, type2, pos2);
13197 }
13198
13199 /* Construct a basic map where the given dimensions are equal to each other.
13200 */
equator(__isl_take isl_space * space,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13201 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
13202 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13203 {
13204 isl_basic_map *bmap = NULL;
13205 int i;
13206 isl_size total;
13207
13208 total = isl_space_dim(space, isl_dim_all);
13209 if (total < 0 ||
13210 isl_space_check_range(space, type1, pos1, 1) < 0 ||
13211 isl_space_check_range(space, type2, pos2, 1) < 0)
13212 goto error;
13213
13214 if (type1 == type2 && pos1 == pos2)
13215 return isl_basic_map_universe(space);
13216
13217 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
13218 i = isl_basic_map_alloc_equality(bmap);
13219 if (i < 0)
13220 goto error;
13221 isl_seq_clr(bmap->eq[i], 1 + total);
13222 pos1 += isl_basic_map_offset(bmap, type1);
13223 pos2 += isl_basic_map_offset(bmap, type2);
13224 isl_int_set_si(bmap->eq[i][pos1], -1);
13225 isl_int_set_si(bmap->eq[i][pos2], 1);
13226 bmap = isl_basic_map_finalize(bmap);
13227 isl_space_free(space);
13228 return bmap;
13229 error:
13230 isl_space_free(space);
13231 isl_basic_map_free(bmap);
13232 return NULL;
13233 }
13234
13235 /* Add a constraint imposing that the given two dimensions are equal.
13236 */
isl_basic_map_equate(__isl_take isl_basic_map * bmap,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13237 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
13238 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13239 {
13240 isl_basic_map *eq;
13241
13242 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13243
13244 bmap = isl_basic_map_intersect(bmap, eq);
13245
13246 return bmap;
13247 }
13248
13249 /* Add a constraint imposing that the given two dimensions are equal.
13250 */
isl_map_equate(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13251 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
13252 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13253 {
13254 isl_basic_map *bmap;
13255
13256 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
13257
13258 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13259
13260 return map;
13261 }
13262
13263 /* Add a constraint imposing that the given two dimensions have opposite values.
13264 */
isl_map_oppose(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13265 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
13266 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13267 {
13268 isl_basic_map *bmap = NULL;
13269 int i;
13270 isl_size total;
13271
13272 if (isl_map_check_range(map, type1, pos1, 1) < 0)
13273 return isl_map_free(map);
13274 if (isl_map_check_range(map, type2, pos2, 1) < 0)
13275 return isl_map_free(map);
13276
13277 total = isl_map_dim(map, isl_dim_all);
13278 if (total < 0)
13279 return isl_map_free(map);
13280 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
13281 i = isl_basic_map_alloc_equality(bmap);
13282 if (i < 0)
13283 goto error;
13284 isl_seq_clr(bmap->eq[i], 1 + total);
13285 pos1 += isl_basic_map_offset(bmap, type1);
13286 pos2 += isl_basic_map_offset(bmap, type2);
13287 isl_int_set_si(bmap->eq[i][pos1], 1);
13288 isl_int_set_si(bmap->eq[i][pos2], 1);
13289 bmap = isl_basic_map_finalize(bmap);
13290
13291 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13292
13293 return map;
13294 error:
13295 isl_basic_map_free(bmap);
13296 isl_map_free(map);
13297 return NULL;
13298 }
13299
13300 /* Construct a constraint imposing that the value of the first dimension is
13301 * greater than or equal to that of the second.
13302 */
constraint_order_ge(__isl_take isl_space * space,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13303 static __isl_give isl_constraint *constraint_order_ge(
13304 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
13305 enum isl_dim_type type2, int pos2)
13306 {
13307 isl_constraint *c;
13308
13309 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13310 isl_space_check_range(space, type2, pos2, 1) < 0)
13311 space = isl_space_free(space);
13312 if (!space)
13313 return NULL;
13314
13315 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
13316
13317 if (type1 == type2 && pos1 == pos2)
13318 return c;
13319
13320 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
13321 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
13322
13323 return c;
13324 }
13325
13326 /* Add a constraint imposing that the value of the first dimension is
13327 * greater than or equal to that of the second.
13328 */
isl_basic_map_order_ge(__isl_take isl_basic_map * bmap,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13329 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
13330 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13331 {
13332 isl_constraint *c;
13333 isl_space *space;
13334
13335 if (type1 == type2 && pos1 == pos2)
13336 return bmap;
13337 space = isl_basic_map_get_space(bmap);
13338 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13339 bmap = isl_basic_map_add_constraint(bmap, c);
13340
13341 return bmap;
13342 }
13343
13344 /* Add a constraint imposing that the value of the first dimension is
13345 * greater than or equal to that of the second.
13346 */
isl_map_order_ge(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13347 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
13348 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13349 {
13350 isl_constraint *c;
13351 isl_space *space;
13352
13353 if (type1 == type2 && pos1 == pos2)
13354 return map;
13355 space = isl_map_get_space(map);
13356 c = constraint_order_ge(space, type1, pos1, type2, pos2);
13357 map = isl_map_add_constraint(map, c);
13358
13359 return map;
13360 }
13361
13362 /* Add a constraint imposing that the value of the first dimension is
13363 * less than or equal to that of the second.
13364 */
isl_map_order_le(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13365 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
13366 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13367 {
13368 return isl_map_order_ge(map, type2, pos2, type1, pos1);
13369 }
13370
13371 /* Construct a basic map where the value of the first dimension is
13372 * greater than that of the second.
13373 */
greator(__isl_take isl_space * space,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13374 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
13375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13376 {
13377 isl_basic_map *bmap = NULL;
13378 int i;
13379 isl_size total;
13380
13381 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
13382 isl_space_check_range(space, type2, pos2, 1) < 0)
13383 goto error;
13384
13385 if (type1 == type2 && pos1 == pos2)
13386 return isl_basic_map_empty(space);
13387
13388 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
13389 total = isl_basic_map_dim(bmap, isl_dim_all);
13390 i = isl_basic_map_alloc_inequality(bmap);
13391 if (total < 0 || i < 0)
13392 return isl_basic_map_free(bmap);
13393 isl_seq_clr(bmap->ineq[i], 1 + total);
13394 pos1 += isl_basic_map_offset(bmap, type1);
13395 pos2 += isl_basic_map_offset(bmap, type2);
13396 isl_int_set_si(bmap->ineq[i][pos1], 1);
13397 isl_int_set_si(bmap->ineq[i][pos2], -1);
13398 isl_int_set_si(bmap->ineq[i][0], -1);
13399 bmap = isl_basic_map_finalize(bmap);
13400
13401 return bmap;
13402 error:
13403 isl_space_free(space);
13404 isl_basic_map_free(bmap);
13405 return NULL;
13406 }
13407
13408 /* Add a constraint imposing that the value of the first dimension is
13409 * greater than that of the second.
13410 */
isl_basic_map_order_gt(__isl_take isl_basic_map * bmap,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13411 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
13412 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13413 {
13414 isl_basic_map *gt;
13415
13416 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
13417
13418 bmap = isl_basic_map_intersect(bmap, gt);
13419
13420 return bmap;
13421 }
13422
13423 /* Add a constraint imposing that the value of the first dimension is
13424 * greater than that of the second.
13425 */
isl_map_order_gt(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13426 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
13427 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13428 {
13429 isl_basic_map *bmap;
13430
13431 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
13432
13433 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
13434
13435 return map;
13436 }
13437
13438 /* Add a constraint imposing that the value of the first dimension is
13439 * smaller than that of the second.
13440 */
isl_map_order_lt(__isl_take isl_map * map,enum isl_dim_type type1,int pos1,enum isl_dim_type type2,int pos2)13441 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
13442 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
13443 {
13444 return isl_map_order_gt(map, type2, pos2, type1, pos1);
13445 }
13446
isl_basic_map_get_div(__isl_keep isl_basic_map * bmap,int pos)13447 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
13448 int pos)
13449 {
13450 isl_aff *div;
13451 isl_local_space *ls;
13452
13453 if (!bmap)
13454 return NULL;
13455
13456 if (!isl_basic_map_divs_known(bmap))
13457 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13458 "some divs are unknown", return NULL);
13459
13460 ls = isl_basic_map_get_local_space(bmap);
13461 div = isl_local_space_get_div(ls, pos);
13462 isl_local_space_free(ls);
13463
13464 return div;
13465 }
13466
isl_basic_set_get_div(__isl_keep isl_basic_set * bset,int pos)13467 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
13468 int pos)
13469 {
13470 return isl_basic_map_get_div(bset, pos);
13471 }
13472
13473 /* Plug in "subs" for dimension "type", "pos" of "bset".
13474 *
13475 * Let i be the dimension to replace and let "subs" be of the form
13476 *
13477 * f/d
13478 *
13479 * Any integer division with a non-zero coefficient for i,
13480 *
13481 * floor((a i + g)/m)
13482 *
13483 * is replaced by
13484 *
13485 * floor((a f + d g)/(m d))
13486 *
13487 * Constraints of the form
13488 *
13489 * a i + g
13490 *
13491 * are replaced by
13492 *
13493 * a f + d g
13494 *
13495 * We currently require that "subs" is an integral expression.
13496 * Handling rational expressions may require us to add stride constraints
13497 * as we do in isl_basic_set_preimage_multi_aff.
13498 */
isl_basic_set_substitute(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned pos,__isl_keep isl_aff * subs)13499 __isl_give isl_basic_set *isl_basic_set_substitute(
13500 __isl_take isl_basic_set *bset,
13501 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13502 {
13503 int i;
13504 isl_int v;
13505 isl_ctx *ctx;
13506 isl_size n_div;
13507
13508 if (bset && isl_basic_set_plain_is_empty(bset))
13509 return bset;
13510
13511 bset = isl_basic_set_cow(bset);
13512 if (!bset || !subs)
13513 goto error;
13514
13515 ctx = isl_basic_set_get_ctx(bset);
13516 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
13517 isl_die(ctx, isl_error_invalid,
13518 "spaces don't match", goto error);
13519 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
13520 if (n_div < 0)
13521 goto error;
13522 if (n_div != 0)
13523 isl_die(ctx, isl_error_unsupported,
13524 "cannot handle divs yet", goto error);
13525 if (!isl_int_is_one(subs->v->el[0]))
13526 isl_die(ctx, isl_error_invalid,
13527 "can only substitute integer expressions", goto error);
13528
13529 pos += isl_basic_set_offset(bset, type);
13530
13531 isl_int_init(v);
13532
13533 for (i = 0; i < bset->n_eq; ++i) {
13534 if (isl_int_is_zero(bset->eq[i][pos]))
13535 continue;
13536 isl_int_set(v, bset->eq[i][pos]);
13537 isl_int_set_si(bset->eq[i][pos], 0);
13538 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
13539 v, subs->v->el + 1, subs->v->size - 1);
13540 }
13541
13542 for (i = 0; i < bset->n_ineq; ++i) {
13543 if (isl_int_is_zero(bset->ineq[i][pos]))
13544 continue;
13545 isl_int_set(v, bset->ineq[i][pos]);
13546 isl_int_set_si(bset->ineq[i][pos], 0);
13547 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
13548 v, subs->v->el + 1, subs->v->size - 1);
13549 }
13550
13551 for (i = 0; i < bset->n_div; ++i) {
13552 if (isl_int_is_zero(bset->div[i][1 + pos]))
13553 continue;
13554 isl_int_set(v, bset->div[i][1 + pos]);
13555 isl_int_set_si(bset->div[i][1 + pos], 0);
13556 isl_seq_combine(bset->div[i] + 1,
13557 subs->v->el[0], bset->div[i] + 1,
13558 v, subs->v->el + 1, subs->v->size - 1);
13559 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
13560 }
13561
13562 isl_int_clear(v);
13563
13564 bset = isl_basic_set_simplify(bset);
13565 return isl_basic_set_finalize(bset);
13566 error:
13567 isl_basic_set_free(bset);
13568 return NULL;
13569 }
13570
13571 /* Plug in "subs" for dimension "type", "pos" of "set".
13572 */
isl_set_substitute(__isl_take isl_set * set,enum isl_dim_type type,unsigned pos,__isl_keep isl_aff * subs)13573 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
13574 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
13575 {
13576 int i;
13577
13578 if (set && isl_set_plain_is_empty(set))
13579 return set;
13580
13581 set = isl_set_cow(set);
13582 if (!set || !subs)
13583 goto error;
13584
13585 for (i = set->n - 1; i >= 0; --i) {
13586 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
13587 set = set_from_map(remove_if_empty(set_to_map(set), i));
13588 if (!set)
13589 return NULL;
13590 }
13591
13592 return set;
13593 error:
13594 isl_set_free(set);
13595 return NULL;
13596 }
13597
13598 /* Check if the range of "ma" is compatible with the domain or range
13599 * (depending on "type") of "bmap".
13600 */
check_basic_map_compatible_range_multi_aff(__isl_keep isl_basic_map * bmap,enum isl_dim_type type,__isl_keep isl_multi_aff * ma)13601 static isl_stat check_basic_map_compatible_range_multi_aff(
13602 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
13603 __isl_keep isl_multi_aff *ma)
13604 {
13605 isl_bool m;
13606 isl_space *ma_space;
13607
13608 ma_space = isl_multi_aff_get_space(ma);
13609
13610 m = isl_space_has_equal_params(bmap->dim, ma_space);
13611 if (m < 0)
13612 goto error;
13613 if (!m)
13614 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13615 "parameters don't match", goto error);
13616 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
13617 if (m < 0)
13618 goto error;
13619 if (!m)
13620 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
13621 "spaces don't match", goto error);
13622
13623 isl_space_free(ma_space);
13624 return isl_stat_ok;
13625 error:
13626 isl_space_free(ma_space);
13627 return isl_stat_error;
13628 }
13629
13630 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
13631 * coefficients before the transformed range of dimensions,
13632 * the "n_after" coefficients after the transformed range of dimensions
13633 * and the coefficients of the other divs in "bmap".
13634 */
set_ma_divs(__isl_take isl_basic_map * bmap,__isl_keep isl_multi_aff * ma,int n_before,int n_after,int n_div)13635 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
13636 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
13637 {
13638 int i;
13639 isl_size n_param;
13640 isl_size n_set;
13641 isl_local_space *ls;
13642
13643 if (n_div == 0)
13644 return bmap;
13645
13646 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
13647 n_param = isl_local_space_dim(ls, isl_dim_param);
13648 n_set = isl_local_space_dim(ls, isl_dim_set);
13649 if (n_param < 0 || n_set < 0)
13650 return isl_basic_map_free(bmap);
13651
13652 for (i = 0; i < n_div; ++i) {
13653 int o_bmap = 0, o_ls = 0;
13654
13655 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
13656 o_bmap += 1 + 1 + n_param;
13657 o_ls += 1 + 1 + n_param;
13658 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
13659 o_bmap += n_before;
13660 isl_seq_cpy(bmap->div[i] + o_bmap,
13661 ls->div->row[i] + o_ls, n_set);
13662 o_bmap += n_set;
13663 o_ls += n_set;
13664 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
13665 o_bmap += n_after;
13666 isl_seq_cpy(bmap->div[i] + o_bmap,
13667 ls->div->row[i] + o_ls, n_div);
13668 o_bmap += n_div;
13669 o_ls += n_div;
13670 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
13671 bmap = isl_basic_map_add_div_constraints(bmap, i);
13672 if (!bmap)
13673 goto error;
13674 }
13675
13676 isl_local_space_free(ls);
13677 return bmap;
13678 error:
13679 isl_local_space_free(ls);
13680 return isl_basic_map_free(bmap);
13681 }
13682
13683 /* How many stride constraints does "ma" enforce?
13684 * That is, how many of the affine expressions have a denominator
13685 * different from one?
13686 */
multi_aff_strides(__isl_keep isl_multi_aff * ma)13687 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
13688 {
13689 int i;
13690 int strides = 0;
13691
13692 for (i = 0; i < ma->n; ++i)
13693 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
13694 strides++;
13695
13696 return strides;
13697 }
13698
13699 /* For each affine expression in ma of the form
13700 *
13701 * x_i = (f_i y + h_i)/m_i
13702 *
13703 * with m_i different from one, add a constraint to "bmap"
13704 * of the form
13705 *
13706 * f_i y + h_i = m_i alpha_i
13707 *
13708 * with alpha_i an additional existentially quantified variable.
13709 *
13710 * The input variables of "ma" correspond to a subset of the variables
13711 * of "bmap". There are "n_before" variables in "bmap" before this
13712 * subset and "n_after" variables after this subset.
13713 * The integer divisions of the affine expressions in "ma" are assumed
13714 * to have been aligned. There are "n_div_ma" of them and
13715 * they appear first in "bmap", straight after the "n_after" variables.
13716 */
add_ma_strides(__isl_take isl_basic_map * bmap,__isl_keep isl_multi_aff * ma,int n_before,int n_after,int n_div_ma)13717 static __isl_give isl_basic_map *add_ma_strides(
13718 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
13719 int n_before, int n_after, int n_div_ma)
13720 {
13721 int i, k;
13722 int div;
13723 isl_size total;
13724 isl_size n_param;
13725 isl_size n_in;
13726
13727 total = isl_basic_map_dim(bmap, isl_dim_all);
13728 n_param = isl_multi_aff_dim(ma, isl_dim_param);
13729 n_in = isl_multi_aff_dim(ma, isl_dim_in);
13730 if (total < 0 || n_param < 0 || n_in < 0)
13731 return isl_basic_map_free(bmap);
13732 for (i = 0; i < ma->n; ++i) {
13733 int o_bmap = 0, o_ma = 1;
13734
13735 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
13736 continue;
13737 div = isl_basic_map_alloc_div(bmap);
13738 k = isl_basic_map_alloc_equality(bmap);
13739 if (div < 0 || k < 0)
13740 goto error;
13741 isl_int_set_si(bmap->div[div][0], 0);
13742 isl_seq_cpy(bmap->eq[k] + o_bmap,
13743 ma->u.p[i]->v->el + o_ma, 1 + n_param);
13744 o_bmap += 1 + n_param;
13745 o_ma += 1 + n_param;
13746 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
13747 o_bmap += n_before;
13748 isl_seq_cpy(bmap->eq[k] + o_bmap,
13749 ma->u.p[i]->v->el + o_ma, n_in);
13750 o_bmap += n_in;
13751 o_ma += n_in;
13752 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
13753 o_bmap += n_after;
13754 isl_seq_cpy(bmap->eq[k] + o_bmap,
13755 ma->u.p[i]->v->el + o_ma, n_div_ma);
13756 o_bmap += n_div_ma;
13757 o_ma += n_div_ma;
13758 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
13759 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
13760 total++;
13761 }
13762
13763 return bmap;
13764 error:
13765 isl_basic_map_free(bmap);
13766 return NULL;
13767 }
13768
13769 /* Replace the domain or range space (depending on "type) of "space" by "set".
13770 */
isl_space_set(__isl_take isl_space * space,enum isl_dim_type type,__isl_take isl_space * set)13771 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
13772 enum isl_dim_type type, __isl_take isl_space *set)
13773 {
13774 if (type == isl_dim_in) {
13775 space = isl_space_range(space);
13776 space = isl_space_map_from_domain_and_range(set, space);
13777 } else {
13778 space = isl_space_domain(space);
13779 space = isl_space_map_from_domain_and_range(space, set);
13780 }
13781
13782 return space;
13783 }
13784
13785 /* Compute the preimage of the domain or range (depending on "type")
13786 * of "bmap" under the function represented by "ma".
13787 * In other words, plug in "ma" in the domain or range of "bmap".
13788 * The result is a basic map that lives in the same space as "bmap"
13789 * except that the domain or range has been replaced by
13790 * the domain space of "ma".
13791 *
13792 * If bmap is represented by
13793 *
13794 * A(p) + S u + B x + T v + C(divs) >= 0,
13795 *
13796 * where u and x are input and output dimensions if type == isl_dim_out
13797 * while x and v are input and output dimensions if type == isl_dim_in,
13798 * and ma is represented by
13799 *
13800 * x = D(p) + F(y) + G(divs')
13801 *
13802 * then the result is
13803 *
13804 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
13805 *
13806 * The divs in the input set are similarly adjusted.
13807 * In particular
13808 *
13809 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
13810 *
13811 * becomes
13812 *
13813 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
13814 * B_i G(divs') + c_i(divs))/n_i)
13815 *
13816 * If bmap is not a rational map and if F(y) involves any denominators
13817 *
13818 * x_i = (f_i y + h_i)/m_i
13819 *
13820 * then additional constraints are added to ensure that we only
13821 * map back integer points. That is we enforce
13822 *
13823 * f_i y + h_i = m_i alpha_i
13824 *
13825 * with alpha_i an additional existentially quantified variable.
13826 *
13827 * We first copy over the divs from "ma".
13828 * Then we add the modified constraints and divs from "bmap".
13829 * Finally, we add the stride constraints, if needed.
13830 */
isl_basic_map_preimage_multi_aff(__isl_take isl_basic_map * bmap,enum isl_dim_type type,__isl_take isl_multi_aff * ma)13831 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
13832 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
13833 __isl_take isl_multi_aff *ma)
13834 {
13835 int i, k;
13836 isl_space *space;
13837 isl_basic_map *res = NULL;
13838 isl_size n_before, n_after, n_div_bmap, n_div_ma;
13839 isl_int f, c1, c2, g;
13840 isl_bool rational;
13841 int strides;
13842
13843 isl_int_init(f);
13844 isl_int_init(c1);
13845 isl_int_init(c2);
13846 isl_int_init(g);
13847
13848 ma = isl_multi_aff_align_divs(ma);
13849 if (!bmap || !ma)
13850 goto error;
13851 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13852 goto error;
13853
13854 if (type == isl_dim_in) {
13855 n_before = 0;
13856 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13857 } else {
13858 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13859 n_after = 0;
13860 }
13861 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13862 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
13863 if (n_before < 0 || n_after < 0 || n_div_bmap < 0 || n_div_ma < 0)
13864 goto error;
13865
13866 space = isl_multi_aff_get_domain_space(ma);
13867 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13868 rational = isl_basic_map_is_rational(bmap);
13869 strides = rational ? 0 : multi_aff_strides(ma);
13870 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13871 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13872 if (rational)
13873 res = isl_basic_map_set_rational(res);
13874
13875 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13876 if (isl_basic_map_alloc_div(res) < 0)
13877 goto error;
13878
13879 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
13880 if (!res)
13881 goto error;
13882
13883 for (i = 0; i < bmap->n_eq; ++i) {
13884 k = isl_basic_map_alloc_equality(res);
13885 if (k < 0)
13886 goto error;
13887 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13888 n_after, n_div_ma, n_div_bmap,
13889 f, c1, c2, g, 0) < 0)
13890 goto error;
13891 }
13892
13893 for (i = 0; i < bmap->n_ineq; ++i) {
13894 k = isl_basic_map_alloc_inequality(res);
13895 if (k < 0)
13896 goto error;
13897 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13898 n_after, n_div_ma, n_div_bmap,
13899 f, c1, c2, g, 0) < 0)
13900 goto error;
13901 }
13902
13903 for (i = 0; i < bmap->n_div; ++i) {
13904 if (isl_int_is_zero(bmap->div[i][0])) {
13905 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13906 continue;
13907 }
13908 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13909 n_before, n_after, n_div_ma, n_div_bmap,
13910 f, c1, c2, g, 1) < 0)
13911 goto error;
13912 }
13913
13914 if (strides)
13915 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13916
13917 isl_int_clear(f);
13918 isl_int_clear(c1);
13919 isl_int_clear(c2);
13920 isl_int_clear(g);
13921 isl_basic_map_free(bmap);
13922 isl_multi_aff_free(ma);
13923 res = isl_basic_map_simplify(res);
13924 return isl_basic_map_finalize(res);
13925 error:
13926 isl_int_clear(f);
13927 isl_int_clear(c1);
13928 isl_int_clear(c2);
13929 isl_int_clear(g);
13930 isl_basic_map_free(bmap);
13931 isl_multi_aff_free(ma);
13932 isl_basic_map_free(res);
13933 return NULL;
13934 }
13935
13936 /* Compute the preimage of "bset" under the function represented by "ma".
13937 * In other words, plug in "ma" in "bset". The result is a basic set
13938 * that lives in the domain space of "ma".
13939 */
isl_basic_set_preimage_multi_aff(__isl_take isl_basic_set * bset,__isl_take isl_multi_aff * ma)13940 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13941 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13942 {
13943 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13944 }
13945
13946 /* Compute the preimage of the domain of "bmap" under the function
13947 * represented by "ma".
13948 * In other words, plug in "ma" in the domain of "bmap".
13949 * The result is a basic map that lives in the same space as "bmap"
13950 * except that the domain has been replaced by the domain space of "ma".
13951 */
isl_basic_map_preimage_domain_multi_aff(__isl_take isl_basic_map * bmap,__isl_take isl_multi_aff * ma)13952 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13953 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13954 {
13955 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13956 }
13957
13958 /* Compute the preimage of the range of "bmap" under the function
13959 * represented by "ma".
13960 * In other words, plug in "ma" in the range of "bmap".
13961 * The result is a basic map that lives in the same space as "bmap"
13962 * except that the range has been replaced by the domain space of "ma".
13963 */
isl_basic_map_preimage_range_multi_aff(__isl_take isl_basic_map * bmap,__isl_take isl_multi_aff * ma)13964 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13965 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13966 {
13967 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13968 }
13969
13970 /* Check if the range of "ma" is compatible with the domain or range
13971 * (depending on "type") of "map".
13972 * Return isl_stat_error if anything is wrong.
13973 */
check_map_compatible_range_multi_aff(__isl_keep isl_map * map,enum isl_dim_type type,__isl_keep isl_multi_aff * ma)13974 static isl_stat check_map_compatible_range_multi_aff(
13975 __isl_keep isl_map *map, enum isl_dim_type type,
13976 __isl_keep isl_multi_aff *ma)
13977 {
13978 isl_bool m;
13979 isl_space *ma_space;
13980
13981 ma_space = isl_multi_aff_get_space(ma);
13982 m = isl_map_space_tuple_is_equal(map, type, ma_space, isl_dim_out);
13983 isl_space_free(ma_space);
13984 if (m < 0)
13985 return isl_stat_error;
13986 if (!m)
13987 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13988 "spaces don't match", return isl_stat_error);
13989 return isl_stat_ok;
13990 }
13991
13992 /* Compute the preimage of the domain or range (depending on "type")
13993 * of "map" under the function represented by "ma".
13994 * In other words, plug in "ma" in the domain or range of "map".
13995 * The result is a map that lives in the same space as "map"
13996 * except that the domain or range has been replaced by
13997 * the domain space of "ma".
13998 *
13999 * The parameters are assumed to have been aligned.
14000 */
map_preimage_multi_aff(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_multi_aff * ma)14001 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
14002 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14003 {
14004 int i;
14005 isl_space *space;
14006
14007 map = isl_map_cow(map);
14008 ma = isl_multi_aff_align_divs(ma);
14009 if (!map || !ma)
14010 goto error;
14011 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
14012 goto error;
14013
14014 for (i = 0; i < map->n; ++i) {
14015 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
14016 isl_multi_aff_copy(ma));
14017 if (!map->p[i])
14018 goto error;
14019 }
14020
14021 space = isl_multi_aff_get_domain_space(ma);
14022 space = isl_space_set(isl_map_get_space(map), type, space);
14023
14024 isl_space_free(isl_map_take_space(map));
14025 map = isl_map_restore_space(map, space);
14026 if (!map)
14027 goto error;
14028
14029 isl_multi_aff_free(ma);
14030 if (map->n > 1)
14031 ISL_F_CLR(map, ISL_MAP_DISJOINT);
14032 ISL_F_CLR(map, ISL_SET_NORMALIZED);
14033 return map;
14034 error:
14035 isl_multi_aff_free(ma);
14036 isl_map_free(map);
14037 return NULL;
14038 }
14039
14040 /* Compute the preimage of the domain or range (depending on "type")
14041 * of "map" under the function represented by "ma".
14042 * In other words, plug in "ma" in the domain or range of "map".
14043 * The result is a map that lives in the same space as "map"
14044 * except that the domain or range has been replaced by
14045 * the domain space of "ma".
14046 */
isl_map_preimage_multi_aff(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_multi_aff * ma)14047 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
14048 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
14049 {
14050 isl_bool aligned;
14051
14052 if (!map || !ma)
14053 goto error;
14054
14055 aligned = isl_map_space_has_equal_params(map, ma->space);
14056 if (aligned < 0)
14057 goto error;
14058 if (aligned)
14059 return map_preimage_multi_aff(map, type, ma);
14060
14061 if (isl_map_check_named_params(map) < 0)
14062 goto error;
14063 if (!isl_space_has_named_params(ma->space))
14064 isl_die(map->ctx, isl_error_invalid,
14065 "unaligned unnamed parameters", goto error);
14066 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
14067 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
14068
14069 return map_preimage_multi_aff(map, type, ma);
14070 error:
14071 isl_multi_aff_free(ma);
14072 return isl_map_free(map);
14073 }
14074
14075 /* Compute the preimage of "set" under the function represented by "ma".
14076 * In other words, plug in "ma" in "set". The result is a set
14077 * that lives in the domain space of "ma".
14078 */
isl_set_preimage_multi_aff(__isl_take isl_set * set,__isl_take isl_multi_aff * ma)14079 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
14080 __isl_take isl_multi_aff *ma)
14081 {
14082 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
14083 }
14084
14085 /* Compute the preimage of the domain of "map" under the function
14086 * represented by "ma".
14087 * In other words, plug in "ma" in the domain of "map".
14088 * The result is a map that lives in the same space as "map"
14089 * except that the domain has been replaced by the domain space of "ma".
14090 */
isl_map_preimage_domain_multi_aff(__isl_take isl_map * map,__isl_take isl_multi_aff * ma)14091 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
14092 __isl_take isl_multi_aff *ma)
14093 {
14094 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
14095 }
14096
14097 /* Compute the preimage of the range of "map" under the function
14098 * represented by "ma".
14099 * In other words, plug in "ma" in the range of "map".
14100 * The result is a map that lives in the same space as "map"
14101 * except that the range has been replaced by the domain space of "ma".
14102 */
isl_map_preimage_range_multi_aff(__isl_take isl_map * map,__isl_take isl_multi_aff * ma)14103 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
14104 __isl_take isl_multi_aff *ma)
14105 {
14106 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
14107 }
14108
14109 /* Compute the preimage of "map" under the function represented by "pma".
14110 * In other words, plug in "pma" in the domain or range of "map".
14111 * The result is a map that lives in the same space as "map",
14112 * except that the space of type "type" has been replaced by
14113 * the domain space of "pma".
14114 *
14115 * The parameters of "map" and "pma" are assumed to have been aligned.
14116 */
isl_map_preimage_pw_multi_aff_aligned(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_pw_multi_aff * pma)14117 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
14118 __isl_take isl_map *map, enum isl_dim_type type,
14119 __isl_take isl_pw_multi_aff *pma)
14120 {
14121 int i;
14122 isl_map *res;
14123
14124 if (!pma)
14125 goto error;
14126
14127 if (pma->n == 0) {
14128 isl_pw_multi_aff_free(pma);
14129 res = isl_map_empty(isl_map_get_space(map));
14130 isl_map_free(map);
14131 return res;
14132 }
14133
14134 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14135 isl_multi_aff_copy(pma->p[0].maff));
14136 if (type == isl_dim_in)
14137 res = isl_map_intersect_domain(res,
14138 isl_map_copy(pma->p[0].set));
14139 else
14140 res = isl_map_intersect_range(res,
14141 isl_map_copy(pma->p[0].set));
14142
14143 for (i = 1; i < pma->n; ++i) {
14144 isl_map *res_i;
14145
14146 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
14147 isl_multi_aff_copy(pma->p[i].maff));
14148 if (type == isl_dim_in)
14149 res_i = isl_map_intersect_domain(res_i,
14150 isl_map_copy(pma->p[i].set));
14151 else
14152 res_i = isl_map_intersect_range(res_i,
14153 isl_map_copy(pma->p[i].set));
14154 res = isl_map_union(res, res_i);
14155 }
14156
14157 isl_pw_multi_aff_free(pma);
14158 isl_map_free(map);
14159 return res;
14160 error:
14161 isl_pw_multi_aff_free(pma);
14162 isl_map_free(map);
14163 return NULL;
14164 }
14165
14166 /* Compute the preimage of "map" under the function represented by "pma".
14167 * In other words, plug in "pma" in the domain or range of "map".
14168 * The result is a map that lives in the same space as "map",
14169 * except that the space of type "type" has been replaced by
14170 * the domain space of "pma".
14171 */
isl_map_preimage_pw_multi_aff(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_pw_multi_aff * pma)14172 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
14173 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
14174 {
14175 isl_bool aligned;
14176
14177 if (!map || !pma)
14178 goto error;
14179
14180 aligned = isl_map_space_has_equal_params(map, pma->dim);
14181 if (aligned < 0)
14182 goto error;
14183 if (aligned)
14184 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14185
14186 if (isl_map_check_named_params(map) < 0)
14187 goto error;
14188 if (isl_pw_multi_aff_check_named_params(pma) < 0)
14189 goto error;
14190 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
14191 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
14192
14193 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
14194 error:
14195 isl_pw_multi_aff_free(pma);
14196 return isl_map_free(map);
14197 }
14198
14199 /* Compute the preimage of "set" under the function represented by "pma".
14200 * In other words, plug in "pma" in "set". The result is a set
14201 * that lives in the domain space of "pma".
14202 */
isl_set_preimage_pw_multi_aff(__isl_take isl_set * set,__isl_take isl_pw_multi_aff * pma)14203 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
14204 __isl_take isl_pw_multi_aff *pma)
14205 {
14206 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
14207 }
14208
14209 /* Compute the preimage of the domain of "map" under the function
14210 * represented by "pma".
14211 * In other words, plug in "pma" in the domain of "map".
14212 * The result is a map that lives in the same space as "map",
14213 * except that domain space has been replaced by the domain space of "pma".
14214 */
isl_map_preimage_domain_pw_multi_aff(__isl_take isl_map * map,__isl_take isl_pw_multi_aff * pma)14215 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
14216 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14217 {
14218 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
14219 }
14220
14221 /* Compute the preimage of the range of "map" under the function
14222 * represented by "pma".
14223 * In other words, plug in "pma" in the range of "map".
14224 * The result is a map that lives in the same space as "map",
14225 * except that range space has been replaced by the domain space of "pma".
14226 */
isl_map_preimage_range_pw_multi_aff(__isl_take isl_map * map,__isl_take isl_pw_multi_aff * pma)14227 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
14228 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
14229 {
14230 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
14231 }
14232
14233 /* Compute the preimage of "map" under the function represented by "mpa".
14234 * In other words, plug in "mpa" in the domain or range of "map".
14235 * The result is a map that lives in the same space as "map",
14236 * except that the space of type "type" has been replaced by
14237 * the domain space of "mpa".
14238 *
14239 * If the map does not involve any constraints that refer to the
14240 * dimensions of the substituted space, then the only possible
14241 * effect of "mpa" on the map is to map the space to a different space.
14242 * We create a separate isl_multi_aff to effectuate this change
14243 * in order to avoid spurious splitting of the map along the pieces
14244 * of "mpa".
14245 * If "mpa" has a non-trivial explicit domain, however,
14246 * then the full substitution should be performed.
14247 */
isl_map_preimage_multi_pw_aff(__isl_take isl_map * map,enum isl_dim_type type,__isl_take isl_multi_pw_aff * mpa)14248 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
14249 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
14250 {
14251 isl_size n;
14252 isl_bool full;
14253 isl_pw_multi_aff *pma;
14254
14255 n = isl_map_dim(map, type);
14256 if (n < 0 || !mpa)
14257 goto error;
14258
14259 full = isl_map_involves_dims(map, type, 0, n);
14260 if (full >= 0 && !full)
14261 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
14262 if (full < 0)
14263 goto error;
14264 if (!full) {
14265 isl_space *space;
14266 isl_multi_aff *ma;
14267
14268 space = isl_multi_pw_aff_get_space(mpa);
14269 isl_multi_pw_aff_free(mpa);
14270 ma = isl_multi_aff_zero(space);
14271 return isl_map_preimage_multi_aff(map, type, ma);
14272 }
14273
14274 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
14275 return isl_map_preimage_pw_multi_aff(map, type, pma);
14276 error:
14277 isl_map_free(map);
14278 isl_multi_pw_aff_free(mpa);
14279 return NULL;
14280 }
14281
14282 /* Compute the preimage of "map" under the function represented by "mpa".
14283 * In other words, plug in "mpa" in the domain "map".
14284 * The result is a map that lives in the same space as "map",
14285 * except that domain space has been replaced by the domain space of "mpa".
14286 */
isl_map_preimage_domain_multi_pw_aff(__isl_take isl_map * map,__isl_take isl_multi_pw_aff * mpa)14287 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
14288 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
14289 {
14290 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
14291 }
14292
14293 /* Compute the preimage of "set" by the function represented by "mpa".
14294 * In other words, plug in "mpa" in "set".
14295 */
isl_set_preimage_multi_pw_aff(__isl_take isl_set * set,__isl_take isl_multi_pw_aff * mpa)14296 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
14297 __isl_take isl_multi_pw_aff *mpa)
14298 {
14299 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
14300 }
14301
14302 /* Return a copy of the equality constraints of "bset" as a matrix.
14303 */
isl_basic_set_extract_equalities(__isl_keep isl_basic_set * bset)14304 __isl_give isl_mat *isl_basic_set_extract_equalities(
14305 __isl_keep isl_basic_set *bset)
14306 {
14307 isl_ctx *ctx;
14308 isl_size total;
14309
14310 total = isl_basic_set_dim(bset, isl_dim_all);
14311 if (total < 0)
14312 return NULL;
14313
14314 ctx = isl_basic_set_get_ctx(bset);
14315 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, 1 + total);
14316 }
14317
14318 /* Are the "n" "coefficients" starting at "first" of the integer division
14319 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
14320 * to each other?
14321 * The "coefficient" at position 0 is the denominator.
14322 * The "coefficient" at position 1 is the constant term.
14323 */
isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map * bmap1,int pos1,__isl_keep isl_basic_map * bmap2,int pos2,unsigned first,unsigned n)14324 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
14325 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
14326 unsigned first, unsigned n)
14327 {
14328 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
14329 return isl_bool_error;
14330 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
14331 return isl_bool_error;
14332 return isl_seq_eq(bmap1->div[pos1] + first,
14333 bmap2->div[pos2] + first, n);
14334 }
14335
14336 /* Are the integer division expressions at position "pos1" in "bmap1" and
14337 * "pos2" in "bmap2" equal to each other, except that the constant terms
14338 * are different?
14339 */
isl_basic_map_equal_div_expr_except_constant(__isl_keep isl_basic_map * bmap1,int pos1,__isl_keep isl_basic_map * bmap2,int pos2)14340 isl_bool isl_basic_map_equal_div_expr_except_constant(
14341 __isl_keep isl_basic_map *bmap1, int pos1,
14342 __isl_keep isl_basic_map *bmap2, int pos2)
14343 {
14344 isl_bool equal;
14345 isl_size total, total2;
14346
14347 total = isl_basic_map_dim(bmap1, isl_dim_all);
14348 total2 = isl_basic_map_dim(bmap2, isl_dim_all);
14349 if (total < 0 || total2 < 0)
14350 return isl_bool_error;
14351 if (total != total2)
14352 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
14353 "incomparable div expressions", return isl_bool_error);
14354 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14355 0, 1);
14356 if (equal < 0 || !equal)
14357 return equal;
14358 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14359 1, 1);
14360 if (equal < 0 || equal)
14361 return isl_bool_not(equal);
14362 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
14363 2, total);
14364 }
14365
14366 /* Replace the numerator of the constant term of the integer division
14367 * expression at position "div" in "bmap" by "value".
14368 * The caller guarantees that this does not change the meaning
14369 * of the input.
14370 */
isl_basic_map_set_div_expr_constant_num_si_inplace(__isl_take isl_basic_map * bmap,int div,int value)14371 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
14372 __isl_take isl_basic_map *bmap, int div, int value)
14373 {
14374 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
14375 return isl_basic_map_free(bmap);
14376
14377 isl_int_set_si(bmap->div[div][1], value);
14378
14379 return bmap;
14380 }
14381
14382 /* Is the point "inner" internal to inequality constraint "ineq"
14383 * of "bset"?
14384 * The point is considered to be internal to the inequality constraint,
14385 * if it strictly lies on the positive side of the inequality constraint,
14386 * or if it lies on the constraint and the constraint is lexico-positive.
14387 */
is_internal(__isl_keep isl_vec * inner,__isl_keep isl_basic_set * bset,int ineq)14388 static isl_bool is_internal(__isl_keep isl_vec *inner,
14389 __isl_keep isl_basic_set *bset, int ineq)
14390 {
14391 isl_ctx *ctx;
14392 int pos;
14393 isl_size total;
14394
14395 if (!inner || !bset)
14396 return isl_bool_error;
14397
14398 ctx = isl_basic_set_get_ctx(bset);
14399 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
14400 &ctx->normalize_gcd);
14401 if (!isl_int_is_zero(ctx->normalize_gcd))
14402 return isl_int_is_nonneg(ctx->normalize_gcd);
14403
14404 total = isl_basic_set_dim(bset, isl_dim_all);
14405 if (total < 0)
14406 return isl_bool_error;
14407 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
14408 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
14409 }
14410
14411 /* Tighten the inequality constraints of "bset" that are outward with respect
14412 * to the point "vec".
14413 * That is, tighten the constraints that are not satisfied by "vec".
14414 *
14415 * "vec" is a point internal to some superset S of "bset" that is used
14416 * to make the subsets of S disjoint, by tightening one half of the constraints
14417 * that separate two subsets. In particular, the constraints of S
14418 * are all satisfied by "vec" and should not be tightened.
14419 * Of the internal constraints, those that have "vec" on the outside
14420 * are tightened. The shared facet is included in the adjacent subset
14421 * with the opposite constraint.
14422 * For constraints that saturate "vec", this criterion cannot be used
14423 * to determine which of the two sides should be tightened.
14424 * Instead, the sign of the first non-zero coefficient is used
14425 * to make this choice. Note that this second criterion is never used
14426 * on the constraints of S since "vec" is interior to "S".
14427 */
isl_basic_set_tighten_outward(__isl_take isl_basic_set * bset,__isl_keep isl_vec * vec)14428 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
14429 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
14430 {
14431 int j;
14432
14433 bset = isl_basic_set_cow(bset);
14434 if (!bset)
14435 return NULL;
14436 for (j = 0; j < bset->n_ineq; ++j) {
14437 isl_bool internal;
14438
14439 internal = is_internal(vec, bset, j);
14440 if (internal < 0)
14441 return isl_basic_set_free(bset);
14442 if (internal)
14443 continue;
14444 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
14445 }
14446
14447 return bset;
14448 }
14449
14450 /* Replace the variables x of type "type" starting at "first" in "bmap"
14451 * by x' with x = M x' with M the matrix trans.
14452 * That is, replace the corresponding coefficients c by c M.
14453 *
14454 * The transformation matrix should be a square matrix.
14455 */
isl_basic_map_transform_dims(__isl_take isl_basic_map * bmap,enum isl_dim_type type,unsigned first,__isl_take isl_mat * trans)14456 __isl_give isl_basic_map *isl_basic_map_transform_dims(
14457 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
14458 __isl_take isl_mat *trans)
14459 {
14460 unsigned pos;
14461
14462 bmap = isl_basic_map_cow(bmap);
14463 if (!bmap || !trans)
14464 goto error;
14465
14466 if (trans->n_row != trans->n_col)
14467 isl_die(trans->ctx, isl_error_invalid,
14468 "expecting square transformation matrix", goto error);
14469 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
14470 goto error;
14471
14472 pos = isl_basic_map_offset(bmap, type) + first;
14473
14474 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
14475 isl_mat_copy(trans)) < 0)
14476 goto error;
14477 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
14478 isl_mat_copy(trans)) < 0)
14479 goto error;
14480 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
14481 isl_mat_copy(trans)) < 0)
14482 goto error;
14483
14484 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
14485 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
14486
14487 isl_mat_free(trans);
14488 return bmap;
14489 error:
14490 isl_mat_free(trans);
14491 isl_basic_map_free(bmap);
14492 return NULL;
14493 }
14494
14495 /* Replace the variables x of type "type" starting at "first" in "bset"
14496 * by x' with x = M x' with M the matrix trans.
14497 * That is, replace the corresponding coefficients c by c M.
14498 *
14499 * The transformation matrix should be a square matrix.
14500 */
isl_basic_set_transform_dims(__isl_take isl_basic_set * bset,enum isl_dim_type type,unsigned first,__isl_take isl_mat * trans)14501 __isl_give isl_basic_set *isl_basic_set_transform_dims(
14502 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
14503 __isl_take isl_mat *trans)
14504 {
14505 return isl_basic_map_transform_dims(bset, type, first, trans);
14506 }
14507