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 /* Maps dst positions to src positions */
24 __isl_give isl_dim_map
*isl_dim_map_alloc(isl_ctx
*ctx
, unsigned len
)
27 struct isl_dim_map
*dim_map
;
28 dim_map
= isl_alloc(ctx
, struct isl_dim_map
,
29 sizeof(struct isl_dim_map
) + len
* sizeof(int));
32 dim_map
->len
= 1 + len
;
34 for (i
= 0; i
< len
; ++i
)
35 dim_map
->pos
[1 + i
] = -1;
39 void isl_dim_map_dim_range(__isl_keep isl_dim_map
*dim_map
,
40 struct isl_dim
*dim
, enum isl_dim_type type
,
41 unsigned first
, unsigned n
, unsigned dst_pos
)
49 src_pos
= 1 + isl_dim_offset(dim
, type
);
50 for (i
= 0; i
< n
; ++i
)
51 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ first
+ i
;
54 void isl_dim_map_dim(__isl_keep isl_dim_map
*dim_map
, __isl_keep isl_dim
*dim
,
55 enum isl_dim_type type
, unsigned dst_pos
)
57 isl_dim_map_dim_range(dim_map
, dim
, type
,
58 0, isl_dim_size(dim
, type
), dst_pos
);
61 void isl_dim_map_div(__isl_keep isl_dim_map
*dim_map
,
62 __isl_keep isl_basic_map
*bmap
, unsigned dst_pos
)
67 if (!dim_map
|| !bmap
)
70 src_pos
= 1 + isl_dim_total(bmap
->dim
);
71 for (i
= 0; i
< bmap
->n_div
; ++i
)
72 dim_map
->pos
[1 + dst_pos
+ i
] = src_pos
+ i
;
75 void isl_dim_map_dump(struct isl_dim_map
*dim_map
)
79 for (i
= 0; i
< dim_map
->len
; ++i
)
80 fprintf(stderr
, "%d -> %d; ", i
, dim_map
->pos
[i
]);
81 fprintf(stderr
, "\n");
84 static void copy_constraint_dim_map(isl_int
*dst
, isl_int
*src
,
85 struct isl_dim_map
*dim_map
)
89 for (i
= 0; i
< dim_map
->len
; ++i
) {
90 if (dim_map
->pos
[i
] < 0)
91 isl_int_set_si(dst
[i
], 0);
93 isl_int_set(dst
[i
], src
[dim_map
->pos
[i
]]);
97 static void copy_div_dim_map(isl_int
*dst
, isl_int
*src
,
98 struct isl_dim_map
*dim_map
)
100 isl_int_set(dst
[0], src
[0]);
101 copy_constraint_dim_map(dst
+1, src
+1, dim_map
);
104 __isl_give isl_basic_map
*isl_basic_map_add_constraints_dim_map(
105 __isl_take isl_basic_map
*dst
, __isl_take isl_basic_map
*src
,
106 __isl_take isl_dim_map
*dim_map
)
110 if (!src
|| !dst
|| !dim_map
)
113 for (i
= 0; i
< src
->n_eq
; ++i
) {
114 int i1
= isl_basic_map_alloc_equality(dst
);
117 copy_constraint_dim_map(dst
->eq
[i1
], src
->eq
[i
], dim_map
);
120 for (i
= 0; i
< src
->n_ineq
; ++i
) {
121 int i1
= isl_basic_map_alloc_inequality(dst
);
124 copy_constraint_dim_map(dst
->ineq
[i1
], src
->ineq
[i
], dim_map
);
127 for (i
= 0; i
< src
->n_div
; ++i
) {
128 int i1
= isl_basic_map_alloc_div(dst
);
131 copy_div_dim_map(dst
->div
[i1
], src
->div
[i
], dim_map
);
135 isl_basic_map_free(src
);
140 isl_basic_map_free(src
);
141 isl_basic_map_free(dst
);
145 /* Extend the given dim_map with mappings for the divs in bmap.
147 __isl_give isl_dim_map
*isl_dim_map_extend(__isl_keep isl_dim_map
*dim_map
,
148 __isl_keep isl_basic_map
*bmap
)
151 struct isl_dim_map
*res
;
154 offset
= isl_basic_map_offset(bmap
, isl_dim_div
);
156 res
= isl_dim_map_alloc(bmap
->ctx
, dim_map
->len
- 1 + bmap
->n_div
);
160 for (i
= 0; i
< dim_map
->len
; ++i
)
161 res
->pos
[i
] = dim_map
->pos
[i
];
162 for (i
= 0; i
< bmap
->n_div
; ++i
)
163 res
->pos
[dim_map
->len
+ i
] = offset
+ i
;
168 /* Extract a dim_map from a reordering.
169 * We essentially need to reverse the mapping, and add an offset
170 * of 1 for the constant term.
172 __isl_give isl_dim_map
*isl_dim_map_from_reordering(
173 __isl_keep isl_reordering
*exp
)
177 struct isl_dim_map
*dim_map
;
182 ctx
= isl_dim_get_ctx(exp
->dim
);
183 dim_map
= isl_dim_map_alloc(ctx
, isl_dim_total(exp
->dim
));
187 for (i
= 0; i
< exp
->len
; ++i
)
188 dim_map
->pos
[1 + exp
->pos
[i
]] = 1 + i
;