postpone calls to pet_scop_embed until after calls to scop_add_while
[pet.git] / scop.c
blobeb1d8d18bf943e40e265f855f343e5d2df8d9680
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "aff.h"
40 #include "expr.h"
41 #include "expr_access_type.h"
42 #include "filter.h"
43 #include "loc.h"
44 #include "nest.h"
45 #include "scop.h"
46 #include "tree.h"
47 #include "print.h"
48 #include "value_bounds.h"
50 /* pet_scop with extra information that is used during parsing and printing.
52 * In particular, we keep track of conditions under which we want
53 * to skip the rest of the current loop iteration (skip[pet_skip_now])
54 * and of conditions under which we want to skip subsequent
55 * loop iterations (skip[pet_skip_later]).
57 * The conditions are represented as index expressions defined
58 * over the outer loop iterators. The index expression is either
59 * a boolean affine expression or an access to a variable, which
60 * is assumed to attain values zero and one. The condition holds
61 * if the variable has value one or if the affine expression
62 * has value one (typically for only part of the domain).
64 * A missing condition (skip[type] == NULL) means that we don't want
65 * to skip anything.
67 * Additionally, we keep track of the original input file
68 * inside pet_transform_C_source.
70 struct pet_scop_ext {
71 struct pet_scop scop;
73 isl_multi_pw_aff *skip[2];
74 FILE *input;
77 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
78 * The input domain is anonymous and is the same as the domains
79 * of the access expressions inside "tree".
80 * These domains are modified to include the name of the statement.
81 * This name is given by tree->label if it is non-NULL.
82 * Otherwise, the name is constructed as S_<id>.
84 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
85 int id, __isl_take pet_tree *tree)
87 struct pet_stmt *stmt;
88 isl_ctx *ctx;
89 isl_id *label;
90 isl_space *space;
91 isl_map *sched;
92 isl_multi_aff *ma;
93 isl_multi_pw_aff *add_name;
94 char name[50];
96 if (!domain || !tree)
97 goto error;
99 ctx = pet_tree_get_ctx(tree);
100 stmt = isl_calloc_type(ctx, struct pet_stmt);
101 if (!stmt)
102 goto error;
104 if (tree->label) {
105 label = isl_id_copy(tree->label);
106 } else {
107 snprintf(name, sizeof(name), "S_%d", id);
108 label = isl_id_alloc(ctx, name, NULL);
110 domain = isl_set_set_tuple_id(domain, label);
111 space = isl_set_get_space(domain);
112 space = pet_nested_remove_from_space(space);
113 sched = isl_map_universe(isl_space_from_domain(isl_space_copy(space)));
114 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
116 add_name = isl_multi_pw_aff_from_multi_aff(ma);
117 tree = pet_tree_update_domain(tree, add_name);
119 stmt->loc = pet_tree_get_loc(tree);
120 stmt->domain = domain;
121 stmt->schedule = sched;
122 stmt->body = tree;
124 if (!stmt->domain || !stmt->schedule || !stmt->body)
125 return pet_stmt_free(stmt);
127 return stmt;
128 error:
129 isl_set_free(domain);
130 pet_tree_free(tree);
131 return NULL;
134 void *pet_stmt_free(struct pet_stmt *stmt)
136 int i;
138 if (!stmt)
139 return NULL;
141 pet_loc_free(stmt->loc);
142 isl_set_free(stmt->domain);
143 isl_map_free(stmt->schedule);
144 pet_tree_free(stmt->body);
146 for (i = 0; i < stmt->n_arg; ++i)
147 pet_expr_free(stmt->args[i]);
148 free(stmt->args);
150 free(stmt);
151 return NULL;
154 /* Return the iteration space of "stmt".
156 * If the statement has arguments, then stmt->domain is a wrapped map
157 * mapping the iteration domain to the values of the arguments
158 * for which this statement is executed.
159 * In this case, we need to extract the domain space of this wrapped map.
161 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
163 isl_space *space;
165 if (!stmt)
166 return NULL;
168 space = isl_set_get_space(stmt->domain);
169 if (isl_space_is_wrapping(space))
170 space = isl_space_domain(isl_space_unwrap(space));
172 return space;
175 static void stmt_dump(struct pet_stmt *stmt, int indent)
177 int i;
179 if (!stmt)
180 return;
182 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
183 fprintf(stderr, "%*s", indent, "");
184 isl_set_dump(stmt->domain);
185 fprintf(stderr, "%*s", indent, "");
186 isl_map_dump(stmt->schedule);
187 pet_tree_dump_with_indent(stmt->body, indent);
188 for (i = 0; i < stmt->n_arg; ++i)
189 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
192 void pet_stmt_dump(struct pet_stmt *stmt)
194 stmt_dump(stmt, 0);
197 /* Allocate a new pet_type with the given "name" and "definition".
199 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
200 const char *definition)
202 struct pet_type *type;
204 type = isl_alloc_type(ctx, struct pet_type);
205 if (!type)
206 return NULL;
208 type->name = strdup(name);
209 type->definition = strdup(definition);
211 if (!type->name || !type->definition)
212 return pet_type_free(type);
214 return type;
217 /* Free "type" and return NULL.
219 struct pet_type *pet_type_free(struct pet_type *type)
221 if (!type)
222 return NULL;
224 free(type->name);
225 free(type->definition);
227 free(type);
228 return NULL;
231 struct pet_array *pet_array_free(struct pet_array *array)
233 if (!array)
234 return NULL;
236 isl_set_free(array->context);
237 isl_set_free(array->extent);
238 isl_set_free(array->value_bounds);
239 free(array->element_type);
241 free(array);
242 return NULL;
245 void pet_array_dump(struct pet_array *array)
247 if (!array)
248 return;
250 isl_set_dump(array->context);
251 isl_set_dump(array->extent);
252 isl_set_dump(array->value_bounds);
253 fprintf(stderr, "%s%s%s\n", array->element_type,
254 array->element_is_record ? " element-is-record" : "",
255 array->live_out ? " live-out" : "");
258 /* Alloc a pet_scop structure, with extra room for information that
259 * is only used during parsing.
261 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
263 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
266 /* Construct a pet_scop in the given space and with room for n statements.
268 * The context is initialized as a universe set in "space".
270 * Since no information on the location is known at this point,
271 * scop->loc is initialized with pet_loc_dummy.
273 static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n)
275 isl_ctx *ctx;
276 struct pet_scop *scop;
278 if (!space)
279 return NULL;
281 ctx = isl_space_get_ctx(space);
282 scop = pet_scop_alloc(ctx);
283 if (!scop)
284 goto error;
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;
296 error:
297 isl_space_free(space);
298 return NULL;
301 /* Construct a pet_scop in the given space containing 0 statements.
303 struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
305 return scop_alloc(space, 0);
308 /* Given either an iteration domain or a wrapped map with
309 * the iteration domain in the domain and some arguments
310 * in the range, return the iteration domain.
311 * That is, drop the arguments if there are any.
313 static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
315 if (isl_set_is_wrapping(domain))
316 domain = isl_map_domain(isl_set_unwrap(domain));
317 return domain;
320 /* Update "context" with the constraints imposed on the outer iteration
321 * domain by access expression "expr".
322 * "context" lives in an anonymous space, while the domain of the access
323 * relation of "expr" refers to a particular statement.
324 * This reference therefore needs to be stripped off.
326 static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
327 __isl_take isl_set *context)
329 isl_multi_pw_aff *mpa;
330 isl_set *domain;
332 mpa = pet_expr_access_get_index(expr);
333 domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
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 * If "expr" represents a kill statement, then its argument is the entire
354 * extent of the array being killed. Do not update "context" based
355 * on this argument as that would impose constraints that ensure that
356 * the array is non-empty.
358 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
359 __isl_take isl_set *context)
361 int i;
363 if (expr->type == pet_expr_op && expr->op == pet_op_kill)
364 return context;
366 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
367 int is_aff;
368 isl_set *context1, *context2;
370 is_aff = pet_expr_is_affine(expr->args[0]);
371 if (is_aff < 0)
372 goto error;
374 context = expr_extract_context(expr->args[0], context);
375 context1 = expr_extract_context(expr->args[1],
376 isl_set_copy(context));
377 context2 = expr_extract_context(expr->args[2], context);
379 if (is_aff) {
380 isl_multi_pw_aff *mpa;
381 isl_pw_aff *pa;
382 isl_set *zero_set;
384 mpa = pet_expr_access_get_index(expr->args[0]);
385 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
386 isl_multi_pw_aff_free(mpa);
387 zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
388 zero_set = isl_set_reset_tuple_id(zero_set);
389 context1 = isl_set_subtract(context1,
390 isl_set_copy(zero_set));
391 context2 = isl_set_intersect(context2, zero_set);
394 context = isl_set_union(context1, context2);
395 context = isl_set_coalesce(context);
397 return context;
400 for (i = 0; i < expr->n_arg; ++i)
401 context = expr_extract_context(expr->args[i], context);
403 if (expr->type == pet_expr_access)
404 context = access_extract_context(expr, context);
406 return context;
407 error:
408 isl_set_free(context);
409 return NULL;
412 /* Is "stmt" an assume statement with an affine assumption?
414 int pet_stmt_is_affine_assume(struct pet_stmt *stmt)
416 if (!stmt)
417 return 0;
418 return pet_tree_is_affine_assume(stmt->body);
421 /* Given an assume statement "stmt" with an access argument,
422 * return the index expression of the argument.
424 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
426 if (!stmt)
427 return NULL;
428 return pet_tree_assume_get_index(stmt->body);
431 /* Update "context" with the constraints imposed on the outer iteration
432 * domain by "stmt".
434 * If the statement is an assume statement with an affine expression,
435 * then intersect "context" with that expression.
436 * Otherwise, if the statement body is an expression tree,
437 * then intersect "context" with the context of this expression.
438 * Note that we cannot safely extract a context from subtrees
439 * of the statement body since we cannot tell when those subtrees
440 * are executed, if at all.
442 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
443 __isl_take isl_set *context)
445 int i;
446 pet_expr *body;
448 if (pet_stmt_is_affine_assume(stmt)) {
449 isl_multi_pw_aff *index;
450 isl_pw_aff *pa;
451 isl_set *cond;
453 index = pet_stmt_assume_get_index(stmt);
454 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
455 isl_multi_pw_aff_free(index);
456 cond = isl_pw_aff_non_zero_set(pa);
457 cond = isl_set_reset_tuple_id(cond);
458 return isl_set_intersect(context, cond);
461 for (i = 0; i < stmt->n_arg; ++i)
462 context = expr_extract_context(stmt->args[i], context);
464 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
465 return context;
467 body = pet_tree_expr_get_expr(stmt->body);
468 context = expr_extract_context(body, context);
469 pet_expr_free(body);
471 return context;
474 /* Construct a pet_scop in the given space that contains the given pet_stmt.
476 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
477 struct pet_stmt *stmt)
479 struct pet_scop *scop;
481 if (!stmt)
482 space = isl_space_free(space);
484 scop = scop_alloc(space, 1);
485 if (!scop)
486 goto error;
488 scop->context = stmt_extract_context(stmt, scop->context);
489 if (!scop->context)
490 goto error;
492 scop->stmts[0] = stmt;
493 scop->loc = pet_loc_copy(stmt->loc);
495 if (!scop->loc)
496 return pet_scop_free(scop);
498 return scop;
499 error:
500 pet_stmt_free(stmt);
501 pet_scop_free(scop);
502 return NULL;
505 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
506 * does it represent an affine expression?
508 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
510 int has_id;
512 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
513 if (has_id < 0)
514 return -1;
516 return !has_id;
519 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
521 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
522 __isl_take isl_set *dom)
524 isl_pw_aff *pa;
525 pa = isl_set_indicator_function(set);
526 pa = isl_pw_aff_intersect_domain(pa, dom);
527 return pa;
530 /* Return "lhs || rhs", defined on the shared definition domain.
532 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
533 __isl_take isl_pw_aff *rhs)
535 isl_set *cond;
536 isl_set *dom;
538 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
539 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
540 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
541 isl_pw_aff_non_zero_set(rhs));
542 cond = isl_set_coalesce(cond);
543 return indicator_function(cond, dom);
546 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
547 * ext may be equal to either ext1 or ext2.
549 * The two skips that need to be combined are assumed to be affine expressions.
551 * We need to skip in ext if we need to skip in either ext1 or ext2.
552 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
554 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
555 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
556 enum pet_skip type)
558 isl_pw_aff *skip, *skip1, *skip2;
560 if (!ext)
561 return NULL;
562 if (!ext1->skip[type] && !ext2->skip[type])
563 return ext;
564 if (!ext1->skip[type]) {
565 if (ext == ext2)
566 return ext;
567 ext->skip[type] = ext2->skip[type];
568 ext2->skip[type] = NULL;
569 return ext;
571 if (!ext2->skip[type]) {
572 if (ext == ext1)
573 return ext;
574 ext->skip[type] = ext1->skip[type];
575 ext1->skip[type] = NULL;
576 return ext;
579 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
580 !multi_pw_aff_is_affine(ext2->skip[type]))
581 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
582 isl_error_internal, "can only combine affine skips",
583 goto error);
585 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
586 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
587 skip = pw_aff_or(skip1, skip2);
588 isl_multi_pw_aff_free(ext1->skip[type]);
589 ext1->skip[type] = NULL;
590 isl_multi_pw_aff_free(ext2->skip[type]);
591 ext2->skip[type] = NULL;
592 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
593 if (!ext->skip[type])
594 goto error;
596 return ext;
597 error:
598 pet_scop_free(&ext->scop);
599 return NULL;
602 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
603 * where type takes on the values pet_skip_now and pet_skip_later.
604 * scop may be equal to either scop1 or scop2.
606 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
607 struct pet_scop *scop1, struct pet_scop *scop2)
609 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
610 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
611 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
613 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
614 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
615 return &ext->scop;
618 /* Update start and end of scop->loc to include the region from "start"
619 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
620 * does not have any offset information yet and we simply take the information
621 * from "start" and "end". Otherwise, we update loc using "start" and "end".
623 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
624 unsigned start, unsigned end)
626 if (!scop)
627 return NULL;
629 if (scop->loc == &pet_loc_dummy)
630 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
631 start, end, -1, strdup(""));
632 else
633 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
635 if (!scop->loc)
636 return pet_scop_free(scop);
638 return scop;
641 /* Update start and end of scop->loc to include the region identified
642 * by "loc".
644 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
645 __isl_keep pet_loc *loc)
647 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
648 pet_loc_get_end(loc));
651 /* Replace the location of "scop" by "loc".
653 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
654 __isl_take pet_loc *loc)
656 if (!scop || !loc)
657 goto error;
659 pet_loc_free(scop->loc);
660 scop->loc = loc;
662 return scop;
663 error:
664 pet_loc_free(loc);
665 pet_scop_free(scop);
666 return NULL;
669 /* Does "implication" appear in the list of implications of "scop"?
671 static int is_known_implication(struct pet_scop *scop,
672 struct pet_implication *implication)
674 int i;
676 for (i = 0; i < scop->n_implication; ++i) {
677 struct pet_implication *pi = scop->implications[i];
678 int equal;
680 if (pi->satisfied != implication->satisfied)
681 continue;
682 equal = isl_map_is_equal(pi->extension, implication->extension);
683 if (equal < 0)
684 return -1;
685 if (equal)
686 return 1;
689 return 0;
692 /* Store the concatenation of the implications of "scop1" and "scop2"
693 * in "scop", removing duplicates (i.e., implications in "scop2" that
694 * already appear in "scop1").
696 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
697 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
699 int i, j;
701 if (!scop)
702 return NULL;
704 if (scop2->n_implication == 0) {
705 scop->n_implication = scop1->n_implication;
706 scop->implications = scop1->implications;
707 scop1->n_implication = 0;
708 scop1->implications = NULL;
709 return scop;
712 if (scop1->n_implication == 0) {
713 scop->n_implication = scop2->n_implication;
714 scop->implications = scop2->implications;
715 scop2->n_implication = 0;
716 scop2->implications = NULL;
717 return scop;
720 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
721 scop1->n_implication + scop2->n_implication);
722 if (!scop->implications)
723 return pet_scop_free(scop);
725 for (i = 0; i < scop1->n_implication; ++i) {
726 scop->implications[i] = scop1->implications[i];
727 scop1->implications[i] = NULL;
730 scop->n_implication = scop1->n_implication;
731 j = scop1->n_implication;
732 for (i = 0; i < scop2->n_implication; ++i) {
733 int known;
735 known = is_known_implication(scop, scop2->implications[i]);
736 if (known < 0)
737 return pet_scop_free(scop);
738 if (known)
739 continue;
740 scop->implications[j++] = scop2->implications[i];
741 scop2->implications[i] = NULL;
743 scop->n_implication = j;
745 return scop;
748 /* Combine the offset information of "scop1" and "scop2" into "scop".
750 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
751 struct pet_scop *scop1, struct pet_scop *scop2)
753 if (scop1->loc != &pet_loc_dummy)
754 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
755 if (scop2->loc != &pet_loc_dummy)
756 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
757 return scop;
760 /* Create and return an independence that filters out the dependences
761 * in "filter" with local variables "local".
763 static struct pet_independence *new_independence(
764 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
766 isl_ctx *ctx;
767 struct pet_independence *independence;
769 if (!filter || !local)
770 goto error;
771 ctx = isl_union_map_get_ctx(filter);
772 independence = isl_alloc_type(ctx, struct pet_independence);
773 if (!independence)
774 goto error;
776 independence->filter = filter;
777 independence->local = local;
779 return independence;
780 error:
781 isl_union_map_free(filter);
782 isl_union_set_free(local);
783 return NULL;
786 /* Add an independence that filters out the dependences
787 * in "filter" with local variables "local" to "scop".
789 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
790 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
792 isl_ctx *ctx;
793 struct pet_independence *independence;
794 struct pet_independence **independences;
796 ctx = isl_union_map_get_ctx(filter);
797 independence = new_independence(filter, local);
798 if (!scop || !independence)
799 goto error;
801 independences = isl_realloc_array(ctx, scop->independences,
802 struct pet_independence *,
803 scop->n_independence + 1);
804 if (!independences)
805 goto error;
806 scop->independences = independences;
807 scop->independences[scop->n_independence] = independence;
808 scop->n_independence++;
810 return scop;
811 error:
812 pet_independence_free(independence);
813 pet_scop_free(scop);
814 return NULL;
817 /* Store the concatenation of the independences of "scop1" and "scop2"
818 * in "scop".
820 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
821 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
823 int i, off;
825 if (!scop)
826 return NULL;
828 if (scop2->n_independence == 0) {
829 scop->n_independence = scop1->n_independence;
830 scop->independences = scop1->independences;
831 scop1->n_independence = 0;
832 scop1->independences = NULL;
833 return scop;
836 if (scop1->n_independence == 0) {
837 scop->n_independence = scop2->n_independence;
838 scop->independences = scop2->independences;
839 scop2->n_independence = 0;
840 scop2->independences = NULL;
841 return scop;
844 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
845 scop1->n_independence + scop2->n_independence);
846 if (!scop->independences)
847 return pet_scop_free(scop);
849 for (i = 0; i < scop1->n_independence; ++i) {
850 scop->independences[i] = scop1->independences[i];
851 scop1->independences[i] = NULL;
854 off = scop1->n_independence;
855 for (i = 0; i < scop2->n_independence; ++i) {
856 scop->independences[off + i] = scop2->independences[i];
857 scop2->independences[i] = NULL;
859 scop->n_independence = scop1->n_independence + scop2->n_independence;
861 return scop;
864 /* Construct a pet_scop that contains the offset information,
865 * arrays, statements and skip information in "scop1" and "scop2".
867 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
868 struct pet_scop *scop2)
870 int i;
871 isl_space *space;
872 struct pet_scop *scop = NULL;
874 if (!scop1 || !scop2)
875 goto error;
877 if (scop1->n_stmt == 0) {
878 scop2 = scop_combine_skips(scop2, scop1, scop2);
879 pet_scop_free(scop1);
880 return scop2;
883 if (scop2->n_stmt == 0) {
884 scop1 = scop_combine_skips(scop1, scop1, scop2);
885 pet_scop_free(scop2);
886 return scop1;
889 space = isl_set_get_space(scop1->context);
890 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt);
891 if (!scop)
892 goto error;
894 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
895 scop1->n_array + scop2->n_array);
896 if (!scop->arrays)
897 goto error;
898 scop->n_array = scop1->n_array + scop2->n_array;
900 for (i = 0; i < scop1->n_stmt; ++i) {
901 scop->stmts[i] = scop1->stmts[i];
902 scop1->stmts[i] = NULL;
905 for (i = 0; i < scop2->n_stmt; ++i) {
906 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
907 scop2->stmts[i] = NULL;
910 for (i = 0; i < scop1->n_array; ++i) {
911 scop->arrays[i] = scop1->arrays[i];
912 scop1->arrays[i] = NULL;
915 for (i = 0; i < scop2->n_array; ++i) {
916 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
917 scop2->arrays[i] = NULL;
920 scop = scop_collect_implications(ctx, scop, scop1, scop2);
921 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
922 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
923 scop = scop_combine_skips(scop, scop1, scop2);
924 scop = scop_combine_start_end(scop, scop1, scop2);
925 scop = scop_collect_independences(ctx, scop, scop1, scop2);
927 pet_scop_free(scop1);
928 pet_scop_free(scop2);
929 return scop;
930 error:
931 pet_scop_free(scop1);
932 pet_scop_free(scop2);
933 pet_scop_free(scop);
934 return NULL;
937 /* Apply the skip condition "skip" to "scop".
938 * That is, make sure "scop" is not executed when the condition holds.
940 * If "skip" is an affine expression, we add the conditions under
941 * which the expression is zero to the context and the skip conditions
942 * of "scop".
943 * Otherwise, we add a filter on the variable attaining the value zero.
945 static struct pet_scop *restrict_skip(struct pet_scop *scop,
946 __isl_take isl_multi_pw_aff *skip)
948 isl_set *zero;
949 isl_pw_aff *pa;
950 int is_aff;
952 if (!scop || !skip)
953 goto error;
955 is_aff = multi_pw_aff_is_affine(skip);
956 if (is_aff < 0)
957 goto error;
959 if (!is_aff)
960 return pet_scop_filter(scop, skip, 0);
962 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
963 isl_multi_pw_aff_free(skip);
964 zero = isl_pw_aff_zero_set(pa);
965 scop = pet_scop_restrict(scop, zero);
967 return scop;
968 error:
969 isl_multi_pw_aff_free(skip);
970 return pet_scop_free(scop);
973 /* Construct a pet_scop that contains the arrays, statements and
974 * skip information in "scop1" and "scop2", where the two scops
975 * are executed "in sequence". That is, breaks and continues
976 * in scop1 have an effect on scop2.
978 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
979 struct pet_scop *scop2)
981 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
982 scop2 = restrict_skip(scop2,
983 pet_scop_get_skip(scop1, pet_skip_now));
984 return pet_scop_add(ctx, scop1, scop2);
987 /* Construct a pet_scop that contains the arrays, statements and
988 * skip information in "scop1" and "scop2", where the two scops
989 * are executed "in parallel". That is, any break or continue
990 * in scop1 has no effect on scop2.
992 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
993 struct pet_scop *scop2)
995 return pet_scop_add(ctx, scop1, scop2);
998 void *pet_implication_free(struct pet_implication *implication)
1000 int i;
1002 if (!implication)
1003 return NULL;
1005 isl_map_free(implication->extension);
1007 free(implication);
1008 return NULL;
1011 void *pet_independence_free(struct pet_independence *independence)
1013 if (!independence)
1014 return NULL;
1016 isl_union_map_free(independence->filter);
1017 isl_union_set_free(independence->local);
1019 free(independence);
1020 return NULL;
1023 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1025 int i;
1026 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1028 if (!scop)
1029 return NULL;
1030 pet_loc_free(scop->loc);
1031 isl_set_free(scop->context);
1032 isl_set_free(scop->context_value);
1033 if (scop->types)
1034 for (i = 0; i < scop->n_type; ++i)
1035 pet_type_free(scop->types[i]);
1036 free(scop->types);
1037 if (scop->arrays)
1038 for (i = 0; i < scop->n_array; ++i)
1039 pet_array_free(scop->arrays[i]);
1040 free(scop->arrays);
1041 if (scop->stmts)
1042 for (i = 0; i < scop->n_stmt; ++i)
1043 pet_stmt_free(scop->stmts[i]);
1044 free(scop->stmts);
1045 if (scop->implications)
1046 for (i = 0; i < scop->n_implication; ++i)
1047 pet_implication_free(scop->implications[i]);
1048 free(scop->implications);
1049 if (scop->independences)
1050 for (i = 0; i < scop->n_independence; ++i)
1051 pet_independence_free(scop->independences[i]);
1052 free(scop->independences);
1053 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1054 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1055 free(scop);
1056 return NULL;
1059 void pet_type_dump(struct pet_type *type)
1061 if (!type)
1062 return;
1064 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1067 void pet_implication_dump(struct pet_implication *implication)
1069 if (!implication)
1070 return;
1072 fprintf(stderr, "%d\n", implication->satisfied);
1073 isl_map_dump(implication->extension);
1076 void pet_scop_dump(struct pet_scop *scop)
1078 int i;
1079 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1081 if (!scop)
1082 return;
1084 isl_set_dump(scop->context);
1085 isl_set_dump(scop->context_value);
1086 for (i = 0; i < scop->n_type; ++i)
1087 pet_type_dump(scop->types[i]);
1088 for (i = 0; i < scop->n_array; ++i)
1089 pet_array_dump(scop->arrays[i]);
1090 for (i = 0; i < scop->n_stmt; ++i)
1091 pet_stmt_dump(scop->stmts[i]);
1092 for (i = 0; i < scop->n_implication; ++i)
1093 pet_implication_dump(scop->implications[i]);
1095 if (ext->skip[0]) {
1096 fprintf(stderr, "skip\n");
1097 isl_multi_pw_aff_dump(ext->skip[0]);
1098 isl_multi_pw_aff_dump(ext->skip[1]);
1102 /* Return 1 if the two pet_arrays are equivalent.
1104 * We don't compare element_size as this may be target dependent.
1106 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1108 if (!array1 || !array2)
1109 return 0;
1111 if (!isl_set_is_equal(array1->context, array2->context))
1112 return 0;
1113 if (!isl_set_is_equal(array1->extent, array2->extent))
1114 return 0;
1115 if (!!array1->value_bounds != !!array2->value_bounds)
1116 return 0;
1117 if (array1->value_bounds &&
1118 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1119 return 0;
1120 if (strcmp(array1->element_type, array2->element_type))
1121 return 0;
1122 if (array1->element_is_record != array2->element_is_record)
1123 return 0;
1124 if (array1->live_out != array2->live_out)
1125 return 0;
1126 if (array1->uniquely_defined != array2->uniquely_defined)
1127 return 0;
1128 if (array1->declared != array2->declared)
1129 return 0;
1130 if (array1->exposed != array2->exposed)
1131 return 0;
1133 return 1;
1136 /* Return 1 if the two pet_stmts are equivalent.
1138 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1140 int i;
1142 if (!stmt1 || !stmt2)
1143 return 0;
1145 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1146 return 0;
1147 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1148 return 0;
1149 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
1150 return 0;
1151 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1152 return 0;
1153 if (stmt1->n_arg != stmt2->n_arg)
1154 return 0;
1155 for (i = 0; i < stmt1->n_arg; ++i) {
1156 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1157 return 0;
1160 return 1;
1163 /* Return 1 if the two pet_types are equivalent.
1165 * We only compare the names of the types since the exact representation
1166 * of the definition may depend on the version of clang being used.
1168 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1170 if (!type1 || !type2)
1171 return 0;
1173 if (strcmp(type1->name, type2->name))
1174 return 0;
1176 return 1;
1179 /* Return 1 if the two pet_implications are equivalent.
1181 int pet_implication_is_equal(struct pet_implication *implication1,
1182 struct pet_implication *implication2)
1184 if (!implication1 || !implication2)
1185 return 0;
1187 if (implication1->satisfied != implication2->satisfied)
1188 return 0;
1189 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1190 return 0;
1192 return 1;
1195 /* Return 1 if the two pet_independences are equivalent.
1197 int pet_independence_is_equal(struct pet_independence *independence1,
1198 struct pet_independence *independence2)
1200 if (!independence1 || !independence2)
1201 return 0;
1203 if (!isl_union_map_is_equal(independence1->filter,
1204 independence2->filter))
1205 return 0;
1206 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1207 return 0;
1209 return 1;
1212 /* Return 1 if the two pet_scops are equivalent.
1214 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1216 int i;
1218 if (!scop1 || !scop2)
1219 return 0;
1221 if (!isl_set_is_equal(scop1->context, scop2->context))
1222 return 0;
1223 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1224 return 0;
1226 if (scop1->n_type != scop2->n_type)
1227 return 0;
1228 for (i = 0; i < scop1->n_type; ++i)
1229 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1230 return 0;
1232 if (scop1->n_array != scop2->n_array)
1233 return 0;
1234 for (i = 0; i < scop1->n_array; ++i)
1235 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1236 return 0;
1238 if (scop1->n_stmt != scop2->n_stmt)
1239 return 0;
1240 for (i = 0; i < scop1->n_stmt; ++i)
1241 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1242 return 0;
1244 if (scop1->n_implication != scop2->n_implication)
1245 return 0;
1246 for (i = 0; i < scop1->n_implication; ++i)
1247 if (!pet_implication_is_equal(scop1->implications[i],
1248 scop2->implications[i]))
1249 return 0;
1251 if (scop1->n_independence != scop2->n_independence)
1252 return 0;
1253 for (i = 0; i < scop1->n_independence; ++i)
1254 if (!pet_independence_is_equal(scop1->independences[i],
1255 scop2->independences[i]))
1256 return 0;
1258 return 1;
1261 /* Does the set "extent" reference a virtual array, i.e.,
1262 * one with user pointer equal to NULL?
1263 * A virtual array does not have any members.
1265 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1267 isl_id *id;
1268 int is_virtual;
1270 if (!isl_set_has_tuple_id(extent))
1271 return 0;
1272 if (isl_set_is_wrapping(extent))
1273 return 0;
1274 id = isl_set_get_tuple_id(extent);
1275 is_virtual = !isl_id_get_user(id);
1276 isl_id_free(id);
1278 return is_virtual;
1281 /* Intersect the initial dimensions of "array" with "domain", provided
1282 * that "array" represents a virtual array.
1284 * If "array" is virtual, then We take the preimage of "domain"
1285 * over the projection of the extent of "array" onto its initial dimensions
1286 * and intersect this extent with the result.
1288 static struct pet_array *virtual_array_intersect_domain_prefix(
1289 struct pet_array *array, __isl_take isl_set *domain)
1291 int n;
1292 isl_space *space;
1293 isl_multi_aff *ma;
1295 if (!array || !extent_is_virtual_array(array->extent)) {
1296 isl_set_free(domain);
1297 return array;
1300 space = isl_set_get_space(array->extent);
1301 n = isl_set_dim(domain, isl_dim_set);
1302 ma = pet_prefix_projection(space, n);
1303 domain = isl_set_preimage_multi_aff(domain, ma);
1305 array->extent = isl_set_intersect(array->extent, domain);
1306 if (!array->extent)
1307 return pet_array_free(array);
1309 return array;
1312 /* Intersect the initial dimensions of the domain of "stmt"
1313 * with "domain".
1315 * We take the preimage of "domain" over the projection of the
1316 * domain of "stmt" onto its initial dimensions and intersect
1317 * the domain of "stmt" with the result.
1319 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1320 __isl_take isl_set *domain)
1322 int n;
1323 isl_space *space;
1324 isl_multi_aff *ma;
1326 if (!stmt)
1327 goto error;
1329 space = isl_set_get_space(stmt->domain);
1330 n = isl_set_dim(domain, isl_dim_set);
1331 ma = pet_prefix_projection(space, n);
1332 domain = isl_set_preimage_multi_aff(domain, ma);
1334 stmt->domain = isl_set_intersect(stmt->domain, domain);
1335 if (!stmt->domain)
1336 return pet_stmt_free(stmt);
1338 return stmt;
1339 error:
1340 isl_set_free(domain);
1341 return pet_stmt_free(stmt);
1344 /* Intersect the initial dimensions of the domain of "implication"
1345 * with "domain".
1347 * We take the preimage of "domain" over the projection of the
1348 * domain of "implication" onto its initial dimensions and intersect
1349 * the domain of "implication" with the result.
1351 static struct pet_implication *implication_intersect_domain_prefix(
1352 struct pet_implication *implication, __isl_take isl_set *domain)
1354 int n;
1355 isl_space *space;
1356 isl_multi_aff *ma;
1358 if (!implication)
1359 goto error;
1361 space = isl_map_get_space(implication->extension);
1362 n = isl_set_dim(domain, isl_dim_set);
1363 ma = pet_prefix_projection(isl_space_domain(space), n);
1364 domain = isl_set_preimage_multi_aff(domain, ma);
1366 implication->extension =
1367 isl_map_intersect_domain(implication->extension, domain);
1368 if (!implication->extension)
1369 return pet_implication_free(implication);
1371 return implication;
1372 error:
1373 isl_set_free(domain);
1374 return pet_implication_free(implication);
1377 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1379 * The extents of the virtual arrays match the iteration domains,
1380 * so if the iteration domain changes, we need to change those extents too.
1382 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1383 __isl_take isl_set *domain)
1385 int i;
1387 if (!scop)
1388 goto error;
1390 for (i = 0; i < scop->n_array; ++i) {
1391 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1392 scop->arrays[i], isl_set_copy(domain));
1393 if (!scop->arrays[i])
1394 goto error;
1397 for (i = 0; i < scop->n_stmt; ++i) {
1398 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1399 isl_set_copy(domain));
1400 if (!scop->stmts[i])
1401 goto error;
1404 for (i = 0; i < scop->n_implication; ++i) {
1405 scop->implications[i] =
1406 implication_intersect_domain_prefix(scop->implications[i],
1407 isl_set_copy(domain));
1408 if (!scop->implications[i])
1409 return pet_scop_free(scop);
1412 isl_set_free(domain);
1413 return scop;
1414 error:
1415 isl_set_free(domain);
1416 return pet_scop_free(scop);
1419 /* Prefix the schedule of "stmt" with an extra dimension with constant
1420 * value "pos".
1422 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1424 if (!stmt)
1425 return NULL;
1427 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1428 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1429 if (!stmt->schedule)
1430 return pet_stmt_free(stmt);
1432 return stmt;
1435 /* Prefix the schedules of all statements in "scop" with an extra
1436 * dimension with constant value "pos".
1438 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1440 int i;
1442 if (!scop)
1443 return NULL;
1445 for (i = 0; i < scop->n_stmt; ++i) {
1446 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1447 if (!scop->stmts[i])
1448 return pet_scop_free(scop);
1451 return scop;
1454 /* Prefix the schedule of "stmt" with "sched".
1456 * The domain of "sched" refers the current outer loop iterators and
1457 * needs to be mapped to the iteration domain of "stmt" first
1458 * before being prepended to the schedule of "stmt".
1460 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1461 __isl_take isl_map *sched)
1463 int n;
1464 isl_space *space;
1465 isl_multi_aff *ma;
1467 if (!stmt)
1468 goto error;
1470 space = pet_stmt_get_space(stmt);
1471 n = isl_map_dim(sched, isl_dim_in);
1472 ma = pet_prefix_projection(space, n);
1473 sched = isl_map_preimage_domain_multi_aff(sched, ma);
1474 stmt->schedule = isl_map_flat_range_product(sched, stmt->schedule);
1475 if (!stmt->schedule)
1476 return pet_stmt_free(stmt);
1478 return stmt;
1479 error:
1480 isl_map_free(sched);
1481 return NULL;
1484 /* Update the context with respect to an embedding into a loop
1485 * with iteration domain "dom".
1486 * The input context lives in the same space as "dom".
1487 * The output context has the inner dimension removed.
1489 * An outer loop iterator value is invalid for the embedding if
1490 * any of the corresponding inner iterator values is invalid.
1491 * That is, an outer loop iterator value is valid only if all the corresponding
1492 * inner iterator values are valid.
1493 * We therefore compute the set of outer loop iterators l
1495 * forall i: dom(l,i) => valid(l,i)
1497 * or
1499 * forall i: not dom(l,i) or valid(l,i)
1501 * or
1503 * not exists i: dom(l,i) and not valid(l,i)
1505 * i.e.,
1507 * not exists i: (dom \ valid)(l,i)
1509 * If there are any unnamed parameters in "dom", then we consider
1510 * a parameter value to be valid if it is valid for any value of those
1511 * unnamed parameters. They are therefore projected out at the end.
1513 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1514 __isl_keep isl_set *dom)
1516 int pos;
1518 pos = isl_set_dim(context, isl_dim_set) - 1;
1519 context = isl_set_subtract(isl_set_copy(dom), context);
1520 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1521 context = isl_set_complement(context);
1522 context = pet_nested_remove_from_set(context);
1524 return context;
1527 /* Update the implication with respect to an embedding into a loop
1528 * with iteration domain "dom".
1530 * Since embed_access extends virtual arrays along with the domain
1531 * of the access, we need to do the same with domain and range
1532 * of the implication. Since the original implication is only valid
1533 * within a given iteration of the loop, the extended implication
1534 * maps the extra array dimension corresponding to the extra loop
1535 * to itself.
1537 static struct pet_implication *pet_implication_embed(
1538 struct pet_implication *implication, __isl_take isl_set *dom)
1540 isl_id *id;
1541 isl_map *map;
1543 if (!implication)
1544 goto error;
1546 map = isl_set_identity(dom);
1547 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1548 map = isl_map_flat_product(map, implication->extension);
1549 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1550 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1551 implication->extension = map;
1552 if (!implication->extension)
1553 return pet_implication_free(implication);
1555 return implication;
1556 error:
1557 isl_set_free(dom);
1558 return NULL;
1561 /* Adjust the context and statement schedules according to an embedding
1562 * in a loop with iteration domain "dom" and schedule "sched".
1564 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1565 __isl_take isl_aff *sched)
1567 int i;
1568 isl_map *sched_map;
1570 sched_map = isl_map_from_aff(sched);
1572 if (!scop)
1573 goto error;
1575 scop->context = context_embed(scop->context, dom);
1576 if (!scop->context)
1577 goto error;
1579 for (i = 0; i < scop->n_stmt; ++i) {
1580 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1581 isl_map_copy(sched_map));
1582 if (!scop->stmts[i])
1583 goto error;
1586 isl_set_free(dom);
1587 isl_map_free(sched_map);
1588 return scop;
1589 error:
1590 isl_set_free(dom);
1591 isl_map_free(sched_map);
1592 return pet_scop_free(scop);
1595 /* Add extra conditions to scop->skip[type].
1597 * The new skip condition only holds if it held before
1598 * and the condition is true. It does not hold if it did not hold
1599 * before or the condition is false.
1601 * The skip condition is assumed to be an affine expression.
1603 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1604 enum pet_skip type, __isl_keep isl_set *cond)
1606 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1607 isl_pw_aff *skip;
1608 isl_set *dom;
1610 if (!scop)
1611 return NULL;
1612 if (!ext->skip[type])
1613 return scop;
1615 if (!multi_pw_aff_is_affine(ext->skip[type]))
1616 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1617 isl_error_internal, "can only restrict affine skips",
1618 return pet_scop_free(scop));
1620 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1621 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1622 cond = isl_set_copy(cond);
1623 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1624 skip = indicator_function(cond, dom);
1625 isl_multi_pw_aff_free(ext->skip[type]);
1626 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1627 if (!ext->skip[type])
1628 return pet_scop_free(scop);
1630 return scop;
1633 /* Adjust the context and the skip conditions to the fact that
1634 * the scop was created in a context where "cond" holds.
1636 * An outer loop iterator or parameter value is valid for the result
1637 * if it was valid for the original scop and satisfies "cond" or if it does
1638 * not satisfy "cond" as in this case the scop is not executed
1639 * and the original constraints on these values are irrelevant.
1641 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1642 __isl_take isl_set *cond)
1644 int i;
1646 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1647 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1649 if (!scop)
1650 goto error;
1652 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1653 scop->context = isl_set_union(scop->context,
1654 isl_set_complement(isl_set_copy(cond)));
1655 scop->context = isl_set_coalesce(scop->context);
1656 scop->context = pet_nested_remove_from_set(scop->context);
1657 if (!scop->context)
1658 goto error;
1660 isl_set_free(cond);
1661 return scop;
1662 error:
1663 isl_set_free(cond);
1664 return pet_scop_free(scop);
1667 /* Insert an argument expression corresponding to "test" in front
1668 * of the list of arguments described by *n_arg and *args.
1670 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1671 __isl_keep isl_multi_pw_aff *test)
1673 int i;
1674 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1676 if (!test)
1677 return -1;
1679 if (!*args) {
1680 *args = isl_calloc_array(ctx, pet_expr *, 1);
1681 if (!*args)
1682 return -1;
1683 } else {
1684 pet_expr **ext;
1685 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1686 if (!ext)
1687 return -1;
1688 for (i = 0; i < *n_arg; ++i)
1689 ext[1 + i] = (*args)[i];
1690 free(*args);
1691 *args = ext;
1693 (*n_arg)++;
1694 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1695 if (!(*args)[0])
1696 return -1;
1698 return 0;
1701 /* Look through the applications in "scop" for any that can be
1702 * applied to the filter expressed by "map" and "satisified".
1703 * If there is any, then apply it to "map" and return the result.
1704 * Otherwise, return "map".
1705 * "id" is the identifier of the virtual array.
1707 * We only introduce at most one implication for any given virtual array,
1708 * so we can apply the implication and return as soon as we find one.
1710 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1711 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1713 int i;
1715 for (i = 0; i < scop->n_implication; ++i) {
1716 struct pet_implication *pi = scop->implications[i];
1717 isl_id *pi_id;
1719 if (pi->satisfied != satisfied)
1720 continue;
1721 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1722 isl_id_free(pi_id);
1723 if (pi_id != id)
1724 continue;
1726 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1729 return map;
1732 /* Is the filter expressed by "test" and "satisfied" implied
1733 * by filter "pos" on "domain", with filter "expr", taking into
1734 * account the implications of "scop"?
1736 * For filter on domain implying that expressed by "test" and "satisfied",
1737 * the filter needs to be an access to the same (virtual) array as "test" and
1738 * the filter value needs to be equal to "satisfied".
1739 * Moreover, the filter access relation, possibly extended by
1740 * the implications in "scop" needs to contain "test".
1742 static int implies_filter(struct pet_scop *scop,
1743 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1744 __isl_keep isl_map *test, int satisfied)
1746 isl_id *test_id, *arg_id;
1747 isl_val *val;
1748 int is_int;
1749 int s;
1750 int is_subset;
1751 isl_map *implied;
1753 if (expr->type != pet_expr_access)
1754 return 0;
1755 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1756 arg_id = pet_expr_access_get_id(expr);
1757 isl_id_free(arg_id);
1758 isl_id_free(test_id);
1759 if (test_id != arg_id)
1760 return 0;
1761 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1762 is_int = isl_val_is_int(val);
1763 if (is_int)
1764 s = isl_val_get_num_si(val);
1765 isl_val_free(val);
1766 if (!val)
1767 return -1;
1768 if (!is_int)
1769 return 0;
1770 if (s != satisfied)
1771 return 0;
1773 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1774 implied = apply_implications(scop, implied, test_id, satisfied);
1775 is_subset = isl_map_is_subset(test, implied);
1776 isl_map_free(implied);
1778 return is_subset;
1781 /* Is the filter expressed by "test" and "satisfied" implied
1782 * by any of the filters on the domain of "stmt", taking into
1783 * account the implications of "scop"?
1785 static int filter_implied(struct pet_scop *scop,
1786 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1788 int i;
1789 int implied;
1790 isl_id *test_id;
1791 isl_map *domain;
1792 isl_map *test_map;
1794 if (!scop || !stmt || !test)
1795 return -1;
1796 if (scop->n_implication == 0)
1797 return 0;
1798 if (stmt->n_arg == 0)
1799 return 0;
1801 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1802 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1804 implied = 0;
1805 for (i = 0; i < stmt->n_arg; ++i) {
1806 implied = implies_filter(scop, domain, i, stmt->args[i],
1807 test_map, satisfied);
1808 if (implied < 0 || implied)
1809 break;
1812 isl_map_free(test_map);
1813 isl_map_free(domain);
1814 return implied;
1817 /* Make the statement "stmt" depend on the value of "test"
1818 * being equal to "satisfied" by adjusting stmt->domain.
1820 * The domain of "test" corresponds to the (zero or more) outer dimensions
1821 * of the iteration domain.
1823 * We first extend "test" to apply to the entire iteration domain and
1824 * then check if the filter that we are about to add is implied
1825 * by any of the current filters, possibly taking into account
1826 * the implications in "scop". If so, we leave "stmt" untouched and return.
1828 * Otherwise, we insert an argument corresponding to a read to "test"
1829 * from the iteration domain of "stmt" in front of the list of arguments.
1830 * We also insert a corresponding output dimension in the wrapped
1831 * map contained in stmt->domain, with value set to "satisfied".
1833 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1834 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1836 int i;
1837 int implied;
1838 isl_id *id;
1839 isl_ctx *ctx;
1840 isl_pw_multi_aff *pma;
1841 isl_multi_aff *add_dom;
1842 isl_space *space;
1843 isl_local_space *ls;
1844 int n_test_dom;
1846 if (!stmt || !test)
1847 goto error;
1849 space = pet_stmt_get_space(stmt);
1850 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1851 space = isl_space_from_domain(space);
1852 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1853 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1854 ls = isl_local_space_from_space(isl_space_domain(space));
1855 for (i = 0; i < n_test_dom; ++i) {
1856 isl_aff *aff;
1857 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1858 isl_dim_set, i);
1859 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1861 isl_local_space_free(ls);
1862 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1864 implied = filter_implied(scop, stmt, test, satisfied);
1865 if (implied < 0)
1866 goto error;
1867 if (implied) {
1868 isl_multi_pw_aff_free(test);
1869 return stmt;
1872 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1873 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1874 id, satisfied);
1875 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1877 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1878 goto error;
1880 isl_multi_pw_aff_free(test);
1881 return stmt;
1882 error:
1883 isl_multi_pw_aff_free(test);
1884 return pet_stmt_free(stmt);
1887 /* Does "scop" have a skip condition of the given "type"?
1889 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1891 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1893 if (!scop)
1894 return -1;
1895 return ext->skip[type] != NULL;
1898 /* Does "scop" have a skip condition of the given "type" that
1899 * is an affine expression?
1901 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1903 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1905 if (!scop)
1906 return -1;
1907 if (!ext->skip[type])
1908 return 0;
1909 return multi_pw_aff_is_affine(ext->skip[type]);
1912 /* Does "scop" have a skip condition of the given "type" that
1913 * is not an affine expression?
1915 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1917 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1918 int aff;
1920 if (!scop)
1921 return -1;
1922 if (!ext->skip[type])
1923 return 0;
1924 aff = multi_pw_aff_is_affine(ext->skip[type]);
1925 if (aff < 0)
1926 return -1;
1927 return !aff;
1930 /* Does "scop" have a skip condition of the given "type" that
1931 * is affine and holds on the entire domain?
1933 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1935 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1936 isl_pw_aff *pa;
1937 isl_set *set;
1938 int is_aff;
1939 int is_univ;
1941 is_aff = pet_scop_has_affine_skip(scop, type);
1942 if (is_aff < 0 || !is_aff)
1943 return is_aff;
1945 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1946 set = isl_pw_aff_non_zero_set(pa);
1947 is_univ = isl_set_plain_is_universe(set);
1948 isl_set_free(set);
1950 return is_univ;
1953 /* Replace scop->skip[type] by "skip".
1955 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
1956 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
1958 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1960 if (!scop || !skip)
1961 goto error;
1963 isl_multi_pw_aff_free(ext->skip[type]);
1964 ext->skip[type] = skip;
1966 return scop;
1967 error:
1968 isl_multi_pw_aff_free(skip);
1969 return pet_scop_free(scop);
1972 /* Return a copy of scop->skip[type].
1974 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
1975 enum pet_skip type)
1977 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1979 if (!scop)
1980 return NULL;
1982 return isl_multi_pw_aff_copy(ext->skip[type]);
1985 /* Assuming scop->skip[type] is an affine expression,
1986 * return the constraints on the outer loop domain for which the skip condition
1987 * holds.
1989 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
1990 enum pet_skip type)
1992 isl_multi_pw_aff *skip;
1993 isl_pw_aff *pa;
1995 skip = pet_scop_get_skip(scop, type);
1996 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1997 isl_multi_pw_aff_free(skip);
1998 return isl_pw_aff_non_zero_set(pa);
2001 /* Return the identifier of the variable that is accessed by
2002 * the skip condition of the given type.
2004 * The skip condition is assumed not to be an affine condition.
2006 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2007 enum pet_skip type)
2009 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2011 if (!scop)
2012 return NULL;
2014 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2017 /* Return an access pet_expr corresponding to the skip condition
2018 * of the given type.
2020 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2021 enum pet_skip type)
2023 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2026 /* Drop the skip condition scop->skip[type].
2028 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2030 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2032 if (!scop)
2033 return;
2035 isl_multi_pw_aff_free(ext->skip[type]);
2036 ext->skip[type] = NULL;
2039 /* Drop all skip conditions on "scop".
2041 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2043 pet_scop_reset_skip(scop, pet_skip_now);
2044 pet_scop_reset_skip(scop, pet_skip_later);
2046 return scop;
2049 /* Make the skip condition (if any) depend on the value of "test" being
2050 * equal to "satisfied".
2052 * We only support the case where the original skip condition is universal,
2053 * i.e., where skipping is unconditional, and where satisfied == 1.
2054 * In this case, the skip condition is changed to skip only when
2055 * "test" is equal to one.
2057 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2058 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2060 int is_univ = 0;
2062 if (!scop)
2063 return NULL;
2064 if (!pet_scop_has_skip(scop, type))
2065 return scop;
2067 if (satisfied)
2068 is_univ = pet_scop_has_universal_skip(scop, type);
2069 if (is_univ < 0)
2070 return pet_scop_free(scop);
2071 if (satisfied && is_univ) {
2072 isl_multi_pw_aff *skip;
2073 skip = isl_multi_pw_aff_copy(test);
2074 scop = pet_scop_set_skip(scop, type, skip);
2075 if (!scop)
2076 return NULL;
2077 } else {
2078 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2079 "skip expression cannot be filtered",
2080 return pet_scop_free(scop));
2083 return scop;
2086 /* Make all statements in "scop" depend on the value of "test"
2087 * being equal to "satisfied" by adjusting their domains.
2089 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2090 __isl_take isl_multi_pw_aff *test, int satisfied)
2092 int i;
2094 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2095 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2097 if (!scop || !test)
2098 goto error;
2100 for (i = 0; i < scop->n_stmt; ++i) {
2101 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2102 isl_multi_pw_aff_copy(test), satisfied);
2103 if (!scop->stmts[i])
2104 goto error;
2107 isl_multi_pw_aff_free(test);
2108 return scop;
2109 error:
2110 isl_multi_pw_aff_free(test);
2111 return pet_scop_free(scop);
2114 /* Add the parameters of the access expression "expr" to "space".
2116 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2118 int i;
2119 isl_space *expr_space;
2120 isl_space **space = user;
2122 expr_space = pet_expr_access_get_parameter_space(expr);
2123 *space = isl_space_align_params(*space, expr_space);
2125 return *space ? 0 : -1;
2128 /* Add all parameters in "stmt" to "space" and return the result.
2130 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2131 __isl_take isl_space *space)
2133 int i;
2135 if (!stmt)
2136 return isl_space_free(space);
2138 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2139 space = isl_space_align_params(space,
2140 isl_map_get_space(stmt->schedule));
2141 for (i = 0; i < stmt->n_arg; ++i)
2142 if (pet_expr_foreach_access_expr(stmt->args[i],
2143 &access_collect_params, &space) < 0)
2144 space = isl_space_free(space);
2145 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2146 &space) < 0)
2147 space = isl_space_free(space);
2149 return space;
2152 /* Add all parameters in "array" to "space" and return the result.
2154 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2155 __isl_take isl_space *space)
2157 if (!array)
2158 return isl_space_free(space);
2160 space = isl_space_align_params(space,
2161 isl_set_get_space(array->context));
2162 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2164 return space;
2167 /* Add all parameters in "independence" to "space" and return the result.
2169 static __isl_give isl_space *independence_collect_params(
2170 struct pet_independence *independence, __isl_take isl_space *space)
2172 if (!independence)
2173 return isl_space_free(space);
2175 space = isl_space_align_params(space,
2176 isl_union_map_get_space(independence->filter));
2177 space = isl_space_align_params(space,
2178 isl_union_set_get_space(independence->local));
2180 return space;
2183 /* Collect all parameters in "scop" in a parameter space and return the result.
2185 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2187 isl_space *space;
2188 int i;
2190 if (!scop)
2191 return NULL;
2193 space = isl_set_get_space(scop->context);
2195 for (i = 0; i < scop->n_array; ++i)
2196 space = array_collect_params(scop->arrays[i], space);
2198 for (i = 0; i < scop->n_stmt; ++i)
2199 space = stmt_collect_params(scop->stmts[i], space);
2201 for (i = 0; i < scop->n_independence; ++i)
2202 space = independence_collect_params(scop->independences[i],
2203 space);
2205 return space;
2208 /* Add all parameters in "space" to the domain, schedule and
2209 * all access relations in "stmt".
2211 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2212 __isl_take isl_space *space)
2214 int i;
2216 if (!stmt)
2217 goto error;
2219 stmt->domain = isl_set_align_params(stmt->domain,
2220 isl_space_copy(space));
2221 stmt->schedule = isl_map_align_params(stmt->schedule,
2222 isl_space_copy(space));
2224 for (i = 0; i < stmt->n_arg; ++i) {
2225 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2226 isl_space_copy(space));
2227 if (!stmt->args[i])
2228 goto error;
2230 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2232 if (!stmt->domain || !stmt->schedule || !stmt->body)
2233 goto error;
2235 isl_space_free(space);
2236 return stmt;
2237 error:
2238 isl_space_free(space);
2239 return pet_stmt_free(stmt);
2242 /* Add all parameters in "space" to "array".
2244 static struct pet_array *array_propagate_params(struct pet_array *array,
2245 __isl_take isl_space *space)
2247 if (!array)
2248 goto error;
2250 array->context = isl_set_align_params(array->context,
2251 isl_space_copy(space));
2252 array->extent = isl_set_align_params(array->extent,
2253 isl_space_copy(space));
2254 if (array->value_bounds) {
2255 array->value_bounds = isl_set_align_params(array->value_bounds,
2256 isl_space_copy(space));
2257 if (!array->value_bounds)
2258 goto error;
2261 if (!array->context || !array->extent)
2262 goto error;
2264 isl_space_free(space);
2265 return array;
2266 error:
2267 isl_space_free(space);
2268 return pet_array_free(array);
2271 /* Add all parameters in "space" to "independence".
2273 static struct pet_independence *independence_propagate_params(
2274 struct pet_independence *independence, __isl_take isl_space *space)
2276 if (!independence)
2277 goto error;
2279 independence->filter = isl_union_map_align_params(independence->filter,
2280 isl_space_copy(space));
2281 independence->local = isl_union_set_align_params(independence->local,
2282 isl_space_copy(space));
2283 if (!independence->filter || !independence->local)
2284 goto error;
2286 isl_space_free(space);
2287 return independence;
2288 error:
2289 isl_space_free(space);
2290 return pet_independence_free(independence);
2293 /* Add all parameters in "space" to "scop".
2295 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2296 __isl_take isl_space *space)
2298 int i;
2300 if (!scop)
2301 goto error;
2303 scop->context = isl_set_align_params(scop->context,
2304 isl_space_copy(space));
2305 if (!scop->context)
2306 goto error;
2308 for (i = 0; i < scop->n_array; ++i) {
2309 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2310 isl_space_copy(space));
2311 if (!scop->arrays[i])
2312 goto error;
2315 for (i = 0; i < scop->n_stmt; ++i) {
2316 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2317 isl_space_copy(space));
2318 if (!scop->stmts[i])
2319 goto error;
2322 for (i = 0; i < scop->n_independence; ++i) {
2323 scop->independences[i] = independence_propagate_params(
2324 scop->independences[i], isl_space_copy(space));
2325 if (!scop->independences[i])
2326 goto error;
2329 isl_space_free(space);
2330 return scop;
2331 error:
2332 isl_space_free(space);
2333 return pet_scop_free(scop);
2336 /* Update all isl_sets and isl_maps in "scop" such that they all
2337 * have the same parameters.
2339 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2341 isl_space *space;
2343 if (!scop)
2344 return NULL;
2346 space = scop_collect_params(scop);
2348 scop = scop_propagate_params(scop, space);
2350 return scop;
2353 /* Add the access relation of the give "type" of the access expression "expr"
2354 * to "accesses" and return the result.
2355 * The domain of the access relation is intersected with "domain".
2356 * If "tag" is set, then the access relation is tagged with
2357 * the corresponding reference identifier.
2359 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2360 enum pet_expr_access_type type, int tag,
2361 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2363 isl_union_map *access;
2365 access = pet_expr_access_get_access(expr, type);
2366 access = isl_union_map_intersect_domain(access,
2367 isl_union_set_copy(domain));
2368 if (tag)
2369 access = pet_expr_tag_access(expr, access);
2370 return isl_union_map_union(accesses, access);
2373 /* Internal data structure for expr_collect_accesses.
2375 * "type" is the type of accesses we want to collect.
2376 * "tag" is set if the access relations should be tagged with
2377 * the corresponding reference identifiers.
2378 * "domain" are constraints on the domain of the access relations.
2379 * "accesses" collects the results.
2381 struct pet_expr_collect_accesses_data {
2382 enum pet_expr_access_type type;
2383 int tag;
2384 isl_union_set *domain;
2386 isl_union_map *accesses;
2389 /* Add the access relation of the access expression "expr"
2390 * to data->accesses if the access expression is a read and we are collecting
2391 * reads and/or it is a write and we are collecting writes.
2392 * The domains of the access relations are intersected with data->domain.
2393 * If data->tag is set, then the access relations are tagged with
2394 * the corresponding reference identifiers.
2396 * If data->type is pet_expr_access_must_write, then we only add
2397 * the accesses that are definitely performed. Otherwise, we add
2398 * all potential accesses.
2399 * In particular, if the access has any arguments, then in case of
2400 * pet_expr_access_must_write we currently skip the access completely.
2401 * In other cases, we project out the values of the access arguments.
2403 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2405 struct pet_expr_collect_accesses_data *data = user;
2406 int i;
2407 isl_id *id;
2408 isl_space *dim;
2410 if (!expr)
2411 return -1;
2413 if (pet_expr_is_affine(expr))
2414 return 0;
2415 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2416 return 0;
2418 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2419 ((data->type == pet_expr_access_may_write ||
2420 data->type == pet_expr_access_must_write) && expr->acc.write))
2421 data->accesses = expr_collect_access(expr,
2422 data->type, data->tag,
2423 data->accesses, data->domain);
2425 return data->accesses ? 0 : -1;
2428 /* Collect and return all access relations of the given "type" in "stmt".
2429 * If "tag" is set, then the access relations are tagged with
2430 * the corresponding reference identifiers.
2431 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2432 * we simply add the argument of the kill operation.
2434 * If we are looking for definite accesses (pet_expr_access_must_write
2435 * or pet_expr_access_killed), then we only add the accesses that are
2436 * definitely performed. Otherwise, we add all potential accesses.
2437 * In particular, if the statement has any arguments, then if we are looking
2438 * for definite accesses we currently skip the statement completely. Othewise,
2439 * we project out the values of the statement arguments.
2440 * If the statement body is not an expression tree, then we cannot
2441 * know for sure if/when the accesses inside the tree are performed.
2442 * We therefore ignore such statements when we are looking for
2443 * definite accesses.
2445 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2446 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2448 struct pet_expr_collect_accesses_data data = { type, tag };
2449 int must;
2450 isl_set *domain;
2452 if (!stmt)
2453 return NULL;
2455 data.accesses = isl_union_map_empty(dim);
2457 if (type == pet_expr_access_must_write ||
2458 type == pet_expr_access_killed)
2459 must = 1;
2460 else
2461 must = 0;
2463 if (must && stmt->n_arg > 0)
2464 return data.accesses;
2465 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2466 return data.accesses;
2468 domain = drop_arguments(isl_set_copy(stmt->domain));
2469 data.domain = isl_union_set_from_set(domain);
2471 if (type == pet_expr_access_killed) {
2472 pet_expr *body, *arg;
2474 body = pet_tree_expr_get_expr(stmt->body);
2475 arg = pet_expr_get_arg(body, 0);
2476 data.accesses = expr_collect_access(arg,
2477 pet_expr_access_killed, tag,
2478 data.accesses, data.domain);
2479 pet_expr_free(arg);
2480 pet_expr_free(body);
2481 } else if (pet_tree_foreach_access_expr(stmt->body,
2482 &expr_collect_accesses, &data) < 0)
2483 data.accesses = isl_union_map_free(data.accesses);
2485 isl_union_set_free(data.domain);
2487 return data.accesses;
2490 /* Is "stmt" an assignment statement?
2492 int pet_stmt_is_assign(struct pet_stmt *stmt)
2494 if (!stmt)
2495 return 0;
2496 return pet_tree_is_assign(stmt->body);
2499 /* Is "stmt" a kill statement?
2501 int pet_stmt_is_kill(struct pet_stmt *stmt)
2503 if (!stmt)
2504 return 0;
2505 return pet_tree_is_kill(stmt->body);
2508 /* Is "stmt" an assume statement?
2510 int pet_stmt_is_assume(struct pet_stmt *stmt)
2512 if (!stmt)
2513 return 0;
2514 return pet_tree_is_assume(stmt->body);
2517 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2519 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2520 __isl_keep isl_map *map, __isl_keep isl_set *set)
2522 isl_map *gist;
2524 gist = isl_map_copy(map);
2525 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2526 return isl_union_map_add_map(umap, gist);
2529 /* Compute a mapping from all arrays (of structs) in scop
2530 * to their members.
2532 * If "from_outermost" is set, then the domain only consists
2533 * of outermost arrays.
2534 * If "to_innermost" is set, then the range only consists
2535 * of innermost arrays.
2537 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2538 int from_outermost, int to_innermost)
2540 int i;
2541 isl_union_map *to_inner;
2543 if (!scop)
2544 return NULL;
2546 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2548 for (i = 0; i < scop->n_array; ++i) {
2549 struct pet_array *array = scop->arrays[i];
2550 isl_set *set;
2551 isl_map *map;
2553 if (to_innermost && array->element_is_record)
2554 continue;
2556 set = isl_set_copy(array->extent);
2557 map = isl_set_identity(isl_set_copy(set));
2559 while (set && isl_set_is_wrapping(set)) {
2560 isl_id *id;
2561 isl_map *wrapped;
2563 if (!from_outermost)
2564 to_inner = add_gisted(to_inner, map, set);
2566 id = isl_set_get_tuple_id(set);
2567 wrapped = isl_set_unwrap(set);
2568 wrapped = isl_map_domain_map(wrapped);
2569 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2570 map = isl_map_apply_domain(map, wrapped);
2571 set = isl_map_domain(isl_map_copy(map));
2574 map = isl_map_gist_domain(map, set);
2575 to_inner = isl_union_map_add_map(to_inner, map);
2578 return to_inner;
2581 /* Compute a mapping from all arrays (of structs) in scop
2582 * to their innermost arrays.
2584 * In particular, for each array of a primitive type, the result
2585 * contains the identity mapping on that array.
2586 * For each array involving member accesses, the result
2587 * contains a mapping from the elements of any intermediate array of structs
2588 * to all corresponding elements of the innermost nested arrays.
2590 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2591 struct pet_scop *scop)
2593 return compute_to_inner(scop, 0, 1);
2596 /* Compute a mapping from all outermost arrays (of structs) in scop
2597 * to their innermost members.
2599 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2601 return compute_to_inner(scop, 1, 1);
2604 /* Compute a mapping from all outermost arrays (of structs) in scop
2605 * to their members, including the outermost arrays themselves.
2607 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2609 return compute_to_inner(scop, 1, 0);
2612 /* Collect and return all access relations of the given "type" in "scop".
2613 * If "type" is pet_expr_access_killed, then we only add the arguments of
2614 * kill operations.
2615 * If we are looking for definite accesses (pet_expr_access_must_write
2616 * or pet_expr_access_killed), then we only add the accesses that are
2617 * definitely performed. Otherwise, we add all potential accesses.
2618 * If "tag" is set, then the access relations are tagged with
2619 * the corresponding reference identifiers.
2620 * For accesses to structures, the returned access relation accesses
2621 * all individual fields in the structures.
2623 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2624 enum pet_expr_access_type type, int tag)
2626 int i;
2627 isl_union_map *accesses;
2628 isl_union_set *arrays;
2629 isl_union_map *to_inner;
2631 if (!scop)
2632 return NULL;
2634 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2636 for (i = 0; i < scop->n_stmt; ++i) {
2637 struct pet_stmt *stmt = scop->stmts[i];
2638 isl_union_map *accesses_i;
2639 isl_space *space;
2641 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2642 continue;
2644 space = isl_set_get_space(scop->context);
2645 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2646 accesses = isl_union_map_union(accesses, accesses_i);
2649 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2650 for (i = 0; i < scop->n_array; ++i) {
2651 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2652 arrays = isl_union_set_add_set(arrays, extent);
2654 accesses = isl_union_map_intersect_range(accesses, arrays);
2656 to_inner = pet_scop_compute_any_to_inner(scop);
2657 accesses = isl_union_map_apply_range(accesses, to_inner);
2659 return accesses;
2662 /* Collect all potential read access relations.
2664 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2666 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2669 /* Collect all potential write access relations.
2671 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2673 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2676 /* Collect all definite write access relations.
2678 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2680 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2683 /* Collect all definite kill access relations.
2685 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2687 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2690 /* Collect all tagged potential read access relations.
2692 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2693 struct pet_scop *scop)
2695 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2698 /* Collect all tagged potential write access relations.
2700 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2701 struct pet_scop *scop)
2703 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2706 /* Collect all tagged definite write access relations.
2708 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2709 struct pet_scop *scop)
2711 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2714 /* Collect all tagged definite kill access relations.
2716 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2717 struct pet_scop *scop)
2719 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2722 /* Collect and return the union of iteration domains in "scop".
2724 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2726 int i;
2727 isl_set *domain_i;
2728 isl_union_set *domain;
2730 if (!scop)
2731 return NULL;
2733 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2735 for (i = 0; i < scop->n_stmt; ++i) {
2736 domain_i = isl_set_copy(scop->stmts[i]->domain);
2737 if (scop->stmts[i]->n_arg > 0)
2738 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2739 domain = isl_union_set_add_set(domain, domain_i);
2742 return domain;
2745 /* Collect and return the schedules of the statements in "scop".
2746 * The range is normalized to the maximal number of scheduling
2747 * dimensions.
2749 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2751 int i, j;
2752 isl_map *schedule_i;
2753 isl_union_map *schedule;
2754 int depth, max_depth = 0;
2756 if (!scop)
2757 return NULL;
2759 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2761 for (i = 0; i < scop->n_stmt; ++i) {
2762 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2763 if (depth > max_depth)
2764 max_depth = depth;
2767 for (i = 0; i < scop->n_stmt; ++i) {
2768 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2769 depth = isl_map_dim(schedule_i, isl_dim_out);
2770 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2771 max_depth - depth);
2772 for (j = depth; j < max_depth; ++j)
2773 schedule_i = isl_map_fix_si(schedule_i,
2774 isl_dim_out, j, 0);
2775 schedule = isl_union_map_add_map(schedule, schedule_i);
2778 return schedule;
2781 /* Add a reference identifier to all access expressions in "stmt".
2782 * "n_ref" points to an integer that contains the sequence number
2783 * of the next reference.
2785 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2787 int i;
2789 if (!stmt)
2790 return NULL;
2792 for (i = 0; i < stmt->n_arg; ++i) {
2793 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2794 if (!stmt->args[i])
2795 return pet_stmt_free(stmt);
2798 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2799 if (!stmt->body)
2800 return pet_stmt_free(stmt);
2802 return stmt;
2805 /* Add a reference identifier to all access expressions in "scop".
2807 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2809 int i;
2810 int n_ref;
2812 if (!scop)
2813 return NULL;
2815 n_ref = 0;
2816 for (i = 0; i < scop->n_stmt; ++i) {
2817 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2818 if (!scop->stmts[i])
2819 return pet_scop_free(scop);
2822 return scop;
2825 /* Reset the user pointer on all parameter ids in "array".
2827 static struct pet_array *array_anonymize(struct pet_array *array)
2829 if (!array)
2830 return NULL;
2832 array->context = isl_set_reset_user(array->context);
2833 array->extent = isl_set_reset_user(array->extent);
2834 if (!array->context || !array->extent)
2835 return pet_array_free(array);
2837 return array;
2840 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2842 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2844 int i;
2845 isl_space *space;
2846 isl_set *domain;
2848 if (!stmt)
2849 return NULL;
2851 stmt->domain = isl_set_reset_user(stmt->domain);
2852 stmt->schedule = isl_map_reset_user(stmt->schedule);
2853 if (!stmt->domain || !stmt->schedule)
2854 return pet_stmt_free(stmt);
2856 for (i = 0; i < stmt->n_arg; ++i) {
2857 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2858 if (!stmt->args[i])
2859 return pet_stmt_free(stmt);
2862 stmt->body = pet_tree_anonymize(stmt->body);
2863 if (!stmt->body)
2864 return pet_stmt_free(stmt);
2866 return stmt;
2869 /* Reset the user pointer on the tuple ids and all parameter ids
2870 * in "implication".
2872 static struct pet_implication *implication_anonymize(
2873 struct pet_implication *implication)
2875 if (!implication)
2876 return NULL;
2878 implication->extension = isl_map_reset_user(implication->extension);
2879 if (!implication->extension)
2880 return pet_implication_free(implication);
2882 return implication;
2885 /* Reset the user pointer on the tuple ids and all parameter ids
2886 * in "independence".
2888 static struct pet_independence *independence_anonymize(
2889 struct pet_independence *independence)
2891 if (!independence)
2892 return NULL;
2894 independence->filter = isl_union_map_reset_user(independence->filter);
2895 independence->local = isl_union_set_reset_user(independence->local);
2896 if (!independence->filter || !independence->local)
2897 return pet_independence_free(independence);
2899 return independence;
2902 /* Reset the user pointer on all parameter and tuple ids in "scop".
2904 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2906 int i;
2908 if (!scop)
2909 return NULL;
2911 scop->context = isl_set_reset_user(scop->context);
2912 scop->context_value = isl_set_reset_user(scop->context_value);
2913 if (!scop->context || !scop->context_value)
2914 return pet_scop_free(scop);
2916 for (i = 0; i < scop->n_array; ++i) {
2917 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2918 if (!scop->arrays[i])
2919 return pet_scop_free(scop);
2922 for (i = 0; i < scop->n_stmt; ++i) {
2923 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2924 if (!scop->stmts[i])
2925 return pet_scop_free(scop);
2928 for (i = 0; i < scop->n_implication; ++i) {
2929 scop->implications[i] =
2930 implication_anonymize(scop->implications[i]);
2931 if (!scop->implications[i])
2932 return pet_scop_free(scop);
2935 for (i = 0; i < scop->n_independence; ++i) {
2936 scop->independences[i] =
2937 independence_anonymize(scop->independences[i]);
2938 if (!scop->independences[i])
2939 return pet_scop_free(scop);
2942 return scop;
2945 /* Compute the gist of the iteration domain and all access relations
2946 * of "stmt" based on the constraints on the parameters specified by "context"
2947 * and the constraints on the values of nested accesses specified
2948 * by "value_bounds".
2950 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2951 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2953 int i;
2954 isl_set *domain;
2956 if (!stmt)
2957 return NULL;
2959 domain = isl_set_copy(stmt->domain);
2960 if (stmt->n_arg > 0)
2961 domain = isl_map_domain(isl_set_unwrap(domain));
2963 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2965 for (i = 0; i < stmt->n_arg; ++i) {
2966 stmt->args[i] = pet_expr_gist(stmt->args[i],
2967 domain, value_bounds);
2968 if (!stmt->args[i])
2969 goto error;
2972 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
2973 if (!stmt->body)
2974 goto error;
2976 isl_set_free(domain);
2978 domain = isl_set_universe(pet_stmt_get_space(stmt));
2979 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2980 if (stmt->n_arg > 0)
2981 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
2982 value_bounds);
2983 stmt->domain = isl_set_gist(stmt->domain, domain);
2984 if (!stmt->domain)
2985 return pet_stmt_free(stmt);
2987 return stmt;
2988 error:
2989 isl_set_free(domain);
2990 return pet_stmt_free(stmt);
2993 /* Compute the gist of the extent of the array
2994 * based on the constraints on the parameters specified by "context".
2996 static struct pet_array *array_gist(struct pet_array *array,
2997 __isl_keep isl_set *context)
2999 if (!array)
3000 return NULL;
3002 array->extent = isl_set_gist_params(array->extent,
3003 isl_set_copy(context));
3004 if (!array->extent)
3005 return pet_array_free(array);
3007 return array;
3010 /* Compute the gist of all sets and relations in "scop"
3011 * based on the constraints on the parameters specified by "scop->context"
3012 * and the constraints on the values of nested accesses specified
3013 * by "value_bounds".
3015 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3016 __isl_keep isl_union_map *value_bounds)
3018 int i;
3020 if (!scop)
3021 return NULL;
3023 scop->context = isl_set_coalesce(scop->context);
3024 if (!scop->context)
3025 return pet_scop_free(scop);
3027 for (i = 0; i < scop->n_array; ++i) {
3028 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3029 if (!scop->arrays[i])
3030 return pet_scop_free(scop);
3033 for (i = 0; i < scop->n_stmt; ++i) {
3034 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3035 value_bounds);
3036 if (!scop->stmts[i])
3037 return pet_scop_free(scop);
3040 return scop;
3043 /* Intersect the context of "scop" with "context".
3044 * To ensure that we don't introduce any unnamed parameters in
3045 * the context of "scop", we first remove the unnamed parameters
3046 * from "context".
3048 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3049 __isl_take isl_set *context)
3051 if (!scop)
3052 goto error;
3054 context = pet_nested_remove_from_set(context);
3055 scop->context = isl_set_intersect(scop->context, context);
3056 if (!scop->context)
3057 return pet_scop_free(scop);
3059 return scop;
3060 error:
3061 isl_set_free(context);
3062 return pet_scop_free(scop);
3065 /* Drop the current context of "scop". That is, replace the context
3066 * by a universal set.
3068 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3070 isl_space *space;
3072 if (!scop)
3073 return NULL;
3075 space = isl_set_get_space(scop->context);
3076 isl_set_free(scop->context);
3077 scop->context = isl_set_universe(space);
3078 if (!scop->context)
3079 return pet_scop_free(scop);
3081 return scop;
3084 /* Append "array" to the arrays of "scop".
3086 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3087 struct pet_array *array)
3089 isl_ctx *ctx;
3090 struct pet_array **arrays;
3092 if (!array || !scop)
3093 goto error;
3095 ctx = isl_set_get_ctx(scop->context);
3096 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3097 scop->n_array + 1);
3098 if (!arrays)
3099 goto error;
3100 scop->arrays = arrays;
3101 scop->arrays[scop->n_array] = array;
3102 scop->n_array++;
3104 return scop;
3105 error:
3106 pet_array_free(array);
3107 return pet_scop_free(scop);
3110 /* Create an index expression for an access to a virtual array
3111 * representing the result of a condition.
3112 * Unlike other accessed data, the id of the array is NULL as
3113 * there is no ValueDecl in the program corresponding to the virtual
3114 * array.
3115 * The index expression is created as an identity mapping on "space".
3116 * That is, the dimension of the array is the same as that of "space".
3118 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3119 int test_nr)
3121 isl_id *id;
3122 char name[50];
3124 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3125 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3126 space = isl_space_map_from_set(space);
3127 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3128 return isl_multi_pw_aff_identity(space);
3131 /* Add an array with the given extent to the list
3132 * of arrays in "scop" and return the extended pet_scop.
3133 * Specifically, the extent is determined by the image of "domain"
3134 * under "index".
3135 * "int_size" is the number of bytes needed to represent values of type "int".
3136 * The array is marked as attaining values 0 and 1 only and
3137 * as each element being assigned at most once.
3139 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3140 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3141 int int_size)
3143 isl_ctx *ctx;
3144 isl_space *space;
3145 struct pet_array *array;
3146 isl_map *access;
3148 if (!scop || !domain || !index)
3149 goto error;
3151 ctx = isl_multi_pw_aff_get_ctx(index);
3152 array = isl_calloc_type(ctx, struct pet_array);
3153 if (!array)
3154 goto error;
3156 access = isl_map_from_multi_pw_aff(index);
3157 access = isl_map_intersect_domain(access, domain);
3158 array->extent = isl_map_range(access);
3159 space = isl_space_params_alloc(ctx, 0);
3160 array->context = isl_set_universe(space);
3161 space = isl_space_set_alloc(ctx, 0, 1);
3162 array->value_bounds = isl_set_universe(space);
3163 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3164 isl_dim_set, 0, 0);
3165 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3166 isl_dim_set, 0, 1);
3167 array->element_type = strdup("int");
3168 array->element_size = int_size;
3169 array->uniquely_defined = 1;
3171 if (!array->extent || !array->context)
3172 array = pet_array_free(array);
3174 scop = pet_scop_add_array(scop, array);
3176 return scop;
3177 error:
3178 isl_set_free(domain);
3179 isl_multi_pw_aff_free(index);
3180 return pet_scop_free(scop);
3183 /* Create and return an implication on filter values equal to "satisfied"
3184 * with extension "map".
3186 static struct pet_implication *new_implication(__isl_take isl_map *map,
3187 int satisfied)
3189 isl_ctx *ctx;
3190 struct pet_implication *implication;
3192 if (!map)
3193 return NULL;
3194 ctx = isl_map_get_ctx(map);
3195 implication = isl_alloc_type(ctx, struct pet_implication);
3196 if (!implication)
3197 goto error;
3199 implication->extension = map;
3200 implication->satisfied = satisfied;
3202 return implication;
3203 error:
3204 isl_map_free(map);
3205 return NULL;
3208 /* Add an implication on filter values equal to "satisfied"
3209 * with extension "map" to "scop".
3211 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3212 __isl_take isl_map *map, int satisfied)
3214 isl_ctx *ctx;
3215 struct pet_implication *implication;
3216 struct pet_implication **implications;
3218 implication = new_implication(map, satisfied);
3219 if (!scop || !implication)
3220 goto error;
3222 ctx = isl_set_get_ctx(scop->context);
3223 implications = isl_realloc_array(ctx, scop->implications,
3224 struct pet_implication *,
3225 scop->n_implication + 1);
3226 if (!implications)
3227 goto error;
3228 scop->implications = implications;
3229 scop->implications[scop->n_implication] = implication;
3230 scop->n_implication++;
3232 return scop;
3233 error:
3234 pet_implication_free(implication);
3235 return pet_scop_free(scop);
3238 /* Create and return a function that maps the iteration domains
3239 * of the statements in "scop" onto their outer "n" dimensions.
3240 * "space" is the parameters space of the created function.
3242 static __isl_give isl_union_pw_multi_aff *outer_projection(
3243 struct pet_scop *scop, __isl_take isl_space *space, int n)
3245 int i;
3246 isl_union_pw_multi_aff *res;
3248 res = isl_union_pw_multi_aff_empty(space);
3250 if (!scop)
3251 return isl_union_pw_multi_aff_free(res);
3253 for (i = 0; i < scop->n_stmt; ++i) {
3254 struct pet_stmt *stmt = scop->stmts[i];
3255 isl_space *space;
3256 isl_multi_aff *ma;
3257 isl_pw_multi_aff *pma;
3259 space = pet_stmt_get_space(stmt);
3260 ma = pet_prefix_projection(space, n);
3261 pma = isl_pw_multi_aff_from_multi_aff(ma);
3262 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3265 return res;
3268 /* Add an independence to "scop" for the inner iterator of "domain"
3269 * with local variables "local", where "domain" represents the outer
3270 * loop iterators of all statements in "scop".
3271 * If "sign" is positive, then the inner iterator increases.
3272 * Otherwise it decreases.
3274 * The independence is supposed to filter out any dependence of
3275 * an iteration of domain on a previous iteration along the inner dimension.
3276 * We therefore create a mapping from an iteration to later iterations and
3277 * then plug in the projection of the iterations domains of "scop"
3278 * onto the outer loop iterators.
3280 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3281 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3283 int i, dim;
3284 isl_space *space;
3285 isl_map *map;
3286 isl_union_map *independence;
3287 isl_union_pw_multi_aff *proj;
3289 if (!scop || !domain || !local)
3290 goto error;
3292 dim = isl_set_dim(domain, isl_dim_set);
3293 space = isl_space_map_from_set(isl_set_get_space(domain));
3294 map = isl_map_universe(space);
3295 for (i = 0; i + 1 < dim; ++i)
3296 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3297 if (sign > 0)
3298 map = isl_map_order_lt(map,
3299 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3300 else
3301 map = isl_map_order_gt(map,
3302 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3304 independence = isl_union_map_from_map(map);
3305 space = isl_space_params(isl_set_get_space(domain));
3306 proj = outer_projection(scop, space, dim);
3307 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3308 independence, isl_union_pw_multi_aff_copy(proj));
3309 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3310 independence, proj);
3312 scop = pet_scop_add_independence(scop, independence, local);
3314 return scop;
3315 error:
3316 isl_union_set_free(local);
3317 return pet_scop_free(scop);
3320 /* Given an access expression, check if it is data dependent.
3321 * If so, set *found and abort the search.
3323 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3325 int *found = user;
3327 if (pet_expr_get_n_arg(expr) > 0) {
3328 *found = 1;
3329 return -1;
3332 return 0;
3335 /* Does "scop" contain any data dependent accesses?
3337 * Check the body of each statement for such accesses.
3339 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3341 int i;
3342 int found = 0;
3344 if (!scop)
3345 return -1;
3347 for (i = 0; i < scop->n_stmt; ++i) {
3348 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3349 &is_data_dependent, &found);
3350 if (r < 0 && !found)
3351 return -1;
3352 if (found)
3353 return found;
3356 return found;
3359 /* Does "scop" contain and data dependent conditions?
3361 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3363 int i;
3365 if (!scop)
3366 return -1;
3368 for (i = 0; i < scop->n_stmt; ++i)
3369 if (scop->stmts[i]->n_arg > 0)
3370 return 1;
3372 return 0;
3375 /* Keep track of the "input" file inside the (extended) "scop".
3377 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3379 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3381 if (!scop)
3382 return NULL;
3384 ext->input = input;
3386 return scop;
3389 /* Print the original code corresponding to "scop" to printer "p".
3391 * pet_scop_print_original can only be called from
3392 * a pet_transform_C_source callback. This means that the input
3393 * file is stored in the extended scop and that the printer prints
3394 * to a file.
3396 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3397 __isl_take isl_printer *p)
3399 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3400 FILE *output;
3401 unsigned start, end;
3403 if (!scop || !p)
3404 return isl_printer_free(p);
3406 if (!ext->input)
3407 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3408 "no input file stored in scop",
3409 return isl_printer_free(p));
3411 output = isl_printer_get_file(p);
3412 if (!output)
3413 return isl_printer_free(p);
3415 start = pet_loc_get_start(scop->loc);
3416 end = pet_loc_get_end(scop->loc);
3417 if (copy(ext->input, output, start, end) < 0)
3418 return isl_printer_free(p);
3420 return p;