isl_ast_codegen.c: refine_eliminated: do not add stride guard
[isl.git] / isl_map.c
blob91a1605e1f38a30896e9727f0c519c9b38c78a99
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
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include "isl_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
25 #include <isl_seq.h>
26 #include <isl/set.h>
27 #include <isl/map.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
30 #include "isl_tab.h"
31 #include <isl/vec.h>
32 #include <isl_mat_private.h>
33 #include <isl_vec_private.h>
34 #include <isl_dim_map.h>
35 #include <isl_local_space_private.h>
36 #include <isl_aff_private.h>
37 #include <isl_options_private.h>
38 #include <isl_morph.h>
39 #include <isl_val_private.h>
40 #include <isl/deprecated/map_int.h>
41 #include <isl/deprecated/set_int.h>
43 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
45 switch (type) {
46 case isl_dim_param: return dim->nparam;
47 case isl_dim_in: return dim->n_in;
48 case isl_dim_out: return dim->n_out;
49 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
50 default: return 0;
54 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return 1;
58 case isl_dim_in: return 1 + dim->nparam;
59 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
60 default: return 0;
64 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
65 enum isl_dim_type type)
67 if (!bmap)
68 return 0;
69 switch (type) {
70 case isl_dim_cst: return 1;
71 case isl_dim_param:
72 case isl_dim_in:
73 case isl_dim_out: return isl_space_dim(bmap->dim, type);
74 case isl_dim_div: return bmap->n_div;
75 case isl_dim_all: return isl_basic_map_total_dim(bmap);
76 default: return 0;
80 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
82 return map ? n(map->dim, type) : 0;
85 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
87 return set ? n(set->dim, type) : 0;
90 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
91 enum isl_dim_type type)
93 isl_space *dim = bmap->dim;
94 switch (type) {
95 case isl_dim_cst: return 0;
96 case isl_dim_param: return 1;
97 case isl_dim_in: return 1 + dim->nparam;
98 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
99 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
100 default: return 0;
104 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
105 enum isl_dim_type type)
107 return isl_basic_map_offset(bset, type);
110 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
112 return pos(map->dim, type);
115 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
116 enum isl_dim_type type)
118 return isl_basic_map_dim(bset, type);
121 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
123 return isl_basic_set_dim(bset, isl_dim_set);
126 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
128 return isl_basic_set_dim(bset, isl_dim_param);
131 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
133 if (!bset)
134 return 0;
135 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
138 unsigned isl_set_n_dim(__isl_keep isl_set *set)
140 return isl_set_dim(set, isl_dim_set);
143 unsigned isl_set_n_param(__isl_keep isl_set *set)
145 return isl_set_dim(set, isl_dim_param);
148 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->n_in : 0;
153 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
155 return bmap ? bmap->dim->n_out : 0;
158 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
160 return bmap ? bmap->dim->nparam : 0;
163 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
165 return bmap ? bmap->n_div : 0;
168 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
170 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
173 unsigned isl_map_n_in(const struct isl_map *map)
175 return map ? map->dim->n_in : 0;
178 unsigned isl_map_n_out(const struct isl_map *map)
180 return map ? map->dim->n_out : 0;
183 unsigned isl_map_n_param(const struct isl_map *map)
185 return map ? map->dim->nparam : 0;
188 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
190 int m;
191 if (!map || !set)
192 return -1;
193 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
194 if (m < 0 || !m)
195 return m;
196 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
199 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
200 struct isl_basic_set *bset)
202 int m;
203 if (!bmap || !bset)
204 return -1;
205 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
211 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
213 int m;
214 if (!map || !set)
215 return -1;
216 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
217 if (m < 0 || !m)
218 return m;
219 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
222 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
223 struct isl_basic_set *bset)
225 int m;
226 if (!bmap || !bset)
227 return -1;
228 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
229 if (m < 0 || !m)
230 return m;
231 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
234 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
236 return bmap ? bmap->ctx : NULL;
239 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
241 return bset ? bset->ctx : NULL;
244 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
246 return map ? map->ctx : NULL;
249 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
251 return set ? set->ctx : NULL;
254 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
256 if (!bmap)
257 return NULL;
258 return isl_space_copy(bmap->dim);
261 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
263 if (!bset)
264 return NULL;
265 return isl_space_copy(bset->dim);
268 /* Extract the divs in "bmap" as a matrix.
270 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
272 int i;
273 isl_ctx *ctx;
274 isl_mat *div;
275 unsigned total;
276 unsigned cols;
278 if (!bmap)
279 return NULL;
281 ctx = isl_basic_map_get_ctx(bmap);
282 total = isl_space_dim(bmap->dim, isl_dim_all);
283 cols = 1 + 1 + total + bmap->n_div;
284 div = isl_mat_alloc(ctx, bmap->n_div, cols);
285 if (!div)
286 return NULL;
288 for (i = 0; i < bmap->n_div; ++i)
289 isl_seq_cpy(div->row[i], bmap->div[i], cols);
291 return div;
294 /* Extract the divs in "bset" as a matrix.
296 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
298 return isl_basic_map_get_divs(bset);
301 __isl_give isl_local_space *isl_basic_map_get_local_space(
302 __isl_keep isl_basic_map *bmap)
304 isl_mat *div;
306 if (!bmap)
307 return NULL;
309 div = isl_basic_map_get_divs(bmap);
310 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
313 __isl_give isl_local_space *isl_basic_set_get_local_space(
314 __isl_keep isl_basic_set *bset)
316 return isl_basic_map_get_local_space(bset);
319 __isl_give isl_basic_map *isl_basic_map_from_local_space(
320 __isl_take isl_local_space *ls)
322 int i;
323 int n_div;
324 isl_basic_map *bmap;
326 if (!ls)
327 return NULL;
329 n_div = isl_local_space_dim(ls, isl_dim_div);
330 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
331 n_div, 0, 2 * n_div);
333 for (i = 0; i < n_div; ++i)
334 if (isl_basic_map_alloc_div(bmap) < 0)
335 goto error;
337 for (i = 0; i < n_div; ++i) {
338 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
339 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
340 goto error;
343 isl_local_space_free(ls);
344 return bmap;
345 error:
346 isl_local_space_free(ls);
347 isl_basic_map_free(bmap);
348 return NULL;
351 __isl_give isl_basic_set *isl_basic_set_from_local_space(
352 __isl_take isl_local_space *ls)
354 return isl_basic_map_from_local_space(ls);
357 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
359 if (!map)
360 return NULL;
361 return isl_space_copy(map->dim);
364 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
366 if (!set)
367 return NULL;
368 return isl_space_copy(set->dim);
371 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
372 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
374 bmap = isl_basic_map_cow(bmap);
375 if (!bmap)
376 return NULL;
377 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
378 if (!bmap->dim)
379 goto error;
380 bmap = isl_basic_map_finalize(bmap);
381 return bmap;
382 error:
383 isl_basic_map_free(bmap);
384 return NULL;
387 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
388 __isl_take isl_basic_set *bset, const char *s)
390 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
393 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
394 enum isl_dim_type type)
396 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
399 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
400 enum isl_dim_type type, const char *s)
402 int i;
404 map = isl_map_cow(map);
405 if (!map)
406 return NULL;
408 map->dim = isl_space_set_tuple_name(map->dim, type, s);
409 if (!map->dim)
410 goto error;
412 for (i = 0; i < map->n; ++i) {
413 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
414 if (!map->p[i])
415 goto error;
418 return map;
419 error:
420 isl_map_free(map);
421 return NULL;
424 /* Replace the identifier of the tuple of type "type" by "id".
426 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
427 __isl_take isl_basic_map *bmap,
428 enum isl_dim_type type, __isl_take isl_id *id)
430 bmap = isl_basic_map_cow(bmap);
431 if (!bmap)
432 goto error;
433 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
434 if (!bmap->dim)
435 return isl_basic_map_free(bmap);
436 bmap = isl_basic_map_finalize(bmap);
437 return bmap;
438 error:
439 isl_id_free(id);
440 return NULL;
443 /* Replace the identifier of the tuple by "id".
445 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
446 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
448 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
451 /* Does the input or output tuple have a name?
453 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
455 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
458 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
459 enum isl_dim_type type)
461 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
464 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
465 const char *s)
467 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
470 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
471 enum isl_dim_type type, __isl_take isl_id *id)
473 map = isl_map_cow(map);
474 if (!map)
475 goto error;
477 map->dim = isl_space_set_tuple_id(map->dim, type, id);
479 return isl_map_reset_space(map, isl_space_copy(map->dim));
480 error:
481 isl_id_free(id);
482 return NULL;
485 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
486 __isl_take isl_id *id)
488 return isl_map_set_tuple_id(set, isl_dim_set, id);
491 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
492 enum isl_dim_type type)
494 map = isl_map_cow(map);
495 if (!map)
496 return NULL;
498 map->dim = isl_space_reset_tuple_id(map->dim, type);
500 return isl_map_reset_space(map, isl_space_copy(map->dim));
503 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
505 return isl_map_reset_tuple_id(set, isl_dim_set);
508 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
510 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
513 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
514 enum isl_dim_type type)
516 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
519 int isl_set_has_tuple_id(__isl_keep isl_set *set)
521 return isl_map_has_tuple_id(set, isl_dim_set);
524 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
526 return isl_map_get_tuple_id(set, isl_dim_set);
529 /* Does the set tuple have a name?
531 int isl_set_has_tuple_name(__isl_keep isl_set *set)
533 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
537 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
539 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
542 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
544 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
547 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
548 enum isl_dim_type type, unsigned pos)
550 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
553 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
554 enum isl_dim_type type, unsigned pos)
556 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
559 /* Does the given dimension have a name?
561 int isl_map_has_dim_name(__isl_keep isl_map *map,
562 enum isl_dim_type type, unsigned pos)
564 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
567 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
568 enum isl_dim_type type, unsigned pos)
570 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
573 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
574 enum isl_dim_type type, unsigned pos)
576 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
579 /* Does the given dimension have a name?
581 int isl_set_has_dim_name(__isl_keep isl_set *set,
582 enum isl_dim_type type, unsigned pos)
584 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
587 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
588 __isl_take isl_basic_map *bmap,
589 enum isl_dim_type type, unsigned pos, const char *s)
591 bmap = isl_basic_map_cow(bmap);
592 if (!bmap)
593 return NULL;
594 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
595 if (!bmap->dim)
596 goto error;
597 return isl_basic_map_finalize(bmap);
598 error:
599 isl_basic_map_free(bmap);
600 return NULL;
603 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
604 enum isl_dim_type type, unsigned pos, const char *s)
606 int i;
608 map = isl_map_cow(map);
609 if (!map)
610 return NULL;
612 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
613 if (!map->dim)
614 goto error;
616 for (i = 0; i < map->n; ++i) {
617 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
618 if (!map->p[i])
619 goto error;
622 return map;
623 error:
624 isl_map_free(map);
625 return NULL;
628 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
629 __isl_take isl_basic_set *bset,
630 enum isl_dim_type type, unsigned pos, const char *s)
632 return (isl_basic_set *)isl_basic_map_set_dim_name(
633 (isl_basic_map *)bset, type, pos, s);
636 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
637 enum isl_dim_type type, unsigned pos, const char *s)
639 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
642 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
643 enum isl_dim_type type, unsigned pos)
645 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
648 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
649 enum isl_dim_type type, unsigned pos)
651 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
654 int isl_map_has_dim_id(__isl_keep isl_map *map,
655 enum isl_dim_type type, unsigned pos)
657 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
660 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
661 enum isl_dim_type type, unsigned pos)
663 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
666 int isl_set_has_dim_id(__isl_keep isl_set *set,
667 enum isl_dim_type type, unsigned pos)
669 return isl_map_has_dim_id(set, type, pos);
672 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
673 enum isl_dim_type type, unsigned pos)
675 return isl_map_get_dim_id(set, type, pos);
678 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
679 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
681 map = isl_map_cow(map);
682 if (!map)
683 goto error;
685 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
687 return isl_map_reset_space(map, isl_space_copy(map->dim));
688 error:
689 isl_id_free(id);
690 return NULL;
693 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
694 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
696 return isl_map_set_dim_id(set, type, pos, id);
699 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
700 __isl_keep isl_id *id)
702 if (!map)
703 return -1;
704 return isl_space_find_dim_by_id(map->dim, type, id);
707 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
708 __isl_keep isl_id *id)
710 return isl_map_find_dim_by_id(set, type, id);
713 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
714 const char *name)
716 if (!map)
717 return -1;
718 return isl_space_find_dim_by_name(map->dim, type, name);
721 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
722 const char *name)
724 return isl_map_find_dim_by_name(set, type, name);
727 /* Reset the user pointer on all identifiers of parameters and tuples
728 * of the space of "map".
730 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
732 isl_space *space;
734 space = isl_map_get_space(map);
735 space = isl_space_reset_user(space);
736 map = isl_map_reset_space(map, space);
738 return map;
741 /* Reset the user pointer on all identifiers of parameters and tuples
742 * of the space of "set".
744 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
746 return isl_map_reset_user(set);
749 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
751 if (!bmap)
752 return -1;
753 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
756 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
758 return isl_basic_map_is_rational(bset);
761 /* Does "bmap" contain any rational points?
763 * If "bmap" has an equality for each dimension, equating the dimension
764 * to an integer constant, then it has no rational points, even if it
765 * is marked as rational.
767 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
769 int has_rational = 1;
770 unsigned total;
772 if (!bmap)
773 return -1;
774 if (isl_basic_map_plain_is_empty(bmap))
775 return 0;
776 if (!isl_basic_map_is_rational(bmap))
777 return 0;
778 bmap = isl_basic_map_copy(bmap);
779 bmap = isl_basic_map_implicit_equalities(bmap);
780 if (!bmap)
781 return -1;
782 total = isl_basic_map_total_dim(bmap);
783 if (bmap->n_eq == total) {
784 int i, j;
785 for (i = 0; i < bmap->n_eq; ++i) {
786 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
787 if (j < 0)
788 break;
789 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
790 !isl_int_is_negone(bmap->eq[i][1 + j]))
791 break;
792 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
793 total - j - 1);
794 if (j >= 0)
795 break;
797 if (i == bmap->n_eq)
798 has_rational = 0;
800 isl_basic_map_free(bmap);
802 return has_rational;
805 /* Does "map" contain any rational points?
807 int isl_map_has_rational(__isl_keep isl_map *map)
809 int i;
810 int has_rational;
812 if (!map)
813 return -1;
814 for (i = 0; i < map->n; ++i) {
815 has_rational = isl_basic_map_has_rational(map->p[i]);
816 if (has_rational < 0)
817 return -1;
818 if (has_rational)
819 return 1;
821 return 0;
824 /* Does "set" contain any rational points?
826 int isl_set_has_rational(__isl_keep isl_set *set)
828 return isl_map_has_rational(set);
831 /* Is this basic set a parameter domain?
833 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
835 if (!bset)
836 return -1;
837 return isl_space_is_params(bset->dim);
840 /* Is this set a parameter domain?
842 int isl_set_is_params(__isl_keep isl_set *set)
844 if (!set)
845 return -1;
846 return isl_space_is_params(set->dim);
849 /* Is this map actually a parameter domain?
850 * Users should never call this function. Outside of isl,
851 * a map can never be a parameter domain.
853 int isl_map_is_params(__isl_keep isl_map *map)
855 if (!map)
856 return -1;
857 return isl_space_is_params(map->dim);
860 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
861 struct isl_basic_map *bmap, unsigned extra,
862 unsigned n_eq, unsigned n_ineq)
864 int i;
865 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
867 bmap->ctx = ctx;
868 isl_ctx_ref(ctx);
870 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
871 if (isl_blk_is_error(bmap->block))
872 goto error;
874 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
875 if ((n_ineq + n_eq) && !bmap->ineq)
876 goto error;
878 if (extra == 0) {
879 bmap->block2 = isl_blk_empty();
880 bmap->div = NULL;
881 } else {
882 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
883 if (isl_blk_is_error(bmap->block2))
884 goto error;
886 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
887 if (!bmap->div)
888 goto error;
891 for (i = 0; i < n_ineq + n_eq; ++i)
892 bmap->ineq[i] = bmap->block.data + i * row_size;
894 for (i = 0; i < extra; ++i)
895 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
897 bmap->ref = 1;
898 bmap->flags = 0;
899 bmap->c_size = n_eq + n_ineq;
900 bmap->eq = bmap->ineq + n_ineq;
901 bmap->extra = extra;
902 bmap->n_eq = 0;
903 bmap->n_ineq = 0;
904 bmap->n_div = 0;
905 bmap->sample = NULL;
907 return bmap;
908 error:
909 isl_basic_map_free(bmap);
910 return NULL;
913 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
914 unsigned nparam, unsigned dim, unsigned extra,
915 unsigned n_eq, unsigned n_ineq)
917 struct isl_basic_map *bmap;
918 isl_space *space;
920 space = isl_space_set_alloc(ctx, nparam, dim);
921 if (!space)
922 return NULL;
924 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
925 return (struct isl_basic_set *)bmap;
928 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
929 unsigned extra, unsigned n_eq, unsigned n_ineq)
931 struct isl_basic_map *bmap;
932 if (!dim)
933 return NULL;
934 isl_assert(dim->ctx, dim->n_in == 0, goto error);
935 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
936 return (struct isl_basic_set *)bmap;
937 error:
938 isl_space_free(dim);
939 return NULL;
942 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
943 unsigned extra, unsigned n_eq, unsigned n_ineq)
945 struct isl_basic_map *bmap;
947 if (!dim)
948 return NULL;
949 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
950 if (!bmap)
951 goto error;
952 bmap->dim = dim;
954 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
955 error:
956 isl_space_free(dim);
957 return NULL;
960 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
961 unsigned nparam, unsigned in, unsigned out, unsigned extra,
962 unsigned n_eq, unsigned n_ineq)
964 struct isl_basic_map *bmap;
965 isl_space *dim;
967 dim = isl_space_alloc(ctx, nparam, in, out);
968 if (!dim)
969 return NULL;
971 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
972 return bmap;
975 static void dup_constraints(
976 struct isl_basic_map *dst, struct isl_basic_map *src)
978 int i;
979 unsigned total = isl_basic_map_total_dim(src);
981 for (i = 0; i < src->n_eq; ++i) {
982 int j = isl_basic_map_alloc_equality(dst);
983 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
986 for (i = 0; i < src->n_ineq; ++i) {
987 int j = isl_basic_map_alloc_inequality(dst);
988 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
991 for (i = 0; i < src->n_div; ++i) {
992 int j = isl_basic_map_alloc_div(dst);
993 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
995 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
998 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1000 struct isl_basic_map *dup;
1002 if (!bmap)
1003 return NULL;
1004 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1005 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1006 if (!dup)
1007 return NULL;
1008 dup_constraints(dup, bmap);
1009 dup->flags = bmap->flags;
1010 dup->sample = isl_vec_copy(bmap->sample);
1011 return dup;
1014 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1016 struct isl_basic_map *dup;
1018 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1019 return (struct isl_basic_set *)dup;
1022 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1024 if (!bset)
1025 return NULL;
1027 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1028 bset->ref++;
1029 return bset;
1031 return isl_basic_set_dup(bset);
1034 struct isl_set *isl_set_copy(struct isl_set *set)
1036 if (!set)
1037 return NULL;
1039 set->ref++;
1040 return set;
1043 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1045 if (!bmap)
1046 return NULL;
1048 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1049 bmap->ref++;
1050 return bmap;
1052 bmap = isl_basic_map_dup(bmap);
1053 if (bmap)
1054 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1055 return bmap;
1058 struct isl_map *isl_map_copy(struct isl_map *map)
1060 if (!map)
1061 return NULL;
1063 map->ref++;
1064 return map;
1067 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1069 if (!bmap)
1070 return NULL;
1072 if (--bmap->ref > 0)
1073 return NULL;
1075 isl_ctx_deref(bmap->ctx);
1076 free(bmap->div);
1077 isl_blk_free(bmap->ctx, bmap->block2);
1078 free(bmap->ineq);
1079 isl_blk_free(bmap->ctx, bmap->block);
1080 isl_vec_free(bmap->sample);
1081 isl_space_free(bmap->dim);
1082 free(bmap);
1084 return NULL;
1087 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1089 return isl_basic_map_free((struct isl_basic_map *)bset);
1092 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1094 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1097 __isl_give isl_map *isl_map_align_params_map_map_and(
1098 __isl_take isl_map *map1, __isl_take isl_map *map2,
1099 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1100 __isl_take isl_map *map2))
1102 if (!map1 || !map2)
1103 goto error;
1104 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1105 return fn(map1, map2);
1106 if (!isl_space_has_named_params(map1->dim) ||
1107 !isl_space_has_named_params(map2->dim))
1108 isl_die(map1->ctx, isl_error_invalid,
1109 "unaligned unnamed parameters", goto error);
1110 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1111 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1112 return fn(map1, map2);
1113 error:
1114 isl_map_free(map1);
1115 isl_map_free(map2);
1116 return NULL;
1119 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1120 __isl_keep isl_map *map2,
1121 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1123 int r;
1125 if (!map1 || !map2)
1126 return -1;
1127 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1128 return fn(map1, map2);
1129 if (!isl_space_has_named_params(map1->dim) ||
1130 !isl_space_has_named_params(map2->dim))
1131 isl_die(map1->ctx, isl_error_invalid,
1132 "unaligned unnamed parameters", return -1);
1133 map1 = isl_map_copy(map1);
1134 map2 = isl_map_copy(map2);
1135 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1136 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1137 r = fn(map1, map2);
1138 isl_map_free(map1);
1139 isl_map_free(map2);
1140 return r;
1143 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1145 struct isl_ctx *ctx;
1146 if (!bmap)
1147 return -1;
1148 ctx = bmap->ctx;
1149 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1150 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1151 return -1);
1152 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1153 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1154 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1155 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1156 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1157 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1158 isl_int *t;
1159 int j = isl_basic_map_alloc_inequality(bmap);
1160 if (j < 0)
1161 return -1;
1162 t = bmap->ineq[j];
1163 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1164 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1165 bmap->eq[-1] = t;
1166 bmap->n_eq++;
1167 bmap->n_ineq--;
1168 bmap->eq--;
1169 return 0;
1171 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1172 bmap->extra - bmap->n_div);
1173 return bmap->n_eq++;
1176 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1178 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1181 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1183 if (!bmap)
1184 return -1;
1185 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1186 bmap->n_eq -= n;
1187 return 0;
1190 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1192 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1195 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1197 isl_int *t;
1198 if (!bmap)
1199 return -1;
1200 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1202 if (pos != bmap->n_eq - 1) {
1203 t = bmap->eq[pos];
1204 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1205 bmap->eq[bmap->n_eq - 1] = t;
1207 bmap->n_eq--;
1208 return 0;
1211 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1213 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1216 /* Turn inequality "pos" of "bmap" into an equality.
1218 * In particular, we move the inequality in front of the equalities
1219 * and move the last inequality in the position of the moved inequality.
1220 * Note that isl_tab_make_equalities_explicit depends on this particular
1221 * change in the ordering of the constraints.
1223 void isl_basic_map_inequality_to_equality(
1224 struct isl_basic_map *bmap, unsigned pos)
1226 isl_int *t;
1228 t = bmap->ineq[pos];
1229 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1230 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1231 bmap->eq[-1] = t;
1232 bmap->n_eq++;
1233 bmap->n_ineq--;
1234 bmap->eq--;
1235 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1237 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1238 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1241 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1243 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1246 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1248 struct isl_ctx *ctx;
1249 if (!bmap)
1250 return -1;
1251 ctx = bmap->ctx;
1252 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1256 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1257 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1258 1 + isl_basic_map_total_dim(bmap),
1259 bmap->extra - bmap->n_div);
1260 return bmap->n_ineq++;
1263 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1265 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1268 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1270 if (!bmap)
1271 return -1;
1272 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1273 bmap->n_ineq -= n;
1274 return 0;
1277 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1279 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1282 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1284 isl_int *t;
1285 if (!bmap)
1286 return -1;
1287 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1289 if (pos != bmap->n_ineq - 1) {
1290 t = bmap->ineq[pos];
1291 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1292 bmap->ineq[bmap->n_ineq - 1] = t;
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1295 bmap->n_ineq--;
1296 return 0;
1299 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1301 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1304 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1305 isl_int *eq)
1307 int k;
1309 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1310 if (!bmap)
1311 return NULL;
1312 k = isl_basic_map_alloc_equality(bmap);
1313 if (k < 0)
1314 goto error;
1315 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1316 return bmap;
1317 error:
1318 isl_basic_map_free(bmap);
1319 return NULL;
1322 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1323 isl_int *eq)
1325 return (isl_basic_set *)
1326 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1329 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1330 isl_int *ineq)
1332 int k;
1334 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1335 if (!bmap)
1336 return NULL;
1337 k = isl_basic_map_alloc_inequality(bmap);
1338 if (k < 0)
1339 goto error;
1340 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1341 return bmap;
1342 error:
1343 isl_basic_map_free(bmap);
1344 return NULL;
1347 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1348 isl_int *ineq)
1350 return (isl_basic_set *)
1351 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1354 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1356 if (!bmap)
1357 return -1;
1358 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1359 isl_seq_clr(bmap->div[bmap->n_div] +
1360 1 + 1 + isl_basic_map_total_dim(bmap),
1361 bmap->extra - bmap->n_div);
1362 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1363 return bmap->n_div++;
1366 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1368 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1371 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1376 bmap->n_div -= n;
1377 return 0;
1380 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1382 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1385 /* Copy constraint from src to dst, putting the vars of src at offset
1386 * dim_off in dst and the divs of src at offset div_off in dst.
1387 * If both sets are actually map, then dim_off applies to the input
1388 * variables.
1390 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1391 struct isl_basic_map *src_map, isl_int *src,
1392 unsigned in_off, unsigned out_off, unsigned div_off)
1394 unsigned src_nparam = isl_basic_map_n_param(src_map);
1395 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1396 unsigned src_in = isl_basic_map_n_in(src_map);
1397 unsigned dst_in = isl_basic_map_n_in(dst_map);
1398 unsigned src_out = isl_basic_map_n_out(src_map);
1399 unsigned dst_out = isl_basic_map_n_out(dst_map);
1400 isl_int_set(dst[0], src[0]);
1401 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1402 if (dst_nparam > src_nparam)
1403 isl_seq_clr(dst+1+src_nparam,
1404 dst_nparam - src_nparam);
1405 isl_seq_clr(dst+1+dst_nparam, in_off);
1406 isl_seq_cpy(dst+1+dst_nparam+in_off,
1407 src+1+src_nparam,
1408 isl_min(dst_in-in_off, src_in));
1409 if (dst_in-in_off > src_in)
1410 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1411 dst_in - in_off - src_in);
1412 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1413 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1414 src+1+src_nparam+src_in,
1415 isl_min(dst_out-out_off, src_out));
1416 if (dst_out-out_off > src_out)
1417 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1418 dst_out - out_off - src_out);
1419 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1420 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1421 src+1+src_nparam+src_in+src_out,
1422 isl_min(dst_map->extra-div_off, src_map->n_div));
1423 if (dst_map->n_div-div_off > src_map->n_div)
1424 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1425 div_off+src_map->n_div,
1426 dst_map->n_div - div_off - src_map->n_div);
1429 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1430 struct isl_basic_map *src_map, isl_int *src,
1431 unsigned in_off, unsigned out_off, unsigned div_off)
1433 isl_int_set(dst[0], src[0]);
1434 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1437 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1438 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1440 int i;
1441 unsigned div_off;
1443 if (!bmap1 || !bmap2)
1444 goto error;
1446 div_off = bmap1->n_div;
1448 for (i = 0; i < bmap2->n_eq; ++i) {
1449 int i1 = isl_basic_map_alloc_equality(bmap1);
1450 if (i1 < 0)
1451 goto error;
1452 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1453 i_pos, o_pos, div_off);
1456 for (i = 0; i < bmap2->n_ineq; ++i) {
1457 int i1 = isl_basic_map_alloc_inequality(bmap1);
1458 if (i1 < 0)
1459 goto error;
1460 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1461 i_pos, o_pos, div_off);
1464 for (i = 0; i < bmap2->n_div; ++i) {
1465 int i1 = isl_basic_map_alloc_div(bmap1);
1466 if (i1 < 0)
1467 goto error;
1468 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1469 i_pos, o_pos, div_off);
1472 isl_basic_map_free(bmap2);
1474 return bmap1;
1476 error:
1477 isl_basic_map_free(bmap1);
1478 isl_basic_map_free(bmap2);
1479 return NULL;
1482 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1483 struct isl_basic_set *bset2, unsigned pos)
1485 return (struct isl_basic_set *)
1486 add_constraints((struct isl_basic_map *)bset1,
1487 (struct isl_basic_map *)bset2, 0, pos);
1490 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1491 __isl_take isl_space *dim, unsigned extra,
1492 unsigned n_eq, unsigned n_ineq)
1494 struct isl_basic_map *ext;
1495 unsigned flags;
1496 int dims_ok;
1498 if (!dim)
1499 goto error;
1501 if (!base)
1502 goto error;
1504 dims_ok = isl_space_is_equal(base->dim, dim) &&
1505 base->extra >= base->n_div + extra;
1507 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1508 room_for_ineq(base, n_ineq)) {
1509 isl_space_free(dim);
1510 return base;
1513 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1514 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1515 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1516 extra += base->extra;
1517 n_eq += base->n_eq;
1518 n_ineq += base->n_ineq;
1520 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1521 dim = NULL;
1522 if (!ext)
1523 goto error;
1525 if (dims_ok)
1526 ext->sample = isl_vec_copy(base->sample);
1527 flags = base->flags;
1528 ext = add_constraints(ext, base, 0, 0);
1529 if (ext) {
1530 ext->flags = flags;
1531 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1534 return ext;
1536 error:
1537 isl_space_free(dim);
1538 isl_basic_map_free(base);
1539 return NULL;
1542 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1543 __isl_take isl_space *dim, unsigned extra,
1544 unsigned n_eq, unsigned n_ineq)
1546 return (struct isl_basic_set *)
1547 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1548 extra, n_eq, n_ineq);
1551 struct isl_basic_map *isl_basic_map_extend_constraints(
1552 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1554 if (!base)
1555 return NULL;
1556 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1557 0, n_eq, n_ineq);
1560 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1561 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1562 unsigned n_eq, unsigned n_ineq)
1564 struct isl_basic_map *bmap;
1565 isl_space *dim;
1567 if (!base)
1568 return NULL;
1569 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1570 if (!dim)
1571 goto error;
1573 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1574 return bmap;
1575 error:
1576 isl_basic_map_free(base);
1577 return NULL;
1580 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1581 unsigned nparam, unsigned dim, unsigned extra,
1582 unsigned n_eq, unsigned n_ineq)
1584 return (struct isl_basic_set *)
1585 isl_basic_map_extend((struct isl_basic_map *)base,
1586 nparam, 0, dim, extra, n_eq, n_ineq);
1589 struct isl_basic_set *isl_basic_set_extend_constraints(
1590 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1592 return (struct isl_basic_set *)
1593 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1594 n_eq, n_ineq);
1597 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1599 return (struct isl_basic_set *)
1600 isl_basic_map_cow((struct isl_basic_map *)bset);
1603 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1605 if (!bmap)
1606 return NULL;
1608 if (bmap->ref > 1) {
1609 bmap->ref--;
1610 bmap = isl_basic_map_dup(bmap);
1612 if (bmap)
1613 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1614 return bmap;
1617 struct isl_set *isl_set_cow(struct isl_set *set)
1619 if (!set)
1620 return NULL;
1622 if (set->ref == 1)
1623 return set;
1624 set->ref--;
1625 return isl_set_dup(set);
1628 struct isl_map *isl_map_cow(struct isl_map *map)
1630 if (!map)
1631 return NULL;
1633 if (map->ref == 1)
1634 return map;
1635 map->ref--;
1636 return isl_map_dup(map);
1639 static void swap_vars(struct isl_blk blk, isl_int *a,
1640 unsigned a_len, unsigned b_len)
1642 isl_seq_cpy(blk.data, a+a_len, b_len);
1643 isl_seq_cpy(blk.data+b_len, a, a_len);
1644 isl_seq_cpy(a, blk.data, b_len+a_len);
1647 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1648 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1650 int i;
1651 struct isl_blk blk;
1653 if (!bmap)
1654 goto error;
1656 isl_assert(bmap->ctx,
1657 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1659 if (n1 == 0 || n2 == 0)
1660 return bmap;
1662 bmap = isl_basic_map_cow(bmap);
1663 if (!bmap)
1664 return NULL;
1666 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1667 if (isl_blk_is_error(blk))
1668 goto error;
1670 for (i = 0; i < bmap->n_eq; ++i)
1671 swap_vars(blk,
1672 bmap->eq[i] + pos, n1, n2);
1674 for (i = 0; i < bmap->n_ineq; ++i)
1675 swap_vars(blk,
1676 bmap->ineq[i] + pos, n1, n2);
1678 for (i = 0; i < bmap->n_div; ++i)
1679 swap_vars(blk,
1680 bmap->div[i]+1 + pos, n1, n2);
1682 isl_blk_free(bmap->ctx, blk);
1684 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1685 bmap = isl_basic_map_gauss(bmap, NULL);
1686 return isl_basic_map_finalize(bmap);
1687 error:
1688 isl_basic_map_free(bmap);
1689 return NULL;
1692 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1693 __isl_take isl_basic_set *bset, unsigned n)
1695 unsigned dim;
1696 unsigned nparam;
1698 if (!bset)
1699 return NULL;
1701 nparam = isl_basic_set_n_param(bset);
1702 dim = isl_basic_set_n_dim(bset);
1703 isl_assert(bset->ctx, n <= dim, goto error);
1705 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1706 error:
1707 isl_basic_set_free(bset);
1708 return NULL;
1711 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1713 int i = 0;
1714 unsigned total;
1715 if (!bmap)
1716 goto error;
1717 total = isl_basic_map_total_dim(bmap);
1718 isl_basic_map_free_div(bmap, bmap->n_div);
1719 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1720 if (bmap->n_eq > 0)
1721 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1722 else {
1723 i = isl_basic_map_alloc_equality(bmap);
1724 if (i < 0)
1725 goto error;
1727 isl_int_set_si(bmap->eq[i][0], 1);
1728 isl_seq_clr(bmap->eq[i]+1, total);
1729 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1730 isl_vec_free(bmap->sample);
1731 bmap->sample = NULL;
1732 return isl_basic_map_finalize(bmap);
1733 error:
1734 isl_basic_map_free(bmap);
1735 return NULL;
1738 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1740 return (struct isl_basic_set *)
1741 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1744 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1745 * of "bmap").
1747 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1749 isl_int *t = bmap->div[a];
1750 bmap->div[a] = bmap->div[b];
1751 bmap->div[b] = t;
1754 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1755 * div definitions accordingly.
1757 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1759 int i;
1760 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1762 swap_div(bmap, a, b);
1764 for (i = 0; i < bmap->n_eq; ++i)
1765 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1767 for (i = 0; i < bmap->n_ineq; ++i)
1768 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1770 for (i = 0; i < bmap->n_div; ++i)
1771 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1772 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1775 /* Eliminate the specified n dimensions starting at first from the
1776 * constraints, without removing the dimensions from the space.
1777 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1779 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1780 enum isl_dim_type type, unsigned first, unsigned n)
1782 int i;
1784 if (!map)
1785 return NULL;
1786 if (n == 0)
1787 return map;
1789 if (first + n > isl_map_dim(map, type) || first + n < first)
1790 isl_die(map->ctx, isl_error_invalid,
1791 "index out of bounds", goto error);
1793 map = isl_map_cow(map);
1794 if (!map)
1795 return NULL;
1797 for (i = 0; i < map->n; ++i) {
1798 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1799 if (!map->p[i])
1800 goto error;
1802 return map;
1803 error:
1804 isl_map_free(map);
1805 return NULL;
1808 /* Eliminate the specified n dimensions starting at first from the
1809 * constraints, without removing the dimensions from the space.
1810 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1812 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1813 enum isl_dim_type type, unsigned first, unsigned n)
1815 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1818 /* Eliminate the specified n dimensions starting at first from the
1819 * constraints, without removing the dimensions from the space.
1820 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1822 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1823 unsigned first, unsigned n)
1825 return isl_set_eliminate(set, isl_dim_set, first, n);
1828 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1829 __isl_take isl_basic_map *bmap)
1831 if (!bmap)
1832 return NULL;
1833 bmap = isl_basic_map_eliminate_vars(bmap,
1834 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1835 if (!bmap)
1836 return NULL;
1837 bmap->n_div = 0;
1838 return isl_basic_map_finalize(bmap);
1841 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1842 __isl_take isl_basic_set *bset)
1844 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1845 (struct isl_basic_map *)bset);
1848 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1850 int i;
1852 if (!map)
1853 return NULL;
1854 if (map->n == 0)
1855 return map;
1857 map = isl_map_cow(map);
1858 if (!map)
1859 return NULL;
1861 for (i = 0; i < map->n; ++i) {
1862 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1863 if (!map->p[i])
1864 goto error;
1866 return map;
1867 error:
1868 isl_map_free(map);
1869 return NULL;
1872 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1874 return isl_map_remove_divs(set);
1877 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1878 enum isl_dim_type type, unsigned first, unsigned n)
1880 if (!bmap)
1881 return NULL;
1882 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1883 goto error);
1884 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1885 return bmap;
1886 bmap = isl_basic_map_eliminate_vars(bmap,
1887 isl_basic_map_offset(bmap, type) - 1 + first, n);
1888 if (!bmap)
1889 return bmap;
1890 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1891 return bmap;
1892 bmap = isl_basic_map_drop(bmap, type, first, n);
1893 return bmap;
1894 error:
1895 isl_basic_map_free(bmap);
1896 return NULL;
1899 /* Return true if the definition of the given div (recursively) involves
1900 * any of the given variables.
1902 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1903 unsigned first, unsigned n)
1905 int i;
1906 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1908 if (isl_int_is_zero(bmap->div[div][0]))
1909 return 0;
1910 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1911 return 1;
1913 for (i = bmap->n_div - 1; i >= 0; --i) {
1914 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1915 continue;
1916 if (div_involves_vars(bmap, i, first, n))
1917 return 1;
1920 return 0;
1923 /* Try and add a lower and/or upper bound on "div" to "bmap"
1924 * based on inequality "i".
1925 * "total" is the total number of variables (excluding the divs).
1926 * "v" is a temporary object that can be used during the calculations.
1927 * If "lb" is set, then a lower bound should be constructed.
1928 * If "ub" is set, then an upper bound should be constructed.
1930 * The calling function has already checked that the inequality does not
1931 * reference "div", but we still need to check that the inequality is
1932 * of the right form. We'll consider the case where we want to construct
1933 * a lower bound. The construction of upper bounds is similar.
1935 * Let "div" be of the form
1937 * q = floor((a + f(x))/d)
1939 * We essentially check if constraint "i" is of the form
1941 * b + f(x) >= 0
1943 * so that we can use it to derive a lower bound on "div".
1944 * However, we allow a slightly more general form
1946 * b + g(x) >= 0
1948 * with the condition that the coefficients of g(x) - f(x) are all
1949 * divisible by d.
1950 * Rewriting this constraint as
1952 * 0 >= -b - g(x)
1954 * adding a + f(x) to both sides and dividing by d, we obtain
1956 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1958 * Taking the floor on both sides, we obtain
1960 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1962 * or
1964 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1966 * In the case of an upper bound, we construct the constraint
1968 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1971 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1972 __isl_take isl_basic_map *bmap, int div, int i,
1973 unsigned total, isl_int v, int lb, int ub)
1975 int j;
1977 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1978 if (lb) {
1979 isl_int_sub(v, bmap->ineq[i][1 + j],
1980 bmap->div[div][1 + 1 + j]);
1981 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1983 if (ub) {
1984 isl_int_add(v, bmap->ineq[i][1 + j],
1985 bmap->div[div][1 + 1 + j]);
1986 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1989 if (!lb && !ub)
1990 return bmap;
1992 bmap = isl_basic_map_cow(bmap);
1993 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1994 if (lb) {
1995 int k = isl_basic_map_alloc_inequality(bmap);
1996 if (k < 0)
1997 goto error;
1998 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1999 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2000 bmap->div[div][1 + j]);
2001 isl_int_cdiv_q(bmap->ineq[k][j],
2002 bmap->ineq[k][j], bmap->div[div][0]);
2004 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2006 if (ub) {
2007 int k = isl_basic_map_alloc_inequality(bmap);
2008 if (k < 0)
2009 goto error;
2010 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2011 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2012 bmap->div[div][1 + j]);
2013 isl_int_fdiv_q(bmap->ineq[k][j],
2014 bmap->ineq[k][j], bmap->div[div][0]);
2016 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2019 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2020 return bmap;
2021 error:
2022 isl_basic_map_free(bmap);
2023 return NULL;
2026 /* This function is called right before "div" is eliminated from "bmap"
2027 * using Fourier-Motzkin.
2028 * Look through the constraints of "bmap" for constraints on the argument
2029 * of the integer division and use them to construct constraints on the
2030 * integer division itself. These constraints can then be combined
2031 * during the Fourier-Motzkin elimination.
2032 * Note that it is only useful to introduce lower bounds on "div"
2033 * if "bmap" already contains upper bounds on "div" as the newly
2034 * introduce lower bounds can then be combined with the pre-existing
2035 * upper bounds. Similarly for upper bounds.
2036 * We therefore first check if "bmap" contains any lower and/or upper bounds
2037 * on "div".
2039 * It is interesting to note that the introduction of these constraints
2040 * can indeed lead to more accurate results, even when compared to
2041 * deriving constraints on the argument of "div" from constraints on "div".
2042 * Consider, for example, the set
2044 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2046 * The second constraint can be rewritten as
2048 * 2 * [(-i-2j+3)/4] + k >= 0
2050 * from which we can derive
2052 * -i - 2j + 3 >= -2k
2054 * or
2056 * i + 2j <= 3 + 2k
2058 * Combined with the first constraint, we obtain
2060 * -3 <= 3 + 2k or k >= -3
2062 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2063 * the first constraint, we obtain
2065 * [(i + 2j)/4] >= [-3/4] = -1
2067 * Combining this constraint with the second constraint, we obtain
2069 * k >= -2
2071 static __isl_give isl_basic_map *insert_bounds_on_div(
2072 __isl_take isl_basic_map *bmap, int div)
2074 int i;
2075 int check_lb, check_ub;
2076 isl_int v;
2077 unsigned total;
2079 if (!bmap)
2080 return NULL;
2082 if (isl_int_is_zero(bmap->div[div][0]))
2083 return bmap;
2085 total = isl_space_dim(bmap->dim, isl_dim_all);
2087 check_lb = 0;
2088 check_ub = 0;
2089 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2090 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2091 if (s > 0)
2092 check_ub = 1;
2093 if (s < 0)
2094 check_lb = 1;
2097 if (!check_lb && !check_ub)
2098 return bmap;
2100 isl_int_init(v);
2102 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2103 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2104 continue;
2106 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2107 check_lb, check_ub);
2110 isl_int_clear(v);
2112 return bmap;
2115 /* Remove all divs (recursively) involving any of the given dimensions
2116 * in their definitions.
2118 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2119 __isl_take isl_basic_map *bmap,
2120 enum isl_dim_type type, unsigned first, unsigned n)
2122 int i;
2124 if (!bmap)
2125 return NULL;
2126 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2127 goto error);
2128 first += isl_basic_map_offset(bmap, type);
2130 for (i = bmap->n_div - 1; i >= 0; --i) {
2131 if (!div_involves_vars(bmap, i, first, n))
2132 continue;
2133 bmap = insert_bounds_on_div(bmap, i);
2134 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2135 if (!bmap)
2136 return NULL;
2137 i = bmap->n_div;
2140 return bmap;
2141 error:
2142 isl_basic_map_free(bmap);
2143 return NULL;
2146 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2147 __isl_take isl_basic_set *bset,
2148 enum isl_dim_type type, unsigned first, unsigned n)
2150 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2153 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2154 enum isl_dim_type type, unsigned first, unsigned n)
2156 int i;
2158 if (!map)
2159 return NULL;
2160 if (map->n == 0)
2161 return map;
2163 map = isl_map_cow(map);
2164 if (!map)
2165 return NULL;
2167 for (i = 0; i < map->n; ++i) {
2168 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2169 type, first, n);
2170 if (!map->p[i])
2171 goto error;
2173 return map;
2174 error:
2175 isl_map_free(map);
2176 return NULL;
2179 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2180 enum isl_dim_type type, unsigned first, unsigned n)
2182 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2183 type, first, n);
2186 /* Does the desciption of "bmap" depend on the specified dimensions?
2187 * We also check whether the dimensions appear in any of the div definitions.
2188 * In principle there is no need for this check. If the dimensions appear
2189 * in a div definition, they also appear in the defining constraints of that
2190 * div.
2192 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 int i;
2197 if (!bmap)
2198 return -1;
2200 if (first + n > isl_basic_map_dim(bmap, type))
2201 isl_die(bmap->ctx, isl_error_invalid,
2202 "index out of bounds", return -1);
2204 first += isl_basic_map_offset(bmap, type);
2205 for (i = 0; i < bmap->n_eq; ++i)
2206 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2207 return 1;
2208 for (i = 0; i < bmap->n_ineq; ++i)
2209 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2210 return 1;
2211 for (i = 0; i < bmap->n_div; ++i) {
2212 if (isl_int_is_zero(bmap->div[i][0]))
2213 continue;
2214 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2215 return 1;
2218 return 0;
2221 int isl_map_involves_dims(__isl_keep isl_map *map,
2222 enum isl_dim_type type, unsigned first, unsigned n)
2224 int i;
2226 if (!map)
2227 return -1;
2229 if (first + n > isl_map_dim(map, type))
2230 isl_die(map->ctx, isl_error_invalid,
2231 "index out of bounds", return -1);
2233 for (i = 0; i < map->n; ++i) {
2234 int involves = isl_basic_map_involves_dims(map->p[i],
2235 type, first, n);
2236 if (involves < 0 || involves)
2237 return involves;
2240 return 0;
2243 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2244 enum isl_dim_type type, unsigned first, unsigned n)
2246 return isl_basic_map_involves_dims(bset, type, first, n);
2249 int isl_set_involves_dims(__isl_keep isl_set *set,
2250 enum isl_dim_type type, unsigned first, unsigned n)
2252 return isl_map_involves_dims(set, type, first, n);
2255 /* Return true if the definition of the given div is unknown or depends
2256 * on unknown divs.
2258 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2260 int i;
2261 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2263 if (isl_int_is_zero(bmap->div[div][0]))
2264 return 1;
2266 for (i = bmap->n_div - 1; i >= 0; --i) {
2267 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2268 continue;
2269 if (div_is_unknown(bmap, i))
2270 return 1;
2273 return 0;
2276 /* Remove all divs that are unknown or defined in terms of unknown divs.
2278 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2279 __isl_take isl_basic_map *bmap)
2281 int i;
2283 if (!bmap)
2284 return NULL;
2286 for (i = bmap->n_div - 1; i >= 0; --i) {
2287 if (!div_is_unknown(bmap, i))
2288 continue;
2289 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2290 if (!bmap)
2291 return NULL;
2292 i = bmap->n_div;
2295 return bmap;
2298 /* Remove all divs that are unknown or defined in terms of unknown divs.
2300 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2301 __isl_take isl_basic_set *bset)
2303 return isl_basic_map_remove_unknown_divs(bset);
2306 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2308 int i;
2310 if (!map)
2311 return NULL;
2312 if (map->n == 0)
2313 return map;
2315 map = isl_map_cow(map);
2316 if (!map)
2317 return NULL;
2319 for (i = 0; i < map->n; ++i) {
2320 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2321 if (!map->p[i])
2322 goto error;
2324 return map;
2325 error:
2326 isl_map_free(map);
2327 return NULL;
2330 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2332 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2335 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2336 __isl_take isl_basic_set *bset,
2337 enum isl_dim_type type, unsigned first, unsigned n)
2339 return (isl_basic_set *)
2340 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2343 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2344 enum isl_dim_type type, unsigned first, unsigned n)
2346 int i;
2348 if (n == 0)
2349 return map;
2351 map = isl_map_cow(map);
2352 if (!map)
2353 return NULL;
2354 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2356 for (i = 0; i < map->n; ++i) {
2357 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2358 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2359 if (!map->p[i])
2360 goto error;
2362 map = isl_map_drop(map, type, first, n);
2363 return map;
2364 error:
2365 isl_map_free(map);
2366 return NULL;
2369 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2375 /* Project out n inputs starting at first using Fourier-Motzkin */
2376 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2377 unsigned first, unsigned n)
2379 return isl_map_remove_dims(map, isl_dim_in, first, n);
2382 static void dump_term(struct isl_basic_map *bmap,
2383 isl_int c, int pos, FILE *out)
2385 const char *name;
2386 unsigned in = isl_basic_map_n_in(bmap);
2387 unsigned dim = in + isl_basic_map_n_out(bmap);
2388 unsigned nparam = isl_basic_map_n_param(bmap);
2389 if (!pos)
2390 isl_int_print(out, c, 0);
2391 else {
2392 if (!isl_int_is_one(c))
2393 isl_int_print(out, c, 0);
2394 if (pos < 1 + nparam) {
2395 name = isl_space_get_dim_name(bmap->dim,
2396 isl_dim_param, pos - 1);
2397 if (name)
2398 fprintf(out, "%s", name);
2399 else
2400 fprintf(out, "p%d", pos - 1);
2401 } else if (pos < 1 + nparam + in)
2402 fprintf(out, "i%d", pos - 1 - nparam);
2403 else if (pos < 1 + nparam + dim)
2404 fprintf(out, "o%d", pos - 1 - nparam - in);
2405 else
2406 fprintf(out, "e%d", pos - 1 - nparam - dim);
2410 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2411 int sign, FILE *out)
2413 int i;
2414 int first;
2415 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2416 isl_int v;
2418 isl_int_init(v);
2419 for (i = 0, first = 1; i < len; ++i) {
2420 if (isl_int_sgn(c[i]) * sign <= 0)
2421 continue;
2422 if (!first)
2423 fprintf(out, " + ");
2424 first = 0;
2425 isl_int_abs(v, c[i]);
2426 dump_term(bmap, v, i, out);
2428 isl_int_clear(v);
2429 if (first)
2430 fprintf(out, "0");
2433 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2434 const char *op, FILE *out, int indent)
2436 int i;
2438 fprintf(out, "%*s", indent, "");
2440 dump_constraint_sign(bmap, c, 1, out);
2441 fprintf(out, " %s ", op);
2442 dump_constraint_sign(bmap, c, -1, out);
2444 fprintf(out, "\n");
2446 for (i = bmap->n_div; i < bmap->extra; ++i) {
2447 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2448 continue;
2449 fprintf(out, "%*s", indent, "");
2450 fprintf(out, "ERROR: unused div coefficient not zero\n");
2451 abort();
2455 static void dump_constraints(struct isl_basic_map *bmap,
2456 isl_int **c, unsigned n,
2457 const char *op, FILE *out, int indent)
2459 int i;
2461 for (i = 0; i < n; ++i)
2462 dump_constraint(bmap, c[i], op, out, indent);
2465 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2467 int j;
2468 int first = 1;
2469 unsigned total = isl_basic_map_total_dim(bmap);
2471 for (j = 0; j < 1 + total; ++j) {
2472 if (isl_int_is_zero(exp[j]))
2473 continue;
2474 if (!first && isl_int_is_pos(exp[j]))
2475 fprintf(out, "+");
2476 dump_term(bmap, exp[j], j, out);
2477 first = 0;
2481 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2483 int i;
2485 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2486 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2488 for (i = 0; i < bmap->n_div; ++i) {
2489 fprintf(out, "%*s", indent, "");
2490 fprintf(out, "e%d = [(", i);
2491 dump_affine(bmap, bmap->div[i]+1, out);
2492 fprintf(out, ")/");
2493 isl_int_print(out, bmap->div[i][0], 0);
2494 fprintf(out, "]\n");
2498 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2499 FILE *out, int indent)
2501 if (!bset) {
2502 fprintf(out, "null basic set\n");
2503 return;
2506 fprintf(out, "%*s", indent, "");
2507 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2508 bset->ref, bset->dim->nparam, bset->dim->n_out,
2509 bset->extra, bset->flags);
2510 dump((struct isl_basic_map *)bset, out, indent);
2513 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2514 FILE *out, int indent)
2516 if (!bmap) {
2517 fprintf(out, "null basic map\n");
2518 return;
2521 fprintf(out, "%*s", indent, "");
2522 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2523 "flags: %x, n_name: %d\n",
2524 bmap->ref,
2525 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2526 bmap->extra, bmap->flags, bmap->dim->n_id);
2527 dump(bmap, out, indent);
2530 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2532 unsigned total;
2533 if (!bmap)
2534 return -1;
2535 total = isl_basic_map_total_dim(bmap);
2536 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2537 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2538 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2540 return 0;
2543 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2544 unsigned flags)
2546 struct isl_set *set;
2548 if (!dim)
2549 return NULL;
2550 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2551 isl_assert(dim->ctx, n >= 0, goto error);
2552 set = isl_alloc(dim->ctx, struct isl_set,
2553 sizeof(struct isl_set) +
2554 (n - 1) * sizeof(struct isl_basic_set *));
2555 if (!set)
2556 goto error;
2558 set->ctx = dim->ctx;
2559 isl_ctx_ref(set->ctx);
2560 set->ref = 1;
2561 set->size = n;
2562 set->n = 0;
2563 set->dim = dim;
2564 set->flags = flags;
2565 return set;
2566 error:
2567 isl_space_free(dim);
2568 return NULL;
2571 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2572 unsigned nparam, unsigned dim, int n, unsigned flags)
2574 struct isl_set *set;
2575 isl_space *dims;
2577 dims = isl_space_alloc(ctx, nparam, 0, dim);
2578 if (!dims)
2579 return NULL;
2581 set = isl_set_alloc_space(dims, n, flags);
2582 return set;
2585 /* Make sure "map" has room for at least "n" more basic maps.
2587 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2589 int i;
2590 struct isl_map *grown = NULL;
2592 if (!map)
2593 return NULL;
2594 isl_assert(map->ctx, n >= 0, goto error);
2595 if (map->n + n <= map->size)
2596 return map;
2597 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2598 if (!grown)
2599 goto error;
2600 for (i = 0; i < map->n; ++i) {
2601 grown->p[i] = isl_basic_map_copy(map->p[i]);
2602 if (!grown->p[i])
2603 goto error;
2604 grown->n++;
2606 isl_map_free(map);
2607 return grown;
2608 error:
2609 isl_map_free(grown);
2610 isl_map_free(map);
2611 return NULL;
2614 /* Make sure "set" has room for at least "n" more basic sets.
2616 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2618 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2621 struct isl_set *isl_set_dup(struct isl_set *set)
2623 int i;
2624 struct isl_set *dup;
2626 if (!set)
2627 return NULL;
2629 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2630 if (!dup)
2631 return NULL;
2632 for (i = 0; i < set->n; ++i)
2633 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2634 return dup;
2637 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2639 return isl_map_from_basic_map(bset);
2642 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2644 struct isl_map *map;
2646 if (!bmap)
2647 return NULL;
2649 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2650 return isl_map_add_basic_map(map, bmap);
2653 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2654 __isl_take isl_basic_set *bset)
2656 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2657 (struct isl_basic_map *)bset);
2660 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2662 int i;
2664 if (!set)
2665 return NULL;
2667 if (--set->ref > 0)
2668 return NULL;
2670 isl_ctx_deref(set->ctx);
2671 for (i = 0; i < set->n; ++i)
2672 isl_basic_set_free(set->p[i]);
2673 isl_space_free(set->dim);
2674 free(set);
2676 return NULL;
2679 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2681 int i;
2683 if (!set) {
2684 fprintf(out, "null set\n");
2685 return;
2688 fprintf(out, "%*s", indent, "");
2689 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2690 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2691 set->flags);
2692 for (i = 0; i < set->n; ++i) {
2693 fprintf(out, "%*s", indent, "");
2694 fprintf(out, "basic set %d:\n", i);
2695 isl_basic_set_print_internal(set->p[i], out, indent+4);
2699 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2701 int i;
2703 if (!map) {
2704 fprintf(out, "null map\n");
2705 return;
2708 fprintf(out, "%*s", indent, "");
2709 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2710 "flags: %x, n_name: %d\n",
2711 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2712 map->dim->n_out, map->flags, map->dim->n_id);
2713 for (i = 0; i < map->n; ++i) {
2714 fprintf(out, "%*s", indent, "");
2715 fprintf(out, "basic map %d:\n", i);
2716 isl_basic_map_print_internal(map->p[i], out, indent+4);
2720 struct isl_basic_map *isl_basic_map_intersect_domain(
2721 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2723 struct isl_basic_map *bmap_domain;
2725 if (!bmap || !bset)
2726 goto error;
2728 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2729 bset->dim, isl_dim_param), goto error);
2731 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2732 isl_assert(bset->ctx,
2733 isl_basic_map_compatible_domain(bmap, bset), goto error);
2735 bmap = isl_basic_map_cow(bmap);
2736 if (!bmap)
2737 goto error;
2738 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2739 bset->n_div, bset->n_eq, bset->n_ineq);
2740 bmap_domain = isl_basic_map_from_domain(bset);
2741 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2743 bmap = isl_basic_map_simplify(bmap);
2744 return isl_basic_map_finalize(bmap);
2745 error:
2746 isl_basic_map_free(bmap);
2747 isl_basic_set_free(bset);
2748 return NULL;
2751 struct isl_basic_map *isl_basic_map_intersect_range(
2752 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2754 struct isl_basic_map *bmap_range;
2756 if (!bmap || !bset)
2757 goto error;
2759 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2760 bset->dim, isl_dim_param), goto error);
2762 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2763 isl_assert(bset->ctx,
2764 isl_basic_map_compatible_range(bmap, bset), goto error);
2766 if (isl_basic_set_is_universe(bset)) {
2767 isl_basic_set_free(bset);
2768 return bmap;
2771 bmap = isl_basic_map_cow(bmap);
2772 if (!bmap)
2773 goto error;
2774 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2775 bset->n_div, bset->n_eq, bset->n_ineq);
2776 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2777 bmap = add_constraints(bmap, bmap_range, 0, 0);
2779 bmap = isl_basic_map_simplify(bmap);
2780 return isl_basic_map_finalize(bmap);
2781 error:
2782 isl_basic_map_free(bmap);
2783 isl_basic_set_free(bset);
2784 return NULL;
2787 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2789 int i;
2790 unsigned total;
2791 isl_int s;
2793 if (!bmap || !vec)
2794 return -1;
2796 total = 1 + isl_basic_map_total_dim(bmap);
2797 if (total != vec->size)
2798 return -1;
2800 isl_int_init(s);
2802 for (i = 0; i < bmap->n_eq; ++i) {
2803 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2804 if (!isl_int_is_zero(s)) {
2805 isl_int_clear(s);
2806 return 0;
2810 for (i = 0; i < bmap->n_ineq; ++i) {
2811 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2812 if (isl_int_is_neg(s)) {
2813 isl_int_clear(s);
2814 return 0;
2818 isl_int_clear(s);
2820 return 1;
2823 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2825 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2828 struct isl_basic_map *isl_basic_map_intersect(
2829 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2831 struct isl_vec *sample = NULL;
2833 if (!bmap1 || !bmap2)
2834 goto error;
2836 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2837 bmap2->dim, isl_dim_param), goto error);
2838 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2839 isl_space_dim(bmap1->dim, isl_dim_param) &&
2840 isl_space_dim(bmap2->dim, isl_dim_all) !=
2841 isl_space_dim(bmap2->dim, isl_dim_param))
2842 return isl_basic_map_intersect(bmap2, bmap1);
2844 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2845 isl_space_dim(bmap2->dim, isl_dim_param))
2846 isl_assert(bmap1->ctx,
2847 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2849 if (bmap1->sample &&
2850 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2851 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2852 sample = isl_vec_copy(bmap1->sample);
2853 else if (bmap2->sample &&
2854 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2855 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2856 sample = isl_vec_copy(bmap2->sample);
2858 bmap1 = isl_basic_map_cow(bmap1);
2859 if (!bmap1)
2860 goto error;
2861 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2862 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2863 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2865 if (!bmap1)
2866 isl_vec_free(sample);
2867 else if (sample) {
2868 isl_vec_free(bmap1->sample);
2869 bmap1->sample = sample;
2872 bmap1 = isl_basic_map_simplify(bmap1);
2873 return isl_basic_map_finalize(bmap1);
2874 error:
2875 if (sample)
2876 isl_vec_free(sample);
2877 isl_basic_map_free(bmap1);
2878 isl_basic_map_free(bmap2);
2879 return NULL;
2882 struct isl_basic_set *isl_basic_set_intersect(
2883 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2885 return (struct isl_basic_set *)
2886 isl_basic_map_intersect(
2887 (struct isl_basic_map *)bset1,
2888 (struct isl_basic_map *)bset2);
2891 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2892 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2894 return isl_basic_set_intersect(bset1, bset2);
2897 /* Special case of isl_map_intersect, where both map1 and map2
2898 * are convex, without any divs and such that either map1 or map2
2899 * contains a single constraint. This constraint is then simply
2900 * added to the other map.
2902 static __isl_give isl_map *map_intersect_add_constraint(
2903 __isl_take isl_map *map1, __isl_take isl_map *map2)
2905 isl_assert(map1->ctx, map1->n == 1, goto error);
2906 isl_assert(map2->ctx, map1->n == 1, goto error);
2907 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2908 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2910 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2911 return isl_map_intersect(map2, map1);
2913 isl_assert(map2->ctx,
2914 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2916 map1 = isl_map_cow(map1);
2917 if (!map1)
2918 goto error;
2919 if (isl_map_plain_is_empty(map1)) {
2920 isl_map_free(map2);
2921 return map1;
2923 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2924 if (map2->p[0]->n_eq == 1)
2925 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2926 else
2927 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2928 map2->p[0]->ineq[0]);
2930 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2931 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2932 if (!map1->p[0])
2933 goto error;
2935 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2936 isl_basic_map_free(map1->p[0]);
2937 map1->n = 0;
2940 isl_map_free(map2);
2942 return map1;
2943 error:
2944 isl_map_free(map1);
2945 isl_map_free(map2);
2946 return NULL;
2949 /* map2 may be either a parameter domain or a map living in the same
2950 * space as map1.
2952 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2953 __isl_take isl_map *map2)
2955 unsigned flags = 0;
2956 isl_map *result;
2957 int i, j;
2959 if (!map1 || !map2)
2960 goto error;
2962 if ((isl_map_plain_is_empty(map1) ||
2963 isl_map_plain_is_universe(map2)) &&
2964 isl_space_is_equal(map1->dim, map2->dim)) {
2965 isl_map_free(map2);
2966 return map1;
2968 if ((isl_map_plain_is_empty(map2) ||
2969 isl_map_plain_is_universe(map1)) &&
2970 isl_space_is_equal(map1->dim, map2->dim)) {
2971 isl_map_free(map1);
2972 return map2;
2975 if (map1->n == 1 && map2->n == 1 &&
2976 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2977 isl_space_is_equal(map1->dim, map2->dim) &&
2978 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2979 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2980 return map_intersect_add_constraint(map1, map2);
2982 if (isl_space_dim(map2->dim, isl_dim_all) !=
2983 isl_space_dim(map2->dim, isl_dim_param))
2984 isl_assert(map1->ctx,
2985 isl_space_is_equal(map1->dim, map2->dim), goto error);
2987 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2988 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2989 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2991 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2992 map1->n * map2->n, flags);
2993 if (!result)
2994 goto error;
2995 for (i = 0; i < map1->n; ++i)
2996 for (j = 0; j < map2->n; ++j) {
2997 struct isl_basic_map *part;
2998 part = isl_basic_map_intersect(
2999 isl_basic_map_copy(map1->p[i]),
3000 isl_basic_map_copy(map2->p[j]));
3001 if (isl_basic_map_is_empty(part) < 0)
3002 part = isl_basic_map_free(part);
3003 result = isl_map_add_basic_map(result, part);
3004 if (!result)
3005 goto error;
3007 isl_map_free(map1);
3008 isl_map_free(map2);
3009 return result;
3010 error:
3011 isl_map_free(map1);
3012 isl_map_free(map2);
3013 return NULL;
3016 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3017 __isl_take isl_map *map2)
3019 if (!map1 || !map2)
3020 goto error;
3021 if (!isl_space_is_equal(map1->dim, map2->dim))
3022 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3023 "spaces don't match", goto error);
3024 return map_intersect_internal(map1, map2);
3025 error:
3026 isl_map_free(map1);
3027 isl_map_free(map2);
3028 return NULL;
3031 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3032 __isl_take isl_map *map2)
3034 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3037 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3039 return (struct isl_set *)
3040 isl_map_intersect((struct isl_map *)set1,
3041 (struct isl_map *)set2);
3044 /* map_intersect_internal accepts intersections
3045 * with parameter domains, so we can just call that function.
3047 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3048 __isl_take isl_set *params)
3050 return map_intersect_internal(map, params);
3053 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3054 __isl_take isl_map *map2)
3056 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3059 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3060 __isl_take isl_set *params)
3062 return isl_map_intersect_params(set, params);
3065 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3067 isl_space *dim;
3068 struct isl_basic_set *bset;
3069 unsigned in;
3071 if (!bmap)
3072 return NULL;
3073 bmap = isl_basic_map_cow(bmap);
3074 if (!bmap)
3075 return NULL;
3076 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3077 in = isl_basic_map_n_in(bmap);
3078 bset = isl_basic_set_from_basic_map(bmap);
3079 bset = isl_basic_set_swap_vars(bset, in);
3080 return isl_basic_map_from_basic_set(bset, dim);
3083 static __isl_give isl_basic_map *basic_map_space_reset(
3084 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3086 isl_space *space;
3088 if (!bmap)
3089 return NULL;
3090 if (!isl_space_is_named_or_nested(bmap->dim, type))
3091 return bmap;
3093 space = isl_basic_map_get_space(bmap);
3094 space = isl_space_reset(space, type);
3095 bmap = isl_basic_map_reset_space(bmap, space);
3096 return bmap;
3099 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3100 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3101 unsigned pos, unsigned n)
3103 isl_space *res_dim;
3104 struct isl_basic_map *res;
3105 struct isl_dim_map *dim_map;
3106 unsigned total, off;
3107 enum isl_dim_type t;
3109 if (n == 0)
3110 return basic_map_space_reset(bmap, type);
3112 if (!bmap)
3113 return NULL;
3115 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3117 total = isl_basic_map_total_dim(bmap) + n;
3118 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3119 off = 0;
3120 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3121 if (t != type) {
3122 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3123 } else {
3124 unsigned size = isl_basic_map_dim(bmap, t);
3125 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3126 0, pos, off);
3127 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3128 pos, size - pos, off + pos + n);
3130 off += isl_space_dim(res_dim, t);
3132 isl_dim_map_div(dim_map, bmap, off);
3134 res = isl_basic_map_alloc_space(res_dim,
3135 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3136 if (isl_basic_map_is_rational(bmap))
3137 res = isl_basic_map_set_rational(res);
3138 if (isl_basic_map_plain_is_empty(bmap)) {
3139 isl_basic_map_free(bmap);
3140 free(dim_map);
3141 return isl_basic_map_set_to_empty(res);
3143 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3144 return isl_basic_map_finalize(res);
3147 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3148 __isl_take isl_basic_set *bset,
3149 enum isl_dim_type type, unsigned pos, unsigned n)
3151 return isl_basic_map_insert_dims(bset, type, pos, n);
3154 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3155 enum isl_dim_type type, unsigned n)
3157 if (!bmap)
3158 return NULL;
3159 return isl_basic_map_insert_dims(bmap, type,
3160 isl_basic_map_dim(bmap, type), n);
3163 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3164 enum isl_dim_type type, unsigned n)
3166 if (!bset)
3167 return NULL;
3168 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3169 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3170 error:
3171 isl_basic_set_free(bset);
3172 return NULL;
3175 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3176 enum isl_dim_type type)
3178 isl_space *space;
3180 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3181 return map;
3183 space = isl_map_get_space(map);
3184 space = isl_space_reset(space, type);
3185 map = isl_map_reset_space(map, space);
3186 return map;
3189 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3190 enum isl_dim_type type, unsigned pos, unsigned n)
3192 int i;
3194 if (n == 0)
3195 return map_space_reset(map, type);
3197 map = isl_map_cow(map);
3198 if (!map)
3199 return NULL;
3201 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3202 if (!map->dim)
3203 goto error;
3205 for (i = 0; i < map->n; ++i) {
3206 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3207 if (!map->p[i])
3208 goto error;
3211 return map;
3212 error:
3213 isl_map_free(map);
3214 return NULL;
3217 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3218 enum isl_dim_type type, unsigned pos, unsigned n)
3220 return isl_map_insert_dims(set, type, pos, n);
3223 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3224 enum isl_dim_type type, unsigned n)
3226 if (!map)
3227 return NULL;
3228 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3231 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3232 enum isl_dim_type type, unsigned n)
3234 if (!set)
3235 return NULL;
3236 isl_assert(set->ctx, type != isl_dim_in, goto error);
3237 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3238 error:
3239 isl_set_free(set);
3240 return NULL;
3243 __isl_give isl_basic_map *isl_basic_map_move_dims(
3244 __isl_take isl_basic_map *bmap,
3245 enum isl_dim_type dst_type, unsigned dst_pos,
3246 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3248 struct isl_dim_map *dim_map;
3249 struct isl_basic_map *res;
3250 enum isl_dim_type t;
3251 unsigned total, off;
3253 if (!bmap)
3254 return NULL;
3255 if (n == 0)
3256 return bmap;
3258 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3259 goto error);
3261 if (dst_type == src_type && dst_pos == src_pos)
3262 return bmap;
3264 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3266 if (pos(bmap->dim, dst_type) + dst_pos ==
3267 pos(bmap->dim, src_type) + src_pos +
3268 ((src_type < dst_type) ? n : 0)) {
3269 bmap = isl_basic_map_cow(bmap);
3270 if (!bmap)
3271 return NULL;
3273 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3274 src_type, src_pos, n);
3275 if (!bmap->dim)
3276 goto error;
3278 bmap = isl_basic_map_finalize(bmap);
3280 return bmap;
3283 total = isl_basic_map_total_dim(bmap);
3284 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3286 off = 0;
3287 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3288 unsigned size = isl_space_dim(bmap->dim, t);
3289 if (t == dst_type) {
3290 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3291 0, dst_pos, off);
3292 off += dst_pos;
3293 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3294 src_pos, n, off);
3295 off += n;
3296 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3297 dst_pos, size - dst_pos, off);
3298 off += size - dst_pos;
3299 } else if (t == src_type) {
3300 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3301 0, src_pos, off);
3302 off += src_pos;
3303 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3304 src_pos + n, size - src_pos - n, off);
3305 off += size - src_pos - n;
3306 } else {
3307 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3308 off += size;
3311 isl_dim_map_div(dim_map, bmap, off);
3313 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3314 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3315 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3316 if (!bmap)
3317 goto error;
3319 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3320 src_type, src_pos, n);
3321 if (!bmap->dim)
3322 goto error;
3324 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3325 bmap = isl_basic_map_gauss(bmap, NULL);
3326 bmap = isl_basic_map_finalize(bmap);
3328 return bmap;
3329 error:
3330 isl_basic_map_free(bmap);
3331 return NULL;
3334 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3335 enum isl_dim_type dst_type, unsigned dst_pos,
3336 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3338 return (isl_basic_set *)isl_basic_map_move_dims(
3339 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3342 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3343 enum isl_dim_type dst_type, unsigned dst_pos,
3344 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3346 if (!set)
3347 return NULL;
3348 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3349 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3350 src_type, src_pos, n);
3351 error:
3352 isl_set_free(set);
3353 return NULL;
3356 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3357 enum isl_dim_type dst_type, unsigned dst_pos,
3358 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3360 int i;
3362 if (!map)
3363 return NULL;
3364 if (n == 0)
3365 return map;
3367 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3368 goto error);
3370 if (dst_type == src_type && dst_pos == src_pos)
3371 return map;
3373 isl_assert(map->ctx, dst_type != src_type, goto error);
3375 map = isl_map_cow(map);
3376 if (!map)
3377 return NULL;
3379 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3380 if (!map->dim)
3381 goto error;
3383 for (i = 0; i < map->n; ++i) {
3384 map->p[i] = isl_basic_map_move_dims(map->p[i],
3385 dst_type, dst_pos,
3386 src_type, src_pos, n);
3387 if (!map->p[i])
3388 goto error;
3391 return map;
3392 error:
3393 isl_map_free(map);
3394 return NULL;
3397 /* Move the specified dimensions to the last columns right before
3398 * the divs. Don't change the dimension specification of bmap.
3399 * That's the responsibility of the caller.
3401 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3402 enum isl_dim_type type, unsigned first, unsigned n)
3404 struct isl_dim_map *dim_map;
3405 struct isl_basic_map *res;
3406 enum isl_dim_type t;
3407 unsigned total, off;
3409 if (!bmap)
3410 return NULL;
3411 if (pos(bmap->dim, type) + first + n ==
3412 1 + isl_space_dim(bmap->dim, isl_dim_all))
3413 return bmap;
3415 total = isl_basic_map_total_dim(bmap);
3416 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3418 off = 0;
3419 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3420 unsigned size = isl_space_dim(bmap->dim, t);
3421 if (t == type) {
3422 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3423 0, first, off);
3424 off += first;
3425 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3426 first, n, total - bmap->n_div - n);
3427 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3428 first + n, size - (first + n), off);
3429 off += size - (first + n);
3430 } else {
3431 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3432 off += size;
3435 isl_dim_map_div(dim_map, bmap, off + n);
3437 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3438 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3439 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3440 return res;
3443 /* Insert "n" rows in the divs of "bmap".
3445 * The number of columns is not changed, which means that the last
3446 * dimensions of "bmap" are being reintepreted as the new divs.
3447 * The space of "bmap" is not adjusted, however, which means
3448 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3449 * from the space of "bmap" is the responsibility of the caller.
3451 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3452 int n)
3454 int i;
3455 size_t row_size;
3456 isl_int **new_div;
3457 isl_int *old;
3459 bmap = isl_basic_map_cow(bmap);
3460 if (!bmap)
3461 return NULL;
3463 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3464 old = bmap->block2.data;
3465 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3466 (bmap->extra + n) * (1 + row_size));
3467 if (!bmap->block2.data)
3468 return isl_basic_map_free(bmap);
3469 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3470 if (!new_div)
3471 return isl_basic_map_free(bmap);
3472 for (i = 0; i < n; ++i) {
3473 new_div[i] = bmap->block2.data +
3474 (bmap->extra + i) * (1 + row_size);
3475 isl_seq_clr(new_div[i], 1 + row_size);
3477 for (i = 0; i < bmap->extra; ++i)
3478 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3479 free(bmap->div);
3480 bmap->div = new_div;
3481 bmap->n_div += n;
3482 bmap->extra += n;
3484 return bmap;
3487 /* Turn the n dimensions of type type, starting at first
3488 * into existentially quantified variables.
3490 __isl_give isl_basic_map *isl_basic_map_project_out(
3491 __isl_take isl_basic_map *bmap,
3492 enum isl_dim_type type, unsigned first, unsigned n)
3494 if (n == 0)
3495 return basic_map_space_reset(bmap, type);
3497 if (!bmap)
3498 return NULL;
3500 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3501 return isl_basic_map_remove_dims(bmap, type, first, n);
3503 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3504 goto error);
3506 bmap = move_last(bmap, type, first, n);
3507 bmap = isl_basic_map_cow(bmap);
3508 bmap = insert_div_rows(bmap, n);
3509 if (!bmap)
3510 return NULL;
3512 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3513 if (!bmap->dim)
3514 goto error;
3515 bmap = isl_basic_map_simplify(bmap);
3516 bmap = isl_basic_map_drop_redundant_divs(bmap);
3517 return isl_basic_map_finalize(bmap);
3518 error:
3519 isl_basic_map_free(bmap);
3520 return NULL;
3523 /* Turn the n dimensions of type type, starting at first
3524 * into existentially quantified variables.
3526 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3527 enum isl_dim_type type, unsigned first, unsigned n)
3529 return (isl_basic_set *)isl_basic_map_project_out(
3530 (isl_basic_map *)bset, type, first, n);
3533 /* Turn the n dimensions of type type, starting at first
3534 * into existentially quantified variables.
3536 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3537 enum isl_dim_type type, unsigned first, unsigned n)
3539 int i;
3541 if (!map)
3542 return NULL;
3544 if (n == 0)
3545 return map_space_reset(map, type);
3547 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3549 map = isl_map_cow(map);
3550 if (!map)
3551 return NULL;
3553 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3554 if (!map->dim)
3555 goto error;
3557 for (i = 0; i < map->n; ++i) {
3558 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3559 if (!map->p[i])
3560 goto error;
3563 return map;
3564 error:
3565 isl_map_free(map);
3566 return NULL;
3569 /* Turn the n dimensions of type type, starting at first
3570 * into existentially quantified variables.
3572 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3573 enum isl_dim_type type, unsigned first, unsigned n)
3575 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3578 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3580 int i, j;
3582 for (i = 0; i < n; ++i) {
3583 j = isl_basic_map_alloc_div(bmap);
3584 if (j < 0)
3585 goto error;
3586 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3588 return bmap;
3589 error:
3590 isl_basic_map_free(bmap);
3591 return NULL;
3594 struct isl_basic_map *isl_basic_map_apply_range(
3595 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3597 isl_space *dim_result = NULL;
3598 struct isl_basic_map *bmap;
3599 unsigned n_in, n_out, n, nparam, total, pos;
3600 struct isl_dim_map *dim_map1, *dim_map2;
3602 if (!bmap1 || !bmap2)
3603 goto error;
3604 if (!isl_space_match(bmap1->dim, isl_dim_param,
3605 bmap2->dim, isl_dim_param))
3606 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3607 "parameters don't match", goto error);
3608 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3609 bmap2->dim, isl_dim_in))
3610 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3611 "spaces don't match", goto error);
3613 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3614 isl_space_copy(bmap2->dim));
3616 n_in = isl_basic_map_n_in(bmap1);
3617 n_out = isl_basic_map_n_out(bmap2);
3618 n = isl_basic_map_n_out(bmap1);
3619 nparam = isl_basic_map_n_param(bmap1);
3621 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3622 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3623 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3624 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3625 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3626 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3627 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3628 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3629 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3630 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3631 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3633 bmap = isl_basic_map_alloc_space(dim_result,
3634 bmap1->n_div + bmap2->n_div + n,
3635 bmap1->n_eq + bmap2->n_eq,
3636 bmap1->n_ineq + bmap2->n_ineq);
3637 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3638 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3639 bmap = add_divs(bmap, n);
3640 bmap = isl_basic_map_simplify(bmap);
3641 bmap = isl_basic_map_drop_redundant_divs(bmap);
3642 return isl_basic_map_finalize(bmap);
3643 error:
3644 isl_basic_map_free(bmap1);
3645 isl_basic_map_free(bmap2);
3646 return NULL;
3649 struct isl_basic_set *isl_basic_set_apply(
3650 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3652 if (!bset || !bmap)
3653 goto error;
3655 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3656 goto error);
3658 return (struct isl_basic_set *)
3659 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3660 error:
3661 isl_basic_set_free(bset);
3662 isl_basic_map_free(bmap);
3663 return NULL;
3666 struct isl_basic_map *isl_basic_map_apply_domain(
3667 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3669 if (!bmap1 || !bmap2)
3670 goto error;
3672 isl_assert(bmap1->ctx,
3673 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3674 isl_assert(bmap1->ctx,
3675 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3676 goto error);
3678 bmap1 = isl_basic_map_reverse(bmap1);
3679 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3680 return isl_basic_map_reverse(bmap1);
3681 error:
3682 isl_basic_map_free(bmap1);
3683 isl_basic_map_free(bmap2);
3684 return NULL;
3687 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3688 * A \cap B -> f(A) + f(B)
3690 struct isl_basic_map *isl_basic_map_sum(
3691 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3693 unsigned n_in, n_out, nparam, total, pos;
3694 struct isl_basic_map *bmap = NULL;
3695 struct isl_dim_map *dim_map1, *dim_map2;
3696 int i;
3698 if (!bmap1 || !bmap2)
3699 goto error;
3701 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3702 goto error);
3704 nparam = isl_basic_map_n_param(bmap1);
3705 n_in = isl_basic_map_n_in(bmap1);
3706 n_out = isl_basic_map_n_out(bmap1);
3708 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3709 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3710 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3711 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3712 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3713 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3714 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3715 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3716 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3717 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3718 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3720 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3721 bmap1->n_div + bmap2->n_div + 2 * n_out,
3722 bmap1->n_eq + bmap2->n_eq + n_out,
3723 bmap1->n_ineq + bmap2->n_ineq);
3724 for (i = 0; i < n_out; ++i) {
3725 int j = isl_basic_map_alloc_equality(bmap);
3726 if (j < 0)
3727 goto error;
3728 isl_seq_clr(bmap->eq[j], 1+total);
3729 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3730 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3731 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3733 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3735 bmap = add_divs(bmap, 2 * n_out);
3737 bmap = isl_basic_map_simplify(bmap);
3738 return isl_basic_map_finalize(bmap);
3739 error:
3740 isl_basic_map_free(bmap);
3741 isl_basic_map_free(bmap1);
3742 isl_basic_map_free(bmap2);
3743 return NULL;
3746 /* Given two maps A -> f(A) and B -> g(B), construct a map
3747 * A \cap B -> f(A) + f(B)
3749 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3751 struct isl_map *result;
3752 int i, j;
3754 if (!map1 || !map2)
3755 goto error;
3757 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3759 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3760 map1->n * map2->n, 0);
3761 if (!result)
3762 goto error;
3763 for (i = 0; i < map1->n; ++i)
3764 for (j = 0; j < map2->n; ++j) {
3765 struct isl_basic_map *part;
3766 part = isl_basic_map_sum(
3767 isl_basic_map_copy(map1->p[i]),
3768 isl_basic_map_copy(map2->p[j]));
3769 if (isl_basic_map_is_empty(part))
3770 isl_basic_map_free(part);
3771 else
3772 result = isl_map_add_basic_map(result, part);
3773 if (!result)
3774 goto error;
3776 isl_map_free(map1);
3777 isl_map_free(map2);
3778 return result;
3779 error:
3780 isl_map_free(map1);
3781 isl_map_free(map2);
3782 return NULL;
3785 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3786 __isl_take isl_set *set2)
3788 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3791 /* Given a basic map A -> f(A), construct A -> -f(A).
3793 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3795 int i, j;
3796 unsigned off, n;
3798 bmap = isl_basic_map_cow(bmap);
3799 if (!bmap)
3800 return NULL;
3802 n = isl_basic_map_dim(bmap, isl_dim_out);
3803 off = isl_basic_map_offset(bmap, isl_dim_out);
3804 for (i = 0; i < bmap->n_eq; ++i)
3805 for (j = 0; j < n; ++j)
3806 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3807 for (i = 0; i < bmap->n_ineq; ++i)
3808 for (j = 0; j < n; ++j)
3809 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3810 for (i = 0; i < bmap->n_div; ++i)
3811 for (j = 0; j < n; ++j)
3812 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3813 bmap = isl_basic_map_gauss(bmap, NULL);
3814 return isl_basic_map_finalize(bmap);
3817 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3819 return isl_basic_map_neg(bset);
3822 /* Given a map A -> f(A), construct A -> -f(A).
3824 struct isl_map *isl_map_neg(struct isl_map *map)
3826 int i;
3828 map = isl_map_cow(map);
3829 if (!map)
3830 return NULL;
3832 for (i = 0; i < map->n; ++i) {
3833 map->p[i] = isl_basic_map_neg(map->p[i]);
3834 if (!map->p[i])
3835 goto error;
3838 return map;
3839 error:
3840 isl_map_free(map);
3841 return NULL;
3844 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3846 return (isl_set *)isl_map_neg((isl_map *)set);
3849 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3850 * A -> floor(f(A)/d).
3852 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3853 isl_int d)
3855 unsigned n_in, n_out, nparam, total, pos;
3856 struct isl_basic_map *result = NULL;
3857 struct isl_dim_map *dim_map;
3858 int i;
3860 if (!bmap)
3861 return NULL;
3863 nparam = isl_basic_map_n_param(bmap);
3864 n_in = isl_basic_map_n_in(bmap);
3865 n_out = isl_basic_map_n_out(bmap);
3867 total = nparam + n_in + n_out + bmap->n_div + n_out;
3868 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3869 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3870 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3871 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3872 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3874 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3875 bmap->n_div + n_out,
3876 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3877 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3878 result = add_divs(result, n_out);
3879 for (i = 0; i < n_out; ++i) {
3880 int j;
3881 j = isl_basic_map_alloc_inequality(result);
3882 if (j < 0)
3883 goto error;
3884 isl_seq_clr(result->ineq[j], 1+total);
3885 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3886 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3887 j = isl_basic_map_alloc_inequality(result);
3888 if (j < 0)
3889 goto error;
3890 isl_seq_clr(result->ineq[j], 1+total);
3891 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3892 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3893 isl_int_sub_ui(result->ineq[j][0], d, 1);
3896 result = isl_basic_map_simplify(result);
3897 return isl_basic_map_finalize(result);
3898 error:
3899 isl_basic_map_free(result);
3900 return NULL;
3903 /* Given a map A -> f(A) and an integer d, construct a map
3904 * A -> floor(f(A)/d).
3906 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3908 int i;
3910 map = isl_map_cow(map);
3911 if (!map)
3912 return NULL;
3914 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3915 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3916 for (i = 0; i < map->n; ++i) {
3917 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3918 if (!map->p[i])
3919 goto error;
3922 return map;
3923 error:
3924 isl_map_free(map);
3925 return NULL;
3928 /* Given a map A -> f(A) and an integer d, construct a map
3929 * A -> floor(f(A)/d).
3931 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3932 __isl_take isl_val *d)
3934 if (!map || !d)
3935 goto error;
3936 if (!isl_val_is_int(d))
3937 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3938 "expecting integer denominator", goto error);
3939 map = isl_map_floordiv(map, d->n);
3940 isl_val_free(d);
3941 return map;
3942 error:
3943 isl_map_free(map);
3944 isl_val_free(d);
3945 return NULL;
3948 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3950 int i;
3951 unsigned nparam;
3952 unsigned n_in;
3954 i = isl_basic_map_alloc_equality(bmap);
3955 if (i < 0)
3956 goto error;
3957 nparam = isl_basic_map_n_param(bmap);
3958 n_in = isl_basic_map_n_in(bmap);
3959 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3960 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3961 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3962 return isl_basic_map_finalize(bmap);
3963 error:
3964 isl_basic_map_free(bmap);
3965 return NULL;
3968 /* Add a constraints to "bmap" expressing i_pos < o_pos
3970 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3972 int i;
3973 unsigned nparam;
3974 unsigned n_in;
3976 i = isl_basic_map_alloc_inequality(bmap);
3977 if (i < 0)
3978 goto error;
3979 nparam = isl_basic_map_n_param(bmap);
3980 n_in = isl_basic_map_n_in(bmap);
3981 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3982 isl_int_set_si(bmap->ineq[i][0], -1);
3983 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3984 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3985 return isl_basic_map_finalize(bmap);
3986 error:
3987 isl_basic_map_free(bmap);
3988 return NULL;
3991 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3993 static __isl_give isl_basic_map *var_less_or_equal(
3994 __isl_take isl_basic_map *bmap, unsigned pos)
3996 int i;
3997 unsigned nparam;
3998 unsigned n_in;
4000 i = isl_basic_map_alloc_inequality(bmap);
4001 if (i < 0)
4002 goto error;
4003 nparam = isl_basic_map_n_param(bmap);
4004 n_in = isl_basic_map_n_in(bmap);
4005 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4006 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4007 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4008 return isl_basic_map_finalize(bmap);
4009 error:
4010 isl_basic_map_free(bmap);
4011 return NULL;
4014 /* Add a constraints to "bmap" expressing i_pos > o_pos
4016 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4018 int i;
4019 unsigned nparam;
4020 unsigned n_in;
4022 i = isl_basic_map_alloc_inequality(bmap);
4023 if (i < 0)
4024 goto error;
4025 nparam = isl_basic_map_n_param(bmap);
4026 n_in = isl_basic_map_n_in(bmap);
4027 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4028 isl_int_set_si(bmap->ineq[i][0], -1);
4029 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4030 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4031 return isl_basic_map_finalize(bmap);
4032 error:
4033 isl_basic_map_free(bmap);
4034 return NULL;
4037 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4039 static __isl_give isl_basic_map *var_more_or_equal(
4040 __isl_take isl_basic_map *bmap, unsigned pos)
4042 int i;
4043 unsigned nparam;
4044 unsigned n_in;
4046 i = isl_basic_map_alloc_inequality(bmap);
4047 if (i < 0)
4048 goto error;
4049 nparam = isl_basic_map_n_param(bmap);
4050 n_in = isl_basic_map_n_in(bmap);
4051 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4052 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4053 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4054 return isl_basic_map_finalize(bmap);
4055 error:
4056 isl_basic_map_free(bmap);
4057 return NULL;
4060 __isl_give isl_basic_map *isl_basic_map_equal(
4061 __isl_take isl_space *dim, unsigned n_equal)
4063 int i;
4064 struct isl_basic_map *bmap;
4065 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4066 if (!bmap)
4067 return NULL;
4068 for (i = 0; i < n_equal && bmap; ++i)
4069 bmap = var_equal(bmap, i);
4070 return isl_basic_map_finalize(bmap);
4073 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4075 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4076 unsigned pos)
4078 int i;
4079 struct isl_basic_map *bmap;
4080 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4081 if (!bmap)
4082 return NULL;
4083 for (i = 0; i < pos && bmap; ++i)
4084 bmap = var_equal(bmap, i);
4085 if (bmap)
4086 bmap = var_less(bmap, pos);
4087 return isl_basic_map_finalize(bmap);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4092 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4093 __isl_take isl_space *dim, unsigned pos)
4095 int i;
4096 isl_basic_map *bmap;
4098 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4099 for (i = 0; i < pos; ++i)
4100 bmap = var_equal(bmap, i);
4101 bmap = var_less_or_equal(bmap, pos);
4102 return isl_basic_map_finalize(bmap);
4105 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4107 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4108 unsigned pos)
4110 int i;
4111 struct isl_basic_map *bmap;
4112 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4113 if (!bmap)
4114 return NULL;
4115 for (i = 0; i < pos && bmap; ++i)
4116 bmap = var_equal(bmap, i);
4117 if (bmap)
4118 bmap = var_more(bmap, pos);
4119 return isl_basic_map_finalize(bmap);
4122 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4124 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4125 __isl_take isl_space *dim, unsigned pos)
4127 int i;
4128 isl_basic_map *bmap;
4130 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4131 for (i = 0; i < pos; ++i)
4132 bmap = var_equal(bmap, i);
4133 bmap = var_more_or_equal(bmap, pos);
4134 return isl_basic_map_finalize(bmap);
4137 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4138 unsigned n, int equal)
4140 struct isl_map *map;
4141 int i;
4143 if (n == 0 && equal)
4144 return isl_map_universe(dims);
4146 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4148 for (i = 0; i + 1 < n; ++i)
4149 map = isl_map_add_basic_map(map,
4150 isl_basic_map_less_at(isl_space_copy(dims), i));
4151 if (n > 0) {
4152 if (equal)
4153 map = isl_map_add_basic_map(map,
4154 isl_basic_map_less_or_equal_at(dims, n - 1));
4155 else
4156 map = isl_map_add_basic_map(map,
4157 isl_basic_map_less_at(dims, n - 1));
4158 } else
4159 isl_space_free(dims);
4161 return map;
4164 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4166 if (!dims)
4167 return NULL;
4168 return map_lex_lte_first(dims, dims->n_out, equal);
4171 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4173 return map_lex_lte_first(dim, n, 0);
4176 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4178 return map_lex_lte_first(dim, n, 1);
4181 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4183 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4186 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4188 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4191 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4192 unsigned n, int equal)
4194 struct isl_map *map;
4195 int i;
4197 if (n == 0 && equal)
4198 return isl_map_universe(dims);
4200 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4202 for (i = 0; i + 1 < n; ++i)
4203 map = isl_map_add_basic_map(map,
4204 isl_basic_map_more_at(isl_space_copy(dims), i));
4205 if (n > 0) {
4206 if (equal)
4207 map = isl_map_add_basic_map(map,
4208 isl_basic_map_more_or_equal_at(dims, n - 1));
4209 else
4210 map = isl_map_add_basic_map(map,
4211 isl_basic_map_more_at(dims, n - 1));
4212 } else
4213 isl_space_free(dims);
4215 return map;
4218 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4220 if (!dims)
4221 return NULL;
4222 return map_lex_gte_first(dims, dims->n_out, equal);
4225 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4227 return map_lex_gte_first(dim, n, 0);
4230 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4232 return map_lex_gte_first(dim, n, 1);
4235 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4237 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4240 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4242 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4245 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4246 __isl_take isl_set *set2)
4248 isl_map *map;
4249 map = isl_map_lex_le(isl_set_get_space(set1));
4250 map = isl_map_intersect_domain(map, set1);
4251 map = isl_map_intersect_range(map, set2);
4252 return map;
4255 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4256 __isl_take isl_set *set2)
4258 isl_map *map;
4259 map = isl_map_lex_lt(isl_set_get_space(set1));
4260 map = isl_map_intersect_domain(map, set1);
4261 map = isl_map_intersect_range(map, set2);
4262 return map;
4265 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4266 __isl_take isl_set *set2)
4268 isl_map *map;
4269 map = isl_map_lex_ge(isl_set_get_space(set1));
4270 map = isl_map_intersect_domain(map, set1);
4271 map = isl_map_intersect_range(map, set2);
4272 return map;
4275 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4276 __isl_take isl_set *set2)
4278 isl_map *map;
4279 map = isl_map_lex_gt(isl_set_get_space(set1));
4280 map = isl_map_intersect_domain(map, set1);
4281 map = isl_map_intersect_range(map, set2);
4282 return map;
4285 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4286 __isl_take isl_map *map2)
4288 isl_map *map;
4289 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4290 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4291 map = isl_map_apply_range(map, isl_map_reverse(map2));
4292 return map;
4295 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4296 __isl_take isl_map *map2)
4298 isl_map *map;
4299 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4300 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4301 map = isl_map_apply_range(map, isl_map_reverse(map2));
4302 return map;
4305 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4306 __isl_take isl_map *map2)
4308 isl_map *map;
4309 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4310 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4311 map = isl_map_apply_range(map, isl_map_reverse(map2));
4312 return map;
4315 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4316 __isl_take isl_map *map2)
4318 isl_map *map;
4319 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4320 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4321 map = isl_map_apply_range(map, isl_map_reverse(map2));
4322 return map;
4325 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4326 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4328 struct isl_basic_map *bmap;
4330 bset = isl_basic_set_cow(bset);
4331 if (!bset || !dim)
4332 goto error;
4334 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4335 isl_space_free(bset->dim);
4336 bmap = (struct isl_basic_map *) bset;
4337 bmap->dim = dim;
4338 return isl_basic_map_finalize(bmap);
4339 error:
4340 isl_basic_set_free(bset);
4341 isl_space_free(dim);
4342 return NULL;
4345 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4347 if (!bmap)
4348 goto error;
4349 if (bmap->dim->n_in == 0)
4350 return (struct isl_basic_set *)bmap;
4351 bmap = isl_basic_map_cow(bmap);
4352 if (!bmap)
4353 goto error;
4354 bmap->dim = isl_space_as_set_space(bmap->dim);
4355 if (!bmap->dim)
4356 goto error;
4357 bmap = isl_basic_map_finalize(bmap);
4358 return (struct isl_basic_set *)bmap;
4359 error:
4360 isl_basic_map_free(bmap);
4361 return NULL;
4364 /* For a div d = floor(f/m), add the constraint
4366 * f - m d >= 0
4368 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4369 unsigned pos, isl_int *div)
4371 int i;
4372 unsigned total = isl_basic_map_total_dim(bmap);
4374 i = isl_basic_map_alloc_inequality(bmap);
4375 if (i < 0)
4376 return -1;
4377 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4378 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4380 return 0;
4383 /* For a div d = floor(f/m), add the constraint
4385 * -(f-(n-1)) + m d >= 0
4387 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4388 unsigned pos, isl_int *div)
4390 int i;
4391 unsigned total = isl_basic_map_total_dim(bmap);
4393 i = isl_basic_map_alloc_inequality(bmap);
4394 if (i < 0)
4395 return -1;
4396 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4397 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4398 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4399 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4401 return 0;
4404 /* For a div d = floor(f/m), add the constraints
4406 * f - m d >= 0
4407 * -(f-(n-1)) + m d >= 0
4409 * Note that the second constraint is the negation of
4411 * f - m d >= n
4413 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4414 unsigned pos, isl_int *div)
4416 if (add_upper_div_constraint(bmap, pos, div) < 0)
4417 return -1;
4418 if (add_lower_div_constraint(bmap, pos, div) < 0)
4419 return -1;
4420 return 0;
4423 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4424 unsigned pos, isl_int *div)
4426 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4427 pos, div);
4430 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4432 unsigned total = isl_basic_map_total_dim(bmap);
4433 unsigned div_pos = total - bmap->n_div + div;
4435 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4436 bmap->div[div]);
4439 /* For each known div d = floor(f/m), add the constraints
4441 * f - m d >= 0
4442 * -(f-(n-1)) + m d >= 0
4444 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4445 __isl_take isl_basic_map *bmap)
4447 int i;
4448 unsigned n_div;
4450 if (!bmap)
4451 return NULL;
4452 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4453 if (n_div == 0)
4454 return bmap;
4455 bmap = isl_basic_map_cow(bmap);
4456 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4457 if (!bmap)
4458 return NULL;
4459 for (i = 0; i < n_div; ++i) {
4460 if (isl_int_is_zero(bmap->div[i][0]))
4461 continue;
4462 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4463 return isl_basic_map_free(bmap);
4466 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4467 bmap = isl_basic_map_finalize(bmap);
4468 return bmap;
4471 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4473 * In particular, if this div is of the form d = floor(f/m),
4474 * then add the constraint
4476 * f - m d >= 0
4478 * if sign < 0 or the constraint
4480 * -(f-(n-1)) + m d >= 0
4482 * if sign > 0.
4484 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4485 unsigned div, int sign)
4487 unsigned total;
4488 unsigned div_pos;
4490 if (!bmap)
4491 return -1;
4493 total = isl_basic_map_total_dim(bmap);
4494 div_pos = total - bmap->n_div + div;
4496 if (sign < 0)
4497 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4498 else
4499 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4502 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4504 return isl_basic_map_add_div_constraints(bset, div);
4507 struct isl_basic_set *isl_basic_map_underlying_set(
4508 struct isl_basic_map *bmap)
4510 if (!bmap)
4511 goto error;
4512 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4513 bmap->n_div == 0 &&
4514 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4515 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4516 return (struct isl_basic_set *)bmap;
4517 bmap = isl_basic_map_cow(bmap);
4518 if (!bmap)
4519 goto error;
4520 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4521 if (!bmap->dim)
4522 goto error;
4523 bmap->extra -= bmap->n_div;
4524 bmap->n_div = 0;
4525 bmap = isl_basic_map_finalize(bmap);
4526 return (struct isl_basic_set *)bmap;
4527 error:
4528 isl_basic_map_free(bmap);
4529 return NULL;
4532 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4533 __isl_take isl_basic_set *bset)
4535 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4538 /* Replace each element in "list" by the result of applying
4539 * isl_basic_set_underlying_set to the element.
4541 __isl_give isl_basic_set_list *isl_basic_set_list_underlying_set(
4542 __isl_take isl_basic_set_list *list)
4544 int i, n;
4546 if (!list)
4547 return NULL;
4549 n = isl_basic_set_list_n_basic_set(list);
4550 for (i = 0; i < n; ++i) {
4551 isl_basic_set *bset;
4553 bset = isl_basic_set_list_get_basic_set(list, i);
4554 bset = isl_basic_set_underlying_set(bset);
4555 list = isl_basic_set_list_set_basic_set(list, i, bset);
4558 return list;
4561 struct isl_basic_map *isl_basic_map_overlying_set(
4562 struct isl_basic_set *bset, struct isl_basic_map *like)
4564 struct isl_basic_map *bmap;
4565 struct isl_ctx *ctx;
4566 unsigned total;
4567 int i;
4569 if (!bset || !like)
4570 goto error;
4571 ctx = bset->ctx;
4572 isl_assert(ctx, bset->n_div == 0, goto error);
4573 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4574 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4575 goto error);
4576 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4577 isl_basic_map_free(like);
4578 return (struct isl_basic_map *)bset;
4580 bset = isl_basic_set_cow(bset);
4581 if (!bset)
4582 goto error;
4583 total = bset->dim->n_out + bset->extra;
4584 bmap = (struct isl_basic_map *)bset;
4585 isl_space_free(bmap->dim);
4586 bmap->dim = isl_space_copy(like->dim);
4587 if (!bmap->dim)
4588 goto error;
4589 bmap->n_div = like->n_div;
4590 bmap->extra += like->n_div;
4591 if (bmap->extra) {
4592 unsigned ltotal;
4593 isl_int **div;
4594 ltotal = total - bmap->extra + like->extra;
4595 if (ltotal > total)
4596 ltotal = total;
4597 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4598 bmap->extra * (1 + 1 + total));
4599 if (isl_blk_is_error(bmap->block2))
4600 goto error;
4601 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4602 if (!div)
4603 goto error;
4604 bmap->div = div;
4605 for (i = 0; i < bmap->extra; ++i)
4606 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4607 for (i = 0; i < like->n_div; ++i) {
4608 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4609 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4611 bmap = isl_basic_map_add_known_div_constraints(bmap);
4613 isl_basic_map_free(like);
4614 bmap = isl_basic_map_simplify(bmap);
4615 bmap = isl_basic_map_finalize(bmap);
4616 return bmap;
4617 error:
4618 isl_basic_map_free(like);
4619 isl_basic_set_free(bset);
4620 return NULL;
4623 struct isl_basic_set *isl_basic_set_from_underlying_set(
4624 struct isl_basic_set *bset, struct isl_basic_set *like)
4626 return (struct isl_basic_set *)
4627 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4630 struct isl_set *isl_set_from_underlying_set(
4631 struct isl_set *set, struct isl_basic_set *like)
4633 int i;
4635 if (!set || !like)
4636 goto error;
4637 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4638 goto error);
4639 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4640 isl_basic_set_free(like);
4641 return set;
4643 set = isl_set_cow(set);
4644 if (!set)
4645 goto error;
4646 for (i = 0; i < set->n; ++i) {
4647 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4648 isl_basic_set_copy(like));
4649 if (!set->p[i])
4650 goto error;
4652 isl_space_free(set->dim);
4653 set->dim = isl_space_copy(like->dim);
4654 if (!set->dim)
4655 goto error;
4656 isl_basic_set_free(like);
4657 return set;
4658 error:
4659 isl_basic_set_free(like);
4660 isl_set_free(set);
4661 return NULL;
4664 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4666 int i;
4668 map = isl_map_cow(map);
4669 if (!map)
4670 return NULL;
4671 map->dim = isl_space_cow(map->dim);
4672 if (!map->dim)
4673 goto error;
4675 for (i = 1; i < map->n; ++i)
4676 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4677 goto error);
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = (struct isl_basic_map *)
4680 isl_basic_map_underlying_set(map->p[i]);
4681 if (!map->p[i])
4682 goto error;
4684 if (map->n == 0)
4685 map->dim = isl_space_underlying(map->dim, 0);
4686 else {
4687 isl_space_free(map->dim);
4688 map->dim = isl_space_copy(map->p[0]->dim);
4690 if (!map->dim)
4691 goto error;
4692 return (struct isl_set *)map;
4693 error:
4694 isl_map_free(map);
4695 return NULL;
4698 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4700 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4703 __isl_give isl_basic_map *isl_basic_map_reset_space(
4704 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4706 bmap = isl_basic_map_cow(bmap);
4707 if (!bmap || !dim)
4708 goto error;
4710 isl_space_free(bmap->dim);
4711 bmap->dim = dim;
4713 bmap = isl_basic_map_finalize(bmap);
4715 return bmap;
4716 error:
4717 isl_basic_map_free(bmap);
4718 isl_space_free(dim);
4719 return NULL;
4722 __isl_give isl_basic_set *isl_basic_set_reset_space(
4723 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4725 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4726 dim);
4729 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4730 __isl_take isl_space *dim)
4732 int i;
4734 map = isl_map_cow(map);
4735 if (!map || !dim)
4736 goto error;
4738 for (i = 0; i < map->n; ++i) {
4739 map->p[i] = isl_basic_map_reset_space(map->p[i],
4740 isl_space_copy(dim));
4741 if (!map->p[i])
4742 goto error;
4744 isl_space_free(map->dim);
4745 map->dim = dim;
4747 return map;
4748 error:
4749 isl_map_free(map);
4750 isl_space_free(dim);
4751 return NULL;
4754 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4755 __isl_take isl_space *dim)
4757 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4760 /* Compute the parameter domain of the given basic set.
4762 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4764 isl_space *space;
4765 unsigned n;
4767 if (isl_basic_set_is_params(bset))
4768 return bset;
4770 n = isl_basic_set_dim(bset, isl_dim_set);
4771 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4772 space = isl_basic_set_get_space(bset);
4773 space = isl_space_params(space);
4774 bset = isl_basic_set_reset_space(bset, space);
4775 return bset;
4778 /* Construct a zero-dimensional basic set with the given parameter domain.
4780 __isl_give isl_basic_set *isl_basic_set_from_params(
4781 __isl_take isl_basic_set *bset)
4783 isl_space *space;
4784 space = isl_basic_set_get_space(bset);
4785 space = isl_space_set_from_params(space);
4786 bset = isl_basic_set_reset_space(bset, space);
4787 return bset;
4790 /* Compute the parameter domain of the given set.
4792 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4794 isl_space *space;
4795 unsigned n;
4797 if (isl_set_is_params(set))
4798 return set;
4800 n = isl_set_dim(set, isl_dim_set);
4801 set = isl_set_project_out(set, isl_dim_set, 0, n);
4802 space = isl_set_get_space(set);
4803 space = isl_space_params(space);
4804 set = isl_set_reset_space(set, space);
4805 return set;
4808 /* Construct a zero-dimensional set with the given parameter domain.
4810 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4812 isl_space *space;
4813 space = isl_set_get_space(set);
4814 space = isl_space_set_from_params(space);
4815 set = isl_set_reset_space(set, space);
4816 return set;
4819 /* Compute the parameter domain of the given map.
4821 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4823 isl_space *space;
4824 unsigned n;
4826 n = isl_map_dim(map, isl_dim_in);
4827 map = isl_map_project_out(map, isl_dim_in, 0, n);
4828 n = isl_map_dim(map, isl_dim_out);
4829 map = isl_map_project_out(map, isl_dim_out, 0, n);
4830 space = isl_map_get_space(map);
4831 space = isl_space_params(space);
4832 map = isl_map_reset_space(map, space);
4833 return map;
4836 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4838 isl_space *dim;
4839 struct isl_basic_set *domain;
4840 unsigned n_in;
4841 unsigned n_out;
4843 if (!bmap)
4844 return NULL;
4845 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4847 n_in = isl_basic_map_n_in(bmap);
4848 n_out = isl_basic_map_n_out(bmap);
4849 domain = isl_basic_set_from_basic_map(bmap);
4850 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4852 domain = isl_basic_set_reset_space(domain, dim);
4854 return domain;
4857 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4859 if (!bmap)
4860 return -1;
4861 return isl_space_may_be_set(bmap->dim);
4864 /* Is this basic map actually a set?
4865 * Users should never call this function. Outside of isl,
4866 * the type should indicate whether something is a set or a map.
4868 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4870 if (!bmap)
4871 return -1;
4872 return isl_space_is_set(bmap->dim);
4875 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4877 if (!bmap)
4878 return NULL;
4879 if (isl_basic_map_is_set(bmap))
4880 return bmap;
4881 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4884 __isl_give isl_basic_map *isl_basic_map_domain_map(
4885 __isl_take isl_basic_map *bmap)
4887 int i, k;
4888 isl_space *dim;
4889 isl_basic_map *domain;
4890 int nparam, n_in, n_out;
4891 unsigned total;
4893 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4894 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4897 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4898 domain = isl_basic_map_universe(dim);
4900 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4901 bmap = isl_basic_map_apply_range(bmap, domain);
4902 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4904 total = isl_basic_map_total_dim(bmap);
4906 for (i = 0; i < n_in; ++i) {
4907 k = isl_basic_map_alloc_equality(bmap);
4908 if (k < 0)
4909 goto error;
4910 isl_seq_clr(bmap->eq[k], 1 + total);
4911 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4912 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4915 bmap = isl_basic_map_gauss(bmap, NULL);
4916 return isl_basic_map_finalize(bmap);
4917 error:
4918 isl_basic_map_free(bmap);
4919 return NULL;
4922 __isl_give isl_basic_map *isl_basic_map_range_map(
4923 __isl_take isl_basic_map *bmap)
4925 int i, k;
4926 isl_space *dim;
4927 isl_basic_map *range;
4928 int nparam, n_in, n_out;
4929 unsigned total;
4931 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4932 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4933 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4935 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4936 range = isl_basic_map_universe(dim);
4938 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4939 bmap = isl_basic_map_apply_range(bmap, range);
4940 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4942 total = isl_basic_map_total_dim(bmap);
4944 for (i = 0; i < n_out; ++i) {
4945 k = isl_basic_map_alloc_equality(bmap);
4946 if (k < 0)
4947 goto error;
4948 isl_seq_clr(bmap->eq[k], 1 + total);
4949 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4950 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4953 bmap = isl_basic_map_gauss(bmap, NULL);
4954 return isl_basic_map_finalize(bmap);
4955 error:
4956 isl_basic_map_free(bmap);
4957 return NULL;
4960 int isl_map_may_be_set(__isl_keep isl_map *map)
4962 if (!map)
4963 return -1;
4964 return isl_space_may_be_set(map->dim);
4967 /* Is this map actually a set?
4968 * Users should never call this function. Outside of isl,
4969 * the type should indicate whether something is a set or a map.
4971 int isl_map_is_set(__isl_keep isl_map *map)
4973 if (!map)
4974 return -1;
4975 return isl_space_is_set(map->dim);
4978 struct isl_set *isl_map_range(struct isl_map *map)
4980 int i;
4981 struct isl_set *set;
4983 if (!map)
4984 goto error;
4985 if (isl_map_is_set(map))
4986 return (isl_set *)map;
4988 map = isl_map_cow(map);
4989 if (!map)
4990 goto error;
4992 set = (struct isl_set *) map;
4993 set->dim = isl_space_range(set->dim);
4994 if (!set->dim)
4995 goto error;
4996 for (i = 0; i < map->n; ++i) {
4997 set->p[i] = isl_basic_map_range(map->p[i]);
4998 if (!set->p[i])
4999 goto error;
5001 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5002 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5003 return set;
5004 error:
5005 isl_map_free(map);
5006 return NULL;
5009 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5011 int i;
5013 map = isl_map_cow(map);
5014 if (!map)
5015 return NULL;
5017 map->dim = isl_space_domain_map(map->dim);
5018 if (!map->dim)
5019 goto error;
5020 for (i = 0; i < map->n; ++i) {
5021 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5022 if (!map->p[i])
5023 goto error;
5025 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5026 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5027 return map;
5028 error:
5029 isl_map_free(map);
5030 return NULL;
5033 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5035 int i;
5036 isl_space *range_dim;
5038 map = isl_map_cow(map);
5039 if (!map)
5040 return NULL;
5042 range_dim = isl_space_range(isl_map_get_space(map));
5043 range_dim = isl_space_from_range(range_dim);
5044 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5045 map->dim = isl_space_join(map->dim, range_dim);
5046 if (!map->dim)
5047 goto error;
5048 for (i = 0; i < map->n; ++i) {
5049 map->p[i] = isl_basic_map_range_map(map->p[i]);
5050 if (!map->p[i])
5051 goto error;
5053 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5054 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5055 return map;
5056 error:
5057 isl_map_free(map);
5058 return NULL;
5061 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5062 __isl_take isl_space *dim)
5064 int i;
5065 struct isl_map *map = NULL;
5067 set = isl_set_cow(set);
5068 if (!set || !dim)
5069 goto error;
5070 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5071 map = (struct isl_map *)set;
5072 for (i = 0; i < set->n; ++i) {
5073 map->p[i] = isl_basic_map_from_basic_set(
5074 set->p[i], isl_space_copy(dim));
5075 if (!map->p[i])
5076 goto error;
5078 isl_space_free(map->dim);
5079 map->dim = dim;
5080 return map;
5081 error:
5082 isl_space_free(dim);
5083 isl_set_free(set);
5084 return NULL;
5087 __isl_give isl_basic_map *isl_basic_map_from_domain(
5088 __isl_take isl_basic_set *bset)
5090 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5093 __isl_give isl_basic_map *isl_basic_map_from_range(
5094 __isl_take isl_basic_set *bset)
5096 isl_space *space;
5097 space = isl_basic_set_get_space(bset);
5098 space = isl_space_from_range(space);
5099 bset = isl_basic_set_reset_space(bset, space);
5100 return (isl_basic_map *)bset;
5103 /* Create a relation with the given set as range.
5104 * The domain of the created relation is a zero-dimensional
5105 * flat anonymous space.
5107 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5109 isl_space *space;
5110 space = isl_set_get_space(set);
5111 space = isl_space_from_range(space);
5112 set = isl_set_reset_space(set, space);
5113 return (struct isl_map *)set;
5116 /* Create a relation with the given set as domain.
5117 * The range of the created relation is a zero-dimensional
5118 * flat anonymous space.
5120 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5122 return isl_map_reverse(isl_map_from_range(set));
5125 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5126 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5128 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5131 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5132 __isl_take isl_set *range)
5134 return isl_map_apply_range(isl_map_reverse(domain), range);
5137 struct isl_set *isl_set_from_map(struct isl_map *map)
5139 int i;
5140 struct isl_set *set = NULL;
5142 if (!map)
5143 return NULL;
5144 map = isl_map_cow(map);
5145 if (!map)
5146 return NULL;
5147 map->dim = isl_space_as_set_space(map->dim);
5148 if (!map->dim)
5149 goto error;
5150 set = (struct isl_set *)map;
5151 for (i = 0; i < map->n; ++i) {
5152 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5153 if (!set->p[i])
5154 goto error;
5156 return set;
5157 error:
5158 isl_map_free(map);
5159 return NULL;
5162 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5163 unsigned flags)
5165 struct isl_map *map;
5167 if (!dim)
5168 return NULL;
5169 if (n < 0)
5170 isl_die(dim->ctx, isl_error_internal,
5171 "negative number of basic maps", goto error);
5172 map = isl_alloc(dim->ctx, struct isl_map,
5173 sizeof(struct isl_map) +
5174 (n - 1) * sizeof(struct isl_basic_map *));
5175 if (!map)
5176 goto error;
5178 map->ctx = dim->ctx;
5179 isl_ctx_ref(map->ctx);
5180 map->ref = 1;
5181 map->size = n;
5182 map->n = 0;
5183 map->dim = dim;
5184 map->flags = flags;
5185 return map;
5186 error:
5187 isl_space_free(dim);
5188 return NULL;
5191 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5192 unsigned nparam, unsigned in, unsigned out, int n,
5193 unsigned flags)
5195 struct isl_map *map;
5196 isl_space *dims;
5198 dims = isl_space_alloc(ctx, nparam, in, out);
5199 if (!dims)
5200 return NULL;
5202 map = isl_map_alloc_space(dims, n, flags);
5203 return map;
5206 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5208 struct isl_basic_map *bmap;
5209 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5210 bmap = isl_basic_map_set_to_empty(bmap);
5211 return bmap;
5214 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5216 struct isl_basic_set *bset;
5217 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5218 bset = isl_basic_set_set_to_empty(bset);
5219 return bset;
5222 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5224 struct isl_basic_map *bmap;
5225 if (!model)
5226 return NULL;
5227 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5228 bmap = isl_basic_map_set_to_empty(bmap);
5229 return bmap;
5232 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5234 struct isl_basic_map *bmap;
5235 if (!model)
5236 return NULL;
5237 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5238 bmap = isl_basic_map_set_to_empty(bmap);
5239 return bmap;
5242 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5244 struct isl_basic_set *bset;
5245 if (!model)
5246 return NULL;
5247 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5248 bset = isl_basic_set_set_to_empty(bset);
5249 return bset;
5252 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5254 struct isl_basic_map *bmap;
5255 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5256 bmap = isl_basic_map_finalize(bmap);
5257 return bmap;
5260 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5262 struct isl_basic_set *bset;
5263 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5264 bset = isl_basic_set_finalize(bset);
5265 return bset;
5268 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5270 int i;
5271 unsigned total = isl_space_dim(dim, isl_dim_all);
5272 isl_basic_map *bmap;
5274 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5275 for (i = 0; i < total; ++i) {
5276 int k = isl_basic_map_alloc_inequality(bmap);
5277 if (k < 0)
5278 goto error;
5279 isl_seq_clr(bmap->ineq[k], 1 + total);
5280 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5282 return bmap;
5283 error:
5284 isl_basic_map_free(bmap);
5285 return NULL;
5288 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5290 return isl_basic_map_nat_universe(dim);
5293 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5295 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5298 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5300 return isl_map_nat_universe(dim);
5303 __isl_give isl_basic_map *isl_basic_map_universe_like(
5304 __isl_keep isl_basic_map *model)
5306 if (!model)
5307 return NULL;
5308 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5311 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5313 if (!model)
5314 return NULL;
5315 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5318 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5319 __isl_keep isl_set *model)
5321 if (!model)
5322 return NULL;
5323 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5326 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5328 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5331 struct isl_map *isl_map_empty_like(struct isl_map *model)
5333 if (!model)
5334 return NULL;
5335 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5338 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5340 if (!model)
5341 return NULL;
5342 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5345 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5347 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5350 struct isl_set *isl_set_empty_like(struct isl_set *model)
5352 if (!model)
5353 return NULL;
5354 return isl_set_empty(isl_space_copy(model->dim));
5357 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5359 struct isl_map *map;
5360 if (!dim)
5361 return NULL;
5362 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5363 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5364 return map;
5367 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5369 struct isl_set *set;
5370 if (!dim)
5371 return NULL;
5372 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5373 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5374 return set;
5377 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5379 if (!model)
5380 return NULL;
5381 return isl_set_universe(isl_space_copy(model->dim));
5384 struct isl_map *isl_map_dup(struct isl_map *map)
5386 int i;
5387 struct isl_map *dup;
5389 if (!map)
5390 return NULL;
5391 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5392 for (i = 0; i < map->n; ++i)
5393 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5394 return dup;
5397 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5398 __isl_take isl_basic_map *bmap)
5400 if (!bmap || !map)
5401 goto error;
5402 if (isl_basic_map_plain_is_empty(bmap)) {
5403 isl_basic_map_free(bmap);
5404 return map;
5406 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5407 isl_assert(map->ctx, map->n < map->size, goto error);
5408 map->p[map->n] = bmap;
5409 map->n++;
5410 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5411 return map;
5412 error:
5413 if (map)
5414 isl_map_free(map);
5415 if (bmap)
5416 isl_basic_map_free(bmap);
5417 return NULL;
5420 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5422 int i;
5424 if (!map)
5425 return NULL;
5427 if (--map->ref > 0)
5428 return NULL;
5430 isl_ctx_deref(map->ctx);
5431 for (i = 0; i < map->n; ++i)
5432 isl_basic_map_free(map->p[i]);
5433 isl_space_free(map->dim);
5434 free(map);
5436 return NULL;
5439 struct isl_map *isl_map_extend(struct isl_map *base,
5440 unsigned nparam, unsigned n_in, unsigned n_out)
5442 int i;
5444 base = isl_map_cow(base);
5445 if (!base)
5446 return NULL;
5448 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5449 if (!base->dim)
5450 goto error;
5451 for (i = 0; i < base->n; ++i) {
5452 base->p[i] = isl_basic_map_extend_space(base->p[i],
5453 isl_space_copy(base->dim), 0, 0, 0);
5454 if (!base->p[i])
5455 goto error;
5457 return base;
5458 error:
5459 isl_map_free(base);
5460 return NULL;
5463 struct isl_set *isl_set_extend(struct isl_set *base,
5464 unsigned nparam, unsigned dim)
5466 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5467 nparam, 0, dim);
5470 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5471 struct isl_basic_map *bmap, unsigned pos, int value)
5473 int j;
5475 bmap = isl_basic_map_cow(bmap);
5476 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5477 j = isl_basic_map_alloc_equality(bmap);
5478 if (j < 0)
5479 goto error;
5480 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5481 isl_int_set_si(bmap->eq[j][pos], -1);
5482 isl_int_set_si(bmap->eq[j][0], value);
5483 bmap = isl_basic_map_simplify(bmap);
5484 return isl_basic_map_finalize(bmap);
5485 error:
5486 isl_basic_map_free(bmap);
5487 return NULL;
5490 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5491 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5493 int j;
5495 bmap = isl_basic_map_cow(bmap);
5496 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5497 j = isl_basic_map_alloc_equality(bmap);
5498 if (j < 0)
5499 goto error;
5500 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5501 isl_int_set_si(bmap->eq[j][pos], -1);
5502 isl_int_set(bmap->eq[j][0], value);
5503 bmap = isl_basic_map_simplify(bmap);
5504 return isl_basic_map_finalize(bmap);
5505 error:
5506 isl_basic_map_free(bmap);
5507 return NULL;
5510 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5511 enum isl_dim_type type, unsigned pos, int value)
5513 if (!bmap)
5514 return NULL;
5515 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5516 return isl_basic_map_fix_pos_si(bmap,
5517 isl_basic_map_offset(bmap, type) + pos, value);
5518 error:
5519 isl_basic_map_free(bmap);
5520 return NULL;
5523 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5524 enum isl_dim_type type, unsigned pos, isl_int value)
5526 if (!bmap)
5527 return NULL;
5528 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5529 return isl_basic_map_fix_pos(bmap,
5530 isl_basic_map_offset(bmap, type) + pos, value);
5531 error:
5532 isl_basic_map_free(bmap);
5533 return NULL;
5536 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5537 * to be equal to "v".
5539 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5540 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5542 if (!bmap || !v)
5543 goto error;
5544 if (!isl_val_is_int(v))
5545 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5546 "expecting integer value", goto error);
5547 if (pos >= isl_basic_map_dim(bmap, type))
5548 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5549 "index out of bounds", goto error);
5550 pos += isl_basic_map_offset(bmap, type);
5551 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5552 isl_val_free(v);
5553 return bmap;
5554 error:
5555 isl_basic_map_free(bmap);
5556 isl_val_free(v);
5557 return NULL;
5560 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5561 * to be equal to "v".
5563 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5564 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5566 return isl_basic_map_fix_val(bset, type, pos, v);
5569 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5570 enum isl_dim_type type, unsigned pos, int value)
5572 return (struct isl_basic_set *)
5573 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5574 type, pos, value);
5577 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5578 enum isl_dim_type type, unsigned pos, isl_int value)
5580 return (struct isl_basic_set *)
5581 isl_basic_map_fix((struct isl_basic_map *)bset,
5582 type, pos, value);
5585 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5586 unsigned input, int value)
5588 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5591 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5592 unsigned dim, int value)
5594 return (struct isl_basic_set *)
5595 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5596 isl_dim_set, dim, value);
5599 static int remove_if_empty(__isl_keep isl_map *map, int i)
5601 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5603 if (empty < 0)
5604 return -1;
5605 if (!empty)
5606 return 0;
5608 isl_basic_map_free(map->p[i]);
5609 if (i != map->n - 1) {
5610 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5611 map->p[i] = map->p[map->n - 1];
5613 map->n--;
5615 return 0;
5618 /* Perform "fn" on each basic map of "map", where we may not be holding
5619 * the only reference to "map".
5620 * In particular, "fn" should be a semantics preserving operation
5621 * that we want to apply to all copies of "map". We therefore need
5622 * to be careful not to modify "map" in a way that breaks "map"
5623 * in case anything goes wrong.
5625 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5626 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5628 struct isl_basic_map *bmap;
5629 int i;
5631 if (!map)
5632 return NULL;
5634 for (i = map->n - 1; i >= 0; --i) {
5635 bmap = isl_basic_map_copy(map->p[i]);
5636 bmap = fn(bmap);
5637 if (!bmap)
5638 goto error;
5639 isl_basic_map_free(map->p[i]);
5640 map->p[i] = bmap;
5641 if (remove_if_empty(map, i) < 0)
5642 goto error;
5645 return map;
5646 error:
5647 isl_map_free(map);
5648 return NULL;
5651 struct isl_map *isl_map_fix_si(struct isl_map *map,
5652 enum isl_dim_type type, unsigned pos, int value)
5654 int i;
5656 map = isl_map_cow(map);
5657 if (!map)
5658 return NULL;
5660 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5661 for (i = map->n - 1; i >= 0; --i) {
5662 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5663 if (remove_if_empty(map, i) < 0)
5664 goto error;
5666 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5667 return map;
5668 error:
5669 isl_map_free(map);
5670 return NULL;
5673 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5674 enum isl_dim_type type, unsigned pos, int value)
5676 return (struct isl_set *)
5677 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5680 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5681 enum isl_dim_type type, unsigned pos, isl_int value)
5683 int i;
5685 map = isl_map_cow(map);
5686 if (!map)
5687 return NULL;
5689 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5690 for (i = 0; i < map->n; ++i) {
5691 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5692 if (!map->p[i])
5693 goto error;
5695 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5696 return map;
5697 error:
5698 isl_map_free(map);
5699 return NULL;
5702 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5703 enum isl_dim_type type, unsigned pos, isl_int value)
5705 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5708 /* Fix the value of the variable at position "pos" of type "type" of "map"
5709 * to be equal to "v".
5711 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5712 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5714 int i;
5716 map = isl_map_cow(map);
5717 if (!map || !v)
5718 goto error;
5720 if (!isl_val_is_int(v))
5721 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5722 "expecting integer value", goto error);
5723 if (pos >= isl_map_dim(map, type))
5724 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5725 "index out of bounds", goto error);
5726 for (i = map->n - 1; i >= 0; --i) {
5727 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5728 isl_val_copy(v));
5729 if (remove_if_empty(map, i) < 0)
5730 goto error;
5732 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5733 isl_val_free(v);
5734 return map;
5735 error:
5736 isl_map_free(map);
5737 isl_val_free(v);
5738 return NULL;
5741 /* Fix the value of the variable at position "pos" of type "type" of "set"
5742 * to be equal to "v".
5744 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5745 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5747 return isl_map_fix_val(set, type, pos, v);
5750 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5751 unsigned input, int value)
5753 return isl_map_fix_si(map, isl_dim_in, input, value);
5756 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5758 return (struct isl_set *)
5759 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5762 static __isl_give isl_basic_map *basic_map_bound_si(
5763 __isl_take isl_basic_map *bmap,
5764 enum isl_dim_type type, unsigned pos, int value, int upper)
5766 int j;
5768 if (!bmap)
5769 return NULL;
5770 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5771 pos += isl_basic_map_offset(bmap, type);
5772 bmap = isl_basic_map_cow(bmap);
5773 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5774 j = isl_basic_map_alloc_inequality(bmap);
5775 if (j < 0)
5776 goto error;
5777 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5778 if (upper) {
5779 isl_int_set_si(bmap->ineq[j][pos], -1);
5780 isl_int_set_si(bmap->ineq[j][0], value);
5781 } else {
5782 isl_int_set_si(bmap->ineq[j][pos], 1);
5783 isl_int_set_si(bmap->ineq[j][0], -value);
5785 bmap = isl_basic_map_simplify(bmap);
5786 return isl_basic_map_finalize(bmap);
5787 error:
5788 isl_basic_map_free(bmap);
5789 return NULL;
5792 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5793 __isl_take isl_basic_map *bmap,
5794 enum isl_dim_type type, unsigned pos, int value)
5796 return basic_map_bound_si(bmap, type, pos, value, 0);
5799 /* Constrain the values of the given dimension to be no greater than "value".
5801 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5802 __isl_take isl_basic_map *bmap,
5803 enum isl_dim_type type, unsigned pos, int value)
5805 return basic_map_bound_si(bmap, type, pos, value, 1);
5808 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5809 unsigned dim, isl_int value)
5811 int j;
5813 bset = isl_basic_set_cow(bset);
5814 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5815 j = isl_basic_set_alloc_inequality(bset);
5816 if (j < 0)
5817 goto error;
5818 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5819 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5820 isl_int_neg(bset->ineq[j][0], value);
5821 bset = isl_basic_set_simplify(bset);
5822 return isl_basic_set_finalize(bset);
5823 error:
5824 isl_basic_set_free(bset);
5825 return NULL;
5828 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5829 enum isl_dim_type type, unsigned pos, int value, int upper)
5831 int i;
5833 map = isl_map_cow(map);
5834 if (!map)
5835 return NULL;
5837 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5838 for (i = 0; i < map->n; ++i) {
5839 map->p[i] = basic_map_bound_si(map->p[i],
5840 type, pos, value, upper);
5841 if (!map->p[i])
5842 goto error;
5844 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5845 return map;
5846 error:
5847 isl_map_free(map);
5848 return NULL;
5851 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5852 enum isl_dim_type type, unsigned pos, int value)
5854 return map_bound_si(map, type, pos, value, 0);
5857 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5858 enum isl_dim_type type, unsigned pos, int value)
5860 return map_bound_si(map, type, pos, value, 1);
5863 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5864 enum isl_dim_type type, unsigned pos, int value)
5866 return (struct isl_set *)
5867 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5870 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5871 enum isl_dim_type type, unsigned pos, int value)
5873 return isl_map_upper_bound_si(set, type, pos, value);
5876 /* Bound the given variable of "bmap" from below (or above is "upper"
5877 * is set) to "value".
5879 static __isl_give isl_basic_map *basic_map_bound(
5880 __isl_take isl_basic_map *bmap,
5881 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5883 int j;
5885 if (!bmap)
5886 return NULL;
5887 if (pos >= isl_basic_map_dim(bmap, type))
5888 isl_die(bmap->ctx, isl_error_invalid,
5889 "index out of bounds", goto error);
5890 pos += isl_basic_map_offset(bmap, type);
5891 bmap = isl_basic_map_cow(bmap);
5892 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5893 j = isl_basic_map_alloc_inequality(bmap);
5894 if (j < 0)
5895 goto error;
5896 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5897 if (upper) {
5898 isl_int_set_si(bmap->ineq[j][pos], -1);
5899 isl_int_set(bmap->ineq[j][0], value);
5900 } else {
5901 isl_int_set_si(bmap->ineq[j][pos], 1);
5902 isl_int_neg(bmap->ineq[j][0], value);
5904 bmap = isl_basic_map_simplify(bmap);
5905 return isl_basic_map_finalize(bmap);
5906 error:
5907 isl_basic_map_free(bmap);
5908 return NULL;
5911 /* Bound the given variable of "map" from below (or above is "upper"
5912 * is set) to "value".
5914 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5915 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5917 int i;
5919 map = isl_map_cow(map);
5920 if (!map)
5921 return NULL;
5923 if (pos >= isl_map_dim(map, type))
5924 isl_die(map->ctx, isl_error_invalid,
5925 "index out of bounds", goto error);
5926 for (i = map->n - 1; i >= 0; --i) {
5927 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5928 if (remove_if_empty(map, i) < 0)
5929 goto error;
5931 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5932 return map;
5933 error:
5934 isl_map_free(map);
5935 return NULL;
5938 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5939 enum isl_dim_type type, unsigned pos, isl_int value)
5941 return map_bound(map, type, pos, value, 0);
5944 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5945 enum isl_dim_type type, unsigned pos, isl_int value)
5947 return map_bound(map, type, pos, value, 1);
5950 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5951 enum isl_dim_type type, unsigned pos, isl_int value)
5953 return isl_map_lower_bound(set, type, pos, value);
5956 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5957 enum isl_dim_type type, unsigned pos, isl_int value)
5959 return isl_map_upper_bound(set, type, pos, value);
5962 /* Force the values of the variable at position "pos" of type "type" of "set"
5963 * to be no smaller than "value".
5965 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5966 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5968 if (!value)
5969 goto error;
5970 if (!isl_val_is_int(value))
5971 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5972 "expecting integer value", goto error);
5973 set = isl_set_lower_bound(set, type, pos, value->n);
5974 isl_val_free(value);
5975 return set;
5976 error:
5977 isl_val_free(value);
5978 isl_set_free(set);
5979 return NULL;
5982 /* Force the values of the variable at position "pos" of type "type" of "set"
5983 * to be no greater than "value".
5985 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5986 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5988 if (!value)
5989 goto error;
5990 if (!isl_val_is_int(value))
5991 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5992 "expecting integer value", goto error);
5993 set = isl_set_upper_bound(set, type, pos, value->n);
5994 isl_val_free(value);
5995 return set;
5996 error:
5997 isl_val_free(value);
5998 isl_set_free(set);
5999 return NULL;
6002 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6003 isl_int value)
6005 int i;
6007 set = isl_set_cow(set);
6008 if (!set)
6009 return NULL;
6011 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6012 for (i = 0; i < set->n; ++i) {
6013 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6014 if (!set->p[i])
6015 goto error;
6017 return set;
6018 error:
6019 isl_set_free(set);
6020 return NULL;
6023 struct isl_map *isl_map_reverse(struct isl_map *map)
6025 int i;
6027 map = isl_map_cow(map);
6028 if (!map)
6029 return NULL;
6031 map->dim = isl_space_reverse(map->dim);
6032 if (!map->dim)
6033 goto error;
6034 for (i = 0; i < map->n; ++i) {
6035 map->p[i] = isl_basic_map_reverse(map->p[i]);
6036 if (!map->p[i])
6037 goto error;
6039 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6040 return map;
6041 error:
6042 isl_map_free(map);
6043 return NULL;
6046 static struct isl_map *isl_basic_map_partial_lexopt(
6047 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6048 struct isl_set **empty, int max)
6050 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6053 struct isl_map *isl_basic_map_partial_lexmax(
6054 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6055 struct isl_set **empty)
6057 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6060 struct isl_map *isl_basic_map_partial_lexmin(
6061 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6062 struct isl_set **empty)
6064 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6067 struct isl_set *isl_basic_set_partial_lexmin(
6068 struct isl_basic_set *bset, struct isl_basic_set *dom,
6069 struct isl_set **empty)
6071 return (struct isl_set *)
6072 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6073 dom, empty);
6076 struct isl_set *isl_basic_set_partial_lexmax(
6077 struct isl_basic_set *bset, struct isl_basic_set *dom,
6078 struct isl_set **empty)
6080 return (struct isl_set *)
6081 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6082 dom, empty);
6085 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6086 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6087 __isl_give isl_set **empty)
6089 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6092 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6093 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6094 __isl_give isl_set **empty)
6096 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6099 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6100 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6101 __isl_give isl_set **empty)
6103 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6106 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6107 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6108 __isl_give isl_set **empty)
6110 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6113 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6114 __isl_take isl_basic_map *bmap, int max)
6116 isl_basic_set *dom = NULL;
6117 isl_space *dom_space;
6119 if (!bmap)
6120 goto error;
6121 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6122 dom = isl_basic_set_universe(dom_space);
6123 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6124 error:
6125 isl_basic_map_free(bmap);
6126 return NULL;
6129 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6130 __isl_take isl_basic_map *bmap)
6132 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6135 #undef TYPE
6136 #define TYPE isl_pw_multi_aff
6137 #undef SUFFIX
6138 #define SUFFIX _pw_multi_aff
6139 #undef EMPTY
6140 #define EMPTY isl_pw_multi_aff_empty
6141 #undef ADD
6142 #define ADD isl_pw_multi_aff_union_add
6143 #include "isl_map_lexopt_templ.c"
6145 /* Given a map "map", compute the lexicographically minimal
6146 * (or maximal) image element for each domain element in dom,
6147 * in the form of an isl_pw_multi_aff.
6148 * Set *empty to those elements in dom that do not have an image element.
6150 * We first compute the lexicographically minimal or maximal element
6151 * in the first basic map. This results in a partial solution "res"
6152 * and a subset "todo" of dom that still need to be handled.
6153 * We then consider each of the remaining maps in "map" and successively
6154 * update both "res" and "todo".
6156 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6157 __isl_take isl_map *map, __isl_take isl_set *dom,
6158 __isl_give isl_set **empty, int max)
6160 int i;
6161 isl_pw_multi_aff *res;
6162 isl_set *todo;
6164 if (!map || !dom)
6165 goto error;
6167 if (isl_map_plain_is_empty(map)) {
6168 if (empty)
6169 *empty = dom;
6170 else
6171 isl_set_free(dom);
6172 return isl_pw_multi_aff_from_map(map);
6175 res = basic_map_partial_lexopt_pw_multi_aff(
6176 isl_basic_map_copy(map->p[0]),
6177 isl_set_copy(dom), &todo, max);
6179 for (i = 1; i < map->n; ++i) {
6180 isl_pw_multi_aff *res_i;
6181 isl_set *todo_i;
6183 res_i = basic_map_partial_lexopt_pw_multi_aff(
6184 isl_basic_map_copy(map->p[i]),
6185 isl_set_copy(dom), &todo_i, max);
6187 if (max)
6188 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6189 else
6190 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6192 todo = isl_set_intersect(todo, todo_i);
6195 isl_set_free(dom);
6196 isl_map_free(map);
6198 if (empty)
6199 *empty = todo;
6200 else
6201 isl_set_free(todo);
6203 return res;
6204 error:
6205 if (empty)
6206 *empty = NULL;
6207 isl_set_free(dom);
6208 isl_map_free(map);
6209 return NULL;
6212 #undef TYPE
6213 #define TYPE isl_map
6214 #undef SUFFIX
6215 #define SUFFIX
6216 #undef EMPTY
6217 #define EMPTY isl_map_empty
6218 #undef ADD
6219 #define ADD isl_map_union_disjoint
6220 #include "isl_map_lexopt_templ.c"
6222 /* Given a map "map", compute the lexicographically minimal
6223 * (or maximal) image element for each domain element in dom.
6224 * Set *empty to those elements in dom that do not have an image element.
6226 * We first compute the lexicographically minimal or maximal element
6227 * in the first basic map. This results in a partial solution "res"
6228 * and a subset "todo" of dom that still need to be handled.
6229 * We then consider each of the remaining maps in "map" and successively
6230 * update both "res" and "todo".
6232 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6233 * Assume we are computing the lexicographical maximum.
6234 * We first compute the lexicographically maximal element in basic map i.
6235 * This results in a partial solution res_i and a subset todo_i.
6236 * Then we combine these results with those obtain for the first k basic maps
6237 * to obtain a result that is valid for the first k+1 basic maps.
6238 * In particular, the set where there is no solution is the set where
6239 * there is no solution for the first k basic maps and also no solution
6240 * for the ith basic map, i.e.,
6242 * todo^i = todo^k * todo_i
6244 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6245 * solutions, arbitrarily breaking ties in favor of res^k.
6246 * That is, when res^k(a) >= res_i(a), we pick res^k and
6247 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6248 * the lexicographic order.)
6249 * In practice, we compute
6251 * res^k * (res_i . "<=")
6253 * and
6255 * res_i * (res^k . "<")
6257 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6258 * where only one of res^k and res_i provides a solution and we simply pick
6259 * that one, i.e.,
6261 * res^k * todo_i
6262 * and
6263 * res_i * todo^k
6265 * Note that we only compute these intersections when dom(res^k) intersects
6266 * dom(res_i). Otherwise, the only effect of these intersections is to
6267 * potentially break up res^k and res_i into smaller pieces.
6268 * We want to avoid such splintering as much as possible.
6269 * In fact, an earlier implementation of this function would look for
6270 * better results in the domain of res^k and for extra results in todo^k,
6271 * but this would always result in a splintering according to todo^k,
6272 * even when the domain of basic map i is disjoint from the domains of
6273 * the previous basic maps.
6275 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6276 __isl_take isl_map *map, __isl_take isl_set *dom,
6277 __isl_give isl_set **empty, int max)
6279 int i;
6280 struct isl_map *res;
6281 struct isl_set *todo;
6283 if (!map || !dom)
6284 goto error;
6286 if (isl_map_plain_is_empty(map)) {
6287 if (empty)
6288 *empty = dom;
6289 else
6290 isl_set_free(dom);
6291 return map;
6294 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6295 isl_set_copy(dom), &todo, max);
6297 for (i = 1; i < map->n; ++i) {
6298 isl_map *lt, *le;
6299 isl_map *res_i;
6300 isl_set *todo_i;
6301 isl_space *dim = isl_space_range(isl_map_get_space(res));
6303 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6304 isl_set_copy(dom), &todo_i, max);
6306 if (max) {
6307 lt = isl_map_lex_lt(isl_space_copy(dim));
6308 le = isl_map_lex_le(dim);
6309 } else {
6310 lt = isl_map_lex_gt(isl_space_copy(dim));
6311 le = isl_map_lex_ge(dim);
6313 lt = isl_map_apply_range(isl_map_copy(res), lt);
6314 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6315 le = isl_map_apply_range(isl_map_copy(res_i), le);
6316 le = isl_map_intersect(le, isl_map_copy(res));
6318 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6319 res = isl_map_intersect_domain(res,
6320 isl_set_copy(todo_i));
6321 res_i = isl_map_intersect_domain(res_i,
6322 isl_set_copy(todo));
6325 res = isl_map_union_disjoint(res, res_i);
6326 res = isl_map_union_disjoint(res, lt);
6327 res = isl_map_union_disjoint(res, le);
6329 todo = isl_set_intersect(todo, todo_i);
6332 isl_set_free(dom);
6333 isl_map_free(map);
6335 if (empty)
6336 *empty = todo;
6337 else
6338 isl_set_free(todo);
6340 return res;
6341 error:
6342 if (empty)
6343 *empty = NULL;
6344 isl_set_free(dom);
6345 isl_map_free(map);
6346 return NULL;
6349 __isl_give isl_map *isl_map_partial_lexmax(
6350 __isl_take isl_map *map, __isl_take isl_set *dom,
6351 __isl_give isl_set **empty)
6353 return isl_map_partial_lexopt(map, dom, empty, 1);
6356 __isl_give isl_map *isl_map_partial_lexmin(
6357 __isl_take isl_map *map, __isl_take isl_set *dom,
6358 __isl_give isl_set **empty)
6360 return isl_map_partial_lexopt(map, dom, empty, 0);
6363 __isl_give isl_set *isl_set_partial_lexmin(
6364 __isl_take isl_set *set, __isl_take isl_set *dom,
6365 __isl_give isl_set **empty)
6367 return (struct isl_set *)
6368 isl_map_partial_lexmin((struct isl_map *)set,
6369 dom, empty);
6372 __isl_give isl_set *isl_set_partial_lexmax(
6373 __isl_take isl_set *set, __isl_take isl_set *dom,
6374 __isl_give isl_set **empty)
6376 return (struct isl_set *)
6377 isl_map_partial_lexmax((struct isl_map *)set,
6378 dom, empty);
6381 /* Compute the lexicographic minimum (or maximum if "max" is set)
6382 * of "bmap" over its domain.
6384 * Since we are not interested in the part of the domain space where
6385 * there is no solution, we initialize the domain to those constraints
6386 * of "bmap" that only involve the parameters and the input dimensions.
6387 * This relieves the parametric programming engine from detecting those
6388 * inequalities and transferring them to the context. More importantly,
6389 * it ensures that those inequalities are transferred first and not
6390 * intermixed with inequalities that actually split the domain.
6392 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6394 int n_div;
6395 int n_out;
6396 isl_basic_map *copy;
6397 isl_basic_set *dom;
6399 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6400 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6401 copy = isl_basic_map_copy(bmap);
6402 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6403 isl_dim_div, 0, n_div);
6404 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6405 isl_dim_out, 0, n_out);
6406 dom = isl_basic_map_domain(copy);
6407 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6410 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6412 return isl_basic_map_lexopt(bmap, 0);
6415 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6417 return isl_basic_map_lexopt(bmap, 1);
6420 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6422 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6425 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6427 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6430 /* Extract the first and only affine expression from list
6431 * and then add it to *pwaff with the given dom.
6432 * This domain is known to be disjoint from other domains
6433 * because of the way isl_basic_map_foreach_lexmax works.
6435 static int update_dim_opt(__isl_take isl_basic_set *dom,
6436 __isl_take isl_aff_list *list, void *user)
6438 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6439 isl_aff *aff;
6440 isl_pw_aff **pwaff = user;
6441 isl_pw_aff *pwaff_i;
6443 if (!list)
6444 goto error;
6445 if (isl_aff_list_n_aff(list) != 1)
6446 isl_die(ctx, isl_error_internal,
6447 "expecting single element list", goto error);
6449 aff = isl_aff_list_get_aff(list, 0);
6450 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6452 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6454 isl_aff_list_free(list);
6456 return 0;
6457 error:
6458 isl_basic_set_free(dom);
6459 isl_aff_list_free(list);
6460 return -1;
6463 /* Given a basic map with one output dimension, compute the minimum or
6464 * maximum of that dimension as an isl_pw_aff.
6466 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6467 * call update_dim_opt on each leaf of the result.
6469 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6470 int max)
6472 isl_space *dim = isl_basic_map_get_space(bmap);
6473 isl_pw_aff *pwaff;
6474 int r;
6476 dim = isl_space_from_domain(isl_space_domain(dim));
6477 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6478 pwaff = isl_pw_aff_empty(dim);
6480 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6481 if (r < 0)
6482 return isl_pw_aff_free(pwaff);
6484 return pwaff;
6487 /* Compute the minimum or maximum of the given output dimension
6488 * as a function of the parameters and the input dimensions,
6489 * but independently of the other output dimensions.
6491 * We first project out the other output dimension and then compute
6492 * the "lexicographic" maximum in each basic map, combining the results
6493 * using isl_pw_aff_union_max.
6495 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6496 int max)
6498 int i;
6499 isl_pw_aff *pwaff;
6500 unsigned n_out;
6502 n_out = isl_map_dim(map, isl_dim_out);
6503 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6504 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6505 if (!map)
6506 return NULL;
6508 if (map->n == 0) {
6509 isl_space *dim = isl_map_get_space(map);
6510 isl_map_free(map);
6511 return isl_pw_aff_empty(dim);
6514 pwaff = basic_map_dim_opt(map->p[0], max);
6515 for (i = 1; i < map->n; ++i) {
6516 isl_pw_aff *pwaff_i;
6518 pwaff_i = basic_map_dim_opt(map->p[i], max);
6519 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6522 isl_map_free(map);
6524 return pwaff;
6527 /* Compute the maximum of the given output dimension as a function of the
6528 * parameters and input dimensions, but independently of
6529 * the other output dimensions.
6531 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6533 return map_dim_opt(map, pos, 1);
6536 /* Compute the minimum or maximum of the given set dimension
6537 * as a function of the parameters,
6538 * but independently of the other set dimensions.
6540 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6541 int max)
6543 return map_dim_opt(set, pos, max);
6546 /* Compute the maximum of the given set dimension as a function of the
6547 * parameters, but independently of the other set dimensions.
6549 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6551 return set_dim_opt(set, pos, 1);
6554 /* Compute the minimum of the given set dimension as a function of the
6555 * parameters, but independently of the other set dimensions.
6557 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6559 return set_dim_opt(set, pos, 0);
6562 /* Apply a preimage specified by "mat" on the parameters of "bset".
6563 * bset is assumed to have only parameters and divs.
6565 static struct isl_basic_set *basic_set_parameter_preimage(
6566 struct isl_basic_set *bset, struct isl_mat *mat)
6568 unsigned nparam;
6570 if (!bset || !mat)
6571 goto error;
6573 bset->dim = isl_space_cow(bset->dim);
6574 if (!bset->dim)
6575 goto error;
6577 nparam = isl_basic_set_dim(bset, isl_dim_param);
6579 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6581 bset->dim->nparam = 0;
6582 bset->dim->n_out = nparam;
6583 bset = isl_basic_set_preimage(bset, mat);
6584 if (bset) {
6585 bset->dim->nparam = bset->dim->n_out;
6586 bset->dim->n_out = 0;
6588 return bset;
6589 error:
6590 isl_mat_free(mat);
6591 isl_basic_set_free(bset);
6592 return NULL;
6595 /* Apply a preimage specified by "mat" on the parameters of "set".
6596 * set is assumed to have only parameters and divs.
6598 static struct isl_set *set_parameter_preimage(
6599 struct isl_set *set, struct isl_mat *mat)
6601 isl_space *dim = NULL;
6602 unsigned nparam;
6604 if (!set || !mat)
6605 goto error;
6607 dim = isl_space_copy(set->dim);
6608 dim = isl_space_cow(dim);
6609 if (!dim)
6610 goto error;
6612 nparam = isl_set_dim(set, isl_dim_param);
6614 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6616 dim->nparam = 0;
6617 dim->n_out = nparam;
6618 isl_set_reset_space(set, dim);
6619 set = isl_set_preimage(set, mat);
6620 if (!set)
6621 goto error2;
6622 dim = isl_space_copy(set->dim);
6623 dim = isl_space_cow(dim);
6624 if (!dim)
6625 goto error2;
6626 dim->nparam = dim->n_out;
6627 dim->n_out = 0;
6628 isl_set_reset_space(set, dim);
6629 return set;
6630 error:
6631 isl_space_free(dim);
6632 isl_mat_free(mat);
6633 error2:
6634 isl_set_free(set);
6635 return NULL;
6638 /* Intersect the basic set "bset" with the affine space specified by the
6639 * equalities in "eq".
6641 static struct isl_basic_set *basic_set_append_equalities(
6642 struct isl_basic_set *bset, struct isl_mat *eq)
6644 int i, k;
6645 unsigned len;
6647 if (!bset || !eq)
6648 goto error;
6650 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6651 eq->n_row, 0);
6652 if (!bset)
6653 goto error;
6655 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6656 for (i = 0; i < eq->n_row; ++i) {
6657 k = isl_basic_set_alloc_equality(bset);
6658 if (k < 0)
6659 goto error;
6660 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6661 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6663 isl_mat_free(eq);
6665 bset = isl_basic_set_gauss(bset, NULL);
6666 bset = isl_basic_set_finalize(bset);
6668 return bset;
6669 error:
6670 isl_mat_free(eq);
6671 isl_basic_set_free(bset);
6672 return NULL;
6675 /* Intersect the set "set" with the affine space specified by the
6676 * equalities in "eq".
6678 static struct isl_set *set_append_equalities(struct isl_set *set,
6679 struct isl_mat *eq)
6681 int i;
6683 if (!set || !eq)
6684 goto error;
6686 for (i = 0; i < set->n; ++i) {
6687 set->p[i] = basic_set_append_equalities(set->p[i],
6688 isl_mat_copy(eq));
6689 if (!set->p[i])
6690 goto error;
6692 isl_mat_free(eq);
6693 return set;
6694 error:
6695 isl_mat_free(eq);
6696 isl_set_free(set);
6697 return NULL;
6700 /* Given a basic set "bset" that only involves parameters and existentially
6701 * quantified variables, return the index of the first equality
6702 * that only involves parameters. If there is no such equality then
6703 * return bset->n_eq.
6705 * This function assumes that isl_basic_set_gauss has been called on "bset".
6707 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6709 int i, j;
6710 unsigned nparam, n_div;
6712 if (!bset)
6713 return -1;
6715 nparam = isl_basic_set_dim(bset, isl_dim_param);
6716 n_div = isl_basic_set_dim(bset, isl_dim_div);
6718 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6719 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6720 ++i;
6723 return i;
6726 /* Compute an explicit representation for the existentially quantified
6727 * variables in "bset" by computing the "minimal value" of the set
6728 * variables. Since there are no set variables, the computation of
6729 * the minimal value essentially computes an explicit representation
6730 * of the non-empty part(s) of "bset".
6732 * The input only involves parameters and existentially quantified variables.
6733 * All equalities among parameters have been removed.
6735 * Since the existentially quantified variables in the result are in general
6736 * going to be different from those in the input, we first replace
6737 * them by the minimal number of variables based on their equalities.
6738 * This should simplify the parametric integer programming.
6740 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6742 isl_morph *morph1, *morph2;
6743 isl_set *set;
6744 unsigned n;
6746 if (!bset)
6747 return NULL;
6748 if (bset->n_eq == 0)
6749 return isl_basic_set_lexmin(bset);
6751 morph1 = isl_basic_set_parameter_compression(bset);
6752 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6753 bset = isl_basic_set_lift(bset);
6754 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6755 bset = isl_morph_basic_set(morph2, bset);
6756 n = isl_basic_set_dim(bset, isl_dim_set);
6757 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6759 set = isl_basic_set_lexmin(bset);
6761 set = isl_morph_set(isl_morph_inverse(morph1), set);
6763 return set;
6766 /* Project the given basic set onto its parameter domain, possibly introducing
6767 * new, explicit, existential variables in the constraints.
6768 * The input has parameters and (possibly implicit) existential variables.
6769 * The output has the same parameters, but only
6770 * explicit existentially quantified variables.
6772 * The actual projection is performed by pip, but pip doesn't seem
6773 * to like equalities very much, so we first remove the equalities
6774 * among the parameters by performing a variable compression on
6775 * the parameters. Afterward, an inverse transformation is performed
6776 * and the equalities among the parameters are inserted back in.
6778 * The variable compression on the parameters may uncover additional
6779 * equalities that were only implicit before. We therefore check
6780 * if there are any new parameter equalities in the result and
6781 * if so recurse. The removal of parameter equalities is required
6782 * for the parameter compression performed by base_compute_divs.
6784 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6786 int i;
6787 struct isl_mat *eq;
6788 struct isl_mat *T, *T2;
6789 struct isl_set *set;
6790 unsigned nparam;
6792 bset = isl_basic_set_cow(bset);
6793 if (!bset)
6794 return NULL;
6796 if (bset->n_eq == 0)
6797 return base_compute_divs(bset);
6799 bset = isl_basic_set_gauss(bset, NULL);
6800 if (!bset)
6801 return NULL;
6802 if (isl_basic_set_plain_is_empty(bset))
6803 return isl_set_from_basic_set(bset);
6805 i = first_parameter_equality(bset);
6806 if (i == bset->n_eq)
6807 return base_compute_divs(bset);
6809 nparam = isl_basic_set_dim(bset, isl_dim_param);
6810 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6811 0, 1 + nparam);
6812 eq = isl_mat_cow(eq);
6813 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6814 if (T && T->n_col == 0) {
6815 isl_mat_free(T);
6816 isl_mat_free(T2);
6817 isl_mat_free(eq);
6818 bset = isl_basic_set_set_to_empty(bset);
6819 return isl_set_from_basic_set(bset);
6821 bset = basic_set_parameter_preimage(bset, T);
6823 i = first_parameter_equality(bset);
6824 if (!bset)
6825 set = NULL;
6826 else if (i == bset->n_eq)
6827 set = base_compute_divs(bset);
6828 else
6829 set = parameter_compute_divs(bset);
6830 set = set_parameter_preimage(set, T2);
6831 set = set_append_equalities(set, eq);
6832 return set;
6835 /* Insert the divs from "ls" before those of "bmap".
6837 * The number of columns is not changed, which means that the last
6838 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6839 * The caller is responsible for removing the same number of dimensions
6840 * from the space of "bmap".
6842 static __isl_give isl_basic_map *insert_divs_from_local_space(
6843 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6845 int i;
6846 int n_div;
6847 int old_n_div;
6849 n_div = isl_local_space_dim(ls, isl_dim_div);
6850 if (n_div == 0)
6851 return bmap;
6853 old_n_div = bmap->n_div;
6854 bmap = insert_div_rows(bmap, n_div);
6855 if (!bmap)
6856 return NULL;
6858 for (i = 0; i < n_div; ++i) {
6859 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6860 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6863 return bmap;
6866 /* Replace the space of "bmap" by the space and divs of "ls".
6868 * If "ls" has any divs, then we simplify the result since we may
6869 * have discovered some additional equalities that could simplify
6870 * the div expressions.
6872 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6873 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6875 int n_div;
6877 bmap = isl_basic_map_cow(bmap);
6878 if (!bmap || !ls)
6879 goto error;
6881 n_div = isl_local_space_dim(ls, isl_dim_div);
6882 bmap = insert_divs_from_local_space(bmap, ls);
6883 if (!bmap)
6884 goto error;
6886 isl_space_free(bmap->dim);
6887 bmap->dim = isl_local_space_get_space(ls);
6888 if (!bmap->dim)
6889 goto error;
6891 isl_local_space_free(ls);
6892 if (n_div > 0)
6893 bmap = isl_basic_map_simplify(bmap);
6894 bmap = isl_basic_map_finalize(bmap);
6895 return bmap;
6896 error:
6897 isl_basic_map_free(bmap);
6898 isl_local_space_free(ls);
6899 return NULL;
6902 /* Replace the space of "map" by the space and divs of "ls".
6904 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6905 __isl_take isl_local_space *ls)
6907 int i;
6909 map = isl_map_cow(map);
6910 if (!map || !ls)
6911 goto error;
6913 for (i = 0; i < map->n; ++i) {
6914 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6915 isl_local_space_copy(ls));
6916 if (!map->p[i])
6917 goto error;
6919 isl_space_free(map->dim);
6920 map->dim = isl_local_space_get_space(ls);
6921 if (!map->dim)
6922 goto error;
6924 isl_local_space_free(ls);
6925 return map;
6926 error:
6927 isl_local_space_free(ls);
6928 isl_map_free(map);
6929 return NULL;
6932 /* Compute an explicit representation for the existentially
6933 * quantified variables for which do not know any explicit representation yet.
6935 * We first sort the existentially quantified variables so that the
6936 * existentially quantified variables for which we already have an explicit
6937 * representation are placed before those for which we do not.
6938 * The input dimensions, the output dimensions and the existentially
6939 * quantified variables for which we already have an explicit
6940 * representation are then turned into parameters.
6941 * compute_divs returns a map with the same parameters and
6942 * no input or output dimensions and the dimension specification
6943 * is reset to that of the input, including the existentially quantified
6944 * variables for which we already had an explicit representation.
6946 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6948 struct isl_basic_set *bset;
6949 struct isl_set *set;
6950 struct isl_map *map;
6951 isl_space *dim;
6952 isl_local_space *ls;
6953 unsigned nparam;
6954 unsigned n_in;
6955 unsigned n_out;
6956 unsigned n_known;
6957 int i;
6959 bmap = isl_basic_map_sort_divs(bmap);
6960 bmap = isl_basic_map_cow(bmap);
6961 if (!bmap)
6962 return NULL;
6964 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6965 if (isl_int_is_zero(bmap->div[n_known][0]))
6966 break;
6968 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6969 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6970 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6971 dim = isl_space_set_alloc(bmap->ctx,
6972 nparam + n_in + n_out + n_known, 0);
6973 if (!dim)
6974 goto error;
6976 ls = isl_basic_map_get_local_space(bmap);
6977 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6978 n_known, bmap->n_div - n_known);
6979 if (n_known > 0) {
6980 for (i = n_known; i < bmap->n_div; ++i)
6981 swap_div(bmap, i - n_known, i);
6982 bmap->n_div -= n_known;
6983 bmap->extra -= n_known;
6985 bmap = isl_basic_map_reset_space(bmap, dim);
6986 bset = (struct isl_basic_set *)bmap;
6988 set = parameter_compute_divs(bset);
6989 map = (struct isl_map *)set;
6990 map = replace_space_by_local_space(map, ls);
6992 return map;
6993 error:
6994 isl_basic_map_free(bmap);
6995 return NULL;
6998 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7000 int i;
7001 unsigned off;
7003 if (!bmap)
7004 return -1;
7006 off = isl_space_dim(bmap->dim, isl_dim_all);
7007 for (i = 0; i < bmap->n_div; ++i) {
7008 if (isl_int_is_zero(bmap->div[i][0]))
7009 return 0;
7010 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7011 return -1);
7013 return 1;
7016 static int map_divs_known(__isl_keep isl_map *map)
7018 int i;
7020 if (!map)
7021 return -1;
7023 for (i = 0; i < map->n; ++i) {
7024 int known = isl_basic_map_divs_known(map->p[i]);
7025 if (known <= 0)
7026 return known;
7029 return 1;
7032 /* If bmap contains any unknown divs, then compute explicit
7033 * expressions for them. However, this computation may be
7034 * quite expensive, so first try to remove divs that aren't
7035 * strictly needed.
7037 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7039 int known;
7040 struct isl_map *map;
7042 known = isl_basic_map_divs_known(bmap);
7043 if (known < 0)
7044 goto error;
7045 if (known)
7046 return isl_map_from_basic_map(bmap);
7048 bmap = isl_basic_map_drop_redundant_divs(bmap);
7050 known = isl_basic_map_divs_known(bmap);
7051 if (known < 0)
7052 goto error;
7053 if (known)
7054 return isl_map_from_basic_map(bmap);
7056 map = compute_divs(bmap);
7057 return map;
7058 error:
7059 isl_basic_map_free(bmap);
7060 return NULL;
7063 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7065 int i;
7066 int known;
7067 struct isl_map *res;
7069 if (!map)
7070 return NULL;
7071 if (map->n == 0)
7072 return map;
7074 known = map_divs_known(map);
7075 if (known < 0) {
7076 isl_map_free(map);
7077 return NULL;
7079 if (known)
7080 return map;
7082 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7083 for (i = 1 ; i < map->n; ++i) {
7084 struct isl_map *r2;
7085 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7086 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7087 res = isl_map_union_disjoint(res, r2);
7088 else
7089 res = isl_map_union(res, r2);
7091 isl_map_free(map);
7093 return res;
7096 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7098 return (struct isl_set *)
7099 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7102 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7104 return (struct isl_set *)
7105 isl_map_compute_divs((struct isl_map *)set);
7108 struct isl_set *isl_map_domain(struct isl_map *map)
7110 int i;
7111 struct isl_set *set;
7113 if (!map)
7114 goto error;
7116 map = isl_map_cow(map);
7117 if (!map)
7118 return NULL;
7120 set = (struct isl_set *)map;
7121 set->dim = isl_space_domain(set->dim);
7122 if (!set->dim)
7123 goto error;
7124 for (i = 0; i < map->n; ++i) {
7125 set->p[i] = isl_basic_map_domain(map->p[i]);
7126 if (!set->p[i])
7127 goto error;
7129 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7130 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7131 return set;
7132 error:
7133 isl_map_free(map);
7134 return NULL;
7137 /* Return the union of "map1" and "map2", where we assume for now that
7138 * "map1" and "map2" are disjoint. Note that the basic maps inside
7139 * "map1" or "map2" may not be disjoint from each other.
7140 * Also note that this function is also called from isl_map_union,
7141 * which takes care of handling the situation where "map1" and "map2"
7142 * may not be disjoint.
7144 * If one of the inputs is empty, we can simply return the other input.
7145 * Similarly, if one of the inputs is universal, then it is equal to the union.
7147 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7148 __isl_take isl_map *map2)
7150 int i;
7151 unsigned flags = 0;
7152 struct isl_map *map = NULL;
7153 int is_universe;
7155 if (!map1 || !map2)
7156 goto error;
7158 if (!isl_space_is_equal(map1->dim, map2->dim))
7159 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7160 "spaces don't match", goto error);
7162 if (map1->n == 0) {
7163 isl_map_free(map1);
7164 return map2;
7166 if (map2->n == 0) {
7167 isl_map_free(map2);
7168 return map1;
7171 is_universe = isl_map_plain_is_universe(map1);
7172 if (is_universe < 0)
7173 goto error;
7174 if (is_universe) {
7175 isl_map_free(map2);
7176 return map1;
7179 is_universe = isl_map_plain_is_universe(map2);
7180 if (is_universe < 0)
7181 goto error;
7182 if (is_universe) {
7183 isl_map_free(map1);
7184 return map2;
7187 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7188 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7189 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7191 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7192 map1->n + map2->n, flags);
7193 if (!map)
7194 goto error;
7195 for (i = 0; i < map1->n; ++i) {
7196 map = isl_map_add_basic_map(map,
7197 isl_basic_map_copy(map1->p[i]));
7198 if (!map)
7199 goto error;
7201 for (i = 0; i < map2->n; ++i) {
7202 map = isl_map_add_basic_map(map,
7203 isl_basic_map_copy(map2->p[i]));
7204 if (!map)
7205 goto error;
7207 isl_map_free(map1);
7208 isl_map_free(map2);
7209 return map;
7210 error:
7211 isl_map_free(map);
7212 isl_map_free(map1);
7213 isl_map_free(map2);
7214 return NULL;
7217 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7218 * guaranteed to be disjoint by the caller.
7220 * Note that this functions is called from within isl_map_make_disjoint,
7221 * so we have to be careful not to touch the constraints of the inputs
7222 * in any way.
7224 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7225 __isl_take isl_map *map2)
7227 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7230 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7231 * not be disjoint. The parameters are assumed to have been aligned.
7233 * We currently simply call map_union_disjoint, the internal operation
7234 * of which does not really depend on the inputs being disjoint.
7235 * If the result contains more than one basic map, then we clear
7236 * the disjoint flag since the result may contain basic maps from
7237 * both inputs and these are not guaranteed to be disjoint.
7239 * As a special case, if "map1" and "map2" are obviously equal,
7240 * then we simply return "map1".
7242 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7243 __isl_take isl_map *map2)
7245 int equal;
7247 if (!map1 || !map2)
7248 goto error;
7250 equal = isl_map_plain_is_equal(map1, map2);
7251 if (equal < 0)
7252 goto error;
7253 if (equal) {
7254 isl_map_free(map2);
7255 return map1;
7258 map1 = map_union_disjoint(map1, map2);
7259 if (!map1)
7260 return NULL;
7261 if (map1->n > 1)
7262 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7263 return map1;
7264 error:
7265 isl_map_free(map1);
7266 isl_map_free(map2);
7267 return NULL;
7270 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7271 * not be disjoint.
7273 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7274 __isl_take isl_map *map2)
7276 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7279 struct isl_set *isl_set_union_disjoint(
7280 struct isl_set *set1, struct isl_set *set2)
7282 return (struct isl_set *)
7283 isl_map_union_disjoint(
7284 (struct isl_map *)set1, (struct isl_map *)set2);
7287 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7289 return (struct isl_set *)
7290 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7293 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7294 * the results.
7296 * "map" and "set" are assumed to be compatible and non-NULL.
7298 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7299 __isl_take isl_set *set,
7300 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7301 __isl_take isl_basic_set *bset))
7303 unsigned flags = 0;
7304 struct isl_map *result;
7305 int i, j;
7307 if (isl_set_plain_is_universe(set)) {
7308 isl_set_free(set);
7309 return map;
7312 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7313 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7314 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7316 result = isl_map_alloc_space(isl_space_copy(map->dim),
7317 map->n * set->n, flags);
7318 for (i = 0; result && i < map->n; ++i)
7319 for (j = 0; j < set->n; ++j) {
7320 result = isl_map_add_basic_map(result,
7321 fn(isl_basic_map_copy(map->p[i]),
7322 isl_basic_set_copy(set->p[j])));
7323 if (!result)
7324 break;
7327 isl_map_free(map);
7328 isl_set_free(set);
7329 return result;
7332 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7333 __isl_take isl_set *set)
7335 if (!map || !set)
7336 goto error;
7338 if (!isl_map_compatible_range(map, set))
7339 isl_die(set->ctx, isl_error_invalid,
7340 "incompatible spaces", goto error);
7342 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7343 error:
7344 isl_map_free(map);
7345 isl_set_free(set);
7346 return NULL;
7349 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7350 __isl_take isl_set *set)
7352 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7355 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7356 __isl_take isl_set *set)
7358 if (!map || !set)
7359 goto error;
7361 if (!isl_map_compatible_domain(map, set))
7362 isl_die(set->ctx, isl_error_invalid,
7363 "incompatible spaces", goto error);
7365 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7366 error:
7367 isl_map_free(map);
7368 isl_set_free(set);
7369 return NULL;
7372 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7373 __isl_take isl_set *set)
7375 return isl_map_align_params_map_map_and(map, set,
7376 &map_intersect_domain);
7379 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7380 __isl_take isl_map *map2)
7382 if (!map1 || !map2)
7383 goto error;
7384 map1 = isl_map_reverse(map1);
7385 map1 = isl_map_apply_range(map1, map2);
7386 return isl_map_reverse(map1);
7387 error:
7388 isl_map_free(map1);
7389 isl_map_free(map2);
7390 return NULL;
7393 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7394 __isl_take isl_map *map2)
7396 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7399 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7400 __isl_take isl_map *map2)
7402 isl_space *dim_result;
7403 struct isl_map *result;
7404 int i, j;
7406 if (!map1 || !map2)
7407 goto error;
7409 dim_result = isl_space_join(isl_space_copy(map1->dim),
7410 isl_space_copy(map2->dim));
7412 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7413 if (!result)
7414 goto error;
7415 for (i = 0; i < map1->n; ++i)
7416 for (j = 0; j < map2->n; ++j) {
7417 result = isl_map_add_basic_map(result,
7418 isl_basic_map_apply_range(
7419 isl_basic_map_copy(map1->p[i]),
7420 isl_basic_map_copy(map2->p[j])));
7421 if (!result)
7422 goto error;
7424 isl_map_free(map1);
7425 isl_map_free(map2);
7426 if (result && result->n <= 1)
7427 ISL_F_SET(result, ISL_MAP_DISJOINT);
7428 return result;
7429 error:
7430 isl_map_free(map1);
7431 isl_map_free(map2);
7432 return NULL;
7435 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7436 __isl_take isl_map *map2)
7438 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7442 * returns range - domain
7444 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7446 isl_space *dims, *target_dim;
7447 struct isl_basic_set *bset;
7448 unsigned dim;
7449 unsigned nparam;
7450 int i;
7452 if (!bmap)
7453 goto error;
7454 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7455 bmap->dim, isl_dim_out),
7456 goto error);
7457 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7458 dim = isl_basic_map_n_in(bmap);
7459 nparam = isl_basic_map_n_param(bmap);
7460 bset = isl_basic_set_from_basic_map(bmap);
7461 bset = isl_basic_set_cow(bset);
7462 dims = isl_basic_set_get_space(bset);
7463 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7464 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7465 bset = isl_basic_set_swap_vars(bset, 2*dim);
7466 for (i = 0; i < dim; ++i) {
7467 int j = isl_basic_map_alloc_equality(
7468 (struct isl_basic_map *)bset);
7469 if (j < 0) {
7470 bset = isl_basic_set_free(bset);
7471 break;
7473 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7474 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7475 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7476 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7478 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7479 bset = isl_basic_set_reset_space(bset, target_dim);
7480 return bset;
7481 error:
7482 isl_basic_map_free(bmap);
7483 return NULL;
7487 * returns range - domain
7489 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7491 int i;
7492 isl_space *dim;
7493 struct isl_set *result;
7495 if (!map)
7496 return NULL;
7498 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7499 map->dim, isl_dim_out),
7500 goto error);
7501 dim = isl_map_get_space(map);
7502 dim = isl_space_domain(dim);
7503 result = isl_set_alloc_space(dim, map->n, 0);
7504 if (!result)
7505 goto error;
7506 for (i = 0; i < map->n; ++i)
7507 result = isl_set_add_basic_set(result,
7508 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7509 isl_map_free(map);
7510 return result;
7511 error:
7512 isl_map_free(map);
7513 return NULL;
7517 * returns [domain -> range] -> range - domain
7519 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7520 __isl_take isl_basic_map *bmap)
7522 int i, k;
7523 isl_space *dim;
7524 isl_basic_map *domain;
7525 int nparam, n;
7526 unsigned total;
7528 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7529 isl_die(bmap->ctx, isl_error_invalid,
7530 "domain and range don't match", goto error);
7532 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7533 n = isl_basic_map_dim(bmap, isl_dim_in);
7535 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7536 domain = isl_basic_map_universe(dim);
7538 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7539 bmap = isl_basic_map_apply_range(bmap, domain);
7540 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7542 total = isl_basic_map_total_dim(bmap);
7544 for (i = 0; i < n; ++i) {
7545 k = isl_basic_map_alloc_equality(bmap);
7546 if (k < 0)
7547 goto error;
7548 isl_seq_clr(bmap->eq[k], 1 + total);
7549 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7550 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7551 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7554 bmap = isl_basic_map_gauss(bmap, NULL);
7555 return isl_basic_map_finalize(bmap);
7556 error:
7557 isl_basic_map_free(bmap);
7558 return NULL;
7562 * returns [domain -> range] -> range - domain
7564 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7566 int i;
7567 isl_space *domain_dim;
7569 if (!map)
7570 return NULL;
7572 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7573 isl_die(map->ctx, isl_error_invalid,
7574 "domain and range don't match", goto error);
7576 map = isl_map_cow(map);
7577 if (!map)
7578 return NULL;
7580 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7581 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7582 map->dim = isl_space_join(map->dim, domain_dim);
7583 if (!map->dim)
7584 goto error;
7585 for (i = 0; i < map->n; ++i) {
7586 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7587 if (!map->p[i])
7588 goto error;
7590 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7591 return map;
7592 error:
7593 isl_map_free(map);
7594 return NULL;
7597 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7599 struct isl_basic_map *bmap;
7600 unsigned nparam;
7601 unsigned dim;
7602 int i;
7604 if (!dims)
7605 return NULL;
7607 nparam = dims->nparam;
7608 dim = dims->n_out;
7609 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7610 if (!bmap)
7611 goto error;
7613 for (i = 0; i < dim; ++i) {
7614 int j = isl_basic_map_alloc_equality(bmap);
7615 if (j < 0)
7616 goto error;
7617 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7618 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7619 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7621 return isl_basic_map_finalize(bmap);
7622 error:
7623 isl_basic_map_free(bmap);
7624 return NULL;
7627 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7629 if (!dim)
7630 return NULL;
7631 if (dim->n_in != dim->n_out)
7632 isl_die(dim->ctx, isl_error_invalid,
7633 "number of input and output dimensions needs to be "
7634 "the same", goto error);
7635 return basic_map_identity(dim);
7636 error:
7637 isl_space_free(dim);
7638 return NULL;
7641 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7643 if (!model || !model->dim)
7644 return NULL;
7645 return isl_basic_map_identity(isl_space_copy(model->dim));
7648 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7650 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7653 struct isl_map *isl_map_identity_like(struct isl_map *model)
7655 if (!model || !model->dim)
7656 return NULL;
7657 return isl_map_identity(isl_space_copy(model->dim));
7660 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7662 if (!model || !model->dim)
7663 return NULL;
7664 return isl_map_identity(isl_space_copy(model->dim));
7667 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7669 isl_space *dim = isl_set_get_space(set);
7670 isl_map *id;
7671 id = isl_map_identity(isl_space_map_from_set(dim));
7672 return isl_map_intersect_range(id, set);
7675 /* Construct a basic set with all set dimensions having only non-negative
7676 * values.
7678 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7679 __isl_take isl_space *space)
7681 int i;
7682 unsigned nparam;
7683 unsigned dim;
7684 struct isl_basic_set *bset;
7686 if (!space)
7687 return NULL;
7688 nparam = space->nparam;
7689 dim = space->n_out;
7690 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7691 if (!bset)
7692 return NULL;
7693 for (i = 0; i < dim; ++i) {
7694 int k = isl_basic_set_alloc_inequality(bset);
7695 if (k < 0)
7696 goto error;
7697 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7698 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7700 return bset;
7701 error:
7702 isl_basic_set_free(bset);
7703 return NULL;
7706 /* Construct the half-space x_pos >= 0.
7708 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7709 int pos)
7711 int k;
7712 isl_basic_set *nonneg;
7714 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7715 k = isl_basic_set_alloc_inequality(nonneg);
7716 if (k < 0)
7717 goto error;
7718 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7719 isl_int_set_si(nonneg->ineq[k][pos], 1);
7721 return isl_basic_set_finalize(nonneg);
7722 error:
7723 isl_basic_set_free(nonneg);
7724 return NULL;
7727 /* Construct the half-space x_pos <= -1.
7729 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7731 int k;
7732 isl_basic_set *neg;
7734 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7735 k = isl_basic_set_alloc_inequality(neg);
7736 if (k < 0)
7737 goto error;
7738 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7739 isl_int_set_si(neg->ineq[k][0], -1);
7740 isl_int_set_si(neg->ineq[k][pos], -1);
7742 return isl_basic_set_finalize(neg);
7743 error:
7744 isl_basic_set_free(neg);
7745 return NULL;
7748 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7749 enum isl_dim_type type, unsigned first, unsigned n)
7751 int i;
7752 isl_basic_set *nonneg;
7753 isl_basic_set *neg;
7755 if (!set)
7756 return NULL;
7757 if (n == 0)
7758 return set;
7760 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7762 for (i = 0; i < n; ++i) {
7763 nonneg = nonneg_halfspace(isl_set_get_space(set),
7764 pos(set->dim, type) + first + i);
7765 neg = neg_halfspace(isl_set_get_space(set),
7766 pos(set->dim, type) + first + i);
7768 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7771 return set;
7772 error:
7773 isl_set_free(set);
7774 return NULL;
7777 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7778 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7779 void *user)
7781 isl_set *half;
7783 if (!set)
7784 return -1;
7785 if (isl_set_plain_is_empty(set)) {
7786 isl_set_free(set);
7787 return 0;
7789 if (first == len)
7790 return fn(set, signs, user);
7792 signs[first] = 1;
7793 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7794 1 + first));
7795 half = isl_set_intersect(half, isl_set_copy(set));
7796 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7797 goto error;
7799 signs[first] = -1;
7800 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7801 1 + first));
7802 half = isl_set_intersect(half, set);
7803 return foreach_orthant(half, signs, first + 1, len, fn, user);
7804 error:
7805 isl_set_free(set);
7806 return -1;
7809 /* Call "fn" on the intersections of "set" with each of the orthants
7810 * (except for obviously empty intersections). The orthant is identified
7811 * by the signs array, with each entry having value 1 or -1 according
7812 * to the sign of the corresponding variable.
7814 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7815 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7816 void *user)
7818 unsigned nparam;
7819 unsigned nvar;
7820 int *signs;
7821 int r;
7823 if (!set)
7824 return -1;
7825 if (isl_set_plain_is_empty(set))
7826 return 0;
7828 nparam = isl_set_dim(set, isl_dim_param);
7829 nvar = isl_set_dim(set, isl_dim_set);
7831 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7833 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7834 fn, user);
7836 free(signs);
7838 return r;
7841 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7843 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7846 int isl_basic_map_is_subset(
7847 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7849 int is_subset;
7850 struct isl_map *map1;
7851 struct isl_map *map2;
7853 if (!bmap1 || !bmap2)
7854 return -1;
7856 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7857 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7859 is_subset = isl_map_is_subset(map1, map2);
7861 isl_map_free(map1);
7862 isl_map_free(map2);
7864 return is_subset;
7867 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7868 __isl_keep isl_basic_set *bset2)
7870 return isl_basic_map_is_subset(bset1, bset2);
7873 int isl_basic_map_is_equal(
7874 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7876 int is_subset;
7878 if (!bmap1 || !bmap2)
7879 return -1;
7880 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7881 if (is_subset != 1)
7882 return is_subset;
7883 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7884 return is_subset;
7887 int isl_basic_set_is_equal(
7888 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7890 return isl_basic_map_is_equal(
7891 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7894 int isl_map_is_empty(struct isl_map *map)
7896 int i;
7897 int is_empty;
7899 if (!map)
7900 return -1;
7901 for (i = 0; i < map->n; ++i) {
7902 is_empty = isl_basic_map_is_empty(map->p[i]);
7903 if (is_empty < 0)
7904 return -1;
7905 if (!is_empty)
7906 return 0;
7908 return 1;
7911 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7913 return map ? map->n == 0 : -1;
7916 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7918 return isl_map_plain_is_empty(map);
7921 int isl_set_plain_is_empty(struct isl_set *set)
7923 return set ? set->n == 0 : -1;
7926 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7928 return isl_set_plain_is_empty(set);
7931 int isl_set_is_empty(struct isl_set *set)
7933 return isl_map_is_empty((struct isl_map *)set);
7936 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7938 if (!map1 || !map2)
7939 return -1;
7941 return isl_space_is_equal(map1->dim, map2->dim);
7944 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7946 if (!set1 || !set2)
7947 return -1;
7949 return isl_space_is_equal(set1->dim, set2->dim);
7952 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7954 int is_subset;
7956 if (!map1 || !map2)
7957 return -1;
7958 is_subset = isl_map_is_subset(map1, map2);
7959 if (is_subset != 1)
7960 return is_subset;
7961 is_subset = isl_map_is_subset(map2, map1);
7962 return is_subset;
7965 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7967 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7970 int isl_basic_map_is_strict_subset(
7971 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7973 int is_subset;
7975 if (!bmap1 || !bmap2)
7976 return -1;
7977 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7978 if (is_subset != 1)
7979 return is_subset;
7980 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7981 if (is_subset == -1)
7982 return is_subset;
7983 return !is_subset;
7986 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7988 int is_subset;
7990 if (!map1 || !map2)
7991 return -1;
7992 is_subset = isl_map_is_subset(map1, map2);
7993 if (is_subset != 1)
7994 return is_subset;
7995 is_subset = isl_map_is_subset(map2, map1);
7996 if (is_subset == -1)
7997 return is_subset;
7998 return !is_subset;
8001 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8003 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8006 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8008 if (!bmap)
8009 return -1;
8010 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8013 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8015 if (!bset)
8016 return -1;
8017 return bset->n_eq == 0 && bset->n_ineq == 0;
8020 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8022 int i;
8024 if (!map)
8025 return -1;
8027 for (i = 0; i < map->n; ++i) {
8028 int r = isl_basic_map_is_universe(map->p[i]);
8029 if (r < 0 || r)
8030 return r;
8033 return 0;
8036 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8038 return isl_map_plain_is_universe((isl_map *) set);
8041 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8043 return isl_set_plain_is_universe(set);
8046 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8048 struct isl_basic_set *bset = NULL;
8049 struct isl_vec *sample = NULL;
8050 int empty;
8051 unsigned total;
8053 if (!bmap)
8054 return -1;
8056 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8057 return 1;
8059 if (isl_basic_map_is_universe(bmap))
8060 return 0;
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8063 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8064 copy = isl_basic_map_remove_redundancies(copy);
8065 empty = isl_basic_map_plain_is_empty(copy);
8066 isl_basic_map_free(copy);
8067 return empty;
8070 total = 1 + isl_basic_map_total_dim(bmap);
8071 if (bmap->sample && bmap->sample->size == total) {
8072 int contains = isl_basic_map_contains(bmap, bmap->sample);
8073 if (contains < 0)
8074 return -1;
8075 if (contains)
8076 return 0;
8078 isl_vec_free(bmap->sample);
8079 bmap->sample = NULL;
8080 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8081 if (!bset)
8082 return -1;
8083 sample = isl_basic_set_sample_vec(bset);
8084 if (!sample)
8085 return -1;
8086 empty = sample->size == 0;
8087 isl_vec_free(bmap->sample);
8088 bmap->sample = sample;
8089 if (empty)
8090 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8092 return empty;
8095 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8097 if (!bmap)
8098 return -1;
8099 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8102 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8104 return isl_basic_map_plain_is_empty(bmap);
8107 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8109 if (!bset)
8110 return -1;
8111 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8114 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8116 return isl_basic_set_plain_is_empty(bset);
8119 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8121 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8124 struct isl_map *isl_basic_map_union(
8125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8127 struct isl_map *map;
8128 if (!bmap1 || !bmap2)
8129 goto error;
8131 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8133 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8134 if (!map)
8135 goto error;
8136 map = isl_map_add_basic_map(map, bmap1);
8137 map = isl_map_add_basic_map(map, bmap2);
8138 return map;
8139 error:
8140 isl_basic_map_free(bmap1);
8141 isl_basic_map_free(bmap2);
8142 return NULL;
8145 struct isl_set *isl_basic_set_union(
8146 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8148 return (struct isl_set *)isl_basic_map_union(
8149 (struct isl_basic_map *)bset1,
8150 (struct isl_basic_map *)bset2);
8153 /* Order divs such that any div only depends on previous divs */
8154 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8156 int i;
8157 unsigned off;
8159 if (!bmap)
8160 return NULL;
8162 off = isl_space_dim(bmap->dim, isl_dim_all);
8164 for (i = 0; i < bmap->n_div; ++i) {
8165 int pos;
8166 if (isl_int_is_zero(bmap->div[i][0]))
8167 continue;
8168 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8169 bmap->n_div-i);
8170 if (pos == -1)
8171 continue;
8172 isl_basic_map_swap_div(bmap, i, i + pos);
8173 --i;
8175 return bmap;
8178 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8180 return (struct isl_basic_set *)
8181 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8184 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8186 int i;
8188 if (!map)
8189 return 0;
8191 for (i = 0; i < map->n; ++i) {
8192 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8193 if (!map->p[i])
8194 goto error;
8197 return map;
8198 error:
8199 isl_map_free(map);
8200 return NULL;
8203 /* Apply the expansion computed by isl_merge_divs.
8204 * The expansion itself is given by "exp" while the resulting
8205 * list of divs is given by "div".
8207 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8208 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8210 int i, j;
8211 int n_div;
8213 bset = isl_basic_set_cow(bset);
8214 if (!bset || !div)
8215 goto error;
8217 if (div->n_row < bset->n_div)
8218 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8219 "not an expansion", goto error);
8221 n_div = bset->n_div;
8222 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8223 div->n_row - n_div, 0,
8224 2 * (div->n_row - n_div));
8226 for (i = n_div; i < div->n_row; ++i)
8227 if (isl_basic_set_alloc_div(bset) < 0)
8228 goto error;
8230 j = n_div - 1;
8231 for (i = div->n_row - 1; i >= 0; --i) {
8232 if (j >= 0 && exp[j] == i) {
8233 if (i != j)
8234 isl_basic_map_swap_div(bset, i, j);
8235 j--;
8236 } else {
8237 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8238 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8239 goto error;
8243 isl_mat_free(div);
8244 return bset;
8245 error:
8246 isl_basic_set_free(bset);
8247 isl_mat_free(div);
8248 return NULL;
8251 /* Look for a div in dst that corresponds to the div "div" in src.
8252 * The divs before "div" in src and dst are assumed to be the same.
8254 * Returns -1 if no corresponding div was found and the position
8255 * of the corresponding div in dst otherwise.
8257 static int find_div(struct isl_basic_map *dst,
8258 struct isl_basic_map *src, unsigned div)
8260 int i;
8262 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8264 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8265 for (i = div; i < dst->n_div; ++i)
8266 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8267 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8268 dst->n_div - div) == -1)
8269 return i;
8270 return -1;
8273 struct isl_basic_map *isl_basic_map_align_divs(
8274 struct isl_basic_map *dst, struct isl_basic_map *src)
8276 int i;
8277 unsigned total;
8279 if (!dst || !src)
8280 goto error;
8282 if (src->n_div == 0)
8283 return dst;
8285 for (i = 0; i < src->n_div; ++i)
8286 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8288 src = isl_basic_map_order_divs(src);
8289 dst = isl_basic_map_cow(dst);
8290 if (!dst)
8291 return NULL;
8292 dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8293 src->n_div, 0, 2 * src->n_div);
8294 if (!dst)
8295 return NULL;
8296 total = isl_space_dim(src->dim, isl_dim_all);
8297 for (i = 0; i < src->n_div; ++i) {
8298 int j = find_div(dst, src, i);
8299 if (j < 0) {
8300 j = isl_basic_map_alloc_div(dst);
8301 if (j < 0)
8302 goto error;
8303 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8304 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8305 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8306 goto error;
8308 if (j != i)
8309 isl_basic_map_swap_div(dst, i, j);
8311 return dst;
8312 error:
8313 isl_basic_map_free(dst);
8314 return NULL;
8317 struct isl_basic_set *isl_basic_set_align_divs(
8318 struct isl_basic_set *dst, struct isl_basic_set *src)
8320 return (struct isl_basic_set *)isl_basic_map_align_divs(
8321 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8324 struct isl_map *isl_map_align_divs(struct isl_map *map)
8326 int i;
8328 if (!map)
8329 return NULL;
8330 if (map->n == 0)
8331 return map;
8332 map = isl_map_compute_divs(map);
8333 map = isl_map_cow(map);
8334 if (!map)
8335 return NULL;
8337 for (i = 1; i < map->n; ++i)
8338 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8339 for (i = 1; i < map->n; ++i) {
8340 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8341 if (!map->p[i])
8342 return isl_map_free(map);
8345 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8346 return map;
8349 struct isl_set *isl_set_align_divs(struct isl_set *set)
8351 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8354 /* Align the divs of the basic sets in "set" to those
8355 * of the basic sets in "list", as well as to the other basic sets in "set".
8356 * The elements in "list" are assumed to have known divs.
8358 __isl_give isl_set *isl_set_align_divs_to_basic_set_list(
8359 __isl_take isl_set *set, __isl_keep isl_basic_set_list *list)
8361 int i, n;
8363 set = isl_set_compute_divs(set);
8364 set = isl_set_cow(set);
8365 if (!set || !list)
8366 return isl_set_free(set);
8367 if (set->n == 0)
8368 return set;
8370 n = isl_basic_set_list_n_basic_set(list);
8371 for (i = 0; i < n; ++i) {
8372 isl_basic_set *bset;
8374 bset = isl_basic_set_list_get_basic_set(list, i);
8375 set->p[0] = isl_basic_set_align_divs(set->p[0], bset);
8376 isl_basic_set_free(bset);
8378 if (!set->p[0])
8379 return isl_set_free(set);
8381 return isl_set_align_divs(set);
8384 /* Align the divs of each element of "list" to those of "bset".
8385 * Both "bset" and the elements of "list" are assumed to have known divs.
8387 __isl_give isl_basic_set_list *isl_basic_set_list_align_divs_to_basic_set(
8388 __isl_take isl_basic_set_list *list, __isl_keep isl_basic_set *bset)
8390 int i, n;
8392 if (!list || !bset)
8393 return isl_basic_set_list_free(list);
8395 n = isl_basic_set_list_n_basic_set(list);
8396 for (i = 0; i < n; ++i) {
8397 isl_basic_set *bset_i;
8399 bset_i = isl_basic_set_list_get_basic_set(list, i);
8400 bset_i = isl_basic_set_align_divs(bset_i, bset);
8401 list = isl_basic_set_list_set_basic_set(list, i, bset_i);
8404 return list;
8407 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8408 __isl_take isl_map *map)
8410 if (!set || !map)
8411 goto error;
8412 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8413 map = isl_map_intersect_domain(map, set);
8414 set = isl_map_range(map);
8415 return set;
8416 error:
8417 isl_set_free(set);
8418 isl_map_free(map);
8419 return NULL;
8422 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8423 __isl_take isl_map *map)
8425 return isl_map_align_params_map_map_and(set, map, &set_apply);
8428 /* There is no need to cow as removing empty parts doesn't change
8429 * the meaning of the set.
8431 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8433 int i;
8435 if (!map)
8436 return NULL;
8438 for (i = map->n - 1; i >= 0; --i)
8439 remove_if_empty(map, i);
8441 return map;
8444 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8446 return (struct isl_set *)
8447 isl_map_remove_empty_parts((struct isl_map *)set);
8450 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8452 struct isl_basic_map *bmap;
8453 if (!map || map->n == 0)
8454 return NULL;
8455 bmap = map->p[map->n-1];
8456 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8457 return isl_basic_map_copy(bmap);
8460 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8462 return (struct isl_basic_set *)
8463 isl_map_copy_basic_map((struct isl_map *)set);
8466 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8467 __isl_keep isl_basic_map *bmap)
8469 int i;
8471 if (!map || !bmap)
8472 goto error;
8473 for (i = map->n-1; i >= 0; --i) {
8474 if (map->p[i] != bmap)
8475 continue;
8476 map = isl_map_cow(map);
8477 if (!map)
8478 goto error;
8479 isl_basic_map_free(map->p[i]);
8480 if (i != map->n-1) {
8481 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8482 map->p[i] = map->p[map->n-1];
8484 map->n--;
8485 return map;
8487 return map;
8488 error:
8489 isl_map_free(map);
8490 return NULL;
8493 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8494 struct isl_basic_set *bset)
8496 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8497 (struct isl_basic_map *)bset);
8500 /* Given two basic sets bset1 and bset2, compute the maximal difference
8501 * between the values of dimension pos in bset1 and those in bset2
8502 * for any common value of the parameters and dimensions preceding pos.
8504 static enum isl_lp_result basic_set_maximal_difference_at(
8505 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8506 int pos, isl_int *opt)
8508 isl_space *dims;
8509 struct isl_basic_map *bmap1 = NULL;
8510 struct isl_basic_map *bmap2 = NULL;
8511 struct isl_ctx *ctx;
8512 struct isl_vec *obj;
8513 unsigned total;
8514 unsigned nparam;
8515 unsigned dim1, dim2;
8516 enum isl_lp_result res;
8518 if (!bset1 || !bset2)
8519 return isl_lp_error;
8521 nparam = isl_basic_set_n_param(bset1);
8522 dim1 = isl_basic_set_n_dim(bset1);
8523 dim2 = isl_basic_set_n_dim(bset2);
8524 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8525 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8526 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8527 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8528 if (!bmap1 || !bmap2)
8529 goto error;
8530 bmap1 = isl_basic_map_cow(bmap1);
8531 bmap1 = isl_basic_map_extend(bmap1, nparam,
8532 pos, (dim1 - pos) + (dim2 - pos),
8533 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8534 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8535 if (!bmap1)
8536 goto error2;
8537 total = isl_basic_map_total_dim(bmap1);
8538 ctx = bmap1->ctx;
8539 obj = isl_vec_alloc(ctx, 1 + total);
8540 if (!obj)
8541 goto error2;
8542 isl_seq_clr(obj->block.data, 1 + total);
8543 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8544 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8545 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8546 opt, NULL, NULL);
8547 isl_basic_map_free(bmap1);
8548 isl_vec_free(obj);
8549 return res;
8550 error:
8551 isl_basic_map_free(bmap2);
8552 error2:
8553 isl_basic_map_free(bmap1);
8554 return isl_lp_error;
8557 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8558 * for any common value of the parameters and dimensions preceding pos
8559 * in both basic sets, the values of dimension pos in bset1 are
8560 * smaller or larger than those in bset2.
8562 * Returns
8563 * 1 if bset1 follows bset2
8564 * -1 if bset1 precedes bset2
8565 * 0 if bset1 and bset2 are incomparable
8566 * -2 if some error occurred.
8568 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8569 struct isl_basic_set *bset2, int pos)
8571 isl_int opt;
8572 enum isl_lp_result res;
8573 int cmp;
8575 isl_int_init(opt);
8577 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8579 if (res == isl_lp_empty)
8580 cmp = 0;
8581 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8582 res == isl_lp_unbounded)
8583 cmp = 1;
8584 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8585 cmp = -1;
8586 else
8587 cmp = -2;
8589 isl_int_clear(opt);
8590 return cmp;
8593 /* Given two basic sets bset1 and bset2, check whether
8594 * for any common value of the parameters and dimensions preceding pos
8595 * there is a value of dimension pos in bset1 that is larger
8596 * than a value of the same dimension in bset2.
8598 * Return
8599 * 1 if there exists such a pair
8600 * 0 if there is no such pair, but there is a pair of equal values
8601 * -1 otherwise
8602 * -2 if some error occurred.
8604 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8605 __isl_keep isl_basic_set *bset2, int pos)
8607 isl_int opt;
8608 enum isl_lp_result res;
8609 int cmp;
8611 isl_int_init(opt);
8613 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8615 if (res == isl_lp_empty)
8616 cmp = -1;
8617 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8618 res == isl_lp_unbounded)
8619 cmp = 1;
8620 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8621 cmp = -1;
8622 else if (res == isl_lp_ok)
8623 cmp = 0;
8624 else
8625 cmp = -2;
8627 isl_int_clear(opt);
8628 return cmp;
8631 /* Given two sets set1 and set2, check whether
8632 * for any common value of the parameters and dimensions preceding pos
8633 * there is a value of dimension pos in set1 that is larger
8634 * than a value of the same dimension in set2.
8636 * Return
8637 * 1 if there exists such a pair
8638 * 0 if there is no such pair, but there is a pair of equal values
8639 * -1 otherwise
8640 * -2 if some error occurred.
8642 int isl_set_follows_at(__isl_keep isl_set *set1,
8643 __isl_keep isl_set *set2, int pos)
8645 int i, j;
8646 int follows = -1;
8648 if (!set1 || !set2)
8649 return -2;
8651 for (i = 0; i < set1->n; ++i)
8652 for (j = 0; j < set2->n; ++j) {
8653 int f;
8654 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8655 if (f == 1 || f == -2)
8656 return f;
8657 if (f > follows)
8658 follows = f;
8661 return follows;
8664 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8665 unsigned pos, isl_int *val)
8667 int i;
8668 int d;
8669 unsigned total;
8671 if (!bmap)
8672 return -1;
8673 total = isl_basic_map_total_dim(bmap);
8674 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8675 for (; d+1 > pos; --d)
8676 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8677 break;
8678 if (d != pos)
8679 continue;
8680 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8681 return 0;
8682 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8683 return 0;
8684 if (!isl_int_is_one(bmap->eq[i][1+d]))
8685 return 0;
8686 if (val)
8687 isl_int_neg(*val, bmap->eq[i][0]);
8688 return 1;
8690 return 0;
8693 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8694 unsigned pos, isl_int *val)
8696 int i;
8697 isl_int v;
8698 isl_int tmp;
8699 int fixed;
8701 if (!map)
8702 return -1;
8703 if (map->n == 0)
8704 return 0;
8705 if (map->n == 1)
8706 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8707 isl_int_init(v);
8708 isl_int_init(tmp);
8709 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8710 for (i = 1; fixed == 1 && i < map->n; ++i) {
8711 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8712 if (fixed == 1 && isl_int_ne(tmp, v))
8713 fixed = 0;
8715 if (val)
8716 isl_int_set(*val, v);
8717 isl_int_clear(tmp);
8718 isl_int_clear(v);
8719 return fixed;
8722 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8723 unsigned pos, isl_int *val)
8725 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8726 pos, val);
8729 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8730 isl_int *val)
8732 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8735 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8736 enum isl_dim_type type, unsigned pos, isl_int *val)
8738 if (pos >= isl_basic_map_dim(bmap, type))
8739 return -1;
8740 return isl_basic_map_plain_has_fixed_var(bmap,
8741 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8744 /* If "bmap" obviously lies on a hyperplane where the given dimension
8745 * has a fixed value, then return that value.
8746 * Otherwise return NaN.
8748 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8749 __isl_keep isl_basic_map *bmap,
8750 enum isl_dim_type type, unsigned pos)
8752 isl_ctx *ctx;
8753 isl_val *v;
8754 int fixed;
8756 if (!bmap)
8757 return NULL;
8758 ctx = isl_basic_map_get_ctx(bmap);
8759 v = isl_val_alloc(ctx);
8760 if (!v)
8761 return NULL;
8762 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8763 if (fixed < 0)
8764 return isl_val_free(v);
8765 if (fixed) {
8766 isl_int_set_si(v->d, 1);
8767 return v;
8769 isl_val_free(v);
8770 return isl_val_nan(ctx);
8773 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8774 enum isl_dim_type type, unsigned pos, isl_int *val)
8776 if (pos >= isl_map_dim(map, type))
8777 return -1;
8778 return isl_map_plain_has_fixed_var(map,
8779 map_offset(map, type) - 1 + pos, val);
8782 /* If "map" obviously lies on a hyperplane where the given dimension
8783 * has a fixed value, then return that value.
8784 * Otherwise return NaN.
8786 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8787 enum isl_dim_type type, unsigned pos)
8789 isl_ctx *ctx;
8790 isl_val *v;
8791 int fixed;
8793 if (!map)
8794 return NULL;
8795 ctx = isl_map_get_ctx(map);
8796 v = isl_val_alloc(ctx);
8797 if (!v)
8798 return NULL;
8799 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8800 if (fixed < 0)
8801 return isl_val_free(v);
8802 if (fixed) {
8803 isl_int_set_si(v->d, 1);
8804 return v;
8806 isl_val_free(v);
8807 return isl_val_nan(ctx);
8810 /* If "set" obviously lies on a hyperplane where the given dimension
8811 * has a fixed value, then return that value.
8812 * Otherwise return NaN.
8814 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8815 enum isl_dim_type type, unsigned pos)
8817 return isl_map_plain_get_val_if_fixed(set, type, pos);
8820 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8821 enum isl_dim_type type, unsigned pos, isl_int *val)
8823 return isl_map_plain_is_fixed(set, type, pos, val);
8826 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8827 enum isl_dim_type type, unsigned pos, isl_int *val)
8829 return isl_map_plain_is_fixed(map, type, pos, val);
8832 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8833 * then return this fixed value in *val.
8835 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8836 unsigned dim, isl_int *val)
8838 return isl_basic_set_plain_has_fixed_var(bset,
8839 isl_basic_set_n_param(bset) + dim, val);
8842 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8843 * then return this fixed value in *val.
8845 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8846 unsigned dim, isl_int *val)
8848 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8851 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8852 unsigned dim, isl_int *val)
8854 return isl_set_plain_dim_is_fixed(set, dim, val);
8857 /* Check if input variable in has fixed value and if so and if val is not NULL,
8858 * then return this fixed value in *val.
8860 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8861 unsigned in, isl_int *val)
8863 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8866 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8867 * and if val is not NULL, then return this lower bound in *val.
8869 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8870 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8872 int i, i_eq = -1, i_ineq = -1;
8873 isl_int *c;
8874 unsigned total;
8875 unsigned nparam;
8877 if (!bset)
8878 return -1;
8879 total = isl_basic_set_total_dim(bset);
8880 nparam = isl_basic_set_n_param(bset);
8881 for (i = 0; i < bset->n_eq; ++i) {
8882 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8883 continue;
8884 if (i_eq != -1)
8885 return 0;
8886 i_eq = i;
8888 for (i = 0; i < bset->n_ineq; ++i) {
8889 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8890 continue;
8891 if (i_eq != -1 || i_ineq != -1)
8892 return 0;
8893 i_ineq = i;
8895 if (i_eq == -1 && i_ineq == -1)
8896 return 0;
8897 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8898 /* The coefficient should always be one due to normalization. */
8899 if (!isl_int_is_one(c[1+nparam+dim]))
8900 return 0;
8901 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8902 return 0;
8903 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8904 total - nparam - dim - 1) != -1)
8905 return 0;
8906 if (val)
8907 isl_int_neg(*val, c[0]);
8908 return 1;
8911 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8912 unsigned dim, isl_int *val)
8914 int i;
8915 isl_int v;
8916 isl_int tmp;
8917 int fixed;
8919 if (!set)
8920 return -1;
8921 if (set->n == 0)
8922 return 0;
8923 if (set->n == 1)
8924 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8925 dim, val);
8926 isl_int_init(v);
8927 isl_int_init(tmp);
8928 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8929 dim, &v);
8930 for (i = 1; fixed == 1 && i < set->n; ++i) {
8931 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8932 dim, &tmp);
8933 if (fixed == 1 && isl_int_ne(tmp, v))
8934 fixed = 0;
8936 if (val)
8937 isl_int_set(*val, v);
8938 isl_int_clear(tmp);
8939 isl_int_clear(v);
8940 return fixed;
8943 struct constraint {
8944 unsigned size;
8945 isl_int *c;
8948 /* uset_gist depends on constraints without existentially quantified
8949 * variables sorting first.
8951 static int qsort_constraint_cmp(const void *p1, const void *p2)
8953 const struct constraint *c1 = (const struct constraint *)p1;
8954 const struct constraint *c2 = (const struct constraint *)p2;
8955 int l1, l2;
8956 unsigned size = isl_min(c1->size, c2->size);
8958 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8959 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8961 if (l1 != l2)
8962 return l1 - l2;
8964 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8967 static struct isl_basic_map *isl_basic_map_sort_constraints(
8968 struct isl_basic_map *bmap)
8970 int i;
8971 struct constraint *c;
8972 unsigned total;
8974 if (!bmap)
8975 return NULL;
8976 if (bmap->n_ineq == 0)
8977 return bmap;
8978 total = isl_basic_map_total_dim(bmap);
8979 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8980 if (!c)
8981 goto error;
8982 for (i = 0; i < bmap->n_ineq; ++i) {
8983 c[i].size = total;
8984 c[i].c = bmap->ineq[i];
8986 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8987 for (i = 0; i < bmap->n_ineq; ++i)
8988 bmap->ineq[i] = c[i].c;
8989 free(c);
8990 return bmap;
8991 error:
8992 isl_basic_map_free(bmap);
8993 return NULL;
8996 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8997 __isl_take isl_basic_set *bset)
8999 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9000 (struct isl_basic_map *)bset);
9003 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9005 if (!bmap)
9006 return NULL;
9007 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9008 return bmap;
9009 bmap = isl_basic_map_remove_redundancies(bmap);
9010 bmap = isl_basic_map_sort_constraints(bmap);
9011 if (bmap)
9012 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9013 return bmap;
9016 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9018 return (struct isl_basic_set *)isl_basic_map_normalize(
9019 (struct isl_basic_map *)bset);
9022 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9023 const __isl_keep isl_basic_map *bmap2)
9025 int i, cmp;
9026 unsigned total;
9028 if (bmap1 == bmap2)
9029 return 0;
9030 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9031 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9032 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9033 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9034 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9035 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9036 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9037 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9038 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9039 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9040 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9041 return 0;
9042 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9043 return 1;
9044 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9045 return -1;
9046 if (bmap1->n_eq != bmap2->n_eq)
9047 return bmap1->n_eq - bmap2->n_eq;
9048 if (bmap1->n_ineq != bmap2->n_ineq)
9049 return bmap1->n_ineq - bmap2->n_ineq;
9050 if (bmap1->n_div != bmap2->n_div)
9051 return bmap1->n_div - bmap2->n_div;
9052 total = isl_basic_map_total_dim(bmap1);
9053 for (i = 0; i < bmap1->n_eq; ++i) {
9054 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9055 if (cmp)
9056 return cmp;
9058 for (i = 0; i < bmap1->n_ineq; ++i) {
9059 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9060 if (cmp)
9061 return cmp;
9063 for (i = 0; i < bmap1->n_div; ++i) {
9064 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9065 if (cmp)
9066 return cmp;
9068 return 0;
9071 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9072 const __isl_keep isl_basic_set *bset2)
9074 return isl_basic_map_plain_cmp(bset1, bset2);
9077 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9079 int i, cmp;
9081 if (set1 == set2)
9082 return 0;
9083 if (set1->n != set2->n)
9084 return set1->n - set2->n;
9086 for (i = 0; i < set1->n; ++i) {
9087 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9088 if (cmp)
9089 return cmp;
9092 return 0;
9095 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9096 __isl_keep isl_basic_map *bmap2)
9098 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9101 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9102 __isl_keep isl_basic_set *bset2)
9104 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9105 (isl_basic_map *)bset2);
9108 static int qsort_bmap_cmp(const void *p1, const void *p2)
9110 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9111 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9113 return isl_basic_map_plain_cmp(bmap1, bmap2);
9116 /* We normalize in place, but if anything goes wrong we need
9117 * to return NULL, so we need to make sure we don't change the
9118 * meaning of any possible other copies of map.
9120 struct isl_map *isl_map_normalize(struct isl_map *map)
9122 int i, j;
9123 struct isl_basic_map *bmap;
9125 if (!map)
9126 return NULL;
9127 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9128 return map;
9129 for (i = 0; i < map->n; ++i) {
9130 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9131 if (!bmap)
9132 goto error;
9133 isl_basic_map_free(map->p[i]);
9134 map->p[i] = bmap;
9136 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9137 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9138 map = isl_map_remove_empty_parts(map);
9139 if (!map)
9140 return NULL;
9141 for (i = map->n - 1; i >= 1; --i) {
9142 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
9143 continue;
9144 isl_basic_map_free(map->p[i-1]);
9145 for (j = i; j < map->n; ++j)
9146 map->p[j-1] = map->p[j];
9147 map->n--;
9149 return map;
9150 error:
9151 isl_map_free(map);
9152 return NULL;
9155 struct isl_set *isl_set_normalize(struct isl_set *set)
9157 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9160 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9162 int i;
9163 int equal;
9165 if (!map1 || !map2)
9166 return -1;
9168 if (map1 == map2)
9169 return 1;
9170 if (!isl_space_is_equal(map1->dim, map2->dim))
9171 return 0;
9173 map1 = isl_map_copy(map1);
9174 map2 = isl_map_copy(map2);
9175 map1 = isl_map_normalize(map1);
9176 map2 = isl_map_normalize(map2);
9177 if (!map1 || !map2)
9178 goto error;
9179 equal = map1->n == map2->n;
9180 for (i = 0; equal && i < map1->n; ++i) {
9181 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9182 if (equal < 0)
9183 goto error;
9185 isl_map_free(map1);
9186 isl_map_free(map2);
9187 return equal;
9188 error:
9189 isl_map_free(map1);
9190 isl_map_free(map2);
9191 return -1;
9194 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9196 return isl_map_plain_is_equal(map1, map2);
9199 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9201 return isl_map_plain_is_equal((struct isl_map *)set1,
9202 (struct isl_map *)set2);
9205 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9207 return isl_set_plain_is_equal(set1, set2);
9210 /* Return an interval that ranges from min to max (inclusive)
9212 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9213 isl_int min, isl_int max)
9215 int k;
9216 struct isl_basic_set *bset = NULL;
9218 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9219 if (!bset)
9220 goto error;
9222 k = isl_basic_set_alloc_inequality(bset);
9223 if (k < 0)
9224 goto error;
9225 isl_int_set_si(bset->ineq[k][1], 1);
9226 isl_int_neg(bset->ineq[k][0], min);
9228 k = isl_basic_set_alloc_inequality(bset);
9229 if (k < 0)
9230 goto error;
9231 isl_int_set_si(bset->ineq[k][1], -1);
9232 isl_int_set(bset->ineq[k][0], max);
9234 return bset;
9235 error:
9236 isl_basic_set_free(bset);
9237 return NULL;
9240 /* Return the basic sets in "set" as a list.
9242 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9243 __isl_keep isl_set *set)
9245 int i;
9246 isl_ctx *ctx;
9247 isl_basic_set_list *list;
9249 if (!set)
9250 return NULL;
9251 ctx = isl_set_get_ctx(set);
9252 list = isl_basic_set_list_alloc(ctx, set->n);
9254 for (i = 0; i < set->n; ++i) {
9255 isl_basic_set *bset;
9257 bset = isl_basic_set_copy(set->p[i]);
9258 list = isl_basic_set_list_add(list, bset);
9261 return list;
9264 /* Return the intersection of the elements in the non-empty list "list".
9265 * All elements are assumed to live in the same space.
9267 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9268 __isl_take struct isl_basic_set_list *list)
9270 int i, n;
9271 isl_basic_set *bset;
9273 if (!list)
9274 return NULL;
9275 n = isl_basic_set_list_n_basic_set(list);
9276 if (n < 1)
9277 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9278 "expecting non-empty list", goto error);
9280 bset = isl_basic_set_list_get_basic_set(list, 0);
9281 for (i = 1; i < n; ++i) {
9282 isl_basic_set *bset_i;
9284 bset_i = isl_basic_set_list_get_basic_set(list, i);
9285 bset = isl_basic_set_intersect(bset, bset_i);
9288 isl_basic_set_list_free(list);
9289 return bset;
9290 error:
9291 isl_basic_set_list_free(list);
9292 return NULL;
9295 /* Return the Cartesian product of the basic sets in list (in the given order).
9297 __isl_give isl_basic_set *isl_basic_set_list_product(
9298 __isl_take struct isl_basic_set_list *list)
9300 int i;
9301 unsigned dim;
9302 unsigned nparam;
9303 unsigned extra;
9304 unsigned n_eq;
9305 unsigned n_ineq;
9306 struct isl_basic_set *product = NULL;
9308 if (!list)
9309 goto error;
9310 isl_assert(list->ctx, list->n > 0, goto error);
9311 isl_assert(list->ctx, list->p[0], goto error);
9312 nparam = isl_basic_set_n_param(list->p[0]);
9313 dim = isl_basic_set_n_dim(list->p[0]);
9314 extra = list->p[0]->n_div;
9315 n_eq = list->p[0]->n_eq;
9316 n_ineq = list->p[0]->n_ineq;
9317 for (i = 1; i < list->n; ++i) {
9318 isl_assert(list->ctx, list->p[i], goto error);
9319 isl_assert(list->ctx,
9320 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9321 dim += isl_basic_set_n_dim(list->p[i]);
9322 extra += list->p[i]->n_div;
9323 n_eq += list->p[i]->n_eq;
9324 n_ineq += list->p[i]->n_ineq;
9326 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9327 n_eq, n_ineq);
9328 if (!product)
9329 goto error;
9330 dim = 0;
9331 for (i = 0; i < list->n; ++i) {
9332 isl_basic_set_add_constraints(product,
9333 isl_basic_set_copy(list->p[i]), dim);
9334 dim += isl_basic_set_n_dim(list->p[i]);
9336 isl_basic_set_list_free(list);
9337 return product;
9338 error:
9339 isl_basic_set_free(product);
9340 isl_basic_set_list_free(list);
9341 return NULL;
9344 struct isl_basic_map *isl_basic_map_product(
9345 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9347 isl_space *dim_result = NULL;
9348 struct isl_basic_map *bmap;
9349 unsigned in1, in2, out1, out2, nparam, total, pos;
9350 struct isl_dim_map *dim_map1, *dim_map2;
9352 if (!bmap1 || !bmap2)
9353 goto error;
9355 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9356 bmap2->dim, isl_dim_param), goto error);
9357 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9358 isl_space_copy(bmap2->dim));
9360 in1 = isl_basic_map_n_in(bmap1);
9361 in2 = isl_basic_map_n_in(bmap2);
9362 out1 = isl_basic_map_n_out(bmap1);
9363 out2 = isl_basic_map_n_out(bmap2);
9364 nparam = isl_basic_map_n_param(bmap1);
9366 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9367 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9368 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9369 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9370 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9371 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9372 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9373 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9374 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9375 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9376 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9378 bmap = isl_basic_map_alloc_space(dim_result,
9379 bmap1->n_div + bmap2->n_div,
9380 bmap1->n_eq + bmap2->n_eq,
9381 bmap1->n_ineq + bmap2->n_ineq);
9382 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9383 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9384 bmap = isl_basic_map_simplify(bmap);
9385 return isl_basic_map_finalize(bmap);
9386 error:
9387 isl_basic_map_free(bmap1);
9388 isl_basic_map_free(bmap2);
9389 return NULL;
9392 __isl_give isl_basic_map *isl_basic_map_flat_product(
9393 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9395 isl_basic_map *prod;
9397 prod = isl_basic_map_product(bmap1, bmap2);
9398 prod = isl_basic_map_flatten(prod);
9399 return prod;
9402 __isl_give isl_basic_set *isl_basic_set_flat_product(
9403 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9405 return isl_basic_map_flat_range_product(bset1, bset2);
9408 __isl_give isl_basic_map *isl_basic_map_domain_product(
9409 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9411 isl_space *space_result = NULL;
9412 isl_basic_map *bmap;
9413 unsigned in1, in2, out, nparam, total, pos;
9414 struct isl_dim_map *dim_map1, *dim_map2;
9416 if (!bmap1 || !bmap2)
9417 goto error;
9419 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9420 isl_space_copy(bmap2->dim));
9422 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9423 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9424 out = isl_basic_map_dim(bmap1, isl_dim_out);
9425 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9427 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9428 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9429 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9430 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9431 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9432 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9433 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9434 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9435 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9436 isl_dim_map_div(dim_map1, bmap1, pos += out);
9437 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9439 bmap = isl_basic_map_alloc_space(space_result,
9440 bmap1->n_div + bmap2->n_div,
9441 bmap1->n_eq + bmap2->n_eq,
9442 bmap1->n_ineq + bmap2->n_ineq);
9443 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9444 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9445 bmap = isl_basic_map_simplify(bmap);
9446 return isl_basic_map_finalize(bmap);
9447 error:
9448 isl_basic_map_free(bmap1);
9449 isl_basic_map_free(bmap2);
9450 return NULL;
9453 __isl_give isl_basic_map *isl_basic_map_range_product(
9454 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9456 isl_space *dim_result = NULL;
9457 isl_basic_map *bmap;
9458 unsigned in, out1, out2, nparam, total, pos;
9459 struct isl_dim_map *dim_map1, *dim_map2;
9461 if (!bmap1 || !bmap2)
9462 goto error;
9464 if (!isl_space_match(bmap1->dim, isl_dim_param,
9465 bmap2->dim, isl_dim_param))
9466 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9467 "parameters don't match", goto error);
9469 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9470 isl_space_copy(bmap2->dim));
9472 in = isl_basic_map_dim(bmap1, isl_dim_in);
9473 out1 = isl_basic_map_n_out(bmap1);
9474 out2 = isl_basic_map_n_out(bmap2);
9475 nparam = isl_basic_map_n_param(bmap1);
9477 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9478 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9479 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9480 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9481 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9482 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9483 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9484 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9485 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9486 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9487 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9489 bmap = isl_basic_map_alloc_space(dim_result,
9490 bmap1->n_div + bmap2->n_div,
9491 bmap1->n_eq + bmap2->n_eq,
9492 bmap1->n_ineq + bmap2->n_ineq);
9493 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9494 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9495 bmap = isl_basic_map_simplify(bmap);
9496 return isl_basic_map_finalize(bmap);
9497 error:
9498 isl_basic_map_free(bmap1);
9499 isl_basic_map_free(bmap2);
9500 return NULL;
9503 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9504 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9506 isl_basic_map *prod;
9508 prod = isl_basic_map_range_product(bmap1, bmap2);
9509 prod = isl_basic_map_flatten_range(prod);
9510 return prod;
9513 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9514 __isl_take isl_map *map2,
9515 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9516 __isl_take isl_space *right),
9517 __isl_give isl_basic_map *(*basic_map_product)(
9518 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9520 unsigned flags = 0;
9521 struct isl_map *result;
9522 int i, j;
9524 if (!map1 || !map2)
9525 goto error;
9527 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9528 map2->dim, isl_dim_param), goto error);
9530 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9531 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9532 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9534 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9535 isl_space_copy(map2->dim)),
9536 map1->n * map2->n, flags);
9537 if (!result)
9538 goto error;
9539 for (i = 0; i < map1->n; ++i)
9540 for (j = 0; j < map2->n; ++j) {
9541 struct isl_basic_map *part;
9542 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9543 isl_basic_map_copy(map2->p[j]));
9544 if (isl_basic_map_is_empty(part))
9545 isl_basic_map_free(part);
9546 else
9547 result = isl_map_add_basic_map(result, part);
9548 if (!result)
9549 goto error;
9551 isl_map_free(map1);
9552 isl_map_free(map2);
9553 return result;
9554 error:
9555 isl_map_free(map1);
9556 isl_map_free(map2);
9557 return NULL;
9560 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9562 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9563 __isl_take isl_map *map2)
9565 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9568 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9569 __isl_take isl_map *map2)
9571 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9574 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9576 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9577 __isl_take isl_map *map2)
9579 isl_map *prod;
9581 prod = isl_map_product(map1, map2);
9582 prod = isl_map_flatten(prod);
9583 return prod;
9586 /* Given two set A and B, construct its Cartesian product A x B.
9588 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9590 return isl_map_range_product(set1, set2);
9593 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9594 __isl_take isl_set *set2)
9596 return isl_map_flat_range_product(set1, set2);
9599 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9601 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9602 __isl_take isl_map *map2)
9604 return map_product(map1, map2, &isl_space_domain_product,
9605 &isl_basic_map_domain_product);
9608 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9610 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9611 __isl_take isl_map *map2)
9613 return map_product(map1, map2, &isl_space_range_product,
9614 &isl_basic_map_range_product);
9617 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9618 __isl_take isl_map *map2)
9620 return isl_map_align_params_map_map_and(map1, map2,
9621 &map_domain_product_aligned);
9624 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9625 __isl_take isl_map *map2)
9627 return isl_map_align_params_map_map_and(map1, map2,
9628 &map_range_product_aligned);
9631 /* Given a map A -> [B -> C], extract the map A -> B.
9633 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9635 isl_space *space;
9636 int total, keep;
9638 if (!map)
9639 return NULL;
9640 if (!isl_space_range_is_wrapping(map->dim))
9641 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9642 "range is not a product", return isl_map_free(map));
9644 space = isl_map_get_space(map);
9645 total = isl_space_dim(space, isl_dim_out);
9646 space = isl_space_range_factor_domain(space);
9647 keep = isl_space_dim(space, isl_dim_out);
9648 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9649 map = isl_map_reset_space(map, space);
9651 return map;
9654 /* Given a map A -> [B -> C], extract the map A -> C.
9656 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9658 isl_space *space;
9659 int total, keep;
9661 if (!map)
9662 return NULL;
9663 if (!isl_space_range_is_wrapping(map->dim))
9664 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9665 "range is not a product", return isl_map_free(map));
9667 space = isl_map_get_space(map);
9668 total = isl_space_dim(space, isl_dim_out);
9669 space = isl_space_range_factor_range(space);
9670 keep = isl_space_dim(space, isl_dim_out);
9671 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9672 map = isl_map_reset_space(map, space);
9674 return map;
9677 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9679 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9680 __isl_take isl_map *map2)
9682 isl_map *prod;
9684 prod = isl_map_domain_product(map1, map2);
9685 prod = isl_map_flatten_domain(prod);
9686 return prod;
9689 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9691 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9692 __isl_take isl_map *map2)
9694 isl_map *prod;
9696 prod = isl_map_range_product(map1, map2);
9697 prod = isl_map_flatten_range(prod);
9698 return prod;
9701 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9703 int i;
9704 uint32_t hash = isl_hash_init();
9705 unsigned total;
9707 if (!bmap)
9708 return 0;
9709 bmap = isl_basic_map_copy(bmap);
9710 bmap = isl_basic_map_normalize(bmap);
9711 if (!bmap)
9712 return 0;
9713 total = isl_basic_map_total_dim(bmap);
9714 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9715 for (i = 0; i < bmap->n_eq; ++i) {
9716 uint32_t c_hash;
9717 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9718 isl_hash_hash(hash, c_hash);
9720 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9721 for (i = 0; i < bmap->n_ineq; ++i) {
9722 uint32_t c_hash;
9723 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9724 isl_hash_hash(hash, c_hash);
9726 isl_hash_byte(hash, bmap->n_div & 0xFF);
9727 for (i = 0; i < bmap->n_div; ++i) {
9728 uint32_t c_hash;
9729 if (isl_int_is_zero(bmap->div[i][0]))
9730 continue;
9731 isl_hash_byte(hash, i & 0xFF);
9732 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9733 isl_hash_hash(hash, c_hash);
9735 isl_basic_map_free(bmap);
9736 return hash;
9739 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9741 return isl_basic_map_get_hash((isl_basic_map *)bset);
9744 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9746 int i;
9747 uint32_t hash;
9749 if (!map)
9750 return 0;
9751 map = isl_map_copy(map);
9752 map = isl_map_normalize(map);
9753 if (!map)
9754 return 0;
9756 hash = isl_hash_init();
9757 for (i = 0; i < map->n; ++i) {
9758 uint32_t bmap_hash;
9759 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9760 isl_hash_hash(hash, bmap_hash);
9763 isl_map_free(map);
9765 return hash;
9768 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9770 return isl_map_get_hash((isl_map *)set);
9773 /* Check if the value for dimension dim is completely determined
9774 * by the values of the other parameters and variables.
9775 * That is, check if dimension dim is involved in an equality.
9777 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9779 int i;
9780 unsigned nparam;
9782 if (!bset)
9783 return -1;
9784 nparam = isl_basic_set_n_param(bset);
9785 for (i = 0; i < bset->n_eq; ++i)
9786 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9787 return 1;
9788 return 0;
9791 /* Check if the value for dimension dim is completely determined
9792 * by the values of the other parameters and variables.
9793 * That is, check if dimension dim is involved in an equality
9794 * for each of the subsets.
9796 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9798 int i;
9800 if (!set)
9801 return -1;
9802 for (i = 0; i < set->n; ++i) {
9803 int unique;
9804 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9805 if (unique != 1)
9806 return unique;
9808 return 1;
9811 int isl_set_n_basic_set(__isl_keep isl_set *set)
9813 return set ? set->n : 0;
9816 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9817 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9819 int i;
9821 if (!map)
9822 return -1;
9824 for (i = 0; i < map->n; ++i)
9825 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9826 return -1;
9828 return 0;
9831 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9832 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9834 int i;
9836 if (!set)
9837 return -1;
9839 for (i = 0; i < set->n; ++i)
9840 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9841 return -1;
9843 return 0;
9846 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9848 isl_space *dim;
9850 if (!bset)
9851 return NULL;
9853 bset = isl_basic_set_cow(bset);
9854 if (!bset)
9855 return NULL;
9857 dim = isl_basic_set_get_space(bset);
9858 dim = isl_space_lift(dim, bset->n_div);
9859 if (!dim)
9860 goto error;
9861 isl_space_free(bset->dim);
9862 bset->dim = dim;
9863 bset->extra -= bset->n_div;
9864 bset->n_div = 0;
9866 bset = isl_basic_set_finalize(bset);
9868 return bset;
9869 error:
9870 isl_basic_set_free(bset);
9871 return NULL;
9874 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9876 int i;
9877 isl_space *dim;
9878 unsigned n_div;
9880 set = isl_set_align_divs(set);
9882 if (!set)
9883 return NULL;
9885 set = isl_set_cow(set);
9886 if (!set)
9887 return NULL;
9889 n_div = set->p[0]->n_div;
9890 dim = isl_set_get_space(set);
9891 dim = isl_space_lift(dim, n_div);
9892 if (!dim)
9893 goto error;
9894 isl_space_free(set->dim);
9895 set->dim = dim;
9897 for (i = 0; i < set->n; ++i) {
9898 set->p[i] = isl_basic_set_lift(set->p[i]);
9899 if (!set->p[i])
9900 goto error;
9903 return set;
9904 error:
9905 isl_set_free(set);
9906 return NULL;
9909 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9911 isl_space *dim;
9912 struct isl_basic_map *bmap;
9913 unsigned n_set;
9914 unsigned n_div;
9915 unsigned n_param;
9916 unsigned total;
9917 int i, k, l;
9919 set = isl_set_align_divs(set);
9921 if (!set)
9922 return NULL;
9924 dim = isl_set_get_space(set);
9925 if (set->n == 0 || set->p[0]->n_div == 0) {
9926 isl_set_free(set);
9927 return isl_map_identity(isl_space_map_from_set(dim));
9930 n_div = set->p[0]->n_div;
9931 dim = isl_space_map_from_set(dim);
9932 n_param = isl_space_dim(dim, isl_dim_param);
9933 n_set = isl_space_dim(dim, isl_dim_in);
9934 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9935 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9936 for (i = 0; i < n_set; ++i)
9937 bmap = var_equal(bmap, i);
9939 total = n_param + n_set + n_set + n_div;
9940 for (i = 0; i < n_div; ++i) {
9941 k = isl_basic_map_alloc_inequality(bmap);
9942 if (k < 0)
9943 goto error;
9944 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9945 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9946 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9947 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9948 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9949 set->p[0]->div[i][0]);
9951 l = isl_basic_map_alloc_inequality(bmap);
9952 if (l < 0)
9953 goto error;
9954 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9955 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9956 set->p[0]->div[i][0]);
9957 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9960 isl_set_free(set);
9961 bmap = isl_basic_map_simplify(bmap);
9962 bmap = isl_basic_map_finalize(bmap);
9963 return isl_map_from_basic_map(bmap);
9964 error:
9965 isl_set_free(set);
9966 isl_basic_map_free(bmap);
9967 return NULL;
9970 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9972 unsigned dim;
9973 int size = 0;
9975 if (!bset)
9976 return -1;
9978 dim = isl_basic_set_total_dim(bset);
9979 size += bset->n_eq * (1 + dim);
9980 size += bset->n_ineq * (1 + dim);
9981 size += bset->n_div * (2 + dim);
9983 return size;
9986 int isl_set_size(__isl_keep isl_set *set)
9988 int i;
9989 int size = 0;
9991 if (!set)
9992 return -1;
9994 for (i = 0; i < set->n; ++i)
9995 size += isl_basic_set_size(set->p[i]);
9997 return size;
10000 /* Check if there is any lower bound (if lower == 0) and/or upper
10001 * bound (if upper == 0) on the specified dim.
10003 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10004 enum isl_dim_type type, unsigned pos, int lower, int upper)
10006 int i;
10008 if (!bmap)
10009 return -1;
10011 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10013 pos += isl_basic_map_offset(bmap, type);
10015 for (i = 0; i < bmap->n_div; ++i) {
10016 if (isl_int_is_zero(bmap->div[i][0]))
10017 continue;
10018 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10019 return 1;
10022 for (i = 0; i < bmap->n_eq; ++i)
10023 if (!isl_int_is_zero(bmap->eq[i][pos]))
10024 return 1;
10026 for (i = 0; i < bmap->n_ineq; ++i) {
10027 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10028 if (sgn > 0)
10029 lower = 1;
10030 if (sgn < 0)
10031 upper = 1;
10034 return lower && upper;
10037 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10038 enum isl_dim_type type, unsigned pos)
10040 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10043 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10044 enum isl_dim_type type, unsigned pos)
10046 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10049 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10050 enum isl_dim_type type, unsigned pos)
10052 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10055 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10056 enum isl_dim_type type, unsigned pos)
10058 int i;
10060 if (!map)
10061 return -1;
10063 for (i = 0; i < map->n; ++i) {
10064 int bounded;
10065 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10066 if (bounded < 0 || !bounded)
10067 return bounded;
10070 return 1;
10073 /* Return 1 if the specified dim is involved in both an upper bound
10074 * and a lower bound.
10076 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10077 enum isl_dim_type type, unsigned pos)
10079 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10082 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10084 static int has_any_bound(__isl_keep isl_map *map,
10085 enum isl_dim_type type, unsigned pos,
10086 int (*fn)(__isl_keep isl_basic_map *bmap,
10087 enum isl_dim_type type, unsigned pos))
10089 int i;
10091 if (!map)
10092 return -1;
10094 for (i = 0; i < map->n; ++i) {
10095 int bounded;
10096 bounded = fn(map->p[i], type, pos);
10097 if (bounded < 0 || bounded)
10098 return bounded;
10101 return 0;
10104 /* Return 1 if the specified dim is involved in any lower bound.
10106 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10107 enum isl_dim_type type, unsigned pos)
10109 return has_any_bound(set, type, pos,
10110 &isl_basic_map_dim_has_lower_bound);
10113 /* Return 1 if the specified dim is involved in any upper bound.
10115 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10116 enum isl_dim_type type, unsigned pos)
10118 return has_any_bound(set, type, pos,
10119 &isl_basic_map_dim_has_upper_bound);
10122 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10124 static int has_bound(__isl_keep isl_map *map,
10125 enum isl_dim_type type, unsigned pos,
10126 int (*fn)(__isl_keep isl_basic_map *bmap,
10127 enum isl_dim_type type, unsigned pos))
10129 int i;
10131 if (!map)
10132 return -1;
10134 for (i = 0; i < map->n; ++i) {
10135 int bounded;
10136 bounded = fn(map->p[i], type, pos);
10137 if (bounded < 0 || !bounded)
10138 return bounded;
10141 return 1;
10144 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10146 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10147 enum isl_dim_type type, unsigned pos)
10149 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10152 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10154 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10155 enum isl_dim_type type, unsigned pos)
10157 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10160 /* For each of the "n" variables starting at "first", determine
10161 * the sign of the variable and put the results in the first "n"
10162 * elements of the array "signs".
10163 * Sign
10164 * 1 means that the variable is non-negative
10165 * -1 means that the variable is non-positive
10166 * 0 means the variable attains both positive and negative values.
10168 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10169 unsigned first, unsigned n, int *signs)
10171 isl_vec *bound = NULL;
10172 struct isl_tab *tab = NULL;
10173 struct isl_tab_undo *snap;
10174 int i;
10176 if (!bset || !signs)
10177 return -1;
10179 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10180 tab = isl_tab_from_basic_set(bset, 0);
10181 if (!bound || !tab)
10182 goto error;
10184 isl_seq_clr(bound->el, bound->size);
10185 isl_int_set_si(bound->el[0], -1);
10187 snap = isl_tab_snap(tab);
10188 for (i = 0; i < n; ++i) {
10189 int empty;
10191 isl_int_set_si(bound->el[1 + first + i], -1);
10192 if (isl_tab_add_ineq(tab, bound->el) < 0)
10193 goto error;
10194 empty = tab->empty;
10195 isl_int_set_si(bound->el[1 + first + i], 0);
10196 if (isl_tab_rollback(tab, snap) < 0)
10197 goto error;
10199 if (empty) {
10200 signs[i] = 1;
10201 continue;
10204 isl_int_set_si(bound->el[1 + first + i], 1);
10205 if (isl_tab_add_ineq(tab, bound->el) < 0)
10206 goto error;
10207 empty = tab->empty;
10208 isl_int_set_si(bound->el[1 + first + i], 0);
10209 if (isl_tab_rollback(tab, snap) < 0)
10210 goto error;
10212 signs[i] = empty ? -1 : 0;
10215 isl_tab_free(tab);
10216 isl_vec_free(bound);
10217 return 0;
10218 error:
10219 isl_tab_free(tab);
10220 isl_vec_free(bound);
10221 return -1;
10224 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10225 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10227 if (!bset || !signs)
10228 return -1;
10229 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10230 return -1);
10232 first += pos(bset->dim, type) - 1;
10233 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10236 /* Check if the given basic map is obviously single-valued.
10237 * In particular, for each output dimension, check that there is
10238 * an equality that defines the output dimension in terms of
10239 * earlier dimensions.
10241 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10243 int i, j;
10244 unsigned total;
10245 unsigned n_out;
10246 unsigned o_out;
10248 if (!bmap)
10249 return -1;
10251 total = 1 + isl_basic_map_total_dim(bmap);
10252 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10253 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10255 for (i = 0; i < n_out; ++i) {
10256 for (j = 0; j < bmap->n_eq; ++j) {
10257 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
10258 continue;
10259 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
10260 total - (o_out + i + 1)) == -1)
10261 break;
10263 if (j >= bmap->n_eq)
10264 return 0;
10267 return 1;
10270 /* Check if the given basic map is single-valued.
10271 * We simply compute
10273 * M \circ M^-1
10275 * and check if the result is a subset of the identity mapping.
10277 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10279 isl_space *space;
10280 isl_basic_map *test;
10281 isl_basic_map *id;
10282 int sv;
10284 sv = isl_basic_map_plain_is_single_valued(bmap);
10285 if (sv < 0 || sv)
10286 return sv;
10288 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10289 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10291 space = isl_basic_map_get_space(bmap);
10292 space = isl_space_map_from_set(isl_space_range(space));
10293 id = isl_basic_map_identity(space);
10295 sv = isl_basic_map_is_subset(test, id);
10297 isl_basic_map_free(test);
10298 isl_basic_map_free(id);
10300 return sv;
10303 /* Check if the given map is obviously single-valued.
10305 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10307 if (!map)
10308 return -1;
10309 if (map->n == 0)
10310 return 1;
10311 if (map->n >= 2)
10312 return 0;
10314 return isl_basic_map_plain_is_single_valued(map->p[0]);
10317 /* Check if the given map is single-valued.
10318 * We simply compute
10320 * M \circ M^-1
10322 * and check if the result is a subset of the identity mapping.
10324 int isl_map_is_single_valued(__isl_keep isl_map *map)
10326 isl_space *dim;
10327 isl_map *test;
10328 isl_map *id;
10329 int sv;
10331 sv = isl_map_plain_is_single_valued(map);
10332 if (sv < 0 || sv)
10333 return sv;
10335 test = isl_map_reverse(isl_map_copy(map));
10336 test = isl_map_apply_range(test, isl_map_copy(map));
10338 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10339 id = isl_map_identity(dim);
10341 sv = isl_map_is_subset(test, id);
10343 isl_map_free(test);
10344 isl_map_free(id);
10346 return sv;
10349 int isl_map_is_injective(__isl_keep isl_map *map)
10351 int in;
10353 map = isl_map_copy(map);
10354 map = isl_map_reverse(map);
10355 in = isl_map_is_single_valued(map);
10356 isl_map_free(map);
10358 return in;
10361 /* Check if the given map is obviously injective.
10363 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10365 int in;
10367 map = isl_map_copy(map);
10368 map = isl_map_reverse(map);
10369 in = isl_map_plain_is_single_valued(map);
10370 isl_map_free(map);
10372 return in;
10375 int isl_map_is_bijective(__isl_keep isl_map *map)
10377 int sv;
10379 sv = isl_map_is_single_valued(map);
10380 if (sv < 0 || !sv)
10381 return sv;
10383 return isl_map_is_injective(map);
10386 int isl_set_is_singleton(__isl_keep isl_set *set)
10388 return isl_map_is_single_valued((isl_map *)set);
10391 int isl_map_is_translation(__isl_keep isl_map *map)
10393 int ok;
10394 isl_set *delta;
10396 delta = isl_map_deltas(isl_map_copy(map));
10397 ok = isl_set_is_singleton(delta);
10398 isl_set_free(delta);
10400 return ok;
10403 static int unique(isl_int *p, unsigned pos, unsigned len)
10405 if (isl_seq_first_non_zero(p, pos) != -1)
10406 return 0;
10407 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10408 return 0;
10409 return 1;
10412 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10414 int i, j;
10415 unsigned nvar;
10416 unsigned ovar;
10418 if (!bset)
10419 return -1;
10421 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10422 return 0;
10424 nvar = isl_basic_set_dim(bset, isl_dim_set);
10425 ovar = isl_space_offset(bset->dim, isl_dim_set);
10426 for (j = 0; j < nvar; ++j) {
10427 int lower = 0, upper = 0;
10428 for (i = 0; i < bset->n_eq; ++i) {
10429 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10430 continue;
10431 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10432 return 0;
10433 break;
10435 if (i < bset->n_eq)
10436 continue;
10437 for (i = 0; i < bset->n_ineq; ++i) {
10438 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10439 continue;
10440 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10441 return 0;
10442 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10443 lower = 1;
10444 else
10445 upper = 1;
10447 if (!lower || !upper)
10448 return 0;
10451 return 1;
10454 int isl_set_is_box(__isl_keep isl_set *set)
10456 if (!set)
10457 return -1;
10458 if (set->n != 1)
10459 return 0;
10461 return isl_basic_set_is_box(set->p[0]);
10464 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10466 if (!bset)
10467 return -1;
10469 return isl_space_is_wrapping(bset->dim);
10472 int isl_set_is_wrapping(__isl_keep isl_set *set)
10474 if (!set)
10475 return -1;
10477 return isl_space_is_wrapping(set->dim);
10480 /* Is the domain of "map" a wrapped relation?
10482 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10484 if (!map)
10485 return -1;
10487 return isl_space_domain_is_wrapping(map->dim);
10490 /* Is the range of "map" a wrapped relation?
10492 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10494 if (!map)
10495 return -1;
10497 return isl_space_range_is_wrapping(map->dim);
10500 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10502 bmap = isl_basic_map_cow(bmap);
10503 if (!bmap)
10504 return NULL;
10506 bmap->dim = isl_space_wrap(bmap->dim);
10507 if (!bmap->dim)
10508 goto error;
10510 bmap = isl_basic_map_finalize(bmap);
10512 return (isl_basic_set *)bmap;
10513 error:
10514 isl_basic_map_free(bmap);
10515 return NULL;
10518 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10520 int i;
10522 map = isl_map_cow(map);
10523 if (!map)
10524 return NULL;
10526 for (i = 0; i < map->n; ++i) {
10527 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10528 if (!map->p[i])
10529 goto error;
10531 map->dim = isl_space_wrap(map->dim);
10532 if (!map->dim)
10533 goto error;
10535 return (isl_set *)map;
10536 error:
10537 isl_map_free(map);
10538 return NULL;
10541 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10543 bset = isl_basic_set_cow(bset);
10544 if (!bset)
10545 return NULL;
10547 bset->dim = isl_space_unwrap(bset->dim);
10548 if (!bset->dim)
10549 goto error;
10551 bset = isl_basic_set_finalize(bset);
10553 return (isl_basic_map *)bset;
10554 error:
10555 isl_basic_set_free(bset);
10556 return NULL;
10559 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10561 int i;
10563 if (!set)
10564 return NULL;
10566 if (!isl_set_is_wrapping(set))
10567 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10568 goto error);
10570 set = isl_set_cow(set);
10571 if (!set)
10572 return NULL;
10574 for (i = 0; i < set->n; ++i) {
10575 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10576 if (!set->p[i])
10577 goto error;
10580 set->dim = isl_space_unwrap(set->dim);
10581 if (!set->dim)
10582 goto error;
10584 return (isl_map *)set;
10585 error:
10586 isl_set_free(set);
10587 return NULL;
10590 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10591 enum isl_dim_type type)
10593 if (!bmap)
10594 return NULL;
10596 if (!isl_space_is_named_or_nested(bmap->dim, type))
10597 return bmap;
10599 bmap = isl_basic_map_cow(bmap);
10600 if (!bmap)
10601 return NULL;
10603 bmap->dim = isl_space_reset(bmap->dim, type);
10604 if (!bmap->dim)
10605 goto error;
10607 bmap = isl_basic_map_finalize(bmap);
10609 return bmap;
10610 error:
10611 isl_basic_map_free(bmap);
10612 return NULL;
10615 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10616 enum isl_dim_type type)
10618 int i;
10620 if (!map)
10621 return NULL;
10623 if (!isl_space_is_named_or_nested(map->dim, type))
10624 return map;
10626 map = isl_map_cow(map);
10627 if (!map)
10628 return NULL;
10630 for (i = 0; i < map->n; ++i) {
10631 map->p[i] = isl_basic_map_reset(map->p[i], type);
10632 if (!map->p[i])
10633 goto error;
10635 map->dim = isl_space_reset(map->dim, type);
10636 if (!map->dim)
10637 goto error;
10639 return map;
10640 error:
10641 isl_map_free(map);
10642 return NULL;
10645 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10647 if (!bmap)
10648 return NULL;
10650 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10651 return bmap;
10653 bmap = isl_basic_map_cow(bmap);
10654 if (!bmap)
10655 return NULL;
10657 bmap->dim = isl_space_flatten(bmap->dim);
10658 if (!bmap->dim)
10659 goto error;
10661 bmap = isl_basic_map_finalize(bmap);
10663 return bmap;
10664 error:
10665 isl_basic_map_free(bmap);
10666 return NULL;
10669 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10671 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10674 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10675 __isl_take isl_basic_map *bmap)
10677 if (!bmap)
10678 return NULL;
10680 if (!bmap->dim->nested[0])
10681 return bmap;
10683 bmap = isl_basic_map_cow(bmap);
10684 if (!bmap)
10685 return NULL;
10687 bmap->dim = isl_space_flatten_domain(bmap->dim);
10688 if (!bmap->dim)
10689 goto error;
10691 bmap = isl_basic_map_finalize(bmap);
10693 return bmap;
10694 error:
10695 isl_basic_map_free(bmap);
10696 return NULL;
10699 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10700 __isl_take isl_basic_map *bmap)
10702 if (!bmap)
10703 return NULL;
10705 if (!bmap->dim->nested[1])
10706 return bmap;
10708 bmap = isl_basic_map_cow(bmap);
10709 if (!bmap)
10710 return NULL;
10712 bmap->dim = isl_space_flatten_range(bmap->dim);
10713 if (!bmap->dim)
10714 goto error;
10716 bmap = isl_basic_map_finalize(bmap);
10718 return bmap;
10719 error:
10720 isl_basic_map_free(bmap);
10721 return NULL;
10724 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10726 int i;
10728 if (!map)
10729 return NULL;
10731 if (!map->dim->nested[0] && !map->dim->nested[1])
10732 return map;
10734 map = isl_map_cow(map);
10735 if (!map)
10736 return NULL;
10738 for (i = 0; i < map->n; ++i) {
10739 map->p[i] = isl_basic_map_flatten(map->p[i]);
10740 if (!map->p[i])
10741 goto error;
10743 map->dim = isl_space_flatten(map->dim);
10744 if (!map->dim)
10745 goto error;
10747 return map;
10748 error:
10749 isl_map_free(map);
10750 return NULL;
10753 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10755 return (isl_set *)isl_map_flatten((isl_map *)set);
10758 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10760 isl_space *dim, *flat_dim;
10761 isl_map *map;
10763 dim = isl_set_get_space(set);
10764 flat_dim = isl_space_flatten(isl_space_copy(dim));
10765 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10766 map = isl_map_intersect_domain(map, set);
10768 return map;
10771 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10773 int i;
10775 if (!map)
10776 return NULL;
10778 if (!map->dim->nested[0])
10779 return map;
10781 map = isl_map_cow(map);
10782 if (!map)
10783 return NULL;
10785 for (i = 0; i < map->n; ++i) {
10786 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10787 if (!map->p[i])
10788 goto error;
10790 map->dim = isl_space_flatten_domain(map->dim);
10791 if (!map->dim)
10792 goto error;
10794 return map;
10795 error:
10796 isl_map_free(map);
10797 return NULL;
10800 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10802 int i;
10804 if (!map)
10805 return NULL;
10807 if (!map->dim->nested[1])
10808 return map;
10810 map = isl_map_cow(map);
10811 if (!map)
10812 return NULL;
10814 for (i = 0; i < map->n; ++i) {
10815 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10816 if (!map->p[i])
10817 goto error;
10819 map->dim = isl_space_flatten_range(map->dim);
10820 if (!map->dim)
10821 goto error;
10823 return map;
10824 error:
10825 isl_map_free(map);
10826 return NULL;
10829 /* Reorder the dimensions of "bmap" according to the given dim_map
10830 * and set the dimension specification to "dim".
10832 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10833 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10835 isl_basic_map *res;
10836 unsigned flags;
10838 bmap = isl_basic_map_cow(bmap);
10839 if (!bmap || !dim || !dim_map)
10840 goto error;
10842 flags = bmap->flags;
10843 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10844 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10845 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10846 res = isl_basic_map_alloc_space(dim,
10847 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10848 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10849 if (res)
10850 res->flags = flags;
10851 res = isl_basic_map_finalize(res);
10852 return res;
10853 error:
10854 free(dim_map);
10855 isl_basic_map_free(bmap);
10856 isl_space_free(dim);
10857 return NULL;
10860 /* Reorder the dimensions of "map" according to given reordering.
10862 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10863 __isl_take isl_reordering *r)
10865 int i;
10866 struct isl_dim_map *dim_map;
10868 map = isl_map_cow(map);
10869 dim_map = isl_dim_map_from_reordering(r);
10870 if (!map || !r || !dim_map)
10871 goto error;
10873 for (i = 0; i < map->n; ++i) {
10874 struct isl_dim_map *dim_map_i;
10876 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10878 map->p[i] = isl_basic_map_realign(map->p[i],
10879 isl_space_copy(r->dim), dim_map_i);
10881 if (!map->p[i])
10882 goto error;
10885 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10887 isl_reordering_free(r);
10888 free(dim_map);
10889 return map;
10890 error:
10891 free(dim_map);
10892 isl_map_free(map);
10893 isl_reordering_free(r);
10894 return NULL;
10897 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10898 __isl_take isl_reordering *r)
10900 return (isl_set *)isl_map_realign((isl_map *)set, r);
10903 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10904 __isl_take isl_space *model)
10906 isl_ctx *ctx;
10908 if (!map || !model)
10909 goto error;
10911 ctx = isl_space_get_ctx(model);
10912 if (!isl_space_has_named_params(model))
10913 isl_die(ctx, isl_error_invalid,
10914 "model has unnamed parameters", goto error);
10915 if (!isl_space_has_named_params(map->dim))
10916 isl_die(ctx, isl_error_invalid,
10917 "relation has unnamed parameters", goto error);
10918 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10919 isl_reordering *exp;
10921 model = isl_space_drop_dims(model, isl_dim_in,
10922 0, isl_space_dim(model, isl_dim_in));
10923 model = isl_space_drop_dims(model, isl_dim_out,
10924 0, isl_space_dim(model, isl_dim_out));
10925 exp = isl_parameter_alignment_reordering(map->dim, model);
10926 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10927 map = isl_map_realign(map, exp);
10930 isl_space_free(model);
10931 return map;
10932 error:
10933 isl_space_free(model);
10934 isl_map_free(map);
10935 return NULL;
10938 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10939 __isl_take isl_space *model)
10941 return isl_map_align_params(set, model);
10944 /* Align the parameters of "bmap" to those of "model", introducing
10945 * additional parameters if needed.
10947 __isl_give isl_basic_map *isl_basic_map_align_params(
10948 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10950 isl_ctx *ctx;
10952 if (!bmap || !model)
10953 goto error;
10955 ctx = isl_space_get_ctx(model);
10956 if (!isl_space_has_named_params(model))
10957 isl_die(ctx, isl_error_invalid,
10958 "model has unnamed parameters", goto error);
10959 if (!isl_space_has_named_params(bmap->dim))
10960 isl_die(ctx, isl_error_invalid,
10961 "relation has unnamed parameters", goto error);
10962 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10963 isl_reordering *exp;
10964 struct isl_dim_map *dim_map;
10966 model = isl_space_drop_dims(model, isl_dim_in,
10967 0, isl_space_dim(model, isl_dim_in));
10968 model = isl_space_drop_dims(model, isl_dim_out,
10969 0, isl_space_dim(model, isl_dim_out));
10970 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10971 exp = isl_reordering_extend_space(exp,
10972 isl_basic_map_get_space(bmap));
10973 dim_map = isl_dim_map_from_reordering(exp);
10974 bmap = isl_basic_map_realign(bmap,
10975 exp ? isl_space_copy(exp->dim) : NULL,
10976 isl_dim_map_extend(dim_map, bmap));
10977 isl_reordering_free(exp);
10978 free(dim_map);
10981 isl_space_free(model);
10982 return bmap;
10983 error:
10984 isl_space_free(model);
10985 isl_basic_map_free(bmap);
10986 return NULL;
10989 /* Align the parameters of "bset" to those of "model", introducing
10990 * additional parameters if needed.
10992 __isl_give isl_basic_set *isl_basic_set_align_params(
10993 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10995 return isl_basic_map_align_params(bset, model);
10998 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10999 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11000 enum isl_dim_type c2, enum isl_dim_type c3,
11001 enum isl_dim_type c4, enum isl_dim_type c5)
11003 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11004 struct isl_mat *mat;
11005 int i, j, k;
11006 int pos;
11008 if (!bmap)
11009 return NULL;
11010 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11011 isl_basic_map_total_dim(bmap) + 1);
11012 if (!mat)
11013 return NULL;
11014 for (i = 0; i < bmap->n_eq; ++i)
11015 for (j = 0, pos = 0; j < 5; ++j) {
11016 int off = isl_basic_map_offset(bmap, c[j]);
11017 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11018 isl_int_set(mat->row[i][pos],
11019 bmap->eq[i][off + k]);
11020 ++pos;
11024 return mat;
11027 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11028 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11029 enum isl_dim_type c2, enum isl_dim_type c3,
11030 enum isl_dim_type c4, enum isl_dim_type c5)
11032 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11033 struct isl_mat *mat;
11034 int i, j, k;
11035 int pos;
11037 if (!bmap)
11038 return NULL;
11039 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11040 isl_basic_map_total_dim(bmap) + 1);
11041 if (!mat)
11042 return NULL;
11043 for (i = 0; i < bmap->n_ineq; ++i)
11044 for (j = 0, pos = 0; j < 5; ++j) {
11045 int off = isl_basic_map_offset(bmap, c[j]);
11046 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11047 isl_int_set(mat->row[i][pos],
11048 bmap->ineq[i][off + k]);
11049 ++pos;
11053 return mat;
11056 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11057 __isl_take isl_space *dim,
11058 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11059 enum isl_dim_type c2, enum isl_dim_type c3,
11060 enum isl_dim_type c4, enum isl_dim_type c5)
11062 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11063 isl_basic_map *bmap;
11064 unsigned total;
11065 unsigned extra;
11066 int i, j, k, l;
11067 int pos;
11069 if (!dim || !eq || !ineq)
11070 goto error;
11072 if (eq->n_col != ineq->n_col)
11073 isl_die(dim->ctx, isl_error_invalid,
11074 "equalities and inequalities matrices should have "
11075 "same number of columns", goto error);
11077 total = 1 + isl_space_dim(dim, isl_dim_all);
11079 if (eq->n_col < total)
11080 isl_die(dim->ctx, isl_error_invalid,
11081 "number of columns too small", goto error);
11083 extra = eq->n_col - total;
11085 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11086 eq->n_row, ineq->n_row);
11087 if (!bmap)
11088 goto error;
11089 for (i = 0; i < extra; ++i) {
11090 k = isl_basic_map_alloc_div(bmap);
11091 if (k < 0)
11092 goto error;
11093 isl_int_set_si(bmap->div[k][0], 0);
11095 for (i = 0; i < eq->n_row; ++i) {
11096 l = isl_basic_map_alloc_equality(bmap);
11097 if (l < 0)
11098 goto error;
11099 for (j = 0, pos = 0; j < 5; ++j) {
11100 int off = isl_basic_map_offset(bmap, c[j]);
11101 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11102 isl_int_set(bmap->eq[l][off + k],
11103 eq->row[i][pos]);
11104 ++pos;
11108 for (i = 0; i < ineq->n_row; ++i) {
11109 l = isl_basic_map_alloc_inequality(bmap);
11110 if (l < 0)
11111 goto error;
11112 for (j = 0, pos = 0; j < 5; ++j) {
11113 int off = isl_basic_map_offset(bmap, c[j]);
11114 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11115 isl_int_set(bmap->ineq[l][off + k],
11116 ineq->row[i][pos]);
11117 ++pos;
11122 isl_space_free(dim);
11123 isl_mat_free(eq);
11124 isl_mat_free(ineq);
11126 bmap = isl_basic_map_simplify(bmap);
11127 return isl_basic_map_finalize(bmap);
11128 error:
11129 isl_space_free(dim);
11130 isl_mat_free(eq);
11131 isl_mat_free(ineq);
11132 return NULL;
11135 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11136 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11137 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11139 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11140 c1, c2, c3, c4, isl_dim_in);
11143 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11144 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11145 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11147 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11148 c1, c2, c3, c4, isl_dim_in);
11151 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11152 __isl_take isl_space *dim,
11153 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11154 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11156 return (isl_basic_set*)
11157 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11158 c1, c2, c3, c4, isl_dim_in);
11161 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11163 if (!bmap)
11164 return -1;
11166 return isl_space_can_zip(bmap->dim);
11169 int isl_map_can_zip(__isl_keep isl_map *map)
11171 if (!map)
11172 return -1;
11174 return isl_space_can_zip(map->dim);
11177 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11178 * (A -> C) -> (B -> D).
11180 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11182 unsigned pos;
11183 unsigned n1;
11184 unsigned n2;
11186 if (!bmap)
11187 return NULL;
11189 if (!isl_basic_map_can_zip(bmap))
11190 isl_die(bmap->ctx, isl_error_invalid,
11191 "basic map cannot be zipped", goto error);
11192 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11193 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11194 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11195 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11196 bmap = isl_basic_map_cow(bmap);
11197 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11198 if (!bmap)
11199 return NULL;
11200 bmap->dim = isl_space_zip(bmap->dim);
11201 if (!bmap->dim)
11202 goto error;
11203 return bmap;
11204 error:
11205 isl_basic_map_free(bmap);
11206 return NULL;
11209 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11210 * (A -> C) -> (B -> D).
11212 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11214 int i;
11216 if (!map)
11217 return NULL;
11219 if (!isl_map_can_zip(map))
11220 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11221 goto error);
11223 map = isl_map_cow(map);
11224 if (!map)
11225 return NULL;
11227 for (i = 0; i < map->n; ++i) {
11228 map->p[i] = isl_basic_map_zip(map->p[i]);
11229 if (!map->p[i])
11230 goto error;
11233 map->dim = isl_space_zip(map->dim);
11234 if (!map->dim)
11235 goto error;
11237 return map;
11238 error:
11239 isl_map_free(map);
11240 return NULL;
11243 /* Can we apply isl_basic_map_curry to "bmap"?
11244 * That is, does it have a nested relation in its domain?
11246 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11248 if (!bmap)
11249 return -1;
11251 return isl_space_can_curry(bmap->dim);
11254 /* Can we apply isl_map_curry to "map"?
11255 * That is, does it have a nested relation in its domain?
11257 int isl_map_can_curry(__isl_keep isl_map *map)
11259 if (!map)
11260 return -1;
11262 return isl_space_can_curry(map->dim);
11265 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11266 * A -> (B -> C).
11268 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11271 if (!bmap)
11272 return NULL;
11274 if (!isl_basic_map_can_curry(bmap))
11275 isl_die(bmap->ctx, isl_error_invalid,
11276 "basic map cannot be curried", goto error);
11277 bmap = isl_basic_map_cow(bmap);
11278 if (!bmap)
11279 return NULL;
11280 bmap->dim = isl_space_curry(bmap->dim);
11281 if (!bmap->dim)
11282 goto error;
11283 return bmap;
11284 error:
11285 isl_basic_map_free(bmap);
11286 return NULL;
11289 /* Given a map (A -> B) -> C, return the corresponding map
11290 * A -> (B -> C).
11292 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11294 int i;
11296 if (!map)
11297 return NULL;
11299 if (!isl_map_can_curry(map))
11300 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11301 goto error);
11303 map = isl_map_cow(map);
11304 if (!map)
11305 return NULL;
11307 for (i = 0; i < map->n; ++i) {
11308 map->p[i] = isl_basic_map_curry(map->p[i]);
11309 if (!map->p[i])
11310 goto error;
11313 map->dim = isl_space_curry(map->dim);
11314 if (!map->dim)
11315 goto error;
11317 return map;
11318 error:
11319 isl_map_free(map);
11320 return NULL;
11323 /* Can we apply isl_basic_map_uncurry to "bmap"?
11324 * That is, does it have a nested relation in its domain?
11326 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11328 if (!bmap)
11329 return -1;
11331 return isl_space_can_uncurry(bmap->dim);
11334 /* Can we apply isl_map_uncurry to "map"?
11335 * That is, does it have a nested relation in its domain?
11337 int isl_map_can_uncurry(__isl_keep isl_map *map)
11339 if (!map)
11340 return -1;
11342 return isl_space_can_uncurry(map->dim);
11345 /* Given a basic map A -> (B -> C), return the corresponding basic map
11346 * (A -> B) -> C.
11348 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11351 if (!bmap)
11352 return NULL;
11354 if (!isl_basic_map_can_uncurry(bmap))
11355 isl_die(bmap->ctx, isl_error_invalid,
11356 "basic map cannot be uncurried",
11357 return isl_basic_map_free(bmap));
11358 bmap = isl_basic_map_cow(bmap);
11359 if (!bmap)
11360 return NULL;
11361 bmap->dim = isl_space_uncurry(bmap->dim);
11362 if (!bmap->dim)
11363 return isl_basic_map_free(bmap);
11364 return bmap;
11367 /* Given a map A -> (B -> C), return the corresponding map
11368 * (A -> B) -> C.
11370 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11372 int i;
11374 if (!map)
11375 return NULL;
11377 if (!isl_map_can_uncurry(map))
11378 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11379 return isl_map_free(map));
11381 map = isl_map_cow(map);
11382 if (!map)
11383 return NULL;
11385 for (i = 0; i < map->n; ++i) {
11386 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11387 if (!map->p[i])
11388 return isl_map_free(map);
11391 map->dim = isl_space_uncurry(map->dim);
11392 if (!map->dim)
11393 return isl_map_free(map);
11395 return map;
11398 /* Construct a basic map mapping the domain of the affine expression
11399 * to a one-dimensional range prescribed by the affine expression.
11401 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11403 int k;
11404 int pos;
11405 isl_local_space *ls;
11406 isl_basic_map *bmap;
11408 if (!aff)
11409 return NULL;
11411 ls = isl_aff_get_local_space(aff);
11412 bmap = isl_basic_map_from_local_space(ls);
11413 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11414 k = isl_basic_map_alloc_equality(bmap);
11415 if (k < 0)
11416 goto error;
11418 pos = isl_basic_map_offset(bmap, isl_dim_out);
11419 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11420 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11421 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11422 aff->v->size - (pos + 1));
11424 isl_aff_free(aff);
11425 bmap = isl_basic_map_finalize(bmap);
11426 return bmap;
11427 error:
11428 isl_aff_free(aff);
11429 isl_basic_map_free(bmap);
11430 return NULL;
11433 /* Construct a map mapping the domain of the affine expression
11434 * to a one-dimensional range prescribed by the affine expression.
11436 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11438 isl_basic_map *bmap;
11440 bmap = isl_basic_map_from_aff(aff);
11441 return isl_map_from_basic_map(bmap);
11444 /* Construct a basic map mapping the domain the multi-affine expression
11445 * to its range, with each dimension in the range equated to the
11446 * corresponding affine expression.
11448 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11449 __isl_take isl_multi_aff *maff)
11451 int i;
11452 isl_space *space;
11453 isl_basic_map *bmap;
11455 if (!maff)
11456 return NULL;
11458 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11459 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11460 "invalid space", goto error);
11462 space = isl_space_domain(isl_multi_aff_get_space(maff));
11463 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11465 for (i = 0; i < maff->n; ++i) {
11466 isl_aff *aff;
11467 isl_basic_map *bmap_i;
11469 aff = isl_aff_copy(maff->p[i]);
11470 bmap_i = isl_basic_map_from_aff(aff);
11472 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11475 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11477 isl_multi_aff_free(maff);
11478 return bmap;
11479 error:
11480 isl_multi_aff_free(maff);
11481 return NULL;
11484 /* Construct a map mapping the domain the multi-affine expression
11485 * to its range, with each dimension in the range equated to the
11486 * corresponding affine expression.
11488 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11490 isl_basic_map *bmap;
11492 bmap = isl_basic_map_from_multi_aff(maff);
11493 return isl_map_from_basic_map(bmap);
11496 /* Construct a basic map mapping a domain in the given space to
11497 * to an n-dimensional range, with n the number of elements in the list,
11498 * where each coordinate in the range is prescribed by the
11499 * corresponding affine expression.
11500 * The domains of all affine expressions in the list are assumed to match
11501 * domain_dim.
11503 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11504 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11506 int i;
11507 isl_space *dim;
11508 isl_basic_map *bmap;
11510 if (!list)
11511 return NULL;
11513 dim = isl_space_from_domain(domain_dim);
11514 bmap = isl_basic_map_universe(dim);
11516 for (i = 0; i < list->n; ++i) {
11517 isl_aff *aff;
11518 isl_basic_map *bmap_i;
11520 aff = isl_aff_copy(list->p[i]);
11521 bmap_i = isl_basic_map_from_aff(aff);
11523 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11526 isl_aff_list_free(list);
11527 return bmap;
11530 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11531 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11533 return isl_map_equate(set, type1, pos1, type2, pos2);
11536 /* Construct a basic map where the given dimensions are equal to each other.
11538 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11539 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11541 isl_basic_map *bmap = NULL;
11542 int i;
11544 if (!space)
11545 return NULL;
11547 if (pos1 >= isl_space_dim(space, type1))
11548 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11549 "index out of bounds", goto error);
11550 if (pos2 >= isl_space_dim(space, type2))
11551 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11552 "index out of bounds", goto error);
11554 if (type1 == type2 && pos1 == pos2)
11555 return isl_basic_map_universe(space);
11557 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11558 i = isl_basic_map_alloc_equality(bmap);
11559 if (i < 0)
11560 goto error;
11561 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11562 pos1 += isl_basic_map_offset(bmap, type1);
11563 pos2 += isl_basic_map_offset(bmap, type2);
11564 isl_int_set_si(bmap->eq[i][pos1], -1);
11565 isl_int_set_si(bmap->eq[i][pos2], 1);
11566 bmap = isl_basic_map_finalize(bmap);
11567 isl_space_free(space);
11568 return bmap;
11569 error:
11570 isl_space_free(space);
11571 isl_basic_map_free(bmap);
11572 return NULL;
11575 /* Add a constraint imposing that the given two dimensions are equal.
11577 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11578 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11580 isl_basic_map *eq;
11582 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11584 bmap = isl_basic_map_intersect(bmap, eq);
11586 return bmap;
11589 /* Add a constraint imposing that the given two dimensions are equal.
11591 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11592 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11594 isl_basic_map *bmap;
11596 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11598 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11600 return map;
11603 /* Add a constraint imposing that the given two dimensions have opposite values.
11605 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11606 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11608 isl_basic_map *bmap = NULL;
11609 int i;
11611 if (!map)
11612 return NULL;
11614 if (pos1 >= isl_map_dim(map, type1))
11615 isl_die(map->ctx, isl_error_invalid,
11616 "index out of bounds", goto error);
11617 if (pos2 >= isl_map_dim(map, type2))
11618 isl_die(map->ctx, isl_error_invalid,
11619 "index out of bounds", goto error);
11621 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11622 i = isl_basic_map_alloc_equality(bmap);
11623 if (i < 0)
11624 goto error;
11625 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11626 pos1 += isl_basic_map_offset(bmap, type1);
11627 pos2 += isl_basic_map_offset(bmap, type2);
11628 isl_int_set_si(bmap->eq[i][pos1], 1);
11629 isl_int_set_si(bmap->eq[i][pos2], 1);
11630 bmap = isl_basic_map_finalize(bmap);
11632 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11634 return map;
11635 error:
11636 isl_basic_map_free(bmap);
11637 isl_map_free(map);
11638 return NULL;
11641 /* Construct a constraint imposing that the value of the first dimension is
11642 * greater than or equal to that of the second.
11644 static __isl_give isl_constraint *constraint_order_ge(
11645 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11646 enum isl_dim_type type2, int pos2)
11648 isl_constraint *c;
11650 if (!space)
11651 return NULL;
11653 c = isl_inequality_alloc(isl_local_space_from_space(space));
11655 if (pos1 >= isl_constraint_dim(c, type1))
11656 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11657 "index out of bounds", return isl_constraint_free(c));
11658 if (pos2 >= isl_constraint_dim(c, type2))
11659 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11660 "index out of bounds", return isl_constraint_free(c));
11662 if (type1 == type2 && pos1 == pos2)
11663 return c;
11665 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11666 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11668 return c;
11671 /* Add a constraint imposing that the value of the first dimension is
11672 * greater than or equal to that of the second.
11674 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11675 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11677 isl_constraint *c;
11678 isl_space *space;
11680 if (type1 == type2 && pos1 == pos2)
11681 return bmap;
11682 space = isl_basic_map_get_space(bmap);
11683 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11684 bmap = isl_basic_map_add_constraint(bmap, c);
11686 return bmap;
11689 /* Add a constraint imposing that the value of the first dimension is
11690 * greater than or equal to that of the second.
11692 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11693 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11695 isl_constraint *c;
11696 isl_space *space;
11698 if (type1 == type2 && pos1 == pos2)
11699 return map;
11700 space = isl_map_get_space(map);
11701 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11702 map = isl_map_add_constraint(map, c);
11704 return map;
11707 /* Add a constraint imposing that the value of the first dimension is
11708 * less than or equal to that of the second.
11710 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11711 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11713 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11716 /* Construct a basic map where the value of the first dimension is
11717 * greater than that of the second.
11719 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11720 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11722 isl_basic_map *bmap = NULL;
11723 int i;
11725 if (!space)
11726 return NULL;
11728 if (pos1 >= isl_space_dim(space, type1))
11729 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11730 "index out of bounds", goto error);
11731 if (pos2 >= isl_space_dim(space, type2))
11732 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11733 "index out of bounds", goto error);
11735 if (type1 == type2 && pos1 == pos2)
11736 return isl_basic_map_empty(space);
11738 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11739 i = isl_basic_map_alloc_inequality(bmap);
11740 if (i < 0)
11741 return isl_basic_map_free(bmap);
11742 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11743 pos1 += isl_basic_map_offset(bmap, type1);
11744 pos2 += isl_basic_map_offset(bmap, type2);
11745 isl_int_set_si(bmap->ineq[i][pos1], 1);
11746 isl_int_set_si(bmap->ineq[i][pos2], -1);
11747 isl_int_set_si(bmap->ineq[i][0], -1);
11748 bmap = isl_basic_map_finalize(bmap);
11750 return bmap;
11751 error:
11752 isl_space_free(space);
11753 isl_basic_map_free(bmap);
11754 return NULL;
11757 /* Add a constraint imposing that the value of the first dimension is
11758 * greater than that of the second.
11760 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11761 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11763 isl_basic_map *gt;
11765 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11767 bmap = isl_basic_map_intersect(bmap, gt);
11769 return bmap;
11772 /* Add a constraint imposing that the value of the first dimension is
11773 * greater than that of the second.
11775 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11776 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11778 isl_basic_map *bmap;
11780 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11782 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11784 return map;
11787 /* Add a constraint imposing that the value of the first dimension is
11788 * smaller than that of the second.
11790 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11791 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11793 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11796 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11797 int pos)
11799 isl_aff *div;
11800 isl_local_space *ls;
11802 if (!bmap)
11803 return NULL;
11805 if (!isl_basic_map_divs_known(bmap))
11806 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11807 "some divs are unknown", return NULL);
11809 ls = isl_basic_map_get_local_space(bmap);
11810 div = isl_local_space_get_div(ls, pos);
11811 isl_local_space_free(ls);
11813 return div;
11816 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11817 int pos)
11819 return isl_basic_map_get_div(bset, pos);
11822 /* Plug in "subs" for dimension "type", "pos" of "bset".
11824 * Let i be the dimension to replace and let "subs" be of the form
11826 * f/d
11828 * Any integer division with a non-zero coefficient for i,
11830 * floor((a i + g)/m)
11832 * is replaced by
11834 * floor((a f + d g)/(m d))
11836 * Constraints of the form
11838 * a i + g
11840 * are replaced by
11842 * a f + d g
11844 * We currently require that "subs" is an integral expression.
11845 * Handling rational expressions may require us to add stride constraints
11846 * as we do in isl_basic_set_preimage_multi_aff.
11848 __isl_give isl_basic_set *isl_basic_set_substitute(
11849 __isl_take isl_basic_set *bset,
11850 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11852 int i;
11853 isl_int v;
11854 isl_ctx *ctx;
11856 if (bset && isl_basic_set_plain_is_empty(bset))
11857 return bset;
11859 bset = isl_basic_set_cow(bset);
11860 if (!bset || !subs)
11861 goto error;
11863 ctx = isl_basic_set_get_ctx(bset);
11864 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11865 isl_die(ctx, isl_error_invalid,
11866 "spaces don't match", goto error);
11867 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11868 isl_die(ctx, isl_error_unsupported,
11869 "cannot handle divs yet", goto error);
11870 if (!isl_int_is_one(subs->v->el[0]))
11871 isl_die(ctx, isl_error_invalid,
11872 "can only substitute integer expressions", goto error);
11874 pos += isl_basic_set_offset(bset, type);
11876 isl_int_init(v);
11878 for (i = 0; i < bset->n_eq; ++i) {
11879 if (isl_int_is_zero(bset->eq[i][pos]))
11880 continue;
11881 isl_int_set(v, bset->eq[i][pos]);
11882 isl_int_set_si(bset->eq[i][pos], 0);
11883 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11884 v, subs->v->el + 1, subs->v->size - 1);
11887 for (i = 0; i < bset->n_ineq; ++i) {
11888 if (isl_int_is_zero(bset->ineq[i][pos]))
11889 continue;
11890 isl_int_set(v, bset->ineq[i][pos]);
11891 isl_int_set_si(bset->ineq[i][pos], 0);
11892 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11893 v, subs->v->el + 1, subs->v->size - 1);
11896 for (i = 0; i < bset->n_div; ++i) {
11897 if (isl_int_is_zero(bset->div[i][1 + pos]))
11898 continue;
11899 isl_int_set(v, bset->div[i][1 + pos]);
11900 isl_int_set_si(bset->div[i][1 + pos], 0);
11901 isl_seq_combine(bset->div[i] + 1,
11902 subs->v->el[0], bset->div[i] + 1,
11903 v, subs->v->el + 1, subs->v->size - 1);
11904 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11907 isl_int_clear(v);
11909 bset = isl_basic_set_simplify(bset);
11910 return isl_basic_set_finalize(bset);
11911 error:
11912 isl_basic_set_free(bset);
11913 return NULL;
11916 /* Plug in "subs" for dimension "type", "pos" of "set".
11918 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11919 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11921 int i;
11923 if (set && isl_set_plain_is_empty(set))
11924 return set;
11926 set = isl_set_cow(set);
11927 if (!set || !subs)
11928 goto error;
11930 for (i = set->n - 1; i >= 0; --i) {
11931 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11932 if (remove_if_empty(set, i) < 0)
11933 goto error;
11936 return set;
11937 error:
11938 isl_set_free(set);
11939 return NULL;
11942 /* Check if the range of "ma" is compatible with the domain or range
11943 * (depending on "type") of "bmap".
11944 * Return -1 if anything is wrong.
11946 static int check_basic_map_compatible_range_multi_aff(
11947 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11948 __isl_keep isl_multi_aff *ma)
11950 int m;
11951 isl_space *ma_space;
11953 ma_space = isl_multi_aff_get_space(ma);
11955 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
11956 if (m < 0)
11957 goto error;
11958 if (!m)
11959 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11960 "parameters don't match", goto error);
11961 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11962 if (m < 0)
11963 goto error;
11964 if (!m)
11965 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11966 "spaces don't match", goto error);
11968 isl_space_free(ma_space);
11969 return m;
11970 error:
11971 isl_space_free(ma_space);
11972 return -1;
11975 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11976 * coefficients before the transformed range of dimensions,
11977 * the "n_after" coefficients after the transformed range of dimensions
11978 * and the coefficients of the other divs in "bmap".
11980 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11981 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11983 int i;
11984 int n_param;
11985 int n_set;
11986 isl_local_space *ls;
11988 if (n_div == 0)
11989 return 0;
11991 ls = isl_aff_get_domain_local_space(ma->p[0]);
11992 if (!ls)
11993 return -1;
11995 n_param = isl_local_space_dim(ls, isl_dim_param);
11996 n_set = isl_local_space_dim(ls, isl_dim_set);
11997 for (i = 0; i < n_div; ++i) {
11998 int o_bmap = 0, o_ls = 0;
12000 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12001 o_bmap += 1 + 1 + n_param;
12002 o_ls += 1 + 1 + n_param;
12003 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12004 o_bmap += n_before;
12005 isl_seq_cpy(bmap->div[i] + o_bmap,
12006 ls->div->row[i] + o_ls, n_set);
12007 o_bmap += n_set;
12008 o_ls += n_set;
12009 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12010 o_bmap += n_after;
12011 isl_seq_cpy(bmap->div[i] + o_bmap,
12012 ls->div->row[i] + o_ls, n_div);
12013 o_bmap += n_div;
12014 o_ls += n_div;
12015 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12016 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12017 goto error;
12020 isl_local_space_free(ls);
12021 return 0;
12022 error:
12023 isl_local_space_free(ls);
12024 return -1;
12027 /* How many stride constraints does "ma" enforce?
12028 * That is, how many of the affine expressions have a denominator
12029 * different from one?
12031 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12033 int i;
12034 int strides = 0;
12036 for (i = 0; i < ma->n; ++i)
12037 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12038 strides++;
12040 return strides;
12043 /* For each affine expression in ma of the form
12045 * x_i = (f_i y + h_i)/m_i
12047 * with m_i different from one, add a constraint to "bmap"
12048 * of the form
12050 * f_i y + h_i = m_i alpha_i
12052 * with alpha_i an additional existentially quantified variable.
12054 static __isl_give isl_basic_map *add_ma_strides(
12055 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12056 int n_before, int n_after)
12058 int i, k;
12059 int div;
12060 int total;
12061 int n_param;
12062 int n_in;
12063 int n_div;
12065 total = isl_basic_map_total_dim(bmap);
12066 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12067 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12068 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12069 for (i = 0; i < ma->n; ++i) {
12070 int o_bmap = 0, o_ma = 1;
12072 if (isl_int_is_one(ma->p[i]->v->el[0]))
12073 continue;
12074 div = isl_basic_map_alloc_div(bmap);
12075 k = isl_basic_map_alloc_equality(bmap);
12076 if (div < 0 || k < 0)
12077 goto error;
12078 isl_int_set_si(bmap->div[div][0], 0);
12079 isl_seq_cpy(bmap->eq[k] + o_bmap,
12080 ma->p[i]->v->el + o_ma, 1 + n_param);
12081 o_bmap += 1 + n_param;
12082 o_ma += 1 + n_param;
12083 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12084 o_bmap += n_before;
12085 isl_seq_cpy(bmap->eq[k] + o_bmap,
12086 ma->p[i]->v->el + o_ma, n_in);
12087 o_bmap += n_in;
12088 o_ma += n_in;
12089 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12090 o_bmap += n_after;
12091 isl_seq_cpy(bmap->eq[k] + o_bmap,
12092 ma->p[i]->v->el + o_ma, n_div);
12093 o_bmap += n_div;
12094 o_ma += n_div;
12095 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12096 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12097 total++;
12100 return bmap;
12101 error:
12102 isl_basic_map_free(bmap);
12103 return NULL;
12106 /* Replace the domain or range space (depending on "type) of "space" by "set".
12108 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12109 enum isl_dim_type type, __isl_take isl_space *set)
12111 if (type == isl_dim_in) {
12112 space = isl_space_range(space);
12113 space = isl_space_map_from_domain_and_range(set, space);
12114 } else {
12115 space = isl_space_domain(space);
12116 space = isl_space_map_from_domain_and_range(space, set);
12119 return space;
12122 /* Compute the preimage of the domain or range (depending on "type")
12123 * of "bmap" under the function represented by "ma".
12124 * In other words, plug in "ma" in the domain or range of "bmap".
12125 * The result is a basic map that lives in the same space as "bmap"
12126 * except that the domain or range has been replaced by
12127 * the domain space of "ma".
12129 * If bmap is represented by
12131 * A(p) + S u + B x + T v + C(divs) >= 0,
12133 * where u and x are input and output dimensions if type == isl_dim_out
12134 * while x and v are input and output dimensions if type == isl_dim_in,
12135 * and ma is represented by
12137 * x = D(p) + F(y) + G(divs')
12139 * then the result is
12141 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12143 * The divs in the input set are similarly adjusted.
12144 * In particular
12146 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12148 * becomes
12150 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12151 * B_i G(divs') + c_i(divs))/n_i)
12153 * If bmap is not a rational map and if F(y) involves any denominators
12155 * x_i = (f_i y + h_i)/m_i
12157 * then additional constraints are added to ensure that we only
12158 * map back integer points. That is we enforce
12160 * f_i y + h_i = m_i alpha_i
12162 * with alpha_i an additional existentially quantified variable.
12164 * We first copy over the divs from "ma".
12165 * Then we add the modified constraints and divs from "bmap".
12166 * Finally, we add the stride constraints, if needed.
12168 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12169 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12170 __isl_take isl_multi_aff *ma)
12172 int i, k;
12173 isl_space *space;
12174 isl_basic_map *res = NULL;
12175 int n_before, n_after, n_div_bmap, n_div_ma;
12176 isl_int f, c1, c2, g;
12177 int rational, strides;
12179 isl_int_init(f);
12180 isl_int_init(c1);
12181 isl_int_init(c2);
12182 isl_int_init(g);
12184 ma = isl_multi_aff_align_divs(ma);
12185 if (!bmap || !ma)
12186 goto error;
12187 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12188 goto error;
12190 if (type == isl_dim_in) {
12191 n_before = 0;
12192 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12193 } else {
12194 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12195 n_after = 0;
12197 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12198 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12200 space = isl_multi_aff_get_domain_space(ma);
12201 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12202 rational = isl_basic_map_is_rational(bmap);
12203 strides = rational ? 0 : multi_aff_strides(ma);
12204 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12205 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12206 if (rational)
12207 res = isl_basic_map_set_rational(res);
12209 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12210 if (isl_basic_map_alloc_div(res) < 0)
12211 goto error;
12213 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12214 goto error;
12216 for (i = 0; i < bmap->n_eq; ++i) {
12217 k = isl_basic_map_alloc_equality(res);
12218 if (k < 0)
12219 goto error;
12220 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12221 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12224 for (i = 0; i < bmap->n_ineq; ++i) {
12225 k = isl_basic_map_alloc_inequality(res);
12226 if (k < 0)
12227 goto error;
12228 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12229 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12232 for (i = 0; i < bmap->n_div; ++i) {
12233 if (isl_int_is_zero(bmap->div[i][0])) {
12234 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12235 continue;
12237 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12238 n_before, n_after, n_div_ma, n_div_bmap,
12239 f, c1, c2, g, 1);
12242 if (strides)
12243 res = add_ma_strides(res, ma, n_before, n_after);
12245 isl_int_clear(f);
12246 isl_int_clear(c1);
12247 isl_int_clear(c2);
12248 isl_int_clear(g);
12249 isl_basic_map_free(bmap);
12250 isl_multi_aff_free(ma);
12251 res = isl_basic_set_simplify(res);
12252 return isl_basic_map_finalize(res);
12253 error:
12254 isl_int_clear(f);
12255 isl_int_clear(c1);
12256 isl_int_clear(c2);
12257 isl_int_clear(g);
12258 isl_basic_map_free(bmap);
12259 isl_multi_aff_free(ma);
12260 isl_basic_map_free(res);
12261 return NULL;
12264 /* Compute the preimage of "bset" under the function represented by "ma".
12265 * In other words, plug in "ma" in "bset". The result is a basic set
12266 * that lives in the domain space of "ma".
12268 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12269 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12271 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12274 /* Compute the preimage of the domain of "bmap" under the function
12275 * represented by "ma".
12276 * In other words, plug in "ma" in the domain of "bmap".
12277 * The result is a basic map that lives in the same space as "bmap"
12278 * except that the domain has been replaced by the domain space of "ma".
12280 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12281 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12283 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12286 /* Compute the preimage of the range of "bmap" under the function
12287 * represented by "ma".
12288 * In other words, plug in "ma" in the range of "bmap".
12289 * The result is a basic map that lives in the same space as "bmap"
12290 * except that the range has been replaced by the domain space of "ma".
12292 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12293 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12295 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12298 /* Check if the range of "ma" is compatible with the domain or range
12299 * (depending on "type") of "map".
12300 * Return -1 if anything is wrong.
12302 static int check_map_compatible_range_multi_aff(
12303 __isl_keep isl_map *map, enum isl_dim_type type,
12304 __isl_keep isl_multi_aff *ma)
12306 int m;
12307 isl_space *ma_space;
12309 ma_space = isl_multi_aff_get_space(ma);
12310 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12311 isl_space_free(ma_space);
12312 if (m >= 0 && !m)
12313 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12314 "spaces don't match", return -1);
12315 return m;
12318 /* Compute the preimage of the domain or range (depending on "type")
12319 * of "map" under the function represented by "ma".
12320 * In other words, plug in "ma" in the domain or range of "map".
12321 * The result is a map that lives in the same space as "map"
12322 * except that the domain or range has been replaced by
12323 * the domain space of "ma".
12325 * The parameters are assumed to have been aligned.
12327 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12328 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12330 int i;
12331 isl_space *space;
12333 map = isl_map_cow(map);
12334 ma = isl_multi_aff_align_divs(ma);
12335 if (!map || !ma)
12336 goto error;
12337 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12338 goto error;
12340 for (i = 0; i < map->n; ++i) {
12341 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12342 isl_multi_aff_copy(ma));
12343 if (!map->p[i])
12344 goto error;
12347 space = isl_multi_aff_get_domain_space(ma);
12348 space = isl_space_set(isl_map_get_space(map), type, space);
12350 isl_space_free(map->dim);
12351 map->dim = space;
12352 if (!map->dim)
12353 goto error;
12355 isl_multi_aff_free(ma);
12356 if (map->n > 1)
12357 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12358 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12359 return map;
12360 error:
12361 isl_multi_aff_free(ma);
12362 isl_map_free(map);
12363 return NULL;
12366 /* Compute the preimage of the domain or range (depending on "type")
12367 * of "map" under the function represented by "ma".
12368 * In other words, plug in "ma" in the domain or range of "map".
12369 * The result is a map that lives in the same space as "map"
12370 * except that the domain or range has been replaced by
12371 * the domain space of "ma".
12373 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12374 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12376 if (!map || !ma)
12377 goto error;
12379 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12380 return map_preimage_multi_aff(map, type, ma);
12382 if (!isl_space_has_named_params(map->dim) ||
12383 !isl_space_has_named_params(ma->space))
12384 isl_die(map->ctx, isl_error_invalid,
12385 "unaligned unnamed parameters", goto error);
12386 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12387 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12389 return map_preimage_multi_aff(map, type, ma);
12390 error:
12391 isl_multi_aff_free(ma);
12392 return isl_map_free(map);
12395 /* Compute the preimage of "set" under the function represented by "ma".
12396 * In other words, plug in "ma" in "set". The result is a set
12397 * that lives in the domain space of "ma".
12399 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12400 __isl_take isl_multi_aff *ma)
12402 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12405 /* Compute the preimage of the domain of "map" under the function
12406 * represented by "ma".
12407 * In other words, plug in "ma" in the domain of "map".
12408 * The result is a map that lives in the same space as "map"
12409 * except that the domain has been replaced by the domain space of "ma".
12411 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12412 __isl_take isl_multi_aff *ma)
12414 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12417 /* Compute the preimage of the range of "map" under the function
12418 * represented by "ma".
12419 * In other words, plug in "ma" in the range of "map".
12420 * The result is a map that lives in the same space as "map"
12421 * except that the range has been replaced by the domain space of "ma".
12423 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12424 __isl_take isl_multi_aff *ma)
12426 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12429 /* Compute the preimage of "map" under the function represented by "pma".
12430 * In other words, plug in "pma" in the domain or range of "map".
12431 * The result is a map that lives in the same space as "map",
12432 * except that the space of type "type" has been replaced by
12433 * the domain space of "pma".
12435 * The parameters of "map" and "pma" are assumed to have been aligned.
12437 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12438 __isl_take isl_map *map, enum isl_dim_type type,
12439 __isl_take isl_pw_multi_aff *pma)
12441 int i;
12442 isl_map *res;
12444 if (!pma)
12445 goto error;
12447 if (pma->n == 0) {
12448 isl_pw_multi_aff_free(pma);
12449 res = isl_map_empty(isl_map_get_space(map));
12450 isl_map_free(map);
12451 return res;
12454 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12455 isl_multi_aff_copy(pma->p[0].maff));
12456 if (type == isl_dim_in)
12457 res = isl_map_intersect_domain(res,
12458 isl_map_copy(pma->p[0].set));
12459 else
12460 res = isl_map_intersect_range(res,
12461 isl_map_copy(pma->p[0].set));
12463 for (i = 1; i < pma->n; ++i) {
12464 isl_map *res_i;
12466 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12467 isl_multi_aff_copy(pma->p[i].maff));
12468 if (type == isl_dim_in)
12469 res_i = isl_map_intersect_domain(res_i,
12470 isl_map_copy(pma->p[i].set));
12471 else
12472 res_i = isl_map_intersect_range(res_i,
12473 isl_map_copy(pma->p[i].set));
12474 res = isl_map_union(res, res_i);
12477 isl_pw_multi_aff_free(pma);
12478 isl_map_free(map);
12479 return res;
12480 error:
12481 isl_pw_multi_aff_free(pma);
12482 isl_map_free(map);
12483 return NULL;
12486 /* Compute the preimage of "map" under the function represented by "pma".
12487 * In other words, plug in "pma" in the domain or range of "map".
12488 * The result is a map that lives in the same space as "map",
12489 * except that the space of type "type" has been replaced by
12490 * the domain space of "pma".
12492 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12493 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12495 if (!map || !pma)
12496 goto error;
12498 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12499 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12501 if (!isl_space_has_named_params(map->dim) ||
12502 !isl_space_has_named_params(pma->dim))
12503 isl_die(map->ctx, isl_error_invalid,
12504 "unaligned unnamed parameters", goto error);
12505 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12506 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12508 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12509 error:
12510 isl_pw_multi_aff_free(pma);
12511 return isl_map_free(map);
12514 /* Compute the preimage of "set" under the function represented by "pma".
12515 * In other words, plug in "pma" in "set". The result is a set
12516 * that lives in the domain space of "pma".
12518 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12519 __isl_take isl_pw_multi_aff *pma)
12521 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12524 /* Compute the preimage of the domain of "map" under the function
12525 * represented by "pma".
12526 * In other words, plug in "pma" in the domain of "map".
12527 * The result is a map that lives in the same space as "map",
12528 * except that domain space has been replaced by the domain space of "pma".
12530 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12531 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12533 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12536 /* Compute the preimage of the range of "map" under the function
12537 * represented by "pma".
12538 * In other words, plug in "pma" in the range of "map".
12539 * The result is a map that lives in the same space as "map",
12540 * except that range space has been replaced by the domain space of "pma".
12542 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12543 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12545 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12548 /* Compute the preimage of "map" under the function represented by "mpa".
12549 * In other words, plug in "mpa" in the domain or range of "map".
12550 * The result is a map that lives in the same space as "map",
12551 * except that the space of type "type" has been replaced by
12552 * the domain space of "mpa".
12554 * If the map does not involve any constraints that refer to the
12555 * dimensions of the substituted space, then the only possible
12556 * effect of "mpa" on the map is to map the space to a different space.
12557 * We create a separate isl_multi_aff to effectuate this change
12558 * in order to avoid spurious splitting of the map along the pieces
12559 * of "mpa".
12561 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12562 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12564 int n;
12565 isl_pw_multi_aff *pma;
12567 if (!map || !mpa)
12568 goto error;
12570 n = isl_map_dim(map, type);
12571 if (!isl_map_involves_dims(map, type, 0, n)) {
12572 isl_space *space;
12573 isl_multi_aff *ma;
12575 space = isl_multi_pw_aff_get_space(mpa);
12576 isl_multi_pw_aff_free(mpa);
12577 ma = isl_multi_aff_zero(space);
12578 return isl_map_preimage_multi_aff(map, type, ma);
12581 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12582 return isl_map_preimage_pw_multi_aff(map, type, pma);
12583 error:
12584 isl_map_free(map);
12585 isl_multi_pw_aff_free(mpa);
12586 return NULL;
12589 /* Compute the preimage of "map" under the function represented by "mpa".
12590 * In other words, plug in "mpa" in the domain "map".
12591 * The result is a map that lives in the same space as "map",
12592 * except that domain space has been replaced by the domain space of "mpa".
12594 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12595 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12597 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12600 /* Compute the preimage of "set" by the function represented by "mpa".
12601 * In other words, plug in "mpa" in "set".
12603 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12604 __isl_take isl_multi_pw_aff *mpa)
12606 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);