isl_union_map_compute_flow: return accesses with no source
[isl.git] / isl_map.c
blob5b3281e91bf01d3936478c110cced1408e562d26
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 #include <string.h>
14 #include <strings.h>
15 #include <isl_map_private.h>
16 #include <isl/ctx.h>
17 #include <isl/blk.h>
18 #include "isl_dim_private.h"
19 #include "isl_equalities.h"
20 #include <isl/list.h>
21 #include <isl/lp.h>
22 #include <isl/seq.h>
23 #include <isl/set.h>
24 #include <isl/map.h>
25 #include "isl_map_piplib.h"
26 #include <isl_reordering.h>
27 #include "isl_sample.h"
28 #include "isl_tab.h"
29 #include <isl/vec.h>
30 #include <isl_mat_private.h>
32 /* Maps dst positions to src positions */
33 struct isl_dim_map {
34 unsigned len;
35 int pos[1];
38 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
40 int i;
41 struct isl_dim_map *dim_map;
42 dim_map = isl_alloc(ctx, struct isl_dim_map,
43 sizeof(struct isl_dim_map) + len * sizeof(int));
44 if (!dim_map)
45 return NULL;
46 dim_map->len = 1 + len;
47 dim_map->pos[0] = 0;
48 for (i = 0; i < len; ++i)
49 dim_map->pos[1 + i] = -1;
50 return dim_map;
53 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
55 switch (type) {
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
60 default: return 0;
64 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
75 struct isl_dim *dim, enum isl_dim_type type,
76 unsigned first, unsigned n, unsigned dst_pos)
78 int i;
79 unsigned src_pos;
81 if (!dim_map || !dim)
82 return;
84 src_pos = pos(dim, type);
85 for (i = 0; i < n; ++i)
86 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
89 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
90 enum isl_dim_type type, unsigned dst_pos)
92 isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
95 static void isl_dim_map_div(struct isl_dim_map *dim_map,
96 struct isl_basic_map *bmap, unsigned dst_pos)
98 int i;
99 unsigned src_pos;
101 if (!dim_map || !bmap)
102 return;
104 src_pos = 1 + isl_dim_total(bmap->dim);
105 for (i = 0; i < bmap->n_div; ++i)
106 dim_map->pos[1 + dst_pos + i] = src_pos + i;
109 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
111 int i;
113 for (i = 0; i < dim_map->len; ++i)
114 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
115 fprintf(stderr, "\n");
118 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
119 enum isl_dim_type type)
121 if (!bmap)
122 return 0;
123 switch (type) {
124 case isl_dim_cst: return 1;
125 case isl_dim_param:
126 case isl_dim_in:
127 case isl_dim_out: return isl_dim_size(bmap->dim, type);
128 case isl_dim_div: return bmap->n_div;
129 case isl_dim_all: return isl_basic_map_total_dim(bmap);
130 default: return 0;
134 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
136 return map ? n(map->dim, type) : 0;
139 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
141 return set ? n(set->dim, type) : 0;
144 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
145 enum isl_dim_type type)
147 struct isl_dim *dim = bmap->dim;
148 switch (type) {
149 case isl_dim_cst: return 0;
150 case isl_dim_param: return 1;
151 case isl_dim_in: return 1 + dim->nparam;
152 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
153 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
154 default: return 0;
158 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
160 return pos(map->dim, type);
163 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
164 enum isl_dim_type type)
166 return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
169 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
171 return isl_basic_set_dim(bset, isl_dim_set);
174 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
176 return isl_basic_set_dim(bset, isl_dim_param);
179 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
181 return isl_dim_total(bset->dim) + bset->n_div;
184 unsigned isl_set_n_dim(const struct isl_set *set)
186 return isl_set_dim(set, isl_dim_set);
189 unsigned isl_set_n_param(const struct isl_set *set)
191 return isl_set_dim(set, isl_dim_param);
194 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
196 return bmap ? bmap->dim->n_in : 0;
199 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
201 return bmap ? bmap->dim->n_out : 0;
204 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
206 return bmap ? bmap->dim->nparam : 0;
209 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
211 return bmap ? bmap->n_div : 0;
214 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
216 return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
219 unsigned isl_map_n_in(const struct isl_map *map)
221 return map->dim->n_in;
224 unsigned isl_map_n_out(const struct isl_map *map)
226 return map->dim->n_out;
229 unsigned isl_map_n_param(const struct isl_map *map)
231 return map->dim->nparam;
234 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
236 int m;
237 if (!map || !set)
238 return -1;
239 m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
240 if (m < 0 || !m)
241 return m;
242 return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
245 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
246 struct isl_basic_set *bset)
248 int m;
249 if (!bmap || !bset)
250 return -1;
251 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
252 if (m < 0 || !m)
253 return m;
254 return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
257 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
258 struct isl_basic_set *bset)
260 int m;
261 if (!bmap || !bset)
262 return -1;
263 m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
264 if (m < 0 || !m)
265 return m;
266 return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
269 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
271 return bmap ? bmap->ctx : NULL;
274 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
276 return bset ? bset->ctx : NULL;
279 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
281 return map ? map->ctx : NULL;
284 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
286 return set ? set->ctx : NULL;
289 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
291 if (!bmap)
292 return NULL;
293 return isl_dim_copy(bmap->dim);
296 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
298 if (!bset)
299 return NULL;
300 return isl_dim_copy(bset->dim);
303 struct isl_dim *isl_map_get_dim(struct isl_map *map)
305 if (!map)
306 return NULL;
307 return isl_dim_copy(map->dim);
310 struct isl_dim *isl_set_get_dim(struct isl_set *set)
312 if (!set)
313 return NULL;
314 return isl_dim_copy(set->dim);
317 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
318 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
320 bmap = isl_basic_map_cow(bmap);
321 if (!bmap)
322 return NULL;
323 bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
324 if (!bmap->dim)
325 goto error;
326 bmap = isl_basic_map_finalize(bmap);
327 return bmap;
328 error:
329 isl_basic_map_free(bmap);
330 return NULL;
333 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
334 enum isl_dim_type type)
336 return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
339 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
340 enum isl_dim_type type, const char *s)
342 int i;
344 map = isl_map_cow(map);
345 if (!map)
346 return NULL;
348 map->dim = isl_dim_set_tuple_name(map->dim, type, s);
349 if (!map->dim)
350 goto error;
352 for (i = 0; i < map->n; ++i) {
353 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
354 if (!map->p[i])
355 goto error;
358 return map;
359 error:
360 isl_map_free(map);
361 return NULL;
364 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
365 enum isl_dim_type type)
367 return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
370 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
371 const char *s)
373 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
376 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
378 return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
381 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
383 return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
386 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
387 enum isl_dim_type type, unsigned pos)
389 return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
392 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
393 enum isl_dim_type type, unsigned pos)
395 return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
398 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
399 enum isl_dim_type type, unsigned pos)
401 return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
404 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
405 __isl_take isl_basic_map *bmap,
406 enum isl_dim_type type, unsigned pos, const char *s)
408 if (!bmap)
409 return NULL;
410 bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
411 if (!bmap->dim)
412 goto error;
413 return bmap;
414 error:
415 isl_basic_map_free(bmap);
416 return NULL;
419 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
420 enum isl_dim_type type, unsigned pos, const char *s)
422 int i;
424 if (!map)
425 return NULL;
427 map->dim = isl_dim_set_name(map->dim, type, pos, s);
428 if (!map->dim)
429 goto error;
431 for (i = 0; i < map->n; ++i) {
432 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
433 if (!map->p[i])
434 goto error;
437 return map;
438 error:
439 isl_map_free(map);
440 return NULL;
443 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
444 __isl_take isl_basic_set *bset,
445 enum isl_dim_type type, unsigned pos, const char *s)
447 return (isl_basic_set *)isl_basic_map_set_dim_name(
448 (isl_basic_map *)bset, type, pos, s);
451 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
452 enum isl_dim_type type, unsigned pos, const char *s)
454 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
457 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
459 if (!bmap)
460 return -1;
461 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
464 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
465 struct isl_basic_map *bmap, unsigned extra,
466 unsigned n_eq, unsigned n_ineq)
468 int i;
469 size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
471 bmap->ctx = ctx;
472 isl_ctx_ref(ctx);
474 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
475 if (isl_blk_is_error(bmap->block))
476 goto error;
478 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
479 if (!bmap->ineq)
480 goto error;
482 if (extra == 0) {
483 bmap->block2 = isl_blk_empty();
484 bmap->div = NULL;
485 } else {
486 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
487 if (isl_blk_is_error(bmap->block2))
488 goto error;
490 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
491 if (!bmap->div)
492 goto error;
495 for (i = 0; i < n_ineq + n_eq; ++i)
496 bmap->ineq[i] = bmap->block.data + i * row_size;
498 for (i = 0; i < extra; ++i)
499 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
501 bmap->ref = 1;
502 bmap->flags = 0;
503 bmap->c_size = n_eq + n_ineq;
504 bmap->eq = bmap->ineq + n_ineq;
505 bmap->extra = extra;
506 bmap->n_eq = 0;
507 bmap->n_ineq = 0;
508 bmap->n_div = 0;
509 bmap->sample = NULL;
511 return bmap;
512 error:
513 isl_basic_map_free(bmap);
514 return NULL;
517 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
518 unsigned nparam, unsigned dim, unsigned extra,
519 unsigned n_eq, unsigned n_ineq)
521 struct isl_basic_map *bmap;
522 bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
523 return (struct isl_basic_set *)bmap;
526 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
527 unsigned extra, unsigned n_eq, unsigned n_ineq)
529 struct isl_basic_map *bmap;
530 if (!dim)
531 return NULL;
532 isl_assert(dim->ctx, dim->n_in == 0, goto error);
533 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
534 return (struct isl_basic_set *)bmap;
535 error:
536 isl_dim_free(dim);
537 return NULL;
540 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
541 unsigned extra, unsigned n_eq, unsigned n_ineq)
543 struct isl_basic_map *bmap;
545 if (!dim)
546 return NULL;
547 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
548 if (!bmap)
549 goto error;
550 bmap->dim = dim;
552 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
553 error:
554 isl_dim_free(dim);
555 return NULL;
558 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
559 unsigned nparam, unsigned in, unsigned out, unsigned extra,
560 unsigned n_eq, unsigned n_ineq)
562 struct isl_basic_map *bmap;
563 struct isl_dim *dim;
565 dim = isl_dim_alloc(ctx, nparam, in, out);
566 if (!dim)
567 return NULL;
569 bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
570 return bmap;
573 static void dup_constraints(
574 struct isl_basic_map *dst, struct isl_basic_map *src)
576 int i;
577 unsigned total = isl_basic_map_total_dim(src);
579 for (i = 0; i < src->n_eq; ++i) {
580 int j = isl_basic_map_alloc_equality(dst);
581 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
584 for (i = 0; i < src->n_ineq; ++i) {
585 int j = isl_basic_map_alloc_inequality(dst);
586 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
589 for (i = 0; i < src->n_div; ++i) {
590 int j = isl_basic_map_alloc_div(dst);
591 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
593 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
596 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
598 struct isl_basic_map *dup;
600 if (!bmap)
601 return NULL;
602 dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
603 bmap->n_div, bmap->n_eq, bmap->n_ineq);
604 if (!dup)
605 return NULL;
606 dup_constraints(dup, bmap);
607 dup->flags = bmap->flags;
608 dup->sample = isl_vec_copy(bmap->sample);
609 return dup;
612 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
614 struct isl_basic_map *dup;
616 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
617 return (struct isl_basic_set *)dup;
620 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
622 if (!bset)
623 return NULL;
625 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
626 bset->ref++;
627 return bset;
629 return isl_basic_set_dup(bset);
632 struct isl_set *isl_set_copy(struct isl_set *set)
634 if (!set)
635 return NULL;
637 set->ref++;
638 return set;
641 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
643 if (!bmap)
644 return NULL;
646 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
647 bmap->ref++;
648 return bmap;
650 bmap = isl_basic_map_dup(bmap);
651 if (bmap)
652 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
653 return bmap;
656 struct isl_map *isl_map_copy(struct isl_map *map)
658 if (!map)
659 return NULL;
661 map->ref++;
662 return map;
665 void isl_basic_map_free(struct isl_basic_map *bmap)
667 if (!bmap)
668 return;
670 if (--bmap->ref > 0)
671 return;
673 isl_ctx_deref(bmap->ctx);
674 free(bmap->div);
675 isl_blk_free(bmap->ctx, bmap->block2);
676 free(bmap->ineq);
677 isl_blk_free(bmap->ctx, bmap->block);
678 isl_vec_free(bmap->sample);
679 isl_dim_free(bmap->dim);
680 free(bmap);
683 void isl_basic_set_free(struct isl_basic_set *bset)
685 isl_basic_map_free((struct isl_basic_map *)bset);
688 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
690 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
693 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
695 struct isl_ctx *ctx;
696 if (!bmap)
697 return -1;
698 ctx = bmap->ctx;
699 isl_assert(ctx, room_for_con(bmap, 1), return -1);
700 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
701 return -1);
702 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
703 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
704 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
705 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
706 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
707 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
708 isl_int *t;
709 int j = isl_basic_map_alloc_inequality(bmap);
710 if (j < 0)
711 return -1;
712 t = bmap->ineq[j];
713 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
714 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
715 bmap->eq[-1] = t;
716 bmap->n_eq++;
717 bmap->n_ineq--;
718 bmap->eq--;
719 return 0;
721 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
722 bmap->extra - bmap->n_div);
723 return bmap->n_eq++;
726 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
728 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
731 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
733 if (!bmap)
734 return -1;
735 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
736 bmap->n_eq -= n;
737 return 0;
740 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
742 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
745 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
747 isl_int *t;
748 if (!bmap)
749 return -1;
750 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
752 if (pos != bmap->n_eq - 1) {
753 t = bmap->eq[pos];
754 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
755 bmap->eq[bmap->n_eq - 1] = t;
757 bmap->n_eq--;
758 return 0;
761 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
763 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
766 void isl_basic_map_inequality_to_equality(
767 struct isl_basic_map *bmap, unsigned pos)
769 isl_int *t;
771 t = bmap->ineq[pos];
772 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
773 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
774 bmap->eq[-1] = t;
775 bmap->n_eq++;
776 bmap->n_ineq--;
777 bmap->eq--;
778 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
779 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
780 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
781 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
784 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
786 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
789 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
791 struct isl_ctx *ctx;
792 if (!bmap)
793 return -1;
794 ctx = bmap->ctx;
795 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
796 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
797 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
798 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
799 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
800 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
801 1 + isl_basic_map_total_dim(bmap),
802 bmap->extra - bmap->n_div);
803 return bmap->n_ineq++;
806 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
808 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
811 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
813 if (!bmap)
814 return -1;
815 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
816 bmap->n_ineq -= n;
817 return 0;
820 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
822 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
825 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
827 isl_int *t;
828 if (!bmap)
829 return -1;
830 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
832 if (pos != bmap->n_ineq - 1) {
833 t = bmap->ineq[pos];
834 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
835 bmap->ineq[bmap->n_ineq - 1] = t;
836 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
838 bmap->n_ineq--;
839 return 0;
842 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
844 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
847 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
848 isl_int *eq)
850 int k;
852 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
853 if (!bmap)
854 return NULL;
855 k = isl_basic_map_alloc_equality(bmap);
856 if (k < 0)
857 goto error;
858 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
859 return bmap;
860 error:
861 isl_basic_map_free(bmap);
862 return NULL;
865 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
866 isl_int *eq)
868 return (isl_basic_set *)
869 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
872 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
873 isl_int *ineq)
875 int k;
877 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
878 if (!bmap)
879 return NULL;
880 k = isl_basic_map_alloc_inequality(bmap);
881 if (k < 0)
882 goto error;
883 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
884 return bmap;
885 error:
886 isl_basic_map_free(bmap);
887 return NULL;
890 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
891 isl_int *ineq)
893 return (isl_basic_set *)
894 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
897 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
899 if (!bmap)
900 return -1;
901 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
902 isl_seq_clr(bmap->div[bmap->n_div] +
903 1 + 1 + isl_basic_map_total_dim(bmap),
904 bmap->extra - bmap->n_div);
905 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
906 return bmap->n_div++;
909 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
911 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
914 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
916 if (!bmap)
917 return -1;
918 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
919 bmap->n_div -= n;
920 return 0;
923 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
925 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
928 /* Copy constraint from src to dst, putting the vars of src at offset
929 * dim_off in dst and the divs of src at offset div_off in dst.
930 * If both sets are actually map, then dim_off applies to the input
931 * variables.
933 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
934 struct isl_basic_map *src_map, isl_int *src,
935 unsigned in_off, unsigned out_off, unsigned div_off)
937 unsigned src_nparam = isl_basic_map_n_param(src_map);
938 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
939 unsigned src_in = isl_basic_map_n_in(src_map);
940 unsigned dst_in = isl_basic_map_n_in(dst_map);
941 unsigned src_out = isl_basic_map_n_out(src_map);
942 unsigned dst_out = isl_basic_map_n_out(dst_map);
943 isl_int_set(dst[0], src[0]);
944 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
945 if (dst_nparam > src_nparam)
946 isl_seq_clr(dst+1+src_nparam,
947 dst_nparam - src_nparam);
948 isl_seq_clr(dst+1+dst_nparam, in_off);
949 isl_seq_cpy(dst+1+dst_nparam+in_off,
950 src+1+src_nparam,
951 isl_min(dst_in-in_off, src_in));
952 if (dst_in-in_off > src_in)
953 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
954 dst_in - in_off - src_in);
955 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
956 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
957 src+1+src_nparam+src_in,
958 isl_min(dst_out-out_off, src_out));
959 if (dst_out-out_off > src_out)
960 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
961 dst_out - out_off - src_out);
962 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
963 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
964 src+1+src_nparam+src_in+src_out,
965 isl_min(dst_map->extra-div_off, src_map->n_div));
966 if (dst_map->n_div-div_off > src_map->n_div)
967 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
968 div_off+src_map->n_div,
969 dst_map->n_div - div_off - src_map->n_div);
972 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
973 struct isl_basic_map *src_map, isl_int *src,
974 unsigned in_off, unsigned out_off, unsigned div_off)
976 isl_int_set(dst[0], src[0]);
977 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
980 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
981 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
983 int i;
984 unsigned div_off;
986 if (!bmap1 || !bmap2)
987 goto error;
989 div_off = bmap1->n_div;
991 for (i = 0; i < bmap2->n_eq; ++i) {
992 int i1 = isl_basic_map_alloc_equality(bmap1);
993 if (i1 < 0)
994 goto error;
995 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
996 i_pos, o_pos, div_off);
999 for (i = 0; i < bmap2->n_ineq; ++i) {
1000 int i1 = isl_basic_map_alloc_inequality(bmap1);
1001 if (i1 < 0)
1002 goto error;
1003 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1004 i_pos, o_pos, div_off);
1007 for (i = 0; i < bmap2->n_div; ++i) {
1008 int i1 = isl_basic_map_alloc_div(bmap1);
1009 if (i1 < 0)
1010 goto error;
1011 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1012 i_pos, o_pos, div_off);
1015 isl_basic_map_free(bmap2);
1017 return bmap1;
1019 error:
1020 isl_basic_map_free(bmap1);
1021 isl_basic_map_free(bmap2);
1022 return NULL;
1025 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
1026 struct isl_dim_map *dim_map)
1028 int i;
1030 for (i = 0; i < dim_map->len; ++i) {
1031 if (dim_map->pos[i] < 0)
1032 isl_int_set_si(dst[i], 0);
1033 else
1034 isl_int_set(dst[i], src[dim_map->pos[i]]);
1038 static void copy_div_dim_map(isl_int *dst, isl_int *src,
1039 struct isl_dim_map *dim_map)
1041 isl_int_set(dst[0], src[0]);
1042 copy_constraint_dim_map(dst+1, src+1, dim_map);
1045 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
1046 struct isl_basic_map *src, struct isl_dim_map *dim_map)
1048 int i;
1050 if (!src || !dst || !dim_map)
1051 goto error;
1053 for (i = 0; i < src->n_eq; ++i) {
1054 int i1 = isl_basic_map_alloc_equality(dst);
1055 if (i1 < 0)
1056 goto error;
1057 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
1060 for (i = 0; i < src->n_ineq; ++i) {
1061 int i1 = isl_basic_map_alloc_inequality(dst);
1062 if (i1 < 0)
1063 goto error;
1064 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
1067 for (i = 0; i < src->n_div; ++i) {
1068 int i1 = isl_basic_map_alloc_div(dst);
1069 if (i1 < 0)
1070 goto error;
1071 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
1074 free(dim_map);
1075 isl_basic_map_free(src);
1077 return dst;
1078 error:
1079 free(dim_map);
1080 isl_basic_map_free(src);
1081 isl_basic_map_free(dst);
1082 return NULL;
1085 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1086 struct isl_basic_set *bset2, unsigned pos)
1088 return (struct isl_basic_set *)
1089 add_constraints((struct isl_basic_map *)bset1,
1090 (struct isl_basic_map *)bset2, 0, pos);
1093 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1094 struct isl_dim *dim, unsigned extra,
1095 unsigned n_eq, unsigned n_ineq)
1097 struct isl_basic_map *ext;
1098 unsigned flags;
1099 int dims_ok;
1101 if (!dim)
1102 goto error;
1104 if (!base)
1105 goto error;
1107 dims_ok = isl_dim_equal(base->dim, dim) &&
1108 base->extra >= base->n_div + extra;
1110 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1111 room_for_ineq(base, n_ineq)) {
1112 isl_dim_free(dim);
1113 return base;
1116 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1117 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1118 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1119 extra += base->extra;
1120 n_eq += base->n_eq;
1121 n_ineq += base->n_ineq;
1123 ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1124 dim = NULL;
1125 if (!ext)
1126 goto error;
1128 if (dims_ok)
1129 ext->sample = isl_vec_copy(base->sample);
1130 flags = base->flags;
1131 ext = add_constraints(ext, base, 0, 0);
1132 if (ext) {
1133 ext->flags = flags;
1134 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1137 return ext;
1139 error:
1140 isl_dim_free(dim);
1141 isl_basic_map_free(base);
1142 return NULL;
1145 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1146 struct isl_dim *dim, unsigned extra,
1147 unsigned n_eq, unsigned n_ineq)
1149 return (struct isl_basic_set *)
1150 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1151 extra, n_eq, n_ineq);
1154 struct isl_basic_map *isl_basic_map_extend_constraints(
1155 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1157 if (!base)
1158 return NULL;
1159 return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1160 0, n_eq, n_ineq);
1163 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1164 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1165 unsigned n_eq, unsigned n_ineq)
1167 struct isl_basic_map *bmap;
1168 struct isl_dim *dim;
1170 if (!base)
1171 return NULL;
1172 dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1173 if (!dim)
1174 goto error;
1176 bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1177 return bmap;
1178 error:
1179 isl_basic_map_free(base);
1180 return NULL;
1183 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1184 unsigned nparam, unsigned dim, unsigned extra,
1185 unsigned n_eq, unsigned n_ineq)
1187 return (struct isl_basic_set *)
1188 isl_basic_map_extend((struct isl_basic_map *)base,
1189 nparam, 0, dim, extra, n_eq, n_ineq);
1192 struct isl_basic_set *isl_basic_set_extend_constraints(
1193 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1195 return (struct isl_basic_set *)
1196 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1197 n_eq, n_ineq);
1200 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1202 return (struct isl_basic_set *)
1203 isl_basic_map_cow((struct isl_basic_map *)bset);
1206 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1208 if (!bmap)
1209 return NULL;
1211 if (bmap->ref > 1) {
1212 bmap->ref--;
1213 bmap = isl_basic_map_dup(bmap);
1215 if (bmap)
1216 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1217 return bmap;
1220 struct isl_set *isl_set_cow(struct isl_set *set)
1222 if (!set)
1223 return NULL;
1225 if (set->ref == 1)
1226 return set;
1227 set->ref--;
1228 return isl_set_dup(set);
1231 struct isl_map *isl_map_cow(struct isl_map *map)
1233 if (!map)
1234 return NULL;
1236 if (map->ref == 1)
1237 return map;
1238 map->ref--;
1239 return isl_map_dup(map);
1242 static void swap_vars(struct isl_blk blk, isl_int *a,
1243 unsigned a_len, unsigned b_len)
1245 isl_seq_cpy(blk.data, a+a_len, b_len);
1246 isl_seq_cpy(blk.data+b_len, a, a_len);
1247 isl_seq_cpy(a, blk.data, b_len+a_len);
1250 struct isl_basic_set *isl_basic_set_swap_vars(
1251 struct isl_basic_set *bset, unsigned n)
1253 int i;
1254 struct isl_blk blk;
1255 unsigned dim;
1256 unsigned nparam;
1258 if (!bset)
1259 goto error;
1261 nparam = isl_basic_set_n_param(bset);
1262 dim = isl_basic_set_n_dim(bset);
1263 isl_assert(bset->ctx, n <= dim, goto error);
1265 if (n == dim)
1266 return bset;
1268 bset = isl_basic_set_cow(bset);
1269 if (!bset)
1270 return NULL;
1272 blk = isl_blk_alloc(bset->ctx, dim);
1273 if (isl_blk_is_error(blk))
1274 goto error;
1276 for (i = 0; i < bset->n_eq; ++i)
1277 swap_vars(blk,
1278 bset->eq[i]+1+nparam, n, dim - n);
1280 for (i = 0; i < bset->n_ineq; ++i)
1281 swap_vars(blk,
1282 bset->ineq[i]+1+nparam, n, dim - n);
1284 for (i = 0; i < bset->n_div; ++i)
1285 swap_vars(blk,
1286 bset->div[i]+1+1+nparam, n, dim - n);
1288 isl_blk_free(bset->ctx, blk);
1290 ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1291 bset = isl_basic_set_gauss(bset, NULL);
1292 return isl_basic_set_finalize(bset);
1293 error:
1294 isl_basic_set_free(bset);
1295 return NULL;
1298 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1300 int i;
1301 set = isl_set_cow(set);
1302 if (!set)
1303 return NULL;
1305 for (i = 0; i < set->n; ++i) {
1306 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1307 if (!set->p[i]) {
1308 isl_set_free(set);
1309 return NULL;
1312 ISL_F_CLR(set, ISL_SET_NORMALIZED);
1313 return set;
1316 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1318 int i = 0;
1319 unsigned total;
1320 if (!bmap)
1321 goto error;
1322 total = isl_basic_map_total_dim(bmap);
1323 isl_basic_map_free_div(bmap, bmap->n_div);
1324 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1325 if (bmap->n_eq > 0)
1326 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1327 else {
1328 i = isl_basic_map_alloc_equality(bmap);
1329 if (i < 0)
1330 goto error;
1332 isl_int_set_si(bmap->eq[i][0], 1);
1333 isl_seq_clr(bmap->eq[i]+1, total);
1334 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1335 isl_vec_free(bmap->sample);
1336 bmap->sample = NULL;
1337 return isl_basic_map_finalize(bmap);
1338 error:
1339 isl_basic_map_free(bmap);
1340 return NULL;
1343 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1345 return (struct isl_basic_set *)
1346 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1349 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1351 int i;
1352 unsigned off = isl_dim_total(bmap->dim);
1353 isl_int *t = bmap->div[a];
1354 bmap->div[a] = bmap->div[b];
1355 bmap->div[b] = t;
1357 for (i = 0; i < bmap->n_eq; ++i)
1358 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1360 for (i = 0; i < bmap->n_ineq; ++i)
1361 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1363 for (i = 0; i < bmap->n_div; ++i)
1364 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1365 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1368 /* Eliminate the specified n dimensions starting at first from the
1369 * constraints using Fourier-Motzkin. The dimensions themselves
1370 * are not removed.
1372 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1373 enum isl_dim_type type, unsigned first, unsigned n)
1375 int i;
1377 if (!map)
1378 return NULL;
1379 if (n == 0)
1380 return map;
1382 map = isl_map_cow(map);
1383 if (!map)
1384 return NULL;
1385 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1386 first += pos(map->dim, type) - 1;
1388 for (i = 0; i < map->n; ++i) {
1389 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1390 if (!map->p[i])
1391 goto error;
1393 return map;
1394 error:
1395 isl_map_free(map);
1396 return NULL;
1399 /* Eliminate the specified n dimensions starting at first from the
1400 * constraints using Fourier-Motzkin. The dimensions themselves
1401 * are not removed.
1403 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1404 enum isl_dim_type type, unsigned first, unsigned n)
1406 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1409 /* Eliminate the specified n dimensions starting at first from the
1410 * constraints using Fourier-Motzkin. The dimensions themselves
1411 * are not removed.
1413 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1414 unsigned first, unsigned n)
1416 return isl_set_eliminate(set, isl_dim_set, first, n);
1419 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1420 __isl_take isl_basic_map *bmap)
1422 bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1423 bmap->n_div);
1424 if (!bmap)
1425 return NULL;
1426 bmap->n_div = 0;
1427 return isl_basic_map_finalize(bmap);
1430 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1431 __isl_take isl_basic_set *bset)
1433 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1434 (struct isl_basic_map *)bset);
1437 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1439 int i;
1441 if (!set)
1442 return NULL;
1443 if (set->n == 0)
1444 return set;
1446 set = isl_set_cow(set);
1447 if (!set)
1448 return NULL;
1450 for (i = 0; i < set->n; ++i) {
1451 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1452 if (!set->p[i])
1453 goto error;
1455 return set;
1456 error:
1457 isl_set_free(set);
1458 return NULL;
1461 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1462 enum isl_dim_type type, unsigned first, unsigned n)
1464 if (!bmap)
1465 return NULL;
1466 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1467 goto error);
1468 if (n == 0)
1469 return bmap;
1470 bmap = isl_basic_map_eliminate_vars(bmap,
1471 isl_basic_map_offset(bmap, type) - 1 + first, n);
1472 if (!bmap)
1473 return bmap;
1474 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1475 return bmap;
1476 bmap = isl_basic_map_drop(bmap, type, first, n);
1477 return bmap;
1478 error:
1479 isl_basic_map_free(bmap);
1480 return NULL;
1483 /* Return true if the definition of the given div (recursively) involves
1484 * any of the given variables.
1486 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1487 unsigned first, unsigned n)
1489 int i;
1490 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1492 if (isl_int_is_zero(bmap->div[div][0]))
1493 return 0;
1494 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1495 return 1;
1497 for (i = bmap->n_div - 1; i >= 0; --i) {
1498 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1499 continue;
1500 if (div_involves_vars(bmap, i, first, n))
1501 return 1;
1504 return 0;
1507 /* Remove all divs (recursively) involving any of the given dimensions
1508 * in their definitions.
1510 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1511 __isl_take isl_basic_map *bmap,
1512 enum isl_dim_type type, unsigned first, unsigned n)
1514 int i;
1516 if (!bmap)
1517 return NULL;
1518 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1519 goto error);
1520 first += isl_basic_map_offset(bmap, type);
1522 for (i = bmap->n_div - 1; i >= 0; --i) {
1523 if (!div_involves_vars(bmap, i, first, n))
1524 continue;
1525 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1528 return bmap;
1529 error:
1530 isl_basic_map_free(bmap);
1531 return NULL;
1534 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1535 enum isl_dim_type type, unsigned first, unsigned n)
1537 int i;
1539 if (!map)
1540 return NULL;
1541 if (map->n == 0)
1542 return map;
1544 map = isl_map_cow(map);
1545 if (!map)
1546 return NULL;
1548 for (i = 0; i < map->n; ++i) {
1549 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1550 type, first, n);
1551 if (!map->p[i])
1552 goto error;
1554 return map;
1555 error:
1556 isl_map_free(map);
1557 return NULL;
1560 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1561 enum isl_dim_type type, unsigned first, unsigned n)
1563 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1564 type, first, n);
1567 /* Return true if the definition of the given div is unknown or depends
1568 * on unknown divs.
1570 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1572 int i;
1573 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1575 if (isl_int_is_zero(bmap->div[div][0]))
1576 return 1;
1578 for (i = bmap->n_div - 1; i >= 0; --i) {
1579 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1580 continue;
1581 if (div_is_unknown(bmap, i))
1582 return 1;
1585 return 0;
1588 /* Remove all divs that are unknown or defined in terms of unknown divs.
1590 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1591 __isl_take isl_basic_map *bmap)
1593 int i;
1595 if (!bmap)
1596 return NULL;
1598 for (i = bmap->n_div - 1; i >= 0; --i) {
1599 if (!div_is_unknown(bmap, i))
1600 continue;
1601 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1604 return bmap;
1605 error:
1606 isl_basic_map_free(bmap);
1607 return NULL;
1610 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1612 int i;
1614 if (!map)
1615 return NULL;
1616 if (map->n == 0)
1617 return map;
1619 map = isl_map_cow(map);
1620 if (!map)
1621 return NULL;
1623 for (i = 0; i < map->n; ++i) {
1624 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1625 if (!map->p[i])
1626 goto error;
1628 return map;
1629 error:
1630 isl_map_free(map);
1631 return NULL;
1634 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1636 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1639 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1640 __isl_take isl_basic_set *bset,
1641 enum isl_dim_type type, unsigned first, unsigned n)
1643 return (isl_basic_set *)
1644 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1647 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1648 enum isl_dim_type type, unsigned first, unsigned n)
1650 int i;
1652 if (n == 0)
1653 return map;
1655 map = isl_map_cow(map);
1656 if (!map)
1657 return NULL;
1658 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1660 for (i = 0; i < map->n; ++i) {
1661 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1662 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1663 if (!map->p[i])
1664 goto error;
1666 map = isl_map_drop(map, type, first, n);
1667 return map;
1668 error:
1669 isl_map_free(map);
1670 return NULL;
1673 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1674 enum isl_dim_type type, unsigned first, unsigned n)
1676 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1679 /* Project out n inputs starting at first using Fourier-Motzkin */
1680 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1681 unsigned first, unsigned n)
1683 return isl_map_remove_dims(map, isl_dim_in, first, n);
1686 static void dump_term(struct isl_basic_map *bmap,
1687 isl_int c, int pos, FILE *out)
1689 const char *name;
1690 unsigned in = isl_basic_map_n_in(bmap);
1691 unsigned dim = in + isl_basic_map_n_out(bmap);
1692 unsigned nparam = isl_basic_map_n_param(bmap);
1693 if (!pos)
1694 isl_int_print(out, c, 0);
1695 else {
1696 if (!isl_int_is_one(c))
1697 isl_int_print(out, c, 0);
1698 if (pos < 1 + nparam) {
1699 name = isl_dim_get_name(bmap->dim,
1700 isl_dim_param, pos - 1);
1701 if (name)
1702 fprintf(out, "%s", name);
1703 else
1704 fprintf(out, "p%d", pos - 1);
1705 } else if (pos < 1 + nparam + in)
1706 fprintf(out, "i%d", pos - 1 - nparam);
1707 else if (pos < 1 + nparam + dim)
1708 fprintf(out, "o%d", pos - 1 - nparam - in);
1709 else
1710 fprintf(out, "e%d", pos - 1 - nparam - dim);
1714 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1715 int sign, FILE *out)
1717 int i;
1718 int first;
1719 unsigned len = 1 + isl_basic_map_total_dim(bmap);
1720 isl_int v;
1722 isl_int_init(v);
1723 for (i = 0, first = 1; i < len; ++i) {
1724 if (isl_int_sgn(c[i]) * sign <= 0)
1725 continue;
1726 if (!first)
1727 fprintf(out, " + ");
1728 first = 0;
1729 isl_int_abs(v, c[i]);
1730 dump_term(bmap, v, i, out);
1732 isl_int_clear(v);
1733 if (first)
1734 fprintf(out, "0");
1737 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1738 const char *op, FILE *out, int indent)
1740 int i;
1742 fprintf(out, "%*s", indent, "");
1744 dump_constraint_sign(bmap, c, 1, out);
1745 fprintf(out, " %s ", op);
1746 dump_constraint_sign(bmap, c, -1, out);
1748 fprintf(out, "\n");
1750 for (i = bmap->n_div; i < bmap->extra; ++i) {
1751 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1752 continue;
1753 fprintf(out, "%*s", indent, "");
1754 fprintf(out, "ERROR: unused div coefficient not zero\n");
1755 abort();
1759 static void dump_constraints(struct isl_basic_map *bmap,
1760 isl_int **c, unsigned n,
1761 const char *op, FILE *out, int indent)
1763 int i;
1765 for (i = 0; i < n; ++i)
1766 dump_constraint(bmap, c[i], op, out, indent);
1769 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1771 int j;
1772 int first = 1;
1773 unsigned total = isl_basic_map_total_dim(bmap);
1775 for (j = 0; j < 1 + total; ++j) {
1776 if (isl_int_is_zero(exp[j]))
1777 continue;
1778 if (!first && isl_int_is_pos(exp[j]))
1779 fprintf(out, "+");
1780 dump_term(bmap, exp[j], j, out);
1781 first = 0;
1785 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1787 int i;
1789 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1790 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1792 for (i = 0; i < bmap->n_div; ++i) {
1793 fprintf(out, "%*s", indent, "");
1794 fprintf(out, "e%d = [(", i);
1795 dump_affine(bmap, bmap->div[i]+1, out);
1796 fprintf(out, ")/");
1797 isl_int_print(out, bmap->div[i][0], 0);
1798 fprintf(out, "]\n");
1802 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1803 FILE *out, int indent)
1805 if (!bset) {
1806 fprintf(out, "null basic set\n");
1807 return;
1810 fprintf(out, "%*s", indent, "");
1811 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1812 bset->ref, bset->dim->nparam, bset->dim->n_out,
1813 bset->extra, bset->flags);
1814 dump((struct isl_basic_map *)bset, out, indent);
1817 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1818 FILE *out, int indent)
1820 if (!bmap) {
1821 fprintf(out, "null basic map\n");
1822 return;
1825 fprintf(out, "%*s", indent, "");
1826 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1827 "flags: %x, n_name: %d\n",
1828 bmap->ref,
1829 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1830 bmap->extra, bmap->flags, bmap->dim->n_name);
1831 dump(bmap, out, indent);
1834 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1836 unsigned total;
1837 if (!bmap)
1838 return -1;
1839 total = isl_basic_map_total_dim(bmap);
1840 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1841 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1842 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1843 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1844 return 0;
1847 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1849 struct isl_set *set;
1851 if (!dim)
1852 return NULL;
1853 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1854 isl_assert(dim->ctx, n >= 0, goto error);
1855 set = isl_alloc(dim->ctx, struct isl_set,
1856 sizeof(struct isl_set) +
1857 (n - 1) * sizeof(struct isl_basic_set *));
1858 if (!set)
1859 goto error;
1861 set->ctx = dim->ctx;
1862 isl_ctx_ref(set->ctx);
1863 set->ref = 1;
1864 set->size = n;
1865 set->n = 0;
1866 set->dim = dim;
1867 set->flags = flags;
1868 return set;
1869 error:
1870 isl_dim_free(dim);
1871 return NULL;
1874 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1875 unsigned nparam, unsigned dim, int n, unsigned flags)
1877 struct isl_set *set;
1878 struct isl_dim *dims;
1880 dims = isl_dim_alloc(ctx, nparam, 0, dim);
1881 if (!dims)
1882 return NULL;
1884 set = isl_set_alloc_dim(dims, n, flags);
1885 return set;
1888 /* Make sure "map" has room for at least "n" more basic maps.
1890 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1892 int i;
1893 struct isl_map *grown = NULL;
1895 if (!map)
1896 return NULL;
1897 isl_assert(map->ctx, n >= 0, goto error);
1898 if (map->n + n <= map->size)
1899 return map;
1900 grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1901 if (!grown)
1902 goto error;
1903 for (i = 0; i < map->n; ++i) {
1904 grown->p[i] = isl_basic_map_copy(map->p[i]);
1905 if (!grown->p[i])
1906 goto error;
1907 grown->n++;
1909 isl_map_free(map);
1910 return grown;
1911 error:
1912 isl_map_free(grown);
1913 isl_map_free(map);
1914 return NULL;
1917 /* Make sure "set" has room for at least "n" more basic sets.
1919 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1921 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1924 struct isl_set *isl_set_dup(struct isl_set *set)
1926 int i;
1927 struct isl_set *dup;
1929 if (!set)
1930 return NULL;
1932 dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1933 if (!dup)
1934 return NULL;
1935 for (i = 0; i < set->n; ++i)
1936 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1937 return dup;
1940 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1942 return isl_map_from_basic_map(bset);
1945 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1947 struct isl_map *map;
1949 if (!bmap)
1950 return NULL;
1952 map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1953 return isl_map_add_basic_map(map, bmap);
1956 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1957 __isl_take isl_basic_set *bset)
1959 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1960 (struct isl_basic_map *)bset);
1963 void isl_set_free(struct isl_set *set)
1965 int i;
1967 if (!set)
1968 return;
1970 if (--set->ref > 0)
1971 return;
1973 isl_ctx_deref(set->ctx);
1974 for (i = 0; i < set->n; ++i)
1975 isl_basic_set_free(set->p[i]);
1976 isl_dim_free(set->dim);
1977 free(set);
1980 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
1982 int i;
1984 if (!set) {
1985 fprintf(out, "null set\n");
1986 return;
1989 fprintf(out, "%*s", indent, "");
1990 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1991 set->ref, set->n, set->dim->nparam, set->dim->n_out,
1992 set->flags);
1993 for (i = 0; i < set->n; ++i) {
1994 fprintf(out, "%*s", indent, "");
1995 fprintf(out, "basic set %d:\n", i);
1996 isl_basic_set_print_internal(set->p[i], out, indent+4);
2000 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2002 int i;
2004 if (!map) {
2005 fprintf(out, "null map\n");
2006 return;
2009 fprintf(out, "%*s", indent, "");
2010 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2011 "flags: %x, n_name: %d\n",
2012 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2013 map->dim->n_out, map->flags, map->dim->n_name);
2014 for (i = 0; i < map->n; ++i) {
2015 fprintf(out, "%*s", indent, "");
2016 fprintf(out, "basic map %d:\n", i);
2017 isl_basic_map_print_internal(map->p[i], out, indent+4);
2021 struct isl_basic_map *isl_basic_map_intersect_domain(
2022 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2024 struct isl_basic_map *bmap_domain;
2026 if (!bmap || !bset)
2027 goto error;
2029 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2030 bset->dim, isl_dim_param), goto error);
2032 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2033 isl_assert(bset->ctx,
2034 isl_basic_map_compatible_domain(bmap, bset), goto error);
2036 bmap = isl_basic_map_cow(bmap);
2037 if (!bmap)
2038 goto error;
2039 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2040 bset->n_div, bset->n_eq, bset->n_ineq);
2041 bmap_domain = isl_basic_map_from_domain(bset);
2042 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2044 bmap = isl_basic_map_simplify(bmap);
2045 return isl_basic_map_finalize(bmap);
2046 error:
2047 isl_basic_map_free(bmap);
2048 isl_basic_set_free(bset);
2049 return NULL;
2052 struct isl_basic_map *isl_basic_map_intersect_range(
2053 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2055 struct isl_basic_map *bmap_range;
2057 if (!bmap || !bset)
2058 goto error;
2060 isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2061 bset->dim, isl_dim_param), goto error);
2063 if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2064 isl_assert(bset->ctx,
2065 isl_basic_map_compatible_range(bmap, bset), goto error);
2067 bmap = isl_basic_map_cow(bmap);
2068 if (!bmap)
2069 goto error;
2070 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2071 bset->n_div, bset->n_eq, bset->n_ineq);
2072 bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2073 bmap = add_constraints(bmap, bmap_range, 0, 0);
2075 bmap = isl_basic_map_simplify(bmap);
2076 return isl_basic_map_finalize(bmap);
2077 error:
2078 isl_basic_map_free(bmap);
2079 isl_basic_set_free(bset);
2080 return NULL;
2083 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2085 int i;
2086 unsigned total;
2087 isl_int s;
2089 total = 1 + isl_basic_map_total_dim(bmap);
2090 if (total != vec->size)
2091 return -1;
2093 isl_int_init(s);
2095 for (i = 0; i < bmap->n_eq; ++i) {
2096 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2097 if (!isl_int_is_zero(s)) {
2098 isl_int_clear(s);
2099 return 0;
2103 for (i = 0; i < bmap->n_ineq; ++i) {
2104 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2105 if (isl_int_is_neg(s)) {
2106 isl_int_clear(s);
2107 return 0;
2111 isl_int_clear(s);
2113 return 1;
2116 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2118 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2121 struct isl_basic_map *isl_basic_map_intersect(
2122 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2124 struct isl_vec *sample = NULL;
2126 if (!bmap1 || !bmap2)
2127 goto error;
2129 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2130 bmap2->dim, isl_dim_param), goto error);
2131 if (isl_dim_total(bmap1->dim) ==
2132 isl_dim_size(bmap1->dim, isl_dim_param) &&
2133 isl_dim_total(bmap2->dim) !=
2134 isl_dim_size(bmap2->dim, isl_dim_param))
2135 return isl_basic_map_intersect(bmap2, bmap1);
2137 if (isl_dim_total(bmap2->dim) !=
2138 isl_dim_size(bmap2->dim, isl_dim_param))
2139 isl_assert(bmap1->ctx,
2140 isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2142 if (bmap1->sample &&
2143 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2144 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2145 sample = isl_vec_copy(bmap1->sample);
2146 else if (bmap2->sample &&
2147 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2148 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2149 sample = isl_vec_copy(bmap2->sample);
2151 bmap1 = isl_basic_map_cow(bmap1);
2152 if (!bmap1)
2153 goto error;
2154 bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2155 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2156 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2158 if (!bmap1)
2159 isl_vec_free(sample);
2160 else if (sample) {
2161 isl_vec_free(bmap1->sample);
2162 bmap1->sample = sample;
2165 bmap1 = isl_basic_map_simplify(bmap1);
2166 return isl_basic_map_finalize(bmap1);
2167 error:
2168 if (sample)
2169 isl_vec_free(sample);
2170 isl_basic_map_free(bmap1);
2171 isl_basic_map_free(bmap2);
2172 return NULL;
2175 struct isl_basic_set *isl_basic_set_intersect(
2176 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2178 return (struct isl_basic_set *)
2179 isl_basic_map_intersect(
2180 (struct isl_basic_map *)bset1,
2181 (struct isl_basic_map *)bset2);
2184 /* Special case of isl_map_intersect, where both map1 and map2
2185 * are convex, without any divs and such that either map1 or map2
2186 * contains a single constraint. This constraint is then simply
2187 * added to the other map.
2189 static __isl_give isl_map *map_intersect_add_constraint(
2190 __isl_take isl_map *map1, __isl_take isl_map *map2)
2192 struct isl_basic_map *bmap1;
2193 struct isl_basic_map *bmap2;
2195 isl_assert(map1->ctx, map1->n == 1, goto error);
2196 isl_assert(map2->ctx, map1->n == 1, goto error);
2197 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2198 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2200 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2201 return isl_map_intersect(map2, map1);
2203 isl_assert(map2->ctx,
2204 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2206 map1 = isl_map_cow(map1);
2207 if (!map1)
2208 goto error;
2209 if (isl_map_fast_is_empty(map1)) {
2210 isl_map_free(map2);
2211 return map1;
2213 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2214 if (map2->p[0]->n_eq == 1)
2215 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2216 else
2217 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2218 map2->p[0]->ineq[0]);
2220 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2221 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2222 if (!map1->p[0])
2223 goto error;
2225 if (isl_basic_map_fast_is_empty(map1->p[0])) {
2226 isl_basic_map_free(map1->p[0]);
2227 map1->n = 0;
2230 isl_map_free(map2);
2232 return map1;
2233 error:
2234 isl_map_free(map1);
2235 isl_map_free(map2);
2236 return NULL;
2239 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
2241 unsigned flags = 0;
2242 struct isl_map *result;
2243 int i, j;
2245 if (!map1 || !map2)
2246 goto error;
2248 if (isl_map_fast_is_empty(map1)) {
2249 isl_map_free(map2);
2250 return map1;
2252 if (isl_map_fast_is_empty(map2)) {
2253 isl_map_free(map1);
2254 return map2;
2257 if (map1->n == 1 && map2->n == 1 &&
2258 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2259 isl_dim_equal(map1->dim, map2->dim) &&
2260 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2261 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2262 return map_intersect_add_constraint(map1, map2);
2263 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2264 map2->dim, isl_dim_param), goto error);
2265 if (isl_dim_total(map1->dim) ==
2266 isl_dim_size(map1->dim, isl_dim_param) &&
2267 isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2268 return isl_map_intersect(map2, map1);
2270 if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2271 isl_assert(map1->ctx,
2272 isl_dim_equal(map1->dim, map2->dim), goto error);
2274 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2275 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2276 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2278 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2279 map1->n * map2->n, flags);
2280 if (!result)
2281 goto error;
2282 for (i = 0; i < map1->n; ++i)
2283 for (j = 0; j < map2->n; ++j) {
2284 struct isl_basic_map *part;
2285 part = isl_basic_map_intersect(
2286 isl_basic_map_copy(map1->p[i]),
2287 isl_basic_map_copy(map2->p[j]));
2288 if (isl_basic_map_is_empty(part))
2289 isl_basic_map_free(part);
2290 else
2291 result = isl_map_add_basic_map(result, part);
2292 if (!result)
2293 goto error;
2295 isl_map_free(map1);
2296 isl_map_free(map2);
2297 return result;
2298 error:
2299 isl_map_free(map1);
2300 isl_map_free(map2);
2301 return NULL;
2304 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2306 return (struct isl_set *)
2307 isl_map_intersect((struct isl_map *)set1,
2308 (struct isl_map *)set2);
2311 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2313 struct isl_dim *dim;
2314 struct isl_basic_set *bset;
2315 unsigned in;
2317 if (!bmap)
2318 return NULL;
2319 bmap = isl_basic_map_cow(bmap);
2320 if (!bmap)
2321 return NULL;
2322 dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2323 in = isl_basic_map_n_in(bmap);
2324 bset = isl_basic_set_from_basic_map(bmap);
2325 bset = isl_basic_set_swap_vars(bset, in);
2326 return isl_basic_map_from_basic_set(bset, dim);
2329 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2330 enum isl_dim_type type, unsigned pos, unsigned n)
2332 struct isl_dim *res_dim;
2333 struct isl_basic_map *res;
2334 struct isl_dim_map *dim_map;
2335 unsigned total, off;
2336 enum isl_dim_type t;
2338 if (n == 0)
2339 return bmap;
2341 if (!bmap)
2342 return NULL;
2344 res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2346 total = isl_basic_map_total_dim(bmap) + n;
2347 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2348 off = 0;
2349 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2350 if (t != type) {
2351 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2352 } else {
2353 unsigned size = isl_basic_map_dim(bmap, t);
2354 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2355 0, pos, off);
2356 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2357 pos, size - pos, off + pos + n);
2359 off += isl_dim_size(res_dim, t);
2361 isl_dim_map_div(dim_map, bmap, off);
2363 res = isl_basic_map_alloc_dim(res_dim,
2364 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2365 res = add_constraints_dim_map(res, bmap, dim_map);
2366 return isl_basic_map_finalize(res);
2369 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2370 enum isl_dim_type type, unsigned n)
2372 if (!bmap)
2373 return NULL;
2374 return isl_basic_map_insert(bmap, type,
2375 isl_basic_map_dim(bmap, type), n);
2378 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2379 enum isl_dim_type type, unsigned n)
2381 if (!bset)
2382 return NULL;
2383 isl_assert(bset->ctx, type != isl_dim_in, goto error);
2384 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2385 error:
2386 isl_basic_set_free(bset);
2387 return NULL;
2390 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2391 enum isl_dim_type type, unsigned pos, unsigned n)
2393 int i;
2395 if (n == 0)
2396 return map;
2398 map = isl_map_cow(map);
2399 if (!map)
2400 return NULL;
2402 map->dim = isl_dim_insert(map->dim, type, pos, n);
2403 if (!map->dim)
2404 goto error;
2406 for (i = 0; i < map->n; ++i) {
2407 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2408 if (!map->p[i])
2409 goto error;
2412 return map;
2413 error:
2414 isl_map_free(map);
2415 return NULL;
2418 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2419 enum isl_dim_type type, unsigned pos, unsigned n)
2421 return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2424 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2425 enum isl_dim_type type, unsigned n)
2427 if (!map)
2428 return NULL;
2429 return isl_map_insert(map, type, isl_map_dim(map, type), n);
2432 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2433 enum isl_dim_type type, unsigned n)
2435 if (!set)
2436 return NULL;
2437 isl_assert(set->ctx, type != isl_dim_in, goto error);
2438 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2439 error:
2440 isl_set_free(set);
2441 return NULL;
2444 __isl_give isl_basic_map *isl_basic_map_move_dims(
2445 __isl_take isl_basic_map *bmap,
2446 enum isl_dim_type dst_type, unsigned dst_pos,
2447 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2449 int i;
2450 struct isl_dim_map *dim_map;
2451 struct isl_basic_map *res;
2452 enum isl_dim_type t;
2453 unsigned total, off;
2455 if (!bmap)
2456 return NULL;
2457 if (n == 0)
2458 return bmap;
2460 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2461 goto error);
2463 if (dst_type == src_type && dst_pos == src_pos)
2464 return bmap;
2466 isl_assert(bmap->ctx, dst_type != src_type, goto error);
2468 if (pos(bmap->dim, dst_type) + dst_pos ==
2469 pos(bmap->dim, src_type) + src_pos +
2470 ((src_type < dst_type) ? n : 0)) {
2471 bmap = isl_basic_map_cow(bmap);
2472 if (!bmap)
2473 return NULL;
2475 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2476 src_type, src_pos, n);
2477 if (!bmap->dim)
2478 goto error;
2480 bmap = isl_basic_map_finalize(bmap);
2482 return bmap;
2485 total = isl_basic_map_total_dim(bmap);
2486 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2488 off = 0;
2489 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2490 unsigned size = isl_dim_size(bmap->dim, t);
2491 if (t == dst_type) {
2492 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2493 0, dst_pos, off);
2494 off += dst_pos;
2495 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2496 src_pos, n, off);
2497 off += n;
2498 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2499 dst_pos, size - dst_pos, off);
2500 off += size - dst_pos;
2501 } else if (t == src_type) {
2502 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2503 0, src_pos, off);
2504 off += src_pos;
2505 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2506 src_pos + n, size - src_pos - n, off);
2507 off += size - src_pos - n;
2508 } else {
2509 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2510 off += size;
2513 isl_dim_map_div(dim_map, bmap, off);
2515 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2516 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2517 bmap = add_constraints_dim_map(res, bmap, dim_map);
2519 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2520 src_type, src_pos, n);
2521 if (!bmap->dim)
2522 goto error;
2524 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2525 bmap = isl_basic_map_gauss(bmap, NULL);
2526 bmap = isl_basic_map_finalize(bmap);
2528 return bmap;
2529 error:
2530 isl_basic_map_free(bmap);
2531 return NULL;
2534 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2535 enum isl_dim_type dst_type, unsigned dst_pos,
2536 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2538 return (isl_basic_set *)isl_basic_map_move_dims(
2539 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2542 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2543 enum isl_dim_type dst_type, unsigned dst_pos,
2544 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2546 if (!set)
2547 return NULL;
2548 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2549 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2550 src_type, src_pos, n);
2551 error:
2552 isl_set_free(set);
2553 return NULL;
2556 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2557 enum isl_dim_type dst_type, unsigned dst_pos,
2558 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2560 int i;
2562 if (!map)
2563 return NULL;
2564 if (n == 0)
2565 return map;
2567 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2568 goto error);
2570 if (dst_type == src_type && dst_pos == src_pos)
2571 return map;
2573 isl_assert(map->ctx, dst_type != src_type, goto error);
2575 map = isl_map_cow(map);
2576 if (!map)
2577 return NULL;
2579 map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2580 if (!map->dim)
2581 goto error;
2583 for (i = 0; i < map->n; ++i) {
2584 map->p[i] = isl_basic_map_move_dims(map->p[i],
2585 dst_type, dst_pos,
2586 src_type, src_pos, n);
2587 if (!map->p[i])
2588 goto error;
2591 return map;
2592 error:
2593 isl_map_free(map);
2594 return NULL;
2597 /* Move the specified dimensions to the last columns right before
2598 * the divs. Don't change the dimension specification of bmap.
2599 * That's the responsibility of the caller.
2601 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2602 enum isl_dim_type type, unsigned first, unsigned n)
2604 struct isl_dim_map *dim_map;
2605 struct isl_basic_map *res;
2606 enum isl_dim_type t;
2607 unsigned total, off;
2609 if (!bmap)
2610 return NULL;
2611 if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2612 return bmap;
2614 total = isl_basic_map_total_dim(bmap);
2615 dim_map = isl_dim_map_alloc(bmap->ctx, total);
2617 off = 0;
2618 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2619 unsigned size = isl_dim_size(bmap->dim, t);
2620 if (t == type) {
2621 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2622 0, first, off);
2623 off += first;
2624 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2625 first, n, total - bmap->n_div - n);
2626 isl_dim_map_dim_range(dim_map, bmap->dim, t,
2627 first + n, size - (first + n), off);
2628 off += size - (first + n);
2629 } else {
2630 isl_dim_map_dim(dim_map, bmap->dim, t, off);
2631 off += size;
2634 isl_dim_map_div(dim_map, bmap, off + n);
2636 res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2637 bmap->n_div, bmap->n_eq, bmap->n_ineq);
2638 res = add_constraints_dim_map(res, bmap, dim_map);
2639 return res;
2642 /* Turn the n dimensions of type type, starting at first
2643 * into existentially quantified variables.
2645 __isl_give isl_basic_map *isl_basic_map_project_out(
2646 __isl_take isl_basic_map *bmap,
2647 enum isl_dim_type type, unsigned first, unsigned n)
2649 int i;
2650 size_t row_size;
2651 isl_int **new_div;
2652 isl_int *old;
2654 if (n == 0)
2655 return bmap;
2657 if (!bmap)
2658 return NULL;
2660 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2661 return isl_basic_map_remove_dims(bmap, type, first, n);
2663 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2664 goto error);
2666 bmap = move_last(bmap, type, first, n);
2667 bmap = isl_basic_map_cow(bmap);
2668 if (!bmap)
2669 return NULL;
2671 row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2672 old = bmap->block2.data;
2673 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2674 (bmap->extra + n) * (1 + row_size));
2675 if (!bmap->block2.data)
2676 goto error;
2677 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2678 if (!new_div)
2679 goto error;
2680 for (i = 0; i < n; ++i) {
2681 new_div[i] = bmap->block2.data +
2682 (bmap->extra + i) * (1 + row_size);
2683 isl_seq_clr(new_div[i], 1 + row_size);
2685 for (i = 0; i < bmap->extra; ++i)
2686 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2687 free(bmap->div);
2688 bmap->div = new_div;
2689 bmap->n_div += n;
2690 bmap->extra += n;
2692 bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2693 if (!bmap->dim)
2694 goto error;
2695 bmap = isl_basic_map_simplify(bmap);
2696 bmap = isl_basic_map_drop_redundant_divs(bmap);
2697 return isl_basic_map_finalize(bmap);
2698 error:
2699 isl_basic_map_free(bmap);
2700 return NULL;
2703 /* Turn the n dimensions of type type, starting at first
2704 * into existentially quantified variables.
2706 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2707 enum isl_dim_type type, unsigned first, unsigned n)
2709 return (isl_basic_set *)isl_basic_map_project_out(
2710 (isl_basic_map *)bset, type, first, n);
2713 /* Turn the n dimensions of type type, starting at first
2714 * into existentially quantified variables.
2716 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2717 enum isl_dim_type type, unsigned first, unsigned n)
2719 int i;
2721 if (!map)
2722 return NULL;
2724 if (n == 0)
2725 return map;
2727 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2729 map = isl_map_cow(map);
2730 if (!map)
2731 return NULL;
2733 map->dim = isl_dim_drop(map->dim, type, first, n);
2734 if (!map->dim)
2735 goto error;
2737 for (i = 0; i < map->n; ++i) {
2738 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2739 if (!map->p[i])
2740 goto error;
2743 return map;
2744 error:
2745 isl_map_free(map);
2746 return NULL;
2749 /* Turn the n dimensions of type type, starting at first
2750 * into existentially quantified variables.
2752 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2753 enum isl_dim_type type, unsigned first, unsigned n)
2755 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2758 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2760 int i, j;
2762 for (i = 0; i < n; ++i) {
2763 j = isl_basic_map_alloc_div(bmap);
2764 if (j < 0)
2765 goto error;
2766 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2768 return bmap;
2769 error:
2770 isl_basic_map_free(bmap);
2771 return NULL;
2774 struct isl_basic_map *isl_basic_map_apply_range(
2775 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2777 struct isl_dim *dim_result = NULL;
2778 struct isl_basic_map *bmap;
2779 unsigned n_in, n_out, n, nparam, total, pos;
2780 struct isl_dim_map *dim_map1, *dim_map2;
2782 if (!bmap1 || !bmap2)
2783 goto error;
2785 dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2786 isl_dim_copy(bmap2->dim));
2788 n_in = isl_basic_map_n_in(bmap1);
2789 n_out = isl_basic_map_n_out(bmap2);
2790 n = isl_basic_map_n_out(bmap1);
2791 nparam = isl_basic_map_n_param(bmap1);
2793 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2794 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2795 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2796 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2797 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2798 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2799 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2800 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2801 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2802 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2803 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2805 bmap = isl_basic_map_alloc_dim(dim_result,
2806 bmap1->n_div + bmap2->n_div + n,
2807 bmap1->n_eq + bmap2->n_eq,
2808 bmap1->n_ineq + bmap2->n_ineq);
2809 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2810 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2811 bmap = add_divs(bmap, n);
2812 bmap = isl_basic_map_simplify(bmap);
2813 bmap = isl_basic_map_drop_redundant_divs(bmap);
2814 return isl_basic_map_finalize(bmap);
2815 error:
2816 isl_basic_map_free(bmap1);
2817 isl_basic_map_free(bmap2);
2818 return NULL;
2821 struct isl_basic_set *isl_basic_set_apply(
2822 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2824 if (!bset || !bmap)
2825 goto error;
2827 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2828 goto error);
2830 return (struct isl_basic_set *)
2831 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2832 error:
2833 isl_basic_set_free(bset);
2834 isl_basic_map_free(bmap);
2835 return NULL;
2838 struct isl_basic_map *isl_basic_map_apply_domain(
2839 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2841 if (!bmap1 || !bmap2)
2842 goto error;
2844 isl_assert(bmap1->ctx,
2845 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2846 isl_assert(bmap1->ctx,
2847 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2848 goto error);
2850 bmap1 = isl_basic_map_reverse(bmap1);
2851 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2852 return isl_basic_map_reverse(bmap1);
2853 error:
2854 isl_basic_map_free(bmap1);
2855 isl_basic_map_free(bmap2);
2856 return NULL;
2859 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2860 * A \cap B -> f(A) + f(B)
2862 struct isl_basic_map *isl_basic_map_sum(
2863 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2865 unsigned n_in, n_out, nparam, total, pos;
2866 struct isl_basic_map *bmap = NULL;
2867 struct isl_dim_map *dim_map1, *dim_map2;
2868 int i;
2870 if (!bmap1 || !bmap2)
2871 goto error;
2873 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2874 goto error);
2876 nparam = isl_basic_map_n_param(bmap1);
2877 n_in = isl_basic_map_n_in(bmap1);
2878 n_out = isl_basic_map_n_out(bmap1);
2880 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2881 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2882 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2883 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2884 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2885 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2886 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2887 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2888 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2889 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2890 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2892 bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2893 bmap1->n_div + bmap2->n_div + 2 * n_out,
2894 bmap1->n_eq + bmap2->n_eq + n_out,
2895 bmap1->n_ineq + bmap2->n_ineq);
2896 for (i = 0; i < n_out; ++i) {
2897 int j = isl_basic_map_alloc_equality(bmap);
2898 if (j < 0)
2899 goto error;
2900 isl_seq_clr(bmap->eq[j], 1+total);
2901 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2902 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2903 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2905 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2906 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2907 bmap = add_divs(bmap, 2 * n_out);
2909 bmap = isl_basic_map_simplify(bmap);
2910 return isl_basic_map_finalize(bmap);
2911 error:
2912 isl_basic_map_free(bmap);
2913 isl_basic_map_free(bmap1);
2914 isl_basic_map_free(bmap2);
2915 return NULL;
2918 /* Given two maps A -> f(A) and B -> g(B), construct a map
2919 * A \cap B -> f(A) + f(B)
2921 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2923 struct isl_map *result;
2924 int i, j;
2926 if (!map1 || !map2)
2927 goto error;
2929 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2931 result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2932 map1->n * map2->n, 0);
2933 if (!result)
2934 goto error;
2935 for (i = 0; i < map1->n; ++i)
2936 for (j = 0; j < map2->n; ++j) {
2937 struct isl_basic_map *part;
2938 part = isl_basic_map_sum(
2939 isl_basic_map_copy(map1->p[i]),
2940 isl_basic_map_copy(map2->p[j]));
2941 if (isl_basic_map_is_empty(part))
2942 isl_basic_map_free(part);
2943 else
2944 result = isl_map_add_basic_map(result, part);
2945 if (!result)
2946 goto error;
2948 isl_map_free(map1);
2949 isl_map_free(map2);
2950 return result;
2951 error:
2952 isl_map_free(map1);
2953 isl_map_free(map2);
2954 return NULL;
2957 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2958 __isl_take isl_set *set2)
2960 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2963 /* Given a basic map A -> f(A), construct A -> -f(A).
2965 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2967 int i, j;
2968 unsigned off, n;
2970 bmap = isl_basic_map_cow(bmap);
2971 if (!bmap)
2972 return NULL;
2974 n = isl_basic_map_dim(bmap, isl_dim_out);
2975 off = isl_basic_map_offset(bmap, isl_dim_out);
2976 for (i = 0; i < bmap->n_eq; ++i)
2977 for (j = 0; j < n; ++j)
2978 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2979 for (i = 0; i < bmap->n_ineq; ++i)
2980 for (j = 0; j < n; ++j)
2981 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2982 for (i = 0; i < bmap->n_div; ++i)
2983 for (j = 0; j < n; ++j)
2984 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2985 return isl_basic_map_finalize(bmap);
2988 /* Given a map A -> f(A), construct A -> -f(A).
2990 struct isl_map *isl_map_neg(struct isl_map *map)
2992 int i;
2994 map = isl_map_cow(map);
2995 if (!map)
2996 return NULL;
2998 for (i = 0; i < map->n; ++i) {
2999 map->p[i] = isl_basic_map_neg(map->p[i]);
3000 if (!map->p[i])
3001 goto error;
3004 return map;
3005 error:
3006 isl_map_free(map);
3007 return NULL;
3010 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3012 return (isl_set *)isl_map_neg((isl_map *)set);
3015 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3016 * A -> floor(f(A)/d).
3018 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3019 isl_int d)
3021 unsigned n_in, n_out, nparam, total, pos;
3022 struct isl_basic_map *result = NULL;
3023 struct isl_dim_map *dim_map;
3024 int i;
3026 if (!bmap)
3027 return NULL;
3029 nparam = isl_basic_map_n_param(bmap);
3030 n_in = isl_basic_map_n_in(bmap);
3031 n_out = isl_basic_map_n_out(bmap);
3033 total = nparam + n_in + n_out + bmap->n_div + n_out;
3034 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3035 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3036 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3037 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3038 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3040 result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3041 bmap->n_div + n_out,
3042 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3043 result = add_constraints_dim_map(result, bmap, dim_map);
3044 result = add_divs(result, n_out);
3045 for (i = 0; i < n_out; ++i) {
3046 int j;
3047 j = isl_basic_map_alloc_inequality(result);
3048 if (j < 0)
3049 goto error;
3050 isl_seq_clr(result->ineq[j], 1+total);
3051 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3052 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3053 j = isl_basic_map_alloc_inequality(result);
3054 if (j < 0)
3055 goto error;
3056 isl_seq_clr(result->ineq[j], 1+total);
3057 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3058 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3059 isl_int_sub_ui(result->ineq[j][0], d, 1);
3062 result = isl_basic_map_simplify(result);
3063 return isl_basic_map_finalize(result);
3064 error:
3065 isl_basic_map_free(result);
3066 return NULL;
3069 /* Given a map A -> f(A) and an integer d, construct a map
3070 * A -> floor(f(A)/d).
3072 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3074 int i;
3076 map = isl_map_cow(map);
3077 if (!map)
3078 return NULL;
3080 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3081 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3082 for (i = 0; i < map->n; ++i) {
3083 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3084 if (!map->p[i])
3085 goto error;
3088 return map;
3089 error:
3090 isl_map_free(map);
3091 return NULL;
3094 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3096 int i;
3097 unsigned nparam;
3098 unsigned n_in;
3100 i = isl_basic_map_alloc_equality(bmap);
3101 if (i < 0)
3102 goto error;
3103 nparam = isl_basic_map_n_param(bmap);
3104 n_in = isl_basic_map_n_in(bmap);
3105 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3106 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3107 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3108 return isl_basic_map_finalize(bmap);
3109 error:
3110 isl_basic_map_free(bmap);
3111 return NULL;
3114 /* Add a constraints to "bmap" expressing i_pos < o_pos
3116 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3118 int i;
3119 unsigned nparam;
3120 unsigned n_in;
3122 i = isl_basic_map_alloc_inequality(bmap);
3123 if (i < 0)
3124 goto error;
3125 nparam = isl_basic_map_n_param(bmap);
3126 n_in = isl_basic_map_n_in(bmap);
3127 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3128 isl_int_set_si(bmap->ineq[i][0], -1);
3129 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3130 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3131 return isl_basic_map_finalize(bmap);
3132 error:
3133 isl_basic_map_free(bmap);
3134 return NULL;
3137 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3139 static __isl_give isl_basic_map *var_less_or_equal(
3140 __isl_take isl_basic_map *bmap, unsigned pos)
3142 int i;
3143 unsigned nparam;
3144 unsigned n_in;
3146 i = isl_basic_map_alloc_inequality(bmap);
3147 if (i < 0)
3148 goto error;
3149 nparam = isl_basic_map_n_param(bmap);
3150 n_in = isl_basic_map_n_in(bmap);
3151 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3152 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3153 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3154 return isl_basic_map_finalize(bmap);
3155 error:
3156 isl_basic_map_free(bmap);
3157 return NULL;
3160 /* Add a constraints to "bmap" expressing i_pos > o_pos
3162 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3164 int i;
3165 unsigned nparam;
3166 unsigned n_in;
3168 i = isl_basic_map_alloc_inequality(bmap);
3169 if (i < 0)
3170 goto error;
3171 nparam = isl_basic_map_n_param(bmap);
3172 n_in = isl_basic_map_n_in(bmap);
3173 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3174 isl_int_set_si(bmap->ineq[i][0], -1);
3175 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3176 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3177 return isl_basic_map_finalize(bmap);
3178 error:
3179 isl_basic_map_free(bmap);
3180 return NULL;
3183 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3185 static __isl_give isl_basic_map *var_more_or_equal(
3186 __isl_take isl_basic_map *bmap, unsigned pos)
3188 int i;
3189 unsigned nparam;
3190 unsigned n_in;
3192 i = isl_basic_map_alloc_inequality(bmap);
3193 if (i < 0)
3194 goto error;
3195 nparam = isl_basic_map_n_param(bmap);
3196 n_in = isl_basic_map_n_in(bmap);
3197 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3198 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3199 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3200 return isl_basic_map_finalize(bmap);
3201 error:
3202 isl_basic_map_free(bmap);
3203 return NULL;
3206 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3208 int i;
3209 struct isl_basic_map *bmap;
3210 bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3211 if (!bmap)
3212 return NULL;
3213 for (i = 0; i < n_equal && bmap; ++i)
3214 bmap = var_equal(bmap, i);
3215 return isl_basic_map_finalize(bmap);
3218 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3220 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3222 int i;
3223 struct isl_basic_map *bmap;
3224 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3225 if (!bmap)
3226 return NULL;
3227 for (i = 0; i < pos && bmap; ++i)
3228 bmap = var_equal(bmap, i);
3229 if (bmap)
3230 bmap = var_less(bmap, pos);
3231 return isl_basic_map_finalize(bmap);
3234 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3236 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3237 __isl_take isl_dim *dim, unsigned pos)
3239 int i;
3240 isl_basic_map *bmap;
3242 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3243 for (i = 0; i < pos; ++i)
3244 bmap = var_equal(bmap, i);
3245 bmap = var_less_or_equal(bmap, pos);
3246 return isl_basic_map_finalize(bmap);
3249 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3251 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3253 int i;
3254 struct isl_basic_map *bmap;
3255 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3256 if (!bmap)
3257 return NULL;
3258 for (i = 0; i < pos && bmap; ++i)
3259 bmap = var_equal(bmap, i);
3260 if (bmap)
3261 bmap = var_more(bmap, pos);
3262 return isl_basic_map_finalize(bmap);
3265 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3267 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3268 __isl_take isl_dim *dim, unsigned pos)
3270 int i;
3271 isl_basic_map *bmap;
3273 bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3274 for (i = 0; i < pos; ++i)
3275 bmap = var_equal(bmap, i);
3276 bmap = var_more_or_equal(bmap, pos);
3277 return isl_basic_map_finalize(bmap);
3280 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3281 unsigned n, int equal)
3283 struct isl_map *map;
3284 int i;
3286 if (n == 0 && equal)
3287 return isl_map_universe(dims);
3289 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3291 for (i = 0; i + 1 < n; ++i)
3292 map = isl_map_add_basic_map(map,
3293 isl_basic_map_less_at(isl_dim_copy(dims), i));
3294 if (n > 0) {
3295 if (equal)
3296 map = isl_map_add_basic_map(map,
3297 isl_basic_map_less_or_equal_at(dims, n - 1));
3298 else
3299 map = isl_map_add_basic_map(map,
3300 isl_basic_map_less_at(dims, n - 1));
3301 } else
3302 isl_dim_free(dims);
3304 return map;
3307 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3309 if (!dims)
3310 return NULL;
3311 return map_lex_lte_first(dims, dims->n_out, equal);
3314 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3316 return map_lex_lte_first(dim, n, 0);
3319 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3321 return map_lex_lte_first(dim, n, 1);
3324 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3326 return map_lex_lte(isl_dim_map(set_dim), 0);
3329 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3331 return map_lex_lte(isl_dim_map(set_dim), 1);
3334 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3335 unsigned n, int equal)
3337 struct isl_map *map;
3338 int i;
3340 if (n == 0 && equal)
3341 return isl_map_universe(dims);
3343 map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3345 for (i = 0; i + 1 < n; ++i)
3346 map = isl_map_add_basic_map(map,
3347 isl_basic_map_more_at(isl_dim_copy(dims), i));
3348 if (n > 0) {
3349 if (equal)
3350 map = isl_map_add_basic_map(map,
3351 isl_basic_map_more_or_equal_at(dims, n - 1));
3352 else
3353 map = isl_map_add_basic_map(map,
3354 isl_basic_map_more_at(dims, n - 1));
3355 } else
3356 isl_dim_free(dims);
3358 return map;
3361 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3363 if (!dims)
3364 return NULL;
3365 return map_lex_gte_first(dims, dims->n_out, equal);
3368 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3370 return map_lex_gte_first(dim, n, 0);
3373 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3375 return map_lex_gte_first(dim, n, 1);
3378 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3380 return map_lex_gte(isl_dim_map(set_dim), 0);
3383 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3385 return map_lex_gte(isl_dim_map(set_dim), 1);
3388 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3389 __isl_take isl_set *set2)
3391 isl_map *map;
3392 map = isl_map_lex_le(isl_set_get_dim(set1));
3393 map = isl_map_intersect_domain(map, set1);
3394 map = isl_map_intersect_range(map, set2);
3395 return map;
3398 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3399 __isl_take isl_set *set2)
3401 isl_map *map;
3402 map = isl_map_lex_lt(isl_set_get_dim(set1));
3403 map = isl_map_intersect_domain(map, set1);
3404 map = isl_map_intersect_range(map, set2);
3405 return map;
3408 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3409 __isl_take isl_set *set2)
3411 isl_map *map;
3412 map = isl_map_lex_ge(isl_set_get_dim(set1));
3413 map = isl_map_intersect_domain(map, set1);
3414 map = isl_map_intersect_range(map, set2);
3415 return map;
3418 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3419 __isl_take isl_set *set2)
3421 isl_map *map;
3422 map = isl_map_lex_gt(isl_set_get_dim(set1));
3423 map = isl_map_intersect_domain(map, set1);
3424 map = isl_map_intersect_range(map, set2);
3425 return map;
3428 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3429 __isl_take isl_map *map2)
3431 isl_map *map;
3432 map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3433 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3434 map = isl_map_apply_range(map, isl_map_reverse(map2));
3435 return map;
3438 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3439 __isl_take isl_map *map2)
3441 isl_map *map;
3442 map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3443 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3444 map = isl_map_apply_range(map, isl_map_reverse(map2));
3445 return map;
3448 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3449 __isl_take isl_map *map2)
3451 isl_map *map;
3452 map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3453 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3454 map = isl_map_apply_range(map, isl_map_reverse(map2));
3455 return map;
3458 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3459 __isl_take isl_map *map2)
3461 isl_map *map;
3462 map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3463 map = isl_map_apply_domain(map, isl_map_reverse(map1));
3464 map = isl_map_apply_range(map, isl_map_reverse(map2));
3465 return map;
3468 struct isl_basic_map *isl_basic_map_from_basic_set(
3469 struct isl_basic_set *bset, struct isl_dim *dim)
3471 struct isl_basic_map *bmap;
3473 bset = isl_basic_set_cow(bset);
3474 if (!bset || !dim)
3475 goto error;
3477 isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3478 isl_dim_free(bset->dim);
3479 bmap = (struct isl_basic_map *) bset;
3480 bmap->dim = dim;
3481 return isl_basic_map_finalize(bmap);
3482 error:
3483 isl_basic_set_free(bset);
3484 isl_dim_free(dim);
3485 return NULL;
3488 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3490 if (!bmap)
3491 goto error;
3492 if (bmap->dim->n_in == 0)
3493 return (struct isl_basic_set *)bmap;
3494 bmap = isl_basic_map_cow(bmap);
3495 if (!bmap)
3496 goto error;
3497 bmap->dim = isl_dim_as_set_dim(bmap->dim);
3498 if (!bmap->dim)
3499 goto error;
3500 bmap = isl_basic_map_finalize(bmap);
3501 return (struct isl_basic_set *)bmap;
3502 error:
3503 isl_basic_map_free(bmap);
3504 return NULL;
3507 /* For a div d = floor(f/m), add the constraints
3509 * f - m d >= 0
3510 * -(f-(n-1)) + m d >= 0
3512 * Note that the second constraint is the negation of
3514 * f - m d >= n
3516 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3517 unsigned pos, isl_int *div)
3519 int i, j;
3520 unsigned total = isl_basic_map_total_dim(bmap);
3522 i = isl_basic_map_alloc_inequality(bmap);
3523 if (i < 0)
3524 return -1;
3525 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3526 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3528 j = isl_basic_map_alloc_inequality(bmap);
3529 if (j < 0)
3530 return -1;
3531 isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3532 isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3533 isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3534 return j;
3537 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3538 unsigned pos, isl_int *div)
3540 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3541 pos, div);
3544 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3546 unsigned total = isl_basic_map_total_dim(bmap);
3547 unsigned div_pos = total - bmap->n_div + div;
3549 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3550 bmap->div[div]);
3553 struct isl_basic_set *isl_basic_map_underlying_set(
3554 struct isl_basic_map *bmap)
3556 if (!bmap)
3557 goto error;
3558 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3559 bmap->n_div == 0 &&
3560 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3561 !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3562 return (struct isl_basic_set *)bmap;
3563 bmap = isl_basic_map_cow(bmap);
3564 if (!bmap)
3565 goto error;
3566 bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3567 if (!bmap->dim)
3568 goto error;
3569 bmap->extra -= bmap->n_div;
3570 bmap->n_div = 0;
3571 bmap = isl_basic_map_finalize(bmap);
3572 return (struct isl_basic_set *)bmap;
3573 error:
3574 return NULL;
3577 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3578 __isl_take isl_basic_set *bset)
3580 return isl_basic_map_underlying_set((isl_basic_map *)bset);
3583 struct isl_basic_map *isl_basic_map_overlying_set(
3584 struct isl_basic_set *bset, struct isl_basic_map *like)
3586 struct isl_basic_map *bmap;
3587 struct isl_ctx *ctx;
3588 unsigned total;
3589 int i;
3591 if (!bset || !like)
3592 goto error;
3593 ctx = bset->ctx;
3594 isl_assert(ctx, bset->n_div == 0, goto error);
3595 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3596 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3597 goto error);
3598 if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3599 isl_basic_map_free(like);
3600 return (struct isl_basic_map *)bset;
3602 bset = isl_basic_set_cow(bset);
3603 if (!bset)
3604 goto error;
3605 total = bset->dim->n_out + bset->extra;
3606 bmap = (struct isl_basic_map *)bset;
3607 isl_dim_free(bmap->dim);
3608 bmap->dim = isl_dim_copy(like->dim);
3609 if (!bmap->dim)
3610 goto error;
3611 bmap->n_div = like->n_div;
3612 bmap->extra += like->n_div;
3613 if (bmap->extra) {
3614 unsigned ltotal;
3615 ltotal = total - bmap->extra + like->extra;
3616 if (ltotal > total)
3617 ltotal = total;
3618 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3619 bmap->extra * (1 + 1 + total));
3620 if (isl_blk_is_error(bmap->block2))
3621 goto error;
3622 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3623 bmap->extra);
3624 if (!bmap->div)
3625 goto error;
3626 for (i = 0; i < bmap->extra; ++i)
3627 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3628 for (i = 0; i < like->n_div; ++i) {
3629 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3630 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3632 bmap = isl_basic_map_extend_constraints(bmap,
3633 0, 2 * like->n_div);
3634 for (i = 0; i < like->n_div; ++i) {
3635 if (isl_int_is_zero(bmap->div[i][0]))
3636 continue;
3637 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3638 goto error;
3641 isl_basic_map_free(like);
3642 bmap = isl_basic_map_simplify(bmap);
3643 bmap = isl_basic_map_finalize(bmap);
3644 return bmap;
3645 error:
3646 isl_basic_map_free(like);
3647 isl_basic_set_free(bset);
3648 return NULL;
3651 struct isl_basic_set *isl_basic_set_from_underlying_set(
3652 struct isl_basic_set *bset, struct isl_basic_set *like)
3654 return (struct isl_basic_set *)
3655 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3658 struct isl_set *isl_set_from_underlying_set(
3659 struct isl_set *set, struct isl_basic_set *like)
3661 int i;
3663 if (!set || !like)
3664 goto error;
3665 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3666 goto error);
3667 if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3668 isl_basic_set_free(like);
3669 return set;
3671 set = isl_set_cow(set);
3672 if (!set)
3673 goto error;
3674 for (i = 0; i < set->n; ++i) {
3675 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3676 isl_basic_set_copy(like));
3677 if (!set->p[i])
3678 goto error;
3680 isl_dim_free(set->dim);
3681 set->dim = isl_dim_copy(like->dim);
3682 if (!set->dim)
3683 goto error;
3684 isl_basic_set_free(like);
3685 return set;
3686 error:
3687 isl_basic_set_free(like);
3688 isl_set_free(set);
3689 return NULL;
3692 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3694 int i;
3696 map = isl_map_cow(map);
3697 if (!map)
3698 return NULL;
3699 map->dim = isl_dim_cow(map->dim);
3700 if (!map->dim)
3701 goto error;
3703 for (i = 1; i < map->n; ++i)
3704 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3705 goto error);
3706 for (i = 0; i < map->n; ++i) {
3707 map->p[i] = (struct isl_basic_map *)
3708 isl_basic_map_underlying_set(map->p[i]);
3709 if (!map->p[i])
3710 goto error;
3712 if (map->n == 0)
3713 map->dim = isl_dim_underlying(map->dim, 0);
3714 else {
3715 isl_dim_free(map->dim);
3716 map->dim = isl_dim_copy(map->p[0]->dim);
3718 if (!map->dim)
3719 goto error;
3720 return (struct isl_set *)map;
3721 error:
3722 isl_map_free(map);
3723 return NULL;
3726 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3728 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3731 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3732 __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3734 bmap = isl_basic_map_cow(bmap);
3735 if (!bmap || !dim)
3736 goto error;
3738 isl_dim_free(bmap->dim);
3739 bmap->dim = dim;
3741 bmap = isl_basic_map_finalize(bmap);
3743 return bmap;
3744 error:
3745 isl_basic_map_free(bmap);
3746 isl_dim_free(dim);
3747 return NULL;
3750 static __isl_give isl_basic_set *isl_basic_set_reset_dim(
3751 __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3753 return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3754 dim);
3757 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3758 __isl_take isl_dim *dim)
3760 int i;
3762 map = isl_map_cow(map);
3763 if (!map || !dim)
3764 goto error;
3766 for (i = 0; i < map->n; ++i) {
3767 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3768 isl_dim_copy(dim));
3769 if (!map->p[i])
3770 goto error;
3772 isl_dim_free(map->dim);
3773 map->dim = dim;
3775 return map;
3776 error:
3777 isl_map_free(map);
3778 isl_dim_free(dim);
3779 return NULL;
3782 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3783 __isl_take isl_dim *dim)
3785 return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3788 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3790 isl_dim *dim;
3791 struct isl_basic_set *domain;
3792 unsigned n_in;
3793 unsigned n_out;
3795 if (!bmap)
3796 return NULL;
3797 dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3799 n_in = isl_basic_map_n_in(bmap);
3800 n_out = isl_basic_map_n_out(bmap);
3801 domain = isl_basic_set_from_basic_map(bmap);
3802 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3804 domain = isl_basic_set_reset_dim(domain, dim);
3806 return domain;
3809 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3811 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3814 __isl_give isl_basic_map *isl_basic_map_domain_map(
3815 __isl_take isl_basic_map *bmap)
3817 int i, k;
3818 isl_dim *dim;
3819 isl_basic_map *domain;
3820 isl_basic_set *bset;
3821 int nparam, n_in, n_out;
3822 unsigned total;
3824 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3825 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3826 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3828 dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
3829 domain = isl_basic_map_universe(dim);
3831 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3832 bmap = isl_basic_map_apply_range(bmap, domain);
3833 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
3835 total = isl_basic_map_total_dim(bmap);
3837 for (i = 0; i < n_in; ++i) {
3838 k = isl_basic_map_alloc_equality(bmap);
3839 if (k < 0)
3840 goto error;
3841 isl_seq_clr(bmap->eq[k], 1 + total);
3842 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
3843 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3846 bmap = isl_basic_map_gauss(bmap, NULL);
3847 return isl_basic_map_finalize(bmap);
3848 error:
3849 isl_basic_map_free(bmap);
3850 return NULL;
3853 __isl_give isl_basic_map *isl_basic_map_range_map(
3854 __isl_take isl_basic_map *bmap)
3856 int i, k;
3857 isl_dim *dim;
3858 isl_basic_map *range;
3859 isl_basic_set *bset;
3860 int nparam, n_in, n_out;
3861 unsigned total;
3863 nparam = isl_basic_map_dim(bmap, isl_dim_param);
3864 n_in = isl_basic_map_dim(bmap, isl_dim_in);
3865 n_out = isl_basic_map_dim(bmap, isl_dim_out);
3867 dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
3868 range = isl_basic_map_universe(dim);
3870 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
3871 bmap = isl_basic_map_apply_range(bmap, range);
3872 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
3874 total = isl_basic_map_total_dim(bmap);
3876 for (i = 0; i < n_out; ++i) {
3877 k = isl_basic_map_alloc_equality(bmap);
3878 if (k < 0)
3879 goto error;
3880 isl_seq_clr(bmap->eq[k], 1 + total);
3881 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
3882 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
3885 bmap = isl_basic_map_gauss(bmap, NULL);
3886 return isl_basic_map_finalize(bmap);
3887 error:
3888 isl_basic_map_free(bmap);
3889 return NULL;
3892 struct isl_set *isl_map_range(struct isl_map *map)
3894 int i;
3895 struct isl_set *set;
3897 if (!map)
3898 goto error;
3899 if (isl_map_dim(map, isl_dim_in) == 0)
3900 return (isl_set *)map;
3902 map = isl_map_cow(map);
3903 if (!map)
3904 goto error;
3906 set = (struct isl_set *) map;
3907 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3908 if (!set->dim)
3909 goto error;
3910 for (i = 0; i < map->n; ++i) {
3911 set->p[i] = isl_basic_map_range(map->p[i]);
3912 if (!set->p[i])
3913 goto error;
3915 ISL_F_CLR(set, ISL_MAP_DISJOINT);
3916 ISL_F_CLR(set, ISL_SET_NORMALIZED);
3917 return set;
3918 error:
3919 isl_map_free(map);
3920 return NULL;
3923 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
3925 int i;
3926 isl_dim *domain_dim;
3928 map = isl_map_cow(map);
3929 if (!map)
3930 return NULL;
3932 domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
3933 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3934 map->dim = isl_dim_join(map->dim, domain_dim);
3935 if (!map->dim)
3936 goto error;
3937 for (i = 0; i < map->n; ++i) {
3938 map->p[i] = isl_basic_map_domain_map(map->p[i]);
3939 if (!map->p[i])
3940 goto error;
3942 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3943 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3944 return map;
3945 error:
3946 isl_map_free(map);
3947 return NULL;
3950 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
3952 int i;
3953 isl_dim *range_dim;
3955 map = isl_map_cow(map);
3956 if (!map)
3957 return NULL;
3959 range_dim = isl_dim_range(isl_map_get_dim(map));
3960 map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
3961 map->dim = isl_dim_join(map->dim, range_dim);
3962 if (!map->dim)
3963 goto error;
3964 for (i = 0; i < map->n; ++i) {
3965 map->p[i] = isl_basic_map_range_map(map->p[i]);
3966 if (!map->p[i])
3967 goto error;
3969 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3970 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3971 return map;
3972 error:
3973 isl_map_free(map);
3974 return NULL;
3977 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3979 int i;
3980 struct isl_map *map = NULL;
3982 set = isl_set_cow(set);
3983 if (!set || !dim)
3984 goto error;
3985 isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3986 map = (struct isl_map *)set;
3987 for (i = 0; i < set->n; ++i) {
3988 map->p[i] = isl_basic_map_from_basic_set(
3989 set->p[i], isl_dim_copy(dim));
3990 if (!map->p[i])
3991 goto error;
3993 isl_dim_free(map->dim);
3994 map->dim = dim;
3995 return map;
3996 error:
3997 isl_dim_free(dim);
3998 isl_set_free(set);
3999 return NULL;
4002 __isl_give isl_basic_map *isl_basic_map_from_domain(
4003 __isl_take isl_basic_set *bset)
4005 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4008 __isl_give isl_basic_map *isl_basic_map_from_range(
4009 __isl_take isl_basic_set *bset)
4011 return (isl_basic_map *)bset;
4014 struct isl_map *isl_map_from_range(struct isl_set *set)
4016 return (struct isl_map *)set;
4019 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4021 return isl_map_reverse(isl_map_from_range(set));
4024 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4025 __isl_take isl_set *range)
4027 return isl_map_apply_range(isl_map_from_domain(domain),
4028 isl_map_from_range(range));
4031 struct isl_set *isl_set_from_map(struct isl_map *map)
4033 int i;
4034 struct isl_set *set = NULL;
4036 if (!map)
4037 return NULL;
4038 map = isl_map_cow(map);
4039 if (!map)
4040 return NULL;
4041 map->dim = isl_dim_as_set_dim(map->dim);
4042 if (!map->dim)
4043 goto error;
4044 set = (struct isl_set *)map;
4045 for (i = 0; i < map->n; ++i) {
4046 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4047 if (!set->p[i])
4048 goto error;
4050 return set;
4051 error:
4052 isl_map_free(map);
4053 return NULL;
4056 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4058 struct isl_map *map;
4060 if (!dim)
4061 return NULL;
4062 isl_assert(dim->ctx, n >= 0, return NULL);
4063 map = isl_alloc(dim->ctx, struct isl_map,
4064 sizeof(struct isl_map) +
4065 (n - 1) * sizeof(struct isl_basic_map *));
4066 if (!map)
4067 goto error;
4069 map->ctx = dim->ctx;
4070 isl_ctx_ref(map->ctx);
4071 map->ref = 1;
4072 map->size = n;
4073 map->n = 0;
4074 map->dim = dim;
4075 map->flags = flags;
4076 return map;
4077 error:
4078 isl_dim_free(dim);
4079 return NULL;
4082 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4083 unsigned nparam, unsigned in, unsigned out, int n,
4084 unsigned flags)
4086 struct isl_map *map;
4087 struct isl_dim *dims;
4089 dims = isl_dim_alloc(ctx, nparam, in, out);
4090 if (!dims)
4091 return NULL;
4093 map = isl_map_alloc_dim(dims, n, flags);
4094 return map;
4097 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4099 struct isl_basic_map *bmap;
4100 bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4101 bmap = isl_basic_map_set_to_empty(bmap);
4102 return bmap;
4105 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4107 struct isl_basic_set *bset;
4108 bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4109 bset = isl_basic_set_set_to_empty(bset);
4110 return bset;
4113 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4115 struct isl_basic_map *bmap;
4116 if (!model)
4117 return NULL;
4118 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4119 bmap = isl_basic_map_set_to_empty(bmap);
4120 return bmap;
4123 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4125 struct isl_basic_map *bmap;
4126 if (!model)
4127 return NULL;
4128 bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4129 bmap = isl_basic_map_set_to_empty(bmap);
4130 return bmap;
4133 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4135 struct isl_basic_set *bset;
4136 if (!model)
4137 return NULL;
4138 bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4139 bset = isl_basic_set_set_to_empty(bset);
4140 return bset;
4143 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4145 struct isl_basic_map *bmap;
4146 bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4147 bmap = isl_basic_map_finalize(bmap);
4148 return bmap;
4151 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4153 struct isl_basic_set *bset;
4154 bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4155 bset = isl_basic_set_finalize(bset);
4156 return bset;
4159 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4161 int i;
4162 unsigned total = isl_dim_total(dim);
4163 isl_basic_map *bmap;
4165 bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4166 for (i = 0; i < total; ++i) {
4167 int k = isl_basic_map_alloc_inequality(bmap);
4168 if (k < 0)
4169 goto error;
4170 isl_seq_clr(bmap->ineq[k], 1 + total);
4171 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4173 return bmap;
4174 error:
4175 isl_basic_map_free(bmap);
4176 return NULL;
4179 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4181 return isl_basic_map_nat_universe(dim);
4184 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4186 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4189 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4191 return isl_map_nat_universe(dim);
4194 __isl_give isl_basic_map *isl_basic_map_universe_like(
4195 __isl_keep isl_basic_map *model)
4197 if (!model)
4198 return NULL;
4199 return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4202 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4204 if (!model)
4205 return NULL;
4206 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4209 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4210 __isl_keep isl_set *model)
4212 if (!model)
4213 return NULL;
4214 return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4217 struct isl_map *isl_map_empty(struct isl_dim *dim)
4219 return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4222 struct isl_map *isl_map_empty_like(struct isl_map *model)
4224 if (!model)
4225 return NULL;
4226 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4229 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4231 if (!model)
4232 return NULL;
4233 return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4236 struct isl_set *isl_set_empty(struct isl_dim *dim)
4238 return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4241 struct isl_set *isl_set_empty_like(struct isl_set *model)
4243 if (!model)
4244 return NULL;
4245 return isl_set_empty(isl_dim_copy(model->dim));
4248 struct isl_map *isl_map_universe(struct isl_dim *dim)
4250 struct isl_map *map;
4251 if (!dim)
4252 return NULL;
4253 map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4254 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4255 return map;
4258 struct isl_set *isl_set_universe(struct isl_dim *dim)
4260 struct isl_set *set;
4261 if (!dim)
4262 return NULL;
4263 set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4264 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4265 return set;
4268 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4270 if (!model)
4271 return NULL;
4272 return isl_set_universe(isl_dim_copy(model->dim));
4275 struct isl_map *isl_map_dup(struct isl_map *map)
4277 int i;
4278 struct isl_map *dup;
4280 if (!map)
4281 return NULL;
4282 dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4283 for (i = 0; i < map->n; ++i)
4284 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4285 return dup;
4288 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4289 __isl_take isl_basic_map *bmap)
4291 if (!bmap || !map)
4292 goto error;
4293 if (isl_basic_map_fast_is_empty(bmap)) {
4294 isl_basic_map_free(bmap);
4295 return map;
4297 isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4298 isl_assert(map->ctx, map->n < map->size, goto error);
4299 map->p[map->n] = bmap;
4300 map->n++;
4301 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4302 return map;
4303 error:
4304 if (map)
4305 isl_map_free(map);
4306 if (bmap)
4307 isl_basic_map_free(bmap);
4308 return NULL;
4311 void isl_map_free(struct isl_map *map)
4313 int i;
4315 if (!map)
4316 return;
4318 if (--map->ref > 0)
4319 return;
4321 isl_ctx_deref(map->ctx);
4322 for (i = 0; i < map->n; ++i)
4323 isl_basic_map_free(map->p[i]);
4324 isl_dim_free(map->dim);
4325 free(map);
4328 struct isl_map *isl_map_extend(struct isl_map *base,
4329 unsigned nparam, unsigned n_in, unsigned n_out)
4331 int i;
4333 base = isl_map_cow(base);
4334 if (!base)
4335 return NULL;
4337 base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4338 if (!base->dim)
4339 goto error;
4340 for (i = 0; i < base->n; ++i) {
4341 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4342 isl_dim_copy(base->dim), 0, 0, 0);
4343 if (!base->p[i])
4344 goto error;
4346 return base;
4347 error:
4348 isl_map_free(base);
4349 return NULL;
4352 struct isl_set *isl_set_extend(struct isl_set *base,
4353 unsigned nparam, unsigned dim)
4355 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4356 nparam, 0, dim);
4359 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4360 struct isl_basic_map *bmap, unsigned pos, int value)
4362 int j;
4364 bmap = isl_basic_map_cow(bmap);
4365 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4366 j = isl_basic_map_alloc_equality(bmap);
4367 if (j < 0)
4368 goto error;
4369 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4370 isl_int_set_si(bmap->eq[j][pos], -1);
4371 isl_int_set_si(bmap->eq[j][0], value);
4372 bmap = isl_basic_map_simplify(bmap);
4373 return isl_basic_map_finalize(bmap);
4374 error:
4375 isl_basic_map_free(bmap);
4376 return NULL;
4379 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4380 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4382 int j;
4384 bmap = isl_basic_map_cow(bmap);
4385 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4386 j = isl_basic_map_alloc_equality(bmap);
4387 if (j < 0)
4388 goto error;
4389 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4390 isl_int_set_si(bmap->eq[j][pos], -1);
4391 isl_int_set(bmap->eq[j][0], value);
4392 bmap = isl_basic_map_simplify(bmap);
4393 return isl_basic_map_finalize(bmap);
4394 error:
4395 isl_basic_map_free(bmap);
4396 return NULL;
4399 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4400 enum isl_dim_type type, unsigned pos, int value)
4402 if (!bmap)
4403 return NULL;
4404 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4405 return isl_basic_map_fix_pos_si(bmap,
4406 isl_basic_map_offset(bmap, type) + pos, value);
4407 error:
4408 isl_basic_map_free(bmap);
4409 return NULL;
4412 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4413 enum isl_dim_type type, unsigned pos, isl_int value)
4415 if (!bmap)
4416 return NULL;
4417 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4418 return isl_basic_map_fix_pos(bmap,
4419 isl_basic_map_offset(bmap, type) + pos, value);
4420 error:
4421 isl_basic_map_free(bmap);
4422 return NULL;
4425 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4426 enum isl_dim_type type, unsigned pos, int value)
4428 return (struct isl_basic_set *)
4429 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4430 type, pos, value);
4433 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4434 enum isl_dim_type type, unsigned pos, isl_int value)
4436 return (struct isl_basic_set *)
4437 isl_basic_map_fix((struct isl_basic_map *)bset,
4438 type, pos, value);
4441 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4442 unsigned input, int value)
4444 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4447 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4448 unsigned dim, int value)
4450 return (struct isl_basic_set *)
4451 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4452 isl_dim_set, dim, value);
4455 struct isl_map *isl_map_fix_si(struct isl_map *map,
4456 enum isl_dim_type type, unsigned pos, int value)
4458 int i;
4460 map = isl_map_cow(map);
4461 if (!map)
4462 return NULL;
4464 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4465 for (i = 0; i < map->n; ++i) {
4466 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4467 if (!map->p[i])
4468 goto error;
4470 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4471 return map;
4472 error:
4473 isl_map_free(map);
4474 return NULL;
4477 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4478 enum isl_dim_type type, unsigned pos, int value)
4480 return (struct isl_set *)
4481 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4484 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4485 enum isl_dim_type type, unsigned pos, isl_int value)
4487 int i;
4489 map = isl_map_cow(map);
4490 if (!map)
4491 return NULL;
4493 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4494 for (i = 0; i < map->n; ++i) {
4495 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4496 if (!map->p[i])
4497 goto error;
4499 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4500 return map;
4501 error:
4502 isl_map_free(map);
4503 return NULL;
4506 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4507 enum isl_dim_type type, unsigned pos, isl_int value)
4509 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4512 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4513 unsigned input, int value)
4515 return isl_map_fix_si(map, isl_dim_in, input, value);
4518 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4520 return (struct isl_set *)
4521 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4524 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4525 __isl_take isl_basic_map *bmap,
4526 enum isl_dim_type type, unsigned pos, int value)
4528 int j;
4530 if (!bmap)
4531 return NULL;
4532 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4533 pos += isl_basic_map_offset(bmap, type);
4534 bmap = isl_basic_map_cow(bmap);
4535 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4536 j = isl_basic_map_alloc_inequality(bmap);
4537 if (j < 0)
4538 goto error;
4539 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4540 isl_int_set_si(bmap->ineq[j][pos], 1);
4541 isl_int_set_si(bmap->ineq[j][0], -value);
4542 bmap = isl_basic_map_simplify(bmap);
4543 return isl_basic_map_finalize(bmap);
4544 error:
4545 isl_basic_map_free(bmap);
4546 return NULL;
4549 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4550 unsigned dim, isl_int value)
4552 int j;
4554 bset = isl_basic_set_cow(bset);
4555 bset = isl_basic_set_extend_constraints(bset, 0, 1);
4556 j = isl_basic_set_alloc_inequality(bset);
4557 if (j < 0)
4558 goto error;
4559 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4560 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4561 isl_int_neg(bset->ineq[j][0], value);
4562 bset = isl_basic_set_simplify(bset);
4563 return isl_basic_set_finalize(bset);
4564 error:
4565 isl_basic_set_free(bset);
4566 return NULL;
4569 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4570 enum isl_dim_type type, unsigned pos, int value)
4572 int i;
4574 map = isl_map_cow(map);
4575 if (!map)
4576 return NULL;
4578 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4579 for (i = 0; i < map->n; ++i) {
4580 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4581 type, pos, value);
4582 if (!map->p[i])
4583 goto error;
4585 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4586 return map;
4587 error:
4588 isl_map_free(map);
4589 return NULL;
4592 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4593 enum isl_dim_type type, unsigned pos, int value)
4595 return (struct isl_set *)
4596 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4599 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4600 isl_int value)
4602 int i;
4604 set = isl_set_cow(set);
4605 if (!set)
4606 return NULL;
4608 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4609 for (i = 0; i < set->n; ++i) {
4610 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4611 if (!set->p[i])
4612 goto error;
4614 return set;
4615 error:
4616 isl_set_free(set);
4617 return NULL;
4620 struct isl_map *isl_map_reverse(struct isl_map *map)
4622 int i;
4624 map = isl_map_cow(map);
4625 if (!map)
4626 return NULL;
4628 map->dim = isl_dim_reverse(map->dim);
4629 if (!map->dim)
4630 goto error;
4631 for (i = 0; i < map->n; ++i) {
4632 map->p[i] = isl_basic_map_reverse(map->p[i]);
4633 if (!map->p[i])
4634 goto error;
4636 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4637 return map;
4638 error:
4639 isl_map_free(map);
4640 return NULL;
4643 static struct isl_map *isl_basic_map_partial_lexopt(
4644 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4645 struct isl_set **empty, int max)
4647 if (!bmap)
4648 goto error;
4649 if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4650 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4651 else
4652 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4653 error:
4654 isl_basic_map_free(bmap);
4655 isl_basic_set_free(dom);
4656 if (empty)
4657 *empty = NULL;
4658 return NULL;
4661 struct isl_map *isl_basic_map_partial_lexmax(
4662 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4663 struct isl_set **empty)
4665 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4668 struct isl_map *isl_basic_map_partial_lexmin(
4669 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4670 struct isl_set **empty)
4672 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4675 struct isl_set *isl_basic_set_partial_lexmin(
4676 struct isl_basic_set *bset, struct isl_basic_set *dom,
4677 struct isl_set **empty)
4679 return (struct isl_set *)
4680 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4681 dom, empty);
4684 struct isl_set *isl_basic_set_partial_lexmax(
4685 struct isl_basic_set *bset, struct isl_basic_set *dom,
4686 struct isl_set **empty)
4688 return (struct isl_set *)
4689 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4690 dom, empty);
4693 /* Given a basic map "bmap", compute the lexicographically minimal
4694 * (or maximal) image element for each domain element in dom.
4695 * Set *empty to those elements in dom that do not have an image element.
4697 * We first make sure the basic sets in dom are disjoint and then
4698 * simply collect the results over each of the basic sets separately.
4699 * We could probably improve the efficiency a bit by moving the union
4700 * domain down into the parametric integer programming.
4702 static __isl_give isl_map *basic_map_partial_lexopt(
4703 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4704 __isl_give isl_set **empty, int max)
4706 int i;
4707 struct isl_map *res;
4709 dom = isl_set_make_disjoint(dom);
4710 if (!dom)
4711 goto error;
4713 if (isl_set_fast_is_empty(dom)) {
4714 res = isl_map_empty_like_basic_map(bmap);
4715 *empty = isl_set_empty_like(dom);
4716 isl_set_free(dom);
4717 isl_basic_map_free(bmap);
4718 return res;
4721 res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4722 isl_basic_set_copy(dom->p[0]), empty, max);
4724 for (i = 1; i < dom->n; ++i) {
4725 struct isl_map *res_i;
4726 struct isl_set *empty_i;
4728 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4729 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4731 res = isl_map_union_disjoint(res, res_i);
4732 *empty = isl_set_union_disjoint(*empty, empty_i);
4735 isl_set_free(dom);
4736 isl_basic_map_free(bmap);
4737 return res;
4738 error:
4739 *empty = NULL;
4740 isl_set_free(dom);
4741 isl_basic_map_free(bmap);
4742 return NULL;
4745 /* Given a map "map", compute the lexicographically minimal
4746 * (or maximal) image element for each domain element in dom.
4747 * Set *empty to those elements in dom that do not have an image element.
4749 * We first compute the lexicographically minimal or maximal element
4750 * in the first basic map. This results in a partial solution "res"
4751 * and a subset "todo" of dom that still need to be handled.
4752 * We then consider each of the remaining maps in "map" and successively
4753 * improve both "res" and "todo".
4755 * Let res^k and todo^k be the results after k steps and let i = k + 1.
4756 * Assume we are computing the lexicographical maximum.
4757 * We first compute the lexicographically maximal element in basic map i.
4758 * This results in a partial solution res_i and a subset todo_i.
4759 * Then we combine these results with those obtain for the first k basic maps
4760 * to obtain a result that is valid for the first k+1 basic maps.
4761 * In particular, the set where there is no solution is the set where
4762 * there is no solution for the first k basic maps and also no solution
4763 * for the ith basic map, i.e.,
4765 * todo^i = todo^k * todo_i
4767 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4768 * solutions, arbitrarily breaking ties in favor of res^k.
4769 * That is, when res^k(a) >= res_i(a), we pick res^k and
4770 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
4771 * the lexicographic order.)
4772 * In practice, we compute
4774 * res^k * (res_i . "<=")
4776 * and
4778 * res_i * (res^k . "<")
4780 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
4781 * where only one of res^k and res_i provides a solution and we simply pick
4782 * that one, i.e.,
4784 * res^k * todo_i
4785 * and
4786 * res_i * todo^k
4788 * Note that we only compute these intersections when dom(res^k) intersects
4789 * dom(res_i). Otherwise, the only effect of these intersections is to
4790 * potentially break up res^k and res_i into smaller pieces.
4791 * We want to avoid such splintering as much as possible.
4792 * In fact, an earlier implementation of this function would look for
4793 * better results in the domain of res^k and for extra results in todo^k,
4794 * but this would always result in a splintering according to todo^k,
4795 * even when the domain of basic map i is disjoint from the domains of
4796 * the previous basic maps.
4798 static __isl_give isl_map *isl_map_partial_lexopt(
4799 __isl_take isl_map *map, __isl_take isl_set *dom,
4800 __isl_give isl_set **empty, int max)
4802 int i;
4803 struct isl_map *res;
4804 struct isl_set *todo;
4806 if (!map || !dom)
4807 goto error;
4809 if (isl_map_fast_is_empty(map)) {
4810 if (empty)
4811 *empty = dom;
4812 else
4813 isl_set_free(dom);
4814 return map;
4817 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4818 isl_set_copy(dom), &todo, max);
4820 for (i = 1; i < map->n; ++i) {
4821 isl_map *lt, *le;
4822 isl_map *res_i;
4823 isl_set *todo_i;
4824 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
4826 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4827 isl_set_copy(dom), &todo_i, max);
4829 if (max) {
4830 lt = isl_map_lex_lt(isl_dim_copy(dim));
4831 le = isl_map_lex_le(dim);
4832 } else {
4833 lt = isl_map_lex_gt(isl_dim_copy(dim));
4834 le = isl_map_lex_ge(dim);
4836 lt = isl_map_apply_range(isl_map_copy(res), lt);
4837 lt = isl_map_intersect(lt, isl_map_copy(res_i));
4838 le = isl_map_apply_range(isl_map_copy(res_i), le);
4839 le = isl_map_intersect(le, isl_map_copy(res));
4841 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
4842 res = isl_map_intersect_domain(res,
4843 isl_set_copy(todo_i));
4844 res_i = isl_map_intersect_domain(res_i,
4845 isl_set_copy(todo));
4848 res = isl_map_union_disjoint(res, res_i);
4849 res = isl_map_union_disjoint(res, lt);
4850 res = isl_map_union_disjoint(res, le);
4852 todo = isl_set_intersect(todo, todo_i);
4855 isl_set_free(dom);
4856 isl_map_free(map);
4858 if (empty)
4859 *empty = todo;
4860 else
4861 isl_set_free(todo);
4863 return res;
4864 error:
4865 if (empty)
4866 *empty = NULL;
4867 isl_set_free(dom);
4868 isl_map_free(map);
4869 return NULL;
4872 __isl_give isl_map *isl_map_partial_lexmax(
4873 __isl_take isl_map *map, __isl_take isl_set *dom,
4874 __isl_give isl_set **empty)
4876 return isl_map_partial_lexopt(map, dom, empty, 1);
4879 __isl_give isl_map *isl_map_partial_lexmin(
4880 __isl_take isl_map *map, __isl_take isl_set *dom,
4881 __isl_give isl_set **empty)
4883 return isl_map_partial_lexopt(map, dom, empty, 0);
4886 __isl_give isl_set *isl_set_partial_lexmin(
4887 __isl_take isl_set *set, __isl_take isl_set *dom,
4888 __isl_give isl_set **empty)
4890 return (struct isl_set *)
4891 isl_map_partial_lexmin((struct isl_map *)set,
4892 dom, empty);
4895 __isl_give isl_set *isl_set_partial_lexmax(
4896 __isl_take isl_set *set, __isl_take isl_set *dom,
4897 __isl_give isl_set **empty)
4899 return (struct isl_set *)
4900 isl_map_partial_lexmax((struct isl_map *)set,
4901 dom, empty);
4904 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4906 struct isl_basic_set *dom = NULL;
4907 struct isl_dim *dom_dim;
4909 if (!bmap)
4910 goto error;
4911 dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4912 dom = isl_basic_set_universe(dom_dim);
4913 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4914 error:
4915 isl_basic_map_free(bmap);
4916 return NULL;
4919 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4921 return isl_basic_map_lexopt(bmap, 0);
4924 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4926 return isl_basic_map_lexopt(bmap, 1);
4929 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4931 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4934 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4936 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4939 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4941 struct isl_set *dom = NULL;
4942 struct isl_dim *dom_dim;
4944 if (!map)
4945 goto error;
4946 dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4947 dom = isl_set_universe(dom_dim);
4948 return isl_map_partial_lexopt(map, dom, NULL, max);
4949 error:
4950 isl_map_free(map);
4951 return NULL;
4954 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4956 return isl_map_lexopt(map, 0);
4959 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4961 return isl_map_lexopt(map, 1);
4964 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4966 return (isl_set *)isl_map_lexmin((isl_map *)set);
4969 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4971 return (isl_set *)isl_map_lexmax((isl_map *)set);
4974 /* Apply a preimage specified by "mat" on the parameters of "bset".
4975 * bset is assumed to have only parameters and divs.
4977 static struct isl_basic_set *basic_set_parameter_preimage(
4978 struct isl_basic_set *bset, struct isl_mat *mat)
4980 unsigned nparam;
4982 if (!bset || !mat)
4983 goto error;
4985 bset->dim = isl_dim_cow(bset->dim);
4986 if (!bset->dim)
4987 goto error;
4989 nparam = isl_basic_set_dim(bset, isl_dim_param);
4991 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4993 bset->dim->nparam = 0;
4994 bset->dim->n_out = nparam;
4995 bset = isl_basic_set_preimage(bset, mat);
4996 if (bset) {
4997 bset->dim->nparam = bset->dim->n_out;
4998 bset->dim->n_out = 0;
5000 return bset;
5001 error:
5002 isl_mat_free(mat);
5003 isl_basic_set_free(bset);
5004 return NULL;
5007 /* Apply a preimage specified by "mat" on the parameters of "set".
5008 * set is assumed to have only parameters and divs.
5010 static struct isl_set *set_parameter_preimage(
5011 struct isl_set *set, struct isl_mat *mat)
5013 struct isl_dim *dim = NULL;
5014 unsigned nparam;
5016 if (!set || !mat)
5017 goto error;
5019 dim = isl_dim_copy(set->dim);
5020 dim = isl_dim_cow(dim);
5021 if (!dim)
5022 goto error;
5024 nparam = isl_set_dim(set, isl_dim_param);
5026 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5028 dim->nparam = 0;
5029 dim->n_out = nparam;
5030 isl_set_reset_dim(set, dim);
5031 set = isl_set_preimage(set, mat);
5032 if (!set)
5033 goto error2;
5034 dim = isl_dim_copy(set->dim);
5035 dim = isl_dim_cow(dim);
5036 if (!dim)
5037 goto error2;
5038 dim->nparam = dim->n_out;
5039 dim->n_out = 0;
5040 isl_set_reset_dim(set, dim);
5041 return set;
5042 error:
5043 isl_dim_free(dim);
5044 isl_mat_free(mat);
5045 error2:
5046 isl_set_free(set);
5047 return NULL;
5050 /* Intersect the basic set "bset" with the affine space specified by the
5051 * equalities in "eq".
5053 static struct isl_basic_set *basic_set_append_equalities(
5054 struct isl_basic_set *bset, struct isl_mat *eq)
5056 int i, k;
5057 unsigned len;
5059 if (!bset || !eq)
5060 goto error;
5062 bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5063 eq->n_row, 0);
5064 if (!bset)
5065 goto error;
5067 len = 1 + isl_dim_total(bset->dim) + bset->extra;
5068 for (i = 0; i < eq->n_row; ++i) {
5069 k = isl_basic_set_alloc_equality(bset);
5070 if (k < 0)
5071 goto error;
5072 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5073 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5075 isl_mat_free(eq);
5077 bset = isl_basic_set_gauss(bset, NULL);
5078 bset = isl_basic_set_finalize(bset);
5080 return bset;
5081 error:
5082 isl_mat_free(eq);
5083 isl_basic_set_free(bset);
5084 return NULL;
5087 /* Intersect the set "set" with the affine space specified by the
5088 * equalities in "eq".
5090 static struct isl_set *set_append_equalities(struct isl_set *set,
5091 struct isl_mat *eq)
5093 int i;
5095 if (!set || !eq)
5096 goto error;
5098 for (i = 0; i < set->n; ++i) {
5099 set->p[i] = basic_set_append_equalities(set->p[i],
5100 isl_mat_copy(eq));
5101 if (!set->p[i])
5102 goto error;
5104 isl_mat_free(eq);
5105 return set;
5106 error:
5107 isl_mat_free(eq);
5108 isl_set_free(set);
5109 return NULL;
5112 /* Project the given basic set onto its parameter domain, possibly introducing
5113 * new, explicit, existential variables in the constraints.
5114 * The input has parameters and (possibly implicit) existential variables.
5115 * The output has the same parameters, but only
5116 * explicit existentially quantified variables.
5118 * The actual projection is performed by pip, but pip doesn't seem
5119 * to like equalities very much, so we first remove the equalities
5120 * among the parameters by performing a variable compression on
5121 * the parameters. Afterward, an inverse transformation is performed
5122 * and the equalities among the parameters are inserted back in.
5124 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5126 int i, j;
5127 struct isl_mat *eq;
5128 struct isl_mat *T, *T2;
5129 struct isl_set *set;
5130 unsigned nparam, n_div;
5132 bset = isl_basic_set_cow(bset);
5133 if (!bset)
5134 return NULL;
5136 if (bset->n_eq == 0)
5137 return isl_basic_set_lexmin(bset);
5139 isl_basic_set_gauss(bset, NULL);
5141 nparam = isl_basic_set_dim(bset, isl_dim_param);
5142 n_div = isl_basic_set_dim(bset, isl_dim_div);
5144 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5145 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5146 ++i;
5148 if (i == bset->n_eq)
5149 return isl_basic_set_lexmin(bset);
5151 eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
5152 0, 1 + nparam);
5153 eq = isl_mat_cow(eq);
5154 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5155 if (T && T->n_col == 0) {
5156 isl_mat_free(T);
5157 isl_mat_free(T2);
5158 isl_mat_free(eq);
5159 bset = isl_basic_set_set_to_empty(bset);
5160 return isl_set_from_basic_set(bset);
5162 bset = basic_set_parameter_preimage(bset, T);
5164 set = isl_basic_set_lexmin(bset);
5165 set = set_parameter_preimage(set, T2);
5166 set = set_append_equalities(set, eq);
5167 return set;
5170 /* Compute an explicit representation for all the existentially
5171 * quantified variables.
5172 * The input and output dimensions are first turned into parameters.
5173 * compute_divs then returns a map with the same parameters and
5174 * no input or output dimensions and the dimension specification
5175 * is reset to that of the input.
5177 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5179 struct isl_basic_set *bset;
5180 struct isl_set *set;
5181 struct isl_map *map;
5182 struct isl_dim *dim, *orig_dim = NULL;
5183 unsigned nparam;
5184 unsigned n_in;
5185 unsigned n_out;
5187 bmap = isl_basic_map_cow(bmap);
5188 if (!bmap)
5189 return NULL;
5191 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5192 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5193 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5194 dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5195 if (!dim)
5196 goto error;
5198 orig_dim = bmap->dim;
5199 bmap->dim = dim;
5200 bset = (struct isl_basic_set *)bmap;
5202 set = parameter_compute_divs(bset);
5203 map = (struct isl_map *)set;
5204 map = isl_map_reset_dim(map, orig_dim);
5206 return map;
5207 error:
5208 isl_basic_map_free(bmap);
5209 return NULL;
5212 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5214 int i;
5215 unsigned off;
5217 if (!bmap)
5218 return -1;
5220 off = isl_dim_total(bmap->dim);
5221 for (i = 0; i < bmap->n_div; ++i) {
5222 if (isl_int_is_zero(bmap->div[i][0]))
5223 return 0;
5224 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5225 return -1);
5227 return 1;
5230 static int map_divs_known(__isl_keep isl_map *map)
5232 int i;
5234 if (!map)
5235 return -1;
5237 for (i = 0; i < map->n; ++i) {
5238 int known = basic_map_divs_known(map->p[i]);
5239 if (known <= 0)
5240 return known;
5243 return 1;
5246 /* If bmap contains any unknown divs, then compute explicit
5247 * expressions for them. However, this computation may be
5248 * quite expensive, so first try to remove divs that aren't
5249 * strictly needed.
5251 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5253 int i;
5254 int known;
5255 struct isl_map *map;
5257 known = basic_map_divs_known(bmap);
5258 if (known < 0)
5259 goto error;
5260 if (known)
5261 return isl_map_from_basic_map(bmap);
5263 bmap = isl_basic_map_drop_redundant_divs(bmap);
5265 known = basic_map_divs_known(bmap);
5266 if (known < 0)
5267 goto error;
5268 if (known)
5269 return isl_map_from_basic_map(bmap);
5271 map = compute_divs(bmap);
5272 return map;
5273 error:
5274 isl_basic_map_free(bmap);
5275 return NULL;
5278 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5280 int i;
5281 int known;
5282 struct isl_map *res;
5284 if (!map)
5285 return NULL;
5286 if (map->n == 0)
5287 return map;
5289 known = map_divs_known(map);
5290 if (known < 0) {
5291 isl_map_free(map);
5292 return NULL;
5294 if (known)
5295 return map;
5297 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5298 for (i = 1 ; i < map->n; ++i) {
5299 struct isl_map *r2;
5300 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5301 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5302 res = isl_map_union_disjoint(res, r2);
5303 else
5304 res = isl_map_union(res, r2);
5306 isl_map_free(map);
5308 return res;
5311 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5313 return (struct isl_set *)
5314 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5317 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5319 return (struct isl_set *)
5320 isl_map_compute_divs((struct isl_map *)set);
5323 struct isl_set *isl_map_domain(struct isl_map *map)
5325 int i;
5326 struct isl_set *set;
5328 if (!map)
5329 goto error;
5331 map = isl_map_cow(map);
5332 if (!map)
5333 return NULL;
5335 set = (struct isl_set *)map;
5336 set->dim = isl_dim_domain(set->dim);
5337 if (!set->dim)
5338 goto error;
5339 for (i = 0; i < map->n; ++i) {
5340 set->p[i] = isl_basic_map_domain(map->p[i]);
5341 if (!set->p[i])
5342 goto error;
5344 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5345 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5346 return set;
5347 error:
5348 isl_map_free(map);
5349 return NULL;
5352 struct isl_map *isl_map_union_disjoint(
5353 struct isl_map *map1, struct isl_map *map2)
5355 int i;
5356 unsigned flags = 0;
5357 struct isl_map *map = NULL;
5359 if (!map1 || !map2)
5360 goto error;
5362 if (map1->n == 0) {
5363 isl_map_free(map1);
5364 return map2;
5366 if (map2->n == 0) {
5367 isl_map_free(map2);
5368 return map1;
5371 isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5373 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5374 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5375 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5377 map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5378 map1->n + map2->n, flags);
5379 if (!map)
5380 goto error;
5381 for (i = 0; i < map1->n; ++i) {
5382 map = isl_map_add_basic_map(map,
5383 isl_basic_map_copy(map1->p[i]));
5384 if (!map)
5385 goto error;
5387 for (i = 0; i < map2->n; ++i) {
5388 map = isl_map_add_basic_map(map,
5389 isl_basic_map_copy(map2->p[i]));
5390 if (!map)
5391 goto error;
5393 isl_map_free(map1);
5394 isl_map_free(map2);
5395 return map;
5396 error:
5397 isl_map_free(map);
5398 isl_map_free(map1);
5399 isl_map_free(map2);
5400 return NULL;
5403 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5405 map1 = isl_map_union_disjoint(map1, map2);
5406 if (!map1)
5407 return NULL;
5408 if (map1->n > 1)
5409 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5410 return map1;
5413 struct isl_set *isl_set_union_disjoint(
5414 struct isl_set *set1, struct isl_set *set2)
5416 return (struct isl_set *)
5417 isl_map_union_disjoint(
5418 (struct isl_map *)set1, (struct isl_map *)set2);
5421 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5423 return (struct isl_set *)
5424 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5427 struct isl_map *isl_map_intersect_range(
5428 struct isl_map *map, struct isl_set *set)
5430 unsigned flags = 0;
5431 struct isl_map *result;
5432 int i, j;
5434 if (!map || !set)
5435 goto error;
5437 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5438 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5439 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5441 result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5442 map->n * set->n, flags);
5443 if (!result)
5444 goto error;
5445 for (i = 0; i < map->n; ++i)
5446 for (j = 0; j < set->n; ++j) {
5447 result = isl_map_add_basic_map(result,
5448 isl_basic_map_intersect_range(
5449 isl_basic_map_copy(map->p[i]),
5450 isl_basic_set_copy(set->p[j])));
5451 if (!result)
5452 goto error;
5454 isl_map_free(map);
5455 isl_set_free(set);
5456 return result;
5457 error:
5458 isl_map_free(map);
5459 isl_set_free(set);
5460 return NULL;
5463 struct isl_map *isl_map_intersect_domain(
5464 struct isl_map *map, struct isl_set *set)
5466 return isl_map_reverse(
5467 isl_map_intersect_range(isl_map_reverse(map), set));
5470 struct isl_map *isl_map_apply_domain(
5471 struct isl_map *map1, struct isl_map *map2)
5473 if (!map1 || !map2)
5474 goto error;
5475 map1 = isl_map_reverse(map1);
5476 map1 = isl_map_apply_range(map1, map2);
5477 return isl_map_reverse(map1);
5478 error:
5479 isl_map_free(map1);
5480 isl_map_free(map2);
5481 return NULL;
5484 struct isl_map *isl_map_apply_range(
5485 struct isl_map *map1, struct isl_map *map2)
5487 struct isl_dim *dim_result;
5488 struct isl_map *result;
5489 int i, j;
5491 if (!map1 || !map2)
5492 goto error;
5494 dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5495 isl_dim_copy(map2->dim));
5497 result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5498 if (!result)
5499 goto error;
5500 for (i = 0; i < map1->n; ++i)
5501 for (j = 0; j < map2->n; ++j) {
5502 result = isl_map_add_basic_map(result,
5503 isl_basic_map_apply_range(
5504 isl_basic_map_copy(map1->p[i]),
5505 isl_basic_map_copy(map2->p[j])));
5506 if (!result)
5507 goto error;
5509 isl_map_free(map1);
5510 isl_map_free(map2);
5511 if (result && result->n <= 1)
5512 ISL_F_SET(result, ISL_MAP_DISJOINT);
5513 return result;
5514 error:
5515 isl_map_free(map1);
5516 isl_map_free(map2);
5517 return NULL;
5521 * returns range - domain
5523 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5525 isl_dim *dims, *target_dim;
5526 struct isl_basic_set *bset;
5527 unsigned dim;
5528 unsigned nparam;
5529 int i;
5531 if (!bmap)
5532 goto error;
5533 isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5534 bmap->dim, isl_dim_out),
5535 goto error);
5536 target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5537 dim = isl_basic_map_n_in(bmap);
5538 nparam = isl_basic_map_n_param(bmap);
5539 bset = isl_basic_set_from_basic_map(bmap);
5540 bset = isl_basic_set_cow(bset);
5541 dims = isl_basic_set_get_dim(bset);
5542 dims = isl_dim_add(dims, isl_dim_set, dim);
5543 bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5544 bset = isl_basic_set_swap_vars(bset, 2*dim);
5545 for (i = 0; i < dim; ++i) {
5546 int j = isl_basic_map_alloc_equality(
5547 (struct isl_basic_map *)bset);
5548 if (j < 0)
5549 goto error;
5550 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5551 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5552 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5553 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5555 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5556 bset = isl_basic_set_reset_dim(bset, target_dim);
5557 return bset;
5558 error:
5559 isl_basic_map_free(bmap);
5560 return NULL;
5564 * returns range - domain
5566 struct isl_set *isl_map_deltas(struct isl_map *map)
5568 int i;
5569 isl_dim *dim;
5570 struct isl_set *result;
5572 if (!map)
5573 return NULL;
5575 isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5576 map->dim, isl_dim_out),
5577 goto error);
5578 dim = isl_map_get_dim(map);
5579 dim = isl_dim_domain(dim);
5580 result = isl_set_alloc_dim(dim, map->n, map->flags);
5581 if (!result)
5582 goto error;
5583 for (i = 0; i < map->n; ++i)
5584 result = isl_set_add_basic_set(result,
5585 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5586 isl_map_free(map);
5587 return result;
5588 error:
5589 isl_map_free(map);
5590 return NULL;
5593 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
5595 struct isl_basic_map *bmap;
5596 unsigned nparam;
5597 unsigned dim;
5598 int i;
5600 if (!dims)
5601 return NULL;
5603 nparam = dims->nparam;
5604 dim = dims->n_out;
5605 bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
5606 if (!bmap)
5607 goto error;
5609 for (i = 0; i < dim; ++i) {
5610 int j = isl_basic_map_alloc_equality(bmap);
5611 if (j < 0)
5612 goto error;
5613 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5614 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5615 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5617 return isl_basic_map_finalize(bmap);
5618 error:
5619 isl_basic_map_free(bmap);
5620 return NULL;
5623 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5625 struct isl_dim *dim = isl_dim_map(set_dim);
5626 if (!dim)
5627 return NULL;
5628 return basic_map_identity(dim);
5631 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5633 if (!model || !model->dim)
5634 return NULL;
5635 isl_assert(model->ctx,
5636 model->dim->n_in == model->dim->n_out, return NULL);
5637 return basic_map_identity(isl_dim_copy(model->dim));
5640 static struct isl_map *map_identity(struct isl_dim *dim)
5642 struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5643 return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5646 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5648 struct isl_dim *dim = isl_dim_map(set_dim);
5649 if (!dim)
5650 return NULL;
5651 return map_identity(dim);
5654 struct isl_map *isl_map_identity_like(struct isl_map *model)
5656 if (!model || !model->dim)
5657 return NULL;
5658 isl_assert(model->ctx,
5659 model->dim->n_in == model->dim->n_out, return NULL);
5660 return map_identity(isl_dim_copy(model->dim));
5663 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5665 if (!model || !model->dim)
5666 return NULL;
5667 isl_assert(model->ctx,
5668 model->dim->n_in == model->dim->n_out, return NULL);
5669 return map_identity(isl_dim_copy(model->dim));
5672 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
5674 isl_dim *dim = isl_set_get_dim(set);
5675 isl_map *id;
5676 id = isl_map_identity(dim);
5677 return isl_map_intersect_range(id, set);
5680 /* Construct a basic set with all set dimensions having only non-negative
5681 * values.
5683 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5685 int i;
5686 unsigned nparam;
5687 unsigned dim;
5688 struct isl_basic_set *bset;
5690 if (!dims)
5691 return NULL;
5692 nparam = dims->nparam;
5693 dim = dims->n_out;
5694 bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5695 if (!bset)
5696 return NULL;
5697 for (i = 0; i < dim; ++i) {
5698 int k = isl_basic_set_alloc_inequality(bset);
5699 if (k < 0)
5700 goto error;
5701 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5702 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5704 return bset;
5705 error:
5706 isl_basic_set_free(bset);
5707 return NULL;
5710 /* Construct the half-space x_pos >= 0.
5712 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
5713 int pos)
5715 int k;
5716 isl_basic_set *nonneg;
5718 nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5719 k = isl_basic_set_alloc_inequality(nonneg);
5720 if (k < 0)
5721 goto error;
5722 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5723 isl_int_set_si(nonneg->ineq[k][pos], 1);
5725 return isl_basic_set_finalize(nonneg);
5726 error:
5727 isl_basic_set_free(nonneg);
5728 return NULL;
5731 /* Construct the half-space x_pos <= -1.
5733 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
5735 int k;
5736 isl_basic_set *neg;
5738 neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
5739 k = isl_basic_set_alloc_inequality(neg);
5740 if (k < 0)
5741 goto error;
5742 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5743 isl_int_set_si(neg->ineq[k][0], -1);
5744 isl_int_set_si(neg->ineq[k][pos], -1);
5746 return isl_basic_set_finalize(neg);
5747 error:
5748 isl_basic_set_free(neg);
5749 return NULL;
5752 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5753 enum isl_dim_type type, unsigned first, unsigned n)
5755 int i;
5756 isl_basic_set *nonneg;
5757 isl_basic_set *neg;
5759 if (!set)
5760 return NULL;
5761 if (n == 0)
5762 return set;
5764 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5766 for (i = 0; i < n; ++i) {
5767 nonneg = nonneg_halfspace(isl_set_get_dim(set),
5768 pos(set->dim, type) + first + i);
5769 neg = neg_halfspace(isl_set_get_dim(set),
5770 pos(set->dim, type) + first + i);
5772 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5775 return set;
5776 error:
5777 isl_set_free(set);
5778 return NULL;
5781 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
5782 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5783 void *user)
5785 isl_set *half;
5787 if (!set)
5788 return -1;
5789 if (isl_set_fast_is_empty(set)) {
5790 isl_set_free(set);
5791 return 0;
5793 if (first == len)
5794 return fn(set, signs, user);
5796 signs[first] = 1;
5797 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
5798 1 + first));
5799 half = isl_set_intersect(half, isl_set_copy(set));
5800 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
5801 goto error;
5803 signs[first] = -1;
5804 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
5805 1 + first));
5806 half = isl_set_intersect(half, set);
5807 return foreach_orthant(half, signs, first + 1, len, fn, user);
5808 error:
5809 isl_set_free(set);
5810 return -1;
5813 /* Call "fn" on the intersections of "set" with each of the orthants
5814 * (except for obviously empty intersections). The orthant is identified
5815 * by the signs array, with each entry having value 1 or -1 according
5816 * to the sign of the corresponding variable.
5818 int isl_set_foreach_orthant(__isl_keep isl_set *set,
5819 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
5820 void *user)
5822 unsigned nparam;
5823 unsigned nvar;
5824 int *signs;
5825 int r;
5827 if (!set)
5828 return -1;
5829 if (isl_set_fast_is_empty(set))
5830 return 0;
5832 nparam = isl_set_dim(set, isl_dim_param);
5833 nvar = isl_set_dim(set, isl_dim_set);
5835 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
5837 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
5838 fn, user);
5840 free(signs);
5842 return r;
5845 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5847 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5850 int isl_basic_map_is_subset(
5851 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5853 int is_subset;
5854 struct isl_map *map1;
5855 struct isl_map *map2;
5857 if (!bmap1 || !bmap2)
5858 return -1;
5860 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5861 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5863 is_subset = isl_map_is_subset(map1, map2);
5865 isl_map_free(map1);
5866 isl_map_free(map2);
5868 return is_subset;
5871 int isl_basic_map_is_equal(
5872 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5874 int is_subset;
5876 if (!bmap1 || !bmap2)
5877 return -1;
5878 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5879 if (is_subset != 1)
5880 return is_subset;
5881 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5882 return is_subset;
5885 int isl_basic_set_is_equal(
5886 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5888 return isl_basic_map_is_equal(
5889 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5892 int isl_map_is_empty(struct isl_map *map)
5894 int i;
5895 int is_empty;
5897 if (!map)
5898 return -1;
5899 for (i = 0; i < map->n; ++i) {
5900 is_empty = isl_basic_map_is_empty(map->p[i]);
5901 if (is_empty < 0)
5902 return -1;
5903 if (!is_empty)
5904 return 0;
5906 return 1;
5909 int isl_map_fast_is_empty(struct isl_map *map)
5911 return map ? map->n == 0 : -1;
5914 int isl_set_fast_is_empty(struct isl_set *set)
5916 return set ? set->n == 0 : -1;
5919 int isl_set_is_empty(struct isl_set *set)
5921 return isl_map_is_empty((struct isl_map *)set);
5924 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
5926 if (!map1 || !map2)
5927 return -1;
5929 return isl_dim_equal(map1->dim, map2->dim);
5932 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5934 if (!set1 || !set2)
5935 return -1;
5937 return isl_dim_equal(set1->dim, set2->dim);
5940 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5942 int is_subset;
5944 if (!map1 || !map2)
5945 return -1;
5946 is_subset = isl_map_is_subset(map1, map2);
5947 if (is_subset != 1)
5948 return is_subset;
5949 is_subset = isl_map_is_subset(map2, map1);
5950 return is_subset;
5953 int isl_basic_map_is_strict_subset(
5954 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5956 int is_subset;
5958 if (!bmap1 || !bmap2)
5959 return -1;
5960 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5961 if (is_subset != 1)
5962 return is_subset;
5963 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5964 if (is_subset == -1)
5965 return is_subset;
5966 return !is_subset;
5969 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5971 int is_subset;
5973 if (!map1 || !map2)
5974 return -1;
5975 is_subset = isl_map_is_subset(map1, map2);
5976 if (is_subset != 1)
5977 return is_subset;
5978 is_subset = isl_map_is_subset(map2, map1);
5979 if (is_subset == -1)
5980 return is_subset;
5981 return !is_subset;
5984 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5986 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5989 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5991 if (!bmap)
5992 return -1;
5993 return bmap->n_eq == 0 && bmap->n_ineq == 0;
5996 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5998 if (!bset)
5999 return -1;
6000 return bset->n_eq == 0 && bset->n_ineq == 0;
6003 int isl_map_fast_is_universe(__isl_keep isl_map *map)
6005 int i;
6007 if (!map)
6008 return -1;
6010 for (i = 0; i < map->n; ++i) {
6011 int r = isl_basic_map_is_universe(map->p[i]);
6012 if (r < 0 || r)
6013 return r;
6016 return 0;
6019 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6021 return isl_map_fast_is_universe((isl_map *) set);
6024 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6026 struct isl_basic_set *bset = NULL;
6027 struct isl_vec *sample = NULL;
6028 int empty;
6029 unsigned total;
6031 if (!bmap)
6032 return -1;
6034 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6035 return 1;
6037 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6038 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6039 copy = isl_basic_map_remove_redundancies(copy);
6040 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6041 isl_basic_map_free(copy);
6042 return empty;
6045 total = 1 + isl_basic_map_total_dim(bmap);
6046 if (bmap->sample && bmap->sample->size == total) {
6047 int contains = isl_basic_map_contains(bmap, bmap->sample);
6048 if (contains < 0)
6049 return -1;
6050 if (contains)
6051 return 0;
6053 isl_vec_free(bmap->sample);
6054 bmap->sample = NULL;
6055 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6056 if (!bset)
6057 return -1;
6058 sample = isl_basic_set_sample_vec(bset);
6059 if (!sample)
6060 return -1;
6061 empty = sample->size == 0;
6062 isl_vec_free(bmap->sample);
6063 bmap->sample = sample;
6064 if (empty)
6065 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6067 return empty;
6070 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
6072 if (!bmap)
6073 return -1;
6074 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6077 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
6079 if (!bset)
6080 return -1;
6081 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6084 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6086 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6089 struct isl_map *isl_basic_map_union(
6090 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6092 struct isl_map *map;
6093 if (!bmap1 || !bmap2)
6094 return NULL;
6096 isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6098 map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6099 if (!map)
6100 goto error;
6101 map = isl_map_add_basic_map(map, bmap1);
6102 map = isl_map_add_basic_map(map, bmap2);
6103 return map;
6104 error:
6105 isl_basic_map_free(bmap1);
6106 isl_basic_map_free(bmap2);
6107 return NULL;
6110 struct isl_set *isl_basic_set_union(
6111 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6113 return (struct isl_set *)isl_basic_map_union(
6114 (struct isl_basic_map *)bset1,
6115 (struct isl_basic_map *)bset2);
6118 /* Order divs such that any div only depends on previous divs */
6119 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6121 int i;
6122 unsigned off;
6124 if (!bmap)
6125 return NULL;
6127 off = isl_dim_total(bmap->dim);
6129 for (i = 0; i < bmap->n_div; ++i) {
6130 int pos;
6131 if (isl_int_is_zero(bmap->div[i][0]))
6132 continue;
6133 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6134 bmap->n_div-i);
6135 if (pos == -1)
6136 continue;
6137 isl_basic_map_swap_div(bmap, i, i + pos);
6138 --i;
6140 return bmap;
6143 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6145 return (struct isl_basic_set *)
6146 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6149 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6151 int i;
6153 if (!map)
6154 return 0;
6156 for (i = 0; i < map->n; ++i) {
6157 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6158 if (!map->p[i])
6159 goto error;
6162 return map;
6163 error:
6164 isl_map_free(map);
6165 return NULL;
6168 /* Look for a div in dst that corresponds to the div "div" in src.
6169 * The divs before "div" in src and dst are assumed to be the same.
6171 * Returns -1 if no corresponding div was found and the position
6172 * of the corresponding div in dst otherwise.
6174 static int find_div(struct isl_basic_map *dst,
6175 struct isl_basic_map *src, unsigned div)
6177 int i;
6179 unsigned total = isl_dim_total(src->dim);
6181 isl_assert(dst->ctx, div <= dst->n_div, return -1);
6182 for (i = div; i < dst->n_div; ++i)
6183 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6184 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6185 dst->n_div - div) == -1)
6186 return i;
6187 return -1;
6190 struct isl_basic_map *isl_basic_map_align_divs(
6191 struct isl_basic_map *dst, struct isl_basic_map *src)
6193 int i;
6194 unsigned total = isl_dim_total(src->dim);
6196 if (!dst || !src)
6197 goto error;
6199 if (src->n_div == 0)
6200 return dst;
6202 for (i = 0; i < src->n_div; ++i)
6203 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6205 src = isl_basic_map_order_divs(src);
6206 dst = isl_basic_map_cow(dst);
6207 dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6208 src->n_div, 0, 2 * src->n_div);
6209 if (!dst)
6210 return NULL;
6211 for (i = 0; i < src->n_div; ++i) {
6212 int j = find_div(dst, src, i);
6213 if (j < 0) {
6214 j = isl_basic_map_alloc_div(dst);
6215 if (j < 0)
6216 goto error;
6217 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6218 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6219 if (isl_basic_map_add_div_constraints(dst, j) < 0)
6220 goto error;
6222 if (j != i)
6223 isl_basic_map_swap_div(dst, i, j);
6225 return dst;
6226 error:
6227 isl_basic_map_free(dst);
6228 return NULL;
6231 struct isl_basic_set *isl_basic_set_align_divs(
6232 struct isl_basic_set *dst, struct isl_basic_set *src)
6234 return (struct isl_basic_set *)isl_basic_map_align_divs(
6235 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6238 struct isl_map *isl_map_align_divs(struct isl_map *map)
6240 int i;
6242 if (!map)
6243 return NULL;
6244 if (map->n == 0)
6245 return map;
6246 map = isl_map_compute_divs(map);
6247 map = isl_map_cow(map);
6248 if (!map)
6249 return NULL;
6251 for (i = 1; i < map->n; ++i)
6252 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6253 for (i = 1; i < map->n; ++i)
6254 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6256 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6257 return map;
6260 struct isl_set *isl_set_align_divs(struct isl_set *set)
6262 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6265 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
6267 if (!set || !map)
6268 goto error;
6269 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6270 map = isl_map_intersect_domain(map, set);
6271 set = isl_map_range(map);
6272 return set;
6273 error:
6274 isl_set_free(set);
6275 isl_map_free(map);
6276 return NULL;
6279 /* There is no need to cow as removing empty parts doesn't change
6280 * the meaning of the set.
6282 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6284 int i;
6286 if (!map)
6287 return NULL;
6289 for (i = map->n-1; i >= 0; --i) {
6290 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6291 continue;
6292 isl_basic_map_free(map->p[i]);
6293 if (i != map->n-1) {
6294 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6295 map->p[i] = map->p[map->n-1];
6297 map->n--;
6300 return map;
6303 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6305 return (struct isl_set *)
6306 isl_map_remove_empty_parts((struct isl_map *)set);
6309 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6311 struct isl_basic_map *bmap;
6312 if (!map || map->n == 0)
6313 return NULL;
6314 bmap = map->p[map->n-1];
6315 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6316 return isl_basic_map_copy(bmap);
6319 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6321 return (struct isl_basic_set *)
6322 isl_map_copy_basic_map((struct isl_map *)set);
6325 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6326 __isl_keep isl_basic_map *bmap)
6328 int i;
6330 if (!map || !bmap)
6331 goto error;
6332 for (i = map->n-1; i >= 0; --i) {
6333 if (map->p[i] != bmap)
6334 continue;
6335 map = isl_map_cow(map);
6336 if (!map)
6337 goto error;
6338 isl_basic_map_free(map->p[i]);
6339 if (i != map->n-1) {
6340 ISL_F_CLR(map, ISL_SET_NORMALIZED);
6341 map->p[i] = map->p[map->n-1];
6343 map->n--;
6344 return map;
6346 return map;
6347 error:
6348 isl_map_free(map);
6349 return NULL;
6352 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6353 struct isl_basic_set *bset)
6355 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6356 (struct isl_basic_map *)bset);
6359 /* Given two basic sets bset1 and bset2, compute the maximal difference
6360 * between the values of dimension pos in bset1 and those in bset2
6361 * for any common value of the parameters and dimensions preceding pos.
6363 static enum isl_lp_result basic_set_maximal_difference_at(
6364 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6365 int pos, isl_int *opt)
6367 struct isl_dim *dims;
6368 struct isl_basic_map *bmap1 = NULL;
6369 struct isl_basic_map *bmap2 = NULL;
6370 struct isl_ctx *ctx;
6371 struct isl_vec *obj;
6372 unsigned total;
6373 unsigned nparam;
6374 unsigned dim1, dim2;
6375 enum isl_lp_result res;
6377 if (!bset1 || !bset2)
6378 return isl_lp_error;
6380 nparam = isl_basic_set_n_param(bset1);
6381 dim1 = isl_basic_set_n_dim(bset1);
6382 dim2 = isl_basic_set_n_dim(bset2);
6383 dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6384 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6385 dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6386 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6387 if (!bmap1 || !bmap2)
6388 goto error;
6389 bmap1 = isl_basic_map_cow(bmap1);
6390 bmap1 = isl_basic_map_extend(bmap1, nparam,
6391 pos, (dim1 - pos) + (dim2 - pos),
6392 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6393 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6394 if (!bmap1)
6395 goto error;
6396 total = isl_basic_map_total_dim(bmap1);
6397 ctx = bmap1->ctx;
6398 obj = isl_vec_alloc(ctx, 1 + total);
6399 isl_seq_clr(obj->block.data, 1 + total);
6400 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6401 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6402 if (!obj)
6403 goto error;
6404 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6405 opt, NULL, NULL);
6406 isl_basic_map_free(bmap1);
6407 isl_vec_free(obj);
6408 return res;
6409 error:
6410 isl_basic_map_free(bmap1);
6411 isl_basic_map_free(bmap2);
6412 return isl_lp_error;
6415 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6416 * for any common value of the parameters and dimensions preceding pos
6417 * in both basic sets, the values of dimension pos in bset1 are
6418 * smaller or larger than those in bset2.
6420 * Returns
6421 * 1 if bset1 follows bset2
6422 * -1 if bset1 precedes bset2
6423 * 0 if bset1 and bset2 are incomparable
6424 * -2 if some error occurred.
6426 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6427 struct isl_basic_set *bset2, int pos)
6429 isl_int opt;
6430 enum isl_lp_result res;
6431 int cmp;
6433 isl_int_init(opt);
6435 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6437 if (res == isl_lp_empty)
6438 cmp = 0;
6439 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6440 res == isl_lp_unbounded)
6441 cmp = 1;
6442 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6443 cmp = -1;
6444 else
6445 cmp = -2;
6447 isl_int_clear(opt);
6448 return cmp;
6451 /* Given two basic sets bset1 and bset2, check whether
6452 * for any common value of the parameters and dimensions preceding pos
6453 * there is a value of dimension pos in bset1 that is larger
6454 * than a value of the same dimension in bset2.
6456 * Return
6457 * 1 if there exists such a pair
6458 * 0 if there is no such pair, but there is a pair of equal values
6459 * -1 otherwise
6460 * -2 if some error occurred.
6462 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
6463 __isl_keep isl_basic_set *bset2, int pos)
6465 isl_int opt;
6466 enum isl_lp_result res;
6467 int cmp;
6469 isl_int_init(opt);
6471 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6473 if (res == isl_lp_empty)
6474 cmp = -1;
6475 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6476 res == isl_lp_unbounded)
6477 cmp = 1;
6478 else if (res == isl_lp_ok && isl_int_is_neg(opt))
6479 cmp = -1;
6480 else if (res == isl_lp_ok)
6481 cmp = 0;
6482 else
6483 cmp = -2;
6485 isl_int_clear(opt);
6486 return cmp;
6489 /* Given two sets set1 and set2, check whether
6490 * for any common value of the parameters and dimensions preceding pos
6491 * there is a value of dimension pos in set1 that is larger
6492 * than a value of the same dimension in set2.
6494 * Return
6495 * 1 if there exists such a pair
6496 * 0 if there is no such pair, but there is a pair of equal values
6497 * -1 otherwise
6498 * -2 if some error occurred.
6500 int isl_set_follows_at(__isl_keep isl_set *set1,
6501 __isl_keep isl_set *set2, int pos)
6503 int i, j;
6504 int follows = -1;
6506 if (!set1 || !set2)
6507 return -2;
6509 for (i = 0; i < set1->n; ++i)
6510 for (j = 0; j < set2->n; ++j) {
6511 int f;
6512 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
6513 if (f == 1 || f == -2)
6514 return f;
6515 if (f > follows)
6516 follows = f;
6519 return follows;
6522 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
6523 unsigned pos, isl_int *val)
6525 int i;
6526 int d;
6527 unsigned total;
6529 if (!bmap)
6530 return -1;
6531 total = isl_basic_map_total_dim(bmap);
6532 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
6533 for (; d+1 > pos; --d)
6534 if (!isl_int_is_zero(bmap->eq[i][1+d]))
6535 break;
6536 if (d != pos)
6537 continue;
6538 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
6539 return 0;
6540 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
6541 return 0;
6542 if (!isl_int_is_one(bmap->eq[i][1+d]))
6543 return 0;
6544 if (val)
6545 isl_int_neg(*val, bmap->eq[i][0]);
6546 return 1;
6548 return 0;
6551 static int isl_map_fast_has_fixed_var(struct isl_map *map,
6552 unsigned pos, isl_int *val)
6554 int i;
6555 isl_int v;
6556 isl_int tmp;
6557 int fixed;
6559 if (!map)
6560 return -1;
6561 if (map->n == 0)
6562 return 0;
6563 if (map->n == 1)
6564 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
6565 isl_int_init(v);
6566 isl_int_init(tmp);
6567 fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
6568 for (i = 1; fixed == 1 && i < map->n; ++i) {
6569 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
6570 if (fixed == 1 && isl_int_ne(tmp, v))
6571 fixed = 0;
6573 if (val)
6574 isl_int_set(*val, v);
6575 isl_int_clear(tmp);
6576 isl_int_clear(v);
6577 return fixed;
6580 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
6581 unsigned pos, isl_int *val)
6583 return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
6584 pos, val);
6587 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
6588 isl_int *val)
6590 return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
6593 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
6594 enum isl_dim_type type, unsigned pos, isl_int *val)
6596 if (pos >= isl_basic_map_dim(bmap, type))
6597 return -1;
6598 return isl_basic_map_fast_has_fixed_var(bmap,
6599 isl_basic_map_offset(bmap, type) - 1 + pos, val);
6602 int isl_map_fast_is_fixed(struct isl_map *map,
6603 enum isl_dim_type type, unsigned pos, isl_int *val)
6605 if (pos >= isl_map_dim(map, type))
6606 return -1;
6607 return isl_map_fast_has_fixed_var(map,
6608 map_offset(map, type) - 1 + pos, val);
6611 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6612 * then return this fixed value in *val.
6614 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
6615 isl_int *val)
6617 return isl_basic_set_fast_has_fixed_var(bset,
6618 isl_basic_set_n_param(bset) + dim, val);
6621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
6622 * then return this fixed value in *val.
6624 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
6626 return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
6629 /* Check if input variable in has fixed value and if so and if val is not NULL,
6630 * then return this fixed value in *val.
6632 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
6634 return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
6637 /* Check if dimension dim has an (obvious) fixed lower bound and if so
6638 * and if val is not NULL, then return this lower bound in *val.
6640 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
6641 unsigned dim, isl_int *val)
6643 int i, i_eq = -1, i_ineq = -1;
6644 isl_int *c;
6645 unsigned total;
6646 unsigned nparam;
6648 if (!bset)
6649 return -1;
6650 total = isl_basic_set_total_dim(bset);
6651 nparam = isl_basic_set_n_param(bset);
6652 for (i = 0; i < bset->n_eq; ++i) {
6653 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
6654 continue;
6655 if (i_eq != -1)
6656 return 0;
6657 i_eq = i;
6659 for (i = 0; i < bset->n_ineq; ++i) {
6660 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
6661 continue;
6662 if (i_eq != -1 || i_ineq != -1)
6663 return 0;
6664 i_ineq = i;
6666 if (i_eq == -1 && i_ineq == -1)
6667 return 0;
6668 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
6669 /* The coefficient should always be one due to normalization. */
6670 if (!isl_int_is_one(c[1+nparam+dim]))
6671 return 0;
6672 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
6673 return 0;
6674 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
6675 total - nparam - dim - 1) != -1)
6676 return 0;
6677 if (val)
6678 isl_int_neg(*val, c[0]);
6679 return 1;
6682 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
6683 unsigned dim, isl_int *val)
6685 int i;
6686 isl_int v;
6687 isl_int tmp;
6688 int fixed;
6690 if (!set)
6691 return -1;
6692 if (set->n == 0)
6693 return 0;
6694 if (set->n == 1)
6695 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6696 dim, val);
6697 isl_int_init(v);
6698 isl_int_init(tmp);
6699 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
6700 dim, &v);
6701 for (i = 1; fixed == 1 && i < set->n; ++i) {
6702 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
6703 dim, &tmp);
6704 if (fixed == 1 && isl_int_ne(tmp, v))
6705 fixed = 0;
6707 if (val)
6708 isl_int_set(*val, v);
6709 isl_int_clear(tmp);
6710 isl_int_clear(v);
6711 return fixed;
6714 struct constraint {
6715 unsigned size;
6716 isl_int *c;
6719 static int qsort_constraint_cmp(const void *p1, const void *p2)
6721 const struct constraint *c1 = (const struct constraint *)p1;
6722 const struct constraint *c2 = (const struct constraint *)p2;
6723 unsigned size = isl_min(c1->size, c2->size);
6724 return isl_seq_cmp(c1->c, c2->c, size);
6727 static struct isl_basic_map *isl_basic_map_sort_constraints(
6728 struct isl_basic_map *bmap)
6730 int i;
6731 struct constraint *c;
6732 unsigned total;
6734 if (!bmap)
6735 return NULL;
6736 total = isl_basic_map_total_dim(bmap);
6737 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6738 if (!c)
6739 goto error;
6740 for (i = 0; i < bmap->n_ineq; ++i) {
6741 c[i].size = total;
6742 c[i].c = bmap->ineq[i];
6744 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6745 for (i = 0; i < bmap->n_ineq; ++i)
6746 bmap->ineq[i] = c[i].c;
6747 free(c);
6748 return bmap;
6749 error:
6750 isl_basic_map_free(bmap);
6751 return NULL;
6754 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6755 __isl_take isl_basic_set *bset)
6757 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6758 (struct isl_basic_map *)bset);
6761 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6763 if (!bmap)
6764 return NULL;
6765 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6766 return bmap;
6767 bmap = isl_basic_map_remove_redundancies(bmap);
6768 bmap = isl_basic_map_sort_constraints(bmap);
6769 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6770 return bmap;
6773 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6775 return (struct isl_basic_set *)isl_basic_map_normalize(
6776 (struct isl_basic_map *)bset);
6779 int isl_basic_map_fast_cmp(const __isl_keep isl_basic_map *bmap1,
6780 const __isl_keep isl_basic_map *bmap2)
6782 int i, cmp;
6783 unsigned total;
6785 if (bmap1 == bmap2)
6786 return 0;
6787 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6788 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6789 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6790 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6791 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6792 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6793 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6794 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6795 return 0;
6796 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6797 return 1;
6798 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6799 return -1;
6800 if (bmap1->n_eq != bmap2->n_eq)
6801 return bmap1->n_eq - bmap2->n_eq;
6802 if (bmap1->n_ineq != bmap2->n_ineq)
6803 return bmap1->n_ineq - bmap2->n_ineq;
6804 if (bmap1->n_div != bmap2->n_div)
6805 return bmap1->n_div - bmap2->n_div;
6806 total = isl_basic_map_total_dim(bmap1);
6807 for (i = 0; i < bmap1->n_eq; ++i) {
6808 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6809 if (cmp)
6810 return cmp;
6812 for (i = 0; i < bmap1->n_ineq; ++i) {
6813 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6814 if (cmp)
6815 return cmp;
6817 for (i = 0; i < bmap1->n_div; ++i) {
6818 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6819 if (cmp)
6820 return cmp;
6822 return 0;
6825 int isl_basic_map_fast_is_equal(__isl_keep isl_basic_map *bmap1,
6826 __isl_keep isl_basic_map *bmap2)
6828 return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6831 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6832 __isl_keep isl_basic_set *bset2)
6834 return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6835 (isl_basic_map *)bset2);
6838 static int qsort_bmap_cmp(const void *p1, const void *p2)
6840 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6841 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6843 return isl_basic_map_fast_cmp(bmap1, bmap2);
6846 /* We normalize in place, but if anything goes wrong we need
6847 * to return NULL, so we need to make sure we don't change the
6848 * meaning of any possible other copies of map.
6850 struct isl_map *isl_map_normalize(struct isl_map *map)
6852 int i, j;
6853 struct isl_basic_map *bmap;
6855 if (!map)
6856 return NULL;
6857 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6858 return map;
6859 for (i = 0; i < map->n; ++i) {
6860 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6861 if (!bmap)
6862 goto error;
6863 isl_basic_map_free(map->p[i]);
6864 map->p[i] = bmap;
6866 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6867 ISL_F_SET(map, ISL_MAP_NORMALIZED);
6868 map = isl_map_remove_empty_parts(map);
6869 if (!map)
6870 return NULL;
6871 for (i = map->n - 1; i >= 1; --i) {
6872 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6873 continue;
6874 isl_basic_map_free(map->p[i-1]);
6875 for (j = i; j < map->n; ++j)
6876 map->p[j-1] = map->p[j];
6877 map->n--;
6879 return map;
6880 error:
6881 isl_map_free(map);
6882 return NULL;
6886 struct isl_set *isl_set_normalize(struct isl_set *set)
6888 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6891 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6893 int i;
6894 int equal;
6896 if (!map1 || !map2)
6897 return -1;
6899 if (map1 == map2)
6900 return 1;
6901 if (!isl_dim_equal(map1->dim, map2->dim))
6902 return 0;
6904 map1 = isl_map_copy(map1);
6905 map2 = isl_map_copy(map2);
6906 map1 = isl_map_normalize(map1);
6907 map2 = isl_map_normalize(map2);
6908 if (!map1 || !map2)
6909 goto error;
6910 equal = map1->n == map2->n;
6911 for (i = 0; equal && i < map1->n; ++i) {
6912 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6913 if (equal < 0)
6914 goto error;
6916 isl_map_free(map1);
6917 isl_map_free(map2);
6918 return equal;
6919 error:
6920 isl_map_free(map1);
6921 isl_map_free(map2);
6922 return -1;
6925 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6927 return isl_map_fast_is_equal((struct isl_map *)set1,
6928 (struct isl_map *)set2);
6931 /* Return an interval that ranges from min to max (inclusive)
6933 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6934 isl_int min, isl_int max)
6936 int k;
6937 struct isl_basic_set *bset = NULL;
6939 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6940 if (!bset)
6941 goto error;
6943 k = isl_basic_set_alloc_inequality(bset);
6944 if (k < 0)
6945 goto error;
6946 isl_int_set_si(bset->ineq[k][1], 1);
6947 isl_int_neg(bset->ineq[k][0], min);
6949 k = isl_basic_set_alloc_inequality(bset);
6950 if (k < 0)
6951 goto error;
6952 isl_int_set_si(bset->ineq[k][1], -1);
6953 isl_int_set(bset->ineq[k][0], max);
6955 return bset;
6956 error:
6957 isl_basic_set_free(bset);
6958 return NULL;
6961 /* Return the Cartesian product of the basic sets in list (in the given order).
6963 __isl_give isl_basic_set *isl_basic_set_list_product(
6964 __isl_take struct isl_basic_set_list *list)
6966 int i;
6967 unsigned dim;
6968 unsigned nparam;
6969 unsigned extra;
6970 unsigned n_eq;
6971 unsigned n_ineq;
6972 struct isl_basic_set *product = NULL;
6974 if (!list)
6975 goto error;
6976 isl_assert(list->ctx, list->n > 0, goto error);
6977 isl_assert(list->ctx, list->p[0], goto error);
6978 nparam = isl_basic_set_n_param(list->p[0]);
6979 dim = isl_basic_set_n_dim(list->p[0]);
6980 extra = list->p[0]->n_div;
6981 n_eq = list->p[0]->n_eq;
6982 n_ineq = list->p[0]->n_ineq;
6983 for (i = 1; i < list->n; ++i) {
6984 isl_assert(list->ctx, list->p[i], goto error);
6985 isl_assert(list->ctx,
6986 nparam == isl_basic_set_n_param(list->p[i]), goto error);
6987 dim += isl_basic_set_n_dim(list->p[i]);
6988 extra += list->p[i]->n_div;
6989 n_eq += list->p[i]->n_eq;
6990 n_ineq += list->p[i]->n_ineq;
6992 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6993 n_eq, n_ineq);
6994 if (!product)
6995 goto error;
6996 dim = 0;
6997 for (i = 0; i < list->n; ++i) {
6998 isl_basic_set_add_constraints(product,
6999 isl_basic_set_copy(list->p[i]), dim);
7000 dim += isl_basic_set_n_dim(list->p[i]);
7002 isl_basic_set_list_free(list);
7003 return product;
7004 error:
7005 isl_basic_set_free(product);
7006 isl_basic_set_list_free(list);
7007 return NULL;
7010 struct isl_basic_map *isl_basic_map_product(
7011 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7013 struct isl_dim *dim_result = NULL;
7014 struct isl_basic_map *bmap;
7015 unsigned in1, in2, out1, out2, nparam, total, pos;
7016 struct isl_dim_map *dim_map1, *dim_map2;
7018 if (!bmap1 || !bmap2)
7019 goto error;
7021 isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7022 bmap2->dim, isl_dim_param), goto error);
7023 dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7024 isl_dim_copy(bmap2->dim));
7026 in1 = isl_basic_map_n_in(bmap1);
7027 in2 = isl_basic_map_n_in(bmap2);
7028 out1 = isl_basic_map_n_out(bmap1);
7029 out2 = isl_basic_map_n_out(bmap2);
7030 nparam = isl_basic_map_n_param(bmap1);
7032 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7033 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7034 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7035 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7036 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7037 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7038 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7039 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7040 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7041 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7042 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7044 bmap = isl_basic_map_alloc_dim(dim_result,
7045 bmap1->n_div + bmap2->n_div,
7046 bmap1->n_eq + bmap2->n_eq,
7047 bmap1->n_ineq + bmap2->n_ineq);
7048 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7049 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7050 bmap = isl_basic_map_simplify(bmap);
7051 return isl_basic_map_finalize(bmap);
7052 error:
7053 isl_basic_map_free(bmap1);
7054 isl_basic_map_free(bmap2);
7055 return NULL;
7058 __isl_give isl_basic_map *isl_basic_map_range_product(
7059 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7061 isl_dim *dim_result = NULL;
7062 isl_basic_map *bmap;
7063 unsigned in, out1, out2, nparam, total, pos;
7064 struct isl_dim_map *dim_map1, *dim_map2;
7066 if (!bmap1 || !bmap2)
7067 goto error;
7069 dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7070 isl_dim_copy(bmap2->dim));
7072 in = isl_basic_map_dim(bmap1, isl_dim_in);
7073 out1 = isl_basic_map_n_out(bmap1);
7074 out2 = isl_basic_map_n_out(bmap2);
7075 nparam = isl_basic_map_n_param(bmap1);
7077 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7078 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7079 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7080 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7081 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7082 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7083 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7084 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7085 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7086 isl_dim_map_div(dim_map1, bmap1, pos += out2);
7087 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7089 bmap = isl_basic_map_alloc_dim(dim_result,
7090 bmap1->n_div + bmap2->n_div,
7091 bmap1->n_eq + bmap2->n_eq,
7092 bmap1->n_ineq + bmap2->n_ineq);
7093 bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
7094 bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
7095 bmap = isl_basic_map_simplify(bmap);
7096 return isl_basic_map_finalize(bmap);
7097 error:
7098 isl_basic_map_free(bmap1);
7099 isl_basic_map_free(bmap2);
7100 return NULL;
7103 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7104 __isl_take isl_map *map2,
7105 __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7106 __isl_take isl_dim *right),
7107 __isl_give isl_basic_map *(*basic_map_product)(
7108 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7110 unsigned flags = 0;
7111 struct isl_map *result;
7112 int i, j;
7114 if (!map1 || !map2)
7115 goto error;
7117 isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7118 map2->dim, isl_dim_param), goto error);
7120 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7121 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7122 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7124 result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7125 isl_dim_copy(map2->dim)),
7126 map1->n * map2->n, flags);
7127 if (!result)
7128 goto error;
7129 for (i = 0; i < map1->n; ++i)
7130 for (j = 0; j < map2->n; ++j) {
7131 struct isl_basic_map *part;
7132 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7133 isl_basic_map_copy(map2->p[j]));
7134 if (isl_basic_map_is_empty(part))
7135 isl_basic_map_free(part);
7136 else
7137 result = isl_map_add_basic_map(result, part);
7138 if (!result)
7139 goto error;
7141 isl_map_free(map1);
7142 isl_map_free(map2);
7143 return result;
7144 error:
7145 isl_map_free(map1);
7146 isl_map_free(map2);
7147 return NULL;
7150 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7152 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
7154 return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7157 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7159 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7160 __isl_take isl_map *map2)
7162 isl_map *prod;
7164 prod = isl_map_product(map1, map2);
7165 prod = isl_map_flatten(prod);
7166 return prod;
7169 /* Given two set A and B, construct its Cartesian product A x B.
7171 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7173 return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7174 (struct isl_map *)set2);
7177 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7178 __isl_take isl_set *set2)
7180 return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7183 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7185 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7186 __isl_take isl_map *map2)
7188 return map_product(map1, map2, &isl_dim_range_product,
7189 &isl_basic_map_range_product);
7192 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7194 int i;
7195 uint32_t hash = isl_hash_init();
7196 unsigned total;
7198 if (!bmap)
7199 return 0;
7200 bmap = isl_basic_map_copy(bmap);
7201 bmap = isl_basic_map_normalize(bmap);
7202 if (!bmap)
7203 return 0;
7204 total = isl_basic_map_total_dim(bmap);
7205 isl_hash_byte(hash, bmap->n_eq & 0xFF);
7206 for (i = 0; i < bmap->n_eq; ++i) {
7207 uint32_t c_hash;
7208 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7209 isl_hash_hash(hash, c_hash);
7211 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7212 for (i = 0; i < bmap->n_ineq; ++i) {
7213 uint32_t c_hash;
7214 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7215 isl_hash_hash(hash, c_hash);
7217 isl_hash_byte(hash, bmap->n_div & 0xFF);
7218 for (i = 0; i < bmap->n_div; ++i) {
7219 uint32_t c_hash;
7220 if (isl_int_is_zero(bmap->div[i][0]))
7221 continue;
7222 isl_hash_byte(hash, i & 0xFF);
7223 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7224 isl_hash_hash(hash, c_hash);
7226 isl_basic_map_free(bmap);
7227 return hash;
7230 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7232 return isl_basic_map_get_hash((isl_basic_map *)bset);
7235 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7237 int i;
7238 uint32_t hash;
7240 if (!map)
7241 return 0;
7242 map = isl_map_copy(map);
7243 map = isl_map_normalize(map);
7244 if (!map)
7245 return 0;
7247 hash = isl_hash_init();
7248 for (i = 0; i < map->n; ++i) {
7249 uint32_t bmap_hash;
7250 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7251 isl_hash_hash(hash, bmap_hash);
7254 isl_map_free(map);
7256 return hash;
7259 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7261 return isl_map_get_hash((isl_map *)set);
7264 /* Check if the value for dimension dim is completely determined
7265 * by the values of the other parameters and variables.
7266 * That is, check if dimension dim is involved in an equality.
7268 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7270 int i;
7271 unsigned nparam;
7273 if (!bset)
7274 return -1;
7275 nparam = isl_basic_set_n_param(bset);
7276 for (i = 0; i < bset->n_eq; ++i)
7277 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7278 return 1;
7279 return 0;
7282 /* Check if the value for dimension dim is completely determined
7283 * by the values of the other parameters and variables.
7284 * That is, check if dimension dim is involved in an equality
7285 * for each of the subsets.
7287 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7289 int i;
7291 if (!set)
7292 return -1;
7293 for (i = 0; i < set->n; ++i) {
7294 int unique;
7295 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7296 if (unique != 1)
7297 return unique;
7299 return 1;
7302 int isl_set_n_basic_set(__isl_keep isl_set *set)
7304 return set ? set->n : 0;
7307 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7308 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7310 int i;
7312 if (!map)
7313 return -1;
7315 for (i = 0; i < map->n; ++i)
7316 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7317 return -1;
7319 return 0;
7322 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7323 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7325 int i;
7327 if (!set)
7328 return -1;
7330 for (i = 0; i < set->n; ++i)
7331 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7332 return -1;
7334 return 0;
7337 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7339 struct isl_dim *dim;
7341 if (!bset)
7342 return NULL;
7344 if (bset->n_div == 0)
7345 return bset;
7347 bset = isl_basic_set_cow(bset);
7348 if (!bset)
7349 return NULL;
7351 dim = isl_basic_set_get_dim(bset);
7352 dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
7353 if (!dim)
7354 goto error;
7355 isl_dim_free(bset->dim);
7356 bset->dim = dim;
7357 bset->extra -= bset->n_div;
7358 bset->n_div = 0;
7360 bset = isl_basic_set_finalize(bset);
7362 return bset;
7363 error:
7364 isl_basic_set_free(bset);
7365 return NULL;
7368 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7370 int i;
7371 struct isl_dim *dim;
7372 unsigned n_div;
7374 set = isl_set_align_divs(set);
7376 if (!set)
7377 return NULL;
7378 if (set->n == 0 || set->p[0]->n_div == 0)
7379 return set;
7381 set = isl_set_cow(set);
7382 if (!set)
7383 return NULL;
7385 n_div = set->p[0]->n_div;
7386 dim = isl_set_get_dim(set);
7387 dim = isl_dim_add(dim, isl_dim_set, n_div);
7388 if (!dim)
7389 goto error;
7390 isl_dim_free(set->dim);
7391 set->dim = dim;
7393 for (i = 0; i < set->n; ++i) {
7394 set->p[i] = isl_basic_set_lift(set->p[i]);
7395 if (!set->p[i])
7396 goto error;
7399 return set;
7400 error:
7401 isl_set_free(set);
7402 return NULL;
7405 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
7407 struct isl_dim *dim;
7408 struct isl_basic_map *bmap;
7409 unsigned n_set;
7410 unsigned n_div;
7411 unsigned n_param;
7412 unsigned total;
7413 int i, k, l;
7415 set = isl_set_align_divs(set);
7417 if (!set)
7418 return NULL;
7420 dim = isl_set_get_dim(set);
7421 if (set->n == 0 || set->p[0]->n_div == 0) {
7422 isl_set_free(set);
7423 return isl_map_identity(dim);
7426 n_div = set->p[0]->n_div;
7427 dim = isl_dim_map(dim);
7428 n_param = isl_dim_size(dim, isl_dim_param);
7429 n_set = isl_dim_size(dim, isl_dim_in);
7430 dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
7431 bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
7432 for (i = 0; i < n_set; ++i)
7433 bmap = var_equal(bmap, i);
7435 total = n_param + n_set + n_set + n_div;
7436 for (i = 0; i < n_div; ++i) {
7437 k = isl_basic_map_alloc_inequality(bmap);
7438 if (k < 0)
7439 goto error;
7440 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
7441 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
7442 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
7443 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
7444 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
7445 set->p[0]->div[i][0]);
7447 l = isl_basic_map_alloc_inequality(bmap);
7448 if (l < 0)
7449 goto error;
7450 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
7451 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
7452 set->p[0]->div[i][0]);
7453 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
7456 isl_set_free(set);
7457 return isl_map_from_basic_map(bmap);
7458 error:
7459 isl_set_free(set);
7460 isl_basic_map_free(bmap);
7461 return NULL;
7464 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
7466 unsigned dim;
7467 int size = 0;
7469 if (!bset)
7470 return -1;
7472 dim = isl_basic_set_total_dim(bset);
7473 size += bset->n_eq * (1 + dim);
7474 size += bset->n_ineq * (1 + dim);
7475 size += bset->n_div * (2 + dim);
7477 return size;
7480 int isl_set_size(__isl_keep isl_set *set)
7482 int i;
7483 int size = 0;
7485 if (!set)
7486 return -1;
7488 for (i = 0; i < set->n; ++i)
7489 size += isl_basic_set_size(set->p[i]);
7491 return size;
7494 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
7495 enum isl_dim_type type, unsigned pos)
7497 int i;
7498 int lower, upper;
7500 if (!bmap)
7501 return -1;
7503 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
7505 pos += isl_basic_map_offset(bmap, type);
7507 for (i = 0; i < bmap->n_eq; ++i)
7508 if (!isl_int_is_zero(bmap->eq[i][pos]))
7509 return 1;
7511 lower = upper = 0;
7512 for (i = 0; i < bmap->n_ineq; ++i) {
7513 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
7514 if (sgn > 0)
7515 lower = 1;
7516 if (sgn < 0)
7517 upper = 1;
7520 return lower && upper;
7523 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
7524 enum isl_dim_type type, unsigned pos)
7526 int i;
7528 if (!map)
7529 return -1;
7531 for (i = 0; i < map->n; ++i) {
7532 int bounded;
7533 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
7534 if (bounded < 0 || !bounded)
7535 return bounded;
7538 return 1;
7541 /* Return 1 if the specified dim is involved in both an upper bound
7542 * and a lower bound.
7544 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
7545 enum isl_dim_type type, unsigned pos)
7547 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
7550 /* For each of the "n" variables starting at "first", determine
7551 * the sign of the variable and put the results in the first "n"
7552 * elements of the array "signs".
7553 * Sign
7554 * 1 means that the variable is non-negative
7555 * -1 means that the variable is non-positive
7556 * 0 means the variable attains both positive and negative values.
7558 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
7559 unsigned first, unsigned n, int *signs)
7561 isl_vec *bound = NULL;
7562 struct isl_tab *tab = NULL;
7563 struct isl_tab_undo *snap;
7564 int i;
7566 if (!bset || !signs)
7567 return -1;
7569 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
7570 tab = isl_tab_from_basic_set(bset);
7571 if (!bound || !tab)
7572 goto error;
7574 isl_seq_clr(bound->el, bound->size);
7575 isl_int_set_si(bound->el[0], -1);
7577 snap = isl_tab_snap(tab);
7578 for (i = 0; i < n; ++i) {
7579 int empty;
7581 isl_int_set_si(bound->el[1 + first + i], -1);
7582 if (isl_tab_add_ineq(tab, bound->el) < 0)
7583 goto error;
7584 empty = tab->empty;
7585 isl_int_set_si(bound->el[1 + first + i], 0);
7586 if (isl_tab_rollback(tab, snap) < 0)
7587 goto error;
7589 if (empty) {
7590 signs[i] = 1;
7591 continue;
7594 isl_int_set_si(bound->el[1 + first + i], 1);
7595 if (isl_tab_add_ineq(tab, bound->el) < 0)
7596 goto error;
7597 empty = tab->empty;
7598 isl_int_set_si(bound->el[1 + first + i], 0);
7599 if (isl_tab_rollback(tab, snap) < 0)
7600 goto error;
7602 signs[i] = empty ? -1 : 0;
7605 isl_tab_free(tab);
7606 isl_vec_free(bound);
7607 return 0;
7608 error:
7609 isl_tab_free(tab);
7610 isl_vec_free(bound);
7611 return -1;
7614 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
7615 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
7617 if (!bset || !signs)
7618 return -1;
7619 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
7620 return -1);
7622 first += pos(bset->dim, type) - 1;
7623 return isl_basic_set_vars_get_sign(bset, first, n, signs);
7626 /* Check if the given basic map is obviously single-valued.
7627 * In particular, for each output dimension, check that there is
7628 * an equality that defines the output dimension in terms of
7629 * earlier dimensions.
7631 int isl_basic_map_fast_is_single_valued(__isl_keep isl_basic_map *bmap)
7633 int i, j;
7634 unsigned total;
7635 unsigned n_out;
7636 unsigned o_out;
7638 if (!bmap)
7639 return -1;
7641 total = 1 + isl_basic_map_total_dim(bmap);
7642 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7643 o_out = isl_basic_map_offset(bmap, isl_dim_out);
7645 for (i = 0; i < n_out; ++i) {
7646 for (j = 0; j < bmap->n_eq; ++j) {
7647 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
7648 continue;
7649 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
7650 total - (o_out + i + 1)) == -1)
7651 break;
7653 if (j >= bmap->n_eq)
7654 return 0;
7657 return 1;
7660 /* Check if the given map is obviously single-valued.
7662 int isl_map_fast_is_single_valued(__isl_keep isl_map *map)
7664 int sv;
7666 if (!map)
7667 return -1;
7668 if (map->n == 0)
7669 return 1;
7670 if (map->n >= 2)
7671 return 0;
7673 return isl_basic_map_fast_is_single_valued(map->p[0]);
7676 /* Check if the given map is single-valued.
7677 * We simply compute
7679 * M \circ M^-1
7681 * and check if the result is a subset of the identity mapping.
7683 int isl_map_is_single_valued(__isl_keep isl_map *map)
7685 isl_map *test;
7686 isl_map *id;
7687 int sv;
7689 sv = isl_map_fast_is_single_valued(map);
7690 if (sv < 0 || sv)
7691 return sv;
7693 test = isl_map_reverse(isl_map_copy(map));
7694 test = isl_map_apply_range(test, isl_map_copy(map));
7696 id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
7698 sv = isl_map_is_subset(test, id);
7700 isl_map_free(test);
7701 isl_map_free(id);
7703 return sv;
7706 int isl_map_is_bijective(__isl_keep isl_map *map)
7708 int sv;
7710 sv = isl_map_is_single_valued(map);
7711 if (sv < 0 || !sv)
7712 return sv;
7714 map = isl_map_copy(map);
7715 map = isl_map_reverse(map);
7716 sv = isl_map_is_single_valued(map);
7717 isl_map_free(map);
7719 return sv;
7722 int isl_set_is_singleton(__isl_keep isl_set *set)
7724 return isl_map_is_single_valued((isl_map *)set);
7727 int isl_map_is_translation(__isl_keep isl_map *map)
7729 int ok;
7730 isl_set *delta;
7732 delta = isl_map_deltas(isl_map_copy(map));
7733 ok = isl_set_is_singleton(delta);
7734 isl_set_free(delta);
7736 return ok;
7739 static int unique(isl_int *p, unsigned pos, unsigned len)
7741 if (isl_seq_first_non_zero(p, pos) != -1)
7742 return 0;
7743 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
7744 return 0;
7745 return 1;
7748 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
7750 int i, j;
7751 unsigned nvar;
7752 unsigned ovar;
7754 if (!bset)
7755 return -1;
7757 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
7758 return 0;
7760 nvar = isl_basic_set_dim(bset, isl_dim_set);
7761 ovar = isl_dim_offset(bset->dim, isl_dim_set);
7762 for (j = 0; j < nvar; ++j) {
7763 int lower = 0, upper = 0;
7764 for (i = 0; i < bset->n_eq; ++i) {
7765 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
7766 continue;
7767 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
7768 return 0;
7769 break;
7771 if (i < bset->n_eq)
7772 continue;
7773 for (i = 0; i < bset->n_ineq; ++i) {
7774 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
7775 continue;
7776 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
7777 return 0;
7778 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
7779 lower = 1;
7780 else
7781 upper = 1;
7783 if (!lower || !upper)
7784 return 0;
7787 return 1;
7790 int isl_set_is_box(__isl_keep isl_set *set)
7792 if (!set)
7793 return -1;
7794 if (set->n != 1)
7795 return 0;
7797 return isl_basic_set_is_box(set->p[0]);
7800 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
7802 if (!bset)
7803 return -1;
7805 return isl_dim_is_wrapping(bset->dim);
7808 int isl_set_is_wrapping(__isl_keep isl_set *set)
7810 if (!set)
7811 return -1;
7813 return isl_dim_is_wrapping(set->dim);
7816 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
7818 bmap = isl_basic_map_cow(bmap);
7819 if (!bmap)
7820 return NULL;
7822 bmap->dim = isl_dim_wrap(bmap->dim);
7823 if (!bmap->dim)
7824 goto error;
7826 bmap = isl_basic_map_finalize(bmap);
7828 return (isl_basic_set *)bmap;
7829 error:
7830 isl_basic_map_free(bmap);
7831 return NULL;
7834 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
7836 int i;
7838 map = isl_map_cow(map);
7839 if (!map)
7840 return NULL;
7842 for (i = 0; i < map->n; ++i) {
7843 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
7844 if (!map->p[i])
7845 goto error;
7847 map->dim = isl_dim_wrap(map->dim);
7848 if (!map->dim)
7849 goto error;
7851 return (isl_set *)map;
7852 error:
7853 isl_map_free(map);
7854 return NULL;
7857 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
7859 bset = isl_basic_set_cow(bset);
7860 if (!bset)
7861 return NULL;
7863 bset->dim = isl_dim_unwrap(bset->dim);
7864 if (!bset->dim)
7865 goto error;
7867 bset = isl_basic_set_finalize(bset);
7869 return (isl_basic_map *)bset;
7870 error:
7871 isl_basic_set_free(bset);
7872 return NULL;
7875 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
7877 int i;
7879 if (!set)
7880 return NULL;
7882 if (!isl_set_is_wrapping(set))
7883 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
7884 goto error);
7886 set = isl_set_cow(set);
7887 if (!set)
7888 return NULL;
7890 for (i = 0; i < set->n; ++i) {
7891 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
7892 if (!set->p[i])
7893 goto error;
7896 set->dim = isl_dim_unwrap(set->dim);
7897 if (!set->dim)
7898 goto error;
7900 return (isl_map *)set;
7901 error:
7902 isl_set_free(set);
7903 return NULL;
7906 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
7907 enum isl_dim_type type)
7909 if (!bmap)
7910 return NULL;
7912 if (!isl_dim_is_named_or_nested(bmap->dim, type))
7913 return bmap;
7915 bmap = isl_basic_map_cow(bmap);
7916 if (!bmap)
7917 return NULL;
7919 bmap->dim = isl_dim_reset(bmap->dim, type);
7920 if (!bmap->dim)
7921 goto error;
7923 bmap = isl_basic_map_finalize(bmap);
7925 return bmap;
7926 error:
7927 isl_basic_map_free(bmap);
7928 return NULL;
7931 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
7932 enum isl_dim_type type)
7934 int i;
7936 if (!map)
7937 return NULL;
7939 if (!isl_dim_is_named_or_nested(map->dim, type))
7940 return map;
7942 map = isl_map_cow(map);
7943 if (!map)
7944 return NULL;
7946 for (i = 0; i < map->n; ++i) {
7947 map->p[i] = isl_basic_map_reset(map->p[i], type);
7948 if (!map->p[i])
7949 goto error;
7951 map->dim = isl_dim_reset(map->dim, type);
7952 if (!map->dim)
7953 goto error;
7955 return map;
7956 error:
7957 isl_map_free(map);
7958 return NULL;
7961 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
7963 if (!bmap)
7964 return NULL;
7966 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
7967 return bmap;
7969 bmap = isl_basic_map_cow(bmap);
7970 if (!bmap)
7971 return NULL;
7973 bmap->dim = isl_dim_flatten(bmap->dim);
7974 if (!bmap->dim)
7975 goto error;
7977 bmap = isl_basic_map_finalize(bmap);
7979 return bmap;
7980 error:
7981 isl_basic_map_free(bmap);
7982 return NULL;
7985 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
7987 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
7990 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
7992 int i;
7994 if (!map)
7995 return NULL;
7997 if (!map->dim->nested[0] && !map->dim->nested[1])
7998 return map;
8000 map = isl_map_cow(map);
8001 if (!map)
8002 return NULL;
8004 for (i = 0; i < map->n; ++i) {
8005 map->p[i] = isl_basic_map_flatten(map->p[i]);
8006 if (!map->p[i])
8007 goto error;
8009 map->dim = isl_dim_flatten(map->dim);
8010 if (!map->dim)
8011 goto error;
8013 return map;
8014 error:
8015 isl_map_free(map);
8016 return NULL;
8019 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8021 return (isl_set *)isl_map_flatten((isl_map *)set);
8024 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8026 isl_dim *dim, *flat_dim;
8027 isl_map *map;
8029 dim = isl_set_get_dim(set);
8030 flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8031 map = map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8032 map = isl_map_intersect_domain(map, set);
8034 return map;
8037 /* Extend the given dim_map with mappings for the divs in bmap.
8039 static __isl_give struct isl_dim_map *extend_dim_map(
8040 __isl_keep struct isl_dim_map *dim_map,
8041 __isl_keep isl_basic_map *bmap)
8043 int i;
8044 struct isl_dim_map *res;
8045 int offset;
8047 offset = isl_basic_map_offset(bmap, isl_dim_div);
8049 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
8050 if (!res)
8051 return NULL;
8053 for (i = 0; i < dim_map->len; ++i)
8054 res->pos[i] = dim_map->pos[i];
8055 for (i = 0; i < bmap->n_div; ++i)
8056 res->pos[dim_map->len + i] = offset + i;
8058 return res;
8061 /* Extract a dim_map from a reordering.
8062 * We essentially need to reverse the mapping, and add an offset
8063 * of 1 for the constant term.
8065 __isl_give struct isl_dim_map *isl_dim_map_from_reordering(
8066 __isl_keep isl_reordering *exp)
8068 int i;
8069 struct isl_dim_map *dim_map;
8071 if (!exp)
8072 return NULL;
8074 dim_map = isl_dim_map_alloc(exp->dim->ctx, isl_dim_total(exp->dim));
8075 if (!dim_map)
8076 return NULL;
8078 for (i = 0; i < exp->len; ++i)
8079 dim_map->pos[1 + exp->pos[i]] = 1 + i;
8081 return dim_map;
8084 /* Reorder the dimensions of "bmap" according to the given dim_map
8085 * and set the dimension specification to "dim".
8087 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8088 __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8090 isl_basic_map *res;
8092 bmap = isl_basic_map_cow(bmap);
8093 if (!bmap || !dim || !dim_map)
8094 goto error;
8096 res = isl_basic_map_alloc_dim(dim,
8097 bmap->n_div, bmap->n_eq, bmap->n_ineq);
8098 res = add_constraints_dim_map(res, bmap, dim_map);
8099 res = isl_basic_map_finalize(res);
8100 return res;
8101 error:
8102 free(dim_map);
8103 isl_basic_map_free(bmap);
8104 isl_dim_free(dim);
8105 return NULL;
8108 /* Reorder the dimensions of "map" according to given reordering.
8110 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8111 __isl_take isl_reordering *r)
8113 int i;
8114 struct isl_dim_map *dim_map;
8116 map = isl_map_cow(map);
8117 dim_map = isl_dim_map_from_reordering(r);
8118 if (!map || !r || !dim_map)
8119 goto error;
8121 for (i = 0; i < map->n; ++i) {
8122 struct isl_dim_map *dim_map_i;
8124 dim_map_i = extend_dim_map(dim_map, map->p[i]);
8126 map->p[i] = isl_basic_map_realign(map->p[i],
8127 isl_dim_copy(r->dim), dim_map_i);
8129 if (!map->p[i])
8130 goto error;
8133 map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8135 isl_reordering_free(r);
8136 free(dim_map);
8137 return map;
8138 error:
8139 free(dim_map);
8140 isl_map_free(map);
8141 isl_reordering_free(r);
8142 return NULL;
8145 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8146 __isl_take isl_reordering *r)
8148 return (isl_set *)isl_map_realign((isl_map *)set, r);
8151 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8152 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8153 enum isl_dim_type c2, enum isl_dim_type c3,
8154 enum isl_dim_type c4, enum isl_dim_type c5)
8156 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8157 struct isl_mat *mat;
8158 int i, j, k;
8159 int pos;
8161 if (!bmap)
8162 return NULL;
8163 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8164 isl_basic_map_total_dim(bmap) + 1);
8165 if (!mat)
8166 return NULL;
8167 for (i = 0; i < bmap->n_eq; ++i)
8168 for (j = 0, pos = 0; j < 5; ++j) {
8169 int off = isl_basic_map_offset(bmap, c[j]);
8170 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8171 isl_int_set(mat->row[i][pos],
8172 bmap->eq[i][off + k]);
8173 ++pos;
8177 return mat;
8180 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8181 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8182 enum isl_dim_type c2, enum isl_dim_type c3,
8183 enum isl_dim_type c4, enum isl_dim_type c5)
8185 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8186 struct isl_mat *mat;
8187 int i, j, k;
8188 int pos;
8190 if (!bmap)
8191 return NULL;
8192 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8193 isl_basic_map_total_dim(bmap) + 1);
8194 if (!mat)
8195 return NULL;
8196 for (i = 0; i < bmap->n_ineq; ++i)
8197 for (j = 0, pos = 0; j < 5; ++j) {
8198 int off = isl_basic_map_offset(bmap, c[j]);
8199 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8200 isl_int_set(mat->row[i][pos],
8201 bmap->ineq[i][off + k]);
8202 ++pos;
8206 return mat;
8209 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8210 __isl_take isl_dim *dim,
8211 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8212 enum isl_dim_type c2, enum isl_dim_type c3,
8213 enum isl_dim_type c4, enum isl_dim_type c5)
8215 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8216 isl_basic_map *bmap;
8217 unsigned total;
8218 unsigned extra;
8219 int i, j, k, l;
8220 int pos;
8222 if (!dim || !eq || !ineq)
8223 goto error;
8225 if (eq->n_col != ineq->n_col)
8226 isl_die(dim->ctx, isl_error_invalid,
8227 "equalities and inequalities matrices should have "
8228 "same number of columns", goto error);
8230 total = 1 + isl_dim_total(dim);
8232 if (eq->n_col < total)
8233 isl_die(dim->ctx, isl_error_invalid,
8234 "number of columns too small", goto error);
8236 extra = eq->n_col - total;
8238 bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8239 eq->n_row, ineq->n_row);
8240 if (!bmap)
8241 goto error;
8242 for (i = 0; i < extra; ++i) {
8243 k = isl_basic_map_alloc_div(bmap);
8244 if (k < 0)
8245 goto error;
8246 isl_int_set_si(bmap->div[k][0], 0);
8248 for (i = 0; i < eq->n_row; ++i) {
8249 l = isl_basic_map_alloc_equality(bmap);
8250 if (l < 0)
8251 goto error;
8252 for (j = 0, pos = 0; j < 5; ++j) {
8253 int off = isl_basic_map_offset(bmap, c[j]);
8254 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8255 isl_int_set(bmap->eq[l][off + k],
8256 eq->row[i][pos]);
8257 ++pos;
8261 for (i = 0; i < ineq->n_row; ++i) {
8262 l = isl_basic_map_alloc_inequality(bmap);
8263 if (l < 0)
8264 goto error;
8265 for (j = 0, pos = 0; j < 5; ++j) {
8266 int off = isl_basic_map_offset(bmap, c[j]);
8267 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8268 isl_int_set(bmap->ineq[l][off + k],
8269 ineq->row[i][pos]);
8270 ++pos;
8275 isl_dim_free(dim);
8276 isl_mat_free(eq);
8277 isl_mat_free(ineq);
8279 return bmap;
8280 error:
8281 isl_dim_free(dim);
8282 isl_mat_free(eq);
8283 isl_mat_free(ineq);
8284 return NULL;
8287 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8288 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8289 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8291 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8292 c1, c2, c3, c4, isl_dim_in);
8295 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8296 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8297 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8299 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8300 c1, c2, c3, c4, isl_dim_in);
8303 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8304 __isl_take isl_dim *dim,
8305 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8306 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8308 return (isl_basic_set*)
8309 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
8310 c1, c2, c3, c4, isl_dim_in);