nest.c: directly include required headers
[pet.git] / summary.c
blobef00333454cb1536dbc8648b7c21c6c2ad5a9ce3
1 /*
2 * Copyright 2014 Ecole Normale Superieure. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY ECOLE NORMALE SUPERIEURE ''AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECOLE NORMALE SUPERIEURE OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * The views and conclusions contained in the software and documentation
29 * are those of the authors and should not be interpreted as
30 * representing official policies, either expressed or implied, of
31 * Ecole Normale Superieure.
34 #include <isl/union_set.h>
36 #include "aff.h"
37 #include "summary.h"
39 /* A pet_function_summary objects represents an argument of a function.
41 * If "type" is pet_arg_int, then the argument has an integer type and
42 * can be used to describe the accesses performed by the pet_arg_array
43 * arguments. In this case, "id" refers to the formal argument.
45 * If "type" is pet_arg_array, then we keep track of the accesses
46 * through this argument in the access relations in "access".
47 * The domains of these access relations refer to the integer arguments
48 * of the function. That is, the input dimensions correspond
49 * to the arguments of type pet_arg_int.
51 * If "type" is pet_arg_other, then we do not keep track of any
52 * further information.
54 struct pet_function_summary_arg {
55 enum pet_arg_type type;
57 union {
58 isl_id *id;
59 isl_union_map *access[pet_expr_access_end];
63 /* A pet_function_summary object keeps track of the accesses performed
64 * by a function in terms of the function arguments.
66 * "n" is the number of arguments.
67 * "arg" contains a description of the "n" arguments.
69 struct pet_function_summary {
70 int ref;
71 isl_ctx *ctx;
73 unsigned n;
75 struct pet_function_summary_arg arg[];
78 /* Construct and return a new pet_function_summary object with
79 * "n_arg" arguments, initialized to pet_arg_other.
81 __isl_give pet_function_summary *pet_function_summary_alloc(isl_ctx *ctx,
82 unsigned n_arg)
84 pet_function_summary *summary;
85 int i;
87 summary = isl_calloc(ctx, struct pet_function_summary,
88 sizeof(struct pet_function_summary) +
89 n_arg * sizeof(struct pet_function_summary_arg));
90 if (!summary)
91 return summary;
93 summary->ctx = ctx;
94 isl_ctx_ref(ctx);
95 summary->ref = 1;
96 summary->n = n_arg;
97 for (i = 0; i < n_arg; ++i)
98 summary->arg[i].type = pet_arg_other;
100 return summary;
103 /* Return an extra reference to "summary".
105 __isl_give pet_function_summary *pet_function_summary_copy(
106 __isl_keep pet_function_summary *summary)
108 if (!summary)
109 return NULL;
111 summary->ref++;
112 return summary;
115 /* Return the isl_ctx in which "summary" was created.
117 isl_ctx *pet_function_summary_get_ctx(__isl_keep pet_function_summary *summary)
119 return summary ? summary->ctx : NULL;
122 /* Free the data stored in "arg".
124 static void free_arg(struct pet_function_summary_arg *arg)
126 enum pet_expr_access_type type;
128 if (arg->type == pet_arg_int)
129 isl_id_free(arg->id);
130 if (arg->type != pet_arg_array)
131 return;
132 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
133 arg->access[type] = isl_union_map_free(arg->access[type]);
136 /* Free a reference to "summary".
138 __isl_null pet_function_summary *pet_function_summary_free(
139 __isl_take pet_function_summary *summary)
141 int i;
143 if (!summary)
144 return NULL;
145 if (--summary->ref > 0)
146 return NULL;
148 for (i = 0; i < summary->n; ++i)
149 free_arg(&summary->arg[i]);
151 isl_ctx_deref(summary->ctx);
152 free(summary);
153 return NULL;
156 /* Return the number of arguments of the function summarized by "summary".
158 int pet_function_summary_get_n_arg(__isl_keep pet_function_summary *summary)
160 if (!summary)
161 return -1;
163 return summary->n;
166 /* Mark the argument at position "pos" of "summary" as an integer argument
167 * with the given identifier.
169 __isl_give pet_function_summary *pet_function_summary_set_int(
170 __isl_take pet_function_summary *summary, int pos,
171 __isl_take isl_id *id)
173 if (!summary || !id)
174 goto error;
176 if (pos < 0 || pos >= summary->n)
177 isl_die(summary->ctx, isl_error_invalid,
178 "position out of bounds", goto error);
180 free_arg(&summary->arg[pos]);
182 summary->arg[pos].type = pet_arg_int;
183 summary->arg[pos].id = id;
185 return summary;
186 error:
187 isl_id_free(id);
188 return pet_function_summary_free(summary);
191 /* Mark the argument at position "pos" of "summary" as an array argument
192 * with the given sets of accessed elements.
193 * The integer arguments of "summary" may appear as parameters
194 * in these sets of accessed elements.
195 * These parameters are turned into input dimensions of
196 * the corresponding access relations, which are then associated
197 * to the array argument.
198 * The order of the input dimensions is the same as the order
199 * in which the integer arguments appear in the sequence of arguments.
201 __isl_give pet_function_summary *pet_function_summary_set_array(
202 __isl_take pet_function_summary *summary, int pos,
203 __isl_take isl_union_set *may_read, __isl_take isl_union_set *may_write,
204 __isl_take isl_union_set *must_write)
206 int i, n;
207 isl_space *space;
208 enum pet_expr_access_type type;
210 if (!summary || !may_read || !may_write || !must_write)
211 goto error;
213 if (pos < 0 || pos >= summary->n)
214 isl_die(summary->ctx, isl_error_invalid,
215 "position out of bounds", goto error);
217 n = 0;
218 for (i = 0; i < summary->n; ++i)
219 if (pet_function_summary_arg_is_int(summary, i))
220 n++;
222 space = isl_space_params_alloc(summary->ctx, n);
224 n = 0;
225 for (i = 0; i < summary->n; ++i)
226 if (pet_function_summary_arg_is_int(summary, i))
227 space = isl_space_set_dim_id(space, isl_dim_param, n++,
228 isl_id_copy(summary->arg[i].id));
230 free_arg(&summary->arg[pos]);
232 summary->arg[pos].type = pet_arg_array;
233 summary->arg[pos].access[pet_expr_access_may_read] =
234 isl_union_map_from_range(may_read);
235 summary->arg[pos].access[pet_expr_access_may_write] =
236 isl_union_map_from_range(may_write);
237 summary->arg[pos].access[pet_expr_access_must_write] =
238 isl_union_map_from_range(must_write);
240 for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type) {
241 isl_union_map *umap;
242 umap = summary->arg[pos].access[type];
243 umap = isl_union_map_align_params(umap, isl_space_copy(space));
244 umap = pet_union_map_move_dims(umap, isl_dim_in, 0,
245 isl_dim_param, 0, n);
246 summary->arg[pos].access[type] = umap;
247 if (!umap)
248 break;
251 isl_space_free(space);
253 if (type < pet_expr_access_end)
254 return pet_function_summary_free(summary);
256 return summary;
257 error:
258 isl_union_set_free(may_read);
259 isl_union_set_free(may_write);
260 isl_union_set_free(must_write);
261 return pet_function_summary_free(summary);
264 /* Has the argument of "summary" at position "pos" been marked
265 * as an integer argument?
267 int pet_function_summary_arg_is_int(__isl_keep pet_function_summary *summary,
268 int pos)
270 if (!summary)
271 return -1;
273 if (pos < 0 || pos >= summary->n)
274 isl_die(summary->ctx, isl_error_invalid,
275 "position out of bounds", return -1);
277 return summary->arg[pos].type == pet_arg_int;
280 /* Has the argument of "summary" at position "pos" been marked
281 * as an array argument?
283 int pet_function_summary_arg_is_array(__isl_keep pet_function_summary *summary,
284 int pos)
286 if (!summary)
287 return -1;
289 if (pos < 0 || pos >= summary->n)
290 isl_die(summary->ctx, isl_error_invalid,
291 "position out of bounds", return -1);
293 return summary->arg[pos].type == pet_arg_array;
296 /* Return the access relation of type "type" associated to
297 * the argument of "summary" at position "pos", which is assumed
298 * to be an array argument.
300 __isl_give isl_union_map *pet_function_summary_arg_get_access(
301 __isl_keep pet_function_summary *summary, int pos,
302 enum pet_expr_access_type type)
304 if (!summary)
305 return NULL;
306 if (pos < 0 || pos >= summary->n)
307 isl_die(summary->ctx, isl_error_invalid,
308 "position out of bounds", return NULL);
309 if (summary->arg[pos].type != pet_arg_array)
310 isl_die(summary->ctx, isl_error_invalid,
311 "not an array argument", return NULL);
313 return isl_union_map_copy(summary->arg[pos].access[type]);
316 /* Print "summary" to "p" in YAML format.
318 __isl_give isl_printer *pet_function_summary_print(
319 __isl_keep pet_function_summary *summary, __isl_take isl_printer *p)
321 int i;
323 if (!summary || !p)
324 return isl_printer_free(p);
325 p = isl_printer_yaml_start_sequence(p);
326 for (i = 0; i < summary->n; ++i) {
327 switch (summary->arg[i].type) {
328 case pet_arg_int:
329 p = isl_printer_yaml_start_mapping(p);
330 p = isl_printer_print_str(p, "id");
331 p = isl_printer_yaml_next(p);
332 p = isl_printer_print_id(p, summary->arg[i].id);
333 p = isl_printer_yaml_next(p);
334 p = isl_printer_yaml_end_mapping(p);
335 break;
336 case pet_arg_other:
337 p = isl_printer_print_str(p, "other");
338 break;
339 case pet_arg_array:
340 p = isl_printer_yaml_start_mapping(p);
341 p = isl_printer_print_str(p, "may_read");
342 p = isl_printer_yaml_next(p);
343 p = isl_printer_print_union_map(p,
344 summary->arg[i].access[pet_expr_access_may_read]);
345 p = isl_printer_yaml_next(p);
346 p = isl_printer_print_str(p, "may_write");
347 p = isl_printer_yaml_next(p);
348 p = isl_printer_print_union_map(p,
349 summary->arg[i].access[pet_expr_access_may_write]);
350 p = isl_printer_yaml_next(p);
351 p = isl_printer_print_str(p, "must_write");
352 p = isl_printer_yaml_next(p);
353 p = isl_printer_print_union_map(p,
354 summary->arg[i].access[pet_expr_access_must_write]);
355 p = isl_printer_yaml_next(p);
356 p = isl_printer_yaml_end_mapping(p);
357 break;
360 p = isl_printer_yaml_end_sequence(p);
362 return p;
365 /* Dump "summary" to stderr.
367 void pet_function_summary_dump(__isl_keep pet_function_summary *summary)
369 isl_printer *p;
371 if (!summary)
372 return;
374 p = isl_printer_to_file(pet_function_summary_get_ctx(summary), stderr);
375 p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_BLOCK);
376 p = pet_function_summary_print(summary, p);
378 isl_printer_free(p);