add encapsulate_dynamic_control option
[pet.git] / scop.c
blobf17c259b47245fb23e021d4cc415d21d0a42448d
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 "filter.h"
42 #include "loc.h"
43 #include "nest.h"
44 #include "scop.h"
45 #include "tree.h"
46 #include "print.h"
47 #include "value_bounds.h"
49 /* pet_scop with extra information that is used during parsing and printing.
51 * In particular, we keep track of conditions under which we want
52 * to skip the rest of the current loop iteration (skip[pet_skip_now])
53 * and of conditions under which we want to skip subsequent
54 * loop iterations (skip[pet_skip_later]).
56 * The conditions are represented as index expressions defined
57 * over the outer loop iterators. The index expression is either
58 * a boolean affine expression or an access to a variable, which
59 * is assumed to attain values zero and one. The condition holds
60 * if the variable has value one or if the affine expression
61 * has value one (typically for only part of the domain).
63 * A missing condition (skip[type] == NULL) means that we don't want
64 * to skip anything.
66 * Additionally, we keep track of the original input file
67 * inside pet_transform_C_source.
69 struct pet_scop_ext {
70 struct pet_scop scop;
72 isl_multi_pw_aff *skip[2];
73 FILE *input;
76 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
77 * The input domain is anonymous and is the same as the domains
78 * of the access expressions inside "tree".
79 * These domains are modified to include the name of the statement.
80 * This name is given by tree->label if it is non-NULL.
81 * Otherwise, the name is constructed as S_<id>.
83 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
84 int id, __isl_take pet_tree *tree)
86 struct pet_stmt *stmt;
87 isl_ctx *ctx;
88 isl_id *label;
89 isl_space *space;
90 isl_map *sched;
91 isl_multi_aff *ma;
92 isl_multi_pw_aff *add_name;
93 char name[50];
95 if (!domain || !tree)
96 goto error;
98 ctx = pet_tree_get_ctx(tree);
99 stmt = isl_calloc_type(ctx, struct pet_stmt);
100 if (!stmt)
101 goto error;
103 if (tree->label) {
104 label = isl_id_copy(tree->label);
105 } else {
106 snprintf(name, sizeof(name), "S_%d", id);
107 label = isl_id_alloc(ctx, name, NULL);
109 domain = isl_set_set_tuple_id(domain, label);
110 space = isl_set_get_space(domain);
111 space = pet_nested_remove_from_space(space);
112 sched = isl_map_universe(isl_space_from_domain(isl_space_copy(space)));
113 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
115 add_name = isl_multi_pw_aff_from_multi_aff(ma);
116 tree = pet_tree_update_domain(tree, add_name);
118 stmt->loc = pet_tree_get_loc(tree);
119 stmt->domain = domain;
120 stmt->schedule = sched;
121 stmt->body = tree;
123 if (!stmt->domain || !stmt->schedule || !stmt->body)
124 return pet_stmt_free(stmt);
126 return stmt;
127 error:
128 isl_set_free(domain);
129 isl_id_free(label);
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 /* Return the constraints on the iteration domain in the access relation
306 * "access".
307 * If the corresponding access expression has arguments then the domain
308 * of "access" is a wrapped relation with the iteration domain in the domain
309 * and the arguments in the range.
311 static __isl_give isl_set *access_domain(__isl_take isl_map *access)
313 isl_set *domain;
315 domain = isl_map_domain(access);
316 if (isl_set_is_wrapping(domain))
317 domain = isl_map_domain(isl_set_unwrap(domain));
319 return domain;
322 /* Update "context" with the constraints imposed on the outer iteration
323 * domain by "access".
324 * "context" lives in an anonymous space, while the domain of "access"
325 * refers to a particular statement. This reference therefore needs to be
326 * stripped off.
328 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
329 __isl_take isl_set *context)
331 isl_set *domain;
333 domain = access_domain(isl_map_copy(access));
334 domain = isl_set_reset_tuple_id(domain);
335 context = isl_set_intersect(context, domain);
336 return context;
339 /* Update "context" with the constraints imposed on the outer iteration
340 * domain by "expr".
342 * "context" lives in an anonymous space, while the domains of
343 * the access relations in "expr" refer to a particular statement.
344 * This reference therefore needs to be stripped off.
346 * If "expr" represents a conditional operator, then a parameter or outer
347 * iterator value needs to be valid for the condition and
348 * for at least one of the remaining two arguments.
349 * If the condition is an affine expression, then we can be a bit more specific.
350 * The value then has to be valid for the second argument for
351 * non-zero accesses and valid for the third argument for zero accesses.
353 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
354 __isl_take isl_set *context)
356 int i;
358 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
359 int is_aff;
360 isl_set *context1, *context2;
362 is_aff = pet_expr_is_affine(expr->args[0]);
363 if (is_aff < 0)
364 goto error;
366 context = expr_extract_context(expr->args[0], context);
367 context1 = expr_extract_context(expr->args[1],
368 isl_set_copy(context));
369 context2 = expr_extract_context(expr->args[2], context);
371 if (is_aff) {
372 isl_map *access;
373 isl_set *zero_set;
375 access = isl_map_copy(expr->args[0]->acc.access);
376 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
377 zero_set = access_domain(access);
378 zero_set = isl_set_reset_tuple_id(zero_set);
379 context1 = isl_set_subtract(context1,
380 isl_set_copy(zero_set));
381 context2 = isl_set_intersect(context2, zero_set);
384 context = isl_set_union(context1, context2);
385 context = isl_set_coalesce(context);
387 return context;
390 for (i = 0; i < expr->n_arg; ++i)
391 context = expr_extract_context(expr->args[i], context);
393 if (expr->type == pet_expr_access)
394 context = access_extract_context(expr->acc.access, context);
396 return context;
397 error:
398 isl_set_free(context);
399 return NULL;
402 /* Is "stmt" an assume statement with an affine assumption?
404 int pet_stmt_is_affine_assume(struct pet_stmt *stmt)
406 if (!stmt)
407 return 0;
408 return pet_tree_is_affine_assume(stmt->body);
411 /* Given an assume statement "stmt" with an access argument,
412 * return the index expression of the argument.
414 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
416 if (!stmt)
417 return NULL;
418 return pet_tree_assume_get_index(stmt->body);
421 /* Update "context" with the constraints imposed on the outer iteration
422 * domain by "stmt".
424 * If the statement is an assume statement with an affine expression,
425 * then intersect "context" with that expression.
426 * Otherwise, if the statement body is an expression tree,
427 * then intersect "context" with the context of this expression.
428 * Note that we cannot safely extract a context from subtrees
429 * of the statement body since we cannot tell when those subtrees
430 * are executed, if at all.
432 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
433 __isl_take isl_set *context)
435 int i;
436 pet_expr *body;
438 if (pet_stmt_is_affine_assume(stmt)) {
439 isl_multi_pw_aff *index;
440 isl_pw_aff *pa;
441 isl_set *cond;
443 index = pet_stmt_assume_get_index(stmt);
444 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
445 isl_multi_pw_aff_free(index);
446 cond = isl_pw_aff_non_zero_set(pa);
447 cond = isl_set_reset_tuple_id(cond);
448 return isl_set_intersect(context, cond);
451 for (i = 0; i < stmt->n_arg; ++i)
452 context = expr_extract_context(stmt->args[i], context);
454 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
455 return context;
457 body = pet_tree_expr_get_expr(stmt->body);
458 context = expr_extract_context(body, context);
459 pet_expr_free(body);
461 return context;
464 /* Construct a pet_scop in the given space that contains the given pet_stmt.
466 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
467 struct pet_stmt *stmt)
469 struct pet_scop *scop;
471 if (!stmt)
472 space = isl_space_free(space);
474 scop = scop_alloc(space, 1);
475 if (!scop)
476 goto error;
478 scop->context = stmt_extract_context(stmt, scop->context);
479 if (!scop->context)
480 goto error;
482 scop->stmts[0] = stmt;
483 scop->loc = pet_loc_copy(stmt->loc);
485 if (!scop->loc)
486 return pet_scop_free(scop);
488 return scop;
489 error:
490 pet_stmt_free(stmt);
491 pet_scop_free(scop);
492 return NULL;
495 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
496 * does it represent an affine expression?
498 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
500 int has_id;
502 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
503 if (has_id < 0)
504 return -1;
506 return !has_id;
509 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
511 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
512 __isl_take isl_set *dom)
514 isl_pw_aff *pa;
515 pa = isl_set_indicator_function(set);
516 pa = isl_pw_aff_intersect_domain(pa, dom);
517 return pa;
520 /* Return "lhs || rhs", defined on the shared definition domain.
522 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
523 __isl_take isl_pw_aff *rhs)
525 isl_set *cond;
526 isl_set *dom;
528 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
529 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
530 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
531 isl_pw_aff_non_zero_set(rhs));
532 cond = isl_set_coalesce(cond);
533 return indicator_function(cond, dom);
536 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
537 * ext may be equal to either ext1 or ext2.
539 * The two skips that need to be combined are assumed to be affine expressions.
541 * We need to skip in ext if we need to skip in either ext1 or ext2.
542 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
544 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
545 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
546 enum pet_skip type)
548 isl_pw_aff *skip, *skip1, *skip2;
550 if (!ext)
551 return NULL;
552 if (!ext1->skip[type] && !ext2->skip[type])
553 return ext;
554 if (!ext1->skip[type]) {
555 if (ext == ext2)
556 return ext;
557 ext->skip[type] = ext2->skip[type];
558 ext2->skip[type] = NULL;
559 return ext;
561 if (!ext2->skip[type]) {
562 if (ext == ext1)
563 return ext;
564 ext->skip[type] = ext1->skip[type];
565 ext1->skip[type] = NULL;
566 return ext;
569 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
570 !multi_pw_aff_is_affine(ext2->skip[type]))
571 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
572 isl_error_internal, "can only combine affine skips",
573 goto error);
575 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
576 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
577 skip = pw_aff_or(skip1, skip2);
578 isl_multi_pw_aff_free(ext1->skip[type]);
579 ext1->skip[type] = NULL;
580 isl_multi_pw_aff_free(ext2->skip[type]);
581 ext2->skip[type] = NULL;
582 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
583 if (!ext->skip[type])
584 goto error;
586 return ext;
587 error:
588 pet_scop_free(&ext->scop);
589 return NULL;
592 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
593 * where type takes on the values pet_skip_now and pet_skip_later.
594 * scop may be equal to either scop1 or scop2.
596 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
597 struct pet_scop *scop1, struct pet_scop *scop2)
599 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
600 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
601 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
603 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
604 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
605 return &ext->scop;
608 /* Update start and end of scop->loc to include the region from "start"
609 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
610 * does not have any offset information yet and we simply take the information
611 * from "start" and "end". Otherwise, we update loc using "start" and "end".
613 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
614 unsigned start, unsigned end)
616 if (!scop)
617 return NULL;
619 if (scop->loc == &pet_loc_dummy)
620 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
621 start, end, -1, strdup(""));
622 else
623 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
625 if (!scop->loc)
626 return pet_scop_free(scop);
628 return scop;
631 /* Update start and end of scop->loc to include the region identified
632 * by "loc".
634 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
635 __isl_keep pet_loc *loc)
637 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
638 pet_loc_get_end(loc));
641 /* Replace the location of "scop" by "loc".
643 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
644 __isl_take pet_loc *loc)
646 if (!scop || !loc)
647 goto error;
649 pet_loc_free(scop->loc);
650 scop->loc = loc;
652 return scop;
653 error:
654 pet_loc_free(loc);
655 pet_scop_free(scop);
656 return NULL;
659 /* Does "implication" appear in the list of implications of "scop"?
661 static int is_known_implication(struct pet_scop *scop,
662 struct pet_implication *implication)
664 int i;
666 for (i = 0; i < scop->n_implication; ++i) {
667 struct pet_implication *pi = scop->implications[i];
668 int equal;
670 if (pi->satisfied != implication->satisfied)
671 continue;
672 equal = isl_map_is_equal(pi->extension, implication->extension);
673 if (equal < 0)
674 return -1;
675 if (equal)
676 return 1;
679 return 0;
682 /* Store the concatenation of the implications of "scop1" and "scop2"
683 * in "scop", removing duplicates (i.e., implications in "scop2" that
684 * already appear in "scop1").
686 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
687 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
689 int i, j;
691 if (!scop)
692 return NULL;
694 if (scop2->n_implication == 0) {
695 scop->n_implication = scop1->n_implication;
696 scop->implications = scop1->implications;
697 scop1->n_implication = 0;
698 scop1->implications = NULL;
699 return scop;
702 if (scop1->n_implication == 0) {
703 scop->n_implication = scop2->n_implication;
704 scop->implications = scop2->implications;
705 scop2->n_implication = 0;
706 scop2->implications = NULL;
707 return scop;
710 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
711 scop1->n_implication + scop2->n_implication);
712 if (!scop->implications)
713 return pet_scop_free(scop);
715 for (i = 0; i < scop1->n_implication; ++i) {
716 scop->implications[i] = scop1->implications[i];
717 scop1->implications[i] = NULL;
720 scop->n_implication = scop1->n_implication;
721 j = scop1->n_implication;
722 for (i = 0; i < scop2->n_implication; ++i) {
723 int known;
725 known = is_known_implication(scop, scop2->implications[i]);
726 if (known < 0)
727 return pet_scop_free(scop);
728 if (known)
729 continue;
730 scop->implications[j++] = scop2->implications[i];
731 scop2->implications[i] = NULL;
733 scop->n_implication = j;
735 return scop;
738 /* Combine the offset information of "scop1" and "scop2" into "scop".
740 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
741 struct pet_scop *scop1, struct pet_scop *scop2)
743 if (scop1->loc != &pet_loc_dummy)
744 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
745 if (scop2->loc != &pet_loc_dummy)
746 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
747 return scop;
750 /* Construct a pet_scop that contains the offset information,
751 * arrays, statements and skip information in "scop1" and "scop2".
753 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
754 struct pet_scop *scop2)
756 int i;
757 isl_space *space;
758 struct pet_scop *scop = NULL;
760 if (!scop1 || !scop2)
761 goto error;
763 if (scop1->n_stmt == 0) {
764 scop2 = scop_combine_skips(scop2, scop1, scop2);
765 pet_scop_free(scop1);
766 return scop2;
769 if (scop2->n_stmt == 0) {
770 scop1 = scop_combine_skips(scop1, scop1, scop2);
771 pet_scop_free(scop2);
772 return scop1;
775 space = isl_set_get_space(scop1->context);
776 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt);
777 if (!scop)
778 goto error;
780 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
781 scop1->n_array + scop2->n_array);
782 if (!scop->arrays)
783 goto error;
784 scop->n_array = scop1->n_array + scop2->n_array;
786 for (i = 0; i < scop1->n_stmt; ++i) {
787 scop->stmts[i] = scop1->stmts[i];
788 scop1->stmts[i] = NULL;
791 for (i = 0; i < scop2->n_stmt; ++i) {
792 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
793 scop2->stmts[i] = NULL;
796 for (i = 0; i < scop1->n_array; ++i) {
797 scop->arrays[i] = scop1->arrays[i];
798 scop1->arrays[i] = NULL;
801 for (i = 0; i < scop2->n_array; ++i) {
802 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
803 scop2->arrays[i] = NULL;
806 scop = scop_collect_implications(ctx, scop, scop1, scop2);
807 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
808 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
809 scop = scop_combine_skips(scop, scop1, scop2);
810 scop = scop_combine_start_end(scop, scop1, scop2);
812 pet_scop_free(scop1);
813 pet_scop_free(scop2);
814 return scop;
815 error:
816 pet_scop_free(scop1);
817 pet_scop_free(scop2);
818 pet_scop_free(scop);
819 return NULL;
822 /* Apply the skip condition "skip" to "scop".
823 * That is, make sure "scop" is not executed when the condition holds.
825 * If "skip" is an affine expression, we add the conditions under
826 * which the expression is zero to the iteration domains.
827 * Otherwise, we add a filter on the variable attaining the value zero.
829 static struct pet_scop *restrict_skip(struct pet_scop *scop,
830 __isl_take isl_multi_pw_aff *skip)
832 isl_set *zero;
833 isl_pw_aff *pa;
834 int is_aff;
836 if (!scop || !skip)
837 goto error;
839 is_aff = multi_pw_aff_is_affine(skip);
840 if (is_aff < 0)
841 goto error;
843 if (!is_aff)
844 return pet_scop_filter(scop, skip, 0);
846 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
847 isl_multi_pw_aff_free(skip);
848 zero = isl_pw_aff_zero_set(pa);
849 scop = pet_scop_restrict(scop, zero);
851 return scop;
852 error:
853 isl_multi_pw_aff_free(skip);
854 return pet_scop_free(scop);
857 /* Construct a pet_scop that contains the arrays, statements and
858 * skip information in "scop1" and "scop2", where the two scops
859 * are executed "in sequence". That is, breaks and continues
860 * in scop1 have an effect on scop2.
862 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
863 struct pet_scop *scop2)
865 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
866 scop2 = restrict_skip(scop2,
867 pet_scop_get_skip(scop1, pet_skip_now));
868 return pet_scop_add(ctx, scop1, scop2);
871 /* Construct a pet_scop that contains the arrays, statements and
872 * skip information in "scop1" and "scop2", where the two scops
873 * are executed "in parallel". That is, any break or continue
874 * in scop1 has no effect on scop2.
876 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
877 struct pet_scop *scop2)
879 return pet_scop_add(ctx, scop1, scop2);
882 void *pet_implication_free(struct pet_implication *implication)
884 int i;
886 if (!implication)
887 return NULL;
889 isl_map_free(implication->extension);
891 free(implication);
892 return NULL;
895 struct pet_scop *pet_scop_free(struct pet_scop *scop)
897 int i;
898 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
900 if (!scop)
901 return NULL;
902 pet_loc_free(scop->loc);
903 isl_set_free(scop->context);
904 isl_set_free(scop->context_value);
905 if (scop->types)
906 for (i = 0; i < scop->n_type; ++i)
907 pet_type_free(scop->types[i]);
908 free(scop->types);
909 if (scop->arrays)
910 for (i = 0; i < scop->n_array; ++i)
911 pet_array_free(scop->arrays[i]);
912 free(scop->arrays);
913 if (scop->stmts)
914 for (i = 0; i < scop->n_stmt; ++i)
915 pet_stmt_free(scop->stmts[i]);
916 free(scop->stmts);
917 if (scop->implications)
918 for (i = 0; i < scop->n_implication; ++i)
919 pet_implication_free(scop->implications[i]);
920 free(scop->implications);
921 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
922 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
923 free(scop);
924 return NULL;
927 void pet_type_dump(struct pet_type *type)
929 if (!type)
930 return;
932 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
935 void pet_implication_dump(struct pet_implication *implication)
937 if (!implication)
938 return;
940 fprintf(stderr, "%d\n", implication->satisfied);
941 isl_map_dump(implication->extension);
944 void pet_scop_dump(struct pet_scop *scop)
946 int i;
947 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
949 if (!scop)
950 return;
952 isl_set_dump(scop->context);
953 isl_set_dump(scop->context_value);
954 for (i = 0; i < scop->n_type; ++i)
955 pet_type_dump(scop->types[i]);
956 for (i = 0; i < scop->n_array; ++i)
957 pet_array_dump(scop->arrays[i]);
958 for (i = 0; i < scop->n_stmt; ++i)
959 pet_stmt_dump(scop->stmts[i]);
960 for (i = 0; i < scop->n_implication; ++i)
961 pet_implication_dump(scop->implications[i]);
963 if (ext->skip[0]) {
964 fprintf(stderr, "skip\n");
965 isl_multi_pw_aff_dump(ext->skip[0]);
966 isl_multi_pw_aff_dump(ext->skip[1]);
970 /* Return 1 if the two pet_arrays are equivalent.
972 * We don't compare element_size as this may be target dependent.
974 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
976 if (!array1 || !array2)
977 return 0;
979 if (!isl_set_is_equal(array1->context, array2->context))
980 return 0;
981 if (!isl_set_is_equal(array1->extent, array2->extent))
982 return 0;
983 if (!!array1->value_bounds != !!array2->value_bounds)
984 return 0;
985 if (array1->value_bounds &&
986 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
987 return 0;
988 if (strcmp(array1->element_type, array2->element_type))
989 return 0;
990 if (array1->element_is_record != array2->element_is_record)
991 return 0;
992 if (array1->live_out != array2->live_out)
993 return 0;
994 if (array1->uniquely_defined != array2->uniquely_defined)
995 return 0;
996 if (array1->declared != array2->declared)
997 return 0;
998 if (array1->exposed != array2->exposed)
999 return 0;
1001 return 1;
1004 /* Return 1 if the two pet_stmts are equivalent.
1006 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1008 int i;
1010 if (!stmt1 || !stmt2)
1011 return 0;
1013 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1014 return 0;
1015 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1016 return 0;
1017 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1018 return 0;
1019 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1020 return 0;
1021 if (stmt1->n_arg != stmt2->n_arg)
1022 return 0;
1023 for (i = 0; i < stmt1->n_arg; ++i) {
1024 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1025 return 0;
1028 return 1;
1031 /* Return 1 if the two pet_types are equivalent.
1033 * We only compare the names of the types since the exact representation
1034 * of the definition may depend on the version of clang being used.
1036 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1038 if (!type1 || !type2)
1039 return 0;
1041 if (strcmp(type1->name, type2->name))
1042 return 0;
1044 return 1;
1047 /* Return 1 if the two pet_implications are equivalent.
1049 int pet_implication_is_equal(struct pet_implication *implication1,
1050 struct pet_implication *implication2)
1052 if (!implication1 || !implication2)
1053 return 0;
1055 if (implication1->satisfied != implication2->satisfied)
1056 return 0;
1057 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1058 return 0;
1060 return 1;
1063 /* Return 1 if the two pet_scops are equivalent.
1065 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1067 int i;
1069 if (!scop1 || !scop2)
1070 return 0;
1072 if (!isl_set_is_equal(scop1->context, scop2->context))
1073 return 0;
1074 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1075 return 0;
1077 if (scop1->n_type != scop2->n_type)
1078 return 0;
1079 for (i = 0; i < scop1->n_type; ++i)
1080 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1081 return 0;
1083 if (scop1->n_array != scop2->n_array)
1084 return 0;
1085 for (i = 0; i < scop1->n_array; ++i)
1086 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1087 return 0;
1089 if (scop1->n_stmt != scop2->n_stmt)
1090 return 0;
1091 for (i = 0; i < scop1->n_stmt; ++i)
1092 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1093 return 0;
1095 if (scop1->n_implication != scop2->n_implication)
1096 return 0;
1097 for (i = 0; i < scop1->n_implication; ++i)
1098 if (!pet_implication_is_equal(scop1->implications[i],
1099 scop2->implications[i]))
1100 return 0;
1102 return 1;
1105 /* Does the set "extent" reference a virtual array, i.e.,
1106 * one with user pointer equal to NULL?
1107 * A virtual array does not have any members.
1109 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1111 isl_id *id;
1112 int is_virtual;
1114 if (!isl_set_has_tuple_id(extent))
1115 return 0;
1116 if (isl_set_is_wrapping(extent))
1117 return 0;
1118 id = isl_set_get_tuple_id(extent);
1119 is_virtual = !isl_id_get_user(id);
1120 isl_id_free(id);
1122 return is_virtual;
1125 /* Intersect the initial dimensions of "array" with "domain", provided
1126 * that "array" represents a virtual array.
1128 * If "array" is virtual, then We take the preimage of "domain"
1129 * over the projection of the extent of "array" onto its initial dimensions
1130 * and intersect this extent with the result.
1132 static struct pet_array *virtual_array_intersect_domain_prefix(
1133 struct pet_array *array, __isl_take isl_set *domain)
1135 int n;
1136 isl_space *space;
1137 isl_multi_aff *ma;
1139 if (!array || !extent_is_virtual_array(array->extent)) {
1140 isl_set_free(domain);
1141 return array;
1144 space = isl_set_get_space(array->extent);
1145 n = isl_set_dim(domain, isl_dim_set);
1146 ma = pet_prefix_projection(space, n);
1147 domain = isl_set_preimage_multi_aff(domain, ma);
1149 array->extent = isl_set_intersect(array->extent, domain);
1150 if (!array->extent)
1151 return pet_array_free(array);
1153 return array;
1156 /* Intersect the initial dimensions of the domain of "stmt"
1157 * with "domain".
1159 * We take the preimage of "domain" over the projection of the
1160 * domain of "stmt" onto its initial dimensions and intersect
1161 * the domain of "stmt" with the result.
1163 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1164 __isl_take isl_set *domain)
1166 int n;
1167 isl_space *space;
1168 isl_multi_aff *ma;
1170 if (!stmt)
1171 goto error;
1173 space = isl_set_get_space(stmt->domain);
1174 n = isl_set_dim(domain, isl_dim_set);
1175 ma = pet_prefix_projection(space, n);
1176 domain = isl_set_preimage_multi_aff(domain, ma);
1178 stmt->domain = isl_set_intersect(stmt->domain, domain);
1179 if (!stmt->domain)
1180 return pet_stmt_free(stmt);
1182 return stmt;
1183 error:
1184 isl_set_free(domain);
1185 return pet_stmt_free(stmt);
1188 /* Intersect the initial dimensions of the domain of "implication"
1189 * with "domain".
1191 * We take the preimage of "domain" over the projection of the
1192 * domain of "implication" onto its initial dimensions and intersect
1193 * the domain of "implication" with the result.
1195 static struct pet_implication *implication_intersect_domain_prefix(
1196 struct pet_implication *implication, __isl_take isl_set *domain)
1198 int n;
1199 isl_space *space;
1200 isl_multi_aff *ma;
1202 if (!implication)
1203 goto error;
1205 space = isl_map_get_space(implication->extension);
1206 n = isl_set_dim(domain, isl_dim_set);
1207 ma = pet_prefix_projection(isl_space_domain(space), n);
1208 domain = isl_set_preimage_multi_aff(domain, ma);
1210 implication->extension =
1211 isl_map_intersect_domain(implication->extension, domain);
1212 if (!implication->extension)
1213 return pet_implication_free(implication);
1215 return implication;
1216 error:
1217 isl_set_free(domain);
1218 return pet_implication_free(implication);
1221 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1223 * The extents of the virtual arrays match the iteration domains,
1224 * so if the iteration domain changes, we need to change those extents too.
1226 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1227 __isl_take isl_set *domain)
1229 int i;
1231 if (!scop)
1232 goto error;
1234 for (i = 0; i < scop->n_array; ++i) {
1235 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1236 scop->arrays[i], isl_set_copy(domain));
1237 if (!scop->arrays[i])
1238 goto error;
1241 for (i = 0; i < scop->n_stmt; ++i) {
1242 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1243 isl_set_copy(domain));
1244 if (!scop->stmts[i])
1245 goto error;
1248 for (i = 0; i < scop->n_implication; ++i) {
1249 scop->implications[i] =
1250 implication_intersect_domain_prefix(scop->implications[i],
1251 isl_set_copy(domain));
1252 if (!scop->implications[i])
1253 return pet_scop_free(scop);
1256 isl_set_free(domain);
1257 return scop;
1258 error:
1259 isl_set_free(domain);
1260 return pet_scop_free(scop);
1263 /* Prefix the schedule of "stmt" with an extra dimension with constant
1264 * value "pos".
1266 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1268 if (!stmt)
1269 return NULL;
1271 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1272 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1273 if (!stmt->schedule)
1274 return pet_stmt_free(stmt);
1276 return stmt;
1279 /* Prefix the schedules of all statements in "scop" with an extra
1280 * dimension with constant value "pos".
1282 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1284 int i;
1286 if (!scop)
1287 return NULL;
1289 for (i = 0; i < scop->n_stmt; ++i) {
1290 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1291 if (!scop->stmts[i])
1292 return pet_scop_free(scop);
1295 return scop;
1298 /* Prefix the schedule of "stmt" with "sched".
1300 * The domain of "sched" refers the current outer loop iterators and
1301 * needs to be mapped to the iteration domain of "stmt" first
1302 * before being prepended to the schedule of "stmt".
1304 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1305 __isl_take isl_map *sched)
1307 int n;
1308 isl_space *space;
1309 isl_multi_aff *ma;
1311 if (!stmt)
1312 goto error;
1314 space = pet_stmt_get_space(stmt);
1315 n = isl_map_dim(sched, isl_dim_in);
1316 ma = pet_prefix_projection(space, n);
1317 sched = isl_map_preimage_domain_multi_aff(sched, ma);
1318 stmt->schedule = isl_map_flat_range_product(sched, stmt->schedule);
1319 if (!stmt->schedule)
1320 return pet_stmt_free(stmt);
1322 return stmt;
1323 error:
1324 isl_map_free(sched);
1325 return NULL;
1328 /* Update the context with respect to an embedding into a loop
1329 * with iteration domain "dom".
1330 * The input context lives in the same space as "dom".
1331 * The output context has the inner dimension removed.
1333 * An outer loop iterator value is invalid for the embedding if
1334 * any of the corresponding inner iterator values is invalid.
1335 * That is, an outer loop iterator value is valid only if all the corresponding
1336 * inner iterator values are valid.
1337 * We therefore compute the set of outer loop iterators l
1339 * forall i: dom(l,i) => valid(l,i)
1341 * or
1343 * forall i: not dom(l,i) or valid(l,i)
1345 * or
1347 * not exists i: dom(l,i) and not valid(l,i)
1349 * i.e.,
1351 * not exists i: (dom \ valid)(l,i)
1353 * If there are any unnamed parameters in "dom", then we consider
1354 * a parameter value to be valid if it is valid for any value of those
1355 * unnamed parameters. They are therefore projected out at the end.
1357 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1358 __isl_keep isl_set *dom)
1360 int pos;
1362 pos = isl_set_dim(context, isl_dim_set) - 1;
1363 context = isl_set_subtract(isl_set_copy(dom), context);
1364 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1365 context = isl_set_complement(context);
1366 context = pet_nested_remove_from_set(context);
1368 return context;
1371 /* Update the implication with respect to an embedding into a loop
1372 * with iteration domain "dom".
1374 * Since embed_access extends virtual arrays along with the domain
1375 * of the access, we need to do the same with domain and range
1376 * of the implication. Since the original implication is only valid
1377 * within a given iteration of the loop, the extended implication
1378 * maps the extra array dimension corresponding to the extra loop
1379 * to itself.
1381 static struct pet_implication *pet_implication_embed(
1382 struct pet_implication *implication, __isl_take isl_set *dom)
1384 isl_id *id;
1385 isl_map *map;
1387 if (!implication)
1388 goto error;
1390 map = isl_set_identity(dom);
1391 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1392 map = isl_map_flat_product(map, implication->extension);
1393 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1394 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1395 implication->extension = map;
1396 if (!implication->extension)
1397 return pet_implication_free(implication);
1399 return implication;
1400 error:
1401 isl_set_free(dom);
1402 return NULL;
1405 /* Adjust the context and statement schedules according to an embedding
1406 * in a loop with iteration domain "dom" and schedule "sched".
1408 * Any skip conditions within the loop have no effect outside of the loop.
1409 * The caller is responsible for making sure skip[pet_skip_later] has been
1410 * taken into account.
1412 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1413 __isl_take isl_aff *sched)
1415 int i;
1416 isl_map *sched_map;
1418 sched_map = isl_map_from_aff(sched);
1420 if (!scop)
1421 goto error;
1423 pet_scop_reset_skip(scop, pet_skip_now);
1424 pet_scop_reset_skip(scop, pet_skip_later);
1426 scop->context = context_embed(scop->context, dom);
1427 if (!scop->context)
1428 goto error;
1430 for (i = 0; i < scop->n_stmt; ++i) {
1431 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1432 isl_map_copy(sched_map));
1433 if (!scop->stmts[i])
1434 goto error;
1437 isl_set_free(dom);
1438 isl_map_free(sched_map);
1439 return scop;
1440 error:
1441 isl_set_free(dom);
1442 isl_map_free(sched_map);
1443 return pet_scop_free(scop);
1446 /* Add extra conditions to scop->skip[type].
1448 * The new skip condition only holds if it held before
1449 * and the condition is true. It does not hold if it did not hold
1450 * before or the condition is false.
1452 * The skip condition is assumed to be an affine expression.
1454 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1455 enum pet_skip type, __isl_keep isl_set *cond)
1457 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1458 isl_pw_aff *skip;
1459 isl_set *dom;
1461 if (!scop)
1462 return NULL;
1463 if (!ext->skip[type])
1464 return scop;
1466 if (!multi_pw_aff_is_affine(ext->skip[type]))
1467 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1468 isl_error_internal, "can only restrict affine skips",
1469 return pet_scop_free(scop));
1471 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1472 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1473 cond = isl_set_copy(cond);
1474 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1475 skip = indicator_function(cond, dom);
1476 isl_multi_pw_aff_free(ext->skip[type]);
1477 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1478 if (!ext->skip[type])
1479 return pet_scop_free(scop);
1481 return scop;
1484 /* Adjust the context and the skip conditions to the fact that
1485 * the scop was created in a context where "cond" holds.
1487 * An outer loop iterator or parameter value is valid for the result
1488 * if it was valid for the original scop and satisfies "cond" or if it does
1489 * not satisfy "cond" as in this case the scop is not executed
1490 * and the original constraints on these values are irrelevant.
1492 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1493 __isl_take isl_set *cond)
1495 int i;
1497 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1498 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1500 if (!scop)
1501 goto error;
1503 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1504 scop->context = isl_set_union(scop->context,
1505 isl_set_complement(isl_set_copy(cond)));
1506 scop->context = isl_set_coalesce(scop->context);
1507 scop->context = pet_nested_remove_from_set(scop->context);
1508 if (!scop->context)
1509 goto error;
1511 isl_set_free(cond);
1512 return scop;
1513 error:
1514 isl_set_free(cond);
1515 return pet_scop_free(scop);
1518 /* Insert an argument expression corresponding to "test" in front
1519 * of the list of arguments described by *n_arg and *args.
1521 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1522 __isl_keep isl_multi_pw_aff *test)
1524 int i;
1525 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1527 if (!test)
1528 return -1;
1530 if (!*args) {
1531 *args = isl_calloc_array(ctx, pet_expr *, 1);
1532 if (!*args)
1533 return -1;
1534 } else {
1535 pet_expr **ext;
1536 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1537 if (!ext)
1538 return -1;
1539 for (i = 0; i < *n_arg; ++i)
1540 ext[1 + i] = (*args)[i];
1541 free(*args);
1542 *args = ext;
1544 (*n_arg)++;
1545 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1546 if (!(*args)[0])
1547 return -1;
1549 return 0;
1552 /* Look through the applications in "scop" for any that can be
1553 * applied to the filter expressed by "map" and "satisified".
1554 * If there is any, then apply it to "map" and return the result.
1555 * Otherwise, return "map".
1556 * "id" is the identifier of the virtual array.
1558 * We only introduce at most one implication for any given virtual array,
1559 * so we can apply the implication and return as soon as we find one.
1561 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1562 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1564 int i;
1566 for (i = 0; i < scop->n_implication; ++i) {
1567 struct pet_implication *pi = scop->implications[i];
1568 isl_id *pi_id;
1570 if (pi->satisfied != satisfied)
1571 continue;
1572 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1573 isl_id_free(pi_id);
1574 if (pi_id != id)
1575 continue;
1577 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1580 return map;
1583 /* Is the filter expressed by "test" and "satisfied" implied
1584 * by filter "pos" on "domain", with filter "expr", taking into
1585 * account the implications of "scop"?
1587 * For filter on domain implying that expressed by "test" and "satisfied",
1588 * the filter needs to be an access to the same (virtual) array as "test" and
1589 * the filter value needs to be equal to "satisfied".
1590 * Moreover, the filter access relation, possibly extended by
1591 * the implications in "scop" needs to contain "test".
1593 static int implies_filter(struct pet_scop *scop,
1594 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1595 __isl_keep isl_map *test, int satisfied)
1597 isl_id *test_id, *arg_id;
1598 isl_val *val;
1599 int is_int;
1600 int s;
1601 int is_subset;
1602 isl_map *implied;
1604 if (expr->type != pet_expr_access)
1605 return 0;
1606 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1607 arg_id = pet_expr_access_get_id(expr);
1608 isl_id_free(arg_id);
1609 isl_id_free(test_id);
1610 if (test_id != arg_id)
1611 return 0;
1612 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1613 is_int = isl_val_is_int(val);
1614 if (is_int)
1615 s = isl_val_get_num_si(val);
1616 isl_val_free(val);
1617 if (!val)
1618 return -1;
1619 if (!is_int)
1620 return 0;
1621 if (s != satisfied)
1622 return 0;
1624 implied = isl_map_copy(expr->acc.access);
1625 implied = apply_implications(scop, implied, test_id, satisfied);
1626 is_subset = isl_map_is_subset(test, implied);
1627 isl_map_free(implied);
1629 return is_subset;
1632 /* Is the filter expressed by "test" and "satisfied" implied
1633 * by any of the filters on the domain of "stmt", taking into
1634 * account the implications of "scop"?
1636 static int filter_implied(struct pet_scop *scop,
1637 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1639 int i;
1640 int implied;
1641 isl_id *test_id;
1642 isl_map *domain;
1643 isl_map *test_map;
1645 if (!scop || !stmt || !test)
1646 return -1;
1647 if (scop->n_implication == 0)
1648 return 0;
1649 if (stmt->n_arg == 0)
1650 return 0;
1652 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1653 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1655 implied = 0;
1656 for (i = 0; i < stmt->n_arg; ++i) {
1657 implied = implies_filter(scop, domain, i, stmt->args[i],
1658 test_map, satisfied);
1659 if (implied < 0 || implied)
1660 break;
1663 isl_map_free(test_map);
1664 isl_map_free(domain);
1665 return implied;
1668 /* Make the statement "stmt" depend on the value of "test"
1669 * being equal to "satisfied" by adjusting stmt->domain.
1671 * The domain of "test" corresponds to the (zero or more) outer dimensions
1672 * of the iteration domain.
1674 * We first extend "test" to apply to the entire iteration domain and
1675 * then check if the filter that we are about to add is implied
1676 * by any of the current filters, possibly taking into account
1677 * the implications in "scop". If so, we leave "stmt" untouched and return.
1679 * Otherwise, we insert an argument corresponding to a read to "test"
1680 * from the iteration domain of "stmt" in front of the list of arguments.
1681 * We also insert a corresponding output dimension in the wrapped
1682 * map contained in stmt->domain, with value set to "satisfied".
1684 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1685 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1687 int i;
1688 int implied;
1689 isl_id *id;
1690 isl_ctx *ctx;
1691 isl_pw_multi_aff *pma;
1692 isl_multi_aff *add_dom;
1693 isl_space *space;
1694 isl_local_space *ls;
1695 int n_test_dom;
1697 if (!stmt || !test)
1698 goto error;
1700 space = pet_stmt_get_space(stmt);
1701 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1702 space = isl_space_from_domain(space);
1703 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1704 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1705 ls = isl_local_space_from_space(isl_space_domain(space));
1706 for (i = 0; i < n_test_dom; ++i) {
1707 isl_aff *aff;
1708 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1709 isl_dim_set, i);
1710 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1712 isl_local_space_free(ls);
1713 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1715 implied = filter_implied(scop, stmt, test, satisfied);
1716 if (implied < 0)
1717 goto error;
1718 if (implied) {
1719 isl_multi_pw_aff_free(test);
1720 return stmt;
1723 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1724 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1725 id, satisfied);
1726 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1728 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1729 goto error;
1731 isl_multi_pw_aff_free(test);
1732 return stmt;
1733 error:
1734 isl_multi_pw_aff_free(test);
1735 return pet_stmt_free(stmt);
1738 /* Does "scop" have a skip condition of the given "type"?
1740 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1742 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1744 if (!scop)
1745 return -1;
1746 return ext->skip[type] != NULL;
1749 /* Does "scop" have a skip condition of the given "type" that
1750 * is an affine expression?
1752 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1754 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1756 if (!scop)
1757 return -1;
1758 if (!ext->skip[type])
1759 return 0;
1760 return multi_pw_aff_is_affine(ext->skip[type]);
1763 /* Does "scop" have a skip condition of the given "type" that
1764 * is not an affine expression?
1766 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1768 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1769 int aff;
1771 if (!scop)
1772 return -1;
1773 if (!ext->skip[type])
1774 return 0;
1775 aff = multi_pw_aff_is_affine(ext->skip[type]);
1776 if (aff < 0)
1777 return -1;
1778 return !aff;
1781 /* Does "scop" have a skip condition of the given "type" that
1782 * is affine and holds on the entire domain?
1784 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1786 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1787 isl_pw_aff *pa;
1788 isl_set *set;
1789 int is_aff;
1790 int is_univ;
1792 is_aff = pet_scop_has_affine_skip(scop, type);
1793 if (is_aff < 0 || !is_aff)
1794 return is_aff;
1796 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1797 set = isl_pw_aff_non_zero_set(pa);
1798 is_univ = isl_set_plain_is_universe(set);
1799 isl_set_free(set);
1801 return is_univ;
1804 /* Replace scop->skip[type] by "skip".
1806 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1807 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
1809 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1811 if (!scop || !skip)
1812 goto error;
1814 isl_multi_pw_aff_free(ext->skip[type]);
1815 ext->skip[type] = skip;
1817 return scop;
1818 error:
1819 isl_multi_pw_aff_free(skip);
1820 return pet_scop_free(scop);
1823 /* Return a copy of scop->skip[type].
1825 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
1826 enum pet_skip type)
1828 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1830 if (!scop)
1831 return NULL;
1833 return isl_multi_pw_aff_copy(ext->skip[type]);
1836 /* Assuming scop->skip[type] is an affine expression,
1837 * return the constraints on the outer loop domain for which the skip condition
1838 * holds.
1840 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
1841 enum pet_skip type)
1843 isl_multi_pw_aff *skip;
1844 isl_pw_aff *pa;
1846 skip = pet_scop_get_skip(scop, type);
1847 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1848 isl_multi_pw_aff_free(skip);
1849 return isl_pw_aff_non_zero_set(pa);
1852 /* Return the identifier of the variable that is accessed by
1853 * the skip condition of the given type.
1855 * The skip condition is assumed not to be an affine condition.
1857 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
1858 enum pet_skip type)
1860 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1862 if (!scop)
1863 return NULL;
1865 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
1868 /* Return an access pet_expr corresponding to the skip condition
1869 * of the given type.
1871 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
1872 enum pet_skip type)
1874 return pet_expr_from_index(pet_scop_get_skip(scop, type));
1877 /* Drop the the skip condition scop->skip[type].
1879 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
1881 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1883 if (!scop)
1884 return;
1886 isl_multi_pw_aff_free(ext->skip[type]);
1887 ext->skip[type] = NULL;
1890 /* Make the skip condition (if any) depend on the value of "test" being
1891 * equal to "satisfied".
1893 * We only support the case where the original skip condition is universal,
1894 * i.e., where skipping is unconditional, and where satisfied == 1.
1895 * In this case, the skip condition is changed to skip only when
1896 * "test" is equal to one.
1898 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
1899 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
1901 int is_univ = 0;
1903 if (!scop)
1904 return NULL;
1905 if (!pet_scop_has_skip(scop, type))
1906 return scop;
1908 if (satisfied)
1909 is_univ = pet_scop_has_universal_skip(scop, type);
1910 if (is_univ < 0)
1911 return pet_scop_free(scop);
1912 if (satisfied && is_univ) {
1913 isl_multi_pw_aff *skip;
1914 skip = isl_multi_pw_aff_copy(test);
1915 scop = pet_scop_set_skip(scop, type, skip);
1916 if (!scop)
1917 return NULL;
1918 } else {
1919 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
1920 "skip expression cannot be filtered",
1921 return pet_scop_free(scop));
1924 return scop;
1927 /* Make all statements in "scop" depend on the value of "test"
1928 * being equal to "satisfied" by adjusting their domains.
1930 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
1931 __isl_take isl_multi_pw_aff *test, int satisfied)
1933 int i;
1935 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
1936 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
1938 if (!scop || !test)
1939 goto error;
1941 for (i = 0; i < scop->n_stmt; ++i) {
1942 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
1943 isl_multi_pw_aff_copy(test), satisfied);
1944 if (!scop->stmts[i])
1945 goto error;
1948 isl_multi_pw_aff_free(test);
1949 return scop;
1950 error:
1951 isl_multi_pw_aff_free(test);
1952 return pet_scop_free(scop);
1955 /* Add the parameters of the access expression "expr" to "space".
1957 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
1959 int i;
1960 isl_space **space = user;
1962 *space = isl_space_align_params(*space,
1963 isl_map_get_space(expr->acc.access));
1965 return *space ? 0 : -1;
1968 /* Add all parameters in "stmt" to "space" and return the result.
1970 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
1971 __isl_take isl_space *space)
1973 int i;
1975 if (!stmt)
1976 return isl_space_free(space);
1978 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
1979 space = isl_space_align_params(space,
1980 isl_map_get_space(stmt->schedule));
1981 for (i = 0; i < stmt->n_arg; ++i)
1982 if (pet_expr_foreach_access_expr(stmt->args[i],
1983 &access_collect_params, &space) < 0)
1984 space = isl_space_free(space);
1985 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
1986 &space) < 0)
1987 space = isl_space_free(space);
1989 return space;
1992 /* Add all parameters in "array" to "space" and return the result.
1994 static __isl_give isl_space *array_collect_params(struct pet_array *array,
1995 __isl_take isl_space *space)
1997 if (!array)
1998 return isl_space_free(space);
2000 space = isl_space_align_params(space,
2001 isl_set_get_space(array->context));
2002 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2004 return space;
2007 /* Add all parameters in "scop" to "space" and return the result.
2009 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2010 __isl_take isl_space *space)
2012 int i;
2014 if (!scop)
2015 return isl_space_free(space);
2017 for (i = 0; i < scop->n_array; ++i)
2018 space = array_collect_params(scop->arrays[i], space);
2020 for (i = 0; i < scop->n_stmt; ++i)
2021 space = stmt_collect_params(scop->stmts[i], space);
2023 return space;
2026 /* Add all parameters in "space" to the domain, schedule and
2027 * all access relations in "stmt".
2029 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2030 __isl_take isl_space *space)
2032 int i;
2034 if (!stmt)
2035 goto error;
2037 stmt->domain = isl_set_align_params(stmt->domain,
2038 isl_space_copy(space));
2039 stmt->schedule = isl_map_align_params(stmt->schedule,
2040 isl_space_copy(space));
2042 for (i = 0; i < stmt->n_arg; ++i) {
2043 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2044 isl_space_copy(space));
2045 if (!stmt->args[i])
2046 goto error;
2048 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2050 if (!stmt->domain || !stmt->schedule || !stmt->body)
2051 goto error;
2053 isl_space_free(space);
2054 return stmt;
2055 error:
2056 isl_space_free(space);
2057 return pet_stmt_free(stmt);
2060 /* Add all parameters in "space" to "array".
2062 static struct pet_array *array_propagate_params(struct pet_array *array,
2063 __isl_take isl_space *space)
2065 if (!array)
2066 goto error;
2068 array->context = isl_set_align_params(array->context,
2069 isl_space_copy(space));
2070 array->extent = isl_set_align_params(array->extent,
2071 isl_space_copy(space));
2072 if (array->value_bounds) {
2073 array->value_bounds = isl_set_align_params(array->value_bounds,
2074 isl_space_copy(space));
2075 if (!array->value_bounds)
2076 goto error;
2079 if (!array->context || !array->extent)
2080 goto error;
2082 isl_space_free(space);
2083 return array;
2084 error:
2085 isl_space_free(space);
2086 return pet_array_free(array);
2089 /* Add all parameters in "space" to "scop".
2091 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2092 __isl_take isl_space *space)
2094 int i;
2096 if (!scop)
2097 goto error;
2099 for (i = 0; i < scop->n_array; ++i) {
2100 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2101 isl_space_copy(space));
2102 if (!scop->arrays[i])
2103 goto error;
2106 for (i = 0; i < scop->n_stmt; ++i) {
2107 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2108 isl_space_copy(space));
2109 if (!scop->stmts[i])
2110 goto error;
2113 isl_space_free(space);
2114 return scop;
2115 error:
2116 isl_space_free(space);
2117 return pet_scop_free(scop);
2120 /* Update all isl_sets and isl_maps in "scop" such that they all
2121 * have the same parameters.
2123 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2125 isl_space *space;
2127 if (!scop)
2128 return NULL;
2130 space = isl_set_get_space(scop->context);
2131 space = scop_collect_params(scop, space);
2133 scop->context = isl_set_align_params(scop->context,
2134 isl_space_copy(space));
2135 scop = scop_propagate_params(scop, space);
2137 if (scop && !scop->context)
2138 return pet_scop_free(scop);
2140 return scop;
2143 /* Add the access relation of the access expression "expr" to "accesses" and
2144 * return the result.
2145 * The domain of the access relation is intersected with "domain".
2146 * If "tag" is set, then the access relation is tagged with
2147 * the corresponding reference identifier.
2149 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2150 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2152 isl_map *access;
2154 access = pet_expr_access_get_may_access(expr);
2155 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2156 if (tag)
2157 access = pet_expr_tag_access(expr, access);
2158 return isl_union_map_add_map(accesses, access);
2161 /* Internal data structure for expr_collect_accesses.
2163 * "read" is set if we want to collect read accesses.
2164 * "write" is set if we want to collect write accesses.
2165 * "must" is set if we only want definite accesses.
2166 * "tag" is set if the access relations should be tagged with
2167 * the corresponding reference identifiers.
2168 * "domain" are constraints on the domain of the access relations.
2169 * "accesses" collects the results.
2171 struct pet_expr_collect_accesses_data {
2172 int read;
2173 int write;
2174 int must;
2175 int tag;
2176 isl_set *domain;
2178 isl_union_map *accesses;
2181 /* Add the access relation of the access expression "expr"
2182 * to data->accesses if the access expression is a read and data->read is set
2183 * and/or it is a write and data->write is set.
2184 * The domains of the access relations are intersected with data->domain.
2185 * If data->tag is set, then the access relations are tagged with
2186 * the corresponding reference identifiers.
2188 * If data->must is set, then we only add the accesses that are definitely
2189 * performed. Otherwise, we add all potential accesses.
2190 * In particular, if the access has any arguments, then if data->must is
2191 * set we currently skip the access completely. If data->must is not set,
2192 * we project out the values of the access arguments.
2194 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2196 struct pet_expr_collect_accesses_data *data = user;
2197 int i;
2198 isl_id *id;
2199 isl_space *dim;
2201 if (!expr)
2202 return -1;
2204 if (pet_expr_is_affine(expr))
2205 return 0;
2206 if (data->must && expr->n_arg != 0)
2207 return 0;
2209 if ((data->read && expr->acc.read) || (data->write && expr->acc.write))
2210 data->accesses = expr_collect_access(expr, data->tag,
2211 data->accesses, data->domain);
2213 return data->accesses ? 0 : -1;
2216 /* Collect and return all read access relations (if "read" is set)
2217 * and/or all write access relations (if "write" is set) in "stmt".
2218 * If "tag" is set, then the access relations are tagged with
2219 * the corresponding reference identifiers.
2220 * If "kill" is set, then "stmt" is a kill statement and we simply
2221 * add the argument of the kill operation.
2223 * If "must" is set, then we only add the accesses that are definitely
2224 * performed. Otherwise, we add all potential accesses.
2225 * In particular, if the statement has any arguments, then if "must" is
2226 * set we currently skip the statement completely. If "must" is not set,
2227 * we project out the values of the statement arguments.
2228 * If the statement body is not an expression tree, then we cannot
2229 * know for sure if/when the accesses inside the tree are performed.
2230 * We therefore ignore such statements when "must" is set.
2232 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2233 int read, int write, int kill, int must, int tag,
2234 __isl_take isl_space *dim)
2236 struct pet_expr_collect_accesses_data data = { read, write, must, tag };
2238 if (!stmt)
2239 return NULL;
2241 data.accesses = isl_union_map_empty(dim);
2243 if (must && stmt->n_arg > 0)
2244 return data.accesses;
2245 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2246 return data.accesses;
2248 data.domain = isl_set_copy(stmt->domain);
2249 if (isl_set_is_wrapping(data.domain))
2250 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2252 if (kill) {
2253 pet_expr *body, *arg;
2255 body = pet_tree_expr_get_expr(stmt->body);
2256 arg = pet_expr_get_arg(body, 0);
2257 data.accesses = expr_collect_access(arg, tag,
2258 data.accesses, data.domain);
2259 pet_expr_free(arg);
2260 pet_expr_free(body);
2261 } else if (pet_tree_foreach_access_expr(stmt->body,
2262 &expr_collect_accesses, &data) < 0)
2263 data.accesses = isl_union_map_free(data.accesses);
2265 isl_set_free(data.domain);
2267 return data.accesses;
2270 /* Is "stmt" an assignment statement?
2272 int pet_stmt_is_assign(struct pet_stmt *stmt)
2274 if (!stmt)
2275 return 0;
2276 return pet_tree_is_assign(stmt->body);
2279 /* Is "stmt" a kill statement?
2281 int pet_stmt_is_kill(struct pet_stmt *stmt)
2283 if (!stmt)
2284 return 0;
2285 return pet_tree_is_kill(stmt->body);
2288 /* Is "stmt" an assume statement?
2290 int pet_stmt_is_assume(struct pet_stmt *stmt)
2292 if (!stmt)
2293 return 0;
2294 return pet_tree_is_assume(stmt->body);
2297 /* Compute a mapping from all arrays (of structs) in scop
2298 * to their innermost arrays.
2300 * In particular, for each array of a primitive type, the result
2301 * contains the identity mapping on that array.
2302 * For each array involving member accesses, the result
2303 * contains a mapping from the elements of any intermediate array of structs
2304 * to all corresponding elements of the innermost nested arrays.
2306 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2308 int i;
2309 isl_union_map *to_inner;
2311 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2313 for (i = 0; i < scop->n_array; ++i) {
2314 struct pet_array *array = scop->arrays[i];
2315 isl_set *set;
2316 isl_map *map, *gist;
2318 if (array->element_is_record)
2319 continue;
2321 map = isl_set_identity(isl_set_copy(array->extent));
2323 set = isl_map_domain(isl_map_copy(map));
2324 gist = isl_map_copy(map);
2325 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2326 to_inner = isl_union_map_add_map(to_inner, gist);
2328 while (set && isl_set_is_wrapping(set)) {
2329 isl_id *id;
2330 isl_map *wrapped;
2332 id = isl_set_get_tuple_id(set);
2333 wrapped = isl_set_unwrap(set);
2334 wrapped = isl_map_domain_map(wrapped);
2335 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2336 map = isl_map_apply_domain(map, wrapped);
2337 set = isl_map_domain(isl_map_copy(map));
2338 gist = isl_map_copy(map);
2339 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2340 to_inner = isl_union_map_add_map(to_inner, gist);
2343 isl_set_free(set);
2344 isl_map_free(map);
2347 return to_inner;
2350 /* Collect and return all read access relations (if "read" is set)
2351 * and/or all write access relations (if "write" is set) in "scop".
2352 * If "kill" is set, then we only add the arguments of kill operations.
2353 * If "must" is set, then we only add the accesses that are definitely
2354 * performed. Otherwise, we add all potential accesses.
2355 * If "tag" is set, then the access relations are tagged with
2356 * the corresponding reference identifiers.
2357 * For accesses to structures, the returned access relation accesses
2358 * all individual fields in the structures.
2360 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2361 int read, int write, int kill, int must, int tag)
2363 int i;
2364 isl_union_map *accesses;
2365 isl_union_set *arrays;
2366 isl_union_map *to_inner;
2368 if (!scop)
2369 return NULL;
2371 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2373 for (i = 0; i < scop->n_stmt; ++i) {
2374 struct pet_stmt *stmt = scop->stmts[i];
2375 isl_union_map *accesses_i;
2376 isl_space *space;
2378 if (kill && !pet_stmt_is_kill(stmt))
2379 continue;
2381 space = isl_set_get_space(scop->context);
2382 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2383 must, tag, space);
2384 accesses = isl_union_map_union(accesses, accesses_i);
2387 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2388 for (i = 0; i < scop->n_array; ++i) {
2389 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2390 arrays = isl_union_set_add_set(arrays, extent);
2392 accesses = isl_union_map_intersect_range(accesses, arrays);
2394 to_inner = compute_to_inner(scop);
2395 accesses = isl_union_map_apply_range(accesses, to_inner);
2397 return accesses;
2400 /* Collect all potential read access relations.
2402 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2404 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2407 /* Collect all potential write access relations.
2409 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2411 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2414 /* Collect all definite write access relations.
2416 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2418 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2421 /* Collect all definite kill access relations.
2423 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2425 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2428 /* Collect all tagged potential read access relations.
2430 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2431 struct pet_scop *scop)
2433 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2436 /* Collect all tagged potential write access relations.
2438 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2439 struct pet_scop *scop)
2441 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2444 /* Collect all tagged definite write access relations.
2446 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2447 struct pet_scop *scop)
2449 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2452 /* Collect all tagged definite kill access relations.
2454 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2455 struct pet_scop *scop)
2457 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2460 /* Collect and return the union of iteration domains in "scop".
2462 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2464 int i;
2465 isl_set *domain_i;
2466 isl_union_set *domain;
2468 if (!scop)
2469 return NULL;
2471 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2473 for (i = 0; i < scop->n_stmt; ++i) {
2474 domain_i = isl_set_copy(scop->stmts[i]->domain);
2475 domain = isl_union_set_add_set(domain, domain_i);
2478 return domain;
2481 /* Collect and return the schedules of the statements in "scop".
2482 * The range is normalized to the maximal number of scheduling
2483 * dimensions.
2485 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2487 int i, j;
2488 isl_map *schedule_i;
2489 isl_union_map *schedule;
2490 int depth, max_depth = 0;
2492 if (!scop)
2493 return NULL;
2495 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2497 for (i = 0; i < scop->n_stmt; ++i) {
2498 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2499 if (depth > max_depth)
2500 max_depth = depth;
2503 for (i = 0; i < scop->n_stmt; ++i) {
2504 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2505 depth = isl_map_dim(schedule_i, isl_dim_out);
2506 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2507 max_depth - depth);
2508 for (j = depth; j < max_depth; ++j)
2509 schedule_i = isl_map_fix_si(schedule_i,
2510 isl_dim_out, j, 0);
2511 schedule = isl_union_map_add_map(schedule, schedule_i);
2514 return schedule;
2517 /* Add a reference identifier to all access expressions in "stmt".
2518 * "n_ref" points to an integer that contains the sequence number
2519 * of the next reference.
2521 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2523 int i;
2525 if (!stmt)
2526 return NULL;
2528 for (i = 0; i < stmt->n_arg; ++i) {
2529 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2530 if (!stmt->args[i])
2531 return pet_stmt_free(stmt);
2534 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2535 if (!stmt->body)
2536 return pet_stmt_free(stmt);
2538 return stmt;
2541 /* Add a reference identifier to all access expressions in "scop".
2543 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2545 int i;
2546 int n_ref;
2548 if (!scop)
2549 return NULL;
2551 n_ref = 0;
2552 for (i = 0; i < scop->n_stmt; ++i) {
2553 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2554 if (!scop->stmts[i])
2555 return pet_scop_free(scop);
2558 return scop;
2561 /* Reset the user pointer on all parameter ids in "array".
2563 static struct pet_array *array_anonymize(struct pet_array *array)
2565 if (!array)
2566 return NULL;
2568 array->context = isl_set_reset_user(array->context);
2569 array->extent = isl_set_reset_user(array->extent);
2570 if (!array->context || !array->extent)
2571 return pet_array_free(array);
2573 return array;
2576 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2578 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2580 int i;
2581 isl_space *space;
2582 isl_set *domain;
2584 if (!stmt)
2585 return NULL;
2587 stmt->domain = isl_set_reset_user(stmt->domain);
2588 stmt->schedule = isl_map_reset_user(stmt->schedule);
2589 if (!stmt->domain || !stmt->schedule)
2590 return pet_stmt_free(stmt);
2592 for (i = 0; i < stmt->n_arg; ++i) {
2593 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2594 if (!stmt->args[i])
2595 return pet_stmt_free(stmt);
2598 stmt->body = pet_tree_anonymize(stmt->body);
2599 if (!stmt->body)
2600 return pet_stmt_free(stmt);
2602 return stmt;
2605 /* Reset the user pointer on the tuple ids and all parameter ids
2606 * in "implication".
2608 static struct pet_implication *implication_anonymize(
2609 struct pet_implication *implication)
2611 if (!implication)
2612 return NULL;
2614 implication->extension = isl_map_reset_user(implication->extension);
2615 if (!implication->extension)
2616 return pet_implication_free(implication);
2618 return implication;
2621 /* Reset the user pointer on all parameter and tuple ids in "scop".
2623 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2625 int i;
2627 if (!scop)
2628 return NULL;
2630 scop->context = isl_set_reset_user(scop->context);
2631 scop->context_value = isl_set_reset_user(scop->context_value);
2632 if (!scop->context || !scop->context_value)
2633 return pet_scop_free(scop);
2635 for (i = 0; i < scop->n_array; ++i) {
2636 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2637 if (!scop->arrays[i])
2638 return pet_scop_free(scop);
2641 for (i = 0; i < scop->n_stmt; ++i) {
2642 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2643 if (!scop->stmts[i])
2644 return pet_scop_free(scop);
2647 for (i = 0; i < scop->n_implication; ++i) {
2648 scop->implications[i] =
2649 implication_anonymize(scop->implications[i]);
2650 if (!scop->implications[i])
2651 return pet_scop_free(scop);
2654 return scop;
2657 /* Compute the gist of the iteration domain and all access relations
2658 * of "stmt" based on the constraints on the parameters specified by "context"
2659 * and the constraints on the values of nested accesses specified
2660 * by "value_bounds".
2662 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2663 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2665 int i;
2666 isl_set *domain;
2668 if (!stmt)
2669 return NULL;
2671 domain = isl_set_copy(stmt->domain);
2672 if (stmt->n_arg > 0)
2673 domain = isl_map_domain(isl_set_unwrap(domain));
2675 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2677 for (i = 0; i < stmt->n_arg; ++i) {
2678 stmt->args[i] = pet_expr_gist(stmt->args[i],
2679 domain, value_bounds);
2680 if (!stmt->args[i])
2681 goto error;
2684 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
2685 if (!stmt->body)
2686 goto error;
2688 isl_set_free(domain);
2690 domain = isl_set_universe(pet_stmt_get_space(stmt));
2691 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2692 if (stmt->n_arg > 0)
2693 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
2694 value_bounds);
2695 stmt->domain = isl_set_gist(stmt->domain, domain);
2696 if (!stmt->domain)
2697 return pet_stmt_free(stmt);
2699 return stmt;
2700 error:
2701 isl_set_free(domain);
2702 return pet_stmt_free(stmt);
2705 /* Compute the gist of the extent of the array
2706 * based on the constraints on the parameters specified by "context".
2708 static struct pet_array *array_gist(struct pet_array *array,
2709 __isl_keep isl_set *context)
2711 if (!array)
2712 return NULL;
2714 array->extent = isl_set_gist_params(array->extent,
2715 isl_set_copy(context));
2716 if (!array->extent)
2717 return pet_array_free(array);
2719 return array;
2722 /* Compute the gist of all sets and relations in "scop"
2723 * based on the constraints on the parameters specified by "scop->context"
2724 * and the constraints on the values of nested accesses specified
2725 * by "value_bounds".
2727 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2728 __isl_keep isl_union_map *value_bounds)
2730 int i;
2732 if (!scop)
2733 return NULL;
2735 scop->context = isl_set_coalesce(scop->context);
2736 if (!scop->context)
2737 return pet_scop_free(scop);
2739 for (i = 0; i < scop->n_array; ++i) {
2740 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2741 if (!scop->arrays[i])
2742 return pet_scop_free(scop);
2745 for (i = 0; i < scop->n_stmt; ++i) {
2746 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2747 value_bounds);
2748 if (!scop->stmts[i])
2749 return pet_scop_free(scop);
2752 return scop;
2755 /* Intersect the context of "scop" with "context".
2756 * To ensure that we don't introduce any unnamed parameters in
2757 * the context of "scop", we first remove the unnamed parameters
2758 * from "context".
2760 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2761 __isl_take isl_set *context)
2763 if (!scop)
2764 goto error;
2766 context = pet_nested_remove_from_set(context);
2767 scop->context = isl_set_intersect(scop->context, context);
2768 if (!scop->context)
2769 return pet_scop_free(scop);
2771 return scop;
2772 error:
2773 isl_set_free(context);
2774 return pet_scop_free(scop);
2777 /* Drop the current context of "scop". That is, replace the context
2778 * by a universal set.
2780 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
2782 isl_space *space;
2784 if (!scop)
2785 return NULL;
2787 space = isl_set_get_space(scop->context);
2788 isl_set_free(scop->context);
2789 scop->context = isl_set_universe(space);
2790 if (!scop->context)
2791 return pet_scop_free(scop);
2793 return scop;
2796 /* Append "array" to the arrays of "scop".
2798 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
2799 struct pet_array *array)
2801 isl_ctx *ctx;
2802 struct pet_array **arrays;
2804 if (!array || !scop)
2805 goto error;
2807 ctx = isl_set_get_ctx(scop->context);
2808 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
2809 scop->n_array + 1);
2810 if (!arrays)
2811 goto error;
2812 scop->arrays = arrays;
2813 scop->arrays[scop->n_array] = array;
2814 scop->n_array++;
2816 return scop;
2817 error:
2818 pet_array_free(array);
2819 return pet_scop_free(scop);
2822 /* Create an index expression for an access to a virtual array
2823 * representing the result of a condition.
2824 * Unlike other accessed data, the id of the array is NULL as
2825 * there is no ValueDecl in the program corresponding to the virtual
2826 * array.
2827 * The index expression is created as an identity mapping on "space".
2828 * That is, the dimension of the array is the same as that of "space".
2830 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
2831 int test_nr)
2833 isl_id *id;
2834 char name[50];
2836 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
2837 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
2838 space = isl_space_map_from_set(space);
2839 space = isl_space_set_tuple_id(space, isl_dim_out, id);
2840 return isl_multi_pw_aff_identity(space);
2843 /* Add an array with the given extent to the list
2844 * of arrays in "scop" and return the extended pet_scop.
2845 * Specifically, the extent is determined by the image of "domain"
2846 * under "index".
2847 * "int_size" is the number of bytes needed to represent values of type "int".
2848 * The array is marked as attaining values 0 and 1 only and
2849 * as each element being assigned at most once.
2851 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
2852 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
2853 int int_size)
2855 isl_ctx *ctx;
2856 isl_space *space;
2857 struct pet_array *array;
2858 isl_map *access;
2860 if (!scop || !domain || !index)
2861 goto error;
2863 ctx = isl_multi_pw_aff_get_ctx(index);
2864 array = isl_calloc_type(ctx, struct pet_array);
2865 if (!array)
2866 goto error;
2868 access = isl_map_from_multi_pw_aff(index);
2869 access = isl_map_intersect_domain(access, domain);
2870 array->extent = isl_map_range(access);
2871 space = isl_space_params_alloc(ctx, 0);
2872 array->context = isl_set_universe(space);
2873 space = isl_space_set_alloc(ctx, 0, 1);
2874 array->value_bounds = isl_set_universe(space);
2875 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
2876 isl_dim_set, 0, 0);
2877 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
2878 isl_dim_set, 0, 1);
2879 array->element_type = strdup("int");
2880 array->element_size = int_size;
2881 array->uniquely_defined = 1;
2883 if (!array->extent || !array->context)
2884 array = pet_array_free(array);
2886 scop = pet_scop_add_array(scop, array);
2888 return scop;
2889 error:
2890 isl_set_free(domain);
2891 isl_multi_pw_aff_free(index);
2892 return pet_scop_free(scop);
2895 /* Create and return an implication on filter values equal to "satisfied"
2896 * with extension "map".
2898 static struct pet_implication *new_implication(__isl_take isl_map *map,
2899 int satisfied)
2901 isl_ctx *ctx;
2902 struct pet_implication *implication;
2904 if (!map)
2905 return NULL;
2906 ctx = isl_map_get_ctx(map);
2907 implication = isl_alloc_type(ctx, struct pet_implication);
2908 if (!implication)
2909 goto error;
2911 implication->extension = map;
2912 implication->satisfied = satisfied;
2914 return implication;
2915 error:
2916 isl_map_free(map);
2917 return NULL;
2920 /* Add an implication on filter values equal to "satisfied"
2921 * with extension "map" to "scop".
2923 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
2924 __isl_take isl_map *map, int satisfied)
2926 isl_ctx *ctx;
2927 struct pet_implication *implication;
2928 struct pet_implication **implications;
2930 implication = new_implication(map, satisfied);
2931 if (!scop || !implication)
2932 goto error;
2934 ctx = isl_set_get_ctx(scop->context);
2935 implications = isl_realloc_array(ctx, scop->implications,
2936 struct pet_implication *,
2937 scop->n_implication + 1);
2938 if (!implications)
2939 goto error;
2940 scop->implications = implications;
2941 scop->implications[scop->n_implication] = implication;
2942 scop->n_implication++;
2944 return scop;
2945 error:
2946 pet_implication_free(implication);
2947 return pet_scop_free(scop);
2950 /* Given an access expression, check if it is data dependent.
2951 * If so, set *found and abort the search.
2953 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
2955 int *found = user;
2957 if (pet_expr_get_n_arg(expr) > 0) {
2958 *found = 1;
2959 return -1;
2962 return 0;
2965 /* Does "scop" contain any data dependent accesses?
2967 * Check the body of each statement for such accesses.
2969 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
2971 int i;
2972 int found = 0;
2974 if (!scop)
2975 return -1;
2977 for (i = 0; i < scop->n_stmt; ++i) {
2978 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
2979 &is_data_dependent, &found);
2980 if (r < 0 && !found)
2981 return -1;
2982 if (found)
2983 return found;
2986 return found;
2989 /* Does "scop" contain and data dependent conditions?
2991 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
2993 int i;
2995 if (!scop)
2996 return -1;
2998 for (i = 0; i < scop->n_stmt; ++i)
2999 if (scop->stmts[i]->n_arg > 0)
3000 return 1;
3002 return 0;
3005 /* Keep track of the "input" file inside the (extended) "scop".
3007 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3009 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3011 if (!scop)
3012 return NULL;
3014 ext->input = input;
3016 return scop;
3019 /* Print the original code corresponding to "scop" to printer "p".
3021 * pet_scop_print_original can only be called from
3022 * a pet_transform_C_source callback. This means that the input
3023 * file is stored in the extended scop and that the printer prints
3024 * to a file.
3026 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3027 __isl_take isl_printer *p)
3029 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3030 FILE *output;
3031 unsigned start, end;
3033 if (!scop || !p)
3034 return isl_printer_free(p);
3036 if (!ext->input)
3037 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3038 "no input file stored in scop",
3039 return isl_printer_free(p));
3041 output = isl_printer_get_file(p);
3042 if (!output)
3043 return isl_printer_free(p);
3045 start = pet_loc_get_start(scop->loc);
3046 end = pet_loc_get_end(scop->loc);
3047 if (copy(ext->input, output, start, end) < 0)
3048 return isl_printer_free(p);
3050 return p;