PetScan::get_array_size: take isl_id representing array as input
[pet.git] / scop.c
blobad05baf561f6cd59861e6c9056d4991819711136
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 isl_space_free(space);
491 return NULL;
494 set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
495 domain = isl_union_set_from_set(set);
496 schedule = isl_schedule_from_domain(domain);
498 scop = scop_alloc(space, 1, schedule);
499 if (!scop)
500 goto error;
502 scop->context = stmt_extract_context(stmt, scop->context);
503 if (!scop->context)
504 goto error;
506 scop->stmts[0] = stmt;
507 scop->loc = pet_loc_copy(stmt->loc);
509 if (!scop->loc)
510 return pet_scop_free(scop);
512 return scop;
513 error:
514 pet_stmt_free(stmt);
515 pet_scop_free(scop);
516 return NULL;
519 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
520 * does it represent an affine expression?
522 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
524 int has_id;
526 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
527 if (has_id < 0)
528 return -1;
530 return !has_id;
533 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
535 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
536 __isl_take isl_set *dom)
538 isl_pw_aff *pa;
539 pa = isl_set_indicator_function(set);
540 pa = isl_pw_aff_intersect_domain(pa, dom);
541 return pa;
544 /* Return "lhs || rhs", defined on the shared definition domain.
546 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
547 __isl_take isl_pw_aff *rhs)
549 isl_set *cond;
550 isl_set *dom;
552 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
553 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
554 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
555 isl_pw_aff_non_zero_set(rhs));
556 cond = isl_set_coalesce(cond);
557 return indicator_function(cond, dom);
560 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
561 * ext may be equal to either ext1 or ext2.
563 * The two skips that need to be combined are assumed to be affine expressions.
565 * We need to skip in ext if we need to skip in either ext1 or ext2.
566 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
568 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
569 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
570 enum pet_skip type)
572 isl_pw_aff *skip, *skip1, *skip2;
574 if (!ext)
575 return NULL;
576 if (!ext1->skip[type] && !ext2->skip[type])
577 return ext;
578 if (!ext1->skip[type]) {
579 if (ext == ext2)
580 return ext;
581 ext->skip[type] = ext2->skip[type];
582 ext2->skip[type] = NULL;
583 return ext;
585 if (!ext2->skip[type]) {
586 if (ext == ext1)
587 return ext;
588 ext->skip[type] = ext1->skip[type];
589 ext1->skip[type] = NULL;
590 return ext;
593 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
594 !multi_pw_aff_is_affine(ext2->skip[type]))
595 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
596 isl_error_internal, "can only combine affine skips",
597 goto error);
599 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
600 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
601 skip = pw_aff_or(skip1, skip2);
602 isl_multi_pw_aff_free(ext1->skip[type]);
603 ext1->skip[type] = NULL;
604 isl_multi_pw_aff_free(ext2->skip[type]);
605 ext2->skip[type] = NULL;
606 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
607 if (!ext->skip[type])
608 goto error;
610 return ext;
611 error:
612 pet_scop_free(&ext->scop);
613 return NULL;
616 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
617 * where type takes on the values pet_skip_now and pet_skip_later.
618 * scop may be equal to either scop1 or scop2.
620 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
621 struct pet_scop *scop1, struct pet_scop *scop2)
623 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
624 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
625 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
627 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
628 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
629 return &ext->scop;
632 /* Update start and end of scop->loc to include the region from "start"
633 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
634 * does not have any offset information yet and we simply take the information
635 * from "start" and "end". Otherwise, we update loc using "start" and "end".
637 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
638 unsigned start, unsigned end)
640 if (!scop)
641 return NULL;
643 if (scop->loc == &pet_loc_dummy)
644 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
645 start, end, -1, strdup(""));
646 else
647 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
649 if (!scop->loc)
650 return pet_scop_free(scop);
652 return scop;
655 /* Update start and end of scop->loc to include the region identified
656 * by "loc".
658 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
659 __isl_keep pet_loc *loc)
661 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
662 pet_loc_get_end(loc));
665 /* Replace the location of "scop" by "loc".
667 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
668 __isl_take pet_loc *loc)
670 if (!scop || !loc)
671 goto error;
673 pet_loc_free(scop->loc);
674 scop->loc = loc;
676 return scop;
677 error:
678 pet_loc_free(loc);
679 pet_scop_free(scop);
680 return NULL;
683 /* Does "implication" appear in the list of implications of "scop"?
685 static int is_known_implication(struct pet_scop *scop,
686 struct pet_implication *implication)
688 int i;
690 for (i = 0; i < scop->n_implication; ++i) {
691 struct pet_implication *pi = scop->implications[i];
692 int equal;
694 if (pi->satisfied != implication->satisfied)
695 continue;
696 equal = isl_map_is_equal(pi->extension, implication->extension);
697 if (equal < 0)
698 return -1;
699 if (equal)
700 return 1;
703 return 0;
706 /* Store the concatenation of the implications of "scop1" and "scop2"
707 * in "scop", removing duplicates (i.e., implications in "scop2" that
708 * already appear in "scop1").
710 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
711 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
713 int i, j;
715 if (!scop)
716 return NULL;
718 if (scop2->n_implication == 0) {
719 scop->n_implication = scop1->n_implication;
720 scop->implications = scop1->implications;
721 scop1->n_implication = 0;
722 scop1->implications = NULL;
723 return scop;
726 if (scop1->n_implication == 0) {
727 scop->n_implication = scop2->n_implication;
728 scop->implications = scop2->implications;
729 scop2->n_implication = 0;
730 scop2->implications = NULL;
731 return scop;
734 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
735 scop1->n_implication + scop2->n_implication);
736 if (!scop->implications)
737 return pet_scop_free(scop);
739 for (i = 0; i < scop1->n_implication; ++i) {
740 scop->implications[i] = scop1->implications[i];
741 scop1->implications[i] = NULL;
744 scop->n_implication = scop1->n_implication;
745 j = scop1->n_implication;
746 for (i = 0; i < scop2->n_implication; ++i) {
747 int known;
749 known = is_known_implication(scop, scop2->implications[i]);
750 if (known < 0)
751 return pet_scop_free(scop);
752 if (known)
753 continue;
754 scop->implications[j++] = scop2->implications[i];
755 scop2->implications[i] = NULL;
757 scop->n_implication = j;
759 return scop;
762 /* Combine the offset information of "scop1" and "scop2" into "scop".
764 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
765 struct pet_scop *scop1, struct pet_scop *scop2)
767 if (scop1->loc != &pet_loc_dummy)
768 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
769 if (scop2->loc != &pet_loc_dummy)
770 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
771 return scop;
774 /* Create and return an independence that filters out the dependences
775 * in "filter" with local variables "local".
777 static struct pet_independence *new_independence(
778 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
780 isl_ctx *ctx;
781 struct pet_independence *independence;
783 if (!filter || !local)
784 goto error;
785 ctx = isl_union_map_get_ctx(filter);
786 independence = isl_alloc_type(ctx, struct pet_independence);
787 if (!independence)
788 goto error;
790 independence->filter = filter;
791 independence->local = local;
793 return independence;
794 error:
795 isl_union_map_free(filter);
796 isl_union_set_free(local);
797 return NULL;
800 /* Add an independence that filters out the dependences
801 * in "filter" with local variables "local" to "scop".
803 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
804 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
806 isl_ctx *ctx;
807 struct pet_independence *independence;
808 struct pet_independence **independences;
810 ctx = isl_union_map_get_ctx(filter);
811 independence = new_independence(filter, local);
812 if (!scop || !independence)
813 goto error;
815 independences = isl_realloc_array(ctx, scop->independences,
816 struct pet_independence *,
817 scop->n_independence + 1);
818 if (!independences)
819 goto error;
820 scop->independences = independences;
821 scop->independences[scop->n_independence] = independence;
822 scop->n_independence++;
824 return scop;
825 error:
826 pet_independence_free(independence);
827 pet_scop_free(scop);
828 return NULL;
831 /* Store the concatenation of the independences of "scop1" and "scop2"
832 * in "scop".
834 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
835 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
837 int i, off;
839 if (!scop)
840 return NULL;
842 if (scop2->n_independence == 0) {
843 scop->n_independence = scop1->n_independence;
844 scop->independences = scop1->independences;
845 scop1->n_independence = 0;
846 scop1->independences = NULL;
847 return scop;
850 if (scop1->n_independence == 0) {
851 scop->n_independence = scop2->n_independence;
852 scop->independences = scop2->independences;
853 scop2->n_independence = 0;
854 scop2->independences = NULL;
855 return scop;
858 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
859 scop1->n_independence + scop2->n_independence);
860 if (!scop->independences)
861 return pet_scop_free(scop);
863 for (i = 0; i < scop1->n_independence; ++i) {
864 scop->independences[i] = scop1->independences[i];
865 scop1->independences[i] = NULL;
868 off = scop1->n_independence;
869 for (i = 0; i < scop2->n_independence; ++i) {
870 scop->independences[off + i] = scop2->independences[i];
871 scop2->independences[i] = NULL;
873 scop->n_independence = scop1->n_independence + scop2->n_independence;
875 return scop;
878 /* Construct a pet_scop with the given schedule
879 * that contains the offset information,
880 * arrays, statements and skip information in "scop1" and "scop2".
882 static struct pet_scop *pet_scop_add(isl_ctx *ctx,
883 __isl_take isl_schedule *schedule, struct pet_scop *scop1,
884 struct pet_scop *scop2)
886 int i;
887 isl_space *space;
888 struct pet_scop *scop = NULL;
890 if (!scop1 || !scop2)
891 goto error;
893 if (scop1->n_stmt == 0) {
894 scop2 = scop_combine_skips(scop2, scop1, scop2);
895 pet_scop_free(scop1);
896 isl_schedule_free(schedule);
897 return scop2;
900 if (scop2->n_stmt == 0) {
901 scop1 = scop_combine_skips(scop1, scop1, scop2);
902 pet_scop_free(scop2);
903 isl_schedule_free(schedule);
904 return scop1;
907 space = isl_set_get_space(scop1->context);
908 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
909 isl_schedule_copy(schedule));
910 if (!scop)
911 goto error;
913 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
914 scop1->n_array + scop2->n_array);
915 if (!scop->arrays)
916 goto error;
917 scop->n_array = scop1->n_array + scop2->n_array;
919 for (i = 0; i < scop1->n_stmt; ++i) {
920 scop->stmts[i] = scop1->stmts[i];
921 scop1->stmts[i] = NULL;
924 for (i = 0; i < scop2->n_stmt; ++i) {
925 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
926 scop2->stmts[i] = NULL;
929 for (i = 0; i < scop1->n_array; ++i) {
930 scop->arrays[i] = scop1->arrays[i];
931 scop1->arrays[i] = NULL;
934 for (i = 0; i < scop2->n_array; ++i) {
935 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
936 scop2->arrays[i] = NULL;
939 scop = scop_collect_implications(ctx, scop, scop1, scop2);
940 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
941 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
942 scop = scop_combine_skips(scop, scop1, scop2);
943 scop = scop_combine_start_end(scop, scop1, scop2);
944 scop = scop_collect_independences(ctx, scop, scop1, scop2);
946 pet_scop_free(scop1);
947 pet_scop_free(scop2);
948 isl_schedule_free(schedule);
949 return scop;
950 error:
951 pet_scop_free(scop1);
952 pet_scop_free(scop2);
953 pet_scop_free(scop);
954 isl_schedule_free(schedule);
955 return NULL;
958 /* Apply the skip condition "skip" to "scop".
959 * That is, make sure "scop" is not executed when the condition holds.
961 * If "skip" is an affine expression, we add the conditions under
962 * which the expression is zero to the context and the skip conditions
963 * of "scop".
964 * Otherwise, we add a filter on the variable attaining the value zero.
966 static struct pet_scop *restrict_skip(struct pet_scop *scop,
967 __isl_take isl_multi_pw_aff *skip)
969 isl_set *zero;
970 isl_pw_aff *pa;
971 int is_aff;
973 if (!scop || !skip)
974 goto error;
976 is_aff = multi_pw_aff_is_affine(skip);
977 if (is_aff < 0)
978 goto error;
980 if (!is_aff)
981 return pet_scop_filter(scop, skip, 0);
983 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
984 isl_multi_pw_aff_free(skip);
985 zero = isl_pw_aff_zero_set(pa);
986 scop = pet_scop_restrict(scop, zero);
988 return scop;
989 error:
990 isl_multi_pw_aff_free(skip);
991 return pet_scop_free(scop);
994 /* Construct a pet_scop that contains the arrays, statements and
995 * skip information in "scop1" and "scop2", where the two scops
996 * are executed "in sequence". That is, breaks and continues
997 * in scop1 have an effect on scop2 and the schedule of the result
998 * is the sequence of the schedules of "scop1" and "scop2".
1000 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1001 struct pet_scop *scop2)
1003 isl_schedule *schedule;
1005 if (!scop1 || !scop2)
1006 goto error;
1008 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1009 scop2 = restrict_skip(scop2,
1010 pet_scop_get_skip(scop1, pet_skip_now));
1011 schedule = isl_schedule_sequence(isl_schedule_copy(scop1->schedule),
1012 isl_schedule_copy(scop2->schedule));
1013 return pet_scop_add(ctx, schedule, scop1, scop2);
1014 error:
1015 pet_scop_free(scop1);
1016 pet_scop_free(scop2);
1017 return NULL;
1020 /* Construct a pet_scop that contains the arrays, statements and
1021 * skip information in "scop1" and "scop2", where the two scops
1022 * are executed "in parallel". That is, any break or continue
1023 * in scop1 has no effect on scop2 and the schedule of the result
1024 * is the set of the schedules of "scop1" and "scop2".
1026 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1027 struct pet_scop *scop2)
1029 isl_schedule *schedule;
1031 if (!scop1 || !scop2)
1032 goto error;
1034 schedule = isl_schedule_set(isl_schedule_copy(scop1->schedule),
1035 isl_schedule_copy(scop2->schedule));
1036 return pet_scop_add(ctx, schedule, scop1, scop2);
1037 error:
1038 pet_scop_free(scop1);
1039 pet_scop_free(scop2);
1040 return NULL;
1043 void *pet_implication_free(struct pet_implication *implication)
1045 int i;
1047 if (!implication)
1048 return NULL;
1050 isl_map_free(implication->extension);
1052 free(implication);
1053 return NULL;
1056 void *pet_independence_free(struct pet_independence *independence)
1058 if (!independence)
1059 return NULL;
1061 isl_union_map_free(independence->filter);
1062 isl_union_set_free(independence->local);
1064 free(independence);
1065 return NULL;
1068 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1070 int i;
1071 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1073 if (!scop)
1074 return NULL;
1075 pet_loc_free(scop->loc);
1076 isl_set_free(scop->context);
1077 isl_set_free(scop->context_value);
1078 isl_schedule_free(scop->schedule);
1079 if (scop->types)
1080 for (i = 0; i < scop->n_type; ++i)
1081 pet_type_free(scop->types[i]);
1082 free(scop->types);
1083 if (scop->arrays)
1084 for (i = 0; i < scop->n_array; ++i)
1085 pet_array_free(scop->arrays[i]);
1086 free(scop->arrays);
1087 if (scop->stmts)
1088 for (i = 0; i < scop->n_stmt; ++i)
1089 pet_stmt_free(scop->stmts[i]);
1090 free(scop->stmts);
1091 if (scop->implications)
1092 for (i = 0; i < scop->n_implication; ++i)
1093 pet_implication_free(scop->implications[i]);
1094 free(scop->implications);
1095 if (scop->independences)
1096 for (i = 0; i < scop->n_independence; ++i)
1097 pet_independence_free(scop->independences[i]);
1098 free(scop->independences);
1099 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1100 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1101 free(scop);
1102 return NULL;
1105 void pet_type_dump(struct pet_type *type)
1107 if (!type)
1108 return;
1110 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1113 void pet_implication_dump(struct pet_implication *implication)
1115 if (!implication)
1116 return;
1118 fprintf(stderr, "%d\n", implication->satisfied);
1119 isl_map_dump(implication->extension);
1122 void pet_scop_dump(struct pet_scop *scop)
1124 int i;
1125 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1127 if (!scop)
1128 return;
1130 isl_set_dump(scop->context);
1131 isl_set_dump(scop->context_value);
1132 isl_schedule_dump(scop->schedule);
1133 for (i = 0; i < scop->n_type; ++i)
1134 pet_type_dump(scop->types[i]);
1135 for (i = 0; i < scop->n_array; ++i)
1136 pet_array_dump(scop->arrays[i]);
1137 for (i = 0; i < scop->n_stmt; ++i)
1138 pet_stmt_dump(scop->stmts[i]);
1139 for (i = 0; i < scop->n_implication; ++i)
1140 pet_implication_dump(scop->implications[i]);
1142 if (ext->skip[0]) {
1143 fprintf(stderr, "skip\n");
1144 isl_multi_pw_aff_dump(ext->skip[0]);
1145 isl_multi_pw_aff_dump(ext->skip[1]);
1149 /* Return 1 if the two pet_arrays are equivalent.
1151 * We don't compare element_size as this may be target dependent.
1153 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1155 if (!array1 || !array2)
1156 return 0;
1158 if (!isl_set_is_equal(array1->context, array2->context))
1159 return 0;
1160 if (!isl_set_is_equal(array1->extent, array2->extent))
1161 return 0;
1162 if (!!array1->value_bounds != !!array2->value_bounds)
1163 return 0;
1164 if (array1->value_bounds &&
1165 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1166 return 0;
1167 if (strcmp(array1->element_type, array2->element_type))
1168 return 0;
1169 if (array1->element_is_record != array2->element_is_record)
1170 return 0;
1171 if (array1->live_out != array2->live_out)
1172 return 0;
1173 if (array1->uniquely_defined != array2->uniquely_defined)
1174 return 0;
1175 if (array1->declared != array2->declared)
1176 return 0;
1177 if (array1->exposed != array2->exposed)
1178 return 0;
1180 return 1;
1183 /* Return 1 if the two pet_stmts are equivalent.
1185 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1187 int i;
1189 if (!stmt1 || !stmt2)
1190 return 0;
1192 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1193 return 0;
1194 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1195 return 0;
1196 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1197 return 0;
1198 if (stmt1->n_arg != stmt2->n_arg)
1199 return 0;
1200 for (i = 0; i < stmt1->n_arg; ++i) {
1201 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1202 return 0;
1205 return 1;
1208 /* Return 1 if the two pet_types are equivalent.
1210 * We only compare the names of the types since the exact representation
1211 * of the definition may depend on the version of clang being used.
1213 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1215 if (!type1 || !type2)
1216 return 0;
1218 if (strcmp(type1->name, type2->name))
1219 return 0;
1221 return 1;
1224 /* Return 1 if the two pet_implications are equivalent.
1226 int pet_implication_is_equal(struct pet_implication *implication1,
1227 struct pet_implication *implication2)
1229 if (!implication1 || !implication2)
1230 return 0;
1232 if (implication1->satisfied != implication2->satisfied)
1233 return 0;
1234 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1235 return 0;
1237 return 1;
1240 /* Return 1 if the two pet_independences are equivalent.
1242 int pet_independence_is_equal(struct pet_independence *independence1,
1243 struct pet_independence *independence2)
1245 if (!independence1 || !independence2)
1246 return 0;
1248 if (!isl_union_map_is_equal(independence1->filter,
1249 independence2->filter))
1250 return 0;
1251 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1252 return 0;
1254 return 1;
1257 /* Return 1 if the two pet_scops are equivalent.
1259 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1261 int i;
1262 int equal;
1264 if (!scop1 || !scop2)
1265 return 0;
1267 if (!isl_set_is_equal(scop1->context, scop2->context))
1268 return 0;
1269 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1270 return 0;
1271 equal = isl_schedule_plain_is_equal(scop1->schedule, scop2->schedule);
1272 if (equal < 0)
1273 return -1;
1274 if (!equal)
1275 return 0;
1277 if (scop1->n_type != scop2->n_type)
1278 return 0;
1279 for (i = 0; i < scop1->n_type; ++i)
1280 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1281 return 0;
1283 if (scop1->n_array != scop2->n_array)
1284 return 0;
1285 for (i = 0; i < scop1->n_array; ++i)
1286 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1287 return 0;
1289 if (scop1->n_stmt != scop2->n_stmt)
1290 return 0;
1291 for (i = 0; i < scop1->n_stmt; ++i)
1292 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1293 return 0;
1295 if (scop1->n_implication != scop2->n_implication)
1296 return 0;
1297 for (i = 0; i < scop1->n_implication; ++i)
1298 if (!pet_implication_is_equal(scop1->implications[i],
1299 scop2->implications[i]))
1300 return 0;
1302 if (scop1->n_independence != scop2->n_independence)
1303 return 0;
1304 for (i = 0; i < scop1->n_independence; ++i)
1305 if (!pet_independence_is_equal(scop1->independences[i],
1306 scop2->independences[i]))
1307 return 0;
1309 return 1;
1312 /* Does the set "extent" reference a virtual array, i.e.,
1313 * one with user pointer equal to NULL?
1314 * A virtual array does not have any members.
1316 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1318 isl_id *id;
1319 int is_virtual;
1321 if (!isl_set_has_tuple_id(extent))
1322 return 0;
1323 if (isl_set_is_wrapping(extent))
1324 return 0;
1325 id = isl_set_get_tuple_id(extent);
1326 is_virtual = !isl_id_get_user(id);
1327 isl_id_free(id);
1329 return is_virtual;
1332 /* Intersect the initial dimensions of "array" with "domain", provided
1333 * that "array" represents a virtual array.
1335 * If "array" is virtual, then We take the preimage of "domain"
1336 * over the projection of the extent of "array" onto its initial dimensions
1337 * and intersect this extent with the result.
1339 static struct pet_array *virtual_array_intersect_domain_prefix(
1340 struct pet_array *array, __isl_take isl_set *domain)
1342 int n;
1343 isl_space *space;
1344 isl_multi_aff *ma;
1346 if (!array || !extent_is_virtual_array(array->extent)) {
1347 isl_set_free(domain);
1348 return array;
1351 space = isl_set_get_space(array->extent);
1352 n = isl_set_dim(domain, isl_dim_set);
1353 ma = pet_prefix_projection(space, n);
1354 domain = isl_set_preimage_multi_aff(domain, ma);
1356 array->extent = isl_set_intersect(array->extent, domain);
1357 if (!array->extent)
1358 return pet_array_free(array);
1360 return array;
1363 /* Intersect the initial dimensions of the domain of "stmt"
1364 * with "domain".
1366 * We take the preimage of "domain" over the projection of the
1367 * domain of "stmt" onto its initial dimensions and intersect
1368 * the domain of "stmt" with the result.
1370 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1371 __isl_take isl_set *domain)
1373 int n;
1374 isl_space *space;
1375 isl_multi_aff *ma;
1377 if (!stmt)
1378 goto error;
1380 space = isl_set_get_space(stmt->domain);
1381 n = isl_set_dim(domain, isl_dim_set);
1382 ma = pet_prefix_projection(space, n);
1383 domain = isl_set_preimage_multi_aff(domain, ma);
1385 stmt->domain = isl_set_intersect(stmt->domain, domain);
1386 if (!stmt->domain)
1387 return pet_stmt_free(stmt);
1389 return stmt;
1390 error:
1391 isl_set_free(domain);
1392 return pet_stmt_free(stmt);
1395 /* Intersect the initial dimensions of the domain of "implication"
1396 * with "domain".
1398 * We take the preimage of "domain" over the projection of the
1399 * domain of "implication" onto its initial dimensions and intersect
1400 * the domain of "implication" with the result.
1402 static struct pet_implication *implication_intersect_domain_prefix(
1403 struct pet_implication *implication, __isl_take isl_set *domain)
1405 int n;
1406 isl_space *space;
1407 isl_multi_aff *ma;
1409 if (!implication)
1410 goto error;
1412 space = isl_map_get_space(implication->extension);
1413 n = isl_set_dim(domain, isl_dim_set);
1414 ma = pet_prefix_projection(isl_space_domain(space), n);
1415 domain = isl_set_preimage_multi_aff(domain, ma);
1417 implication->extension =
1418 isl_map_intersect_domain(implication->extension, domain);
1419 if (!implication->extension)
1420 return pet_implication_free(implication);
1422 return implication;
1423 error:
1424 isl_set_free(domain);
1425 return pet_implication_free(implication);
1428 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1430 * The extents of the virtual arrays match the iteration domains,
1431 * so if the iteration domain changes, we need to change those extents too.
1433 * The domain of the schedule is intersected with (i.e., replaced by)
1434 * the union of the updated iteration domains.
1436 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1437 __isl_take isl_set *domain)
1439 int i;
1441 if (!scop)
1442 goto error;
1444 for (i = 0; i < scop->n_array; ++i) {
1445 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1446 scop->arrays[i], isl_set_copy(domain));
1447 if (!scop->arrays[i])
1448 goto error;
1451 for (i = 0; i < scop->n_stmt; ++i) {
1452 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1453 isl_set_copy(domain));
1454 if (!scop->stmts[i])
1455 goto error;
1458 for (i = 0; i < scop->n_implication; ++i) {
1459 scop->implications[i] =
1460 implication_intersect_domain_prefix(scop->implications[i],
1461 isl_set_copy(domain));
1462 if (!scop->implications[i])
1463 return pet_scop_free(scop);
1466 scop->schedule = isl_schedule_intersect_domain(scop->schedule,
1467 pet_scop_get_instance_set(scop));
1468 if (!scop->schedule)
1469 goto error;
1471 isl_set_free(domain);
1472 return scop;
1473 error:
1474 isl_set_free(domain);
1475 return pet_scop_free(scop);
1478 /* Update the context with respect to an embedding into a loop
1479 * with iteration domain "dom".
1480 * The input context lives in the same space as "dom".
1481 * The output context has the inner dimension removed.
1483 * An outer loop iterator value is invalid for the embedding if
1484 * any of the corresponding inner iterator values is invalid.
1485 * That is, an outer loop iterator value is valid only if all the corresponding
1486 * inner iterator values are valid.
1487 * We therefore compute the set of outer loop iterators l
1489 * forall i: dom(l,i) => valid(l,i)
1491 * or
1493 * forall i: not dom(l,i) or valid(l,i)
1495 * or
1497 * not exists i: dom(l,i) and not valid(l,i)
1499 * i.e.,
1501 * not exists i: (dom \ valid)(l,i)
1503 * If there are any unnamed parameters in "dom", then we consider
1504 * a parameter value to be valid if it is valid for any value of those
1505 * unnamed parameters. They are therefore projected out at the end.
1507 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1508 __isl_keep isl_set *dom)
1510 int pos;
1512 pos = isl_set_dim(context, isl_dim_set) - 1;
1513 context = isl_set_subtract(isl_set_copy(dom), context);
1514 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1515 context = isl_set_complement(context);
1516 context = pet_nested_remove_from_set(context);
1518 return context;
1521 /* Update the implication with respect to an embedding into a loop
1522 * with iteration domain "dom".
1524 * Since embed_access extends virtual arrays along with the domain
1525 * of the access, we need to do the same with domain and range
1526 * of the implication. Since the original implication is only valid
1527 * within a given iteration of the loop, the extended implication
1528 * maps the extra array dimension corresponding to the extra loop
1529 * to itself.
1531 static struct pet_implication *pet_implication_embed(
1532 struct pet_implication *implication, __isl_take isl_set *dom)
1534 isl_id *id;
1535 isl_map *map;
1537 if (!implication)
1538 goto error;
1540 map = isl_set_identity(dom);
1541 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1542 map = isl_map_flat_product(map, implication->extension);
1543 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1544 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1545 implication->extension = map;
1546 if (!implication->extension)
1547 return pet_implication_free(implication);
1549 return implication;
1550 error:
1551 isl_set_free(dom);
1552 return NULL;
1555 /* Internal data structure for outer_projection_mupa.
1557 * "n" is the number of outer dimensions onto which to project.
1558 * "res" collects the result.
1560 struct pet_outer_projection_data {
1561 int n;
1562 isl_union_pw_multi_aff *res;
1565 /* Create a function that maps "set" onto its outer data->n dimensions and
1566 * add it to data->res.
1568 static isl_stat add_outer_projection(__isl_take isl_set *set, void *user)
1570 struct pet_outer_projection_data *data = user;
1571 int dim;
1572 isl_space *space;
1573 isl_pw_multi_aff *pma;
1575 dim = isl_set_dim(set, isl_dim_set);
1576 space = isl_set_get_space(set);
1577 pma = isl_pw_multi_aff_project_out_map(space,
1578 isl_dim_set, data->n, dim - data->n);
1579 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
1581 isl_set_free(set);
1583 return isl_stat_ok;
1586 /* Create and return a function that maps the sets in "domain"
1587 * onto their outer "n" dimensions.
1589 static __isl_give isl_multi_union_pw_aff *outer_projection_mupa(
1590 __isl_take isl_union_set *domain, int n)
1592 struct pet_outer_projection_data data;
1593 isl_space *space;
1595 space = isl_union_set_get_space(domain);
1596 data.n = n;
1597 data.res = isl_union_pw_multi_aff_empty(space);
1598 if (isl_union_set_foreach_set(domain, &add_outer_projection, &data) < 0)
1599 data.res = isl_union_pw_multi_aff_free(data.res);
1601 isl_union_set_free(domain);
1602 return isl_multi_union_pw_aff_from_union_pw_multi_aff(data.res);
1605 /* Embed "schedule" in a loop with schedule "prefix".
1606 * The domain of "prefix" corresponds to the outer dimensions
1607 * of the iteration domains.
1608 * We therefore construct a projection onto these outer dimensions,
1609 * compose it with "prefix" and then add the result as a band schedule.
1611 * If the domain of the schedule is empty, then there is no need
1612 * to insert any node.
1614 static __isl_give isl_schedule *schedule_embed(
1615 __isl_take isl_schedule *schedule, __isl_keep isl_multi_aff *prefix)
1617 int n;
1618 int empty;
1619 isl_union_set *domain;
1620 isl_multi_aff *ma;
1621 isl_multi_union_pw_aff *mupa;
1623 domain = isl_schedule_get_domain(schedule);
1624 empty = isl_union_set_is_empty(domain);
1625 if (empty < 0 || empty) {
1626 isl_union_set_free(domain);
1627 return empty < 0 ? isl_schedule_free(schedule) : schedule;
1630 n = isl_multi_aff_dim(prefix, isl_dim_in);
1631 mupa = outer_projection_mupa(domain, n);
1632 ma = isl_multi_aff_copy(prefix);
1633 mupa = isl_multi_union_pw_aff_apply_multi_aff(mupa, ma);
1634 schedule = isl_schedule_insert_partial_schedule(schedule, mupa);
1636 return schedule;
1639 /* Adjust the context and the schedule according to an embedding
1640 * in a loop with iteration domain "dom" and schedule "sched".
1642 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1643 __isl_take isl_multi_aff *sched)
1645 int i;
1647 if (!scop)
1648 goto error;
1650 scop->context = context_embed(scop->context, dom);
1651 if (!scop->context)
1652 goto error;
1654 scop->schedule = schedule_embed(scop->schedule, sched);
1655 if (!scop->schedule)
1656 goto error;
1658 isl_set_free(dom);
1659 isl_multi_aff_free(sched);
1660 return scop;
1661 error:
1662 isl_set_free(dom);
1663 isl_multi_aff_free(sched);
1664 return pet_scop_free(scop);
1667 /* Add extra conditions to scop->skip[type].
1669 * The new skip condition only holds if it held before
1670 * and the condition is true. It does not hold if it did not hold
1671 * before or the condition is false.
1673 * The skip condition is assumed to be an affine expression.
1675 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1676 enum pet_skip type, __isl_keep isl_set *cond)
1678 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1679 isl_pw_aff *skip;
1680 isl_set *dom;
1682 if (!scop)
1683 return NULL;
1684 if (!ext->skip[type])
1685 return scop;
1687 if (!multi_pw_aff_is_affine(ext->skip[type]))
1688 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1689 isl_error_internal, "can only restrict affine skips",
1690 return pet_scop_free(scop));
1692 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1693 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1694 cond = isl_set_copy(cond);
1695 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1696 skip = indicator_function(cond, dom);
1697 isl_multi_pw_aff_free(ext->skip[type]);
1698 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1699 if (!ext->skip[type])
1700 return pet_scop_free(scop);
1702 return scop;
1705 /* Adjust the context and the skip conditions to the fact that
1706 * the scop was created in a context where "cond" holds.
1708 * An outer loop iterator or parameter value is valid for the result
1709 * if it was valid for the original scop and satisfies "cond" or if it does
1710 * not satisfy "cond" as in this case the scop is not executed
1711 * and the original constraints on these values are irrelevant.
1713 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1714 __isl_take isl_set *cond)
1716 int i;
1718 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1719 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1721 if (!scop)
1722 goto error;
1724 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1725 scop->context = isl_set_union(scop->context,
1726 isl_set_complement(isl_set_copy(cond)));
1727 scop->context = isl_set_coalesce(scop->context);
1728 scop->context = pet_nested_remove_from_set(scop->context);
1729 if (!scop->context)
1730 goto error;
1732 isl_set_free(cond);
1733 return scop;
1734 error:
1735 isl_set_free(cond);
1736 return pet_scop_free(scop);
1739 /* Insert an argument expression corresponding to "test" in front
1740 * of the list of arguments described by *n_arg and *args.
1742 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1743 __isl_keep isl_multi_pw_aff *test)
1745 int i;
1746 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1748 if (!test)
1749 return -1;
1751 if (!*args) {
1752 *args = isl_calloc_array(ctx, pet_expr *, 1);
1753 if (!*args)
1754 return -1;
1755 } else {
1756 pet_expr **ext;
1757 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1758 if (!ext)
1759 return -1;
1760 for (i = 0; i < *n_arg; ++i)
1761 ext[1 + i] = (*args)[i];
1762 free(*args);
1763 *args = ext;
1765 (*n_arg)++;
1766 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1767 if (!(*args)[0])
1768 return -1;
1770 return 0;
1773 /* Look through the applications in "scop" for any that can be
1774 * applied to the filter expressed by "map" and "satisified".
1775 * If there is any, then apply it to "map" and return the result.
1776 * Otherwise, return "map".
1777 * "id" is the identifier of the virtual array.
1779 * We only introduce at most one implication for any given virtual array,
1780 * so we can apply the implication and return as soon as we find one.
1782 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1783 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1785 int i;
1787 for (i = 0; i < scop->n_implication; ++i) {
1788 struct pet_implication *pi = scop->implications[i];
1789 isl_id *pi_id;
1791 if (pi->satisfied != satisfied)
1792 continue;
1793 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1794 isl_id_free(pi_id);
1795 if (pi_id != id)
1796 continue;
1798 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1801 return map;
1804 /* Is the filter expressed by "test" and "satisfied" implied
1805 * by filter "pos" on "domain", with filter "expr", taking into
1806 * account the implications of "scop"?
1808 * For filter on domain implying that expressed by "test" and "satisfied",
1809 * the filter needs to be an access to the same (virtual) array as "test" and
1810 * the filter value needs to be equal to "satisfied".
1811 * Moreover, the filter access relation, possibly extended by
1812 * the implications in "scop" needs to contain "test".
1814 static int implies_filter(struct pet_scop *scop,
1815 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1816 __isl_keep isl_map *test, int satisfied)
1818 isl_id *test_id, *arg_id;
1819 isl_val *val;
1820 int is_int;
1821 int s;
1822 int is_subset;
1823 isl_map *implied;
1825 if (expr->type != pet_expr_access)
1826 return 0;
1827 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1828 arg_id = pet_expr_access_get_id(expr);
1829 isl_id_free(arg_id);
1830 isl_id_free(test_id);
1831 if (test_id != arg_id)
1832 return 0;
1833 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1834 is_int = isl_val_is_int(val);
1835 if (is_int)
1836 s = isl_val_get_num_si(val);
1837 isl_val_free(val);
1838 if (!val)
1839 return -1;
1840 if (!is_int)
1841 return 0;
1842 if (s != satisfied)
1843 return 0;
1845 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1846 implied = apply_implications(scop, implied, test_id, satisfied);
1847 is_subset = isl_map_is_subset(test, implied);
1848 isl_map_free(implied);
1850 return is_subset;
1853 /* Is the filter expressed by "test" and "satisfied" implied
1854 * by any of the filters on the domain of "stmt", taking into
1855 * account the implications of "scop"?
1857 static int filter_implied(struct pet_scop *scop,
1858 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1860 int i;
1861 int implied;
1862 isl_id *test_id;
1863 isl_map *domain;
1864 isl_map *test_map;
1866 if (!scop || !stmt || !test)
1867 return -1;
1868 if (scop->n_implication == 0)
1869 return 0;
1870 if (stmt->n_arg == 0)
1871 return 0;
1873 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1874 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1876 implied = 0;
1877 for (i = 0; i < stmt->n_arg; ++i) {
1878 implied = implies_filter(scop, domain, i, stmt->args[i],
1879 test_map, satisfied);
1880 if (implied < 0 || implied)
1881 break;
1884 isl_map_free(test_map);
1885 isl_map_free(domain);
1886 return implied;
1889 /* Make the statement "stmt" depend on the value of "test"
1890 * being equal to "satisfied" by adjusting stmt->domain.
1892 * The domain of "test" corresponds to the (zero or more) outer dimensions
1893 * of the iteration domain.
1895 * We first extend "test" to apply to the entire iteration domain and
1896 * then check if the filter that we are about to add is implied
1897 * by any of the current filters, possibly taking into account
1898 * the implications in "scop". If so, we leave "stmt" untouched and return.
1900 * Otherwise, we insert an argument corresponding to a read to "test"
1901 * from the iteration domain of "stmt" in front of the list of arguments.
1902 * We also insert a corresponding output dimension in the wrapped
1903 * map contained in stmt->domain, with value set to "satisfied".
1905 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1906 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1908 int i;
1909 int implied;
1910 isl_id *id;
1911 isl_ctx *ctx;
1912 isl_pw_multi_aff *pma;
1913 isl_multi_aff *add_dom;
1914 isl_space *space;
1915 isl_local_space *ls;
1916 int n_test_dom;
1918 if (!stmt || !test)
1919 goto error;
1921 space = pet_stmt_get_space(stmt);
1922 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1923 space = isl_space_from_domain(space);
1924 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1925 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1926 ls = isl_local_space_from_space(isl_space_domain(space));
1927 for (i = 0; i < n_test_dom; ++i) {
1928 isl_aff *aff;
1929 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1930 isl_dim_set, i);
1931 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1933 isl_local_space_free(ls);
1934 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1936 implied = filter_implied(scop, stmt, test, satisfied);
1937 if (implied < 0)
1938 goto error;
1939 if (implied) {
1940 isl_multi_pw_aff_free(test);
1941 return stmt;
1944 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1945 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1946 id, satisfied);
1947 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1949 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1950 goto error;
1952 isl_multi_pw_aff_free(test);
1953 return stmt;
1954 error:
1955 isl_multi_pw_aff_free(test);
1956 return pet_stmt_free(stmt);
1959 /* Does "scop" have a skip condition of the given "type"?
1961 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1963 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1965 if (!scop)
1966 return -1;
1967 return ext->skip[type] != NULL;
1970 /* Does "scop" have a skip condition of the given "type" that
1971 * is an affine expression?
1973 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1975 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1977 if (!scop)
1978 return -1;
1979 if (!ext->skip[type])
1980 return 0;
1981 return multi_pw_aff_is_affine(ext->skip[type]);
1984 /* Does "scop" have a skip condition of the given "type" that
1985 * is not an affine expression?
1987 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1989 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1990 int aff;
1992 if (!scop)
1993 return -1;
1994 if (!ext->skip[type])
1995 return 0;
1996 aff = multi_pw_aff_is_affine(ext->skip[type]);
1997 if (aff < 0)
1998 return -1;
1999 return !aff;
2002 /* Does "scop" have a skip condition of the given "type" that
2003 * is affine and holds on the entire domain?
2005 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2007 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2008 isl_pw_aff *pa;
2009 isl_set *set;
2010 int is_aff;
2011 int is_univ;
2013 is_aff = pet_scop_has_affine_skip(scop, type);
2014 if (is_aff < 0 || !is_aff)
2015 return is_aff;
2017 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2018 set = isl_pw_aff_non_zero_set(pa);
2019 is_univ = isl_set_plain_is_universe(set);
2020 isl_set_free(set);
2022 return is_univ;
2025 /* Replace scop->skip[type] by "skip".
2027 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2028 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2030 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2032 if (!scop || !skip)
2033 goto error;
2035 isl_multi_pw_aff_free(ext->skip[type]);
2036 ext->skip[type] = skip;
2038 return scop;
2039 error:
2040 isl_multi_pw_aff_free(skip);
2041 return pet_scop_free(scop);
2044 /* Return a copy of scop->skip[type].
2046 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2047 enum pet_skip type)
2049 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2051 if (!scop)
2052 return NULL;
2054 return isl_multi_pw_aff_copy(ext->skip[type]);
2057 /* Assuming scop->skip[type] is an affine expression,
2058 * return the constraints on the outer loop domain for which the skip condition
2059 * holds.
2061 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2062 enum pet_skip type)
2064 isl_multi_pw_aff *skip;
2065 isl_pw_aff *pa;
2067 skip = pet_scop_get_skip(scop, type);
2068 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2069 isl_multi_pw_aff_free(skip);
2070 return isl_pw_aff_non_zero_set(pa);
2073 /* Return the identifier of the variable that is accessed by
2074 * the skip condition of the given type.
2076 * The skip condition is assumed not to be an affine condition.
2078 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2079 enum pet_skip type)
2081 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2083 if (!scop)
2084 return NULL;
2086 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2089 /* Return an access pet_expr corresponding to the skip condition
2090 * of the given type.
2092 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2093 enum pet_skip type)
2095 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2098 /* Drop the skip condition scop->skip[type].
2100 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2102 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2104 if (!scop)
2105 return;
2107 isl_multi_pw_aff_free(ext->skip[type]);
2108 ext->skip[type] = NULL;
2111 /* Drop all skip conditions on "scop".
2113 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2115 pet_scop_reset_skip(scop, pet_skip_now);
2116 pet_scop_reset_skip(scop, pet_skip_later);
2118 return scop;
2121 /* Make the skip condition (if any) depend on the value of "test" being
2122 * equal to "satisfied".
2124 * We only support the case where the original skip condition is universal,
2125 * i.e., where skipping is unconditional, and where satisfied == 1.
2126 * In this case, the skip condition is changed to skip only when
2127 * "test" is equal to one.
2129 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2130 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2132 int is_univ = 0;
2134 if (!scop)
2135 return NULL;
2136 if (!pet_scop_has_skip(scop, type))
2137 return scop;
2139 if (satisfied)
2140 is_univ = pet_scop_has_universal_skip(scop, type);
2141 if (is_univ < 0)
2142 return pet_scop_free(scop);
2143 if (satisfied && is_univ) {
2144 isl_multi_pw_aff *skip;
2145 skip = isl_multi_pw_aff_copy(test);
2146 scop = pet_scop_set_skip(scop, type, skip);
2147 if (!scop)
2148 return NULL;
2149 } else {
2150 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2151 "skip expression cannot be filtered",
2152 return pet_scop_free(scop));
2155 return scop;
2158 /* Make all statements in "scop" depend on the value of "test"
2159 * being equal to "satisfied" by adjusting their domains.
2161 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2162 __isl_take isl_multi_pw_aff *test, int satisfied)
2164 int i;
2166 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2167 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2169 if (!scop || !test)
2170 goto error;
2172 for (i = 0; i < scop->n_stmt; ++i) {
2173 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2174 isl_multi_pw_aff_copy(test), satisfied);
2175 if (!scop->stmts[i])
2176 goto error;
2179 isl_multi_pw_aff_free(test);
2180 return scop;
2181 error:
2182 isl_multi_pw_aff_free(test);
2183 return pet_scop_free(scop);
2186 /* Add the parameters of the access expression "expr" to "space".
2188 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2190 int i;
2191 isl_space *expr_space;
2192 isl_space **space = user;
2194 expr_space = pet_expr_access_get_parameter_space(expr);
2195 *space = isl_space_align_params(*space, expr_space);
2197 return *space ? 0 : -1;
2200 /* Add all parameters in "stmt" to "space" and return the result.
2202 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2203 __isl_take isl_space *space)
2205 int i;
2207 if (!stmt)
2208 return isl_space_free(space);
2210 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2211 for (i = 0; i < stmt->n_arg; ++i)
2212 if (pet_expr_foreach_access_expr(stmt->args[i],
2213 &access_collect_params, &space) < 0)
2214 space = isl_space_free(space);
2215 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2216 &space) < 0)
2217 space = isl_space_free(space);
2219 return space;
2222 /* Add all parameters in "array" to "space" and return the result.
2224 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2225 __isl_take isl_space *space)
2227 if (!array)
2228 return isl_space_free(space);
2230 space = isl_space_align_params(space,
2231 isl_set_get_space(array->context));
2232 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2234 return space;
2237 /* Add all parameters in "independence" to "space" and return the result.
2239 static __isl_give isl_space *independence_collect_params(
2240 struct pet_independence *independence, __isl_take isl_space *space)
2242 if (!independence)
2243 return isl_space_free(space);
2245 space = isl_space_align_params(space,
2246 isl_union_map_get_space(independence->filter));
2247 space = isl_space_align_params(space,
2248 isl_union_set_get_space(independence->local));
2250 return space;
2253 /* Collect all parameters in "scop" in a parameter space and return the result.
2255 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2257 isl_space *space;
2258 int i;
2260 if (!scop)
2261 return NULL;
2263 space = isl_set_get_space(scop->context);
2265 for (i = 0; i < scop->n_array; ++i)
2266 space = array_collect_params(scop->arrays[i], space);
2268 for (i = 0; i < scop->n_stmt; ++i)
2269 space = stmt_collect_params(scop->stmts[i], space);
2271 for (i = 0; i < scop->n_independence; ++i)
2272 space = independence_collect_params(scop->independences[i],
2273 space);
2275 return space;
2278 /* Add all parameters in "space" to the domain and
2279 * all access relations in "stmt".
2281 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2282 __isl_take isl_space *space)
2284 int i;
2286 if (!stmt)
2287 goto error;
2289 stmt->domain = isl_set_align_params(stmt->domain,
2290 isl_space_copy(space));
2292 for (i = 0; i < stmt->n_arg; ++i) {
2293 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2294 isl_space_copy(space));
2295 if (!stmt->args[i])
2296 goto error;
2298 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2300 if (!stmt->domain || !stmt->body)
2301 goto error;
2303 isl_space_free(space);
2304 return stmt;
2305 error:
2306 isl_space_free(space);
2307 return pet_stmt_free(stmt);
2310 /* Add all parameters in "space" to "array".
2312 static struct pet_array *array_propagate_params(struct pet_array *array,
2313 __isl_take isl_space *space)
2315 if (!array)
2316 goto error;
2318 array->context = isl_set_align_params(array->context,
2319 isl_space_copy(space));
2320 array->extent = isl_set_align_params(array->extent,
2321 isl_space_copy(space));
2322 if (array->value_bounds) {
2323 array->value_bounds = isl_set_align_params(array->value_bounds,
2324 isl_space_copy(space));
2325 if (!array->value_bounds)
2326 goto error;
2329 if (!array->context || !array->extent)
2330 goto error;
2332 isl_space_free(space);
2333 return array;
2334 error:
2335 isl_space_free(space);
2336 return pet_array_free(array);
2339 /* Add all parameters in "space" to "independence".
2341 static struct pet_independence *independence_propagate_params(
2342 struct pet_independence *independence, __isl_take isl_space *space)
2344 if (!independence)
2345 goto error;
2347 independence->filter = isl_union_map_align_params(independence->filter,
2348 isl_space_copy(space));
2349 independence->local = isl_union_set_align_params(independence->local,
2350 isl_space_copy(space));
2351 if (!independence->filter || !independence->local)
2352 goto error;
2354 isl_space_free(space);
2355 return independence;
2356 error:
2357 isl_space_free(space);
2358 return pet_independence_free(independence);
2361 /* Add all parameters in "space" to "scop".
2363 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2364 __isl_take isl_space *space)
2366 int i;
2368 if (!scop)
2369 goto error;
2371 scop->context = isl_set_align_params(scop->context,
2372 isl_space_copy(space));
2373 scop->schedule = isl_schedule_align_params(scop->schedule,
2374 isl_space_copy(space));
2375 if (!scop->context || !scop->schedule)
2376 goto error;
2378 for (i = 0; i < scop->n_array; ++i) {
2379 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2380 isl_space_copy(space));
2381 if (!scop->arrays[i])
2382 goto error;
2385 for (i = 0; i < scop->n_stmt; ++i) {
2386 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2387 isl_space_copy(space));
2388 if (!scop->stmts[i])
2389 goto error;
2392 for (i = 0; i < scop->n_independence; ++i) {
2393 scop->independences[i] = independence_propagate_params(
2394 scop->independences[i], isl_space_copy(space));
2395 if (!scop->independences[i])
2396 goto error;
2399 isl_space_free(space);
2400 return scop;
2401 error:
2402 isl_space_free(space);
2403 return pet_scop_free(scop);
2406 /* Update all isl_sets and isl_maps in "scop" such that they all
2407 * have the same parameters.
2409 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2411 isl_space *space;
2413 if (!scop)
2414 return NULL;
2416 space = scop_collect_params(scop);
2418 scop = scop_propagate_params(scop, space);
2420 return scop;
2423 /* Add the access relation of the give "type" of the access expression "expr"
2424 * to "accesses" and return the result.
2425 * The domain of the access relation is intersected with "domain".
2426 * If "tag" is set, then the access relation is tagged with
2427 * the corresponding reference identifier.
2429 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2430 enum pet_expr_access_type type, int tag,
2431 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2433 isl_union_map *access;
2435 access = pet_expr_access_get_access(expr, type);
2436 access = isl_union_map_intersect_domain(access,
2437 isl_union_set_copy(domain));
2438 if (tag)
2439 access = pet_expr_tag_access(expr, access);
2440 return isl_union_map_union(accesses, access);
2443 /* Internal data structure for expr_collect_accesses.
2445 * "type" is the type of accesses we want to collect.
2446 * "tag" is set if the access relations should be tagged with
2447 * the corresponding reference identifiers.
2448 * "domain" are constraints on the domain of the access relations.
2449 * "accesses" collects the results.
2451 struct pet_expr_collect_accesses_data {
2452 enum pet_expr_access_type type;
2453 int tag;
2454 isl_union_set *domain;
2456 isl_union_map *accesses;
2459 /* Add the access relation of the access expression "expr"
2460 * to data->accesses if the access expression is a read and we are collecting
2461 * reads and/or it is a write and we are collecting writes.
2462 * The domains of the access relations are intersected with data->domain.
2463 * If data->tag is set, then the access relations are tagged with
2464 * the corresponding reference identifiers.
2466 * If data->type is pet_expr_access_must_write, then we only add
2467 * the accesses that are definitely performed. Otherwise, we add
2468 * all potential accesses.
2469 * In particular, if the access has any arguments, then in case of
2470 * pet_expr_access_must_write we currently skip the access completely.
2471 * In other cases, we project out the values of the access arguments.
2473 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2475 struct pet_expr_collect_accesses_data *data = user;
2476 int i;
2477 isl_id *id;
2478 isl_space *dim;
2480 if (!expr)
2481 return -1;
2483 if (pet_expr_is_affine(expr))
2484 return 0;
2485 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2486 return 0;
2488 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2489 ((data->type == pet_expr_access_may_write ||
2490 data->type == pet_expr_access_must_write) && expr->acc.write))
2491 data->accesses = expr_collect_access(expr,
2492 data->type, data->tag,
2493 data->accesses, data->domain);
2495 return data->accesses ? 0 : -1;
2498 /* Collect and return all access relations of the given "type" in "stmt".
2499 * If "tag" is set, then the access relations are tagged with
2500 * the corresponding reference identifiers.
2501 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2502 * we simply add the argument of the kill operation.
2504 * If we are looking for definite accesses (pet_expr_access_must_write
2505 * or pet_expr_access_killed), then we only add the accesses that are
2506 * definitely performed. Otherwise, we add all potential accesses.
2507 * In particular, if the statement has any arguments, then if we are looking
2508 * for definite accesses we currently skip the statement completely. Othewise,
2509 * we project out the values of the statement arguments.
2510 * If the statement body is not an expression tree, then we cannot
2511 * know for sure if/when the accesses inside the tree are performed.
2512 * We therefore ignore such statements when we are looking for
2513 * definite accesses.
2515 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2516 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2518 struct pet_expr_collect_accesses_data data = { type, tag };
2519 int must;
2520 isl_set *domain;
2522 if (!stmt)
2523 return NULL;
2525 data.accesses = isl_union_map_empty(dim);
2527 if (type == pet_expr_access_must_write ||
2528 type == pet_expr_access_killed)
2529 must = 1;
2530 else
2531 must = 0;
2533 if (must && stmt->n_arg > 0)
2534 return data.accesses;
2535 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2536 return data.accesses;
2538 domain = drop_arguments(isl_set_copy(stmt->domain));
2539 data.domain = isl_union_set_from_set(domain);
2541 if (type == pet_expr_access_killed) {
2542 pet_expr *body, *arg;
2544 body = pet_tree_expr_get_expr(stmt->body);
2545 arg = pet_expr_get_arg(body, 0);
2546 data.accesses = expr_collect_access(arg,
2547 pet_expr_access_killed, tag,
2548 data.accesses, data.domain);
2549 pet_expr_free(arg);
2550 pet_expr_free(body);
2551 } else if (pet_tree_foreach_access_expr(stmt->body,
2552 &expr_collect_accesses, &data) < 0)
2553 data.accesses = isl_union_map_free(data.accesses);
2555 isl_union_set_free(data.domain);
2557 return data.accesses;
2560 /* Is "stmt" an assignment statement?
2562 int pet_stmt_is_assign(struct pet_stmt *stmt)
2564 if (!stmt)
2565 return 0;
2566 return pet_tree_is_assign(stmt->body);
2569 /* Is "stmt" a kill statement?
2571 int pet_stmt_is_kill(struct pet_stmt *stmt)
2573 if (!stmt)
2574 return 0;
2575 return pet_tree_is_kill(stmt->body);
2578 /* Is "stmt" an assume statement?
2580 int pet_stmt_is_assume(struct pet_stmt *stmt)
2582 if (!stmt)
2583 return 0;
2584 return pet_tree_is_assume(stmt->body);
2587 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2589 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2590 __isl_keep isl_map *map, __isl_keep isl_set *set)
2592 isl_map *gist;
2594 gist = isl_map_copy(map);
2595 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2596 return isl_union_map_add_map(umap, gist);
2599 /* Compute a mapping from all arrays (of structs) in scop
2600 * to their members.
2602 * If "from_outermost" is set, then the domain only consists
2603 * of outermost arrays.
2604 * If "to_innermost" is set, then the range only consists
2605 * of innermost arrays.
2607 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2608 int from_outermost, int to_innermost)
2610 int i;
2611 isl_union_map *to_inner;
2613 if (!scop)
2614 return NULL;
2616 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2618 for (i = 0; i < scop->n_array; ++i) {
2619 struct pet_array *array = scop->arrays[i];
2620 isl_set *set;
2621 isl_map *map;
2623 if (to_innermost && array->element_is_record)
2624 continue;
2626 set = isl_set_copy(array->extent);
2627 map = isl_set_identity(isl_set_copy(set));
2629 while (set && isl_set_is_wrapping(set)) {
2630 isl_id *id;
2631 isl_map *wrapped;
2633 if (!from_outermost)
2634 to_inner = add_gisted(to_inner, map, set);
2636 id = isl_set_get_tuple_id(set);
2637 wrapped = isl_set_unwrap(set);
2638 wrapped = isl_map_domain_map(wrapped);
2639 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2640 map = isl_map_apply_domain(map, wrapped);
2641 set = isl_map_domain(isl_map_copy(map));
2644 map = isl_map_gist_domain(map, set);
2645 to_inner = isl_union_map_add_map(to_inner, map);
2648 return to_inner;
2651 /* Compute a mapping from all arrays (of structs) in scop
2652 * to their innermost arrays.
2654 * In particular, for each array of a primitive type, the result
2655 * contains the identity mapping on that array.
2656 * For each array involving member accesses, the result
2657 * contains a mapping from the elements of any intermediate array of structs
2658 * to all corresponding elements of the innermost nested arrays.
2660 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2661 struct pet_scop *scop)
2663 return compute_to_inner(scop, 0, 1);
2666 /* Compute a mapping from all outermost arrays (of structs) in scop
2667 * to their innermost members.
2669 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2671 return compute_to_inner(scop, 1, 1);
2674 /* Compute a mapping from all outermost arrays (of structs) in scop
2675 * to their members, including the outermost arrays themselves.
2677 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2679 return compute_to_inner(scop, 1, 0);
2682 /* Collect and return all access relations of the given "type" in "scop".
2683 * If "type" is pet_expr_access_killed, then we only add the arguments of
2684 * kill operations.
2685 * If we are looking for definite accesses (pet_expr_access_must_write
2686 * or pet_expr_access_killed), then we only add the accesses that are
2687 * definitely performed. Otherwise, we add all potential accesses.
2688 * If "tag" is set, then the access relations are tagged with
2689 * the corresponding reference identifiers.
2690 * For accesses to structures, the returned access relation accesses
2691 * all individual fields in the structures.
2693 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2694 enum pet_expr_access_type type, int tag)
2696 int i;
2697 isl_union_map *accesses;
2698 isl_union_set *arrays;
2699 isl_union_map *to_inner;
2701 if (!scop)
2702 return NULL;
2704 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2706 for (i = 0; i < scop->n_stmt; ++i) {
2707 struct pet_stmt *stmt = scop->stmts[i];
2708 isl_union_map *accesses_i;
2709 isl_space *space;
2711 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2712 continue;
2714 space = isl_set_get_space(scop->context);
2715 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2716 accesses = isl_union_map_union(accesses, accesses_i);
2719 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2720 for (i = 0; i < scop->n_array; ++i) {
2721 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2722 arrays = isl_union_set_add_set(arrays, extent);
2724 accesses = isl_union_map_intersect_range(accesses, arrays);
2726 to_inner = pet_scop_compute_any_to_inner(scop);
2727 accesses = isl_union_map_apply_range(accesses, to_inner);
2729 return accesses;
2732 /* Return the potential read access relation.
2734 __isl_give isl_union_map *pet_scop_get_may_reads(struct pet_scop *scop)
2736 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2739 /* Return the potential write access relation.
2741 __isl_give isl_union_map *pet_scop_get_may_writes(struct pet_scop *scop)
2743 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2746 /* Return the definite write access relation.
2748 __isl_give isl_union_map *pet_scop_get_must_writes(struct pet_scop *scop)
2750 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2753 /* Return the definite kill access relation.
2755 __isl_give isl_union_map *pet_scop_get_must_kills(struct pet_scop *scop)
2757 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2760 /* Return the tagged potential read access relation.
2762 __isl_give isl_union_map *pet_scop_get_tagged_may_reads(
2763 struct pet_scop *scop)
2765 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2768 /* Return the tagged potential write access relation.
2770 __isl_give isl_union_map *pet_scop_get_tagged_may_writes(
2771 struct pet_scop *scop)
2773 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2776 /* Return the tagged definite write access relation.
2778 __isl_give isl_union_map *pet_scop_get_tagged_must_writes(
2779 struct pet_scop *scop)
2781 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2784 /* Return the tagged definite kill access relation.
2786 __isl_give isl_union_map *pet_scop_get_tagged_must_kills(
2787 struct pet_scop *scop)
2789 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2792 /* Collect and return the set of all statement instances in "scop".
2794 __isl_give isl_union_set *pet_scop_get_instance_set(struct pet_scop *scop)
2796 int i;
2797 isl_set *domain_i;
2798 isl_union_set *domain;
2800 if (!scop)
2801 return NULL;
2803 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2805 for (i = 0; i < scop->n_stmt; ++i) {
2806 domain_i = isl_set_copy(scop->stmts[i]->domain);
2807 if (scop->stmts[i]->n_arg > 0)
2808 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2809 domain = isl_union_set_add_set(domain, domain_i);
2812 return domain;
2815 /* Return the context of "scop".
2817 __isl_give isl_set *pet_scop_get_context(__isl_keep pet_scop *scop)
2819 if (!scop)
2820 return NULL;
2822 return isl_set_copy(scop->context);
2825 /* Return the schedule of "scop".
2827 __isl_give isl_schedule *pet_scop_get_schedule(__isl_keep pet_scop *scop)
2829 if (!scop)
2830 return NULL;
2832 return isl_schedule_copy(scop->schedule);
2835 /* Add a reference identifier to all access expressions in "stmt".
2836 * "n_ref" points to an integer that contains the sequence number
2837 * of the next reference.
2839 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2841 int i;
2843 if (!stmt)
2844 return NULL;
2846 for (i = 0; i < stmt->n_arg; ++i) {
2847 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2848 if (!stmt->args[i])
2849 return pet_stmt_free(stmt);
2852 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2853 if (!stmt->body)
2854 return pet_stmt_free(stmt);
2856 return stmt;
2859 /* Add a reference identifier to all access expressions in "scop".
2861 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2863 int i;
2864 int n_ref;
2866 if (!scop)
2867 return NULL;
2869 n_ref = 0;
2870 for (i = 0; i < scop->n_stmt; ++i) {
2871 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2872 if (!scop->stmts[i])
2873 return pet_scop_free(scop);
2876 return scop;
2879 /* Reset the user pointer on all parameter ids in "array".
2881 static struct pet_array *array_anonymize(struct pet_array *array)
2883 if (!array)
2884 return NULL;
2886 array->context = isl_set_reset_user(array->context);
2887 array->extent = isl_set_reset_user(array->extent);
2888 if (!array->context || !array->extent)
2889 return pet_array_free(array);
2891 return array;
2894 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2896 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2898 int i;
2899 isl_space *space;
2900 isl_set *domain;
2902 if (!stmt)
2903 return NULL;
2905 stmt->domain = isl_set_reset_user(stmt->domain);
2906 if (!stmt->domain)
2907 return pet_stmt_free(stmt);
2909 for (i = 0; i < stmt->n_arg; ++i) {
2910 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2911 if (!stmt->args[i])
2912 return pet_stmt_free(stmt);
2915 stmt->body = pet_tree_anonymize(stmt->body);
2916 if (!stmt->body)
2917 return pet_stmt_free(stmt);
2919 return stmt;
2922 /* Reset the user pointer on the tuple ids and all parameter ids
2923 * in "implication".
2925 static struct pet_implication *implication_anonymize(
2926 struct pet_implication *implication)
2928 if (!implication)
2929 return NULL;
2931 implication->extension = isl_map_reset_user(implication->extension);
2932 if (!implication->extension)
2933 return pet_implication_free(implication);
2935 return implication;
2938 /* Reset the user pointer on the tuple ids and all parameter ids
2939 * in "independence".
2941 static struct pet_independence *independence_anonymize(
2942 struct pet_independence *independence)
2944 if (!independence)
2945 return NULL;
2947 independence->filter = isl_union_map_reset_user(independence->filter);
2948 independence->local = isl_union_set_reset_user(independence->local);
2949 if (!independence->filter || !independence->local)
2950 return pet_independence_free(independence);
2952 return independence;
2955 /* Reset the user pointer on all parameter and tuple ids in "scop".
2957 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2959 int i;
2961 if (!scop)
2962 return NULL;
2964 scop->context = isl_set_reset_user(scop->context);
2965 scop->context_value = isl_set_reset_user(scop->context_value);
2966 scop->schedule = isl_schedule_reset_user(scop->schedule);
2967 if (!scop->context || !scop->context_value || !scop->schedule)
2968 return pet_scop_free(scop);
2970 for (i = 0; i < scop->n_array; ++i) {
2971 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2972 if (!scop->arrays[i])
2973 return pet_scop_free(scop);
2976 for (i = 0; i < scop->n_stmt; ++i) {
2977 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2978 if (!scop->stmts[i])
2979 return pet_scop_free(scop);
2982 for (i = 0; i < scop->n_implication; ++i) {
2983 scop->implications[i] =
2984 implication_anonymize(scop->implications[i]);
2985 if (!scop->implications[i])
2986 return pet_scop_free(scop);
2989 for (i = 0; i < scop->n_independence; ++i) {
2990 scop->independences[i] =
2991 independence_anonymize(scop->independences[i]);
2992 if (!scop->independences[i])
2993 return pet_scop_free(scop);
2996 return scop;
2999 /* Compute the gist of the iteration domain and all access relations
3000 * of "stmt" based on the constraints on the parameters specified by "context"
3001 * and the constraints on the values of nested accesses specified
3002 * by "value_bounds".
3004 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3005 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3007 int i;
3008 isl_set *domain;
3010 if (!stmt)
3011 return NULL;
3013 domain = isl_set_copy(stmt->domain);
3014 if (stmt->n_arg > 0)
3015 domain = isl_map_domain(isl_set_unwrap(domain));
3017 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3019 for (i = 0; i < stmt->n_arg; ++i) {
3020 stmt->args[i] = pet_expr_gist(stmt->args[i],
3021 domain, value_bounds);
3022 if (!stmt->args[i])
3023 goto error;
3026 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
3027 if (!stmt->body)
3028 goto error;
3030 isl_set_free(domain);
3032 domain = isl_set_universe(pet_stmt_get_space(stmt));
3033 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3034 if (stmt->n_arg > 0)
3035 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3036 value_bounds);
3037 stmt->domain = isl_set_gist(stmt->domain, domain);
3038 if (!stmt->domain)
3039 return pet_stmt_free(stmt);
3041 return stmt;
3042 error:
3043 isl_set_free(domain);
3044 return pet_stmt_free(stmt);
3047 /* Compute the gist of the extent of the array
3048 * based on the constraints on the parameters specified by "context".
3050 static struct pet_array *array_gist(struct pet_array *array,
3051 __isl_keep isl_set *context)
3053 if (!array)
3054 return NULL;
3056 array->extent = isl_set_gist_params(array->extent,
3057 isl_set_copy(context));
3058 if (!array->extent)
3059 return pet_array_free(array);
3061 return array;
3064 /* Compute the gist of all sets and relations in "scop"
3065 * based on the constraints on the parameters specified by "scop->context"
3066 * and the constraints on the values of nested accesses specified
3067 * by "value_bounds".
3069 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3070 __isl_keep isl_union_map *value_bounds)
3072 int i;
3074 if (!scop)
3075 return NULL;
3077 scop->context = isl_set_coalesce(scop->context);
3078 if (!scop->context)
3079 return pet_scop_free(scop);
3081 scop->schedule = isl_schedule_gist_domain_params(scop->schedule,
3082 isl_set_copy(scop->context));
3083 if (!scop->schedule)
3084 return pet_scop_free(scop);
3086 for (i = 0; i < scop->n_array; ++i) {
3087 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3088 if (!scop->arrays[i])
3089 return pet_scop_free(scop);
3092 for (i = 0; i < scop->n_stmt; ++i) {
3093 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3094 value_bounds);
3095 if (!scop->stmts[i])
3096 return pet_scop_free(scop);
3099 return scop;
3102 /* Intersect the context of "scop" with "context".
3103 * To ensure that we don't introduce any unnamed parameters in
3104 * the context of "scop", we first remove the unnamed parameters
3105 * from "context".
3107 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3108 __isl_take isl_set *context)
3110 if (!scop)
3111 goto error;
3113 context = pet_nested_remove_from_set(context);
3114 scop->context = isl_set_intersect(scop->context, context);
3115 if (!scop->context)
3116 return pet_scop_free(scop);
3118 return scop;
3119 error:
3120 isl_set_free(context);
3121 return pet_scop_free(scop);
3124 /* Drop the current context of "scop". That is, replace the context
3125 * by a universal set.
3127 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3129 isl_space *space;
3131 if (!scop)
3132 return NULL;
3134 space = isl_set_get_space(scop->context);
3135 isl_set_free(scop->context);
3136 scop->context = isl_set_universe(space);
3137 if (!scop->context)
3138 return pet_scop_free(scop);
3140 return scop;
3143 /* Append "array" to the arrays of "scop".
3145 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3146 struct pet_array *array)
3148 isl_ctx *ctx;
3149 struct pet_array **arrays;
3151 if (!array || !scop)
3152 goto error;
3154 ctx = isl_set_get_ctx(scop->context);
3155 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3156 scop->n_array + 1);
3157 if (!arrays)
3158 goto error;
3159 scop->arrays = arrays;
3160 scop->arrays[scop->n_array] = array;
3161 scop->n_array++;
3162 scop->context = isl_set_intersect_params(scop->context,
3163 isl_set_copy(array->context));
3164 if (!scop->context)
3165 return pet_scop_free(scop);
3167 return scop;
3168 error:
3169 pet_array_free(array);
3170 return pet_scop_free(scop);
3173 /* Create an index expression for an access to a virtual array
3174 * representing the result of a condition.
3175 * Unlike other accessed data, the id of the array is NULL as
3176 * there is no ValueDecl in the program corresponding to the virtual
3177 * array.
3178 * The index expression is created as an identity mapping on "space".
3179 * That is, the dimension of the array is the same as that of "space".
3181 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3182 int test_nr)
3184 isl_id *id;
3185 char name[50];
3187 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3188 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3189 space = isl_space_map_from_set(space);
3190 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3191 return isl_multi_pw_aff_identity(space);
3194 /* Add an array with the given extent to the list
3195 * of arrays in "scop" and return the extended pet_scop.
3196 * Specifically, the extent is determined by the image of "domain"
3197 * under "index".
3198 * "int_size" is the number of bytes needed to represent values of type "int".
3199 * The array is marked as attaining values 0 and 1 only and
3200 * as each element being assigned at most once.
3202 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3203 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3204 int int_size)
3206 isl_ctx *ctx;
3207 isl_space *space;
3208 struct pet_array *array;
3209 isl_map *access;
3211 if (!scop || !domain || !index)
3212 goto error;
3214 ctx = isl_multi_pw_aff_get_ctx(index);
3215 array = isl_calloc_type(ctx, struct pet_array);
3216 if (!array)
3217 goto error;
3219 access = isl_map_from_multi_pw_aff(index);
3220 access = isl_map_intersect_domain(access, domain);
3221 array->extent = isl_map_range(access);
3222 space = isl_space_params_alloc(ctx, 0);
3223 array->context = isl_set_universe(space);
3224 space = isl_space_set_alloc(ctx, 0, 1);
3225 array->value_bounds = isl_set_universe(space);
3226 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3227 isl_dim_set, 0, 0);
3228 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3229 isl_dim_set, 0, 1);
3230 array->element_type = strdup("int");
3231 array->element_size = int_size;
3232 array->uniquely_defined = 1;
3234 if (!array->extent || !array->context)
3235 array = pet_array_free(array);
3237 scop = pet_scop_add_array(scop, array);
3239 return scop;
3240 error:
3241 isl_set_free(domain);
3242 isl_multi_pw_aff_free(index);
3243 return pet_scop_free(scop);
3246 /* Create and return an implication on filter values equal to "satisfied"
3247 * with extension "map".
3249 static struct pet_implication *new_implication(__isl_take isl_map *map,
3250 int satisfied)
3252 isl_ctx *ctx;
3253 struct pet_implication *implication;
3255 if (!map)
3256 return NULL;
3257 ctx = isl_map_get_ctx(map);
3258 implication = isl_alloc_type(ctx, struct pet_implication);
3259 if (!implication)
3260 goto error;
3262 implication->extension = map;
3263 implication->satisfied = satisfied;
3265 return implication;
3266 error:
3267 isl_map_free(map);
3268 return NULL;
3271 /* Add an implication on filter values equal to "satisfied"
3272 * with extension "map" to "scop".
3274 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3275 __isl_take isl_map *map, int satisfied)
3277 isl_ctx *ctx;
3278 struct pet_implication *implication;
3279 struct pet_implication **implications;
3281 implication = new_implication(map, satisfied);
3282 if (!scop || !implication)
3283 goto error;
3285 ctx = isl_set_get_ctx(scop->context);
3286 implications = isl_realloc_array(ctx, scop->implications,
3287 struct pet_implication *,
3288 scop->n_implication + 1);
3289 if (!implications)
3290 goto error;
3291 scop->implications = implications;
3292 scop->implications[scop->n_implication] = implication;
3293 scop->n_implication++;
3295 return scop;
3296 error:
3297 pet_implication_free(implication);
3298 return pet_scop_free(scop);
3301 /* Create and return a function that maps the iteration domains
3302 * of the statements in "scop" onto their outer "n" dimensions.
3303 * "space" is the parameters space of the created function.
3305 static __isl_give isl_union_pw_multi_aff *outer_projection(
3306 struct pet_scop *scop, __isl_take isl_space *space, int n)
3308 int i;
3309 isl_union_pw_multi_aff *res;
3311 res = isl_union_pw_multi_aff_empty(space);
3313 if (!scop)
3314 return isl_union_pw_multi_aff_free(res);
3316 for (i = 0; i < scop->n_stmt; ++i) {
3317 struct pet_stmt *stmt = scop->stmts[i];
3318 isl_space *space;
3319 isl_multi_aff *ma;
3320 isl_pw_multi_aff *pma;
3322 space = pet_stmt_get_space(stmt);
3323 ma = pet_prefix_projection(space, n);
3324 pma = isl_pw_multi_aff_from_multi_aff(ma);
3325 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3328 return res;
3331 /* Add an independence to "scop" for the inner iterator of "domain"
3332 * with local variables "local", where "domain" represents the outer
3333 * loop iterators of all statements in "scop".
3334 * If "sign" is positive, then the inner iterator increases.
3335 * Otherwise it decreases.
3337 * The independence is supposed to filter out any dependence of
3338 * an iteration of domain on a previous iteration along the inner dimension.
3339 * We therefore create a mapping from an iteration to later iterations and
3340 * then plug in the projection of the iterations domains of "scop"
3341 * onto the outer loop iterators.
3343 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3344 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3346 int i, dim;
3347 isl_space *space;
3348 isl_map *map;
3349 isl_union_map *independence;
3350 isl_union_pw_multi_aff *proj;
3352 if (!scop || !domain || !local)
3353 goto error;
3355 dim = isl_set_dim(domain, isl_dim_set);
3356 space = isl_space_map_from_set(isl_set_get_space(domain));
3357 map = isl_map_universe(space);
3358 for (i = 0; i + 1 < dim; ++i)
3359 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3360 if (sign > 0)
3361 map = isl_map_order_lt(map,
3362 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3363 else
3364 map = isl_map_order_gt(map,
3365 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3367 independence = isl_union_map_from_map(map);
3368 space = isl_space_params(isl_set_get_space(domain));
3369 proj = outer_projection(scop, space, dim);
3370 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3371 independence, isl_union_pw_multi_aff_copy(proj));
3372 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3373 independence, proj);
3375 scop = pet_scop_add_independence(scop, independence, local);
3377 return scop;
3378 error:
3379 isl_union_set_free(local);
3380 return pet_scop_free(scop);
3383 /* Given an access expression, check if it is data dependent.
3384 * If so, set *found and abort the search.
3386 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3388 int *found = user;
3390 if (pet_expr_get_n_arg(expr) > 0) {
3391 *found = 1;
3392 return -1;
3395 return 0;
3398 /* Does "scop" contain any data dependent accesses?
3400 * Check the body of each statement for such accesses.
3402 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3404 int i;
3405 int found = 0;
3407 if (!scop)
3408 return -1;
3410 for (i = 0; i < scop->n_stmt; ++i) {
3411 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3412 &is_data_dependent, &found);
3413 if (r < 0 && !found)
3414 return -1;
3415 if (found)
3416 return found;
3419 return found;
3422 /* Does "scop" contain and data dependent conditions?
3424 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3426 int i;
3428 if (!scop)
3429 return -1;
3431 for (i = 0; i < scop->n_stmt; ++i)
3432 if (scop->stmts[i]->n_arg > 0)
3433 return 1;
3435 return 0;
3438 /* Keep track of the "input" file inside the (extended) "scop".
3440 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3442 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3444 if (!scop)
3445 return NULL;
3447 ext->input = input;
3449 return scop;
3452 /* Print the original code corresponding to "scop" to printer "p".
3454 * pet_scop_print_original can only be called from
3455 * a pet_transform_C_source callback. This means that the input
3456 * file is stored in the extended scop and that the printer prints
3457 * to a file.
3459 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3460 __isl_take isl_printer *p)
3462 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3463 FILE *output;
3464 unsigned start, end;
3466 if (!scop || !p)
3467 return isl_printer_free(p);
3469 if (!ext->input)
3470 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3471 "no input file stored in scop",
3472 return isl_printer_free(p));
3474 output = isl_printer_get_file(p);
3475 if (!output)
3476 return isl_printer_free(p);
3478 start = pet_loc_get_start(scop->loc);
3479 end = pet_loc_get_end(scop->loc);
3480 if (copy(ext->input, output, start, end) < 0)
3481 return isl_printer_free(p);
3483 return p;