isl_vertices.c: tab_for_shifted_cone: allocate room for equality constraints
[isl.git] / isl_map.c
blobf2d71920562d79108bce4010f54a64be0371387a
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 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210 struct isl_basic_set *bset)
212 int m;
213 if (!bmap || !bset)
214 return -1;
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-(n-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 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
821 return isl_basic_map_is_rational(bset);
824 /* Does "bmap" contain any rational points?
826 * If "bmap" has an equality for each dimension, equating the dimension
827 * to an integer constant, then it has no rational points, even if it
828 * is marked as rational.
830 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
832 int has_rational = 1;
833 unsigned total;
835 if (!bmap)
836 return -1;
837 if (isl_basic_map_plain_is_empty(bmap))
838 return 0;
839 if (!isl_basic_map_is_rational(bmap))
840 return 0;
841 bmap = isl_basic_map_copy(bmap);
842 bmap = isl_basic_map_implicit_equalities(bmap);
843 if (!bmap)
844 return -1;
845 total = isl_basic_map_total_dim(bmap);
846 if (bmap->n_eq == total) {
847 int i, j;
848 for (i = 0; i < bmap->n_eq; ++i) {
849 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
850 if (j < 0)
851 break;
852 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
853 !isl_int_is_negone(bmap->eq[i][1 + j]))
854 break;
855 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
856 total - j - 1);
857 if (j >= 0)
858 break;
860 if (i == bmap->n_eq)
861 has_rational = 0;
863 isl_basic_map_free(bmap);
865 return has_rational;
868 /* Does "map" contain any rational points?
870 int isl_map_has_rational(__isl_keep isl_map *map)
872 int i;
873 int has_rational;
875 if (!map)
876 return -1;
877 for (i = 0; i < map->n; ++i) {
878 has_rational = isl_basic_map_has_rational(map->p[i]);
879 if (has_rational < 0)
880 return -1;
881 if (has_rational)
882 return 1;
884 return 0;
887 /* Does "set" contain any rational points?
889 int isl_set_has_rational(__isl_keep isl_set *set)
891 return isl_map_has_rational(set);
894 /* Is this basic set a parameter domain?
896 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
898 if (!bset)
899 return -1;
900 return isl_space_is_params(bset->dim);
903 /* Is this set a parameter domain?
905 isl_bool isl_set_is_params(__isl_keep isl_set *set)
907 if (!set)
908 return isl_bool_error;
909 return isl_space_is_params(set->dim);
912 /* Is this map actually a parameter domain?
913 * Users should never call this function. Outside of isl,
914 * a map can never be a parameter domain.
916 int isl_map_is_params(__isl_keep isl_map *map)
918 if (!map)
919 return -1;
920 return isl_space_is_params(map->dim);
923 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
924 struct isl_basic_map *bmap, unsigned extra,
925 unsigned n_eq, unsigned n_ineq)
927 int i;
928 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
930 bmap->ctx = ctx;
931 isl_ctx_ref(ctx);
933 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
934 if (isl_blk_is_error(bmap->block))
935 goto error;
937 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
938 if ((n_ineq + n_eq) && !bmap->ineq)
939 goto error;
941 if (extra == 0) {
942 bmap->block2 = isl_blk_empty();
943 bmap->div = NULL;
944 } else {
945 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
946 if (isl_blk_is_error(bmap->block2))
947 goto error;
949 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
950 if (!bmap->div)
951 goto error;
954 for (i = 0; i < n_ineq + n_eq; ++i)
955 bmap->ineq[i] = bmap->block.data + i * row_size;
957 for (i = 0; i < extra; ++i)
958 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
960 bmap->ref = 1;
961 bmap->flags = 0;
962 bmap->c_size = n_eq + n_ineq;
963 bmap->eq = bmap->ineq + n_ineq;
964 bmap->extra = extra;
965 bmap->n_eq = 0;
966 bmap->n_ineq = 0;
967 bmap->n_div = 0;
968 bmap->sample = NULL;
970 return bmap;
971 error:
972 isl_basic_map_free(bmap);
973 return NULL;
976 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
977 unsigned nparam, unsigned dim, unsigned extra,
978 unsigned n_eq, unsigned n_ineq)
980 struct isl_basic_map *bmap;
981 isl_space *space;
983 space = isl_space_set_alloc(ctx, nparam, dim);
984 if (!space)
985 return NULL;
987 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
988 return (struct isl_basic_set *)bmap;
991 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
992 unsigned extra, unsigned n_eq, unsigned n_ineq)
994 struct isl_basic_map *bmap;
995 if (!dim)
996 return NULL;
997 isl_assert(dim->ctx, dim->n_in == 0, goto error);
998 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
999 return (struct isl_basic_set *)bmap;
1000 error:
1001 isl_space_free(dim);
1002 return NULL;
1005 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1006 unsigned extra, unsigned n_eq, unsigned n_ineq)
1008 struct isl_basic_map *bmap;
1010 if (!dim)
1011 return NULL;
1012 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1013 if (!bmap)
1014 goto error;
1015 bmap->dim = dim;
1017 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1018 error:
1019 isl_space_free(dim);
1020 return NULL;
1023 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1024 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1025 unsigned n_eq, unsigned n_ineq)
1027 struct isl_basic_map *bmap;
1028 isl_space *dim;
1030 dim = isl_space_alloc(ctx, nparam, in, out);
1031 if (!dim)
1032 return NULL;
1034 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1035 return bmap;
1038 static void dup_constraints(
1039 struct isl_basic_map *dst, struct isl_basic_map *src)
1041 int i;
1042 unsigned total = isl_basic_map_total_dim(src);
1044 for (i = 0; i < src->n_eq; ++i) {
1045 int j = isl_basic_map_alloc_equality(dst);
1046 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1049 for (i = 0; i < src->n_ineq; ++i) {
1050 int j = isl_basic_map_alloc_inequality(dst);
1051 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1054 for (i = 0; i < src->n_div; ++i) {
1055 int j = isl_basic_map_alloc_div(dst);
1056 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1058 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1061 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1063 struct isl_basic_map *dup;
1065 if (!bmap)
1066 return NULL;
1067 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1068 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1069 if (!dup)
1070 return NULL;
1071 dup_constraints(dup, bmap);
1072 dup->flags = bmap->flags;
1073 dup->sample = isl_vec_copy(bmap->sample);
1074 return dup;
1077 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1079 struct isl_basic_map *dup;
1081 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1082 return (struct isl_basic_set *)dup;
1085 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1087 if (!bset)
1088 return NULL;
1090 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1091 bset->ref++;
1092 return bset;
1094 return isl_basic_set_dup(bset);
1097 struct isl_set *isl_set_copy(struct isl_set *set)
1099 if (!set)
1100 return NULL;
1102 set->ref++;
1103 return set;
1106 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1108 if (!bmap)
1109 return NULL;
1111 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1112 bmap->ref++;
1113 return bmap;
1115 bmap = isl_basic_map_dup(bmap);
1116 if (bmap)
1117 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1118 return bmap;
1121 struct isl_map *isl_map_copy(struct isl_map *map)
1123 if (!map)
1124 return NULL;
1126 map->ref++;
1127 return map;
1130 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1132 if (!bmap)
1133 return NULL;
1135 if (--bmap->ref > 0)
1136 return NULL;
1138 isl_ctx_deref(bmap->ctx);
1139 free(bmap->div);
1140 isl_blk_free(bmap->ctx, bmap->block2);
1141 free(bmap->ineq);
1142 isl_blk_free(bmap->ctx, bmap->block);
1143 isl_vec_free(bmap->sample);
1144 isl_space_free(bmap->dim);
1145 free(bmap);
1147 return NULL;
1150 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1152 return isl_basic_map_free((struct isl_basic_map *)bset);
1155 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1157 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1160 __isl_give isl_map *isl_map_align_params_map_map_and(
1161 __isl_take isl_map *map1, __isl_take isl_map *map2,
1162 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1163 __isl_take isl_map *map2))
1165 if (!map1 || !map2)
1166 goto error;
1167 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1168 return fn(map1, map2);
1169 if (!isl_space_has_named_params(map1->dim) ||
1170 !isl_space_has_named_params(map2->dim))
1171 isl_die(map1->ctx, isl_error_invalid,
1172 "unaligned unnamed parameters", goto error);
1173 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1174 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1175 return fn(map1, map2);
1176 error:
1177 isl_map_free(map1);
1178 isl_map_free(map2);
1179 return NULL;
1182 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1183 __isl_keep isl_map *map2,
1184 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1186 isl_bool r;
1188 if (!map1 || !map2)
1189 return isl_bool_error;
1190 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1191 return fn(map1, map2);
1192 if (!isl_space_has_named_params(map1->dim) ||
1193 !isl_space_has_named_params(map2->dim))
1194 isl_die(map1->ctx, isl_error_invalid,
1195 "unaligned unnamed parameters", return isl_bool_error);
1196 map1 = isl_map_copy(map1);
1197 map2 = isl_map_copy(map2);
1198 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1199 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1200 r = fn(map1, map2);
1201 isl_map_free(map1);
1202 isl_map_free(map2);
1203 return r;
1206 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1208 struct isl_ctx *ctx;
1209 if (!bmap)
1210 return -1;
1211 ctx = bmap->ctx;
1212 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1213 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1214 return -1);
1215 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1216 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1217 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1218 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1219 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1220 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1221 isl_int *t;
1222 int j = isl_basic_map_alloc_inequality(bmap);
1223 if (j < 0)
1224 return -1;
1225 t = bmap->ineq[j];
1226 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1227 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1228 bmap->eq[-1] = t;
1229 bmap->n_eq++;
1230 bmap->n_ineq--;
1231 bmap->eq--;
1232 return 0;
1234 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1235 bmap->extra - bmap->n_div);
1236 return bmap->n_eq++;
1239 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1241 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1244 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1246 if (!bmap)
1247 return -1;
1248 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1249 bmap->n_eq -= n;
1250 return 0;
1253 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1255 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1258 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1260 isl_int *t;
1261 if (!bmap)
1262 return -1;
1263 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1265 if (pos != bmap->n_eq - 1) {
1266 t = bmap->eq[pos];
1267 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1268 bmap->eq[bmap->n_eq - 1] = t;
1270 bmap->n_eq--;
1271 return 0;
1274 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1276 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1279 /* Turn inequality "pos" of "bmap" into an equality.
1281 * In particular, we move the inequality in front of the equalities
1282 * and move the last inequality in the position of the moved inequality.
1283 * Note that isl_tab_make_equalities_explicit depends on this particular
1284 * change in the ordering of the constraints.
1286 void isl_basic_map_inequality_to_equality(
1287 struct isl_basic_map *bmap, unsigned pos)
1289 isl_int *t;
1291 t = bmap->ineq[pos];
1292 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1293 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1294 bmap->eq[-1] = t;
1295 bmap->n_eq++;
1296 bmap->n_ineq--;
1297 bmap->eq--;
1298 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1299 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1300 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1301 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1304 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1306 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1309 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1311 struct isl_ctx *ctx;
1312 if (!bmap)
1313 return -1;
1314 ctx = bmap->ctx;
1315 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1316 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1317 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1318 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1319 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1320 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1321 1 + isl_basic_map_total_dim(bmap),
1322 bmap->extra - bmap->n_div);
1323 return bmap->n_ineq++;
1326 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1328 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1331 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1333 if (!bmap)
1334 return -1;
1335 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1336 bmap->n_ineq -= n;
1337 return 0;
1340 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1342 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1345 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1347 isl_int *t;
1348 if (!bmap)
1349 return -1;
1350 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1352 if (pos != bmap->n_ineq - 1) {
1353 t = bmap->ineq[pos];
1354 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1355 bmap->ineq[bmap->n_ineq - 1] = t;
1356 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1358 bmap->n_ineq--;
1359 return 0;
1362 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1364 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1367 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1368 isl_int *eq)
1370 int k;
1372 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1373 if (!bmap)
1374 return NULL;
1375 k = isl_basic_map_alloc_equality(bmap);
1376 if (k < 0)
1377 goto error;
1378 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1379 return bmap;
1380 error:
1381 isl_basic_map_free(bmap);
1382 return NULL;
1385 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1386 isl_int *eq)
1388 return (isl_basic_set *)
1389 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1392 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1393 isl_int *ineq)
1395 int k;
1397 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1398 if (!bmap)
1399 return NULL;
1400 k = isl_basic_map_alloc_inequality(bmap);
1401 if (k < 0)
1402 goto error;
1403 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1404 return bmap;
1405 error:
1406 isl_basic_map_free(bmap);
1407 return NULL;
1410 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1411 isl_int *ineq)
1413 return (isl_basic_set *)
1414 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1417 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1419 if (!bmap)
1420 return -1;
1421 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1422 isl_seq_clr(bmap->div[bmap->n_div] +
1423 1 + 1 + isl_basic_map_total_dim(bmap),
1424 bmap->extra - bmap->n_div);
1425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1426 return bmap->n_div++;
1429 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1431 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1434 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1436 if (!bmap)
1437 return -1;
1438 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1439 bmap->n_div -= n;
1440 return 0;
1443 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1445 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1448 /* Copy constraint from src to dst, putting the vars of src at offset
1449 * dim_off in dst and the divs of src at offset div_off in dst.
1450 * If both sets are actually map, then dim_off applies to the input
1451 * variables.
1453 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1454 struct isl_basic_map *src_map, isl_int *src,
1455 unsigned in_off, unsigned out_off, unsigned div_off)
1457 unsigned src_nparam = isl_basic_map_n_param(src_map);
1458 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1459 unsigned src_in = isl_basic_map_n_in(src_map);
1460 unsigned dst_in = isl_basic_map_n_in(dst_map);
1461 unsigned src_out = isl_basic_map_n_out(src_map);
1462 unsigned dst_out = isl_basic_map_n_out(dst_map);
1463 isl_int_set(dst[0], src[0]);
1464 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1465 if (dst_nparam > src_nparam)
1466 isl_seq_clr(dst+1+src_nparam,
1467 dst_nparam - src_nparam);
1468 isl_seq_clr(dst+1+dst_nparam, in_off);
1469 isl_seq_cpy(dst+1+dst_nparam+in_off,
1470 src+1+src_nparam,
1471 isl_min(dst_in-in_off, src_in));
1472 if (dst_in-in_off > src_in)
1473 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1474 dst_in - in_off - src_in);
1475 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1476 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1477 src+1+src_nparam+src_in,
1478 isl_min(dst_out-out_off, src_out));
1479 if (dst_out-out_off > src_out)
1480 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1481 dst_out - out_off - src_out);
1482 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1483 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1484 src+1+src_nparam+src_in+src_out,
1485 isl_min(dst_map->extra-div_off, src_map->n_div));
1486 if (dst_map->n_div-div_off > src_map->n_div)
1487 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1488 div_off+src_map->n_div,
1489 dst_map->n_div - div_off - src_map->n_div);
1492 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1493 struct isl_basic_map *src_map, isl_int *src,
1494 unsigned in_off, unsigned out_off, unsigned div_off)
1496 isl_int_set(dst[0], src[0]);
1497 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1500 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1501 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1503 int i;
1504 unsigned div_off;
1506 if (!bmap1 || !bmap2)
1507 goto error;
1509 div_off = bmap1->n_div;
1511 for (i = 0; i < bmap2->n_eq; ++i) {
1512 int i1 = isl_basic_map_alloc_equality(bmap1);
1513 if (i1 < 0)
1514 goto error;
1515 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1516 i_pos, o_pos, div_off);
1519 for (i = 0; i < bmap2->n_ineq; ++i) {
1520 int i1 = isl_basic_map_alloc_inequality(bmap1);
1521 if (i1 < 0)
1522 goto error;
1523 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1524 i_pos, o_pos, div_off);
1527 for (i = 0; i < bmap2->n_div; ++i) {
1528 int i1 = isl_basic_map_alloc_div(bmap1);
1529 if (i1 < 0)
1530 goto error;
1531 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1532 i_pos, o_pos, div_off);
1535 isl_basic_map_free(bmap2);
1537 return bmap1;
1539 error:
1540 isl_basic_map_free(bmap1);
1541 isl_basic_map_free(bmap2);
1542 return NULL;
1545 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1546 struct isl_basic_set *bset2, unsigned pos)
1548 return (struct isl_basic_set *)
1549 add_constraints((struct isl_basic_map *)bset1,
1550 (struct isl_basic_map *)bset2, 0, pos);
1553 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1554 __isl_take isl_space *dim, unsigned extra,
1555 unsigned n_eq, unsigned n_ineq)
1557 struct isl_basic_map *ext;
1558 unsigned flags;
1559 int dims_ok;
1561 if (!dim)
1562 goto error;
1564 if (!base)
1565 goto error;
1567 dims_ok = isl_space_is_equal(base->dim, dim) &&
1568 base->extra >= base->n_div + extra;
1570 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1571 room_for_ineq(base, n_ineq)) {
1572 isl_space_free(dim);
1573 return base;
1576 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1577 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1578 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1579 extra += base->extra;
1580 n_eq += base->n_eq;
1581 n_ineq += base->n_ineq;
1583 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1584 dim = NULL;
1585 if (!ext)
1586 goto error;
1588 if (dims_ok)
1589 ext->sample = isl_vec_copy(base->sample);
1590 flags = base->flags;
1591 ext = add_constraints(ext, base, 0, 0);
1592 if (ext) {
1593 ext->flags = flags;
1594 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1597 return ext;
1599 error:
1600 isl_space_free(dim);
1601 isl_basic_map_free(base);
1602 return NULL;
1605 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1606 __isl_take isl_space *dim, unsigned extra,
1607 unsigned n_eq, unsigned n_ineq)
1609 return (struct isl_basic_set *)
1610 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1611 extra, n_eq, n_ineq);
1614 struct isl_basic_map *isl_basic_map_extend_constraints(
1615 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1617 if (!base)
1618 return NULL;
1619 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1620 0, n_eq, n_ineq);
1623 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1624 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1625 unsigned n_eq, unsigned n_ineq)
1627 struct isl_basic_map *bmap;
1628 isl_space *dim;
1630 if (!base)
1631 return NULL;
1632 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1633 if (!dim)
1634 goto error;
1636 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1637 return bmap;
1638 error:
1639 isl_basic_map_free(base);
1640 return NULL;
1643 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1644 unsigned nparam, unsigned dim, unsigned extra,
1645 unsigned n_eq, unsigned n_ineq)
1647 return (struct isl_basic_set *)
1648 isl_basic_map_extend((struct isl_basic_map *)base,
1649 nparam, 0, dim, extra, n_eq, n_ineq);
1652 struct isl_basic_set *isl_basic_set_extend_constraints(
1653 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1655 return (struct isl_basic_set *)
1656 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1657 n_eq, n_ineq);
1660 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1662 return (struct isl_basic_set *)
1663 isl_basic_map_cow((struct isl_basic_map *)bset);
1666 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1668 if (!bmap)
1669 return NULL;
1671 if (bmap->ref > 1) {
1672 bmap->ref--;
1673 bmap = isl_basic_map_dup(bmap);
1675 if (bmap) {
1676 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1677 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1679 return bmap;
1682 struct isl_set *isl_set_cow(struct isl_set *set)
1684 if (!set)
1685 return NULL;
1687 if (set->ref == 1)
1688 return set;
1689 set->ref--;
1690 return isl_set_dup(set);
1693 struct isl_map *isl_map_cow(struct isl_map *map)
1695 if (!map)
1696 return NULL;
1698 if (map->ref == 1)
1699 return map;
1700 map->ref--;
1701 return isl_map_dup(map);
1704 static void swap_vars(struct isl_blk blk, isl_int *a,
1705 unsigned a_len, unsigned b_len)
1707 isl_seq_cpy(blk.data, a+a_len, b_len);
1708 isl_seq_cpy(blk.data+b_len, a, a_len);
1709 isl_seq_cpy(a, blk.data, b_len+a_len);
1712 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1713 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1715 int i;
1716 struct isl_blk blk;
1718 if (!bmap)
1719 goto error;
1721 isl_assert(bmap->ctx,
1722 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1724 if (n1 == 0 || n2 == 0)
1725 return bmap;
1727 bmap = isl_basic_map_cow(bmap);
1728 if (!bmap)
1729 return NULL;
1731 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1732 if (isl_blk_is_error(blk))
1733 goto error;
1735 for (i = 0; i < bmap->n_eq; ++i)
1736 swap_vars(blk,
1737 bmap->eq[i] + pos, n1, n2);
1739 for (i = 0; i < bmap->n_ineq; ++i)
1740 swap_vars(blk,
1741 bmap->ineq[i] + pos, n1, n2);
1743 for (i = 0; i < bmap->n_div; ++i)
1744 swap_vars(blk,
1745 bmap->div[i]+1 + pos, n1, n2);
1747 isl_blk_free(bmap->ctx, blk);
1749 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1750 bmap = isl_basic_map_gauss(bmap, NULL);
1751 return isl_basic_map_finalize(bmap);
1752 error:
1753 isl_basic_map_free(bmap);
1754 return NULL;
1757 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1759 int i = 0;
1760 unsigned total;
1761 if (!bmap)
1762 goto error;
1763 total = isl_basic_map_total_dim(bmap);
1764 isl_basic_map_free_div(bmap, bmap->n_div);
1765 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1766 if (bmap->n_eq > 0)
1767 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1768 else {
1769 i = isl_basic_map_alloc_equality(bmap);
1770 if (i < 0)
1771 goto error;
1773 isl_int_set_si(bmap->eq[i][0], 1);
1774 isl_seq_clr(bmap->eq[i]+1, total);
1775 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1776 isl_vec_free(bmap->sample);
1777 bmap->sample = NULL;
1778 return isl_basic_map_finalize(bmap);
1779 error:
1780 isl_basic_map_free(bmap);
1781 return NULL;
1784 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1786 return (struct isl_basic_set *)
1787 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1790 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1791 * of "bmap").
1793 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1795 isl_int *t = bmap->div[a];
1796 bmap->div[a] = bmap->div[b];
1797 bmap->div[b] = t;
1800 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1801 * div definitions accordingly.
1803 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1805 int i;
1806 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1808 swap_div(bmap, a, b);
1810 for (i = 0; i < bmap->n_eq; ++i)
1811 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1813 for (i = 0; i < bmap->n_ineq; ++i)
1814 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1816 for (i = 0; i < bmap->n_div; ++i)
1817 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1818 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1821 /* Eliminate the specified n dimensions starting at first from the
1822 * constraints, without removing the dimensions from the space.
1823 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1825 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1826 enum isl_dim_type type, unsigned first, unsigned n)
1828 int i;
1830 if (!map)
1831 return NULL;
1832 if (n == 0)
1833 return map;
1835 if (first + n > isl_map_dim(map, type) || first + n < first)
1836 isl_die(map->ctx, isl_error_invalid,
1837 "index out of bounds", goto error);
1839 map = isl_map_cow(map);
1840 if (!map)
1841 return NULL;
1843 for (i = 0; i < map->n; ++i) {
1844 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1845 if (!map->p[i])
1846 goto error;
1848 return map;
1849 error:
1850 isl_map_free(map);
1851 return NULL;
1854 /* Eliminate the specified n dimensions starting at first from the
1855 * constraints, without removing the dimensions from the space.
1856 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1858 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1859 enum isl_dim_type type, unsigned first, unsigned n)
1861 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1864 /* Eliminate the specified n dimensions starting at first from the
1865 * constraints, without removing the dimensions from the space.
1866 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1868 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1869 unsigned first, unsigned n)
1871 return isl_set_eliminate(set, isl_dim_set, first, n);
1874 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1875 __isl_take isl_basic_map *bmap)
1877 if (!bmap)
1878 return NULL;
1879 bmap = isl_basic_map_eliminate_vars(bmap,
1880 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1881 if (!bmap)
1882 return NULL;
1883 bmap->n_div = 0;
1884 return isl_basic_map_finalize(bmap);
1887 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1888 __isl_take isl_basic_set *bset)
1890 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1891 (struct isl_basic_map *)bset);
1894 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1896 int i;
1898 if (!map)
1899 return NULL;
1900 if (map->n == 0)
1901 return map;
1903 map = isl_map_cow(map);
1904 if (!map)
1905 return NULL;
1907 for (i = 0; i < map->n; ++i) {
1908 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1909 if (!map->p[i])
1910 goto error;
1912 return map;
1913 error:
1914 isl_map_free(map);
1915 return NULL;
1918 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1920 return isl_map_remove_divs(set);
1923 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1924 enum isl_dim_type type, unsigned first, unsigned n)
1926 if (!bmap)
1927 return NULL;
1928 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1929 goto error);
1930 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1931 return bmap;
1932 bmap = isl_basic_map_eliminate_vars(bmap,
1933 isl_basic_map_offset(bmap, type) - 1 + first, n);
1934 if (!bmap)
1935 return bmap;
1936 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1937 return bmap;
1938 bmap = isl_basic_map_drop(bmap, type, first, n);
1939 return bmap;
1940 error:
1941 isl_basic_map_free(bmap);
1942 return NULL;
1945 /* Return true if the definition of the given div (recursively) involves
1946 * any of the given variables.
1948 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1949 unsigned first, unsigned n)
1951 int i;
1952 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1954 if (isl_int_is_zero(bmap->div[div][0]))
1955 return 0;
1956 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1957 return 1;
1959 for (i = bmap->n_div - 1; i >= 0; --i) {
1960 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1961 continue;
1962 if (div_involves_vars(bmap, i, first, n))
1963 return 1;
1966 return 0;
1969 /* Try and add a lower and/or upper bound on "div" to "bmap"
1970 * based on inequality "i".
1971 * "total" is the total number of variables (excluding the divs).
1972 * "v" is a temporary object that can be used during the calculations.
1973 * If "lb" is set, then a lower bound should be constructed.
1974 * If "ub" is set, then an upper bound should be constructed.
1976 * The calling function has already checked that the inequality does not
1977 * reference "div", but we still need to check that the inequality is
1978 * of the right form. We'll consider the case where we want to construct
1979 * a lower bound. The construction of upper bounds is similar.
1981 * Let "div" be of the form
1983 * q = floor((a + f(x))/d)
1985 * We essentially check if constraint "i" is of the form
1987 * b + f(x) >= 0
1989 * so that we can use it to derive a lower bound on "div".
1990 * However, we allow a slightly more general form
1992 * b + g(x) >= 0
1994 * with the condition that the coefficients of g(x) - f(x) are all
1995 * divisible by d.
1996 * Rewriting this constraint as
1998 * 0 >= -b - g(x)
2000 * adding a + f(x) to both sides and dividing by d, we obtain
2002 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2004 * Taking the floor on both sides, we obtain
2006 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2008 * or
2010 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2012 * In the case of an upper bound, we construct the constraint
2014 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2017 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2018 __isl_take isl_basic_map *bmap, int div, int i,
2019 unsigned total, isl_int v, int lb, int ub)
2021 int j;
2023 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2024 if (lb) {
2025 isl_int_sub(v, bmap->ineq[i][1 + j],
2026 bmap->div[div][1 + 1 + j]);
2027 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2029 if (ub) {
2030 isl_int_add(v, bmap->ineq[i][1 + j],
2031 bmap->div[div][1 + 1 + j]);
2032 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2035 if (!lb && !ub)
2036 return bmap;
2038 bmap = isl_basic_map_cow(bmap);
2039 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2040 if (lb) {
2041 int k = isl_basic_map_alloc_inequality(bmap);
2042 if (k < 0)
2043 goto error;
2044 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2045 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2046 bmap->div[div][1 + j]);
2047 isl_int_cdiv_q(bmap->ineq[k][j],
2048 bmap->ineq[k][j], bmap->div[div][0]);
2050 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2052 if (ub) {
2053 int k = isl_basic_map_alloc_inequality(bmap);
2054 if (k < 0)
2055 goto error;
2056 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2057 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2058 bmap->div[div][1 + j]);
2059 isl_int_fdiv_q(bmap->ineq[k][j],
2060 bmap->ineq[k][j], bmap->div[div][0]);
2062 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2065 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2066 return bmap;
2067 error:
2068 isl_basic_map_free(bmap);
2069 return NULL;
2072 /* This function is called right before "div" is eliminated from "bmap"
2073 * using Fourier-Motzkin.
2074 * Look through the constraints of "bmap" for constraints on the argument
2075 * of the integer division and use them to construct constraints on the
2076 * integer division itself. These constraints can then be combined
2077 * during the Fourier-Motzkin elimination.
2078 * Note that it is only useful to introduce lower bounds on "div"
2079 * if "bmap" already contains upper bounds on "div" as the newly
2080 * introduce lower bounds can then be combined with the pre-existing
2081 * upper bounds. Similarly for upper bounds.
2082 * We therefore first check if "bmap" contains any lower and/or upper bounds
2083 * on "div".
2085 * It is interesting to note that the introduction of these constraints
2086 * can indeed lead to more accurate results, even when compared to
2087 * deriving constraints on the argument of "div" from constraints on "div".
2088 * Consider, for example, the set
2090 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2092 * The second constraint can be rewritten as
2094 * 2 * [(-i-2j+3)/4] + k >= 0
2096 * from which we can derive
2098 * -i - 2j + 3 >= -2k
2100 * or
2102 * i + 2j <= 3 + 2k
2104 * Combined with the first constraint, we obtain
2106 * -3 <= 3 + 2k or k >= -3
2108 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2109 * the first constraint, we obtain
2111 * [(i + 2j)/4] >= [-3/4] = -1
2113 * Combining this constraint with the second constraint, we obtain
2115 * k >= -2
2117 static __isl_give isl_basic_map *insert_bounds_on_div(
2118 __isl_take isl_basic_map *bmap, int div)
2120 int i;
2121 int check_lb, check_ub;
2122 isl_int v;
2123 unsigned total;
2125 if (!bmap)
2126 return NULL;
2128 if (isl_int_is_zero(bmap->div[div][0]))
2129 return bmap;
2131 total = isl_space_dim(bmap->dim, isl_dim_all);
2133 check_lb = 0;
2134 check_ub = 0;
2135 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2136 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2137 if (s > 0)
2138 check_ub = 1;
2139 if (s < 0)
2140 check_lb = 1;
2143 if (!check_lb && !check_ub)
2144 return bmap;
2146 isl_int_init(v);
2148 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2149 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2150 continue;
2152 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2153 check_lb, check_ub);
2156 isl_int_clear(v);
2158 return bmap;
2161 /* Remove all divs (recursively) involving any of the given dimensions
2162 * in their definitions.
2164 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2165 __isl_take isl_basic_map *bmap,
2166 enum isl_dim_type type, unsigned first, unsigned n)
2168 int i;
2170 if (!bmap)
2171 return NULL;
2172 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2173 goto error);
2174 first += isl_basic_map_offset(bmap, type);
2176 for (i = bmap->n_div - 1; i >= 0; --i) {
2177 if (!div_involves_vars(bmap, i, first, n))
2178 continue;
2179 bmap = insert_bounds_on_div(bmap, i);
2180 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2181 if (!bmap)
2182 return NULL;
2183 i = bmap->n_div;
2186 return bmap;
2187 error:
2188 isl_basic_map_free(bmap);
2189 return NULL;
2192 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2193 __isl_take isl_basic_set *bset,
2194 enum isl_dim_type type, unsigned first, unsigned n)
2196 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2199 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2200 enum isl_dim_type type, unsigned first, unsigned n)
2202 int i;
2204 if (!map)
2205 return NULL;
2206 if (map->n == 0)
2207 return map;
2209 map = isl_map_cow(map);
2210 if (!map)
2211 return NULL;
2213 for (i = 0; i < map->n; ++i) {
2214 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2215 type, first, n);
2216 if (!map->p[i])
2217 goto error;
2219 return map;
2220 error:
2221 isl_map_free(map);
2222 return NULL;
2225 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2226 enum isl_dim_type type, unsigned first, unsigned n)
2228 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2229 type, first, n);
2232 /* Does the desciption of "bmap" depend on the specified dimensions?
2233 * We also check whether the dimensions appear in any of the div definitions.
2234 * In principle there is no need for this check. If the dimensions appear
2235 * in a div definition, they also appear in the defining constraints of that
2236 * div.
2238 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2239 enum isl_dim_type type, unsigned first, unsigned n)
2241 int i;
2243 if (!bmap)
2244 return isl_bool_error;
2246 if (first + n > isl_basic_map_dim(bmap, type))
2247 isl_die(bmap->ctx, isl_error_invalid,
2248 "index out of bounds", return isl_bool_error);
2250 first += isl_basic_map_offset(bmap, type);
2251 for (i = 0; i < bmap->n_eq; ++i)
2252 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2253 return isl_bool_true;
2254 for (i = 0; i < bmap->n_ineq; ++i)
2255 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2256 return isl_bool_true;
2257 for (i = 0; i < bmap->n_div; ++i) {
2258 if (isl_int_is_zero(bmap->div[i][0]))
2259 continue;
2260 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2261 return isl_bool_true;
2264 return isl_bool_false;
2267 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2268 enum isl_dim_type type, unsigned first, unsigned n)
2270 int i;
2272 if (!map)
2273 return isl_bool_error;
2275 if (first + n > isl_map_dim(map, type))
2276 isl_die(map->ctx, isl_error_invalid,
2277 "index out of bounds", return isl_bool_error);
2279 for (i = 0; i < map->n; ++i) {
2280 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2281 type, first, n);
2282 if (involves < 0 || involves)
2283 return involves;
2286 return isl_bool_false;
2289 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 return isl_basic_map_involves_dims(bset, type, first, n);
2295 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2296 enum isl_dim_type type, unsigned first, unsigned n)
2298 return isl_map_involves_dims(set, type, first, n);
2301 /* Return true if the definition of the given div is unknown or depends
2302 * on unknown divs.
2304 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2306 int i;
2307 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2309 if (isl_int_is_zero(bmap->div[div][0]))
2310 return 1;
2312 for (i = bmap->n_div - 1; i >= 0; --i) {
2313 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2314 continue;
2315 if (div_is_unknown(bmap, i))
2316 return 1;
2319 return 0;
2322 /* Remove all divs that are unknown or defined in terms of unknown divs.
2324 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2325 __isl_take isl_basic_map *bmap)
2327 int i;
2329 if (!bmap)
2330 return NULL;
2332 for (i = bmap->n_div - 1; i >= 0; --i) {
2333 if (!div_is_unknown(bmap, i))
2334 continue;
2335 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2336 if (!bmap)
2337 return NULL;
2338 i = bmap->n_div;
2341 return bmap;
2344 /* Remove all divs that are unknown or defined in terms of unknown divs.
2346 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2347 __isl_take isl_basic_set *bset)
2349 return isl_basic_map_remove_unknown_divs(bset);
2352 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2354 int i;
2356 if (!map)
2357 return NULL;
2358 if (map->n == 0)
2359 return map;
2361 map = isl_map_cow(map);
2362 if (!map)
2363 return NULL;
2365 for (i = 0; i < map->n; ++i) {
2366 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2367 if (!map->p[i])
2368 goto error;
2370 return map;
2371 error:
2372 isl_map_free(map);
2373 return NULL;
2376 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2378 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2381 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2382 __isl_take isl_basic_set *bset,
2383 enum isl_dim_type type, unsigned first, unsigned n)
2385 return (isl_basic_set *)
2386 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2389 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2390 enum isl_dim_type type, unsigned first, unsigned n)
2392 int i;
2394 if (n == 0)
2395 return map;
2397 map = isl_map_cow(map);
2398 if (!map)
2399 return NULL;
2400 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2402 for (i = 0; i < map->n; ++i) {
2403 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2404 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2405 if (!map->p[i])
2406 goto error;
2408 map = isl_map_drop(map, type, first, n);
2409 return map;
2410 error:
2411 isl_map_free(map);
2412 return NULL;
2415 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2416 enum isl_dim_type type, unsigned first, unsigned n)
2418 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2421 /* Project out n inputs starting at first using Fourier-Motzkin */
2422 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2423 unsigned first, unsigned n)
2425 return isl_map_remove_dims(map, isl_dim_in, first, n);
2428 static void dump_term(struct isl_basic_map *bmap,
2429 isl_int c, int pos, FILE *out)
2431 const char *name;
2432 unsigned in = isl_basic_map_n_in(bmap);
2433 unsigned dim = in + isl_basic_map_n_out(bmap);
2434 unsigned nparam = isl_basic_map_n_param(bmap);
2435 if (!pos)
2436 isl_int_print(out, c, 0);
2437 else {
2438 if (!isl_int_is_one(c))
2439 isl_int_print(out, c, 0);
2440 if (pos < 1 + nparam) {
2441 name = isl_space_get_dim_name(bmap->dim,
2442 isl_dim_param, pos - 1);
2443 if (name)
2444 fprintf(out, "%s", name);
2445 else
2446 fprintf(out, "p%d", pos - 1);
2447 } else if (pos < 1 + nparam + in)
2448 fprintf(out, "i%d", pos - 1 - nparam);
2449 else if (pos < 1 + nparam + dim)
2450 fprintf(out, "o%d", pos - 1 - nparam - in);
2451 else
2452 fprintf(out, "e%d", pos - 1 - nparam - dim);
2456 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2457 int sign, FILE *out)
2459 int i;
2460 int first;
2461 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2462 isl_int v;
2464 isl_int_init(v);
2465 for (i = 0, first = 1; i < len; ++i) {
2466 if (isl_int_sgn(c[i]) * sign <= 0)
2467 continue;
2468 if (!first)
2469 fprintf(out, " + ");
2470 first = 0;
2471 isl_int_abs(v, c[i]);
2472 dump_term(bmap, v, i, out);
2474 isl_int_clear(v);
2475 if (first)
2476 fprintf(out, "0");
2479 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2480 const char *op, FILE *out, int indent)
2482 int i;
2484 fprintf(out, "%*s", indent, "");
2486 dump_constraint_sign(bmap, c, 1, out);
2487 fprintf(out, " %s ", op);
2488 dump_constraint_sign(bmap, c, -1, out);
2490 fprintf(out, "\n");
2492 for (i = bmap->n_div; i < bmap->extra; ++i) {
2493 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2494 continue;
2495 fprintf(out, "%*s", indent, "");
2496 fprintf(out, "ERROR: unused div coefficient not zero\n");
2497 abort();
2501 static void dump_constraints(struct isl_basic_map *bmap,
2502 isl_int **c, unsigned n,
2503 const char *op, FILE *out, int indent)
2505 int i;
2507 for (i = 0; i < n; ++i)
2508 dump_constraint(bmap, c[i], op, out, indent);
2511 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2513 int j;
2514 int first = 1;
2515 unsigned total = isl_basic_map_total_dim(bmap);
2517 for (j = 0; j < 1 + total; ++j) {
2518 if (isl_int_is_zero(exp[j]))
2519 continue;
2520 if (!first && isl_int_is_pos(exp[j]))
2521 fprintf(out, "+");
2522 dump_term(bmap, exp[j], j, out);
2523 first = 0;
2527 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2529 int i;
2531 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2532 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2534 for (i = 0; i < bmap->n_div; ++i) {
2535 fprintf(out, "%*s", indent, "");
2536 fprintf(out, "e%d = [(", i);
2537 dump_affine(bmap, bmap->div[i]+1, out);
2538 fprintf(out, ")/");
2539 isl_int_print(out, bmap->div[i][0], 0);
2540 fprintf(out, "]\n");
2544 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2545 FILE *out, int indent)
2547 if (!bset) {
2548 fprintf(out, "null basic set\n");
2549 return;
2552 fprintf(out, "%*s", indent, "");
2553 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2554 bset->ref, bset->dim->nparam, bset->dim->n_out,
2555 bset->extra, bset->flags);
2556 dump((struct isl_basic_map *)bset, out, indent);
2559 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2560 FILE *out, int indent)
2562 if (!bmap) {
2563 fprintf(out, "null basic map\n");
2564 return;
2567 fprintf(out, "%*s", indent, "");
2568 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2569 "flags: %x, n_name: %d\n",
2570 bmap->ref,
2571 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2572 bmap->extra, bmap->flags, bmap->dim->n_id);
2573 dump(bmap, out, indent);
2576 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2578 unsigned total;
2579 if (!bmap)
2580 return -1;
2581 total = isl_basic_map_total_dim(bmap);
2582 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2583 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2584 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2585 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2586 return 0;
2589 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2590 unsigned flags)
2592 struct isl_set *set;
2594 if (!dim)
2595 return NULL;
2596 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2597 isl_assert(dim->ctx, n >= 0, goto error);
2598 set = isl_alloc(dim->ctx, struct isl_set,
2599 sizeof(struct isl_set) +
2600 (n - 1) * sizeof(struct isl_basic_set *));
2601 if (!set)
2602 goto error;
2604 set->ctx = dim->ctx;
2605 isl_ctx_ref(set->ctx);
2606 set->ref = 1;
2607 set->size = n;
2608 set->n = 0;
2609 set->dim = dim;
2610 set->flags = flags;
2611 return set;
2612 error:
2613 isl_space_free(dim);
2614 return NULL;
2617 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2618 unsigned nparam, unsigned dim, int n, unsigned flags)
2620 struct isl_set *set;
2621 isl_space *dims;
2623 dims = isl_space_alloc(ctx, nparam, 0, dim);
2624 if (!dims)
2625 return NULL;
2627 set = isl_set_alloc_space(dims, n, flags);
2628 return set;
2631 /* Make sure "map" has room for at least "n" more basic maps.
2633 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2635 int i;
2636 struct isl_map *grown = NULL;
2638 if (!map)
2639 return NULL;
2640 isl_assert(map->ctx, n >= 0, goto error);
2641 if (map->n + n <= map->size)
2642 return map;
2643 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2644 if (!grown)
2645 goto error;
2646 for (i = 0; i < map->n; ++i) {
2647 grown->p[i] = isl_basic_map_copy(map->p[i]);
2648 if (!grown->p[i])
2649 goto error;
2650 grown->n++;
2652 isl_map_free(map);
2653 return grown;
2654 error:
2655 isl_map_free(grown);
2656 isl_map_free(map);
2657 return NULL;
2660 /* Make sure "set" has room for at least "n" more basic sets.
2662 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2664 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2667 struct isl_set *isl_set_dup(struct isl_set *set)
2669 int i;
2670 struct isl_set *dup;
2672 if (!set)
2673 return NULL;
2675 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2676 if (!dup)
2677 return NULL;
2678 for (i = 0; i < set->n; ++i)
2679 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2680 return dup;
2683 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2685 return isl_map_from_basic_map(bset);
2688 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2690 struct isl_map *map;
2692 if (!bmap)
2693 return NULL;
2695 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2696 return isl_map_add_basic_map(map, bmap);
2699 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2700 __isl_take isl_basic_set *bset)
2702 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2703 (struct isl_basic_map *)bset);
2706 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2708 int i;
2710 if (!set)
2711 return NULL;
2713 if (--set->ref > 0)
2714 return NULL;
2716 isl_ctx_deref(set->ctx);
2717 for (i = 0; i < set->n; ++i)
2718 isl_basic_set_free(set->p[i]);
2719 isl_space_free(set->dim);
2720 free(set);
2722 return NULL;
2725 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2727 int i;
2729 if (!set) {
2730 fprintf(out, "null set\n");
2731 return;
2734 fprintf(out, "%*s", indent, "");
2735 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2736 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2737 set->flags);
2738 for (i = 0; i < set->n; ++i) {
2739 fprintf(out, "%*s", indent, "");
2740 fprintf(out, "basic set %d:\n", i);
2741 isl_basic_set_print_internal(set->p[i], out, indent+4);
2745 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2747 int i;
2749 if (!map) {
2750 fprintf(out, "null map\n");
2751 return;
2754 fprintf(out, "%*s", indent, "");
2755 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2756 "flags: %x, n_name: %d\n",
2757 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2758 map->dim->n_out, map->flags, map->dim->n_id);
2759 for (i = 0; i < map->n; ++i) {
2760 fprintf(out, "%*s", indent, "");
2761 fprintf(out, "basic map %d:\n", i);
2762 isl_basic_map_print_internal(map->p[i], out, indent+4);
2766 struct isl_basic_map *isl_basic_map_intersect_domain(
2767 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2769 struct isl_basic_map *bmap_domain;
2771 if (!bmap || !bset)
2772 goto error;
2774 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2775 bset->dim, isl_dim_param), goto error);
2777 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2778 isl_assert(bset->ctx,
2779 isl_basic_map_compatible_domain(bmap, bset), goto error);
2781 bmap = isl_basic_map_cow(bmap);
2782 if (!bmap)
2783 goto error;
2784 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2785 bset->n_div, bset->n_eq, bset->n_ineq);
2786 bmap_domain = isl_basic_map_from_domain(bset);
2787 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2789 bmap = isl_basic_map_simplify(bmap);
2790 return isl_basic_map_finalize(bmap);
2791 error:
2792 isl_basic_map_free(bmap);
2793 isl_basic_set_free(bset);
2794 return NULL;
2797 struct isl_basic_map *isl_basic_map_intersect_range(
2798 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2800 struct isl_basic_map *bmap_range;
2802 if (!bmap || !bset)
2803 goto error;
2805 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2806 bset->dim, isl_dim_param), goto error);
2808 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2809 isl_assert(bset->ctx,
2810 isl_basic_map_compatible_range(bmap, bset), goto error);
2812 if (isl_basic_set_plain_is_universe(bset)) {
2813 isl_basic_set_free(bset);
2814 return bmap;
2817 bmap = isl_basic_map_cow(bmap);
2818 if (!bmap)
2819 goto error;
2820 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2821 bset->n_div, bset->n_eq, bset->n_ineq);
2822 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2823 bmap = add_constraints(bmap, bmap_range, 0, 0);
2825 bmap = isl_basic_map_simplify(bmap);
2826 return isl_basic_map_finalize(bmap);
2827 error:
2828 isl_basic_map_free(bmap);
2829 isl_basic_set_free(bset);
2830 return NULL;
2833 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2834 __isl_keep isl_vec *vec)
2836 int i;
2837 unsigned total;
2838 isl_int s;
2840 if (!bmap || !vec)
2841 return isl_bool_error;
2843 total = 1 + isl_basic_map_total_dim(bmap);
2844 if (total != vec->size)
2845 return isl_bool_error;
2847 isl_int_init(s);
2849 for (i = 0; i < bmap->n_eq; ++i) {
2850 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2851 if (!isl_int_is_zero(s)) {
2852 isl_int_clear(s);
2853 return isl_bool_false;
2857 for (i = 0; i < bmap->n_ineq; ++i) {
2858 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2859 if (isl_int_is_neg(s)) {
2860 isl_int_clear(s);
2861 return isl_bool_false;
2865 isl_int_clear(s);
2867 return isl_bool_true;
2870 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2871 __isl_keep isl_vec *vec)
2873 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2876 struct isl_basic_map *isl_basic_map_intersect(
2877 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2879 struct isl_vec *sample = NULL;
2881 if (!bmap1 || !bmap2)
2882 goto error;
2884 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2885 bmap2->dim, isl_dim_param), goto error);
2886 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2887 isl_space_dim(bmap1->dim, isl_dim_param) &&
2888 isl_space_dim(bmap2->dim, isl_dim_all) !=
2889 isl_space_dim(bmap2->dim, isl_dim_param))
2890 return isl_basic_map_intersect(bmap2, bmap1);
2892 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2893 isl_space_dim(bmap2->dim, isl_dim_param))
2894 isl_assert(bmap1->ctx,
2895 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2897 if (isl_basic_map_plain_is_empty(bmap1)) {
2898 isl_basic_map_free(bmap2);
2899 return bmap1;
2901 if (isl_basic_map_plain_is_empty(bmap2)) {
2902 isl_basic_map_free(bmap1);
2903 return bmap2;
2906 if (bmap1->sample &&
2907 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2908 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2909 sample = isl_vec_copy(bmap1->sample);
2910 else if (bmap2->sample &&
2911 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2912 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2913 sample = isl_vec_copy(bmap2->sample);
2915 bmap1 = isl_basic_map_cow(bmap1);
2916 if (!bmap1)
2917 goto error;
2918 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2919 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2920 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2922 if (!bmap1)
2923 isl_vec_free(sample);
2924 else if (sample) {
2925 isl_vec_free(bmap1->sample);
2926 bmap1->sample = sample;
2929 bmap1 = isl_basic_map_simplify(bmap1);
2930 return isl_basic_map_finalize(bmap1);
2931 error:
2932 if (sample)
2933 isl_vec_free(sample);
2934 isl_basic_map_free(bmap1);
2935 isl_basic_map_free(bmap2);
2936 return NULL;
2939 struct isl_basic_set *isl_basic_set_intersect(
2940 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2942 return (struct isl_basic_set *)
2943 isl_basic_map_intersect(
2944 (struct isl_basic_map *)bset1,
2945 (struct isl_basic_map *)bset2);
2948 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2949 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2951 return isl_basic_set_intersect(bset1, bset2);
2954 /* Special case of isl_map_intersect, where both map1 and map2
2955 * are convex, without any divs and such that either map1 or map2
2956 * contains a single constraint. This constraint is then simply
2957 * added to the other map.
2959 static __isl_give isl_map *map_intersect_add_constraint(
2960 __isl_take isl_map *map1, __isl_take isl_map *map2)
2962 isl_assert(map1->ctx, map1->n == 1, goto error);
2963 isl_assert(map2->ctx, map1->n == 1, goto error);
2964 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2965 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2967 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2968 return isl_map_intersect(map2, map1);
2970 isl_assert(map2->ctx,
2971 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2973 map1 = isl_map_cow(map1);
2974 if (!map1)
2975 goto error;
2976 if (isl_map_plain_is_empty(map1)) {
2977 isl_map_free(map2);
2978 return map1;
2980 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2981 if (map2->p[0]->n_eq == 1)
2982 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2983 else
2984 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2985 map2->p[0]->ineq[0]);
2987 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2988 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2989 if (!map1->p[0])
2990 goto error;
2992 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2993 isl_basic_map_free(map1->p[0]);
2994 map1->n = 0;
2997 isl_map_free(map2);
2999 return map1;
3000 error:
3001 isl_map_free(map1);
3002 isl_map_free(map2);
3003 return NULL;
3006 /* map2 may be either a parameter domain or a map living in the same
3007 * space as map1.
3009 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3010 __isl_take isl_map *map2)
3012 unsigned flags = 0;
3013 isl_map *result;
3014 int i, j;
3016 if (!map1 || !map2)
3017 goto error;
3019 if ((isl_map_plain_is_empty(map1) ||
3020 isl_map_plain_is_universe(map2)) &&
3021 isl_space_is_equal(map1->dim, map2->dim)) {
3022 isl_map_free(map2);
3023 return map1;
3025 if ((isl_map_plain_is_empty(map2) ||
3026 isl_map_plain_is_universe(map1)) &&
3027 isl_space_is_equal(map1->dim, map2->dim)) {
3028 isl_map_free(map1);
3029 return map2;
3032 if (map1->n == 1 && map2->n == 1 &&
3033 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3034 isl_space_is_equal(map1->dim, map2->dim) &&
3035 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3036 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3037 return map_intersect_add_constraint(map1, map2);
3039 if (isl_space_dim(map2->dim, isl_dim_all) !=
3040 isl_space_dim(map2->dim, isl_dim_param))
3041 isl_assert(map1->ctx,
3042 isl_space_is_equal(map1->dim, map2->dim), goto error);
3044 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3045 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3046 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3048 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3049 map1->n * map2->n, flags);
3050 if (!result)
3051 goto error;
3052 for (i = 0; i < map1->n; ++i)
3053 for (j = 0; j < map2->n; ++j) {
3054 struct isl_basic_map *part;
3055 part = isl_basic_map_intersect(
3056 isl_basic_map_copy(map1->p[i]),
3057 isl_basic_map_copy(map2->p[j]));
3058 if (isl_basic_map_is_empty(part) < 0)
3059 part = isl_basic_map_free(part);
3060 result = isl_map_add_basic_map(result, part);
3061 if (!result)
3062 goto error;
3064 isl_map_free(map1);
3065 isl_map_free(map2);
3066 return result;
3067 error:
3068 isl_map_free(map1);
3069 isl_map_free(map2);
3070 return NULL;
3073 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3074 __isl_take isl_map *map2)
3076 if (!map1 || !map2)
3077 goto error;
3078 if (!isl_space_is_equal(map1->dim, map2->dim))
3079 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3080 "spaces don't match", goto error);
3081 return map_intersect_internal(map1, map2);
3082 error:
3083 isl_map_free(map1);
3084 isl_map_free(map2);
3085 return NULL;
3088 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3089 __isl_take isl_map *map2)
3091 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3094 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3096 return (struct isl_set *)
3097 isl_map_intersect((struct isl_map *)set1,
3098 (struct isl_map *)set2);
3101 /* map_intersect_internal accepts intersections
3102 * with parameter domains, so we can just call that function.
3104 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3105 __isl_take isl_set *params)
3107 return map_intersect_internal(map, params);
3110 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3111 __isl_take isl_map *map2)
3113 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3116 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3117 __isl_take isl_set *params)
3119 return isl_map_intersect_params(set, params);
3122 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3124 isl_space *space;
3125 unsigned pos, n1, n2;
3127 if (!bmap)
3128 return NULL;
3129 bmap = isl_basic_map_cow(bmap);
3130 if (!bmap)
3131 return NULL;
3132 space = isl_space_reverse(isl_space_copy(bmap->dim));
3133 pos = isl_basic_map_offset(bmap, isl_dim_in);
3134 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3135 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3136 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3137 return isl_basic_map_reset_space(bmap, space);
3140 static __isl_give isl_basic_map *basic_map_space_reset(
3141 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3143 isl_space *space;
3145 if (!bmap)
3146 return NULL;
3147 if (!isl_space_is_named_or_nested(bmap->dim, type))
3148 return bmap;
3150 space = isl_basic_map_get_space(bmap);
3151 space = isl_space_reset(space, type);
3152 bmap = isl_basic_map_reset_space(bmap, space);
3153 return bmap;
3156 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3157 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3158 unsigned pos, unsigned n)
3160 isl_space *res_dim;
3161 struct isl_basic_map *res;
3162 struct isl_dim_map *dim_map;
3163 unsigned total, off;
3164 enum isl_dim_type t;
3166 if (n == 0)
3167 return basic_map_space_reset(bmap, type);
3169 if (!bmap)
3170 return NULL;
3172 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3174 total = isl_basic_map_total_dim(bmap) + n;
3175 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3176 off = 0;
3177 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3178 if (t != type) {
3179 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3180 } else {
3181 unsigned size = isl_basic_map_dim(bmap, t);
3182 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3183 0, pos, off);
3184 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3185 pos, size - pos, off + pos + n);
3187 off += isl_space_dim(res_dim, t);
3189 isl_dim_map_div(dim_map, bmap, off);
3191 res = isl_basic_map_alloc_space(res_dim,
3192 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3193 if (isl_basic_map_is_rational(bmap))
3194 res = isl_basic_map_set_rational(res);
3195 if (isl_basic_map_plain_is_empty(bmap)) {
3196 isl_basic_map_free(bmap);
3197 free(dim_map);
3198 return isl_basic_map_set_to_empty(res);
3200 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3201 return isl_basic_map_finalize(res);
3204 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3205 __isl_take isl_basic_set *bset,
3206 enum isl_dim_type type, unsigned pos, unsigned n)
3208 return isl_basic_map_insert_dims(bset, type, pos, n);
3211 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3212 enum isl_dim_type type, unsigned n)
3214 if (!bmap)
3215 return NULL;
3216 return isl_basic_map_insert_dims(bmap, type,
3217 isl_basic_map_dim(bmap, type), n);
3220 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3221 enum isl_dim_type type, unsigned n)
3223 if (!bset)
3224 return NULL;
3225 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3226 return isl_basic_map_add_dims(bset, type, n);
3227 error:
3228 isl_basic_set_free(bset);
3229 return NULL;
3232 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3233 enum isl_dim_type type)
3235 isl_space *space;
3237 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3238 return map;
3240 space = isl_map_get_space(map);
3241 space = isl_space_reset(space, type);
3242 map = isl_map_reset_space(map, space);
3243 return map;
3246 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3247 enum isl_dim_type type, unsigned pos, unsigned n)
3249 int i;
3251 if (n == 0)
3252 return map_space_reset(map, type);
3254 map = isl_map_cow(map);
3255 if (!map)
3256 return NULL;
3258 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3259 if (!map->dim)
3260 goto error;
3262 for (i = 0; i < map->n; ++i) {
3263 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3264 if (!map->p[i])
3265 goto error;
3268 return map;
3269 error:
3270 isl_map_free(map);
3271 return NULL;
3274 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3275 enum isl_dim_type type, unsigned pos, unsigned n)
3277 return isl_map_insert_dims(set, type, pos, n);
3280 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3281 enum isl_dim_type type, unsigned n)
3283 if (!map)
3284 return NULL;
3285 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3288 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3289 enum isl_dim_type type, unsigned n)
3291 if (!set)
3292 return NULL;
3293 isl_assert(set->ctx, type != isl_dim_in, goto error);
3294 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3295 error:
3296 isl_set_free(set);
3297 return NULL;
3300 __isl_give isl_basic_map *isl_basic_map_move_dims(
3301 __isl_take isl_basic_map *bmap,
3302 enum isl_dim_type dst_type, unsigned dst_pos,
3303 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3305 struct isl_dim_map *dim_map;
3306 struct isl_basic_map *res;
3307 enum isl_dim_type t;
3308 unsigned total, off;
3310 if (!bmap)
3311 return NULL;
3312 if (n == 0)
3313 return bmap;
3315 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3316 goto error);
3318 if (dst_type == src_type && dst_pos == src_pos)
3319 return bmap;
3321 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3323 if (pos(bmap->dim, dst_type) + dst_pos ==
3324 pos(bmap->dim, src_type) + src_pos +
3325 ((src_type < dst_type) ? n : 0)) {
3326 bmap = isl_basic_map_cow(bmap);
3327 if (!bmap)
3328 return NULL;
3330 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3331 src_type, src_pos, n);
3332 if (!bmap->dim)
3333 goto error;
3335 bmap = isl_basic_map_finalize(bmap);
3337 return bmap;
3340 total = isl_basic_map_total_dim(bmap);
3341 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3343 off = 0;
3344 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3345 unsigned size = isl_space_dim(bmap->dim, t);
3346 if (t == dst_type) {
3347 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3348 0, dst_pos, off);
3349 off += dst_pos;
3350 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3351 src_pos, n, off);
3352 off += n;
3353 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3354 dst_pos, size - dst_pos, off);
3355 off += size - dst_pos;
3356 } else if (t == src_type) {
3357 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3358 0, src_pos, off);
3359 off += src_pos;
3360 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3361 src_pos + n, size - src_pos - n, off);
3362 off += size - src_pos - n;
3363 } else {
3364 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3365 off += size;
3368 isl_dim_map_div(dim_map, bmap, off);
3370 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3371 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3372 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3373 if (!bmap)
3374 goto error;
3376 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3377 src_type, src_pos, n);
3378 if (!bmap->dim)
3379 goto error;
3381 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3382 bmap = isl_basic_map_gauss(bmap, NULL);
3383 bmap = isl_basic_map_finalize(bmap);
3385 return bmap;
3386 error:
3387 isl_basic_map_free(bmap);
3388 return NULL;
3391 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3392 enum isl_dim_type dst_type, unsigned dst_pos,
3393 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3395 return (isl_basic_set *)isl_basic_map_move_dims(
3396 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3399 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3400 enum isl_dim_type dst_type, unsigned dst_pos,
3401 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3403 if (!set)
3404 return NULL;
3405 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3406 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3407 src_type, src_pos, n);
3408 error:
3409 isl_set_free(set);
3410 return NULL;
3413 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3414 enum isl_dim_type dst_type, unsigned dst_pos,
3415 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3417 int i;
3419 if (!map)
3420 return NULL;
3421 if (n == 0)
3422 return map;
3424 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3425 goto error);
3427 if (dst_type == src_type && dst_pos == src_pos)
3428 return map;
3430 isl_assert(map->ctx, dst_type != src_type, goto error);
3432 map = isl_map_cow(map);
3433 if (!map)
3434 return NULL;
3436 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3437 if (!map->dim)
3438 goto error;
3440 for (i = 0; i < map->n; ++i) {
3441 map->p[i] = isl_basic_map_move_dims(map->p[i],
3442 dst_type, dst_pos,
3443 src_type, src_pos, n);
3444 if (!map->p[i])
3445 goto error;
3448 return map;
3449 error:
3450 isl_map_free(map);
3451 return NULL;
3454 /* Move the specified dimensions to the last columns right before
3455 * the divs. Don't change the dimension specification of bmap.
3456 * That's the responsibility of the caller.
3458 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3459 enum isl_dim_type type, unsigned first, unsigned n)
3461 struct isl_dim_map *dim_map;
3462 struct isl_basic_map *res;
3463 enum isl_dim_type t;
3464 unsigned total, off;
3466 if (!bmap)
3467 return NULL;
3468 if (pos(bmap->dim, type) + first + n ==
3469 1 + isl_space_dim(bmap->dim, isl_dim_all))
3470 return bmap;
3472 total = isl_basic_map_total_dim(bmap);
3473 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3475 off = 0;
3476 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3477 unsigned size = isl_space_dim(bmap->dim, t);
3478 if (t == type) {
3479 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3480 0, first, off);
3481 off += first;
3482 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3483 first, n, total - bmap->n_div - n);
3484 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3485 first + n, size - (first + n), off);
3486 off += size - (first + n);
3487 } else {
3488 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3489 off += size;
3492 isl_dim_map_div(dim_map, bmap, off + n);
3494 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3495 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3496 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3497 return res;
3500 /* Insert "n" rows in the divs of "bmap".
3502 * The number of columns is not changed, which means that the last
3503 * dimensions of "bmap" are being reintepreted as the new divs.
3504 * The space of "bmap" is not adjusted, however, which means
3505 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3506 * from the space of "bmap" is the responsibility of the caller.
3508 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3509 int n)
3511 int i;
3512 size_t row_size;
3513 isl_int **new_div;
3514 isl_int *old;
3516 bmap = isl_basic_map_cow(bmap);
3517 if (!bmap)
3518 return NULL;
3520 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3521 old = bmap->block2.data;
3522 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3523 (bmap->extra + n) * (1 + row_size));
3524 if (!bmap->block2.data)
3525 return isl_basic_map_free(bmap);
3526 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3527 if (!new_div)
3528 return isl_basic_map_free(bmap);
3529 for (i = 0; i < n; ++i) {
3530 new_div[i] = bmap->block2.data +
3531 (bmap->extra + i) * (1 + row_size);
3532 isl_seq_clr(new_div[i], 1 + row_size);
3534 for (i = 0; i < bmap->extra; ++i)
3535 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3536 free(bmap->div);
3537 bmap->div = new_div;
3538 bmap->n_div += n;
3539 bmap->extra += n;
3541 return bmap;
3544 /* Drop constraints from "bmap" that only involve the variables
3545 * of "type" in the range [first, first + n] that are not related
3546 * to any of the variables outside that interval.
3547 * These constraints cannot influence the values for the variables
3548 * outside the interval, except in case they cause "bmap" to be empty.
3549 * Only drop the constraints if "bmap" is known to be non-empty.
3551 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3552 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3553 unsigned first, unsigned n)
3555 int i;
3556 int *groups;
3557 unsigned dim, n_div;
3558 isl_bool non_empty;
3560 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3561 if (non_empty < 0)
3562 return isl_basic_map_free(bmap);
3563 if (!non_empty)
3564 return bmap;
3566 dim = isl_basic_map_dim(bmap, isl_dim_all);
3567 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3568 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3569 if (!groups)
3570 return isl_basic_map_free(bmap);
3571 first += isl_basic_map_offset(bmap, type) - 1;
3572 for (i = 0; i < first; ++i)
3573 groups[i] = -1;
3574 for (i = first + n; i < dim - n_div; ++i)
3575 groups[i] = -1;
3577 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3579 return bmap;
3582 /* Turn the n dimensions of type type, starting at first
3583 * into existentially quantified variables.
3585 * If a subset of the projected out variables are unrelated
3586 * to any of the variables that remain, then the constraints
3587 * involving this subset are simply dropped first.
3589 __isl_give isl_basic_map *isl_basic_map_project_out(
3590 __isl_take isl_basic_map *bmap,
3591 enum isl_dim_type type, unsigned first, unsigned n)
3593 if (n == 0)
3594 return basic_map_space_reset(bmap, type);
3595 if (type == isl_dim_div)
3596 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3597 "cannot project out existentially quantified variables",
3598 return isl_basic_map_free(bmap));
3600 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3601 if (!bmap)
3602 return NULL;
3604 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3605 return isl_basic_map_remove_dims(bmap, type, first, n);
3607 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3608 goto error);
3610 bmap = move_last(bmap, type, first, n);
3611 bmap = isl_basic_map_cow(bmap);
3612 bmap = insert_div_rows(bmap, n);
3613 if (!bmap)
3614 return NULL;
3616 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3617 if (!bmap->dim)
3618 goto error;
3619 bmap = isl_basic_map_simplify(bmap);
3620 bmap = isl_basic_map_drop_redundant_divs(bmap);
3621 return isl_basic_map_finalize(bmap);
3622 error:
3623 isl_basic_map_free(bmap);
3624 return NULL;
3627 /* Turn the n dimensions of type type, starting at first
3628 * into existentially quantified variables.
3630 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3631 enum isl_dim_type type, unsigned first, unsigned n)
3633 return (isl_basic_set *)isl_basic_map_project_out(
3634 (isl_basic_map *)bset, type, first, n);
3637 /* Turn the n dimensions of type type, starting at first
3638 * into existentially quantified variables.
3640 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3641 enum isl_dim_type type, unsigned first, unsigned n)
3643 int i;
3645 if (!map)
3646 return NULL;
3648 if (n == 0)
3649 return map_space_reset(map, type);
3651 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3653 map = isl_map_cow(map);
3654 if (!map)
3655 return NULL;
3657 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3658 if (!map->dim)
3659 goto error;
3661 for (i = 0; i < map->n; ++i) {
3662 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3663 if (!map->p[i])
3664 goto error;
3667 return map;
3668 error:
3669 isl_map_free(map);
3670 return NULL;
3673 /* Turn the n dimensions of type type, starting at first
3674 * into existentially quantified variables.
3676 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3677 enum isl_dim_type type, unsigned first, unsigned n)
3679 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3682 /* Return a map that projects the elements in "set" onto their
3683 * "n" set dimensions starting at "first".
3684 * "type" should be equal to isl_dim_set.
3686 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3687 enum isl_dim_type type, unsigned first, unsigned n)
3689 int i;
3690 int dim;
3691 isl_map *map;
3693 if (!set)
3694 return NULL;
3695 if (type != isl_dim_set)
3696 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3697 "only set dimensions can be projected out", goto error);
3698 dim = isl_set_dim(set, isl_dim_set);
3699 if (first + n > dim || first + n < first)
3700 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3701 "index out of bounds", goto error);
3703 map = isl_map_from_domain(set);
3704 map = isl_map_add_dims(map, isl_dim_out, n);
3705 for (i = 0; i < n; ++i)
3706 map = isl_map_equate(map, isl_dim_in, first + i,
3707 isl_dim_out, i);
3708 return map;
3709 error:
3710 isl_set_free(set);
3711 return NULL;
3714 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3716 int i, j;
3718 for (i = 0; i < n; ++i) {
3719 j = isl_basic_map_alloc_div(bmap);
3720 if (j < 0)
3721 goto error;
3722 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3724 return bmap;
3725 error:
3726 isl_basic_map_free(bmap);
3727 return NULL;
3730 struct isl_basic_map *isl_basic_map_apply_range(
3731 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3733 isl_space *dim_result = NULL;
3734 struct isl_basic_map *bmap;
3735 unsigned n_in, n_out, n, nparam, total, pos;
3736 struct isl_dim_map *dim_map1, *dim_map2;
3738 if (!bmap1 || !bmap2)
3739 goto error;
3740 if (!isl_space_match(bmap1->dim, isl_dim_param,
3741 bmap2->dim, isl_dim_param))
3742 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3743 "parameters don't match", goto error);
3744 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3745 bmap2->dim, isl_dim_in))
3746 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3747 "spaces don't match", goto error);
3749 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3750 isl_space_copy(bmap2->dim));
3752 n_in = isl_basic_map_n_in(bmap1);
3753 n_out = isl_basic_map_n_out(bmap2);
3754 n = isl_basic_map_n_out(bmap1);
3755 nparam = isl_basic_map_n_param(bmap1);
3757 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3758 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3759 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3760 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3761 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3762 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3763 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3764 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3765 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3766 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3767 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3769 bmap = isl_basic_map_alloc_space(dim_result,
3770 bmap1->n_div + bmap2->n_div + n,
3771 bmap1->n_eq + bmap2->n_eq,
3772 bmap1->n_ineq + bmap2->n_ineq);
3773 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3774 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3775 bmap = add_divs(bmap, n);
3776 bmap = isl_basic_map_simplify(bmap);
3777 bmap = isl_basic_map_drop_redundant_divs(bmap);
3778 return isl_basic_map_finalize(bmap);
3779 error:
3780 isl_basic_map_free(bmap1);
3781 isl_basic_map_free(bmap2);
3782 return NULL;
3785 struct isl_basic_set *isl_basic_set_apply(
3786 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3788 if (!bset || !bmap)
3789 goto error;
3791 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3792 goto error);
3794 return (struct isl_basic_set *)
3795 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3796 error:
3797 isl_basic_set_free(bset);
3798 isl_basic_map_free(bmap);
3799 return NULL;
3802 struct isl_basic_map *isl_basic_map_apply_domain(
3803 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3805 if (!bmap1 || !bmap2)
3806 goto error;
3808 isl_assert(bmap1->ctx,
3809 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3810 isl_assert(bmap1->ctx,
3811 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3812 goto error);
3814 bmap1 = isl_basic_map_reverse(bmap1);
3815 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3816 return isl_basic_map_reverse(bmap1);
3817 error:
3818 isl_basic_map_free(bmap1);
3819 isl_basic_map_free(bmap2);
3820 return NULL;
3823 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3824 * A \cap B -> f(A) + f(B)
3826 struct isl_basic_map *isl_basic_map_sum(
3827 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3829 unsigned n_in, n_out, nparam, total, pos;
3830 struct isl_basic_map *bmap = NULL;
3831 struct isl_dim_map *dim_map1, *dim_map2;
3832 int i;
3834 if (!bmap1 || !bmap2)
3835 goto error;
3837 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3838 goto error);
3840 nparam = isl_basic_map_n_param(bmap1);
3841 n_in = isl_basic_map_n_in(bmap1);
3842 n_out = isl_basic_map_n_out(bmap1);
3844 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3845 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3846 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3847 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3848 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3849 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3850 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3851 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3852 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3853 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3854 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3856 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3857 bmap1->n_div + bmap2->n_div + 2 * n_out,
3858 bmap1->n_eq + bmap2->n_eq + n_out,
3859 bmap1->n_ineq + bmap2->n_ineq);
3860 for (i = 0; i < n_out; ++i) {
3861 int j = isl_basic_map_alloc_equality(bmap);
3862 if (j < 0)
3863 goto error;
3864 isl_seq_clr(bmap->eq[j], 1+total);
3865 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3866 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3867 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3869 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3870 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3871 bmap = add_divs(bmap, 2 * n_out);
3873 bmap = isl_basic_map_simplify(bmap);
3874 return isl_basic_map_finalize(bmap);
3875 error:
3876 isl_basic_map_free(bmap);
3877 isl_basic_map_free(bmap1);
3878 isl_basic_map_free(bmap2);
3879 return NULL;
3882 /* Given two maps A -> f(A) and B -> g(B), construct a map
3883 * A \cap B -> f(A) + f(B)
3885 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3887 struct isl_map *result;
3888 int i, j;
3890 if (!map1 || !map2)
3891 goto error;
3893 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3895 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3896 map1->n * map2->n, 0);
3897 if (!result)
3898 goto error;
3899 for (i = 0; i < map1->n; ++i)
3900 for (j = 0; j < map2->n; ++j) {
3901 struct isl_basic_map *part;
3902 part = isl_basic_map_sum(
3903 isl_basic_map_copy(map1->p[i]),
3904 isl_basic_map_copy(map2->p[j]));
3905 if (isl_basic_map_is_empty(part))
3906 isl_basic_map_free(part);
3907 else
3908 result = isl_map_add_basic_map(result, part);
3909 if (!result)
3910 goto error;
3912 isl_map_free(map1);
3913 isl_map_free(map2);
3914 return result;
3915 error:
3916 isl_map_free(map1);
3917 isl_map_free(map2);
3918 return NULL;
3921 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3922 __isl_take isl_set *set2)
3924 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3927 /* Given a basic map A -> f(A), construct A -> -f(A).
3929 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3931 int i, j;
3932 unsigned off, n;
3934 bmap = isl_basic_map_cow(bmap);
3935 if (!bmap)
3936 return NULL;
3938 n = isl_basic_map_dim(bmap, isl_dim_out);
3939 off = isl_basic_map_offset(bmap, isl_dim_out);
3940 for (i = 0; i < bmap->n_eq; ++i)
3941 for (j = 0; j < n; ++j)
3942 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3943 for (i = 0; i < bmap->n_ineq; ++i)
3944 for (j = 0; j < n; ++j)
3945 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3946 for (i = 0; i < bmap->n_div; ++i)
3947 for (j = 0; j < n; ++j)
3948 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3949 bmap = isl_basic_map_gauss(bmap, NULL);
3950 return isl_basic_map_finalize(bmap);
3953 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3955 return isl_basic_map_neg(bset);
3958 /* Given a map A -> f(A), construct A -> -f(A).
3960 struct isl_map *isl_map_neg(struct isl_map *map)
3962 int i;
3964 map = isl_map_cow(map);
3965 if (!map)
3966 return NULL;
3968 for (i = 0; i < map->n; ++i) {
3969 map->p[i] = isl_basic_map_neg(map->p[i]);
3970 if (!map->p[i])
3971 goto error;
3974 return map;
3975 error:
3976 isl_map_free(map);
3977 return NULL;
3980 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3982 return (isl_set *)isl_map_neg((isl_map *)set);
3985 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3986 * A -> floor(f(A)/d).
3988 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3989 isl_int d)
3991 unsigned n_in, n_out, nparam, total, pos;
3992 struct isl_basic_map *result = NULL;
3993 struct isl_dim_map *dim_map;
3994 int i;
3996 if (!bmap)
3997 return NULL;
3999 nparam = isl_basic_map_n_param(bmap);
4000 n_in = isl_basic_map_n_in(bmap);
4001 n_out = isl_basic_map_n_out(bmap);
4003 total = nparam + n_in + n_out + bmap->n_div + n_out;
4004 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4005 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4006 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4007 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4008 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4010 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4011 bmap->n_div + n_out,
4012 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4013 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4014 result = add_divs(result, n_out);
4015 for (i = 0; i < n_out; ++i) {
4016 int j;
4017 j = isl_basic_map_alloc_inequality(result);
4018 if (j < 0)
4019 goto error;
4020 isl_seq_clr(result->ineq[j], 1+total);
4021 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4022 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4023 j = isl_basic_map_alloc_inequality(result);
4024 if (j < 0)
4025 goto error;
4026 isl_seq_clr(result->ineq[j], 1+total);
4027 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4028 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4029 isl_int_sub_ui(result->ineq[j][0], d, 1);
4032 result = isl_basic_map_simplify(result);
4033 return isl_basic_map_finalize(result);
4034 error:
4035 isl_basic_map_free(result);
4036 return NULL;
4039 /* Given a map A -> f(A) and an integer d, construct a map
4040 * A -> floor(f(A)/d).
4042 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4044 int i;
4046 map = isl_map_cow(map);
4047 if (!map)
4048 return NULL;
4050 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4051 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4052 for (i = 0; i < map->n; ++i) {
4053 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4054 if (!map->p[i])
4055 goto error;
4058 return map;
4059 error:
4060 isl_map_free(map);
4061 return NULL;
4064 /* Given a map A -> f(A) and an integer d, construct a map
4065 * A -> floor(f(A)/d).
4067 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4068 __isl_take isl_val *d)
4070 if (!map || !d)
4071 goto error;
4072 if (!isl_val_is_int(d))
4073 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4074 "expecting integer denominator", goto error);
4075 map = isl_map_floordiv(map, d->n);
4076 isl_val_free(d);
4077 return map;
4078 error:
4079 isl_map_free(map);
4080 isl_val_free(d);
4081 return NULL;
4084 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4086 int i;
4087 unsigned nparam;
4088 unsigned n_in;
4090 i = isl_basic_map_alloc_equality(bmap);
4091 if (i < 0)
4092 goto error;
4093 nparam = isl_basic_map_n_param(bmap);
4094 n_in = isl_basic_map_n_in(bmap);
4095 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4096 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4097 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4098 return isl_basic_map_finalize(bmap);
4099 error:
4100 isl_basic_map_free(bmap);
4101 return NULL;
4104 /* Add a constraints to "bmap" expressing i_pos < o_pos
4106 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4108 int i;
4109 unsigned nparam;
4110 unsigned n_in;
4112 i = isl_basic_map_alloc_inequality(bmap);
4113 if (i < 0)
4114 goto error;
4115 nparam = isl_basic_map_n_param(bmap);
4116 n_in = isl_basic_map_n_in(bmap);
4117 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4118 isl_int_set_si(bmap->ineq[i][0], -1);
4119 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4120 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4121 return isl_basic_map_finalize(bmap);
4122 error:
4123 isl_basic_map_free(bmap);
4124 return NULL;
4127 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4129 static __isl_give isl_basic_map *var_less_or_equal(
4130 __isl_take isl_basic_map *bmap, unsigned pos)
4132 int i;
4133 unsigned nparam;
4134 unsigned n_in;
4136 i = isl_basic_map_alloc_inequality(bmap);
4137 if (i < 0)
4138 goto error;
4139 nparam = isl_basic_map_n_param(bmap);
4140 n_in = isl_basic_map_n_in(bmap);
4141 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4142 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4143 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4144 return isl_basic_map_finalize(bmap);
4145 error:
4146 isl_basic_map_free(bmap);
4147 return NULL;
4150 /* Add a constraints to "bmap" expressing i_pos > o_pos
4152 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4154 int i;
4155 unsigned nparam;
4156 unsigned n_in;
4158 i = isl_basic_map_alloc_inequality(bmap);
4159 if (i < 0)
4160 goto error;
4161 nparam = isl_basic_map_n_param(bmap);
4162 n_in = isl_basic_map_n_in(bmap);
4163 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4164 isl_int_set_si(bmap->ineq[i][0], -1);
4165 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4166 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4167 return isl_basic_map_finalize(bmap);
4168 error:
4169 isl_basic_map_free(bmap);
4170 return NULL;
4173 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4175 static __isl_give isl_basic_map *var_more_or_equal(
4176 __isl_take isl_basic_map *bmap, unsigned pos)
4178 int i;
4179 unsigned nparam;
4180 unsigned n_in;
4182 i = isl_basic_map_alloc_inequality(bmap);
4183 if (i < 0)
4184 goto error;
4185 nparam = isl_basic_map_n_param(bmap);
4186 n_in = isl_basic_map_n_in(bmap);
4187 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4188 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4189 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4190 return isl_basic_map_finalize(bmap);
4191 error:
4192 isl_basic_map_free(bmap);
4193 return NULL;
4196 __isl_give isl_basic_map *isl_basic_map_equal(
4197 __isl_take isl_space *dim, unsigned n_equal)
4199 int i;
4200 struct isl_basic_map *bmap;
4201 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4202 if (!bmap)
4203 return NULL;
4204 for (i = 0; i < n_equal && bmap; ++i)
4205 bmap = var_equal(bmap, i);
4206 return isl_basic_map_finalize(bmap);
4209 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4211 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4212 unsigned pos)
4214 int i;
4215 struct isl_basic_map *bmap;
4216 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4217 if (!bmap)
4218 return NULL;
4219 for (i = 0; i < pos && bmap; ++i)
4220 bmap = var_equal(bmap, i);
4221 if (bmap)
4222 bmap = var_less(bmap, pos);
4223 return isl_basic_map_finalize(bmap);
4226 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4228 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4229 __isl_take isl_space *dim, unsigned pos)
4231 int i;
4232 isl_basic_map *bmap;
4234 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4235 for (i = 0; i < pos; ++i)
4236 bmap = var_equal(bmap, i);
4237 bmap = var_less_or_equal(bmap, pos);
4238 return isl_basic_map_finalize(bmap);
4241 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4243 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4244 unsigned pos)
4246 int i;
4247 struct isl_basic_map *bmap;
4248 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4249 if (!bmap)
4250 return NULL;
4251 for (i = 0; i < pos && bmap; ++i)
4252 bmap = var_equal(bmap, i);
4253 if (bmap)
4254 bmap = var_more(bmap, pos);
4255 return isl_basic_map_finalize(bmap);
4258 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4260 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4261 __isl_take isl_space *dim, unsigned pos)
4263 int i;
4264 isl_basic_map *bmap;
4266 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4267 for (i = 0; i < pos; ++i)
4268 bmap = var_equal(bmap, i);
4269 bmap = var_more_or_equal(bmap, pos);
4270 return isl_basic_map_finalize(bmap);
4273 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4274 unsigned n, int equal)
4276 struct isl_map *map;
4277 int i;
4279 if (n == 0 && equal)
4280 return isl_map_universe(dims);
4282 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4284 for (i = 0; i + 1 < n; ++i)
4285 map = isl_map_add_basic_map(map,
4286 isl_basic_map_less_at(isl_space_copy(dims), i));
4287 if (n > 0) {
4288 if (equal)
4289 map = isl_map_add_basic_map(map,
4290 isl_basic_map_less_or_equal_at(dims, n - 1));
4291 else
4292 map = isl_map_add_basic_map(map,
4293 isl_basic_map_less_at(dims, n - 1));
4294 } else
4295 isl_space_free(dims);
4297 return map;
4300 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4302 if (!dims)
4303 return NULL;
4304 return map_lex_lte_first(dims, dims->n_out, equal);
4307 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4309 return map_lex_lte_first(dim, n, 0);
4312 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4314 return map_lex_lte_first(dim, n, 1);
4317 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4319 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4322 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4324 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4327 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4328 unsigned n, int equal)
4330 struct isl_map *map;
4331 int i;
4333 if (n == 0 && equal)
4334 return isl_map_universe(dims);
4336 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4338 for (i = 0; i + 1 < n; ++i)
4339 map = isl_map_add_basic_map(map,
4340 isl_basic_map_more_at(isl_space_copy(dims), i));
4341 if (n > 0) {
4342 if (equal)
4343 map = isl_map_add_basic_map(map,
4344 isl_basic_map_more_or_equal_at(dims, n - 1));
4345 else
4346 map = isl_map_add_basic_map(map,
4347 isl_basic_map_more_at(dims, n - 1));
4348 } else
4349 isl_space_free(dims);
4351 return map;
4354 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4356 if (!dims)
4357 return NULL;
4358 return map_lex_gte_first(dims, dims->n_out, equal);
4361 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4363 return map_lex_gte_first(dim, n, 0);
4366 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4368 return map_lex_gte_first(dim, n, 1);
4371 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4373 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4376 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4378 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4381 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4382 __isl_take isl_set *set2)
4384 isl_map *map;
4385 map = isl_map_lex_le(isl_set_get_space(set1));
4386 map = isl_map_intersect_domain(map, set1);
4387 map = isl_map_intersect_range(map, set2);
4388 return map;
4391 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4392 __isl_take isl_set *set2)
4394 isl_map *map;
4395 map = isl_map_lex_lt(isl_set_get_space(set1));
4396 map = isl_map_intersect_domain(map, set1);
4397 map = isl_map_intersect_range(map, set2);
4398 return map;
4401 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4402 __isl_take isl_set *set2)
4404 isl_map *map;
4405 map = isl_map_lex_ge(isl_set_get_space(set1));
4406 map = isl_map_intersect_domain(map, set1);
4407 map = isl_map_intersect_range(map, set2);
4408 return map;
4411 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4412 __isl_take isl_set *set2)
4414 isl_map *map;
4415 map = isl_map_lex_gt(isl_set_get_space(set1));
4416 map = isl_map_intersect_domain(map, set1);
4417 map = isl_map_intersect_range(map, set2);
4418 return map;
4421 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4422 __isl_take isl_map *map2)
4424 isl_map *map;
4425 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4426 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4427 map = isl_map_apply_range(map, isl_map_reverse(map2));
4428 return map;
4431 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4432 __isl_take isl_map *map2)
4434 isl_map *map;
4435 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4436 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4437 map = isl_map_apply_range(map, isl_map_reverse(map2));
4438 return map;
4441 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4442 __isl_take isl_map *map2)
4444 isl_map *map;
4445 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4446 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4447 map = isl_map_apply_range(map, isl_map_reverse(map2));
4448 return map;
4451 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4452 __isl_take isl_map *map2)
4454 isl_map *map;
4455 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4456 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4457 map = isl_map_apply_range(map, isl_map_reverse(map2));
4458 return map;
4461 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4462 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4464 struct isl_basic_map *bmap;
4466 bset = isl_basic_set_cow(bset);
4467 if (!bset || !dim)
4468 goto error;
4470 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4471 isl_space_free(bset->dim);
4472 bmap = (struct isl_basic_map *) bset;
4473 bmap->dim = dim;
4474 return isl_basic_map_finalize(bmap);
4475 error:
4476 isl_basic_set_free(bset);
4477 isl_space_free(dim);
4478 return NULL;
4481 /* For a div d = floor(f/m), add the constraint
4483 * f - m d >= 0
4485 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4486 unsigned pos, isl_int *div)
4488 int i;
4489 unsigned total = isl_basic_map_total_dim(bmap);
4491 i = isl_basic_map_alloc_inequality(bmap);
4492 if (i < 0)
4493 return -1;
4494 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4495 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4497 return 0;
4500 /* For a div d = floor(f/m), add the constraint
4502 * -(f-(n-1)) + m d >= 0
4504 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4505 unsigned pos, isl_int *div)
4507 int i;
4508 unsigned total = isl_basic_map_total_dim(bmap);
4510 i = isl_basic_map_alloc_inequality(bmap);
4511 if (i < 0)
4512 return -1;
4513 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4514 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4515 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4516 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4518 return 0;
4521 /* For a div d = floor(f/m), add the constraints
4523 * f - m d >= 0
4524 * -(f-(n-1)) + m d >= 0
4526 * Note that the second constraint is the negation of
4528 * f - m d >= n
4530 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4531 unsigned pos, isl_int *div)
4533 if (add_upper_div_constraint(bmap, pos, div) < 0)
4534 return -1;
4535 if (add_lower_div_constraint(bmap, pos, div) < 0)
4536 return -1;
4537 return 0;
4540 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4541 unsigned pos, isl_int *div)
4543 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4544 pos, div);
4547 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4549 unsigned total = isl_basic_map_total_dim(bmap);
4550 unsigned div_pos = total - bmap->n_div + div;
4552 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4553 bmap->div[div]);
4556 /* For each known div d = floor(f/m), add the constraints
4558 * f - m d >= 0
4559 * -(f-(n-1)) + m d >= 0
4561 * Remove duplicate constraints in case of some these div constraints
4562 * already appear in "bmap".
4564 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4565 __isl_take isl_basic_map *bmap)
4567 unsigned n_div;
4569 if (!bmap)
4570 return NULL;
4571 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4572 if (n_div == 0)
4573 return bmap;
4575 bmap = add_known_div_constraints(bmap);
4576 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4577 bmap = isl_basic_map_finalize(bmap);
4578 return bmap;
4581 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4583 * In particular, if this div is of the form d = floor(f/m),
4584 * then add the constraint
4586 * f - m d >= 0
4588 * if sign < 0 or the constraint
4590 * -(f-(n-1)) + m d >= 0
4592 * if sign > 0.
4594 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4595 unsigned div, int sign)
4597 unsigned total;
4598 unsigned div_pos;
4600 if (!bmap)
4601 return -1;
4603 total = isl_basic_map_total_dim(bmap);
4604 div_pos = total - bmap->n_div + div;
4606 if (sign < 0)
4607 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4608 else
4609 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4612 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4614 return isl_basic_map_add_div_constraints(bset, div);
4617 struct isl_basic_set *isl_basic_map_underlying_set(
4618 struct isl_basic_map *bmap)
4620 if (!bmap)
4621 goto error;
4622 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4623 bmap->n_div == 0 &&
4624 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4625 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4626 return (struct isl_basic_set *)bmap;
4627 bmap = isl_basic_map_cow(bmap);
4628 if (!bmap)
4629 goto error;
4630 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4631 if (!bmap->dim)
4632 goto error;
4633 bmap->extra -= bmap->n_div;
4634 bmap->n_div = 0;
4635 bmap = isl_basic_map_finalize(bmap);
4636 return (struct isl_basic_set *)bmap;
4637 error:
4638 isl_basic_map_free(bmap);
4639 return NULL;
4642 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4643 __isl_take isl_basic_set *bset)
4645 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4648 /* Replace each element in "list" by the result of applying
4649 * isl_basic_map_underlying_set to the element.
4651 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4652 __isl_take isl_basic_map_list *list)
4654 int i, n;
4656 if (!list)
4657 return NULL;
4659 n = isl_basic_map_list_n_basic_map(list);
4660 for (i = 0; i < n; ++i) {
4661 isl_basic_map *bmap;
4662 isl_basic_set *bset;
4664 bmap = isl_basic_map_list_get_basic_map(list, i);
4665 bset = isl_basic_set_underlying_set(bmap);
4666 list = isl_basic_set_list_set_basic_set(list, i, bset);
4669 return list;
4672 struct isl_basic_map *isl_basic_map_overlying_set(
4673 struct isl_basic_set *bset, struct isl_basic_map *like)
4675 struct isl_basic_map *bmap;
4676 struct isl_ctx *ctx;
4677 unsigned total;
4678 int i;
4680 if (!bset || !like)
4681 goto error;
4682 ctx = bset->ctx;
4683 isl_assert(ctx, bset->n_div == 0, goto error);
4684 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4685 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4686 goto error);
4687 if (like->n_div == 0) {
4688 isl_space *space = isl_basic_map_get_space(like);
4689 isl_basic_map_free(like);
4690 return isl_basic_map_reset_space(bset, space);
4692 bset = isl_basic_set_cow(bset);
4693 if (!bset)
4694 goto error;
4695 total = bset->dim->n_out + bset->extra;
4696 bmap = (struct isl_basic_map *)bset;
4697 isl_space_free(bmap->dim);
4698 bmap->dim = isl_space_copy(like->dim);
4699 if (!bmap->dim)
4700 goto error;
4701 bmap->n_div = like->n_div;
4702 bmap->extra += like->n_div;
4703 if (bmap->extra) {
4704 unsigned ltotal;
4705 isl_int **div;
4706 ltotal = total - bmap->extra + like->extra;
4707 if (ltotal > total)
4708 ltotal = total;
4709 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4710 bmap->extra * (1 + 1 + total));
4711 if (isl_blk_is_error(bmap->block2))
4712 goto error;
4713 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4714 if (!div)
4715 goto error;
4716 bmap->div = div;
4717 for (i = 0; i < bmap->extra; ++i)
4718 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4719 for (i = 0; i < like->n_div; ++i) {
4720 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4721 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4723 bmap = isl_basic_map_add_known_div_constraints(bmap);
4725 isl_basic_map_free(like);
4726 bmap = isl_basic_map_simplify(bmap);
4727 bmap = isl_basic_map_finalize(bmap);
4728 return bmap;
4729 error:
4730 isl_basic_map_free(like);
4731 isl_basic_set_free(bset);
4732 return NULL;
4735 struct isl_basic_set *isl_basic_set_from_underlying_set(
4736 struct isl_basic_set *bset, struct isl_basic_set *like)
4738 return (struct isl_basic_set *)
4739 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4742 struct isl_set *isl_set_from_underlying_set(
4743 struct isl_set *set, struct isl_basic_set *like)
4745 int i;
4747 if (!set || !like)
4748 goto error;
4749 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4750 goto error);
4751 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4752 isl_basic_set_free(like);
4753 return set;
4755 set = isl_set_cow(set);
4756 if (!set)
4757 goto error;
4758 for (i = 0; i < set->n; ++i) {
4759 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4760 isl_basic_set_copy(like));
4761 if (!set->p[i])
4762 goto error;
4764 isl_space_free(set->dim);
4765 set->dim = isl_space_copy(like->dim);
4766 if (!set->dim)
4767 goto error;
4768 isl_basic_set_free(like);
4769 return set;
4770 error:
4771 isl_basic_set_free(like);
4772 isl_set_free(set);
4773 return NULL;
4776 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4778 int i;
4780 map = isl_map_cow(map);
4781 if (!map)
4782 return NULL;
4783 map->dim = isl_space_cow(map->dim);
4784 if (!map->dim)
4785 goto error;
4787 for (i = 1; i < map->n; ++i)
4788 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4789 goto error);
4790 for (i = 0; i < map->n; ++i) {
4791 map->p[i] = (struct isl_basic_map *)
4792 isl_basic_map_underlying_set(map->p[i]);
4793 if (!map->p[i])
4794 goto error;
4796 if (map->n == 0)
4797 map->dim = isl_space_underlying(map->dim, 0);
4798 else {
4799 isl_space_free(map->dim);
4800 map->dim = isl_space_copy(map->p[0]->dim);
4802 if (!map->dim)
4803 goto error;
4804 return (struct isl_set *)map;
4805 error:
4806 isl_map_free(map);
4807 return NULL;
4810 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4812 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4815 /* Replace the space of "bmap" by "space".
4817 * If the space of "bmap" is identical to "space" (including the identifiers
4818 * of the input and output dimensions), then simply return the original input.
4820 __isl_give isl_basic_map *isl_basic_map_reset_space(
4821 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4823 isl_bool equal;
4825 if (!bmap)
4826 goto error;
4827 equal = isl_space_is_equal(bmap->dim, space);
4828 if (equal >= 0 && equal)
4829 equal = isl_space_match(bmap->dim, isl_dim_in,
4830 space, isl_dim_in);
4831 if (equal >= 0 && equal)
4832 equal = isl_space_match(bmap->dim, isl_dim_out,
4833 space, isl_dim_out);
4834 if (equal < 0)
4835 goto error;
4836 if (equal) {
4837 isl_space_free(space);
4838 return bmap;
4840 bmap = isl_basic_map_cow(bmap);
4841 if (!bmap || !space)
4842 goto error;
4844 isl_space_free(bmap->dim);
4845 bmap->dim = space;
4847 bmap = isl_basic_map_finalize(bmap);
4849 return bmap;
4850 error:
4851 isl_basic_map_free(bmap);
4852 isl_space_free(space);
4853 return NULL;
4856 __isl_give isl_basic_set *isl_basic_set_reset_space(
4857 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4859 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4860 dim);
4863 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4864 __isl_take isl_space *dim)
4866 int i;
4868 map = isl_map_cow(map);
4869 if (!map || !dim)
4870 goto error;
4872 for (i = 0; i < map->n; ++i) {
4873 map->p[i] = isl_basic_map_reset_space(map->p[i],
4874 isl_space_copy(dim));
4875 if (!map->p[i])
4876 goto error;
4878 isl_space_free(map->dim);
4879 map->dim = dim;
4881 return map;
4882 error:
4883 isl_map_free(map);
4884 isl_space_free(dim);
4885 return NULL;
4888 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4889 __isl_take isl_space *dim)
4891 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4894 /* Compute the parameter domain of the given basic set.
4896 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4898 isl_space *space;
4899 unsigned n;
4901 if (isl_basic_set_is_params(bset))
4902 return bset;
4904 n = isl_basic_set_dim(bset, isl_dim_set);
4905 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4906 space = isl_basic_set_get_space(bset);
4907 space = isl_space_params(space);
4908 bset = isl_basic_set_reset_space(bset, space);
4909 return bset;
4912 /* Construct a zero-dimensional basic set with the given parameter domain.
4914 __isl_give isl_basic_set *isl_basic_set_from_params(
4915 __isl_take isl_basic_set *bset)
4917 isl_space *space;
4918 space = isl_basic_set_get_space(bset);
4919 space = isl_space_set_from_params(space);
4920 bset = isl_basic_set_reset_space(bset, space);
4921 return bset;
4924 /* Compute the parameter domain of the given set.
4926 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4928 isl_space *space;
4929 unsigned n;
4931 if (isl_set_is_params(set))
4932 return set;
4934 n = isl_set_dim(set, isl_dim_set);
4935 set = isl_set_project_out(set, isl_dim_set, 0, n);
4936 space = isl_set_get_space(set);
4937 space = isl_space_params(space);
4938 set = isl_set_reset_space(set, space);
4939 return set;
4942 /* Construct a zero-dimensional set with the given parameter domain.
4944 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4946 isl_space *space;
4947 space = isl_set_get_space(set);
4948 space = isl_space_set_from_params(space);
4949 set = isl_set_reset_space(set, space);
4950 return set;
4953 /* Compute the parameter domain of the given map.
4955 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4957 isl_space *space;
4958 unsigned n;
4960 n = isl_map_dim(map, isl_dim_in);
4961 map = isl_map_project_out(map, isl_dim_in, 0, n);
4962 n = isl_map_dim(map, isl_dim_out);
4963 map = isl_map_project_out(map, isl_dim_out, 0, n);
4964 space = isl_map_get_space(map);
4965 space = isl_space_params(space);
4966 map = isl_map_reset_space(map, space);
4967 return map;
4970 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4972 isl_space *space;
4973 unsigned n_out;
4975 if (!bmap)
4976 return NULL;
4977 space = isl_space_domain(isl_basic_map_get_space(bmap));
4979 n_out = isl_basic_map_n_out(bmap);
4980 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4982 return isl_basic_map_reset_space(bmap, space);
4985 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4987 if (!bmap)
4988 return -1;
4989 return isl_space_may_be_set(bmap->dim);
4992 /* Is this basic map actually a set?
4993 * Users should never call this function. Outside of isl,
4994 * the type should indicate whether something is a set or a map.
4996 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4998 if (!bmap)
4999 return -1;
5000 return isl_space_is_set(bmap->dim);
5003 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5005 if (!bmap)
5006 return NULL;
5007 if (isl_basic_map_is_set(bmap))
5008 return bmap;
5009 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5012 __isl_give isl_basic_map *isl_basic_map_domain_map(
5013 __isl_take isl_basic_map *bmap)
5015 int i, k;
5016 isl_space *dim;
5017 isl_basic_map *domain;
5018 int nparam, n_in, n_out;
5019 unsigned total;
5021 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5022 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5023 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5025 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5026 domain = isl_basic_map_universe(dim);
5028 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5029 bmap = isl_basic_map_apply_range(bmap, domain);
5030 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5032 total = isl_basic_map_total_dim(bmap);
5034 for (i = 0; i < n_in; ++i) {
5035 k = isl_basic_map_alloc_equality(bmap);
5036 if (k < 0)
5037 goto error;
5038 isl_seq_clr(bmap->eq[k], 1 + total);
5039 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5040 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5043 bmap = isl_basic_map_gauss(bmap, NULL);
5044 return isl_basic_map_finalize(bmap);
5045 error:
5046 isl_basic_map_free(bmap);
5047 return NULL;
5050 __isl_give isl_basic_map *isl_basic_map_range_map(
5051 __isl_take isl_basic_map *bmap)
5053 int i, k;
5054 isl_space *dim;
5055 isl_basic_map *range;
5056 int nparam, n_in, n_out;
5057 unsigned total;
5059 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5060 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5061 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5063 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5064 range = isl_basic_map_universe(dim);
5066 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5067 bmap = isl_basic_map_apply_range(bmap, range);
5068 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5070 total = isl_basic_map_total_dim(bmap);
5072 for (i = 0; i < n_out; ++i) {
5073 k = isl_basic_map_alloc_equality(bmap);
5074 if (k < 0)
5075 goto error;
5076 isl_seq_clr(bmap->eq[k], 1 + total);
5077 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5078 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5081 bmap = isl_basic_map_gauss(bmap, NULL);
5082 return isl_basic_map_finalize(bmap);
5083 error:
5084 isl_basic_map_free(bmap);
5085 return NULL;
5088 int isl_map_may_be_set(__isl_keep isl_map *map)
5090 if (!map)
5091 return -1;
5092 return isl_space_may_be_set(map->dim);
5095 /* Is this map actually a set?
5096 * Users should never call this function. Outside of isl,
5097 * the type should indicate whether something is a set or a map.
5099 int isl_map_is_set(__isl_keep isl_map *map)
5101 if (!map)
5102 return -1;
5103 return isl_space_is_set(map->dim);
5106 struct isl_set *isl_map_range(struct isl_map *map)
5108 int i;
5109 struct isl_set *set;
5111 if (!map)
5112 goto error;
5113 if (isl_map_is_set(map))
5114 return (isl_set *)map;
5116 map = isl_map_cow(map);
5117 if (!map)
5118 goto error;
5120 set = (struct isl_set *) map;
5121 set->dim = isl_space_range(set->dim);
5122 if (!set->dim)
5123 goto error;
5124 for (i = 0; i < map->n; ++i) {
5125 set->p[i] = isl_basic_map_range(map->p[i]);
5126 if (!set->p[i])
5127 goto error;
5129 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5130 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5131 return set;
5132 error:
5133 isl_map_free(map);
5134 return NULL;
5137 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5139 int i;
5141 map = isl_map_cow(map);
5142 if (!map)
5143 return NULL;
5145 map->dim = isl_space_domain_map(map->dim);
5146 if (!map->dim)
5147 goto error;
5148 for (i = 0; i < map->n; ++i) {
5149 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5150 if (!map->p[i])
5151 goto error;
5153 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5154 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5155 return map;
5156 error:
5157 isl_map_free(map);
5158 return NULL;
5161 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5163 int i;
5164 isl_space *range_dim;
5166 map = isl_map_cow(map);
5167 if (!map)
5168 return NULL;
5170 range_dim = isl_space_range(isl_map_get_space(map));
5171 range_dim = isl_space_from_range(range_dim);
5172 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5173 map->dim = isl_space_join(map->dim, range_dim);
5174 if (!map->dim)
5175 goto error;
5176 for (i = 0; i < map->n; ++i) {
5177 map->p[i] = isl_basic_map_range_map(map->p[i]);
5178 if (!map->p[i])
5179 goto error;
5181 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5182 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5183 return map;
5184 error:
5185 isl_map_free(map);
5186 return NULL;
5189 /* Given a wrapped map of the form A[B -> C],
5190 * return the map A[B -> C] -> B.
5192 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5194 isl_id *id;
5195 isl_map *map;
5197 if (!set)
5198 return NULL;
5199 if (!isl_set_has_tuple_id(set))
5200 return isl_map_domain_map(isl_set_unwrap(set));
5202 id = isl_set_get_tuple_id(set);
5203 map = isl_map_domain_map(isl_set_unwrap(set));
5204 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5206 return map;
5209 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5210 __isl_take isl_space *dim)
5212 int i;
5213 struct isl_map *map = NULL;
5215 set = isl_set_cow(set);
5216 if (!set || !dim)
5217 goto error;
5218 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5219 map = (struct isl_map *)set;
5220 for (i = 0; i < set->n; ++i) {
5221 map->p[i] = isl_basic_map_from_basic_set(
5222 set->p[i], isl_space_copy(dim));
5223 if (!map->p[i])
5224 goto error;
5226 isl_space_free(map->dim);
5227 map->dim = dim;
5228 return map;
5229 error:
5230 isl_space_free(dim);
5231 isl_set_free(set);
5232 return NULL;
5235 __isl_give isl_basic_map *isl_basic_map_from_domain(
5236 __isl_take isl_basic_set *bset)
5238 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5241 __isl_give isl_basic_map *isl_basic_map_from_range(
5242 __isl_take isl_basic_set *bset)
5244 isl_space *space;
5245 space = isl_basic_set_get_space(bset);
5246 space = isl_space_from_range(space);
5247 bset = isl_basic_set_reset_space(bset, space);
5248 return (isl_basic_map *)bset;
5251 /* Create a relation with the given set as range.
5252 * The domain of the created relation is a zero-dimensional
5253 * flat anonymous space.
5255 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5257 isl_space *space;
5258 space = isl_set_get_space(set);
5259 space = isl_space_from_range(space);
5260 set = isl_set_reset_space(set, space);
5261 return (struct isl_map *)set;
5264 /* Create a relation with the given set as domain.
5265 * The range of the created relation is a zero-dimensional
5266 * flat anonymous space.
5268 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5270 return isl_map_reverse(isl_map_from_range(set));
5273 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5274 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5276 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5279 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5280 __isl_take isl_set *range)
5282 return isl_map_apply_range(isl_map_reverse(domain), range);
5285 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5286 unsigned flags)
5288 struct isl_map *map;
5290 if (!dim)
5291 return NULL;
5292 if (n < 0)
5293 isl_die(dim->ctx, isl_error_internal,
5294 "negative number of basic maps", goto error);
5295 map = isl_alloc(dim->ctx, struct isl_map,
5296 sizeof(struct isl_map) +
5297 (n - 1) * sizeof(struct isl_basic_map *));
5298 if (!map)
5299 goto error;
5301 map->ctx = dim->ctx;
5302 isl_ctx_ref(map->ctx);
5303 map->ref = 1;
5304 map->size = n;
5305 map->n = 0;
5306 map->dim = dim;
5307 map->flags = flags;
5308 return map;
5309 error:
5310 isl_space_free(dim);
5311 return NULL;
5314 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5315 unsigned nparam, unsigned in, unsigned out, int n,
5316 unsigned flags)
5318 struct isl_map *map;
5319 isl_space *dims;
5321 dims = isl_space_alloc(ctx, nparam, in, out);
5322 if (!dims)
5323 return NULL;
5325 map = isl_map_alloc_space(dims, n, flags);
5326 return map;
5329 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5331 struct isl_basic_map *bmap;
5332 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5333 bmap = isl_basic_map_set_to_empty(bmap);
5334 return bmap;
5337 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5339 struct isl_basic_set *bset;
5340 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5341 bset = isl_basic_set_set_to_empty(bset);
5342 return bset;
5345 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5347 struct isl_basic_map *bmap;
5348 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5349 bmap = isl_basic_map_finalize(bmap);
5350 return bmap;
5353 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5355 struct isl_basic_set *bset;
5356 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5357 bset = isl_basic_set_finalize(bset);
5358 return bset;
5361 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5363 int i;
5364 unsigned total = isl_space_dim(dim, isl_dim_all);
5365 isl_basic_map *bmap;
5367 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5368 for (i = 0; i < total; ++i) {
5369 int k = isl_basic_map_alloc_inequality(bmap);
5370 if (k < 0)
5371 goto error;
5372 isl_seq_clr(bmap->ineq[k], 1 + total);
5373 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5375 return bmap;
5376 error:
5377 isl_basic_map_free(bmap);
5378 return NULL;
5381 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5383 return isl_basic_map_nat_universe(dim);
5386 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5388 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5391 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5393 return isl_map_nat_universe(dim);
5396 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5398 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5401 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5403 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5406 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5408 struct isl_map *map;
5409 if (!dim)
5410 return NULL;
5411 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5412 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5413 return map;
5416 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5418 struct isl_set *set;
5419 if (!dim)
5420 return NULL;
5421 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5422 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5423 return set;
5426 struct isl_map *isl_map_dup(struct isl_map *map)
5428 int i;
5429 struct isl_map *dup;
5431 if (!map)
5432 return NULL;
5433 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5434 for (i = 0; i < map->n; ++i)
5435 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5436 return dup;
5439 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5440 __isl_take isl_basic_map *bmap)
5442 if (!bmap || !map)
5443 goto error;
5444 if (isl_basic_map_plain_is_empty(bmap)) {
5445 isl_basic_map_free(bmap);
5446 return map;
5448 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5449 isl_assert(map->ctx, map->n < map->size, goto error);
5450 map->p[map->n] = bmap;
5451 map->n++;
5452 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5453 return map;
5454 error:
5455 if (map)
5456 isl_map_free(map);
5457 if (bmap)
5458 isl_basic_map_free(bmap);
5459 return NULL;
5462 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5464 int i;
5466 if (!map)
5467 return NULL;
5469 if (--map->ref > 0)
5470 return NULL;
5472 isl_ctx_deref(map->ctx);
5473 for (i = 0; i < map->n; ++i)
5474 isl_basic_map_free(map->p[i]);
5475 isl_space_free(map->dim);
5476 free(map);
5478 return NULL;
5481 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5482 struct isl_basic_map *bmap, unsigned pos, int value)
5484 int j;
5486 bmap = isl_basic_map_cow(bmap);
5487 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5488 j = isl_basic_map_alloc_equality(bmap);
5489 if (j < 0)
5490 goto error;
5491 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5492 isl_int_set_si(bmap->eq[j][pos], -1);
5493 isl_int_set_si(bmap->eq[j][0], value);
5494 bmap = isl_basic_map_simplify(bmap);
5495 return isl_basic_map_finalize(bmap);
5496 error:
5497 isl_basic_map_free(bmap);
5498 return NULL;
5501 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5502 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5504 int j;
5506 bmap = isl_basic_map_cow(bmap);
5507 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5508 j = isl_basic_map_alloc_equality(bmap);
5509 if (j < 0)
5510 goto error;
5511 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5512 isl_int_set_si(bmap->eq[j][pos], -1);
5513 isl_int_set(bmap->eq[j][0], value);
5514 bmap = isl_basic_map_simplify(bmap);
5515 return isl_basic_map_finalize(bmap);
5516 error:
5517 isl_basic_map_free(bmap);
5518 return NULL;
5521 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5522 enum isl_dim_type type, unsigned pos, int value)
5524 if (!bmap)
5525 return NULL;
5526 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5527 return isl_basic_map_fix_pos_si(bmap,
5528 isl_basic_map_offset(bmap, type) + pos, value);
5529 error:
5530 isl_basic_map_free(bmap);
5531 return NULL;
5534 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5535 enum isl_dim_type type, unsigned pos, isl_int value)
5537 if (!bmap)
5538 return NULL;
5539 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5540 return isl_basic_map_fix_pos(bmap,
5541 isl_basic_map_offset(bmap, type) + pos, value);
5542 error:
5543 isl_basic_map_free(bmap);
5544 return NULL;
5547 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5548 * to be equal to "v".
5550 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5551 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5553 if (!bmap || !v)
5554 goto error;
5555 if (!isl_val_is_int(v))
5556 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5557 "expecting integer value", goto error);
5558 if (pos >= isl_basic_map_dim(bmap, type))
5559 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5560 "index out of bounds", goto error);
5561 pos += isl_basic_map_offset(bmap, type);
5562 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5563 isl_val_free(v);
5564 return bmap;
5565 error:
5566 isl_basic_map_free(bmap);
5567 isl_val_free(v);
5568 return NULL;
5571 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5572 * to be equal to "v".
5574 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5575 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5577 return isl_basic_map_fix_val(bset, type, pos, v);
5580 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5581 enum isl_dim_type type, unsigned pos, int value)
5583 return (struct isl_basic_set *)
5584 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5585 type, pos, value);
5588 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5589 enum isl_dim_type type, unsigned pos, isl_int value)
5591 return (struct isl_basic_set *)
5592 isl_basic_map_fix((struct isl_basic_map *)bset,
5593 type, pos, value);
5596 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5597 unsigned input, int value)
5599 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5602 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5603 unsigned dim, int value)
5605 return (struct isl_basic_set *)
5606 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5607 isl_dim_set, dim, value);
5610 static int remove_if_empty(__isl_keep isl_map *map, int i)
5612 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5614 if (empty < 0)
5615 return -1;
5616 if (!empty)
5617 return 0;
5619 isl_basic_map_free(map->p[i]);
5620 if (i != map->n - 1) {
5621 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5622 map->p[i] = map->p[map->n - 1];
5624 map->n--;
5626 return 0;
5629 /* Perform "fn" on each basic map of "map", where we may not be holding
5630 * the only reference to "map".
5631 * In particular, "fn" should be a semantics preserving operation
5632 * that we want to apply to all copies of "map". We therefore need
5633 * to be careful not to modify "map" in a way that breaks "map"
5634 * in case anything goes wrong.
5636 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5637 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5639 struct isl_basic_map *bmap;
5640 int i;
5642 if (!map)
5643 return NULL;
5645 for (i = map->n - 1; i >= 0; --i) {
5646 bmap = isl_basic_map_copy(map->p[i]);
5647 bmap = fn(bmap);
5648 if (!bmap)
5649 goto error;
5650 isl_basic_map_free(map->p[i]);
5651 map->p[i] = bmap;
5652 if (remove_if_empty(map, i) < 0)
5653 goto error;
5656 return map;
5657 error:
5658 isl_map_free(map);
5659 return NULL;
5662 struct isl_map *isl_map_fix_si(struct isl_map *map,
5663 enum isl_dim_type type, unsigned pos, int value)
5665 int i;
5667 map = isl_map_cow(map);
5668 if (!map)
5669 return NULL;
5671 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5672 for (i = map->n - 1; i >= 0; --i) {
5673 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5674 if (remove_if_empty(map, i) < 0)
5675 goto error;
5677 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5678 return map;
5679 error:
5680 isl_map_free(map);
5681 return NULL;
5684 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5685 enum isl_dim_type type, unsigned pos, int value)
5687 return (struct isl_set *)
5688 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5691 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5692 enum isl_dim_type type, unsigned pos, isl_int value)
5694 int i;
5696 map = isl_map_cow(map);
5697 if (!map)
5698 return NULL;
5700 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5701 for (i = 0; i < map->n; ++i) {
5702 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5703 if (!map->p[i])
5704 goto error;
5706 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5707 return map;
5708 error:
5709 isl_map_free(map);
5710 return NULL;
5713 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5714 enum isl_dim_type type, unsigned pos, isl_int value)
5716 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5719 /* Fix the value of the variable at position "pos" of type "type" of "map"
5720 * to be equal to "v".
5722 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5723 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5725 int i;
5727 map = isl_map_cow(map);
5728 if (!map || !v)
5729 goto error;
5731 if (!isl_val_is_int(v))
5732 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5733 "expecting integer value", goto error);
5734 if (pos >= isl_map_dim(map, type))
5735 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5736 "index out of bounds", goto error);
5737 for (i = map->n - 1; i >= 0; --i) {
5738 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5739 isl_val_copy(v));
5740 if (remove_if_empty(map, i) < 0)
5741 goto error;
5743 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5744 isl_val_free(v);
5745 return map;
5746 error:
5747 isl_map_free(map);
5748 isl_val_free(v);
5749 return NULL;
5752 /* Fix the value of the variable at position "pos" of type "type" of "set"
5753 * to be equal to "v".
5755 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5756 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5758 return isl_map_fix_val(set, type, pos, v);
5761 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5762 unsigned input, int value)
5764 return isl_map_fix_si(map, isl_dim_in, input, value);
5767 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5769 return (struct isl_set *)
5770 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5773 static __isl_give isl_basic_map *basic_map_bound_si(
5774 __isl_take isl_basic_map *bmap,
5775 enum isl_dim_type type, unsigned pos, int value, int upper)
5777 int j;
5779 if (!bmap)
5780 return NULL;
5781 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5782 pos += isl_basic_map_offset(bmap, type);
5783 bmap = isl_basic_map_cow(bmap);
5784 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5785 j = isl_basic_map_alloc_inequality(bmap);
5786 if (j < 0)
5787 goto error;
5788 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5789 if (upper) {
5790 isl_int_set_si(bmap->ineq[j][pos], -1);
5791 isl_int_set_si(bmap->ineq[j][0], value);
5792 } else {
5793 isl_int_set_si(bmap->ineq[j][pos], 1);
5794 isl_int_set_si(bmap->ineq[j][0], -value);
5796 bmap = isl_basic_map_simplify(bmap);
5797 return isl_basic_map_finalize(bmap);
5798 error:
5799 isl_basic_map_free(bmap);
5800 return NULL;
5803 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5804 __isl_take isl_basic_map *bmap,
5805 enum isl_dim_type type, unsigned pos, int value)
5807 return basic_map_bound_si(bmap, type, pos, value, 0);
5810 /* Constrain the values of the given dimension to be no greater than "value".
5812 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5813 __isl_take isl_basic_map *bmap,
5814 enum isl_dim_type type, unsigned pos, int value)
5816 return basic_map_bound_si(bmap, type, pos, value, 1);
5819 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5820 unsigned dim, isl_int value)
5822 int j;
5824 bset = isl_basic_set_cow(bset);
5825 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5826 j = isl_basic_set_alloc_inequality(bset);
5827 if (j < 0)
5828 goto error;
5829 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5830 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5831 isl_int_neg(bset->ineq[j][0], value);
5832 bset = isl_basic_set_simplify(bset);
5833 return isl_basic_set_finalize(bset);
5834 error:
5835 isl_basic_set_free(bset);
5836 return NULL;
5839 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5840 enum isl_dim_type type, unsigned pos, int value, int upper)
5842 int i;
5844 map = isl_map_cow(map);
5845 if (!map)
5846 return NULL;
5848 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5849 for (i = 0; i < map->n; ++i) {
5850 map->p[i] = basic_map_bound_si(map->p[i],
5851 type, pos, value, upper);
5852 if (!map->p[i])
5853 goto error;
5855 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5856 return map;
5857 error:
5858 isl_map_free(map);
5859 return NULL;
5862 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5863 enum isl_dim_type type, unsigned pos, int value)
5865 return map_bound_si(map, type, pos, value, 0);
5868 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5869 enum isl_dim_type type, unsigned pos, int value)
5871 return map_bound_si(map, type, pos, value, 1);
5874 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5875 enum isl_dim_type type, unsigned pos, int value)
5877 return (struct isl_set *)
5878 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5881 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5882 enum isl_dim_type type, unsigned pos, int value)
5884 return isl_map_upper_bound_si(set, type, pos, value);
5887 /* Bound the given variable of "bmap" from below (or above is "upper"
5888 * is set) to "value".
5890 static __isl_give isl_basic_map *basic_map_bound(
5891 __isl_take isl_basic_map *bmap,
5892 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5894 int j;
5896 if (!bmap)
5897 return NULL;
5898 if (pos >= isl_basic_map_dim(bmap, type))
5899 isl_die(bmap->ctx, isl_error_invalid,
5900 "index out of bounds", goto error);
5901 pos += isl_basic_map_offset(bmap, type);
5902 bmap = isl_basic_map_cow(bmap);
5903 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5904 j = isl_basic_map_alloc_inequality(bmap);
5905 if (j < 0)
5906 goto error;
5907 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5908 if (upper) {
5909 isl_int_set_si(bmap->ineq[j][pos], -1);
5910 isl_int_set(bmap->ineq[j][0], value);
5911 } else {
5912 isl_int_set_si(bmap->ineq[j][pos], 1);
5913 isl_int_neg(bmap->ineq[j][0], value);
5915 bmap = isl_basic_map_simplify(bmap);
5916 return isl_basic_map_finalize(bmap);
5917 error:
5918 isl_basic_map_free(bmap);
5919 return NULL;
5922 /* Bound the given variable of "map" from below (or above is "upper"
5923 * is set) to "value".
5925 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5926 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5928 int i;
5930 map = isl_map_cow(map);
5931 if (!map)
5932 return NULL;
5934 if (pos >= isl_map_dim(map, type))
5935 isl_die(map->ctx, isl_error_invalid,
5936 "index out of bounds", goto error);
5937 for (i = map->n - 1; i >= 0; --i) {
5938 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5939 if (remove_if_empty(map, i) < 0)
5940 goto error;
5942 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5943 return map;
5944 error:
5945 isl_map_free(map);
5946 return NULL;
5949 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5950 enum isl_dim_type type, unsigned pos, isl_int value)
5952 return map_bound(map, type, pos, value, 0);
5955 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5956 enum isl_dim_type type, unsigned pos, isl_int value)
5958 return map_bound(map, type, pos, value, 1);
5961 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5962 enum isl_dim_type type, unsigned pos, isl_int value)
5964 return isl_map_lower_bound(set, type, pos, value);
5967 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5968 enum isl_dim_type type, unsigned pos, isl_int value)
5970 return isl_map_upper_bound(set, type, pos, value);
5973 /* Force the values of the variable at position "pos" of type "type" of "set"
5974 * to be no smaller than "value".
5976 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5977 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5979 if (!value)
5980 goto error;
5981 if (!isl_val_is_int(value))
5982 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5983 "expecting integer value", goto error);
5984 set = isl_set_lower_bound(set, type, pos, value->n);
5985 isl_val_free(value);
5986 return set;
5987 error:
5988 isl_val_free(value);
5989 isl_set_free(set);
5990 return NULL;
5993 /* Force the values of the variable at position "pos" of type "type" of "set"
5994 * to be no greater than "value".
5996 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5997 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5999 if (!value)
6000 goto error;
6001 if (!isl_val_is_int(value))
6002 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6003 "expecting integer value", goto error);
6004 set = isl_set_upper_bound(set, type, pos, value->n);
6005 isl_val_free(value);
6006 return set;
6007 error:
6008 isl_val_free(value);
6009 isl_set_free(set);
6010 return NULL;
6013 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6014 isl_int value)
6016 int i;
6018 set = isl_set_cow(set);
6019 if (!set)
6020 return NULL;
6022 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6023 for (i = 0; i < set->n; ++i) {
6024 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6025 if (!set->p[i])
6026 goto error;
6028 return set;
6029 error:
6030 isl_set_free(set);
6031 return NULL;
6034 struct isl_map *isl_map_reverse(struct isl_map *map)
6036 int i;
6038 map = isl_map_cow(map);
6039 if (!map)
6040 return NULL;
6042 map->dim = isl_space_reverse(map->dim);
6043 if (!map->dim)
6044 goto error;
6045 for (i = 0; i < map->n; ++i) {
6046 map->p[i] = isl_basic_map_reverse(map->p[i]);
6047 if (!map->p[i])
6048 goto error;
6050 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6051 return map;
6052 error:
6053 isl_map_free(map);
6054 return NULL;
6057 static struct isl_map *isl_basic_map_partial_lexopt(
6058 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6059 struct isl_set **empty, int max)
6061 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6064 struct isl_map *isl_basic_map_partial_lexmax(
6065 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6066 struct isl_set **empty)
6068 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6071 struct isl_map *isl_basic_map_partial_lexmin(
6072 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6073 struct isl_set **empty)
6075 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6078 struct isl_set *isl_basic_set_partial_lexmin(
6079 struct isl_basic_set *bset, struct isl_basic_set *dom,
6080 struct isl_set **empty)
6082 return (struct isl_set *)
6083 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6084 dom, empty);
6087 struct isl_set *isl_basic_set_partial_lexmax(
6088 struct isl_basic_set *bset, struct isl_basic_set *dom,
6089 struct isl_set **empty)
6091 return (struct isl_set *)
6092 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6093 dom, empty);
6096 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6097 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6098 __isl_give isl_set **empty)
6100 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6103 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6104 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6105 __isl_give isl_set **empty)
6107 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6110 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6111 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6112 __isl_give isl_set **empty)
6114 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6117 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6118 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6119 __isl_give isl_set **empty)
6121 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6124 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6125 __isl_take isl_basic_map *bmap, int max)
6127 isl_basic_set *dom = NULL;
6128 isl_space *dom_space;
6130 if (!bmap)
6131 goto error;
6132 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6133 dom = isl_basic_set_universe(dom_space);
6134 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6135 error:
6136 isl_basic_map_free(bmap);
6137 return NULL;
6140 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6141 __isl_take isl_basic_map *bmap)
6143 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6146 #undef TYPE
6147 #define TYPE isl_pw_multi_aff
6148 #undef SUFFIX
6149 #define SUFFIX _pw_multi_aff
6150 #undef EMPTY
6151 #define EMPTY isl_pw_multi_aff_empty
6152 #undef ADD
6153 #define ADD isl_pw_multi_aff_union_add
6154 #include "isl_map_lexopt_templ.c"
6156 /* Given a map "map", compute the lexicographically minimal
6157 * (or maximal) image element for each domain element in dom,
6158 * in the form of an isl_pw_multi_aff.
6159 * If "empty" is not NULL, then set *empty to those elements in dom that
6160 * do not have an image element.
6162 * We first compute the lexicographically minimal or maximal element
6163 * in the first basic map. This results in a partial solution "res"
6164 * and a subset "todo" of dom that still need to be handled.
6165 * We then consider each of the remaining maps in "map" and successively
6166 * update both "res" and "todo".
6167 * If "empty" is NULL, then the todo sets are not needed and therefore
6168 * also not computed.
6170 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6171 __isl_take isl_map *map, __isl_take isl_set *dom,
6172 __isl_give isl_set **empty, int max)
6174 int i;
6175 isl_pw_multi_aff *res;
6176 isl_set *todo;
6178 if (!map || !dom)
6179 goto error;
6181 if (isl_map_plain_is_empty(map)) {
6182 if (empty)
6183 *empty = dom;
6184 else
6185 isl_set_free(dom);
6186 return isl_pw_multi_aff_from_map(map);
6189 res = basic_map_partial_lexopt_pw_multi_aff(
6190 isl_basic_map_copy(map->p[0]),
6191 isl_set_copy(dom), empty, max);
6193 if (empty)
6194 todo = *empty;
6195 for (i = 1; i < map->n; ++i) {
6196 isl_pw_multi_aff *res_i;
6198 res_i = basic_map_partial_lexopt_pw_multi_aff(
6199 isl_basic_map_copy(map->p[i]),
6200 isl_set_copy(dom), empty, max);
6202 if (max)
6203 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6204 else
6205 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6207 if (empty)
6208 todo = isl_set_intersect(todo, *empty);
6211 isl_set_free(dom);
6212 isl_map_free(map);
6214 if (empty)
6215 *empty = todo;
6217 return res;
6218 error:
6219 if (empty)
6220 *empty = NULL;
6221 isl_set_free(dom);
6222 isl_map_free(map);
6223 return NULL;
6226 #undef TYPE
6227 #define TYPE isl_map
6228 #undef SUFFIX
6229 #define SUFFIX
6230 #undef EMPTY
6231 #define EMPTY isl_map_empty
6232 #undef ADD
6233 #define ADD isl_map_union_disjoint
6234 #include "isl_map_lexopt_templ.c"
6236 /* Given a map "map", compute the lexicographically minimal
6237 * (or maximal) image element for each domain element in "dom",
6238 * in the form of an isl_map.
6239 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6240 * do not have an image element.
6242 * If the input consists of more than one disjunct, then first
6243 * compute the desired result in the form of an isl_pw_multi_aff and
6244 * then convert that into an isl_map.
6246 * This function used to have an explicit implementation in terms
6247 * of isl_maps, but it would continually intersect the domains of
6248 * partial results with the complement of the domain of the next
6249 * partial solution, potentially leading to an explosion in the number
6250 * of disjuncts if there are several disjuncts in the input.
6251 * An even earlier implementation of this function would look for
6252 * better results in the domain of the partial result and for extra
6253 * results in the complement of this domain, which would lead to
6254 * even more splintering.
6256 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6257 __isl_take isl_map *map, __isl_take isl_set *dom,
6258 __isl_give isl_set **empty, int max)
6260 struct isl_map *res;
6261 isl_pw_multi_aff *pma;
6263 if (!map || !dom)
6264 goto error;
6266 if (isl_map_plain_is_empty(map)) {
6267 if (empty)
6268 *empty = dom;
6269 else
6270 isl_set_free(dom);
6271 return map;
6274 if (map->n == 1) {
6275 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6276 dom, empty, max);
6277 isl_map_free(map);
6278 return res;
6281 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, max);
6282 return isl_map_from_pw_multi_aff(pma);
6283 error:
6284 if (empty)
6285 *empty = NULL;
6286 isl_set_free(dom);
6287 isl_map_free(map);
6288 return NULL;
6291 __isl_give isl_map *isl_map_partial_lexmax(
6292 __isl_take isl_map *map, __isl_take isl_set *dom,
6293 __isl_give isl_set **empty)
6295 return isl_map_partial_lexopt(map, dom, empty, 1);
6298 __isl_give isl_map *isl_map_partial_lexmin(
6299 __isl_take isl_map *map, __isl_take isl_set *dom,
6300 __isl_give isl_set **empty)
6302 return isl_map_partial_lexopt(map, dom, empty, 0);
6305 __isl_give isl_set *isl_set_partial_lexmin(
6306 __isl_take isl_set *set, __isl_take isl_set *dom,
6307 __isl_give isl_set **empty)
6309 return (struct isl_set *)
6310 isl_map_partial_lexmin((struct isl_map *)set,
6311 dom, empty);
6314 __isl_give isl_set *isl_set_partial_lexmax(
6315 __isl_take isl_set *set, __isl_take isl_set *dom,
6316 __isl_give isl_set **empty)
6318 return (struct isl_set *)
6319 isl_map_partial_lexmax((struct isl_map *)set,
6320 dom, empty);
6323 /* Compute the lexicographic minimum (or maximum if "max" is set)
6324 * of "bmap" over its domain.
6326 * Since we are not interested in the part of the domain space where
6327 * there is no solution, we initialize the domain to those constraints
6328 * of "bmap" that only involve the parameters and the input dimensions.
6329 * This relieves the parametric programming engine from detecting those
6330 * inequalities and transferring them to the context. More importantly,
6331 * it ensures that those inequalities are transferred first and not
6332 * intermixed with inequalities that actually split the domain.
6334 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6336 int n_div;
6337 int n_out;
6338 isl_basic_map *copy;
6339 isl_basic_set *dom;
6341 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6342 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6343 copy = isl_basic_map_copy(bmap);
6344 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6345 isl_dim_div, 0, n_div);
6346 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6347 isl_dim_out, 0, n_out);
6348 dom = isl_basic_map_domain(copy);
6349 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6352 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6354 return isl_basic_map_lexopt(bmap, 0);
6357 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6359 return isl_basic_map_lexopt(bmap, 1);
6362 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6364 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6367 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6369 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6372 /* Extract the first and only affine expression from list
6373 * and then add it to *pwaff with the given dom.
6374 * This domain is known to be disjoint from other domains
6375 * because of the way isl_basic_map_foreach_lexmax works.
6377 static int update_dim_opt(__isl_take isl_basic_set *dom,
6378 __isl_take isl_aff_list *list, void *user)
6380 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6381 isl_aff *aff;
6382 isl_pw_aff **pwaff = user;
6383 isl_pw_aff *pwaff_i;
6385 if (!list)
6386 goto error;
6387 if (isl_aff_list_n_aff(list) != 1)
6388 isl_die(ctx, isl_error_internal,
6389 "expecting single element list", goto error);
6391 aff = isl_aff_list_get_aff(list, 0);
6392 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6394 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6396 isl_aff_list_free(list);
6398 return 0;
6399 error:
6400 isl_basic_set_free(dom);
6401 isl_aff_list_free(list);
6402 return -1;
6405 /* Given a basic map with one output dimension, compute the minimum or
6406 * maximum of that dimension as an isl_pw_aff.
6408 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6409 * call update_dim_opt on each leaf of the result.
6411 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6412 int max)
6414 isl_space *dim = isl_basic_map_get_space(bmap);
6415 isl_pw_aff *pwaff;
6416 int r;
6418 dim = isl_space_from_domain(isl_space_domain(dim));
6419 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6420 pwaff = isl_pw_aff_empty(dim);
6422 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6423 if (r < 0)
6424 return isl_pw_aff_free(pwaff);
6426 return pwaff;
6429 /* Compute the minimum or maximum of the given output dimension
6430 * as a function of the parameters and the input dimensions,
6431 * but independently of the other output dimensions.
6433 * We first project out the other output dimension and then compute
6434 * the "lexicographic" maximum in each basic map, combining the results
6435 * using isl_pw_aff_union_max.
6437 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6438 int max)
6440 int i;
6441 isl_pw_aff *pwaff;
6442 unsigned n_out;
6444 n_out = isl_map_dim(map, isl_dim_out);
6445 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6446 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6447 if (!map)
6448 return NULL;
6450 if (map->n == 0) {
6451 isl_space *dim = isl_map_get_space(map);
6452 isl_map_free(map);
6453 return isl_pw_aff_empty(dim);
6456 pwaff = basic_map_dim_opt(map->p[0], max);
6457 for (i = 1; i < map->n; ++i) {
6458 isl_pw_aff *pwaff_i;
6460 pwaff_i = basic_map_dim_opt(map->p[i], max);
6461 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6464 isl_map_free(map);
6466 return pwaff;
6469 /* Compute the maximum of the given output dimension as a function of the
6470 * parameters and input dimensions, but independently of
6471 * the other output dimensions.
6473 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6475 return map_dim_opt(map, pos, 1);
6478 /* Compute the minimum or maximum of the given set dimension
6479 * as a function of the parameters,
6480 * but independently of the other set dimensions.
6482 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6483 int max)
6485 return map_dim_opt(set, pos, max);
6488 /* Compute the maximum of the given set dimension as a function of the
6489 * parameters, but independently of the other set dimensions.
6491 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6493 return set_dim_opt(set, pos, 1);
6496 /* Compute the minimum of the given set dimension as a function of the
6497 * parameters, but independently of the other set dimensions.
6499 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6501 return set_dim_opt(set, pos, 0);
6504 /* Apply a preimage specified by "mat" on the parameters of "bset".
6505 * bset is assumed to have only parameters and divs.
6507 static struct isl_basic_set *basic_set_parameter_preimage(
6508 struct isl_basic_set *bset, struct isl_mat *mat)
6510 unsigned nparam;
6512 if (!bset || !mat)
6513 goto error;
6515 bset->dim = isl_space_cow(bset->dim);
6516 if (!bset->dim)
6517 goto error;
6519 nparam = isl_basic_set_dim(bset, isl_dim_param);
6521 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6523 bset->dim->nparam = 0;
6524 bset->dim->n_out = nparam;
6525 bset = isl_basic_set_preimage(bset, mat);
6526 if (bset) {
6527 bset->dim->nparam = bset->dim->n_out;
6528 bset->dim->n_out = 0;
6530 return bset;
6531 error:
6532 isl_mat_free(mat);
6533 isl_basic_set_free(bset);
6534 return NULL;
6537 /* Apply a preimage specified by "mat" on the parameters of "set".
6538 * set is assumed to have only parameters and divs.
6540 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6541 __isl_take isl_mat *mat)
6543 isl_space *space;
6544 unsigned nparam;
6546 if (!set || !mat)
6547 goto error;
6549 nparam = isl_set_dim(set, isl_dim_param);
6551 if (mat->n_row != 1 + nparam)
6552 isl_die(isl_set_get_ctx(set), isl_error_internal,
6553 "unexpected number of rows", goto error);
6555 space = isl_set_get_space(set);
6556 space = isl_space_move_dims(space, isl_dim_set, 0,
6557 isl_dim_param, 0, nparam);
6558 set = isl_set_reset_space(set, space);
6559 set = isl_set_preimage(set, mat);
6560 nparam = isl_set_dim(set, isl_dim_out);
6561 space = isl_set_get_space(set);
6562 space = isl_space_move_dims(space, isl_dim_param, 0,
6563 isl_dim_out, 0, nparam);
6564 set = isl_set_reset_space(set, space);
6565 return set;
6566 error:
6567 isl_mat_free(mat);
6568 isl_set_free(set);
6569 return NULL;
6572 /* Intersect the basic set "bset" with the affine space specified by the
6573 * equalities in "eq".
6575 static struct isl_basic_set *basic_set_append_equalities(
6576 struct isl_basic_set *bset, struct isl_mat *eq)
6578 int i, k;
6579 unsigned len;
6581 if (!bset || !eq)
6582 goto error;
6584 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6585 eq->n_row, 0);
6586 if (!bset)
6587 goto error;
6589 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6590 for (i = 0; i < eq->n_row; ++i) {
6591 k = isl_basic_set_alloc_equality(bset);
6592 if (k < 0)
6593 goto error;
6594 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6595 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6597 isl_mat_free(eq);
6599 bset = isl_basic_set_gauss(bset, NULL);
6600 bset = isl_basic_set_finalize(bset);
6602 return bset;
6603 error:
6604 isl_mat_free(eq);
6605 isl_basic_set_free(bset);
6606 return NULL;
6609 /* Intersect the set "set" with the affine space specified by the
6610 * equalities in "eq".
6612 static struct isl_set *set_append_equalities(struct isl_set *set,
6613 struct isl_mat *eq)
6615 int i;
6617 if (!set || !eq)
6618 goto error;
6620 for (i = 0; i < set->n; ++i) {
6621 set->p[i] = basic_set_append_equalities(set->p[i],
6622 isl_mat_copy(eq));
6623 if (!set->p[i])
6624 goto error;
6626 isl_mat_free(eq);
6627 return set;
6628 error:
6629 isl_mat_free(eq);
6630 isl_set_free(set);
6631 return NULL;
6634 /* Given a basic set "bset" that only involves parameters and existentially
6635 * quantified variables, return the index of the first equality
6636 * that only involves parameters. If there is no such equality then
6637 * return bset->n_eq.
6639 * This function assumes that isl_basic_set_gauss has been called on "bset".
6641 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6643 int i, j;
6644 unsigned nparam, n_div;
6646 if (!bset)
6647 return -1;
6649 nparam = isl_basic_set_dim(bset, isl_dim_param);
6650 n_div = isl_basic_set_dim(bset, isl_dim_div);
6652 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6653 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6654 ++i;
6657 return i;
6660 /* Compute an explicit representation for the existentially quantified
6661 * variables in "bset" by computing the "minimal value" of the set
6662 * variables. Since there are no set variables, the computation of
6663 * the minimal value essentially computes an explicit representation
6664 * of the non-empty part(s) of "bset".
6666 * The input only involves parameters and existentially quantified variables.
6667 * All equalities among parameters have been removed.
6669 * Since the existentially quantified variables in the result are in general
6670 * going to be different from those in the input, we first replace
6671 * them by the minimal number of variables based on their equalities.
6672 * This should simplify the parametric integer programming.
6674 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6676 isl_morph *morph1, *morph2;
6677 isl_set *set;
6678 unsigned n;
6680 if (!bset)
6681 return NULL;
6682 if (bset->n_eq == 0)
6683 return isl_basic_set_lexmin(bset);
6685 morph1 = isl_basic_set_parameter_compression(bset);
6686 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6687 bset = isl_basic_set_lift(bset);
6688 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6689 bset = isl_morph_basic_set(morph2, bset);
6690 n = isl_basic_set_dim(bset, isl_dim_set);
6691 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6693 set = isl_basic_set_lexmin(bset);
6695 set = isl_morph_set(isl_morph_inverse(morph1), set);
6697 return set;
6700 /* Project the given basic set onto its parameter domain, possibly introducing
6701 * new, explicit, existential variables in the constraints.
6702 * The input has parameters and (possibly implicit) existential variables.
6703 * The output has the same parameters, but only
6704 * explicit existentially quantified variables.
6706 * The actual projection is performed by pip, but pip doesn't seem
6707 * to like equalities very much, so we first remove the equalities
6708 * among the parameters by performing a variable compression on
6709 * the parameters. Afterward, an inverse transformation is performed
6710 * and the equalities among the parameters are inserted back in.
6712 * The variable compression on the parameters may uncover additional
6713 * equalities that were only implicit before. We therefore check
6714 * if there are any new parameter equalities in the result and
6715 * if so recurse. The removal of parameter equalities is required
6716 * for the parameter compression performed by base_compute_divs.
6718 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6720 int i;
6721 struct isl_mat *eq;
6722 struct isl_mat *T, *T2;
6723 struct isl_set *set;
6724 unsigned nparam;
6726 bset = isl_basic_set_cow(bset);
6727 if (!bset)
6728 return NULL;
6730 if (bset->n_eq == 0)
6731 return base_compute_divs(bset);
6733 bset = isl_basic_set_gauss(bset, NULL);
6734 if (!bset)
6735 return NULL;
6736 if (isl_basic_set_plain_is_empty(bset))
6737 return isl_set_from_basic_set(bset);
6739 i = first_parameter_equality(bset);
6740 if (i == bset->n_eq)
6741 return base_compute_divs(bset);
6743 nparam = isl_basic_set_dim(bset, isl_dim_param);
6744 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6745 0, 1 + nparam);
6746 eq = isl_mat_cow(eq);
6747 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6748 if (T && T->n_col == 0) {
6749 isl_mat_free(T);
6750 isl_mat_free(T2);
6751 isl_mat_free(eq);
6752 bset = isl_basic_set_set_to_empty(bset);
6753 return isl_set_from_basic_set(bset);
6755 bset = basic_set_parameter_preimage(bset, T);
6757 i = first_parameter_equality(bset);
6758 if (!bset)
6759 set = NULL;
6760 else if (i == bset->n_eq)
6761 set = base_compute_divs(bset);
6762 else
6763 set = parameter_compute_divs(bset);
6764 set = set_parameter_preimage(set, T2);
6765 set = set_append_equalities(set, eq);
6766 return set;
6769 /* Insert the divs from "ls" before those of "bmap".
6771 * The number of columns is not changed, which means that the last
6772 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6773 * The caller is responsible for removing the same number of dimensions
6774 * from the space of "bmap".
6776 static __isl_give isl_basic_map *insert_divs_from_local_space(
6777 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6779 int i;
6780 int n_div;
6781 int old_n_div;
6783 n_div = isl_local_space_dim(ls, isl_dim_div);
6784 if (n_div == 0)
6785 return bmap;
6787 old_n_div = bmap->n_div;
6788 bmap = insert_div_rows(bmap, n_div);
6789 if (!bmap)
6790 return NULL;
6792 for (i = 0; i < n_div; ++i) {
6793 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6794 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6797 return bmap;
6800 /* Replace the space of "bmap" by the space and divs of "ls".
6802 * If "ls" has any divs, then we simplify the result since we may
6803 * have discovered some additional equalities that could simplify
6804 * the div expressions.
6806 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6807 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6809 int n_div;
6811 bmap = isl_basic_map_cow(bmap);
6812 if (!bmap || !ls)
6813 goto error;
6815 n_div = isl_local_space_dim(ls, isl_dim_div);
6816 bmap = insert_divs_from_local_space(bmap, ls);
6817 if (!bmap)
6818 goto error;
6820 isl_space_free(bmap->dim);
6821 bmap->dim = isl_local_space_get_space(ls);
6822 if (!bmap->dim)
6823 goto error;
6825 isl_local_space_free(ls);
6826 if (n_div > 0)
6827 bmap = isl_basic_map_simplify(bmap);
6828 bmap = isl_basic_map_finalize(bmap);
6829 return bmap;
6830 error:
6831 isl_basic_map_free(bmap);
6832 isl_local_space_free(ls);
6833 return NULL;
6836 /* Replace the space of "map" by the space and divs of "ls".
6838 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6839 __isl_take isl_local_space *ls)
6841 int i;
6843 map = isl_map_cow(map);
6844 if (!map || !ls)
6845 goto error;
6847 for (i = 0; i < map->n; ++i) {
6848 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6849 isl_local_space_copy(ls));
6850 if (!map->p[i])
6851 goto error;
6853 isl_space_free(map->dim);
6854 map->dim = isl_local_space_get_space(ls);
6855 if (!map->dim)
6856 goto error;
6858 isl_local_space_free(ls);
6859 return map;
6860 error:
6861 isl_local_space_free(ls);
6862 isl_map_free(map);
6863 return NULL;
6866 /* Compute an explicit representation for the existentially
6867 * quantified variables for which do not know any explicit representation yet.
6869 * We first sort the existentially quantified variables so that the
6870 * existentially quantified variables for which we already have an explicit
6871 * representation are placed before those for which we do not.
6872 * The input dimensions, the output dimensions and the existentially
6873 * quantified variables for which we already have an explicit
6874 * representation are then turned into parameters.
6875 * compute_divs returns a map with the same parameters and
6876 * no input or output dimensions and the dimension specification
6877 * is reset to that of the input, including the existentially quantified
6878 * variables for which we already had an explicit representation.
6880 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6882 struct isl_basic_set *bset;
6883 struct isl_set *set;
6884 struct isl_map *map;
6885 isl_space *dim;
6886 isl_local_space *ls;
6887 unsigned nparam;
6888 unsigned n_in;
6889 unsigned n_out;
6890 int n_known;
6891 int i;
6893 bmap = isl_basic_map_sort_divs(bmap);
6894 bmap = isl_basic_map_cow(bmap);
6895 if (!bmap)
6896 return NULL;
6898 n_known = isl_basic_map_first_unknown_div(bmap);
6899 if (n_known < 0)
6900 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6902 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6903 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6904 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6905 dim = isl_space_set_alloc(bmap->ctx,
6906 nparam + n_in + n_out + n_known, 0);
6907 if (!dim)
6908 goto error;
6910 ls = isl_basic_map_get_local_space(bmap);
6911 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6912 n_known, bmap->n_div - n_known);
6913 if (n_known > 0) {
6914 for (i = n_known; i < bmap->n_div; ++i)
6915 swap_div(bmap, i - n_known, i);
6916 bmap->n_div -= n_known;
6917 bmap->extra -= n_known;
6919 bmap = isl_basic_map_reset_space(bmap, dim);
6920 bset = (struct isl_basic_set *)bmap;
6922 set = parameter_compute_divs(bset);
6923 map = (struct isl_map *)set;
6924 map = replace_space_by_local_space(map, ls);
6926 return map;
6927 error:
6928 isl_basic_map_free(bmap);
6929 return NULL;
6932 /* Remove the explicit representation of local variable "div",
6933 * if there is any.
6935 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6936 __isl_take isl_basic_map *bmap, int div)
6938 isl_bool known;
6940 known = isl_basic_map_div_is_known(bmap, div);
6941 if (known < 0)
6942 return isl_basic_map_free(bmap);
6943 if (!known)
6944 return bmap;
6946 bmap = isl_basic_map_cow(bmap);
6947 if (!bmap)
6948 return NULL;
6949 isl_int_set_si(bmap->div[div][0], 0);
6950 return bmap;
6953 /* Does local variable "div" of "bmap" have an explicit representation?
6955 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
6957 if (!bmap)
6958 return isl_bool_error;
6959 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6960 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6961 "position out of bounds", return isl_bool_error);
6962 return !isl_int_is_zero(bmap->div[div][0]);
6965 /* Return the position of the first local variable that does not
6966 * have an explicit representation.
6967 * Return the total number of local variables if they all have
6968 * an explicit representation.
6969 * Return -1 on error.
6971 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6973 int i;
6975 if (!bmap)
6976 return -1;
6978 for (i = 0; i < bmap->n_div; ++i) {
6979 if (!isl_basic_map_div_is_known(bmap, i))
6980 return i;
6982 return bmap->n_div;
6985 /* Does "bmap" have an explicit representation for all local variables?
6987 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6989 int first, n;
6991 n = isl_basic_map_dim(bmap, isl_dim_div);
6992 first = isl_basic_map_first_unknown_div(bmap);
6993 if (first < 0)
6994 return isl_bool_error;
6995 return first == n;
6998 /* Do all basic maps in "map" have an explicit representation
6999 * for all local variables?
7001 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7003 int i;
7005 if (!map)
7006 return isl_bool_error;
7008 for (i = 0; i < map->n; ++i) {
7009 int known = isl_basic_map_divs_known(map->p[i]);
7010 if (known <= 0)
7011 return known;
7014 return isl_bool_true;
7017 /* If bmap contains any unknown divs, then compute explicit
7018 * expressions for them. However, this computation may be
7019 * quite expensive, so first try to remove divs that aren't
7020 * strictly needed.
7022 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7024 int known;
7025 struct isl_map *map;
7027 known = isl_basic_map_divs_known(bmap);
7028 if (known < 0)
7029 goto error;
7030 if (known)
7031 return isl_map_from_basic_map(bmap);
7033 bmap = isl_basic_map_drop_redundant_divs(bmap);
7035 known = isl_basic_map_divs_known(bmap);
7036 if (known < 0)
7037 goto error;
7038 if (known)
7039 return isl_map_from_basic_map(bmap);
7041 map = compute_divs(bmap);
7042 return map;
7043 error:
7044 isl_basic_map_free(bmap);
7045 return NULL;
7048 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7050 int i;
7051 int known;
7052 struct isl_map *res;
7054 if (!map)
7055 return NULL;
7056 if (map->n == 0)
7057 return map;
7059 known = isl_map_divs_known(map);
7060 if (known < 0) {
7061 isl_map_free(map);
7062 return NULL;
7064 if (known)
7065 return map;
7067 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7068 for (i = 1 ; i < map->n; ++i) {
7069 struct isl_map *r2;
7070 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7071 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7072 res = isl_map_union_disjoint(res, r2);
7073 else
7074 res = isl_map_union(res, r2);
7076 isl_map_free(map);
7078 return res;
7081 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7083 return (struct isl_set *)
7084 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7087 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7089 return (struct isl_set *)
7090 isl_map_compute_divs((struct isl_map *)set);
7093 struct isl_set *isl_map_domain(struct isl_map *map)
7095 int i;
7096 struct isl_set *set;
7098 if (!map)
7099 goto error;
7101 map = isl_map_cow(map);
7102 if (!map)
7103 return NULL;
7105 set = (struct isl_set *)map;
7106 set->dim = isl_space_domain(set->dim);
7107 if (!set->dim)
7108 goto error;
7109 for (i = 0; i < map->n; ++i) {
7110 set->p[i] = isl_basic_map_domain(map->p[i]);
7111 if (!set->p[i])
7112 goto error;
7114 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7115 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7116 return set;
7117 error:
7118 isl_map_free(map);
7119 return NULL;
7122 /* Return the union of "map1" and "map2", where we assume for now that
7123 * "map1" and "map2" are disjoint. Note that the basic maps inside
7124 * "map1" or "map2" may not be disjoint from each other.
7125 * Also note that this function is also called from isl_map_union,
7126 * which takes care of handling the situation where "map1" and "map2"
7127 * may not be disjoint.
7129 * If one of the inputs is empty, we can simply return the other input.
7130 * Similarly, if one of the inputs is universal, then it is equal to the union.
7132 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7133 __isl_take isl_map *map2)
7135 int i;
7136 unsigned flags = 0;
7137 struct isl_map *map = NULL;
7138 int is_universe;
7140 if (!map1 || !map2)
7141 goto error;
7143 if (!isl_space_is_equal(map1->dim, map2->dim))
7144 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7145 "spaces don't match", goto error);
7147 if (map1->n == 0) {
7148 isl_map_free(map1);
7149 return map2;
7151 if (map2->n == 0) {
7152 isl_map_free(map2);
7153 return map1;
7156 is_universe = isl_map_plain_is_universe(map1);
7157 if (is_universe < 0)
7158 goto error;
7159 if (is_universe) {
7160 isl_map_free(map2);
7161 return map1;
7164 is_universe = isl_map_plain_is_universe(map2);
7165 if (is_universe < 0)
7166 goto error;
7167 if (is_universe) {
7168 isl_map_free(map1);
7169 return map2;
7172 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7173 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7174 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7176 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7177 map1->n + map2->n, flags);
7178 if (!map)
7179 goto error;
7180 for (i = 0; i < map1->n; ++i) {
7181 map = isl_map_add_basic_map(map,
7182 isl_basic_map_copy(map1->p[i]));
7183 if (!map)
7184 goto error;
7186 for (i = 0; i < map2->n; ++i) {
7187 map = isl_map_add_basic_map(map,
7188 isl_basic_map_copy(map2->p[i]));
7189 if (!map)
7190 goto error;
7192 isl_map_free(map1);
7193 isl_map_free(map2);
7194 return map;
7195 error:
7196 isl_map_free(map);
7197 isl_map_free(map1);
7198 isl_map_free(map2);
7199 return NULL;
7202 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7203 * guaranteed to be disjoint by the caller.
7205 * Note that this functions is called from within isl_map_make_disjoint,
7206 * so we have to be careful not to touch the constraints of the inputs
7207 * in any way.
7209 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7210 __isl_take isl_map *map2)
7212 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7215 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7216 * not be disjoint. The parameters are assumed to have been aligned.
7218 * We currently simply call map_union_disjoint, the internal operation
7219 * of which does not really depend on the inputs being disjoint.
7220 * If the result contains more than one basic map, then we clear
7221 * the disjoint flag since the result may contain basic maps from
7222 * both inputs and these are not guaranteed to be disjoint.
7224 * As a special case, if "map1" and "map2" are obviously equal,
7225 * then we simply return "map1".
7227 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7228 __isl_take isl_map *map2)
7230 int equal;
7232 if (!map1 || !map2)
7233 goto error;
7235 equal = isl_map_plain_is_equal(map1, map2);
7236 if (equal < 0)
7237 goto error;
7238 if (equal) {
7239 isl_map_free(map2);
7240 return map1;
7243 map1 = map_union_disjoint(map1, map2);
7244 if (!map1)
7245 return NULL;
7246 if (map1->n > 1)
7247 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7248 return map1;
7249 error:
7250 isl_map_free(map1);
7251 isl_map_free(map2);
7252 return NULL;
7255 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7256 * not be disjoint.
7258 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7259 __isl_take isl_map *map2)
7261 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7264 struct isl_set *isl_set_union_disjoint(
7265 struct isl_set *set1, struct isl_set *set2)
7267 return (struct isl_set *)
7268 isl_map_union_disjoint(
7269 (struct isl_map *)set1, (struct isl_map *)set2);
7272 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7274 return (struct isl_set *)
7275 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7278 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7279 * the results.
7281 * "map" and "set" are assumed to be compatible and non-NULL.
7283 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7284 __isl_take isl_set *set,
7285 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7286 __isl_take isl_basic_set *bset))
7288 unsigned flags = 0;
7289 struct isl_map *result;
7290 int i, j;
7292 if (isl_set_plain_is_universe(set)) {
7293 isl_set_free(set);
7294 return map;
7297 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7298 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7299 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7301 result = isl_map_alloc_space(isl_space_copy(map->dim),
7302 map->n * set->n, flags);
7303 for (i = 0; result && i < map->n; ++i)
7304 for (j = 0; j < set->n; ++j) {
7305 result = isl_map_add_basic_map(result,
7306 fn(isl_basic_map_copy(map->p[i]),
7307 isl_basic_set_copy(set->p[j])));
7308 if (!result)
7309 break;
7312 isl_map_free(map);
7313 isl_set_free(set);
7314 return result;
7317 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7318 __isl_take isl_set *set)
7320 if (!map || !set)
7321 goto error;
7323 if (!isl_map_compatible_range(map, set))
7324 isl_die(set->ctx, isl_error_invalid,
7325 "incompatible spaces", goto error);
7327 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7328 error:
7329 isl_map_free(map);
7330 isl_set_free(set);
7331 return NULL;
7334 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7335 __isl_take isl_set *set)
7337 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7340 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7341 __isl_take isl_set *set)
7343 if (!map || !set)
7344 goto error;
7346 if (!isl_map_compatible_domain(map, set))
7347 isl_die(set->ctx, isl_error_invalid,
7348 "incompatible spaces", goto error);
7350 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7351 error:
7352 isl_map_free(map);
7353 isl_set_free(set);
7354 return NULL;
7357 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7358 __isl_take isl_set *set)
7360 return isl_map_align_params_map_map_and(map, set,
7361 &map_intersect_domain);
7364 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7365 __isl_take isl_map *map2)
7367 if (!map1 || !map2)
7368 goto error;
7369 map1 = isl_map_reverse(map1);
7370 map1 = isl_map_apply_range(map1, map2);
7371 return isl_map_reverse(map1);
7372 error:
7373 isl_map_free(map1);
7374 isl_map_free(map2);
7375 return NULL;
7378 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7379 __isl_take isl_map *map2)
7381 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7384 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7385 __isl_take isl_map *map2)
7387 isl_space *dim_result;
7388 struct isl_map *result;
7389 int i, j;
7391 if (!map1 || !map2)
7392 goto error;
7394 dim_result = isl_space_join(isl_space_copy(map1->dim),
7395 isl_space_copy(map2->dim));
7397 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7398 if (!result)
7399 goto error;
7400 for (i = 0; i < map1->n; ++i)
7401 for (j = 0; j < map2->n; ++j) {
7402 result = isl_map_add_basic_map(result,
7403 isl_basic_map_apply_range(
7404 isl_basic_map_copy(map1->p[i]),
7405 isl_basic_map_copy(map2->p[j])));
7406 if (!result)
7407 goto error;
7409 isl_map_free(map1);
7410 isl_map_free(map2);
7411 if (result && result->n <= 1)
7412 ISL_F_SET(result, ISL_MAP_DISJOINT);
7413 return result;
7414 error:
7415 isl_map_free(map1);
7416 isl_map_free(map2);
7417 return NULL;
7420 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7421 __isl_take isl_map *map2)
7423 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7427 * returns range - domain
7429 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7431 isl_space *target_space;
7432 struct isl_basic_set *bset;
7433 unsigned dim;
7434 unsigned nparam;
7435 int i;
7437 if (!bmap)
7438 goto error;
7439 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7440 bmap->dim, isl_dim_out),
7441 goto error);
7442 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7443 dim = isl_basic_map_n_in(bmap);
7444 nparam = isl_basic_map_n_param(bmap);
7445 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7446 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7447 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7448 for (i = 0; i < dim; ++i) {
7449 int j = isl_basic_map_alloc_equality(bmap);
7450 if (j < 0) {
7451 bmap = isl_basic_map_free(bmap);
7452 break;
7454 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7455 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7456 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7457 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7459 bset = isl_basic_map_domain(bmap);
7460 bset = isl_basic_set_reset_space(bset, target_space);
7461 return bset;
7462 error:
7463 isl_basic_map_free(bmap);
7464 return NULL;
7468 * returns range - domain
7470 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7472 int i;
7473 isl_space *dim;
7474 struct isl_set *result;
7476 if (!map)
7477 return NULL;
7479 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7480 map->dim, isl_dim_out),
7481 goto error);
7482 dim = isl_map_get_space(map);
7483 dim = isl_space_domain(dim);
7484 result = isl_set_alloc_space(dim, map->n, 0);
7485 if (!result)
7486 goto error;
7487 for (i = 0; i < map->n; ++i)
7488 result = isl_set_add_basic_set(result,
7489 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7490 isl_map_free(map);
7491 return result;
7492 error:
7493 isl_map_free(map);
7494 return NULL;
7498 * returns [domain -> range] -> range - domain
7500 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7501 __isl_take isl_basic_map *bmap)
7503 int i, k;
7504 isl_space *dim;
7505 isl_basic_map *domain;
7506 int nparam, n;
7507 unsigned total;
7509 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7510 bmap->dim, isl_dim_out))
7511 isl_die(bmap->ctx, isl_error_invalid,
7512 "domain and range don't match", goto error);
7514 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7515 n = isl_basic_map_dim(bmap, isl_dim_in);
7517 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7518 domain = isl_basic_map_universe(dim);
7520 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7521 bmap = isl_basic_map_apply_range(bmap, domain);
7522 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7524 total = isl_basic_map_total_dim(bmap);
7526 for (i = 0; i < n; ++i) {
7527 k = isl_basic_map_alloc_equality(bmap);
7528 if (k < 0)
7529 goto error;
7530 isl_seq_clr(bmap->eq[k], 1 + total);
7531 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7532 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7533 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7536 bmap = isl_basic_map_gauss(bmap, NULL);
7537 return isl_basic_map_finalize(bmap);
7538 error:
7539 isl_basic_map_free(bmap);
7540 return NULL;
7544 * returns [domain -> range] -> range - domain
7546 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7548 int i;
7549 isl_space *domain_dim;
7551 if (!map)
7552 return NULL;
7554 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7555 map->dim, isl_dim_out))
7556 isl_die(map->ctx, isl_error_invalid,
7557 "domain and range don't match", goto error);
7559 map = isl_map_cow(map);
7560 if (!map)
7561 return NULL;
7563 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7564 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7565 map->dim = isl_space_join(map->dim, domain_dim);
7566 if (!map->dim)
7567 goto error;
7568 for (i = 0; i < map->n; ++i) {
7569 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7570 if (!map->p[i])
7571 goto error;
7573 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7574 return map;
7575 error:
7576 isl_map_free(map);
7577 return NULL;
7580 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7582 struct isl_basic_map *bmap;
7583 unsigned nparam;
7584 unsigned dim;
7585 int i;
7587 if (!dims)
7588 return NULL;
7590 nparam = dims->nparam;
7591 dim = dims->n_out;
7592 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7593 if (!bmap)
7594 goto error;
7596 for (i = 0; i < dim; ++i) {
7597 int j = isl_basic_map_alloc_equality(bmap);
7598 if (j < 0)
7599 goto error;
7600 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7601 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7602 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7604 return isl_basic_map_finalize(bmap);
7605 error:
7606 isl_basic_map_free(bmap);
7607 return NULL;
7610 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7612 if (!dim)
7613 return NULL;
7614 if (dim->n_in != dim->n_out)
7615 isl_die(dim->ctx, isl_error_invalid,
7616 "number of input and output dimensions needs to be "
7617 "the same", goto error);
7618 return basic_map_identity(dim);
7619 error:
7620 isl_space_free(dim);
7621 return NULL;
7624 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7626 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7629 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7631 isl_space *dim = isl_set_get_space(set);
7632 isl_map *id;
7633 id = isl_map_identity(isl_space_map_from_set(dim));
7634 return isl_map_intersect_range(id, set);
7637 /* Construct a basic set with all set dimensions having only non-negative
7638 * values.
7640 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7641 __isl_take isl_space *space)
7643 int i;
7644 unsigned nparam;
7645 unsigned dim;
7646 struct isl_basic_set *bset;
7648 if (!space)
7649 return NULL;
7650 nparam = space->nparam;
7651 dim = space->n_out;
7652 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7653 if (!bset)
7654 return NULL;
7655 for (i = 0; i < dim; ++i) {
7656 int k = isl_basic_set_alloc_inequality(bset);
7657 if (k < 0)
7658 goto error;
7659 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7660 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7662 return bset;
7663 error:
7664 isl_basic_set_free(bset);
7665 return NULL;
7668 /* Construct the half-space x_pos >= 0.
7670 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7671 int pos)
7673 int k;
7674 isl_basic_set *nonneg;
7676 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7677 k = isl_basic_set_alloc_inequality(nonneg);
7678 if (k < 0)
7679 goto error;
7680 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7681 isl_int_set_si(nonneg->ineq[k][pos], 1);
7683 return isl_basic_set_finalize(nonneg);
7684 error:
7685 isl_basic_set_free(nonneg);
7686 return NULL;
7689 /* Construct the half-space x_pos <= -1.
7691 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7693 int k;
7694 isl_basic_set *neg;
7696 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7697 k = isl_basic_set_alloc_inequality(neg);
7698 if (k < 0)
7699 goto error;
7700 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7701 isl_int_set_si(neg->ineq[k][0], -1);
7702 isl_int_set_si(neg->ineq[k][pos], -1);
7704 return isl_basic_set_finalize(neg);
7705 error:
7706 isl_basic_set_free(neg);
7707 return NULL;
7710 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7711 enum isl_dim_type type, unsigned first, unsigned n)
7713 int i;
7714 unsigned offset;
7715 isl_basic_set *nonneg;
7716 isl_basic_set *neg;
7718 if (!set)
7719 return NULL;
7720 if (n == 0)
7721 return set;
7723 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7725 offset = pos(set->dim, type);
7726 for (i = 0; i < n; ++i) {
7727 nonneg = nonneg_halfspace(isl_set_get_space(set),
7728 offset + first + i);
7729 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7731 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7734 return set;
7735 error:
7736 isl_set_free(set);
7737 return NULL;
7740 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7741 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7742 void *user)
7744 isl_set *half;
7746 if (!set)
7747 return -1;
7748 if (isl_set_plain_is_empty(set)) {
7749 isl_set_free(set);
7750 return 0;
7752 if (first == len)
7753 return fn(set, signs, user);
7755 signs[first] = 1;
7756 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7757 1 + first));
7758 half = isl_set_intersect(half, isl_set_copy(set));
7759 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7760 goto error;
7762 signs[first] = -1;
7763 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7764 1 + first));
7765 half = isl_set_intersect(half, set);
7766 return foreach_orthant(half, signs, first + 1, len, fn, user);
7767 error:
7768 isl_set_free(set);
7769 return -1;
7772 /* Call "fn" on the intersections of "set" with each of the orthants
7773 * (except for obviously empty intersections). The orthant is identified
7774 * by the signs array, with each entry having value 1 or -1 according
7775 * to the sign of the corresponding variable.
7777 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7778 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7779 void *user)
7781 unsigned nparam;
7782 unsigned nvar;
7783 int *signs;
7784 int r;
7786 if (!set)
7787 return -1;
7788 if (isl_set_plain_is_empty(set))
7789 return 0;
7791 nparam = isl_set_dim(set, isl_dim_param);
7792 nvar = isl_set_dim(set, isl_dim_set);
7794 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7796 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7797 fn, user);
7799 free(signs);
7801 return r;
7804 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7806 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7809 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7810 __isl_keep isl_basic_map *bmap2)
7812 int is_subset;
7813 struct isl_map *map1;
7814 struct isl_map *map2;
7816 if (!bmap1 || !bmap2)
7817 return isl_bool_error;
7819 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7820 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7822 is_subset = isl_map_is_subset(map1, map2);
7824 isl_map_free(map1);
7825 isl_map_free(map2);
7827 return is_subset;
7830 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7831 __isl_keep isl_basic_set *bset2)
7833 return isl_basic_map_is_subset(bset1, bset2);
7836 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7837 __isl_keep isl_basic_map *bmap2)
7839 isl_bool is_subset;
7841 if (!bmap1 || !bmap2)
7842 return isl_bool_error;
7843 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7844 if (is_subset != isl_bool_true)
7845 return is_subset;
7846 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7847 return is_subset;
7850 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7851 __isl_keep isl_basic_set *bset2)
7853 return isl_basic_map_is_equal(
7854 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7857 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7859 int i;
7860 int is_empty;
7862 if (!map)
7863 return isl_bool_error;
7864 for (i = 0; i < map->n; ++i) {
7865 is_empty = isl_basic_map_is_empty(map->p[i]);
7866 if (is_empty < 0)
7867 return isl_bool_error;
7868 if (!is_empty)
7869 return isl_bool_false;
7871 return isl_bool_true;
7874 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7876 return map ? map->n == 0 : isl_bool_error;
7879 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7881 return set ? set->n == 0 : isl_bool_error;
7884 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7886 return isl_map_is_empty((struct isl_map *)set);
7889 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7891 if (!map1 || !map2)
7892 return -1;
7894 return isl_space_is_equal(map1->dim, map2->dim);
7897 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7899 if (!set1 || !set2)
7900 return -1;
7902 return isl_space_is_equal(set1->dim, set2->dim);
7905 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7907 isl_bool is_subset;
7909 if (!map1 || !map2)
7910 return isl_bool_error;
7911 is_subset = isl_map_is_subset(map1, map2);
7912 if (is_subset != isl_bool_true)
7913 return is_subset;
7914 is_subset = isl_map_is_subset(map2, map1);
7915 return is_subset;
7918 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7920 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7923 isl_bool isl_basic_map_is_strict_subset(
7924 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7926 isl_bool is_subset;
7928 if (!bmap1 || !bmap2)
7929 return isl_bool_error;
7930 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7931 if (is_subset != isl_bool_true)
7932 return is_subset;
7933 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7934 if (is_subset == isl_bool_error)
7935 return is_subset;
7936 return !is_subset;
7939 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7940 __isl_keep isl_map *map2)
7942 isl_bool is_subset;
7944 if (!map1 || !map2)
7945 return isl_bool_error;
7946 is_subset = isl_map_is_subset(map1, map2);
7947 if (is_subset != isl_bool_true)
7948 return is_subset;
7949 is_subset = isl_map_is_subset(map2, map1);
7950 if (is_subset == isl_bool_error)
7951 return is_subset;
7952 return !is_subset;
7955 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7956 __isl_keep isl_set *set2)
7958 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7961 /* Is "bmap" obviously equal to the universe with the same space?
7963 * That is, does it not have any constraints?
7965 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7967 if (!bmap)
7968 return isl_bool_error;
7969 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7972 /* Is "bset" obviously equal to the universe with the same space?
7974 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7976 return isl_basic_map_plain_is_universe(bset);
7979 /* If "c" does not involve any existentially quantified variables,
7980 * then set *univ to false and abort
7982 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7984 isl_bool *univ = user;
7985 unsigned n;
7987 n = isl_constraint_dim(c, isl_dim_div);
7988 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7989 isl_constraint_free(c);
7990 if (*univ < 0 || !*univ)
7991 return isl_stat_error;
7992 return isl_stat_ok;
7995 /* Is "bmap" equal to the universe with the same space?
7997 * First check if it is obviously equal to the universe.
7998 * If not and if there are any constraints not involving
7999 * existentially quantified variables, then it is certainly
8000 * not equal to the universe.
8001 * Otherwise, check if the universe is a subset of "bmap".
8003 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8005 isl_bool univ;
8006 isl_basic_map *test;
8008 univ = isl_basic_map_plain_is_universe(bmap);
8009 if (univ < 0 || univ)
8010 return univ;
8011 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8012 return isl_bool_false;
8013 univ = isl_bool_true;
8014 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8015 univ)
8016 return isl_bool_error;
8017 if (univ < 0 || !univ)
8018 return univ;
8019 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8020 univ = isl_basic_map_is_subset(test, bmap);
8021 isl_basic_map_free(test);
8022 return univ;
8025 /* Is "bset" equal to the universe with the same space?
8027 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8029 return isl_basic_map_is_universe(bset);
8032 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8034 int i;
8036 if (!map)
8037 return isl_bool_error;
8039 for (i = 0; i < map->n; ++i) {
8040 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8041 if (r < 0 || r)
8042 return r;
8045 return isl_bool_false;
8048 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8050 return isl_map_plain_is_universe((isl_map *) set);
8053 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8055 struct isl_basic_set *bset = NULL;
8056 struct isl_vec *sample = NULL;
8057 isl_bool empty, non_empty;
8059 if (!bmap)
8060 return isl_bool_error;
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8063 return isl_bool_true;
8065 if (isl_basic_map_plain_is_universe(bmap))
8066 return isl_bool_false;
8068 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8069 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8070 copy = isl_basic_map_remove_redundancies(copy);
8071 empty = isl_basic_map_plain_is_empty(copy);
8072 isl_basic_map_free(copy);
8073 return empty;
8076 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8077 if (non_empty < 0)
8078 return isl_bool_error;
8079 if (non_empty)
8080 return isl_bool_false;
8081 isl_vec_free(bmap->sample);
8082 bmap->sample = NULL;
8083 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8084 if (!bset)
8085 return isl_bool_error;
8086 sample = isl_basic_set_sample_vec(bset);
8087 if (!sample)
8088 return isl_bool_error;
8089 empty = sample->size == 0;
8090 isl_vec_free(bmap->sample);
8091 bmap->sample = sample;
8092 if (empty)
8093 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8095 return empty;
8098 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8100 if (!bmap)
8101 return isl_bool_error;
8102 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8105 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8107 if (!bset)
8108 return isl_bool_error;
8109 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8112 /* Is "bmap" known to be non-empty?
8114 * That is, is the cached sample still valid?
8116 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8118 unsigned total;
8120 if (!bmap)
8121 return isl_bool_error;
8122 if (!bmap->sample)
8123 return isl_bool_false;
8124 total = 1 + isl_basic_map_total_dim(bmap);
8125 if (bmap->sample->size != total)
8126 return isl_bool_false;
8127 return isl_basic_map_contains(bmap, bmap->sample);
8130 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8132 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8135 struct isl_map *isl_basic_map_union(
8136 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8138 struct isl_map *map;
8139 if (!bmap1 || !bmap2)
8140 goto error;
8142 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8144 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8145 if (!map)
8146 goto error;
8147 map = isl_map_add_basic_map(map, bmap1);
8148 map = isl_map_add_basic_map(map, bmap2);
8149 return map;
8150 error:
8151 isl_basic_map_free(bmap1);
8152 isl_basic_map_free(bmap2);
8153 return NULL;
8156 struct isl_set *isl_basic_set_union(
8157 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8159 return (struct isl_set *)isl_basic_map_union(
8160 (struct isl_basic_map *)bset1,
8161 (struct isl_basic_map *)bset2);
8164 /* Order divs such that any div only depends on previous divs */
8165 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8167 int i;
8168 unsigned off;
8170 if (!bmap)
8171 return NULL;
8173 off = isl_space_dim(bmap->dim, isl_dim_all);
8175 for (i = 0; i < bmap->n_div; ++i) {
8176 int pos;
8177 if (isl_int_is_zero(bmap->div[i][0]))
8178 continue;
8179 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8180 bmap->n_div-i);
8181 if (pos == -1)
8182 continue;
8183 if (pos == 0)
8184 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8185 "integer division depends on itself",
8186 return isl_basic_map_free(bmap));
8187 isl_basic_map_swap_div(bmap, i, i + pos);
8188 --i;
8190 return bmap;
8193 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8195 return (struct isl_basic_set *)
8196 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8199 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8201 int i;
8203 if (!map)
8204 return 0;
8206 for (i = 0; i < map->n; ++i) {
8207 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8208 if (!map->p[i])
8209 goto error;
8212 return map;
8213 error:
8214 isl_map_free(map);
8215 return NULL;
8218 /* Apply the expansion computed by isl_merge_divs.
8219 * The expansion itself is given by "exp" while the resulting
8220 * list of divs is given by "div".
8222 * Move the integer divisions of "bset" into the right position
8223 * according to "exp" and then introduce the additional integer
8224 * divisions, adding div constraints.
8225 * The moving should be done first to avoid moving coefficients
8226 * in the definitions of the extra integer divisions.
8228 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8229 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8231 int i, j;
8232 int n_div;
8234 bset = isl_basic_set_cow(bset);
8235 if (!bset || !div)
8236 goto error;
8238 if (div->n_row < bset->n_div)
8239 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8240 "not an expansion", goto error);
8242 n_div = bset->n_div;
8243 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8244 div->n_row - n_div, 0,
8245 2 * (div->n_row - n_div));
8247 for (i = n_div; i < div->n_row; ++i)
8248 if (isl_basic_set_alloc_div(bset) < 0)
8249 goto error;
8251 for (j = n_div - 1; j >= 0; --j) {
8252 if (exp[j] == j)
8253 break;
8254 isl_basic_map_swap_div(bset, j, exp[j]);
8256 j = 0;
8257 for (i = 0; i < div->n_row; ++i) {
8258 if (j < n_div && exp[j] == i) {
8259 j++;
8260 } else {
8261 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8262 if (!isl_basic_map_div_is_known(bset, i))
8263 continue;
8264 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8265 goto error;
8269 isl_mat_free(div);
8270 return bset;
8271 error:
8272 isl_basic_set_free(bset);
8273 isl_mat_free(div);
8274 return NULL;
8277 /* Look for a div in dst that corresponds to the div "div" in src.
8278 * The divs before "div" in src and dst are assumed to be the same.
8280 * Returns -1 if no corresponding div was found and the position
8281 * of the corresponding div in dst otherwise.
8283 static int find_div(struct isl_basic_map *dst,
8284 struct isl_basic_map *src, unsigned div)
8286 int i;
8288 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8290 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8291 for (i = div; i < dst->n_div; ++i)
8292 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8293 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8294 dst->n_div - div) == -1)
8295 return i;
8296 return -1;
8299 /* Align the divs of "dst" to those of "src", adding divs from "src"
8300 * if needed. That is, make sure that the first src->n_div divs
8301 * of the result are equal to those of src.
8303 * The result is not finalized as by design it will have redundant
8304 * divs if any divs from "src" were copied.
8306 __isl_give isl_basic_map *isl_basic_map_align_divs(
8307 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8309 int i;
8310 int known, extended;
8311 unsigned total;
8313 if (!dst || !src)
8314 return isl_basic_map_free(dst);
8316 if (src->n_div == 0)
8317 return dst;
8319 known = isl_basic_map_divs_known(src);
8320 if (known < 0)
8321 return isl_basic_map_free(dst);
8322 if (!known)
8323 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8324 "some src divs are unknown",
8325 return isl_basic_map_free(dst));
8327 src = isl_basic_map_order_divs(src);
8329 extended = 0;
8330 total = isl_space_dim(src->dim, isl_dim_all);
8331 for (i = 0; i < src->n_div; ++i) {
8332 int j = find_div(dst, src, i);
8333 if (j < 0) {
8334 if (!extended) {
8335 int extra = src->n_div - i;
8336 dst = isl_basic_map_cow(dst);
8337 if (!dst)
8338 return NULL;
8339 dst = isl_basic_map_extend_space(dst,
8340 isl_space_copy(dst->dim),
8341 extra, 0, 2 * extra);
8342 extended = 1;
8344 j = isl_basic_map_alloc_div(dst);
8345 if (j < 0)
8346 return isl_basic_map_free(dst);
8347 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8348 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8349 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8350 return isl_basic_map_free(dst);
8352 if (j != i)
8353 isl_basic_map_swap_div(dst, i, j);
8355 return dst;
8358 struct isl_basic_set *isl_basic_set_align_divs(
8359 struct isl_basic_set *dst, struct isl_basic_set *src)
8361 return (struct isl_basic_set *)isl_basic_map_align_divs(
8362 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8365 struct isl_map *isl_map_align_divs(struct isl_map *map)
8367 int i;
8369 if (!map)
8370 return NULL;
8371 if (map->n == 0)
8372 return map;
8373 map = isl_map_compute_divs(map);
8374 map = isl_map_cow(map);
8375 if (!map)
8376 return NULL;
8378 for (i = 1; i < map->n; ++i)
8379 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8380 for (i = 1; i < map->n; ++i) {
8381 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8382 if (!map->p[i])
8383 return isl_map_free(map);
8386 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8387 return map;
8390 struct isl_set *isl_set_align_divs(struct isl_set *set)
8392 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8395 /* Align the divs of the basic maps in "map" to those
8396 * of the basic maps in "list", as well as to the other basic maps in "map".
8397 * The elements in "list" are assumed to have known divs.
8399 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8400 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8402 int i, n;
8404 map = isl_map_compute_divs(map);
8405 map = isl_map_cow(map);
8406 if (!map || !list)
8407 return isl_map_free(map);
8408 if (map->n == 0)
8409 return map;
8411 n = isl_basic_map_list_n_basic_map(list);
8412 for (i = 0; i < n; ++i) {
8413 isl_basic_map *bmap;
8415 bmap = isl_basic_map_list_get_basic_map(list, i);
8416 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8417 isl_basic_map_free(bmap);
8419 if (!map->p[0])
8420 return isl_map_free(map);
8422 return isl_map_align_divs(map);
8425 /* Align the divs of each element of "list" to those of "bmap".
8426 * Both "bmap" and the elements of "list" are assumed to have known divs.
8428 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8429 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8431 int i, n;
8433 if (!list || !bmap)
8434 return isl_basic_map_list_free(list);
8436 n = isl_basic_map_list_n_basic_map(list);
8437 for (i = 0; i < n; ++i) {
8438 isl_basic_map *bmap_i;
8440 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8441 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8442 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8445 return list;
8448 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8449 __isl_take isl_map *map)
8451 if (!set || !map)
8452 goto error;
8453 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8454 map = isl_map_intersect_domain(map, set);
8455 set = isl_map_range(map);
8456 return set;
8457 error:
8458 isl_set_free(set);
8459 isl_map_free(map);
8460 return NULL;
8463 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8464 __isl_take isl_map *map)
8466 return isl_map_align_params_map_map_and(set, map, &set_apply);
8469 /* There is no need to cow as removing empty parts doesn't change
8470 * the meaning of the set.
8472 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8474 int i;
8476 if (!map)
8477 return NULL;
8479 for (i = map->n - 1; i >= 0; --i)
8480 remove_if_empty(map, i);
8482 return map;
8485 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8487 return (struct isl_set *)
8488 isl_map_remove_empty_parts((struct isl_map *)set);
8491 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8493 struct isl_basic_map *bmap;
8494 if (!map || map->n == 0)
8495 return NULL;
8496 bmap = map->p[map->n-1];
8497 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8498 return isl_basic_map_copy(bmap);
8501 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8503 return (struct isl_basic_set *)
8504 isl_map_copy_basic_map((struct isl_map *)set);
8507 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8508 __isl_keep isl_basic_map *bmap)
8510 int i;
8512 if (!map || !bmap)
8513 goto error;
8514 for (i = map->n-1; i >= 0; --i) {
8515 if (map->p[i] != bmap)
8516 continue;
8517 map = isl_map_cow(map);
8518 if (!map)
8519 goto error;
8520 isl_basic_map_free(map->p[i]);
8521 if (i != map->n-1) {
8522 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8523 map->p[i] = map->p[map->n-1];
8525 map->n--;
8526 return map;
8528 return map;
8529 error:
8530 isl_map_free(map);
8531 return NULL;
8534 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8535 struct isl_basic_set *bset)
8537 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8538 (struct isl_basic_map *)bset);
8541 /* Given two basic sets bset1 and bset2, compute the maximal difference
8542 * between the values of dimension pos in bset1 and those in bset2
8543 * for any common value of the parameters and dimensions preceding pos.
8545 static enum isl_lp_result basic_set_maximal_difference_at(
8546 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8547 int pos, isl_int *opt)
8549 isl_space *dims;
8550 struct isl_basic_map *bmap1 = NULL;
8551 struct isl_basic_map *bmap2 = NULL;
8552 struct isl_ctx *ctx;
8553 struct isl_vec *obj;
8554 unsigned total;
8555 unsigned nparam;
8556 unsigned dim1, dim2;
8557 enum isl_lp_result res;
8559 if (!bset1 || !bset2)
8560 return isl_lp_error;
8562 nparam = isl_basic_set_n_param(bset1);
8563 dim1 = isl_basic_set_n_dim(bset1);
8564 dim2 = isl_basic_set_n_dim(bset2);
8565 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8566 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8567 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8568 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8569 if (!bmap1 || !bmap2)
8570 goto error;
8571 bmap1 = isl_basic_map_cow(bmap1);
8572 bmap1 = isl_basic_map_extend(bmap1, nparam,
8573 pos, (dim1 - pos) + (dim2 - pos),
8574 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8575 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8576 if (!bmap1)
8577 goto error2;
8578 total = isl_basic_map_total_dim(bmap1);
8579 ctx = bmap1->ctx;
8580 obj = isl_vec_alloc(ctx, 1 + total);
8581 if (!obj)
8582 goto error2;
8583 isl_seq_clr(obj->block.data, 1 + total);
8584 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8585 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8586 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8587 opt, NULL, NULL);
8588 isl_basic_map_free(bmap1);
8589 isl_vec_free(obj);
8590 return res;
8591 error:
8592 isl_basic_map_free(bmap2);
8593 error2:
8594 isl_basic_map_free(bmap1);
8595 return isl_lp_error;
8598 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8599 * for any common value of the parameters and dimensions preceding pos
8600 * in both basic sets, the values of dimension pos in bset1 are
8601 * smaller or larger than those in bset2.
8603 * Returns
8604 * 1 if bset1 follows bset2
8605 * -1 if bset1 precedes bset2
8606 * 0 if bset1 and bset2 are incomparable
8607 * -2 if some error occurred.
8609 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8610 struct isl_basic_set *bset2, int pos)
8612 isl_int opt;
8613 enum isl_lp_result res;
8614 int cmp;
8616 isl_int_init(opt);
8618 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8620 if (res == isl_lp_empty)
8621 cmp = 0;
8622 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8623 res == isl_lp_unbounded)
8624 cmp = 1;
8625 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8626 cmp = -1;
8627 else
8628 cmp = -2;
8630 isl_int_clear(opt);
8631 return cmp;
8634 /* Given two basic sets bset1 and bset2, check whether
8635 * for any common value of the parameters and dimensions preceding pos
8636 * there is a value of dimension pos in bset1 that is larger
8637 * than a value of the same dimension in bset2.
8639 * Return
8640 * 1 if there exists such a pair
8641 * 0 if there is no such pair, but there is a pair of equal values
8642 * -1 otherwise
8643 * -2 if some error occurred.
8645 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8646 __isl_keep isl_basic_set *bset2, int pos)
8648 isl_int opt;
8649 enum isl_lp_result res;
8650 int cmp;
8652 isl_int_init(opt);
8654 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8656 if (res == isl_lp_empty)
8657 cmp = -1;
8658 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8659 res == isl_lp_unbounded)
8660 cmp = 1;
8661 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8662 cmp = -1;
8663 else if (res == isl_lp_ok)
8664 cmp = 0;
8665 else
8666 cmp = -2;
8668 isl_int_clear(opt);
8669 return cmp;
8672 /* Given two sets set1 and set2, check whether
8673 * for any common value of the parameters and dimensions preceding pos
8674 * there is a value of dimension pos in set1 that is larger
8675 * than a value of the same dimension in set2.
8677 * Return
8678 * 1 if there exists such a pair
8679 * 0 if there is no such pair, but there is a pair of equal values
8680 * -1 otherwise
8681 * -2 if some error occurred.
8683 int isl_set_follows_at(__isl_keep isl_set *set1,
8684 __isl_keep isl_set *set2, int pos)
8686 int i, j;
8687 int follows = -1;
8689 if (!set1 || !set2)
8690 return -2;
8692 for (i = 0; i < set1->n; ++i)
8693 for (j = 0; j < set2->n; ++j) {
8694 int f;
8695 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8696 if (f == 1 || f == -2)
8697 return f;
8698 if (f > follows)
8699 follows = f;
8702 return follows;
8705 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8706 unsigned pos, isl_int *val)
8708 int i;
8709 int d;
8710 unsigned total;
8712 if (!bmap)
8713 return -1;
8714 total = isl_basic_map_total_dim(bmap);
8715 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8716 for (; d+1 > pos; --d)
8717 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8718 break;
8719 if (d != pos)
8720 continue;
8721 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8722 return 0;
8723 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8724 return 0;
8725 if (!isl_int_is_one(bmap->eq[i][1+d]))
8726 return 0;
8727 if (val)
8728 isl_int_neg(*val, bmap->eq[i][0]);
8729 return 1;
8731 return 0;
8734 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8735 unsigned pos, isl_int *val)
8737 int i;
8738 isl_int v;
8739 isl_int tmp;
8740 int fixed;
8742 if (!map)
8743 return -1;
8744 if (map->n == 0)
8745 return 0;
8746 if (map->n == 1)
8747 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8748 isl_int_init(v);
8749 isl_int_init(tmp);
8750 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8751 for (i = 1; fixed == 1 && i < map->n; ++i) {
8752 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8753 if (fixed == 1 && isl_int_ne(tmp, v))
8754 fixed = 0;
8756 if (val)
8757 isl_int_set(*val, v);
8758 isl_int_clear(tmp);
8759 isl_int_clear(v);
8760 return fixed;
8763 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8764 unsigned pos, isl_int *val)
8766 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8767 pos, val);
8770 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8771 isl_int *val)
8773 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8776 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8777 enum isl_dim_type type, unsigned pos, isl_int *val)
8779 if (pos >= isl_basic_map_dim(bmap, type))
8780 return -1;
8781 return isl_basic_map_plain_has_fixed_var(bmap,
8782 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8785 /* If "bmap" obviously lies on a hyperplane where the given dimension
8786 * has a fixed value, then return that value.
8787 * Otherwise return NaN.
8789 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8790 __isl_keep isl_basic_map *bmap,
8791 enum isl_dim_type type, unsigned pos)
8793 isl_ctx *ctx;
8794 isl_val *v;
8795 int fixed;
8797 if (!bmap)
8798 return NULL;
8799 ctx = isl_basic_map_get_ctx(bmap);
8800 v = isl_val_alloc(ctx);
8801 if (!v)
8802 return NULL;
8803 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8804 if (fixed < 0)
8805 return isl_val_free(v);
8806 if (fixed) {
8807 isl_int_set_si(v->d, 1);
8808 return v;
8810 isl_val_free(v);
8811 return isl_val_nan(ctx);
8814 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8815 enum isl_dim_type type, unsigned pos, isl_int *val)
8817 if (pos >= isl_map_dim(map, type))
8818 return -1;
8819 return isl_map_plain_has_fixed_var(map,
8820 map_offset(map, type) - 1 + pos, val);
8823 /* If "map" obviously lies on a hyperplane where the given dimension
8824 * has a fixed value, then return that value.
8825 * Otherwise return NaN.
8827 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8828 enum isl_dim_type type, unsigned pos)
8830 isl_ctx *ctx;
8831 isl_val *v;
8832 int fixed;
8834 if (!map)
8835 return NULL;
8836 ctx = isl_map_get_ctx(map);
8837 v = isl_val_alloc(ctx);
8838 if (!v)
8839 return NULL;
8840 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8841 if (fixed < 0)
8842 return isl_val_free(v);
8843 if (fixed) {
8844 isl_int_set_si(v->d, 1);
8845 return v;
8847 isl_val_free(v);
8848 return isl_val_nan(ctx);
8851 /* If "set" obviously lies on a hyperplane where the given dimension
8852 * has a fixed value, then return that value.
8853 * Otherwise return NaN.
8855 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8856 enum isl_dim_type type, unsigned pos)
8858 return isl_map_plain_get_val_if_fixed(set, type, pos);
8861 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8862 enum isl_dim_type type, unsigned pos, isl_int *val)
8864 return isl_map_plain_is_fixed(set, type, pos, val);
8867 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8868 * then return this fixed value in *val.
8870 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8871 unsigned dim, isl_int *val)
8873 return isl_basic_set_plain_has_fixed_var(bset,
8874 isl_basic_set_n_param(bset) + dim, val);
8877 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8878 * then return this fixed value in *val.
8880 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8881 unsigned dim, isl_int *val)
8883 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8886 /* Check if input variable in has fixed value and if so and if val is not NULL,
8887 * then return this fixed value in *val.
8889 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8890 unsigned in, isl_int *val)
8892 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8895 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8896 * and if val is not NULL, then return this lower bound in *val.
8898 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8899 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8901 int i, i_eq = -1, i_ineq = -1;
8902 isl_int *c;
8903 unsigned total;
8904 unsigned nparam;
8906 if (!bset)
8907 return -1;
8908 total = isl_basic_set_total_dim(bset);
8909 nparam = isl_basic_set_n_param(bset);
8910 for (i = 0; i < bset->n_eq; ++i) {
8911 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8912 continue;
8913 if (i_eq != -1)
8914 return 0;
8915 i_eq = i;
8917 for (i = 0; i < bset->n_ineq; ++i) {
8918 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8919 continue;
8920 if (i_eq != -1 || i_ineq != -1)
8921 return 0;
8922 i_ineq = i;
8924 if (i_eq == -1 && i_ineq == -1)
8925 return 0;
8926 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8927 /* The coefficient should always be one due to normalization. */
8928 if (!isl_int_is_one(c[1+nparam+dim]))
8929 return 0;
8930 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8931 return 0;
8932 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8933 total - nparam - dim - 1) != -1)
8934 return 0;
8935 if (val)
8936 isl_int_neg(*val, c[0]);
8937 return 1;
8940 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8941 unsigned dim, isl_int *val)
8943 int i;
8944 isl_int v;
8945 isl_int tmp;
8946 int fixed;
8948 if (!set)
8949 return -1;
8950 if (set->n == 0)
8951 return 0;
8952 if (set->n == 1)
8953 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8954 dim, val);
8955 isl_int_init(v);
8956 isl_int_init(tmp);
8957 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8958 dim, &v);
8959 for (i = 1; fixed == 1 && i < set->n; ++i) {
8960 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8961 dim, &tmp);
8962 if (fixed == 1 && isl_int_ne(tmp, v))
8963 fixed = 0;
8965 if (val)
8966 isl_int_set(*val, v);
8967 isl_int_clear(tmp);
8968 isl_int_clear(v);
8969 return fixed;
8972 /* Return -1 if the constraint "c1" should be sorted before "c2"
8973 * and 1 if it should be sorted after "c2".
8974 * Return 0 if the two constraints are the same (up to the constant term).
8976 * In particular, if a constraint involves later variables than another
8977 * then it is sorted after this other constraint.
8978 * uset_gist depends on constraints without existentially quantified
8979 * variables sorting first.
8981 * For constraints that have the same latest variable, those
8982 * with the same coefficient for this latest variable (first in absolute value
8983 * and then in actual value) are grouped together.
8984 * This is useful for detecting pairs of constraints that can
8985 * be chained in their printed representation.
8987 * Finally, within a group, constraints are sorted according to
8988 * their coefficients (excluding the constant term).
8990 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8992 isl_int **c1 = (isl_int **) p1;
8993 isl_int **c2 = (isl_int **) p2;
8994 int l1, l2;
8995 unsigned size = *(unsigned *) arg;
8996 int cmp;
8998 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8999 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9001 if (l1 != l2)
9002 return l1 - l2;
9004 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9005 if (cmp != 0)
9006 return cmp;
9007 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9008 if (cmp != 0)
9009 return -cmp;
9011 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9014 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9015 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9016 * and 0 if the two constraints are the same (up to the constant term).
9018 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9019 isl_int *c1, isl_int *c2)
9021 unsigned total;
9023 if (!bmap)
9024 return -2;
9025 total = isl_basic_map_total_dim(bmap);
9026 return sort_constraint_cmp(&c1, &c2, &total);
9029 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9030 __isl_take isl_basic_map *bmap)
9032 unsigned total;
9034 if (!bmap)
9035 return NULL;
9036 if (bmap->n_ineq == 0)
9037 return bmap;
9038 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9039 return bmap;
9040 total = isl_basic_map_total_dim(bmap);
9041 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9042 &sort_constraint_cmp, &total) < 0)
9043 return isl_basic_map_free(bmap);
9044 return bmap;
9047 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9048 __isl_take isl_basic_set *bset)
9050 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9051 (struct isl_basic_map *)bset);
9054 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9056 if (!bmap)
9057 return NULL;
9058 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9059 return bmap;
9060 bmap = isl_basic_map_remove_redundancies(bmap);
9061 bmap = isl_basic_map_sort_constraints(bmap);
9062 if (bmap)
9063 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9064 return bmap;
9067 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9069 return (struct isl_basic_set *)isl_basic_map_normalize(
9070 (struct isl_basic_map *)bset);
9073 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9074 const __isl_keep isl_basic_map *bmap2)
9076 int i, cmp;
9077 unsigned total;
9079 if (!bmap1 || !bmap2)
9080 return -1;
9082 if (bmap1 == bmap2)
9083 return 0;
9084 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9085 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9086 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9087 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9088 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9089 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9090 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9091 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9092 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9093 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9094 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9095 return 0;
9096 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9097 return 1;
9098 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9099 return -1;
9100 if (bmap1->n_eq != bmap2->n_eq)
9101 return bmap1->n_eq - bmap2->n_eq;
9102 if (bmap1->n_ineq != bmap2->n_ineq)
9103 return bmap1->n_ineq - bmap2->n_ineq;
9104 if (bmap1->n_div != bmap2->n_div)
9105 return bmap1->n_div - bmap2->n_div;
9106 total = isl_basic_map_total_dim(bmap1);
9107 for (i = 0; i < bmap1->n_eq; ++i) {
9108 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9109 if (cmp)
9110 return cmp;
9112 for (i = 0; i < bmap1->n_ineq; ++i) {
9113 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9114 if (cmp)
9115 return cmp;
9117 for (i = 0; i < bmap1->n_div; ++i) {
9118 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9119 if (cmp)
9120 return cmp;
9122 return 0;
9125 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9126 const __isl_keep isl_basic_set *bset2)
9128 return isl_basic_map_plain_cmp(bset1, bset2);
9131 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9133 int i, cmp;
9135 if (set1 == set2)
9136 return 0;
9137 if (set1->n != set2->n)
9138 return set1->n - set2->n;
9140 for (i = 0; i < set1->n; ++i) {
9141 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9142 if (cmp)
9143 return cmp;
9146 return 0;
9149 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9150 __isl_keep isl_basic_map *bmap2)
9152 if (!bmap1 || !bmap2)
9153 return isl_bool_error;
9154 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9157 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9158 __isl_keep isl_basic_set *bset2)
9160 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9161 (isl_basic_map *)bset2);
9164 static int qsort_bmap_cmp(const void *p1, const void *p2)
9166 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9167 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9169 return isl_basic_map_plain_cmp(bmap1, bmap2);
9172 /* Sort the basic maps of "map" and remove duplicate basic maps.
9174 * While removing basic maps, we make sure that the basic maps remain
9175 * sorted because isl_map_normalize expects the basic maps of the result
9176 * to be sorted.
9178 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9180 int i, j;
9182 map = isl_map_remove_empty_parts(map);
9183 if (!map)
9184 return NULL;
9185 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9186 for (i = map->n - 1; i >= 1; --i) {
9187 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9188 continue;
9189 isl_basic_map_free(map->p[i-1]);
9190 for (j = i; j < map->n; ++j)
9191 map->p[j - 1] = map->p[j];
9192 map->n--;
9195 return map;
9198 /* Remove obvious duplicates among the basic maps of "map".
9200 * Unlike isl_map_normalize, this function does not remove redundant
9201 * constraints and only removes duplicates that have exactly the same
9202 * constraints in the input. It does sort the constraints and
9203 * the basic maps to ease the detection of duplicates.
9205 * If "map" has already been normalized or if the basic maps are
9206 * disjoint, then there can be no duplicates.
9208 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9210 int i;
9211 isl_basic_map *bmap;
9213 if (!map)
9214 return NULL;
9215 if (map->n <= 1)
9216 return map;
9217 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9218 return map;
9219 for (i = 0; i < map->n; ++i) {
9220 bmap = isl_basic_map_copy(map->p[i]);
9221 bmap = isl_basic_map_sort_constraints(bmap);
9222 if (!bmap)
9223 return isl_map_free(map);
9224 isl_basic_map_free(map->p[i]);
9225 map->p[i] = bmap;
9228 map = sort_and_remove_duplicates(map);
9229 return map;
9232 /* We normalize in place, but if anything goes wrong we need
9233 * to return NULL, so we need to make sure we don't change the
9234 * meaning of any possible other copies of map.
9236 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9238 int i;
9239 struct isl_basic_map *bmap;
9241 if (!map)
9242 return NULL;
9243 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9244 return map;
9245 for (i = 0; i < map->n; ++i) {
9246 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9247 if (!bmap)
9248 goto error;
9249 isl_basic_map_free(map->p[i]);
9250 map->p[i] = bmap;
9253 map = sort_and_remove_duplicates(map);
9254 if (map)
9255 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9256 return map;
9257 error:
9258 isl_map_free(map);
9259 return NULL;
9262 struct isl_set *isl_set_normalize(struct isl_set *set)
9264 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9267 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9268 __isl_keep isl_map *map2)
9270 int i;
9271 isl_bool equal;
9273 if (!map1 || !map2)
9274 return isl_bool_error;
9276 if (map1 == map2)
9277 return isl_bool_true;
9278 if (!isl_space_is_equal(map1->dim, map2->dim))
9279 return isl_bool_false;
9281 map1 = isl_map_copy(map1);
9282 map2 = isl_map_copy(map2);
9283 map1 = isl_map_normalize(map1);
9284 map2 = isl_map_normalize(map2);
9285 if (!map1 || !map2)
9286 goto error;
9287 equal = map1->n == map2->n;
9288 for (i = 0; equal && i < map1->n; ++i) {
9289 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9290 if (equal < 0)
9291 goto error;
9293 isl_map_free(map1);
9294 isl_map_free(map2);
9295 return equal;
9296 error:
9297 isl_map_free(map1);
9298 isl_map_free(map2);
9299 return isl_bool_error;
9302 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9303 __isl_keep isl_set *set2)
9305 return isl_map_plain_is_equal((struct isl_map *)set1,
9306 (struct isl_map *)set2);
9309 /* Return an interval that ranges from min to max (inclusive)
9311 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9312 isl_int min, isl_int max)
9314 int k;
9315 struct isl_basic_set *bset = NULL;
9317 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9318 if (!bset)
9319 goto error;
9321 k = isl_basic_set_alloc_inequality(bset);
9322 if (k < 0)
9323 goto error;
9324 isl_int_set_si(bset->ineq[k][1], 1);
9325 isl_int_neg(bset->ineq[k][0], min);
9327 k = isl_basic_set_alloc_inequality(bset);
9328 if (k < 0)
9329 goto error;
9330 isl_int_set_si(bset->ineq[k][1], -1);
9331 isl_int_set(bset->ineq[k][0], max);
9333 return bset;
9334 error:
9335 isl_basic_set_free(bset);
9336 return NULL;
9339 /* Return the basic maps in "map" as a list.
9341 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9342 __isl_keep isl_map *map)
9344 int i;
9345 isl_ctx *ctx;
9346 isl_basic_map_list *list;
9348 if (!map)
9349 return NULL;
9350 ctx = isl_map_get_ctx(map);
9351 list = isl_basic_map_list_alloc(ctx, map->n);
9353 for (i = 0; i < map->n; ++i) {
9354 isl_basic_map *bmap;
9356 bmap = isl_basic_map_copy(map->p[i]);
9357 list = isl_basic_map_list_add(list, bmap);
9360 return list;
9363 /* Return the intersection of the elements in the non-empty list "list".
9364 * All elements are assumed to live in the same space.
9366 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9367 __isl_take isl_basic_map_list *list)
9369 int i, n;
9370 isl_basic_map *bmap;
9372 if (!list)
9373 return NULL;
9374 n = isl_basic_map_list_n_basic_map(list);
9375 if (n < 1)
9376 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9377 "expecting non-empty list", goto error);
9379 bmap = isl_basic_map_list_get_basic_map(list, 0);
9380 for (i = 1; i < n; ++i) {
9381 isl_basic_map *bmap_i;
9383 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9384 bmap = isl_basic_map_intersect(bmap, bmap_i);
9387 isl_basic_map_list_free(list);
9388 return bmap;
9389 error:
9390 isl_basic_map_list_free(list);
9391 return NULL;
9394 /* Return the intersection of the elements in the non-empty list "list".
9395 * All elements are assumed to live in the same space.
9397 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9398 __isl_take isl_basic_set_list *list)
9400 return isl_basic_map_list_intersect(list);
9403 /* Return the union of the elements in the non-empty list "list".
9404 * All elements are assumed to live in the same space.
9406 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9408 int i, n;
9409 isl_set *set;
9411 if (!list)
9412 return NULL;
9413 n = isl_set_list_n_set(list);
9414 if (n < 1)
9415 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9416 "expecting non-empty list", goto error);
9418 set = isl_set_list_get_set(list, 0);
9419 for (i = 1; i < n; ++i) {
9420 isl_set *set_i;
9422 set_i = isl_set_list_get_set(list, i);
9423 set = isl_set_union(set, set_i);
9426 isl_set_list_free(list);
9427 return set;
9428 error:
9429 isl_set_list_free(list);
9430 return NULL;
9433 /* Return the Cartesian product of the basic sets in list (in the given order).
9435 __isl_give isl_basic_set *isl_basic_set_list_product(
9436 __isl_take struct isl_basic_set_list *list)
9438 int i;
9439 unsigned dim;
9440 unsigned nparam;
9441 unsigned extra;
9442 unsigned n_eq;
9443 unsigned n_ineq;
9444 struct isl_basic_set *product = NULL;
9446 if (!list)
9447 goto error;
9448 isl_assert(list->ctx, list->n > 0, goto error);
9449 isl_assert(list->ctx, list->p[0], goto error);
9450 nparam = isl_basic_set_n_param(list->p[0]);
9451 dim = isl_basic_set_n_dim(list->p[0]);
9452 extra = list->p[0]->n_div;
9453 n_eq = list->p[0]->n_eq;
9454 n_ineq = list->p[0]->n_ineq;
9455 for (i = 1; i < list->n; ++i) {
9456 isl_assert(list->ctx, list->p[i], goto error);
9457 isl_assert(list->ctx,
9458 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9459 dim += isl_basic_set_n_dim(list->p[i]);
9460 extra += list->p[i]->n_div;
9461 n_eq += list->p[i]->n_eq;
9462 n_ineq += list->p[i]->n_ineq;
9464 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9465 n_eq, n_ineq);
9466 if (!product)
9467 goto error;
9468 dim = 0;
9469 for (i = 0; i < list->n; ++i) {
9470 isl_basic_set_add_constraints(product,
9471 isl_basic_set_copy(list->p[i]), dim);
9472 dim += isl_basic_set_n_dim(list->p[i]);
9474 isl_basic_set_list_free(list);
9475 return product;
9476 error:
9477 isl_basic_set_free(product);
9478 isl_basic_set_list_free(list);
9479 return NULL;
9482 struct isl_basic_map *isl_basic_map_product(
9483 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9485 isl_space *dim_result = NULL;
9486 struct isl_basic_map *bmap;
9487 unsigned in1, in2, out1, out2, nparam, total, pos;
9488 struct isl_dim_map *dim_map1, *dim_map2;
9490 if (!bmap1 || !bmap2)
9491 goto error;
9493 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9494 bmap2->dim, isl_dim_param), goto error);
9495 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9496 isl_space_copy(bmap2->dim));
9498 in1 = isl_basic_map_n_in(bmap1);
9499 in2 = isl_basic_map_n_in(bmap2);
9500 out1 = isl_basic_map_n_out(bmap1);
9501 out2 = isl_basic_map_n_out(bmap2);
9502 nparam = isl_basic_map_n_param(bmap1);
9504 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9505 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9506 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9507 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9508 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9509 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9510 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9511 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9512 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9513 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9514 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9516 bmap = isl_basic_map_alloc_space(dim_result,
9517 bmap1->n_div + bmap2->n_div,
9518 bmap1->n_eq + bmap2->n_eq,
9519 bmap1->n_ineq + bmap2->n_ineq);
9520 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9521 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9522 bmap = isl_basic_map_simplify(bmap);
9523 return isl_basic_map_finalize(bmap);
9524 error:
9525 isl_basic_map_free(bmap1);
9526 isl_basic_map_free(bmap2);
9527 return NULL;
9530 __isl_give isl_basic_map *isl_basic_map_flat_product(
9531 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9533 isl_basic_map *prod;
9535 prod = isl_basic_map_product(bmap1, bmap2);
9536 prod = isl_basic_map_flatten(prod);
9537 return prod;
9540 __isl_give isl_basic_set *isl_basic_set_flat_product(
9541 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9543 return isl_basic_map_flat_range_product(bset1, bset2);
9546 __isl_give isl_basic_map *isl_basic_map_domain_product(
9547 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9549 isl_space *space_result = NULL;
9550 isl_basic_map *bmap;
9551 unsigned in1, in2, out, nparam, total, pos;
9552 struct isl_dim_map *dim_map1, *dim_map2;
9554 if (!bmap1 || !bmap2)
9555 goto error;
9557 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9558 isl_space_copy(bmap2->dim));
9560 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9561 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9562 out = isl_basic_map_dim(bmap1, isl_dim_out);
9563 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9565 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9566 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9567 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9568 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9569 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9570 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9571 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9572 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9573 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9574 isl_dim_map_div(dim_map1, bmap1, pos += out);
9575 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9577 bmap = isl_basic_map_alloc_space(space_result,
9578 bmap1->n_div + bmap2->n_div,
9579 bmap1->n_eq + bmap2->n_eq,
9580 bmap1->n_ineq + bmap2->n_ineq);
9581 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9582 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9583 bmap = isl_basic_map_simplify(bmap);
9584 return isl_basic_map_finalize(bmap);
9585 error:
9586 isl_basic_map_free(bmap1);
9587 isl_basic_map_free(bmap2);
9588 return NULL;
9591 __isl_give isl_basic_map *isl_basic_map_range_product(
9592 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9594 isl_space *dim_result = NULL;
9595 isl_basic_map *bmap;
9596 unsigned in, out1, out2, nparam, total, pos;
9597 struct isl_dim_map *dim_map1, *dim_map2;
9599 if (!bmap1 || !bmap2)
9600 goto error;
9602 if (!isl_space_match(bmap1->dim, isl_dim_param,
9603 bmap2->dim, isl_dim_param))
9604 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9605 "parameters don't match", goto error);
9607 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9608 isl_space_copy(bmap2->dim));
9610 in = isl_basic_map_dim(bmap1, isl_dim_in);
9611 out1 = isl_basic_map_n_out(bmap1);
9612 out2 = isl_basic_map_n_out(bmap2);
9613 nparam = isl_basic_map_n_param(bmap1);
9615 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9616 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9617 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9618 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9619 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9620 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9621 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9622 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9623 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9624 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9625 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9627 bmap = isl_basic_map_alloc_space(dim_result,
9628 bmap1->n_div + bmap2->n_div,
9629 bmap1->n_eq + bmap2->n_eq,
9630 bmap1->n_ineq + bmap2->n_ineq);
9631 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9632 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9633 bmap = isl_basic_map_simplify(bmap);
9634 return isl_basic_map_finalize(bmap);
9635 error:
9636 isl_basic_map_free(bmap1);
9637 isl_basic_map_free(bmap2);
9638 return NULL;
9641 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9642 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9644 isl_basic_map *prod;
9646 prod = isl_basic_map_range_product(bmap1, bmap2);
9647 prod = isl_basic_map_flatten_range(prod);
9648 return prod;
9651 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9652 * and collect the results.
9653 * The result live in the space obtained by calling "space_product"
9654 * on the spaces of "map1" and "map2".
9655 * If "remove_duplicates" is set then the result may contain duplicates
9656 * (even if the inputs do not) and so we try and remove the obvious
9657 * duplicates.
9659 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9660 __isl_take isl_map *map2,
9661 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9662 __isl_take isl_space *right),
9663 __isl_give isl_basic_map *(*basic_map_product)(
9664 __isl_take isl_basic_map *left,
9665 __isl_take isl_basic_map *right),
9666 int remove_duplicates)
9668 unsigned flags = 0;
9669 struct isl_map *result;
9670 int i, j;
9672 if (!map1 || !map2)
9673 goto error;
9675 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9676 map2->dim, isl_dim_param), goto error);
9678 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9679 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9680 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9682 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9683 isl_space_copy(map2->dim)),
9684 map1->n * map2->n, flags);
9685 if (!result)
9686 goto error;
9687 for (i = 0; i < map1->n; ++i)
9688 for (j = 0; j < map2->n; ++j) {
9689 struct isl_basic_map *part;
9690 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9691 isl_basic_map_copy(map2->p[j]));
9692 if (isl_basic_map_is_empty(part))
9693 isl_basic_map_free(part);
9694 else
9695 result = isl_map_add_basic_map(result, part);
9696 if (!result)
9697 goto error;
9699 if (remove_duplicates)
9700 result = isl_map_remove_obvious_duplicates(result);
9701 isl_map_free(map1);
9702 isl_map_free(map2);
9703 return result;
9704 error:
9705 isl_map_free(map1);
9706 isl_map_free(map2);
9707 return NULL;
9710 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9712 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9713 __isl_take isl_map *map2)
9715 return map_product(map1, map2, &isl_space_product,
9716 &isl_basic_map_product, 0);
9719 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9720 __isl_take isl_map *map2)
9722 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9725 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9727 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9728 __isl_take isl_map *map2)
9730 isl_map *prod;
9732 prod = isl_map_product(map1, map2);
9733 prod = isl_map_flatten(prod);
9734 return prod;
9737 /* Given two set A and B, construct its Cartesian product A x B.
9739 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9741 return isl_map_range_product(set1, set2);
9744 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9745 __isl_take isl_set *set2)
9747 return isl_map_flat_range_product(set1, set2);
9750 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9752 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9753 __isl_take isl_map *map2)
9755 return map_product(map1, map2, &isl_space_domain_product,
9756 &isl_basic_map_domain_product, 1);
9759 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9761 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9762 __isl_take isl_map *map2)
9764 return map_product(map1, map2, &isl_space_range_product,
9765 &isl_basic_map_range_product, 1);
9768 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9769 __isl_take isl_map *map2)
9771 return isl_map_align_params_map_map_and(map1, map2,
9772 &map_domain_product_aligned);
9775 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9776 __isl_take isl_map *map2)
9778 return isl_map_align_params_map_map_and(map1, map2,
9779 &map_range_product_aligned);
9782 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9784 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9786 isl_space *space;
9787 int total1, keep1, total2, keep2;
9789 if (!map)
9790 return NULL;
9791 if (!isl_space_domain_is_wrapping(map->dim) ||
9792 !isl_space_range_is_wrapping(map->dim))
9793 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9794 "not a product", return isl_map_free(map));
9796 space = isl_map_get_space(map);
9797 total1 = isl_space_dim(space, isl_dim_in);
9798 total2 = isl_space_dim(space, isl_dim_out);
9799 space = isl_space_factor_domain(space);
9800 keep1 = isl_space_dim(space, isl_dim_in);
9801 keep2 = isl_space_dim(space, isl_dim_out);
9802 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9803 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9804 map = isl_map_reset_space(map, space);
9806 return map;
9809 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9811 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9813 isl_space *space;
9814 int total1, keep1, total2, keep2;
9816 if (!map)
9817 return NULL;
9818 if (!isl_space_domain_is_wrapping(map->dim) ||
9819 !isl_space_range_is_wrapping(map->dim))
9820 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9821 "not a product", return isl_map_free(map));
9823 space = isl_map_get_space(map);
9824 total1 = isl_space_dim(space, isl_dim_in);
9825 total2 = isl_space_dim(space, isl_dim_out);
9826 space = isl_space_factor_range(space);
9827 keep1 = isl_space_dim(space, isl_dim_in);
9828 keep2 = isl_space_dim(space, isl_dim_out);
9829 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9830 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9831 map = isl_map_reset_space(map, space);
9833 return map;
9836 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9838 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9840 isl_space *space;
9841 int total, keep;
9843 if (!map)
9844 return NULL;
9845 if (!isl_space_domain_is_wrapping(map->dim))
9846 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9847 "domain is not a product", return isl_map_free(map));
9849 space = isl_map_get_space(map);
9850 total = isl_space_dim(space, isl_dim_in);
9851 space = isl_space_domain_factor_domain(space);
9852 keep = isl_space_dim(space, isl_dim_in);
9853 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9854 map = isl_map_reset_space(map, space);
9856 return map;
9859 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9861 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9863 isl_space *space;
9864 int total, keep;
9866 if (!map)
9867 return NULL;
9868 if (!isl_space_domain_is_wrapping(map->dim))
9869 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9870 "domain is not a product", return isl_map_free(map));
9872 space = isl_map_get_space(map);
9873 total = isl_space_dim(space, isl_dim_in);
9874 space = isl_space_domain_factor_range(space);
9875 keep = isl_space_dim(space, isl_dim_in);
9876 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9877 map = isl_map_reset_space(map, space);
9879 return map;
9882 /* Given a map A -> [B -> C], extract the map A -> B.
9884 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9886 isl_space *space;
9887 int total, keep;
9889 if (!map)
9890 return NULL;
9891 if (!isl_space_range_is_wrapping(map->dim))
9892 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9893 "range is not a product", return isl_map_free(map));
9895 space = isl_map_get_space(map);
9896 total = isl_space_dim(space, isl_dim_out);
9897 space = isl_space_range_factor_domain(space);
9898 keep = isl_space_dim(space, isl_dim_out);
9899 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9900 map = isl_map_reset_space(map, space);
9902 return map;
9905 /* Given a map A -> [B -> C], extract the map A -> C.
9907 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9909 isl_space *space;
9910 int total, keep;
9912 if (!map)
9913 return NULL;
9914 if (!isl_space_range_is_wrapping(map->dim))
9915 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9916 "range is not a product", return isl_map_free(map));
9918 space = isl_map_get_space(map);
9919 total = isl_space_dim(space, isl_dim_out);
9920 space = isl_space_range_factor_range(space);
9921 keep = isl_space_dim(space, isl_dim_out);
9922 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9923 map = isl_map_reset_space(map, space);
9925 return map;
9928 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9930 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9931 __isl_take isl_map *map2)
9933 isl_map *prod;
9935 prod = isl_map_domain_product(map1, map2);
9936 prod = isl_map_flatten_domain(prod);
9937 return prod;
9940 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9942 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9943 __isl_take isl_map *map2)
9945 isl_map *prod;
9947 prod = isl_map_range_product(map1, map2);
9948 prod = isl_map_flatten_range(prod);
9949 return prod;
9952 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9954 int i;
9955 uint32_t hash = isl_hash_init();
9956 unsigned total;
9958 if (!bmap)
9959 return 0;
9960 bmap = isl_basic_map_copy(bmap);
9961 bmap = isl_basic_map_normalize(bmap);
9962 if (!bmap)
9963 return 0;
9964 total = isl_basic_map_total_dim(bmap);
9965 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9966 for (i = 0; i < bmap->n_eq; ++i) {
9967 uint32_t c_hash;
9968 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9969 isl_hash_hash(hash, c_hash);
9971 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9972 for (i = 0; i < bmap->n_ineq; ++i) {
9973 uint32_t c_hash;
9974 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9975 isl_hash_hash(hash, c_hash);
9977 isl_hash_byte(hash, bmap->n_div & 0xFF);
9978 for (i = 0; i < bmap->n_div; ++i) {
9979 uint32_t c_hash;
9980 if (isl_int_is_zero(bmap->div[i][0]))
9981 continue;
9982 isl_hash_byte(hash, i & 0xFF);
9983 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9984 isl_hash_hash(hash, c_hash);
9986 isl_basic_map_free(bmap);
9987 return hash;
9990 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9992 return isl_basic_map_get_hash((isl_basic_map *)bset);
9995 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9997 int i;
9998 uint32_t hash;
10000 if (!map)
10001 return 0;
10002 map = isl_map_copy(map);
10003 map = isl_map_normalize(map);
10004 if (!map)
10005 return 0;
10007 hash = isl_hash_init();
10008 for (i = 0; i < map->n; ++i) {
10009 uint32_t bmap_hash;
10010 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10011 isl_hash_hash(hash, bmap_hash);
10014 isl_map_free(map);
10016 return hash;
10019 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10021 return isl_map_get_hash((isl_map *)set);
10024 /* Check if the value for dimension dim is completely determined
10025 * by the values of the other parameters and variables.
10026 * That is, check if dimension dim is involved in an equality.
10028 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10030 int i;
10031 unsigned nparam;
10033 if (!bset)
10034 return -1;
10035 nparam = isl_basic_set_n_param(bset);
10036 for (i = 0; i < bset->n_eq; ++i)
10037 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10038 return 1;
10039 return 0;
10042 /* Check if the value for dimension dim is completely determined
10043 * by the values of the other parameters and variables.
10044 * That is, check if dimension dim is involved in an equality
10045 * for each of the subsets.
10047 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10049 int i;
10051 if (!set)
10052 return -1;
10053 for (i = 0; i < set->n; ++i) {
10054 int unique;
10055 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10056 if (unique != 1)
10057 return unique;
10059 return 1;
10062 /* Return the number of basic maps in the (current) representation of "map".
10064 int isl_map_n_basic_map(__isl_keep isl_map *map)
10066 return map ? map->n : 0;
10069 int isl_set_n_basic_set(__isl_keep isl_set *set)
10071 return set ? set->n : 0;
10074 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10075 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10077 int i;
10079 if (!map)
10080 return isl_stat_error;
10082 for (i = 0; i < map->n; ++i)
10083 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10084 return isl_stat_error;
10086 return isl_stat_ok;
10089 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10090 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10092 int i;
10094 if (!set)
10095 return isl_stat_error;
10097 for (i = 0; i < set->n; ++i)
10098 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10099 return isl_stat_error;
10101 return isl_stat_ok;
10104 /* Return a list of basic sets, the union of which is equal to "set".
10106 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10107 __isl_keep isl_set *set)
10109 int i;
10110 isl_basic_set_list *list;
10112 if (!set)
10113 return NULL;
10115 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10116 for (i = 0; i < set->n; ++i) {
10117 isl_basic_set *bset;
10119 bset = isl_basic_set_copy(set->p[i]);
10120 list = isl_basic_set_list_add(list, bset);
10123 return list;
10126 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10128 isl_space *dim;
10130 if (!bset)
10131 return NULL;
10133 bset = isl_basic_set_cow(bset);
10134 if (!bset)
10135 return NULL;
10137 dim = isl_basic_set_get_space(bset);
10138 dim = isl_space_lift(dim, bset->n_div);
10139 if (!dim)
10140 goto error;
10141 isl_space_free(bset->dim);
10142 bset->dim = dim;
10143 bset->extra -= bset->n_div;
10144 bset->n_div = 0;
10146 bset = isl_basic_set_finalize(bset);
10148 return bset;
10149 error:
10150 isl_basic_set_free(bset);
10151 return NULL;
10154 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10156 int i;
10157 isl_space *dim;
10158 unsigned n_div;
10160 set = isl_set_align_divs(set);
10162 if (!set)
10163 return NULL;
10165 set = isl_set_cow(set);
10166 if (!set)
10167 return NULL;
10169 n_div = set->p[0]->n_div;
10170 dim = isl_set_get_space(set);
10171 dim = isl_space_lift(dim, n_div);
10172 if (!dim)
10173 goto error;
10174 isl_space_free(set->dim);
10175 set->dim = dim;
10177 for (i = 0; i < set->n; ++i) {
10178 set->p[i] = isl_basic_set_lift(set->p[i]);
10179 if (!set->p[i])
10180 goto error;
10183 return set;
10184 error:
10185 isl_set_free(set);
10186 return NULL;
10189 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10191 isl_space *dim;
10192 struct isl_basic_map *bmap;
10193 unsigned n_set;
10194 unsigned n_div;
10195 unsigned n_param;
10196 unsigned total;
10197 int i, k, l;
10199 set = isl_set_align_divs(set);
10201 if (!set)
10202 return NULL;
10204 dim = isl_set_get_space(set);
10205 if (set->n == 0 || set->p[0]->n_div == 0) {
10206 isl_set_free(set);
10207 return isl_map_identity(isl_space_map_from_set(dim));
10210 n_div = set->p[0]->n_div;
10211 dim = isl_space_map_from_set(dim);
10212 n_param = isl_space_dim(dim, isl_dim_param);
10213 n_set = isl_space_dim(dim, isl_dim_in);
10214 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10215 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10216 for (i = 0; i < n_set; ++i)
10217 bmap = var_equal(bmap, i);
10219 total = n_param + n_set + n_set + n_div;
10220 for (i = 0; i < n_div; ++i) {
10221 k = isl_basic_map_alloc_inequality(bmap);
10222 if (k < 0)
10223 goto error;
10224 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10225 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10226 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10227 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10228 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10229 set->p[0]->div[i][0]);
10231 l = isl_basic_map_alloc_inequality(bmap);
10232 if (l < 0)
10233 goto error;
10234 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10235 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10236 set->p[0]->div[i][0]);
10237 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10240 isl_set_free(set);
10241 bmap = isl_basic_map_simplify(bmap);
10242 bmap = isl_basic_map_finalize(bmap);
10243 return isl_map_from_basic_map(bmap);
10244 error:
10245 isl_set_free(set);
10246 isl_basic_map_free(bmap);
10247 return NULL;
10250 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10252 unsigned dim;
10253 int size = 0;
10255 if (!bset)
10256 return -1;
10258 dim = isl_basic_set_total_dim(bset);
10259 size += bset->n_eq * (1 + dim);
10260 size += bset->n_ineq * (1 + dim);
10261 size += bset->n_div * (2 + dim);
10263 return size;
10266 int isl_set_size(__isl_keep isl_set *set)
10268 int i;
10269 int size = 0;
10271 if (!set)
10272 return -1;
10274 for (i = 0; i < set->n; ++i)
10275 size += isl_basic_set_size(set->p[i]);
10277 return size;
10280 /* Check if there is any lower bound (if lower == 0) and/or upper
10281 * bound (if upper == 0) on the specified dim.
10283 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10284 enum isl_dim_type type, unsigned pos, int lower, int upper)
10286 int i;
10288 if (!bmap)
10289 return isl_bool_error;
10291 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10292 return isl_bool_error);
10294 pos += isl_basic_map_offset(bmap, type);
10296 for (i = 0; i < bmap->n_div; ++i) {
10297 if (isl_int_is_zero(bmap->div[i][0]))
10298 continue;
10299 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10300 return isl_bool_true;
10303 for (i = 0; i < bmap->n_eq; ++i)
10304 if (!isl_int_is_zero(bmap->eq[i][pos]))
10305 return isl_bool_true;
10307 for (i = 0; i < bmap->n_ineq; ++i) {
10308 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10309 if (sgn > 0)
10310 lower = 1;
10311 if (sgn < 0)
10312 upper = 1;
10315 return lower && upper;
10318 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10319 enum isl_dim_type type, unsigned pos)
10321 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10324 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10325 enum isl_dim_type type, unsigned pos)
10327 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10330 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10331 enum isl_dim_type type, unsigned pos)
10333 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10336 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10337 enum isl_dim_type type, unsigned pos)
10339 int i;
10341 if (!map)
10342 return -1;
10344 for (i = 0; i < map->n; ++i) {
10345 int bounded;
10346 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10347 if (bounded < 0 || !bounded)
10348 return bounded;
10351 return 1;
10354 /* Return 1 if the specified dim is involved in both an upper bound
10355 * and a lower bound.
10357 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10358 enum isl_dim_type type, unsigned pos)
10360 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10363 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10365 static isl_bool has_any_bound(__isl_keep isl_map *map,
10366 enum isl_dim_type type, unsigned pos,
10367 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10368 enum isl_dim_type type, unsigned pos))
10370 int i;
10372 if (!map)
10373 return isl_bool_error;
10375 for (i = 0; i < map->n; ++i) {
10376 isl_bool bounded;
10377 bounded = fn(map->p[i], type, pos);
10378 if (bounded < 0 || bounded)
10379 return bounded;
10382 return isl_bool_false;
10385 /* Return 1 if the specified dim is involved in any lower bound.
10387 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10388 enum isl_dim_type type, unsigned pos)
10390 return has_any_bound(set, type, pos,
10391 &isl_basic_map_dim_has_lower_bound);
10394 /* Return 1 if the specified dim is involved in any upper bound.
10396 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10397 enum isl_dim_type type, unsigned pos)
10399 return has_any_bound(set, type, pos,
10400 &isl_basic_map_dim_has_upper_bound);
10403 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10405 static isl_bool has_bound(__isl_keep isl_map *map,
10406 enum isl_dim_type type, unsigned pos,
10407 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10408 enum isl_dim_type type, unsigned pos))
10410 int i;
10412 if (!map)
10413 return isl_bool_error;
10415 for (i = 0; i < map->n; ++i) {
10416 isl_bool bounded;
10417 bounded = fn(map->p[i], type, pos);
10418 if (bounded < 0 || !bounded)
10419 return bounded;
10422 return isl_bool_true;
10425 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10427 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10428 enum isl_dim_type type, unsigned pos)
10430 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10433 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10435 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10436 enum isl_dim_type type, unsigned pos)
10438 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10441 /* For each of the "n" variables starting at "first", determine
10442 * the sign of the variable and put the results in the first "n"
10443 * elements of the array "signs".
10444 * Sign
10445 * 1 means that the variable is non-negative
10446 * -1 means that the variable is non-positive
10447 * 0 means the variable attains both positive and negative values.
10449 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10450 unsigned first, unsigned n, int *signs)
10452 isl_vec *bound = NULL;
10453 struct isl_tab *tab = NULL;
10454 struct isl_tab_undo *snap;
10455 int i;
10457 if (!bset || !signs)
10458 return -1;
10460 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10461 tab = isl_tab_from_basic_set(bset, 0);
10462 if (!bound || !tab)
10463 goto error;
10465 isl_seq_clr(bound->el, bound->size);
10466 isl_int_set_si(bound->el[0], -1);
10468 snap = isl_tab_snap(tab);
10469 for (i = 0; i < n; ++i) {
10470 int empty;
10472 isl_int_set_si(bound->el[1 + first + i], -1);
10473 if (isl_tab_add_ineq(tab, bound->el) < 0)
10474 goto error;
10475 empty = tab->empty;
10476 isl_int_set_si(bound->el[1 + first + i], 0);
10477 if (isl_tab_rollback(tab, snap) < 0)
10478 goto error;
10480 if (empty) {
10481 signs[i] = 1;
10482 continue;
10485 isl_int_set_si(bound->el[1 + first + i], 1);
10486 if (isl_tab_add_ineq(tab, bound->el) < 0)
10487 goto error;
10488 empty = tab->empty;
10489 isl_int_set_si(bound->el[1 + first + i], 0);
10490 if (isl_tab_rollback(tab, snap) < 0)
10491 goto error;
10493 signs[i] = empty ? -1 : 0;
10496 isl_tab_free(tab);
10497 isl_vec_free(bound);
10498 return 0;
10499 error:
10500 isl_tab_free(tab);
10501 isl_vec_free(bound);
10502 return -1;
10505 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10506 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10508 if (!bset || !signs)
10509 return -1;
10510 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10511 return -1);
10513 first += pos(bset->dim, type) - 1;
10514 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10517 /* Is it possible for the integer division "div" to depend (possibly
10518 * indirectly) on any output dimensions?
10520 * If the div is undefined, then we conservatively assume that it
10521 * may depend on them.
10522 * Otherwise, we check if it actually depends on them or on any integer
10523 * divisions that may depend on them.
10525 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10527 int i;
10528 unsigned n_out, o_out;
10529 unsigned n_div, o_div;
10531 if (isl_int_is_zero(bmap->div[div][0]))
10532 return 1;
10534 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10535 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10537 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10538 return 1;
10540 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10541 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10543 for (i = 0; i < n_div; ++i) {
10544 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10545 continue;
10546 if (div_may_involve_output(bmap, i))
10547 return 1;
10550 return 0;
10553 /* Return the first integer division of "bmap" in the range
10554 * [first, first + n[ that may depend on any output dimensions and
10555 * that has a non-zero coefficient in "c" (where the first coefficient
10556 * in "c" corresponds to integer division "first").
10558 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10559 isl_int *c, int first, int n)
10561 int k;
10563 if (!bmap)
10564 return -1;
10566 for (k = first; k < first + n; ++k) {
10567 if (isl_int_is_zero(c[k]))
10568 continue;
10569 if (div_may_involve_output(bmap, k))
10570 return k;
10573 return first + n;
10576 /* Look for a pair of inequality constraints in "bmap" of the form
10578 * -l + i >= 0 or i >= l
10579 * and
10580 * n + l - i >= 0 or i <= l + n
10582 * with n < "m" and i the output dimension at position "pos".
10583 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10584 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10585 * and earlier output dimensions, as well as integer divisions that do
10586 * not involve any of the output dimensions.
10588 * Return the index of the first inequality constraint or bmap->n_ineq
10589 * if no such pair can be found.
10591 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10592 int pos, isl_int m)
10594 int i, j;
10595 isl_ctx *ctx;
10596 unsigned total;
10597 unsigned n_div, o_div;
10598 unsigned n_out, o_out;
10599 int less;
10601 if (!bmap)
10602 return -1;
10604 ctx = isl_basic_map_get_ctx(bmap);
10605 total = isl_basic_map_total_dim(bmap);
10606 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10607 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10608 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10609 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10610 for (i = 0; i < bmap->n_ineq; ++i) {
10611 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10612 continue;
10613 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10614 n_out - (pos + 1)) != -1)
10615 continue;
10616 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10617 0, n_div) < n_div)
10618 continue;
10619 for (j = i + 1; j < bmap->n_ineq; ++j) {
10620 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10621 ctx->one))
10622 continue;
10623 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10624 bmap->ineq[j] + 1, total))
10625 continue;
10626 break;
10628 if (j >= bmap->n_ineq)
10629 continue;
10630 isl_int_add(bmap->ineq[i][0],
10631 bmap->ineq[i][0], bmap->ineq[j][0]);
10632 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10633 isl_int_sub(bmap->ineq[i][0],
10634 bmap->ineq[i][0], bmap->ineq[j][0]);
10635 if (!less)
10636 continue;
10637 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10638 return i;
10639 else
10640 return j;
10643 return bmap->n_ineq;
10646 /* Return the index of the equality of "bmap" that defines
10647 * the output dimension "pos" in terms of earlier dimensions.
10648 * The equality may also involve integer divisions, as long
10649 * as those integer divisions are defined in terms of
10650 * parameters or input dimensions.
10651 * In this case, *div is set to the number of integer divisions and
10652 * *ineq is set to the number of inequality constraints (provided
10653 * div and ineq are not NULL).
10655 * The equality may also involve a single integer division involving
10656 * the output dimensions (typically only output dimension "pos") as
10657 * long as the coefficient of output dimension "pos" is 1 or -1 and
10658 * there is a pair of constraints i >= l and i <= l + n, with i referring
10659 * to output dimension "pos", l an expression involving only earlier
10660 * dimensions and n smaller than the coefficient of the integer division
10661 * in the equality. In this case, the output dimension can be defined
10662 * in terms of a modulo expression that does not involve the integer division.
10663 * *div is then set to this single integer division and
10664 * *ineq is set to the index of constraint i >= l.
10666 * Return bmap->n_eq if there is no such equality.
10667 * Return -1 on error.
10669 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10670 int pos, int *div, int *ineq)
10672 int j, k, l;
10673 unsigned n_out, o_out;
10674 unsigned n_div, o_div;
10676 if (!bmap)
10677 return -1;
10679 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10680 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10681 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10682 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10684 if (ineq)
10685 *ineq = bmap->n_ineq;
10686 if (div)
10687 *div = n_div;
10688 for (j = 0; j < bmap->n_eq; ++j) {
10689 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10690 continue;
10691 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10692 n_out - (pos + 1)) != -1)
10693 continue;
10694 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10695 0, n_div);
10696 if (k >= n_div)
10697 return j;
10698 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10699 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10700 continue;
10701 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10702 k + 1, n_div - (k+1)) < n_div)
10703 continue;
10704 l = find_modulo_constraint_pair(bmap, pos,
10705 bmap->eq[j][o_div + k]);
10706 if (l < 0)
10707 return -1;
10708 if (l >= bmap->n_ineq)
10709 continue;
10710 if (div)
10711 *div = k;
10712 if (ineq)
10713 *ineq = l;
10714 return j;
10717 return bmap->n_eq;
10720 /* Check if the given basic map is obviously single-valued.
10721 * In particular, for each output dimension, check that there is
10722 * an equality that defines the output dimension in terms of
10723 * earlier dimensions.
10725 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10727 int i;
10728 unsigned n_out;
10730 if (!bmap)
10731 return isl_bool_error;
10733 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10735 for (i = 0; i < n_out; ++i) {
10736 int eq;
10738 eq = isl_basic_map_output_defining_equality(bmap, i,
10739 NULL, NULL);
10740 if (eq < 0)
10741 return isl_bool_error;
10742 if (eq >= bmap->n_eq)
10743 return isl_bool_false;
10746 return isl_bool_true;
10749 /* Check if the given basic map is single-valued.
10750 * We simply compute
10752 * M \circ M^-1
10754 * and check if the result is a subset of the identity mapping.
10756 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10758 isl_space *space;
10759 isl_basic_map *test;
10760 isl_basic_map *id;
10761 isl_bool sv;
10763 sv = isl_basic_map_plain_is_single_valued(bmap);
10764 if (sv < 0 || sv)
10765 return sv;
10767 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10768 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10770 space = isl_basic_map_get_space(bmap);
10771 space = isl_space_map_from_set(isl_space_range(space));
10772 id = isl_basic_map_identity(space);
10774 sv = isl_basic_map_is_subset(test, id);
10776 isl_basic_map_free(test);
10777 isl_basic_map_free(id);
10779 return sv;
10782 /* Check if the given map is obviously single-valued.
10784 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10786 if (!map)
10787 return isl_bool_error;
10788 if (map->n == 0)
10789 return isl_bool_true;
10790 if (map->n >= 2)
10791 return isl_bool_false;
10793 return isl_basic_map_plain_is_single_valued(map->p[0]);
10796 /* Check if the given map is single-valued.
10797 * We simply compute
10799 * M \circ M^-1
10801 * and check if the result is a subset of the identity mapping.
10803 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10805 isl_space *dim;
10806 isl_map *test;
10807 isl_map *id;
10808 isl_bool sv;
10810 sv = isl_map_plain_is_single_valued(map);
10811 if (sv < 0 || sv)
10812 return sv;
10814 test = isl_map_reverse(isl_map_copy(map));
10815 test = isl_map_apply_range(test, isl_map_copy(map));
10817 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10818 id = isl_map_identity(dim);
10820 sv = isl_map_is_subset(test, id);
10822 isl_map_free(test);
10823 isl_map_free(id);
10825 return sv;
10828 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10830 isl_bool in;
10832 map = isl_map_copy(map);
10833 map = isl_map_reverse(map);
10834 in = isl_map_is_single_valued(map);
10835 isl_map_free(map);
10837 return in;
10840 /* Check if the given map is obviously injective.
10842 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10844 isl_bool in;
10846 map = isl_map_copy(map);
10847 map = isl_map_reverse(map);
10848 in = isl_map_plain_is_single_valued(map);
10849 isl_map_free(map);
10851 return in;
10854 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10856 isl_bool sv;
10858 sv = isl_map_is_single_valued(map);
10859 if (sv < 0 || !sv)
10860 return sv;
10862 return isl_map_is_injective(map);
10865 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10867 return isl_map_is_single_valued((isl_map *)set);
10870 /* Does "map" only map elements to themselves?
10872 * If the domain and range spaces are different, then "map"
10873 * is considered not to be an identity relation, even if it is empty.
10874 * Otherwise, construct the maximal identity relation and
10875 * check whether "map" is a subset of this relation.
10877 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10879 isl_space *space;
10880 isl_map *id;
10881 isl_bool equal, is_identity;
10883 space = isl_map_get_space(map);
10884 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10885 isl_space_free(space);
10886 if (equal < 0 || !equal)
10887 return equal;
10889 id = isl_map_identity(isl_map_get_space(map));
10890 is_identity = isl_map_is_subset(map, id);
10891 isl_map_free(id);
10893 return is_identity;
10896 int isl_map_is_translation(__isl_keep isl_map *map)
10898 int ok;
10899 isl_set *delta;
10901 delta = isl_map_deltas(isl_map_copy(map));
10902 ok = isl_set_is_singleton(delta);
10903 isl_set_free(delta);
10905 return ok;
10908 static int unique(isl_int *p, unsigned pos, unsigned len)
10910 if (isl_seq_first_non_zero(p, pos) != -1)
10911 return 0;
10912 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10913 return 0;
10914 return 1;
10917 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10919 int i, j;
10920 unsigned nvar;
10921 unsigned ovar;
10923 if (!bset)
10924 return -1;
10926 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10927 return 0;
10929 nvar = isl_basic_set_dim(bset, isl_dim_set);
10930 ovar = isl_space_offset(bset->dim, isl_dim_set);
10931 for (j = 0; j < nvar; ++j) {
10932 int lower = 0, upper = 0;
10933 for (i = 0; i < bset->n_eq; ++i) {
10934 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10935 continue;
10936 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10937 return 0;
10938 break;
10940 if (i < bset->n_eq)
10941 continue;
10942 for (i = 0; i < bset->n_ineq; ++i) {
10943 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10944 continue;
10945 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10946 return 0;
10947 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10948 lower = 1;
10949 else
10950 upper = 1;
10952 if (!lower || !upper)
10953 return 0;
10956 return 1;
10959 int isl_set_is_box(__isl_keep isl_set *set)
10961 if (!set)
10962 return -1;
10963 if (set->n != 1)
10964 return 0;
10966 return isl_basic_set_is_box(set->p[0]);
10969 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10971 if (!bset)
10972 return isl_bool_error;
10974 return isl_space_is_wrapping(bset->dim);
10977 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10979 if (!set)
10980 return isl_bool_error;
10982 return isl_space_is_wrapping(set->dim);
10985 /* Modify the space of "map" through a call to "change".
10986 * If "can_change" is set (not NULL), then first call it to check
10987 * if the modification is allowed, printing the error message "cannot_change"
10988 * if it is not.
10990 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10991 isl_bool (*can_change)(__isl_keep isl_map *map),
10992 const char *cannot_change,
10993 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10995 isl_bool ok;
10996 isl_space *space;
10998 if (!map)
10999 return NULL;
11001 ok = can_change ? can_change(map) : isl_bool_true;
11002 if (ok < 0)
11003 return isl_map_free(map);
11004 if (!ok)
11005 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11006 return isl_map_free(map));
11008 space = change(isl_map_get_space(map));
11009 map = isl_map_reset_space(map, space);
11011 return map;
11014 /* Is the domain of "map" a wrapped relation?
11016 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11018 if (!map)
11019 return isl_bool_error;
11021 return isl_space_domain_is_wrapping(map->dim);
11024 /* Is the range of "map" a wrapped relation?
11026 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11028 if (!map)
11029 return isl_bool_error;
11031 return isl_space_range_is_wrapping(map->dim);
11034 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11036 bmap = isl_basic_map_cow(bmap);
11037 if (!bmap)
11038 return NULL;
11040 bmap->dim = isl_space_wrap(bmap->dim);
11041 if (!bmap->dim)
11042 goto error;
11044 bmap = isl_basic_map_finalize(bmap);
11046 return (isl_basic_set *)bmap;
11047 error:
11048 isl_basic_map_free(bmap);
11049 return NULL;
11052 /* Given a map A -> B, return the set (A -> B).
11054 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11056 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11059 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11061 bset = isl_basic_set_cow(bset);
11062 if (!bset)
11063 return NULL;
11065 bset->dim = isl_space_unwrap(bset->dim);
11066 if (!bset->dim)
11067 goto error;
11069 bset = isl_basic_set_finalize(bset);
11071 return (isl_basic_map *)bset;
11072 error:
11073 isl_basic_set_free(bset);
11074 return NULL;
11077 /* Given a set (A -> B), return the map A -> B.
11078 * Error out if "set" is not of the form (A -> B).
11080 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11082 return isl_map_change_space(set, &isl_set_is_wrapping,
11083 "not a wrapping set", &isl_space_unwrap);
11086 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11087 enum isl_dim_type type)
11089 if (!bmap)
11090 return NULL;
11092 if (!isl_space_is_named_or_nested(bmap->dim, type))
11093 return bmap;
11095 bmap = isl_basic_map_cow(bmap);
11096 if (!bmap)
11097 return NULL;
11099 bmap->dim = isl_space_reset(bmap->dim, type);
11100 if (!bmap->dim)
11101 goto error;
11103 bmap = isl_basic_map_finalize(bmap);
11105 return bmap;
11106 error:
11107 isl_basic_map_free(bmap);
11108 return NULL;
11111 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11112 enum isl_dim_type type)
11114 int i;
11116 if (!map)
11117 return NULL;
11119 if (!isl_space_is_named_or_nested(map->dim, type))
11120 return map;
11122 map = isl_map_cow(map);
11123 if (!map)
11124 return NULL;
11126 for (i = 0; i < map->n; ++i) {
11127 map->p[i] = isl_basic_map_reset(map->p[i], type);
11128 if (!map->p[i])
11129 goto error;
11131 map->dim = isl_space_reset(map->dim, type);
11132 if (!map->dim)
11133 goto error;
11135 return map;
11136 error:
11137 isl_map_free(map);
11138 return NULL;
11141 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11143 if (!bmap)
11144 return NULL;
11146 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11147 return bmap;
11149 bmap = isl_basic_map_cow(bmap);
11150 if (!bmap)
11151 return NULL;
11153 bmap->dim = isl_space_flatten(bmap->dim);
11154 if (!bmap->dim)
11155 goto error;
11157 bmap = isl_basic_map_finalize(bmap);
11159 return bmap;
11160 error:
11161 isl_basic_map_free(bmap);
11162 return NULL;
11165 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11167 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11170 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11171 __isl_take isl_basic_map *bmap)
11173 if (!bmap)
11174 return NULL;
11176 if (!bmap->dim->nested[0])
11177 return bmap;
11179 bmap = isl_basic_map_cow(bmap);
11180 if (!bmap)
11181 return NULL;
11183 bmap->dim = isl_space_flatten_domain(bmap->dim);
11184 if (!bmap->dim)
11185 goto error;
11187 bmap = isl_basic_map_finalize(bmap);
11189 return bmap;
11190 error:
11191 isl_basic_map_free(bmap);
11192 return NULL;
11195 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11196 __isl_take isl_basic_map *bmap)
11198 if (!bmap)
11199 return NULL;
11201 if (!bmap->dim->nested[1])
11202 return bmap;
11204 bmap = isl_basic_map_cow(bmap);
11205 if (!bmap)
11206 return NULL;
11208 bmap->dim = isl_space_flatten_range(bmap->dim);
11209 if (!bmap->dim)
11210 goto error;
11212 bmap = isl_basic_map_finalize(bmap);
11214 return bmap;
11215 error:
11216 isl_basic_map_free(bmap);
11217 return NULL;
11220 /* Remove any internal structure from the spaces of domain and range of "map".
11222 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11224 if (!map)
11225 return NULL;
11227 if (!map->dim->nested[0] && !map->dim->nested[1])
11228 return map;
11230 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11233 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11235 return (isl_set *)isl_map_flatten((isl_map *)set);
11238 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11240 isl_space *dim, *flat_dim;
11241 isl_map *map;
11243 dim = isl_set_get_space(set);
11244 flat_dim = isl_space_flatten(isl_space_copy(dim));
11245 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11246 map = isl_map_intersect_domain(map, set);
11248 return map;
11251 /* Remove any internal structure from the space of the domain of "map".
11253 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11255 if (!map)
11256 return NULL;
11258 if (!map->dim->nested[0])
11259 return map;
11261 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11264 /* Remove any internal structure from the space of the range of "map".
11266 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11268 if (!map)
11269 return NULL;
11271 if (!map->dim->nested[1])
11272 return map;
11274 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11277 /* Reorder the dimensions of "bmap" according to the given dim_map
11278 * and set the dimension specification to "dim" and
11279 * perform Gaussian elimination on the result.
11281 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11282 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11284 isl_basic_map *res;
11285 unsigned flags;
11287 bmap = isl_basic_map_cow(bmap);
11288 if (!bmap || !dim || !dim_map)
11289 goto error;
11291 flags = bmap->flags;
11292 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11293 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11294 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11295 res = isl_basic_map_alloc_space(dim,
11296 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11297 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11298 if (res)
11299 res->flags = flags;
11300 res = isl_basic_map_gauss(res, NULL);
11301 res = isl_basic_map_finalize(res);
11302 return res;
11303 error:
11304 free(dim_map);
11305 isl_basic_map_free(bmap);
11306 isl_space_free(dim);
11307 return NULL;
11310 /* Reorder the dimensions of "map" according to given reordering.
11312 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11313 __isl_take isl_reordering *r)
11315 int i;
11316 struct isl_dim_map *dim_map;
11318 map = isl_map_cow(map);
11319 dim_map = isl_dim_map_from_reordering(r);
11320 if (!map || !r || !dim_map)
11321 goto error;
11323 for (i = 0; i < map->n; ++i) {
11324 struct isl_dim_map *dim_map_i;
11326 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11328 map->p[i] = isl_basic_map_realign(map->p[i],
11329 isl_space_copy(r->dim), dim_map_i);
11331 if (!map->p[i])
11332 goto error;
11335 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11337 isl_reordering_free(r);
11338 free(dim_map);
11339 return map;
11340 error:
11341 free(dim_map);
11342 isl_map_free(map);
11343 isl_reordering_free(r);
11344 return NULL;
11347 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11348 __isl_take isl_reordering *r)
11350 return (isl_set *)isl_map_realign((isl_map *)set, r);
11353 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11354 __isl_take isl_space *model)
11356 isl_ctx *ctx;
11358 if (!map || !model)
11359 goto error;
11361 ctx = isl_space_get_ctx(model);
11362 if (!isl_space_has_named_params(model))
11363 isl_die(ctx, isl_error_invalid,
11364 "model has unnamed parameters", goto error);
11365 if (!isl_space_has_named_params(map->dim))
11366 isl_die(ctx, isl_error_invalid,
11367 "relation has unnamed parameters", goto error);
11368 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11369 isl_reordering *exp;
11371 model = isl_space_drop_dims(model, isl_dim_in,
11372 0, isl_space_dim(model, isl_dim_in));
11373 model = isl_space_drop_dims(model, isl_dim_out,
11374 0, isl_space_dim(model, isl_dim_out));
11375 exp = isl_parameter_alignment_reordering(map->dim, model);
11376 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11377 map = isl_map_realign(map, exp);
11380 isl_space_free(model);
11381 return map;
11382 error:
11383 isl_space_free(model);
11384 isl_map_free(map);
11385 return NULL;
11388 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11389 __isl_take isl_space *model)
11391 return isl_map_align_params(set, model);
11394 /* Align the parameters of "bmap" to those of "model", introducing
11395 * additional parameters if needed.
11397 __isl_give isl_basic_map *isl_basic_map_align_params(
11398 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11400 isl_ctx *ctx;
11402 if (!bmap || !model)
11403 goto error;
11405 ctx = isl_space_get_ctx(model);
11406 if (!isl_space_has_named_params(model))
11407 isl_die(ctx, isl_error_invalid,
11408 "model has unnamed parameters", goto error);
11409 if (!isl_space_has_named_params(bmap->dim))
11410 isl_die(ctx, isl_error_invalid,
11411 "relation has unnamed parameters", goto error);
11412 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11413 isl_reordering *exp;
11414 struct isl_dim_map *dim_map;
11416 model = isl_space_drop_dims(model, isl_dim_in,
11417 0, isl_space_dim(model, isl_dim_in));
11418 model = isl_space_drop_dims(model, isl_dim_out,
11419 0, isl_space_dim(model, isl_dim_out));
11420 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11421 exp = isl_reordering_extend_space(exp,
11422 isl_basic_map_get_space(bmap));
11423 dim_map = isl_dim_map_from_reordering(exp);
11424 bmap = isl_basic_map_realign(bmap,
11425 exp ? isl_space_copy(exp->dim) : NULL,
11426 isl_dim_map_extend(dim_map, bmap));
11427 isl_reordering_free(exp);
11428 free(dim_map);
11431 isl_space_free(model);
11432 return bmap;
11433 error:
11434 isl_space_free(model);
11435 isl_basic_map_free(bmap);
11436 return NULL;
11439 /* Align the parameters of "bset" to those of "model", introducing
11440 * additional parameters if needed.
11442 __isl_give isl_basic_set *isl_basic_set_align_params(
11443 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11445 return isl_basic_map_align_params(bset, model);
11448 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11449 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11450 enum isl_dim_type c2, enum isl_dim_type c3,
11451 enum isl_dim_type c4, enum isl_dim_type c5)
11453 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11454 struct isl_mat *mat;
11455 int i, j, k;
11456 int pos;
11458 if (!bmap)
11459 return NULL;
11460 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11461 isl_basic_map_total_dim(bmap) + 1);
11462 if (!mat)
11463 return NULL;
11464 for (i = 0; i < bmap->n_eq; ++i)
11465 for (j = 0, pos = 0; j < 5; ++j) {
11466 int off = isl_basic_map_offset(bmap, c[j]);
11467 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11468 isl_int_set(mat->row[i][pos],
11469 bmap->eq[i][off + k]);
11470 ++pos;
11474 return mat;
11477 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11478 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11479 enum isl_dim_type c2, enum isl_dim_type c3,
11480 enum isl_dim_type c4, enum isl_dim_type c5)
11482 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11483 struct isl_mat *mat;
11484 int i, j, k;
11485 int pos;
11487 if (!bmap)
11488 return NULL;
11489 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11490 isl_basic_map_total_dim(bmap) + 1);
11491 if (!mat)
11492 return NULL;
11493 for (i = 0; i < bmap->n_ineq; ++i)
11494 for (j = 0, pos = 0; j < 5; ++j) {
11495 int off = isl_basic_map_offset(bmap, c[j]);
11496 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11497 isl_int_set(mat->row[i][pos],
11498 bmap->ineq[i][off + k]);
11499 ++pos;
11503 return mat;
11506 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11507 __isl_take isl_space *dim,
11508 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11509 enum isl_dim_type c2, enum isl_dim_type c3,
11510 enum isl_dim_type c4, enum isl_dim_type c5)
11512 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11513 isl_basic_map *bmap;
11514 unsigned total;
11515 unsigned extra;
11516 int i, j, k, l;
11517 int pos;
11519 if (!dim || !eq || !ineq)
11520 goto error;
11522 if (eq->n_col != ineq->n_col)
11523 isl_die(dim->ctx, isl_error_invalid,
11524 "equalities and inequalities matrices should have "
11525 "same number of columns", goto error);
11527 total = 1 + isl_space_dim(dim, isl_dim_all);
11529 if (eq->n_col < total)
11530 isl_die(dim->ctx, isl_error_invalid,
11531 "number of columns too small", goto error);
11533 extra = eq->n_col - total;
11535 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11536 eq->n_row, ineq->n_row);
11537 if (!bmap)
11538 goto error;
11539 for (i = 0; i < extra; ++i) {
11540 k = isl_basic_map_alloc_div(bmap);
11541 if (k < 0)
11542 goto error;
11543 isl_int_set_si(bmap->div[k][0], 0);
11545 for (i = 0; i < eq->n_row; ++i) {
11546 l = isl_basic_map_alloc_equality(bmap);
11547 if (l < 0)
11548 goto error;
11549 for (j = 0, pos = 0; j < 5; ++j) {
11550 int off = isl_basic_map_offset(bmap, c[j]);
11551 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11552 isl_int_set(bmap->eq[l][off + k],
11553 eq->row[i][pos]);
11554 ++pos;
11558 for (i = 0; i < ineq->n_row; ++i) {
11559 l = isl_basic_map_alloc_inequality(bmap);
11560 if (l < 0)
11561 goto error;
11562 for (j = 0, pos = 0; j < 5; ++j) {
11563 int off = isl_basic_map_offset(bmap, c[j]);
11564 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11565 isl_int_set(bmap->ineq[l][off + k],
11566 ineq->row[i][pos]);
11567 ++pos;
11572 isl_space_free(dim);
11573 isl_mat_free(eq);
11574 isl_mat_free(ineq);
11576 bmap = isl_basic_map_simplify(bmap);
11577 return isl_basic_map_finalize(bmap);
11578 error:
11579 isl_space_free(dim);
11580 isl_mat_free(eq);
11581 isl_mat_free(ineq);
11582 return NULL;
11585 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11586 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11587 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11589 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11590 c1, c2, c3, c4, isl_dim_in);
11593 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11594 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11595 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11597 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11598 c1, c2, c3, c4, isl_dim_in);
11601 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11602 __isl_take isl_space *dim,
11603 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11604 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11606 return (isl_basic_set*)
11607 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11608 c1, c2, c3, c4, isl_dim_in);
11611 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11613 if (!bmap)
11614 return isl_bool_error;
11616 return isl_space_can_zip(bmap->dim);
11619 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11621 if (!map)
11622 return isl_bool_error;
11624 return isl_space_can_zip(map->dim);
11627 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11628 * (A -> C) -> (B -> D).
11630 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11632 unsigned pos;
11633 unsigned n1;
11634 unsigned n2;
11636 if (!bmap)
11637 return NULL;
11639 if (!isl_basic_map_can_zip(bmap))
11640 isl_die(bmap->ctx, isl_error_invalid,
11641 "basic map cannot be zipped", goto error);
11642 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11643 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11644 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11645 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11646 bmap = isl_basic_map_cow(bmap);
11647 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11648 if (!bmap)
11649 return NULL;
11650 bmap->dim = isl_space_zip(bmap->dim);
11651 if (!bmap->dim)
11652 goto error;
11653 bmap = isl_basic_map_mark_final(bmap);
11654 return bmap;
11655 error:
11656 isl_basic_map_free(bmap);
11657 return NULL;
11660 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11661 * (A -> C) -> (B -> D).
11663 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11665 int i;
11667 if (!map)
11668 return NULL;
11670 if (!isl_map_can_zip(map))
11671 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11672 goto error);
11674 map = isl_map_cow(map);
11675 if (!map)
11676 return NULL;
11678 for (i = 0; i < map->n; ++i) {
11679 map->p[i] = isl_basic_map_zip(map->p[i]);
11680 if (!map->p[i])
11681 goto error;
11684 map->dim = isl_space_zip(map->dim);
11685 if (!map->dim)
11686 goto error;
11688 return map;
11689 error:
11690 isl_map_free(map);
11691 return NULL;
11694 /* Can we apply isl_basic_map_curry to "bmap"?
11695 * That is, does it have a nested relation in its domain?
11697 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11699 if (!bmap)
11700 return isl_bool_error;
11702 return isl_space_can_curry(bmap->dim);
11705 /* Can we apply isl_map_curry to "map"?
11706 * That is, does it have a nested relation in its domain?
11708 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11710 if (!map)
11711 return isl_bool_error;
11713 return isl_space_can_curry(map->dim);
11716 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11717 * A -> (B -> C).
11719 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11722 if (!bmap)
11723 return NULL;
11725 if (!isl_basic_map_can_curry(bmap))
11726 isl_die(bmap->ctx, isl_error_invalid,
11727 "basic map cannot be curried", goto error);
11728 bmap = isl_basic_map_cow(bmap);
11729 if (!bmap)
11730 return NULL;
11731 bmap->dim = isl_space_curry(bmap->dim);
11732 if (!bmap->dim)
11733 goto error;
11734 bmap = isl_basic_map_mark_final(bmap);
11735 return bmap;
11736 error:
11737 isl_basic_map_free(bmap);
11738 return NULL;
11741 /* Given a map (A -> B) -> C, return the corresponding map
11742 * A -> (B -> C).
11744 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11746 return isl_map_change_space(map, &isl_map_can_curry,
11747 "map cannot be curried", &isl_space_curry);
11750 /* Can isl_map_range_curry be applied to "map"?
11751 * That is, does it have a nested relation in its range,
11752 * the domain of which is itself a nested relation?
11754 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11756 if (!map)
11757 return isl_bool_error;
11759 return isl_space_can_range_curry(map->dim);
11762 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11763 * A -> (B -> (C -> D)).
11765 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11767 return isl_map_change_space(map, &isl_map_can_range_curry,
11768 "map range cannot be curried",
11769 &isl_space_range_curry);
11772 /* Can we apply isl_basic_map_uncurry to "bmap"?
11773 * That is, does it have a nested relation in its domain?
11775 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11777 if (!bmap)
11778 return isl_bool_error;
11780 return isl_space_can_uncurry(bmap->dim);
11783 /* Can we apply isl_map_uncurry to "map"?
11784 * That is, does it have a nested relation in its domain?
11786 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11788 if (!map)
11789 return isl_bool_error;
11791 return isl_space_can_uncurry(map->dim);
11794 /* Given a basic map A -> (B -> C), return the corresponding basic map
11795 * (A -> B) -> C.
11797 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11800 if (!bmap)
11801 return NULL;
11803 if (!isl_basic_map_can_uncurry(bmap))
11804 isl_die(bmap->ctx, isl_error_invalid,
11805 "basic map cannot be uncurried",
11806 return isl_basic_map_free(bmap));
11807 bmap = isl_basic_map_cow(bmap);
11808 if (!bmap)
11809 return NULL;
11810 bmap->dim = isl_space_uncurry(bmap->dim);
11811 if (!bmap->dim)
11812 return isl_basic_map_free(bmap);
11813 bmap = isl_basic_map_mark_final(bmap);
11814 return bmap;
11817 /* Given a map A -> (B -> C), return the corresponding map
11818 * (A -> B) -> C.
11820 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11822 return isl_map_change_space(map, &isl_map_can_uncurry,
11823 "map cannot be uncurried", &isl_space_uncurry);
11826 /* Construct a basic map mapping the domain of the affine expression
11827 * to a one-dimensional range prescribed by the affine expression.
11829 * A NaN affine expression cannot be converted to a basic map.
11831 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11833 int k;
11834 int pos;
11835 isl_bool is_nan;
11836 isl_local_space *ls;
11837 isl_basic_map *bmap = NULL;
11839 if (!aff)
11840 return NULL;
11841 is_nan = isl_aff_is_nan(aff);
11842 if (is_nan < 0)
11843 goto error;
11844 if (is_nan)
11845 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11846 "cannot convert NaN", goto error);
11848 ls = isl_aff_get_local_space(aff);
11849 bmap = isl_basic_map_from_local_space(ls);
11850 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11851 k = isl_basic_map_alloc_equality(bmap);
11852 if (k < 0)
11853 goto error;
11855 pos = isl_basic_map_offset(bmap, isl_dim_out);
11856 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11857 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11858 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11859 aff->v->size - (pos + 1));
11861 isl_aff_free(aff);
11862 bmap = isl_basic_map_finalize(bmap);
11863 return bmap;
11864 error:
11865 isl_aff_free(aff);
11866 isl_basic_map_free(bmap);
11867 return NULL;
11870 /* Construct a map mapping the domain of the affine expression
11871 * to a one-dimensional range prescribed by the affine expression.
11873 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11875 isl_basic_map *bmap;
11877 bmap = isl_basic_map_from_aff(aff);
11878 return isl_map_from_basic_map(bmap);
11881 /* Construct a basic map mapping the domain the multi-affine expression
11882 * to its range, with each dimension in the range equated to the
11883 * corresponding affine expression.
11885 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11886 __isl_take isl_multi_aff *maff)
11888 int i;
11889 isl_space *space;
11890 isl_basic_map *bmap;
11892 if (!maff)
11893 return NULL;
11895 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11896 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11897 "invalid space", goto error);
11899 space = isl_space_domain(isl_multi_aff_get_space(maff));
11900 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11902 for (i = 0; i < maff->n; ++i) {
11903 isl_aff *aff;
11904 isl_basic_map *bmap_i;
11906 aff = isl_aff_copy(maff->p[i]);
11907 bmap_i = isl_basic_map_from_aff(aff);
11909 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11912 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11914 isl_multi_aff_free(maff);
11915 return bmap;
11916 error:
11917 isl_multi_aff_free(maff);
11918 return NULL;
11921 /* Construct a map mapping the domain the multi-affine expression
11922 * to its range, with each dimension in the range equated to the
11923 * corresponding affine expression.
11925 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11927 isl_basic_map *bmap;
11929 bmap = isl_basic_map_from_multi_aff(maff);
11930 return isl_map_from_basic_map(bmap);
11933 /* Construct a basic map mapping a domain in the given space to
11934 * to an n-dimensional range, with n the number of elements in the list,
11935 * where each coordinate in the range is prescribed by the
11936 * corresponding affine expression.
11937 * The domains of all affine expressions in the list are assumed to match
11938 * domain_dim.
11940 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11941 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11943 int i;
11944 isl_space *dim;
11945 isl_basic_map *bmap;
11947 if (!list)
11948 return NULL;
11950 dim = isl_space_from_domain(domain_dim);
11951 bmap = isl_basic_map_universe(dim);
11953 for (i = 0; i < list->n; ++i) {
11954 isl_aff *aff;
11955 isl_basic_map *bmap_i;
11957 aff = isl_aff_copy(list->p[i]);
11958 bmap_i = isl_basic_map_from_aff(aff);
11960 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11963 isl_aff_list_free(list);
11964 return bmap;
11967 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11968 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11970 return isl_map_equate(set, type1, pos1, type2, pos2);
11973 /* Construct a basic map where the given dimensions are equal to each other.
11975 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11976 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11978 isl_basic_map *bmap = NULL;
11979 int i;
11981 if (!space)
11982 return NULL;
11984 if (pos1 >= isl_space_dim(space, type1))
11985 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11986 "index out of bounds", goto error);
11987 if (pos2 >= isl_space_dim(space, type2))
11988 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11989 "index out of bounds", goto error);
11991 if (type1 == type2 && pos1 == pos2)
11992 return isl_basic_map_universe(space);
11994 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11995 i = isl_basic_map_alloc_equality(bmap);
11996 if (i < 0)
11997 goto error;
11998 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11999 pos1 += isl_basic_map_offset(bmap, type1);
12000 pos2 += isl_basic_map_offset(bmap, type2);
12001 isl_int_set_si(bmap->eq[i][pos1], -1);
12002 isl_int_set_si(bmap->eq[i][pos2], 1);
12003 bmap = isl_basic_map_finalize(bmap);
12004 isl_space_free(space);
12005 return bmap;
12006 error:
12007 isl_space_free(space);
12008 isl_basic_map_free(bmap);
12009 return NULL;
12012 /* Add a constraint imposing that the given two dimensions are equal.
12014 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12015 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12017 isl_basic_map *eq;
12019 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12021 bmap = isl_basic_map_intersect(bmap, eq);
12023 return bmap;
12026 /* Add a constraint imposing that the given two dimensions are equal.
12028 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12029 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12031 isl_basic_map *bmap;
12033 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12035 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12037 return map;
12040 /* Add a constraint imposing that the given two dimensions have opposite values.
12042 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12043 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12045 isl_basic_map *bmap = NULL;
12046 int i;
12048 if (!map)
12049 return NULL;
12051 if (pos1 >= isl_map_dim(map, type1))
12052 isl_die(map->ctx, isl_error_invalid,
12053 "index out of bounds", goto error);
12054 if (pos2 >= isl_map_dim(map, type2))
12055 isl_die(map->ctx, isl_error_invalid,
12056 "index out of bounds", goto error);
12058 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12059 i = isl_basic_map_alloc_equality(bmap);
12060 if (i < 0)
12061 goto error;
12062 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12063 pos1 += isl_basic_map_offset(bmap, type1);
12064 pos2 += isl_basic_map_offset(bmap, type2);
12065 isl_int_set_si(bmap->eq[i][pos1], 1);
12066 isl_int_set_si(bmap->eq[i][pos2], 1);
12067 bmap = isl_basic_map_finalize(bmap);
12069 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12071 return map;
12072 error:
12073 isl_basic_map_free(bmap);
12074 isl_map_free(map);
12075 return NULL;
12078 /* Construct a constraint imposing that the value of the first dimension is
12079 * greater than or equal to that of the second.
12081 static __isl_give isl_constraint *constraint_order_ge(
12082 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12083 enum isl_dim_type type2, int pos2)
12085 isl_constraint *c;
12087 if (!space)
12088 return NULL;
12090 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12092 if (pos1 >= isl_constraint_dim(c, type1))
12093 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12094 "index out of bounds", return isl_constraint_free(c));
12095 if (pos2 >= isl_constraint_dim(c, type2))
12096 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12097 "index out of bounds", return isl_constraint_free(c));
12099 if (type1 == type2 && pos1 == pos2)
12100 return c;
12102 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12103 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12105 return c;
12108 /* Add a constraint imposing that the value of the first dimension is
12109 * greater than or equal to that of the second.
12111 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12112 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12114 isl_constraint *c;
12115 isl_space *space;
12117 if (type1 == type2 && pos1 == pos2)
12118 return bmap;
12119 space = isl_basic_map_get_space(bmap);
12120 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12121 bmap = isl_basic_map_add_constraint(bmap, c);
12123 return bmap;
12126 /* Add a constraint imposing that the value of the first dimension is
12127 * greater than or equal to that of the second.
12129 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12130 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12132 isl_constraint *c;
12133 isl_space *space;
12135 if (type1 == type2 && pos1 == pos2)
12136 return map;
12137 space = isl_map_get_space(map);
12138 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12139 map = isl_map_add_constraint(map, c);
12141 return map;
12144 /* Add a constraint imposing that the value of the first dimension is
12145 * less than or equal to that of the second.
12147 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12148 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12150 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12153 /* Construct a basic map where the value of the first dimension is
12154 * greater than that of the second.
12156 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12157 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12159 isl_basic_map *bmap = NULL;
12160 int i;
12162 if (!space)
12163 return NULL;
12165 if (pos1 >= isl_space_dim(space, type1))
12166 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12167 "index out of bounds", goto error);
12168 if (pos2 >= isl_space_dim(space, type2))
12169 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12170 "index out of bounds", goto error);
12172 if (type1 == type2 && pos1 == pos2)
12173 return isl_basic_map_empty(space);
12175 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12176 i = isl_basic_map_alloc_inequality(bmap);
12177 if (i < 0)
12178 return isl_basic_map_free(bmap);
12179 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12180 pos1 += isl_basic_map_offset(bmap, type1);
12181 pos2 += isl_basic_map_offset(bmap, type2);
12182 isl_int_set_si(bmap->ineq[i][pos1], 1);
12183 isl_int_set_si(bmap->ineq[i][pos2], -1);
12184 isl_int_set_si(bmap->ineq[i][0], -1);
12185 bmap = isl_basic_map_finalize(bmap);
12187 return bmap;
12188 error:
12189 isl_space_free(space);
12190 isl_basic_map_free(bmap);
12191 return NULL;
12194 /* Add a constraint imposing that the value of the first dimension is
12195 * greater than that of the second.
12197 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12198 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12200 isl_basic_map *gt;
12202 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12204 bmap = isl_basic_map_intersect(bmap, gt);
12206 return bmap;
12209 /* Add a constraint imposing that the value of the first dimension is
12210 * greater than that of the second.
12212 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12213 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12215 isl_basic_map *bmap;
12217 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12219 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12221 return map;
12224 /* Add a constraint imposing that the value of the first dimension is
12225 * smaller than that of the second.
12227 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12228 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12230 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12233 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12234 int pos)
12236 isl_aff *div;
12237 isl_local_space *ls;
12239 if (!bmap)
12240 return NULL;
12242 if (!isl_basic_map_divs_known(bmap))
12243 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12244 "some divs are unknown", return NULL);
12246 ls = isl_basic_map_get_local_space(bmap);
12247 div = isl_local_space_get_div(ls, pos);
12248 isl_local_space_free(ls);
12250 return div;
12253 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12254 int pos)
12256 return isl_basic_map_get_div(bset, pos);
12259 /* Plug in "subs" for dimension "type", "pos" of "bset".
12261 * Let i be the dimension to replace and let "subs" be of the form
12263 * f/d
12265 * Any integer division with a non-zero coefficient for i,
12267 * floor((a i + g)/m)
12269 * is replaced by
12271 * floor((a f + d g)/(m d))
12273 * Constraints of the form
12275 * a i + g
12277 * are replaced by
12279 * a f + d g
12281 * We currently require that "subs" is an integral expression.
12282 * Handling rational expressions may require us to add stride constraints
12283 * as we do in isl_basic_set_preimage_multi_aff.
12285 __isl_give isl_basic_set *isl_basic_set_substitute(
12286 __isl_take isl_basic_set *bset,
12287 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12289 int i;
12290 isl_int v;
12291 isl_ctx *ctx;
12293 if (bset && isl_basic_set_plain_is_empty(bset))
12294 return bset;
12296 bset = isl_basic_set_cow(bset);
12297 if (!bset || !subs)
12298 goto error;
12300 ctx = isl_basic_set_get_ctx(bset);
12301 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12302 isl_die(ctx, isl_error_invalid,
12303 "spaces don't match", goto error);
12304 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12305 isl_die(ctx, isl_error_unsupported,
12306 "cannot handle divs yet", goto error);
12307 if (!isl_int_is_one(subs->v->el[0]))
12308 isl_die(ctx, isl_error_invalid,
12309 "can only substitute integer expressions", goto error);
12311 pos += isl_basic_set_offset(bset, type);
12313 isl_int_init(v);
12315 for (i = 0; i < bset->n_eq; ++i) {
12316 if (isl_int_is_zero(bset->eq[i][pos]))
12317 continue;
12318 isl_int_set(v, bset->eq[i][pos]);
12319 isl_int_set_si(bset->eq[i][pos], 0);
12320 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12321 v, subs->v->el + 1, subs->v->size - 1);
12324 for (i = 0; i < bset->n_ineq; ++i) {
12325 if (isl_int_is_zero(bset->ineq[i][pos]))
12326 continue;
12327 isl_int_set(v, bset->ineq[i][pos]);
12328 isl_int_set_si(bset->ineq[i][pos], 0);
12329 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12330 v, subs->v->el + 1, subs->v->size - 1);
12333 for (i = 0; i < bset->n_div; ++i) {
12334 if (isl_int_is_zero(bset->div[i][1 + pos]))
12335 continue;
12336 isl_int_set(v, bset->div[i][1 + pos]);
12337 isl_int_set_si(bset->div[i][1 + pos], 0);
12338 isl_seq_combine(bset->div[i] + 1,
12339 subs->v->el[0], bset->div[i] + 1,
12340 v, subs->v->el + 1, subs->v->size - 1);
12341 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12344 isl_int_clear(v);
12346 bset = isl_basic_set_simplify(bset);
12347 return isl_basic_set_finalize(bset);
12348 error:
12349 isl_basic_set_free(bset);
12350 return NULL;
12353 /* Plug in "subs" for dimension "type", "pos" of "set".
12355 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12356 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12358 int i;
12360 if (set && isl_set_plain_is_empty(set))
12361 return set;
12363 set = isl_set_cow(set);
12364 if (!set || !subs)
12365 goto error;
12367 for (i = set->n - 1; i >= 0; --i) {
12368 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12369 if (remove_if_empty(set, i) < 0)
12370 goto error;
12373 return set;
12374 error:
12375 isl_set_free(set);
12376 return NULL;
12379 /* Check if the range of "ma" is compatible with the domain or range
12380 * (depending on "type") of "bmap".
12381 * Return -1 if anything is wrong.
12383 static int check_basic_map_compatible_range_multi_aff(
12384 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12385 __isl_keep isl_multi_aff *ma)
12387 int m;
12388 isl_space *ma_space;
12390 ma_space = isl_multi_aff_get_space(ma);
12392 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12393 if (m < 0)
12394 goto error;
12395 if (!m)
12396 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12397 "parameters don't match", goto error);
12398 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12399 if (m < 0)
12400 goto error;
12401 if (!m)
12402 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12403 "spaces don't match", goto error);
12405 isl_space_free(ma_space);
12406 return m;
12407 error:
12408 isl_space_free(ma_space);
12409 return -1;
12412 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12413 * coefficients before the transformed range of dimensions,
12414 * the "n_after" coefficients after the transformed range of dimensions
12415 * and the coefficients of the other divs in "bmap".
12417 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12418 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12420 int i;
12421 int n_param;
12422 int n_set;
12423 isl_local_space *ls;
12425 if (n_div == 0)
12426 return 0;
12428 ls = isl_aff_get_domain_local_space(ma->p[0]);
12429 if (!ls)
12430 return -1;
12432 n_param = isl_local_space_dim(ls, isl_dim_param);
12433 n_set = isl_local_space_dim(ls, isl_dim_set);
12434 for (i = 0; i < n_div; ++i) {
12435 int o_bmap = 0, o_ls = 0;
12437 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12438 o_bmap += 1 + 1 + n_param;
12439 o_ls += 1 + 1 + n_param;
12440 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12441 o_bmap += n_before;
12442 isl_seq_cpy(bmap->div[i] + o_bmap,
12443 ls->div->row[i] + o_ls, n_set);
12444 o_bmap += n_set;
12445 o_ls += n_set;
12446 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12447 o_bmap += n_after;
12448 isl_seq_cpy(bmap->div[i] + o_bmap,
12449 ls->div->row[i] + o_ls, n_div);
12450 o_bmap += n_div;
12451 o_ls += n_div;
12452 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12453 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12454 goto error;
12457 isl_local_space_free(ls);
12458 return 0;
12459 error:
12460 isl_local_space_free(ls);
12461 return -1;
12464 /* How many stride constraints does "ma" enforce?
12465 * That is, how many of the affine expressions have a denominator
12466 * different from one?
12468 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12470 int i;
12471 int strides = 0;
12473 for (i = 0; i < ma->n; ++i)
12474 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12475 strides++;
12477 return strides;
12480 /* For each affine expression in ma of the form
12482 * x_i = (f_i y + h_i)/m_i
12484 * with m_i different from one, add a constraint to "bmap"
12485 * of the form
12487 * f_i y + h_i = m_i alpha_i
12489 * with alpha_i an additional existentially quantified variable.
12491 * The input variables of "ma" correspond to a subset of the variables
12492 * of "bmap". There are "n_before" variables in "bmap" before this
12493 * subset and "n_after" variables after this subset.
12494 * The integer divisions of the affine expressions in "ma" are assumed
12495 * to have been aligned. There are "n_div_ma" of them and
12496 * they appear first in "bmap", straight after the "n_after" variables.
12498 static __isl_give isl_basic_map *add_ma_strides(
12499 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12500 int n_before, int n_after, int n_div_ma)
12502 int i, k;
12503 int div;
12504 int total;
12505 int n_param;
12506 int n_in;
12508 total = isl_basic_map_total_dim(bmap);
12509 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12510 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12511 for (i = 0; i < ma->n; ++i) {
12512 int o_bmap = 0, o_ma = 1;
12514 if (isl_int_is_one(ma->p[i]->v->el[0]))
12515 continue;
12516 div = isl_basic_map_alloc_div(bmap);
12517 k = isl_basic_map_alloc_equality(bmap);
12518 if (div < 0 || k < 0)
12519 goto error;
12520 isl_int_set_si(bmap->div[div][0], 0);
12521 isl_seq_cpy(bmap->eq[k] + o_bmap,
12522 ma->p[i]->v->el + o_ma, 1 + n_param);
12523 o_bmap += 1 + n_param;
12524 o_ma += 1 + n_param;
12525 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12526 o_bmap += n_before;
12527 isl_seq_cpy(bmap->eq[k] + o_bmap,
12528 ma->p[i]->v->el + o_ma, n_in);
12529 o_bmap += n_in;
12530 o_ma += n_in;
12531 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12532 o_bmap += n_after;
12533 isl_seq_cpy(bmap->eq[k] + o_bmap,
12534 ma->p[i]->v->el + o_ma, n_div_ma);
12535 o_bmap += n_div_ma;
12536 o_ma += n_div_ma;
12537 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12538 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12539 total++;
12542 return bmap;
12543 error:
12544 isl_basic_map_free(bmap);
12545 return NULL;
12548 /* Replace the domain or range space (depending on "type) of "space" by "set".
12550 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12551 enum isl_dim_type type, __isl_take isl_space *set)
12553 if (type == isl_dim_in) {
12554 space = isl_space_range(space);
12555 space = isl_space_map_from_domain_and_range(set, space);
12556 } else {
12557 space = isl_space_domain(space);
12558 space = isl_space_map_from_domain_and_range(space, set);
12561 return space;
12564 /* Compute the preimage of the domain or range (depending on "type")
12565 * of "bmap" under the function represented by "ma".
12566 * In other words, plug in "ma" in the domain or range of "bmap".
12567 * The result is a basic map that lives in the same space as "bmap"
12568 * except that the domain or range has been replaced by
12569 * the domain space of "ma".
12571 * If bmap is represented by
12573 * A(p) + S u + B x + T v + C(divs) >= 0,
12575 * where u and x are input and output dimensions if type == isl_dim_out
12576 * while x and v are input and output dimensions if type == isl_dim_in,
12577 * and ma is represented by
12579 * x = D(p) + F(y) + G(divs')
12581 * then the result is
12583 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12585 * The divs in the input set are similarly adjusted.
12586 * In particular
12588 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12590 * becomes
12592 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12593 * B_i G(divs') + c_i(divs))/n_i)
12595 * If bmap is not a rational map and if F(y) involves any denominators
12597 * x_i = (f_i y + h_i)/m_i
12599 * then additional constraints are added to ensure that we only
12600 * map back integer points. That is we enforce
12602 * f_i y + h_i = m_i alpha_i
12604 * with alpha_i an additional existentially quantified variable.
12606 * We first copy over the divs from "ma".
12607 * Then we add the modified constraints and divs from "bmap".
12608 * Finally, we add the stride constraints, if needed.
12610 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12611 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12612 __isl_take isl_multi_aff *ma)
12614 int i, k;
12615 isl_space *space;
12616 isl_basic_map *res = NULL;
12617 int n_before, n_after, n_div_bmap, n_div_ma;
12618 isl_int f, c1, c2, g;
12619 int rational, strides;
12621 isl_int_init(f);
12622 isl_int_init(c1);
12623 isl_int_init(c2);
12624 isl_int_init(g);
12626 ma = isl_multi_aff_align_divs(ma);
12627 if (!bmap || !ma)
12628 goto error;
12629 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12630 goto error;
12632 if (type == isl_dim_in) {
12633 n_before = 0;
12634 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12635 } else {
12636 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12637 n_after = 0;
12639 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12640 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12642 space = isl_multi_aff_get_domain_space(ma);
12643 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12644 rational = isl_basic_map_is_rational(bmap);
12645 strides = rational ? 0 : multi_aff_strides(ma);
12646 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12647 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12648 if (rational)
12649 res = isl_basic_map_set_rational(res);
12651 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12652 if (isl_basic_map_alloc_div(res) < 0)
12653 goto error;
12655 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12656 goto error;
12658 for (i = 0; i < bmap->n_eq; ++i) {
12659 k = isl_basic_map_alloc_equality(res);
12660 if (k < 0)
12661 goto error;
12662 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12663 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12666 for (i = 0; i < bmap->n_ineq; ++i) {
12667 k = isl_basic_map_alloc_inequality(res);
12668 if (k < 0)
12669 goto error;
12670 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12671 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12674 for (i = 0; i < bmap->n_div; ++i) {
12675 if (isl_int_is_zero(bmap->div[i][0])) {
12676 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12677 continue;
12679 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12680 n_before, n_after, n_div_ma, n_div_bmap,
12681 f, c1, c2, g, 1);
12684 if (strides)
12685 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12687 isl_int_clear(f);
12688 isl_int_clear(c1);
12689 isl_int_clear(c2);
12690 isl_int_clear(g);
12691 isl_basic_map_free(bmap);
12692 isl_multi_aff_free(ma);
12693 res = isl_basic_set_simplify(res);
12694 return isl_basic_map_finalize(res);
12695 error:
12696 isl_int_clear(f);
12697 isl_int_clear(c1);
12698 isl_int_clear(c2);
12699 isl_int_clear(g);
12700 isl_basic_map_free(bmap);
12701 isl_multi_aff_free(ma);
12702 isl_basic_map_free(res);
12703 return NULL;
12706 /* Compute the preimage of "bset" under the function represented by "ma".
12707 * In other words, plug in "ma" in "bset". The result is a basic set
12708 * that lives in the domain space of "ma".
12710 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12711 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12713 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12716 /* Compute the preimage of the domain of "bmap" under the function
12717 * represented by "ma".
12718 * In other words, plug in "ma" in the domain of "bmap".
12719 * The result is a basic map that lives in the same space as "bmap"
12720 * except that the domain has been replaced by the domain space of "ma".
12722 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12723 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12725 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12728 /* Compute the preimage of the range of "bmap" under the function
12729 * represented by "ma".
12730 * In other words, plug in "ma" in the range of "bmap".
12731 * The result is a basic map that lives in the same space as "bmap"
12732 * except that the range has been replaced by the domain space of "ma".
12734 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12735 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12737 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12740 /* Check if the range of "ma" is compatible with the domain or range
12741 * (depending on "type") of "map".
12742 * Return -1 if anything is wrong.
12744 static int check_map_compatible_range_multi_aff(
12745 __isl_keep isl_map *map, enum isl_dim_type type,
12746 __isl_keep isl_multi_aff *ma)
12748 int m;
12749 isl_space *ma_space;
12751 ma_space = isl_multi_aff_get_space(ma);
12752 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12753 isl_space_free(ma_space);
12754 if (m >= 0 && !m)
12755 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12756 "spaces don't match", return -1);
12757 return m;
12760 /* Compute the preimage of the domain or range (depending on "type")
12761 * of "map" under the function represented by "ma".
12762 * In other words, plug in "ma" in the domain or range of "map".
12763 * The result is a map that lives in the same space as "map"
12764 * except that the domain or range has been replaced by
12765 * the domain space of "ma".
12767 * The parameters are assumed to have been aligned.
12769 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12770 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12772 int i;
12773 isl_space *space;
12775 map = isl_map_cow(map);
12776 ma = isl_multi_aff_align_divs(ma);
12777 if (!map || !ma)
12778 goto error;
12779 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12780 goto error;
12782 for (i = 0; i < map->n; ++i) {
12783 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12784 isl_multi_aff_copy(ma));
12785 if (!map->p[i])
12786 goto error;
12789 space = isl_multi_aff_get_domain_space(ma);
12790 space = isl_space_set(isl_map_get_space(map), type, space);
12792 isl_space_free(map->dim);
12793 map->dim = space;
12794 if (!map->dim)
12795 goto error;
12797 isl_multi_aff_free(ma);
12798 if (map->n > 1)
12799 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12800 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12801 return map;
12802 error:
12803 isl_multi_aff_free(ma);
12804 isl_map_free(map);
12805 return NULL;
12808 /* Compute the preimage of the domain or range (depending on "type")
12809 * of "map" under the function represented by "ma".
12810 * In other words, plug in "ma" in the domain or range of "map".
12811 * The result is a map that lives in the same space as "map"
12812 * except that the domain or range has been replaced by
12813 * the domain space of "ma".
12815 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12816 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12818 if (!map || !ma)
12819 goto error;
12821 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12822 return map_preimage_multi_aff(map, type, ma);
12824 if (!isl_space_has_named_params(map->dim) ||
12825 !isl_space_has_named_params(ma->space))
12826 isl_die(map->ctx, isl_error_invalid,
12827 "unaligned unnamed parameters", goto error);
12828 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12829 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12831 return map_preimage_multi_aff(map, type, ma);
12832 error:
12833 isl_multi_aff_free(ma);
12834 return isl_map_free(map);
12837 /* Compute the preimage of "set" under the function represented by "ma".
12838 * In other words, plug in "ma" in "set". The result is a set
12839 * that lives in the domain space of "ma".
12841 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12842 __isl_take isl_multi_aff *ma)
12844 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12847 /* Compute the preimage of the domain of "map" under the function
12848 * represented by "ma".
12849 * In other words, plug in "ma" in the domain of "map".
12850 * The result is a map that lives in the same space as "map"
12851 * except that the domain has been replaced by the domain space of "ma".
12853 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12854 __isl_take isl_multi_aff *ma)
12856 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12859 /* Compute the preimage of the range of "map" under the function
12860 * represented by "ma".
12861 * In other words, plug in "ma" in the range of "map".
12862 * The result is a map that lives in the same space as "map"
12863 * except that the range has been replaced by the domain space of "ma".
12865 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12866 __isl_take isl_multi_aff *ma)
12868 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12871 /* Compute the preimage of "map" under the function represented by "pma".
12872 * In other words, plug in "pma" in the domain or range of "map".
12873 * The result is a map that lives in the same space as "map",
12874 * except that the space of type "type" has been replaced by
12875 * the domain space of "pma".
12877 * The parameters of "map" and "pma" are assumed to have been aligned.
12879 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12880 __isl_take isl_map *map, enum isl_dim_type type,
12881 __isl_take isl_pw_multi_aff *pma)
12883 int i;
12884 isl_map *res;
12886 if (!pma)
12887 goto error;
12889 if (pma->n == 0) {
12890 isl_pw_multi_aff_free(pma);
12891 res = isl_map_empty(isl_map_get_space(map));
12892 isl_map_free(map);
12893 return res;
12896 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12897 isl_multi_aff_copy(pma->p[0].maff));
12898 if (type == isl_dim_in)
12899 res = isl_map_intersect_domain(res,
12900 isl_map_copy(pma->p[0].set));
12901 else
12902 res = isl_map_intersect_range(res,
12903 isl_map_copy(pma->p[0].set));
12905 for (i = 1; i < pma->n; ++i) {
12906 isl_map *res_i;
12908 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12909 isl_multi_aff_copy(pma->p[i].maff));
12910 if (type == isl_dim_in)
12911 res_i = isl_map_intersect_domain(res_i,
12912 isl_map_copy(pma->p[i].set));
12913 else
12914 res_i = isl_map_intersect_range(res_i,
12915 isl_map_copy(pma->p[i].set));
12916 res = isl_map_union(res, res_i);
12919 isl_pw_multi_aff_free(pma);
12920 isl_map_free(map);
12921 return res;
12922 error:
12923 isl_pw_multi_aff_free(pma);
12924 isl_map_free(map);
12925 return NULL;
12928 /* Compute the preimage of "map" under the function represented by "pma".
12929 * In other words, plug in "pma" in the domain or range of "map".
12930 * The result is a map that lives in the same space as "map",
12931 * except that the space of type "type" has been replaced by
12932 * the domain space of "pma".
12934 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12935 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12937 if (!map || !pma)
12938 goto error;
12940 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12941 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12943 if (!isl_space_has_named_params(map->dim) ||
12944 !isl_space_has_named_params(pma->dim))
12945 isl_die(map->ctx, isl_error_invalid,
12946 "unaligned unnamed parameters", goto error);
12947 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12948 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12950 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12951 error:
12952 isl_pw_multi_aff_free(pma);
12953 return isl_map_free(map);
12956 /* Compute the preimage of "set" under the function represented by "pma".
12957 * In other words, plug in "pma" in "set". The result is a set
12958 * that lives in the domain space of "pma".
12960 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12961 __isl_take isl_pw_multi_aff *pma)
12963 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12966 /* Compute the preimage of the domain of "map" under the function
12967 * represented by "pma".
12968 * In other words, plug in "pma" in the domain of "map".
12969 * The result is a map that lives in the same space as "map",
12970 * except that domain space has been replaced by the domain space of "pma".
12972 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12973 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12975 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12978 /* Compute the preimage of the range of "map" under the function
12979 * represented by "pma".
12980 * In other words, plug in "pma" in the range of "map".
12981 * The result is a map that lives in the same space as "map",
12982 * except that range space has been replaced by the domain space of "pma".
12984 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12985 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12987 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12990 /* Compute the preimage of "map" under the function represented by "mpa".
12991 * In other words, plug in "mpa" in the domain or range of "map".
12992 * The result is a map that lives in the same space as "map",
12993 * except that the space of type "type" has been replaced by
12994 * the domain space of "mpa".
12996 * If the map does not involve any constraints that refer to the
12997 * dimensions of the substituted space, then the only possible
12998 * effect of "mpa" on the map is to map the space to a different space.
12999 * We create a separate isl_multi_aff to effectuate this change
13000 * in order to avoid spurious splitting of the map along the pieces
13001 * of "mpa".
13003 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13004 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13006 int n;
13007 isl_pw_multi_aff *pma;
13009 if (!map || !mpa)
13010 goto error;
13012 n = isl_map_dim(map, type);
13013 if (!isl_map_involves_dims(map, type, 0, n)) {
13014 isl_space *space;
13015 isl_multi_aff *ma;
13017 space = isl_multi_pw_aff_get_space(mpa);
13018 isl_multi_pw_aff_free(mpa);
13019 ma = isl_multi_aff_zero(space);
13020 return isl_map_preimage_multi_aff(map, type, ma);
13023 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13024 return isl_map_preimage_pw_multi_aff(map, type, pma);
13025 error:
13026 isl_map_free(map);
13027 isl_multi_pw_aff_free(mpa);
13028 return NULL;
13031 /* Compute the preimage of "map" under the function represented by "mpa".
13032 * In other words, plug in "mpa" in the domain "map".
13033 * The result is a map that lives in the same space as "map",
13034 * except that domain space has been replaced by the domain space of "mpa".
13036 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13037 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13039 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13042 /* Compute the preimage of "set" by the function represented by "mpa".
13043 * In other words, plug in "mpa" in "set".
13045 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13046 __isl_take isl_multi_pw_aff *mpa)
13048 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13051 /* Is the point "inner" internal to inequality constraint "ineq"
13052 * of "bset"?
13053 * The point is considered to be internal to the inequality constraint,
13054 * if it strictly lies on the positive side of the inequality constraint,
13055 * or if it lies on the constraint and the constraint is lexico-positive.
13057 static isl_bool is_internal(__isl_keep isl_vec *inner,
13058 __isl_keep isl_basic_set *bset, int ineq)
13060 isl_ctx *ctx;
13061 int pos;
13062 unsigned total;
13064 if (!inner || !bset)
13065 return isl_bool_error;
13067 ctx = isl_basic_set_get_ctx(bset);
13068 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13069 &ctx->normalize_gcd);
13070 if (!isl_int_is_zero(ctx->normalize_gcd))
13071 return isl_int_is_nonneg(ctx->normalize_gcd);
13073 total = isl_basic_set_dim(bset, isl_dim_all);
13074 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13075 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13078 /* Tighten the inequality constraints of "bset" that are outward with respect
13079 * to the point "vec".
13080 * That is, tighten the constraints that are not satisfied by "vec".
13082 * "vec" is a point internal to some superset S of "bset" that is used
13083 * to make the subsets of S disjoint, by tightening one half of the constraints
13084 * that separate two subsets. In particular, the constraints of S
13085 * are all satisfied by "vec" and should not be tightened.
13086 * Of the internal constraints, those that have "vec" on the outside
13087 * are tightened. The shared facet is included in the adjacent subset
13088 * with the opposite constraint.
13089 * For constraints that saturate "vec", this criterion cannot be used
13090 * to determine which of the two sides should be tightened.
13091 * Instead, the sign of the first non-zero coefficient is used
13092 * to make this choice. Note that this second criterion is never used
13093 * on the constraints of S since "vec" is interior to "S".
13095 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13096 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13098 int j;
13100 bset = isl_basic_set_cow(bset);
13101 if (!bset)
13102 return NULL;
13103 for (j = 0; j < bset->n_ineq; ++j) {
13104 isl_bool internal;
13106 internal = is_internal(vec, bset, j);
13107 if (internal < 0)
13108 return isl_basic_set_free(bset);
13109 if (internal)
13110 continue;
13111 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13114 return bset;