privately export pet_stmt_is_affine_assume
[pet.git] / scop.c
blob8bd57f33422be45cb2f8d7b55c20ca0b23a2458c
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>
38 #include <isl/schedule_node.h>
40 #include "aff.h"
41 #include "expr.h"
42 #include "expr_access_type.h"
43 #include "filter.h"
44 #include "loc.h"
45 #include "nest.h"
46 #include "scop.h"
47 #include "tree.h"
48 #include "print.h"
49 #include "value_bounds.h"
51 /* pet_scop with extra information that is used during parsing and printing.
53 * In particular, we keep track of conditions under which we want
54 * to skip the rest of the current loop iteration (skip[pet_skip_now])
55 * and of conditions under which we want to skip subsequent
56 * loop iterations (skip[pet_skip_later]).
58 * The conditions are represented as index expressions defined
59 * over the outer loop iterators. The index expression is either
60 * a boolean affine expression or an access to a variable, which
61 * is assumed to attain values zero and one. The condition holds
62 * if the variable has value one or if the affine expression
63 * has value one (typically for only part of the domain).
65 * A missing condition (skip[type] == NULL) means that we don't want
66 * to skip anything.
68 * Additionally, we keep track of the original input file
69 * inside pet_transform_C_source.
71 struct pet_scop_ext {
72 struct pet_scop scop;
74 isl_multi_pw_aff *skip[2];
75 FILE *input;
78 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
79 * The input domain is anonymous and is the same as the domains
80 * of the access expressions inside "tree".
81 * These domains are modified to include the name of the statement.
82 * This name is given by tree->label if it is non-NULL.
83 * Otherwise, the name is constructed as S_<id>.
85 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
86 int id, __isl_take pet_tree *tree)
88 struct pet_stmt *stmt;
89 isl_ctx *ctx;
90 isl_id *label;
91 isl_space *space;
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 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
115 add_name = isl_multi_pw_aff_from_multi_aff(ma);
116 tree = pet_tree_update_domain(tree, add_name);
118 stmt->loc = pet_tree_get_loc(tree);
119 stmt->domain = domain;
120 stmt->body = tree;
122 if (!stmt->domain || !stmt->body)
123 return pet_stmt_free(stmt);
125 return stmt;
126 error:
127 isl_set_free(domain);
128 pet_tree_free(tree);
129 return NULL;
132 void *pet_stmt_free(struct pet_stmt *stmt)
134 int i;
136 if (!stmt)
137 return NULL;
139 pet_loc_free(stmt->loc);
140 isl_set_free(stmt->domain);
141 pet_tree_free(stmt->body);
143 for (i = 0; i < stmt->n_arg; ++i)
144 pet_expr_free(stmt->args[i]);
145 free(stmt->args);
147 free(stmt);
148 return NULL;
151 /* Return the iteration space of "stmt".
153 * If the statement has arguments, then stmt->domain is a wrapped map
154 * mapping the iteration domain to the values of the arguments
155 * for which this statement is executed.
156 * In this case, we need to extract the domain space of this wrapped map.
158 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
160 isl_space *space;
162 if (!stmt)
163 return NULL;
165 space = isl_set_get_space(stmt->domain);
166 if (isl_space_is_wrapping(space))
167 space = isl_space_domain(isl_space_unwrap(space));
169 return space;
172 static void stmt_dump(struct pet_stmt *stmt, int indent)
174 int i;
176 if (!stmt)
177 return;
179 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
180 fprintf(stderr, "%*s", indent, "");
181 isl_set_dump(stmt->domain);
182 pet_tree_dump_with_indent(stmt->body, indent);
183 for (i = 0; i < stmt->n_arg; ++i)
184 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
187 void pet_stmt_dump(struct pet_stmt *stmt)
189 stmt_dump(stmt, 0);
192 /* Allocate a new pet_type with the given "name" and "definition".
194 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
195 const char *definition)
197 struct pet_type *type;
199 type = isl_alloc_type(ctx, struct pet_type);
200 if (!type)
201 return NULL;
203 type->name = strdup(name);
204 type->definition = strdup(definition);
206 if (!type->name || !type->definition)
207 return pet_type_free(type);
209 return type;
212 /* Free "type" and return NULL.
214 struct pet_type *pet_type_free(struct pet_type *type)
216 if (!type)
217 return NULL;
219 free(type->name);
220 free(type->definition);
222 free(type);
223 return NULL;
226 struct pet_array *pet_array_free(struct pet_array *array)
228 if (!array)
229 return NULL;
231 isl_set_free(array->context);
232 isl_set_free(array->extent);
233 isl_set_free(array->value_bounds);
234 free(array->element_type);
236 free(array);
237 return NULL;
240 void pet_array_dump(struct pet_array *array)
242 if (!array)
243 return;
245 isl_set_dump(array->context);
246 isl_set_dump(array->extent);
247 isl_set_dump(array->value_bounds);
248 fprintf(stderr, "%s%s%s\n", array->element_type,
249 array->element_is_record ? " element-is-record" : "",
250 array->live_out ? " live-out" : "");
253 /* Alloc a pet_scop structure, with extra room for information that
254 * is only used during parsing.
256 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
258 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
261 /* Construct a pet_scop in the given space, with the given schedule and
262 * room for n statements.
264 * The context is initialized as a universe set in "space".
266 * Since no information on the location is known at this point,
267 * scop->loc is initialized with pet_loc_dummy.
269 static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n,
270 __isl_take isl_schedule *schedule)
272 isl_ctx *ctx;
273 struct pet_scop *scop;
275 if (!space || !schedule)
276 goto error;
278 ctx = isl_space_get_ctx(space);
279 scop = pet_scop_alloc(ctx);
280 if (!scop)
281 goto error;
283 scop->context = isl_set_universe(isl_space_copy(space));
284 scop->context_value = isl_set_universe(isl_space_params(space));
285 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
286 scop->schedule = schedule;
287 if (!scop->context || !scop->stmts)
288 return pet_scop_free(scop);
290 scop->loc = &pet_loc_dummy;
291 scop->n_stmt = n;
293 return scop;
294 error:
295 isl_space_free(space);
296 isl_schedule_free(schedule);
297 return NULL;
300 /* Construct a pet_scop in the given space containing 0 statements
301 * (and therefore an empty iteration domain).
303 struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
305 isl_schedule *schedule;
307 schedule = isl_schedule_empty(isl_space_copy(space));
309 return scop_alloc(space, 0, schedule);
312 /* Given either an iteration domain or a wrapped map with
313 * the iteration domain in the domain and some arguments
314 * in the range, return the iteration domain.
315 * That is, drop the arguments if there are any.
317 static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
319 if (isl_set_is_wrapping(domain))
320 domain = isl_map_domain(isl_set_unwrap(domain));
321 return domain;
324 /* Update "context" with the constraints imposed on the outer iteration
325 * domain by access expression "expr".
326 * "context" lives in an anonymous space, while the domain of the access
327 * relation of "expr" refers to a particular statement.
328 * This reference therefore needs to be stripped off.
330 static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
331 __isl_take isl_set *context)
333 isl_multi_pw_aff *mpa;
334 isl_set *domain;
336 mpa = pet_expr_access_get_index(expr);
337 domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
338 domain = isl_set_reset_tuple_id(domain);
339 context = isl_set_intersect(context, domain);
340 return context;
343 /* Update "context" with the constraints imposed on the outer iteration
344 * domain by "expr".
346 * "context" lives in an anonymous space, while the domains of
347 * the access relations in "expr" refer to a particular statement.
348 * This reference therefore needs to be stripped off.
350 * If "expr" represents a conditional operator, then a parameter or outer
351 * iterator value needs to be valid for the condition and
352 * for at least one of the remaining two arguments.
353 * If the condition is an affine expression, then we can be a bit more specific.
354 * The value then has to be valid for the second argument for
355 * non-zero accesses and valid for the third argument for zero accesses.
357 * If "expr" represents a kill statement, then its argument is the entire
358 * extent of the array being killed. Do not update "context" based
359 * on this argument as that would impose constraints that ensure that
360 * the array is non-empty.
362 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
363 __isl_take isl_set *context)
365 int i;
367 if (expr->type == pet_expr_op && expr->op == pet_op_kill)
368 return context;
370 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
371 int is_aff;
372 isl_set *context1, *context2;
374 is_aff = pet_expr_is_affine(expr->args[0]);
375 if (is_aff < 0)
376 goto error;
378 context = expr_extract_context(expr->args[0], context);
379 context1 = expr_extract_context(expr->args[1],
380 isl_set_copy(context));
381 context2 = expr_extract_context(expr->args[2], context);
383 if (is_aff) {
384 isl_multi_pw_aff *mpa;
385 isl_pw_aff *pa;
386 isl_set *zero_set;
388 mpa = pet_expr_access_get_index(expr->args[0]);
389 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
390 isl_multi_pw_aff_free(mpa);
391 zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
392 zero_set = isl_set_reset_tuple_id(zero_set);
393 context1 = isl_set_subtract(context1,
394 isl_set_copy(zero_set));
395 context2 = isl_set_intersect(context2, zero_set);
398 context = isl_set_union(context1, context2);
399 context = isl_set_coalesce(context);
401 return context;
404 for (i = 0; i < expr->n_arg; ++i)
405 context = expr_extract_context(expr->args[i], context);
407 if (expr->type == pet_expr_access)
408 context = access_extract_context(expr, context);
410 return context;
411 error:
412 isl_set_free(context);
413 return NULL;
416 /* Is "stmt" an assume statement with an affine assumption?
418 isl_bool pet_stmt_is_affine_assume(struct pet_stmt *stmt)
420 if (!stmt)
421 return isl_bool_error;
422 return pet_tree_is_affine_assume(stmt->body);
425 /* Given an assume statement "stmt" with an access argument,
426 * return the index expression of the argument.
428 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
430 if (!stmt)
431 return NULL;
432 return pet_tree_assume_get_index(stmt->body);
435 /* Update "context" with the constraints imposed on the outer iteration
436 * domain by "stmt".
438 * If the statement is an assume statement with an affine expression,
439 * then intersect "context" with that expression.
440 * Otherwise, if the statement body is an expression tree,
441 * then intersect "context" with the context of this expression.
442 * Note that we cannot safely extract a context from subtrees
443 * of the statement body since we cannot tell when those subtrees
444 * are executed, if at all.
446 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
447 __isl_take isl_set *context)
449 int i;
450 isl_bool affine;
451 pet_expr *body;
453 affine = pet_stmt_is_affine_assume(stmt);
454 if (affine < 0)
455 return isl_set_free(context);
456 if (affine) {
457 isl_multi_pw_aff *index;
458 isl_pw_aff *pa;
459 isl_set *cond;
461 index = pet_stmt_assume_get_index(stmt);
462 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
463 isl_multi_pw_aff_free(index);
464 cond = isl_pw_aff_non_zero_set(pa);
465 cond = isl_set_reset_tuple_id(cond);
466 return isl_set_intersect(context, cond);
469 for (i = 0; i < stmt->n_arg; ++i)
470 context = expr_extract_context(stmt->args[i], context);
472 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
473 return context;
475 body = pet_tree_expr_get_expr(stmt->body);
476 context = expr_extract_context(body, context);
477 pet_expr_free(body);
479 return context;
482 /* Construct a pet_scop in the given space that contains the given pet_stmt.
483 * The initial schedule consists of only the iteration domain.
485 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
486 struct pet_stmt *stmt)
488 struct pet_scop *scop;
489 isl_set *set;
490 isl_union_set *domain;
491 isl_schedule *schedule;
493 if (!stmt) {
494 isl_space_free(space);
495 return NULL;
498 set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
499 domain = isl_union_set_from_set(set);
500 schedule = isl_schedule_from_domain(domain);
502 scop = scop_alloc(space, 1, schedule);
503 if (!scop)
504 goto error;
506 scop->context = stmt_extract_context(stmt, scop->context);
507 if (!scop->context)
508 goto error;
510 scop->stmts[0] = stmt;
511 scop->loc = pet_loc_copy(stmt->loc);
513 if (!scop->loc)
514 return pet_scop_free(scop);
516 return scop;
517 error:
518 pet_stmt_free(stmt);
519 pet_scop_free(scop);
520 return NULL;
523 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
524 * does it represent an affine expression?
526 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
528 int has_id;
530 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
531 if (has_id < 0)
532 return -1;
534 return !has_id;
537 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
539 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
540 __isl_take isl_set *dom)
542 isl_pw_aff *pa;
543 pa = isl_set_indicator_function(set);
544 pa = isl_pw_aff_intersect_domain(pa, dom);
545 return pa;
548 /* Return "lhs || rhs", defined on the shared definition domain.
550 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
551 __isl_take isl_pw_aff *rhs)
553 isl_set *cond;
554 isl_set *dom;
556 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
557 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
558 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
559 isl_pw_aff_non_zero_set(rhs));
560 cond = isl_set_coalesce(cond);
561 return indicator_function(cond, dom);
564 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
565 * ext may be equal to either ext1 or ext2.
567 * The two skips that need to be combined are assumed to be affine expressions.
569 * We need to skip in ext if we need to skip in either ext1 or ext2.
570 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
572 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
573 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
574 enum pet_skip type)
576 isl_pw_aff *skip, *skip1, *skip2;
578 if (!ext)
579 return NULL;
580 if (!ext1->skip[type] && !ext2->skip[type])
581 return ext;
582 if (!ext1->skip[type]) {
583 if (ext == ext2)
584 return ext;
585 ext->skip[type] = ext2->skip[type];
586 ext2->skip[type] = NULL;
587 return ext;
589 if (!ext2->skip[type]) {
590 if (ext == ext1)
591 return ext;
592 ext->skip[type] = ext1->skip[type];
593 ext1->skip[type] = NULL;
594 return ext;
597 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
598 !multi_pw_aff_is_affine(ext2->skip[type]))
599 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
600 isl_error_internal, "can only combine affine skips",
601 goto error);
603 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
604 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
605 skip = pw_aff_or(skip1, skip2);
606 isl_multi_pw_aff_free(ext1->skip[type]);
607 ext1->skip[type] = NULL;
608 isl_multi_pw_aff_free(ext2->skip[type]);
609 ext2->skip[type] = NULL;
610 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
611 if (!ext->skip[type])
612 goto error;
614 return ext;
615 error:
616 pet_scop_free(&ext->scop);
617 return NULL;
620 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
621 * where type takes on the values pet_skip_now and pet_skip_later.
622 * scop may be equal to either scop1 or scop2.
624 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
625 struct pet_scop *scop1, struct pet_scop *scop2)
627 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
628 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
629 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
631 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
632 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
633 return &ext->scop;
636 /* Update start and end of scop->loc to include the region from "start"
637 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
638 * does not have any offset information yet and we simply take the information
639 * from "start" and "end". Otherwise, we update loc using "start" and "end".
641 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
642 unsigned start, unsigned end)
644 if (!scop)
645 return NULL;
647 if (scop->loc == &pet_loc_dummy)
648 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
649 start, end, -1, strdup(""));
650 else
651 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
653 if (!scop->loc)
654 return pet_scop_free(scop);
656 return scop;
659 /* Update start and end of scop->loc to include the region identified
660 * by "loc".
662 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
663 __isl_keep pet_loc *loc)
665 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
666 pet_loc_get_end(loc));
669 /* Replace the location of "scop" by "loc".
671 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
672 __isl_take pet_loc *loc)
674 if (!scop || !loc)
675 goto error;
677 pet_loc_free(scop->loc);
678 scop->loc = loc;
680 return scop;
681 error:
682 pet_loc_free(loc);
683 pet_scop_free(scop);
684 return NULL;
687 /* Does "implication" appear in the list of implications of "scop"?
689 static int is_known_implication(struct pet_scop *scop,
690 struct pet_implication *implication)
692 int i;
694 for (i = 0; i < scop->n_implication; ++i) {
695 struct pet_implication *pi = scop->implications[i];
696 int equal;
698 if (pi->satisfied != implication->satisfied)
699 continue;
700 equal = isl_map_is_equal(pi->extension, implication->extension);
701 if (equal < 0)
702 return -1;
703 if (equal)
704 return 1;
707 return 0;
710 /* Store the concatenation of the implications of "scop1" and "scop2"
711 * in "scop", removing duplicates (i.e., implications in "scop2" that
712 * already appear in "scop1").
714 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
715 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
717 int i, j;
719 if (!scop)
720 return NULL;
722 if (scop2->n_implication == 0) {
723 scop->n_implication = scop1->n_implication;
724 scop->implications = scop1->implications;
725 scop1->n_implication = 0;
726 scop1->implications = NULL;
727 return scop;
730 if (scop1->n_implication == 0) {
731 scop->n_implication = scop2->n_implication;
732 scop->implications = scop2->implications;
733 scop2->n_implication = 0;
734 scop2->implications = NULL;
735 return scop;
738 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
739 scop1->n_implication + scop2->n_implication);
740 if (!scop->implications)
741 return pet_scop_free(scop);
743 for (i = 0; i < scop1->n_implication; ++i) {
744 scop->implications[i] = scop1->implications[i];
745 scop1->implications[i] = NULL;
748 scop->n_implication = scop1->n_implication;
749 j = scop1->n_implication;
750 for (i = 0; i < scop2->n_implication; ++i) {
751 int known;
753 known = is_known_implication(scop, scop2->implications[i]);
754 if (known < 0)
755 return pet_scop_free(scop);
756 if (known)
757 continue;
758 scop->implications[j++] = scop2->implications[i];
759 scop2->implications[i] = NULL;
761 scop->n_implication = j;
763 return scop;
766 /* Combine the offset information of "scop1" and "scop2" into "scop".
768 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
769 struct pet_scop *scop1, struct pet_scop *scop2)
771 if (scop1->loc != &pet_loc_dummy)
772 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
773 if (scop2->loc != &pet_loc_dummy)
774 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
775 return scop;
778 /* Create and return an independence that filters out the dependences
779 * in "filter" with local variables "local".
781 static struct pet_independence *new_independence(
782 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
784 isl_ctx *ctx;
785 struct pet_independence *independence;
787 if (!filter || !local)
788 goto error;
789 ctx = isl_union_map_get_ctx(filter);
790 independence = isl_alloc_type(ctx, struct pet_independence);
791 if (!independence)
792 goto error;
794 independence->filter = filter;
795 independence->local = local;
797 return independence;
798 error:
799 isl_union_map_free(filter);
800 isl_union_set_free(local);
801 return NULL;
804 /* Add an independence that filters out the dependences
805 * in "filter" with local variables "local" to "scop".
807 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
808 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
810 isl_ctx *ctx;
811 struct pet_independence *independence;
812 struct pet_independence **independences;
814 ctx = isl_union_map_get_ctx(filter);
815 independence = new_independence(filter, local);
816 if (!scop || !independence)
817 goto error;
819 independences = isl_realloc_array(ctx, scop->independences,
820 struct pet_independence *,
821 scop->n_independence + 1);
822 if (!independences)
823 goto error;
824 scop->independences = independences;
825 scop->independences[scop->n_independence] = independence;
826 scop->n_independence++;
828 return scop;
829 error:
830 pet_independence_free(independence);
831 pet_scop_free(scop);
832 return NULL;
835 /* Store the concatenation of the independences of "scop1" and "scop2"
836 * in "scop".
838 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
839 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
841 int i, off;
843 if (!scop)
844 return NULL;
846 if (scop2->n_independence == 0) {
847 scop->n_independence = scop1->n_independence;
848 scop->independences = scop1->independences;
849 scop1->n_independence = 0;
850 scop1->independences = NULL;
851 return scop;
854 if (scop1->n_independence == 0) {
855 scop->n_independence = scop2->n_independence;
856 scop->independences = scop2->independences;
857 scop2->n_independence = 0;
858 scop2->independences = NULL;
859 return scop;
862 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
863 scop1->n_independence + scop2->n_independence);
864 if (!scop->independences)
865 return pet_scop_free(scop);
867 for (i = 0; i < scop1->n_independence; ++i) {
868 scop->independences[i] = scop1->independences[i];
869 scop1->independences[i] = NULL;
872 off = scop1->n_independence;
873 for (i = 0; i < scop2->n_independence; ++i) {
874 scop->independences[off + i] = scop2->independences[i];
875 scop2->independences[i] = NULL;
877 scop->n_independence = scop1->n_independence + scop2->n_independence;
879 return scop;
882 /* Construct a pet_scop with the given schedule
883 * that contains the offset information,
884 * arrays, statements and skip information in "scop1" and "scop2".
886 static struct pet_scop *pet_scop_add(isl_ctx *ctx,
887 __isl_take isl_schedule *schedule, struct pet_scop *scop1,
888 struct pet_scop *scop2)
890 int i;
891 isl_space *space;
892 struct pet_scop *scop = NULL;
894 if (!scop1 || !scop2)
895 goto error;
897 if (scop1->n_stmt == 0) {
898 scop2 = scop_combine_skips(scop2, scop1, scop2);
899 pet_scop_free(scop1);
900 isl_schedule_free(schedule);
901 return scop2;
904 if (scop2->n_stmt == 0) {
905 scop1 = scop_combine_skips(scop1, scop1, scop2);
906 pet_scop_free(scop2);
907 isl_schedule_free(schedule);
908 return scop1;
911 space = isl_set_get_space(scop1->context);
912 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
913 isl_schedule_copy(schedule));
914 if (!scop)
915 goto error;
917 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
918 scop1->n_array + scop2->n_array);
919 if (!scop->arrays)
920 goto error;
921 scop->n_array = scop1->n_array + scop2->n_array;
923 for (i = 0; i < scop1->n_stmt; ++i) {
924 scop->stmts[i] = scop1->stmts[i];
925 scop1->stmts[i] = NULL;
928 for (i = 0; i < scop2->n_stmt; ++i) {
929 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
930 scop2->stmts[i] = NULL;
933 for (i = 0; i < scop1->n_array; ++i) {
934 scop->arrays[i] = scop1->arrays[i];
935 scop1->arrays[i] = NULL;
938 for (i = 0; i < scop2->n_array; ++i) {
939 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
940 scop2->arrays[i] = NULL;
943 scop = scop_collect_implications(ctx, scop, scop1, scop2);
944 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
945 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
946 scop = scop_combine_skips(scop, scop1, scop2);
947 scop = scop_combine_start_end(scop, scop1, scop2);
948 scop = scop_collect_independences(ctx, scop, scop1, scop2);
950 pet_scop_free(scop1);
951 pet_scop_free(scop2);
952 isl_schedule_free(schedule);
953 return scop;
954 error:
955 pet_scop_free(scop1);
956 pet_scop_free(scop2);
957 pet_scop_free(scop);
958 isl_schedule_free(schedule);
959 return NULL;
962 /* Apply the skip condition "skip" to "scop".
963 * That is, make sure "scop" is not executed when the condition holds.
965 * If "skip" is an affine expression, we add the conditions under
966 * which the expression is zero to the context and the skip conditions
967 * of "scop".
968 * Otherwise, we add a filter on the variable attaining the value zero.
970 static struct pet_scop *restrict_skip(struct pet_scop *scop,
971 __isl_take isl_multi_pw_aff *skip)
973 isl_set *zero;
974 isl_pw_aff *pa;
975 int is_aff;
977 if (!scop || !skip)
978 goto error;
980 is_aff = multi_pw_aff_is_affine(skip);
981 if (is_aff < 0)
982 goto error;
984 if (!is_aff)
985 return pet_scop_filter(scop, skip, 0);
987 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
988 isl_multi_pw_aff_free(skip);
989 zero = isl_pw_aff_zero_set(pa);
990 scop = pet_scop_restrict(scop, zero);
992 return scop;
993 error:
994 isl_multi_pw_aff_free(skip);
995 return pet_scop_free(scop);
998 /* Construct a pet_scop that contains the arrays, statements and
999 * skip information in "scop1" and "scop2", where the two scops
1000 * are executed "in sequence". That is, breaks and continues
1001 * in scop1 have an effect on scop2 and the schedule of the result
1002 * is the sequence of the schedules of "scop1" and "scop2".
1004 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1005 struct pet_scop *scop2)
1007 isl_schedule *schedule;
1009 if (!scop1 || !scop2)
1010 goto error;
1012 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1013 scop2 = restrict_skip(scop2,
1014 pet_scop_get_skip(scop1, pet_skip_now));
1015 schedule = isl_schedule_sequence(isl_schedule_copy(scop1->schedule),
1016 isl_schedule_copy(scop2->schedule));
1017 return pet_scop_add(ctx, schedule, scop1, scop2);
1018 error:
1019 pet_scop_free(scop1);
1020 pet_scop_free(scop2);
1021 return NULL;
1024 /* Construct a pet_scop that contains the arrays, statements and
1025 * skip information in "scop1" and "scop2", where the two scops
1026 * are executed "in parallel". That is, any break or continue
1027 * in scop1 has no effect on scop2 and the schedule of the result
1028 * is the set of the schedules of "scop1" and "scop2".
1030 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1031 struct pet_scop *scop2)
1033 isl_schedule *schedule;
1035 if (!scop1 || !scop2)
1036 goto error;
1038 schedule = isl_schedule_set(isl_schedule_copy(scop1->schedule),
1039 isl_schedule_copy(scop2->schedule));
1040 return pet_scop_add(ctx, schedule, scop1, scop2);
1041 error:
1042 pet_scop_free(scop1);
1043 pet_scop_free(scop2);
1044 return NULL;
1047 void *pet_implication_free(struct pet_implication *implication)
1049 int i;
1051 if (!implication)
1052 return NULL;
1054 isl_map_free(implication->extension);
1056 free(implication);
1057 return NULL;
1060 void *pet_independence_free(struct pet_independence *independence)
1062 if (!independence)
1063 return NULL;
1065 isl_union_map_free(independence->filter);
1066 isl_union_set_free(independence->local);
1068 free(independence);
1069 return NULL;
1072 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1074 int i;
1075 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1077 if (!scop)
1078 return NULL;
1079 pet_loc_free(scop->loc);
1080 isl_set_free(scop->context);
1081 isl_set_free(scop->context_value);
1082 isl_schedule_free(scop->schedule);
1083 if (scop->types)
1084 for (i = 0; i < scop->n_type; ++i)
1085 pet_type_free(scop->types[i]);
1086 free(scop->types);
1087 if (scop->arrays)
1088 for (i = 0; i < scop->n_array; ++i)
1089 pet_array_free(scop->arrays[i]);
1090 free(scop->arrays);
1091 if (scop->stmts)
1092 for (i = 0; i < scop->n_stmt; ++i)
1093 pet_stmt_free(scop->stmts[i]);
1094 free(scop->stmts);
1095 if (scop->implications)
1096 for (i = 0; i < scop->n_implication; ++i)
1097 pet_implication_free(scop->implications[i]);
1098 free(scop->implications);
1099 if (scop->independences)
1100 for (i = 0; i < scop->n_independence; ++i)
1101 pet_independence_free(scop->independences[i]);
1102 free(scop->independences);
1103 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1104 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1105 free(scop);
1106 return NULL;
1109 void pet_type_dump(struct pet_type *type)
1111 if (!type)
1112 return;
1114 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1117 void pet_implication_dump(struct pet_implication *implication)
1119 if (!implication)
1120 return;
1122 fprintf(stderr, "%d\n", implication->satisfied);
1123 isl_map_dump(implication->extension);
1126 void pet_scop_dump(struct pet_scop *scop)
1128 int i;
1129 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1131 if (!scop)
1132 return;
1134 isl_set_dump(scop->context);
1135 isl_set_dump(scop->context_value);
1136 isl_schedule_dump(scop->schedule);
1137 for (i = 0; i < scop->n_type; ++i)
1138 pet_type_dump(scop->types[i]);
1139 for (i = 0; i < scop->n_array; ++i)
1140 pet_array_dump(scop->arrays[i]);
1141 for (i = 0; i < scop->n_stmt; ++i)
1142 pet_stmt_dump(scop->stmts[i]);
1143 for (i = 0; i < scop->n_implication; ++i)
1144 pet_implication_dump(scop->implications[i]);
1146 if (ext->skip[0]) {
1147 fprintf(stderr, "skip\n");
1148 isl_multi_pw_aff_dump(ext->skip[0]);
1149 isl_multi_pw_aff_dump(ext->skip[1]);
1153 /* Return 1 if the two pet_arrays are equivalent.
1155 * We don't compare element_size as this may be target dependent.
1157 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1159 if (!array1 || !array2)
1160 return 0;
1162 if (!isl_set_is_equal(array1->context, array2->context))
1163 return 0;
1164 if (!isl_set_is_equal(array1->extent, array2->extent))
1165 return 0;
1166 if (!!array1->value_bounds != !!array2->value_bounds)
1167 return 0;
1168 if (array1->value_bounds &&
1169 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1170 return 0;
1171 if (strcmp(array1->element_type, array2->element_type))
1172 return 0;
1173 if (array1->element_is_record != array2->element_is_record)
1174 return 0;
1175 if (array1->live_out != array2->live_out)
1176 return 0;
1177 if (array1->uniquely_defined != array2->uniquely_defined)
1178 return 0;
1179 if (array1->declared != array2->declared)
1180 return 0;
1181 if (array1->exposed != array2->exposed)
1182 return 0;
1184 return 1;
1187 /* Return 1 if the two pet_stmts are equivalent.
1189 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1191 int i;
1193 if (!stmt1 || !stmt2)
1194 return 0;
1196 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1197 return 0;
1198 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1199 return 0;
1200 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1201 return 0;
1202 if (stmt1->n_arg != stmt2->n_arg)
1203 return 0;
1204 for (i = 0; i < stmt1->n_arg; ++i) {
1205 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1206 return 0;
1209 return 1;
1212 /* Return 1 if the two pet_types are equivalent.
1214 * We only compare the names of the types since the exact representation
1215 * of the definition may depend on the version of clang being used.
1217 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1219 if (!type1 || !type2)
1220 return 0;
1222 if (strcmp(type1->name, type2->name))
1223 return 0;
1225 return 1;
1228 /* Return 1 if the two pet_implications are equivalent.
1230 int pet_implication_is_equal(struct pet_implication *implication1,
1231 struct pet_implication *implication2)
1233 if (!implication1 || !implication2)
1234 return 0;
1236 if (implication1->satisfied != implication2->satisfied)
1237 return 0;
1238 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1239 return 0;
1241 return 1;
1244 /* Return 1 if the two pet_independences are equivalent.
1246 int pet_independence_is_equal(struct pet_independence *independence1,
1247 struct pet_independence *independence2)
1249 if (!independence1 || !independence2)
1250 return 0;
1252 if (!isl_union_map_is_equal(independence1->filter,
1253 independence2->filter))
1254 return 0;
1255 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1256 return 0;
1258 return 1;
1261 /* Return 1 if the two pet_scops are equivalent.
1263 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1265 int i;
1266 int equal;
1268 if (!scop1 || !scop2)
1269 return 0;
1271 if (!isl_set_is_equal(scop1->context, scop2->context))
1272 return 0;
1273 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1274 return 0;
1275 equal = isl_schedule_plain_is_equal(scop1->schedule, scop2->schedule);
1276 if (equal < 0)
1277 return -1;
1278 if (!equal)
1279 return 0;
1281 if (scop1->n_type != scop2->n_type)
1282 return 0;
1283 for (i = 0; i < scop1->n_type; ++i)
1284 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1285 return 0;
1287 if (scop1->n_array != scop2->n_array)
1288 return 0;
1289 for (i = 0; i < scop1->n_array; ++i)
1290 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1291 return 0;
1293 if (scop1->n_stmt != scop2->n_stmt)
1294 return 0;
1295 for (i = 0; i < scop1->n_stmt; ++i)
1296 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1297 return 0;
1299 if (scop1->n_implication != scop2->n_implication)
1300 return 0;
1301 for (i = 0; i < scop1->n_implication; ++i)
1302 if (!pet_implication_is_equal(scop1->implications[i],
1303 scop2->implications[i]))
1304 return 0;
1306 if (scop1->n_independence != scop2->n_independence)
1307 return 0;
1308 for (i = 0; i < scop1->n_independence; ++i)
1309 if (!pet_independence_is_equal(scop1->independences[i],
1310 scop2->independences[i]))
1311 return 0;
1313 return 1;
1316 /* Does the set "extent" reference a virtual array, i.e.,
1317 * one with user pointer equal to NULL?
1318 * A virtual array does not have any members.
1320 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1322 isl_id *id;
1323 int is_virtual;
1325 if (!isl_set_has_tuple_id(extent))
1326 return 0;
1327 if (isl_set_is_wrapping(extent))
1328 return 0;
1329 id = isl_set_get_tuple_id(extent);
1330 is_virtual = !isl_id_get_user(id);
1331 isl_id_free(id);
1333 return is_virtual;
1336 /* Intersect the initial dimensions of "array" with "domain", provided
1337 * that "array" represents a virtual array.
1339 * If "array" is virtual, then We take the preimage of "domain"
1340 * over the projection of the extent of "array" onto its initial dimensions
1341 * and intersect this extent with the result.
1343 static struct pet_array *virtual_array_intersect_domain_prefix(
1344 struct pet_array *array, __isl_take isl_set *domain)
1346 int n;
1347 isl_space *space;
1348 isl_multi_aff *ma;
1350 if (!array || !extent_is_virtual_array(array->extent)) {
1351 isl_set_free(domain);
1352 return array;
1355 space = isl_set_get_space(array->extent);
1356 n = isl_set_dim(domain, isl_dim_set);
1357 ma = pet_prefix_projection(space, n);
1358 domain = isl_set_preimage_multi_aff(domain, ma);
1360 array->extent = isl_set_intersect(array->extent, domain);
1361 if (!array->extent)
1362 return pet_array_free(array);
1364 return array;
1367 /* Intersect the initial dimensions of the domain of "stmt"
1368 * with "domain".
1370 * We take the preimage of "domain" over the projection of the
1371 * domain of "stmt" onto its initial dimensions and intersect
1372 * the domain of "stmt" with the result.
1374 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1375 __isl_take isl_set *domain)
1377 int n;
1378 isl_space *space;
1379 isl_multi_aff *ma;
1381 if (!stmt)
1382 goto error;
1384 space = isl_set_get_space(stmt->domain);
1385 n = isl_set_dim(domain, isl_dim_set);
1386 ma = pet_prefix_projection(space, n);
1387 domain = isl_set_preimage_multi_aff(domain, ma);
1389 stmt->domain = isl_set_intersect(stmt->domain, domain);
1390 if (!stmt->domain)
1391 return pet_stmt_free(stmt);
1393 return stmt;
1394 error:
1395 isl_set_free(domain);
1396 return pet_stmt_free(stmt);
1399 /* Intersect the initial dimensions of the domain of "implication"
1400 * with "domain".
1402 * We take the preimage of "domain" over the projection of the
1403 * domain of "implication" onto its initial dimensions and intersect
1404 * the domain of "implication" with the result.
1406 static struct pet_implication *implication_intersect_domain_prefix(
1407 struct pet_implication *implication, __isl_take isl_set *domain)
1409 int n;
1410 isl_space *space;
1411 isl_multi_aff *ma;
1413 if (!implication)
1414 goto error;
1416 space = isl_map_get_space(implication->extension);
1417 n = isl_set_dim(domain, isl_dim_set);
1418 ma = pet_prefix_projection(isl_space_domain(space), n);
1419 domain = isl_set_preimage_multi_aff(domain, ma);
1421 implication->extension =
1422 isl_map_intersect_domain(implication->extension, domain);
1423 if (!implication->extension)
1424 return pet_implication_free(implication);
1426 return implication;
1427 error:
1428 isl_set_free(domain);
1429 return pet_implication_free(implication);
1432 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1434 * The extents of the virtual arrays match the iteration domains,
1435 * so if the iteration domain changes, we need to change those extents too.
1437 * The domain of the schedule is intersected with (i.e., replaced by)
1438 * the union of the updated iteration domains.
1440 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1441 __isl_take isl_set *domain)
1443 int i;
1445 if (!scop)
1446 goto error;
1448 for (i = 0; i < scop->n_array; ++i) {
1449 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1450 scop->arrays[i], isl_set_copy(domain));
1451 if (!scop->arrays[i])
1452 goto error;
1455 for (i = 0; i < scop->n_stmt; ++i) {
1456 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1457 isl_set_copy(domain));
1458 if (!scop->stmts[i])
1459 goto error;
1462 for (i = 0; i < scop->n_implication; ++i) {
1463 scop->implications[i] =
1464 implication_intersect_domain_prefix(scop->implications[i],
1465 isl_set_copy(domain));
1466 if (!scop->implications[i])
1467 return pet_scop_free(scop);
1470 scop->schedule = isl_schedule_intersect_domain(scop->schedule,
1471 pet_scop_get_instance_set(scop));
1472 if (!scop->schedule)
1473 goto error;
1475 isl_set_free(domain);
1476 return scop;
1477 error:
1478 isl_set_free(domain);
1479 return pet_scop_free(scop);
1482 /* Update the context with respect to an embedding into a loop
1483 * with iteration domain "dom".
1484 * The input context lives in the same space as "dom".
1485 * The output context has the inner dimension removed.
1487 * An outer loop iterator value is invalid for the embedding if
1488 * any of the corresponding inner iterator values is invalid.
1489 * That is, an outer loop iterator value is valid only if all the corresponding
1490 * inner iterator values are valid.
1491 * We therefore compute the set of outer loop iterators l
1493 * forall i: dom(l,i) => valid(l,i)
1495 * or
1497 * forall i: not dom(l,i) or valid(l,i)
1499 * or
1501 * not exists i: dom(l,i) and not valid(l,i)
1503 * i.e.,
1505 * not exists i: (dom \ valid)(l,i)
1507 * If there are any unnamed parameters in "dom", then we consider
1508 * a parameter value to be valid if it is valid for any value of those
1509 * unnamed parameters. They are therefore projected out at the end.
1511 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1512 __isl_keep isl_set *dom)
1514 int pos;
1516 pos = isl_set_dim(context, isl_dim_set) - 1;
1517 context = isl_set_subtract(isl_set_copy(dom), context);
1518 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1519 context = isl_set_complement(context);
1520 context = pet_nested_remove_from_set(context);
1522 return context;
1525 /* Update the implication with respect to an embedding into a loop
1526 * with iteration domain "dom".
1528 * Since embed_access extends virtual arrays along with the domain
1529 * of the access, we need to do the same with domain and range
1530 * of the implication. Since the original implication is only valid
1531 * within a given iteration of the loop, the extended implication
1532 * maps the extra array dimension corresponding to the extra loop
1533 * to itself.
1535 static struct pet_implication *pet_implication_embed(
1536 struct pet_implication *implication, __isl_take isl_set *dom)
1538 isl_id *id;
1539 isl_map *map;
1541 if (!implication)
1542 goto error;
1544 map = isl_set_identity(dom);
1545 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1546 map = isl_map_flat_product(map, implication->extension);
1547 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1548 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1549 implication->extension = map;
1550 if (!implication->extension)
1551 return pet_implication_free(implication);
1553 return implication;
1554 error:
1555 isl_set_free(dom);
1556 return NULL;
1559 /* Internal data structure for outer_projection_mupa.
1561 * "n" is the number of outer dimensions onto which to project.
1562 * "res" collects the result.
1564 struct pet_outer_projection_data {
1565 int n;
1566 isl_union_pw_multi_aff *res;
1569 /* Create a function that maps "set" onto its outer data->n dimensions and
1570 * add it to data->res.
1572 static isl_stat add_outer_projection(__isl_take isl_set *set, void *user)
1574 struct pet_outer_projection_data *data = user;
1575 int dim;
1576 isl_space *space;
1577 isl_pw_multi_aff *pma;
1579 dim = isl_set_dim(set, isl_dim_set);
1580 space = isl_set_get_space(set);
1581 pma = isl_pw_multi_aff_project_out_map(space,
1582 isl_dim_set, data->n, dim - data->n);
1583 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
1585 isl_set_free(set);
1587 return isl_stat_ok;
1590 /* Create and return a function that maps the sets in "domain"
1591 * onto their outer "n" dimensions.
1593 static __isl_give isl_multi_union_pw_aff *outer_projection_mupa(
1594 __isl_take isl_union_set *domain, int n)
1596 struct pet_outer_projection_data data;
1597 isl_space *space;
1599 space = isl_union_set_get_space(domain);
1600 data.n = n;
1601 data.res = isl_union_pw_multi_aff_empty(space);
1602 if (isl_union_set_foreach_set(domain, &add_outer_projection, &data) < 0)
1603 data.res = isl_union_pw_multi_aff_free(data.res);
1605 isl_union_set_free(domain);
1606 return isl_multi_union_pw_aff_from_union_pw_multi_aff(data.res);
1609 /* Embed "schedule" in a loop with schedule "prefix".
1610 * The domain of "prefix" corresponds to the outer dimensions
1611 * of the iteration domains.
1612 * We therefore construct a projection onto these outer dimensions,
1613 * compose it with "prefix" and then add the result as a band schedule.
1615 * If the domain of the schedule is empty, then there is no need
1616 * to insert any node.
1618 static __isl_give isl_schedule *schedule_embed(
1619 __isl_take isl_schedule *schedule, __isl_keep isl_multi_aff *prefix)
1621 int n;
1622 int empty;
1623 isl_union_set *domain;
1624 isl_multi_aff *ma;
1625 isl_multi_union_pw_aff *mupa;
1627 domain = isl_schedule_get_domain(schedule);
1628 empty = isl_union_set_is_empty(domain);
1629 if (empty < 0 || empty) {
1630 isl_union_set_free(domain);
1631 return empty < 0 ? isl_schedule_free(schedule) : schedule;
1634 n = isl_multi_aff_dim(prefix, isl_dim_in);
1635 mupa = outer_projection_mupa(domain, n);
1636 ma = isl_multi_aff_copy(prefix);
1637 mupa = isl_multi_union_pw_aff_apply_multi_aff(mupa, ma);
1638 schedule = isl_schedule_insert_partial_schedule(schedule, mupa);
1640 return schedule;
1643 /* Adjust the context and the schedule according to an embedding
1644 * in a loop with iteration domain "dom" and schedule "sched".
1646 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1647 __isl_take isl_multi_aff *sched)
1649 int i;
1651 if (!scop)
1652 goto error;
1654 scop->context = context_embed(scop->context, dom);
1655 if (!scop->context)
1656 goto error;
1658 scop->schedule = schedule_embed(scop->schedule, sched);
1659 if (!scop->schedule)
1660 goto error;
1662 isl_set_free(dom);
1663 isl_multi_aff_free(sched);
1664 return scop;
1665 error:
1666 isl_set_free(dom);
1667 isl_multi_aff_free(sched);
1668 return pet_scop_free(scop);
1671 /* Add extra conditions to scop->skip[type].
1673 * The new skip condition only holds if it held before
1674 * and the condition is true. It does not hold if it did not hold
1675 * before or the condition is false.
1677 * The skip condition is assumed to be an affine expression.
1679 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1680 enum pet_skip type, __isl_keep isl_set *cond)
1682 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1683 isl_pw_aff *skip;
1684 isl_set *dom;
1686 if (!scop)
1687 return NULL;
1688 if (!ext->skip[type])
1689 return scop;
1691 if (!multi_pw_aff_is_affine(ext->skip[type]))
1692 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1693 isl_error_internal, "can only restrict affine skips",
1694 return pet_scop_free(scop));
1696 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1697 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1698 cond = isl_set_copy(cond);
1699 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1700 skip = indicator_function(cond, dom);
1701 isl_multi_pw_aff_free(ext->skip[type]);
1702 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1703 if (!ext->skip[type])
1704 return pet_scop_free(scop);
1706 return scop;
1709 /* Adjust the context and the skip conditions to the fact that
1710 * the scop was created in a context where "cond" holds.
1712 * An outer loop iterator or parameter value is valid for the result
1713 * if it was valid for the original scop and satisfies "cond" or if it does
1714 * not satisfy "cond" as in this case the scop is not executed
1715 * and the original constraints on these values are irrelevant.
1717 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1718 __isl_take isl_set *cond)
1720 int i;
1722 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1723 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1725 if (!scop)
1726 goto error;
1728 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1729 scop->context = isl_set_union(scop->context,
1730 isl_set_complement(isl_set_copy(cond)));
1731 scop->context = isl_set_coalesce(scop->context);
1732 scop->context = pet_nested_remove_from_set(scop->context);
1733 if (!scop->context)
1734 goto error;
1736 isl_set_free(cond);
1737 return scop;
1738 error:
1739 isl_set_free(cond);
1740 return pet_scop_free(scop);
1743 /* Insert an argument expression corresponding to "test" in front
1744 * of the list of arguments described by *n_arg and *args.
1746 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1747 __isl_keep isl_multi_pw_aff *test)
1749 int i;
1750 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1752 if (!test)
1753 return -1;
1755 if (!*args) {
1756 *args = isl_calloc_array(ctx, pet_expr *, 1);
1757 if (!*args)
1758 return -1;
1759 } else {
1760 pet_expr **ext;
1761 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1762 if (!ext)
1763 return -1;
1764 for (i = 0; i < *n_arg; ++i)
1765 ext[1 + i] = (*args)[i];
1766 free(*args);
1767 *args = ext;
1769 (*n_arg)++;
1770 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1771 if (!(*args)[0])
1772 return -1;
1774 return 0;
1777 /* Look through the applications in "scop" for any that can be
1778 * applied to the filter expressed by "map" and "satisified".
1779 * If there is any, then apply it to "map" and return the result.
1780 * Otherwise, return "map".
1781 * "id" is the identifier of the virtual array.
1783 * We only introduce at most one implication for any given virtual array,
1784 * so we can apply the implication and return as soon as we find one.
1786 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1787 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1789 int i;
1791 for (i = 0; i < scop->n_implication; ++i) {
1792 struct pet_implication *pi = scop->implications[i];
1793 isl_id *pi_id;
1795 if (pi->satisfied != satisfied)
1796 continue;
1797 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1798 isl_id_free(pi_id);
1799 if (pi_id != id)
1800 continue;
1802 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1805 return map;
1808 /* Is the filter expressed by "test" and "satisfied" implied
1809 * by filter "pos" on "domain", with filter "expr", taking into
1810 * account the implications of "scop"?
1812 * For filter on domain implying that expressed by "test" and "satisfied",
1813 * the filter needs to be an access to the same (virtual) array as "test" and
1814 * the filter value needs to be equal to "satisfied".
1815 * Moreover, the filter access relation, possibly extended by
1816 * the implications in "scop" needs to contain "test".
1818 static int implies_filter(struct pet_scop *scop,
1819 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1820 __isl_keep isl_map *test, int satisfied)
1822 isl_id *test_id, *arg_id;
1823 isl_val *val;
1824 int is_int;
1825 int s;
1826 int is_subset;
1827 isl_map *implied;
1829 if (expr->type != pet_expr_access)
1830 return 0;
1831 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1832 arg_id = pet_expr_access_get_id(expr);
1833 isl_id_free(arg_id);
1834 isl_id_free(test_id);
1835 if (test_id != arg_id)
1836 return 0;
1837 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1838 is_int = isl_val_is_int(val);
1839 if (is_int)
1840 s = isl_val_get_num_si(val);
1841 isl_val_free(val);
1842 if (!val)
1843 return -1;
1844 if (!is_int)
1845 return 0;
1846 if (s != satisfied)
1847 return 0;
1849 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1850 implied = apply_implications(scop, implied, test_id, satisfied);
1851 is_subset = isl_map_is_subset(test, implied);
1852 isl_map_free(implied);
1854 return is_subset;
1857 /* Is the filter expressed by "test" and "satisfied" implied
1858 * by any of the filters on the domain of "stmt", taking into
1859 * account the implications of "scop"?
1861 static int filter_implied(struct pet_scop *scop,
1862 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1864 int i;
1865 int implied;
1866 isl_id *test_id;
1867 isl_map *domain;
1868 isl_map *test_map;
1870 if (!scop || !stmt || !test)
1871 return -1;
1872 if (scop->n_implication == 0)
1873 return 0;
1874 if (stmt->n_arg == 0)
1875 return 0;
1877 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1878 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1880 implied = 0;
1881 for (i = 0; i < stmt->n_arg; ++i) {
1882 implied = implies_filter(scop, domain, i, stmt->args[i],
1883 test_map, satisfied);
1884 if (implied < 0 || implied)
1885 break;
1888 isl_map_free(test_map);
1889 isl_map_free(domain);
1890 return implied;
1893 /* Make the statement "stmt" depend on the value of "test"
1894 * being equal to "satisfied" by adjusting stmt->domain.
1896 * The domain of "test" corresponds to the (zero or more) outer dimensions
1897 * of the iteration domain.
1899 * We first extend "test" to apply to the entire iteration domain and
1900 * then check if the filter that we are about to add is implied
1901 * by any of the current filters, possibly taking into account
1902 * the implications in "scop". If so, we leave "stmt" untouched and return.
1904 * Otherwise, we insert an argument corresponding to a read to "test"
1905 * from the iteration domain of "stmt" in front of the list of arguments.
1906 * We also insert a corresponding output dimension in the wrapped
1907 * map contained in stmt->domain, with value set to "satisfied".
1909 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1910 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1912 int i;
1913 int implied;
1914 isl_id *id;
1915 isl_ctx *ctx;
1916 isl_pw_multi_aff *pma;
1917 isl_multi_aff *add_dom;
1918 isl_space *space;
1919 isl_local_space *ls;
1920 int n_test_dom;
1922 if (!stmt || !test)
1923 goto error;
1925 space = pet_stmt_get_space(stmt);
1926 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1927 space = isl_space_from_domain(space);
1928 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1929 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1930 ls = isl_local_space_from_space(isl_space_domain(space));
1931 for (i = 0; i < n_test_dom; ++i) {
1932 isl_aff *aff;
1933 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1934 isl_dim_set, i);
1935 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1937 isl_local_space_free(ls);
1938 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1940 implied = filter_implied(scop, stmt, test, satisfied);
1941 if (implied < 0)
1942 goto error;
1943 if (implied) {
1944 isl_multi_pw_aff_free(test);
1945 return stmt;
1948 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1949 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1950 id, satisfied);
1951 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1953 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1954 goto error;
1956 isl_multi_pw_aff_free(test);
1957 return stmt;
1958 error:
1959 isl_multi_pw_aff_free(test);
1960 return pet_stmt_free(stmt);
1963 /* Does "scop" have a skip condition of the given "type"?
1965 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1967 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1969 if (!scop)
1970 return -1;
1971 return ext->skip[type] != NULL;
1974 /* Does "scop" have a skip condition of the given "type" that
1975 * is an affine expression?
1977 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1979 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1981 if (!scop)
1982 return -1;
1983 if (!ext->skip[type])
1984 return 0;
1985 return multi_pw_aff_is_affine(ext->skip[type]);
1988 /* Does "scop" have a skip condition of the given "type" that
1989 * is not an affine expression?
1991 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1993 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1994 int aff;
1996 if (!scop)
1997 return -1;
1998 if (!ext->skip[type])
1999 return 0;
2000 aff = multi_pw_aff_is_affine(ext->skip[type]);
2001 if (aff < 0)
2002 return -1;
2003 return !aff;
2006 /* Does "scop" have a skip condition of the given "type" that
2007 * is affine and holds on the entire domain?
2009 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2011 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2012 isl_pw_aff *pa;
2013 isl_set *set;
2014 int is_aff;
2015 int is_univ;
2017 is_aff = pet_scop_has_affine_skip(scop, type);
2018 if (is_aff < 0 || !is_aff)
2019 return is_aff;
2021 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2022 set = isl_pw_aff_non_zero_set(pa);
2023 is_univ = isl_set_plain_is_universe(set);
2024 isl_set_free(set);
2026 return is_univ;
2029 /* Replace scop->skip[type] by "skip".
2031 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2032 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2034 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2036 if (!scop || !skip)
2037 goto error;
2039 isl_multi_pw_aff_free(ext->skip[type]);
2040 ext->skip[type] = skip;
2042 return scop;
2043 error:
2044 isl_multi_pw_aff_free(skip);
2045 return pet_scop_free(scop);
2048 /* Return a copy of scop->skip[type].
2050 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2051 enum pet_skip type)
2053 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2055 if (!scop)
2056 return NULL;
2058 return isl_multi_pw_aff_copy(ext->skip[type]);
2061 /* Assuming scop->skip[type] is an affine expression,
2062 * return the constraints on the outer loop domain for which the skip condition
2063 * holds.
2065 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2066 enum pet_skip type)
2068 isl_multi_pw_aff *skip;
2069 isl_pw_aff *pa;
2071 skip = pet_scop_get_skip(scop, type);
2072 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2073 isl_multi_pw_aff_free(skip);
2074 return isl_pw_aff_non_zero_set(pa);
2077 /* Return the identifier of the variable that is accessed by
2078 * the skip condition of the given type.
2080 * The skip condition is assumed not to be an affine condition.
2082 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2083 enum pet_skip type)
2085 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2087 if (!scop)
2088 return NULL;
2090 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2093 /* Return an access pet_expr corresponding to the skip condition
2094 * of the given type.
2096 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2097 enum pet_skip type)
2099 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2102 /* Drop the skip condition scop->skip[type].
2104 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2106 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2108 if (!scop)
2109 return;
2111 isl_multi_pw_aff_free(ext->skip[type]);
2112 ext->skip[type] = NULL;
2115 /* Drop all skip conditions on "scop".
2117 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2119 pet_scop_reset_skip(scop, pet_skip_now);
2120 pet_scop_reset_skip(scop, pet_skip_later);
2122 return scop;
2125 /* Make the skip condition (if any) depend on the value of "test" being
2126 * equal to "satisfied".
2128 * We only support the case where the original skip condition is universal,
2129 * i.e., where skipping is unconditional, and where satisfied == 1.
2130 * In this case, the skip condition is changed to skip only when
2131 * "test" is equal to one.
2133 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2134 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2136 int is_univ = 0;
2138 if (!scop)
2139 return NULL;
2140 if (!pet_scop_has_skip(scop, type))
2141 return scop;
2143 if (satisfied)
2144 is_univ = pet_scop_has_universal_skip(scop, type);
2145 if (is_univ < 0)
2146 return pet_scop_free(scop);
2147 if (satisfied && is_univ) {
2148 isl_multi_pw_aff *skip;
2149 skip = isl_multi_pw_aff_copy(test);
2150 scop = pet_scop_set_skip(scop, type, skip);
2151 if (!scop)
2152 return NULL;
2153 } else {
2154 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2155 "skip expression cannot be filtered",
2156 return pet_scop_free(scop));
2159 return scop;
2162 /* Make all statements in "scop" depend on the value of "test"
2163 * being equal to "satisfied" by adjusting their domains.
2165 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2166 __isl_take isl_multi_pw_aff *test, int satisfied)
2168 int i;
2170 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2171 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2173 if (!scop || !test)
2174 goto error;
2176 for (i = 0; i < scop->n_stmt; ++i) {
2177 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2178 isl_multi_pw_aff_copy(test), satisfied);
2179 if (!scop->stmts[i])
2180 goto error;
2183 isl_multi_pw_aff_free(test);
2184 return scop;
2185 error:
2186 isl_multi_pw_aff_free(test);
2187 return pet_scop_free(scop);
2190 /* Add the parameters of the access expression "expr" to "space".
2192 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2194 int i;
2195 isl_space *expr_space;
2196 isl_space **space = user;
2198 expr_space = pet_expr_access_get_parameter_space(expr);
2199 *space = isl_space_align_params(*space, expr_space);
2201 return *space ? 0 : -1;
2204 /* Add all parameters in "stmt" to "space" and return the result.
2206 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2207 __isl_take isl_space *space)
2209 int i;
2211 if (!stmt)
2212 return isl_space_free(space);
2214 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2215 for (i = 0; i < stmt->n_arg; ++i)
2216 if (pet_expr_foreach_access_expr(stmt->args[i],
2217 &access_collect_params, &space) < 0)
2218 space = isl_space_free(space);
2219 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2220 &space) < 0)
2221 space = isl_space_free(space);
2223 return space;
2226 /* Add all parameters in "array" to "space" and return the result.
2228 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2229 __isl_take isl_space *space)
2231 if (!array)
2232 return isl_space_free(space);
2234 space = isl_space_align_params(space,
2235 isl_set_get_space(array->context));
2236 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2238 return space;
2241 /* Add all parameters in "independence" to "space" and return the result.
2243 static __isl_give isl_space *independence_collect_params(
2244 struct pet_independence *independence, __isl_take isl_space *space)
2246 if (!independence)
2247 return isl_space_free(space);
2249 space = isl_space_align_params(space,
2250 isl_union_map_get_space(independence->filter));
2251 space = isl_space_align_params(space,
2252 isl_union_set_get_space(independence->local));
2254 return space;
2257 /* Collect all parameters in "scop" in a parameter space and return the result.
2259 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2261 isl_space *space;
2262 int i;
2264 if (!scop)
2265 return NULL;
2267 space = isl_set_get_space(scop->context);
2269 for (i = 0; i < scop->n_array; ++i)
2270 space = array_collect_params(scop->arrays[i], space);
2272 for (i = 0; i < scop->n_stmt; ++i)
2273 space = stmt_collect_params(scop->stmts[i], space);
2275 for (i = 0; i < scop->n_independence; ++i)
2276 space = independence_collect_params(scop->independences[i],
2277 space);
2279 return space;
2282 /* Add all parameters in "space" to the domain and
2283 * all access relations in "stmt".
2285 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2286 __isl_take isl_space *space)
2288 int i;
2290 if (!stmt)
2291 goto error;
2293 stmt->domain = isl_set_align_params(stmt->domain,
2294 isl_space_copy(space));
2296 for (i = 0; i < stmt->n_arg; ++i) {
2297 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2298 isl_space_copy(space));
2299 if (!stmt->args[i])
2300 goto error;
2302 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2304 if (!stmt->domain || !stmt->body)
2305 goto error;
2307 isl_space_free(space);
2308 return stmt;
2309 error:
2310 isl_space_free(space);
2311 return pet_stmt_free(stmt);
2314 /* Add all parameters in "space" to "array".
2316 static struct pet_array *array_propagate_params(struct pet_array *array,
2317 __isl_take isl_space *space)
2319 if (!array)
2320 goto error;
2322 array->context = isl_set_align_params(array->context,
2323 isl_space_copy(space));
2324 array->extent = isl_set_align_params(array->extent,
2325 isl_space_copy(space));
2326 if (array->value_bounds) {
2327 array->value_bounds = isl_set_align_params(array->value_bounds,
2328 isl_space_copy(space));
2329 if (!array->value_bounds)
2330 goto error;
2333 if (!array->context || !array->extent)
2334 goto error;
2336 isl_space_free(space);
2337 return array;
2338 error:
2339 isl_space_free(space);
2340 return pet_array_free(array);
2343 /* Add all parameters in "space" to "independence".
2345 static struct pet_independence *independence_propagate_params(
2346 struct pet_independence *independence, __isl_take isl_space *space)
2348 if (!independence)
2349 goto error;
2351 independence->filter = isl_union_map_align_params(independence->filter,
2352 isl_space_copy(space));
2353 independence->local = isl_union_set_align_params(independence->local,
2354 isl_space_copy(space));
2355 if (!independence->filter || !independence->local)
2356 goto error;
2358 isl_space_free(space);
2359 return independence;
2360 error:
2361 isl_space_free(space);
2362 return pet_independence_free(independence);
2365 /* Add all parameters in "space" to "scop".
2367 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2368 __isl_take isl_space *space)
2370 int i;
2372 if (!scop)
2373 goto error;
2375 scop->context = isl_set_align_params(scop->context,
2376 isl_space_copy(space));
2377 scop->schedule = isl_schedule_align_params(scop->schedule,
2378 isl_space_copy(space));
2379 if (!scop->context || !scop->schedule)
2380 goto error;
2382 for (i = 0; i < scop->n_array; ++i) {
2383 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2384 isl_space_copy(space));
2385 if (!scop->arrays[i])
2386 goto error;
2389 for (i = 0; i < scop->n_stmt; ++i) {
2390 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2391 isl_space_copy(space));
2392 if (!scop->stmts[i])
2393 goto error;
2396 for (i = 0; i < scop->n_independence; ++i) {
2397 scop->independences[i] = independence_propagate_params(
2398 scop->independences[i], isl_space_copy(space));
2399 if (!scop->independences[i])
2400 goto error;
2403 isl_space_free(space);
2404 return scop;
2405 error:
2406 isl_space_free(space);
2407 return pet_scop_free(scop);
2410 /* Update all isl_sets and isl_maps in "scop" such that they all
2411 * have the same parameters.
2413 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2415 isl_space *space;
2417 if (!scop)
2418 return NULL;
2420 space = scop_collect_params(scop);
2422 scop = scop_propagate_params(scop, space);
2424 return scop;
2427 /* Add the access relation of the give "type" of the access expression "expr"
2428 * to "accesses" and return the result.
2429 * The domain of the access relation is intersected with "domain".
2430 * If "tag" is set, then the access relation is tagged with
2431 * the corresponding reference identifier.
2433 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2434 enum pet_expr_access_type type, int tag,
2435 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2437 isl_union_map *access;
2439 access = pet_expr_access_get_access(expr, type);
2440 access = isl_union_map_intersect_domain(access,
2441 isl_union_set_copy(domain));
2442 if (tag)
2443 access = pet_expr_tag_access(expr, access);
2444 return isl_union_map_union(accesses, access);
2447 /* Internal data structure for expr_collect_accesses.
2449 * "type" is the type of accesses we want to collect.
2450 * "tag" is set if the access relations should be tagged with
2451 * the corresponding reference identifiers.
2452 * "domain" are constraints on the domain of the access relations.
2453 * "accesses" collects the results.
2455 struct pet_expr_collect_accesses_data {
2456 enum pet_expr_access_type type;
2457 int tag;
2458 isl_union_set *domain;
2460 isl_union_map *accesses;
2463 /* Add the access relation of the access expression "expr"
2464 * to data->accesses if the access expression is a read and we are collecting
2465 * reads and/or it is a write and we are collecting writes.
2466 * The domains of the access relations are intersected with data->domain.
2467 * If data->tag is set, then the access relations are tagged with
2468 * the corresponding reference identifiers.
2470 * If data->type is pet_expr_access_must_write, then we only add
2471 * the accesses that are definitely performed. Otherwise, we add
2472 * all potential accesses.
2473 * In particular, if the access has any arguments, then in case of
2474 * pet_expr_access_must_write we currently skip the access completely.
2475 * In other cases, we project out the values of the access arguments.
2477 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2479 struct pet_expr_collect_accesses_data *data = user;
2480 int i;
2481 isl_id *id;
2482 isl_space *dim;
2484 if (!expr)
2485 return -1;
2487 if (pet_expr_is_affine(expr))
2488 return 0;
2489 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2490 return 0;
2492 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2493 ((data->type == pet_expr_access_may_write ||
2494 data->type == pet_expr_access_must_write) && expr->acc.write))
2495 data->accesses = expr_collect_access(expr,
2496 data->type, data->tag,
2497 data->accesses, data->domain);
2499 return data->accesses ? 0 : -1;
2502 /* Collect and return all access relations of the given "type" in "stmt".
2503 * If "tag" is set, then the access relations are tagged with
2504 * the corresponding reference identifiers.
2505 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2506 * we simply add the argument of the kill operation.
2508 * If we are looking for definite accesses (pet_expr_access_must_write
2509 * or pet_expr_access_killed), then we only add the accesses that are
2510 * definitely performed. Otherwise, we add all potential accesses.
2511 * In particular, if the statement has any arguments, then if we are looking
2512 * for definite accesses we currently skip the statement completely. Othewise,
2513 * we project out the values of the statement arguments.
2514 * If the statement body is not an expression tree, then we cannot
2515 * know for sure if/when the accesses inside the tree are performed.
2516 * We therefore ignore such statements when we are looking for
2517 * definite accesses.
2519 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2520 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2522 struct pet_expr_collect_accesses_data data = { type, tag };
2523 int must;
2524 isl_set *domain;
2526 if (!stmt)
2527 return NULL;
2529 data.accesses = isl_union_map_empty(dim);
2531 if (type == pet_expr_access_must_write ||
2532 type == pet_expr_access_killed)
2533 must = 1;
2534 else
2535 must = 0;
2537 if (must && stmt->n_arg > 0)
2538 return data.accesses;
2539 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2540 return data.accesses;
2542 domain = drop_arguments(isl_set_copy(stmt->domain));
2543 data.domain = isl_union_set_from_set(domain);
2545 if (type == pet_expr_access_killed) {
2546 pet_expr *body, *arg;
2548 body = pet_tree_expr_get_expr(stmt->body);
2549 arg = pet_expr_get_arg(body, 0);
2550 data.accesses = expr_collect_access(arg,
2551 pet_expr_access_killed, tag,
2552 data.accesses, data.domain);
2553 pet_expr_free(arg);
2554 pet_expr_free(body);
2555 } else if (pet_tree_foreach_access_expr(stmt->body,
2556 &expr_collect_accesses, &data) < 0)
2557 data.accesses = isl_union_map_free(data.accesses);
2559 isl_union_set_free(data.domain);
2561 return data.accesses;
2564 /* Is "stmt" an assignment statement?
2566 int pet_stmt_is_assign(struct pet_stmt *stmt)
2568 if (!stmt)
2569 return 0;
2570 return pet_tree_is_assign(stmt->body);
2573 /* Is "stmt" a kill statement?
2575 int pet_stmt_is_kill(struct pet_stmt *stmt)
2577 if (!stmt)
2578 return 0;
2579 return pet_tree_is_kill(stmt->body);
2582 /* Is "stmt" an assume statement?
2584 int pet_stmt_is_assume(struct pet_stmt *stmt)
2586 if (!stmt)
2587 return 0;
2588 return pet_tree_is_assume(stmt->body);
2591 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2593 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2594 __isl_keep isl_map *map, __isl_keep isl_set *set)
2596 isl_map *gist;
2598 gist = isl_map_copy(map);
2599 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2600 return isl_union_map_add_map(umap, gist);
2603 /* Compute a mapping from all arrays (of structs) in scop
2604 * to their members.
2606 * If "from_outermost" is set, then the domain only consists
2607 * of outermost arrays.
2608 * If "to_innermost" is set, then the range only consists
2609 * of innermost arrays.
2611 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2612 int from_outermost, int to_innermost)
2614 int i;
2615 isl_union_map *to_inner;
2617 if (!scop)
2618 return NULL;
2620 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2622 for (i = 0; i < scop->n_array; ++i) {
2623 struct pet_array *array = scop->arrays[i];
2624 isl_set *set;
2625 isl_map *map;
2627 if (to_innermost && array->element_is_record)
2628 continue;
2630 set = isl_set_copy(array->extent);
2631 map = isl_set_identity(isl_set_copy(set));
2633 while (set && isl_set_is_wrapping(set)) {
2634 isl_id *id;
2635 isl_map *wrapped;
2637 if (!from_outermost)
2638 to_inner = add_gisted(to_inner, map, set);
2640 id = isl_set_get_tuple_id(set);
2641 wrapped = isl_set_unwrap(set);
2642 wrapped = isl_map_domain_map(wrapped);
2643 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2644 map = isl_map_apply_domain(map, wrapped);
2645 set = isl_map_domain(isl_map_copy(map));
2648 map = isl_map_gist_domain(map, set);
2649 to_inner = isl_union_map_add_map(to_inner, map);
2652 return to_inner;
2655 /* Compute a mapping from all arrays (of structs) in scop
2656 * to their innermost arrays.
2658 * In particular, for each array of a primitive type, the result
2659 * contains the identity mapping on that array.
2660 * For each array involving member accesses, the result
2661 * contains a mapping from the elements of any intermediate array of structs
2662 * to all corresponding elements of the innermost nested arrays.
2664 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2665 struct pet_scop *scop)
2667 return compute_to_inner(scop, 0, 1);
2670 /* Compute a mapping from all outermost arrays (of structs) in scop
2671 * to their innermost members.
2673 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2675 return compute_to_inner(scop, 1, 1);
2678 /* Compute a mapping from all outermost arrays (of structs) in scop
2679 * to their members, including the outermost arrays themselves.
2681 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2683 return compute_to_inner(scop, 1, 0);
2686 /* Collect and return all access relations of the given "type" in "scop".
2687 * If "type" is pet_expr_access_killed, then we only add the arguments of
2688 * kill operations.
2689 * If we are looking for definite accesses (pet_expr_access_must_write
2690 * or pet_expr_access_killed), then we only add the accesses that are
2691 * definitely performed. Otherwise, we add all potential accesses.
2692 * If "tag" is set, then the access relations are tagged with
2693 * the corresponding reference identifiers.
2694 * For accesses to structures, the returned access relation accesses
2695 * all individual fields in the structures.
2697 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2698 enum pet_expr_access_type type, int tag)
2700 int i;
2701 isl_union_map *accesses;
2702 isl_union_set *arrays;
2703 isl_union_map *to_inner;
2705 if (!scop)
2706 return NULL;
2708 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2710 for (i = 0; i < scop->n_stmt; ++i) {
2711 struct pet_stmt *stmt = scop->stmts[i];
2712 isl_union_map *accesses_i;
2713 isl_space *space;
2715 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2716 continue;
2718 space = isl_set_get_space(scop->context);
2719 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2720 accesses = isl_union_map_union(accesses, accesses_i);
2723 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2724 for (i = 0; i < scop->n_array; ++i) {
2725 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2726 arrays = isl_union_set_add_set(arrays, extent);
2728 accesses = isl_union_map_intersect_range(accesses, arrays);
2730 to_inner = pet_scop_compute_any_to_inner(scop);
2731 accesses = isl_union_map_apply_range(accesses, to_inner);
2733 return accesses;
2736 /* Return the potential read access relation.
2738 __isl_give isl_union_map *pet_scop_get_may_reads(struct pet_scop *scop)
2740 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2743 /* Return the potential write access relation.
2745 __isl_give isl_union_map *pet_scop_get_may_writes(struct pet_scop *scop)
2747 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2750 /* Return the definite write access relation.
2752 __isl_give isl_union_map *pet_scop_get_must_writes(struct pet_scop *scop)
2754 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2757 /* Return the definite kill access relation.
2759 __isl_give isl_union_map *pet_scop_get_must_kills(struct pet_scop *scop)
2761 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2764 /* Return the tagged potential read access relation.
2766 __isl_give isl_union_map *pet_scop_get_tagged_may_reads(
2767 struct pet_scop *scop)
2769 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2772 /* Return the tagged potential write access relation.
2774 __isl_give isl_union_map *pet_scop_get_tagged_may_writes(
2775 struct pet_scop *scop)
2777 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2780 /* Return the tagged definite write access relation.
2782 __isl_give isl_union_map *pet_scop_get_tagged_must_writes(
2783 struct pet_scop *scop)
2785 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2788 /* Return the tagged definite kill access relation.
2790 __isl_give isl_union_map *pet_scop_get_tagged_must_kills(
2791 struct pet_scop *scop)
2793 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2796 /* Collect and return the set of all statement instances in "scop".
2798 __isl_give isl_union_set *pet_scop_get_instance_set(struct pet_scop *scop)
2800 int i;
2801 isl_set *domain_i;
2802 isl_union_set *domain;
2804 if (!scop)
2805 return NULL;
2807 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2809 for (i = 0; i < scop->n_stmt; ++i) {
2810 domain_i = isl_set_copy(scop->stmts[i]->domain);
2811 if (scop->stmts[i]->n_arg > 0)
2812 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2813 domain = isl_union_set_add_set(domain, domain_i);
2816 return domain;
2819 /* Return the context of "scop".
2821 __isl_give isl_set *pet_scop_get_context(__isl_keep pet_scop *scop)
2823 if (!scop)
2824 return NULL;
2826 return isl_set_copy(scop->context);
2829 /* Return the schedule of "scop".
2831 __isl_give isl_schedule *pet_scop_get_schedule(__isl_keep pet_scop *scop)
2833 if (!scop)
2834 return NULL;
2836 return isl_schedule_copy(scop->schedule);
2839 /* Add a reference identifier to all access expressions in "stmt".
2840 * "n_ref" points to an integer that contains the sequence number
2841 * of the next reference.
2843 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2845 int i;
2847 if (!stmt)
2848 return NULL;
2850 for (i = 0; i < stmt->n_arg; ++i) {
2851 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2852 if (!stmt->args[i])
2853 return pet_stmt_free(stmt);
2856 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2857 if (!stmt->body)
2858 return pet_stmt_free(stmt);
2860 return stmt;
2863 /* Add a reference identifier to all access expressions in "scop".
2865 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2867 int i;
2868 int n_ref;
2870 if (!scop)
2871 return NULL;
2873 n_ref = 0;
2874 for (i = 0; i < scop->n_stmt; ++i) {
2875 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2876 if (!scop->stmts[i])
2877 return pet_scop_free(scop);
2880 return scop;
2883 /* Reset the user pointer on all parameter ids in "array".
2885 static struct pet_array *array_anonymize(struct pet_array *array)
2887 if (!array)
2888 return NULL;
2890 array->context = isl_set_reset_user(array->context);
2891 array->extent = isl_set_reset_user(array->extent);
2892 if (!array->context || !array->extent)
2893 return pet_array_free(array);
2895 return array;
2898 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2900 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2902 int i;
2903 isl_space *space;
2904 isl_set *domain;
2906 if (!stmt)
2907 return NULL;
2909 stmt->domain = isl_set_reset_user(stmt->domain);
2910 if (!stmt->domain)
2911 return pet_stmt_free(stmt);
2913 for (i = 0; i < stmt->n_arg; ++i) {
2914 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2915 if (!stmt->args[i])
2916 return pet_stmt_free(stmt);
2919 stmt->body = pet_tree_anonymize(stmt->body);
2920 if (!stmt->body)
2921 return pet_stmt_free(stmt);
2923 return stmt;
2926 /* Reset the user pointer on the tuple ids and all parameter ids
2927 * in "implication".
2929 static struct pet_implication *implication_anonymize(
2930 struct pet_implication *implication)
2932 if (!implication)
2933 return NULL;
2935 implication->extension = isl_map_reset_user(implication->extension);
2936 if (!implication->extension)
2937 return pet_implication_free(implication);
2939 return implication;
2942 /* Reset the user pointer on the tuple ids and all parameter ids
2943 * in "independence".
2945 static struct pet_independence *independence_anonymize(
2946 struct pet_independence *independence)
2948 if (!independence)
2949 return NULL;
2951 independence->filter = isl_union_map_reset_user(independence->filter);
2952 independence->local = isl_union_set_reset_user(independence->local);
2953 if (!independence->filter || !independence->local)
2954 return pet_independence_free(independence);
2956 return independence;
2959 /* Reset the user pointer on all parameter and tuple ids in "scop".
2961 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2963 int i;
2965 if (!scop)
2966 return NULL;
2968 scop->context = isl_set_reset_user(scop->context);
2969 scop->context_value = isl_set_reset_user(scop->context_value);
2970 scop->schedule = isl_schedule_reset_user(scop->schedule);
2971 if (!scop->context || !scop->context_value || !scop->schedule)
2972 return pet_scop_free(scop);
2974 for (i = 0; i < scop->n_array; ++i) {
2975 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2976 if (!scop->arrays[i])
2977 return pet_scop_free(scop);
2980 for (i = 0; i < scop->n_stmt; ++i) {
2981 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2982 if (!scop->stmts[i])
2983 return pet_scop_free(scop);
2986 for (i = 0; i < scop->n_implication; ++i) {
2987 scop->implications[i] =
2988 implication_anonymize(scop->implications[i]);
2989 if (!scop->implications[i])
2990 return pet_scop_free(scop);
2993 for (i = 0; i < scop->n_independence; ++i) {
2994 scop->independences[i] =
2995 independence_anonymize(scop->independences[i]);
2996 if (!scop->independences[i])
2997 return pet_scop_free(scop);
3000 return scop;
3003 /* Compute the gist of the iteration domain and all access relations
3004 * of "stmt" based on the constraints on the parameters specified by "context"
3005 * and the constraints on the values of nested accesses specified
3006 * by "value_bounds".
3008 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3009 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3011 int i;
3012 isl_set *domain;
3014 if (!stmt)
3015 return NULL;
3017 domain = isl_set_copy(stmt->domain);
3018 if (stmt->n_arg > 0)
3019 domain = isl_map_domain(isl_set_unwrap(domain));
3021 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3023 for (i = 0; i < stmt->n_arg; ++i) {
3024 stmt->args[i] = pet_expr_gist(stmt->args[i],
3025 domain, value_bounds);
3026 if (!stmt->args[i])
3027 goto error;
3030 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
3031 if (!stmt->body)
3032 goto error;
3034 isl_set_free(domain);
3036 domain = isl_set_universe(pet_stmt_get_space(stmt));
3037 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3038 if (stmt->n_arg > 0)
3039 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3040 value_bounds);
3041 stmt->domain = isl_set_gist(stmt->domain, domain);
3042 if (!stmt->domain)
3043 return pet_stmt_free(stmt);
3045 return stmt;
3046 error:
3047 isl_set_free(domain);
3048 return pet_stmt_free(stmt);
3051 /* Compute the gist of the extent of the array
3052 * based on the constraints on the parameters specified by "context".
3054 static struct pet_array *array_gist(struct pet_array *array,
3055 __isl_keep isl_set *context)
3057 if (!array)
3058 return NULL;
3060 array->extent = isl_set_gist_params(array->extent,
3061 isl_set_copy(context));
3062 if (!array->extent)
3063 return pet_array_free(array);
3065 return array;
3068 /* Compute the gist of all sets and relations in "scop"
3069 * based on the constraints on the parameters specified by "scop->context"
3070 * and the constraints on the values of nested accesses specified
3071 * by "value_bounds".
3073 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3074 __isl_keep isl_union_map *value_bounds)
3076 int i;
3078 if (!scop)
3079 return NULL;
3081 scop->context = isl_set_coalesce(scop->context);
3082 if (!scop->context)
3083 return pet_scop_free(scop);
3085 scop->schedule = isl_schedule_gist_domain_params(scop->schedule,
3086 isl_set_copy(scop->context));
3087 if (!scop->schedule)
3088 return pet_scop_free(scop);
3090 for (i = 0; i < scop->n_array; ++i) {
3091 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3092 if (!scop->arrays[i])
3093 return pet_scop_free(scop);
3096 for (i = 0; i < scop->n_stmt; ++i) {
3097 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3098 value_bounds);
3099 if (!scop->stmts[i])
3100 return pet_scop_free(scop);
3103 return scop;
3106 /* Intersect the context of "scop" with "context".
3107 * To ensure that we don't introduce any unnamed parameters in
3108 * the context of "scop", we first remove the unnamed parameters
3109 * from "context".
3111 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3112 __isl_take isl_set *context)
3114 if (!scop)
3115 goto error;
3117 context = pet_nested_remove_from_set(context);
3118 scop->context = isl_set_intersect(scop->context, context);
3119 if (!scop->context)
3120 return pet_scop_free(scop);
3122 return scop;
3123 error:
3124 isl_set_free(context);
3125 return pet_scop_free(scop);
3128 /* Drop the current context of "scop". That is, replace the context
3129 * by a universal set.
3131 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3133 isl_space *space;
3135 if (!scop)
3136 return NULL;
3138 space = isl_set_get_space(scop->context);
3139 isl_set_free(scop->context);
3140 scop->context = isl_set_universe(space);
3141 if (!scop->context)
3142 return pet_scop_free(scop);
3144 return scop;
3147 /* Append "array" to the arrays of "scop".
3149 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3150 struct pet_array *array)
3152 isl_ctx *ctx;
3153 struct pet_array **arrays;
3155 if (!array || !scop)
3156 goto error;
3158 ctx = isl_set_get_ctx(scop->context);
3159 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3160 scop->n_array + 1);
3161 if (!arrays)
3162 goto error;
3163 scop->arrays = arrays;
3164 scop->arrays[scop->n_array] = array;
3165 scop->n_array++;
3166 scop->context = isl_set_intersect_params(scop->context,
3167 isl_set_copy(array->context));
3168 if (!scop->context)
3169 return pet_scop_free(scop);
3171 return scop;
3172 error:
3173 pet_array_free(array);
3174 return pet_scop_free(scop);
3177 /* Create an index expression for an access to a virtual array
3178 * representing the result of a condition.
3179 * Unlike other accessed data, the id of the array is NULL as
3180 * there is no ValueDecl in the program corresponding to the virtual
3181 * array.
3182 * The index expression is created as an identity mapping on "space".
3183 * That is, the dimension of the array is the same as that of "space".
3185 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3186 int test_nr)
3188 isl_id *id;
3189 char name[50];
3191 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3192 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3193 space = isl_space_map_from_set(space);
3194 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3195 return isl_multi_pw_aff_identity(space);
3198 /* Add an array with the given extent to the list
3199 * of arrays in "scop" and return the extended pet_scop.
3200 * Specifically, the extent is determined by the image of "domain"
3201 * under "index".
3202 * "int_size" is the number of bytes needed to represent values of type "int".
3203 * The array is marked as attaining values 0 and 1 only and
3204 * as each element being assigned at most once.
3206 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3207 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3208 int int_size)
3210 isl_ctx *ctx;
3211 isl_space *space;
3212 struct pet_array *array;
3213 isl_map *access;
3215 if (!scop || !domain || !index)
3216 goto error;
3218 ctx = isl_multi_pw_aff_get_ctx(index);
3219 array = isl_calloc_type(ctx, struct pet_array);
3220 if (!array)
3221 goto error;
3223 access = isl_map_from_multi_pw_aff(index);
3224 access = isl_map_intersect_domain(access, domain);
3225 array->extent = isl_map_range(access);
3226 space = isl_space_params_alloc(ctx, 0);
3227 array->context = isl_set_universe(space);
3228 space = isl_space_set_alloc(ctx, 0, 1);
3229 array->value_bounds = isl_set_universe(space);
3230 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3231 isl_dim_set, 0, 0);
3232 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3233 isl_dim_set, 0, 1);
3234 array->element_type = strdup("int");
3235 array->element_size = int_size;
3236 array->uniquely_defined = 1;
3238 if (!array->extent || !array->context)
3239 array = pet_array_free(array);
3241 scop = pet_scop_add_array(scop, array);
3243 return scop;
3244 error:
3245 isl_set_free(domain);
3246 isl_multi_pw_aff_free(index);
3247 return pet_scop_free(scop);
3250 /* Create and return an implication on filter values equal to "satisfied"
3251 * with extension "map".
3253 static struct pet_implication *new_implication(__isl_take isl_map *map,
3254 int satisfied)
3256 isl_ctx *ctx;
3257 struct pet_implication *implication;
3259 if (!map)
3260 return NULL;
3261 ctx = isl_map_get_ctx(map);
3262 implication = isl_alloc_type(ctx, struct pet_implication);
3263 if (!implication)
3264 goto error;
3266 implication->extension = map;
3267 implication->satisfied = satisfied;
3269 return implication;
3270 error:
3271 isl_map_free(map);
3272 return NULL;
3275 /* Add an implication on filter values equal to "satisfied"
3276 * with extension "map" to "scop".
3278 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3279 __isl_take isl_map *map, int satisfied)
3281 isl_ctx *ctx;
3282 struct pet_implication *implication;
3283 struct pet_implication **implications;
3285 implication = new_implication(map, satisfied);
3286 if (!scop || !implication)
3287 goto error;
3289 ctx = isl_set_get_ctx(scop->context);
3290 implications = isl_realloc_array(ctx, scop->implications,
3291 struct pet_implication *,
3292 scop->n_implication + 1);
3293 if (!implications)
3294 goto error;
3295 scop->implications = implications;
3296 scop->implications[scop->n_implication] = implication;
3297 scop->n_implication++;
3299 return scop;
3300 error:
3301 pet_implication_free(implication);
3302 return pet_scop_free(scop);
3305 /* Create and return a function that maps the iteration domains
3306 * of the statements in "scop" onto their outer "n" dimensions.
3307 * "space" is the parameters space of the created function.
3309 static __isl_give isl_union_pw_multi_aff *outer_projection(
3310 struct pet_scop *scop, __isl_take isl_space *space, int n)
3312 int i;
3313 isl_union_pw_multi_aff *res;
3315 res = isl_union_pw_multi_aff_empty(space);
3317 if (!scop)
3318 return isl_union_pw_multi_aff_free(res);
3320 for (i = 0; i < scop->n_stmt; ++i) {
3321 struct pet_stmt *stmt = scop->stmts[i];
3322 isl_space *space;
3323 isl_multi_aff *ma;
3324 isl_pw_multi_aff *pma;
3326 space = pet_stmt_get_space(stmt);
3327 ma = pet_prefix_projection(space, n);
3328 pma = isl_pw_multi_aff_from_multi_aff(ma);
3329 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3332 return res;
3335 /* Add an independence to "scop" for the inner iterator of "domain"
3336 * with local variables "local", where "domain" represents the outer
3337 * loop iterators of all statements in "scop".
3338 * If "sign" is positive, then the inner iterator increases.
3339 * Otherwise it decreases.
3341 * The independence is supposed to filter out any dependence of
3342 * an iteration of domain on a previous iteration along the inner dimension.
3343 * We therefore create a mapping from an iteration to later iterations and
3344 * then plug in the projection of the iterations domains of "scop"
3345 * onto the outer loop iterators.
3347 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3348 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3350 int i, dim;
3351 isl_space *space;
3352 isl_map *map;
3353 isl_union_map *independence;
3354 isl_union_pw_multi_aff *proj;
3356 if (!scop || !domain || !local)
3357 goto error;
3359 dim = isl_set_dim(domain, isl_dim_set);
3360 space = isl_space_map_from_set(isl_set_get_space(domain));
3361 map = isl_map_universe(space);
3362 for (i = 0; i + 1 < dim; ++i)
3363 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3364 if (sign > 0)
3365 map = isl_map_order_lt(map,
3366 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3367 else
3368 map = isl_map_order_gt(map,
3369 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3371 independence = isl_union_map_from_map(map);
3372 space = isl_space_params(isl_set_get_space(domain));
3373 proj = outer_projection(scop, space, dim);
3374 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3375 independence, isl_union_pw_multi_aff_copy(proj));
3376 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3377 independence, proj);
3379 scop = pet_scop_add_independence(scop, independence, local);
3381 return scop;
3382 error:
3383 isl_union_set_free(local);
3384 return pet_scop_free(scop);
3387 /* Given an access expression, check if it is data dependent.
3388 * If so, set *found and abort the search.
3390 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3392 int *found = user;
3394 if (pet_expr_get_n_arg(expr) > 0) {
3395 *found = 1;
3396 return -1;
3399 return 0;
3402 /* Does "scop" contain any data dependent accesses?
3404 * Check the body of each statement for such accesses.
3406 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3408 int i;
3409 int found = 0;
3411 if (!scop)
3412 return -1;
3414 for (i = 0; i < scop->n_stmt; ++i) {
3415 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3416 &is_data_dependent, &found);
3417 if (r < 0 && !found)
3418 return -1;
3419 if (found)
3420 return found;
3423 return found;
3426 /* Does "scop" contain and data dependent conditions?
3428 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3430 int i;
3432 if (!scop)
3433 return -1;
3435 for (i = 0; i < scop->n_stmt; ++i)
3436 if (scop->stmts[i]->n_arg > 0)
3437 return 1;
3439 return 0;
3442 /* Keep track of the "input" file inside the (extended) "scop".
3444 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3446 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3448 if (!scop)
3449 return NULL;
3451 ext->input = input;
3453 return scop;
3456 /* Print the original code corresponding to "scop" to printer "p".
3458 * pet_scop_print_original can only be called from
3459 * a pet_transform_C_source callback. This means that the input
3460 * file is stored in the extended scop and that the printer prints
3461 * to a file.
3463 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3464 __isl_take isl_printer *p)
3466 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3467 FILE *output;
3468 unsigned start, end;
3470 if (!scop || !p)
3471 return isl_printer_free(p);
3473 if (!ext->input)
3474 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3475 "no input file stored in scop",
3476 return isl_printer_free(p));
3478 output = isl_printer_get_file(p);
3479 if (!output)
3480 return isl_printer_free(p);
3482 start = pet_loc_get_start(scop->loc);
3483 end = pet_loc_get_end(scop->loc);
3484 if (copy(ext->input, output, start, end) < 0)
3485 return isl_printer_free(p);
3487 return p;