[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (#98138)
[llvm-project.git] / polly / lib / External / isl / isl_box.c
blob34a49eafb73a082d0fbdabde1c4372b6456e4a52
1 /*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
9 * 91893 Orsay, France
10 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 #include <isl/val.h>
14 #include <isl/space.h>
15 #include <isl_map_private.h>
16 #include <isl_aff_private.h>
17 #include <isl/constraint.h>
18 #include <isl/ilp.h>
19 #include <isl/fixed_box.h>
21 /* Representation of a box of fixed size containing the elements
22 * [offset, offset + size).
23 * "size" lives in the target space of "offset".
25 * If any of the "offsets" is NaN, then the object represents
26 * the failure of finding a fixed-size box.
28 struct isl_fixed_box {
29 isl_multi_aff *offset;
30 isl_multi_val *size;
33 /* Free "box" and return NULL.
35 __isl_null isl_fixed_box *isl_fixed_box_free(__isl_take isl_fixed_box *box)
37 if (!box)
38 return NULL;
39 isl_multi_aff_free(box->offset);
40 isl_multi_val_free(box->size);
41 free(box);
42 return NULL;
45 /* Construct an isl_fixed_box with the given offset and size.
47 static __isl_give isl_fixed_box *isl_fixed_box_alloc(
48 __isl_take isl_multi_aff *offset, __isl_take isl_multi_val *size)
50 isl_ctx *ctx;
51 isl_fixed_box *box;
53 if (!offset || !size)
54 goto error;
55 ctx = isl_multi_aff_get_ctx(offset);
56 box = isl_alloc_type(ctx, struct isl_fixed_box);
57 if (!box)
58 goto error;
59 box->offset = offset;
60 box->size = size;
62 return box;
63 error:
64 isl_multi_aff_free(offset);
65 isl_multi_val_free(size);
66 return NULL;
69 /* Construct an initial isl_fixed_box with zero offsets
70 * in the given space and zero corresponding sizes.
72 static __isl_give isl_fixed_box *isl_fixed_box_init(
73 __isl_take isl_space *space)
75 isl_multi_aff *offset;
76 isl_multi_val *size;
78 offset = isl_multi_aff_zero(isl_space_copy(space));
79 space = isl_space_drop_all_params(isl_space_range(space));
80 size = isl_multi_val_zero(space);
81 return isl_fixed_box_alloc(offset, size);
84 /* Return a copy of "box".
86 __isl_give isl_fixed_box *isl_fixed_box_copy(__isl_keep isl_fixed_box *box)
88 isl_multi_aff *offset;
89 isl_multi_val *size;
91 offset = isl_fixed_box_get_offset(box);
92 size = isl_fixed_box_get_size(box);
93 return isl_fixed_box_alloc(offset, size);
96 /* Replace the offset and size in direction "pos" by "offset" and "size"
97 * (without checking whether "box" is a valid box).
99 static __isl_give isl_fixed_box *isl_fixed_box_set_extent(
100 __isl_take isl_fixed_box *box, int pos, __isl_keep isl_aff *offset,
101 __isl_keep isl_val *size)
103 if (!box)
104 return NULL;
105 box->offset = isl_multi_aff_set_aff(box->offset, pos,
106 isl_aff_copy(offset));
107 box->size = isl_multi_val_set_val(box->size, pos, isl_val_copy(size));
108 if (!box->offset || !box->size)
109 return isl_fixed_box_free(box);
110 return box;
113 /* Replace the offset and size in direction "pos" by "offset" and "size",
114 * if "box" is a valid box.
116 static __isl_give isl_fixed_box *isl_fixed_box_set_valid_extent(
117 __isl_take isl_fixed_box *box, int pos, __isl_keep isl_aff *offset,
118 __isl_keep isl_val *size)
120 isl_bool valid;
122 valid = isl_fixed_box_is_valid(box);
123 if (valid < 0 || !valid)
124 return box;
125 return isl_fixed_box_set_extent(box, pos, offset, size);
128 /* Replace "box" by an invalid box, by setting all offsets to NaN
129 * (and all sizes to infinity).
131 static __isl_give isl_fixed_box *isl_fixed_box_invalidate(
132 __isl_take isl_fixed_box *box)
134 int i;
135 isl_size n;
136 isl_space *space;
137 isl_val *infty;
138 isl_aff *nan;
140 if (!box)
141 return NULL;
142 n = isl_multi_val_dim(box->size, isl_dim_set);
143 if (n < 0)
144 return isl_fixed_box_free(box);
146 infty = isl_val_infty(isl_fixed_box_get_ctx(box));
147 space = isl_space_domain(isl_fixed_box_get_space(box));
148 nan = isl_aff_nan_on_domain(isl_local_space_from_space(space));
149 for (i = 0; i < n; ++i)
150 box = isl_fixed_box_set_extent(box, i, nan, infty);
151 isl_aff_free(nan);
152 isl_val_free(infty);
154 if (!box->offset || !box->size)
155 return isl_fixed_box_free(box);
156 return box;
159 /* Project the domain of the fixed box onto its parameter space.
160 * In particular, project out the domain of the offset.
162 static __isl_give isl_fixed_box *isl_fixed_box_project_domain_on_params(
163 __isl_take isl_fixed_box *box)
165 isl_bool valid;
167 valid = isl_fixed_box_is_valid(box);
168 if (valid < 0)
169 return isl_fixed_box_free(box);
170 if (!valid)
171 return box;
173 box->offset = isl_multi_aff_project_domain_on_params(box->offset);
174 if (!box->offset)
175 return isl_fixed_box_free(box);
177 return box;
180 /* Return the isl_ctx to which "box" belongs.
182 isl_ctx *isl_fixed_box_get_ctx(__isl_keep isl_fixed_box *box)
184 if (!box)
185 return NULL;
186 return isl_multi_aff_get_ctx(box->offset);
189 /* Return the space in which "box" lives.
191 __isl_give isl_space *isl_fixed_box_get_space(__isl_keep isl_fixed_box *box)
193 if (!box)
194 return NULL;
195 return isl_multi_aff_get_space(box->offset);
198 /* Does "box" contain valid information?
200 isl_bool isl_fixed_box_is_valid(__isl_keep isl_fixed_box *box)
202 if (!box)
203 return isl_bool_error;
204 return isl_bool_not(isl_multi_aff_involves_nan(box->offset));
207 /* Return the offsets of the box "box".
209 __isl_give isl_multi_aff *isl_fixed_box_get_offset(
210 __isl_keep isl_fixed_box *box)
212 if (!box)
213 return NULL;
214 return isl_multi_aff_copy(box->offset);
217 /* Return the sizes of the box "box".
219 __isl_give isl_multi_val *isl_fixed_box_get_size(__isl_keep isl_fixed_box *box)
221 if (!box)
222 return NULL;
223 return isl_multi_val_copy(box->size);
226 /* Data used in set_dim_extent and compute_size_in_direction.
228 * "bset" is a wrapped copy of the basic map that has the selected
229 * output dimension as range.
230 * "pos" is the position of the variable representing the output dimension,
231 * i.e., the variable for which the size should be computed. This variable
232 * is also the last variable in "bset".
233 * "size" is the best size found so far
234 * (infinity if no offset was found so far).
235 * "offset" is the offset corresponding to the best size
236 * (NULL if no offset was found so far).
238 struct isl_size_info {
239 isl_basic_set *bset;
240 isl_size pos;
241 isl_val *size;
242 isl_aff *offset;
245 /* Is "c" a suitable bound on dimension "pos" for use as a lower bound
246 * of a fixed-size range.
247 * In particular, it needs to be a lower bound on "pos".
248 * In order for the final offset not to be too complicated,
249 * the constraint itself should also not involve any integer divisions.
251 static isl_bool is_suitable_bound(__isl_keep isl_constraint *c, unsigned pos)
253 isl_size n_div;
254 isl_bool is_bound, any_divs;
256 is_bound = isl_constraint_is_lower_bound(c, isl_dim_set, pos);
257 if (is_bound < 0 || !is_bound)
258 return is_bound;
260 n_div = isl_constraint_dim(c, isl_dim_div);
261 if (n_div < 0)
262 return isl_bool_error;
263 any_divs = isl_constraint_involves_dims(c, isl_dim_div, 0, n_div);
264 return isl_bool_not(any_divs);
267 /* Given a constraint from the basic set describing the bounds on
268 * an array index, check if it is a lower bound, say m i >= b(x), and,
269 * if so, check whether the expression "i - ceil(b(x)/m) + 1" has a constant
270 * upper bound. If so, and if this bound is smaller than any bound
271 * derived from earlier constraints, set the size to this bound on
272 * the expression and the lower bound to ceil(b(x)/m).
274 static isl_stat compute_size_in_direction(__isl_take isl_constraint *c,
275 void *user)
277 struct isl_size_info *info = user;
278 isl_val *v;
279 isl_aff *aff;
280 isl_aff *lb;
281 isl_bool is_bound, better;
283 is_bound = is_suitable_bound(c, info->pos);
284 if (is_bound < 0 || !is_bound) {
285 isl_constraint_free(c);
286 return is_bound < 0 ? isl_stat_error : isl_stat_ok;
289 aff = isl_constraint_get_bound(c, isl_dim_set, info->pos);
290 aff = isl_aff_ceil(aff);
292 lb = isl_aff_copy(aff);
294 aff = isl_aff_neg(aff);
295 aff = isl_aff_add_coefficient_si(aff, isl_dim_in, info->pos, 1);
297 v = isl_basic_set_max_val(info->bset, aff);
298 isl_aff_free(aff);
300 v = isl_val_add_ui(v, 1);
301 better = isl_val_lt(v, info->size);
302 if (better >= 0 && better) {
303 isl_val_free(info->size);
304 info->size = isl_val_copy(v);
305 lb = isl_aff_domain_factor_domain(lb);
306 isl_aff_free(info->offset);
307 info->offset = isl_aff_copy(lb);
309 isl_val_free(v);
310 isl_aff_free(lb);
312 isl_constraint_free(c);
314 return better < 0 ? isl_stat_error : isl_stat_ok;
317 /* Look for a fixed-size range of values for the output dimension "pos"
318 * of "map", by looking for a lower-bound expression in the parameters
319 * and input dimensions such that the range of the output dimension
320 * is a constant shifted by this expression.
322 * In particular, look through the explicit lower bounds on the output dimension
323 * for candidate expressions and pick the one that results in the smallest size.
324 * Initialize the size with infinity and if no better size is found
325 * then invalidate the box. Otherwise, set the offset and size
326 * in the given direction by those that correspond to the smallest size.
328 * Note that while evaluating the size corresponding to a lower bound,
329 * an affine expression is constructed from the lower bound.
330 * This lower bound may therefore not have any unknown local variables.
331 * Eliminate any unknown local variables up front.
332 * No such restriction needs to be imposed on the set over which
333 * the size is computed.
335 static __isl_give isl_fixed_box *set_dim_extent(__isl_take isl_fixed_box *box,
336 __isl_keep isl_map *map, int pos)
338 struct isl_size_info info;
339 isl_bool valid;
340 isl_ctx *ctx;
341 isl_basic_set *bset;
343 if (!box || !map)
344 return isl_fixed_box_free(box);
346 ctx = isl_map_get_ctx(map);
347 map = isl_map_copy(map);
348 map = isl_map_project_onto(map, isl_dim_out, pos, 1);
349 info.size = isl_val_infty(ctx);
350 info.offset = NULL;
351 info.pos = isl_map_dim(map, isl_dim_in);
352 info.bset = isl_basic_map_wrap(isl_map_simple_hull(map));
353 bset = isl_basic_set_copy(info.bset);
354 bset = isl_basic_set_remove_unknown_divs(bset);
355 if (info.pos < 0)
356 bset = isl_basic_set_free(bset);
357 if (isl_basic_set_foreach_constraint(bset,
358 &compute_size_in_direction, &info) < 0)
359 box = isl_fixed_box_free(box);
360 isl_basic_set_free(bset);
361 valid = isl_val_is_int(info.size);
362 if (valid < 0)
363 box = isl_fixed_box_free(box);
364 else if (valid)
365 box = isl_fixed_box_set_valid_extent(box, pos,
366 info.offset, info.size);
367 else
368 box = isl_fixed_box_invalidate(box);
369 isl_val_free(info.size);
370 isl_aff_free(info.offset);
371 isl_basic_set_free(info.bset);
373 return box;
376 /* Try and construct a fixed-size rectangular box with an offset
377 * in terms of the domain of "map" that contains the range of "map".
378 * If no such box can be constructed, then return an invalidated box,
379 * i.e., one where isl_fixed_box_is_valid returns false.
381 * Iterate over the dimensions in the range
382 * setting the corresponding offset and extent.
384 __isl_give isl_fixed_box *isl_map_get_range_simple_fixed_box_hull(
385 __isl_keep isl_map *map)
387 int i;
388 isl_size n;
389 isl_space *space;
390 isl_fixed_box *box;
392 n = isl_map_dim(map, isl_dim_out);
393 if (n < 0)
394 return NULL;
395 space = isl_map_get_space(map);
396 box = isl_fixed_box_init(space);
398 map = isl_map_detect_equalities(isl_map_copy(map));
399 for (i = 0; i < n; ++i) {
400 isl_bool valid;
402 box = set_dim_extent(box, map, i);
403 valid = isl_fixed_box_is_valid(box);
404 if (valid < 0 || !valid)
405 break;
407 isl_map_free(map);
409 return box;
412 /* Compute a fixed box from "set" using "map_box" by treating it as a map
413 * with a zero-dimensional domain and
414 * project out the domain again from the result.
416 static __isl_give isl_fixed_box *fixed_box_as_map(__isl_keep isl_set *set,
417 __isl_give isl_fixed_box *(*map_box)(__isl_keep isl_map *map))
419 isl_map *map;
420 isl_fixed_box *box;
422 map = isl_map_from_range(isl_set_copy(set));
423 box = map_box(map);
424 isl_map_free(map);
425 box = isl_fixed_box_project_domain_on_params(box);
427 return box;
430 /* Try and construct a fixed-size rectangular box with an offset
431 * in terms of the parameters of "set" that contains "set".
432 * If no such box can be constructed, then return an invalidated box,
433 * i.e., one where isl_fixed_box_is_valid returns false.
435 * Compute the box using isl_map_get_range_simple_fixed_box_hull
436 * by constructing a map from the set and
437 * project out the domain again from the result.
439 __isl_give isl_fixed_box *isl_set_get_simple_fixed_box_hull(
440 __isl_keep isl_set *set)
442 return fixed_box_as_map(set, &isl_map_get_range_simple_fixed_box_hull);
445 /* Check whether the output elements lie on a rectangular lattice,
446 * possibly depending on the parameters and the input dimensions.
447 * Return a tile in this lattice.
448 * If no stride information can be found, then return a tile of size 1
449 * (and offset 0).
451 * Obtain stride information in each output dimension separately and
452 * combine the results.
454 __isl_give isl_fixed_box *isl_map_get_range_lattice_tile(
455 __isl_keep isl_map *map)
457 int i;
458 isl_size n;
459 isl_space *space;
460 isl_fixed_box *box;
462 n = isl_map_dim(map, isl_dim_out);
463 if (n < 0)
464 return NULL;
465 space = isl_map_get_space(map);
466 box = isl_fixed_box_init(space);
468 for (i = 0; i < n; ++i) {
469 isl_val *stride;
470 isl_aff *offset;
471 isl_stride_info *si;
473 si = isl_map_get_range_stride_info(map, i);
474 stride = isl_stride_info_get_stride(si);
475 offset = isl_stride_info_get_offset(si);
476 isl_stride_info_free(si);
478 box = isl_fixed_box_set_valid_extent(box, i, offset, stride);
480 isl_aff_free(offset);
481 isl_val_free(stride);
484 return box;
487 /* Check whether the elements lie on a rectangular lattice,
488 * possibly depending on the parameters.
489 * Return a tile in this lattice.
490 * If no stride information can be found, then return a tile of size 1
491 * (and offset 0).
493 * Consider the set as a map with a zero-dimensional domain and
494 * obtain a lattice tile of that map.
496 __isl_give isl_fixed_box *isl_set_get_lattice_tile(__isl_keep isl_set *set)
498 return fixed_box_as_map(set, &isl_map_get_range_lattice_tile);
501 #undef BASE
502 #define BASE multi_val
503 #include "print_yaml_field_templ.c"
505 #undef BASE
506 #define BASE multi_aff
507 #include "print_yaml_field_templ.c"
509 /* Print the information contained in "box" to "p".
510 * The information is printed as a YAML document.
512 __isl_give isl_printer *isl_printer_print_fixed_box(
513 __isl_take isl_printer *p, __isl_keep isl_fixed_box *box)
515 if (!box)
516 return isl_printer_free(p);
518 p = isl_printer_yaml_start_mapping(p);
519 p = print_yaml_field_multi_aff(p, "offset", box->offset);
520 p = print_yaml_field_multi_val(p, "size", box->size);
521 p = isl_printer_yaml_end_mapping(p);
523 return p;
526 #undef BASE
527 #define BASE fixed_box
528 #include <print_templ_yaml.c>