update isl for introduction of isl_stat
[pet.git] / scop.c
blobc07daa647bb72bb2e33f001baed92df36333abea
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 int pet_stmt_is_affine_assume(struct pet_stmt *stmt)
420 if (!stmt)
421 return 0;
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 pet_expr *body;
452 if (pet_stmt_is_affine_assume(stmt)) {
453 isl_multi_pw_aff *index;
454 isl_pw_aff *pa;
455 isl_set *cond;
457 index = pet_stmt_assume_get_index(stmt);
458 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
459 isl_multi_pw_aff_free(index);
460 cond = isl_pw_aff_non_zero_set(pa);
461 cond = isl_set_reset_tuple_id(cond);
462 return isl_set_intersect(context, cond);
465 for (i = 0; i < stmt->n_arg; ++i)
466 context = expr_extract_context(stmt->args[i], context);
468 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
469 return context;
471 body = pet_tree_expr_get_expr(stmt->body);
472 context = expr_extract_context(body, context);
473 pet_expr_free(body);
475 return context;
478 /* Construct a pet_scop in the given space that contains the given pet_stmt.
479 * The initial schedule consists of only the iteration domain.
481 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
482 struct pet_stmt *stmt)
484 struct pet_scop *scop;
485 isl_set *set;
486 isl_union_set *domain;
487 isl_schedule *schedule;
489 if (!stmt)
490 space = isl_space_free(space);
492 set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
493 domain = isl_union_set_from_set(set);
494 schedule = isl_schedule_from_domain(domain);
496 scop = scop_alloc(space, 1, schedule);
497 if (!scop)
498 goto error;
500 scop->context = stmt_extract_context(stmt, scop->context);
501 if (!scop->context)
502 goto error;
504 scop->stmts[0] = stmt;
505 scop->loc = pet_loc_copy(stmt->loc);
507 if (!scop->loc)
508 return pet_scop_free(scop);
510 return scop;
511 error:
512 pet_stmt_free(stmt);
513 pet_scop_free(scop);
514 return NULL;
517 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
518 * does it represent an affine expression?
520 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
522 int has_id;
524 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
525 if (has_id < 0)
526 return -1;
528 return !has_id;
531 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
533 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
534 __isl_take isl_set *dom)
536 isl_pw_aff *pa;
537 pa = isl_set_indicator_function(set);
538 pa = isl_pw_aff_intersect_domain(pa, dom);
539 return pa;
542 /* Return "lhs || rhs", defined on the shared definition domain.
544 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
545 __isl_take isl_pw_aff *rhs)
547 isl_set *cond;
548 isl_set *dom;
550 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
551 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
552 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
553 isl_pw_aff_non_zero_set(rhs));
554 cond = isl_set_coalesce(cond);
555 return indicator_function(cond, dom);
558 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
559 * ext may be equal to either ext1 or ext2.
561 * The two skips that need to be combined are assumed to be affine expressions.
563 * We need to skip in ext if we need to skip in either ext1 or ext2.
564 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
566 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
567 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
568 enum pet_skip type)
570 isl_pw_aff *skip, *skip1, *skip2;
572 if (!ext)
573 return NULL;
574 if (!ext1->skip[type] && !ext2->skip[type])
575 return ext;
576 if (!ext1->skip[type]) {
577 if (ext == ext2)
578 return ext;
579 ext->skip[type] = ext2->skip[type];
580 ext2->skip[type] = NULL;
581 return ext;
583 if (!ext2->skip[type]) {
584 if (ext == ext1)
585 return ext;
586 ext->skip[type] = ext1->skip[type];
587 ext1->skip[type] = NULL;
588 return ext;
591 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
592 !multi_pw_aff_is_affine(ext2->skip[type]))
593 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
594 isl_error_internal, "can only combine affine skips",
595 goto error);
597 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
598 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
599 skip = pw_aff_or(skip1, skip2);
600 isl_multi_pw_aff_free(ext1->skip[type]);
601 ext1->skip[type] = NULL;
602 isl_multi_pw_aff_free(ext2->skip[type]);
603 ext2->skip[type] = NULL;
604 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
605 if (!ext->skip[type])
606 goto error;
608 return ext;
609 error:
610 pet_scop_free(&ext->scop);
611 return NULL;
614 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
615 * where type takes on the values pet_skip_now and pet_skip_later.
616 * scop may be equal to either scop1 or scop2.
618 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
619 struct pet_scop *scop1, struct pet_scop *scop2)
621 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
622 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
623 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
625 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
626 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
627 return &ext->scop;
630 /* Update start and end of scop->loc to include the region from "start"
631 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
632 * does not have any offset information yet and we simply take the information
633 * from "start" and "end". Otherwise, we update loc using "start" and "end".
635 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
636 unsigned start, unsigned end)
638 if (!scop)
639 return NULL;
641 if (scop->loc == &pet_loc_dummy)
642 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
643 start, end, -1, strdup(""));
644 else
645 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
647 if (!scop->loc)
648 return pet_scop_free(scop);
650 return scop;
653 /* Update start and end of scop->loc to include the region identified
654 * by "loc".
656 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
657 __isl_keep pet_loc *loc)
659 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
660 pet_loc_get_end(loc));
663 /* Replace the location of "scop" by "loc".
665 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
666 __isl_take pet_loc *loc)
668 if (!scop || !loc)
669 goto error;
671 pet_loc_free(scop->loc);
672 scop->loc = loc;
674 return scop;
675 error:
676 pet_loc_free(loc);
677 pet_scop_free(scop);
678 return NULL;
681 /* Does "implication" appear in the list of implications of "scop"?
683 static int is_known_implication(struct pet_scop *scop,
684 struct pet_implication *implication)
686 int i;
688 for (i = 0; i < scop->n_implication; ++i) {
689 struct pet_implication *pi = scop->implications[i];
690 int equal;
692 if (pi->satisfied != implication->satisfied)
693 continue;
694 equal = isl_map_is_equal(pi->extension, implication->extension);
695 if (equal < 0)
696 return -1;
697 if (equal)
698 return 1;
701 return 0;
704 /* Store the concatenation of the implications of "scop1" and "scop2"
705 * in "scop", removing duplicates (i.e., implications in "scop2" that
706 * already appear in "scop1").
708 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
709 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
711 int i, j;
713 if (!scop)
714 return NULL;
716 if (scop2->n_implication == 0) {
717 scop->n_implication = scop1->n_implication;
718 scop->implications = scop1->implications;
719 scop1->n_implication = 0;
720 scop1->implications = NULL;
721 return scop;
724 if (scop1->n_implication == 0) {
725 scop->n_implication = scop2->n_implication;
726 scop->implications = scop2->implications;
727 scop2->n_implication = 0;
728 scop2->implications = NULL;
729 return scop;
732 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
733 scop1->n_implication + scop2->n_implication);
734 if (!scop->implications)
735 return pet_scop_free(scop);
737 for (i = 0; i < scop1->n_implication; ++i) {
738 scop->implications[i] = scop1->implications[i];
739 scop1->implications[i] = NULL;
742 scop->n_implication = scop1->n_implication;
743 j = scop1->n_implication;
744 for (i = 0; i < scop2->n_implication; ++i) {
745 int known;
747 known = is_known_implication(scop, scop2->implications[i]);
748 if (known < 0)
749 return pet_scop_free(scop);
750 if (known)
751 continue;
752 scop->implications[j++] = scop2->implications[i];
753 scop2->implications[i] = NULL;
755 scop->n_implication = j;
757 return scop;
760 /* Combine the offset information of "scop1" and "scop2" into "scop".
762 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
763 struct pet_scop *scop1, struct pet_scop *scop2)
765 if (scop1->loc != &pet_loc_dummy)
766 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
767 if (scop2->loc != &pet_loc_dummy)
768 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
769 return scop;
772 /* Create and return an independence that filters out the dependences
773 * in "filter" with local variables "local".
775 static struct pet_independence *new_independence(
776 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
778 isl_ctx *ctx;
779 struct pet_independence *independence;
781 if (!filter || !local)
782 goto error;
783 ctx = isl_union_map_get_ctx(filter);
784 independence = isl_alloc_type(ctx, struct pet_independence);
785 if (!independence)
786 goto error;
788 independence->filter = filter;
789 independence->local = local;
791 return independence;
792 error:
793 isl_union_map_free(filter);
794 isl_union_set_free(local);
795 return NULL;
798 /* Add an independence that filters out the dependences
799 * in "filter" with local variables "local" to "scop".
801 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
802 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
804 isl_ctx *ctx;
805 struct pet_independence *independence;
806 struct pet_independence **independences;
808 ctx = isl_union_map_get_ctx(filter);
809 independence = new_independence(filter, local);
810 if (!scop || !independence)
811 goto error;
813 independences = isl_realloc_array(ctx, scop->independences,
814 struct pet_independence *,
815 scop->n_independence + 1);
816 if (!independences)
817 goto error;
818 scop->independences = independences;
819 scop->independences[scop->n_independence] = independence;
820 scop->n_independence++;
822 return scop;
823 error:
824 pet_independence_free(independence);
825 pet_scop_free(scop);
826 return NULL;
829 /* Store the concatenation of the independences of "scop1" and "scop2"
830 * in "scop".
832 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
833 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
835 int i, off;
837 if (!scop)
838 return NULL;
840 if (scop2->n_independence == 0) {
841 scop->n_independence = scop1->n_independence;
842 scop->independences = scop1->independences;
843 scop1->n_independence = 0;
844 scop1->independences = NULL;
845 return scop;
848 if (scop1->n_independence == 0) {
849 scop->n_independence = scop2->n_independence;
850 scop->independences = scop2->independences;
851 scop2->n_independence = 0;
852 scop2->independences = NULL;
853 return scop;
856 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
857 scop1->n_independence + scop2->n_independence);
858 if (!scop->independences)
859 return pet_scop_free(scop);
861 for (i = 0; i < scop1->n_independence; ++i) {
862 scop->independences[i] = scop1->independences[i];
863 scop1->independences[i] = NULL;
866 off = scop1->n_independence;
867 for (i = 0; i < scop2->n_independence; ++i) {
868 scop->independences[off + i] = scop2->independences[i];
869 scop2->independences[i] = NULL;
871 scop->n_independence = scop1->n_independence + scop2->n_independence;
873 return scop;
876 /* Construct a pet_scop with the given schedule
877 * that contains the offset information,
878 * arrays, statements and skip information in "scop1" and "scop2".
880 static struct pet_scop *pet_scop_add(isl_ctx *ctx,
881 __isl_take isl_schedule *schedule, struct pet_scop *scop1,
882 struct pet_scop *scop2)
884 int i;
885 isl_space *space;
886 struct pet_scop *scop = NULL;
888 if (!scop1 || !scop2)
889 goto error;
891 if (scop1->n_stmt == 0) {
892 scop2 = scop_combine_skips(scop2, scop1, scop2);
893 pet_scop_free(scop1);
894 isl_schedule_free(schedule);
895 return scop2;
898 if (scop2->n_stmt == 0) {
899 scop1 = scop_combine_skips(scop1, scop1, scop2);
900 pet_scop_free(scop2);
901 isl_schedule_free(schedule);
902 return scop1;
905 space = isl_set_get_space(scop1->context);
906 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
907 isl_schedule_copy(schedule));
908 if (!scop)
909 goto error;
911 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
912 scop1->n_array + scop2->n_array);
913 if (!scop->arrays)
914 goto error;
915 scop->n_array = scop1->n_array + scop2->n_array;
917 for (i = 0; i < scop1->n_stmt; ++i) {
918 scop->stmts[i] = scop1->stmts[i];
919 scop1->stmts[i] = NULL;
922 for (i = 0; i < scop2->n_stmt; ++i) {
923 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
924 scop2->stmts[i] = NULL;
927 for (i = 0; i < scop1->n_array; ++i) {
928 scop->arrays[i] = scop1->arrays[i];
929 scop1->arrays[i] = NULL;
932 for (i = 0; i < scop2->n_array; ++i) {
933 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
934 scop2->arrays[i] = NULL;
937 scop = scop_collect_implications(ctx, scop, scop1, scop2);
938 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
939 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
940 scop = scop_combine_skips(scop, scop1, scop2);
941 scop = scop_combine_start_end(scop, scop1, scop2);
942 scop = scop_collect_independences(ctx, scop, scop1, scop2);
944 pet_scop_free(scop1);
945 pet_scop_free(scop2);
946 isl_schedule_free(schedule);
947 return scop;
948 error:
949 pet_scop_free(scop1);
950 pet_scop_free(scop2);
951 pet_scop_free(scop);
952 isl_schedule_free(schedule);
953 return NULL;
956 /* Apply the skip condition "skip" to "scop".
957 * That is, make sure "scop" is not executed when the condition holds.
959 * If "skip" is an affine expression, we add the conditions under
960 * which the expression is zero to the context and the skip conditions
961 * of "scop".
962 * Otherwise, we add a filter on the variable attaining the value zero.
964 static struct pet_scop *restrict_skip(struct pet_scop *scop,
965 __isl_take isl_multi_pw_aff *skip)
967 isl_set *zero;
968 isl_pw_aff *pa;
969 int is_aff;
971 if (!scop || !skip)
972 goto error;
974 is_aff = multi_pw_aff_is_affine(skip);
975 if (is_aff < 0)
976 goto error;
978 if (!is_aff)
979 return pet_scop_filter(scop, skip, 0);
981 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
982 isl_multi_pw_aff_free(skip);
983 zero = isl_pw_aff_zero_set(pa);
984 scop = pet_scop_restrict(scop, zero);
986 return scop;
987 error:
988 isl_multi_pw_aff_free(skip);
989 return pet_scop_free(scop);
992 /* Construct a pet_scop that contains the arrays, statements and
993 * skip information in "scop1" and "scop2", where the two scops
994 * are executed "in sequence". That is, breaks and continues
995 * in scop1 have an effect on scop2 and the schedule of the result
996 * is the sequence of the schedules of "scop1" and "scop2".
998 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
999 struct pet_scop *scop2)
1001 isl_schedule *schedule;
1003 if (!scop1 || !scop2)
1004 goto error;
1006 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1007 scop2 = restrict_skip(scop2,
1008 pet_scop_get_skip(scop1, pet_skip_now));
1009 schedule = isl_schedule_sequence(isl_schedule_copy(scop1->schedule),
1010 isl_schedule_copy(scop2->schedule));
1011 return pet_scop_add(ctx, schedule, scop1, scop2);
1012 error:
1013 pet_scop_free(scop1);
1014 pet_scop_free(scop2);
1015 return NULL;
1018 /* Construct a pet_scop that contains the arrays, statements and
1019 * skip information in "scop1" and "scop2", where the two scops
1020 * are executed "in parallel". That is, any break or continue
1021 * in scop1 has no effect on scop2 and the schedule of the result
1022 * is the set of the schedules of "scop1" and "scop2".
1024 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1025 struct pet_scop *scop2)
1027 isl_schedule *schedule;
1029 if (!scop1 || !scop2)
1030 goto error;
1032 schedule = isl_schedule_set(isl_schedule_copy(scop1->schedule),
1033 isl_schedule_copy(scop2->schedule));
1034 return pet_scop_add(ctx, schedule, scop1, scop2);
1035 error:
1036 pet_scop_free(scop1);
1037 pet_scop_free(scop2);
1038 return NULL;
1041 void *pet_implication_free(struct pet_implication *implication)
1043 int i;
1045 if (!implication)
1046 return NULL;
1048 isl_map_free(implication->extension);
1050 free(implication);
1051 return NULL;
1054 void *pet_independence_free(struct pet_independence *independence)
1056 if (!independence)
1057 return NULL;
1059 isl_union_map_free(independence->filter);
1060 isl_union_set_free(independence->local);
1062 free(independence);
1063 return NULL;
1066 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1068 int i;
1069 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1071 if (!scop)
1072 return NULL;
1073 pet_loc_free(scop->loc);
1074 isl_set_free(scop->context);
1075 isl_set_free(scop->context_value);
1076 isl_schedule_free(scop->schedule);
1077 if (scop->types)
1078 for (i = 0; i < scop->n_type; ++i)
1079 pet_type_free(scop->types[i]);
1080 free(scop->types);
1081 if (scop->arrays)
1082 for (i = 0; i < scop->n_array; ++i)
1083 pet_array_free(scop->arrays[i]);
1084 free(scop->arrays);
1085 if (scop->stmts)
1086 for (i = 0; i < scop->n_stmt; ++i)
1087 pet_stmt_free(scop->stmts[i]);
1088 free(scop->stmts);
1089 if (scop->implications)
1090 for (i = 0; i < scop->n_implication; ++i)
1091 pet_implication_free(scop->implications[i]);
1092 free(scop->implications);
1093 if (scop->independences)
1094 for (i = 0; i < scop->n_independence; ++i)
1095 pet_independence_free(scop->independences[i]);
1096 free(scop->independences);
1097 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1098 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1099 free(scop);
1100 return NULL;
1103 void pet_type_dump(struct pet_type *type)
1105 if (!type)
1106 return;
1108 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1111 void pet_implication_dump(struct pet_implication *implication)
1113 if (!implication)
1114 return;
1116 fprintf(stderr, "%d\n", implication->satisfied);
1117 isl_map_dump(implication->extension);
1120 void pet_scop_dump(struct pet_scop *scop)
1122 int i;
1123 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1125 if (!scop)
1126 return;
1128 isl_set_dump(scop->context);
1129 isl_set_dump(scop->context_value);
1130 isl_schedule_dump(scop->schedule);
1131 for (i = 0; i < scop->n_type; ++i)
1132 pet_type_dump(scop->types[i]);
1133 for (i = 0; i < scop->n_array; ++i)
1134 pet_array_dump(scop->arrays[i]);
1135 for (i = 0; i < scop->n_stmt; ++i)
1136 pet_stmt_dump(scop->stmts[i]);
1137 for (i = 0; i < scop->n_implication; ++i)
1138 pet_implication_dump(scop->implications[i]);
1140 if (ext->skip[0]) {
1141 fprintf(stderr, "skip\n");
1142 isl_multi_pw_aff_dump(ext->skip[0]);
1143 isl_multi_pw_aff_dump(ext->skip[1]);
1147 /* Return 1 if the two pet_arrays are equivalent.
1149 * We don't compare element_size as this may be target dependent.
1151 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1153 if (!array1 || !array2)
1154 return 0;
1156 if (!isl_set_is_equal(array1->context, array2->context))
1157 return 0;
1158 if (!isl_set_is_equal(array1->extent, array2->extent))
1159 return 0;
1160 if (!!array1->value_bounds != !!array2->value_bounds)
1161 return 0;
1162 if (array1->value_bounds &&
1163 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1164 return 0;
1165 if (strcmp(array1->element_type, array2->element_type))
1166 return 0;
1167 if (array1->element_is_record != array2->element_is_record)
1168 return 0;
1169 if (array1->live_out != array2->live_out)
1170 return 0;
1171 if (array1->uniquely_defined != array2->uniquely_defined)
1172 return 0;
1173 if (array1->declared != array2->declared)
1174 return 0;
1175 if (array1->exposed != array2->exposed)
1176 return 0;
1178 return 1;
1181 /* Return 1 if the two pet_stmts are equivalent.
1183 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1185 int i;
1187 if (!stmt1 || !stmt2)
1188 return 0;
1190 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1191 return 0;
1192 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1193 return 0;
1194 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1195 return 0;
1196 if (stmt1->n_arg != stmt2->n_arg)
1197 return 0;
1198 for (i = 0; i < stmt1->n_arg; ++i) {
1199 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1200 return 0;
1203 return 1;
1206 /* Return 1 if the two pet_types are equivalent.
1208 * We only compare the names of the types since the exact representation
1209 * of the definition may depend on the version of clang being used.
1211 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1213 if (!type1 || !type2)
1214 return 0;
1216 if (strcmp(type1->name, type2->name))
1217 return 0;
1219 return 1;
1222 /* Return 1 if the two pet_implications are equivalent.
1224 int pet_implication_is_equal(struct pet_implication *implication1,
1225 struct pet_implication *implication2)
1227 if (!implication1 || !implication2)
1228 return 0;
1230 if (implication1->satisfied != implication2->satisfied)
1231 return 0;
1232 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1233 return 0;
1235 return 1;
1238 /* Return 1 if the two pet_independences are equivalent.
1240 int pet_independence_is_equal(struct pet_independence *independence1,
1241 struct pet_independence *independence2)
1243 if (!independence1 || !independence2)
1244 return 0;
1246 if (!isl_union_map_is_equal(independence1->filter,
1247 independence2->filter))
1248 return 0;
1249 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1250 return 0;
1252 return 1;
1255 /* Return 1 if the two pet_scops are equivalent.
1257 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1259 int i;
1260 int equal;
1262 if (!scop1 || !scop2)
1263 return 0;
1265 if (!isl_set_is_equal(scop1->context, scop2->context))
1266 return 0;
1267 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1268 return 0;
1269 equal = isl_schedule_plain_is_equal(scop1->schedule, scop2->schedule);
1270 if (equal < 0)
1271 return -1;
1272 if (!equal)
1273 return 0;
1275 if (scop1->n_type != scop2->n_type)
1276 return 0;
1277 for (i = 0; i < scop1->n_type; ++i)
1278 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1279 return 0;
1281 if (scop1->n_array != scop2->n_array)
1282 return 0;
1283 for (i = 0; i < scop1->n_array; ++i)
1284 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1285 return 0;
1287 if (scop1->n_stmt != scop2->n_stmt)
1288 return 0;
1289 for (i = 0; i < scop1->n_stmt; ++i)
1290 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1291 return 0;
1293 if (scop1->n_implication != scop2->n_implication)
1294 return 0;
1295 for (i = 0; i < scop1->n_implication; ++i)
1296 if (!pet_implication_is_equal(scop1->implications[i],
1297 scop2->implications[i]))
1298 return 0;
1300 if (scop1->n_independence != scop2->n_independence)
1301 return 0;
1302 for (i = 0; i < scop1->n_independence; ++i)
1303 if (!pet_independence_is_equal(scop1->independences[i],
1304 scop2->independences[i]))
1305 return 0;
1307 return 1;
1310 /* Does the set "extent" reference a virtual array, i.e.,
1311 * one with user pointer equal to NULL?
1312 * A virtual array does not have any members.
1314 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1316 isl_id *id;
1317 int is_virtual;
1319 if (!isl_set_has_tuple_id(extent))
1320 return 0;
1321 if (isl_set_is_wrapping(extent))
1322 return 0;
1323 id = isl_set_get_tuple_id(extent);
1324 is_virtual = !isl_id_get_user(id);
1325 isl_id_free(id);
1327 return is_virtual;
1330 /* Intersect the initial dimensions of "array" with "domain", provided
1331 * that "array" represents a virtual array.
1333 * If "array" is virtual, then We take the preimage of "domain"
1334 * over the projection of the extent of "array" onto its initial dimensions
1335 * and intersect this extent with the result.
1337 static struct pet_array *virtual_array_intersect_domain_prefix(
1338 struct pet_array *array, __isl_take isl_set *domain)
1340 int n;
1341 isl_space *space;
1342 isl_multi_aff *ma;
1344 if (!array || !extent_is_virtual_array(array->extent)) {
1345 isl_set_free(domain);
1346 return array;
1349 space = isl_set_get_space(array->extent);
1350 n = isl_set_dim(domain, isl_dim_set);
1351 ma = pet_prefix_projection(space, n);
1352 domain = isl_set_preimage_multi_aff(domain, ma);
1354 array->extent = isl_set_intersect(array->extent, domain);
1355 if (!array->extent)
1356 return pet_array_free(array);
1358 return array;
1361 /* Intersect the initial dimensions of the domain of "stmt"
1362 * with "domain".
1364 * We take the preimage of "domain" over the projection of the
1365 * domain of "stmt" onto its initial dimensions and intersect
1366 * the domain of "stmt" with the result.
1368 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1369 __isl_take isl_set *domain)
1371 int n;
1372 isl_space *space;
1373 isl_multi_aff *ma;
1375 if (!stmt)
1376 goto error;
1378 space = isl_set_get_space(stmt->domain);
1379 n = isl_set_dim(domain, isl_dim_set);
1380 ma = pet_prefix_projection(space, n);
1381 domain = isl_set_preimage_multi_aff(domain, ma);
1383 stmt->domain = isl_set_intersect(stmt->domain, domain);
1384 if (!stmt->domain)
1385 return pet_stmt_free(stmt);
1387 return stmt;
1388 error:
1389 isl_set_free(domain);
1390 return pet_stmt_free(stmt);
1393 /* Intersect the initial dimensions of the domain of "implication"
1394 * with "domain".
1396 * We take the preimage of "domain" over the projection of the
1397 * domain of "implication" onto its initial dimensions and intersect
1398 * the domain of "implication" with the result.
1400 static struct pet_implication *implication_intersect_domain_prefix(
1401 struct pet_implication *implication, __isl_take isl_set *domain)
1403 int n;
1404 isl_space *space;
1405 isl_multi_aff *ma;
1407 if (!implication)
1408 goto error;
1410 space = isl_map_get_space(implication->extension);
1411 n = isl_set_dim(domain, isl_dim_set);
1412 ma = pet_prefix_projection(isl_space_domain(space), n);
1413 domain = isl_set_preimage_multi_aff(domain, ma);
1415 implication->extension =
1416 isl_map_intersect_domain(implication->extension, domain);
1417 if (!implication->extension)
1418 return pet_implication_free(implication);
1420 return implication;
1421 error:
1422 isl_set_free(domain);
1423 return pet_implication_free(implication);
1426 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1428 * The extents of the virtual arrays match the iteration domains,
1429 * so if the iteration domain changes, we need to change those extents too.
1431 * The domain of the schedule is intersected with (i.e., replaced by)
1432 * the union of the updated iteration domains.
1434 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1435 __isl_take isl_set *domain)
1437 int i;
1439 if (!scop)
1440 goto error;
1442 for (i = 0; i < scop->n_array; ++i) {
1443 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1444 scop->arrays[i], isl_set_copy(domain));
1445 if (!scop->arrays[i])
1446 goto error;
1449 for (i = 0; i < scop->n_stmt; ++i) {
1450 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1451 isl_set_copy(domain));
1452 if (!scop->stmts[i])
1453 goto error;
1456 for (i = 0; i < scop->n_implication; ++i) {
1457 scop->implications[i] =
1458 implication_intersect_domain_prefix(scop->implications[i],
1459 isl_set_copy(domain));
1460 if (!scop->implications[i])
1461 return pet_scop_free(scop);
1464 scop->schedule = isl_schedule_intersect_domain(scop->schedule,
1465 pet_scop_collect_domains(scop));
1466 if (!scop->schedule)
1467 goto error;
1469 isl_set_free(domain);
1470 return scop;
1471 error:
1472 isl_set_free(domain);
1473 return pet_scop_free(scop);
1476 /* Update the context with respect to an embedding into a loop
1477 * with iteration domain "dom".
1478 * The input context lives in the same space as "dom".
1479 * The output context has the inner dimension removed.
1481 * An outer loop iterator value is invalid for the embedding if
1482 * any of the corresponding inner iterator values is invalid.
1483 * That is, an outer loop iterator value is valid only if all the corresponding
1484 * inner iterator values are valid.
1485 * We therefore compute the set of outer loop iterators l
1487 * forall i: dom(l,i) => valid(l,i)
1489 * or
1491 * forall i: not dom(l,i) or valid(l,i)
1493 * or
1495 * not exists i: dom(l,i) and not valid(l,i)
1497 * i.e.,
1499 * not exists i: (dom \ valid)(l,i)
1501 * If there are any unnamed parameters in "dom", then we consider
1502 * a parameter value to be valid if it is valid for any value of those
1503 * unnamed parameters. They are therefore projected out at the end.
1505 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1506 __isl_keep isl_set *dom)
1508 int pos;
1510 pos = isl_set_dim(context, isl_dim_set) - 1;
1511 context = isl_set_subtract(isl_set_copy(dom), context);
1512 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1513 context = isl_set_complement(context);
1514 context = pet_nested_remove_from_set(context);
1516 return context;
1519 /* Update the implication with respect to an embedding into a loop
1520 * with iteration domain "dom".
1522 * Since embed_access extends virtual arrays along with the domain
1523 * of the access, we need to do the same with domain and range
1524 * of the implication. Since the original implication is only valid
1525 * within a given iteration of the loop, the extended implication
1526 * maps the extra array dimension corresponding to the extra loop
1527 * to itself.
1529 static struct pet_implication *pet_implication_embed(
1530 struct pet_implication *implication, __isl_take isl_set *dom)
1532 isl_id *id;
1533 isl_map *map;
1535 if (!implication)
1536 goto error;
1538 map = isl_set_identity(dom);
1539 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1540 map = isl_map_flat_product(map, implication->extension);
1541 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1542 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1543 implication->extension = map;
1544 if (!implication->extension)
1545 return pet_implication_free(implication);
1547 return implication;
1548 error:
1549 isl_set_free(dom);
1550 return NULL;
1553 /* Internal data structure for outer_projection_mupa.
1555 * "n" is the number of outer dimensions onto which to project.
1556 * "res" collects the result.
1558 struct pet_outer_projection_data {
1559 int n;
1560 isl_union_pw_multi_aff *res;
1563 /* Create a function that maps "set" onto its outer data->n dimensions and
1564 * add it to data->res.
1566 static isl_stat add_outer_projection(__isl_take isl_set *set, void *user)
1568 struct pet_outer_projection_data *data = user;
1569 int dim;
1570 isl_space *space;
1571 isl_pw_multi_aff *pma;
1573 dim = isl_set_dim(set, isl_dim_set);
1574 space = isl_set_get_space(set);
1575 pma = isl_pw_multi_aff_project_out_map(space,
1576 isl_dim_set, data->n, dim - data->n);
1577 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
1579 isl_set_free(set);
1581 return isl_stat_ok;
1584 /* Create and return a function that maps the sets in "domain"
1585 * onto their outer "n" dimensions.
1587 static __isl_give isl_multi_union_pw_aff *outer_projection_mupa(
1588 __isl_take isl_union_set *domain, int n)
1590 struct pet_outer_projection_data data;
1591 isl_space *space;
1593 space = isl_union_set_get_space(domain);
1594 data.n = n;
1595 data.res = isl_union_pw_multi_aff_empty(space);
1596 if (isl_union_set_foreach_set(domain, &add_outer_projection, &data) < 0)
1597 data.res = isl_union_pw_multi_aff_free(data.res);
1599 isl_union_set_free(domain);
1600 return isl_multi_union_pw_aff_from_union_pw_multi_aff(data.res);
1603 /* Embed "schedule" in a loop with schedule "prefix".
1604 * The domain of "prefix" corresponds to the outer dimensions
1605 * of the iteration domains.
1606 * We therefore construct a projection onto these outer dimensions,
1607 * compose it with "prefix" and then add the result as a band schedule.
1609 * If the domain of the schedule is empty, then there is no need
1610 * to insert any node.
1612 static __isl_give isl_schedule *schedule_embed(
1613 __isl_take isl_schedule *schedule, __isl_keep isl_multi_aff *prefix)
1615 int n;
1616 int empty;
1617 isl_union_set *domain;
1618 isl_multi_aff *ma;
1619 isl_multi_union_pw_aff *mupa;
1621 domain = isl_schedule_get_domain(schedule);
1622 empty = isl_union_set_is_empty(domain);
1623 if (empty < 0 || empty) {
1624 isl_union_set_free(domain);
1625 return empty < 0 ? isl_schedule_free(schedule) : schedule;
1628 n = isl_multi_aff_dim(prefix, isl_dim_in);
1629 mupa = outer_projection_mupa(domain, n);
1630 ma = isl_multi_aff_copy(prefix);
1631 mupa = isl_multi_union_pw_aff_apply_multi_aff(mupa, ma);
1632 schedule = isl_schedule_insert_partial_schedule(schedule, mupa);
1634 return schedule;
1637 /* Adjust the context and the schedule according to an embedding
1638 * in a loop with iteration domain "dom" and schedule "sched".
1640 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1641 __isl_take isl_multi_aff *sched)
1643 int i;
1645 if (!scop)
1646 goto error;
1648 scop->context = context_embed(scop->context, dom);
1649 if (!scop->context)
1650 goto error;
1652 scop->schedule = schedule_embed(scop->schedule, sched);
1653 if (!scop->schedule)
1654 goto error;
1656 isl_set_free(dom);
1657 isl_multi_aff_free(sched);
1658 return scop;
1659 error:
1660 isl_set_free(dom);
1661 isl_multi_aff_free(sched);
1662 return pet_scop_free(scop);
1665 /* Add extra conditions to scop->skip[type].
1667 * The new skip condition only holds if it held before
1668 * and the condition is true. It does not hold if it did not hold
1669 * before or the condition is false.
1671 * The skip condition is assumed to be an affine expression.
1673 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1674 enum pet_skip type, __isl_keep isl_set *cond)
1676 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1677 isl_pw_aff *skip;
1678 isl_set *dom;
1680 if (!scop)
1681 return NULL;
1682 if (!ext->skip[type])
1683 return scop;
1685 if (!multi_pw_aff_is_affine(ext->skip[type]))
1686 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1687 isl_error_internal, "can only restrict affine skips",
1688 return pet_scop_free(scop));
1690 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1691 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1692 cond = isl_set_copy(cond);
1693 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1694 skip = indicator_function(cond, dom);
1695 isl_multi_pw_aff_free(ext->skip[type]);
1696 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1697 if (!ext->skip[type])
1698 return pet_scop_free(scop);
1700 return scop;
1703 /* Adjust the context and the skip conditions to the fact that
1704 * the scop was created in a context where "cond" holds.
1706 * An outer loop iterator or parameter value is valid for the result
1707 * if it was valid for the original scop and satisfies "cond" or if it does
1708 * not satisfy "cond" as in this case the scop is not executed
1709 * and the original constraints on these values are irrelevant.
1711 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1712 __isl_take isl_set *cond)
1714 int i;
1716 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1717 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1719 if (!scop)
1720 goto error;
1722 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1723 scop->context = isl_set_union(scop->context,
1724 isl_set_complement(isl_set_copy(cond)));
1725 scop->context = isl_set_coalesce(scop->context);
1726 scop->context = pet_nested_remove_from_set(scop->context);
1727 if (!scop->context)
1728 goto error;
1730 isl_set_free(cond);
1731 return scop;
1732 error:
1733 isl_set_free(cond);
1734 return pet_scop_free(scop);
1737 /* Insert an argument expression corresponding to "test" in front
1738 * of the list of arguments described by *n_arg and *args.
1740 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1741 __isl_keep isl_multi_pw_aff *test)
1743 int i;
1744 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1746 if (!test)
1747 return -1;
1749 if (!*args) {
1750 *args = isl_calloc_array(ctx, pet_expr *, 1);
1751 if (!*args)
1752 return -1;
1753 } else {
1754 pet_expr **ext;
1755 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1756 if (!ext)
1757 return -1;
1758 for (i = 0; i < *n_arg; ++i)
1759 ext[1 + i] = (*args)[i];
1760 free(*args);
1761 *args = ext;
1763 (*n_arg)++;
1764 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1765 if (!(*args)[0])
1766 return -1;
1768 return 0;
1771 /* Look through the applications in "scop" for any that can be
1772 * applied to the filter expressed by "map" and "satisified".
1773 * If there is any, then apply it to "map" and return the result.
1774 * Otherwise, return "map".
1775 * "id" is the identifier of the virtual array.
1777 * We only introduce at most one implication for any given virtual array,
1778 * so we can apply the implication and return as soon as we find one.
1780 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1781 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1783 int i;
1785 for (i = 0; i < scop->n_implication; ++i) {
1786 struct pet_implication *pi = scop->implications[i];
1787 isl_id *pi_id;
1789 if (pi->satisfied != satisfied)
1790 continue;
1791 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1792 isl_id_free(pi_id);
1793 if (pi_id != id)
1794 continue;
1796 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1799 return map;
1802 /* Is the filter expressed by "test" and "satisfied" implied
1803 * by filter "pos" on "domain", with filter "expr", taking into
1804 * account the implications of "scop"?
1806 * For filter on domain implying that expressed by "test" and "satisfied",
1807 * the filter needs to be an access to the same (virtual) array as "test" and
1808 * the filter value needs to be equal to "satisfied".
1809 * Moreover, the filter access relation, possibly extended by
1810 * the implications in "scop" needs to contain "test".
1812 static int implies_filter(struct pet_scop *scop,
1813 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1814 __isl_keep isl_map *test, int satisfied)
1816 isl_id *test_id, *arg_id;
1817 isl_val *val;
1818 int is_int;
1819 int s;
1820 int is_subset;
1821 isl_map *implied;
1823 if (expr->type != pet_expr_access)
1824 return 0;
1825 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1826 arg_id = pet_expr_access_get_id(expr);
1827 isl_id_free(arg_id);
1828 isl_id_free(test_id);
1829 if (test_id != arg_id)
1830 return 0;
1831 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1832 is_int = isl_val_is_int(val);
1833 if (is_int)
1834 s = isl_val_get_num_si(val);
1835 isl_val_free(val);
1836 if (!val)
1837 return -1;
1838 if (!is_int)
1839 return 0;
1840 if (s != satisfied)
1841 return 0;
1843 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1844 implied = apply_implications(scop, implied, test_id, satisfied);
1845 is_subset = isl_map_is_subset(test, implied);
1846 isl_map_free(implied);
1848 return is_subset;
1851 /* Is the filter expressed by "test" and "satisfied" implied
1852 * by any of the filters on the domain of "stmt", taking into
1853 * account the implications of "scop"?
1855 static int filter_implied(struct pet_scop *scop,
1856 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1858 int i;
1859 int implied;
1860 isl_id *test_id;
1861 isl_map *domain;
1862 isl_map *test_map;
1864 if (!scop || !stmt || !test)
1865 return -1;
1866 if (scop->n_implication == 0)
1867 return 0;
1868 if (stmt->n_arg == 0)
1869 return 0;
1871 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1872 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1874 implied = 0;
1875 for (i = 0; i < stmt->n_arg; ++i) {
1876 implied = implies_filter(scop, domain, i, stmt->args[i],
1877 test_map, satisfied);
1878 if (implied < 0 || implied)
1879 break;
1882 isl_map_free(test_map);
1883 isl_map_free(domain);
1884 return implied;
1887 /* Make the statement "stmt" depend on the value of "test"
1888 * being equal to "satisfied" by adjusting stmt->domain.
1890 * The domain of "test" corresponds to the (zero or more) outer dimensions
1891 * of the iteration domain.
1893 * We first extend "test" to apply to the entire iteration domain and
1894 * then check if the filter that we are about to add is implied
1895 * by any of the current filters, possibly taking into account
1896 * the implications in "scop". If so, we leave "stmt" untouched and return.
1898 * Otherwise, we insert an argument corresponding to a read to "test"
1899 * from the iteration domain of "stmt" in front of the list of arguments.
1900 * We also insert a corresponding output dimension in the wrapped
1901 * map contained in stmt->domain, with value set to "satisfied".
1903 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1904 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1906 int i;
1907 int implied;
1908 isl_id *id;
1909 isl_ctx *ctx;
1910 isl_pw_multi_aff *pma;
1911 isl_multi_aff *add_dom;
1912 isl_space *space;
1913 isl_local_space *ls;
1914 int n_test_dom;
1916 if (!stmt || !test)
1917 goto error;
1919 space = pet_stmt_get_space(stmt);
1920 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1921 space = isl_space_from_domain(space);
1922 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1923 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1924 ls = isl_local_space_from_space(isl_space_domain(space));
1925 for (i = 0; i < n_test_dom; ++i) {
1926 isl_aff *aff;
1927 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1928 isl_dim_set, i);
1929 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1931 isl_local_space_free(ls);
1932 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1934 implied = filter_implied(scop, stmt, test, satisfied);
1935 if (implied < 0)
1936 goto error;
1937 if (implied) {
1938 isl_multi_pw_aff_free(test);
1939 return stmt;
1942 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1943 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1944 id, satisfied);
1945 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1947 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1948 goto error;
1950 isl_multi_pw_aff_free(test);
1951 return stmt;
1952 error:
1953 isl_multi_pw_aff_free(test);
1954 return pet_stmt_free(stmt);
1957 /* Does "scop" have a skip condition of the given "type"?
1959 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1961 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1963 if (!scop)
1964 return -1;
1965 return ext->skip[type] != NULL;
1968 /* Does "scop" have a skip condition of the given "type" that
1969 * is an affine expression?
1971 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1973 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1975 if (!scop)
1976 return -1;
1977 if (!ext->skip[type])
1978 return 0;
1979 return multi_pw_aff_is_affine(ext->skip[type]);
1982 /* Does "scop" have a skip condition of the given "type" that
1983 * is not an affine expression?
1985 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1987 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1988 int aff;
1990 if (!scop)
1991 return -1;
1992 if (!ext->skip[type])
1993 return 0;
1994 aff = multi_pw_aff_is_affine(ext->skip[type]);
1995 if (aff < 0)
1996 return -1;
1997 return !aff;
2000 /* Does "scop" have a skip condition of the given "type" that
2001 * is affine and holds on the entire domain?
2003 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2005 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2006 isl_pw_aff *pa;
2007 isl_set *set;
2008 int is_aff;
2009 int is_univ;
2011 is_aff = pet_scop_has_affine_skip(scop, type);
2012 if (is_aff < 0 || !is_aff)
2013 return is_aff;
2015 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2016 set = isl_pw_aff_non_zero_set(pa);
2017 is_univ = isl_set_plain_is_universe(set);
2018 isl_set_free(set);
2020 return is_univ;
2023 /* Replace scop->skip[type] by "skip".
2025 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2026 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2028 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2030 if (!scop || !skip)
2031 goto error;
2033 isl_multi_pw_aff_free(ext->skip[type]);
2034 ext->skip[type] = skip;
2036 return scop;
2037 error:
2038 isl_multi_pw_aff_free(skip);
2039 return pet_scop_free(scop);
2042 /* Return a copy of scop->skip[type].
2044 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2045 enum pet_skip type)
2047 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2049 if (!scop)
2050 return NULL;
2052 return isl_multi_pw_aff_copy(ext->skip[type]);
2055 /* Assuming scop->skip[type] is an affine expression,
2056 * return the constraints on the outer loop domain for which the skip condition
2057 * holds.
2059 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2060 enum pet_skip type)
2062 isl_multi_pw_aff *skip;
2063 isl_pw_aff *pa;
2065 skip = pet_scop_get_skip(scop, type);
2066 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2067 isl_multi_pw_aff_free(skip);
2068 return isl_pw_aff_non_zero_set(pa);
2071 /* Return the identifier of the variable that is accessed by
2072 * the skip condition of the given type.
2074 * The skip condition is assumed not to be an affine condition.
2076 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2077 enum pet_skip type)
2079 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2081 if (!scop)
2082 return NULL;
2084 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2087 /* Return an access pet_expr corresponding to the skip condition
2088 * of the given type.
2090 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2091 enum pet_skip type)
2093 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2096 /* Drop the skip condition scop->skip[type].
2098 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2100 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2102 if (!scop)
2103 return;
2105 isl_multi_pw_aff_free(ext->skip[type]);
2106 ext->skip[type] = NULL;
2109 /* Drop all skip conditions on "scop".
2111 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2113 pet_scop_reset_skip(scop, pet_skip_now);
2114 pet_scop_reset_skip(scop, pet_skip_later);
2116 return scop;
2119 /* Make the skip condition (if any) depend on the value of "test" being
2120 * equal to "satisfied".
2122 * We only support the case where the original skip condition is universal,
2123 * i.e., where skipping is unconditional, and where satisfied == 1.
2124 * In this case, the skip condition is changed to skip only when
2125 * "test" is equal to one.
2127 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2128 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2130 int is_univ = 0;
2132 if (!scop)
2133 return NULL;
2134 if (!pet_scop_has_skip(scop, type))
2135 return scop;
2137 if (satisfied)
2138 is_univ = pet_scop_has_universal_skip(scop, type);
2139 if (is_univ < 0)
2140 return pet_scop_free(scop);
2141 if (satisfied && is_univ) {
2142 isl_multi_pw_aff *skip;
2143 skip = isl_multi_pw_aff_copy(test);
2144 scop = pet_scop_set_skip(scop, type, skip);
2145 if (!scop)
2146 return NULL;
2147 } else {
2148 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2149 "skip expression cannot be filtered",
2150 return pet_scop_free(scop));
2153 return scop;
2156 /* Make all statements in "scop" depend on the value of "test"
2157 * being equal to "satisfied" by adjusting their domains.
2159 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2160 __isl_take isl_multi_pw_aff *test, int satisfied)
2162 int i;
2164 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2165 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2167 if (!scop || !test)
2168 goto error;
2170 for (i = 0; i < scop->n_stmt; ++i) {
2171 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2172 isl_multi_pw_aff_copy(test), satisfied);
2173 if (!scop->stmts[i])
2174 goto error;
2177 isl_multi_pw_aff_free(test);
2178 return scop;
2179 error:
2180 isl_multi_pw_aff_free(test);
2181 return pet_scop_free(scop);
2184 /* Add the parameters of the access expression "expr" to "space".
2186 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2188 int i;
2189 isl_space *expr_space;
2190 isl_space **space = user;
2192 expr_space = pet_expr_access_get_parameter_space(expr);
2193 *space = isl_space_align_params(*space, expr_space);
2195 return *space ? 0 : -1;
2198 /* Add all parameters in "stmt" to "space" and return the result.
2200 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2201 __isl_take isl_space *space)
2203 int i;
2205 if (!stmt)
2206 return isl_space_free(space);
2208 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2209 for (i = 0; i < stmt->n_arg; ++i)
2210 if (pet_expr_foreach_access_expr(stmt->args[i],
2211 &access_collect_params, &space) < 0)
2212 space = isl_space_free(space);
2213 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2214 &space) < 0)
2215 space = isl_space_free(space);
2217 return space;
2220 /* Add all parameters in "array" to "space" and return the result.
2222 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2223 __isl_take isl_space *space)
2225 if (!array)
2226 return isl_space_free(space);
2228 space = isl_space_align_params(space,
2229 isl_set_get_space(array->context));
2230 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2232 return space;
2235 /* Add all parameters in "independence" to "space" and return the result.
2237 static __isl_give isl_space *independence_collect_params(
2238 struct pet_independence *independence, __isl_take isl_space *space)
2240 if (!independence)
2241 return isl_space_free(space);
2243 space = isl_space_align_params(space,
2244 isl_union_map_get_space(independence->filter));
2245 space = isl_space_align_params(space,
2246 isl_union_set_get_space(independence->local));
2248 return space;
2251 /* Collect all parameters in "scop" in a parameter space and return the result.
2253 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2255 isl_space *space;
2256 int i;
2258 if (!scop)
2259 return NULL;
2261 space = isl_set_get_space(scop->context);
2263 for (i = 0; i < scop->n_array; ++i)
2264 space = array_collect_params(scop->arrays[i], space);
2266 for (i = 0; i < scop->n_stmt; ++i)
2267 space = stmt_collect_params(scop->stmts[i], space);
2269 for (i = 0; i < scop->n_independence; ++i)
2270 space = independence_collect_params(scop->independences[i],
2271 space);
2273 return space;
2276 /* Add all parameters in "space" to the domain and
2277 * all access relations in "stmt".
2279 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2280 __isl_take isl_space *space)
2282 int i;
2284 if (!stmt)
2285 goto error;
2287 stmt->domain = isl_set_align_params(stmt->domain,
2288 isl_space_copy(space));
2290 for (i = 0; i < stmt->n_arg; ++i) {
2291 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2292 isl_space_copy(space));
2293 if (!stmt->args[i])
2294 goto error;
2296 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2298 if (!stmt->domain || !stmt->body)
2299 goto error;
2301 isl_space_free(space);
2302 return stmt;
2303 error:
2304 isl_space_free(space);
2305 return pet_stmt_free(stmt);
2308 /* Add all parameters in "space" to "array".
2310 static struct pet_array *array_propagate_params(struct pet_array *array,
2311 __isl_take isl_space *space)
2313 if (!array)
2314 goto error;
2316 array->context = isl_set_align_params(array->context,
2317 isl_space_copy(space));
2318 array->extent = isl_set_align_params(array->extent,
2319 isl_space_copy(space));
2320 if (array->value_bounds) {
2321 array->value_bounds = isl_set_align_params(array->value_bounds,
2322 isl_space_copy(space));
2323 if (!array->value_bounds)
2324 goto error;
2327 if (!array->context || !array->extent)
2328 goto error;
2330 isl_space_free(space);
2331 return array;
2332 error:
2333 isl_space_free(space);
2334 return pet_array_free(array);
2337 /* Add all parameters in "space" to "independence".
2339 static struct pet_independence *independence_propagate_params(
2340 struct pet_independence *independence, __isl_take isl_space *space)
2342 if (!independence)
2343 goto error;
2345 independence->filter = isl_union_map_align_params(independence->filter,
2346 isl_space_copy(space));
2347 independence->local = isl_union_set_align_params(independence->local,
2348 isl_space_copy(space));
2349 if (!independence->filter || !independence->local)
2350 goto error;
2352 isl_space_free(space);
2353 return independence;
2354 error:
2355 isl_space_free(space);
2356 return pet_independence_free(independence);
2359 /* Add all parameters in "space" to "scop".
2361 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2362 __isl_take isl_space *space)
2364 int i;
2366 if (!scop)
2367 goto error;
2369 scop->context = isl_set_align_params(scop->context,
2370 isl_space_copy(space));
2371 scop->schedule = isl_schedule_align_params(scop->schedule,
2372 isl_space_copy(space));
2373 if (!scop->context || !scop->schedule)
2374 goto error;
2376 for (i = 0; i < scop->n_array; ++i) {
2377 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2378 isl_space_copy(space));
2379 if (!scop->arrays[i])
2380 goto error;
2383 for (i = 0; i < scop->n_stmt; ++i) {
2384 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2385 isl_space_copy(space));
2386 if (!scop->stmts[i])
2387 goto error;
2390 for (i = 0; i < scop->n_independence; ++i) {
2391 scop->independences[i] = independence_propagate_params(
2392 scop->independences[i], isl_space_copy(space));
2393 if (!scop->independences[i])
2394 goto error;
2397 isl_space_free(space);
2398 return scop;
2399 error:
2400 isl_space_free(space);
2401 return pet_scop_free(scop);
2404 /* Update all isl_sets and isl_maps in "scop" such that they all
2405 * have the same parameters.
2407 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2409 isl_space *space;
2411 if (!scop)
2412 return NULL;
2414 space = scop_collect_params(scop);
2416 scop = scop_propagate_params(scop, space);
2418 return scop;
2421 /* Add the access relation of the give "type" of the access expression "expr"
2422 * to "accesses" and return the result.
2423 * The domain of the access relation is intersected with "domain".
2424 * If "tag" is set, then the access relation is tagged with
2425 * the corresponding reference identifier.
2427 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2428 enum pet_expr_access_type type, int tag,
2429 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2431 isl_union_map *access;
2433 access = pet_expr_access_get_access(expr, type);
2434 access = isl_union_map_intersect_domain(access,
2435 isl_union_set_copy(domain));
2436 if (tag)
2437 access = pet_expr_tag_access(expr, access);
2438 return isl_union_map_union(accesses, access);
2441 /* Internal data structure for expr_collect_accesses.
2443 * "type" is the type of accesses we want to collect.
2444 * "tag" is set if the access relations should be tagged with
2445 * the corresponding reference identifiers.
2446 * "domain" are constraints on the domain of the access relations.
2447 * "accesses" collects the results.
2449 struct pet_expr_collect_accesses_data {
2450 enum pet_expr_access_type type;
2451 int tag;
2452 isl_union_set *domain;
2454 isl_union_map *accesses;
2457 /* Add the access relation of the access expression "expr"
2458 * to data->accesses if the access expression is a read and we are collecting
2459 * reads and/or it is a write and we are collecting writes.
2460 * The domains of the access relations are intersected with data->domain.
2461 * If data->tag is set, then the access relations are tagged with
2462 * the corresponding reference identifiers.
2464 * If data->type is pet_expr_access_must_write, then we only add
2465 * the accesses that are definitely performed. Otherwise, we add
2466 * all potential accesses.
2467 * In particular, if the access has any arguments, then in case of
2468 * pet_expr_access_must_write we currently skip the access completely.
2469 * In other cases, we project out the values of the access arguments.
2471 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2473 struct pet_expr_collect_accesses_data *data = user;
2474 int i;
2475 isl_id *id;
2476 isl_space *dim;
2478 if (!expr)
2479 return -1;
2481 if (pet_expr_is_affine(expr))
2482 return 0;
2483 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2484 return 0;
2486 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2487 ((data->type == pet_expr_access_may_write ||
2488 data->type == pet_expr_access_must_write) && expr->acc.write))
2489 data->accesses = expr_collect_access(expr,
2490 data->type, data->tag,
2491 data->accesses, data->domain);
2493 return data->accesses ? 0 : -1;
2496 /* Collect and return all access relations of the given "type" in "stmt".
2497 * If "tag" is set, then the access relations are tagged with
2498 * the corresponding reference identifiers.
2499 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2500 * we simply add the argument of the kill operation.
2502 * If we are looking for definite accesses (pet_expr_access_must_write
2503 * or pet_expr_access_killed), then we only add the accesses that are
2504 * definitely performed. Otherwise, we add all potential accesses.
2505 * In particular, if the statement has any arguments, then if we are looking
2506 * for definite accesses we currently skip the statement completely. Othewise,
2507 * we project out the values of the statement arguments.
2508 * If the statement body is not an expression tree, then we cannot
2509 * know for sure if/when the accesses inside the tree are performed.
2510 * We therefore ignore such statements when we are looking for
2511 * definite accesses.
2513 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2514 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2516 struct pet_expr_collect_accesses_data data = { type, tag };
2517 int must;
2518 isl_set *domain;
2520 if (!stmt)
2521 return NULL;
2523 data.accesses = isl_union_map_empty(dim);
2525 if (type == pet_expr_access_must_write ||
2526 type == pet_expr_access_killed)
2527 must = 1;
2528 else
2529 must = 0;
2531 if (must && stmt->n_arg > 0)
2532 return data.accesses;
2533 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2534 return data.accesses;
2536 domain = drop_arguments(isl_set_copy(stmt->domain));
2537 data.domain = isl_union_set_from_set(domain);
2539 if (type == pet_expr_access_killed) {
2540 pet_expr *body, *arg;
2542 body = pet_tree_expr_get_expr(stmt->body);
2543 arg = pet_expr_get_arg(body, 0);
2544 data.accesses = expr_collect_access(arg,
2545 pet_expr_access_killed, tag,
2546 data.accesses, data.domain);
2547 pet_expr_free(arg);
2548 pet_expr_free(body);
2549 } else if (pet_tree_foreach_access_expr(stmt->body,
2550 &expr_collect_accesses, &data) < 0)
2551 data.accesses = isl_union_map_free(data.accesses);
2553 isl_union_set_free(data.domain);
2555 return data.accesses;
2558 /* Is "stmt" an assignment statement?
2560 int pet_stmt_is_assign(struct pet_stmt *stmt)
2562 if (!stmt)
2563 return 0;
2564 return pet_tree_is_assign(stmt->body);
2567 /* Is "stmt" a kill statement?
2569 int pet_stmt_is_kill(struct pet_stmt *stmt)
2571 if (!stmt)
2572 return 0;
2573 return pet_tree_is_kill(stmt->body);
2576 /* Is "stmt" an assume statement?
2578 int pet_stmt_is_assume(struct pet_stmt *stmt)
2580 if (!stmt)
2581 return 0;
2582 return pet_tree_is_assume(stmt->body);
2585 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2587 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2588 __isl_keep isl_map *map, __isl_keep isl_set *set)
2590 isl_map *gist;
2592 gist = isl_map_copy(map);
2593 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2594 return isl_union_map_add_map(umap, gist);
2597 /* Compute a mapping from all arrays (of structs) in scop
2598 * to their members.
2600 * If "from_outermost" is set, then the domain only consists
2601 * of outermost arrays.
2602 * If "to_innermost" is set, then the range only consists
2603 * of innermost arrays.
2605 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2606 int from_outermost, int to_innermost)
2608 int i;
2609 isl_union_map *to_inner;
2611 if (!scop)
2612 return NULL;
2614 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2616 for (i = 0; i < scop->n_array; ++i) {
2617 struct pet_array *array = scop->arrays[i];
2618 isl_set *set;
2619 isl_map *map;
2621 if (to_innermost && array->element_is_record)
2622 continue;
2624 set = isl_set_copy(array->extent);
2625 map = isl_set_identity(isl_set_copy(set));
2627 while (set && isl_set_is_wrapping(set)) {
2628 isl_id *id;
2629 isl_map *wrapped;
2631 if (!from_outermost)
2632 to_inner = add_gisted(to_inner, map, set);
2634 id = isl_set_get_tuple_id(set);
2635 wrapped = isl_set_unwrap(set);
2636 wrapped = isl_map_domain_map(wrapped);
2637 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2638 map = isl_map_apply_domain(map, wrapped);
2639 set = isl_map_domain(isl_map_copy(map));
2642 map = isl_map_gist_domain(map, set);
2643 to_inner = isl_union_map_add_map(to_inner, map);
2646 return to_inner;
2649 /* Compute a mapping from all arrays (of structs) in scop
2650 * to their innermost arrays.
2652 * In particular, for each array of a primitive type, the result
2653 * contains the identity mapping on that array.
2654 * For each array involving member accesses, the result
2655 * contains a mapping from the elements of any intermediate array of structs
2656 * to all corresponding elements of the innermost nested arrays.
2658 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2659 struct pet_scop *scop)
2661 return compute_to_inner(scop, 0, 1);
2664 /* Compute a mapping from all outermost arrays (of structs) in scop
2665 * to their innermost members.
2667 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2669 return compute_to_inner(scop, 1, 1);
2672 /* Compute a mapping from all outermost arrays (of structs) in scop
2673 * to their members, including the outermost arrays themselves.
2675 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2677 return compute_to_inner(scop, 1, 0);
2680 /* Collect and return all access relations of the given "type" in "scop".
2681 * If "type" is pet_expr_access_killed, then we only add the arguments of
2682 * kill operations.
2683 * If we are looking for definite accesses (pet_expr_access_must_write
2684 * or pet_expr_access_killed), then we only add the accesses that are
2685 * definitely performed. Otherwise, we add all potential accesses.
2686 * If "tag" is set, then the access relations are tagged with
2687 * the corresponding reference identifiers.
2688 * For accesses to structures, the returned access relation accesses
2689 * all individual fields in the structures.
2691 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2692 enum pet_expr_access_type type, int tag)
2694 int i;
2695 isl_union_map *accesses;
2696 isl_union_set *arrays;
2697 isl_union_map *to_inner;
2699 if (!scop)
2700 return NULL;
2702 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2704 for (i = 0; i < scop->n_stmt; ++i) {
2705 struct pet_stmt *stmt = scop->stmts[i];
2706 isl_union_map *accesses_i;
2707 isl_space *space;
2709 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2710 continue;
2712 space = isl_set_get_space(scop->context);
2713 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2714 accesses = isl_union_map_union(accesses, accesses_i);
2717 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2718 for (i = 0; i < scop->n_array; ++i) {
2719 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2720 arrays = isl_union_set_add_set(arrays, extent);
2722 accesses = isl_union_map_intersect_range(accesses, arrays);
2724 to_inner = pet_scop_compute_any_to_inner(scop);
2725 accesses = isl_union_map_apply_range(accesses, to_inner);
2727 return accesses;
2730 /* Collect all potential read access relations.
2732 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2734 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2737 /* Collect all potential write access relations.
2739 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2741 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2744 /* Collect all definite write access relations.
2746 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2748 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2751 /* Collect all definite kill access relations.
2753 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2755 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2758 /* Collect all tagged potential read access relations.
2760 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2761 struct pet_scop *scop)
2763 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2766 /* Collect all tagged potential write access relations.
2768 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2769 struct pet_scop *scop)
2771 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2774 /* Collect all tagged definite write access relations.
2776 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2777 struct pet_scop *scop)
2779 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2782 /* Collect all tagged definite kill access relations.
2784 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2785 struct pet_scop *scop)
2787 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2790 /* Collect and return the union of iteration domains in "scop".
2792 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2794 int i;
2795 isl_set *domain_i;
2796 isl_union_set *domain;
2798 if (!scop)
2799 return NULL;
2801 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2803 for (i = 0; i < scop->n_stmt; ++i) {
2804 domain_i = isl_set_copy(scop->stmts[i]->domain);
2805 if (scop->stmts[i]->n_arg > 0)
2806 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2807 domain = isl_union_set_add_set(domain, domain_i);
2810 return domain;
2813 /* Add a reference identifier to all access expressions in "stmt".
2814 * "n_ref" points to an integer that contains the sequence number
2815 * of the next reference.
2817 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2819 int i;
2821 if (!stmt)
2822 return NULL;
2824 for (i = 0; i < stmt->n_arg; ++i) {
2825 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2826 if (!stmt->args[i])
2827 return pet_stmt_free(stmt);
2830 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2831 if (!stmt->body)
2832 return pet_stmt_free(stmt);
2834 return stmt;
2837 /* Add a reference identifier to all access expressions in "scop".
2839 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2841 int i;
2842 int n_ref;
2844 if (!scop)
2845 return NULL;
2847 n_ref = 0;
2848 for (i = 0; i < scop->n_stmt; ++i) {
2849 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2850 if (!scop->stmts[i])
2851 return pet_scop_free(scop);
2854 return scop;
2857 /* Reset the user pointer on all parameter ids in "array".
2859 static struct pet_array *array_anonymize(struct pet_array *array)
2861 if (!array)
2862 return NULL;
2864 array->context = isl_set_reset_user(array->context);
2865 array->extent = isl_set_reset_user(array->extent);
2866 if (!array->context || !array->extent)
2867 return pet_array_free(array);
2869 return array;
2872 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2874 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2876 int i;
2877 isl_space *space;
2878 isl_set *domain;
2880 if (!stmt)
2881 return NULL;
2883 stmt->domain = isl_set_reset_user(stmt->domain);
2884 if (!stmt->domain)
2885 return pet_stmt_free(stmt);
2887 for (i = 0; i < stmt->n_arg; ++i) {
2888 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2889 if (!stmt->args[i])
2890 return pet_stmt_free(stmt);
2893 stmt->body = pet_tree_anonymize(stmt->body);
2894 if (!stmt->body)
2895 return pet_stmt_free(stmt);
2897 return stmt;
2900 /* Reset the user pointer on the tuple ids and all parameter ids
2901 * in "implication".
2903 static struct pet_implication *implication_anonymize(
2904 struct pet_implication *implication)
2906 if (!implication)
2907 return NULL;
2909 implication->extension = isl_map_reset_user(implication->extension);
2910 if (!implication->extension)
2911 return pet_implication_free(implication);
2913 return implication;
2916 /* Reset the user pointer on the tuple ids and all parameter ids
2917 * in "independence".
2919 static struct pet_independence *independence_anonymize(
2920 struct pet_independence *independence)
2922 if (!independence)
2923 return NULL;
2925 independence->filter = isl_union_map_reset_user(independence->filter);
2926 independence->local = isl_union_set_reset_user(independence->local);
2927 if (!independence->filter || !independence->local)
2928 return pet_independence_free(independence);
2930 return independence;
2933 /* Reset the user pointer on all parameter and tuple ids in "scop".
2935 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2937 int i;
2939 if (!scop)
2940 return NULL;
2942 scop->context = isl_set_reset_user(scop->context);
2943 scop->context_value = isl_set_reset_user(scop->context_value);
2944 scop->schedule = isl_schedule_reset_user(scop->schedule);
2945 if (!scop->context || !scop->context_value || !scop->schedule)
2946 return pet_scop_free(scop);
2948 for (i = 0; i < scop->n_array; ++i) {
2949 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2950 if (!scop->arrays[i])
2951 return pet_scop_free(scop);
2954 for (i = 0; i < scop->n_stmt; ++i) {
2955 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2956 if (!scop->stmts[i])
2957 return pet_scop_free(scop);
2960 for (i = 0; i < scop->n_implication; ++i) {
2961 scop->implications[i] =
2962 implication_anonymize(scop->implications[i]);
2963 if (!scop->implications[i])
2964 return pet_scop_free(scop);
2967 for (i = 0; i < scop->n_independence; ++i) {
2968 scop->independences[i] =
2969 independence_anonymize(scop->independences[i]);
2970 if (!scop->independences[i])
2971 return pet_scop_free(scop);
2974 return scop;
2977 /* Compute the gist of the iteration domain and all access relations
2978 * of "stmt" based on the constraints on the parameters specified by "context"
2979 * and the constraints on the values of nested accesses specified
2980 * by "value_bounds".
2982 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2983 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2985 int i;
2986 isl_set *domain;
2988 if (!stmt)
2989 return NULL;
2991 domain = isl_set_copy(stmt->domain);
2992 if (stmt->n_arg > 0)
2993 domain = isl_map_domain(isl_set_unwrap(domain));
2995 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2997 for (i = 0; i < stmt->n_arg; ++i) {
2998 stmt->args[i] = pet_expr_gist(stmt->args[i],
2999 domain, value_bounds);
3000 if (!stmt->args[i])
3001 goto error;
3004 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
3005 if (!stmt->body)
3006 goto error;
3008 isl_set_free(domain);
3010 domain = isl_set_universe(pet_stmt_get_space(stmt));
3011 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3012 if (stmt->n_arg > 0)
3013 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3014 value_bounds);
3015 stmt->domain = isl_set_gist(stmt->domain, domain);
3016 if (!stmt->domain)
3017 return pet_stmt_free(stmt);
3019 return stmt;
3020 error:
3021 isl_set_free(domain);
3022 return pet_stmt_free(stmt);
3025 /* Compute the gist of the extent of the array
3026 * based on the constraints on the parameters specified by "context".
3028 static struct pet_array *array_gist(struct pet_array *array,
3029 __isl_keep isl_set *context)
3031 if (!array)
3032 return NULL;
3034 array->extent = isl_set_gist_params(array->extent,
3035 isl_set_copy(context));
3036 if (!array->extent)
3037 return pet_array_free(array);
3039 return array;
3042 /* Compute the gist of all sets and relations in "scop"
3043 * based on the constraints on the parameters specified by "scop->context"
3044 * and the constraints on the values of nested accesses specified
3045 * by "value_bounds".
3047 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3048 __isl_keep isl_union_map *value_bounds)
3050 int i;
3052 if (!scop)
3053 return NULL;
3055 scop->context = isl_set_coalesce(scop->context);
3056 if (!scop->context)
3057 return pet_scop_free(scop);
3059 for (i = 0; i < scop->n_array; ++i) {
3060 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3061 if (!scop->arrays[i])
3062 return pet_scop_free(scop);
3065 for (i = 0; i < scop->n_stmt; ++i) {
3066 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3067 value_bounds);
3068 if (!scop->stmts[i])
3069 return pet_scop_free(scop);
3072 return scop;
3075 /* Intersect the context of "scop" with "context".
3076 * To ensure that we don't introduce any unnamed parameters in
3077 * the context of "scop", we first remove the unnamed parameters
3078 * from "context".
3080 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3081 __isl_take isl_set *context)
3083 if (!scop)
3084 goto error;
3086 context = pet_nested_remove_from_set(context);
3087 scop->context = isl_set_intersect(scop->context, context);
3088 if (!scop->context)
3089 return pet_scop_free(scop);
3091 return scop;
3092 error:
3093 isl_set_free(context);
3094 return pet_scop_free(scop);
3097 /* Drop the current context of "scop". That is, replace the context
3098 * by a universal set.
3100 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3102 isl_space *space;
3104 if (!scop)
3105 return NULL;
3107 space = isl_set_get_space(scop->context);
3108 isl_set_free(scop->context);
3109 scop->context = isl_set_universe(space);
3110 if (!scop->context)
3111 return pet_scop_free(scop);
3113 return scop;
3116 /* Append "array" to the arrays of "scop".
3118 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3119 struct pet_array *array)
3121 isl_ctx *ctx;
3122 struct pet_array **arrays;
3124 if (!array || !scop)
3125 goto error;
3127 ctx = isl_set_get_ctx(scop->context);
3128 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3129 scop->n_array + 1);
3130 if (!arrays)
3131 goto error;
3132 scop->arrays = arrays;
3133 scop->arrays[scop->n_array] = array;
3134 scop->n_array++;
3136 return scop;
3137 error:
3138 pet_array_free(array);
3139 return pet_scop_free(scop);
3142 /* Create an index expression for an access to a virtual array
3143 * representing the result of a condition.
3144 * Unlike other accessed data, the id of the array is NULL as
3145 * there is no ValueDecl in the program corresponding to the virtual
3146 * array.
3147 * The index expression is created as an identity mapping on "space".
3148 * That is, the dimension of the array is the same as that of "space".
3150 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3151 int test_nr)
3153 isl_id *id;
3154 char name[50];
3156 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3157 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3158 space = isl_space_map_from_set(space);
3159 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3160 return isl_multi_pw_aff_identity(space);
3163 /* Add an array with the given extent to the list
3164 * of arrays in "scop" and return the extended pet_scop.
3165 * Specifically, the extent is determined by the image of "domain"
3166 * under "index".
3167 * "int_size" is the number of bytes needed to represent values of type "int".
3168 * The array is marked as attaining values 0 and 1 only and
3169 * as each element being assigned at most once.
3171 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3172 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3173 int int_size)
3175 isl_ctx *ctx;
3176 isl_space *space;
3177 struct pet_array *array;
3178 isl_map *access;
3180 if (!scop || !domain || !index)
3181 goto error;
3183 ctx = isl_multi_pw_aff_get_ctx(index);
3184 array = isl_calloc_type(ctx, struct pet_array);
3185 if (!array)
3186 goto error;
3188 access = isl_map_from_multi_pw_aff(index);
3189 access = isl_map_intersect_domain(access, domain);
3190 array->extent = isl_map_range(access);
3191 space = isl_space_params_alloc(ctx, 0);
3192 array->context = isl_set_universe(space);
3193 space = isl_space_set_alloc(ctx, 0, 1);
3194 array->value_bounds = isl_set_universe(space);
3195 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3196 isl_dim_set, 0, 0);
3197 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3198 isl_dim_set, 0, 1);
3199 array->element_type = strdup("int");
3200 array->element_size = int_size;
3201 array->uniquely_defined = 1;
3203 if (!array->extent || !array->context)
3204 array = pet_array_free(array);
3206 scop = pet_scop_add_array(scop, array);
3208 return scop;
3209 error:
3210 isl_set_free(domain);
3211 isl_multi_pw_aff_free(index);
3212 return pet_scop_free(scop);
3215 /* Create and return an implication on filter values equal to "satisfied"
3216 * with extension "map".
3218 static struct pet_implication *new_implication(__isl_take isl_map *map,
3219 int satisfied)
3221 isl_ctx *ctx;
3222 struct pet_implication *implication;
3224 if (!map)
3225 return NULL;
3226 ctx = isl_map_get_ctx(map);
3227 implication = isl_alloc_type(ctx, struct pet_implication);
3228 if (!implication)
3229 goto error;
3231 implication->extension = map;
3232 implication->satisfied = satisfied;
3234 return implication;
3235 error:
3236 isl_map_free(map);
3237 return NULL;
3240 /* Add an implication on filter values equal to "satisfied"
3241 * with extension "map" to "scop".
3243 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3244 __isl_take isl_map *map, int satisfied)
3246 isl_ctx *ctx;
3247 struct pet_implication *implication;
3248 struct pet_implication **implications;
3250 implication = new_implication(map, satisfied);
3251 if (!scop || !implication)
3252 goto error;
3254 ctx = isl_set_get_ctx(scop->context);
3255 implications = isl_realloc_array(ctx, scop->implications,
3256 struct pet_implication *,
3257 scop->n_implication + 1);
3258 if (!implications)
3259 goto error;
3260 scop->implications = implications;
3261 scop->implications[scop->n_implication] = implication;
3262 scop->n_implication++;
3264 return scop;
3265 error:
3266 pet_implication_free(implication);
3267 return pet_scop_free(scop);
3270 /* Create and return a function that maps the iteration domains
3271 * of the statements in "scop" onto their outer "n" dimensions.
3272 * "space" is the parameters space of the created function.
3274 static __isl_give isl_union_pw_multi_aff *outer_projection(
3275 struct pet_scop *scop, __isl_take isl_space *space, int n)
3277 int i;
3278 isl_union_pw_multi_aff *res;
3280 res = isl_union_pw_multi_aff_empty(space);
3282 if (!scop)
3283 return isl_union_pw_multi_aff_free(res);
3285 for (i = 0; i < scop->n_stmt; ++i) {
3286 struct pet_stmt *stmt = scop->stmts[i];
3287 isl_space *space;
3288 isl_multi_aff *ma;
3289 isl_pw_multi_aff *pma;
3291 space = pet_stmt_get_space(stmt);
3292 ma = pet_prefix_projection(space, n);
3293 pma = isl_pw_multi_aff_from_multi_aff(ma);
3294 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3297 return res;
3300 /* Add an independence to "scop" for the inner iterator of "domain"
3301 * with local variables "local", where "domain" represents the outer
3302 * loop iterators of all statements in "scop".
3303 * If "sign" is positive, then the inner iterator increases.
3304 * Otherwise it decreases.
3306 * The independence is supposed to filter out any dependence of
3307 * an iteration of domain on a previous iteration along the inner dimension.
3308 * We therefore create a mapping from an iteration to later iterations and
3309 * then plug in the projection of the iterations domains of "scop"
3310 * onto the outer loop iterators.
3312 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3313 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3315 int i, dim;
3316 isl_space *space;
3317 isl_map *map;
3318 isl_union_map *independence;
3319 isl_union_pw_multi_aff *proj;
3321 if (!scop || !domain || !local)
3322 goto error;
3324 dim = isl_set_dim(domain, isl_dim_set);
3325 space = isl_space_map_from_set(isl_set_get_space(domain));
3326 map = isl_map_universe(space);
3327 for (i = 0; i + 1 < dim; ++i)
3328 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3329 if (sign > 0)
3330 map = isl_map_order_lt(map,
3331 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3332 else
3333 map = isl_map_order_gt(map,
3334 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3336 independence = isl_union_map_from_map(map);
3337 space = isl_space_params(isl_set_get_space(domain));
3338 proj = outer_projection(scop, space, dim);
3339 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3340 independence, isl_union_pw_multi_aff_copy(proj));
3341 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3342 independence, proj);
3344 scop = pet_scop_add_independence(scop, independence, local);
3346 return scop;
3347 error:
3348 isl_union_set_free(local);
3349 return pet_scop_free(scop);
3352 /* Given an access expression, check if it is data dependent.
3353 * If so, set *found and abort the search.
3355 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3357 int *found = user;
3359 if (pet_expr_get_n_arg(expr) > 0) {
3360 *found = 1;
3361 return -1;
3364 return 0;
3367 /* Does "scop" contain any data dependent accesses?
3369 * Check the body of each statement for such accesses.
3371 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3373 int i;
3374 int found = 0;
3376 if (!scop)
3377 return -1;
3379 for (i = 0; i < scop->n_stmt; ++i) {
3380 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3381 &is_data_dependent, &found);
3382 if (r < 0 && !found)
3383 return -1;
3384 if (found)
3385 return found;
3388 return found;
3391 /* Does "scop" contain and data dependent conditions?
3393 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3395 int i;
3397 if (!scop)
3398 return -1;
3400 for (i = 0; i < scop->n_stmt; ++i)
3401 if (scop->stmts[i]->n_arg > 0)
3402 return 1;
3404 return 0;
3407 /* Keep track of the "input" file inside the (extended) "scop".
3409 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3411 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3413 if (!scop)
3414 return NULL;
3416 ext->input = input;
3418 return scop;
3421 /* Print the original code corresponding to "scop" to printer "p".
3423 * pet_scop_print_original can only be called from
3424 * a pet_transform_C_source callback. This means that the input
3425 * file is stored in the extended scop and that the printer prints
3426 * to a file.
3428 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3429 __isl_take isl_printer *p)
3431 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3432 FILE *output;
3433 unsigned start, end;
3435 if (!scop || !p)
3436 return isl_printer_free(p);
3438 if (!ext->input)
3439 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3440 "no input file stored in scop",
3441 return isl_printer_free(p));
3443 output = isl_printer_get_file(p);
3444 if (!output)
3445 return isl_printer_free(p);
3447 start = pet_loc_get_start(scop->loc);
3448 end = pet_loc_get_end(scop->loc);
3449 if (copy(ext->input, output, start, end) < 0)
3450 return isl_printer_free(p);
3452 return p;