PetScan::extract_argument: handle passing of entire arrays
[pet.git] / scop.c
blob4ebd5f7f2a80d04476ef3fbc46e7a78e96c1873b
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 /* Create and return an independence that filters out the dependences
751 * in "filter" with local variables "local".
753 static struct pet_independence *new_independence(
754 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
756 isl_ctx *ctx;
757 struct pet_independence *independence;
759 if (!filter || !local)
760 goto error;
761 ctx = isl_union_map_get_ctx(filter);
762 independence = isl_alloc_type(ctx, struct pet_independence);
763 if (!independence)
764 goto error;
766 independence->filter = filter;
767 independence->local = local;
769 return independence;
770 error:
771 isl_union_map_free(filter);
772 isl_union_set_free(local);
773 return NULL;
776 /* Add an independence that filters out the dependences
777 * in "filter" with local variables "local" to "scop".
779 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
780 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
782 isl_ctx *ctx;
783 struct pet_independence *independence;
784 struct pet_independence **independences;
786 ctx = isl_union_map_get_ctx(filter);
787 independence = new_independence(filter, local);
788 if (!scop || !independence)
789 goto error;
791 independences = isl_realloc_array(ctx, scop->independences,
792 struct pet_independence *,
793 scop->n_independence + 1);
794 if (!independences)
795 goto error;
796 scop->independences = independences;
797 scop->independences[scop->n_independence] = independence;
798 scop->n_independence++;
800 return scop;
801 error:
802 pet_independence_free(independence);
803 pet_scop_free(scop);
804 return NULL;
807 /* Store the concatenation of the independences of "scop1" and "scop2"
808 * in "scop".
810 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
811 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
813 int i, off;
815 if (!scop)
816 return NULL;
818 if (scop2->n_independence == 0) {
819 scop->n_independence = scop1->n_independence;
820 scop->independences = scop1->independences;
821 scop1->n_independence = 0;
822 scop1->independences = NULL;
823 return scop;
826 if (scop1->n_independence == 0) {
827 scop->n_independence = scop2->n_independence;
828 scop->independences = scop2->independences;
829 scop2->n_independence = 0;
830 scop2->independences = NULL;
831 return scop;
834 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
835 scop1->n_independence + scop2->n_independence);
836 if (!scop->independences)
837 return pet_scop_free(scop);
839 for (i = 0; i < scop1->n_independence; ++i) {
840 scop->independences[i] = scop1->independences[i];
841 scop1->independences[i] = NULL;
844 off = scop1->n_independence;
845 for (i = 0; i < scop2->n_independence; ++i) {
846 scop->independences[off + i] = scop2->independences[i];
847 scop2->independences[i] = NULL;
849 scop->n_independence = scop1->n_independence + scop2->n_independence;
851 return scop;
854 /* Construct a pet_scop that contains the offset information,
855 * arrays, statements and skip information in "scop1" and "scop2".
857 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
858 struct pet_scop *scop2)
860 int i;
861 isl_space *space;
862 struct pet_scop *scop = NULL;
864 if (!scop1 || !scop2)
865 goto error;
867 if (scop1->n_stmt == 0) {
868 scop2 = scop_combine_skips(scop2, scop1, scop2);
869 pet_scop_free(scop1);
870 return scop2;
873 if (scop2->n_stmt == 0) {
874 scop1 = scop_combine_skips(scop1, scop1, scop2);
875 pet_scop_free(scop2);
876 return scop1;
879 space = isl_set_get_space(scop1->context);
880 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt);
881 if (!scop)
882 goto error;
884 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
885 scop1->n_array + scop2->n_array);
886 if (!scop->arrays)
887 goto error;
888 scop->n_array = scop1->n_array + scop2->n_array;
890 for (i = 0; i < scop1->n_stmt; ++i) {
891 scop->stmts[i] = scop1->stmts[i];
892 scop1->stmts[i] = NULL;
895 for (i = 0; i < scop2->n_stmt; ++i) {
896 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
897 scop2->stmts[i] = NULL;
900 for (i = 0; i < scop1->n_array; ++i) {
901 scop->arrays[i] = scop1->arrays[i];
902 scop1->arrays[i] = NULL;
905 for (i = 0; i < scop2->n_array; ++i) {
906 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
907 scop2->arrays[i] = NULL;
910 scop = scop_collect_implications(ctx, scop, scop1, scop2);
911 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
912 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
913 scop = scop_combine_skips(scop, scop1, scop2);
914 scop = scop_combine_start_end(scop, scop1, scop2);
915 scop = scop_collect_independences(ctx, scop, scop1, scop2);
917 pet_scop_free(scop1);
918 pet_scop_free(scop2);
919 return scop;
920 error:
921 pet_scop_free(scop1);
922 pet_scop_free(scop2);
923 pet_scop_free(scop);
924 return NULL;
927 /* Apply the skip condition "skip" to "scop".
928 * That is, make sure "scop" is not executed when the condition holds.
930 * If "skip" is an affine expression, we add the conditions under
931 * which the expression is zero to the iteration domains.
932 * Otherwise, we add a filter on the variable attaining the value zero.
934 static struct pet_scop *restrict_skip(struct pet_scop *scop,
935 __isl_take isl_multi_pw_aff *skip)
937 isl_set *zero;
938 isl_pw_aff *pa;
939 int is_aff;
941 if (!scop || !skip)
942 goto error;
944 is_aff = multi_pw_aff_is_affine(skip);
945 if (is_aff < 0)
946 goto error;
948 if (!is_aff)
949 return pet_scop_filter(scop, skip, 0);
951 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
952 isl_multi_pw_aff_free(skip);
953 zero = isl_pw_aff_zero_set(pa);
954 scop = pet_scop_restrict(scop, zero);
956 return scop;
957 error:
958 isl_multi_pw_aff_free(skip);
959 return pet_scop_free(scop);
962 /* Construct a pet_scop that contains the arrays, statements and
963 * skip information in "scop1" and "scop2", where the two scops
964 * are executed "in sequence". That is, breaks and continues
965 * in scop1 have an effect on scop2.
967 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
968 struct pet_scop *scop2)
970 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
971 scop2 = restrict_skip(scop2,
972 pet_scop_get_skip(scop1, pet_skip_now));
973 return pet_scop_add(ctx, scop1, scop2);
976 /* Construct a pet_scop that contains the arrays, statements and
977 * skip information in "scop1" and "scop2", where the two scops
978 * are executed "in parallel". That is, any break or continue
979 * in scop1 has no effect on scop2.
981 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
982 struct pet_scop *scop2)
984 return pet_scop_add(ctx, scop1, scop2);
987 void *pet_implication_free(struct pet_implication *implication)
989 int i;
991 if (!implication)
992 return NULL;
994 isl_map_free(implication->extension);
996 free(implication);
997 return NULL;
1000 void *pet_independence_free(struct pet_independence *independence)
1002 if (!independence)
1003 return NULL;
1005 isl_union_map_free(independence->filter);
1006 isl_union_set_free(independence->local);
1008 free(independence);
1009 return NULL;
1012 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1014 int i;
1015 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1017 if (!scop)
1018 return NULL;
1019 pet_loc_free(scop->loc);
1020 isl_set_free(scop->context);
1021 isl_set_free(scop->context_value);
1022 if (scop->types)
1023 for (i = 0; i < scop->n_type; ++i)
1024 pet_type_free(scop->types[i]);
1025 free(scop->types);
1026 if (scop->arrays)
1027 for (i = 0; i < scop->n_array; ++i)
1028 pet_array_free(scop->arrays[i]);
1029 free(scop->arrays);
1030 if (scop->stmts)
1031 for (i = 0; i < scop->n_stmt; ++i)
1032 pet_stmt_free(scop->stmts[i]);
1033 free(scop->stmts);
1034 if (scop->implications)
1035 for (i = 0; i < scop->n_implication; ++i)
1036 pet_implication_free(scop->implications[i]);
1037 free(scop->implications);
1038 if (scop->independences)
1039 for (i = 0; i < scop->n_independence; ++i)
1040 pet_independence_free(scop->independences[i]);
1041 free(scop->independences);
1042 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1043 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1044 free(scop);
1045 return NULL;
1048 void pet_type_dump(struct pet_type *type)
1050 if (!type)
1051 return;
1053 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1056 void pet_implication_dump(struct pet_implication *implication)
1058 if (!implication)
1059 return;
1061 fprintf(stderr, "%d\n", implication->satisfied);
1062 isl_map_dump(implication->extension);
1065 void pet_scop_dump(struct pet_scop *scop)
1067 int i;
1068 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1070 if (!scop)
1071 return;
1073 isl_set_dump(scop->context);
1074 isl_set_dump(scop->context_value);
1075 for (i = 0; i < scop->n_type; ++i)
1076 pet_type_dump(scop->types[i]);
1077 for (i = 0; i < scop->n_array; ++i)
1078 pet_array_dump(scop->arrays[i]);
1079 for (i = 0; i < scop->n_stmt; ++i)
1080 pet_stmt_dump(scop->stmts[i]);
1081 for (i = 0; i < scop->n_implication; ++i)
1082 pet_implication_dump(scop->implications[i]);
1084 if (ext->skip[0]) {
1085 fprintf(stderr, "skip\n");
1086 isl_multi_pw_aff_dump(ext->skip[0]);
1087 isl_multi_pw_aff_dump(ext->skip[1]);
1091 /* Return 1 if the two pet_arrays are equivalent.
1093 * We don't compare element_size as this may be target dependent.
1095 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1097 if (!array1 || !array2)
1098 return 0;
1100 if (!isl_set_is_equal(array1->context, array2->context))
1101 return 0;
1102 if (!isl_set_is_equal(array1->extent, array2->extent))
1103 return 0;
1104 if (!!array1->value_bounds != !!array2->value_bounds)
1105 return 0;
1106 if (array1->value_bounds &&
1107 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1108 return 0;
1109 if (strcmp(array1->element_type, array2->element_type))
1110 return 0;
1111 if (array1->element_is_record != array2->element_is_record)
1112 return 0;
1113 if (array1->live_out != array2->live_out)
1114 return 0;
1115 if (array1->uniquely_defined != array2->uniquely_defined)
1116 return 0;
1117 if (array1->declared != array2->declared)
1118 return 0;
1119 if (array1->exposed != array2->exposed)
1120 return 0;
1122 return 1;
1125 /* Return 1 if the two pet_stmts are equivalent.
1127 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1129 int i;
1131 if (!stmt1 || !stmt2)
1132 return 0;
1134 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1135 return 0;
1136 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1137 return 0;
1138 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1139 return 0;
1140 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1141 return 0;
1142 if (stmt1->n_arg != stmt2->n_arg)
1143 return 0;
1144 for (i = 0; i < stmt1->n_arg; ++i) {
1145 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1146 return 0;
1149 return 1;
1152 /* Return 1 if the two pet_types are equivalent.
1154 * We only compare the names of the types since the exact representation
1155 * of the definition may depend on the version of clang being used.
1157 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1159 if (!type1 || !type2)
1160 return 0;
1162 if (strcmp(type1->name, type2->name))
1163 return 0;
1165 return 1;
1168 /* Return 1 if the two pet_implications are equivalent.
1170 int pet_implication_is_equal(struct pet_implication *implication1,
1171 struct pet_implication *implication2)
1173 if (!implication1 || !implication2)
1174 return 0;
1176 if (implication1->satisfied != implication2->satisfied)
1177 return 0;
1178 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1179 return 0;
1181 return 1;
1184 /* Return 1 if the two pet_independences are equivalent.
1186 int pet_independence_is_equal(struct pet_independence *independence1,
1187 struct pet_independence *independence2)
1189 if (!independence1 || !independence2)
1190 return 0;
1192 if (!isl_union_map_is_equal(independence1->filter,
1193 independence2->filter))
1194 return 0;
1195 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1196 return 0;
1198 return 1;
1201 /* Return 1 if the two pet_scops are equivalent.
1203 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1205 int i;
1207 if (!scop1 || !scop2)
1208 return 0;
1210 if (!isl_set_is_equal(scop1->context, scop2->context))
1211 return 0;
1212 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1213 return 0;
1215 if (scop1->n_type != scop2->n_type)
1216 return 0;
1217 for (i = 0; i < scop1->n_type; ++i)
1218 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1219 return 0;
1221 if (scop1->n_array != scop2->n_array)
1222 return 0;
1223 for (i = 0; i < scop1->n_array; ++i)
1224 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1225 return 0;
1227 if (scop1->n_stmt != scop2->n_stmt)
1228 return 0;
1229 for (i = 0; i < scop1->n_stmt; ++i)
1230 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1231 return 0;
1233 if (scop1->n_implication != scop2->n_implication)
1234 return 0;
1235 for (i = 0; i < scop1->n_implication; ++i)
1236 if (!pet_implication_is_equal(scop1->implications[i],
1237 scop2->implications[i]))
1238 return 0;
1240 if (scop1->n_independence != scop2->n_independence)
1241 return 0;
1242 for (i = 0; i < scop1->n_independence; ++i)
1243 if (!pet_independence_is_equal(scop1->independences[i],
1244 scop2->independences[i]))
1245 return 0;
1247 return 1;
1250 /* Does the set "extent" reference a virtual array, i.e.,
1251 * one with user pointer equal to NULL?
1252 * A virtual array does not have any members.
1254 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1256 isl_id *id;
1257 int is_virtual;
1259 if (!isl_set_has_tuple_id(extent))
1260 return 0;
1261 if (isl_set_is_wrapping(extent))
1262 return 0;
1263 id = isl_set_get_tuple_id(extent);
1264 is_virtual = !isl_id_get_user(id);
1265 isl_id_free(id);
1267 return is_virtual;
1270 /* Intersect the initial dimensions of "array" with "domain", provided
1271 * that "array" represents a virtual array.
1273 * If "array" is virtual, then We take the preimage of "domain"
1274 * over the projection of the extent of "array" onto its initial dimensions
1275 * and intersect this extent with the result.
1277 static struct pet_array *virtual_array_intersect_domain_prefix(
1278 struct pet_array *array, __isl_take isl_set *domain)
1280 int n;
1281 isl_space *space;
1282 isl_multi_aff *ma;
1284 if (!array || !extent_is_virtual_array(array->extent)) {
1285 isl_set_free(domain);
1286 return array;
1289 space = isl_set_get_space(array->extent);
1290 n = isl_set_dim(domain, isl_dim_set);
1291 ma = pet_prefix_projection(space, n);
1292 domain = isl_set_preimage_multi_aff(domain, ma);
1294 array->extent = isl_set_intersect(array->extent, domain);
1295 if (!array->extent)
1296 return pet_array_free(array);
1298 return array;
1301 /* Intersect the initial dimensions of the domain of "stmt"
1302 * with "domain".
1304 * We take the preimage of "domain" over the projection of the
1305 * domain of "stmt" onto its initial dimensions and intersect
1306 * the domain of "stmt" with the result.
1308 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1309 __isl_take isl_set *domain)
1311 int n;
1312 isl_space *space;
1313 isl_multi_aff *ma;
1315 if (!stmt)
1316 goto error;
1318 space = isl_set_get_space(stmt->domain);
1319 n = isl_set_dim(domain, isl_dim_set);
1320 ma = pet_prefix_projection(space, n);
1321 domain = isl_set_preimage_multi_aff(domain, ma);
1323 stmt->domain = isl_set_intersect(stmt->domain, domain);
1324 if (!stmt->domain)
1325 return pet_stmt_free(stmt);
1327 return stmt;
1328 error:
1329 isl_set_free(domain);
1330 return pet_stmt_free(stmt);
1333 /* Intersect the initial dimensions of the domain of "implication"
1334 * with "domain".
1336 * We take the preimage of "domain" over the projection of the
1337 * domain of "implication" onto its initial dimensions and intersect
1338 * the domain of "implication" with the result.
1340 static struct pet_implication *implication_intersect_domain_prefix(
1341 struct pet_implication *implication, __isl_take isl_set *domain)
1343 int n;
1344 isl_space *space;
1345 isl_multi_aff *ma;
1347 if (!implication)
1348 goto error;
1350 space = isl_map_get_space(implication->extension);
1351 n = isl_set_dim(domain, isl_dim_set);
1352 ma = pet_prefix_projection(isl_space_domain(space), n);
1353 domain = isl_set_preimage_multi_aff(domain, ma);
1355 implication->extension =
1356 isl_map_intersect_domain(implication->extension, domain);
1357 if (!implication->extension)
1358 return pet_implication_free(implication);
1360 return implication;
1361 error:
1362 isl_set_free(domain);
1363 return pet_implication_free(implication);
1366 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1368 * The extents of the virtual arrays match the iteration domains,
1369 * so if the iteration domain changes, we need to change those extents too.
1371 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1372 __isl_take isl_set *domain)
1374 int i;
1376 if (!scop)
1377 goto error;
1379 for (i = 0; i < scop->n_array; ++i) {
1380 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1381 scop->arrays[i], isl_set_copy(domain));
1382 if (!scop->arrays[i])
1383 goto error;
1386 for (i = 0; i < scop->n_stmt; ++i) {
1387 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1388 isl_set_copy(domain));
1389 if (!scop->stmts[i])
1390 goto error;
1393 for (i = 0; i < scop->n_implication; ++i) {
1394 scop->implications[i] =
1395 implication_intersect_domain_prefix(scop->implications[i],
1396 isl_set_copy(domain));
1397 if (!scop->implications[i])
1398 return pet_scop_free(scop);
1401 isl_set_free(domain);
1402 return scop;
1403 error:
1404 isl_set_free(domain);
1405 return pet_scop_free(scop);
1408 /* Prefix the schedule of "stmt" with an extra dimension with constant
1409 * value "pos".
1411 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1413 if (!stmt)
1414 return NULL;
1416 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1417 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1418 if (!stmt->schedule)
1419 return pet_stmt_free(stmt);
1421 return stmt;
1424 /* Prefix the schedules of all statements in "scop" with an extra
1425 * dimension with constant value "pos".
1427 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1429 int i;
1431 if (!scop)
1432 return NULL;
1434 for (i = 0; i < scop->n_stmt; ++i) {
1435 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1436 if (!scop->stmts[i])
1437 return pet_scop_free(scop);
1440 return scop;
1443 /* Prefix the schedule of "stmt" with "sched".
1445 * The domain of "sched" refers the current outer loop iterators and
1446 * needs to be mapped to the iteration domain of "stmt" first
1447 * before being prepended to the schedule of "stmt".
1449 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1450 __isl_take isl_map *sched)
1452 int n;
1453 isl_space *space;
1454 isl_multi_aff *ma;
1456 if (!stmt)
1457 goto error;
1459 space = pet_stmt_get_space(stmt);
1460 n = isl_map_dim(sched, isl_dim_in);
1461 ma = pet_prefix_projection(space, n);
1462 sched = isl_map_preimage_domain_multi_aff(sched, ma);
1463 stmt->schedule = isl_map_flat_range_product(sched, stmt->schedule);
1464 if (!stmt->schedule)
1465 return pet_stmt_free(stmt);
1467 return stmt;
1468 error:
1469 isl_map_free(sched);
1470 return NULL;
1473 /* Update the context with respect to an embedding into a loop
1474 * with iteration domain "dom".
1475 * The input context lives in the same space as "dom".
1476 * The output context has the inner dimension removed.
1478 * An outer loop iterator value is invalid for the embedding if
1479 * any of the corresponding inner iterator values is invalid.
1480 * That is, an outer loop iterator value is valid only if all the corresponding
1481 * inner iterator values are valid.
1482 * We therefore compute the set of outer loop iterators l
1484 * forall i: dom(l,i) => valid(l,i)
1486 * or
1488 * forall i: not dom(l,i) or valid(l,i)
1490 * or
1492 * not exists i: dom(l,i) and not valid(l,i)
1494 * i.e.,
1496 * not exists i: (dom \ valid)(l,i)
1498 * If there are any unnamed parameters in "dom", then we consider
1499 * a parameter value to be valid if it is valid for any value of those
1500 * unnamed parameters. They are therefore projected out at the end.
1502 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1503 __isl_keep isl_set *dom)
1505 int pos;
1507 pos = isl_set_dim(context, isl_dim_set) - 1;
1508 context = isl_set_subtract(isl_set_copy(dom), context);
1509 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1510 context = isl_set_complement(context);
1511 context = pet_nested_remove_from_set(context);
1513 return context;
1516 /* Update the implication with respect to an embedding into a loop
1517 * with iteration domain "dom".
1519 * Since embed_access extends virtual arrays along with the domain
1520 * of the access, we need to do the same with domain and range
1521 * of the implication. Since the original implication is only valid
1522 * within a given iteration of the loop, the extended implication
1523 * maps the extra array dimension corresponding to the extra loop
1524 * to itself.
1526 static struct pet_implication *pet_implication_embed(
1527 struct pet_implication *implication, __isl_take isl_set *dom)
1529 isl_id *id;
1530 isl_map *map;
1532 if (!implication)
1533 goto error;
1535 map = isl_set_identity(dom);
1536 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1537 map = isl_map_flat_product(map, implication->extension);
1538 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1539 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1540 implication->extension = map;
1541 if (!implication->extension)
1542 return pet_implication_free(implication);
1544 return implication;
1545 error:
1546 isl_set_free(dom);
1547 return NULL;
1550 /* Adjust the context and statement schedules according to an embedding
1551 * in a loop with iteration domain "dom" and schedule "sched".
1553 * Any skip conditions within the loop have no effect outside of the loop.
1554 * The caller is responsible for making sure skip[pet_skip_later] has been
1555 * taken into account.
1557 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1558 __isl_take isl_aff *sched)
1560 int i;
1561 isl_map *sched_map;
1563 sched_map = isl_map_from_aff(sched);
1565 if (!scop)
1566 goto error;
1568 pet_scop_reset_skip(scop, pet_skip_now);
1569 pet_scop_reset_skip(scop, pet_skip_later);
1571 scop->context = context_embed(scop->context, dom);
1572 if (!scop->context)
1573 goto error;
1575 for (i = 0; i < scop->n_stmt; ++i) {
1576 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1577 isl_map_copy(sched_map));
1578 if (!scop->stmts[i])
1579 goto error;
1582 isl_set_free(dom);
1583 isl_map_free(sched_map);
1584 return scop;
1585 error:
1586 isl_set_free(dom);
1587 isl_map_free(sched_map);
1588 return pet_scop_free(scop);
1591 /* Add extra conditions to scop->skip[type].
1593 * The new skip condition only holds if it held before
1594 * and the condition is true. It does not hold if it did not hold
1595 * before or the condition is false.
1597 * The skip condition is assumed to be an affine expression.
1599 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1600 enum pet_skip type, __isl_keep isl_set *cond)
1602 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1603 isl_pw_aff *skip;
1604 isl_set *dom;
1606 if (!scop)
1607 return NULL;
1608 if (!ext->skip[type])
1609 return scop;
1611 if (!multi_pw_aff_is_affine(ext->skip[type]))
1612 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1613 isl_error_internal, "can only restrict affine skips",
1614 return pet_scop_free(scop));
1616 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1617 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1618 cond = isl_set_copy(cond);
1619 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1620 skip = indicator_function(cond, dom);
1621 isl_multi_pw_aff_free(ext->skip[type]);
1622 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1623 if (!ext->skip[type])
1624 return pet_scop_free(scop);
1626 return scop;
1629 /* Adjust the context and the skip conditions to the fact that
1630 * the scop was created in a context where "cond" holds.
1632 * An outer loop iterator or parameter value is valid for the result
1633 * if it was valid for the original scop and satisfies "cond" or if it does
1634 * not satisfy "cond" as in this case the scop is not executed
1635 * and the original constraints on these values are irrelevant.
1637 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1638 __isl_take isl_set *cond)
1640 int i;
1642 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1643 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1645 if (!scop)
1646 goto error;
1648 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1649 scop->context = isl_set_union(scop->context,
1650 isl_set_complement(isl_set_copy(cond)));
1651 scop->context = isl_set_coalesce(scop->context);
1652 scop->context = pet_nested_remove_from_set(scop->context);
1653 if (!scop->context)
1654 goto error;
1656 isl_set_free(cond);
1657 return scop;
1658 error:
1659 isl_set_free(cond);
1660 return pet_scop_free(scop);
1663 /* Insert an argument expression corresponding to "test" in front
1664 * of the list of arguments described by *n_arg and *args.
1666 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1667 __isl_keep isl_multi_pw_aff *test)
1669 int i;
1670 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1672 if (!test)
1673 return -1;
1675 if (!*args) {
1676 *args = isl_calloc_array(ctx, pet_expr *, 1);
1677 if (!*args)
1678 return -1;
1679 } else {
1680 pet_expr **ext;
1681 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1682 if (!ext)
1683 return -1;
1684 for (i = 0; i < *n_arg; ++i)
1685 ext[1 + i] = (*args)[i];
1686 free(*args);
1687 *args = ext;
1689 (*n_arg)++;
1690 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1691 if (!(*args)[0])
1692 return -1;
1694 return 0;
1697 /* Look through the applications in "scop" for any that can be
1698 * applied to the filter expressed by "map" and "satisified".
1699 * If there is any, then apply it to "map" and return the result.
1700 * Otherwise, return "map".
1701 * "id" is the identifier of the virtual array.
1703 * We only introduce at most one implication for any given virtual array,
1704 * so we can apply the implication and return as soon as we find one.
1706 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1707 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1709 int i;
1711 for (i = 0; i < scop->n_implication; ++i) {
1712 struct pet_implication *pi = scop->implications[i];
1713 isl_id *pi_id;
1715 if (pi->satisfied != satisfied)
1716 continue;
1717 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1718 isl_id_free(pi_id);
1719 if (pi_id != id)
1720 continue;
1722 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1725 return map;
1728 /* Is the filter expressed by "test" and "satisfied" implied
1729 * by filter "pos" on "domain", with filter "expr", taking into
1730 * account the implications of "scop"?
1732 * For filter on domain implying that expressed by "test" and "satisfied",
1733 * the filter needs to be an access to the same (virtual) array as "test" and
1734 * the filter value needs to be equal to "satisfied".
1735 * Moreover, the filter access relation, possibly extended by
1736 * the implications in "scop" needs to contain "test".
1738 static int implies_filter(struct pet_scop *scop,
1739 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1740 __isl_keep isl_map *test, int satisfied)
1742 isl_id *test_id, *arg_id;
1743 isl_val *val;
1744 int is_int;
1745 int s;
1746 int is_subset;
1747 isl_map *implied;
1749 if (expr->type != pet_expr_access)
1750 return 0;
1751 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1752 arg_id = pet_expr_access_get_id(expr);
1753 isl_id_free(arg_id);
1754 isl_id_free(test_id);
1755 if (test_id != arg_id)
1756 return 0;
1757 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1758 is_int = isl_val_is_int(val);
1759 if (is_int)
1760 s = isl_val_get_num_si(val);
1761 isl_val_free(val);
1762 if (!val)
1763 return -1;
1764 if (!is_int)
1765 return 0;
1766 if (s != satisfied)
1767 return 0;
1769 implied = isl_map_copy(expr->acc.access);
1770 implied = apply_implications(scop, implied, test_id, satisfied);
1771 is_subset = isl_map_is_subset(test, implied);
1772 isl_map_free(implied);
1774 return is_subset;
1777 /* Is the filter expressed by "test" and "satisfied" implied
1778 * by any of the filters on the domain of "stmt", taking into
1779 * account the implications of "scop"?
1781 static int filter_implied(struct pet_scop *scop,
1782 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1784 int i;
1785 int implied;
1786 isl_id *test_id;
1787 isl_map *domain;
1788 isl_map *test_map;
1790 if (!scop || !stmt || !test)
1791 return -1;
1792 if (scop->n_implication == 0)
1793 return 0;
1794 if (stmt->n_arg == 0)
1795 return 0;
1797 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1798 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1800 implied = 0;
1801 for (i = 0; i < stmt->n_arg; ++i) {
1802 implied = implies_filter(scop, domain, i, stmt->args[i],
1803 test_map, satisfied);
1804 if (implied < 0 || implied)
1805 break;
1808 isl_map_free(test_map);
1809 isl_map_free(domain);
1810 return implied;
1813 /* Make the statement "stmt" depend on the value of "test"
1814 * being equal to "satisfied" by adjusting stmt->domain.
1816 * The domain of "test" corresponds to the (zero or more) outer dimensions
1817 * of the iteration domain.
1819 * We first extend "test" to apply to the entire iteration domain and
1820 * then check if the filter that we are about to add is implied
1821 * by any of the current filters, possibly taking into account
1822 * the implications in "scop". If so, we leave "stmt" untouched and return.
1824 * Otherwise, we insert an argument corresponding to a read to "test"
1825 * from the iteration domain of "stmt" in front of the list of arguments.
1826 * We also insert a corresponding output dimension in the wrapped
1827 * map contained in stmt->domain, with value set to "satisfied".
1829 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1830 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1832 int i;
1833 int implied;
1834 isl_id *id;
1835 isl_ctx *ctx;
1836 isl_pw_multi_aff *pma;
1837 isl_multi_aff *add_dom;
1838 isl_space *space;
1839 isl_local_space *ls;
1840 int n_test_dom;
1842 if (!stmt || !test)
1843 goto error;
1845 space = pet_stmt_get_space(stmt);
1846 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1847 space = isl_space_from_domain(space);
1848 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1849 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1850 ls = isl_local_space_from_space(isl_space_domain(space));
1851 for (i = 0; i < n_test_dom; ++i) {
1852 isl_aff *aff;
1853 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1854 isl_dim_set, i);
1855 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1857 isl_local_space_free(ls);
1858 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1860 implied = filter_implied(scop, stmt, test, satisfied);
1861 if (implied < 0)
1862 goto error;
1863 if (implied) {
1864 isl_multi_pw_aff_free(test);
1865 return stmt;
1868 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1869 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1870 id, satisfied);
1871 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1873 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1874 goto error;
1876 isl_multi_pw_aff_free(test);
1877 return stmt;
1878 error:
1879 isl_multi_pw_aff_free(test);
1880 return pet_stmt_free(stmt);
1883 /* Does "scop" have a skip condition of the given "type"?
1885 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1887 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1889 if (!scop)
1890 return -1;
1891 return ext->skip[type] != NULL;
1894 /* Does "scop" have a skip condition of the given "type" that
1895 * is an affine expression?
1897 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1899 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1901 if (!scop)
1902 return -1;
1903 if (!ext->skip[type])
1904 return 0;
1905 return multi_pw_aff_is_affine(ext->skip[type]);
1908 /* Does "scop" have a skip condition of the given "type" that
1909 * is not an affine expression?
1911 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1913 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1914 int aff;
1916 if (!scop)
1917 return -1;
1918 if (!ext->skip[type])
1919 return 0;
1920 aff = multi_pw_aff_is_affine(ext->skip[type]);
1921 if (aff < 0)
1922 return -1;
1923 return !aff;
1926 /* Does "scop" have a skip condition of the given "type" that
1927 * is affine and holds on the entire domain?
1929 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1931 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1932 isl_pw_aff *pa;
1933 isl_set *set;
1934 int is_aff;
1935 int is_univ;
1937 is_aff = pet_scop_has_affine_skip(scop, type);
1938 if (is_aff < 0 || !is_aff)
1939 return is_aff;
1941 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1942 set = isl_pw_aff_non_zero_set(pa);
1943 is_univ = isl_set_plain_is_universe(set);
1944 isl_set_free(set);
1946 return is_univ;
1949 /* Replace scop->skip[type] by "skip".
1951 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1952 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
1954 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1956 if (!scop || !skip)
1957 goto error;
1959 isl_multi_pw_aff_free(ext->skip[type]);
1960 ext->skip[type] = skip;
1962 return scop;
1963 error:
1964 isl_multi_pw_aff_free(skip);
1965 return pet_scop_free(scop);
1968 /* Return a copy of scop->skip[type].
1970 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
1971 enum pet_skip type)
1973 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1975 if (!scop)
1976 return NULL;
1978 return isl_multi_pw_aff_copy(ext->skip[type]);
1981 /* Assuming scop->skip[type] is an affine expression,
1982 * return the constraints on the outer loop domain for which the skip condition
1983 * holds.
1985 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
1986 enum pet_skip type)
1988 isl_multi_pw_aff *skip;
1989 isl_pw_aff *pa;
1991 skip = pet_scop_get_skip(scop, type);
1992 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1993 isl_multi_pw_aff_free(skip);
1994 return isl_pw_aff_non_zero_set(pa);
1997 /* Return the identifier of the variable that is accessed by
1998 * the skip condition of the given type.
2000 * The skip condition is assumed not to be an affine condition.
2002 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2003 enum pet_skip type)
2005 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2007 if (!scop)
2008 return NULL;
2010 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2013 /* Return an access pet_expr corresponding to the skip condition
2014 * of the given type.
2016 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2017 enum pet_skip type)
2019 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2022 /* Drop the the skip condition scop->skip[type].
2024 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2026 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2028 if (!scop)
2029 return;
2031 isl_multi_pw_aff_free(ext->skip[type]);
2032 ext->skip[type] = NULL;
2035 /* Make the skip condition (if any) depend on the value of "test" being
2036 * equal to "satisfied".
2038 * We only support the case where the original skip condition is universal,
2039 * i.e., where skipping is unconditional, and where satisfied == 1.
2040 * In this case, the skip condition is changed to skip only when
2041 * "test" is equal to one.
2043 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2044 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2046 int is_univ = 0;
2048 if (!scop)
2049 return NULL;
2050 if (!pet_scop_has_skip(scop, type))
2051 return scop;
2053 if (satisfied)
2054 is_univ = pet_scop_has_universal_skip(scop, type);
2055 if (is_univ < 0)
2056 return pet_scop_free(scop);
2057 if (satisfied && is_univ) {
2058 isl_multi_pw_aff *skip;
2059 skip = isl_multi_pw_aff_copy(test);
2060 scop = pet_scop_set_skip(scop, type, skip);
2061 if (!scop)
2062 return NULL;
2063 } else {
2064 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2065 "skip expression cannot be filtered",
2066 return pet_scop_free(scop));
2069 return scop;
2072 /* Make all statements in "scop" depend on the value of "test"
2073 * being equal to "satisfied" by adjusting their domains.
2075 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2076 __isl_take isl_multi_pw_aff *test, int satisfied)
2078 int i;
2080 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2081 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2083 if (!scop || !test)
2084 goto error;
2086 for (i = 0; i < scop->n_stmt; ++i) {
2087 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2088 isl_multi_pw_aff_copy(test), satisfied);
2089 if (!scop->stmts[i])
2090 goto error;
2093 isl_multi_pw_aff_free(test);
2094 return scop;
2095 error:
2096 isl_multi_pw_aff_free(test);
2097 return pet_scop_free(scop);
2100 /* Add the parameters of the access expression "expr" to "space".
2102 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2104 int i;
2105 isl_space **space = user;
2107 *space = isl_space_align_params(*space,
2108 isl_map_get_space(expr->acc.access));
2110 return *space ? 0 : -1;
2113 /* Add all parameters in "stmt" to "space" and return the result.
2115 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2116 __isl_take isl_space *space)
2118 int i;
2120 if (!stmt)
2121 return isl_space_free(space);
2123 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2124 space = isl_space_align_params(space,
2125 isl_map_get_space(stmt->schedule));
2126 for (i = 0; i < stmt->n_arg; ++i)
2127 if (pet_expr_foreach_access_expr(stmt->args[i],
2128 &access_collect_params, &space) < 0)
2129 space = isl_space_free(space);
2130 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2131 &space) < 0)
2132 space = isl_space_free(space);
2134 return space;
2137 /* Add all parameters in "array" to "space" and return the result.
2139 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2140 __isl_take isl_space *space)
2142 if (!array)
2143 return isl_space_free(space);
2145 space = isl_space_align_params(space,
2146 isl_set_get_space(array->context));
2147 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2149 return space;
2152 /* Add all parameters in "independence" to "space" and return the result.
2154 static __isl_give isl_space *independence_collect_params(
2155 struct pet_independence *independence, __isl_take isl_space *space)
2157 if (!independence)
2158 return isl_space_free(space);
2160 space = isl_space_align_params(space,
2161 isl_union_map_get_space(independence->filter));
2162 space = isl_space_align_params(space,
2163 isl_union_set_get_space(independence->local));
2165 return space;
2168 /* Add all parameters in "scop" to "space" and return the result.
2170 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2171 __isl_take isl_space *space)
2173 int i;
2175 if (!scop)
2176 return isl_space_free(space);
2178 for (i = 0; i < scop->n_array; ++i)
2179 space = array_collect_params(scop->arrays[i], space);
2181 for (i = 0; i < scop->n_stmt; ++i)
2182 space = stmt_collect_params(scop->stmts[i], space);
2184 for (i = 0; i < scop->n_independence; ++i)
2185 space = independence_collect_params(scop->independences[i],
2186 space);
2188 return space;
2191 /* Add all parameters in "space" to the domain, schedule and
2192 * all access relations in "stmt".
2194 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2195 __isl_take isl_space *space)
2197 int i;
2199 if (!stmt)
2200 goto error;
2202 stmt->domain = isl_set_align_params(stmt->domain,
2203 isl_space_copy(space));
2204 stmt->schedule = isl_map_align_params(stmt->schedule,
2205 isl_space_copy(space));
2207 for (i = 0; i < stmt->n_arg; ++i) {
2208 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2209 isl_space_copy(space));
2210 if (!stmt->args[i])
2211 goto error;
2213 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2215 if (!stmt->domain || !stmt->schedule || !stmt->body)
2216 goto error;
2218 isl_space_free(space);
2219 return stmt;
2220 error:
2221 isl_space_free(space);
2222 return pet_stmt_free(stmt);
2225 /* Add all parameters in "space" to "array".
2227 static struct pet_array *array_propagate_params(struct pet_array *array,
2228 __isl_take isl_space *space)
2230 if (!array)
2231 goto error;
2233 array->context = isl_set_align_params(array->context,
2234 isl_space_copy(space));
2235 array->extent = isl_set_align_params(array->extent,
2236 isl_space_copy(space));
2237 if (array->value_bounds) {
2238 array->value_bounds = isl_set_align_params(array->value_bounds,
2239 isl_space_copy(space));
2240 if (!array->value_bounds)
2241 goto error;
2244 if (!array->context || !array->extent)
2245 goto error;
2247 isl_space_free(space);
2248 return array;
2249 error:
2250 isl_space_free(space);
2251 return pet_array_free(array);
2254 /* Add all parameters in "space" to "independence".
2256 static struct pet_independence *independence_propagate_params(
2257 struct pet_independence *independence, __isl_take isl_space *space)
2259 if (!independence)
2260 goto error;
2262 independence->filter = isl_union_map_align_params(independence->filter,
2263 isl_space_copy(space));
2264 independence->local = isl_union_set_align_params(independence->local,
2265 isl_space_copy(space));
2266 if (!independence->filter || !independence->local)
2267 goto error;
2269 isl_space_free(space);
2270 return independence;
2271 error:
2272 isl_space_free(space);
2273 return pet_independence_free(independence);
2276 /* Add all parameters in "space" to "scop".
2278 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2279 __isl_take isl_space *space)
2281 int i;
2283 if (!scop)
2284 goto error;
2286 for (i = 0; i < scop->n_array; ++i) {
2287 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2288 isl_space_copy(space));
2289 if (!scop->arrays[i])
2290 goto error;
2293 for (i = 0; i < scop->n_stmt; ++i) {
2294 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2295 isl_space_copy(space));
2296 if (!scop->stmts[i])
2297 goto error;
2300 for (i = 0; i < scop->n_independence; ++i) {
2301 scop->independences[i] = independence_propagate_params(
2302 scop->independences[i], isl_space_copy(space));
2303 if (!scop->independences[i])
2304 goto error;
2307 isl_space_free(space);
2308 return scop;
2309 error:
2310 isl_space_free(space);
2311 return pet_scop_free(scop);
2314 /* Update all isl_sets and isl_maps in "scop" such that they all
2315 * have the same parameters.
2317 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2319 isl_space *space;
2321 if (!scop)
2322 return NULL;
2324 space = isl_set_get_space(scop->context);
2325 space = scop_collect_params(scop, space);
2327 scop->context = isl_set_align_params(scop->context,
2328 isl_space_copy(space));
2329 scop = scop_propagate_params(scop, space);
2331 if (scop && !scop->context)
2332 return pet_scop_free(scop);
2334 return scop;
2337 /* Add the access relation of the access expression "expr" to "accesses" and
2338 * return the result.
2339 * The domain of the access relation is intersected with "domain".
2340 * If "tag" is set, then the access relation is tagged with
2341 * the corresponding reference identifier.
2343 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2344 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2346 isl_map *access;
2348 access = pet_expr_access_get_may_access(expr);
2349 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2350 if (tag)
2351 access = pet_expr_tag_access(expr, access);
2352 return isl_union_map_add_map(accesses, access);
2355 /* Internal data structure for expr_collect_accesses.
2357 * "read" is set if we want to collect read accesses.
2358 * "write" is set if we want to collect write accesses.
2359 * "must" is set if we only want definite accesses.
2360 * "tag" is set if the access relations should be tagged with
2361 * the corresponding reference identifiers.
2362 * "domain" are constraints on the domain of the access relations.
2363 * "accesses" collects the results.
2365 struct pet_expr_collect_accesses_data {
2366 int read;
2367 int write;
2368 int must;
2369 int tag;
2370 isl_set *domain;
2372 isl_union_map *accesses;
2375 /* Add the access relation of the access expression "expr"
2376 * to data->accesses if the access expression is a read and data->read is set
2377 * and/or it is a write and data->write is set.
2378 * The domains of the access relations are intersected with data->domain.
2379 * If data->tag is set, then the access relations are tagged with
2380 * the corresponding reference identifiers.
2382 * If data->must is set, then we only add the accesses that are definitely
2383 * performed. Otherwise, we add all potential accesses.
2384 * In particular, if the access has any arguments, then if data->must is
2385 * set we currently skip the access completely. If data->must is not set,
2386 * we project out the values of the access arguments.
2388 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2390 struct pet_expr_collect_accesses_data *data = user;
2391 int i;
2392 isl_id *id;
2393 isl_space *dim;
2395 if (!expr)
2396 return -1;
2398 if (pet_expr_is_affine(expr))
2399 return 0;
2400 if (data->must && expr->n_arg != 0)
2401 return 0;
2403 if ((data->read && expr->acc.read) || (data->write && expr->acc.write))
2404 data->accesses = expr_collect_access(expr, data->tag,
2405 data->accesses, data->domain);
2407 return data->accesses ? 0 : -1;
2410 /* Collect and return all read access relations (if "read" is set)
2411 * and/or all write access relations (if "write" is set) in "stmt".
2412 * If "tag" is set, then the access relations are tagged with
2413 * the corresponding reference identifiers.
2414 * If "kill" is set, then "stmt" is a kill statement and we simply
2415 * add the argument of the kill operation.
2417 * If "must" is set, then we only add the accesses that are definitely
2418 * performed. Otherwise, we add all potential accesses.
2419 * In particular, if the statement has any arguments, then if "must" is
2420 * set we currently skip the statement completely. If "must" is not set,
2421 * we project out the values of the statement arguments.
2422 * If the statement body is not an expression tree, then we cannot
2423 * know for sure if/when the accesses inside the tree are performed.
2424 * We therefore ignore such statements when "must" is set.
2426 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2427 int read, int write, int kill, int must, int tag,
2428 __isl_take isl_space *dim)
2430 struct pet_expr_collect_accesses_data data = { read, write, must, tag };
2432 if (!stmt)
2433 return NULL;
2435 data.accesses = isl_union_map_empty(dim);
2437 if (must && stmt->n_arg > 0)
2438 return data.accesses;
2439 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2440 return data.accesses;
2442 data.domain = isl_set_copy(stmt->domain);
2443 if (isl_set_is_wrapping(data.domain))
2444 data.domain = isl_map_domain(isl_set_unwrap(data.domain));
2446 if (kill) {
2447 pet_expr *body, *arg;
2449 body = pet_tree_expr_get_expr(stmt->body);
2450 arg = pet_expr_get_arg(body, 0);
2451 data.accesses = expr_collect_access(arg, tag,
2452 data.accesses, data.domain);
2453 pet_expr_free(arg);
2454 pet_expr_free(body);
2455 } else if (pet_tree_foreach_access_expr(stmt->body,
2456 &expr_collect_accesses, &data) < 0)
2457 data.accesses = isl_union_map_free(data.accesses);
2459 isl_set_free(data.domain);
2461 return data.accesses;
2464 /* Is "stmt" an assignment statement?
2466 int pet_stmt_is_assign(struct pet_stmt *stmt)
2468 if (!stmt)
2469 return 0;
2470 return pet_tree_is_assign(stmt->body);
2473 /* Is "stmt" a kill statement?
2475 int pet_stmt_is_kill(struct pet_stmt *stmt)
2477 if (!stmt)
2478 return 0;
2479 return pet_tree_is_kill(stmt->body);
2482 /* Is "stmt" an assume statement?
2484 int pet_stmt_is_assume(struct pet_stmt *stmt)
2486 if (!stmt)
2487 return 0;
2488 return pet_tree_is_assume(stmt->body);
2491 /* Compute a mapping from all arrays (of structs) in scop
2492 * to their innermost arrays.
2494 * In particular, for each array of a primitive type, the result
2495 * contains the identity mapping on that array.
2496 * For each array involving member accesses, the result
2497 * contains a mapping from the elements of any intermediate array of structs
2498 * to all corresponding elements of the innermost nested arrays.
2500 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2502 int i;
2503 isl_union_map *to_inner;
2505 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2507 for (i = 0; i < scop->n_array; ++i) {
2508 struct pet_array *array = scop->arrays[i];
2509 isl_set *set;
2510 isl_map *map, *gist;
2512 if (array->element_is_record)
2513 continue;
2515 map = isl_set_identity(isl_set_copy(array->extent));
2517 set = isl_map_domain(isl_map_copy(map));
2518 gist = isl_map_copy(map);
2519 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2520 to_inner = isl_union_map_add_map(to_inner, gist);
2522 while (set && isl_set_is_wrapping(set)) {
2523 isl_id *id;
2524 isl_map *wrapped;
2526 id = isl_set_get_tuple_id(set);
2527 wrapped = isl_set_unwrap(set);
2528 wrapped = isl_map_domain_map(wrapped);
2529 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2530 map = isl_map_apply_domain(map, wrapped);
2531 set = isl_map_domain(isl_map_copy(map));
2532 gist = isl_map_copy(map);
2533 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2534 to_inner = isl_union_map_add_map(to_inner, gist);
2537 isl_set_free(set);
2538 isl_map_free(map);
2541 return to_inner;
2544 /* Collect and return all read access relations (if "read" is set)
2545 * and/or all write access relations (if "write" is set) in "scop".
2546 * If "kill" is set, then we only add the arguments of kill operations.
2547 * If "must" is set, then we only add the accesses that are definitely
2548 * performed. Otherwise, we add all potential accesses.
2549 * If "tag" is set, then the access relations are tagged with
2550 * the corresponding reference identifiers.
2551 * For accesses to structures, the returned access relation accesses
2552 * all individual fields in the structures.
2554 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2555 int read, int write, int kill, int must, int tag)
2557 int i;
2558 isl_union_map *accesses;
2559 isl_union_set *arrays;
2560 isl_union_map *to_inner;
2562 if (!scop)
2563 return NULL;
2565 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2567 for (i = 0; i < scop->n_stmt; ++i) {
2568 struct pet_stmt *stmt = scop->stmts[i];
2569 isl_union_map *accesses_i;
2570 isl_space *space;
2572 if (kill && !pet_stmt_is_kill(stmt))
2573 continue;
2575 space = isl_set_get_space(scop->context);
2576 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2577 must, tag, space);
2578 accesses = isl_union_map_union(accesses, accesses_i);
2581 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2582 for (i = 0; i < scop->n_array; ++i) {
2583 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2584 arrays = isl_union_set_add_set(arrays, extent);
2586 accesses = isl_union_map_intersect_range(accesses, arrays);
2588 to_inner = compute_to_inner(scop);
2589 accesses = isl_union_map_apply_range(accesses, to_inner);
2591 return accesses;
2594 /* Collect all potential read access relations.
2596 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2598 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2601 /* Collect all potential write access relations.
2603 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2605 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2608 /* Collect all definite write access relations.
2610 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2612 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2615 /* Collect all definite kill access relations.
2617 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2619 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2622 /* Collect all tagged potential read access relations.
2624 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2625 struct pet_scop *scop)
2627 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2630 /* Collect all tagged potential write access relations.
2632 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2633 struct pet_scop *scop)
2635 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2638 /* Collect all tagged definite write access relations.
2640 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2641 struct pet_scop *scop)
2643 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2646 /* Collect all tagged definite kill access relations.
2648 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2649 struct pet_scop *scop)
2651 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2654 /* Collect and return the union of iteration domains in "scop".
2656 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2658 int i;
2659 isl_set *domain_i;
2660 isl_union_set *domain;
2662 if (!scop)
2663 return NULL;
2665 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2667 for (i = 0; i < scop->n_stmt; ++i) {
2668 domain_i = isl_set_copy(scop->stmts[i]->domain);
2669 domain = isl_union_set_add_set(domain, domain_i);
2672 return domain;
2675 /* Collect and return the schedules of the statements in "scop".
2676 * The range is normalized to the maximal number of scheduling
2677 * dimensions.
2679 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2681 int i, j;
2682 isl_map *schedule_i;
2683 isl_union_map *schedule;
2684 int depth, max_depth = 0;
2686 if (!scop)
2687 return NULL;
2689 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2691 for (i = 0; i < scop->n_stmt; ++i) {
2692 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2693 if (depth > max_depth)
2694 max_depth = depth;
2697 for (i = 0; i < scop->n_stmt; ++i) {
2698 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2699 depth = isl_map_dim(schedule_i, isl_dim_out);
2700 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2701 max_depth - depth);
2702 for (j = depth; j < max_depth; ++j)
2703 schedule_i = isl_map_fix_si(schedule_i,
2704 isl_dim_out, j, 0);
2705 schedule = isl_union_map_add_map(schedule, schedule_i);
2708 return schedule;
2711 /* Add a reference identifier to all access expressions in "stmt".
2712 * "n_ref" points to an integer that contains the sequence number
2713 * of the next reference.
2715 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2717 int i;
2719 if (!stmt)
2720 return NULL;
2722 for (i = 0; i < stmt->n_arg; ++i) {
2723 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2724 if (!stmt->args[i])
2725 return pet_stmt_free(stmt);
2728 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2729 if (!stmt->body)
2730 return pet_stmt_free(stmt);
2732 return stmt;
2735 /* Add a reference identifier to all access expressions in "scop".
2737 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2739 int i;
2740 int n_ref;
2742 if (!scop)
2743 return NULL;
2745 n_ref = 0;
2746 for (i = 0; i < scop->n_stmt; ++i) {
2747 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2748 if (!scop->stmts[i])
2749 return pet_scop_free(scop);
2752 return scop;
2755 /* Reset the user pointer on all parameter ids in "array".
2757 static struct pet_array *array_anonymize(struct pet_array *array)
2759 if (!array)
2760 return NULL;
2762 array->context = isl_set_reset_user(array->context);
2763 array->extent = isl_set_reset_user(array->extent);
2764 if (!array->context || !array->extent)
2765 return pet_array_free(array);
2767 return array;
2770 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2772 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2774 int i;
2775 isl_space *space;
2776 isl_set *domain;
2778 if (!stmt)
2779 return NULL;
2781 stmt->domain = isl_set_reset_user(stmt->domain);
2782 stmt->schedule = isl_map_reset_user(stmt->schedule);
2783 if (!stmt->domain || !stmt->schedule)
2784 return pet_stmt_free(stmt);
2786 for (i = 0; i < stmt->n_arg; ++i) {
2787 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2788 if (!stmt->args[i])
2789 return pet_stmt_free(stmt);
2792 stmt->body = pet_tree_anonymize(stmt->body);
2793 if (!stmt->body)
2794 return pet_stmt_free(stmt);
2796 return stmt;
2799 /* Reset the user pointer on the tuple ids and all parameter ids
2800 * in "implication".
2802 static struct pet_implication *implication_anonymize(
2803 struct pet_implication *implication)
2805 if (!implication)
2806 return NULL;
2808 implication->extension = isl_map_reset_user(implication->extension);
2809 if (!implication->extension)
2810 return pet_implication_free(implication);
2812 return implication;
2815 /* Reset the user pointer on the tuple ids and all parameter ids
2816 * in "independence".
2818 static struct pet_independence *independence_anonymize(
2819 struct pet_independence *independence)
2821 if (!independence)
2822 return NULL;
2824 independence->filter = isl_union_map_reset_user(independence->filter);
2825 independence->local = isl_union_set_reset_user(independence->local);
2826 if (!independence->filter || !independence->local)
2827 return pet_independence_free(independence);
2829 return independence;
2832 /* Reset the user pointer on all parameter and tuple ids in "scop".
2834 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2836 int i;
2838 if (!scop)
2839 return NULL;
2841 scop->context = isl_set_reset_user(scop->context);
2842 scop->context_value = isl_set_reset_user(scop->context_value);
2843 if (!scop->context || !scop->context_value)
2844 return pet_scop_free(scop);
2846 for (i = 0; i < scop->n_array; ++i) {
2847 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2848 if (!scop->arrays[i])
2849 return pet_scop_free(scop);
2852 for (i = 0; i < scop->n_stmt; ++i) {
2853 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2854 if (!scop->stmts[i])
2855 return pet_scop_free(scop);
2858 for (i = 0; i < scop->n_implication; ++i) {
2859 scop->implications[i] =
2860 implication_anonymize(scop->implications[i]);
2861 if (!scop->implications[i])
2862 return pet_scop_free(scop);
2865 for (i = 0; i < scop->n_independence; ++i) {
2866 scop->independences[i] =
2867 independence_anonymize(scop->independences[i]);
2868 if (!scop->independences[i])
2869 return pet_scop_free(scop);
2872 return scop;
2875 /* Compute the gist of the iteration domain and all access relations
2876 * of "stmt" based on the constraints on the parameters specified by "context"
2877 * and the constraints on the values of nested accesses specified
2878 * by "value_bounds".
2880 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2881 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2883 int i;
2884 isl_set *domain;
2886 if (!stmt)
2887 return NULL;
2889 domain = isl_set_copy(stmt->domain);
2890 if (stmt->n_arg > 0)
2891 domain = isl_map_domain(isl_set_unwrap(domain));
2893 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2895 for (i = 0; i < stmt->n_arg; ++i) {
2896 stmt->args[i] = pet_expr_gist(stmt->args[i],
2897 domain, value_bounds);
2898 if (!stmt->args[i])
2899 goto error;
2902 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
2903 if (!stmt->body)
2904 goto error;
2906 isl_set_free(domain);
2908 domain = isl_set_universe(pet_stmt_get_space(stmt));
2909 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2910 if (stmt->n_arg > 0)
2911 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
2912 value_bounds);
2913 stmt->domain = isl_set_gist(stmt->domain, domain);
2914 if (!stmt->domain)
2915 return pet_stmt_free(stmt);
2917 return stmt;
2918 error:
2919 isl_set_free(domain);
2920 return pet_stmt_free(stmt);
2923 /* Compute the gist of the extent of the array
2924 * based on the constraints on the parameters specified by "context".
2926 static struct pet_array *array_gist(struct pet_array *array,
2927 __isl_keep isl_set *context)
2929 if (!array)
2930 return NULL;
2932 array->extent = isl_set_gist_params(array->extent,
2933 isl_set_copy(context));
2934 if (!array->extent)
2935 return pet_array_free(array);
2937 return array;
2940 /* Compute the gist of all sets and relations in "scop"
2941 * based on the constraints on the parameters specified by "scop->context"
2942 * and the constraints on the values of nested accesses specified
2943 * by "value_bounds".
2945 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
2946 __isl_keep isl_union_map *value_bounds)
2948 int i;
2950 if (!scop)
2951 return NULL;
2953 scop->context = isl_set_coalesce(scop->context);
2954 if (!scop->context)
2955 return pet_scop_free(scop);
2957 for (i = 0; i < scop->n_array; ++i) {
2958 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
2959 if (!scop->arrays[i])
2960 return pet_scop_free(scop);
2963 for (i = 0; i < scop->n_stmt; ++i) {
2964 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
2965 value_bounds);
2966 if (!scop->stmts[i])
2967 return pet_scop_free(scop);
2970 return scop;
2973 /* Intersect the context of "scop" with "context".
2974 * To ensure that we don't introduce any unnamed parameters in
2975 * the context of "scop", we first remove the unnamed parameters
2976 * from "context".
2978 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
2979 __isl_take isl_set *context)
2981 if (!scop)
2982 goto error;
2984 context = pet_nested_remove_from_set(context);
2985 scop->context = isl_set_intersect(scop->context, context);
2986 if (!scop->context)
2987 return pet_scop_free(scop);
2989 return scop;
2990 error:
2991 isl_set_free(context);
2992 return pet_scop_free(scop);
2995 /* Drop the current context of "scop". That is, replace the context
2996 * by a universal set.
2998 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3000 isl_space *space;
3002 if (!scop)
3003 return NULL;
3005 space = isl_set_get_space(scop->context);
3006 isl_set_free(scop->context);
3007 scop->context = isl_set_universe(space);
3008 if (!scop->context)
3009 return pet_scop_free(scop);
3011 return scop;
3014 /* Append "array" to the arrays of "scop".
3016 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3017 struct pet_array *array)
3019 isl_ctx *ctx;
3020 struct pet_array **arrays;
3022 if (!array || !scop)
3023 goto error;
3025 ctx = isl_set_get_ctx(scop->context);
3026 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3027 scop->n_array + 1);
3028 if (!arrays)
3029 goto error;
3030 scop->arrays = arrays;
3031 scop->arrays[scop->n_array] = array;
3032 scop->n_array++;
3034 return scop;
3035 error:
3036 pet_array_free(array);
3037 return pet_scop_free(scop);
3040 /* Create an index expression for an access to a virtual array
3041 * representing the result of a condition.
3042 * Unlike other accessed data, the id of the array is NULL as
3043 * there is no ValueDecl in the program corresponding to the virtual
3044 * array.
3045 * The index expression is created as an identity mapping on "space".
3046 * That is, the dimension of the array is the same as that of "space".
3048 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3049 int test_nr)
3051 isl_id *id;
3052 char name[50];
3054 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3055 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3056 space = isl_space_map_from_set(space);
3057 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3058 return isl_multi_pw_aff_identity(space);
3061 /* Add an array with the given extent to the list
3062 * of arrays in "scop" and return the extended pet_scop.
3063 * Specifically, the extent is determined by the image of "domain"
3064 * under "index".
3065 * "int_size" is the number of bytes needed to represent values of type "int".
3066 * The array is marked as attaining values 0 and 1 only and
3067 * as each element being assigned at most once.
3069 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3070 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3071 int int_size)
3073 isl_ctx *ctx;
3074 isl_space *space;
3075 struct pet_array *array;
3076 isl_map *access;
3078 if (!scop || !domain || !index)
3079 goto error;
3081 ctx = isl_multi_pw_aff_get_ctx(index);
3082 array = isl_calloc_type(ctx, struct pet_array);
3083 if (!array)
3084 goto error;
3086 access = isl_map_from_multi_pw_aff(index);
3087 access = isl_map_intersect_domain(access, domain);
3088 array->extent = isl_map_range(access);
3089 space = isl_space_params_alloc(ctx, 0);
3090 array->context = isl_set_universe(space);
3091 space = isl_space_set_alloc(ctx, 0, 1);
3092 array->value_bounds = isl_set_universe(space);
3093 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3094 isl_dim_set, 0, 0);
3095 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3096 isl_dim_set, 0, 1);
3097 array->element_type = strdup("int");
3098 array->element_size = int_size;
3099 array->uniquely_defined = 1;
3101 if (!array->extent || !array->context)
3102 array = pet_array_free(array);
3104 scop = pet_scop_add_array(scop, array);
3106 return scop;
3107 error:
3108 isl_set_free(domain);
3109 isl_multi_pw_aff_free(index);
3110 return pet_scop_free(scop);
3113 /* Create and return an implication on filter values equal to "satisfied"
3114 * with extension "map".
3116 static struct pet_implication *new_implication(__isl_take isl_map *map,
3117 int satisfied)
3119 isl_ctx *ctx;
3120 struct pet_implication *implication;
3122 if (!map)
3123 return NULL;
3124 ctx = isl_map_get_ctx(map);
3125 implication = isl_alloc_type(ctx, struct pet_implication);
3126 if (!implication)
3127 goto error;
3129 implication->extension = map;
3130 implication->satisfied = satisfied;
3132 return implication;
3133 error:
3134 isl_map_free(map);
3135 return NULL;
3138 /* Add an implication on filter values equal to "satisfied"
3139 * with extension "map" to "scop".
3141 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3142 __isl_take isl_map *map, int satisfied)
3144 isl_ctx *ctx;
3145 struct pet_implication *implication;
3146 struct pet_implication **implications;
3148 implication = new_implication(map, satisfied);
3149 if (!scop || !implication)
3150 goto error;
3152 ctx = isl_set_get_ctx(scop->context);
3153 implications = isl_realloc_array(ctx, scop->implications,
3154 struct pet_implication *,
3155 scop->n_implication + 1);
3156 if (!implications)
3157 goto error;
3158 scop->implications = implications;
3159 scop->implications[scop->n_implication] = implication;
3160 scop->n_implication++;
3162 return scop;
3163 error:
3164 pet_implication_free(implication);
3165 return pet_scop_free(scop);
3168 /* Create and return a function that maps the iteration domains
3169 * of the statements in "scop" onto their outer "n" dimensions.
3170 * "space" is the parameters space of the created function.
3172 static __isl_give isl_union_pw_multi_aff *outer_projection(
3173 struct pet_scop *scop, __isl_take isl_space *space, int n)
3175 int i;
3176 isl_union_pw_multi_aff *res;
3178 res = isl_union_pw_multi_aff_empty(space);
3180 if (!scop)
3181 return isl_union_pw_multi_aff_free(res);
3183 for (i = 0; i < scop->n_stmt; ++i) {
3184 struct pet_stmt *stmt = scop->stmts[i];
3185 isl_space *space;
3186 isl_multi_aff *ma;
3187 isl_pw_multi_aff *pma;
3189 space = pet_stmt_get_space(stmt);
3190 ma = pet_prefix_projection(space, n);
3191 pma = isl_pw_multi_aff_from_multi_aff(ma);
3192 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3195 return res;
3198 /* Add an independence to "scop" for the inner iterator of "domain"
3199 * with local variables "local", where "domain" represents the outer
3200 * loop iterators of all statements in "scop".
3201 * If "sign" is positive, then the inner iterator increases.
3202 * Otherwise it decreases.
3204 * The independence is supposed to filter out any dependence of
3205 * an iteration of domain on a previous iteration along the inner dimension.
3206 * We therefore create a mapping from an iteration to later iterations and
3207 * then plug in the projection of the iterations domains of "scop"
3208 * onto the outer loop iterators.
3210 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3211 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3213 int i, dim;
3214 isl_space *space;
3215 isl_map *map;
3216 isl_union_map *independence;
3217 isl_union_pw_multi_aff *proj;
3219 if (!scop || !domain || !local)
3220 goto error;
3222 dim = isl_set_dim(domain, isl_dim_set);
3223 space = isl_space_map_from_set(isl_set_get_space(domain));
3224 map = isl_map_universe(space);
3225 for (i = 0; i + 1 < dim; ++i)
3226 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3227 if (sign > 0)
3228 map = isl_map_order_lt(map,
3229 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3230 else
3231 map = isl_map_order_gt(map,
3232 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3234 independence = isl_union_map_from_map(map);
3235 space = isl_space_params(isl_set_get_space(domain));
3236 proj = outer_projection(scop, space, dim);
3237 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3238 independence, isl_union_pw_multi_aff_copy(proj));
3239 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3240 independence, proj);
3242 scop = pet_scop_add_independence(scop, independence, local);
3244 return scop;
3245 error:
3246 isl_union_set_free(local);
3247 return pet_scop_free(scop);
3250 /* Given an access expression, check if it is data dependent.
3251 * If so, set *found and abort the search.
3253 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3255 int *found = user;
3257 if (pet_expr_get_n_arg(expr) > 0) {
3258 *found = 1;
3259 return -1;
3262 return 0;
3265 /* Does "scop" contain any data dependent accesses?
3267 * Check the body of each statement for such accesses.
3269 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3271 int i;
3272 int found = 0;
3274 if (!scop)
3275 return -1;
3277 for (i = 0; i < scop->n_stmt; ++i) {
3278 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3279 &is_data_dependent, &found);
3280 if (r < 0 && !found)
3281 return -1;
3282 if (found)
3283 return found;
3286 return found;
3289 /* Does "scop" contain and data dependent conditions?
3291 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3293 int i;
3295 if (!scop)
3296 return -1;
3298 for (i = 0; i < scop->n_stmt; ++i)
3299 if (scop->stmts[i]->n_arg > 0)
3300 return 1;
3302 return 0;
3305 /* Keep track of the "input" file inside the (extended) "scop".
3307 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3309 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3311 if (!scop)
3312 return NULL;
3314 ext->input = input;
3316 return scop;
3319 /* Print the original code corresponding to "scop" to printer "p".
3321 * pet_scop_print_original can only be called from
3322 * a pet_transform_C_source callback. This means that the input
3323 * file is stored in the extended scop and that the printer prints
3324 * to a file.
3326 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3327 __isl_take isl_printer *p)
3329 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3330 FILE *output;
3331 unsigned start, end;
3333 if (!scop || !p)
3334 return isl_printer_free(p);
3336 if (!ext->input)
3337 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3338 "no input file stored in scop",
3339 return isl_printer_free(p));
3341 output = isl_printer_get_file(p);
3342 if (!output)
3343 return isl_printer_free(p);
3345 start = pet_loc_get_start(scop->loc);
3346 end = pet_loc_get_end(scop->loc);
3347 if (copy(ext->input, output, start, end) < 0)
3348 return isl_printer_free(p);
3350 return p;