isl_schedule_constraints_dump: use YAML facilities of isl_printer
[isl.git] / isl_map.c
blobca26a9b841aaebbd03c58ca9a5ee4432a86114ae
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 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
19 #include <string.h>
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_blk.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl/map.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
32 #include <isl_sort.h>
33 #include "isl_tab.h"
34 #include <isl/vec.h>
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return dim->nparam;
50 case isl_dim_in: return dim->n_in;
51 case isl_dim_out: return dim->n_out;
52 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
53 default: return 0;
57 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
59 switch (type) {
60 case isl_dim_param: return 1;
61 case isl_dim_in: return 1 + dim->nparam;
62 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
63 default: return 0;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
68 enum isl_dim_type type)
70 if (!bmap)
71 return 0;
72 switch (type) {
73 case isl_dim_cst: return 1;
74 case isl_dim_param:
75 case isl_dim_in:
76 case isl_dim_out: return isl_space_dim(bmap->dim, type);
77 case isl_dim_div: return bmap->n_div;
78 case isl_dim_all: return isl_basic_map_total_dim(bmap);
79 default: return 0;
83 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
85 return map ? n(map->dim, type) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
90 return set ? n(set->dim, type) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
94 enum isl_dim_type type)
96 isl_space *space;
98 if (!bmap)
99 return 0;
101 space = bmap->dim;
102 switch (type) {
103 case isl_dim_cst: return 0;
104 case isl_dim_param: return 1;
105 case isl_dim_in: return 1 + space->nparam;
106 case isl_dim_out: return 1 + space->nparam + space->n_in;
107 case isl_dim_div: return 1 + space->nparam + space->n_in +
108 space->n_out;
109 default: return 0;
113 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
114 enum isl_dim_type type)
116 return isl_basic_map_offset(bset, type);
119 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
121 return pos(map->dim, type);
124 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
125 enum isl_dim_type type)
127 return isl_basic_map_dim(bset, type);
130 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
132 return isl_basic_set_dim(bset, isl_dim_set);
135 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
137 return isl_basic_set_dim(bset, isl_dim_param);
140 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
142 if (!bset)
143 return 0;
144 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
147 unsigned isl_set_n_dim(__isl_keep isl_set *set)
149 return isl_set_dim(set, isl_dim_set);
152 unsigned isl_set_n_param(__isl_keep isl_set *set)
154 return isl_set_dim(set, isl_dim_param);
157 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
159 return bmap ? bmap->dim->n_in : 0;
162 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
164 return bmap ? bmap->dim->n_out : 0;
167 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
169 return bmap ? bmap->dim->nparam : 0;
172 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
174 return bmap ? bmap->n_div : 0;
177 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
179 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
182 unsigned isl_map_n_in(const struct isl_map *map)
184 return map ? map->dim->n_in : 0;
187 unsigned isl_map_n_out(const struct isl_map *map)
189 return map ? map->dim->n_out : 0;
192 unsigned isl_map_n_param(const struct isl_map *map)
194 return map ? map->dim->nparam : 0;
197 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
199 int m;
200 if (!map || !set)
201 return -1;
202 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
203 if (m < 0 || !m)
204 return m;
205 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
206 set->dim, isl_dim_set);
209 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
210 __isl_keep isl_basic_set *bset)
212 isl_bool m;
213 if (!bmap || !bset)
214 return isl_bool_error;
215 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
216 if (m < 0 || !m)
217 return m;
218 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
219 bset->dim, isl_dim_set);
222 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
224 int m;
225 if (!map || !set)
226 return -1;
227 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
228 if (m < 0 || !m)
229 return m;
230 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
231 set->dim, isl_dim_set);
234 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
235 struct isl_basic_set *bset)
237 int m;
238 if (!bmap || !bset)
239 return -1;
240 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
241 if (m < 0 || !m)
242 return m;
243 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
244 bset->dim, isl_dim_set);
247 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
249 return bmap ? bmap->ctx : NULL;
252 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
254 return bset ? bset->ctx : NULL;
257 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
259 return map ? map->ctx : NULL;
262 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
264 return set ? set->ctx : NULL;
267 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return NULL;
271 return isl_space_copy(bmap->dim);
274 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
276 if (!bset)
277 return NULL;
278 return isl_space_copy(bset->dim);
281 /* Extract the divs in "bmap" as a matrix.
283 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
285 int i;
286 isl_ctx *ctx;
287 isl_mat *div;
288 unsigned total;
289 unsigned cols;
291 if (!bmap)
292 return NULL;
294 ctx = isl_basic_map_get_ctx(bmap);
295 total = isl_space_dim(bmap->dim, isl_dim_all);
296 cols = 1 + 1 + total + bmap->n_div;
297 div = isl_mat_alloc(ctx, bmap->n_div, cols);
298 if (!div)
299 return NULL;
301 for (i = 0; i < bmap->n_div; ++i)
302 isl_seq_cpy(div->row[i], bmap->div[i], cols);
304 return div;
307 /* Extract the divs in "bset" as a matrix.
309 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
311 return isl_basic_map_get_divs(bset);
314 __isl_give isl_local_space *isl_basic_map_get_local_space(
315 __isl_keep isl_basic_map *bmap)
317 isl_mat *div;
319 if (!bmap)
320 return NULL;
322 div = isl_basic_map_get_divs(bmap);
323 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
326 __isl_give isl_local_space *isl_basic_set_get_local_space(
327 __isl_keep isl_basic_set *bset)
329 return isl_basic_map_get_local_space(bset);
332 /* For each known div d = floor(f/m), add the constraints
334 * f - m d >= 0
335 * -(f-(m-1)) + m d >= 0
337 * Do not finalize the result.
339 static __isl_give isl_basic_map *add_known_div_constraints(
340 __isl_take isl_basic_map *bmap)
342 int i;
343 unsigned n_div;
345 if (!bmap)
346 return NULL;
347 n_div = isl_basic_map_dim(bmap, isl_dim_div);
348 if (n_div == 0)
349 return bmap;
350 bmap = isl_basic_map_cow(bmap);
351 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
352 if (!bmap)
353 return NULL;
354 for (i = 0; i < n_div; ++i) {
355 if (isl_int_is_zero(bmap->div[i][0]))
356 continue;
357 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
358 return isl_basic_map_free(bmap);
361 return bmap;
364 __isl_give isl_basic_map *isl_basic_map_from_local_space(
365 __isl_take isl_local_space *ls)
367 int i;
368 int n_div;
369 isl_basic_map *bmap;
371 if (!ls)
372 return NULL;
374 n_div = isl_local_space_dim(ls, isl_dim_div);
375 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
376 n_div, 0, 2 * n_div);
378 for (i = 0; i < n_div; ++i)
379 if (isl_basic_map_alloc_div(bmap) < 0)
380 goto error;
382 for (i = 0; i < n_div; ++i)
383 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
384 bmap = add_known_div_constraints(bmap);
386 isl_local_space_free(ls);
387 return bmap;
388 error:
389 isl_local_space_free(ls);
390 isl_basic_map_free(bmap);
391 return NULL;
394 __isl_give isl_basic_set *isl_basic_set_from_local_space(
395 __isl_take isl_local_space *ls)
397 return isl_basic_map_from_local_space(ls);
400 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
402 if (!map)
403 return NULL;
404 return isl_space_copy(map->dim);
407 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
409 if (!set)
410 return NULL;
411 return isl_space_copy(set->dim);
414 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
415 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
417 bmap = isl_basic_map_cow(bmap);
418 if (!bmap)
419 return NULL;
420 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
421 if (!bmap->dim)
422 goto error;
423 bmap = isl_basic_map_finalize(bmap);
424 return bmap;
425 error:
426 isl_basic_map_free(bmap);
427 return NULL;
430 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
431 __isl_take isl_basic_set *bset, const char *s)
433 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
436 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
437 enum isl_dim_type type)
439 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
442 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
443 enum isl_dim_type type, const char *s)
445 int i;
447 map = isl_map_cow(map);
448 if (!map)
449 return NULL;
451 map->dim = isl_space_set_tuple_name(map->dim, type, s);
452 if (!map->dim)
453 goto error;
455 for (i = 0; i < map->n; ++i) {
456 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
457 if (!map->p[i])
458 goto error;
461 return map;
462 error:
463 isl_map_free(map);
464 return NULL;
467 /* Replace the identifier of the tuple of type "type" by "id".
469 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
470 __isl_take isl_basic_map *bmap,
471 enum isl_dim_type type, __isl_take isl_id *id)
473 bmap = isl_basic_map_cow(bmap);
474 if (!bmap)
475 goto error;
476 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
477 if (!bmap->dim)
478 return isl_basic_map_free(bmap);
479 bmap = isl_basic_map_finalize(bmap);
480 return bmap;
481 error:
482 isl_id_free(id);
483 return NULL;
486 /* Replace the identifier of the tuple by "id".
488 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
489 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
491 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
494 /* Does the input or output tuple have a name?
496 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
498 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
501 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
502 enum isl_dim_type type)
504 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
507 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
508 const char *s)
510 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
513 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
514 enum isl_dim_type type, __isl_take isl_id *id)
516 map = isl_map_cow(map);
517 if (!map)
518 goto error;
520 map->dim = isl_space_set_tuple_id(map->dim, type, id);
522 return isl_map_reset_space(map, isl_space_copy(map->dim));
523 error:
524 isl_id_free(id);
525 return NULL;
528 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
529 __isl_take isl_id *id)
531 return isl_map_set_tuple_id(set, isl_dim_set, id);
534 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
535 enum isl_dim_type type)
537 map = isl_map_cow(map);
538 if (!map)
539 return NULL;
541 map->dim = isl_space_reset_tuple_id(map->dim, type);
543 return isl_map_reset_space(map, isl_space_copy(map->dim));
546 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
548 return isl_map_reset_tuple_id(set, isl_dim_set);
551 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
553 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
556 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
557 enum isl_dim_type type)
559 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
562 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
564 return isl_map_has_tuple_id(set, isl_dim_set);
567 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
569 return isl_map_get_tuple_id(set, isl_dim_set);
572 /* Does the set tuple have a name?
574 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
576 if (!set)
577 return isl_bool_error;
578 return isl_space_has_tuple_name(set->dim, isl_dim_set);
582 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
584 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
587 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
589 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
592 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
593 enum isl_dim_type type, unsigned pos)
595 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
598 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
599 enum isl_dim_type type, unsigned pos)
601 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
604 /* Does the given dimension have a name?
606 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
607 enum isl_dim_type type, unsigned pos)
609 if (!map)
610 return isl_bool_error;
611 return isl_space_has_dim_name(map->dim, type, pos);
614 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
615 enum isl_dim_type type, unsigned pos)
617 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
620 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
621 enum isl_dim_type type, unsigned pos)
623 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
626 /* Does the given dimension have a name?
628 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
629 enum isl_dim_type type, unsigned pos)
631 if (!set)
632 return isl_bool_error;
633 return isl_space_has_dim_name(set->dim, type, pos);
636 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
637 __isl_take isl_basic_map *bmap,
638 enum isl_dim_type type, unsigned pos, const char *s)
640 bmap = isl_basic_map_cow(bmap);
641 if (!bmap)
642 return NULL;
643 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
644 if (!bmap->dim)
645 goto error;
646 return isl_basic_map_finalize(bmap);
647 error:
648 isl_basic_map_free(bmap);
649 return NULL;
652 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
653 enum isl_dim_type type, unsigned pos, const char *s)
655 int i;
657 map = isl_map_cow(map);
658 if (!map)
659 return NULL;
661 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
662 if (!map->dim)
663 goto error;
665 for (i = 0; i < map->n; ++i) {
666 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
667 if (!map->p[i])
668 goto error;
671 return map;
672 error:
673 isl_map_free(map);
674 return NULL;
677 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
678 __isl_take isl_basic_set *bset,
679 enum isl_dim_type type, unsigned pos, const char *s)
681 return (isl_basic_set *)isl_basic_map_set_dim_name(
682 (isl_basic_map *)bset, type, pos, s);
685 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
686 enum isl_dim_type type, unsigned pos, const char *s)
688 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
691 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
692 enum isl_dim_type type, unsigned pos)
694 if (!bmap)
695 return isl_bool_error;
696 return isl_space_has_dim_id(bmap->dim, type, pos);
699 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
700 enum isl_dim_type type, unsigned pos)
702 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
705 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
706 enum isl_dim_type type, unsigned pos)
708 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
711 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
712 enum isl_dim_type type, unsigned pos)
714 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
717 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
718 enum isl_dim_type type, unsigned pos)
720 return isl_map_has_dim_id(set, type, pos);
723 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
724 enum isl_dim_type type, unsigned pos)
726 return isl_map_get_dim_id(set, type, pos);
729 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
730 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
732 map = isl_map_cow(map);
733 if (!map)
734 goto error;
736 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
738 return isl_map_reset_space(map, isl_space_copy(map->dim));
739 error:
740 isl_id_free(id);
741 return NULL;
744 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
745 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
747 return isl_map_set_dim_id(set, type, pos, id);
750 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
751 __isl_keep isl_id *id)
753 if (!map)
754 return -1;
755 return isl_space_find_dim_by_id(map->dim, type, id);
758 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
759 __isl_keep isl_id *id)
761 return isl_map_find_dim_by_id(set, type, id);
764 /* Return the position of the dimension of the given type and name
765 * in "bmap".
766 * Return -1 if no such dimension can be found.
768 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
769 enum isl_dim_type type, const char *name)
771 if (!bmap)
772 return -1;
773 return isl_space_find_dim_by_name(bmap->dim, type, name);
776 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
777 const char *name)
779 if (!map)
780 return -1;
781 return isl_space_find_dim_by_name(map->dim, type, name);
784 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
785 const char *name)
787 return isl_map_find_dim_by_name(set, type, name);
790 /* Reset the user pointer on all identifiers of parameters and tuples
791 * of the space of "map".
793 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
795 isl_space *space;
797 space = isl_map_get_space(map);
798 space = isl_space_reset_user(space);
799 map = isl_map_reset_space(map, space);
801 return map;
804 /* Reset the user pointer on all identifiers of parameters and tuples
805 * of the space of "set".
807 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
809 return isl_map_reset_user(set);
812 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
814 if (!bmap)
815 return -1;
816 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
819 /* Has "map" been marked as a rational map?
820 * In particular, have all basic maps in "map" been marked this way?
821 * An empty map is not considered to be rational.
822 * Maps where only some of the basic maps are marked rational
823 * are not allowed.
825 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
827 int i;
828 isl_bool rational;
830 if (!map)
831 return isl_bool_error;
832 if (map->n == 0)
833 return isl_bool_false;
834 rational = isl_basic_map_is_rational(map->p[0]);
835 if (rational < 0)
836 return rational;
837 for (i = 1; i < map->n; ++i) {
838 isl_bool rational_i;
840 rational_i = isl_basic_map_is_rational(map->p[i]);
841 if (rational_i < 0)
842 return rational;
843 if (rational != rational_i)
844 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
845 "mixed rational and integer basic maps "
846 "not supported", return isl_bool_error);
849 return rational;
852 /* Has "set" been marked as a rational set?
853 * In particular, have all basic set in "set" been marked this way?
854 * An empty set is not considered to be rational.
855 * Sets where only some of the basic sets are marked rational
856 * are not allowed.
858 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
860 return isl_map_is_rational(set);
863 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
865 return isl_basic_map_is_rational(bset);
868 /* Does "bmap" contain any rational points?
870 * If "bmap" has an equality for each dimension, equating the dimension
871 * to an integer constant, then it has no rational points, even if it
872 * is marked as rational.
874 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
876 int has_rational = 1;
877 unsigned total;
879 if (!bmap)
880 return -1;
881 if (isl_basic_map_plain_is_empty(bmap))
882 return 0;
883 if (!isl_basic_map_is_rational(bmap))
884 return 0;
885 bmap = isl_basic_map_copy(bmap);
886 bmap = isl_basic_map_implicit_equalities(bmap);
887 if (!bmap)
888 return -1;
889 total = isl_basic_map_total_dim(bmap);
890 if (bmap->n_eq == total) {
891 int i, j;
892 for (i = 0; i < bmap->n_eq; ++i) {
893 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
894 if (j < 0)
895 break;
896 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
897 !isl_int_is_negone(bmap->eq[i][1 + j]))
898 break;
899 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
900 total - j - 1);
901 if (j >= 0)
902 break;
904 if (i == bmap->n_eq)
905 has_rational = 0;
907 isl_basic_map_free(bmap);
909 return has_rational;
912 /* Does "map" contain any rational points?
914 int isl_map_has_rational(__isl_keep isl_map *map)
916 int i;
917 int has_rational;
919 if (!map)
920 return -1;
921 for (i = 0; i < map->n; ++i) {
922 has_rational = isl_basic_map_has_rational(map->p[i]);
923 if (has_rational < 0)
924 return -1;
925 if (has_rational)
926 return 1;
928 return 0;
931 /* Does "set" contain any rational points?
933 int isl_set_has_rational(__isl_keep isl_set *set)
935 return isl_map_has_rational(set);
938 /* Is this basic set a parameter domain?
940 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
942 if (!bset)
943 return -1;
944 return isl_space_is_params(bset->dim);
947 /* Is this set a parameter domain?
949 isl_bool isl_set_is_params(__isl_keep isl_set *set)
951 if (!set)
952 return isl_bool_error;
953 return isl_space_is_params(set->dim);
956 /* Is this map actually a parameter domain?
957 * Users should never call this function. Outside of isl,
958 * a map can never be a parameter domain.
960 int isl_map_is_params(__isl_keep isl_map *map)
962 if (!map)
963 return -1;
964 return isl_space_is_params(map->dim);
967 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
968 struct isl_basic_map *bmap, unsigned extra,
969 unsigned n_eq, unsigned n_ineq)
971 int i;
972 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
974 bmap->ctx = ctx;
975 isl_ctx_ref(ctx);
977 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
978 if (isl_blk_is_error(bmap->block))
979 goto error;
981 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
982 if ((n_ineq + n_eq) && !bmap->ineq)
983 goto error;
985 if (extra == 0) {
986 bmap->block2 = isl_blk_empty();
987 bmap->div = NULL;
988 } else {
989 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
990 if (isl_blk_is_error(bmap->block2))
991 goto error;
993 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
994 if (!bmap->div)
995 goto error;
998 for (i = 0; i < n_ineq + n_eq; ++i)
999 bmap->ineq[i] = bmap->block.data + i * row_size;
1001 for (i = 0; i < extra; ++i)
1002 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1004 bmap->ref = 1;
1005 bmap->flags = 0;
1006 bmap->c_size = n_eq + n_ineq;
1007 bmap->eq = bmap->ineq + n_ineq;
1008 bmap->extra = extra;
1009 bmap->n_eq = 0;
1010 bmap->n_ineq = 0;
1011 bmap->n_div = 0;
1012 bmap->sample = NULL;
1014 return bmap;
1015 error:
1016 isl_basic_map_free(bmap);
1017 return NULL;
1020 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1021 unsigned nparam, unsigned dim, unsigned extra,
1022 unsigned n_eq, unsigned n_ineq)
1024 struct isl_basic_map *bmap;
1025 isl_space *space;
1027 space = isl_space_set_alloc(ctx, nparam, dim);
1028 if (!space)
1029 return NULL;
1031 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1032 return (struct isl_basic_set *)bmap;
1035 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1036 unsigned extra, unsigned n_eq, unsigned n_ineq)
1038 struct isl_basic_map *bmap;
1039 if (!dim)
1040 return NULL;
1041 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1042 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1043 return (struct isl_basic_set *)bmap;
1044 error:
1045 isl_space_free(dim);
1046 return NULL;
1049 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1050 unsigned extra, unsigned n_eq, unsigned n_ineq)
1052 struct isl_basic_map *bmap;
1054 if (!dim)
1055 return NULL;
1056 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1057 if (!bmap)
1058 goto error;
1059 bmap->dim = dim;
1061 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1062 error:
1063 isl_space_free(dim);
1064 return NULL;
1067 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1068 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1069 unsigned n_eq, unsigned n_ineq)
1071 struct isl_basic_map *bmap;
1072 isl_space *dim;
1074 dim = isl_space_alloc(ctx, nparam, in, out);
1075 if (!dim)
1076 return NULL;
1078 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1079 return bmap;
1082 static void dup_constraints(
1083 struct isl_basic_map *dst, struct isl_basic_map *src)
1085 int i;
1086 unsigned total = isl_basic_map_total_dim(src);
1088 for (i = 0; i < src->n_eq; ++i) {
1089 int j = isl_basic_map_alloc_equality(dst);
1090 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1093 for (i = 0; i < src->n_ineq; ++i) {
1094 int j = isl_basic_map_alloc_inequality(dst);
1095 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1098 for (i = 0; i < src->n_div; ++i) {
1099 int j = isl_basic_map_alloc_div(dst);
1100 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1102 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1105 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1107 struct isl_basic_map *dup;
1109 if (!bmap)
1110 return NULL;
1111 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1112 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1113 if (!dup)
1114 return NULL;
1115 dup_constraints(dup, bmap);
1116 dup->flags = bmap->flags;
1117 dup->sample = isl_vec_copy(bmap->sample);
1118 return dup;
1121 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1123 struct isl_basic_map *dup;
1125 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1126 return (struct isl_basic_set *)dup;
1129 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1131 if (!bset)
1132 return NULL;
1134 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1135 bset->ref++;
1136 return bset;
1138 return isl_basic_set_dup(bset);
1141 struct isl_set *isl_set_copy(struct isl_set *set)
1143 if (!set)
1144 return NULL;
1146 set->ref++;
1147 return set;
1150 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1152 if (!bmap)
1153 return NULL;
1155 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1156 bmap->ref++;
1157 return bmap;
1159 bmap = isl_basic_map_dup(bmap);
1160 if (bmap)
1161 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1162 return bmap;
1165 struct isl_map *isl_map_copy(struct isl_map *map)
1167 if (!map)
1168 return NULL;
1170 map->ref++;
1171 return map;
1174 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1176 if (!bmap)
1177 return NULL;
1179 if (--bmap->ref > 0)
1180 return NULL;
1182 isl_ctx_deref(bmap->ctx);
1183 free(bmap->div);
1184 isl_blk_free(bmap->ctx, bmap->block2);
1185 free(bmap->ineq);
1186 isl_blk_free(bmap->ctx, bmap->block);
1187 isl_vec_free(bmap->sample);
1188 isl_space_free(bmap->dim);
1189 free(bmap);
1191 return NULL;
1194 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1196 return isl_basic_map_free((struct isl_basic_map *)bset);
1199 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1201 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1204 __isl_give isl_map *isl_map_align_params_map_map_and(
1205 __isl_take isl_map *map1, __isl_take isl_map *map2,
1206 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1207 __isl_take isl_map *map2))
1209 if (!map1 || !map2)
1210 goto error;
1211 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1212 return fn(map1, map2);
1213 if (!isl_space_has_named_params(map1->dim) ||
1214 !isl_space_has_named_params(map2->dim))
1215 isl_die(map1->ctx, isl_error_invalid,
1216 "unaligned unnamed parameters", goto error);
1217 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1218 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1219 return fn(map1, map2);
1220 error:
1221 isl_map_free(map1);
1222 isl_map_free(map2);
1223 return NULL;
1226 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1227 __isl_keep isl_map *map2,
1228 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1230 isl_bool r;
1232 if (!map1 || !map2)
1233 return isl_bool_error;
1234 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1235 return fn(map1, map2);
1236 if (!isl_space_has_named_params(map1->dim) ||
1237 !isl_space_has_named_params(map2->dim))
1238 isl_die(map1->ctx, isl_error_invalid,
1239 "unaligned unnamed parameters", return isl_bool_error);
1240 map1 = isl_map_copy(map1);
1241 map2 = isl_map_copy(map2);
1242 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1243 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1244 r = fn(map1, map2);
1245 isl_map_free(map1);
1246 isl_map_free(map2);
1247 return r;
1250 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1252 struct isl_ctx *ctx;
1253 if (!bmap)
1254 return -1;
1255 ctx = bmap->ctx;
1256 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1257 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1258 return -1);
1259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1260 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1261 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1262 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1263 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1264 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1265 isl_int *t;
1266 int j = isl_basic_map_alloc_inequality(bmap);
1267 if (j < 0)
1268 return -1;
1269 t = bmap->ineq[j];
1270 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1271 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1272 bmap->eq[-1] = t;
1273 bmap->n_eq++;
1274 bmap->n_ineq--;
1275 bmap->eq--;
1276 return 0;
1278 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1279 bmap->extra - bmap->n_div);
1280 return bmap->n_eq++;
1283 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1285 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1288 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1290 if (!bmap)
1291 return -1;
1292 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1293 bmap->n_eq -= n;
1294 return 0;
1297 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1299 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1302 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1304 isl_int *t;
1305 if (!bmap)
1306 return -1;
1307 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1309 if (pos != bmap->n_eq - 1) {
1310 t = bmap->eq[pos];
1311 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1312 bmap->eq[bmap->n_eq - 1] = t;
1314 bmap->n_eq--;
1315 return 0;
1318 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1320 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1323 /* Turn inequality "pos" of "bmap" into an equality.
1325 * In particular, we move the inequality in front of the equalities
1326 * and move the last inequality in the position of the moved inequality.
1327 * Note that isl_tab_make_equalities_explicit depends on this particular
1328 * change in the ordering of the constraints.
1330 void isl_basic_map_inequality_to_equality(
1331 struct isl_basic_map *bmap, unsigned pos)
1333 isl_int *t;
1335 t = bmap->ineq[pos];
1336 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1337 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1338 bmap->eq[-1] = t;
1339 bmap->n_eq++;
1340 bmap->n_ineq--;
1341 bmap->eq--;
1342 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1343 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1344 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1345 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1348 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1350 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1353 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1355 struct isl_ctx *ctx;
1356 if (!bmap)
1357 return -1;
1358 ctx = bmap->ctx;
1359 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1360 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1361 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1362 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1363 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1364 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1365 1 + isl_basic_map_total_dim(bmap),
1366 bmap->extra - bmap->n_div);
1367 return bmap->n_ineq++;
1370 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1372 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1375 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1377 if (!bmap)
1378 return -1;
1379 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1380 bmap->n_ineq -= n;
1381 return 0;
1384 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1386 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1389 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1391 isl_int *t;
1392 if (!bmap)
1393 return -1;
1394 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1396 if (pos != bmap->n_ineq - 1) {
1397 t = bmap->ineq[pos];
1398 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1399 bmap->ineq[bmap->n_ineq - 1] = t;
1400 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1402 bmap->n_ineq--;
1403 return 0;
1406 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1408 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1411 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1412 isl_int *eq)
1414 int k;
1416 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1417 if (!bmap)
1418 return NULL;
1419 k = isl_basic_map_alloc_equality(bmap);
1420 if (k < 0)
1421 goto error;
1422 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1423 return bmap;
1424 error:
1425 isl_basic_map_free(bmap);
1426 return NULL;
1429 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1430 isl_int *eq)
1432 return (isl_basic_set *)
1433 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1436 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1437 isl_int *ineq)
1439 int k;
1441 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1442 if (!bmap)
1443 return NULL;
1444 k = isl_basic_map_alloc_inequality(bmap);
1445 if (k < 0)
1446 goto error;
1447 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1448 return bmap;
1449 error:
1450 isl_basic_map_free(bmap);
1451 return NULL;
1454 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1455 isl_int *ineq)
1457 return (isl_basic_set *)
1458 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1461 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1463 if (!bmap)
1464 return -1;
1465 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1466 isl_seq_clr(bmap->div[bmap->n_div] +
1467 1 + 1 + isl_basic_map_total_dim(bmap),
1468 bmap->extra - bmap->n_div);
1469 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1470 return bmap->n_div++;
1473 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1475 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1478 /* Insert an extra integer division, prescribed by "div", to "bmap"
1479 * at (integer division) position "pos".
1481 * The integer division is first added at the end and then moved
1482 * into the right position.
1484 __isl_give isl_basic_map *isl_basic_map_insert_div(
1485 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1487 int i, k, n_div;
1489 bmap = isl_basic_map_cow(bmap);
1490 if (!bmap || !div)
1491 return isl_basic_map_free(bmap);
1493 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1494 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1495 "unexpected size", return isl_basic_map_free(bmap));
1496 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1497 if (pos < 0 || pos > n_div)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "invalid position", return isl_basic_map_free(bmap));
1501 bmap = isl_basic_map_extend_space(bmap,
1502 isl_basic_map_get_space(bmap), 1, 0, 2);
1503 k = isl_basic_map_alloc_div(bmap);
1504 if (k < 0)
1505 return isl_basic_map_free(bmap);
1506 isl_seq_cpy(bmap->div[k], div->el, div->size);
1507 isl_int_set_si(bmap->div[k][div->size], 0);
1509 for (i = k; i > pos; --i)
1510 isl_basic_map_swap_div(bmap, i, i - 1);
1512 return bmap;
1515 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1517 if (!bmap)
1518 return -1;
1519 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1520 bmap->n_div -= n;
1521 return 0;
1524 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1526 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1529 /* Copy constraint from src to dst, putting the vars of src at offset
1530 * dim_off in dst and the divs of src at offset div_off in dst.
1531 * If both sets are actually map, then dim_off applies to the input
1532 * variables.
1534 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1535 struct isl_basic_map *src_map, isl_int *src,
1536 unsigned in_off, unsigned out_off, unsigned div_off)
1538 unsigned src_nparam = isl_basic_map_n_param(src_map);
1539 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1540 unsigned src_in = isl_basic_map_n_in(src_map);
1541 unsigned dst_in = isl_basic_map_n_in(dst_map);
1542 unsigned src_out = isl_basic_map_n_out(src_map);
1543 unsigned dst_out = isl_basic_map_n_out(dst_map);
1544 isl_int_set(dst[0], src[0]);
1545 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1546 if (dst_nparam > src_nparam)
1547 isl_seq_clr(dst+1+src_nparam,
1548 dst_nparam - src_nparam);
1549 isl_seq_clr(dst+1+dst_nparam, in_off);
1550 isl_seq_cpy(dst+1+dst_nparam+in_off,
1551 src+1+src_nparam,
1552 isl_min(dst_in-in_off, src_in));
1553 if (dst_in-in_off > src_in)
1554 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1555 dst_in - in_off - src_in);
1556 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1557 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1558 src+1+src_nparam+src_in,
1559 isl_min(dst_out-out_off, src_out));
1560 if (dst_out-out_off > src_out)
1561 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1562 dst_out - out_off - src_out);
1563 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1564 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1565 src+1+src_nparam+src_in+src_out,
1566 isl_min(dst_map->extra-div_off, src_map->n_div));
1567 if (dst_map->n_div-div_off > src_map->n_div)
1568 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1569 div_off+src_map->n_div,
1570 dst_map->n_div - div_off - src_map->n_div);
1573 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1574 struct isl_basic_map *src_map, isl_int *src,
1575 unsigned in_off, unsigned out_off, unsigned div_off)
1577 isl_int_set(dst[0], src[0]);
1578 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1581 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1582 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1584 int i;
1585 unsigned div_off;
1587 if (!bmap1 || !bmap2)
1588 goto error;
1590 div_off = bmap1->n_div;
1592 for (i = 0; i < bmap2->n_eq; ++i) {
1593 int i1 = isl_basic_map_alloc_equality(bmap1);
1594 if (i1 < 0)
1595 goto error;
1596 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1597 i_pos, o_pos, div_off);
1600 for (i = 0; i < bmap2->n_ineq; ++i) {
1601 int i1 = isl_basic_map_alloc_inequality(bmap1);
1602 if (i1 < 0)
1603 goto error;
1604 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1605 i_pos, o_pos, div_off);
1608 for (i = 0; i < bmap2->n_div; ++i) {
1609 int i1 = isl_basic_map_alloc_div(bmap1);
1610 if (i1 < 0)
1611 goto error;
1612 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1613 i_pos, o_pos, div_off);
1616 isl_basic_map_free(bmap2);
1618 return bmap1;
1620 error:
1621 isl_basic_map_free(bmap1);
1622 isl_basic_map_free(bmap2);
1623 return NULL;
1626 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1627 struct isl_basic_set *bset2, unsigned pos)
1629 return (struct isl_basic_set *)
1630 add_constraints((struct isl_basic_map *)bset1,
1631 (struct isl_basic_map *)bset2, 0, pos);
1634 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1635 __isl_take isl_space *dim, unsigned extra,
1636 unsigned n_eq, unsigned n_ineq)
1638 struct isl_basic_map *ext;
1639 unsigned flags;
1640 int dims_ok;
1642 if (!dim)
1643 goto error;
1645 if (!base)
1646 goto error;
1648 dims_ok = isl_space_is_equal(base->dim, dim) &&
1649 base->extra >= base->n_div + extra;
1651 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1652 room_for_ineq(base, n_ineq)) {
1653 isl_space_free(dim);
1654 return base;
1657 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1658 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1659 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1660 extra += base->extra;
1661 n_eq += base->n_eq;
1662 n_ineq += base->n_ineq;
1664 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1665 dim = NULL;
1666 if (!ext)
1667 goto error;
1669 if (dims_ok)
1670 ext->sample = isl_vec_copy(base->sample);
1671 flags = base->flags;
1672 ext = add_constraints(ext, base, 0, 0);
1673 if (ext) {
1674 ext->flags = flags;
1675 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1678 return ext;
1680 error:
1681 isl_space_free(dim);
1682 isl_basic_map_free(base);
1683 return NULL;
1686 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1687 __isl_take isl_space *dim, unsigned extra,
1688 unsigned n_eq, unsigned n_ineq)
1690 return (struct isl_basic_set *)
1691 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1692 extra, n_eq, n_ineq);
1695 struct isl_basic_map *isl_basic_map_extend_constraints(
1696 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1698 if (!base)
1699 return NULL;
1700 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1701 0, n_eq, n_ineq);
1704 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1705 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1706 unsigned n_eq, unsigned n_ineq)
1708 struct isl_basic_map *bmap;
1709 isl_space *dim;
1711 if (!base)
1712 return NULL;
1713 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1714 if (!dim)
1715 goto error;
1717 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1718 return bmap;
1719 error:
1720 isl_basic_map_free(base);
1721 return NULL;
1724 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1725 unsigned nparam, unsigned dim, unsigned extra,
1726 unsigned n_eq, unsigned n_ineq)
1728 return (struct isl_basic_set *)
1729 isl_basic_map_extend((struct isl_basic_map *)base,
1730 nparam, 0, dim, extra, n_eq, n_ineq);
1733 struct isl_basic_set *isl_basic_set_extend_constraints(
1734 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1736 return (struct isl_basic_set *)
1737 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1738 n_eq, n_ineq);
1741 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1743 return (struct isl_basic_set *)
1744 isl_basic_map_cow((struct isl_basic_map *)bset);
1747 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1749 if (!bmap)
1750 return NULL;
1752 if (bmap->ref > 1) {
1753 bmap->ref--;
1754 bmap = isl_basic_map_dup(bmap);
1756 if (bmap) {
1757 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1758 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1760 return bmap;
1763 /* Clear all cached information in "map", either because it is about
1764 * to be modified or because it is being freed.
1765 * Always return the same pointer that is passed in.
1766 * This is needed for the use in isl_map_free.
1768 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1770 isl_basic_map_free(map->cached_simple_hull[0]);
1771 isl_basic_map_free(map->cached_simple_hull[1]);
1772 map->cached_simple_hull[0] = NULL;
1773 map->cached_simple_hull[1] = NULL;
1774 return map;
1777 struct isl_set *isl_set_cow(struct isl_set *set)
1779 return isl_map_cow(set);
1782 /* Return an isl_map that is equal to "map" and that has only
1783 * a single reference.
1785 * If the original input already has only one reference, then
1786 * simply return it, but clear all cached information, since
1787 * it may be rendered invalid by the operations that will be
1788 * performed on the result.
1790 * Otherwise, create a duplicate (without any cached information).
1792 struct isl_map *isl_map_cow(struct isl_map *map)
1794 if (!map)
1795 return NULL;
1797 if (map->ref == 1)
1798 return clear_caches(map);
1799 map->ref--;
1800 return isl_map_dup(map);
1803 static void swap_vars(struct isl_blk blk, isl_int *a,
1804 unsigned a_len, unsigned b_len)
1806 isl_seq_cpy(blk.data, a+a_len, b_len);
1807 isl_seq_cpy(blk.data+b_len, a, a_len);
1808 isl_seq_cpy(a, blk.data, b_len+a_len);
1811 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1812 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1814 int i;
1815 struct isl_blk blk;
1817 if (!bmap)
1818 goto error;
1820 isl_assert(bmap->ctx,
1821 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1823 if (n1 == 0 || n2 == 0)
1824 return bmap;
1826 bmap = isl_basic_map_cow(bmap);
1827 if (!bmap)
1828 return NULL;
1830 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1831 if (isl_blk_is_error(blk))
1832 goto error;
1834 for (i = 0; i < bmap->n_eq; ++i)
1835 swap_vars(blk,
1836 bmap->eq[i] + pos, n1, n2);
1838 for (i = 0; i < bmap->n_ineq; ++i)
1839 swap_vars(blk,
1840 bmap->ineq[i] + pos, n1, n2);
1842 for (i = 0; i < bmap->n_div; ++i)
1843 swap_vars(blk,
1844 bmap->div[i]+1 + pos, n1, n2);
1846 isl_blk_free(bmap->ctx, blk);
1848 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1849 bmap = isl_basic_map_gauss(bmap, NULL);
1850 return isl_basic_map_finalize(bmap);
1851 error:
1852 isl_basic_map_free(bmap);
1853 return NULL;
1856 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1858 int i = 0;
1859 unsigned total;
1860 if (!bmap)
1861 goto error;
1862 total = isl_basic_map_total_dim(bmap);
1863 isl_basic_map_free_div(bmap, bmap->n_div);
1864 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1865 if (bmap->n_eq > 0)
1866 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1867 else {
1868 i = isl_basic_map_alloc_equality(bmap);
1869 if (i < 0)
1870 goto error;
1872 isl_int_set_si(bmap->eq[i][0], 1);
1873 isl_seq_clr(bmap->eq[i]+1, total);
1874 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1875 isl_vec_free(bmap->sample);
1876 bmap->sample = NULL;
1877 return isl_basic_map_finalize(bmap);
1878 error:
1879 isl_basic_map_free(bmap);
1880 return NULL;
1883 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1885 return (struct isl_basic_set *)
1886 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1889 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1890 * of "bmap").
1892 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1894 isl_int *t = bmap->div[a];
1895 bmap->div[a] = bmap->div[b];
1896 bmap->div[b] = t;
1899 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1900 * div definitions accordingly.
1902 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1904 int i;
1905 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1907 swap_div(bmap, a, b);
1909 for (i = 0; i < bmap->n_eq; ++i)
1910 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1912 for (i = 0; i < bmap->n_ineq; ++i)
1913 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1915 for (i = 0; i < bmap->n_div; ++i)
1916 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1917 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1920 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1921 * div definitions accordingly.
1923 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1925 isl_basic_map_swap_div(bset, a, b);
1928 /* Eliminate the specified n dimensions starting at first from the
1929 * constraints, without removing the dimensions from the space.
1930 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1932 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1933 enum isl_dim_type type, unsigned first, unsigned n)
1935 int i;
1937 if (!map)
1938 return NULL;
1939 if (n == 0)
1940 return map;
1942 if (first + n > isl_map_dim(map, type) || first + n < first)
1943 isl_die(map->ctx, isl_error_invalid,
1944 "index out of bounds", goto error);
1946 map = isl_map_cow(map);
1947 if (!map)
1948 return NULL;
1950 for (i = 0; i < map->n; ++i) {
1951 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1952 if (!map->p[i])
1953 goto error;
1955 return map;
1956 error:
1957 isl_map_free(map);
1958 return NULL;
1961 /* Eliminate the specified n dimensions starting at first from the
1962 * constraints, without removing the dimensions from the space.
1963 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1965 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1966 enum isl_dim_type type, unsigned first, unsigned n)
1968 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1971 /* Eliminate the specified n dimensions starting at first from the
1972 * constraints, without removing the dimensions from the space.
1973 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1975 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1976 unsigned first, unsigned n)
1978 return isl_set_eliminate(set, isl_dim_set, first, n);
1981 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1982 __isl_take isl_basic_map *bmap)
1984 if (!bmap)
1985 return NULL;
1986 bmap = isl_basic_map_eliminate_vars(bmap,
1987 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1988 if (!bmap)
1989 return NULL;
1990 bmap->n_div = 0;
1991 return isl_basic_map_finalize(bmap);
1994 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1995 __isl_take isl_basic_set *bset)
1997 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1998 (struct isl_basic_map *)bset);
2001 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2003 int i;
2005 if (!map)
2006 return NULL;
2007 if (map->n == 0)
2008 return map;
2010 map = isl_map_cow(map);
2011 if (!map)
2012 return NULL;
2014 for (i = 0; i < map->n; ++i) {
2015 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2016 if (!map->p[i])
2017 goto error;
2019 return map;
2020 error:
2021 isl_map_free(map);
2022 return NULL;
2025 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2027 return isl_map_remove_divs(set);
2030 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2031 enum isl_dim_type type, unsigned first, unsigned n)
2033 if (!bmap)
2034 return NULL;
2035 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2036 goto error);
2037 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2038 return bmap;
2039 bmap = isl_basic_map_eliminate_vars(bmap,
2040 isl_basic_map_offset(bmap, type) - 1 + first, n);
2041 if (!bmap)
2042 return bmap;
2043 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2044 return bmap;
2045 bmap = isl_basic_map_drop(bmap, type, first, n);
2046 return bmap;
2047 error:
2048 isl_basic_map_free(bmap);
2049 return NULL;
2052 /* Return true if the definition of the given div (recursively) involves
2053 * any of the given variables.
2055 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2056 unsigned first, unsigned n)
2058 int i;
2059 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2061 if (isl_int_is_zero(bmap->div[div][0]))
2062 return 0;
2063 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2064 return 1;
2066 for (i = bmap->n_div - 1; i >= 0; --i) {
2067 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2068 continue;
2069 if (div_involves_vars(bmap, i, first, n))
2070 return 1;
2073 return 0;
2076 /* Try and add a lower and/or upper bound on "div" to "bmap"
2077 * based on inequality "i".
2078 * "total" is the total number of variables (excluding the divs).
2079 * "v" is a temporary object that can be used during the calculations.
2080 * If "lb" is set, then a lower bound should be constructed.
2081 * If "ub" is set, then an upper bound should be constructed.
2083 * The calling function has already checked that the inequality does not
2084 * reference "div", but we still need to check that the inequality is
2085 * of the right form. We'll consider the case where we want to construct
2086 * a lower bound. The construction of upper bounds is similar.
2088 * Let "div" be of the form
2090 * q = floor((a + f(x))/d)
2092 * We essentially check if constraint "i" is of the form
2094 * b + f(x) >= 0
2096 * so that we can use it to derive a lower bound on "div".
2097 * However, we allow a slightly more general form
2099 * b + g(x) >= 0
2101 * with the condition that the coefficients of g(x) - f(x) are all
2102 * divisible by d.
2103 * Rewriting this constraint as
2105 * 0 >= -b - g(x)
2107 * adding a + f(x) to both sides and dividing by d, we obtain
2109 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2111 * Taking the floor on both sides, we obtain
2113 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2115 * or
2117 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2119 * In the case of an upper bound, we construct the constraint
2121 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2124 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2125 __isl_take isl_basic_map *bmap, int div, int i,
2126 unsigned total, isl_int v, int lb, int ub)
2128 int j;
2130 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2131 if (lb) {
2132 isl_int_sub(v, bmap->ineq[i][1 + j],
2133 bmap->div[div][1 + 1 + j]);
2134 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2136 if (ub) {
2137 isl_int_add(v, bmap->ineq[i][1 + j],
2138 bmap->div[div][1 + 1 + j]);
2139 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2142 if (!lb && !ub)
2143 return bmap;
2145 bmap = isl_basic_map_cow(bmap);
2146 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2147 if (lb) {
2148 int k = isl_basic_map_alloc_inequality(bmap);
2149 if (k < 0)
2150 goto error;
2151 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2152 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2153 bmap->div[div][1 + j]);
2154 isl_int_cdiv_q(bmap->ineq[k][j],
2155 bmap->ineq[k][j], bmap->div[div][0]);
2157 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2159 if (ub) {
2160 int k = isl_basic_map_alloc_inequality(bmap);
2161 if (k < 0)
2162 goto error;
2163 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2164 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2165 bmap->div[div][1 + j]);
2166 isl_int_fdiv_q(bmap->ineq[k][j],
2167 bmap->ineq[k][j], bmap->div[div][0]);
2169 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2172 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2173 return bmap;
2174 error:
2175 isl_basic_map_free(bmap);
2176 return NULL;
2179 /* This function is called right before "div" is eliminated from "bmap"
2180 * using Fourier-Motzkin.
2181 * Look through the constraints of "bmap" for constraints on the argument
2182 * of the integer division and use them to construct constraints on the
2183 * integer division itself. These constraints can then be combined
2184 * during the Fourier-Motzkin elimination.
2185 * Note that it is only useful to introduce lower bounds on "div"
2186 * if "bmap" already contains upper bounds on "div" as the newly
2187 * introduce lower bounds can then be combined with the pre-existing
2188 * upper bounds. Similarly for upper bounds.
2189 * We therefore first check if "bmap" contains any lower and/or upper bounds
2190 * on "div".
2192 * It is interesting to note that the introduction of these constraints
2193 * can indeed lead to more accurate results, even when compared to
2194 * deriving constraints on the argument of "div" from constraints on "div".
2195 * Consider, for example, the set
2197 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2199 * The second constraint can be rewritten as
2201 * 2 * [(-i-2j+3)/4] + k >= 0
2203 * from which we can derive
2205 * -i - 2j + 3 >= -2k
2207 * or
2209 * i + 2j <= 3 + 2k
2211 * Combined with the first constraint, we obtain
2213 * -3 <= 3 + 2k or k >= -3
2215 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2216 * the first constraint, we obtain
2218 * [(i + 2j)/4] >= [-3/4] = -1
2220 * Combining this constraint with the second constraint, we obtain
2222 * k >= -2
2224 static __isl_give isl_basic_map *insert_bounds_on_div(
2225 __isl_take isl_basic_map *bmap, int div)
2227 int i;
2228 int check_lb, check_ub;
2229 isl_int v;
2230 unsigned total;
2232 if (!bmap)
2233 return NULL;
2235 if (isl_int_is_zero(bmap->div[div][0]))
2236 return bmap;
2238 total = isl_space_dim(bmap->dim, isl_dim_all);
2240 check_lb = 0;
2241 check_ub = 0;
2242 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2243 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2244 if (s > 0)
2245 check_ub = 1;
2246 if (s < 0)
2247 check_lb = 1;
2250 if (!check_lb && !check_ub)
2251 return bmap;
2253 isl_int_init(v);
2255 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2256 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2257 continue;
2259 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2260 check_lb, check_ub);
2263 isl_int_clear(v);
2265 return bmap;
2268 /* Remove all divs (recursively) involving any of the given dimensions
2269 * in their definitions.
2271 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2272 __isl_take isl_basic_map *bmap,
2273 enum isl_dim_type type, unsigned first, unsigned n)
2275 int i;
2277 if (!bmap)
2278 return NULL;
2279 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2280 goto error);
2281 first += isl_basic_map_offset(bmap, type);
2283 for (i = bmap->n_div - 1; i >= 0; --i) {
2284 if (!div_involves_vars(bmap, i, first, n))
2285 continue;
2286 bmap = insert_bounds_on_div(bmap, i);
2287 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2288 if (!bmap)
2289 return NULL;
2290 i = bmap->n_div;
2293 return bmap;
2294 error:
2295 isl_basic_map_free(bmap);
2296 return NULL;
2299 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2300 __isl_take isl_basic_set *bset,
2301 enum isl_dim_type type, unsigned first, unsigned n)
2303 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2306 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2309 int i;
2311 if (!map)
2312 return NULL;
2313 if (map->n == 0)
2314 return map;
2316 map = isl_map_cow(map);
2317 if (!map)
2318 return NULL;
2320 for (i = 0; i < map->n; ++i) {
2321 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2322 type, first, n);
2323 if (!map->p[i])
2324 goto error;
2326 return map;
2327 error:
2328 isl_map_free(map);
2329 return NULL;
2332 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2333 enum isl_dim_type type, unsigned first, unsigned n)
2335 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2336 type, first, n);
2339 /* Does the desciption of "bmap" depend on the specified dimensions?
2340 * We also check whether the dimensions appear in any of the div definitions.
2341 * In principle there is no need for this check. If the dimensions appear
2342 * in a div definition, they also appear in the defining constraints of that
2343 * div.
2345 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2346 enum isl_dim_type type, unsigned first, unsigned n)
2348 int i;
2350 if (!bmap)
2351 return isl_bool_error;
2353 if (first + n > isl_basic_map_dim(bmap, type))
2354 isl_die(bmap->ctx, isl_error_invalid,
2355 "index out of bounds", return isl_bool_error);
2357 first += isl_basic_map_offset(bmap, type);
2358 for (i = 0; i < bmap->n_eq; ++i)
2359 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2360 return isl_bool_true;
2361 for (i = 0; i < bmap->n_ineq; ++i)
2362 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2363 return isl_bool_true;
2364 for (i = 0; i < bmap->n_div; ++i) {
2365 if (isl_int_is_zero(bmap->div[i][0]))
2366 continue;
2367 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2368 return isl_bool_true;
2371 return isl_bool_false;
2374 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2375 enum isl_dim_type type, unsigned first, unsigned n)
2377 int i;
2379 if (!map)
2380 return isl_bool_error;
2382 if (first + n > isl_map_dim(map, type))
2383 isl_die(map->ctx, isl_error_invalid,
2384 "index out of bounds", return isl_bool_error);
2386 for (i = 0; i < map->n; ++i) {
2387 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2388 type, first, n);
2389 if (involves < 0 || involves)
2390 return involves;
2393 return isl_bool_false;
2396 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2397 enum isl_dim_type type, unsigned first, unsigned n)
2399 return isl_basic_map_involves_dims(bset, type, first, n);
2402 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2403 enum isl_dim_type type, unsigned first, unsigned n)
2405 return isl_map_involves_dims(set, type, first, n);
2408 /* Does local variable "div" of "bmap" have a complete explicit representation?
2409 * Having a complete explicit representation requires not only
2410 * an explicit representation, but also that all local variables
2411 * that appear in this explicit representation in turn have
2412 * a complete explicit representation.
2414 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2416 int i;
2417 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2418 isl_bool marked;
2420 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2421 if (marked < 0 || marked)
2422 return isl_bool_not(marked);
2424 for (i = bmap->n_div - 1; i >= 0; --i) {
2425 isl_bool known;
2427 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2428 continue;
2429 known = isl_basic_map_div_is_known(bmap, i);
2430 if (known < 0 || !known)
2431 return known;
2434 return isl_bool_true;
2437 /* Does local variable "div" of "bset" have a complete explicit representation?
2439 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2441 return isl_basic_map_div_is_known(bset, div);
2444 /* Remove all divs that are unknown or defined in terms of unknown divs.
2446 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2447 __isl_take isl_basic_map *bmap)
2449 int i;
2451 if (!bmap)
2452 return NULL;
2454 for (i = bmap->n_div - 1; i >= 0; --i) {
2455 if (isl_basic_map_div_is_known(bmap, i))
2456 continue;
2457 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2458 if (!bmap)
2459 return NULL;
2460 i = bmap->n_div;
2463 return bmap;
2466 /* Remove all divs that are unknown or defined in terms of unknown divs.
2468 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2469 __isl_take isl_basic_set *bset)
2471 return isl_basic_map_remove_unknown_divs(bset);
2474 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2476 int i;
2478 if (!map)
2479 return NULL;
2480 if (map->n == 0)
2481 return map;
2483 map = isl_map_cow(map);
2484 if (!map)
2485 return NULL;
2487 for (i = 0; i < map->n; ++i) {
2488 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2489 if (!map->p[i])
2490 goto error;
2492 return map;
2493 error:
2494 isl_map_free(map);
2495 return NULL;
2498 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2500 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2503 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2504 __isl_take isl_basic_set *bset,
2505 enum isl_dim_type type, unsigned first, unsigned n)
2507 return (isl_basic_set *)
2508 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2511 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2512 enum isl_dim_type type, unsigned first, unsigned n)
2514 int i;
2516 if (n == 0)
2517 return map;
2519 map = isl_map_cow(map);
2520 if (!map)
2521 return NULL;
2522 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2524 for (i = 0; i < map->n; ++i) {
2525 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2526 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2527 if (!map->p[i])
2528 goto error;
2530 map = isl_map_drop(map, type, first, n);
2531 return map;
2532 error:
2533 isl_map_free(map);
2534 return NULL;
2537 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2538 enum isl_dim_type type, unsigned first, unsigned n)
2540 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2543 /* Project out n inputs starting at first using Fourier-Motzkin */
2544 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2545 unsigned first, unsigned n)
2547 return isl_map_remove_dims(map, isl_dim_in, first, n);
2550 static void dump_term(struct isl_basic_map *bmap,
2551 isl_int c, int pos, FILE *out)
2553 const char *name;
2554 unsigned in = isl_basic_map_n_in(bmap);
2555 unsigned dim = in + isl_basic_map_n_out(bmap);
2556 unsigned nparam = isl_basic_map_n_param(bmap);
2557 if (!pos)
2558 isl_int_print(out, c, 0);
2559 else {
2560 if (!isl_int_is_one(c))
2561 isl_int_print(out, c, 0);
2562 if (pos < 1 + nparam) {
2563 name = isl_space_get_dim_name(bmap->dim,
2564 isl_dim_param, pos - 1);
2565 if (name)
2566 fprintf(out, "%s", name);
2567 else
2568 fprintf(out, "p%d", pos - 1);
2569 } else if (pos < 1 + nparam + in)
2570 fprintf(out, "i%d", pos - 1 - nparam);
2571 else if (pos < 1 + nparam + dim)
2572 fprintf(out, "o%d", pos - 1 - nparam - in);
2573 else
2574 fprintf(out, "e%d", pos - 1 - nparam - dim);
2578 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2579 int sign, FILE *out)
2581 int i;
2582 int first;
2583 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2584 isl_int v;
2586 isl_int_init(v);
2587 for (i = 0, first = 1; i < len; ++i) {
2588 if (isl_int_sgn(c[i]) * sign <= 0)
2589 continue;
2590 if (!first)
2591 fprintf(out, " + ");
2592 first = 0;
2593 isl_int_abs(v, c[i]);
2594 dump_term(bmap, v, i, out);
2596 isl_int_clear(v);
2597 if (first)
2598 fprintf(out, "0");
2601 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2602 const char *op, FILE *out, int indent)
2604 int i;
2606 fprintf(out, "%*s", indent, "");
2608 dump_constraint_sign(bmap, c, 1, out);
2609 fprintf(out, " %s ", op);
2610 dump_constraint_sign(bmap, c, -1, out);
2612 fprintf(out, "\n");
2614 for (i = bmap->n_div; i < bmap->extra; ++i) {
2615 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2616 continue;
2617 fprintf(out, "%*s", indent, "");
2618 fprintf(out, "ERROR: unused div coefficient not zero\n");
2619 abort();
2623 static void dump_constraints(struct isl_basic_map *bmap,
2624 isl_int **c, unsigned n,
2625 const char *op, FILE *out, int indent)
2627 int i;
2629 for (i = 0; i < n; ++i)
2630 dump_constraint(bmap, c[i], op, out, indent);
2633 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2635 int j;
2636 int first = 1;
2637 unsigned total = isl_basic_map_total_dim(bmap);
2639 for (j = 0; j < 1 + total; ++j) {
2640 if (isl_int_is_zero(exp[j]))
2641 continue;
2642 if (!first && isl_int_is_pos(exp[j]))
2643 fprintf(out, "+");
2644 dump_term(bmap, exp[j], j, out);
2645 first = 0;
2649 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2651 int i;
2653 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2654 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2656 for (i = 0; i < bmap->n_div; ++i) {
2657 fprintf(out, "%*s", indent, "");
2658 fprintf(out, "e%d = [(", i);
2659 dump_affine(bmap, bmap->div[i]+1, out);
2660 fprintf(out, ")/");
2661 isl_int_print(out, bmap->div[i][0], 0);
2662 fprintf(out, "]\n");
2666 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2667 FILE *out, int indent)
2669 if (!bset) {
2670 fprintf(out, "null basic set\n");
2671 return;
2674 fprintf(out, "%*s", indent, "");
2675 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2676 bset->ref, bset->dim->nparam, bset->dim->n_out,
2677 bset->extra, bset->flags);
2678 dump((struct isl_basic_map *)bset, out, indent);
2681 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2682 FILE *out, int indent)
2684 if (!bmap) {
2685 fprintf(out, "null basic map\n");
2686 return;
2689 fprintf(out, "%*s", indent, "");
2690 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2691 "flags: %x, n_name: %d\n",
2692 bmap->ref,
2693 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2694 bmap->extra, bmap->flags, bmap->dim->n_id);
2695 dump(bmap, out, indent);
2698 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2700 unsigned total;
2701 if (!bmap)
2702 return -1;
2703 total = isl_basic_map_total_dim(bmap);
2704 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2705 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2706 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2707 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2708 return 0;
2711 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2712 unsigned flags)
2714 if (!space)
2715 return NULL;
2716 if (isl_space_dim(space, isl_dim_in) != 0)
2717 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2718 "set cannot have input dimensions", goto error);
2719 return isl_map_alloc_space(space, n, flags);
2720 error:
2721 isl_space_free(space);
2722 return NULL;
2725 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2726 unsigned nparam, unsigned dim, int n, unsigned flags)
2728 struct isl_set *set;
2729 isl_space *dims;
2731 dims = isl_space_alloc(ctx, nparam, 0, dim);
2732 if (!dims)
2733 return NULL;
2735 set = isl_set_alloc_space(dims, n, flags);
2736 return set;
2739 /* Make sure "map" has room for at least "n" more basic maps.
2741 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2743 int i;
2744 struct isl_map *grown = NULL;
2746 if (!map)
2747 return NULL;
2748 isl_assert(map->ctx, n >= 0, goto error);
2749 if (map->n + n <= map->size)
2750 return map;
2751 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2752 if (!grown)
2753 goto error;
2754 for (i = 0; i < map->n; ++i) {
2755 grown->p[i] = isl_basic_map_copy(map->p[i]);
2756 if (!grown->p[i])
2757 goto error;
2758 grown->n++;
2760 isl_map_free(map);
2761 return grown;
2762 error:
2763 isl_map_free(grown);
2764 isl_map_free(map);
2765 return NULL;
2768 /* Make sure "set" has room for at least "n" more basic sets.
2770 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2772 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2775 struct isl_set *isl_set_dup(struct isl_set *set)
2777 int i;
2778 struct isl_set *dup;
2780 if (!set)
2781 return NULL;
2783 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2784 if (!dup)
2785 return NULL;
2786 for (i = 0; i < set->n; ++i)
2787 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2788 return dup;
2791 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2793 return isl_map_from_basic_map(bset);
2796 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2798 struct isl_map *map;
2800 if (!bmap)
2801 return NULL;
2803 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2804 return isl_map_add_basic_map(map, bmap);
2807 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2808 __isl_take isl_basic_set *bset)
2810 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2811 (struct isl_basic_map *)bset);
2814 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2816 return isl_map_free(set);
2819 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2821 int i;
2823 if (!set) {
2824 fprintf(out, "null set\n");
2825 return;
2828 fprintf(out, "%*s", indent, "");
2829 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2830 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2831 set->flags);
2832 for (i = 0; i < set->n; ++i) {
2833 fprintf(out, "%*s", indent, "");
2834 fprintf(out, "basic set %d:\n", i);
2835 isl_basic_set_print_internal(set->p[i], out, indent+4);
2839 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2841 int i;
2843 if (!map) {
2844 fprintf(out, "null map\n");
2845 return;
2848 fprintf(out, "%*s", indent, "");
2849 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2850 "flags: %x, n_name: %d\n",
2851 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2852 map->dim->n_out, map->flags, map->dim->n_id);
2853 for (i = 0; i < map->n; ++i) {
2854 fprintf(out, "%*s", indent, "");
2855 fprintf(out, "basic map %d:\n", i);
2856 isl_basic_map_print_internal(map->p[i], out, indent+4);
2860 struct isl_basic_map *isl_basic_map_intersect_domain(
2861 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2863 struct isl_basic_map *bmap_domain;
2865 if (!bmap || !bset)
2866 goto error;
2868 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2869 bset->dim, isl_dim_param), goto error);
2871 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2872 isl_assert(bset->ctx,
2873 isl_basic_map_compatible_domain(bmap, bset), goto error);
2875 bmap = isl_basic_map_cow(bmap);
2876 if (!bmap)
2877 goto error;
2878 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2879 bset->n_div, bset->n_eq, bset->n_ineq);
2880 bmap_domain = isl_basic_map_from_domain(bset);
2881 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2883 bmap = isl_basic_map_simplify(bmap);
2884 return isl_basic_map_finalize(bmap);
2885 error:
2886 isl_basic_map_free(bmap);
2887 isl_basic_set_free(bset);
2888 return NULL;
2891 struct isl_basic_map *isl_basic_map_intersect_range(
2892 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2894 struct isl_basic_map *bmap_range;
2896 if (!bmap || !bset)
2897 goto error;
2899 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2900 bset->dim, isl_dim_param), goto error);
2902 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2903 isl_assert(bset->ctx,
2904 isl_basic_map_compatible_range(bmap, bset), goto error);
2906 if (isl_basic_set_plain_is_universe(bset)) {
2907 isl_basic_set_free(bset);
2908 return bmap;
2911 bmap = isl_basic_map_cow(bmap);
2912 if (!bmap)
2913 goto error;
2914 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2915 bset->n_div, bset->n_eq, bset->n_ineq);
2916 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2917 bmap = add_constraints(bmap, bmap_range, 0, 0);
2919 bmap = isl_basic_map_simplify(bmap);
2920 return isl_basic_map_finalize(bmap);
2921 error:
2922 isl_basic_map_free(bmap);
2923 isl_basic_set_free(bset);
2924 return NULL;
2927 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2928 __isl_keep isl_vec *vec)
2930 int i;
2931 unsigned total;
2932 isl_int s;
2934 if (!bmap || !vec)
2935 return isl_bool_error;
2937 total = 1 + isl_basic_map_total_dim(bmap);
2938 if (total != vec->size)
2939 return isl_bool_error;
2941 isl_int_init(s);
2943 for (i = 0; i < bmap->n_eq; ++i) {
2944 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2945 if (!isl_int_is_zero(s)) {
2946 isl_int_clear(s);
2947 return isl_bool_false;
2951 for (i = 0; i < bmap->n_ineq; ++i) {
2952 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2953 if (isl_int_is_neg(s)) {
2954 isl_int_clear(s);
2955 return isl_bool_false;
2959 isl_int_clear(s);
2961 return isl_bool_true;
2964 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2965 __isl_keep isl_vec *vec)
2967 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2970 struct isl_basic_map *isl_basic_map_intersect(
2971 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2973 struct isl_vec *sample = NULL;
2975 if (!bmap1 || !bmap2)
2976 goto error;
2978 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2979 bmap2->dim, isl_dim_param), goto error);
2980 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2981 isl_space_dim(bmap1->dim, isl_dim_param) &&
2982 isl_space_dim(bmap2->dim, isl_dim_all) !=
2983 isl_space_dim(bmap2->dim, isl_dim_param))
2984 return isl_basic_map_intersect(bmap2, bmap1);
2986 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2987 isl_space_dim(bmap2->dim, isl_dim_param))
2988 isl_assert(bmap1->ctx,
2989 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2991 if (isl_basic_map_plain_is_empty(bmap1)) {
2992 isl_basic_map_free(bmap2);
2993 return bmap1;
2995 if (isl_basic_map_plain_is_empty(bmap2)) {
2996 isl_basic_map_free(bmap1);
2997 return bmap2;
3000 if (bmap1->sample &&
3001 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3002 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3003 sample = isl_vec_copy(bmap1->sample);
3004 else if (bmap2->sample &&
3005 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3006 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3007 sample = isl_vec_copy(bmap2->sample);
3009 bmap1 = isl_basic_map_cow(bmap1);
3010 if (!bmap1)
3011 goto error;
3012 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3013 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3014 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3016 if (!bmap1)
3017 isl_vec_free(sample);
3018 else if (sample) {
3019 isl_vec_free(bmap1->sample);
3020 bmap1->sample = sample;
3023 bmap1 = isl_basic_map_simplify(bmap1);
3024 return isl_basic_map_finalize(bmap1);
3025 error:
3026 if (sample)
3027 isl_vec_free(sample);
3028 isl_basic_map_free(bmap1);
3029 isl_basic_map_free(bmap2);
3030 return NULL;
3033 struct isl_basic_set *isl_basic_set_intersect(
3034 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3036 return (struct isl_basic_set *)
3037 isl_basic_map_intersect(
3038 (struct isl_basic_map *)bset1,
3039 (struct isl_basic_map *)bset2);
3042 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3043 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3045 return isl_basic_set_intersect(bset1, bset2);
3048 /* Special case of isl_map_intersect, where both map1 and map2
3049 * are convex, without any divs and such that either map1 or map2
3050 * contains a single constraint. This constraint is then simply
3051 * added to the other map.
3053 static __isl_give isl_map *map_intersect_add_constraint(
3054 __isl_take isl_map *map1, __isl_take isl_map *map2)
3056 isl_assert(map1->ctx, map1->n == 1, goto error);
3057 isl_assert(map2->ctx, map1->n == 1, goto error);
3058 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3059 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3061 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3062 return isl_map_intersect(map2, map1);
3064 isl_assert(map2->ctx,
3065 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3067 map1 = isl_map_cow(map1);
3068 if (!map1)
3069 goto error;
3070 if (isl_map_plain_is_empty(map1)) {
3071 isl_map_free(map2);
3072 return map1;
3074 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3075 if (map2->p[0]->n_eq == 1)
3076 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3077 else
3078 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3079 map2->p[0]->ineq[0]);
3081 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3082 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3083 if (!map1->p[0])
3084 goto error;
3086 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3087 isl_basic_map_free(map1->p[0]);
3088 map1->n = 0;
3091 isl_map_free(map2);
3093 return map1;
3094 error:
3095 isl_map_free(map1);
3096 isl_map_free(map2);
3097 return NULL;
3100 /* map2 may be either a parameter domain or a map living in the same
3101 * space as map1.
3103 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3104 __isl_take isl_map *map2)
3106 unsigned flags = 0;
3107 isl_map *result;
3108 int i, j;
3110 if (!map1 || !map2)
3111 goto error;
3113 if ((isl_map_plain_is_empty(map1) ||
3114 isl_map_plain_is_universe(map2)) &&
3115 isl_space_is_equal(map1->dim, map2->dim)) {
3116 isl_map_free(map2);
3117 return map1;
3119 if ((isl_map_plain_is_empty(map2) ||
3120 isl_map_plain_is_universe(map1)) &&
3121 isl_space_is_equal(map1->dim, map2->dim)) {
3122 isl_map_free(map1);
3123 return map2;
3126 if (map1->n == 1 && map2->n == 1 &&
3127 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3128 isl_space_is_equal(map1->dim, map2->dim) &&
3129 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3130 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3131 return map_intersect_add_constraint(map1, map2);
3133 if (isl_space_dim(map2->dim, isl_dim_all) !=
3134 isl_space_dim(map2->dim, isl_dim_param))
3135 isl_assert(map1->ctx,
3136 isl_space_is_equal(map1->dim, map2->dim), goto error);
3138 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3139 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3140 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3142 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3143 map1->n * map2->n, flags);
3144 if (!result)
3145 goto error;
3146 for (i = 0; i < map1->n; ++i)
3147 for (j = 0; j < map2->n; ++j) {
3148 struct isl_basic_map *part;
3149 part = isl_basic_map_intersect(
3150 isl_basic_map_copy(map1->p[i]),
3151 isl_basic_map_copy(map2->p[j]));
3152 if (isl_basic_map_is_empty(part) < 0)
3153 part = isl_basic_map_free(part);
3154 result = isl_map_add_basic_map(result, part);
3155 if (!result)
3156 goto error;
3158 isl_map_free(map1);
3159 isl_map_free(map2);
3160 return result;
3161 error:
3162 isl_map_free(map1);
3163 isl_map_free(map2);
3164 return NULL;
3167 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3168 __isl_take isl_map *map2)
3170 if (!map1 || !map2)
3171 goto error;
3172 if (!isl_space_is_equal(map1->dim, map2->dim))
3173 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3174 "spaces don't match", goto error);
3175 return map_intersect_internal(map1, map2);
3176 error:
3177 isl_map_free(map1);
3178 isl_map_free(map2);
3179 return NULL;
3182 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3183 __isl_take isl_map *map2)
3185 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3188 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3190 return (struct isl_set *)
3191 isl_map_intersect((struct isl_map *)set1,
3192 (struct isl_map *)set2);
3195 /* map_intersect_internal accepts intersections
3196 * with parameter domains, so we can just call that function.
3198 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3199 __isl_take isl_set *params)
3201 return map_intersect_internal(map, params);
3204 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3205 __isl_take isl_map *map2)
3207 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3210 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3211 __isl_take isl_set *params)
3213 return isl_map_intersect_params(set, params);
3216 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3218 isl_space *space;
3219 unsigned pos, n1, n2;
3221 if (!bmap)
3222 return NULL;
3223 bmap = isl_basic_map_cow(bmap);
3224 if (!bmap)
3225 return NULL;
3226 space = isl_space_reverse(isl_space_copy(bmap->dim));
3227 pos = isl_basic_map_offset(bmap, isl_dim_in);
3228 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3229 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3230 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3231 return isl_basic_map_reset_space(bmap, space);
3234 static __isl_give isl_basic_map *basic_map_space_reset(
3235 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3237 isl_space *space;
3239 if (!bmap)
3240 return NULL;
3241 if (!isl_space_is_named_or_nested(bmap->dim, type))
3242 return bmap;
3244 space = isl_basic_map_get_space(bmap);
3245 space = isl_space_reset(space, type);
3246 bmap = isl_basic_map_reset_space(bmap, space);
3247 return bmap;
3250 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3251 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3252 unsigned pos, unsigned n)
3254 isl_space *res_dim;
3255 struct isl_basic_map *res;
3256 struct isl_dim_map *dim_map;
3257 unsigned total, off;
3258 enum isl_dim_type t;
3260 if (n == 0)
3261 return basic_map_space_reset(bmap, type);
3263 if (!bmap)
3264 return NULL;
3266 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3268 total = isl_basic_map_total_dim(bmap) + n;
3269 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3270 off = 0;
3271 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3272 if (t != type) {
3273 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3274 } else {
3275 unsigned size = isl_basic_map_dim(bmap, t);
3276 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3277 0, pos, off);
3278 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3279 pos, size - pos, off + pos + n);
3281 off += isl_space_dim(res_dim, t);
3283 isl_dim_map_div(dim_map, bmap, off);
3285 res = isl_basic_map_alloc_space(res_dim,
3286 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3287 if (isl_basic_map_is_rational(bmap))
3288 res = isl_basic_map_set_rational(res);
3289 if (isl_basic_map_plain_is_empty(bmap)) {
3290 isl_basic_map_free(bmap);
3291 free(dim_map);
3292 return isl_basic_map_set_to_empty(res);
3294 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3295 return isl_basic_map_finalize(res);
3298 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3299 __isl_take isl_basic_set *bset,
3300 enum isl_dim_type type, unsigned pos, unsigned n)
3302 return isl_basic_map_insert_dims(bset, type, pos, n);
3305 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3306 enum isl_dim_type type, unsigned n)
3308 if (!bmap)
3309 return NULL;
3310 return isl_basic_map_insert_dims(bmap, type,
3311 isl_basic_map_dim(bmap, type), n);
3314 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3315 enum isl_dim_type type, unsigned n)
3317 if (!bset)
3318 return NULL;
3319 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3320 return isl_basic_map_add_dims(bset, type, n);
3321 error:
3322 isl_basic_set_free(bset);
3323 return NULL;
3326 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3327 enum isl_dim_type type)
3329 isl_space *space;
3331 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3332 return map;
3334 space = isl_map_get_space(map);
3335 space = isl_space_reset(space, type);
3336 map = isl_map_reset_space(map, space);
3337 return map;
3340 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3341 enum isl_dim_type type, unsigned pos, unsigned n)
3343 int i;
3345 if (n == 0)
3346 return map_space_reset(map, type);
3348 map = isl_map_cow(map);
3349 if (!map)
3350 return NULL;
3352 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3353 if (!map->dim)
3354 goto error;
3356 for (i = 0; i < map->n; ++i) {
3357 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3358 if (!map->p[i])
3359 goto error;
3362 return map;
3363 error:
3364 isl_map_free(map);
3365 return NULL;
3368 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3369 enum isl_dim_type type, unsigned pos, unsigned n)
3371 return isl_map_insert_dims(set, type, pos, n);
3374 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3375 enum isl_dim_type type, unsigned n)
3377 if (!map)
3378 return NULL;
3379 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3382 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3383 enum isl_dim_type type, unsigned n)
3385 if (!set)
3386 return NULL;
3387 isl_assert(set->ctx, type != isl_dim_in, goto error);
3388 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3389 error:
3390 isl_set_free(set);
3391 return NULL;
3394 __isl_give isl_basic_map *isl_basic_map_move_dims(
3395 __isl_take isl_basic_map *bmap,
3396 enum isl_dim_type dst_type, unsigned dst_pos,
3397 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3399 struct isl_dim_map *dim_map;
3400 struct isl_basic_map *res;
3401 enum isl_dim_type t;
3402 unsigned total, off;
3404 if (!bmap)
3405 return NULL;
3406 if (n == 0)
3407 return bmap;
3409 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3410 goto error);
3412 if (dst_type == src_type && dst_pos == src_pos)
3413 return bmap;
3415 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3417 if (pos(bmap->dim, dst_type) + dst_pos ==
3418 pos(bmap->dim, src_type) + src_pos +
3419 ((src_type < dst_type) ? n : 0)) {
3420 bmap = isl_basic_map_cow(bmap);
3421 if (!bmap)
3422 return NULL;
3424 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3425 src_type, src_pos, n);
3426 if (!bmap->dim)
3427 goto error;
3429 bmap = isl_basic_map_finalize(bmap);
3431 return bmap;
3434 total = isl_basic_map_total_dim(bmap);
3435 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3437 off = 0;
3438 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3439 unsigned size = isl_space_dim(bmap->dim, t);
3440 if (t == dst_type) {
3441 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3442 0, dst_pos, off);
3443 off += dst_pos;
3444 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3445 src_pos, n, off);
3446 off += n;
3447 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3448 dst_pos, size - dst_pos, off);
3449 off += size - dst_pos;
3450 } else if (t == src_type) {
3451 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3452 0, src_pos, off);
3453 off += src_pos;
3454 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3455 src_pos + n, size - src_pos - n, off);
3456 off += size - src_pos - n;
3457 } else {
3458 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3459 off += size;
3462 isl_dim_map_div(dim_map, bmap, off);
3464 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3465 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3466 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3467 if (!bmap)
3468 goto error;
3470 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3471 src_type, src_pos, n);
3472 if (!bmap->dim)
3473 goto error;
3475 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3476 bmap = isl_basic_map_gauss(bmap, NULL);
3477 bmap = isl_basic_map_finalize(bmap);
3479 return bmap;
3480 error:
3481 isl_basic_map_free(bmap);
3482 return NULL;
3485 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3486 enum isl_dim_type dst_type, unsigned dst_pos,
3487 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3489 return (isl_basic_set *)isl_basic_map_move_dims(
3490 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3493 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3494 enum isl_dim_type dst_type, unsigned dst_pos,
3495 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3497 if (!set)
3498 return NULL;
3499 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3500 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3501 src_type, src_pos, n);
3502 error:
3503 isl_set_free(set);
3504 return NULL;
3507 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3508 enum isl_dim_type dst_type, unsigned dst_pos,
3509 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3511 int i;
3513 if (!map)
3514 return NULL;
3515 if (n == 0)
3516 return map;
3518 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3519 goto error);
3521 if (dst_type == src_type && dst_pos == src_pos)
3522 return map;
3524 isl_assert(map->ctx, dst_type != src_type, goto error);
3526 map = isl_map_cow(map);
3527 if (!map)
3528 return NULL;
3530 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3531 if (!map->dim)
3532 goto error;
3534 for (i = 0; i < map->n; ++i) {
3535 map->p[i] = isl_basic_map_move_dims(map->p[i],
3536 dst_type, dst_pos,
3537 src_type, src_pos, n);
3538 if (!map->p[i])
3539 goto error;
3542 return map;
3543 error:
3544 isl_map_free(map);
3545 return NULL;
3548 /* Move the specified dimensions to the last columns right before
3549 * the divs. Don't change the dimension specification of bmap.
3550 * That's the responsibility of the caller.
3552 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3553 enum isl_dim_type type, unsigned first, unsigned n)
3555 struct isl_dim_map *dim_map;
3556 struct isl_basic_map *res;
3557 enum isl_dim_type t;
3558 unsigned total, off;
3560 if (!bmap)
3561 return NULL;
3562 if (pos(bmap->dim, type) + first + n ==
3563 1 + isl_space_dim(bmap->dim, isl_dim_all))
3564 return bmap;
3566 total = isl_basic_map_total_dim(bmap);
3567 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3569 off = 0;
3570 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3571 unsigned size = isl_space_dim(bmap->dim, t);
3572 if (t == type) {
3573 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3574 0, first, off);
3575 off += first;
3576 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3577 first, n, total - bmap->n_div - n);
3578 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3579 first + n, size - (first + n), off);
3580 off += size - (first + n);
3581 } else {
3582 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3583 off += size;
3586 isl_dim_map_div(dim_map, bmap, off + n);
3588 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3589 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3590 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3591 return res;
3594 /* Insert "n" rows in the divs of "bmap".
3596 * The number of columns is not changed, which means that the last
3597 * dimensions of "bmap" are being reintepreted as the new divs.
3598 * The space of "bmap" is not adjusted, however, which means
3599 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3600 * from the space of "bmap" is the responsibility of the caller.
3602 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3603 int n)
3605 int i;
3606 size_t row_size;
3607 isl_int **new_div;
3608 isl_int *old;
3610 bmap = isl_basic_map_cow(bmap);
3611 if (!bmap)
3612 return NULL;
3614 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3615 old = bmap->block2.data;
3616 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3617 (bmap->extra + n) * (1 + row_size));
3618 if (!bmap->block2.data)
3619 return isl_basic_map_free(bmap);
3620 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3621 if (!new_div)
3622 return isl_basic_map_free(bmap);
3623 for (i = 0; i < n; ++i) {
3624 new_div[i] = bmap->block2.data +
3625 (bmap->extra + i) * (1 + row_size);
3626 isl_seq_clr(new_div[i], 1 + row_size);
3628 for (i = 0; i < bmap->extra; ++i)
3629 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3630 free(bmap->div);
3631 bmap->div = new_div;
3632 bmap->n_div += n;
3633 bmap->extra += n;
3635 return bmap;
3638 /* Drop constraints from "bmap" that only involve the variables
3639 * of "type" in the range [first, first + n] that are not related
3640 * to any of the variables outside that interval.
3641 * These constraints cannot influence the values for the variables
3642 * outside the interval, except in case they cause "bmap" to be empty.
3643 * Only drop the constraints if "bmap" is known to be non-empty.
3645 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3646 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3647 unsigned first, unsigned n)
3649 int i;
3650 int *groups;
3651 unsigned dim, n_div;
3652 isl_bool non_empty;
3654 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3655 if (non_empty < 0)
3656 return isl_basic_map_free(bmap);
3657 if (!non_empty)
3658 return bmap;
3660 dim = isl_basic_map_dim(bmap, isl_dim_all);
3661 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3662 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3663 if (!groups)
3664 return isl_basic_map_free(bmap);
3665 first += isl_basic_map_offset(bmap, type) - 1;
3666 for (i = 0; i < first; ++i)
3667 groups[i] = -1;
3668 for (i = first + n; i < dim - n_div; ++i)
3669 groups[i] = -1;
3671 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3673 return bmap;
3676 /* Turn the n dimensions of type type, starting at first
3677 * into existentially quantified variables.
3679 * If a subset of the projected out variables are unrelated
3680 * to any of the variables that remain, then the constraints
3681 * involving this subset are simply dropped first.
3683 __isl_give isl_basic_map *isl_basic_map_project_out(
3684 __isl_take isl_basic_map *bmap,
3685 enum isl_dim_type type, unsigned first, unsigned n)
3687 if (n == 0)
3688 return basic_map_space_reset(bmap, type);
3689 if (type == isl_dim_div)
3690 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3691 "cannot project out existentially quantified variables",
3692 return isl_basic_map_free(bmap));
3694 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3695 if (!bmap)
3696 return NULL;
3698 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3699 return isl_basic_map_remove_dims(bmap, type, first, n);
3701 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3702 goto error);
3704 bmap = move_last(bmap, type, first, n);
3705 bmap = isl_basic_map_cow(bmap);
3706 bmap = insert_div_rows(bmap, n);
3707 if (!bmap)
3708 return NULL;
3710 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3711 if (!bmap->dim)
3712 goto error;
3713 bmap = isl_basic_map_simplify(bmap);
3714 bmap = isl_basic_map_drop_redundant_divs(bmap);
3715 return isl_basic_map_finalize(bmap);
3716 error:
3717 isl_basic_map_free(bmap);
3718 return NULL;
3721 /* Turn the n dimensions of type type, starting at first
3722 * into existentially quantified variables.
3724 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3725 enum isl_dim_type type, unsigned first, unsigned n)
3727 return (isl_basic_set *)isl_basic_map_project_out(
3728 (isl_basic_map *)bset, type, first, n);
3731 /* Turn the n dimensions of type type, starting at first
3732 * into existentially quantified variables.
3734 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3735 enum isl_dim_type type, unsigned first, unsigned n)
3737 int i;
3739 if (!map)
3740 return NULL;
3742 if (n == 0)
3743 return map_space_reset(map, type);
3745 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3747 map = isl_map_cow(map);
3748 if (!map)
3749 return NULL;
3751 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3752 if (!map->dim)
3753 goto error;
3755 for (i = 0; i < map->n; ++i) {
3756 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3757 if (!map->p[i])
3758 goto error;
3761 return map;
3762 error:
3763 isl_map_free(map);
3764 return NULL;
3767 /* Turn the n dimensions of type type, starting at first
3768 * into existentially quantified variables.
3770 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3771 enum isl_dim_type type, unsigned first, unsigned n)
3773 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3776 /* Return a map that projects the elements in "set" onto their
3777 * "n" set dimensions starting at "first".
3778 * "type" should be equal to isl_dim_set.
3780 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3781 enum isl_dim_type type, unsigned first, unsigned n)
3783 int i;
3784 int dim;
3785 isl_map *map;
3787 if (!set)
3788 return NULL;
3789 if (type != isl_dim_set)
3790 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3791 "only set dimensions can be projected out", goto error);
3792 dim = isl_set_dim(set, isl_dim_set);
3793 if (first + n > dim || first + n < first)
3794 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3795 "index out of bounds", goto error);
3797 map = isl_map_from_domain(set);
3798 map = isl_map_add_dims(map, isl_dim_out, n);
3799 for (i = 0; i < n; ++i)
3800 map = isl_map_equate(map, isl_dim_in, first + i,
3801 isl_dim_out, i);
3802 return map;
3803 error:
3804 isl_set_free(set);
3805 return NULL;
3808 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3810 int i, j;
3812 for (i = 0; i < n; ++i) {
3813 j = isl_basic_map_alloc_div(bmap);
3814 if (j < 0)
3815 goto error;
3816 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3818 return bmap;
3819 error:
3820 isl_basic_map_free(bmap);
3821 return NULL;
3824 struct isl_basic_map *isl_basic_map_apply_range(
3825 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3827 isl_space *dim_result = NULL;
3828 struct isl_basic_map *bmap;
3829 unsigned n_in, n_out, n, nparam, total, pos;
3830 struct isl_dim_map *dim_map1, *dim_map2;
3832 if (!bmap1 || !bmap2)
3833 goto error;
3834 if (!isl_space_match(bmap1->dim, isl_dim_param,
3835 bmap2->dim, isl_dim_param))
3836 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3837 "parameters don't match", goto error);
3838 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3839 bmap2->dim, isl_dim_in))
3840 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3841 "spaces don't match", goto error);
3843 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3844 isl_space_copy(bmap2->dim));
3846 n_in = isl_basic_map_n_in(bmap1);
3847 n_out = isl_basic_map_n_out(bmap2);
3848 n = isl_basic_map_n_out(bmap1);
3849 nparam = isl_basic_map_n_param(bmap1);
3851 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3852 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3853 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3854 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3855 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3856 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3857 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3858 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3859 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3863 bmap = isl_basic_map_alloc_space(dim_result,
3864 bmap1->n_div + bmap2->n_div + n,
3865 bmap1->n_eq + bmap2->n_eq,
3866 bmap1->n_ineq + bmap2->n_ineq);
3867 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3868 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3869 bmap = add_divs(bmap, n);
3870 bmap = isl_basic_map_simplify(bmap);
3871 bmap = isl_basic_map_drop_redundant_divs(bmap);
3872 return isl_basic_map_finalize(bmap);
3873 error:
3874 isl_basic_map_free(bmap1);
3875 isl_basic_map_free(bmap2);
3876 return NULL;
3879 struct isl_basic_set *isl_basic_set_apply(
3880 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3882 if (!bset || !bmap)
3883 goto error;
3885 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3886 goto error);
3888 return (struct isl_basic_set *)
3889 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3890 error:
3891 isl_basic_set_free(bset);
3892 isl_basic_map_free(bmap);
3893 return NULL;
3896 struct isl_basic_map *isl_basic_map_apply_domain(
3897 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3899 if (!bmap1 || !bmap2)
3900 goto error;
3902 isl_assert(bmap1->ctx,
3903 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3904 isl_assert(bmap1->ctx,
3905 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3906 goto error);
3908 bmap1 = isl_basic_map_reverse(bmap1);
3909 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3910 return isl_basic_map_reverse(bmap1);
3911 error:
3912 isl_basic_map_free(bmap1);
3913 isl_basic_map_free(bmap2);
3914 return NULL;
3917 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3918 * A \cap B -> f(A) + f(B)
3920 struct isl_basic_map *isl_basic_map_sum(
3921 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3923 unsigned n_in, n_out, nparam, total, pos;
3924 struct isl_basic_map *bmap = NULL;
3925 struct isl_dim_map *dim_map1, *dim_map2;
3926 int i;
3928 if (!bmap1 || !bmap2)
3929 goto error;
3931 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3932 goto error);
3934 nparam = isl_basic_map_n_param(bmap1);
3935 n_in = isl_basic_map_n_in(bmap1);
3936 n_out = isl_basic_map_n_out(bmap1);
3938 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3939 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3940 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3941 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3942 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3943 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3944 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3945 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3946 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3950 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3951 bmap1->n_div + bmap2->n_div + 2 * n_out,
3952 bmap1->n_eq + bmap2->n_eq + n_out,
3953 bmap1->n_ineq + bmap2->n_ineq);
3954 for (i = 0; i < n_out; ++i) {
3955 int j = isl_basic_map_alloc_equality(bmap);
3956 if (j < 0)
3957 goto error;
3958 isl_seq_clr(bmap->eq[j], 1+total);
3959 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3960 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3961 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3963 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3964 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3965 bmap = add_divs(bmap, 2 * n_out);
3967 bmap = isl_basic_map_simplify(bmap);
3968 return isl_basic_map_finalize(bmap);
3969 error:
3970 isl_basic_map_free(bmap);
3971 isl_basic_map_free(bmap1);
3972 isl_basic_map_free(bmap2);
3973 return NULL;
3976 /* Given two maps A -> f(A) and B -> g(B), construct a map
3977 * A \cap B -> f(A) + f(B)
3979 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3981 struct isl_map *result;
3982 int i, j;
3984 if (!map1 || !map2)
3985 goto error;
3987 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3989 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3990 map1->n * map2->n, 0);
3991 if (!result)
3992 goto error;
3993 for (i = 0; i < map1->n; ++i)
3994 for (j = 0; j < map2->n; ++j) {
3995 struct isl_basic_map *part;
3996 part = isl_basic_map_sum(
3997 isl_basic_map_copy(map1->p[i]),
3998 isl_basic_map_copy(map2->p[j]));
3999 if (isl_basic_map_is_empty(part))
4000 isl_basic_map_free(part);
4001 else
4002 result = isl_map_add_basic_map(result, part);
4003 if (!result)
4004 goto error;
4006 isl_map_free(map1);
4007 isl_map_free(map2);
4008 return result;
4009 error:
4010 isl_map_free(map1);
4011 isl_map_free(map2);
4012 return NULL;
4015 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4016 __isl_take isl_set *set2)
4018 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
4021 /* Given a basic map A -> f(A), construct A -> -f(A).
4023 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4025 int i, j;
4026 unsigned off, n;
4028 bmap = isl_basic_map_cow(bmap);
4029 if (!bmap)
4030 return NULL;
4032 n = isl_basic_map_dim(bmap, isl_dim_out);
4033 off = isl_basic_map_offset(bmap, isl_dim_out);
4034 for (i = 0; i < bmap->n_eq; ++i)
4035 for (j = 0; j < n; ++j)
4036 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4037 for (i = 0; i < bmap->n_ineq; ++i)
4038 for (j = 0; j < n; ++j)
4039 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4040 for (i = 0; i < bmap->n_div; ++i)
4041 for (j = 0; j < n; ++j)
4042 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4043 bmap = isl_basic_map_gauss(bmap, NULL);
4044 return isl_basic_map_finalize(bmap);
4047 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4049 return isl_basic_map_neg(bset);
4052 /* Given a map A -> f(A), construct A -> -f(A).
4054 struct isl_map *isl_map_neg(struct isl_map *map)
4056 int i;
4058 map = isl_map_cow(map);
4059 if (!map)
4060 return NULL;
4062 for (i = 0; i < map->n; ++i) {
4063 map->p[i] = isl_basic_map_neg(map->p[i]);
4064 if (!map->p[i])
4065 goto error;
4068 return map;
4069 error:
4070 isl_map_free(map);
4071 return NULL;
4074 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4076 return (isl_set *)isl_map_neg((isl_map *)set);
4079 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4080 * A -> floor(f(A)/d).
4082 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4083 isl_int d)
4085 unsigned n_in, n_out, nparam, total, pos;
4086 struct isl_basic_map *result = NULL;
4087 struct isl_dim_map *dim_map;
4088 int i;
4090 if (!bmap)
4091 return NULL;
4093 nparam = isl_basic_map_n_param(bmap);
4094 n_in = isl_basic_map_n_in(bmap);
4095 n_out = isl_basic_map_n_out(bmap);
4097 total = nparam + n_in + n_out + bmap->n_div + n_out;
4098 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4099 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4100 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4101 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4102 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4104 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4105 bmap->n_div + n_out,
4106 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4107 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4108 result = add_divs(result, n_out);
4109 for (i = 0; i < n_out; ++i) {
4110 int j;
4111 j = isl_basic_map_alloc_inequality(result);
4112 if (j < 0)
4113 goto error;
4114 isl_seq_clr(result->ineq[j], 1+total);
4115 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4116 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4117 j = isl_basic_map_alloc_inequality(result);
4118 if (j < 0)
4119 goto error;
4120 isl_seq_clr(result->ineq[j], 1+total);
4121 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4122 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4123 isl_int_sub_ui(result->ineq[j][0], d, 1);
4126 result = isl_basic_map_simplify(result);
4127 return isl_basic_map_finalize(result);
4128 error:
4129 isl_basic_map_free(result);
4130 return NULL;
4133 /* Given a map A -> f(A) and an integer d, construct a map
4134 * A -> floor(f(A)/d).
4136 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4138 int i;
4140 map = isl_map_cow(map);
4141 if (!map)
4142 return NULL;
4144 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4145 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4146 for (i = 0; i < map->n; ++i) {
4147 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4148 if (!map->p[i])
4149 goto error;
4152 return map;
4153 error:
4154 isl_map_free(map);
4155 return NULL;
4158 /* Given a map A -> f(A) and an integer d, construct a map
4159 * A -> floor(f(A)/d).
4161 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4162 __isl_take isl_val *d)
4164 if (!map || !d)
4165 goto error;
4166 if (!isl_val_is_int(d))
4167 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4168 "expecting integer denominator", goto error);
4169 map = isl_map_floordiv(map, d->n);
4170 isl_val_free(d);
4171 return map;
4172 error:
4173 isl_map_free(map);
4174 isl_val_free(d);
4175 return NULL;
4178 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4180 int i;
4181 unsigned nparam;
4182 unsigned n_in;
4184 i = isl_basic_map_alloc_equality(bmap);
4185 if (i < 0)
4186 goto error;
4187 nparam = isl_basic_map_n_param(bmap);
4188 n_in = isl_basic_map_n_in(bmap);
4189 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4190 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4191 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4192 return isl_basic_map_finalize(bmap);
4193 error:
4194 isl_basic_map_free(bmap);
4195 return NULL;
4198 /* Add a constraints to "bmap" expressing i_pos < o_pos
4200 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4202 int i;
4203 unsigned nparam;
4204 unsigned n_in;
4206 i = isl_basic_map_alloc_inequality(bmap);
4207 if (i < 0)
4208 goto error;
4209 nparam = isl_basic_map_n_param(bmap);
4210 n_in = isl_basic_map_n_in(bmap);
4211 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4212 isl_int_set_si(bmap->ineq[i][0], -1);
4213 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4214 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4215 return isl_basic_map_finalize(bmap);
4216 error:
4217 isl_basic_map_free(bmap);
4218 return NULL;
4221 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4223 static __isl_give isl_basic_map *var_less_or_equal(
4224 __isl_take isl_basic_map *bmap, unsigned pos)
4226 int i;
4227 unsigned nparam;
4228 unsigned n_in;
4230 i = isl_basic_map_alloc_inequality(bmap);
4231 if (i < 0)
4232 goto error;
4233 nparam = isl_basic_map_n_param(bmap);
4234 n_in = isl_basic_map_n_in(bmap);
4235 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4236 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4237 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4238 return isl_basic_map_finalize(bmap);
4239 error:
4240 isl_basic_map_free(bmap);
4241 return NULL;
4244 /* Add a constraints to "bmap" expressing i_pos > o_pos
4246 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4248 int i;
4249 unsigned nparam;
4250 unsigned n_in;
4252 i = isl_basic_map_alloc_inequality(bmap);
4253 if (i < 0)
4254 goto error;
4255 nparam = isl_basic_map_n_param(bmap);
4256 n_in = isl_basic_map_n_in(bmap);
4257 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4258 isl_int_set_si(bmap->ineq[i][0], -1);
4259 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4260 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4261 return isl_basic_map_finalize(bmap);
4262 error:
4263 isl_basic_map_free(bmap);
4264 return NULL;
4267 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4269 static __isl_give isl_basic_map *var_more_or_equal(
4270 __isl_take isl_basic_map *bmap, unsigned pos)
4272 int i;
4273 unsigned nparam;
4274 unsigned n_in;
4276 i = isl_basic_map_alloc_inequality(bmap);
4277 if (i < 0)
4278 goto error;
4279 nparam = isl_basic_map_n_param(bmap);
4280 n_in = isl_basic_map_n_in(bmap);
4281 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4282 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4283 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4284 return isl_basic_map_finalize(bmap);
4285 error:
4286 isl_basic_map_free(bmap);
4287 return NULL;
4290 __isl_give isl_basic_map *isl_basic_map_equal(
4291 __isl_take isl_space *dim, unsigned n_equal)
4293 int i;
4294 struct isl_basic_map *bmap;
4295 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4296 if (!bmap)
4297 return NULL;
4298 for (i = 0; i < n_equal && bmap; ++i)
4299 bmap = var_equal(bmap, i);
4300 return isl_basic_map_finalize(bmap);
4303 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4305 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4306 unsigned pos)
4308 int i;
4309 struct isl_basic_map *bmap;
4310 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4311 if (!bmap)
4312 return NULL;
4313 for (i = 0; i < pos && bmap; ++i)
4314 bmap = var_equal(bmap, i);
4315 if (bmap)
4316 bmap = var_less(bmap, pos);
4317 return isl_basic_map_finalize(bmap);
4320 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4322 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4323 __isl_take isl_space *dim, unsigned pos)
4325 int i;
4326 isl_basic_map *bmap;
4328 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4329 for (i = 0; i < pos; ++i)
4330 bmap = var_equal(bmap, i);
4331 bmap = var_less_or_equal(bmap, pos);
4332 return isl_basic_map_finalize(bmap);
4335 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4337 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4338 unsigned pos)
4340 int i;
4341 struct isl_basic_map *bmap;
4342 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4343 if (!bmap)
4344 return NULL;
4345 for (i = 0; i < pos && bmap; ++i)
4346 bmap = var_equal(bmap, i);
4347 if (bmap)
4348 bmap = var_more(bmap, pos);
4349 return isl_basic_map_finalize(bmap);
4352 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4354 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4355 __isl_take isl_space *dim, unsigned pos)
4357 int i;
4358 isl_basic_map *bmap;
4360 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4361 for (i = 0; i < pos; ++i)
4362 bmap = var_equal(bmap, i);
4363 bmap = var_more_or_equal(bmap, pos);
4364 return isl_basic_map_finalize(bmap);
4367 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4368 unsigned n, int equal)
4370 struct isl_map *map;
4371 int i;
4373 if (n == 0 && equal)
4374 return isl_map_universe(dims);
4376 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4378 for (i = 0; i + 1 < n; ++i)
4379 map = isl_map_add_basic_map(map,
4380 isl_basic_map_less_at(isl_space_copy(dims), i));
4381 if (n > 0) {
4382 if (equal)
4383 map = isl_map_add_basic_map(map,
4384 isl_basic_map_less_or_equal_at(dims, n - 1));
4385 else
4386 map = isl_map_add_basic_map(map,
4387 isl_basic_map_less_at(dims, n - 1));
4388 } else
4389 isl_space_free(dims);
4391 return map;
4394 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4396 if (!dims)
4397 return NULL;
4398 return map_lex_lte_first(dims, dims->n_out, equal);
4401 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4403 return map_lex_lte_first(dim, n, 0);
4406 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4408 return map_lex_lte_first(dim, n, 1);
4411 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4413 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4416 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4418 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4421 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4422 unsigned n, int equal)
4424 struct isl_map *map;
4425 int i;
4427 if (n == 0 && equal)
4428 return isl_map_universe(dims);
4430 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4432 for (i = 0; i + 1 < n; ++i)
4433 map = isl_map_add_basic_map(map,
4434 isl_basic_map_more_at(isl_space_copy(dims), i));
4435 if (n > 0) {
4436 if (equal)
4437 map = isl_map_add_basic_map(map,
4438 isl_basic_map_more_or_equal_at(dims, n - 1));
4439 else
4440 map = isl_map_add_basic_map(map,
4441 isl_basic_map_more_at(dims, n - 1));
4442 } else
4443 isl_space_free(dims);
4445 return map;
4448 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4450 if (!dims)
4451 return NULL;
4452 return map_lex_gte_first(dims, dims->n_out, equal);
4455 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4457 return map_lex_gte_first(dim, n, 0);
4460 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4462 return map_lex_gte_first(dim, n, 1);
4465 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4467 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4470 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4472 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4475 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4476 __isl_take isl_set *set2)
4478 isl_map *map;
4479 map = isl_map_lex_le(isl_set_get_space(set1));
4480 map = isl_map_intersect_domain(map, set1);
4481 map = isl_map_intersect_range(map, set2);
4482 return map;
4485 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4486 __isl_take isl_set *set2)
4488 isl_map *map;
4489 map = isl_map_lex_lt(isl_set_get_space(set1));
4490 map = isl_map_intersect_domain(map, set1);
4491 map = isl_map_intersect_range(map, set2);
4492 return map;
4495 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4496 __isl_take isl_set *set2)
4498 isl_map *map;
4499 map = isl_map_lex_ge(isl_set_get_space(set1));
4500 map = isl_map_intersect_domain(map, set1);
4501 map = isl_map_intersect_range(map, set2);
4502 return map;
4505 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4506 __isl_take isl_set *set2)
4508 isl_map *map;
4509 map = isl_map_lex_gt(isl_set_get_space(set1));
4510 map = isl_map_intersect_domain(map, set1);
4511 map = isl_map_intersect_range(map, set2);
4512 return map;
4515 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4516 __isl_take isl_map *map2)
4518 isl_map *map;
4519 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4520 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4521 map = isl_map_apply_range(map, isl_map_reverse(map2));
4522 return map;
4525 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4526 __isl_take isl_map *map2)
4528 isl_map *map;
4529 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4530 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4531 map = isl_map_apply_range(map, isl_map_reverse(map2));
4532 return map;
4535 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4536 __isl_take isl_map *map2)
4538 isl_map *map;
4539 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4540 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4541 map = isl_map_apply_range(map, isl_map_reverse(map2));
4542 return map;
4545 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4546 __isl_take isl_map *map2)
4548 isl_map *map;
4549 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4550 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4551 map = isl_map_apply_range(map, isl_map_reverse(map2));
4552 return map;
4555 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4556 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4558 struct isl_basic_map *bmap;
4560 bset = isl_basic_set_cow(bset);
4561 if (!bset || !dim)
4562 goto error;
4564 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4565 isl_space_free(bset->dim);
4566 bmap = (struct isl_basic_map *) bset;
4567 bmap->dim = dim;
4568 return isl_basic_map_finalize(bmap);
4569 error:
4570 isl_basic_set_free(bset);
4571 isl_space_free(dim);
4572 return NULL;
4575 /* For a div d = floor(f/m), add the constraint
4577 * f - m d >= 0
4579 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4580 unsigned pos, isl_int *div)
4582 int i;
4583 unsigned total = isl_basic_map_total_dim(bmap);
4585 i = isl_basic_map_alloc_inequality(bmap);
4586 if (i < 0)
4587 return -1;
4588 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4589 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4591 return 0;
4594 /* For a div d = floor(f/m), add the constraint
4596 * -(f-(m-1)) + m d >= 0
4598 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4599 unsigned pos, isl_int *div)
4601 int i;
4602 unsigned total = isl_basic_map_total_dim(bmap);
4604 i = isl_basic_map_alloc_inequality(bmap);
4605 if (i < 0)
4606 return -1;
4607 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4608 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4609 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4610 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4612 return 0;
4615 /* For a div d = floor(f/m), add the constraints
4617 * f - m d >= 0
4618 * -(f-(m-1)) + m d >= 0
4620 * Note that the second constraint is the negation of
4622 * f - m d >= m
4624 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4625 unsigned pos, isl_int *div)
4627 if (add_upper_div_constraint(bmap, pos, div) < 0)
4628 return -1;
4629 if (add_lower_div_constraint(bmap, pos, div) < 0)
4630 return -1;
4631 return 0;
4634 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4635 unsigned pos, isl_int *div)
4637 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4638 pos, div);
4641 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4643 unsigned total = isl_basic_map_total_dim(bmap);
4644 unsigned div_pos = total - bmap->n_div + div;
4646 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4647 bmap->div[div]);
4650 /* For each known div d = floor(f/m), add the constraints
4652 * f - m d >= 0
4653 * -(f-(m-1)) + m d >= 0
4655 * Remove duplicate constraints in case of some these div constraints
4656 * already appear in "bmap".
4658 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4659 __isl_take isl_basic_map *bmap)
4661 unsigned n_div;
4663 if (!bmap)
4664 return NULL;
4665 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4666 if (n_div == 0)
4667 return bmap;
4669 bmap = add_known_div_constraints(bmap);
4670 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4671 bmap = isl_basic_map_finalize(bmap);
4672 return bmap;
4675 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4677 * In particular, if this div is of the form d = floor(f/m),
4678 * then add the constraint
4680 * f - m d >= 0
4682 * if sign < 0 or the constraint
4684 * -(f-(m-1)) + m d >= 0
4686 * if sign > 0.
4688 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4689 unsigned div, int sign)
4691 unsigned total;
4692 unsigned div_pos;
4694 if (!bmap)
4695 return -1;
4697 total = isl_basic_map_total_dim(bmap);
4698 div_pos = total - bmap->n_div + div;
4700 if (sign < 0)
4701 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4702 else
4703 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4706 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4708 return isl_basic_map_add_div_constraints(bset, div);
4711 struct isl_basic_set *isl_basic_map_underlying_set(
4712 struct isl_basic_map *bmap)
4714 if (!bmap)
4715 goto error;
4716 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4717 bmap->n_div == 0 &&
4718 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4719 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4720 return (struct isl_basic_set *)bmap;
4721 bmap = isl_basic_map_cow(bmap);
4722 if (!bmap)
4723 goto error;
4724 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4725 if (!bmap->dim)
4726 goto error;
4727 bmap->extra -= bmap->n_div;
4728 bmap->n_div = 0;
4729 bmap = isl_basic_map_finalize(bmap);
4730 return (struct isl_basic_set *)bmap;
4731 error:
4732 isl_basic_map_free(bmap);
4733 return NULL;
4736 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4737 __isl_take isl_basic_set *bset)
4739 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4742 /* Replace each element in "list" by the result of applying
4743 * isl_basic_map_underlying_set to the element.
4745 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4746 __isl_take isl_basic_map_list *list)
4748 int i, n;
4750 if (!list)
4751 return NULL;
4753 n = isl_basic_map_list_n_basic_map(list);
4754 for (i = 0; i < n; ++i) {
4755 isl_basic_map *bmap;
4756 isl_basic_set *bset;
4758 bmap = isl_basic_map_list_get_basic_map(list, i);
4759 bset = isl_basic_set_underlying_set(bmap);
4760 list = isl_basic_set_list_set_basic_set(list, i, bset);
4763 return list;
4766 struct isl_basic_map *isl_basic_map_overlying_set(
4767 struct isl_basic_set *bset, struct isl_basic_map *like)
4769 struct isl_basic_map *bmap;
4770 struct isl_ctx *ctx;
4771 unsigned total;
4772 int i;
4774 if (!bset || !like)
4775 goto error;
4776 ctx = bset->ctx;
4777 isl_assert(ctx, bset->n_div == 0, goto error);
4778 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4779 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4780 goto error);
4781 if (like->n_div == 0) {
4782 isl_space *space = isl_basic_map_get_space(like);
4783 isl_basic_map_free(like);
4784 return isl_basic_map_reset_space(bset, space);
4786 bset = isl_basic_set_cow(bset);
4787 if (!bset)
4788 goto error;
4789 total = bset->dim->n_out + bset->extra;
4790 bmap = (struct isl_basic_map *)bset;
4791 isl_space_free(bmap->dim);
4792 bmap->dim = isl_space_copy(like->dim);
4793 if (!bmap->dim)
4794 goto error;
4795 bmap->n_div = like->n_div;
4796 bmap->extra += like->n_div;
4797 if (bmap->extra) {
4798 unsigned ltotal;
4799 isl_int **div;
4800 ltotal = total - bmap->extra + like->extra;
4801 if (ltotal > total)
4802 ltotal = total;
4803 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4804 bmap->extra * (1 + 1 + total));
4805 if (isl_blk_is_error(bmap->block2))
4806 goto error;
4807 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4808 if (!div)
4809 goto error;
4810 bmap->div = div;
4811 for (i = 0; i < bmap->extra; ++i)
4812 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4813 for (i = 0; i < like->n_div; ++i) {
4814 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4815 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4817 bmap = isl_basic_map_add_known_div_constraints(bmap);
4819 isl_basic_map_free(like);
4820 bmap = isl_basic_map_simplify(bmap);
4821 bmap = isl_basic_map_finalize(bmap);
4822 return bmap;
4823 error:
4824 isl_basic_map_free(like);
4825 isl_basic_set_free(bset);
4826 return NULL;
4829 struct isl_basic_set *isl_basic_set_from_underlying_set(
4830 struct isl_basic_set *bset, struct isl_basic_set *like)
4832 return (struct isl_basic_set *)
4833 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4836 struct isl_set *isl_set_from_underlying_set(
4837 struct isl_set *set, struct isl_basic_set *like)
4839 int i;
4841 if (!set || !like)
4842 goto error;
4843 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4844 goto error);
4845 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4846 isl_basic_set_free(like);
4847 return set;
4849 set = isl_set_cow(set);
4850 if (!set)
4851 goto error;
4852 for (i = 0; i < set->n; ++i) {
4853 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4854 isl_basic_set_copy(like));
4855 if (!set->p[i])
4856 goto error;
4858 isl_space_free(set->dim);
4859 set->dim = isl_space_copy(like->dim);
4860 if (!set->dim)
4861 goto error;
4862 isl_basic_set_free(like);
4863 return set;
4864 error:
4865 isl_basic_set_free(like);
4866 isl_set_free(set);
4867 return NULL;
4870 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4872 int i;
4874 map = isl_map_cow(map);
4875 if (!map)
4876 return NULL;
4877 map->dim = isl_space_cow(map->dim);
4878 if (!map->dim)
4879 goto error;
4881 for (i = 1; i < map->n; ++i)
4882 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4883 goto error);
4884 for (i = 0; i < map->n; ++i) {
4885 map->p[i] = (struct isl_basic_map *)
4886 isl_basic_map_underlying_set(map->p[i]);
4887 if (!map->p[i])
4888 goto error;
4890 if (map->n == 0)
4891 map->dim = isl_space_underlying(map->dim, 0);
4892 else {
4893 isl_space_free(map->dim);
4894 map->dim = isl_space_copy(map->p[0]->dim);
4896 if (!map->dim)
4897 goto error;
4898 return (struct isl_set *)map;
4899 error:
4900 isl_map_free(map);
4901 return NULL;
4904 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4906 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4909 /* Replace the space of "bmap" by "space".
4911 * If the space of "bmap" is identical to "space" (including the identifiers
4912 * of the input and output dimensions), then simply return the original input.
4914 __isl_give isl_basic_map *isl_basic_map_reset_space(
4915 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4917 isl_bool equal;
4919 if (!bmap)
4920 goto error;
4921 equal = isl_space_is_equal(bmap->dim, space);
4922 if (equal >= 0 && equal)
4923 equal = isl_space_match(bmap->dim, isl_dim_in,
4924 space, isl_dim_in);
4925 if (equal >= 0 && equal)
4926 equal = isl_space_match(bmap->dim, isl_dim_out,
4927 space, isl_dim_out);
4928 if (equal < 0)
4929 goto error;
4930 if (equal) {
4931 isl_space_free(space);
4932 return bmap;
4934 bmap = isl_basic_map_cow(bmap);
4935 if (!bmap || !space)
4936 goto error;
4938 isl_space_free(bmap->dim);
4939 bmap->dim = space;
4941 bmap = isl_basic_map_finalize(bmap);
4943 return bmap;
4944 error:
4945 isl_basic_map_free(bmap);
4946 isl_space_free(space);
4947 return NULL;
4950 __isl_give isl_basic_set *isl_basic_set_reset_space(
4951 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4953 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4954 dim);
4957 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4958 __isl_take isl_space *dim)
4960 int i;
4962 map = isl_map_cow(map);
4963 if (!map || !dim)
4964 goto error;
4966 for (i = 0; i < map->n; ++i) {
4967 map->p[i] = isl_basic_map_reset_space(map->p[i],
4968 isl_space_copy(dim));
4969 if (!map->p[i])
4970 goto error;
4972 isl_space_free(map->dim);
4973 map->dim = dim;
4975 return map;
4976 error:
4977 isl_map_free(map);
4978 isl_space_free(dim);
4979 return NULL;
4982 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4983 __isl_take isl_space *dim)
4985 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4988 /* Compute the parameter domain of the given basic set.
4990 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4992 isl_space *space;
4993 unsigned n;
4995 if (isl_basic_set_is_params(bset))
4996 return bset;
4998 n = isl_basic_set_dim(bset, isl_dim_set);
4999 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5000 space = isl_basic_set_get_space(bset);
5001 space = isl_space_params(space);
5002 bset = isl_basic_set_reset_space(bset, space);
5003 return bset;
5006 /* Construct a zero-dimensional basic set with the given parameter domain.
5008 __isl_give isl_basic_set *isl_basic_set_from_params(
5009 __isl_take isl_basic_set *bset)
5011 isl_space *space;
5012 space = isl_basic_set_get_space(bset);
5013 space = isl_space_set_from_params(space);
5014 bset = isl_basic_set_reset_space(bset, space);
5015 return bset;
5018 /* Compute the parameter domain of the given set.
5020 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5022 isl_space *space;
5023 unsigned n;
5025 if (isl_set_is_params(set))
5026 return set;
5028 n = isl_set_dim(set, isl_dim_set);
5029 set = isl_set_project_out(set, isl_dim_set, 0, n);
5030 space = isl_set_get_space(set);
5031 space = isl_space_params(space);
5032 set = isl_set_reset_space(set, space);
5033 return set;
5036 /* Construct a zero-dimensional set with the given parameter domain.
5038 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5040 isl_space *space;
5041 space = isl_set_get_space(set);
5042 space = isl_space_set_from_params(space);
5043 set = isl_set_reset_space(set, space);
5044 return set;
5047 /* Compute the parameter domain of the given map.
5049 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5051 isl_space *space;
5052 unsigned n;
5054 n = isl_map_dim(map, isl_dim_in);
5055 map = isl_map_project_out(map, isl_dim_in, 0, n);
5056 n = isl_map_dim(map, isl_dim_out);
5057 map = isl_map_project_out(map, isl_dim_out, 0, n);
5058 space = isl_map_get_space(map);
5059 space = isl_space_params(space);
5060 map = isl_map_reset_space(map, space);
5061 return map;
5064 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5066 isl_space *space;
5067 unsigned n_out;
5069 if (!bmap)
5070 return NULL;
5071 space = isl_space_domain(isl_basic_map_get_space(bmap));
5073 n_out = isl_basic_map_n_out(bmap);
5074 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5076 return isl_basic_map_reset_space(bmap, space);
5079 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5081 if (!bmap)
5082 return -1;
5083 return isl_space_may_be_set(bmap->dim);
5086 /* Is this basic map actually a set?
5087 * Users should never call this function. Outside of isl,
5088 * the type should indicate whether something is a set or a map.
5090 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5092 if (!bmap)
5093 return -1;
5094 return isl_space_is_set(bmap->dim);
5097 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5099 if (!bmap)
5100 return NULL;
5101 if (isl_basic_map_is_set(bmap))
5102 return bmap;
5103 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5106 __isl_give isl_basic_map *isl_basic_map_domain_map(
5107 __isl_take isl_basic_map *bmap)
5109 int i, k;
5110 isl_space *dim;
5111 isl_basic_map *domain;
5112 int nparam, n_in, n_out;
5113 unsigned total;
5115 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5116 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5117 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5119 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5120 domain = isl_basic_map_universe(dim);
5122 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5123 bmap = isl_basic_map_apply_range(bmap, domain);
5124 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5126 total = isl_basic_map_total_dim(bmap);
5128 for (i = 0; i < n_in; ++i) {
5129 k = isl_basic_map_alloc_equality(bmap);
5130 if (k < 0)
5131 goto error;
5132 isl_seq_clr(bmap->eq[k], 1 + total);
5133 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5134 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5137 bmap = isl_basic_map_gauss(bmap, NULL);
5138 return isl_basic_map_finalize(bmap);
5139 error:
5140 isl_basic_map_free(bmap);
5141 return NULL;
5144 __isl_give isl_basic_map *isl_basic_map_range_map(
5145 __isl_take isl_basic_map *bmap)
5147 int i, k;
5148 isl_space *dim;
5149 isl_basic_map *range;
5150 int nparam, n_in, n_out;
5151 unsigned total;
5153 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5154 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5155 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5157 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5158 range = isl_basic_map_universe(dim);
5160 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5161 bmap = isl_basic_map_apply_range(bmap, range);
5162 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5164 total = isl_basic_map_total_dim(bmap);
5166 for (i = 0; i < n_out; ++i) {
5167 k = isl_basic_map_alloc_equality(bmap);
5168 if (k < 0)
5169 goto error;
5170 isl_seq_clr(bmap->eq[k], 1 + total);
5171 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5172 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5175 bmap = isl_basic_map_gauss(bmap, NULL);
5176 return isl_basic_map_finalize(bmap);
5177 error:
5178 isl_basic_map_free(bmap);
5179 return NULL;
5182 int isl_map_may_be_set(__isl_keep isl_map *map)
5184 if (!map)
5185 return -1;
5186 return isl_space_may_be_set(map->dim);
5189 /* Is this map actually a set?
5190 * Users should never call this function. Outside of isl,
5191 * the type should indicate whether something is a set or a map.
5193 int isl_map_is_set(__isl_keep isl_map *map)
5195 if (!map)
5196 return -1;
5197 return isl_space_is_set(map->dim);
5200 struct isl_set *isl_map_range(struct isl_map *map)
5202 int i;
5203 struct isl_set *set;
5205 if (!map)
5206 goto error;
5207 if (isl_map_is_set(map))
5208 return (isl_set *)map;
5210 map = isl_map_cow(map);
5211 if (!map)
5212 goto error;
5214 set = (struct isl_set *) map;
5215 set->dim = isl_space_range(set->dim);
5216 if (!set->dim)
5217 goto error;
5218 for (i = 0; i < map->n; ++i) {
5219 set->p[i] = isl_basic_map_range(map->p[i]);
5220 if (!set->p[i])
5221 goto error;
5223 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5224 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5225 return set;
5226 error:
5227 isl_map_free(map);
5228 return NULL;
5231 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5233 int i;
5235 map = isl_map_cow(map);
5236 if (!map)
5237 return NULL;
5239 map->dim = isl_space_domain_map(map->dim);
5240 if (!map->dim)
5241 goto error;
5242 for (i = 0; i < map->n; ++i) {
5243 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5244 if (!map->p[i])
5245 goto error;
5247 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5248 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5249 return map;
5250 error:
5251 isl_map_free(map);
5252 return NULL;
5255 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5257 int i;
5258 isl_space *range_dim;
5260 map = isl_map_cow(map);
5261 if (!map)
5262 return NULL;
5264 range_dim = isl_space_range(isl_map_get_space(map));
5265 range_dim = isl_space_from_range(range_dim);
5266 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5267 map->dim = isl_space_join(map->dim, range_dim);
5268 if (!map->dim)
5269 goto error;
5270 for (i = 0; i < map->n; ++i) {
5271 map->p[i] = isl_basic_map_range_map(map->p[i]);
5272 if (!map->p[i])
5273 goto error;
5275 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5276 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5277 return map;
5278 error:
5279 isl_map_free(map);
5280 return NULL;
5283 /* Given a wrapped map of the form A[B -> C],
5284 * return the map A[B -> C] -> B.
5286 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5288 isl_id *id;
5289 isl_map *map;
5291 if (!set)
5292 return NULL;
5293 if (!isl_set_has_tuple_id(set))
5294 return isl_map_domain_map(isl_set_unwrap(set));
5296 id = isl_set_get_tuple_id(set);
5297 map = isl_map_domain_map(isl_set_unwrap(set));
5298 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5300 return map;
5303 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5304 __isl_take isl_space *dim)
5306 int i;
5307 struct isl_map *map = NULL;
5309 set = isl_set_cow(set);
5310 if (!set || !dim)
5311 goto error;
5312 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5313 map = (struct isl_map *)set;
5314 for (i = 0; i < set->n; ++i) {
5315 map->p[i] = isl_basic_map_from_basic_set(
5316 set->p[i], isl_space_copy(dim));
5317 if (!map->p[i])
5318 goto error;
5320 isl_space_free(map->dim);
5321 map->dim = dim;
5322 return map;
5323 error:
5324 isl_space_free(dim);
5325 isl_set_free(set);
5326 return NULL;
5329 __isl_give isl_basic_map *isl_basic_map_from_domain(
5330 __isl_take isl_basic_set *bset)
5332 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5335 __isl_give isl_basic_map *isl_basic_map_from_range(
5336 __isl_take isl_basic_set *bset)
5338 isl_space *space;
5339 space = isl_basic_set_get_space(bset);
5340 space = isl_space_from_range(space);
5341 bset = isl_basic_set_reset_space(bset, space);
5342 return (isl_basic_map *)bset;
5345 /* Create a relation with the given set as range.
5346 * The domain of the created relation is a zero-dimensional
5347 * flat anonymous space.
5349 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5351 isl_space *space;
5352 space = isl_set_get_space(set);
5353 space = isl_space_from_range(space);
5354 set = isl_set_reset_space(set, space);
5355 return (struct isl_map *)set;
5358 /* Create a relation with the given set as domain.
5359 * The range of the created relation is a zero-dimensional
5360 * flat anonymous space.
5362 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5364 return isl_map_reverse(isl_map_from_range(set));
5367 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5368 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5370 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5373 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5374 __isl_take isl_set *range)
5376 return isl_map_apply_range(isl_map_reverse(domain), range);
5379 /* Return a newly allocated isl_map with given space and flags and
5380 * room for "n" basic maps.
5381 * Make sure that all cached information is cleared.
5383 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5384 unsigned flags)
5386 struct isl_map *map;
5388 if (!space)
5389 return NULL;
5390 if (n < 0)
5391 isl_die(space->ctx, isl_error_internal,
5392 "negative number of basic maps", goto error);
5393 map = isl_calloc(space->ctx, struct isl_map,
5394 sizeof(struct isl_map) +
5395 (n - 1) * sizeof(struct isl_basic_map *));
5396 if (!map)
5397 goto error;
5399 map->ctx = space->ctx;
5400 isl_ctx_ref(map->ctx);
5401 map->ref = 1;
5402 map->size = n;
5403 map->n = 0;
5404 map->dim = space;
5405 map->flags = flags;
5406 return map;
5407 error:
5408 isl_space_free(space);
5409 return NULL;
5412 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5413 unsigned nparam, unsigned in, unsigned out, int n,
5414 unsigned flags)
5416 struct isl_map *map;
5417 isl_space *dims;
5419 dims = isl_space_alloc(ctx, nparam, in, out);
5420 if (!dims)
5421 return NULL;
5423 map = isl_map_alloc_space(dims, n, flags);
5424 return map;
5427 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5429 struct isl_basic_map *bmap;
5430 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5431 bmap = isl_basic_map_set_to_empty(bmap);
5432 return bmap;
5435 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5437 struct isl_basic_set *bset;
5438 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5439 bset = isl_basic_set_set_to_empty(bset);
5440 return bset;
5443 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5445 struct isl_basic_map *bmap;
5446 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5447 bmap = isl_basic_map_finalize(bmap);
5448 return bmap;
5451 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5453 struct isl_basic_set *bset;
5454 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5455 bset = isl_basic_set_finalize(bset);
5456 return bset;
5459 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5461 int i;
5462 unsigned total = isl_space_dim(dim, isl_dim_all);
5463 isl_basic_map *bmap;
5465 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5466 for (i = 0; i < total; ++i) {
5467 int k = isl_basic_map_alloc_inequality(bmap);
5468 if (k < 0)
5469 goto error;
5470 isl_seq_clr(bmap->ineq[k], 1 + total);
5471 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5473 return bmap;
5474 error:
5475 isl_basic_map_free(bmap);
5476 return NULL;
5479 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5481 return isl_basic_map_nat_universe(dim);
5484 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5486 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5489 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5491 return isl_map_nat_universe(dim);
5494 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5496 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5499 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5501 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5504 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5506 struct isl_map *map;
5507 if (!dim)
5508 return NULL;
5509 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5510 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5511 return map;
5514 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5516 struct isl_set *set;
5517 if (!dim)
5518 return NULL;
5519 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5520 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5521 return set;
5524 struct isl_map *isl_map_dup(struct isl_map *map)
5526 int i;
5527 struct isl_map *dup;
5529 if (!map)
5530 return NULL;
5531 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5532 for (i = 0; i < map->n; ++i)
5533 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5534 return dup;
5537 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5538 __isl_take isl_basic_map *bmap)
5540 if (!bmap || !map)
5541 goto error;
5542 if (isl_basic_map_plain_is_empty(bmap)) {
5543 isl_basic_map_free(bmap);
5544 return map;
5546 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5547 isl_assert(map->ctx, map->n < map->size, goto error);
5548 map->p[map->n] = bmap;
5549 map->n++;
5550 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5551 return map;
5552 error:
5553 if (map)
5554 isl_map_free(map);
5555 if (bmap)
5556 isl_basic_map_free(bmap);
5557 return NULL;
5560 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5562 int i;
5564 if (!map)
5565 return NULL;
5567 if (--map->ref > 0)
5568 return NULL;
5570 clear_caches(map);
5571 isl_ctx_deref(map->ctx);
5572 for (i = 0; i < map->n; ++i)
5573 isl_basic_map_free(map->p[i]);
5574 isl_space_free(map->dim);
5575 free(map);
5577 return NULL;
5580 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5581 struct isl_basic_map *bmap, unsigned pos, int value)
5583 int j;
5585 bmap = isl_basic_map_cow(bmap);
5586 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5587 j = isl_basic_map_alloc_equality(bmap);
5588 if (j < 0)
5589 goto error;
5590 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5591 isl_int_set_si(bmap->eq[j][pos], -1);
5592 isl_int_set_si(bmap->eq[j][0], value);
5593 bmap = isl_basic_map_simplify(bmap);
5594 return isl_basic_map_finalize(bmap);
5595 error:
5596 isl_basic_map_free(bmap);
5597 return NULL;
5600 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5601 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5603 int j;
5605 bmap = isl_basic_map_cow(bmap);
5606 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5607 j = isl_basic_map_alloc_equality(bmap);
5608 if (j < 0)
5609 goto error;
5610 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5611 isl_int_set_si(bmap->eq[j][pos], -1);
5612 isl_int_set(bmap->eq[j][0], value);
5613 bmap = isl_basic_map_simplify(bmap);
5614 return isl_basic_map_finalize(bmap);
5615 error:
5616 isl_basic_map_free(bmap);
5617 return NULL;
5620 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5621 enum isl_dim_type type, unsigned pos, int value)
5623 if (!bmap)
5624 return NULL;
5625 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5626 return isl_basic_map_fix_pos_si(bmap,
5627 isl_basic_map_offset(bmap, type) + pos, value);
5628 error:
5629 isl_basic_map_free(bmap);
5630 return NULL;
5633 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5634 enum isl_dim_type type, unsigned pos, isl_int value)
5636 if (!bmap)
5637 return NULL;
5638 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5639 return isl_basic_map_fix_pos(bmap,
5640 isl_basic_map_offset(bmap, type) + pos, value);
5641 error:
5642 isl_basic_map_free(bmap);
5643 return NULL;
5646 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5647 * to be equal to "v".
5649 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5650 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5652 if (!bmap || !v)
5653 goto error;
5654 if (!isl_val_is_int(v))
5655 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5656 "expecting integer value", goto error);
5657 if (pos >= isl_basic_map_dim(bmap, type))
5658 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5659 "index out of bounds", goto error);
5660 pos += isl_basic_map_offset(bmap, type);
5661 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5662 isl_val_free(v);
5663 return bmap;
5664 error:
5665 isl_basic_map_free(bmap);
5666 isl_val_free(v);
5667 return NULL;
5670 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5671 * to be equal to "v".
5673 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5674 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5676 return isl_basic_map_fix_val(bset, type, pos, v);
5679 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5680 enum isl_dim_type type, unsigned pos, int value)
5682 return (struct isl_basic_set *)
5683 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5684 type, pos, value);
5687 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5688 enum isl_dim_type type, unsigned pos, isl_int value)
5690 return (struct isl_basic_set *)
5691 isl_basic_map_fix((struct isl_basic_map *)bset,
5692 type, pos, value);
5695 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5696 unsigned input, int value)
5698 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5701 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5702 unsigned dim, int value)
5704 return (struct isl_basic_set *)
5705 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5706 isl_dim_set, dim, value);
5709 static int remove_if_empty(__isl_keep isl_map *map, int i)
5711 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5713 if (empty < 0)
5714 return -1;
5715 if (!empty)
5716 return 0;
5718 isl_basic_map_free(map->p[i]);
5719 if (i != map->n - 1) {
5720 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5721 map->p[i] = map->p[map->n - 1];
5723 map->n--;
5725 return 0;
5728 /* Perform "fn" on each basic map of "map", where we may not be holding
5729 * the only reference to "map".
5730 * In particular, "fn" should be a semantics preserving operation
5731 * that we want to apply to all copies of "map". We therefore need
5732 * to be careful not to modify "map" in a way that breaks "map"
5733 * in case anything goes wrong.
5735 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5736 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5738 struct isl_basic_map *bmap;
5739 int i;
5741 if (!map)
5742 return NULL;
5744 for (i = map->n - 1; i >= 0; --i) {
5745 bmap = isl_basic_map_copy(map->p[i]);
5746 bmap = fn(bmap);
5747 if (!bmap)
5748 goto error;
5749 isl_basic_map_free(map->p[i]);
5750 map->p[i] = bmap;
5751 if (remove_if_empty(map, i) < 0)
5752 goto error;
5755 return map;
5756 error:
5757 isl_map_free(map);
5758 return NULL;
5761 struct isl_map *isl_map_fix_si(struct isl_map *map,
5762 enum isl_dim_type type, unsigned pos, int value)
5764 int i;
5766 map = isl_map_cow(map);
5767 if (!map)
5768 return NULL;
5770 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5771 for (i = map->n - 1; i >= 0; --i) {
5772 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5773 if (remove_if_empty(map, i) < 0)
5774 goto error;
5776 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5777 return map;
5778 error:
5779 isl_map_free(map);
5780 return NULL;
5783 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5784 enum isl_dim_type type, unsigned pos, int value)
5786 return (struct isl_set *)
5787 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5790 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5791 enum isl_dim_type type, unsigned pos, isl_int value)
5793 int i;
5795 map = isl_map_cow(map);
5796 if (!map)
5797 return NULL;
5799 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5800 for (i = 0; i < map->n; ++i) {
5801 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5802 if (!map->p[i])
5803 goto error;
5805 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5806 return map;
5807 error:
5808 isl_map_free(map);
5809 return NULL;
5812 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5813 enum isl_dim_type type, unsigned pos, isl_int value)
5815 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5818 /* Fix the value of the variable at position "pos" of type "type" of "map"
5819 * to be equal to "v".
5821 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5822 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5824 int i;
5826 map = isl_map_cow(map);
5827 if (!map || !v)
5828 goto error;
5830 if (!isl_val_is_int(v))
5831 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5832 "expecting integer value", goto error);
5833 if (pos >= isl_map_dim(map, type))
5834 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5835 "index out of bounds", goto error);
5836 for (i = map->n - 1; i >= 0; --i) {
5837 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5838 isl_val_copy(v));
5839 if (remove_if_empty(map, i) < 0)
5840 goto error;
5842 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5843 isl_val_free(v);
5844 return map;
5845 error:
5846 isl_map_free(map);
5847 isl_val_free(v);
5848 return NULL;
5851 /* Fix the value of the variable at position "pos" of type "type" of "set"
5852 * to be equal to "v".
5854 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5855 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5857 return isl_map_fix_val(set, type, pos, v);
5860 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5861 unsigned input, int value)
5863 return isl_map_fix_si(map, isl_dim_in, input, value);
5866 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5868 return (struct isl_set *)
5869 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5872 static __isl_give isl_basic_map *basic_map_bound_si(
5873 __isl_take isl_basic_map *bmap,
5874 enum isl_dim_type type, unsigned pos, int value, int upper)
5876 int j;
5878 if (!bmap)
5879 return NULL;
5880 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5881 pos += isl_basic_map_offset(bmap, type);
5882 bmap = isl_basic_map_cow(bmap);
5883 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5884 j = isl_basic_map_alloc_inequality(bmap);
5885 if (j < 0)
5886 goto error;
5887 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5888 if (upper) {
5889 isl_int_set_si(bmap->ineq[j][pos], -1);
5890 isl_int_set_si(bmap->ineq[j][0], value);
5891 } else {
5892 isl_int_set_si(bmap->ineq[j][pos], 1);
5893 isl_int_set_si(bmap->ineq[j][0], -value);
5895 bmap = isl_basic_map_simplify(bmap);
5896 return isl_basic_map_finalize(bmap);
5897 error:
5898 isl_basic_map_free(bmap);
5899 return NULL;
5902 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5903 __isl_take isl_basic_map *bmap,
5904 enum isl_dim_type type, unsigned pos, int value)
5906 return basic_map_bound_si(bmap, type, pos, value, 0);
5909 /* Constrain the values of the given dimension to be no greater than "value".
5911 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5912 __isl_take isl_basic_map *bmap,
5913 enum isl_dim_type type, unsigned pos, int value)
5915 return basic_map_bound_si(bmap, type, pos, value, 1);
5918 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5919 unsigned dim, isl_int value)
5921 int j;
5923 bset = isl_basic_set_cow(bset);
5924 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5925 j = isl_basic_set_alloc_inequality(bset);
5926 if (j < 0)
5927 goto error;
5928 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5929 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5930 isl_int_neg(bset->ineq[j][0], value);
5931 bset = isl_basic_set_simplify(bset);
5932 return isl_basic_set_finalize(bset);
5933 error:
5934 isl_basic_set_free(bset);
5935 return NULL;
5938 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5939 enum isl_dim_type type, unsigned pos, int value, int upper)
5941 int i;
5943 map = isl_map_cow(map);
5944 if (!map)
5945 return NULL;
5947 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5948 for (i = 0; i < map->n; ++i) {
5949 map->p[i] = basic_map_bound_si(map->p[i],
5950 type, pos, value, upper);
5951 if (!map->p[i])
5952 goto error;
5954 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5955 return map;
5956 error:
5957 isl_map_free(map);
5958 return NULL;
5961 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5962 enum isl_dim_type type, unsigned pos, int value)
5964 return map_bound_si(map, type, pos, value, 0);
5967 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5968 enum isl_dim_type type, unsigned pos, int value)
5970 return map_bound_si(map, type, pos, value, 1);
5973 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5974 enum isl_dim_type type, unsigned pos, int value)
5976 return (struct isl_set *)
5977 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5980 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5981 enum isl_dim_type type, unsigned pos, int value)
5983 return isl_map_upper_bound_si(set, type, pos, value);
5986 /* Bound the given variable of "bmap" from below (or above is "upper"
5987 * is set) to "value".
5989 static __isl_give isl_basic_map *basic_map_bound(
5990 __isl_take isl_basic_map *bmap,
5991 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5993 int j;
5995 if (!bmap)
5996 return NULL;
5997 if (pos >= isl_basic_map_dim(bmap, type))
5998 isl_die(bmap->ctx, isl_error_invalid,
5999 "index out of bounds", goto error);
6000 pos += isl_basic_map_offset(bmap, type);
6001 bmap = isl_basic_map_cow(bmap);
6002 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6003 j = isl_basic_map_alloc_inequality(bmap);
6004 if (j < 0)
6005 goto error;
6006 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6007 if (upper) {
6008 isl_int_set_si(bmap->ineq[j][pos], -1);
6009 isl_int_set(bmap->ineq[j][0], value);
6010 } else {
6011 isl_int_set_si(bmap->ineq[j][pos], 1);
6012 isl_int_neg(bmap->ineq[j][0], value);
6014 bmap = isl_basic_map_simplify(bmap);
6015 return isl_basic_map_finalize(bmap);
6016 error:
6017 isl_basic_map_free(bmap);
6018 return NULL;
6021 /* Bound the given variable of "map" from below (or above is "upper"
6022 * is set) to "value".
6024 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6025 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6027 int i;
6029 map = isl_map_cow(map);
6030 if (!map)
6031 return NULL;
6033 if (pos >= isl_map_dim(map, type))
6034 isl_die(map->ctx, isl_error_invalid,
6035 "index out of bounds", goto error);
6036 for (i = map->n - 1; i >= 0; --i) {
6037 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6038 if (remove_if_empty(map, i) < 0)
6039 goto error;
6041 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6042 return map;
6043 error:
6044 isl_map_free(map);
6045 return NULL;
6048 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6049 enum isl_dim_type type, unsigned pos, isl_int value)
6051 return map_bound(map, type, pos, value, 0);
6054 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6055 enum isl_dim_type type, unsigned pos, isl_int value)
6057 return map_bound(map, type, pos, value, 1);
6060 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6061 enum isl_dim_type type, unsigned pos, isl_int value)
6063 return isl_map_lower_bound(set, type, pos, value);
6066 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6067 enum isl_dim_type type, unsigned pos, isl_int value)
6069 return isl_map_upper_bound(set, type, pos, value);
6072 /* Force the values of the variable at position "pos" of type "type" of "set"
6073 * to be no smaller than "value".
6075 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6076 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6078 if (!value)
6079 goto error;
6080 if (!isl_val_is_int(value))
6081 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6082 "expecting integer value", goto error);
6083 set = isl_set_lower_bound(set, type, pos, value->n);
6084 isl_val_free(value);
6085 return set;
6086 error:
6087 isl_val_free(value);
6088 isl_set_free(set);
6089 return NULL;
6092 /* Force the values of the variable at position "pos" of type "type" of "set"
6093 * to be no greater than "value".
6095 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6096 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6098 if (!value)
6099 goto error;
6100 if (!isl_val_is_int(value))
6101 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6102 "expecting integer value", goto error);
6103 set = isl_set_upper_bound(set, type, pos, value->n);
6104 isl_val_free(value);
6105 return set;
6106 error:
6107 isl_val_free(value);
6108 isl_set_free(set);
6109 return NULL;
6112 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6113 isl_int value)
6115 int i;
6117 set = isl_set_cow(set);
6118 if (!set)
6119 return NULL;
6121 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6122 for (i = 0; i < set->n; ++i) {
6123 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6124 if (!set->p[i])
6125 goto error;
6127 return set;
6128 error:
6129 isl_set_free(set);
6130 return NULL;
6133 struct isl_map *isl_map_reverse(struct isl_map *map)
6135 int i;
6137 map = isl_map_cow(map);
6138 if (!map)
6139 return NULL;
6141 map->dim = isl_space_reverse(map->dim);
6142 if (!map->dim)
6143 goto error;
6144 for (i = 0; i < map->n; ++i) {
6145 map->p[i] = isl_basic_map_reverse(map->p[i]);
6146 if (!map->p[i])
6147 goto error;
6149 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6150 return map;
6151 error:
6152 isl_map_free(map);
6153 return NULL;
6156 #undef TYPE
6157 #define TYPE isl_pw_multi_aff
6158 #undef SUFFIX
6159 #define SUFFIX _pw_multi_aff
6160 #undef EMPTY
6161 #define EMPTY isl_pw_multi_aff_empty
6162 #undef ADD
6163 #define ADD isl_pw_multi_aff_union_add
6164 #include "isl_map_lexopt_templ.c"
6166 /* Given a map "map", compute the lexicographically minimal
6167 * (or maximal) image element for each domain element in dom,
6168 * in the form of an isl_pw_multi_aff.
6169 * If "empty" is not NULL, then set *empty to those elements in dom that
6170 * do not have an image element.
6171 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6172 * should be computed over the domain of "map". "empty" is also NULL
6173 * in this case.
6175 * We first compute the lexicographically minimal or maximal element
6176 * in the first basic map. This results in a partial solution "res"
6177 * and a subset "todo" of dom that still need to be handled.
6178 * We then consider each of the remaining maps in "map" and successively
6179 * update both "res" and "todo".
6180 * If "empty" is NULL, then the todo sets are not needed and therefore
6181 * also not computed.
6183 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6184 __isl_take isl_map *map, __isl_take isl_set *dom,
6185 __isl_give isl_set **empty, unsigned flags)
6187 int i;
6188 int full;
6189 isl_pw_multi_aff *res;
6190 isl_set *todo;
6192 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6193 if (!map || (!full && !dom))
6194 goto error;
6196 if (isl_map_plain_is_empty(map)) {
6197 if (empty)
6198 *empty = dom;
6199 else
6200 isl_set_free(dom);
6201 return isl_pw_multi_aff_from_map(map);
6204 res = basic_map_partial_lexopt_pw_multi_aff(
6205 isl_basic_map_copy(map->p[0]),
6206 isl_set_copy(dom), empty, flags);
6208 if (empty)
6209 todo = *empty;
6210 for (i = 1; i < map->n; ++i) {
6211 isl_pw_multi_aff *res_i;
6213 res_i = basic_map_partial_lexopt_pw_multi_aff(
6214 isl_basic_map_copy(map->p[i]),
6215 isl_set_copy(dom), empty, flags);
6217 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6218 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6219 else
6220 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6222 if (empty)
6223 todo = isl_set_intersect(todo, *empty);
6226 isl_set_free(dom);
6227 isl_map_free(map);
6229 if (empty)
6230 *empty = todo;
6232 return res;
6233 error:
6234 if (empty)
6235 *empty = NULL;
6236 isl_set_free(dom);
6237 isl_map_free(map);
6238 return NULL;
6241 #undef TYPE
6242 #define TYPE isl_map
6243 #undef SUFFIX
6244 #define SUFFIX
6245 #undef EMPTY
6246 #define EMPTY isl_map_empty
6247 #undef ADD
6248 #define ADD isl_map_union_disjoint
6249 #include "isl_map_lexopt_templ.c"
6251 /* Given a map "map", compute the lexicographically minimal
6252 * (or maximal) image element for each domain element in "dom",
6253 * in the form of an isl_map.
6254 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6255 * do not have an image element.
6256 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6257 * should be computed over the domain of "map". "empty" is also NULL
6258 * in this case.
6260 * If the input consists of more than one disjunct, then first
6261 * compute the desired result in the form of an isl_pw_multi_aff and
6262 * then convert that into an isl_map.
6264 * This function used to have an explicit implementation in terms
6265 * of isl_maps, but it would continually intersect the domains of
6266 * partial results with the complement of the domain of the next
6267 * partial solution, potentially leading to an explosion in the number
6268 * of disjuncts if there are several disjuncts in the input.
6269 * An even earlier implementation of this function would look for
6270 * better results in the domain of the partial result and for extra
6271 * results in the complement of this domain, which would lead to
6272 * even more splintering.
6274 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6275 __isl_take isl_map *map, __isl_take isl_set *dom,
6276 __isl_give isl_set **empty, unsigned flags)
6278 int full;
6279 struct isl_map *res;
6280 isl_pw_multi_aff *pma;
6282 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6283 if (!map || (!full && !dom))
6284 goto error;
6286 if (isl_map_plain_is_empty(map)) {
6287 if (empty)
6288 *empty = dom;
6289 else
6290 isl_set_free(dom);
6291 return map;
6294 if (map->n == 1) {
6295 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6296 dom, empty, flags);
6297 isl_map_free(map);
6298 return res;
6301 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6302 flags);
6303 return isl_map_from_pw_multi_aff(pma);
6304 error:
6305 if (empty)
6306 *empty = NULL;
6307 isl_set_free(dom);
6308 isl_map_free(map);
6309 return NULL;
6312 __isl_give isl_map *isl_map_partial_lexmax(
6313 __isl_take isl_map *map, __isl_take isl_set *dom,
6314 __isl_give isl_set **empty)
6316 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6319 __isl_give isl_map *isl_map_partial_lexmin(
6320 __isl_take isl_map *map, __isl_take isl_set *dom,
6321 __isl_give isl_set **empty)
6323 return isl_map_partial_lexopt(map, dom, empty, 0);
6326 __isl_give isl_set *isl_set_partial_lexmin(
6327 __isl_take isl_set *set, __isl_take isl_set *dom,
6328 __isl_give isl_set **empty)
6330 return (struct isl_set *)
6331 isl_map_partial_lexmin((struct isl_map *)set,
6332 dom, empty);
6335 __isl_give isl_set *isl_set_partial_lexmax(
6336 __isl_take isl_set *set, __isl_take isl_set *dom,
6337 __isl_give isl_set **empty)
6339 return (struct isl_set *)
6340 isl_map_partial_lexmax((struct isl_map *)set,
6341 dom, empty);
6344 /* Compute the lexicographic minimum (or maximum if "flags" includes
6345 * ISL_OPT_MAX) of "bset" over its parametric domain.
6347 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6348 unsigned flags)
6350 return isl_basic_map_lexopt(bset, flags);
6353 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6355 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6358 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6360 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6363 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6365 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6368 /* Compute the lexicographic minimum of "bset" over its parametric domain
6369 * for the purpose of quantifier elimination.
6370 * That is, find an explicit representation for all the existentially
6371 * quantified variables in "bset" by computing their lexicographic
6372 * minimum.
6374 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6375 __isl_take isl_basic_set *bset)
6377 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6380 /* Extract the first and only affine expression from list
6381 * and then add it to *pwaff with the given dom.
6382 * This domain is known to be disjoint from other domains
6383 * because of the way isl_basic_map_foreach_lexmax works.
6385 static int update_dim_opt(__isl_take isl_basic_set *dom,
6386 __isl_take isl_aff_list *list, void *user)
6388 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6389 isl_aff *aff;
6390 isl_pw_aff **pwaff = user;
6391 isl_pw_aff *pwaff_i;
6393 if (!list)
6394 goto error;
6395 if (isl_aff_list_n_aff(list) != 1)
6396 isl_die(ctx, isl_error_internal,
6397 "expecting single element list", goto error);
6399 aff = isl_aff_list_get_aff(list, 0);
6400 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6402 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6404 isl_aff_list_free(list);
6406 return 0;
6407 error:
6408 isl_basic_set_free(dom);
6409 isl_aff_list_free(list);
6410 return -1;
6413 /* Given a basic map with one output dimension, compute the minimum or
6414 * maximum of that dimension as an isl_pw_aff.
6416 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6417 * call update_dim_opt on each leaf of the result.
6419 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6420 int max)
6422 isl_space *dim = isl_basic_map_get_space(bmap);
6423 isl_pw_aff *pwaff;
6424 int r;
6426 dim = isl_space_from_domain(isl_space_domain(dim));
6427 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6428 pwaff = isl_pw_aff_empty(dim);
6430 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6431 if (r < 0)
6432 return isl_pw_aff_free(pwaff);
6434 return pwaff;
6437 /* Compute the minimum or maximum of the given output dimension
6438 * as a function of the parameters and the input dimensions,
6439 * but independently of the other output dimensions.
6441 * We first project out the other output dimension and then compute
6442 * the "lexicographic" maximum in each basic map, combining the results
6443 * using isl_pw_aff_union_max.
6445 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6446 int max)
6448 int i;
6449 isl_pw_aff *pwaff;
6450 unsigned n_out;
6452 n_out = isl_map_dim(map, isl_dim_out);
6453 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6454 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6455 if (!map)
6456 return NULL;
6458 if (map->n == 0) {
6459 isl_space *dim = isl_map_get_space(map);
6460 isl_map_free(map);
6461 return isl_pw_aff_empty(dim);
6464 pwaff = basic_map_dim_opt(map->p[0], max);
6465 for (i = 1; i < map->n; ++i) {
6466 isl_pw_aff *pwaff_i;
6468 pwaff_i = basic_map_dim_opt(map->p[i], max);
6469 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6472 isl_map_free(map);
6474 return pwaff;
6477 /* Compute the minimum of the given output dimension as a function of the
6478 * parameters and input dimensions, but independently of
6479 * the other output dimensions.
6481 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6483 return map_dim_opt(map, pos, 0);
6486 /* Compute the maximum of the given output dimension as a function of the
6487 * parameters and input dimensions, but independently of
6488 * the other output dimensions.
6490 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6492 return map_dim_opt(map, pos, 1);
6495 /* Compute the minimum or maximum of the given set dimension
6496 * as a function of the parameters,
6497 * but independently of the other set dimensions.
6499 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6500 int max)
6502 return map_dim_opt(set, pos, max);
6505 /* Compute the maximum of the given set dimension as a function of the
6506 * parameters, but independently of the other set dimensions.
6508 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6510 return set_dim_opt(set, pos, 1);
6513 /* Compute the minimum of the given set dimension as a function of the
6514 * parameters, but independently of the other set dimensions.
6516 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6518 return set_dim_opt(set, pos, 0);
6521 /* Apply a preimage specified by "mat" on the parameters of "bset".
6522 * bset is assumed to have only parameters and divs.
6524 static struct isl_basic_set *basic_set_parameter_preimage(
6525 struct isl_basic_set *bset, struct isl_mat *mat)
6527 unsigned nparam;
6529 if (!bset || !mat)
6530 goto error;
6532 bset->dim = isl_space_cow(bset->dim);
6533 if (!bset->dim)
6534 goto error;
6536 nparam = isl_basic_set_dim(bset, isl_dim_param);
6538 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6540 bset->dim->nparam = 0;
6541 bset->dim->n_out = nparam;
6542 bset = isl_basic_set_preimage(bset, mat);
6543 if (bset) {
6544 bset->dim->nparam = bset->dim->n_out;
6545 bset->dim->n_out = 0;
6547 return bset;
6548 error:
6549 isl_mat_free(mat);
6550 isl_basic_set_free(bset);
6551 return NULL;
6554 /* Apply a preimage specified by "mat" on the parameters of "set".
6555 * set is assumed to have only parameters and divs.
6557 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6558 __isl_take isl_mat *mat)
6560 isl_space *space;
6561 unsigned nparam;
6563 if (!set || !mat)
6564 goto error;
6566 nparam = isl_set_dim(set, isl_dim_param);
6568 if (mat->n_row != 1 + nparam)
6569 isl_die(isl_set_get_ctx(set), isl_error_internal,
6570 "unexpected number of rows", goto error);
6572 space = isl_set_get_space(set);
6573 space = isl_space_move_dims(space, isl_dim_set, 0,
6574 isl_dim_param, 0, nparam);
6575 set = isl_set_reset_space(set, space);
6576 set = isl_set_preimage(set, mat);
6577 nparam = isl_set_dim(set, isl_dim_out);
6578 space = isl_set_get_space(set);
6579 space = isl_space_move_dims(space, isl_dim_param, 0,
6580 isl_dim_out, 0, nparam);
6581 set = isl_set_reset_space(set, space);
6582 return set;
6583 error:
6584 isl_mat_free(mat);
6585 isl_set_free(set);
6586 return NULL;
6589 /* Intersect the basic set "bset" with the affine space specified by the
6590 * equalities in "eq".
6592 static struct isl_basic_set *basic_set_append_equalities(
6593 struct isl_basic_set *bset, struct isl_mat *eq)
6595 int i, k;
6596 unsigned len;
6598 if (!bset || !eq)
6599 goto error;
6601 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6602 eq->n_row, 0);
6603 if (!bset)
6604 goto error;
6606 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6607 for (i = 0; i < eq->n_row; ++i) {
6608 k = isl_basic_set_alloc_equality(bset);
6609 if (k < 0)
6610 goto error;
6611 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6612 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6614 isl_mat_free(eq);
6616 bset = isl_basic_set_gauss(bset, NULL);
6617 bset = isl_basic_set_finalize(bset);
6619 return bset;
6620 error:
6621 isl_mat_free(eq);
6622 isl_basic_set_free(bset);
6623 return NULL;
6626 /* Intersect the set "set" with the affine space specified by the
6627 * equalities in "eq".
6629 static struct isl_set *set_append_equalities(struct isl_set *set,
6630 struct isl_mat *eq)
6632 int i;
6634 if (!set || !eq)
6635 goto error;
6637 for (i = 0; i < set->n; ++i) {
6638 set->p[i] = basic_set_append_equalities(set->p[i],
6639 isl_mat_copy(eq));
6640 if (!set->p[i])
6641 goto error;
6643 isl_mat_free(eq);
6644 return set;
6645 error:
6646 isl_mat_free(eq);
6647 isl_set_free(set);
6648 return NULL;
6651 /* Given a basic set "bset" that only involves parameters and existentially
6652 * quantified variables, return the index of the first equality
6653 * that only involves parameters. If there is no such equality then
6654 * return bset->n_eq.
6656 * This function assumes that isl_basic_set_gauss has been called on "bset".
6658 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6660 int i, j;
6661 unsigned nparam, n_div;
6663 if (!bset)
6664 return -1;
6666 nparam = isl_basic_set_dim(bset, isl_dim_param);
6667 n_div = isl_basic_set_dim(bset, isl_dim_div);
6669 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6670 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6671 ++i;
6674 return i;
6677 /* Compute an explicit representation for the existentially quantified
6678 * variables in "bset" by computing the "minimal value" of the set
6679 * variables. Since there are no set variables, the computation of
6680 * the minimal value essentially computes an explicit representation
6681 * of the non-empty part(s) of "bset".
6683 * The input only involves parameters and existentially quantified variables.
6684 * All equalities among parameters have been removed.
6686 * Since the existentially quantified variables in the result are in general
6687 * going to be different from those in the input, we first replace
6688 * them by the minimal number of variables based on their equalities.
6689 * This should simplify the parametric integer programming.
6691 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6693 isl_morph *morph1, *morph2;
6694 isl_set *set;
6695 unsigned n;
6697 if (!bset)
6698 return NULL;
6699 if (bset->n_eq == 0)
6700 return isl_basic_set_lexmin_compute_divs(bset);
6702 morph1 = isl_basic_set_parameter_compression(bset);
6703 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6704 bset = isl_basic_set_lift(bset);
6705 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6706 bset = isl_morph_basic_set(morph2, bset);
6707 n = isl_basic_set_dim(bset, isl_dim_set);
6708 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6710 set = isl_basic_set_lexmin_compute_divs(bset);
6712 set = isl_morph_set(isl_morph_inverse(morph1), set);
6714 return set;
6717 /* Project the given basic set onto its parameter domain, possibly introducing
6718 * new, explicit, existential variables in the constraints.
6719 * The input has parameters and (possibly implicit) existential variables.
6720 * The output has the same parameters, but only
6721 * explicit existentially quantified variables.
6723 * The actual projection is performed by pip, but pip doesn't seem
6724 * to like equalities very much, so we first remove the equalities
6725 * among the parameters by performing a variable compression on
6726 * the parameters. Afterward, an inverse transformation is performed
6727 * and the equalities among the parameters are inserted back in.
6729 * The variable compression on the parameters may uncover additional
6730 * equalities that were only implicit before. We therefore check
6731 * if there are any new parameter equalities in the result and
6732 * if so recurse. The removal of parameter equalities is required
6733 * for the parameter compression performed by base_compute_divs.
6735 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6737 int i;
6738 struct isl_mat *eq;
6739 struct isl_mat *T, *T2;
6740 struct isl_set *set;
6741 unsigned nparam;
6743 bset = isl_basic_set_cow(bset);
6744 if (!bset)
6745 return NULL;
6747 if (bset->n_eq == 0)
6748 return base_compute_divs(bset);
6750 bset = isl_basic_set_gauss(bset, NULL);
6751 if (!bset)
6752 return NULL;
6753 if (isl_basic_set_plain_is_empty(bset))
6754 return isl_set_from_basic_set(bset);
6756 i = first_parameter_equality(bset);
6757 if (i == bset->n_eq)
6758 return base_compute_divs(bset);
6760 nparam = isl_basic_set_dim(bset, isl_dim_param);
6761 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6762 0, 1 + nparam);
6763 eq = isl_mat_cow(eq);
6764 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6765 if (T && T->n_col == 0) {
6766 isl_mat_free(T);
6767 isl_mat_free(T2);
6768 isl_mat_free(eq);
6769 bset = isl_basic_set_set_to_empty(bset);
6770 return isl_set_from_basic_set(bset);
6772 bset = basic_set_parameter_preimage(bset, T);
6774 i = first_parameter_equality(bset);
6775 if (!bset)
6776 set = NULL;
6777 else if (i == bset->n_eq)
6778 set = base_compute_divs(bset);
6779 else
6780 set = parameter_compute_divs(bset);
6781 set = set_parameter_preimage(set, T2);
6782 set = set_append_equalities(set, eq);
6783 return set;
6786 /* Insert the divs from "ls" before those of "bmap".
6788 * The number of columns is not changed, which means that the last
6789 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6790 * The caller is responsible for removing the same number of dimensions
6791 * from the space of "bmap".
6793 static __isl_give isl_basic_map *insert_divs_from_local_space(
6794 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6796 int i;
6797 int n_div;
6798 int old_n_div;
6800 n_div = isl_local_space_dim(ls, isl_dim_div);
6801 if (n_div == 0)
6802 return bmap;
6804 old_n_div = bmap->n_div;
6805 bmap = insert_div_rows(bmap, n_div);
6806 if (!bmap)
6807 return NULL;
6809 for (i = 0; i < n_div; ++i) {
6810 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6811 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6814 return bmap;
6817 /* Replace the space of "bmap" by the space and divs of "ls".
6819 * If "ls" has any divs, then we simplify the result since we may
6820 * have discovered some additional equalities that could simplify
6821 * the div expressions.
6823 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6824 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6826 int n_div;
6828 bmap = isl_basic_map_cow(bmap);
6829 if (!bmap || !ls)
6830 goto error;
6832 n_div = isl_local_space_dim(ls, isl_dim_div);
6833 bmap = insert_divs_from_local_space(bmap, ls);
6834 if (!bmap)
6835 goto error;
6837 isl_space_free(bmap->dim);
6838 bmap->dim = isl_local_space_get_space(ls);
6839 if (!bmap->dim)
6840 goto error;
6842 isl_local_space_free(ls);
6843 if (n_div > 0)
6844 bmap = isl_basic_map_simplify(bmap);
6845 bmap = isl_basic_map_finalize(bmap);
6846 return bmap;
6847 error:
6848 isl_basic_map_free(bmap);
6849 isl_local_space_free(ls);
6850 return NULL;
6853 /* Replace the space of "map" by the space and divs of "ls".
6855 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6856 __isl_take isl_local_space *ls)
6858 int i;
6860 map = isl_map_cow(map);
6861 if (!map || !ls)
6862 goto error;
6864 for (i = 0; i < map->n; ++i) {
6865 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6866 isl_local_space_copy(ls));
6867 if (!map->p[i])
6868 goto error;
6870 isl_space_free(map->dim);
6871 map->dim = isl_local_space_get_space(ls);
6872 if (!map->dim)
6873 goto error;
6875 isl_local_space_free(ls);
6876 return map;
6877 error:
6878 isl_local_space_free(ls);
6879 isl_map_free(map);
6880 return NULL;
6883 /* Compute an explicit representation for the existentially
6884 * quantified variables for which do not know any explicit representation yet.
6886 * We first sort the existentially quantified variables so that the
6887 * existentially quantified variables for which we already have an explicit
6888 * representation are placed before those for which we do not.
6889 * The input dimensions, the output dimensions and the existentially
6890 * quantified variables for which we already have an explicit
6891 * representation are then turned into parameters.
6892 * compute_divs returns a map with the same parameters and
6893 * no input or output dimensions and the dimension specification
6894 * is reset to that of the input, including the existentially quantified
6895 * variables for which we already had an explicit representation.
6897 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6899 struct isl_basic_set *bset;
6900 struct isl_set *set;
6901 struct isl_map *map;
6902 isl_space *dim;
6903 isl_local_space *ls;
6904 unsigned nparam;
6905 unsigned n_in;
6906 unsigned n_out;
6907 int n_known;
6908 int i;
6910 bmap = isl_basic_map_sort_divs(bmap);
6911 bmap = isl_basic_map_cow(bmap);
6912 if (!bmap)
6913 return NULL;
6915 n_known = isl_basic_map_first_unknown_div(bmap);
6916 if (n_known < 0)
6917 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6919 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6920 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6921 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6922 dim = isl_space_set_alloc(bmap->ctx,
6923 nparam + n_in + n_out + n_known, 0);
6924 if (!dim)
6925 goto error;
6927 ls = isl_basic_map_get_local_space(bmap);
6928 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6929 n_known, bmap->n_div - n_known);
6930 if (n_known > 0) {
6931 for (i = n_known; i < bmap->n_div; ++i)
6932 swap_div(bmap, i - n_known, i);
6933 bmap->n_div -= n_known;
6934 bmap->extra -= n_known;
6936 bmap = isl_basic_map_reset_space(bmap, dim);
6937 bset = (struct isl_basic_set *)bmap;
6939 set = parameter_compute_divs(bset);
6940 map = (struct isl_map *)set;
6941 map = replace_space_by_local_space(map, ls);
6943 return map;
6944 error:
6945 isl_basic_map_free(bmap);
6946 return NULL;
6949 /* Remove the explicit representation of local variable "div",
6950 * if there is any.
6952 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6953 __isl_take isl_basic_map *bmap, int div)
6955 isl_bool unknown;
6957 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6958 if (unknown < 0)
6959 return isl_basic_map_free(bmap);
6960 if (unknown)
6961 return bmap;
6963 bmap = isl_basic_map_cow(bmap);
6964 if (!bmap)
6965 return NULL;
6966 isl_int_set_si(bmap->div[div][0], 0);
6967 return bmap;
6970 /* Is local variable "div" of "bmap" marked as not having an explicit
6971 * representation?
6972 * Note that even if "div" is not marked in this way and therefore
6973 * has an explicit representation, this representation may still
6974 * depend (indirectly) on other local variables that do not
6975 * have an explicit representation.
6977 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6978 int div)
6980 if (!bmap)
6981 return isl_bool_error;
6982 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6983 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6984 "position out of bounds", return isl_bool_error);
6985 return isl_int_is_zero(bmap->div[div][0]);
6988 /* Return the position of the first local variable that does not
6989 * have an explicit representation.
6990 * Return the total number of local variables if they all have
6991 * an explicit representation.
6992 * Return -1 on error.
6994 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6996 int i;
6998 if (!bmap)
6999 return -1;
7001 for (i = 0; i < bmap->n_div; ++i) {
7002 if (!isl_basic_map_div_is_known(bmap, i))
7003 return i;
7005 return bmap->n_div;
7008 /* Return the position of the first local variable that does not
7009 * have an explicit representation.
7010 * Return the total number of local variables if they all have
7011 * an explicit representation.
7012 * Return -1 on error.
7014 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7016 return isl_basic_map_first_unknown_div(bset);
7019 /* Does "bmap" have an explicit representation for all local variables?
7021 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7023 int first, n;
7025 n = isl_basic_map_dim(bmap, isl_dim_div);
7026 first = isl_basic_map_first_unknown_div(bmap);
7027 if (first < 0)
7028 return isl_bool_error;
7029 return first == n;
7032 /* Do all basic maps in "map" have an explicit representation
7033 * for all local variables?
7035 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7037 int i;
7039 if (!map)
7040 return isl_bool_error;
7042 for (i = 0; i < map->n; ++i) {
7043 int known = isl_basic_map_divs_known(map->p[i]);
7044 if (known <= 0)
7045 return known;
7048 return isl_bool_true;
7051 /* If bmap contains any unknown divs, then compute explicit
7052 * expressions for them. However, this computation may be
7053 * quite expensive, so first try to remove divs that aren't
7054 * strictly needed.
7056 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7058 int known;
7059 struct isl_map *map;
7061 known = isl_basic_map_divs_known(bmap);
7062 if (known < 0)
7063 goto error;
7064 if (known)
7065 return isl_map_from_basic_map(bmap);
7067 bmap = isl_basic_map_drop_redundant_divs(bmap);
7069 known = isl_basic_map_divs_known(bmap);
7070 if (known < 0)
7071 goto error;
7072 if (known)
7073 return isl_map_from_basic_map(bmap);
7075 map = compute_divs(bmap);
7076 return map;
7077 error:
7078 isl_basic_map_free(bmap);
7079 return NULL;
7082 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7084 int i;
7085 int known;
7086 struct isl_map *res;
7088 if (!map)
7089 return NULL;
7090 if (map->n == 0)
7091 return map;
7093 known = isl_map_divs_known(map);
7094 if (known < 0) {
7095 isl_map_free(map);
7096 return NULL;
7098 if (known)
7099 return map;
7101 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7102 for (i = 1 ; i < map->n; ++i) {
7103 struct isl_map *r2;
7104 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7105 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7106 res = isl_map_union_disjoint(res, r2);
7107 else
7108 res = isl_map_union(res, r2);
7110 isl_map_free(map);
7112 return res;
7115 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7117 return (struct isl_set *)
7118 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7121 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7123 return (struct isl_set *)
7124 isl_map_compute_divs((struct isl_map *)set);
7127 struct isl_set *isl_map_domain(struct isl_map *map)
7129 int i;
7130 struct isl_set *set;
7132 if (!map)
7133 goto error;
7135 map = isl_map_cow(map);
7136 if (!map)
7137 return NULL;
7139 set = (struct isl_set *)map;
7140 set->dim = isl_space_domain(set->dim);
7141 if (!set->dim)
7142 goto error;
7143 for (i = 0; i < map->n; ++i) {
7144 set->p[i] = isl_basic_map_domain(map->p[i]);
7145 if (!set->p[i])
7146 goto error;
7148 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7149 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7150 return set;
7151 error:
7152 isl_map_free(map);
7153 return NULL;
7156 /* Return the union of "map1" and "map2", where we assume for now that
7157 * "map1" and "map2" are disjoint. Note that the basic maps inside
7158 * "map1" or "map2" may not be disjoint from each other.
7159 * Also note that this function is also called from isl_map_union,
7160 * which takes care of handling the situation where "map1" and "map2"
7161 * may not be disjoint.
7163 * If one of the inputs is empty, we can simply return the other input.
7164 * Similarly, if one of the inputs is universal, then it is equal to the union.
7166 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7167 __isl_take isl_map *map2)
7169 int i;
7170 unsigned flags = 0;
7171 struct isl_map *map = NULL;
7172 int is_universe;
7174 if (!map1 || !map2)
7175 goto error;
7177 if (!isl_space_is_equal(map1->dim, map2->dim))
7178 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7179 "spaces don't match", goto error);
7181 if (map1->n == 0) {
7182 isl_map_free(map1);
7183 return map2;
7185 if (map2->n == 0) {
7186 isl_map_free(map2);
7187 return map1;
7190 is_universe = isl_map_plain_is_universe(map1);
7191 if (is_universe < 0)
7192 goto error;
7193 if (is_universe) {
7194 isl_map_free(map2);
7195 return map1;
7198 is_universe = isl_map_plain_is_universe(map2);
7199 if (is_universe < 0)
7200 goto error;
7201 if (is_universe) {
7202 isl_map_free(map1);
7203 return map2;
7206 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7207 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7208 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7210 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7211 map1->n + map2->n, flags);
7212 if (!map)
7213 goto error;
7214 for (i = 0; i < map1->n; ++i) {
7215 map = isl_map_add_basic_map(map,
7216 isl_basic_map_copy(map1->p[i]));
7217 if (!map)
7218 goto error;
7220 for (i = 0; i < map2->n; ++i) {
7221 map = isl_map_add_basic_map(map,
7222 isl_basic_map_copy(map2->p[i]));
7223 if (!map)
7224 goto error;
7226 isl_map_free(map1);
7227 isl_map_free(map2);
7228 return map;
7229 error:
7230 isl_map_free(map);
7231 isl_map_free(map1);
7232 isl_map_free(map2);
7233 return NULL;
7236 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7237 * guaranteed to be disjoint by the caller.
7239 * Note that this functions is called from within isl_map_make_disjoint,
7240 * so we have to be careful not to touch the constraints of the inputs
7241 * in any way.
7243 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7244 __isl_take isl_map *map2)
7246 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7249 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7250 * not be disjoint. The parameters are assumed to have been aligned.
7252 * We currently simply call map_union_disjoint, the internal operation
7253 * of which does not really depend on the inputs being disjoint.
7254 * If the result contains more than one basic map, then we clear
7255 * the disjoint flag since the result may contain basic maps from
7256 * both inputs and these are not guaranteed to be disjoint.
7258 * As a special case, if "map1" and "map2" are obviously equal,
7259 * then we simply return "map1".
7261 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7262 __isl_take isl_map *map2)
7264 int equal;
7266 if (!map1 || !map2)
7267 goto error;
7269 equal = isl_map_plain_is_equal(map1, map2);
7270 if (equal < 0)
7271 goto error;
7272 if (equal) {
7273 isl_map_free(map2);
7274 return map1;
7277 map1 = map_union_disjoint(map1, map2);
7278 if (!map1)
7279 return NULL;
7280 if (map1->n > 1)
7281 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7282 return map1;
7283 error:
7284 isl_map_free(map1);
7285 isl_map_free(map2);
7286 return NULL;
7289 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7290 * not be disjoint.
7292 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7293 __isl_take isl_map *map2)
7295 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7298 struct isl_set *isl_set_union_disjoint(
7299 struct isl_set *set1, struct isl_set *set2)
7301 return (struct isl_set *)
7302 isl_map_union_disjoint(
7303 (struct isl_map *)set1, (struct isl_map *)set2);
7306 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7308 return (struct isl_set *)
7309 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7312 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7313 * the results.
7315 * "map" and "set" are assumed to be compatible and non-NULL.
7317 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7318 __isl_take isl_set *set,
7319 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7320 __isl_take isl_basic_set *bset))
7322 unsigned flags = 0;
7323 struct isl_map *result;
7324 int i, j;
7326 if (isl_set_plain_is_universe(set)) {
7327 isl_set_free(set);
7328 return map;
7331 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7332 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7333 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7335 result = isl_map_alloc_space(isl_space_copy(map->dim),
7336 map->n * set->n, flags);
7337 for (i = 0; result && i < map->n; ++i)
7338 for (j = 0; j < set->n; ++j) {
7339 result = isl_map_add_basic_map(result,
7340 fn(isl_basic_map_copy(map->p[i]),
7341 isl_basic_set_copy(set->p[j])));
7342 if (!result)
7343 break;
7346 isl_map_free(map);
7347 isl_set_free(set);
7348 return result;
7351 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7352 __isl_take isl_set *set)
7354 if (!map || !set)
7355 goto error;
7357 if (!isl_map_compatible_range(map, set))
7358 isl_die(set->ctx, isl_error_invalid,
7359 "incompatible spaces", goto error);
7361 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7362 error:
7363 isl_map_free(map);
7364 isl_set_free(set);
7365 return NULL;
7368 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7369 __isl_take isl_set *set)
7371 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7374 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7375 __isl_take isl_set *set)
7377 if (!map || !set)
7378 goto error;
7380 if (!isl_map_compatible_domain(map, set))
7381 isl_die(set->ctx, isl_error_invalid,
7382 "incompatible spaces", goto error);
7384 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7385 error:
7386 isl_map_free(map);
7387 isl_set_free(set);
7388 return NULL;
7391 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7392 __isl_take isl_set *set)
7394 return isl_map_align_params_map_map_and(map, set,
7395 &map_intersect_domain);
7398 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7399 __isl_take isl_map *map2)
7401 if (!map1 || !map2)
7402 goto error;
7403 map1 = isl_map_reverse(map1);
7404 map1 = isl_map_apply_range(map1, map2);
7405 return isl_map_reverse(map1);
7406 error:
7407 isl_map_free(map1);
7408 isl_map_free(map2);
7409 return NULL;
7412 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7413 __isl_take isl_map *map2)
7415 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7418 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7419 __isl_take isl_map *map2)
7421 isl_space *dim_result;
7422 struct isl_map *result;
7423 int i, j;
7425 if (!map1 || !map2)
7426 goto error;
7428 dim_result = isl_space_join(isl_space_copy(map1->dim),
7429 isl_space_copy(map2->dim));
7431 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7432 if (!result)
7433 goto error;
7434 for (i = 0; i < map1->n; ++i)
7435 for (j = 0; j < map2->n; ++j) {
7436 result = isl_map_add_basic_map(result,
7437 isl_basic_map_apply_range(
7438 isl_basic_map_copy(map1->p[i]),
7439 isl_basic_map_copy(map2->p[j])));
7440 if (!result)
7441 goto error;
7443 isl_map_free(map1);
7444 isl_map_free(map2);
7445 if (result && result->n <= 1)
7446 ISL_F_SET(result, ISL_MAP_DISJOINT);
7447 return result;
7448 error:
7449 isl_map_free(map1);
7450 isl_map_free(map2);
7451 return NULL;
7454 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7455 __isl_take isl_map *map2)
7457 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7461 * returns range - domain
7463 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7465 isl_space *target_space;
7466 struct isl_basic_set *bset;
7467 unsigned dim;
7468 unsigned nparam;
7469 int i;
7471 if (!bmap)
7472 goto error;
7473 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7474 bmap->dim, isl_dim_out),
7475 goto error);
7476 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7477 dim = isl_basic_map_n_in(bmap);
7478 nparam = isl_basic_map_n_param(bmap);
7479 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7480 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7481 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7482 for (i = 0; i < dim; ++i) {
7483 int j = isl_basic_map_alloc_equality(bmap);
7484 if (j < 0) {
7485 bmap = isl_basic_map_free(bmap);
7486 break;
7488 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7489 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7490 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7491 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7493 bset = isl_basic_map_domain(bmap);
7494 bset = isl_basic_set_reset_space(bset, target_space);
7495 return bset;
7496 error:
7497 isl_basic_map_free(bmap);
7498 return NULL;
7502 * returns range - domain
7504 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7506 int i;
7507 isl_space *dim;
7508 struct isl_set *result;
7510 if (!map)
7511 return NULL;
7513 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7514 map->dim, isl_dim_out),
7515 goto error);
7516 dim = isl_map_get_space(map);
7517 dim = isl_space_domain(dim);
7518 result = isl_set_alloc_space(dim, map->n, 0);
7519 if (!result)
7520 goto error;
7521 for (i = 0; i < map->n; ++i)
7522 result = isl_set_add_basic_set(result,
7523 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7524 isl_map_free(map);
7525 return result;
7526 error:
7527 isl_map_free(map);
7528 return NULL;
7532 * returns [domain -> range] -> range - domain
7534 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7535 __isl_take isl_basic_map *bmap)
7537 int i, k;
7538 isl_space *dim;
7539 isl_basic_map *domain;
7540 int nparam, n;
7541 unsigned total;
7543 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7544 bmap->dim, isl_dim_out))
7545 isl_die(bmap->ctx, isl_error_invalid,
7546 "domain and range don't match", goto error);
7548 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7549 n = isl_basic_map_dim(bmap, isl_dim_in);
7551 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7552 domain = isl_basic_map_universe(dim);
7554 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7555 bmap = isl_basic_map_apply_range(bmap, domain);
7556 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7558 total = isl_basic_map_total_dim(bmap);
7560 for (i = 0; i < n; ++i) {
7561 k = isl_basic_map_alloc_equality(bmap);
7562 if (k < 0)
7563 goto error;
7564 isl_seq_clr(bmap->eq[k], 1 + total);
7565 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7566 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7567 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7570 bmap = isl_basic_map_gauss(bmap, NULL);
7571 return isl_basic_map_finalize(bmap);
7572 error:
7573 isl_basic_map_free(bmap);
7574 return NULL;
7578 * returns [domain -> range] -> range - domain
7580 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7582 int i;
7583 isl_space *domain_dim;
7585 if (!map)
7586 return NULL;
7588 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7589 map->dim, isl_dim_out))
7590 isl_die(map->ctx, isl_error_invalid,
7591 "domain and range don't match", goto error);
7593 map = isl_map_cow(map);
7594 if (!map)
7595 return NULL;
7597 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7598 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7599 map->dim = isl_space_join(map->dim, domain_dim);
7600 if (!map->dim)
7601 goto error;
7602 for (i = 0; i < map->n; ++i) {
7603 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7604 if (!map->p[i])
7605 goto error;
7607 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7608 return map;
7609 error:
7610 isl_map_free(map);
7611 return NULL;
7614 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7616 struct isl_basic_map *bmap;
7617 unsigned nparam;
7618 unsigned dim;
7619 int i;
7621 if (!dims)
7622 return NULL;
7624 nparam = dims->nparam;
7625 dim = dims->n_out;
7626 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7627 if (!bmap)
7628 goto error;
7630 for (i = 0; i < dim; ++i) {
7631 int j = isl_basic_map_alloc_equality(bmap);
7632 if (j < 0)
7633 goto error;
7634 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7635 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7636 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7638 return isl_basic_map_finalize(bmap);
7639 error:
7640 isl_basic_map_free(bmap);
7641 return NULL;
7644 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7646 if (!dim)
7647 return NULL;
7648 if (dim->n_in != dim->n_out)
7649 isl_die(dim->ctx, isl_error_invalid,
7650 "number of input and output dimensions needs to be "
7651 "the same", goto error);
7652 return basic_map_identity(dim);
7653 error:
7654 isl_space_free(dim);
7655 return NULL;
7658 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7660 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7663 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7665 isl_space *dim = isl_set_get_space(set);
7666 isl_map *id;
7667 id = isl_map_identity(isl_space_map_from_set(dim));
7668 return isl_map_intersect_range(id, set);
7671 /* Construct a basic set with all set dimensions having only non-negative
7672 * values.
7674 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7675 __isl_take isl_space *space)
7677 int i;
7678 unsigned nparam;
7679 unsigned dim;
7680 struct isl_basic_set *bset;
7682 if (!space)
7683 return NULL;
7684 nparam = space->nparam;
7685 dim = space->n_out;
7686 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7687 if (!bset)
7688 return NULL;
7689 for (i = 0; i < dim; ++i) {
7690 int k = isl_basic_set_alloc_inequality(bset);
7691 if (k < 0)
7692 goto error;
7693 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7694 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7696 return bset;
7697 error:
7698 isl_basic_set_free(bset);
7699 return NULL;
7702 /* Construct the half-space x_pos >= 0.
7704 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7705 int pos)
7707 int k;
7708 isl_basic_set *nonneg;
7710 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7711 k = isl_basic_set_alloc_inequality(nonneg);
7712 if (k < 0)
7713 goto error;
7714 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7715 isl_int_set_si(nonneg->ineq[k][pos], 1);
7717 return isl_basic_set_finalize(nonneg);
7718 error:
7719 isl_basic_set_free(nonneg);
7720 return NULL;
7723 /* Construct the half-space x_pos <= -1.
7725 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7727 int k;
7728 isl_basic_set *neg;
7730 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7731 k = isl_basic_set_alloc_inequality(neg);
7732 if (k < 0)
7733 goto error;
7734 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7735 isl_int_set_si(neg->ineq[k][0], -1);
7736 isl_int_set_si(neg->ineq[k][pos], -1);
7738 return isl_basic_set_finalize(neg);
7739 error:
7740 isl_basic_set_free(neg);
7741 return NULL;
7744 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7745 enum isl_dim_type type, unsigned first, unsigned n)
7747 int i;
7748 unsigned offset;
7749 isl_basic_set *nonneg;
7750 isl_basic_set *neg;
7752 if (!set)
7753 return NULL;
7754 if (n == 0)
7755 return set;
7757 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7759 offset = pos(set->dim, type);
7760 for (i = 0; i < n; ++i) {
7761 nonneg = nonneg_halfspace(isl_set_get_space(set),
7762 offset + first + i);
7763 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7765 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7768 return set;
7769 error:
7770 isl_set_free(set);
7771 return NULL;
7774 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7775 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7776 void *user)
7778 isl_set *half;
7780 if (!set)
7781 return -1;
7782 if (isl_set_plain_is_empty(set)) {
7783 isl_set_free(set);
7784 return 0;
7786 if (first == len)
7787 return fn(set, signs, user);
7789 signs[first] = 1;
7790 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7791 1 + first));
7792 half = isl_set_intersect(half, isl_set_copy(set));
7793 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7794 goto error;
7796 signs[first] = -1;
7797 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7798 1 + first));
7799 half = isl_set_intersect(half, set);
7800 return foreach_orthant(half, signs, first + 1, len, fn, user);
7801 error:
7802 isl_set_free(set);
7803 return -1;
7806 /* Call "fn" on the intersections of "set" with each of the orthants
7807 * (except for obviously empty intersections). The orthant is identified
7808 * by the signs array, with each entry having value 1 or -1 according
7809 * to the sign of the corresponding variable.
7811 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7812 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7813 void *user)
7815 unsigned nparam;
7816 unsigned nvar;
7817 int *signs;
7818 int r;
7820 if (!set)
7821 return -1;
7822 if (isl_set_plain_is_empty(set))
7823 return 0;
7825 nparam = isl_set_dim(set, isl_dim_param);
7826 nvar = isl_set_dim(set, isl_dim_set);
7828 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7830 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7831 fn, user);
7833 free(signs);
7835 return r;
7838 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7840 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7843 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7844 __isl_keep isl_basic_map *bmap2)
7846 int is_subset;
7847 struct isl_map *map1;
7848 struct isl_map *map2;
7850 if (!bmap1 || !bmap2)
7851 return isl_bool_error;
7853 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7854 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7856 is_subset = isl_map_is_subset(map1, map2);
7858 isl_map_free(map1);
7859 isl_map_free(map2);
7861 return is_subset;
7864 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7865 __isl_keep isl_basic_set *bset2)
7867 return isl_basic_map_is_subset(bset1, bset2);
7870 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7871 __isl_keep isl_basic_map *bmap2)
7873 isl_bool is_subset;
7875 if (!bmap1 || !bmap2)
7876 return isl_bool_error;
7877 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7878 if (is_subset != isl_bool_true)
7879 return is_subset;
7880 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7881 return is_subset;
7884 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7885 __isl_keep isl_basic_set *bset2)
7887 return isl_basic_map_is_equal(
7888 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7891 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7893 int i;
7894 int is_empty;
7896 if (!map)
7897 return isl_bool_error;
7898 for (i = 0; i < map->n; ++i) {
7899 is_empty = isl_basic_map_is_empty(map->p[i]);
7900 if (is_empty < 0)
7901 return isl_bool_error;
7902 if (!is_empty)
7903 return isl_bool_false;
7905 return isl_bool_true;
7908 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7910 return map ? map->n == 0 : isl_bool_error;
7913 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7915 return set ? set->n == 0 : isl_bool_error;
7918 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7920 return isl_map_is_empty((struct isl_map *)set);
7923 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7925 if (!map1 || !map2)
7926 return -1;
7928 return isl_space_is_equal(map1->dim, map2->dim);
7931 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7933 if (!set1 || !set2)
7934 return -1;
7936 return isl_space_is_equal(set1->dim, set2->dim);
7939 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7941 isl_bool is_subset;
7943 if (!map1 || !map2)
7944 return isl_bool_error;
7945 is_subset = isl_map_is_subset(map1, map2);
7946 if (is_subset != isl_bool_true)
7947 return is_subset;
7948 is_subset = isl_map_is_subset(map2, map1);
7949 return is_subset;
7952 /* Is "map1" equal to "map2"?
7954 * First check if they are obviously equal.
7955 * If not, then perform a more detailed analysis.
7957 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7959 isl_bool equal;
7961 equal = isl_map_plain_is_equal(map1, map2);
7962 if (equal < 0 || equal)
7963 return equal;
7964 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7967 isl_bool isl_basic_map_is_strict_subset(
7968 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7970 isl_bool is_subset;
7972 if (!bmap1 || !bmap2)
7973 return isl_bool_error;
7974 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7975 if (is_subset != isl_bool_true)
7976 return is_subset;
7977 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7978 if (is_subset == isl_bool_error)
7979 return is_subset;
7980 return !is_subset;
7983 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7984 __isl_keep isl_map *map2)
7986 isl_bool is_subset;
7988 if (!map1 || !map2)
7989 return isl_bool_error;
7990 is_subset = isl_map_is_subset(map1, map2);
7991 if (is_subset != isl_bool_true)
7992 return is_subset;
7993 is_subset = isl_map_is_subset(map2, map1);
7994 if (is_subset == isl_bool_error)
7995 return is_subset;
7996 return !is_subset;
7999 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8000 __isl_keep isl_set *set2)
8002 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8005 /* Is "bmap" obviously equal to the universe with the same space?
8007 * That is, does it not have any constraints?
8009 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8011 if (!bmap)
8012 return isl_bool_error;
8013 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8016 /* Is "bset" obviously equal to the universe with the same space?
8018 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8020 return isl_basic_map_plain_is_universe(bset);
8023 /* If "c" does not involve any existentially quantified variables,
8024 * then set *univ to false and abort
8026 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8028 isl_bool *univ = user;
8029 unsigned n;
8031 n = isl_constraint_dim(c, isl_dim_div);
8032 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8033 isl_constraint_free(c);
8034 if (*univ < 0 || !*univ)
8035 return isl_stat_error;
8036 return isl_stat_ok;
8039 /* Is "bmap" equal to the universe with the same space?
8041 * First check if it is obviously equal to the universe.
8042 * If not and if there are any constraints not involving
8043 * existentially quantified variables, then it is certainly
8044 * not equal to the universe.
8045 * Otherwise, check if the universe is a subset of "bmap".
8047 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8049 isl_bool univ;
8050 isl_basic_map *test;
8052 univ = isl_basic_map_plain_is_universe(bmap);
8053 if (univ < 0 || univ)
8054 return univ;
8055 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8056 return isl_bool_false;
8057 univ = isl_bool_true;
8058 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8059 univ)
8060 return isl_bool_error;
8061 if (univ < 0 || !univ)
8062 return univ;
8063 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8064 univ = isl_basic_map_is_subset(test, bmap);
8065 isl_basic_map_free(test);
8066 return univ;
8069 /* Is "bset" equal to the universe with the same space?
8071 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8073 return isl_basic_map_is_universe(bset);
8076 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8078 int i;
8080 if (!map)
8081 return isl_bool_error;
8083 for (i = 0; i < map->n; ++i) {
8084 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8085 if (r < 0 || r)
8086 return r;
8089 return isl_bool_false;
8092 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8094 return isl_map_plain_is_universe((isl_map *) set);
8097 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8099 struct isl_basic_set *bset = NULL;
8100 struct isl_vec *sample = NULL;
8101 isl_bool empty, non_empty;
8103 if (!bmap)
8104 return isl_bool_error;
8106 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8107 return isl_bool_true;
8109 if (isl_basic_map_plain_is_universe(bmap))
8110 return isl_bool_false;
8112 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8113 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8114 copy = isl_basic_map_remove_redundancies(copy);
8115 empty = isl_basic_map_plain_is_empty(copy);
8116 isl_basic_map_free(copy);
8117 return empty;
8120 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8121 if (non_empty < 0)
8122 return isl_bool_error;
8123 if (non_empty)
8124 return isl_bool_false;
8125 isl_vec_free(bmap->sample);
8126 bmap->sample = NULL;
8127 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8128 if (!bset)
8129 return isl_bool_error;
8130 sample = isl_basic_set_sample_vec(bset);
8131 if (!sample)
8132 return isl_bool_error;
8133 empty = sample->size == 0;
8134 isl_vec_free(bmap->sample);
8135 bmap->sample = sample;
8136 if (empty)
8137 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8139 return empty;
8142 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8144 if (!bmap)
8145 return isl_bool_error;
8146 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8149 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8151 if (!bset)
8152 return isl_bool_error;
8153 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8156 /* Is "bmap" known to be non-empty?
8158 * That is, is the cached sample still valid?
8160 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8162 unsigned total;
8164 if (!bmap)
8165 return isl_bool_error;
8166 if (!bmap->sample)
8167 return isl_bool_false;
8168 total = 1 + isl_basic_map_total_dim(bmap);
8169 if (bmap->sample->size != total)
8170 return isl_bool_false;
8171 return isl_basic_map_contains(bmap, bmap->sample);
8174 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8176 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8179 struct isl_map *isl_basic_map_union(
8180 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8182 struct isl_map *map;
8183 if (!bmap1 || !bmap2)
8184 goto error;
8186 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8188 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8189 if (!map)
8190 goto error;
8191 map = isl_map_add_basic_map(map, bmap1);
8192 map = isl_map_add_basic_map(map, bmap2);
8193 return map;
8194 error:
8195 isl_basic_map_free(bmap1);
8196 isl_basic_map_free(bmap2);
8197 return NULL;
8200 struct isl_set *isl_basic_set_union(
8201 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8203 return (struct isl_set *)isl_basic_map_union(
8204 (struct isl_basic_map *)bset1,
8205 (struct isl_basic_map *)bset2);
8208 /* Order divs such that any div only depends on previous divs */
8209 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8211 int i;
8212 unsigned off;
8214 if (!bmap)
8215 return NULL;
8217 off = isl_space_dim(bmap->dim, isl_dim_all);
8219 for (i = 0; i < bmap->n_div; ++i) {
8220 int pos;
8221 if (isl_int_is_zero(bmap->div[i][0]))
8222 continue;
8223 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8224 bmap->n_div-i);
8225 if (pos == -1)
8226 continue;
8227 if (pos == 0)
8228 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8229 "integer division depends on itself",
8230 return isl_basic_map_free(bmap));
8231 isl_basic_map_swap_div(bmap, i, i + pos);
8232 --i;
8234 return bmap;
8237 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8239 return (struct isl_basic_set *)
8240 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8243 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8245 int i;
8247 if (!map)
8248 return 0;
8250 for (i = 0; i < map->n; ++i) {
8251 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8252 if (!map->p[i])
8253 goto error;
8256 return map;
8257 error:
8258 isl_map_free(map);
8259 return NULL;
8262 /* Apply the expansion computed by isl_merge_divs.
8263 * The expansion itself is given by "exp" while the resulting
8264 * list of divs is given by "div".
8266 * Move the integer divisions of "bmap" into the right position
8267 * according to "exp" and then introduce the additional integer
8268 * divisions, adding div constraints.
8269 * The moving should be done first to avoid moving coefficients
8270 * in the definitions of the extra integer divisions.
8272 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8273 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8275 int i, j;
8276 int n_div;
8278 bmap = isl_basic_map_cow(bmap);
8279 if (!bmap || !div)
8280 goto error;
8282 if (div->n_row < bmap->n_div)
8283 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8284 "not an expansion", goto error);
8286 n_div = bmap->n_div;
8287 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8288 div->n_row - n_div, 0,
8289 2 * (div->n_row - n_div));
8291 for (i = n_div; i < div->n_row; ++i)
8292 if (isl_basic_map_alloc_div(bmap) < 0)
8293 goto error;
8295 for (j = n_div - 1; j >= 0; --j) {
8296 if (exp[j] == j)
8297 break;
8298 isl_basic_map_swap_div(bmap, j, exp[j]);
8300 j = 0;
8301 for (i = 0; i < div->n_row; ++i) {
8302 if (j < n_div && exp[j] == i) {
8303 j++;
8304 } else {
8305 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8306 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8307 continue;
8308 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8309 goto error;
8313 isl_mat_free(div);
8314 return bmap;
8315 error:
8316 isl_basic_map_free(bmap);
8317 isl_mat_free(div);
8318 return NULL;
8321 /* Apply the expansion computed by isl_merge_divs.
8322 * The expansion itself is given by "exp" while the resulting
8323 * list of divs is given by "div".
8325 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8326 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8328 return isl_basic_map_expand_divs(bset, div, exp);
8331 /* Look for a div in dst that corresponds to the div "div" in src.
8332 * The divs before "div" in src and dst are assumed to be the same.
8334 * Returns -1 if no corresponding div was found and the position
8335 * of the corresponding div in dst otherwise.
8337 static int find_div(struct isl_basic_map *dst,
8338 struct isl_basic_map *src, unsigned div)
8340 int i;
8342 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8344 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8345 for (i = div; i < dst->n_div; ++i)
8346 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8347 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8348 dst->n_div - div) == -1)
8349 return i;
8350 return -1;
8353 /* Align the divs of "dst" to those of "src", adding divs from "src"
8354 * if needed. That is, make sure that the first src->n_div divs
8355 * of the result are equal to those of src.
8357 * The result is not finalized as by design it will have redundant
8358 * divs if any divs from "src" were copied.
8360 __isl_give isl_basic_map *isl_basic_map_align_divs(
8361 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8363 int i;
8364 int known, extended;
8365 unsigned total;
8367 if (!dst || !src)
8368 return isl_basic_map_free(dst);
8370 if (src->n_div == 0)
8371 return dst;
8373 known = isl_basic_map_divs_known(src);
8374 if (known < 0)
8375 return isl_basic_map_free(dst);
8376 if (!known)
8377 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8378 "some src divs are unknown",
8379 return isl_basic_map_free(dst));
8381 src = isl_basic_map_order_divs(src);
8383 extended = 0;
8384 total = isl_space_dim(src->dim, isl_dim_all);
8385 for (i = 0; i < src->n_div; ++i) {
8386 int j = find_div(dst, src, i);
8387 if (j < 0) {
8388 if (!extended) {
8389 int extra = src->n_div - i;
8390 dst = isl_basic_map_cow(dst);
8391 if (!dst)
8392 return NULL;
8393 dst = isl_basic_map_extend_space(dst,
8394 isl_space_copy(dst->dim),
8395 extra, 0, 2 * extra);
8396 extended = 1;
8398 j = isl_basic_map_alloc_div(dst);
8399 if (j < 0)
8400 return isl_basic_map_free(dst);
8401 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8402 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8403 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8404 return isl_basic_map_free(dst);
8406 if (j != i)
8407 isl_basic_map_swap_div(dst, i, j);
8409 return dst;
8412 struct isl_basic_set *isl_basic_set_align_divs(
8413 struct isl_basic_set *dst, struct isl_basic_set *src)
8415 return (struct isl_basic_set *)isl_basic_map_align_divs(
8416 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8419 struct isl_map *isl_map_align_divs(struct isl_map *map)
8421 int i;
8423 if (!map)
8424 return NULL;
8425 if (map->n == 0)
8426 return map;
8427 map = isl_map_compute_divs(map);
8428 map = isl_map_cow(map);
8429 if (!map)
8430 return NULL;
8432 for (i = 1; i < map->n; ++i)
8433 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8434 for (i = 1; i < map->n; ++i) {
8435 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8436 if (!map->p[i])
8437 return isl_map_free(map);
8440 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8441 return map;
8444 struct isl_set *isl_set_align_divs(struct isl_set *set)
8446 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8449 /* Align the divs of the basic maps in "map" to those
8450 * of the basic maps in "list", as well as to the other basic maps in "map".
8451 * The elements in "list" are assumed to have known divs.
8453 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8454 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8456 int i, n;
8458 map = isl_map_compute_divs(map);
8459 map = isl_map_cow(map);
8460 if (!map || !list)
8461 return isl_map_free(map);
8462 if (map->n == 0)
8463 return map;
8465 n = isl_basic_map_list_n_basic_map(list);
8466 for (i = 0; i < n; ++i) {
8467 isl_basic_map *bmap;
8469 bmap = isl_basic_map_list_get_basic_map(list, i);
8470 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8471 isl_basic_map_free(bmap);
8473 if (!map->p[0])
8474 return isl_map_free(map);
8476 return isl_map_align_divs(map);
8479 /* Align the divs of each element of "list" to those of "bmap".
8480 * Both "bmap" and the elements of "list" are assumed to have known divs.
8482 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8483 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8485 int i, n;
8487 if (!list || !bmap)
8488 return isl_basic_map_list_free(list);
8490 n = isl_basic_map_list_n_basic_map(list);
8491 for (i = 0; i < n; ++i) {
8492 isl_basic_map *bmap_i;
8494 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8495 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8496 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8499 return list;
8502 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8503 __isl_take isl_map *map)
8505 if (!set || !map)
8506 goto error;
8507 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8508 map = isl_map_intersect_domain(map, set);
8509 set = isl_map_range(map);
8510 return set;
8511 error:
8512 isl_set_free(set);
8513 isl_map_free(map);
8514 return NULL;
8517 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8518 __isl_take isl_map *map)
8520 return isl_map_align_params_map_map_and(set, map, &set_apply);
8523 /* There is no need to cow as removing empty parts doesn't change
8524 * the meaning of the set.
8526 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8528 int i;
8530 if (!map)
8531 return NULL;
8533 for (i = map->n - 1; i >= 0; --i)
8534 remove_if_empty(map, i);
8536 return map;
8539 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8541 return (struct isl_set *)
8542 isl_map_remove_empty_parts((struct isl_map *)set);
8545 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8547 struct isl_basic_map *bmap;
8548 if (!map || map->n == 0)
8549 return NULL;
8550 bmap = map->p[map->n-1];
8551 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8552 return isl_basic_map_copy(bmap);
8555 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8557 return (struct isl_basic_set *)
8558 isl_map_copy_basic_map((struct isl_map *)set);
8561 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8562 __isl_keep isl_basic_map *bmap)
8564 int i;
8566 if (!map || !bmap)
8567 goto error;
8568 for (i = map->n-1; i >= 0; --i) {
8569 if (map->p[i] != bmap)
8570 continue;
8571 map = isl_map_cow(map);
8572 if (!map)
8573 goto error;
8574 isl_basic_map_free(map->p[i]);
8575 if (i != map->n-1) {
8576 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8577 map->p[i] = map->p[map->n-1];
8579 map->n--;
8580 return map;
8582 return map;
8583 error:
8584 isl_map_free(map);
8585 return NULL;
8588 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8589 struct isl_basic_set *bset)
8591 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8592 (struct isl_basic_map *)bset);
8595 /* Given two basic sets bset1 and bset2, compute the maximal difference
8596 * between the values of dimension pos in bset1 and those in bset2
8597 * for any common value of the parameters and dimensions preceding pos.
8599 static enum isl_lp_result basic_set_maximal_difference_at(
8600 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8601 int pos, isl_int *opt)
8603 isl_space *dims;
8604 struct isl_basic_map *bmap1 = NULL;
8605 struct isl_basic_map *bmap2 = NULL;
8606 struct isl_ctx *ctx;
8607 struct isl_vec *obj;
8608 unsigned total;
8609 unsigned nparam;
8610 unsigned dim1, dim2;
8611 enum isl_lp_result res;
8613 if (!bset1 || !bset2)
8614 return isl_lp_error;
8616 nparam = isl_basic_set_n_param(bset1);
8617 dim1 = isl_basic_set_n_dim(bset1);
8618 dim2 = isl_basic_set_n_dim(bset2);
8619 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8620 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8621 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8622 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8623 if (!bmap1 || !bmap2)
8624 goto error;
8625 bmap1 = isl_basic_map_cow(bmap1);
8626 bmap1 = isl_basic_map_extend(bmap1, nparam,
8627 pos, (dim1 - pos) + (dim2 - pos),
8628 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8629 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8630 if (!bmap1)
8631 goto error2;
8632 total = isl_basic_map_total_dim(bmap1);
8633 ctx = bmap1->ctx;
8634 obj = isl_vec_alloc(ctx, 1 + total);
8635 if (!obj)
8636 goto error2;
8637 isl_seq_clr(obj->block.data, 1 + total);
8638 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8639 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8640 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8641 opt, NULL, NULL);
8642 isl_basic_map_free(bmap1);
8643 isl_vec_free(obj);
8644 return res;
8645 error:
8646 isl_basic_map_free(bmap2);
8647 error2:
8648 isl_basic_map_free(bmap1);
8649 return isl_lp_error;
8652 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8653 * for any common value of the parameters and dimensions preceding pos
8654 * in both basic sets, the values of dimension pos in bset1 are
8655 * smaller or larger than those in bset2.
8657 * Returns
8658 * 1 if bset1 follows bset2
8659 * -1 if bset1 precedes bset2
8660 * 0 if bset1 and bset2 are incomparable
8661 * -2 if some error occurred.
8663 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8664 struct isl_basic_set *bset2, int pos)
8666 isl_int opt;
8667 enum isl_lp_result res;
8668 int cmp;
8670 isl_int_init(opt);
8672 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8674 if (res == isl_lp_empty)
8675 cmp = 0;
8676 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8677 res == isl_lp_unbounded)
8678 cmp = 1;
8679 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8680 cmp = -1;
8681 else
8682 cmp = -2;
8684 isl_int_clear(opt);
8685 return cmp;
8688 /* Given two basic sets bset1 and bset2, check whether
8689 * for any common value of the parameters and dimensions preceding pos
8690 * there is a value of dimension pos in bset1 that is larger
8691 * than a value of the same dimension in bset2.
8693 * Return
8694 * 1 if there exists such a pair
8695 * 0 if there is no such pair, but there is a pair of equal values
8696 * -1 otherwise
8697 * -2 if some error occurred.
8699 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8700 __isl_keep isl_basic_set *bset2, int pos)
8702 isl_int opt;
8703 enum isl_lp_result res;
8704 int cmp;
8706 isl_int_init(opt);
8708 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8710 if (res == isl_lp_empty)
8711 cmp = -1;
8712 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8713 res == isl_lp_unbounded)
8714 cmp = 1;
8715 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8716 cmp = -1;
8717 else if (res == isl_lp_ok)
8718 cmp = 0;
8719 else
8720 cmp = -2;
8722 isl_int_clear(opt);
8723 return cmp;
8726 /* Given two sets set1 and set2, check whether
8727 * for any common value of the parameters and dimensions preceding pos
8728 * there is a value of dimension pos in set1 that is larger
8729 * than a value of the same dimension in set2.
8731 * Return
8732 * 1 if there exists such a pair
8733 * 0 if there is no such pair, but there is a pair of equal values
8734 * -1 otherwise
8735 * -2 if some error occurred.
8737 int isl_set_follows_at(__isl_keep isl_set *set1,
8738 __isl_keep isl_set *set2, int pos)
8740 int i, j;
8741 int follows = -1;
8743 if (!set1 || !set2)
8744 return -2;
8746 for (i = 0; i < set1->n; ++i)
8747 for (j = 0; j < set2->n; ++j) {
8748 int f;
8749 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8750 if (f == 1 || f == -2)
8751 return f;
8752 if (f > follows)
8753 follows = f;
8756 return follows;
8759 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8760 unsigned pos, isl_int *val)
8762 int i;
8763 int d;
8764 unsigned total;
8766 if (!bmap)
8767 return -1;
8768 total = isl_basic_map_total_dim(bmap);
8769 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8770 for (; d+1 > pos; --d)
8771 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8772 break;
8773 if (d != pos)
8774 continue;
8775 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8776 return 0;
8777 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8778 return 0;
8779 if (!isl_int_is_one(bmap->eq[i][1+d]))
8780 return 0;
8781 if (val)
8782 isl_int_neg(*val, bmap->eq[i][0]);
8783 return 1;
8785 return 0;
8788 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8789 unsigned pos, isl_int *val)
8791 int i;
8792 isl_int v;
8793 isl_int tmp;
8794 int fixed;
8796 if (!map)
8797 return -1;
8798 if (map->n == 0)
8799 return 0;
8800 if (map->n == 1)
8801 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8802 isl_int_init(v);
8803 isl_int_init(tmp);
8804 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8805 for (i = 1; fixed == 1 && i < map->n; ++i) {
8806 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8807 if (fixed == 1 && isl_int_ne(tmp, v))
8808 fixed = 0;
8810 if (val)
8811 isl_int_set(*val, v);
8812 isl_int_clear(tmp);
8813 isl_int_clear(v);
8814 return fixed;
8817 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8818 unsigned pos, isl_int *val)
8820 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8821 pos, val);
8824 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8825 isl_int *val)
8827 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8830 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8831 enum isl_dim_type type, unsigned pos, isl_int *val)
8833 if (pos >= isl_basic_map_dim(bmap, type))
8834 return -1;
8835 return isl_basic_map_plain_has_fixed_var(bmap,
8836 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8839 /* If "bmap" obviously lies on a hyperplane where the given dimension
8840 * has a fixed value, then return that value.
8841 * Otherwise return NaN.
8843 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8844 __isl_keep isl_basic_map *bmap,
8845 enum isl_dim_type type, unsigned pos)
8847 isl_ctx *ctx;
8848 isl_val *v;
8849 int fixed;
8851 if (!bmap)
8852 return NULL;
8853 ctx = isl_basic_map_get_ctx(bmap);
8854 v = isl_val_alloc(ctx);
8855 if (!v)
8856 return NULL;
8857 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8858 if (fixed < 0)
8859 return isl_val_free(v);
8860 if (fixed) {
8861 isl_int_set_si(v->d, 1);
8862 return v;
8864 isl_val_free(v);
8865 return isl_val_nan(ctx);
8868 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8869 enum isl_dim_type type, unsigned pos, isl_int *val)
8871 if (pos >= isl_map_dim(map, type))
8872 return -1;
8873 return isl_map_plain_has_fixed_var(map,
8874 map_offset(map, type) - 1 + pos, val);
8877 /* If "map" obviously lies on a hyperplane where the given dimension
8878 * has a fixed value, then return that value.
8879 * Otherwise return NaN.
8881 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8882 enum isl_dim_type type, unsigned pos)
8884 isl_ctx *ctx;
8885 isl_val *v;
8886 int fixed;
8888 if (!map)
8889 return NULL;
8890 ctx = isl_map_get_ctx(map);
8891 v = isl_val_alloc(ctx);
8892 if (!v)
8893 return NULL;
8894 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8895 if (fixed < 0)
8896 return isl_val_free(v);
8897 if (fixed) {
8898 isl_int_set_si(v->d, 1);
8899 return v;
8901 isl_val_free(v);
8902 return isl_val_nan(ctx);
8905 /* If "set" obviously lies on a hyperplane where the given dimension
8906 * has a fixed value, then return that value.
8907 * Otherwise return NaN.
8909 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8910 enum isl_dim_type type, unsigned pos)
8912 return isl_map_plain_get_val_if_fixed(set, type, pos);
8915 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8916 enum isl_dim_type type, unsigned pos, isl_int *val)
8918 return isl_map_plain_is_fixed(set, type, pos, val);
8921 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8922 * then return this fixed value in *val.
8924 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8925 unsigned dim, isl_int *val)
8927 return isl_basic_set_plain_has_fixed_var(bset,
8928 isl_basic_set_n_param(bset) + dim, val);
8931 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8932 * then return this fixed value in *val.
8934 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8935 unsigned dim, isl_int *val)
8937 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8940 /* Check if input variable in has fixed value and if so and if val is not NULL,
8941 * then return this fixed value in *val.
8943 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8944 unsigned in, isl_int *val)
8946 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8949 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8950 * and if val is not NULL, then return this lower bound in *val.
8952 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8953 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8955 int i, i_eq = -1, i_ineq = -1;
8956 isl_int *c;
8957 unsigned total;
8958 unsigned nparam;
8960 if (!bset)
8961 return -1;
8962 total = isl_basic_set_total_dim(bset);
8963 nparam = isl_basic_set_n_param(bset);
8964 for (i = 0; i < bset->n_eq; ++i) {
8965 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8966 continue;
8967 if (i_eq != -1)
8968 return 0;
8969 i_eq = i;
8971 for (i = 0; i < bset->n_ineq; ++i) {
8972 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8973 continue;
8974 if (i_eq != -1 || i_ineq != -1)
8975 return 0;
8976 i_ineq = i;
8978 if (i_eq == -1 && i_ineq == -1)
8979 return 0;
8980 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8981 /* The coefficient should always be one due to normalization. */
8982 if (!isl_int_is_one(c[1+nparam+dim]))
8983 return 0;
8984 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8985 return 0;
8986 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8987 total - nparam - dim - 1) != -1)
8988 return 0;
8989 if (val)
8990 isl_int_neg(*val, c[0]);
8991 return 1;
8994 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8995 unsigned dim, isl_int *val)
8997 int i;
8998 isl_int v;
8999 isl_int tmp;
9000 int fixed;
9002 if (!set)
9003 return -1;
9004 if (set->n == 0)
9005 return 0;
9006 if (set->n == 1)
9007 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9008 dim, val);
9009 isl_int_init(v);
9010 isl_int_init(tmp);
9011 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9012 dim, &v);
9013 for (i = 1; fixed == 1 && i < set->n; ++i) {
9014 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9015 dim, &tmp);
9016 if (fixed == 1 && isl_int_ne(tmp, v))
9017 fixed = 0;
9019 if (val)
9020 isl_int_set(*val, v);
9021 isl_int_clear(tmp);
9022 isl_int_clear(v);
9023 return fixed;
9026 /* Return -1 if the constraint "c1" should be sorted before "c2"
9027 * and 1 if it should be sorted after "c2".
9028 * Return 0 if the two constraints are the same (up to the constant term).
9030 * In particular, if a constraint involves later variables than another
9031 * then it is sorted after this other constraint.
9032 * uset_gist depends on constraints without existentially quantified
9033 * variables sorting first.
9035 * For constraints that have the same latest variable, those
9036 * with the same coefficient for this latest variable (first in absolute value
9037 * and then in actual value) are grouped together.
9038 * This is useful for detecting pairs of constraints that can
9039 * be chained in their printed representation.
9041 * Finally, within a group, constraints are sorted according to
9042 * their coefficients (excluding the constant term).
9044 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9046 isl_int **c1 = (isl_int **) p1;
9047 isl_int **c2 = (isl_int **) p2;
9048 int l1, l2;
9049 unsigned size = *(unsigned *) arg;
9050 int cmp;
9052 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9053 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9055 if (l1 != l2)
9056 return l1 - l2;
9058 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9059 if (cmp != 0)
9060 return cmp;
9061 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9062 if (cmp != 0)
9063 return -cmp;
9065 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9068 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9069 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9070 * and 0 if the two constraints are the same (up to the constant term).
9072 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9073 isl_int *c1, isl_int *c2)
9075 unsigned total;
9077 if (!bmap)
9078 return -2;
9079 total = isl_basic_map_total_dim(bmap);
9080 return sort_constraint_cmp(&c1, &c2, &total);
9083 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9084 __isl_take isl_basic_map *bmap)
9086 unsigned total;
9088 if (!bmap)
9089 return NULL;
9090 if (bmap->n_ineq == 0)
9091 return bmap;
9092 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9093 return bmap;
9094 total = isl_basic_map_total_dim(bmap);
9095 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9096 &sort_constraint_cmp, &total) < 0)
9097 return isl_basic_map_free(bmap);
9098 return bmap;
9101 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9102 __isl_take isl_basic_set *bset)
9104 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9105 (struct isl_basic_map *)bset);
9108 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9110 if (!bmap)
9111 return NULL;
9112 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9113 return bmap;
9114 bmap = isl_basic_map_remove_redundancies(bmap);
9115 bmap = isl_basic_map_sort_constraints(bmap);
9116 if (bmap)
9117 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9118 return bmap;
9121 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9123 return (struct isl_basic_set *)isl_basic_map_normalize(
9124 (struct isl_basic_map *)bset);
9127 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9128 const __isl_keep isl_basic_map *bmap2)
9130 int i, cmp;
9131 unsigned total;
9133 if (!bmap1 || !bmap2)
9134 return -1;
9136 if (bmap1 == bmap2)
9137 return 0;
9138 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9139 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9140 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9141 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9142 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9143 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9144 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9145 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9146 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9147 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9148 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9149 return 0;
9150 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9151 return 1;
9152 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9153 return -1;
9154 if (bmap1->n_eq != bmap2->n_eq)
9155 return bmap1->n_eq - bmap2->n_eq;
9156 if (bmap1->n_ineq != bmap2->n_ineq)
9157 return bmap1->n_ineq - bmap2->n_ineq;
9158 if (bmap1->n_div != bmap2->n_div)
9159 return bmap1->n_div - bmap2->n_div;
9160 total = isl_basic_map_total_dim(bmap1);
9161 for (i = 0; i < bmap1->n_eq; ++i) {
9162 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9163 if (cmp)
9164 return cmp;
9166 for (i = 0; i < bmap1->n_ineq; ++i) {
9167 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9168 if (cmp)
9169 return cmp;
9171 for (i = 0; i < bmap1->n_div; ++i) {
9172 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9173 if (cmp)
9174 return cmp;
9176 return 0;
9179 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9180 const __isl_keep isl_basic_set *bset2)
9182 return isl_basic_map_plain_cmp(bset1, bset2);
9185 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9187 int i, cmp;
9189 if (set1 == set2)
9190 return 0;
9191 if (set1->n != set2->n)
9192 return set1->n - set2->n;
9194 for (i = 0; i < set1->n; ++i) {
9195 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9196 if (cmp)
9197 return cmp;
9200 return 0;
9203 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9204 __isl_keep isl_basic_map *bmap2)
9206 if (!bmap1 || !bmap2)
9207 return isl_bool_error;
9208 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9211 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9212 __isl_keep isl_basic_set *bset2)
9214 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9215 (isl_basic_map *)bset2);
9218 static int qsort_bmap_cmp(const void *p1, const void *p2)
9220 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9221 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9223 return isl_basic_map_plain_cmp(bmap1, bmap2);
9226 /* Sort the basic maps of "map" and remove duplicate basic maps.
9228 * While removing basic maps, we make sure that the basic maps remain
9229 * sorted because isl_map_normalize expects the basic maps of the result
9230 * to be sorted.
9232 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9234 int i, j;
9236 map = isl_map_remove_empty_parts(map);
9237 if (!map)
9238 return NULL;
9239 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9240 for (i = map->n - 1; i >= 1; --i) {
9241 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9242 continue;
9243 isl_basic_map_free(map->p[i-1]);
9244 for (j = i; j < map->n; ++j)
9245 map->p[j - 1] = map->p[j];
9246 map->n--;
9249 return map;
9252 /* Remove obvious duplicates among the basic maps of "map".
9254 * Unlike isl_map_normalize, this function does not remove redundant
9255 * constraints and only removes duplicates that have exactly the same
9256 * constraints in the input. It does sort the constraints and
9257 * the basic maps to ease the detection of duplicates.
9259 * If "map" has already been normalized or if the basic maps are
9260 * disjoint, then there can be no duplicates.
9262 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9264 int i;
9265 isl_basic_map *bmap;
9267 if (!map)
9268 return NULL;
9269 if (map->n <= 1)
9270 return map;
9271 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9272 return map;
9273 for (i = 0; i < map->n; ++i) {
9274 bmap = isl_basic_map_copy(map->p[i]);
9275 bmap = isl_basic_map_sort_constraints(bmap);
9276 if (!bmap)
9277 return isl_map_free(map);
9278 isl_basic_map_free(map->p[i]);
9279 map->p[i] = bmap;
9282 map = sort_and_remove_duplicates(map);
9283 return map;
9286 /* We normalize in place, but if anything goes wrong we need
9287 * to return NULL, so we need to make sure we don't change the
9288 * meaning of any possible other copies of map.
9290 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9292 int i;
9293 struct isl_basic_map *bmap;
9295 if (!map)
9296 return NULL;
9297 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9298 return map;
9299 for (i = 0; i < map->n; ++i) {
9300 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9301 if (!bmap)
9302 goto error;
9303 isl_basic_map_free(map->p[i]);
9304 map->p[i] = bmap;
9307 map = sort_and_remove_duplicates(map);
9308 if (map)
9309 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9310 return map;
9311 error:
9312 isl_map_free(map);
9313 return NULL;
9316 struct isl_set *isl_set_normalize(struct isl_set *set)
9318 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9321 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9322 __isl_keep isl_map *map2)
9324 int i;
9325 isl_bool equal;
9327 if (!map1 || !map2)
9328 return isl_bool_error;
9330 if (map1 == map2)
9331 return isl_bool_true;
9332 if (!isl_space_is_equal(map1->dim, map2->dim))
9333 return isl_bool_false;
9335 map1 = isl_map_copy(map1);
9336 map2 = isl_map_copy(map2);
9337 map1 = isl_map_normalize(map1);
9338 map2 = isl_map_normalize(map2);
9339 if (!map1 || !map2)
9340 goto error;
9341 equal = map1->n == map2->n;
9342 for (i = 0; equal && i < map1->n; ++i) {
9343 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9344 if (equal < 0)
9345 goto error;
9347 isl_map_free(map1);
9348 isl_map_free(map2);
9349 return equal;
9350 error:
9351 isl_map_free(map1);
9352 isl_map_free(map2);
9353 return isl_bool_error;
9356 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9357 __isl_keep isl_set *set2)
9359 return isl_map_plain_is_equal((struct isl_map *)set1,
9360 (struct isl_map *)set2);
9363 /* Return an interval that ranges from min to max (inclusive)
9365 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9366 isl_int min, isl_int max)
9368 int k;
9369 struct isl_basic_set *bset = NULL;
9371 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9372 if (!bset)
9373 goto error;
9375 k = isl_basic_set_alloc_inequality(bset);
9376 if (k < 0)
9377 goto error;
9378 isl_int_set_si(bset->ineq[k][1], 1);
9379 isl_int_neg(bset->ineq[k][0], min);
9381 k = isl_basic_set_alloc_inequality(bset);
9382 if (k < 0)
9383 goto error;
9384 isl_int_set_si(bset->ineq[k][1], -1);
9385 isl_int_set(bset->ineq[k][0], max);
9387 return bset;
9388 error:
9389 isl_basic_set_free(bset);
9390 return NULL;
9393 /* Return the basic maps in "map" as a list.
9395 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9396 __isl_keep isl_map *map)
9398 int i;
9399 isl_ctx *ctx;
9400 isl_basic_map_list *list;
9402 if (!map)
9403 return NULL;
9404 ctx = isl_map_get_ctx(map);
9405 list = isl_basic_map_list_alloc(ctx, map->n);
9407 for (i = 0; i < map->n; ++i) {
9408 isl_basic_map *bmap;
9410 bmap = isl_basic_map_copy(map->p[i]);
9411 list = isl_basic_map_list_add(list, bmap);
9414 return list;
9417 /* Return the intersection of the elements in the non-empty list "list".
9418 * All elements are assumed to live in the same space.
9420 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9421 __isl_take isl_basic_map_list *list)
9423 int i, n;
9424 isl_basic_map *bmap;
9426 if (!list)
9427 return NULL;
9428 n = isl_basic_map_list_n_basic_map(list);
9429 if (n < 1)
9430 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9431 "expecting non-empty list", goto error);
9433 bmap = isl_basic_map_list_get_basic_map(list, 0);
9434 for (i = 1; i < n; ++i) {
9435 isl_basic_map *bmap_i;
9437 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9438 bmap = isl_basic_map_intersect(bmap, bmap_i);
9441 isl_basic_map_list_free(list);
9442 return bmap;
9443 error:
9444 isl_basic_map_list_free(list);
9445 return NULL;
9448 /* Return the intersection of the elements in the non-empty list "list".
9449 * All elements are assumed to live in the same space.
9451 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9452 __isl_take isl_basic_set_list *list)
9454 return isl_basic_map_list_intersect(list);
9457 /* Return the union of the elements in the non-empty list "list".
9458 * All elements are assumed to live in the same space.
9460 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9462 int i, n;
9463 isl_set *set;
9465 if (!list)
9466 return NULL;
9467 n = isl_set_list_n_set(list);
9468 if (n < 1)
9469 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9470 "expecting non-empty list", goto error);
9472 set = isl_set_list_get_set(list, 0);
9473 for (i = 1; i < n; ++i) {
9474 isl_set *set_i;
9476 set_i = isl_set_list_get_set(list, i);
9477 set = isl_set_union(set, set_i);
9480 isl_set_list_free(list);
9481 return set;
9482 error:
9483 isl_set_list_free(list);
9484 return NULL;
9487 /* Return the Cartesian product of the basic sets in list (in the given order).
9489 __isl_give isl_basic_set *isl_basic_set_list_product(
9490 __isl_take struct isl_basic_set_list *list)
9492 int i;
9493 unsigned dim;
9494 unsigned nparam;
9495 unsigned extra;
9496 unsigned n_eq;
9497 unsigned n_ineq;
9498 struct isl_basic_set *product = NULL;
9500 if (!list)
9501 goto error;
9502 isl_assert(list->ctx, list->n > 0, goto error);
9503 isl_assert(list->ctx, list->p[0], goto error);
9504 nparam = isl_basic_set_n_param(list->p[0]);
9505 dim = isl_basic_set_n_dim(list->p[0]);
9506 extra = list->p[0]->n_div;
9507 n_eq = list->p[0]->n_eq;
9508 n_ineq = list->p[0]->n_ineq;
9509 for (i = 1; i < list->n; ++i) {
9510 isl_assert(list->ctx, list->p[i], goto error);
9511 isl_assert(list->ctx,
9512 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9513 dim += isl_basic_set_n_dim(list->p[i]);
9514 extra += list->p[i]->n_div;
9515 n_eq += list->p[i]->n_eq;
9516 n_ineq += list->p[i]->n_ineq;
9518 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9519 n_eq, n_ineq);
9520 if (!product)
9521 goto error;
9522 dim = 0;
9523 for (i = 0; i < list->n; ++i) {
9524 isl_basic_set_add_constraints(product,
9525 isl_basic_set_copy(list->p[i]), dim);
9526 dim += isl_basic_set_n_dim(list->p[i]);
9528 isl_basic_set_list_free(list);
9529 return product;
9530 error:
9531 isl_basic_set_free(product);
9532 isl_basic_set_list_free(list);
9533 return NULL;
9536 struct isl_basic_map *isl_basic_map_product(
9537 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9539 isl_space *dim_result = NULL;
9540 struct isl_basic_map *bmap;
9541 unsigned in1, in2, out1, out2, nparam, total, pos;
9542 struct isl_dim_map *dim_map1, *dim_map2;
9544 if (!bmap1 || !bmap2)
9545 goto error;
9547 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9548 bmap2->dim, isl_dim_param), goto error);
9549 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9550 isl_space_copy(bmap2->dim));
9552 in1 = isl_basic_map_n_in(bmap1);
9553 in2 = isl_basic_map_n_in(bmap2);
9554 out1 = isl_basic_map_n_out(bmap1);
9555 out2 = isl_basic_map_n_out(bmap2);
9556 nparam = isl_basic_map_n_param(bmap1);
9558 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9559 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9560 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9561 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9562 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9563 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9564 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9565 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9566 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9567 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9568 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9570 bmap = isl_basic_map_alloc_space(dim_result,
9571 bmap1->n_div + bmap2->n_div,
9572 bmap1->n_eq + bmap2->n_eq,
9573 bmap1->n_ineq + bmap2->n_ineq);
9574 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9575 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9576 bmap = isl_basic_map_simplify(bmap);
9577 return isl_basic_map_finalize(bmap);
9578 error:
9579 isl_basic_map_free(bmap1);
9580 isl_basic_map_free(bmap2);
9581 return NULL;
9584 __isl_give isl_basic_map *isl_basic_map_flat_product(
9585 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9587 isl_basic_map *prod;
9589 prod = isl_basic_map_product(bmap1, bmap2);
9590 prod = isl_basic_map_flatten(prod);
9591 return prod;
9594 __isl_give isl_basic_set *isl_basic_set_flat_product(
9595 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9597 return isl_basic_map_flat_range_product(bset1, bset2);
9600 __isl_give isl_basic_map *isl_basic_map_domain_product(
9601 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9603 isl_space *space_result = NULL;
9604 isl_basic_map *bmap;
9605 unsigned in1, in2, out, nparam, total, pos;
9606 struct isl_dim_map *dim_map1, *dim_map2;
9608 if (!bmap1 || !bmap2)
9609 goto error;
9611 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9612 isl_space_copy(bmap2->dim));
9614 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9615 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9616 out = isl_basic_map_dim(bmap1, isl_dim_out);
9617 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9619 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9620 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9621 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9622 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9623 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9624 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9625 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9626 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9627 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9628 isl_dim_map_div(dim_map1, bmap1, pos += out);
9629 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9631 bmap = isl_basic_map_alloc_space(space_result,
9632 bmap1->n_div + bmap2->n_div,
9633 bmap1->n_eq + bmap2->n_eq,
9634 bmap1->n_ineq + bmap2->n_ineq);
9635 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9636 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9637 bmap = isl_basic_map_simplify(bmap);
9638 return isl_basic_map_finalize(bmap);
9639 error:
9640 isl_basic_map_free(bmap1);
9641 isl_basic_map_free(bmap2);
9642 return NULL;
9645 __isl_give isl_basic_map *isl_basic_map_range_product(
9646 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9648 int rational;
9649 isl_space *dim_result = NULL;
9650 isl_basic_map *bmap;
9651 unsigned in, out1, out2, nparam, total, pos;
9652 struct isl_dim_map *dim_map1, *dim_map2;
9654 rational = isl_basic_map_is_rational(bmap1);
9655 if (rational >= 0 && rational)
9656 rational = isl_basic_map_is_rational(bmap2);
9657 if (!bmap1 || !bmap2 || rational < 0)
9658 goto error;
9660 if (!isl_space_match(bmap1->dim, isl_dim_param,
9661 bmap2->dim, isl_dim_param))
9662 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9663 "parameters don't match", goto error);
9665 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9666 isl_space_copy(bmap2->dim));
9668 in = isl_basic_map_dim(bmap1, isl_dim_in);
9669 out1 = isl_basic_map_n_out(bmap1);
9670 out2 = isl_basic_map_n_out(bmap2);
9671 nparam = isl_basic_map_n_param(bmap1);
9673 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9674 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9675 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9676 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9677 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9678 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9679 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9680 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9681 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9682 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9683 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9685 bmap = isl_basic_map_alloc_space(dim_result,
9686 bmap1->n_div + bmap2->n_div,
9687 bmap1->n_eq + bmap2->n_eq,
9688 bmap1->n_ineq + bmap2->n_ineq);
9689 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9690 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9691 if (rational)
9692 bmap = isl_basic_map_set_rational(bmap);
9693 bmap = isl_basic_map_simplify(bmap);
9694 return isl_basic_map_finalize(bmap);
9695 error:
9696 isl_basic_map_free(bmap1);
9697 isl_basic_map_free(bmap2);
9698 return NULL;
9701 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9702 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9704 isl_basic_map *prod;
9706 prod = isl_basic_map_range_product(bmap1, bmap2);
9707 prod = isl_basic_map_flatten_range(prod);
9708 return prod;
9711 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9712 * and collect the results.
9713 * The result live in the space obtained by calling "space_product"
9714 * on the spaces of "map1" and "map2".
9715 * If "remove_duplicates" is set then the result may contain duplicates
9716 * (even if the inputs do not) and so we try and remove the obvious
9717 * duplicates.
9719 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9720 __isl_take isl_map *map2,
9721 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9722 __isl_take isl_space *right),
9723 __isl_give isl_basic_map *(*basic_map_product)(
9724 __isl_take isl_basic_map *left,
9725 __isl_take isl_basic_map *right),
9726 int remove_duplicates)
9728 unsigned flags = 0;
9729 struct isl_map *result;
9730 int i, j;
9732 if (!map1 || !map2)
9733 goto error;
9735 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9736 map2->dim, isl_dim_param), goto error);
9738 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9739 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9740 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9742 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9743 isl_space_copy(map2->dim)),
9744 map1->n * map2->n, flags);
9745 if (!result)
9746 goto error;
9747 for (i = 0; i < map1->n; ++i)
9748 for (j = 0; j < map2->n; ++j) {
9749 struct isl_basic_map *part;
9750 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9751 isl_basic_map_copy(map2->p[j]));
9752 if (isl_basic_map_is_empty(part))
9753 isl_basic_map_free(part);
9754 else
9755 result = isl_map_add_basic_map(result, part);
9756 if (!result)
9757 goto error;
9759 if (remove_duplicates)
9760 result = isl_map_remove_obvious_duplicates(result);
9761 isl_map_free(map1);
9762 isl_map_free(map2);
9763 return result;
9764 error:
9765 isl_map_free(map1);
9766 isl_map_free(map2);
9767 return NULL;
9770 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9772 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9773 __isl_take isl_map *map2)
9775 return map_product(map1, map2, &isl_space_product,
9776 &isl_basic_map_product, 0);
9779 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9780 __isl_take isl_map *map2)
9782 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9785 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9787 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9788 __isl_take isl_map *map2)
9790 isl_map *prod;
9792 prod = isl_map_product(map1, map2);
9793 prod = isl_map_flatten(prod);
9794 return prod;
9797 /* Given two set A and B, construct its Cartesian product A x B.
9799 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9801 return isl_map_range_product(set1, set2);
9804 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9805 __isl_take isl_set *set2)
9807 return isl_map_flat_range_product(set1, set2);
9810 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9812 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9813 __isl_take isl_map *map2)
9815 return map_product(map1, map2, &isl_space_domain_product,
9816 &isl_basic_map_domain_product, 1);
9819 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9821 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9822 __isl_take isl_map *map2)
9824 return map_product(map1, map2, &isl_space_range_product,
9825 &isl_basic_map_range_product, 1);
9828 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9829 __isl_take isl_map *map2)
9831 return isl_map_align_params_map_map_and(map1, map2,
9832 &map_domain_product_aligned);
9835 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9836 __isl_take isl_map *map2)
9838 return isl_map_align_params_map_map_and(map1, map2,
9839 &map_range_product_aligned);
9842 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9844 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9846 isl_space *space;
9847 int total1, keep1, total2, keep2;
9849 if (!map)
9850 return NULL;
9851 if (!isl_space_domain_is_wrapping(map->dim) ||
9852 !isl_space_range_is_wrapping(map->dim))
9853 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9854 "not a product", return isl_map_free(map));
9856 space = isl_map_get_space(map);
9857 total1 = isl_space_dim(space, isl_dim_in);
9858 total2 = isl_space_dim(space, isl_dim_out);
9859 space = isl_space_factor_domain(space);
9860 keep1 = isl_space_dim(space, isl_dim_in);
9861 keep2 = isl_space_dim(space, isl_dim_out);
9862 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9863 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9864 map = isl_map_reset_space(map, space);
9866 return map;
9869 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9871 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9873 isl_space *space;
9874 int total1, keep1, total2, keep2;
9876 if (!map)
9877 return NULL;
9878 if (!isl_space_domain_is_wrapping(map->dim) ||
9879 !isl_space_range_is_wrapping(map->dim))
9880 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9881 "not a product", return isl_map_free(map));
9883 space = isl_map_get_space(map);
9884 total1 = isl_space_dim(space, isl_dim_in);
9885 total2 = isl_space_dim(space, isl_dim_out);
9886 space = isl_space_factor_range(space);
9887 keep1 = isl_space_dim(space, isl_dim_in);
9888 keep2 = isl_space_dim(space, isl_dim_out);
9889 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9890 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9891 map = isl_map_reset_space(map, space);
9893 return map;
9896 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9898 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9900 isl_space *space;
9901 int total, keep;
9903 if (!map)
9904 return NULL;
9905 if (!isl_space_domain_is_wrapping(map->dim))
9906 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9907 "domain is not a product", return isl_map_free(map));
9909 space = isl_map_get_space(map);
9910 total = isl_space_dim(space, isl_dim_in);
9911 space = isl_space_domain_factor_domain(space);
9912 keep = isl_space_dim(space, isl_dim_in);
9913 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9914 map = isl_map_reset_space(map, space);
9916 return map;
9919 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9921 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9923 isl_space *space;
9924 int total, keep;
9926 if (!map)
9927 return NULL;
9928 if (!isl_space_domain_is_wrapping(map->dim))
9929 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9930 "domain is not a product", return isl_map_free(map));
9932 space = isl_map_get_space(map);
9933 total = isl_space_dim(space, isl_dim_in);
9934 space = isl_space_domain_factor_range(space);
9935 keep = isl_space_dim(space, isl_dim_in);
9936 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9937 map = isl_map_reset_space(map, space);
9939 return map;
9942 /* Given a map A -> [B -> C], extract the map A -> B.
9944 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9946 isl_space *space;
9947 int total, keep;
9949 if (!map)
9950 return NULL;
9951 if (!isl_space_range_is_wrapping(map->dim))
9952 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9953 "range is not a product", return isl_map_free(map));
9955 space = isl_map_get_space(map);
9956 total = isl_space_dim(space, isl_dim_out);
9957 space = isl_space_range_factor_domain(space);
9958 keep = isl_space_dim(space, isl_dim_out);
9959 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9960 map = isl_map_reset_space(map, space);
9962 return map;
9965 /* Given a map A -> [B -> C], extract the map A -> C.
9967 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9969 isl_space *space;
9970 int total, keep;
9972 if (!map)
9973 return NULL;
9974 if (!isl_space_range_is_wrapping(map->dim))
9975 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9976 "range is not a product", return isl_map_free(map));
9978 space = isl_map_get_space(map);
9979 total = isl_space_dim(space, isl_dim_out);
9980 space = isl_space_range_factor_range(space);
9981 keep = isl_space_dim(space, isl_dim_out);
9982 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9983 map = isl_map_reset_space(map, space);
9985 return map;
9988 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9990 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9991 __isl_take isl_map *map2)
9993 isl_map *prod;
9995 prod = isl_map_domain_product(map1, map2);
9996 prod = isl_map_flatten_domain(prod);
9997 return prod;
10000 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10002 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10003 __isl_take isl_map *map2)
10005 isl_map *prod;
10007 prod = isl_map_range_product(map1, map2);
10008 prod = isl_map_flatten_range(prod);
10009 return prod;
10012 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10014 int i;
10015 uint32_t hash = isl_hash_init();
10016 unsigned total;
10018 if (!bmap)
10019 return 0;
10020 bmap = isl_basic_map_copy(bmap);
10021 bmap = isl_basic_map_normalize(bmap);
10022 if (!bmap)
10023 return 0;
10024 total = isl_basic_map_total_dim(bmap);
10025 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10026 for (i = 0; i < bmap->n_eq; ++i) {
10027 uint32_t c_hash;
10028 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10029 isl_hash_hash(hash, c_hash);
10031 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10032 for (i = 0; i < bmap->n_ineq; ++i) {
10033 uint32_t c_hash;
10034 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10035 isl_hash_hash(hash, c_hash);
10037 isl_hash_byte(hash, bmap->n_div & 0xFF);
10038 for (i = 0; i < bmap->n_div; ++i) {
10039 uint32_t c_hash;
10040 if (isl_int_is_zero(bmap->div[i][0]))
10041 continue;
10042 isl_hash_byte(hash, i & 0xFF);
10043 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10044 isl_hash_hash(hash, c_hash);
10046 isl_basic_map_free(bmap);
10047 return hash;
10050 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10052 return isl_basic_map_get_hash((isl_basic_map *)bset);
10055 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10057 int i;
10058 uint32_t hash;
10060 if (!map)
10061 return 0;
10062 map = isl_map_copy(map);
10063 map = isl_map_normalize(map);
10064 if (!map)
10065 return 0;
10067 hash = isl_hash_init();
10068 for (i = 0; i < map->n; ++i) {
10069 uint32_t bmap_hash;
10070 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10071 isl_hash_hash(hash, bmap_hash);
10074 isl_map_free(map);
10076 return hash;
10079 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10081 return isl_map_get_hash((isl_map *)set);
10084 /* Check if the value for dimension dim is completely determined
10085 * by the values of the other parameters and variables.
10086 * That is, check if dimension dim is involved in an equality.
10088 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10090 int i;
10091 unsigned nparam;
10093 if (!bset)
10094 return -1;
10095 nparam = isl_basic_set_n_param(bset);
10096 for (i = 0; i < bset->n_eq; ++i)
10097 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10098 return 1;
10099 return 0;
10102 /* Check if the value for dimension dim is completely determined
10103 * by the values of the other parameters and variables.
10104 * That is, check if dimension dim is involved in an equality
10105 * for each of the subsets.
10107 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10109 int i;
10111 if (!set)
10112 return -1;
10113 for (i = 0; i < set->n; ++i) {
10114 int unique;
10115 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10116 if (unique != 1)
10117 return unique;
10119 return 1;
10122 /* Return the number of basic maps in the (current) representation of "map".
10124 int isl_map_n_basic_map(__isl_keep isl_map *map)
10126 return map ? map->n : 0;
10129 int isl_set_n_basic_set(__isl_keep isl_set *set)
10131 return set ? set->n : 0;
10134 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10135 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10137 int i;
10139 if (!map)
10140 return isl_stat_error;
10142 for (i = 0; i < map->n; ++i)
10143 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10144 return isl_stat_error;
10146 return isl_stat_ok;
10149 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10150 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10152 int i;
10154 if (!set)
10155 return isl_stat_error;
10157 for (i = 0; i < set->n; ++i)
10158 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10159 return isl_stat_error;
10161 return isl_stat_ok;
10164 /* Return a list of basic sets, the union of which is equal to "set".
10166 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10167 __isl_keep isl_set *set)
10169 int i;
10170 isl_basic_set_list *list;
10172 if (!set)
10173 return NULL;
10175 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10176 for (i = 0; i < set->n; ++i) {
10177 isl_basic_set *bset;
10179 bset = isl_basic_set_copy(set->p[i]);
10180 list = isl_basic_set_list_add(list, bset);
10183 return list;
10186 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10188 isl_space *dim;
10190 if (!bset)
10191 return NULL;
10193 bset = isl_basic_set_cow(bset);
10194 if (!bset)
10195 return NULL;
10197 dim = isl_basic_set_get_space(bset);
10198 dim = isl_space_lift(dim, bset->n_div);
10199 if (!dim)
10200 goto error;
10201 isl_space_free(bset->dim);
10202 bset->dim = dim;
10203 bset->extra -= bset->n_div;
10204 bset->n_div = 0;
10206 bset = isl_basic_set_finalize(bset);
10208 return bset;
10209 error:
10210 isl_basic_set_free(bset);
10211 return NULL;
10214 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10216 int i;
10217 isl_space *dim;
10218 unsigned n_div;
10220 set = isl_set_align_divs(set);
10222 if (!set)
10223 return NULL;
10225 set = isl_set_cow(set);
10226 if (!set)
10227 return NULL;
10229 n_div = set->p[0]->n_div;
10230 dim = isl_set_get_space(set);
10231 dim = isl_space_lift(dim, n_div);
10232 if (!dim)
10233 goto error;
10234 isl_space_free(set->dim);
10235 set->dim = dim;
10237 for (i = 0; i < set->n; ++i) {
10238 set->p[i] = isl_basic_set_lift(set->p[i]);
10239 if (!set->p[i])
10240 goto error;
10243 return set;
10244 error:
10245 isl_set_free(set);
10246 return NULL;
10249 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10251 isl_space *dim;
10252 struct isl_basic_map *bmap;
10253 unsigned n_set;
10254 unsigned n_div;
10255 unsigned n_param;
10256 unsigned total;
10257 int i, k, l;
10259 set = isl_set_align_divs(set);
10261 if (!set)
10262 return NULL;
10264 dim = isl_set_get_space(set);
10265 if (set->n == 0 || set->p[0]->n_div == 0) {
10266 isl_set_free(set);
10267 return isl_map_identity(isl_space_map_from_set(dim));
10270 n_div = set->p[0]->n_div;
10271 dim = isl_space_map_from_set(dim);
10272 n_param = isl_space_dim(dim, isl_dim_param);
10273 n_set = isl_space_dim(dim, isl_dim_in);
10274 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10275 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10276 for (i = 0; i < n_set; ++i)
10277 bmap = var_equal(bmap, i);
10279 total = n_param + n_set + n_set + n_div;
10280 for (i = 0; i < n_div; ++i) {
10281 k = isl_basic_map_alloc_inequality(bmap);
10282 if (k < 0)
10283 goto error;
10284 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10285 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10286 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10287 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10288 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10289 set->p[0]->div[i][0]);
10291 l = isl_basic_map_alloc_inequality(bmap);
10292 if (l < 0)
10293 goto error;
10294 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10295 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10296 set->p[0]->div[i][0]);
10297 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10300 isl_set_free(set);
10301 bmap = isl_basic_map_simplify(bmap);
10302 bmap = isl_basic_map_finalize(bmap);
10303 return isl_map_from_basic_map(bmap);
10304 error:
10305 isl_set_free(set);
10306 isl_basic_map_free(bmap);
10307 return NULL;
10310 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10312 unsigned dim;
10313 int size = 0;
10315 if (!bset)
10316 return -1;
10318 dim = isl_basic_set_total_dim(bset);
10319 size += bset->n_eq * (1 + dim);
10320 size += bset->n_ineq * (1 + dim);
10321 size += bset->n_div * (2 + dim);
10323 return size;
10326 int isl_set_size(__isl_keep isl_set *set)
10328 int i;
10329 int size = 0;
10331 if (!set)
10332 return -1;
10334 for (i = 0; i < set->n; ++i)
10335 size += isl_basic_set_size(set->p[i]);
10337 return size;
10340 /* Check if there is any lower bound (if lower == 0) and/or upper
10341 * bound (if upper == 0) on the specified dim.
10343 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10344 enum isl_dim_type type, unsigned pos, int lower, int upper)
10346 int i;
10348 if (!bmap)
10349 return isl_bool_error;
10351 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10352 return isl_bool_error);
10354 pos += isl_basic_map_offset(bmap, type);
10356 for (i = 0; i < bmap->n_div; ++i) {
10357 if (isl_int_is_zero(bmap->div[i][0]))
10358 continue;
10359 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10360 return isl_bool_true;
10363 for (i = 0; i < bmap->n_eq; ++i)
10364 if (!isl_int_is_zero(bmap->eq[i][pos]))
10365 return isl_bool_true;
10367 for (i = 0; i < bmap->n_ineq; ++i) {
10368 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10369 if (sgn > 0)
10370 lower = 1;
10371 if (sgn < 0)
10372 upper = 1;
10375 return lower && upper;
10378 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10379 enum isl_dim_type type, unsigned pos)
10381 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10384 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10385 enum isl_dim_type type, unsigned pos)
10387 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10390 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10391 enum isl_dim_type type, unsigned pos)
10393 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10396 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10397 enum isl_dim_type type, unsigned pos)
10399 int i;
10401 if (!map)
10402 return -1;
10404 for (i = 0; i < map->n; ++i) {
10405 int bounded;
10406 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10407 if (bounded < 0 || !bounded)
10408 return bounded;
10411 return 1;
10414 /* Return 1 if the specified dim is involved in both an upper bound
10415 * and a lower bound.
10417 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10418 enum isl_dim_type type, unsigned pos)
10420 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10423 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10425 static isl_bool has_any_bound(__isl_keep isl_map *map,
10426 enum isl_dim_type type, unsigned pos,
10427 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10428 enum isl_dim_type type, unsigned pos))
10430 int i;
10432 if (!map)
10433 return isl_bool_error;
10435 for (i = 0; i < map->n; ++i) {
10436 isl_bool bounded;
10437 bounded = fn(map->p[i], type, pos);
10438 if (bounded < 0 || bounded)
10439 return bounded;
10442 return isl_bool_false;
10445 /* Return 1 if the specified dim is involved in any lower bound.
10447 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10448 enum isl_dim_type type, unsigned pos)
10450 return has_any_bound(set, type, pos,
10451 &isl_basic_map_dim_has_lower_bound);
10454 /* Return 1 if the specified dim is involved in any upper bound.
10456 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10457 enum isl_dim_type type, unsigned pos)
10459 return has_any_bound(set, type, pos,
10460 &isl_basic_map_dim_has_upper_bound);
10463 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10465 static isl_bool has_bound(__isl_keep isl_map *map,
10466 enum isl_dim_type type, unsigned pos,
10467 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10468 enum isl_dim_type type, unsigned pos))
10470 int i;
10472 if (!map)
10473 return isl_bool_error;
10475 for (i = 0; i < map->n; ++i) {
10476 isl_bool bounded;
10477 bounded = fn(map->p[i], type, pos);
10478 if (bounded < 0 || !bounded)
10479 return bounded;
10482 return isl_bool_true;
10485 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10487 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10488 enum isl_dim_type type, unsigned pos)
10490 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10493 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10495 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10496 enum isl_dim_type type, unsigned pos)
10498 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10501 /* For each of the "n" variables starting at "first", determine
10502 * the sign of the variable and put the results in the first "n"
10503 * elements of the array "signs".
10504 * Sign
10505 * 1 means that the variable is non-negative
10506 * -1 means that the variable is non-positive
10507 * 0 means the variable attains both positive and negative values.
10509 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10510 unsigned first, unsigned n, int *signs)
10512 isl_vec *bound = NULL;
10513 struct isl_tab *tab = NULL;
10514 struct isl_tab_undo *snap;
10515 int i;
10517 if (!bset || !signs)
10518 return -1;
10520 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10521 tab = isl_tab_from_basic_set(bset, 0);
10522 if (!bound || !tab)
10523 goto error;
10525 isl_seq_clr(bound->el, bound->size);
10526 isl_int_set_si(bound->el[0], -1);
10528 snap = isl_tab_snap(tab);
10529 for (i = 0; i < n; ++i) {
10530 int empty;
10532 isl_int_set_si(bound->el[1 + first + i], -1);
10533 if (isl_tab_add_ineq(tab, bound->el) < 0)
10534 goto error;
10535 empty = tab->empty;
10536 isl_int_set_si(bound->el[1 + first + i], 0);
10537 if (isl_tab_rollback(tab, snap) < 0)
10538 goto error;
10540 if (empty) {
10541 signs[i] = 1;
10542 continue;
10545 isl_int_set_si(bound->el[1 + first + i], 1);
10546 if (isl_tab_add_ineq(tab, bound->el) < 0)
10547 goto error;
10548 empty = tab->empty;
10549 isl_int_set_si(bound->el[1 + first + i], 0);
10550 if (isl_tab_rollback(tab, snap) < 0)
10551 goto error;
10553 signs[i] = empty ? -1 : 0;
10556 isl_tab_free(tab);
10557 isl_vec_free(bound);
10558 return 0;
10559 error:
10560 isl_tab_free(tab);
10561 isl_vec_free(bound);
10562 return -1;
10565 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10566 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10568 if (!bset || !signs)
10569 return -1;
10570 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10571 return -1);
10573 first += pos(bset->dim, type) - 1;
10574 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10577 /* Is it possible for the integer division "div" to depend (possibly
10578 * indirectly) on any output dimensions?
10580 * If the div is undefined, then we conservatively assume that it
10581 * may depend on them.
10582 * Otherwise, we check if it actually depends on them or on any integer
10583 * divisions that may depend on them.
10585 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10587 int i;
10588 unsigned n_out, o_out;
10589 unsigned n_div, o_div;
10591 if (isl_int_is_zero(bmap->div[div][0]))
10592 return 1;
10594 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10595 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10597 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10598 return 1;
10600 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10601 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10603 for (i = 0; i < n_div; ++i) {
10604 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10605 continue;
10606 if (div_may_involve_output(bmap, i))
10607 return 1;
10610 return 0;
10613 /* Return the first integer division of "bmap" in the range
10614 * [first, first + n[ that may depend on any output dimensions and
10615 * that has a non-zero coefficient in "c" (where the first coefficient
10616 * in "c" corresponds to integer division "first").
10618 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10619 isl_int *c, int first, int n)
10621 int k;
10623 if (!bmap)
10624 return -1;
10626 for (k = first; k < first + n; ++k) {
10627 if (isl_int_is_zero(c[k]))
10628 continue;
10629 if (div_may_involve_output(bmap, k))
10630 return k;
10633 return first + n;
10636 /* Look for a pair of inequality constraints in "bmap" of the form
10638 * -l + i >= 0 or i >= l
10639 * and
10640 * n + l - i >= 0 or i <= l + n
10642 * with n < "m" and i the output dimension at position "pos".
10643 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10644 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10645 * and earlier output dimensions, as well as integer divisions that do
10646 * not involve any of the output dimensions.
10648 * Return the index of the first inequality constraint or bmap->n_ineq
10649 * if no such pair can be found.
10651 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10652 int pos, isl_int m)
10654 int i, j;
10655 isl_ctx *ctx;
10656 unsigned total;
10657 unsigned n_div, o_div;
10658 unsigned n_out, o_out;
10659 int less;
10661 if (!bmap)
10662 return -1;
10664 ctx = isl_basic_map_get_ctx(bmap);
10665 total = isl_basic_map_total_dim(bmap);
10666 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10667 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10668 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10669 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10670 for (i = 0; i < bmap->n_ineq; ++i) {
10671 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10672 continue;
10673 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10674 n_out - (pos + 1)) != -1)
10675 continue;
10676 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10677 0, n_div) < n_div)
10678 continue;
10679 for (j = i + 1; j < bmap->n_ineq; ++j) {
10680 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10681 ctx->one))
10682 continue;
10683 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10684 bmap->ineq[j] + 1, total))
10685 continue;
10686 break;
10688 if (j >= bmap->n_ineq)
10689 continue;
10690 isl_int_add(bmap->ineq[i][0],
10691 bmap->ineq[i][0], bmap->ineq[j][0]);
10692 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10693 isl_int_sub(bmap->ineq[i][0],
10694 bmap->ineq[i][0], bmap->ineq[j][0]);
10695 if (!less)
10696 continue;
10697 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10698 return i;
10699 else
10700 return j;
10703 return bmap->n_ineq;
10706 /* Return the index of the equality of "bmap" that defines
10707 * the output dimension "pos" in terms of earlier dimensions.
10708 * The equality may also involve integer divisions, as long
10709 * as those integer divisions are defined in terms of
10710 * parameters or input dimensions.
10711 * In this case, *div is set to the number of integer divisions and
10712 * *ineq is set to the number of inequality constraints (provided
10713 * div and ineq are not NULL).
10715 * The equality may also involve a single integer division involving
10716 * the output dimensions (typically only output dimension "pos") as
10717 * long as the coefficient of output dimension "pos" is 1 or -1 and
10718 * there is a pair of constraints i >= l and i <= l + n, with i referring
10719 * to output dimension "pos", l an expression involving only earlier
10720 * dimensions and n smaller than the coefficient of the integer division
10721 * in the equality. In this case, the output dimension can be defined
10722 * in terms of a modulo expression that does not involve the integer division.
10723 * *div is then set to this single integer division and
10724 * *ineq is set to the index of constraint i >= l.
10726 * Return bmap->n_eq if there is no such equality.
10727 * Return -1 on error.
10729 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10730 int pos, int *div, int *ineq)
10732 int j, k, l;
10733 unsigned n_out, o_out;
10734 unsigned n_div, o_div;
10736 if (!bmap)
10737 return -1;
10739 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10740 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10741 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10742 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10744 if (ineq)
10745 *ineq = bmap->n_ineq;
10746 if (div)
10747 *div = n_div;
10748 for (j = 0; j < bmap->n_eq; ++j) {
10749 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10750 continue;
10751 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10752 n_out - (pos + 1)) != -1)
10753 continue;
10754 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10755 0, n_div);
10756 if (k >= n_div)
10757 return j;
10758 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10759 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10760 continue;
10761 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10762 k + 1, n_div - (k+1)) < n_div)
10763 continue;
10764 l = find_modulo_constraint_pair(bmap, pos,
10765 bmap->eq[j][o_div + k]);
10766 if (l < 0)
10767 return -1;
10768 if (l >= bmap->n_ineq)
10769 continue;
10770 if (div)
10771 *div = k;
10772 if (ineq)
10773 *ineq = l;
10774 return j;
10777 return bmap->n_eq;
10780 /* Check if the given basic map is obviously single-valued.
10781 * In particular, for each output dimension, check that there is
10782 * an equality that defines the output dimension in terms of
10783 * earlier dimensions.
10785 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10787 int i;
10788 unsigned n_out;
10790 if (!bmap)
10791 return isl_bool_error;
10793 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10795 for (i = 0; i < n_out; ++i) {
10796 int eq;
10798 eq = isl_basic_map_output_defining_equality(bmap, i,
10799 NULL, NULL);
10800 if (eq < 0)
10801 return isl_bool_error;
10802 if (eq >= bmap->n_eq)
10803 return isl_bool_false;
10806 return isl_bool_true;
10809 /* Check if the given basic map is single-valued.
10810 * We simply compute
10812 * M \circ M^-1
10814 * and check if the result is a subset of the identity mapping.
10816 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10818 isl_space *space;
10819 isl_basic_map *test;
10820 isl_basic_map *id;
10821 isl_bool sv;
10823 sv = isl_basic_map_plain_is_single_valued(bmap);
10824 if (sv < 0 || sv)
10825 return sv;
10827 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10828 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10830 space = isl_basic_map_get_space(bmap);
10831 space = isl_space_map_from_set(isl_space_range(space));
10832 id = isl_basic_map_identity(space);
10834 sv = isl_basic_map_is_subset(test, id);
10836 isl_basic_map_free(test);
10837 isl_basic_map_free(id);
10839 return sv;
10842 /* Check if the given map is obviously single-valued.
10844 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10846 if (!map)
10847 return isl_bool_error;
10848 if (map->n == 0)
10849 return isl_bool_true;
10850 if (map->n >= 2)
10851 return isl_bool_false;
10853 return isl_basic_map_plain_is_single_valued(map->p[0]);
10856 /* Check if the given map is single-valued.
10857 * We simply compute
10859 * M \circ M^-1
10861 * and check if the result is a subset of the identity mapping.
10863 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10865 isl_space *dim;
10866 isl_map *test;
10867 isl_map *id;
10868 isl_bool sv;
10870 sv = isl_map_plain_is_single_valued(map);
10871 if (sv < 0 || sv)
10872 return sv;
10874 test = isl_map_reverse(isl_map_copy(map));
10875 test = isl_map_apply_range(test, isl_map_copy(map));
10877 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10878 id = isl_map_identity(dim);
10880 sv = isl_map_is_subset(test, id);
10882 isl_map_free(test);
10883 isl_map_free(id);
10885 return sv;
10888 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10890 isl_bool in;
10892 map = isl_map_copy(map);
10893 map = isl_map_reverse(map);
10894 in = isl_map_is_single_valued(map);
10895 isl_map_free(map);
10897 return in;
10900 /* Check if the given map is obviously injective.
10902 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10904 isl_bool in;
10906 map = isl_map_copy(map);
10907 map = isl_map_reverse(map);
10908 in = isl_map_plain_is_single_valued(map);
10909 isl_map_free(map);
10911 return in;
10914 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10916 isl_bool sv;
10918 sv = isl_map_is_single_valued(map);
10919 if (sv < 0 || !sv)
10920 return sv;
10922 return isl_map_is_injective(map);
10925 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10927 return isl_map_is_single_valued((isl_map *)set);
10930 /* Does "map" only map elements to themselves?
10932 * If the domain and range spaces are different, then "map"
10933 * is considered not to be an identity relation, even if it is empty.
10934 * Otherwise, construct the maximal identity relation and
10935 * check whether "map" is a subset of this relation.
10937 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10939 isl_space *space;
10940 isl_map *id;
10941 isl_bool equal, is_identity;
10943 space = isl_map_get_space(map);
10944 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10945 isl_space_free(space);
10946 if (equal < 0 || !equal)
10947 return equal;
10949 id = isl_map_identity(isl_map_get_space(map));
10950 is_identity = isl_map_is_subset(map, id);
10951 isl_map_free(id);
10953 return is_identity;
10956 int isl_map_is_translation(__isl_keep isl_map *map)
10958 int ok;
10959 isl_set *delta;
10961 delta = isl_map_deltas(isl_map_copy(map));
10962 ok = isl_set_is_singleton(delta);
10963 isl_set_free(delta);
10965 return ok;
10968 static int unique(isl_int *p, unsigned pos, unsigned len)
10970 if (isl_seq_first_non_zero(p, pos) != -1)
10971 return 0;
10972 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10973 return 0;
10974 return 1;
10977 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10979 int i, j;
10980 unsigned nvar;
10981 unsigned ovar;
10983 if (!bset)
10984 return -1;
10986 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10987 return 0;
10989 nvar = isl_basic_set_dim(bset, isl_dim_set);
10990 ovar = isl_space_offset(bset->dim, isl_dim_set);
10991 for (j = 0; j < nvar; ++j) {
10992 int lower = 0, upper = 0;
10993 for (i = 0; i < bset->n_eq; ++i) {
10994 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10995 continue;
10996 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10997 return 0;
10998 break;
11000 if (i < bset->n_eq)
11001 continue;
11002 for (i = 0; i < bset->n_ineq; ++i) {
11003 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11004 continue;
11005 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11006 return 0;
11007 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11008 lower = 1;
11009 else
11010 upper = 1;
11012 if (!lower || !upper)
11013 return 0;
11016 return 1;
11019 int isl_set_is_box(__isl_keep isl_set *set)
11021 if (!set)
11022 return -1;
11023 if (set->n != 1)
11024 return 0;
11026 return isl_basic_set_is_box(set->p[0]);
11029 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11031 if (!bset)
11032 return isl_bool_error;
11034 return isl_space_is_wrapping(bset->dim);
11037 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11039 if (!set)
11040 return isl_bool_error;
11042 return isl_space_is_wrapping(set->dim);
11045 /* Modify the space of "map" through a call to "change".
11046 * If "can_change" is set (not NULL), then first call it to check
11047 * if the modification is allowed, printing the error message "cannot_change"
11048 * if it is not.
11050 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11051 isl_bool (*can_change)(__isl_keep isl_map *map),
11052 const char *cannot_change,
11053 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11055 isl_bool ok;
11056 isl_space *space;
11058 if (!map)
11059 return NULL;
11061 ok = can_change ? can_change(map) : isl_bool_true;
11062 if (ok < 0)
11063 return isl_map_free(map);
11064 if (!ok)
11065 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11066 return isl_map_free(map));
11068 space = change(isl_map_get_space(map));
11069 map = isl_map_reset_space(map, space);
11071 return map;
11074 /* Is the domain of "map" a wrapped relation?
11076 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11078 if (!map)
11079 return isl_bool_error;
11081 return isl_space_domain_is_wrapping(map->dim);
11084 /* Is the range of "map" a wrapped relation?
11086 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11088 if (!map)
11089 return isl_bool_error;
11091 return isl_space_range_is_wrapping(map->dim);
11094 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11096 bmap = isl_basic_map_cow(bmap);
11097 if (!bmap)
11098 return NULL;
11100 bmap->dim = isl_space_wrap(bmap->dim);
11101 if (!bmap->dim)
11102 goto error;
11104 bmap = isl_basic_map_finalize(bmap);
11106 return (isl_basic_set *)bmap;
11107 error:
11108 isl_basic_map_free(bmap);
11109 return NULL;
11112 /* Given a map A -> B, return the set (A -> B).
11114 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11116 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11119 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11121 bset = isl_basic_set_cow(bset);
11122 if (!bset)
11123 return NULL;
11125 bset->dim = isl_space_unwrap(bset->dim);
11126 if (!bset->dim)
11127 goto error;
11129 bset = isl_basic_set_finalize(bset);
11131 return (isl_basic_map *)bset;
11132 error:
11133 isl_basic_set_free(bset);
11134 return NULL;
11137 /* Given a set (A -> B), return the map A -> B.
11138 * Error out if "set" is not of the form (A -> B).
11140 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11142 return isl_map_change_space(set, &isl_set_is_wrapping,
11143 "not a wrapping set", &isl_space_unwrap);
11146 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11147 enum isl_dim_type type)
11149 if (!bmap)
11150 return NULL;
11152 if (!isl_space_is_named_or_nested(bmap->dim, type))
11153 return bmap;
11155 bmap = isl_basic_map_cow(bmap);
11156 if (!bmap)
11157 return NULL;
11159 bmap->dim = isl_space_reset(bmap->dim, type);
11160 if (!bmap->dim)
11161 goto error;
11163 bmap = isl_basic_map_finalize(bmap);
11165 return bmap;
11166 error:
11167 isl_basic_map_free(bmap);
11168 return NULL;
11171 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11172 enum isl_dim_type type)
11174 int i;
11176 if (!map)
11177 return NULL;
11179 if (!isl_space_is_named_or_nested(map->dim, type))
11180 return map;
11182 map = isl_map_cow(map);
11183 if (!map)
11184 return NULL;
11186 for (i = 0; i < map->n; ++i) {
11187 map->p[i] = isl_basic_map_reset(map->p[i], type);
11188 if (!map->p[i])
11189 goto error;
11191 map->dim = isl_space_reset(map->dim, type);
11192 if (!map->dim)
11193 goto error;
11195 return map;
11196 error:
11197 isl_map_free(map);
11198 return NULL;
11201 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11203 if (!bmap)
11204 return NULL;
11206 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11207 return bmap;
11209 bmap = isl_basic_map_cow(bmap);
11210 if (!bmap)
11211 return NULL;
11213 bmap->dim = isl_space_flatten(bmap->dim);
11214 if (!bmap->dim)
11215 goto error;
11217 bmap = isl_basic_map_finalize(bmap);
11219 return bmap;
11220 error:
11221 isl_basic_map_free(bmap);
11222 return NULL;
11225 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11227 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11230 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11231 __isl_take isl_basic_map *bmap)
11233 if (!bmap)
11234 return NULL;
11236 if (!bmap->dim->nested[0])
11237 return bmap;
11239 bmap = isl_basic_map_cow(bmap);
11240 if (!bmap)
11241 return NULL;
11243 bmap->dim = isl_space_flatten_domain(bmap->dim);
11244 if (!bmap->dim)
11245 goto error;
11247 bmap = isl_basic_map_finalize(bmap);
11249 return bmap;
11250 error:
11251 isl_basic_map_free(bmap);
11252 return NULL;
11255 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11256 __isl_take isl_basic_map *bmap)
11258 if (!bmap)
11259 return NULL;
11261 if (!bmap->dim->nested[1])
11262 return bmap;
11264 bmap = isl_basic_map_cow(bmap);
11265 if (!bmap)
11266 return NULL;
11268 bmap->dim = isl_space_flatten_range(bmap->dim);
11269 if (!bmap->dim)
11270 goto error;
11272 bmap = isl_basic_map_finalize(bmap);
11274 return bmap;
11275 error:
11276 isl_basic_map_free(bmap);
11277 return NULL;
11280 /* Remove any internal structure from the spaces of domain and range of "map".
11282 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11284 if (!map)
11285 return NULL;
11287 if (!map->dim->nested[0] && !map->dim->nested[1])
11288 return map;
11290 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11293 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11295 return (isl_set *)isl_map_flatten((isl_map *)set);
11298 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11300 isl_space *dim, *flat_dim;
11301 isl_map *map;
11303 dim = isl_set_get_space(set);
11304 flat_dim = isl_space_flatten(isl_space_copy(dim));
11305 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11306 map = isl_map_intersect_domain(map, set);
11308 return map;
11311 /* Remove any internal structure from the space of the domain of "map".
11313 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11315 if (!map)
11316 return NULL;
11318 if (!map->dim->nested[0])
11319 return map;
11321 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11324 /* Remove any internal structure from the space of the range of "map".
11326 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11328 if (!map)
11329 return NULL;
11331 if (!map->dim->nested[1])
11332 return map;
11334 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11337 /* Reorder the dimensions of "bmap" according to the given dim_map
11338 * and set the dimension specification to "dim" and
11339 * perform Gaussian elimination on the result.
11341 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11342 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11344 isl_basic_map *res;
11345 unsigned flags;
11347 bmap = isl_basic_map_cow(bmap);
11348 if (!bmap || !dim || !dim_map)
11349 goto error;
11351 flags = bmap->flags;
11352 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11353 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11354 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11355 res = isl_basic_map_alloc_space(dim,
11356 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11357 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11358 if (res)
11359 res->flags = flags;
11360 res = isl_basic_map_gauss(res, NULL);
11361 res = isl_basic_map_finalize(res);
11362 return res;
11363 error:
11364 free(dim_map);
11365 isl_basic_map_free(bmap);
11366 isl_space_free(dim);
11367 return NULL;
11370 /* Reorder the dimensions of "map" according to given reordering.
11372 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11373 __isl_take isl_reordering *r)
11375 int i;
11376 struct isl_dim_map *dim_map;
11378 map = isl_map_cow(map);
11379 dim_map = isl_dim_map_from_reordering(r);
11380 if (!map || !r || !dim_map)
11381 goto error;
11383 for (i = 0; i < map->n; ++i) {
11384 struct isl_dim_map *dim_map_i;
11386 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11388 map->p[i] = isl_basic_map_realign(map->p[i],
11389 isl_space_copy(r->dim), dim_map_i);
11391 if (!map->p[i])
11392 goto error;
11395 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11397 isl_reordering_free(r);
11398 free(dim_map);
11399 return map;
11400 error:
11401 free(dim_map);
11402 isl_map_free(map);
11403 isl_reordering_free(r);
11404 return NULL;
11407 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11408 __isl_take isl_reordering *r)
11410 return (isl_set *)isl_map_realign((isl_map *)set, r);
11413 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11414 __isl_take isl_space *model)
11416 isl_ctx *ctx;
11418 if (!map || !model)
11419 goto error;
11421 ctx = isl_space_get_ctx(model);
11422 if (!isl_space_has_named_params(model))
11423 isl_die(ctx, isl_error_invalid,
11424 "model has unnamed parameters", goto error);
11425 if (!isl_space_has_named_params(map->dim))
11426 isl_die(ctx, isl_error_invalid,
11427 "relation has unnamed parameters", goto error);
11428 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11429 isl_reordering *exp;
11431 model = isl_space_drop_dims(model, isl_dim_in,
11432 0, isl_space_dim(model, isl_dim_in));
11433 model = isl_space_drop_dims(model, isl_dim_out,
11434 0, isl_space_dim(model, isl_dim_out));
11435 exp = isl_parameter_alignment_reordering(map->dim, model);
11436 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11437 map = isl_map_realign(map, exp);
11440 isl_space_free(model);
11441 return map;
11442 error:
11443 isl_space_free(model);
11444 isl_map_free(map);
11445 return NULL;
11448 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11449 __isl_take isl_space *model)
11451 return isl_map_align_params(set, model);
11454 /* Align the parameters of "bmap" to those of "model", introducing
11455 * additional parameters if needed.
11457 __isl_give isl_basic_map *isl_basic_map_align_params(
11458 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11460 isl_ctx *ctx;
11462 if (!bmap || !model)
11463 goto error;
11465 ctx = isl_space_get_ctx(model);
11466 if (!isl_space_has_named_params(model))
11467 isl_die(ctx, isl_error_invalid,
11468 "model has unnamed parameters", goto error);
11469 if (!isl_space_has_named_params(bmap->dim))
11470 isl_die(ctx, isl_error_invalid,
11471 "relation has unnamed parameters", goto error);
11472 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11473 isl_reordering *exp;
11474 struct isl_dim_map *dim_map;
11476 model = isl_space_drop_dims(model, isl_dim_in,
11477 0, isl_space_dim(model, isl_dim_in));
11478 model = isl_space_drop_dims(model, isl_dim_out,
11479 0, isl_space_dim(model, isl_dim_out));
11480 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11481 exp = isl_reordering_extend_space(exp,
11482 isl_basic_map_get_space(bmap));
11483 dim_map = isl_dim_map_from_reordering(exp);
11484 bmap = isl_basic_map_realign(bmap,
11485 exp ? isl_space_copy(exp->dim) : NULL,
11486 isl_dim_map_extend(dim_map, bmap));
11487 isl_reordering_free(exp);
11488 free(dim_map);
11491 isl_space_free(model);
11492 return bmap;
11493 error:
11494 isl_space_free(model);
11495 isl_basic_map_free(bmap);
11496 return NULL;
11499 /* Align the parameters of "bset" to those of "model", introducing
11500 * additional parameters if needed.
11502 __isl_give isl_basic_set *isl_basic_set_align_params(
11503 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11505 return isl_basic_map_align_params(bset, model);
11508 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11509 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11510 enum isl_dim_type c2, enum isl_dim_type c3,
11511 enum isl_dim_type c4, enum isl_dim_type c5)
11513 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11514 struct isl_mat *mat;
11515 int i, j, k;
11516 int pos;
11518 if (!bmap)
11519 return NULL;
11520 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11521 isl_basic_map_total_dim(bmap) + 1);
11522 if (!mat)
11523 return NULL;
11524 for (i = 0; i < bmap->n_eq; ++i)
11525 for (j = 0, pos = 0; j < 5; ++j) {
11526 int off = isl_basic_map_offset(bmap, c[j]);
11527 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11528 isl_int_set(mat->row[i][pos],
11529 bmap->eq[i][off + k]);
11530 ++pos;
11534 return mat;
11537 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11538 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11539 enum isl_dim_type c2, enum isl_dim_type c3,
11540 enum isl_dim_type c4, enum isl_dim_type c5)
11542 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11543 struct isl_mat *mat;
11544 int i, j, k;
11545 int pos;
11547 if (!bmap)
11548 return NULL;
11549 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11550 isl_basic_map_total_dim(bmap) + 1);
11551 if (!mat)
11552 return NULL;
11553 for (i = 0; i < bmap->n_ineq; ++i)
11554 for (j = 0, pos = 0; j < 5; ++j) {
11555 int off = isl_basic_map_offset(bmap, c[j]);
11556 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11557 isl_int_set(mat->row[i][pos],
11558 bmap->ineq[i][off + k]);
11559 ++pos;
11563 return mat;
11566 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11567 __isl_take isl_space *dim,
11568 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11569 enum isl_dim_type c2, enum isl_dim_type c3,
11570 enum isl_dim_type c4, enum isl_dim_type c5)
11572 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11573 isl_basic_map *bmap;
11574 unsigned total;
11575 unsigned extra;
11576 int i, j, k, l;
11577 int pos;
11579 if (!dim || !eq || !ineq)
11580 goto error;
11582 if (eq->n_col != ineq->n_col)
11583 isl_die(dim->ctx, isl_error_invalid,
11584 "equalities and inequalities matrices should have "
11585 "same number of columns", goto error);
11587 total = 1 + isl_space_dim(dim, isl_dim_all);
11589 if (eq->n_col < total)
11590 isl_die(dim->ctx, isl_error_invalid,
11591 "number of columns too small", goto error);
11593 extra = eq->n_col - total;
11595 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11596 eq->n_row, ineq->n_row);
11597 if (!bmap)
11598 goto error;
11599 for (i = 0; i < extra; ++i) {
11600 k = isl_basic_map_alloc_div(bmap);
11601 if (k < 0)
11602 goto error;
11603 isl_int_set_si(bmap->div[k][0], 0);
11605 for (i = 0; i < eq->n_row; ++i) {
11606 l = isl_basic_map_alloc_equality(bmap);
11607 if (l < 0)
11608 goto error;
11609 for (j = 0, pos = 0; j < 5; ++j) {
11610 int off = isl_basic_map_offset(bmap, c[j]);
11611 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11612 isl_int_set(bmap->eq[l][off + k],
11613 eq->row[i][pos]);
11614 ++pos;
11618 for (i = 0; i < ineq->n_row; ++i) {
11619 l = isl_basic_map_alloc_inequality(bmap);
11620 if (l < 0)
11621 goto error;
11622 for (j = 0, pos = 0; j < 5; ++j) {
11623 int off = isl_basic_map_offset(bmap, c[j]);
11624 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11625 isl_int_set(bmap->ineq[l][off + k],
11626 ineq->row[i][pos]);
11627 ++pos;
11632 isl_space_free(dim);
11633 isl_mat_free(eq);
11634 isl_mat_free(ineq);
11636 bmap = isl_basic_map_simplify(bmap);
11637 return isl_basic_map_finalize(bmap);
11638 error:
11639 isl_space_free(dim);
11640 isl_mat_free(eq);
11641 isl_mat_free(ineq);
11642 return NULL;
11645 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11646 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11647 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11649 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11650 c1, c2, c3, c4, isl_dim_in);
11653 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11654 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11655 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11657 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11658 c1, c2, c3, c4, isl_dim_in);
11661 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11662 __isl_take isl_space *dim,
11663 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11664 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11666 return (isl_basic_set*)
11667 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11668 c1, c2, c3, c4, isl_dim_in);
11671 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11673 if (!bmap)
11674 return isl_bool_error;
11676 return isl_space_can_zip(bmap->dim);
11679 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11681 if (!map)
11682 return isl_bool_error;
11684 return isl_space_can_zip(map->dim);
11687 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11688 * (A -> C) -> (B -> D).
11690 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11692 unsigned pos;
11693 unsigned n1;
11694 unsigned n2;
11696 if (!bmap)
11697 return NULL;
11699 if (!isl_basic_map_can_zip(bmap))
11700 isl_die(bmap->ctx, isl_error_invalid,
11701 "basic map cannot be zipped", goto error);
11702 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11703 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11704 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11705 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11706 bmap = isl_basic_map_cow(bmap);
11707 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11708 if (!bmap)
11709 return NULL;
11710 bmap->dim = isl_space_zip(bmap->dim);
11711 if (!bmap->dim)
11712 goto error;
11713 bmap = isl_basic_map_mark_final(bmap);
11714 return bmap;
11715 error:
11716 isl_basic_map_free(bmap);
11717 return NULL;
11720 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11721 * (A -> C) -> (B -> D).
11723 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11725 int i;
11727 if (!map)
11728 return NULL;
11730 if (!isl_map_can_zip(map))
11731 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11732 goto error);
11734 map = isl_map_cow(map);
11735 if (!map)
11736 return NULL;
11738 for (i = 0; i < map->n; ++i) {
11739 map->p[i] = isl_basic_map_zip(map->p[i]);
11740 if (!map->p[i])
11741 goto error;
11744 map->dim = isl_space_zip(map->dim);
11745 if (!map->dim)
11746 goto error;
11748 return map;
11749 error:
11750 isl_map_free(map);
11751 return NULL;
11754 /* Can we apply isl_basic_map_curry to "bmap"?
11755 * That is, does it have a nested relation in its domain?
11757 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11759 if (!bmap)
11760 return isl_bool_error;
11762 return isl_space_can_curry(bmap->dim);
11765 /* Can we apply isl_map_curry to "map"?
11766 * That is, does it have a nested relation in its domain?
11768 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11770 if (!map)
11771 return isl_bool_error;
11773 return isl_space_can_curry(map->dim);
11776 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11777 * A -> (B -> C).
11779 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11782 if (!bmap)
11783 return NULL;
11785 if (!isl_basic_map_can_curry(bmap))
11786 isl_die(bmap->ctx, isl_error_invalid,
11787 "basic map cannot be curried", goto error);
11788 bmap = isl_basic_map_cow(bmap);
11789 if (!bmap)
11790 return NULL;
11791 bmap->dim = isl_space_curry(bmap->dim);
11792 if (!bmap->dim)
11793 goto error;
11794 bmap = isl_basic_map_mark_final(bmap);
11795 return bmap;
11796 error:
11797 isl_basic_map_free(bmap);
11798 return NULL;
11801 /* Given a map (A -> B) -> C, return the corresponding map
11802 * A -> (B -> C).
11804 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11806 return isl_map_change_space(map, &isl_map_can_curry,
11807 "map cannot be curried", &isl_space_curry);
11810 /* Can isl_map_range_curry be applied to "map"?
11811 * That is, does it have a nested relation in its range,
11812 * the domain of which is itself a nested relation?
11814 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11816 if (!map)
11817 return isl_bool_error;
11819 return isl_space_can_range_curry(map->dim);
11822 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11823 * A -> (B -> (C -> D)).
11825 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11827 return isl_map_change_space(map, &isl_map_can_range_curry,
11828 "map range cannot be curried",
11829 &isl_space_range_curry);
11832 /* Can we apply isl_basic_map_uncurry to "bmap"?
11833 * That is, does it have a nested relation in its domain?
11835 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11837 if (!bmap)
11838 return isl_bool_error;
11840 return isl_space_can_uncurry(bmap->dim);
11843 /* Can we apply isl_map_uncurry to "map"?
11844 * That is, does it have a nested relation in its domain?
11846 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11848 if (!map)
11849 return isl_bool_error;
11851 return isl_space_can_uncurry(map->dim);
11854 /* Given a basic map A -> (B -> C), return the corresponding basic map
11855 * (A -> B) -> C.
11857 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11860 if (!bmap)
11861 return NULL;
11863 if (!isl_basic_map_can_uncurry(bmap))
11864 isl_die(bmap->ctx, isl_error_invalid,
11865 "basic map cannot be uncurried",
11866 return isl_basic_map_free(bmap));
11867 bmap = isl_basic_map_cow(bmap);
11868 if (!bmap)
11869 return NULL;
11870 bmap->dim = isl_space_uncurry(bmap->dim);
11871 if (!bmap->dim)
11872 return isl_basic_map_free(bmap);
11873 bmap = isl_basic_map_mark_final(bmap);
11874 return bmap;
11877 /* Given a map A -> (B -> C), return the corresponding map
11878 * (A -> B) -> C.
11880 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11882 return isl_map_change_space(map, &isl_map_can_uncurry,
11883 "map cannot be uncurried", &isl_space_uncurry);
11886 /* Construct a basic map mapping the domain of the affine expression
11887 * to a one-dimensional range prescribed by the affine expression.
11888 * If "rational" is set, then construct a rational basic map.
11890 * A NaN affine expression cannot be converted to a basic map.
11892 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11893 __isl_take isl_aff *aff, int rational)
11895 int k;
11896 int pos;
11897 isl_bool is_nan;
11898 isl_local_space *ls;
11899 isl_basic_map *bmap = NULL;
11901 if (!aff)
11902 return NULL;
11903 is_nan = isl_aff_is_nan(aff);
11904 if (is_nan < 0)
11905 goto error;
11906 if (is_nan)
11907 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11908 "cannot convert NaN", goto error);
11910 ls = isl_aff_get_local_space(aff);
11911 bmap = isl_basic_map_from_local_space(ls);
11912 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11913 k = isl_basic_map_alloc_equality(bmap);
11914 if (k < 0)
11915 goto error;
11917 pos = isl_basic_map_offset(bmap, isl_dim_out);
11918 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11919 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11920 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11921 aff->v->size - (pos + 1));
11923 isl_aff_free(aff);
11924 if (rational)
11925 bmap = isl_basic_map_set_rational(bmap);
11926 bmap = isl_basic_map_finalize(bmap);
11927 return bmap;
11928 error:
11929 isl_aff_free(aff);
11930 isl_basic_map_free(bmap);
11931 return NULL;
11934 /* Construct a basic map mapping the domain of the affine expression
11935 * to a one-dimensional range prescribed by the affine expression.
11937 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11939 return isl_basic_map_from_aff2(aff, 0);
11942 /* Construct a map mapping the domain of the affine expression
11943 * to a one-dimensional range prescribed by the affine expression.
11945 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11947 isl_basic_map *bmap;
11949 bmap = isl_basic_map_from_aff(aff);
11950 return isl_map_from_basic_map(bmap);
11953 /* Construct a basic map mapping the domain the multi-affine expression
11954 * to its range, with each dimension in the range equated to the
11955 * corresponding affine expression.
11956 * If "rational" is set, then construct a rational basic map.
11958 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11959 __isl_take isl_multi_aff *maff, int rational)
11961 int i;
11962 isl_space *space;
11963 isl_basic_map *bmap;
11965 if (!maff)
11966 return NULL;
11968 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11969 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11970 "invalid space", goto error);
11972 space = isl_space_domain(isl_multi_aff_get_space(maff));
11973 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11974 if (rational)
11975 bmap = isl_basic_map_set_rational(bmap);
11977 for (i = 0; i < maff->n; ++i) {
11978 isl_aff *aff;
11979 isl_basic_map *bmap_i;
11981 aff = isl_aff_copy(maff->p[i]);
11982 bmap_i = isl_basic_map_from_aff2(aff, rational);
11984 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11987 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11989 isl_multi_aff_free(maff);
11990 return bmap;
11991 error:
11992 isl_multi_aff_free(maff);
11993 return NULL;
11996 /* Construct a basic map mapping the domain the multi-affine expression
11997 * to its range, with each dimension in the range equated to the
11998 * corresponding affine expression.
12000 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12001 __isl_take isl_multi_aff *ma)
12003 return isl_basic_map_from_multi_aff2(ma, 0);
12006 /* Construct a map mapping the domain the multi-affine expression
12007 * to its range, with each dimension in the range equated to the
12008 * corresponding affine expression.
12010 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12012 isl_basic_map *bmap;
12014 bmap = isl_basic_map_from_multi_aff(maff);
12015 return isl_map_from_basic_map(bmap);
12018 /* Construct a basic map mapping a domain in the given space to
12019 * to an n-dimensional range, with n the number of elements in the list,
12020 * where each coordinate in the range is prescribed by the
12021 * corresponding affine expression.
12022 * The domains of all affine expressions in the list are assumed to match
12023 * domain_dim.
12025 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12026 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12028 int i;
12029 isl_space *dim;
12030 isl_basic_map *bmap;
12032 if (!list)
12033 return NULL;
12035 dim = isl_space_from_domain(domain_dim);
12036 bmap = isl_basic_map_universe(dim);
12038 for (i = 0; i < list->n; ++i) {
12039 isl_aff *aff;
12040 isl_basic_map *bmap_i;
12042 aff = isl_aff_copy(list->p[i]);
12043 bmap_i = isl_basic_map_from_aff(aff);
12045 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12048 isl_aff_list_free(list);
12049 return bmap;
12052 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12053 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12055 return isl_map_equate(set, type1, pos1, type2, pos2);
12058 /* Construct a basic map where the given dimensions are equal to each other.
12060 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12061 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12063 isl_basic_map *bmap = NULL;
12064 int i;
12066 if (!space)
12067 return NULL;
12069 if (pos1 >= isl_space_dim(space, type1))
12070 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12071 "index out of bounds", goto error);
12072 if (pos2 >= isl_space_dim(space, type2))
12073 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12074 "index out of bounds", goto error);
12076 if (type1 == type2 && pos1 == pos2)
12077 return isl_basic_map_universe(space);
12079 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12080 i = isl_basic_map_alloc_equality(bmap);
12081 if (i < 0)
12082 goto error;
12083 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12084 pos1 += isl_basic_map_offset(bmap, type1);
12085 pos2 += isl_basic_map_offset(bmap, type2);
12086 isl_int_set_si(bmap->eq[i][pos1], -1);
12087 isl_int_set_si(bmap->eq[i][pos2], 1);
12088 bmap = isl_basic_map_finalize(bmap);
12089 isl_space_free(space);
12090 return bmap;
12091 error:
12092 isl_space_free(space);
12093 isl_basic_map_free(bmap);
12094 return NULL;
12097 /* Add a constraint imposing that the given two dimensions are equal.
12099 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12100 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12102 isl_basic_map *eq;
12104 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12106 bmap = isl_basic_map_intersect(bmap, eq);
12108 return bmap;
12111 /* Add a constraint imposing that the given two dimensions are equal.
12113 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12114 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12116 isl_basic_map *bmap;
12118 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12120 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12122 return map;
12125 /* Add a constraint imposing that the given two dimensions have opposite values.
12127 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12128 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12130 isl_basic_map *bmap = NULL;
12131 int i;
12133 if (!map)
12134 return NULL;
12136 if (pos1 >= isl_map_dim(map, type1))
12137 isl_die(map->ctx, isl_error_invalid,
12138 "index out of bounds", goto error);
12139 if (pos2 >= isl_map_dim(map, type2))
12140 isl_die(map->ctx, isl_error_invalid,
12141 "index out of bounds", goto error);
12143 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12144 i = isl_basic_map_alloc_equality(bmap);
12145 if (i < 0)
12146 goto error;
12147 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12148 pos1 += isl_basic_map_offset(bmap, type1);
12149 pos2 += isl_basic_map_offset(bmap, type2);
12150 isl_int_set_si(bmap->eq[i][pos1], 1);
12151 isl_int_set_si(bmap->eq[i][pos2], 1);
12152 bmap = isl_basic_map_finalize(bmap);
12154 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12156 return map;
12157 error:
12158 isl_basic_map_free(bmap);
12159 isl_map_free(map);
12160 return NULL;
12163 /* Construct a constraint imposing that the value of the first dimension is
12164 * greater than or equal to that of the second.
12166 static __isl_give isl_constraint *constraint_order_ge(
12167 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12168 enum isl_dim_type type2, int pos2)
12170 isl_constraint *c;
12172 if (!space)
12173 return NULL;
12175 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12177 if (pos1 >= isl_constraint_dim(c, type1))
12178 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12179 "index out of bounds", return isl_constraint_free(c));
12180 if (pos2 >= isl_constraint_dim(c, type2))
12181 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12182 "index out of bounds", return isl_constraint_free(c));
12184 if (type1 == type2 && pos1 == pos2)
12185 return c;
12187 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12188 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12190 return c;
12193 /* Add a constraint imposing that the value of the first dimension is
12194 * greater than or equal to that of the second.
12196 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12197 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12199 isl_constraint *c;
12200 isl_space *space;
12202 if (type1 == type2 && pos1 == pos2)
12203 return bmap;
12204 space = isl_basic_map_get_space(bmap);
12205 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12206 bmap = isl_basic_map_add_constraint(bmap, c);
12208 return bmap;
12211 /* Add a constraint imposing that the value of the first dimension is
12212 * greater than or equal to that of the second.
12214 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12215 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12217 isl_constraint *c;
12218 isl_space *space;
12220 if (type1 == type2 && pos1 == pos2)
12221 return map;
12222 space = isl_map_get_space(map);
12223 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12224 map = isl_map_add_constraint(map, c);
12226 return map;
12229 /* Add a constraint imposing that the value of the first dimension is
12230 * less than or equal to that of the second.
12232 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12233 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12235 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12238 /* Construct a basic map where the value of the first dimension is
12239 * greater than that of the second.
12241 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12244 isl_basic_map *bmap = NULL;
12245 int i;
12247 if (!space)
12248 return NULL;
12250 if (pos1 >= isl_space_dim(space, type1))
12251 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12252 "index out of bounds", goto error);
12253 if (pos2 >= isl_space_dim(space, type2))
12254 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12255 "index out of bounds", goto error);
12257 if (type1 == type2 && pos1 == pos2)
12258 return isl_basic_map_empty(space);
12260 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12261 i = isl_basic_map_alloc_inequality(bmap);
12262 if (i < 0)
12263 return isl_basic_map_free(bmap);
12264 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12265 pos1 += isl_basic_map_offset(bmap, type1);
12266 pos2 += isl_basic_map_offset(bmap, type2);
12267 isl_int_set_si(bmap->ineq[i][pos1], 1);
12268 isl_int_set_si(bmap->ineq[i][pos2], -1);
12269 isl_int_set_si(bmap->ineq[i][0], -1);
12270 bmap = isl_basic_map_finalize(bmap);
12272 return bmap;
12273 error:
12274 isl_space_free(space);
12275 isl_basic_map_free(bmap);
12276 return NULL;
12279 /* Add a constraint imposing that the value of the first dimension is
12280 * greater than that of the second.
12282 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12283 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12285 isl_basic_map *gt;
12287 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12289 bmap = isl_basic_map_intersect(bmap, gt);
12291 return bmap;
12294 /* Add a constraint imposing that the value of the first dimension is
12295 * greater than that of the second.
12297 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12298 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12300 isl_basic_map *bmap;
12302 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12304 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12306 return map;
12309 /* Add a constraint imposing that the value of the first dimension is
12310 * smaller than that of the second.
12312 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12313 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12315 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12318 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12319 int pos)
12321 isl_aff *div;
12322 isl_local_space *ls;
12324 if (!bmap)
12325 return NULL;
12327 if (!isl_basic_map_divs_known(bmap))
12328 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12329 "some divs are unknown", return NULL);
12331 ls = isl_basic_map_get_local_space(bmap);
12332 div = isl_local_space_get_div(ls, pos);
12333 isl_local_space_free(ls);
12335 return div;
12338 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12339 int pos)
12341 return isl_basic_map_get_div(bset, pos);
12344 /* Plug in "subs" for dimension "type", "pos" of "bset".
12346 * Let i be the dimension to replace and let "subs" be of the form
12348 * f/d
12350 * Any integer division with a non-zero coefficient for i,
12352 * floor((a i + g)/m)
12354 * is replaced by
12356 * floor((a f + d g)/(m d))
12358 * Constraints of the form
12360 * a i + g
12362 * are replaced by
12364 * a f + d g
12366 * We currently require that "subs" is an integral expression.
12367 * Handling rational expressions may require us to add stride constraints
12368 * as we do in isl_basic_set_preimage_multi_aff.
12370 __isl_give isl_basic_set *isl_basic_set_substitute(
12371 __isl_take isl_basic_set *bset,
12372 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12374 int i;
12375 isl_int v;
12376 isl_ctx *ctx;
12378 if (bset && isl_basic_set_plain_is_empty(bset))
12379 return bset;
12381 bset = isl_basic_set_cow(bset);
12382 if (!bset || !subs)
12383 goto error;
12385 ctx = isl_basic_set_get_ctx(bset);
12386 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12387 isl_die(ctx, isl_error_invalid,
12388 "spaces don't match", goto error);
12389 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12390 isl_die(ctx, isl_error_unsupported,
12391 "cannot handle divs yet", goto error);
12392 if (!isl_int_is_one(subs->v->el[0]))
12393 isl_die(ctx, isl_error_invalid,
12394 "can only substitute integer expressions", goto error);
12396 pos += isl_basic_set_offset(bset, type);
12398 isl_int_init(v);
12400 for (i = 0; i < bset->n_eq; ++i) {
12401 if (isl_int_is_zero(bset->eq[i][pos]))
12402 continue;
12403 isl_int_set(v, bset->eq[i][pos]);
12404 isl_int_set_si(bset->eq[i][pos], 0);
12405 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12406 v, subs->v->el + 1, subs->v->size - 1);
12409 for (i = 0; i < bset->n_ineq; ++i) {
12410 if (isl_int_is_zero(bset->ineq[i][pos]))
12411 continue;
12412 isl_int_set(v, bset->ineq[i][pos]);
12413 isl_int_set_si(bset->ineq[i][pos], 0);
12414 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12415 v, subs->v->el + 1, subs->v->size - 1);
12418 for (i = 0; i < bset->n_div; ++i) {
12419 if (isl_int_is_zero(bset->div[i][1 + pos]))
12420 continue;
12421 isl_int_set(v, bset->div[i][1 + pos]);
12422 isl_int_set_si(bset->div[i][1 + pos], 0);
12423 isl_seq_combine(bset->div[i] + 1,
12424 subs->v->el[0], bset->div[i] + 1,
12425 v, subs->v->el + 1, subs->v->size - 1);
12426 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12429 isl_int_clear(v);
12431 bset = isl_basic_set_simplify(bset);
12432 return isl_basic_set_finalize(bset);
12433 error:
12434 isl_basic_set_free(bset);
12435 return NULL;
12438 /* Plug in "subs" for dimension "type", "pos" of "set".
12440 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12441 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12443 int i;
12445 if (set && isl_set_plain_is_empty(set))
12446 return set;
12448 set = isl_set_cow(set);
12449 if (!set || !subs)
12450 goto error;
12452 for (i = set->n - 1; i >= 0; --i) {
12453 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12454 if (remove_if_empty(set, i) < 0)
12455 goto error;
12458 return set;
12459 error:
12460 isl_set_free(set);
12461 return NULL;
12464 /* Check if the range of "ma" is compatible with the domain or range
12465 * (depending on "type") of "bmap".
12466 * Return -1 if anything is wrong.
12468 static int check_basic_map_compatible_range_multi_aff(
12469 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12470 __isl_keep isl_multi_aff *ma)
12472 int m;
12473 isl_space *ma_space;
12475 ma_space = isl_multi_aff_get_space(ma);
12477 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12478 if (m < 0)
12479 goto error;
12480 if (!m)
12481 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12482 "parameters don't match", goto error);
12483 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12484 if (m < 0)
12485 goto error;
12486 if (!m)
12487 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12488 "spaces don't match", goto error);
12490 isl_space_free(ma_space);
12491 return m;
12492 error:
12493 isl_space_free(ma_space);
12494 return -1;
12497 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12498 * coefficients before the transformed range of dimensions,
12499 * the "n_after" coefficients after the transformed range of dimensions
12500 * and the coefficients of the other divs in "bmap".
12502 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12503 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12505 int i;
12506 int n_param;
12507 int n_set;
12508 isl_local_space *ls;
12510 if (n_div == 0)
12511 return 0;
12513 ls = isl_aff_get_domain_local_space(ma->p[0]);
12514 if (!ls)
12515 return -1;
12517 n_param = isl_local_space_dim(ls, isl_dim_param);
12518 n_set = isl_local_space_dim(ls, isl_dim_set);
12519 for (i = 0; i < n_div; ++i) {
12520 int o_bmap = 0, o_ls = 0;
12522 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12523 o_bmap += 1 + 1 + n_param;
12524 o_ls += 1 + 1 + n_param;
12525 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12526 o_bmap += n_before;
12527 isl_seq_cpy(bmap->div[i] + o_bmap,
12528 ls->div->row[i] + o_ls, n_set);
12529 o_bmap += n_set;
12530 o_ls += n_set;
12531 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12532 o_bmap += n_after;
12533 isl_seq_cpy(bmap->div[i] + o_bmap,
12534 ls->div->row[i] + o_ls, n_div);
12535 o_bmap += n_div;
12536 o_ls += n_div;
12537 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12538 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12539 goto error;
12542 isl_local_space_free(ls);
12543 return 0;
12544 error:
12545 isl_local_space_free(ls);
12546 return -1;
12549 /* How many stride constraints does "ma" enforce?
12550 * That is, how many of the affine expressions have a denominator
12551 * different from one?
12553 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12555 int i;
12556 int strides = 0;
12558 for (i = 0; i < ma->n; ++i)
12559 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12560 strides++;
12562 return strides;
12565 /* For each affine expression in ma of the form
12567 * x_i = (f_i y + h_i)/m_i
12569 * with m_i different from one, add a constraint to "bmap"
12570 * of the form
12572 * f_i y + h_i = m_i alpha_i
12574 * with alpha_i an additional existentially quantified variable.
12576 * The input variables of "ma" correspond to a subset of the variables
12577 * of "bmap". There are "n_before" variables in "bmap" before this
12578 * subset and "n_after" variables after this subset.
12579 * The integer divisions of the affine expressions in "ma" are assumed
12580 * to have been aligned. There are "n_div_ma" of them and
12581 * they appear first in "bmap", straight after the "n_after" variables.
12583 static __isl_give isl_basic_map *add_ma_strides(
12584 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12585 int n_before, int n_after, int n_div_ma)
12587 int i, k;
12588 int div;
12589 int total;
12590 int n_param;
12591 int n_in;
12593 total = isl_basic_map_total_dim(bmap);
12594 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12595 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12596 for (i = 0; i < ma->n; ++i) {
12597 int o_bmap = 0, o_ma = 1;
12599 if (isl_int_is_one(ma->p[i]->v->el[0]))
12600 continue;
12601 div = isl_basic_map_alloc_div(bmap);
12602 k = isl_basic_map_alloc_equality(bmap);
12603 if (div < 0 || k < 0)
12604 goto error;
12605 isl_int_set_si(bmap->div[div][0], 0);
12606 isl_seq_cpy(bmap->eq[k] + o_bmap,
12607 ma->p[i]->v->el + o_ma, 1 + n_param);
12608 o_bmap += 1 + n_param;
12609 o_ma += 1 + n_param;
12610 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12611 o_bmap += n_before;
12612 isl_seq_cpy(bmap->eq[k] + o_bmap,
12613 ma->p[i]->v->el + o_ma, n_in);
12614 o_bmap += n_in;
12615 o_ma += n_in;
12616 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12617 o_bmap += n_after;
12618 isl_seq_cpy(bmap->eq[k] + o_bmap,
12619 ma->p[i]->v->el + o_ma, n_div_ma);
12620 o_bmap += n_div_ma;
12621 o_ma += n_div_ma;
12622 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12623 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12624 total++;
12627 return bmap;
12628 error:
12629 isl_basic_map_free(bmap);
12630 return NULL;
12633 /* Replace the domain or range space (depending on "type) of "space" by "set".
12635 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12636 enum isl_dim_type type, __isl_take isl_space *set)
12638 if (type == isl_dim_in) {
12639 space = isl_space_range(space);
12640 space = isl_space_map_from_domain_and_range(set, space);
12641 } else {
12642 space = isl_space_domain(space);
12643 space = isl_space_map_from_domain_and_range(space, set);
12646 return space;
12649 /* Compute the preimage of the domain or range (depending on "type")
12650 * of "bmap" under the function represented by "ma".
12651 * In other words, plug in "ma" in the domain or range of "bmap".
12652 * The result is a basic map that lives in the same space as "bmap"
12653 * except that the domain or range has been replaced by
12654 * the domain space of "ma".
12656 * If bmap is represented by
12658 * A(p) + S u + B x + T v + C(divs) >= 0,
12660 * where u and x are input and output dimensions if type == isl_dim_out
12661 * while x and v are input and output dimensions if type == isl_dim_in,
12662 * and ma is represented by
12664 * x = D(p) + F(y) + G(divs')
12666 * then the result is
12668 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12670 * The divs in the input set are similarly adjusted.
12671 * In particular
12673 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12675 * becomes
12677 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12678 * B_i G(divs') + c_i(divs))/n_i)
12680 * If bmap is not a rational map and if F(y) involves any denominators
12682 * x_i = (f_i y + h_i)/m_i
12684 * then additional constraints are added to ensure that we only
12685 * map back integer points. That is we enforce
12687 * f_i y + h_i = m_i alpha_i
12689 * with alpha_i an additional existentially quantified variable.
12691 * We first copy over the divs from "ma".
12692 * Then we add the modified constraints and divs from "bmap".
12693 * Finally, we add the stride constraints, if needed.
12695 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12696 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12697 __isl_take isl_multi_aff *ma)
12699 int i, k;
12700 isl_space *space;
12701 isl_basic_map *res = NULL;
12702 int n_before, n_after, n_div_bmap, n_div_ma;
12703 isl_int f, c1, c2, g;
12704 int rational, strides;
12706 isl_int_init(f);
12707 isl_int_init(c1);
12708 isl_int_init(c2);
12709 isl_int_init(g);
12711 ma = isl_multi_aff_align_divs(ma);
12712 if (!bmap || !ma)
12713 goto error;
12714 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12715 goto error;
12717 if (type == isl_dim_in) {
12718 n_before = 0;
12719 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12720 } else {
12721 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12722 n_after = 0;
12724 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12725 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12727 space = isl_multi_aff_get_domain_space(ma);
12728 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12729 rational = isl_basic_map_is_rational(bmap);
12730 strides = rational ? 0 : multi_aff_strides(ma);
12731 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12732 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12733 if (rational)
12734 res = isl_basic_map_set_rational(res);
12736 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12737 if (isl_basic_map_alloc_div(res) < 0)
12738 goto error;
12740 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12741 goto error;
12743 for (i = 0; i < bmap->n_eq; ++i) {
12744 k = isl_basic_map_alloc_equality(res);
12745 if (k < 0)
12746 goto error;
12747 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12748 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12751 for (i = 0; i < bmap->n_ineq; ++i) {
12752 k = isl_basic_map_alloc_inequality(res);
12753 if (k < 0)
12754 goto error;
12755 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12756 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12759 for (i = 0; i < bmap->n_div; ++i) {
12760 if (isl_int_is_zero(bmap->div[i][0])) {
12761 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12762 continue;
12764 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12765 n_before, n_after, n_div_ma, n_div_bmap,
12766 f, c1, c2, g, 1);
12769 if (strides)
12770 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12772 isl_int_clear(f);
12773 isl_int_clear(c1);
12774 isl_int_clear(c2);
12775 isl_int_clear(g);
12776 isl_basic_map_free(bmap);
12777 isl_multi_aff_free(ma);
12778 res = isl_basic_set_simplify(res);
12779 return isl_basic_map_finalize(res);
12780 error:
12781 isl_int_clear(f);
12782 isl_int_clear(c1);
12783 isl_int_clear(c2);
12784 isl_int_clear(g);
12785 isl_basic_map_free(bmap);
12786 isl_multi_aff_free(ma);
12787 isl_basic_map_free(res);
12788 return NULL;
12791 /* Compute the preimage of "bset" under the function represented by "ma".
12792 * In other words, plug in "ma" in "bset". The result is a basic set
12793 * that lives in the domain space of "ma".
12795 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12796 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12798 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12801 /* Compute the preimage of the domain of "bmap" under the function
12802 * represented by "ma".
12803 * In other words, plug in "ma" in the domain of "bmap".
12804 * The result is a basic map that lives in the same space as "bmap"
12805 * except that the domain has been replaced by the domain space of "ma".
12807 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12808 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12810 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12813 /* Compute the preimage of the range of "bmap" under the function
12814 * represented by "ma".
12815 * In other words, plug in "ma" in the range of "bmap".
12816 * The result is a basic map that lives in the same space as "bmap"
12817 * except that the range has been replaced by the domain space of "ma".
12819 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12820 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12822 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12825 /* Check if the range of "ma" is compatible with the domain or range
12826 * (depending on "type") of "map".
12827 * Return -1 if anything is wrong.
12829 static int check_map_compatible_range_multi_aff(
12830 __isl_keep isl_map *map, enum isl_dim_type type,
12831 __isl_keep isl_multi_aff *ma)
12833 int m;
12834 isl_space *ma_space;
12836 ma_space = isl_multi_aff_get_space(ma);
12837 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12838 isl_space_free(ma_space);
12839 if (m >= 0 && !m)
12840 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12841 "spaces don't match", return -1);
12842 return m;
12845 /* Compute the preimage of the domain or range (depending on "type")
12846 * of "map" under the function represented by "ma".
12847 * In other words, plug in "ma" in the domain or range of "map".
12848 * The result is a map that lives in the same space as "map"
12849 * except that the domain or range has been replaced by
12850 * the domain space of "ma".
12852 * The parameters are assumed to have been aligned.
12854 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12855 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12857 int i;
12858 isl_space *space;
12860 map = isl_map_cow(map);
12861 ma = isl_multi_aff_align_divs(ma);
12862 if (!map || !ma)
12863 goto error;
12864 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12865 goto error;
12867 for (i = 0; i < map->n; ++i) {
12868 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12869 isl_multi_aff_copy(ma));
12870 if (!map->p[i])
12871 goto error;
12874 space = isl_multi_aff_get_domain_space(ma);
12875 space = isl_space_set(isl_map_get_space(map), type, space);
12877 isl_space_free(map->dim);
12878 map->dim = space;
12879 if (!map->dim)
12880 goto error;
12882 isl_multi_aff_free(ma);
12883 if (map->n > 1)
12884 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12885 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12886 return map;
12887 error:
12888 isl_multi_aff_free(ma);
12889 isl_map_free(map);
12890 return NULL;
12893 /* Compute the preimage of the domain or range (depending on "type")
12894 * of "map" under the function represented by "ma".
12895 * In other words, plug in "ma" in the domain or range of "map".
12896 * The result is a map that lives in the same space as "map"
12897 * except that the domain or range has been replaced by
12898 * the domain space of "ma".
12900 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12901 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12903 if (!map || !ma)
12904 goto error;
12906 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12907 return map_preimage_multi_aff(map, type, ma);
12909 if (!isl_space_has_named_params(map->dim) ||
12910 !isl_space_has_named_params(ma->space))
12911 isl_die(map->ctx, isl_error_invalid,
12912 "unaligned unnamed parameters", goto error);
12913 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12914 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12916 return map_preimage_multi_aff(map, type, ma);
12917 error:
12918 isl_multi_aff_free(ma);
12919 return isl_map_free(map);
12922 /* Compute the preimage of "set" under the function represented by "ma".
12923 * In other words, plug in "ma" in "set". The result is a set
12924 * that lives in the domain space of "ma".
12926 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12927 __isl_take isl_multi_aff *ma)
12929 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12932 /* Compute the preimage of the domain of "map" under the function
12933 * represented by "ma".
12934 * In other words, plug in "ma" in the domain of "map".
12935 * The result is a map that lives in the same space as "map"
12936 * except that the domain has been replaced by the domain space of "ma".
12938 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12939 __isl_take isl_multi_aff *ma)
12941 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12944 /* Compute the preimage of the range of "map" under the function
12945 * represented by "ma".
12946 * In other words, plug in "ma" in the range of "map".
12947 * The result is a map that lives in the same space as "map"
12948 * except that the range has been replaced by the domain space of "ma".
12950 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12951 __isl_take isl_multi_aff *ma)
12953 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12956 /* Compute the preimage of "map" under the function represented by "pma".
12957 * In other words, plug in "pma" in the domain or range of "map".
12958 * The result is a map that lives in the same space as "map",
12959 * except that the space of type "type" has been replaced by
12960 * the domain space of "pma".
12962 * The parameters of "map" and "pma" are assumed to have been aligned.
12964 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12965 __isl_take isl_map *map, enum isl_dim_type type,
12966 __isl_take isl_pw_multi_aff *pma)
12968 int i;
12969 isl_map *res;
12971 if (!pma)
12972 goto error;
12974 if (pma->n == 0) {
12975 isl_pw_multi_aff_free(pma);
12976 res = isl_map_empty(isl_map_get_space(map));
12977 isl_map_free(map);
12978 return res;
12981 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12982 isl_multi_aff_copy(pma->p[0].maff));
12983 if (type == isl_dim_in)
12984 res = isl_map_intersect_domain(res,
12985 isl_map_copy(pma->p[0].set));
12986 else
12987 res = isl_map_intersect_range(res,
12988 isl_map_copy(pma->p[0].set));
12990 for (i = 1; i < pma->n; ++i) {
12991 isl_map *res_i;
12993 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12994 isl_multi_aff_copy(pma->p[i].maff));
12995 if (type == isl_dim_in)
12996 res_i = isl_map_intersect_domain(res_i,
12997 isl_map_copy(pma->p[i].set));
12998 else
12999 res_i = isl_map_intersect_range(res_i,
13000 isl_map_copy(pma->p[i].set));
13001 res = isl_map_union(res, res_i);
13004 isl_pw_multi_aff_free(pma);
13005 isl_map_free(map);
13006 return res;
13007 error:
13008 isl_pw_multi_aff_free(pma);
13009 isl_map_free(map);
13010 return NULL;
13013 /* Compute the preimage of "map" under the function represented by "pma".
13014 * In other words, plug in "pma" in the domain or range of "map".
13015 * The result is a map that lives in the same space as "map",
13016 * except that the space of type "type" has been replaced by
13017 * the domain space of "pma".
13019 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13020 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13022 if (!map || !pma)
13023 goto error;
13025 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13026 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13028 if (!isl_space_has_named_params(map->dim) ||
13029 !isl_space_has_named_params(pma->dim))
13030 isl_die(map->ctx, isl_error_invalid,
13031 "unaligned unnamed parameters", goto error);
13032 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13033 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13035 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13036 error:
13037 isl_pw_multi_aff_free(pma);
13038 return isl_map_free(map);
13041 /* Compute the preimage of "set" under the function represented by "pma".
13042 * In other words, plug in "pma" in "set". The result is a set
13043 * that lives in the domain space of "pma".
13045 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13046 __isl_take isl_pw_multi_aff *pma)
13048 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13051 /* Compute the preimage of the domain of "map" under the function
13052 * represented by "pma".
13053 * In other words, plug in "pma" in the domain of "map".
13054 * The result is a map that lives in the same space as "map",
13055 * except that domain space has been replaced by the domain space of "pma".
13057 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13058 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13060 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13063 /* Compute the preimage of the range of "map" under the function
13064 * represented by "pma".
13065 * In other words, plug in "pma" in the range of "map".
13066 * The result is a map that lives in the same space as "map",
13067 * except that range space has been replaced by the domain space of "pma".
13069 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13070 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13072 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13075 /* Compute the preimage of "map" under the function represented by "mpa".
13076 * In other words, plug in "mpa" in the domain or range of "map".
13077 * The result is a map that lives in the same space as "map",
13078 * except that the space of type "type" has been replaced by
13079 * the domain space of "mpa".
13081 * If the map does not involve any constraints that refer to the
13082 * dimensions of the substituted space, then the only possible
13083 * effect of "mpa" on the map is to map the space to a different space.
13084 * We create a separate isl_multi_aff to effectuate this change
13085 * in order to avoid spurious splitting of the map along the pieces
13086 * of "mpa".
13088 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13089 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13091 int n;
13092 isl_pw_multi_aff *pma;
13094 if (!map || !mpa)
13095 goto error;
13097 n = isl_map_dim(map, type);
13098 if (!isl_map_involves_dims(map, type, 0, n)) {
13099 isl_space *space;
13100 isl_multi_aff *ma;
13102 space = isl_multi_pw_aff_get_space(mpa);
13103 isl_multi_pw_aff_free(mpa);
13104 ma = isl_multi_aff_zero(space);
13105 return isl_map_preimage_multi_aff(map, type, ma);
13108 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13109 return isl_map_preimage_pw_multi_aff(map, type, pma);
13110 error:
13111 isl_map_free(map);
13112 isl_multi_pw_aff_free(mpa);
13113 return NULL;
13116 /* Compute the preimage of "map" under the function represented by "mpa".
13117 * In other words, plug in "mpa" in the domain "map".
13118 * The result is a map that lives in the same space as "map",
13119 * except that domain space has been replaced by the domain space of "mpa".
13121 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13122 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13124 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13127 /* Compute the preimage of "set" by the function represented by "mpa".
13128 * In other words, plug in "mpa" in "set".
13130 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13131 __isl_take isl_multi_pw_aff *mpa)
13133 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);