pet_expr: put pet_expr_call field in substructure
[pet.git] / scop.c
blob72beacb1b20523d975ce19d28a2dec3e58387bea
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "aff.h"
40 #include "expr.h"
41 #include "expr_access_type.h"
42 #include "filter.h"
43 #include "loc.h"
44 #include "nest.h"
45 #include "scop.h"
46 #include "tree.h"
47 #include "print.h"
48 #include "value_bounds.h"
50 /* pet_scop with extra information that is used during parsing and printing.
52 * In particular, we keep track of conditions under which we want
53 * to skip the rest of the current loop iteration (skip[pet_skip_now])
54 * and of conditions under which we want to skip subsequent
55 * loop iterations (skip[pet_skip_later]).
57 * The conditions are represented as index expressions defined
58 * over the outer loop iterators. The index expression is either
59 * a boolean affine expression or an access to a variable, which
60 * is assumed to attain values zero and one. The condition holds
61 * if the variable has value one or if the affine expression
62 * has value one (typically for only part of the domain).
64 * A missing condition (skip[type] == NULL) means that we don't want
65 * to skip anything.
67 * Additionally, we keep track of the original input file
68 * inside pet_transform_C_source.
70 struct pet_scop_ext {
71 struct pet_scop scop;
73 isl_multi_pw_aff *skip[2];
74 FILE *input;
77 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
78 * The input domain is anonymous and is the same as the domains
79 * of the access expressions inside "tree".
80 * These domains are modified to include the name of the statement.
81 * This name is given by tree->label if it is non-NULL.
82 * Otherwise, the name is constructed as S_<id>.
84 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
85 int id, __isl_take pet_tree *tree)
87 struct pet_stmt *stmt;
88 isl_ctx *ctx;
89 isl_id *label;
90 isl_space *space;
91 isl_map *sched;
92 isl_multi_aff *ma;
93 isl_multi_pw_aff *add_name;
94 char name[50];
96 if (!domain || !tree)
97 goto error;
99 ctx = pet_tree_get_ctx(tree);
100 stmt = isl_calloc_type(ctx, struct pet_stmt);
101 if (!stmt)
102 goto error;
104 if (tree->label) {
105 label = isl_id_copy(tree->label);
106 } else {
107 snprintf(name, sizeof(name), "S_%d", id);
108 label = isl_id_alloc(ctx, name, NULL);
110 domain = isl_set_set_tuple_id(domain, label);
111 space = isl_set_get_space(domain);
112 space = pet_nested_remove_from_space(space);
113 sched = isl_map_universe(isl_space_from_domain(isl_space_copy(space)));
114 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
116 add_name = isl_multi_pw_aff_from_multi_aff(ma);
117 tree = pet_tree_update_domain(tree, add_name);
119 stmt->loc = pet_tree_get_loc(tree);
120 stmt->domain = domain;
121 stmt->schedule = sched;
122 stmt->body = tree;
124 if (!stmt->domain || !stmt->schedule || !stmt->body)
125 return pet_stmt_free(stmt);
127 return stmt;
128 error:
129 isl_set_free(domain);
130 pet_tree_free(tree);
131 return NULL;
134 void *pet_stmt_free(struct pet_stmt *stmt)
136 int i;
138 if (!stmt)
139 return NULL;
141 pet_loc_free(stmt->loc);
142 isl_set_free(stmt->domain);
143 isl_map_free(stmt->schedule);
144 pet_tree_free(stmt->body);
146 for (i = 0; i < stmt->n_arg; ++i)
147 pet_expr_free(stmt->args[i]);
148 free(stmt->args);
150 free(stmt);
151 return NULL;
154 /* Return the iteration space of "stmt".
156 * If the statement has arguments, then stmt->domain is a wrapped map
157 * mapping the iteration domain to the values of the arguments
158 * for which this statement is executed.
159 * In this case, we need to extract the domain space of this wrapped map.
161 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
163 isl_space *space;
165 if (!stmt)
166 return NULL;
168 space = isl_set_get_space(stmt->domain);
169 if (isl_space_is_wrapping(space))
170 space = isl_space_domain(isl_space_unwrap(space));
172 return space;
175 static void stmt_dump(struct pet_stmt *stmt, int indent)
177 int i;
179 if (!stmt)
180 return;
182 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
183 fprintf(stderr, "%*s", indent, "");
184 isl_set_dump(stmt->domain);
185 fprintf(stderr, "%*s", indent, "");
186 isl_map_dump(stmt->schedule);
187 pet_tree_dump_with_indent(stmt->body, indent);
188 for (i = 0; i < stmt->n_arg; ++i)
189 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
192 void pet_stmt_dump(struct pet_stmt *stmt)
194 stmt_dump(stmt, 0);
197 /* Allocate a new pet_type with the given "name" and "definition".
199 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
200 const char *definition)
202 struct pet_type *type;
204 type = isl_alloc_type(ctx, struct pet_type);
205 if (!type)
206 return NULL;
208 type->name = strdup(name);
209 type->definition = strdup(definition);
211 if (!type->name || !type->definition)
212 return pet_type_free(type);
214 return type;
217 /* Free "type" and return NULL.
219 struct pet_type *pet_type_free(struct pet_type *type)
221 if (!type)
222 return NULL;
224 free(type->name);
225 free(type->definition);
227 free(type);
228 return NULL;
231 struct pet_array *pet_array_free(struct pet_array *array)
233 if (!array)
234 return NULL;
236 isl_set_free(array->context);
237 isl_set_free(array->extent);
238 isl_set_free(array->value_bounds);
239 free(array->element_type);
241 free(array);
242 return NULL;
245 void pet_array_dump(struct pet_array *array)
247 if (!array)
248 return;
250 isl_set_dump(array->context);
251 isl_set_dump(array->extent);
252 isl_set_dump(array->value_bounds);
253 fprintf(stderr, "%s%s%s\n", array->element_type,
254 array->element_is_record ? " element-is-record" : "",
255 array->live_out ? " live-out" : "");
258 /* Alloc a pet_scop structure, with extra room for information that
259 * is only used during parsing.
261 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
263 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
266 /* Construct a pet_scop in the given space and with room for n statements.
268 * The context is initialized as a universe set in "space".
270 * Since no information on the location is known at this point,
271 * scop->loc is initialized with pet_loc_dummy.
273 static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n)
275 isl_ctx *ctx;
276 struct pet_scop *scop;
278 if (!space)
279 return NULL;
281 ctx = isl_space_get_ctx(space);
282 scop = pet_scop_alloc(ctx);
283 if (!scop)
284 return NULL;
286 scop->context = isl_set_universe(isl_space_copy(space));
287 scop->context_value = isl_set_universe(isl_space_params(space));
288 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
289 if (!scop->context || !scop->stmts)
290 return pet_scop_free(scop);
292 scop->loc = &pet_loc_dummy;
293 scop->n_stmt = n;
295 return scop;
298 /* Construct a pet_scop in the given space containing 0 statements.
300 struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
302 return scop_alloc(space, 0);
305 /* Given either an iteration domain or a wrapped map with
306 * the iteration domain in the domain and some arguments
307 * in the range, return the iteration domain.
308 * That is, drop the arguments if there are any.
310 static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
312 if (isl_set_is_wrapping(domain))
313 domain = isl_map_domain(isl_set_unwrap(domain));
314 return domain;
317 /* Update "context" with the constraints imposed on the outer iteration
318 * domain by access expression "expr".
319 * "context" lives in an anonymous space, while the domain of the access
320 * relation of "expr" refers to a particular statement.
321 * This reference therefore needs to be stripped off.
323 static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
324 __isl_take isl_set *context)
326 isl_multi_pw_aff *mpa;
327 isl_set *domain;
329 mpa = pet_expr_access_get_index(expr);
330 domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
331 domain = isl_set_reset_tuple_id(domain);
332 context = isl_set_intersect(context, domain);
333 return context;
336 /* Update "context" with the constraints imposed on the outer iteration
337 * domain by "expr".
339 * "context" lives in an anonymous space, while the domains of
340 * the access relations in "expr" refer to a particular statement.
341 * This reference therefore needs to be stripped off.
343 * If "expr" represents a conditional operator, then a parameter or outer
344 * iterator value needs to be valid for the condition and
345 * for at least one of the remaining two arguments.
346 * If the condition is an affine expression, then we can be a bit more specific.
347 * The value then has to be valid for the second argument for
348 * non-zero accesses and valid for the third argument for zero accesses.
350 * If "expr" represents a kill statement, then its argument is the entire
351 * extent of the array being killed. Do not update "context" based
352 * on this argument as that would impose constraints that ensure that
353 * the array is non-empty.
355 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
356 __isl_take isl_set *context)
358 int i;
360 if (expr->type == pet_expr_op && expr->op == pet_op_kill)
361 return context;
363 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
364 int is_aff;
365 isl_set *context1, *context2;
367 is_aff = pet_expr_is_affine(expr->args[0]);
368 if (is_aff < 0)
369 goto error;
371 context = expr_extract_context(expr->args[0], context);
372 context1 = expr_extract_context(expr->args[1],
373 isl_set_copy(context));
374 context2 = expr_extract_context(expr->args[2], context);
376 if (is_aff) {
377 isl_multi_pw_aff *mpa;
378 isl_pw_aff *pa;
379 isl_set *zero_set;
381 mpa = pet_expr_access_get_index(expr->args[0]);
382 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
383 isl_multi_pw_aff_free(mpa);
384 zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
385 zero_set = isl_set_reset_tuple_id(zero_set);
386 context1 = isl_set_subtract(context1,
387 isl_set_copy(zero_set));
388 context2 = isl_set_intersect(context2, zero_set);
391 context = isl_set_union(context1, context2);
392 context = isl_set_coalesce(context);
394 return context;
397 for (i = 0; i < expr->n_arg; ++i)
398 context = expr_extract_context(expr->args[i], context);
400 if (expr->type == pet_expr_access)
401 context = access_extract_context(expr, context);
403 return context;
404 error:
405 isl_set_free(context);
406 return NULL;
409 /* Is "stmt" an assume statement with an affine assumption?
411 int pet_stmt_is_affine_assume(struct pet_stmt *stmt)
413 if (!stmt)
414 return 0;
415 return pet_tree_is_affine_assume(stmt->body);
418 /* Given an assume statement "stmt" with an access argument,
419 * return the index expression of the argument.
421 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
423 if (!stmt)
424 return NULL;
425 return pet_tree_assume_get_index(stmt->body);
428 /* Update "context" with the constraints imposed on the outer iteration
429 * domain by "stmt".
431 * If the statement is an assume statement with an affine expression,
432 * then intersect "context" with that expression.
433 * Otherwise, if the statement body is an expression tree,
434 * then intersect "context" with the context of this expression.
435 * Note that we cannot safely extract a context from subtrees
436 * of the statement body since we cannot tell when those subtrees
437 * are executed, if at all.
439 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
440 __isl_take isl_set *context)
442 int i;
443 pet_expr *body;
445 if (pet_stmt_is_affine_assume(stmt)) {
446 isl_multi_pw_aff *index;
447 isl_pw_aff *pa;
448 isl_set *cond;
450 index = pet_stmt_assume_get_index(stmt);
451 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
452 isl_multi_pw_aff_free(index);
453 cond = isl_pw_aff_non_zero_set(pa);
454 cond = isl_set_reset_tuple_id(cond);
455 return isl_set_intersect(context, cond);
458 for (i = 0; i < stmt->n_arg; ++i)
459 context = expr_extract_context(stmt->args[i], context);
461 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
462 return context;
464 body = pet_tree_expr_get_expr(stmt->body);
465 context = expr_extract_context(body, context);
466 pet_expr_free(body);
468 return context;
471 /* Construct a pet_scop in the given space that contains the given pet_stmt.
473 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
474 struct pet_stmt *stmt)
476 struct pet_scop *scop;
478 if (!stmt)
479 space = isl_space_free(space);
481 scop = scop_alloc(space, 1);
482 if (!scop)
483 goto error;
485 scop->context = stmt_extract_context(stmt, scop->context);
486 if (!scop->context)
487 goto error;
489 scop->stmts[0] = stmt;
490 scop->loc = pet_loc_copy(stmt->loc);
492 if (!scop->loc)
493 return pet_scop_free(scop);
495 return scop;
496 error:
497 pet_stmt_free(stmt);
498 pet_scop_free(scop);
499 return NULL;
502 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
503 * does it represent an affine expression?
505 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
507 int has_id;
509 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
510 if (has_id < 0)
511 return -1;
513 return !has_id;
516 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
518 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
519 __isl_take isl_set *dom)
521 isl_pw_aff *pa;
522 pa = isl_set_indicator_function(set);
523 pa = isl_pw_aff_intersect_domain(pa, dom);
524 return pa;
527 /* Return "lhs || rhs", defined on the shared definition domain.
529 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
530 __isl_take isl_pw_aff *rhs)
532 isl_set *cond;
533 isl_set *dom;
535 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
536 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
537 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
538 isl_pw_aff_non_zero_set(rhs));
539 cond = isl_set_coalesce(cond);
540 return indicator_function(cond, dom);
543 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
544 * ext may be equal to either ext1 or ext2.
546 * The two skips that need to be combined are assumed to be affine expressions.
548 * We need to skip in ext if we need to skip in either ext1 or ext2.
549 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
551 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
552 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
553 enum pet_skip type)
555 isl_pw_aff *skip, *skip1, *skip2;
557 if (!ext)
558 return NULL;
559 if (!ext1->skip[type] && !ext2->skip[type])
560 return ext;
561 if (!ext1->skip[type]) {
562 if (ext == ext2)
563 return ext;
564 ext->skip[type] = ext2->skip[type];
565 ext2->skip[type] = NULL;
566 return ext;
568 if (!ext2->skip[type]) {
569 if (ext == ext1)
570 return ext;
571 ext->skip[type] = ext1->skip[type];
572 ext1->skip[type] = NULL;
573 return ext;
576 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
577 !multi_pw_aff_is_affine(ext2->skip[type]))
578 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
579 isl_error_internal, "can only combine affine skips",
580 goto error);
582 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
583 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
584 skip = pw_aff_or(skip1, skip2);
585 isl_multi_pw_aff_free(ext1->skip[type]);
586 ext1->skip[type] = NULL;
587 isl_multi_pw_aff_free(ext2->skip[type]);
588 ext2->skip[type] = NULL;
589 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
590 if (!ext->skip[type])
591 goto error;
593 return ext;
594 error:
595 pet_scop_free(&ext->scop);
596 return NULL;
599 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
600 * where type takes on the values pet_skip_now and pet_skip_later.
601 * scop may be equal to either scop1 or scop2.
603 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
604 struct pet_scop *scop1, struct pet_scop *scop2)
606 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
607 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
608 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
610 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
611 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
612 return &ext->scop;
615 /* Update start and end of scop->loc to include the region from "start"
616 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
617 * does not have any offset information yet and we simply take the information
618 * from "start" and "end". Otherwise, we update loc using "start" and "end".
620 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
621 unsigned start, unsigned end)
623 if (!scop)
624 return NULL;
626 if (scop->loc == &pet_loc_dummy)
627 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
628 start, end, -1, strdup(""));
629 else
630 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
632 if (!scop->loc)
633 return pet_scop_free(scop);
635 return scop;
638 /* Update start and end of scop->loc to include the region identified
639 * by "loc".
641 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
642 __isl_keep pet_loc *loc)
644 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
645 pet_loc_get_end(loc));
648 /* Replace the location of "scop" by "loc".
650 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
651 __isl_take pet_loc *loc)
653 if (!scop || !loc)
654 goto error;
656 pet_loc_free(scop->loc);
657 scop->loc = loc;
659 return scop;
660 error:
661 pet_loc_free(loc);
662 pet_scop_free(scop);
663 return NULL;
666 /* Does "implication" appear in the list of implications of "scop"?
668 static int is_known_implication(struct pet_scop *scop,
669 struct pet_implication *implication)
671 int i;
673 for (i = 0; i < scop->n_implication; ++i) {
674 struct pet_implication *pi = scop->implications[i];
675 int equal;
677 if (pi->satisfied != implication->satisfied)
678 continue;
679 equal = isl_map_is_equal(pi->extension, implication->extension);
680 if (equal < 0)
681 return -1;
682 if (equal)
683 return 1;
686 return 0;
689 /* Store the concatenation of the implications of "scop1" and "scop2"
690 * in "scop", removing duplicates (i.e., implications in "scop2" that
691 * already appear in "scop1").
693 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
694 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
696 int i, j;
698 if (!scop)
699 return NULL;
701 if (scop2->n_implication == 0) {
702 scop->n_implication = scop1->n_implication;
703 scop->implications = scop1->implications;
704 scop1->n_implication = 0;
705 scop1->implications = NULL;
706 return scop;
709 if (scop1->n_implication == 0) {
710 scop->n_implication = scop2->n_implication;
711 scop->implications = scop2->implications;
712 scop2->n_implication = 0;
713 scop2->implications = NULL;
714 return scop;
717 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
718 scop1->n_implication + scop2->n_implication);
719 if (!scop->implications)
720 return pet_scop_free(scop);
722 for (i = 0; i < scop1->n_implication; ++i) {
723 scop->implications[i] = scop1->implications[i];
724 scop1->implications[i] = NULL;
727 scop->n_implication = scop1->n_implication;
728 j = scop1->n_implication;
729 for (i = 0; i < scop2->n_implication; ++i) {
730 int known;
732 known = is_known_implication(scop, scop2->implications[i]);
733 if (known < 0)
734 return pet_scop_free(scop);
735 if (known)
736 continue;
737 scop->implications[j++] = scop2->implications[i];
738 scop2->implications[i] = NULL;
740 scop->n_implication = j;
742 return scop;
745 /* Combine the offset information of "scop1" and "scop2" into "scop".
747 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
748 struct pet_scop *scop1, struct pet_scop *scop2)
750 if (scop1->loc != &pet_loc_dummy)
751 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
752 if (scop2->loc != &pet_loc_dummy)
753 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
754 return scop;
757 /* Create and return an independence that filters out the dependences
758 * in "filter" with local variables "local".
760 static struct pet_independence *new_independence(
761 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
763 isl_ctx *ctx;
764 struct pet_independence *independence;
766 if (!filter || !local)
767 goto error;
768 ctx = isl_union_map_get_ctx(filter);
769 independence = isl_alloc_type(ctx, struct pet_independence);
770 if (!independence)
771 goto error;
773 independence->filter = filter;
774 independence->local = local;
776 return independence;
777 error:
778 isl_union_map_free(filter);
779 isl_union_set_free(local);
780 return NULL;
783 /* Add an independence that filters out the dependences
784 * in "filter" with local variables "local" to "scop".
786 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
787 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
789 isl_ctx *ctx;
790 struct pet_independence *independence;
791 struct pet_independence **independences;
793 ctx = isl_union_map_get_ctx(filter);
794 independence = new_independence(filter, local);
795 if (!scop || !independence)
796 goto error;
798 independences = isl_realloc_array(ctx, scop->independences,
799 struct pet_independence *,
800 scop->n_independence + 1);
801 if (!independences)
802 goto error;
803 scop->independences = independences;
804 scop->independences[scop->n_independence] = independence;
805 scop->n_independence++;
807 return scop;
808 error:
809 pet_independence_free(independence);
810 pet_scop_free(scop);
811 return NULL;
814 /* Store the concatenation of the independences of "scop1" and "scop2"
815 * in "scop".
817 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
818 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
820 int i, off;
822 if (!scop)
823 return NULL;
825 if (scop2->n_independence == 0) {
826 scop->n_independence = scop1->n_independence;
827 scop->independences = scop1->independences;
828 scop1->n_independence = 0;
829 scop1->independences = NULL;
830 return scop;
833 if (scop1->n_independence == 0) {
834 scop->n_independence = scop2->n_independence;
835 scop->independences = scop2->independences;
836 scop2->n_independence = 0;
837 scop2->independences = NULL;
838 return scop;
841 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
842 scop1->n_independence + scop2->n_independence);
843 if (!scop->independences)
844 return pet_scop_free(scop);
846 for (i = 0; i < scop1->n_independence; ++i) {
847 scop->independences[i] = scop1->independences[i];
848 scop1->independences[i] = NULL;
851 off = scop1->n_independence;
852 for (i = 0; i < scop2->n_independence; ++i) {
853 scop->independences[off + i] = scop2->independences[i];
854 scop2->independences[i] = NULL;
856 scop->n_independence = scop1->n_independence + scop2->n_independence;
858 return scop;
861 /* Construct a pet_scop that contains the offset information,
862 * arrays, statements and skip information in "scop1" and "scop2".
864 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
865 struct pet_scop *scop2)
867 int i;
868 isl_space *space;
869 struct pet_scop *scop = NULL;
871 if (!scop1 || !scop2)
872 goto error;
874 if (scop1->n_stmt == 0) {
875 scop2 = scop_combine_skips(scop2, scop1, scop2);
876 pet_scop_free(scop1);
877 return scop2;
880 if (scop2->n_stmt == 0) {
881 scop1 = scop_combine_skips(scop1, scop1, scop2);
882 pet_scop_free(scop2);
883 return scop1;
886 space = isl_set_get_space(scop1->context);
887 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt);
888 if (!scop)
889 goto error;
891 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
892 scop1->n_array + scop2->n_array);
893 if (!scop->arrays)
894 goto error;
895 scop->n_array = scop1->n_array + scop2->n_array;
897 for (i = 0; i < scop1->n_stmt; ++i) {
898 scop->stmts[i] = scop1->stmts[i];
899 scop1->stmts[i] = NULL;
902 for (i = 0; i < scop2->n_stmt; ++i) {
903 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
904 scop2->stmts[i] = NULL;
907 for (i = 0; i < scop1->n_array; ++i) {
908 scop->arrays[i] = scop1->arrays[i];
909 scop1->arrays[i] = NULL;
912 for (i = 0; i < scop2->n_array; ++i) {
913 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
914 scop2->arrays[i] = NULL;
917 scop = scop_collect_implications(ctx, scop, scop1, scop2);
918 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
919 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
920 scop = scop_combine_skips(scop, scop1, scop2);
921 scop = scop_combine_start_end(scop, scop1, scop2);
922 scop = scop_collect_independences(ctx, scop, scop1, scop2);
924 pet_scop_free(scop1);
925 pet_scop_free(scop2);
926 return scop;
927 error:
928 pet_scop_free(scop1);
929 pet_scop_free(scop2);
930 pet_scop_free(scop);
931 return NULL;
934 /* Apply the skip condition "skip" to "scop".
935 * That is, make sure "scop" is not executed when the condition holds.
937 * If "skip" is an affine expression, we add the conditions under
938 * which the expression is zero to the context and the skip conditions
939 * of "scop".
940 * Otherwise, we add a filter on the variable attaining the value zero.
942 static struct pet_scop *restrict_skip(struct pet_scop *scop,
943 __isl_take isl_multi_pw_aff *skip)
945 isl_set *zero;
946 isl_pw_aff *pa;
947 int is_aff;
949 if (!scop || !skip)
950 goto error;
952 is_aff = multi_pw_aff_is_affine(skip);
953 if (is_aff < 0)
954 goto error;
956 if (!is_aff)
957 return pet_scop_filter(scop, skip, 0);
959 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
960 isl_multi_pw_aff_free(skip);
961 zero = isl_pw_aff_zero_set(pa);
962 scop = pet_scop_restrict(scop, zero);
964 return scop;
965 error:
966 isl_multi_pw_aff_free(skip);
967 return pet_scop_free(scop);
970 /* Construct a pet_scop that contains the arrays, statements and
971 * skip information in "scop1" and "scop2", where the two scops
972 * are executed "in sequence". That is, breaks and continues
973 * in scop1 have an effect on scop2.
975 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
976 struct pet_scop *scop2)
978 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
979 scop2 = restrict_skip(scop2,
980 pet_scop_get_skip(scop1, pet_skip_now));
981 return pet_scop_add(ctx, scop1, scop2);
984 /* Construct a pet_scop that contains the arrays, statements and
985 * skip information in "scop1" and "scop2", where the two scops
986 * are executed "in parallel". That is, any break or continue
987 * in scop1 has no effect on scop2.
989 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
990 struct pet_scop *scop2)
992 return pet_scop_add(ctx, scop1, scop2);
995 void *pet_implication_free(struct pet_implication *implication)
997 int i;
999 if (!implication)
1000 return NULL;
1002 isl_map_free(implication->extension);
1004 free(implication);
1005 return NULL;
1008 void *pet_independence_free(struct pet_independence *independence)
1010 if (!independence)
1011 return NULL;
1013 isl_union_map_free(independence->filter);
1014 isl_union_set_free(independence->local);
1016 free(independence);
1017 return NULL;
1020 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1022 int i;
1023 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1025 if (!scop)
1026 return NULL;
1027 pet_loc_free(scop->loc);
1028 isl_set_free(scop->context);
1029 isl_set_free(scop->context_value);
1030 if (scop->types)
1031 for (i = 0; i < scop->n_type; ++i)
1032 pet_type_free(scop->types[i]);
1033 free(scop->types);
1034 if (scop->arrays)
1035 for (i = 0; i < scop->n_array; ++i)
1036 pet_array_free(scop->arrays[i]);
1037 free(scop->arrays);
1038 if (scop->stmts)
1039 for (i = 0; i < scop->n_stmt; ++i)
1040 pet_stmt_free(scop->stmts[i]);
1041 free(scop->stmts);
1042 if (scop->implications)
1043 for (i = 0; i < scop->n_implication; ++i)
1044 pet_implication_free(scop->implications[i]);
1045 free(scop->implications);
1046 if (scop->independences)
1047 for (i = 0; i < scop->n_independence; ++i)
1048 pet_independence_free(scop->independences[i]);
1049 free(scop->independences);
1050 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1051 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1052 free(scop);
1053 return NULL;
1056 void pet_type_dump(struct pet_type *type)
1058 if (!type)
1059 return;
1061 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1064 void pet_implication_dump(struct pet_implication *implication)
1066 if (!implication)
1067 return;
1069 fprintf(stderr, "%d\n", implication->satisfied);
1070 isl_map_dump(implication->extension);
1073 void pet_scop_dump(struct pet_scop *scop)
1075 int i;
1076 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1078 if (!scop)
1079 return;
1081 isl_set_dump(scop->context);
1082 isl_set_dump(scop->context_value);
1083 for (i = 0; i < scop->n_type; ++i)
1084 pet_type_dump(scop->types[i]);
1085 for (i = 0; i < scop->n_array; ++i)
1086 pet_array_dump(scop->arrays[i]);
1087 for (i = 0; i < scop->n_stmt; ++i)
1088 pet_stmt_dump(scop->stmts[i]);
1089 for (i = 0; i < scop->n_implication; ++i)
1090 pet_implication_dump(scop->implications[i]);
1092 if (ext->skip[0]) {
1093 fprintf(stderr, "skip\n");
1094 isl_multi_pw_aff_dump(ext->skip[0]);
1095 isl_multi_pw_aff_dump(ext->skip[1]);
1099 /* Return 1 if the two pet_arrays are equivalent.
1101 * We don't compare element_size as this may be target dependent.
1103 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1105 if (!array1 || !array2)
1106 return 0;
1108 if (!isl_set_is_equal(array1->context, array2->context))
1109 return 0;
1110 if (!isl_set_is_equal(array1->extent, array2->extent))
1111 return 0;
1112 if (!!array1->value_bounds != !!array2->value_bounds)
1113 return 0;
1114 if (array1->value_bounds &&
1115 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1116 return 0;
1117 if (strcmp(array1->element_type, array2->element_type))
1118 return 0;
1119 if (array1->element_is_record != array2->element_is_record)
1120 return 0;
1121 if (array1->live_out != array2->live_out)
1122 return 0;
1123 if (array1->uniquely_defined != array2->uniquely_defined)
1124 return 0;
1125 if (array1->declared != array2->declared)
1126 return 0;
1127 if (array1->exposed != array2->exposed)
1128 return 0;
1130 return 1;
1133 /* Return 1 if the two pet_stmts are equivalent.
1135 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1137 int i;
1139 if (!stmt1 || !stmt2)
1140 return 0;
1142 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1143 return 0;
1144 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1145 return 0;
1146 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1147 return 0;
1148 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1149 return 0;
1150 if (stmt1->n_arg != stmt2->n_arg)
1151 return 0;
1152 for (i = 0; i < stmt1->n_arg; ++i) {
1153 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1154 return 0;
1157 return 1;
1160 /* Return 1 if the two pet_types are equivalent.
1162 * We only compare the names of the types since the exact representation
1163 * of the definition may depend on the version of clang being used.
1165 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1167 if (!type1 || !type2)
1168 return 0;
1170 if (strcmp(type1->name, type2->name))
1171 return 0;
1173 return 1;
1176 /* Return 1 if the two pet_implications are equivalent.
1178 int pet_implication_is_equal(struct pet_implication *implication1,
1179 struct pet_implication *implication2)
1181 if (!implication1 || !implication2)
1182 return 0;
1184 if (implication1->satisfied != implication2->satisfied)
1185 return 0;
1186 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1187 return 0;
1189 return 1;
1192 /* Return 1 if the two pet_independences are equivalent.
1194 int pet_independence_is_equal(struct pet_independence *independence1,
1195 struct pet_independence *independence2)
1197 if (!independence1 || !independence2)
1198 return 0;
1200 if (!isl_union_map_is_equal(independence1->filter,
1201 independence2->filter))
1202 return 0;
1203 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1204 return 0;
1206 return 1;
1209 /* Return 1 if the two pet_scops are equivalent.
1211 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1213 int i;
1215 if (!scop1 || !scop2)
1216 return 0;
1218 if (!isl_set_is_equal(scop1->context, scop2->context))
1219 return 0;
1220 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1221 return 0;
1223 if (scop1->n_type != scop2->n_type)
1224 return 0;
1225 for (i = 0; i < scop1->n_type; ++i)
1226 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1227 return 0;
1229 if (scop1->n_array != scop2->n_array)
1230 return 0;
1231 for (i = 0; i < scop1->n_array; ++i)
1232 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1233 return 0;
1235 if (scop1->n_stmt != scop2->n_stmt)
1236 return 0;
1237 for (i = 0; i < scop1->n_stmt; ++i)
1238 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1239 return 0;
1241 if (scop1->n_implication != scop2->n_implication)
1242 return 0;
1243 for (i = 0; i < scop1->n_implication; ++i)
1244 if (!pet_implication_is_equal(scop1->implications[i],
1245 scop2->implications[i]))
1246 return 0;
1248 if (scop1->n_independence != scop2->n_independence)
1249 return 0;
1250 for (i = 0; i < scop1->n_independence; ++i)
1251 if (!pet_independence_is_equal(scop1->independences[i],
1252 scop2->independences[i]))
1253 return 0;
1255 return 1;
1258 /* Does the set "extent" reference a virtual array, i.e.,
1259 * one with user pointer equal to NULL?
1260 * A virtual array does not have any members.
1262 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1264 isl_id *id;
1265 int is_virtual;
1267 if (!isl_set_has_tuple_id(extent))
1268 return 0;
1269 if (isl_set_is_wrapping(extent))
1270 return 0;
1271 id = isl_set_get_tuple_id(extent);
1272 is_virtual = !isl_id_get_user(id);
1273 isl_id_free(id);
1275 return is_virtual;
1278 /* Intersect the initial dimensions of "array" with "domain", provided
1279 * that "array" represents a virtual array.
1281 * If "array" is virtual, then We take the preimage of "domain"
1282 * over the projection of the extent of "array" onto its initial dimensions
1283 * and intersect this extent with the result.
1285 static struct pet_array *virtual_array_intersect_domain_prefix(
1286 struct pet_array *array, __isl_take isl_set *domain)
1288 int n;
1289 isl_space *space;
1290 isl_multi_aff *ma;
1292 if (!array || !extent_is_virtual_array(array->extent)) {
1293 isl_set_free(domain);
1294 return array;
1297 space = isl_set_get_space(array->extent);
1298 n = isl_set_dim(domain, isl_dim_set);
1299 ma = pet_prefix_projection(space, n);
1300 domain = isl_set_preimage_multi_aff(domain, ma);
1302 array->extent = isl_set_intersect(array->extent, domain);
1303 if (!array->extent)
1304 return pet_array_free(array);
1306 return array;
1309 /* Intersect the initial dimensions of the domain of "stmt"
1310 * with "domain".
1312 * We take the preimage of "domain" over the projection of the
1313 * domain of "stmt" onto its initial dimensions and intersect
1314 * the domain of "stmt" with the result.
1316 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1317 __isl_take isl_set *domain)
1319 int n;
1320 isl_space *space;
1321 isl_multi_aff *ma;
1323 if (!stmt)
1324 goto error;
1326 space = isl_set_get_space(stmt->domain);
1327 n = isl_set_dim(domain, isl_dim_set);
1328 ma = pet_prefix_projection(space, n);
1329 domain = isl_set_preimage_multi_aff(domain, ma);
1331 stmt->domain = isl_set_intersect(stmt->domain, domain);
1332 if (!stmt->domain)
1333 return pet_stmt_free(stmt);
1335 return stmt;
1336 error:
1337 isl_set_free(domain);
1338 return pet_stmt_free(stmt);
1341 /* Intersect the initial dimensions of the domain of "implication"
1342 * with "domain".
1344 * We take the preimage of "domain" over the projection of the
1345 * domain of "implication" onto its initial dimensions and intersect
1346 * the domain of "implication" with the result.
1348 static struct pet_implication *implication_intersect_domain_prefix(
1349 struct pet_implication *implication, __isl_take isl_set *domain)
1351 int n;
1352 isl_space *space;
1353 isl_multi_aff *ma;
1355 if (!implication)
1356 goto error;
1358 space = isl_map_get_space(implication->extension);
1359 n = isl_set_dim(domain, isl_dim_set);
1360 ma = pet_prefix_projection(isl_space_domain(space), n);
1361 domain = isl_set_preimage_multi_aff(domain, ma);
1363 implication->extension =
1364 isl_map_intersect_domain(implication->extension, domain);
1365 if (!implication->extension)
1366 return pet_implication_free(implication);
1368 return implication;
1369 error:
1370 isl_set_free(domain);
1371 return pet_implication_free(implication);
1374 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1376 * The extents of the virtual arrays match the iteration domains,
1377 * so if the iteration domain changes, we need to change those extents too.
1379 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1380 __isl_take isl_set *domain)
1382 int i;
1384 if (!scop)
1385 goto error;
1387 for (i = 0; i < scop->n_array; ++i) {
1388 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1389 scop->arrays[i], isl_set_copy(domain));
1390 if (!scop->arrays[i])
1391 goto error;
1394 for (i = 0; i < scop->n_stmt; ++i) {
1395 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1396 isl_set_copy(domain));
1397 if (!scop->stmts[i])
1398 goto error;
1401 for (i = 0; i < scop->n_implication; ++i) {
1402 scop->implications[i] =
1403 implication_intersect_domain_prefix(scop->implications[i],
1404 isl_set_copy(domain));
1405 if (!scop->implications[i])
1406 return pet_scop_free(scop);
1409 isl_set_free(domain);
1410 return scop;
1411 error:
1412 isl_set_free(domain);
1413 return pet_scop_free(scop);
1416 /* Prefix the schedule of "stmt" with an extra dimension with constant
1417 * value "pos".
1419 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1421 if (!stmt)
1422 return NULL;
1424 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1425 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1426 if (!stmt->schedule)
1427 return pet_stmt_free(stmt);
1429 return stmt;
1432 /* Prefix the schedules of all statements in "scop" with an extra
1433 * dimension with constant value "pos".
1435 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1437 int i;
1439 if (!scop)
1440 return NULL;
1442 for (i = 0; i < scop->n_stmt; ++i) {
1443 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1444 if (!scop->stmts[i])
1445 return pet_scop_free(scop);
1448 return scop;
1451 /* Prefix the schedule of "stmt" with "sched".
1453 * The domain of "sched" refers the current outer loop iterators and
1454 * needs to be mapped to the iteration domain of "stmt" first
1455 * before being prepended to the schedule of "stmt".
1457 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1458 __isl_take isl_map *sched)
1460 int n;
1461 isl_space *space;
1462 isl_multi_aff *ma;
1464 if (!stmt)
1465 goto error;
1467 space = pet_stmt_get_space(stmt);
1468 n = isl_map_dim(sched, isl_dim_in);
1469 ma = pet_prefix_projection(space, n);
1470 sched = isl_map_preimage_domain_multi_aff(sched, ma);
1471 stmt->schedule = isl_map_flat_range_product(sched, stmt->schedule);
1472 if (!stmt->schedule)
1473 return pet_stmt_free(stmt);
1475 return stmt;
1476 error:
1477 isl_map_free(sched);
1478 return NULL;
1481 /* Update the context with respect to an embedding into a loop
1482 * with iteration domain "dom".
1483 * The input context lives in the same space as "dom".
1484 * The output context has the inner dimension removed.
1486 * An outer loop iterator value is invalid for the embedding if
1487 * any of the corresponding inner iterator values is invalid.
1488 * That is, an outer loop iterator value is valid only if all the corresponding
1489 * inner iterator values are valid.
1490 * We therefore compute the set of outer loop iterators l
1492 * forall i: dom(l,i) => valid(l,i)
1494 * or
1496 * forall i: not dom(l,i) or valid(l,i)
1498 * or
1500 * not exists i: dom(l,i) and not valid(l,i)
1502 * i.e.,
1504 * not exists i: (dom \ valid)(l,i)
1506 * If there are any unnamed parameters in "dom", then we consider
1507 * a parameter value to be valid if it is valid for any value of those
1508 * unnamed parameters. They are therefore projected out at the end.
1510 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1511 __isl_keep isl_set *dom)
1513 int pos;
1515 pos = isl_set_dim(context, isl_dim_set) - 1;
1516 context = isl_set_subtract(isl_set_copy(dom), context);
1517 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1518 context = isl_set_complement(context);
1519 context = pet_nested_remove_from_set(context);
1521 return context;
1524 /* Update the implication with respect to an embedding into a loop
1525 * with iteration domain "dom".
1527 * Since embed_access extends virtual arrays along with the domain
1528 * of the access, we need to do the same with domain and range
1529 * of the implication. Since the original implication is only valid
1530 * within a given iteration of the loop, the extended implication
1531 * maps the extra array dimension corresponding to the extra loop
1532 * to itself.
1534 static struct pet_implication *pet_implication_embed(
1535 struct pet_implication *implication, __isl_take isl_set *dom)
1537 isl_id *id;
1538 isl_map *map;
1540 if (!implication)
1541 goto error;
1543 map = isl_set_identity(dom);
1544 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1545 map = isl_map_flat_product(map, implication->extension);
1546 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1547 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1548 implication->extension = map;
1549 if (!implication->extension)
1550 return pet_implication_free(implication);
1552 return implication;
1553 error:
1554 isl_set_free(dom);
1555 return NULL;
1558 /* Adjust the context and statement schedules according to an embedding
1559 * in a loop with iteration domain "dom" and schedule "sched".
1561 * Any skip conditions within the loop have no effect outside of the loop.
1562 * The caller is responsible for making sure skip[pet_skip_later] has been
1563 * taken into account.
1565 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1566 __isl_take isl_aff *sched)
1568 int i;
1569 isl_map *sched_map;
1571 sched_map = isl_map_from_aff(sched);
1573 if (!scop)
1574 goto error;
1576 pet_scop_reset_skip(scop, pet_skip_now);
1577 pet_scop_reset_skip(scop, pet_skip_later);
1579 scop->context = context_embed(scop->context, dom);
1580 if (!scop->context)
1581 goto error;
1583 for (i = 0; i < scop->n_stmt; ++i) {
1584 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1585 isl_map_copy(sched_map));
1586 if (!scop->stmts[i])
1587 goto error;
1590 isl_set_free(dom);
1591 isl_map_free(sched_map);
1592 return scop;
1593 error:
1594 isl_set_free(dom);
1595 isl_map_free(sched_map);
1596 return pet_scop_free(scop);
1599 /* Add extra conditions to scop->skip[type].
1601 * The new skip condition only holds if it held before
1602 * and the condition is true. It does not hold if it did not hold
1603 * before or the condition is false.
1605 * The skip condition is assumed to be an affine expression.
1607 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1608 enum pet_skip type, __isl_keep isl_set *cond)
1610 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1611 isl_pw_aff *skip;
1612 isl_set *dom;
1614 if (!scop)
1615 return NULL;
1616 if (!ext->skip[type])
1617 return scop;
1619 if (!multi_pw_aff_is_affine(ext->skip[type]))
1620 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1621 isl_error_internal, "can only restrict affine skips",
1622 return pet_scop_free(scop));
1624 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1625 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1626 cond = isl_set_copy(cond);
1627 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1628 skip = indicator_function(cond, dom);
1629 isl_multi_pw_aff_free(ext->skip[type]);
1630 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1631 if (!ext->skip[type])
1632 return pet_scop_free(scop);
1634 return scop;
1637 /* Adjust the context and the skip conditions to the fact that
1638 * the scop was created in a context where "cond" holds.
1640 * An outer loop iterator or parameter value is valid for the result
1641 * if it was valid for the original scop and satisfies "cond" or if it does
1642 * not satisfy "cond" as in this case the scop is not executed
1643 * and the original constraints on these values are irrelevant.
1645 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1646 __isl_take isl_set *cond)
1648 int i;
1650 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1651 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1653 if (!scop)
1654 goto error;
1656 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1657 scop->context = isl_set_union(scop->context,
1658 isl_set_complement(isl_set_copy(cond)));
1659 scop->context = isl_set_coalesce(scop->context);
1660 scop->context = pet_nested_remove_from_set(scop->context);
1661 if (!scop->context)
1662 goto error;
1664 isl_set_free(cond);
1665 return scop;
1666 error:
1667 isl_set_free(cond);
1668 return pet_scop_free(scop);
1671 /* Insert an argument expression corresponding to "test" in front
1672 * of the list of arguments described by *n_arg and *args.
1674 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1675 __isl_keep isl_multi_pw_aff *test)
1677 int i;
1678 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1680 if (!test)
1681 return -1;
1683 if (!*args) {
1684 *args = isl_calloc_array(ctx, pet_expr *, 1);
1685 if (!*args)
1686 return -1;
1687 } else {
1688 pet_expr **ext;
1689 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1690 if (!ext)
1691 return -1;
1692 for (i = 0; i < *n_arg; ++i)
1693 ext[1 + i] = (*args)[i];
1694 free(*args);
1695 *args = ext;
1697 (*n_arg)++;
1698 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1699 if (!(*args)[0])
1700 return -1;
1702 return 0;
1705 /* Look through the applications in "scop" for any that can be
1706 * applied to the filter expressed by "map" and "satisified".
1707 * If there is any, then apply it to "map" and return the result.
1708 * Otherwise, return "map".
1709 * "id" is the identifier of the virtual array.
1711 * We only introduce at most one implication for any given virtual array,
1712 * so we can apply the implication and return as soon as we find one.
1714 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1715 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1717 int i;
1719 for (i = 0; i < scop->n_implication; ++i) {
1720 struct pet_implication *pi = scop->implications[i];
1721 isl_id *pi_id;
1723 if (pi->satisfied != satisfied)
1724 continue;
1725 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1726 isl_id_free(pi_id);
1727 if (pi_id != id)
1728 continue;
1730 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1733 return map;
1736 /* Is the filter expressed by "test" and "satisfied" implied
1737 * by filter "pos" on "domain", with filter "expr", taking into
1738 * account the implications of "scop"?
1740 * For filter on domain implying that expressed by "test" and "satisfied",
1741 * the filter needs to be an access to the same (virtual) array as "test" and
1742 * the filter value needs to be equal to "satisfied".
1743 * Moreover, the filter access relation, possibly extended by
1744 * the implications in "scop" needs to contain "test".
1746 static int implies_filter(struct pet_scop *scop,
1747 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1748 __isl_keep isl_map *test, int satisfied)
1750 isl_id *test_id, *arg_id;
1751 isl_val *val;
1752 int is_int;
1753 int s;
1754 int is_subset;
1755 isl_map *implied;
1757 if (expr->type != pet_expr_access)
1758 return 0;
1759 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1760 arg_id = pet_expr_access_get_id(expr);
1761 isl_id_free(arg_id);
1762 isl_id_free(test_id);
1763 if (test_id != arg_id)
1764 return 0;
1765 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1766 is_int = isl_val_is_int(val);
1767 if (is_int)
1768 s = isl_val_get_num_si(val);
1769 isl_val_free(val);
1770 if (!val)
1771 return -1;
1772 if (!is_int)
1773 return 0;
1774 if (s != satisfied)
1775 return 0;
1777 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1778 implied = apply_implications(scop, implied, test_id, satisfied);
1779 is_subset = isl_map_is_subset(test, implied);
1780 isl_map_free(implied);
1782 return is_subset;
1785 /* Is the filter expressed by "test" and "satisfied" implied
1786 * by any of the filters on the domain of "stmt", taking into
1787 * account the implications of "scop"?
1789 static int filter_implied(struct pet_scop *scop,
1790 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1792 int i;
1793 int implied;
1794 isl_id *test_id;
1795 isl_map *domain;
1796 isl_map *test_map;
1798 if (!scop || !stmt || !test)
1799 return -1;
1800 if (scop->n_implication == 0)
1801 return 0;
1802 if (stmt->n_arg == 0)
1803 return 0;
1805 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1806 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1808 implied = 0;
1809 for (i = 0; i < stmt->n_arg; ++i) {
1810 implied = implies_filter(scop, domain, i, stmt->args[i],
1811 test_map, satisfied);
1812 if (implied < 0 || implied)
1813 break;
1816 isl_map_free(test_map);
1817 isl_map_free(domain);
1818 return implied;
1821 /* Make the statement "stmt" depend on the value of "test"
1822 * being equal to "satisfied" by adjusting stmt->domain.
1824 * The domain of "test" corresponds to the (zero or more) outer dimensions
1825 * of the iteration domain.
1827 * We first extend "test" to apply to the entire iteration domain and
1828 * then check if the filter that we are about to add is implied
1829 * by any of the current filters, possibly taking into account
1830 * the implications in "scop". If so, we leave "stmt" untouched and return.
1832 * Otherwise, we insert an argument corresponding to a read to "test"
1833 * from the iteration domain of "stmt" in front of the list of arguments.
1834 * We also insert a corresponding output dimension in the wrapped
1835 * map contained in stmt->domain, with value set to "satisfied".
1837 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1838 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1840 int i;
1841 int implied;
1842 isl_id *id;
1843 isl_ctx *ctx;
1844 isl_pw_multi_aff *pma;
1845 isl_multi_aff *add_dom;
1846 isl_space *space;
1847 isl_local_space *ls;
1848 int n_test_dom;
1850 if (!stmt || !test)
1851 goto error;
1853 space = pet_stmt_get_space(stmt);
1854 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1855 space = isl_space_from_domain(space);
1856 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1857 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1858 ls = isl_local_space_from_space(isl_space_domain(space));
1859 for (i = 0; i < n_test_dom; ++i) {
1860 isl_aff *aff;
1861 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1862 isl_dim_set, i);
1863 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1865 isl_local_space_free(ls);
1866 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1868 implied = filter_implied(scop, stmt, test, satisfied);
1869 if (implied < 0)
1870 goto error;
1871 if (implied) {
1872 isl_multi_pw_aff_free(test);
1873 return stmt;
1876 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1877 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1878 id, satisfied);
1879 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1881 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1882 goto error;
1884 isl_multi_pw_aff_free(test);
1885 return stmt;
1886 error:
1887 isl_multi_pw_aff_free(test);
1888 return pet_stmt_free(stmt);
1891 /* Does "scop" have a skip condition of the given "type"?
1893 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1895 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1897 if (!scop)
1898 return -1;
1899 return ext->skip[type] != NULL;
1902 /* Does "scop" have a skip condition of the given "type" that
1903 * is an affine expression?
1905 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1907 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1909 if (!scop)
1910 return -1;
1911 if (!ext->skip[type])
1912 return 0;
1913 return multi_pw_aff_is_affine(ext->skip[type]);
1916 /* Does "scop" have a skip condition of the given "type" that
1917 * is not an affine expression?
1919 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1921 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1922 int aff;
1924 if (!scop)
1925 return -1;
1926 if (!ext->skip[type])
1927 return 0;
1928 aff = multi_pw_aff_is_affine(ext->skip[type]);
1929 if (aff < 0)
1930 return -1;
1931 return !aff;
1934 /* Does "scop" have a skip condition of the given "type" that
1935 * is affine and holds on the entire domain?
1937 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1939 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1940 isl_pw_aff *pa;
1941 isl_set *set;
1942 int is_aff;
1943 int is_univ;
1945 is_aff = pet_scop_has_affine_skip(scop, type);
1946 if (is_aff < 0 || !is_aff)
1947 return is_aff;
1949 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1950 set = isl_pw_aff_non_zero_set(pa);
1951 is_univ = isl_set_plain_is_universe(set);
1952 isl_set_free(set);
1954 return is_univ;
1957 /* Replace scop->skip[type] by "skip".
1959 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1960 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
1962 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1964 if (!scop || !skip)
1965 goto error;
1967 isl_multi_pw_aff_free(ext->skip[type]);
1968 ext->skip[type] = skip;
1970 return scop;
1971 error:
1972 isl_multi_pw_aff_free(skip);
1973 return pet_scop_free(scop);
1976 /* Return a copy of scop->skip[type].
1978 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
1979 enum pet_skip type)
1981 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1983 if (!scop)
1984 return NULL;
1986 return isl_multi_pw_aff_copy(ext->skip[type]);
1989 /* Assuming scop->skip[type] is an affine expression,
1990 * return the constraints on the outer loop domain for which the skip condition
1991 * holds.
1993 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
1994 enum pet_skip type)
1996 isl_multi_pw_aff *skip;
1997 isl_pw_aff *pa;
1999 skip = pet_scop_get_skip(scop, type);
2000 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2001 isl_multi_pw_aff_free(skip);
2002 return isl_pw_aff_non_zero_set(pa);
2005 /* Return the identifier of the variable that is accessed by
2006 * the skip condition of the given type.
2008 * The skip condition is assumed not to be an affine condition.
2010 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2011 enum pet_skip type)
2013 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2015 if (!scop)
2016 return NULL;
2018 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2021 /* Return an access pet_expr corresponding to the skip condition
2022 * of the given type.
2024 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2025 enum pet_skip type)
2027 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2030 /* Drop the the skip condition scop->skip[type].
2032 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2034 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2036 if (!scop)
2037 return;
2039 isl_multi_pw_aff_free(ext->skip[type]);
2040 ext->skip[type] = NULL;
2043 /* Make the skip condition (if any) depend on the value of "test" being
2044 * equal to "satisfied".
2046 * We only support the case where the original skip condition is universal,
2047 * i.e., where skipping is unconditional, and where satisfied == 1.
2048 * In this case, the skip condition is changed to skip only when
2049 * "test" is equal to one.
2051 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2052 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2054 int is_univ = 0;
2056 if (!scop)
2057 return NULL;
2058 if (!pet_scop_has_skip(scop, type))
2059 return scop;
2061 if (satisfied)
2062 is_univ = pet_scop_has_universal_skip(scop, type);
2063 if (is_univ < 0)
2064 return pet_scop_free(scop);
2065 if (satisfied && is_univ) {
2066 isl_multi_pw_aff *skip;
2067 skip = isl_multi_pw_aff_copy(test);
2068 scop = pet_scop_set_skip(scop, type, skip);
2069 if (!scop)
2070 return NULL;
2071 } else {
2072 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2073 "skip expression cannot be filtered",
2074 return pet_scop_free(scop));
2077 return scop;
2080 /* Make all statements in "scop" depend on the value of "test"
2081 * being equal to "satisfied" by adjusting their domains.
2083 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2084 __isl_take isl_multi_pw_aff *test, int satisfied)
2086 int i;
2088 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2089 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2091 if (!scop || !test)
2092 goto error;
2094 for (i = 0; i < scop->n_stmt; ++i) {
2095 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2096 isl_multi_pw_aff_copy(test), satisfied);
2097 if (!scop->stmts[i])
2098 goto error;
2101 isl_multi_pw_aff_free(test);
2102 return scop;
2103 error:
2104 isl_multi_pw_aff_free(test);
2105 return pet_scop_free(scop);
2108 /* Add the parameters of the access expression "expr" to "space".
2110 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2112 int i;
2113 isl_space *expr_space;
2114 isl_space **space = user;
2116 expr_space = pet_expr_access_get_parameter_space(expr);
2117 *space = isl_space_align_params(*space, expr_space);
2119 return *space ? 0 : -1;
2122 /* Add all parameters in "stmt" to "space" and return the result.
2124 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2125 __isl_take isl_space *space)
2127 int i;
2129 if (!stmt)
2130 return isl_space_free(space);
2132 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2133 space = isl_space_align_params(space,
2134 isl_map_get_space(stmt->schedule));
2135 for (i = 0; i < stmt->n_arg; ++i)
2136 if (pet_expr_foreach_access_expr(stmt->args[i],
2137 &access_collect_params, &space) < 0)
2138 space = isl_space_free(space);
2139 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2140 &space) < 0)
2141 space = isl_space_free(space);
2143 return space;
2146 /* Add all parameters in "array" to "space" and return the result.
2148 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2149 __isl_take isl_space *space)
2151 if (!array)
2152 return isl_space_free(space);
2154 space = isl_space_align_params(space,
2155 isl_set_get_space(array->context));
2156 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2158 return space;
2161 /* Add all parameters in "independence" to "space" and return the result.
2163 static __isl_give isl_space *independence_collect_params(
2164 struct pet_independence *independence, __isl_take isl_space *space)
2166 if (!independence)
2167 return isl_space_free(space);
2169 space = isl_space_align_params(space,
2170 isl_union_map_get_space(independence->filter));
2171 space = isl_space_align_params(space,
2172 isl_union_set_get_space(independence->local));
2174 return space;
2177 /* Add all parameters in "scop" to "space" and return the result.
2179 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2180 __isl_take isl_space *space)
2182 int i;
2184 if (!scop)
2185 return isl_space_free(space);
2187 for (i = 0; i < scop->n_array; ++i)
2188 space = array_collect_params(scop->arrays[i], space);
2190 for (i = 0; i < scop->n_stmt; ++i)
2191 space = stmt_collect_params(scop->stmts[i], space);
2193 for (i = 0; i < scop->n_independence; ++i)
2194 space = independence_collect_params(scop->independences[i],
2195 space);
2197 return space;
2200 /* Add all parameters in "space" to the domain, schedule and
2201 * all access relations in "stmt".
2203 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2204 __isl_take isl_space *space)
2206 int i;
2208 if (!stmt)
2209 goto error;
2211 stmt->domain = isl_set_align_params(stmt->domain,
2212 isl_space_copy(space));
2213 stmt->schedule = isl_map_align_params(stmt->schedule,
2214 isl_space_copy(space));
2216 for (i = 0; i < stmt->n_arg; ++i) {
2217 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2218 isl_space_copy(space));
2219 if (!stmt->args[i])
2220 goto error;
2222 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2224 if (!stmt->domain || !stmt->schedule || !stmt->body)
2225 goto error;
2227 isl_space_free(space);
2228 return stmt;
2229 error:
2230 isl_space_free(space);
2231 return pet_stmt_free(stmt);
2234 /* Add all parameters in "space" to "array".
2236 static struct pet_array *array_propagate_params(struct pet_array *array,
2237 __isl_take isl_space *space)
2239 if (!array)
2240 goto error;
2242 array->context = isl_set_align_params(array->context,
2243 isl_space_copy(space));
2244 array->extent = isl_set_align_params(array->extent,
2245 isl_space_copy(space));
2246 if (array->value_bounds) {
2247 array->value_bounds = isl_set_align_params(array->value_bounds,
2248 isl_space_copy(space));
2249 if (!array->value_bounds)
2250 goto error;
2253 if (!array->context || !array->extent)
2254 goto error;
2256 isl_space_free(space);
2257 return array;
2258 error:
2259 isl_space_free(space);
2260 return pet_array_free(array);
2263 /* Add all parameters in "space" to "independence".
2265 static struct pet_independence *independence_propagate_params(
2266 struct pet_independence *independence, __isl_take isl_space *space)
2268 if (!independence)
2269 goto error;
2271 independence->filter = isl_union_map_align_params(independence->filter,
2272 isl_space_copy(space));
2273 independence->local = isl_union_set_align_params(independence->local,
2274 isl_space_copy(space));
2275 if (!independence->filter || !independence->local)
2276 goto error;
2278 isl_space_free(space);
2279 return independence;
2280 error:
2281 isl_space_free(space);
2282 return pet_independence_free(independence);
2285 /* Add all parameters in "space" to "scop".
2287 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2288 __isl_take isl_space *space)
2290 int i;
2292 if (!scop)
2293 goto error;
2295 for (i = 0; i < scop->n_array; ++i) {
2296 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2297 isl_space_copy(space));
2298 if (!scop->arrays[i])
2299 goto error;
2302 for (i = 0; i < scop->n_stmt; ++i) {
2303 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2304 isl_space_copy(space));
2305 if (!scop->stmts[i])
2306 goto error;
2309 for (i = 0; i < scop->n_independence; ++i) {
2310 scop->independences[i] = independence_propagate_params(
2311 scop->independences[i], isl_space_copy(space));
2312 if (!scop->independences[i])
2313 goto error;
2316 isl_space_free(space);
2317 return scop;
2318 error:
2319 isl_space_free(space);
2320 return pet_scop_free(scop);
2323 /* Update all isl_sets and isl_maps in "scop" such that they all
2324 * have the same parameters.
2326 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2328 isl_space *space;
2330 if (!scop)
2331 return NULL;
2333 space = isl_set_get_space(scop->context);
2334 space = scop_collect_params(scop, space);
2336 scop->context = isl_set_align_params(scop->context,
2337 isl_space_copy(space));
2338 scop = scop_propagate_params(scop, space);
2340 if (scop && !scop->context)
2341 return pet_scop_free(scop);
2343 return scop;
2346 /* Add the access relation of the give "type" of the access expression "expr"
2347 * to "accesses" and return the result.
2348 * The domain of the access relation is intersected with "domain".
2349 * If "tag" is set, then the access relation is tagged with
2350 * the corresponding reference identifier.
2352 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2353 enum pet_expr_access_type type, int tag,
2354 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2356 isl_union_map *access;
2358 access = pet_expr_access_get_access(expr, type);
2359 access = isl_union_map_intersect_domain(access,
2360 isl_union_set_copy(domain));
2361 if (tag)
2362 access = pet_expr_tag_access(expr, access);
2363 return isl_union_map_union(accesses, access);
2366 /* Internal data structure for expr_collect_accesses.
2368 * "type" is the type of accesses we want to collect.
2369 * "tag" is set if the access relations should be tagged with
2370 * the corresponding reference identifiers.
2371 * "domain" are constraints on the domain of the access relations.
2372 * "accesses" collects the results.
2374 struct pet_expr_collect_accesses_data {
2375 enum pet_expr_access_type type;
2376 int tag;
2377 isl_union_set *domain;
2379 isl_union_map *accesses;
2382 /* Add the access relation of the access expression "expr"
2383 * to data->accesses if the access expression is a read and we are collecting
2384 * reads and/or it is a write and we are collecting writes.
2385 * The domains of the access relations are intersected with data->domain.
2386 * If data->tag is set, then the access relations are tagged with
2387 * the corresponding reference identifiers.
2389 * If data->type is pet_expr_access_must_write, then we only add
2390 * the accesses that are definitely performed. Otherwise, we add
2391 * all potential accesses.
2392 * In particular, if the access has any arguments, then in case of
2393 * pet_expr_access_must_write we currently skip the access completely.
2394 * In other cases, we project out the values of the access arguments.
2396 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2398 struct pet_expr_collect_accesses_data *data = user;
2399 int i;
2400 isl_id *id;
2401 isl_space *dim;
2403 if (!expr)
2404 return -1;
2406 if (pet_expr_is_affine(expr))
2407 return 0;
2408 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2409 return 0;
2411 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2412 ((data->type == pet_expr_access_may_write ||
2413 data->type == pet_expr_access_must_write) && expr->acc.write))
2414 data->accesses = expr_collect_access(expr,
2415 data->type, data->tag,
2416 data->accesses, data->domain);
2418 return data->accesses ? 0 : -1;
2421 /* Collect and return all access relations of the given "type" in "stmt".
2422 * If "tag" is set, then the access relations are tagged with
2423 * the corresponding reference identifiers.
2424 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2425 * we simply add the argument of the kill operation.
2427 * If we are looking for definite accesses (pet_expr_access_must_write
2428 * or pet_expr_access_killed), then we only add the accesses that are
2429 * definitely performed. Otherwise, we add all potential accesses.
2430 * In particular, if the statement has any arguments, then if we are looking
2431 * for definite accesses we currently skip the statement completely. Othewise,
2432 * we project out the values of the statement arguments.
2433 * If the statement body is not an expression tree, then we cannot
2434 * know for sure if/when the accesses inside the tree are performed.
2435 * We therefore ignore such statements when we are looking for
2436 * definite accesses.
2438 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2439 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2441 struct pet_expr_collect_accesses_data data = { type, tag };
2442 int must;
2443 isl_set *domain;
2445 if (!stmt)
2446 return NULL;
2448 data.accesses = isl_union_map_empty(dim);
2450 if (type == pet_expr_access_must_write ||
2451 type == pet_expr_access_killed)
2452 must = 1;
2453 else
2454 must = 0;
2456 if (must && stmt->n_arg > 0)
2457 return data.accesses;
2458 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2459 return data.accesses;
2461 domain = drop_arguments(isl_set_copy(stmt->domain));
2462 data.domain = isl_union_set_from_set(domain);
2464 if (type == pet_expr_access_killed) {
2465 pet_expr *body, *arg;
2467 body = pet_tree_expr_get_expr(stmt->body);
2468 arg = pet_expr_get_arg(body, 0);
2469 data.accesses = expr_collect_access(arg,
2470 pet_expr_access_killed, tag,
2471 data.accesses, data.domain);
2472 pet_expr_free(arg);
2473 pet_expr_free(body);
2474 } else if (pet_tree_foreach_access_expr(stmt->body,
2475 &expr_collect_accesses, &data) < 0)
2476 data.accesses = isl_union_map_free(data.accesses);
2478 isl_union_set_free(data.domain);
2480 return data.accesses;
2483 /* Is "stmt" an assignment statement?
2485 int pet_stmt_is_assign(struct pet_stmt *stmt)
2487 if (!stmt)
2488 return 0;
2489 return pet_tree_is_assign(stmt->body);
2492 /* Is "stmt" a kill statement?
2494 int pet_stmt_is_kill(struct pet_stmt *stmt)
2496 if (!stmt)
2497 return 0;
2498 return pet_tree_is_kill(stmt->body);
2501 /* Is "stmt" an assume statement?
2503 int pet_stmt_is_assume(struct pet_stmt *stmt)
2505 if (!stmt)
2506 return 0;
2507 return pet_tree_is_assume(stmt->body);
2510 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2512 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2513 __isl_keep isl_map *map, __isl_keep isl_set *set)
2515 isl_map *gist;
2517 gist = isl_map_copy(map);
2518 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2519 return isl_union_map_add_map(umap, gist);
2522 /* Compute a mapping from all arrays (of structs) in scop
2523 * to their members.
2525 * If "from_outermost" is set, then the domain only consists
2526 * of outermost arrays.
2527 * If "to_innermost" is set, then the range only consists
2528 * of innermost arrays.
2530 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2531 int from_outermost, int to_innermost)
2533 int i;
2534 isl_union_map *to_inner;
2536 if (!scop)
2537 return NULL;
2539 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2541 for (i = 0; i < scop->n_array; ++i) {
2542 struct pet_array *array = scop->arrays[i];
2543 isl_set *set;
2544 isl_map *map;
2546 if (to_innermost && array->element_is_record)
2547 continue;
2549 set = isl_set_copy(array->extent);
2550 map = isl_set_identity(isl_set_copy(set));
2552 while (set && isl_set_is_wrapping(set)) {
2553 isl_id *id;
2554 isl_map *wrapped;
2556 if (!from_outermost)
2557 to_inner = add_gisted(to_inner, map, set);
2559 id = isl_set_get_tuple_id(set);
2560 wrapped = isl_set_unwrap(set);
2561 wrapped = isl_map_domain_map(wrapped);
2562 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2563 map = isl_map_apply_domain(map, wrapped);
2564 set = isl_map_domain(isl_map_copy(map));
2567 map = isl_map_gist_domain(map, set);
2568 to_inner = isl_union_map_add_map(to_inner, map);
2571 return to_inner;
2574 /* Compute a mapping from all arrays (of structs) in scop
2575 * to their innermost arrays.
2577 * In particular, for each array of a primitive type, the result
2578 * contains the identity mapping on that array.
2579 * For each array involving member accesses, the result
2580 * contains a mapping from the elements of any intermediate array of structs
2581 * to all corresponding elements of the innermost nested arrays.
2583 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2584 struct pet_scop *scop)
2586 return compute_to_inner(scop, 0, 1);
2589 /* Compute a mapping from all outermost arrays (of structs) in scop
2590 * to their innermost members.
2592 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2594 return compute_to_inner(scop, 1, 1);
2597 /* Compute a mapping from all outermost arrays (of structs) in scop
2598 * to their members, including the outermost arrays themselves.
2600 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2602 return compute_to_inner(scop, 1, 0);
2605 /* Collect and return all access relations of the given "type" in "scop".
2606 * If "type" is pet_expr_access_killed, then we only add the arguments of
2607 * kill operations.
2608 * If we are looking for definite accesses (pet_expr_access_must_write
2609 * or pet_expr_access_killed), then we only add the accesses that are
2610 * definitely performed. Otherwise, we add all potential accesses.
2611 * If "tag" is set, then the access relations are tagged with
2612 * the corresponding reference identifiers.
2613 * For accesses to structures, the returned access relation accesses
2614 * all individual fields in the structures.
2616 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2617 enum pet_expr_access_type type, int tag)
2619 int i;
2620 isl_union_map *accesses;
2621 isl_union_set *arrays;
2622 isl_union_map *to_inner;
2624 if (!scop)
2625 return NULL;
2627 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2629 for (i = 0; i < scop->n_stmt; ++i) {
2630 struct pet_stmt *stmt = scop->stmts[i];
2631 isl_union_map *accesses_i;
2632 isl_space *space;
2634 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2635 continue;
2637 space = isl_set_get_space(scop->context);
2638 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2639 accesses = isl_union_map_union(accesses, accesses_i);
2642 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2643 for (i = 0; i < scop->n_array; ++i) {
2644 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2645 arrays = isl_union_set_add_set(arrays, extent);
2647 accesses = isl_union_map_intersect_range(accesses, arrays);
2649 to_inner = pet_scop_compute_any_to_inner(scop);
2650 accesses = isl_union_map_apply_range(accesses, to_inner);
2652 return accesses;
2655 /* Collect all potential read access relations.
2657 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2659 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2662 /* Collect all potential write access relations.
2664 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2666 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2669 /* Collect all definite write access relations.
2671 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2673 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2676 /* Collect all definite kill access relations.
2678 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2680 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2683 /* Collect all tagged potential read access relations.
2685 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2686 struct pet_scop *scop)
2688 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2691 /* Collect all tagged potential write access relations.
2693 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2694 struct pet_scop *scop)
2696 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2699 /* Collect all tagged definite write access relations.
2701 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2702 struct pet_scop *scop)
2704 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2707 /* Collect all tagged definite kill access relations.
2709 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2710 struct pet_scop *scop)
2712 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2715 /* Collect and return the union of iteration domains in "scop".
2717 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2719 int i;
2720 isl_set *domain_i;
2721 isl_union_set *domain;
2723 if (!scop)
2724 return NULL;
2726 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2728 for (i = 0; i < scop->n_stmt; ++i) {
2729 domain_i = isl_set_copy(scop->stmts[i]->domain);
2730 domain = isl_union_set_add_set(domain, domain_i);
2733 return domain;
2736 /* Collect and return the schedules of the statements in "scop".
2737 * The range is normalized to the maximal number of scheduling
2738 * dimensions.
2740 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2742 int i, j;
2743 isl_map *schedule_i;
2744 isl_union_map *schedule;
2745 int depth, max_depth = 0;
2747 if (!scop)
2748 return NULL;
2750 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2752 for (i = 0; i < scop->n_stmt; ++i) {
2753 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2754 if (depth > max_depth)
2755 max_depth = depth;
2758 for (i = 0; i < scop->n_stmt; ++i) {
2759 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2760 depth = isl_map_dim(schedule_i, isl_dim_out);
2761 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2762 max_depth - depth);
2763 for (j = depth; j < max_depth; ++j)
2764 schedule_i = isl_map_fix_si(schedule_i,
2765 isl_dim_out, j, 0);
2766 schedule = isl_union_map_add_map(schedule, schedule_i);
2769 return schedule;
2772 /* Add a reference identifier to all access expressions in "stmt".
2773 * "n_ref" points to an integer that contains the sequence number
2774 * of the next reference.
2776 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2778 int i;
2780 if (!stmt)
2781 return NULL;
2783 for (i = 0; i < stmt->n_arg; ++i) {
2784 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2785 if (!stmt->args[i])
2786 return pet_stmt_free(stmt);
2789 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2790 if (!stmt->body)
2791 return pet_stmt_free(stmt);
2793 return stmt;
2796 /* Add a reference identifier to all access expressions in "scop".
2798 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2800 int i;
2801 int n_ref;
2803 if (!scop)
2804 return NULL;
2806 n_ref = 0;
2807 for (i = 0; i < scop->n_stmt; ++i) {
2808 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2809 if (!scop->stmts[i])
2810 return pet_scop_free(scop);
2813 return scop;
2816 /* Reset the user pointer on all parameter ids in "array".
2818 static struct pet_array *array_anonymize(struct pet_array *array)
2820 if (!array)
2821 return NULL;
2823 array->context = isl_set_reset_user(array->context);
2824 array->extent = isl_set_reset_user(array->extent);
2825 if (!array->context || !array->extent)
2826 return pet_array_free(array);
2828 return array;
2831 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2833 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2835 int i;
2836 isl_space *space;
2837 isl_set *domain;
2839 if (!stmt)
2840 return NULL;
2842 stmt->domain = isl_set_reset_user(stmt->domain);
2843 stmt->schedule = isl_map_reset_user(stmt->schedule);
2844 if (!stmt->domain || !stmt->schedule)
2845 return pet_stmt_free(stmt);
2847 for (i = 0; i < stmt->n_arg; ++i) {
2848 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2849 if (!stmt->args[i])
2850 return pet_stmt_free(stmt);
2853 stmt->body = pet_tree_anonymize(stmt->body);
2854 if (!stmt->body)
2855 return pet_stmt_free(stmt);
2857 return stmt;
2860 /* Reset the user pointer on the tuple ids and all parameter ids
2861 * in "implication".
2863 static struct pet_implication *implication_anonymize(
2864 struct pet_implication *implication)
2866 if (!implication)
2867 return NULL;
2869 implication->extension = isl_map_reset_user(implication->extension);
2870 if (!implication->extension)
2871 return pet_implication_free(implication);
2873 return implication;
2876 /* Reset the user pointer on the tuple ids and all parameter ids
2877 * in "independence".
2879 static struct pet_independence *independence_anonymize(
2880 struct pet_independence *independence)
2882 if (!independence)
2883 return NULL;
2885 independence->filter = isl_union_map_reset_user(independence->filter);
2886 independence->local = isl_union_set_reset_user(independence->local);
2887 if (!independence->filter || !independence->local)
2888 return pet_independence_free(independence);
2890 return independence;
2893 /* Reset the user pointer on all parameter and tuple ids in "scop".
2895 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2897 int i;
2899 if (!scop)
2900 return NULL;
2902 scop->context = isl_set_reset_user(scop->context);
2903 scop->context_value = isl_set_reset_user(scop->context_value);
2904 if (!scop->context || !scop->context_value)
2905 return pet_scop_free(scop);
2907 for (i = 0; i < scop->n_array; ++i) {
2908 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2909 if (!scop->arrays[i])
2910 return pet_scop_free(scop);
2913 for (i = 0; i < scop->n_stmt; ++i) {
2914 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2915 if (!scop->stmts[i])
2916 return pet_scop_free(scop);
2919 for (i = 0; i < scop->n_implication; ++i) {
2920 scop->implications[i] =
2921 implication_anonymize(scop->implications[i]);
2922 if (!scop->implications[i])
2923 return pet_scop_free(scop);
2926 for (i = 0; i < scop->n_independence; ++i) {
2927 scop->independences[i] =
2928 independence_anonymize(scop->independences[i]);
2929 if (!scop->independences[i])
2930 return pet_scop_free(scop);
2933 return scop;
2936 /* Compute the gist of the iteration domain and all access relations
2937 * of "stmt" based on the constraints on the parameters specified by "context"
2938 * and the constraints on the values of nested accesses specified
2939 * by "value_bounds".
2941 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2942 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2944 int i;
2945 isl_set *domain;
2947 if (!stmt)
2948 return NULL;
2950 domain = isl_set_copy(stmt->domain);
2951 if (stmt->n_arg > 0)
2952 domain = isl_map_domain(isl_set_unwrap(domain));
2954 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2956 for (i = 0; i < stmt->n_arg; ++i) {
2957 stmt->args[i] = pet_expr_gist(stmt->args[i],
2958 domain, value_bounds);
2959 if (!stmt->args[i])
2960 goto error;
2963 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
2964 if (!stmt->body)
2965 goto error;
2967 isl_set_free(domain);
2969 domain = isl_set_universe(pet_stmt_get_space(stmt));
2970 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2971 if (stmt->n_arg > 0)
2972 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
2973 value_bounds);
2974 stmt->domain = isl_set_gist(stmt->domain, domain);
2975 if (!stmt->domain)
2976 return pet_stmt_free(stmt);
2978 return stmt;
2979 error:
2980 isl_set_free(domain);
2981 return pet_stmt_free(stmt);
2984 /* Compute the gist of the extent of the array
2985 * based on the constraints on the parameters specified by "context".
2987 static struct pet_array *array_gist(struct pet_array *array,
2988 __isl_keep isl_set *context)
2990 if (!array)
2991 return NULL;
2993 array->extent = isl_set_gist_params(array->extent,
2994 isl_set_copy(context));
2995 if (!array->extent)
2996 return pet_array_free(array);
2998 return array;
3001 /* Compute the gist of all sets and relations in "scop"
3002 * based on the constraints on the parameters specified by "scop->context"
3003 * and the constraints on the values of nested accesses specified
3004 * by "value_bounds".
3006 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3007 __isl_keep isl_union_map *value_bounds)
3009 int i;
3011 if (!scop)
3012 return NULL;
3014 scop->context = isl_set_coalesce(scop->context);
3015 if (!scop->context)
3016 return pet_scop_free(scop);
3018 for (i = 0; i < scop->n_array; ++i) {
3019 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3020 if (!scop->arrays[i])
3021 return pet_scop_free(scop);
3024 for (i = 0; i < scop->n_stmt; ++i) {
3025 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3026 value_bounds);
3027 if (!scop->stmts[i])
3028 return pet_scop_free(scop);
3031 return scop;
3034 /* Intersect the context of "scop" with "context".
3035 * To ensure that we don't introduce any unnamed parameters in
3036 * the context of "scop", we first remove the unnamed parameters
3037 * from "context".
3039 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3040 __isl_take isl_set *context)
3042 if (!scop)
3043 goto error;
3045 context = pet_nested_remove_from_set(context);
3046 scop->context = isl_set_intersect(scop->context, context);
3047 if (!scop->context)
3048 return pet_scop_free(scop);
3050 return scop;
3051 error:
3052 isl_set_free(context);
3053 return pet_scop_free(scop);
3056 /* Drop the current context of "scop". That is, replace the context
3057 * by a universal set.
3059 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3061 isl_space *space;
3063 if (!scop)
3064 return NULL;
3066 space = isl_set_get_space(scop->context);
3067 isl_set_free(scop->context);
3068 scop->context = isl_set_universe(space);
3069 if (!scop->context)
3070 return pet_scop_free(scop);
3072 return scop;
3075 /* Append "array" to the arrays of "scop".
3077 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3078 struct pet_array *array)
3080 isl_ctx *ctx;
3081 struct pet_array **arrays;
3083 if (!array || !scop)
3084 goto error;
3086 ctx = isl_set_get_ctx(scop->context);
3087 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3088 scop->n_array + 1);
3089 if (!arrays)
3090 goto error;
3091 scop->arrays = arrays;
3092 scop->arrays[scop->n_array] = array;
3093 scop->n_array++;
3095 return scop;
3096 error:
3097 pet_array_free(array);
3098 return pet_scop_free(scop);
3101 /* Create an index expression for an access to a virtual array
3102 * representing the result of a condition.
3103 * Unlike other accessed data, the id of the array is NULL as
3104 * there is no ValueDecl in the program corresponding to the virtual
3105 * array.
3106 * The index expression is created as an identity mapping on "space".
3107 * That is, the dimension of the array is the same as that of "space".
3109 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3110 int test_nr)
3112 isl_id *id;
3113 char name[50];
3115 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3116 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3117 space = isl_space_map_from_set(space);
3118 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3119 return isl_multi_pw_aff_identity(space);
3122 /* Add an array with the given extent to the list
3123 * of arrays in "scop" and return the extended pet_scop.
3124 * Specifically, the extent is determined by the image of "domain"
3125 * under "index".
3126 * "int_size" is the number of bytes needed to represent values of type "int".
3127 * The array is marked as attaining values 0 and 1 only and
3128 * as each element being assigned at most once.
3130 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3131 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3132 int int_size)
3134 isl_ctx *ctx;
3135 isl_space *space;
3136 struct pet_array *array;
3137 isl_map *access;
3139 if (!scop || !domain || !index)
3140 goto error;
3142 ctx = isl_multi_pw_aff_get_ctx(index);
3143 array = isl_calloc_type(ctx, struct pet_array);
3144 if (!array)
3145 goto error;
3147 access = isl_map_from_multi_pw_aff(index);
3148 access = isl_map_intersect_domain(access, domain);
3149 array->extent = isl_map_range(access);
3150 space = isl_space_params_alloc(ctx, 0);
3151 array->context = isl_set_universe(space);
3152 space = isl_space_set_alloc(ctx, 0, 1);
3153 array->value_bounds = isl_set_universe(space);
3154 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3155 isl_dim_set, 0, 0);
3156 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3157 isl_dim_set, 0, 1);
3158 array->element_type = strdup("int");
3159 array->element_size = int_size;
3160 array->uniquely_defined = 1;
3162 if (!array->extent || !array->context)
3163 array = pet_array_free(array);
3165 scop = pet_scop_add_array(scop, array);
3167 return scop;
3168 error:
3169 isl_set_free(domain);
3170 isl_multi_pw_aff_free(index);
3171 return pet_scop_free(scop);
3174 /* Create and return an implication on filter values equal to "satisfied"
3175 * with extension "map".
3177 static struct pet_implication *new_implication(__isl_take isl_map *map,
3178 int satisfied)
3180 isl_ctx *ctx;
3181 struct pet_implication *implication;
3183 if (!map)
3184 return NULL;
3185 ctx = isl_map_get_ctx(map);
3186 implication = isl_alloc_type(ctx, struct pet_implication);
3187 if (!implication)
3188 goto error;
3190 implication->extension = map;
3191 implication->satisfied = satisfied;
3193 return implication;
3194 error:
3195 isl_map_free(map);
3196 return NULL;
3199 /* Add an implication on filter values equal to "satisfied"
3200 * with extension "map" to "scop".
3202 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3203 __isl_take isl_map *map, int satisfied)
3205 isl_ctx *ctx;
3206 struct pet_implication *implication;
3207 struct pet_implication **implications;
3209 implication = new_implication(map, satisfied);
3210 if (!scop || !implication)
3211 goto error;
3213 ctx = isl_set_get_ctx(scop->context);
3214 implications = isl_realloc_array(ctx, scop->implications,
3215 struct pet_implication *,
3216 scop->n_implication + 1);
3217 if (!implications)
3218 goto error;
3219 scop->implications = implications;
3220 scop->implications[scop->n_implication] = implication;
3221 scop->n_implication++;
3223 return scop;
3224 error:
3225 pet_implication_free(implication);
3226 return pet_scop_free(scop);
3229 /* Create and return a function that maps the iteration domains
3230 * of the statements in "scop" onto their outer "n" dimensions.
3231 * "space" is the parameters space of the created function.
3233 static __isl_give isl_union_pw_multi_aff *outer_projection(
3234 struct pet_scop *scop, __isl_take isl_space *space, int n)
3236 int i;
3237 isl_union_pw_multi_aff *res;
3239 res = isl_union_pw_multi_aff_empty(space);
3241 if (!scop)
3242 return isl_union_pw_multi_aff_free(res);
3244 for (i = 0; i < scop->n_stmt; ++i) {
3245 struct pet_stmt *stmt = scop->stmts[i];
3246 isl_space *space;
3247 isl_multi_aff *ma;
3248 isl_pw_multi_aff *pma;
3250 space = pet_stmt_get_space(stmt);
3251 ma = pet_prefix_projection(space, n);
3252 pma = isl_pw_multi_aff_from_multi_aff(ma);
3253 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3256 return res;
3259 /* Add an independence to "scop" for the inner iterator of "domain"
3260 * with local variables "local", where "domain" represents the outer
3261 * loop iterators of all statements in "scop".
3262 * If "sign" is positive, then the inner iterator increases.
3263 * Otherwise it decreases.
3265 * The independence is supposed to filter out any dependence of
3266 * an iteration of domain on a previous iteration along the inner dimension.
3267 * We therefore create a mapping from an iteration to later iterations and
3268 * then plug in the projection of the iterations domains of "scop"
3269 * onto the outer loop iterators.
3271 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3272 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3274 int i, dim;
3275 isl_space *space;
3276 isl_map *map;
3277 isl_union_map *independence;
3278 isl_union_pw_multi_aff *proj;
3280 if (!scop || !domain || !local)
3281 goto error;
3283 dim = isl_set_dim(domain, isl_dim_set);
3284 space = isl_space_map_from_set(isl_set_get_space(domain));
3285 map = isl_map_universe(space);
3286 for (i = 0; i + 1 < dim; ++i)
3287 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3288 if (sign > 0)
3289 map = isl_map_order_lt(map,
3290 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3291 else
3292 map = isl_map_order_gt(map,
3293 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3295 independence = isl_union_map_from_map(map);
3296 space = isl_space_params(isl_set_get_space(domain));
3297 proj = outer_projection(scop, space, dim);
3298 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3299 independence, isl_union_pw_multi_aff_copy(proj));
3300 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3301 independence, proj);
3303 scop = pet_scop_add_independence(scop, independence, local);
3305 return scop;
3306 error:
3307 isl_union_set_free(local);
3308 return pet_scop_free(scop);
3311 /* Given an access expression, check if it is data dependent.
3312 * If so, set *found and abort the search.
3314 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3316 int *found = user;
3318 if (pet_expr_get_n_arg(expr) > 0) {
3319 *found = 1;
3320 return -1;
3323 return 0;
3326 /* Does "scop" contain any data dependent accesses?
3328 * Check the body of each statement for such accesses.
3330 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3332 int i;
3333 int found = 0;
3335 if (!scop)
3336 return -1;
3338 for (i = 0; i < scop->n_stmt; ++i) {
3339 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3340 &is_data_dependent, &found);
3341 if (r < 0 && !found)
3342 return -1;
3343 if (found)
3344 return found;
3347 return found;
3350 /* Does "scop" contain and data dependent conditions?
3352 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3354 int i;
3356 if (!scop)
3357 return -1;
3359 for (i = 0; i < scop->n_stmt; ++i)
3360 if (scop->stmts[i]->n_arg > 0)
3361 return 1;
3363 return 0;
3366 /* Keep track of the "input" file inside the (extended) "scop".
3368 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3370 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3372 if (!scop)
3373 return NULL;
3375 ext->input = input;
3377 return scop;
3380 /* Print the original code corresponding to "scop" to printer "p".
3382 * pet_scop_print_original can only be called from
3383 * a pet_transform_C_source callback. This means that the input
3384 * file is stored in the extended scop and that the printer prints
3385 * to a file.
3387 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3388 __isl_take isl_printer *p)
3390 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3391 FILE *output;
3392 unsigned start, end;
3394 if (!scop || !p)
3395 return isl_printer_free(p);
3397 if (!ext->input)
3398 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3399 "no input file stored in scop",
3400 return isl_printer_free(p));
3402 output = isl_printer_get_file(p);
3403 if (!output)
3404 return isl_printer_free(p);
3406 start = pet_loc_get_start(scop->loc);
3407 end = pet_loc_get_end(scop->loc);
3408 if (copy(ext->input, output, start, end) < 0)
3409 return isl_printer_free(p);
3411 return p;