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 <isl_map_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_dim_map.h>
16 #include <isl_reordering.h>
18 struct isl_dim_map_entry {
23 /* Maps dst positions to src positions */
26 struct isl_dim_map_entry m[1];
29 __isl_give isl_dim_map *isl_dim_map_alloc(isl_ctx *ctx, unsigned len)
32 struct isl_dim_map *dim_map;
33 dim_map = isl_alloc(ctx, struct isl_dim_map,
34 sizeof(struct isl_dim_map) + len * sizeof(struct isl_dim_map_entry));
37 dim_map->len = 1 + len;
38 dim_map->m[0].pos = 0;
39 dim_map->m[0].sgn = 1;
40 for (i = 0; i < len; ++i)
41 dim_map->m[1 + i].sgn = 0;
45 void isl_dim_map_dim_range(__isl_keep isl_dim_map *dim_map,
46 struct isl_dim *dim, enum isl_dim_type type,
47 unsigned first, unsigned n, unsigned dst_pos)
55 src_pos = 1 + isl_dim_offset(dim, type);
56 for (i = 0; i < n; ++i) {
57 dim_map->m[1 + dst_pos + i].pos = src_pos + first + i;
58 dim_map->m[1 + dst_pos + i].sgn = 1;
62 void isl_dim_map_dim(__isl_keep isl_dim_map *dim_map, __isl_keep isl_dim *dim,
63 enum isl_dim_type type, unsigned dst_pos)
65 isl_dim_map_dim_range(dim_map, dim, type,
66 0, isl_dim_size(dim, type), dst_pos);
69 void isl_dim_map_div(__isl_keep isl_dim_map *dim_map,
70 __isl_keep isl_basic_map *bmap, unsigned dst_pos)
75 if (!dim_map || !bmap)
78 src_pos = 1 + isl_dim_total(bmap->dim);
79 for (i = 0; i < bmap->n_div; ++i) {
80 dim_map->m[1 + dst_pos + i].pos = src_pos + i;
81 dim_map->m[1 + dst_pos + i].sgn = 1;
85 void isl_dim_map_dump(struct isl_dim_map *dim_map)
89 for (i = 0; i < dim_map->len; ++i)
90 fprintf(stderr, "%d -> %d * %d; ", i,
91 dim_map->m[i].sgn, dim_map->m[i].pos);
92 fprintf(stderr, "\n");
95 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
96 struct isl_dim_map *dim_map)
100 for (i = 0; i < dim_map->len; ++i) {
101 if (dim_map->m[i].sgn == 0)
102 isl_int_set_si(dst[i], 0);
103 else if (dim_map->m[i].sgn > 0)
104 isl_int_set(dst[i], src[dim_map->m[i].pos]);
106 isl_int_neg(dst[i], src[dim_map->m[i].pos]);
110 static void copy_div_dim_map(isl_int *dst, isl_int *src,
111 struct isl_dim_map *dim_map)
113 isl_int_set(dst[0], src[0]);
114 copy_constraint_dim_map(dst+1, src+1, dim_map);
117 __isl_give isl_basic_map *isl_basic_map_add_constraints_dim_map(
118 __isl_take isl_basic_map *dst, __isl_take isl_basic_map *src,
119 __isl_take isl_dim_map *dim_map)
123 if (!src || !dst || !dim_map)
126 for (i = 0; i < src->n_eq; ++i) {
127 int i1 = isl_basic_map_alloc_equality(dst);
130 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
133 for (i = 0; i < src->n_ineq; ++i) {
134 int i1 = isl_basic_map_alloc_inequality(dst);
137 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
140 for (i = 0; i < src->n_div; ++i) {
141 int i1 = isl_basic_map_alloc_div(dst);
144 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
148 isl_basic_map_free(src);
153 isl_basic_map_free(src);
154 isl_basic_map_free(dst);
158 /* Extend the given dim_map with mappings for the divs in bmap.
160 __isl_give isl_dim_map *isl_dim_map_extend(__isl_keep isl_dim_map *dim_map,
161 __isl_keep isl_basic_map *bmap)
164 struct isl_dim_map *res;
167 offset = isl_basic_map_offset(bmap, isl_dim_div);
169 res = isl_dim_map_alloc(bmap->ctx, dim_map->len - 1 + bmap->n_div);
173 for (i = 0; i < dim_map->len; ++i)
174 res->m[i] = dim_map->m[i];
175 for (i = 0; i < bmap->n_div; ++i) {
176 res->m[dim_map->len + i].pos = offset + i;
177 res->m[dim_map->len + i].sgn = 1;
183 /* Extract a dim_map from a reordering.
184 * We essentially need to reverse the mapping, and add an offset
185 * of 1 for the constant term.
187 __isl_give isl_dim_map *isl_dim_map_from_reordering(
188 __isl_keep isl_reordering *exp)
192 struct isl_dim_map *dim_map;
197 ctx = isl_dim_get_ctx(exp->dim);
198 dim_map = isl_dim_map_alloc(ctx, isl_dim_total(exp->dim));
202 for (i = 0; i < exp->len; ++i) {
203 dim_map->m[1 + exp->pos[i]].pos = 1 + i;
204 dim_map->m[1 + exp->pos[i]].sgn = 1;