2 * Copyright 2012-2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
11 #include <isl_map_private.h>
12 #include <isl_aff_private.h>
13 #include <isl/constraint.h>
16 /* Stride information about a specific set dimension.
17 * The values of the set dimension are equal to
18 * "offset" plus a multiple of "stride".
20 struct isl_stride_info
{
25 /* Return the ctx to which "si" belongs.
27 isl_ctx
*isl_stride_info_get_ctx(__isl_keep isl_stride_info
*si
)
32 return isl_val_get_ctx(si
->stride
);
35 /* Free "si" and return NULL.
37 __isl_null isl_stride_info
*isl_stride_info_free(
38 __isl_take isl_stride_info
*si
)
42 isl_val_free(si
->stride
);
43 isl_aff_free(si
->offset
);
48 /* Construct an isl_stride_info object with given offset and stride.
50 __isl_give isl_stride_info
*isl_stride_info_alloc(
51 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
53 struct isl_stride_info
*si
;
55 if (!stride
|| !offset
)
57 si
= isl_alloc_type(isl_val_get_ctx(stride
), struct isl_stride_info
);
69 /* Make a copy of "si" and return it.
71 __isl_give isl_stride_info
*isl_stride_info_copy(
72 __isl_keep isl_stride_info
*si
)
77 return isl_stride_info_alloc(isl_val_copy(si
->stride
),
78 isl_aff_copy(si
->offset
));
81 /* Return the stride of "si".
83 __isl_give isl_val
*isl_stride_info_get_stride(__isl_keep isl_stride_info
*si
)
87 return isl_val_copy(si
->stride
);
90 /* Return the offset of "si".
92 __isl_give isl_aff
*isl_stride_info_get_offset(__isl_keep isl_stride_info
*si
)
96 return isl_aff_copy(si
->offset
);
99 /* Information used inside detect_stride.
101 * "pos" is the set dimension at which the stride is being determined.
102 * "want_offset" is set if the offset should be computed.
103 * "found" is set if some stride was found already.
104 * "stride" and "offset" contain the (combined) stride and offset
105 * found so far and are NULL when "found" is not set.
106 * If "want_offset" is not set, then "offset" remains NULL.
108 struct isl_detect_stride_data
{
116 /* Set the stride and offset of data->pos to the given
117 * value and expression.
119 * If we had already found a stride before, then the two strides
120 * are combined into a single stride.
122 * In particular, if the new stride information is of the form
126 * and the old stride information is of the form
130 * then we compute the extended gcd of s and s2
134 * with g = gcd(s,s2), multiply the first equation with t1 = b s2/g
135 * and the second with t2 = a s1/g.
138 * i = (b s2 + a s1)/g i = t1 f + t2 f2 + (s s2)/g (...)
140 * so that t1 f + t2 f2 is the combined offset and (s s2)/g = lcm(s,s2)
141 * is the combined stride.
143 static isl_stat
set_stride(struct isl_detect_stride_data
*data
,
144 __isl_take isl_val
*stride
, __isl_take isl_aff
*offset
)
146 if (!stride
|| !offset
)
150 isl_val
*stride2
, *a
, *b
, *g
;
153 stride2
= data
->stride
;
154 g
= isl_val_gcdext(isl_val_copy(stride
), isl_val_copy(stride2
),
156 a
= isl_val_mul(a
, isl_val_copy(stride
));
157 a
= isl_val_div(a
, isl_val_copy(g
));
158 stride2
= isl_val_div(stride2
, g
);
159 b
= isl_val_mul(b
, isl_val_copy(stride2
));
160 stride
= isl_val_mul(stride
, stride2
);
162 if (!data
->want_offset
) {
166 offset2
= data
->offset
;
167 offset2
= isl_aff_scale_val(offset2
, a
);
168 offset
= isl_aff_scale_val(offset
, b
);
169 offset
= isl_aff_add(offset
, offset2
);
174 data
->stride
= stride
;
175 if (data
->want_offset
)
176 data
->offset
= offset
;
178 isl_aff_free(offset
);
179 if (!data
->stride
|| (data
->want_offset
&& !data
->offset
))
180 return isl_stat_error
;
184 isl_val_free(stride
);
185 isl_aff_free(offset
);
186 return isl_stat_error
;
189 /* Check if constraint "c" imposes any stride on dimension data->pos
190 * and, if so, update the stride information in "data".
192 * In order to impose a stride on the dimension, "c" needs to be an equality
193 * and it needs to involve the dimension. Note that "c" may also be
194 * a div constraint and thus an inequality that we cannot use.
196 * Let c be of the form
198 * h(p) + g * v * i + g * stride * f(alpha) = 0
200 * with h(p) an expression in terms of the parameters and other dimensions
201 * and f(alpha) an expression in terms of the existentially quantified
204 * If "stride" is not zero and not one, then it represents a non-trivial stride
205 * on "i". We compute a and b such that
211 * g v i = -h(p) + g stride f(alpha)
213 * a g v i = -a h(p) + g stride f(alpha)
215 * a g v i + b g stride i = -a h(p) + g stride * (...)
217 * g i = -a h(p) + g stride * (...)
219 * i = -a h(p)/g + stride * (...)
221 * The expression "-a h(p)/g" can therefore be used as offset.
223 static isl_stat
detect_stride(__isl_take isl_constraint
*c
, void *user
)
225 struct isl_detect_stride_data
*data
= user
;
229 isl_stat r
= isl_stat_ok
;
230 isl_val
*v
, *stride
, *m
;
231 isl_bool is_eq
, relevant
, has_stride
;
233 is_eq
= isl_constraint_is_equality(c
);
234 relevant
= isl_constraint_involves_dims(c
, isl_dim_set
, data
->pos
, 1);
235 if (is_eq
< 0 || relevant
< 0)
237 if (!is_eq
|| !relevant
) {
238 isl_constraint_free(c
);
242 n_div
= isl_constraint_dim(c
, isl_dim_div
);
245 ctx
= isl_constraint_get_ctx(c
);
246 stride
= isl_val_zero(ctx
);
247 for (i
= 0; i
< n_div
; ++i
) {
248 v
= isl_constraint_get_coefficient_val(c
, isl_dim_div
, i
);
249 stride
= isl_val_gcd(stride
, v
);
252 v
= isl_constraint_get_coefficient_val(c
, isl_dim_set
, data
->pos
);
253 m
= isl_val_gcd(isl_val_copy(stride
), isl_val_copy(v
));
254 stride
= isl_val_div(stride
, isl_val_copy(m
));
255 v
= isl_val_div(v
, isl_val_copy(m
));
257 has_stride
= isl_val_gt_si(stride
, 1);
258 if (has_stride
>= 0 && has_stride
) {
260 isl_val
*gcd
, *a
, *b
;
262 gcd
= isl_val_gcdext(v
, isl_val_copy(stride
), &a
, &b
);
266 aff
= isl_constraint_get_aff(c
);
267 for (i
= 0; i
< n_div
; ++i
)
268 aff
= isl_aff_set_coefficient_si(aff
,
270 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_in
, data
->pos
, 0);
271 aff
= isl_aff_remove_unused_divs(aff
);
273 aff
= isl_aff_scale_val(aff
, a
);
274 aff
= isl_aff_scale_down_val(aff
, m
);
275 r
= set_stride(data
, stride
, aff
);
277 isl_val_free(stride
);
282 isl_constraint_free(c
);
284 return isl_stat_error
;
287 isl_constraint_free(c
);
288 return isl_stat_error
;
291 /* Check if the constraints in "set" imply any stride on set dimension "pos" and
292 * store the results in data->stride and data->offset.
294 * In particular, compute the affine hull and then check if
295 * any of the constraints in the hull impose any stride on the dimension.
296 * If no such constraint can be found, then the offset is taken
297 * to be the zero expression and the stride is taken to be one.
299 static void set_detect_stride(__isl_keep isl_set
*set
, int pos
,
300 struct isl_detect_stride_data
*data
)
304 hull
= isl_set_affine_hull(isl_set_copy(set
));
310 if (isl_basic_set_foreach_constraint(hull
, &detect_stride
, data
) < 0)
314 data
->stride
= isl_val_one(isl_set_get_ctx(set
));
315 if (data
->want_offset
) {
319 space
= isl_set_get_space(set
);
320 ls
= isl_local_space_from_space(space
);
321 data
->offset
= isl_aff_zero_on_domain(ls
);
324 isl_basic_set_free(hull
);
327 isl_basic_set_free(hull
);
328 data
->stride
= isl_val_free(data
->stride
);
329 data
->offset
= isl_aff_free(data
->offset
);
332 /* Check if the constraints in "set" imply any stride on set dimension "pos" and
333 * return the results in the form of an offset and a stride.
335 __isl_give isl_stride_info
*isl_set_get_stride_info(__isl_keep isl_set
*set
,
338 struct isl_detect_stride_data data
;
340 data
.want_offset
= 1;
341 set_detect_stride(set
, pos
, &data
);
343 return isl_stride_info_alloc(data
.stride
, data
.offset
);
346 /* Check if the constraints in "set" imply any stride on set dimension "pos" and
347 * return this stride.
349 __isl_give isl_val
*isl_set_get_stride(__isl_keep isl_set
*set
, int pos
)
351 struct isl_detect_stride_data data
;
353 data
.want_offset
= 0;
354 set_detect_stride(set
, pos
, &data
);
359 /* Check if the constraints in "map" imply any stride on output dimension "pos",
360 * independently of any other output dimensions, and
361 * return the results in the form of an offset and a stride.
363 * Convert the input to a set with only the input dimensions and
364 * the single output dimension such that it be passed to
365 * isl_set_get_stride_info and convert the result back to
366 * an expression defined over the domain of "map".
368 __isl_give isl_stride_info
*isl_map_get_range_stride_info(
369 __isl_keep isl_map
*map
, int pos
)
375 n_in
= isl_map_dim(map
, isl_dim_in
);
378 map
= isl_map_copy(map
);
379 map
= isl_map_project_onto(map
, isl_dim_out
, pos
, 1);
380 set
= isl_map_wrap(map
);
381 si
= isl_set_get_stride_info(set
, n_in
);
385 si
->offset
= isl_aff_domain_factor_domain(si
->offset
);
387 return isl_stride_info_free(si
);