pet_scop_from_pet_tree: create statements directly from expression trees
[pet.git] / value_bounds.c
blobbe43b3674256ed7fd4f36099faa565592ea9ab12
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
35 #include <isl/map.h>
37 #include "expr.h"
38 #include "value_bounds.h"
40 /* If "value_bounds" contains any bounds on the variable accessed by "arg",
41 * then intersect the range of "map" with the valid set of values.
43 static __isl_give isl_map *access_apply_value_bounds(__isl_take isl_map *map,
44 __isl_keep pet_expr *arg, __isl_keep isl_union_map *value_bounds)
46 isl_id *id;
47 isl_map *vb;
48 isl_space *space;
49 isl_ctx *ctx = isl_map_get_ctx(map);
51 id = pet_expr_access_get_id(arg);
52 space = isl_space_alloc(ctx, 0, 0, 1);
53 space = isl_space_set_tuple_id(space, isl_dim_in, id);
54 vb = isl_union_map_extract_map(value_bounds, space);
55 if (!isl_map_plain_is_empty(vb))
56 map = isl_map_intersect_range(map, isl_map_range(vb));
57 else
58 isl_map_free(vb);
60 return map;
63 /* Given a set "domain", return a wrapped relation with the given set
64 * as domain and a range of dimension "n_arg", where each coordinate
65 * is either unbounded or, if the corresponding element of args is of
66 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
68 __isl_give isl_set *pet_value_bounds_apply(__isl_take isl_set *domain,
69 unsigned n_arg, __isl_keep pet_expr **args,
70 __isl_keep isl_union_map *value_bounds)
72 int i;
73 isl_map *map;
74 isl_space *space;
76 map = isl_map_from_domain(domain);
77 space = isl_map_get_space(map);
78 space = isl_space_add_dims(space, isl_dim_out, 1);
80 for (i = 0; i < n_arg; ++i) {
81 isl_map *map_i;
82 pet_expr *arg = args[i];
84 map_i = isl_map_universe(isl_space_copy(space));
85 if (arg->type == pet_expr_access)
86 map_i = access_apply_value_bounds(map_i, arg,
87 value_bounds);
88 map = isl_map_flat_range_product(map, map_i);
90 isl_space_free(space);
92 return isl_map_wrap(map);