scop.c: stmt_extract_context: extract out pet_stmt_assume_get_affine_condition
[pet.git] / scop.c
blob20dbf2567c2398a2cd7b39f3ad99d153c4d45929
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
38 #include <isl/schedule_node.h>
40 #include "aff.h"
41 #include "expr.h"
42 #include "expr_access_type.h"
43 #include "filter.h"
44 #include "loc.h"
45 #include "nest.h"
46 #include "scop.h"
47 #include "tree.h"
48 #include "print.h"
49 #include "value_bounds.h"
51 /* pet_scop with extra information that is used during parsing and printing.
53 * In particular, we keep track of conditions under which we want
54 * to skip the rest of the current loop iteration (skip[pet_skip_now])
55 * and of conditions under which we want to skip subsequent
56 * loop iterations (skip[pet_skip_later]).
58 * The conditions are represented as index expressions defined
59 * over the outer loop iterators. The index expression is either
60 * a boolean affine expression or an access to a variable, which
61 * is assumed to attain values zero and one. The condition holds
62 * if the variable has value one or if the affine expression
63 * has value one (typically for only part of the domain).
65 * A missing condition (skip[type] == NULL) means that we don't want
66 * to skip anything.
68 * Additionally, we keep track of the original input file
69 * inside pet_transform_C_source.
71 struct pet_scop_ext {
72 struct pet_scop scop;
74 isl_multi_pw_aff *skip[2];
75 FILE *input;
78 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
79 * The input domain is anonymous and is the same as the domains
80 * of the access expressions inside "tree".
81 * These domains are modified to include the name of the statement.
82 * This name is given by tree->label if it is non-NULL.
83 * Otherwise, the name is constructed as S_<id>.
85 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
86 int id, __isl_take pet_tree *tree)
88 struct pet_stmt *stmt;
89 isl_ctx *ctx;
90 isl_id *label;
91 isl_space *space;
92 isl_multi_aff *ma;
93 isl_multi_pw_aff *add_name;
94 char name[50];
96 if (!domain || !tree)
97 goto error;
99 ctx = pet_tree_get_ctx(tree);
100 stmt = isl_calloc_type(ctx, struct pet_stmt);
101 if (!stmt)
102 goto error;
104 if (tree->label) {
105 label = isl_id_copy(tree->label);
106 } else {
107 snprintf(name, sizeof(name), "S_%d", id);
108 label = isl_id_alloc(ctx, name, NULL);
110 domain = isl_set_set_tuple_id(domain, label);
111 space = isl_set_get_space(domain);
112 space = pet_nested_remove_from_space(space);
113 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
115 add_name = isl_multi_pw_aff_from_multi_aff(ma);
116 tree = pet_tree_update_domain(tree, add_name);
118 stmt->loc = pet_tree_get_loc(tree);
119 stmt->domain = domain;
120 stmt->body = tree;
122 if (!stmt->domain || !stmt->body)
123 return pet_stmt_free(stmt);
125 return stmt;
126 error:
127 isl_set_free(domain);
128 pet_tree_free(tree);
129 return NULL;
132 void *pet_stmt_free(struct pet_stmt *stmt)
134 int i;
136 if (!stmt)
137 return NULL;
139 pet_loc_free(stmt->loc);
140 isl_set_free(stmt->domain);
141 pet_tree_free(stmt->body);
143 for (i = 0; i < stmt->n_arg; ++i)
144 pet_expr_free(stmt->args[i]);
145 free(stmt->args);
147 free(stmt);
148 return NULL;
151 /* Return the iteration space of "stmt".
153 * If the statement has arguments, then stmt->domain is a wrapped map
154 * mapping the iteration domain to the values of the arguments
155 * for which this statement is executed.
156 * In this case, we need to extract the domain space of this wrapped map.
158 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
160 isl_space *space;
162 if (!stmt)
163 return NULL;
165 space = isl_set_get_space(stmt->domain);
166 if (isl_space_is_wrapping(space))
167 space = isl_space_domain(isl_space_unwrap(space));
169 return space;
172 static void stmt_dump(struct pet_stmt *stmt, int indent)
174 int i;
176 if (!stmt)
177 return;
179 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
180 fprintf(stderr, "%*s", indent, "");
181 isl_set_dump(stmt->domain);
182 pet_tree_dump_with_indent(stmt->body, indent);
183 for (i = 0; i < stmt->n_arg; ++i)
184 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
187 void pet_stmt_dump(struct pet_stmt *stmt)
189 stmt_dump(stmt, 0);
192 /* Allocate a new pet_type with the given "name" and "definition".
194 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
195 const char *definition)
197 struct pet_type *type;
199 type = isl_alloc_type(ctx, struct pet_type);
200 if (!type)
201 return NULL;
203 type->name = strdup(name);
204 type->definition = strdup(definition);
206 if (!type->name || !type->definition)
207 return pet_type_free(type);
209 return type;
212 /* Free "type" and return NULL.
214 struct pet_type *pet_type_free(struct pet_type *type)
216 if (!type)
217 return NULL;
219 free(type->name);
220 free(type->definition);
222 free(type);
223 return NULL;
226 struct pet_array *pet_array_free(struct pet_array *array)
228 if (!array)
229 return NULL;
231 isl_set_free(array->context);
232 isl_set_free(array->extent);
233 isl_set_free(array->value_bounds);
234 free(array->element_type);
236 free(array);
237 return NULL;
240 void pet_array_dump(struct pet_array *array)
242 if (!array)
243 return;
245 isl_set_dump(array->context);
246 isl_set_dump(array->extent);
247 isl_set_dump(array->value_bounds);
248 fprintf(stderr, "%s%s%s\n", array->element_type,
249 array->element_is_record ? " element-is-record" : "",
250 array->live_out ? " live-out" : "");
253 /* Alloc a pet_scop structure, with extra room for information that
254 * is only used during parsing.
256 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
258 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
261 /* Construct a pet_scop in the given space, with the given schedule and
262 * room for n statements.
264 * The context is initialized as a universe set in "space".
266 * Since no information on the location is known at this point,
267 * scop->loc is initialized with pet_loc_dummy.
269 static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n,
270 __isl_take isl_schedule *schedule)
272 isl_ctx *ctx;
273 struct pet_scop *scop;
275 if (!space || !schedule)
276 goto error;
278 ctx = isl_space_get_ctx(space);
279 scop = pet_scop_alloc(ctx);
280 if (!scop)
281 goto error;
283 scop->context = isl_set_universe(isl_space_copy(space));
284 scop->context_value = isl_set_universe(isl_space_params(space));
285 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
286 scop->schedule = schedule;
287 if (!scop->context || !scop->stmts)
288 return pet_scop_free(scop);
290 scop->loc = &pet_loc_dummy;
291 scop->n_stmt = n;
293 return scop;
294 error:
295 isl_space_free(space);
296 isl_schedule_free(schedule);
297 return NULL;
300 /* Construct a pet_scop in the given space containing 0 statements
301 * (and therefore an empty iteration domain).
303 struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
305 isl_schedule *schedule;
307 schedule = isl_schedule_empty(isl_space_copy(space));
309 return scop_alloc(space, 0, schedule);
312 /* Given either an iteration domain or a wrapped map with
313 * the iteration domain in the domain and some arguments
314 * in the range, return the iteration domain.
315 * That is, drop the arguments if there are any.
317 static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
319 if (isl_set_is_wrapping(domain))
320 domain = isl_map_domain(isl_set_unwrap(domain));
321 return domain;
324 /* Update "context" with the constraints imposed on the outer iteration
325 * domain by access expression "expr".
326 * "context" lives in an anonymous space, while the domain of the access
327 * relation of "expr" refers to a particular statement.
328 * This reference therefore needs to be stripped off.
330 static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
331 __isl_take isl_set *context)
333 isl_multi_pw_aff *mpa;
334 isl_set *domain;
336 mpa = pet_expr_access_get_index(expr);
337 domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
338 domain = isl_set_reset_tuple_id(domain);
339 context = isl_set_intersect(context, domain);
340 return context;
343 /* Update "context" with the constraints imposed on the outer iteration
344 * domain by "expr".
346 * "context" lives in an anonymous space, while the domains of
347 * the access relations in "expr" refer to a particular statement.
348 * This reference therefore needs to be stripped off.
350 * If "expr" represents a conditional operator, then a parameter or outer
351 * iterator value needs to be valid for the condition and
352 * for at least one of the remaining two arguments.
353 * If the condition is an affine expression, then we can be a bit more specific.
354 * The value then has to be valid for the second argument for
355 * non-zero accesses and valid for the third argument for zero accesses.
357 * If "expr" represents a kill statement, then its argument is the entire
358 * extent of the array being killed. Do not update "context" based
359 * on this argument as that would impose constraints that ensure that
360 * the array is non-empty.
362 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
363 __isl_take isl_set *context)
365 int i;
367 if (expr->type == pet_expr_op && expr->op == pet_op_kill)
368 return context;
370 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
371 int is_aff;
372 isl_set *context1, *context2;
374 is_aff = pet_expr_is_affine(expr->args[0]);
375 if (is_aff < 0)
376 goto error;
378 context = expr_extract_context(expr->args[0], context);
379 context1 = expr_extract_context(expr->args[1],
380 isl_set_copy(context));
381 context2 = expr_extract_context(expr->args[2], context);
383 if (is_aff) {
384 isl_multi_pw_aff *mpa;
385 isl_pw_aff *pa;
386 isl_set *zero_set;
388 mpa = pet_expr_access_get_index(expr->args[0]);
389 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
390 isl_multi_pw_aff_free(mpa);
391 zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
392 zero_set = isl_set_reset_tuple_id(zero_set);
393 context1 = isl_set_subtract(context1,
394 isl_set_copy(zero_set));
395 context2 = isl_set_intersect(context2, zero_set);
398 context = isl_set_union(context1, context2);
399 context = isl_set_coalesce(context);
401 return context;
404 for (i = 0; i < expr->n_arg; ++i)
405 context = expr_extract_context(expr->args[i], context);
407 if (expr->type == pet_expr_access)
408 context = access_extract_context(expr, context);
410 return context;
411 error:
412 isl_set_free(context);
413 return NULL;
416 /* Is "stmt" an assume statement with an affine assumption?
418 isl_bool pet_stmt_is_affine_assume(struct pet_stmt *stmt)
420 if (!stmt)
421 return isl_bool_error;
422 return pet_tree_is_affine_assume(stmt->body);
425 /* Given an assume statement "stmt" with an access argument,
426 * return the index expression of the argument.
428 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
430 if (!stmt)
431 return NULL;
432 return pet_tree_assume_get_index(stmt->body);
435 /* Assuming "stmt" is an assume statement with an affine assumption,
436 * return the assumption as a set.
438 __isl_give isl_set *pet_stmt_assume_get_affine_condition(struct pet_stmt *stmt)
440 isl_multi_pw_aff *index;
441 isl_pw_aff *pa;
443 index = pet_stmt_assume_get_index(stmt);
444 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
445 isl_multi_pw_aff_free(index);
446 return isl_pw_aff_non_zero_set(pa);
449 /* Update "context" with the constraints imposed on the outer iteration
450 * domain by "stmt".
452 * If the statement is an assume statement with an affine expression,
453 * then intersect "context" with that expression.
454 * Otherwise, if the statement body is an expression tree,
455 * then intersect "context" with the context of this expression.
456 * Note that we cannot safely extract a context from subtrees
457 * of the statement body since we cannot tell when those subtrees
458 * are executed, if at all.
460 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
461 __isl_take isl_set *context)
463 int i;
464 isl_bool affine;
465 pet_expr *body;
467 affine = pet_stmt_is_affine_assume(stmt);
468 if (affine < 0)
469 return isl_set_free(context);
470 if (affine) {
471 isl_set *cond;
473 cond = pet_stmt_assume_get_affine_condition(stmt);
474 cond = isl_set_reset_tuple_id(cond);
475 return isl_set_intersect(context, cond);
478 for (i = 0; i < stmt->n_arg; ++i)
479 context = expr_extract_context(stmt->args[i], context);
481 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
482 return context;
484 body = pet_tree_expr_get_expr(stmt->body);
485 context = expr_extract_context(body, context);
486 pet_expr_free(body);
488 return context;
491 /* Construct a pet_scop in the given space that contains the given pet_stmt.
492 * The initial schedule consists of only the iteration domain.
494 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
495 struct pet_stmt *stmt)
497 struct pet_scop *scop;
498 isl_set *set;
499 isl_union_set *domain;
500 isl_schedule *schedule;
502 if (!stmt) {
503 isl_space_free(space);
504 return NULL;
507 set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
508 domain = isl_union_set_from_set(set);
509 schedule = isl_schedule_from_domain(domain);
511 scop = scop_alloc(space, 1, schedule);
512 if (!scop)
513 goto error;
515 scop->context = stmt_extract_context(stmt, scop->context);
516 if (!scop->context)
517 goto error;
519 scop->stmts[0] = stmt;
520 scop->loc = pet_loc_copy(stmt->loc);
522 if (!scop->loc)
523 return pet_scop_free(scop);
525 return scop;
526 error:
527 pet_stmt_free(stmt);
528 pet_scop_free(scop);
529 return NULL;
532 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
533 * does it represent an affine expression?
535 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
537 int has_id;
539 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
540 if (has_id < 0)
541 return -1;
543 return !has_id;
546 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
548 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
549 __isl_take isl_set *dom)
551 isl_pw_aff *pa;
552 pa = isl_set_indicator_function(set);
553 pa = isl_pw_aff_intersect_domain(pa, dom);
554 return pa;
557 /* Return "lhs || rhs", defined on the shared definition domain.
559 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
560 __isl_take isl_pw_aff *rhs)
562 isl_set *cond;
563 isl_set *dom;
565 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
566 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
567 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
568 isl_pw_aff_non_zero_set(rhs));
569 cond = isl_set_coalesce(cond);
570 return indicator_function(cond, dom);
573 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
574 * ext may be equal to either ext1 or ext2.
576 * The two skips that need to be combined are assumed to be affine expressions.
578 * We need to skip in ext if we need to skip in either ext1 or ext2.
579 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
581 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
582 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
583 enum pet_skip type)
585 isl_pw_aff *skip, *skip1, *skip2;
587 if (!ext)
588 return NULL;
589 if (!ext1->skip[type] && !ext2->skip[type])
590 return ext;
591 if (!ext1->skip[type]) {
592 if (ext == ext2)
593 return ext;
594 ext->skip[type] = ext2->skip[type];
595 ext2->skip[type] = NULL;
596 return ext;
598 if (!ext2->skip[type]) {
599 if (ext == ext1)
600 return ext;
601 ext->skip[type] = ext1->skip[type];
602 ext1->skip[type] = NULL;
603 return ext;
606 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
607 !multi_pw_aff_is_affine(ext2->skip[type]))
608 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
609 isl_error_internal, "can only combine affine skips",
610 goto error);
612 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
613 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
614 skip = pw_aff_or(skip1, skip2);
615 isl_multi_pw_aff_free(ext1->skip[type]);
616 ext1->skip[type] = NULL;
617 isl_multi_pw_aff_free(ext2->skip[type]);
618 ext2->skip[type] = NULL;
619 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
620 if (!ext->skip[type])
621 goto error;
623 return ext;
624 error:
625 pet_scop_free(&ext->scop);
626 return NULL;
629 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
630 * where type takes on the values pet_skip_now and pet_skip_later.
631 * scop may be equal to either scop1 or scop2.
633 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
634 struct pet_scop *scop1, struct pet_scop *scop2)
636 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
637 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
638 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
640 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
641 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
642 return &ext->scop;
645 /* Update start and end of scop->loc to include the region from "start"
646 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
647 * does not have any offset information yet and we simply take the information
648 * from "start" and "end". Otherwise, we update loc using "start" and "end".
650 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
651 unsigned start, unsigned end)
653 if (!scop)
654 return NULL;
656 if (scop->loc == &pet_loc_dummy)
657 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
658 start, end, -1, strdup(""));
659 else
660 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
662 if (!scop->loc)
663 return pet_scop_free(scop);
665 return scop;
668 /* Update start and end of scop->loc to include the region identified
669 * by "loc".
671 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
672 __isl_keep pet_loc *loc)
674 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
675 pet_loc_get_end(loc));
678 /* Replace the location of "scop" by "loc".
680 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
681 __isl_take pet_loc *loc)
683 if (!scop || !loc)
684 goto error;
686 pet_loc_free(scop->loc);
687 scop->loc = loc;
689 return scop;
690 error:
691 pet_loc_free(loc);
692 pet_scop_free(scop);
693 return NULL;
696 /* Does "implication" appear in the list of implications of "scop"?
698 static int is_known_implication(struct pet_scop *scop,
699 struct pet_implication *implication)
701 int i;
703 for (i = 0; i < scop->n_implication; ++i) {
704 struct pet_implication *pi = scop->implications[i];
705 int equal;
707 if (pi->satisfied != implication->satisfied)
708 continue;
709 equal = isl_map_is_equal(pi->extension, implication->extension);
710 if (equal < 0)
711 return -1;
712 if (equal)
713 return 1;
716 return 0;
719 /* Store the concatenation of the implications of "scop1" and "scop2"
720 * in "scop", removing duplicates (i.e., implications in "scop2" that
721 * already appear in "scop1").
723 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
724 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
726 int i, j;
728 if (!scop)
729 return NULL;
731 if (scop2->n_implication == 0) {
732 scop->n_implication = scop1->n_implication;
733 scop->implications = scop1->implications;
734 scop1->n_implication = 0;
735 scop1->implications = NULL;
736 return scop;
739 if (scop1->n_implication == 0) {
740 scop->n_implication = scop2->n_implication;
741 scop->implications = scop2->implications;
742 scop2->n_implication = 0;
743 scop2->implications = NULL;
744 return scop;
747 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
748 scop1->n_implication + scop2->n_implication);
749 if (!scop->implications)
750 return pet_scop_free(scop);
752 for (i = 0; i < scop1->n_implication; ++i) {
753 scop->implications[i] = scop1->implications[i];
754 scop1->implications[i] = NULL;
757 scop->n_implication = scop1->n_implication;
758 j = scop1->n_implication;
759 for (i = 0; i < scop2->n_implication; ++i) {
760 int known;
762 known = is_known_implication(scop, scop2->implications[i]);
763 if (known < 0)
764 return pet_scop_free(scop);
765 if (known)
766 continue;
767 scop->implications[j++] = scop2->implications[i];
768 scop2->implications[i] = NULL;
770 scop->n_implication = j;
772 return scop;
775 /* Combine the offset information of "scop1" and "scop2" into "scop".
777 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
778 struct pet_scop *scop1, struct pet_scop *scop2)
780 if (scop1->loc != &pet_loc_dummy)
781 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
782 if (scop2->loc != &pet_loc_dummy)
783 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
784 return scop;
787 /* Create and return an independence that filters out the dependences
788 * in "filter" with local variables "local".
790 static struct pet_independence *new_independence(
791 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
793 isl_ctx *ctx;
794 struct pet_independence *independence;
796 if (!filter || !local)
797 goto error;
798 ctx = isl_union_map_get_ctx(filter);
799 independence = isl_alloc_type(ctx, struct pet_independence);
800 if (!independence)
801 goto error;
803 independence->filter = filter;
804 independence->local = local;
806 return independence;
807 error:
808 isl_union_map_free(filter);
809 isl_union_set_free(local);
810 return NULL;
813 /* Add an independence that filters out the dependences
814 * in "filter" with local variables "local" to "scop".
816 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
817 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
819 isl_ctx *ctx;
820 struct pet_independence *independence;
821 struct pet_independence **independences;
823 ctx = isl_union_map_get_ctx(filter);
824 independence = new_independence(filter, local);
825 if (!scop || !independence)
826 goto error;
828 independences = isl_realloc_array(ctx, scop->independences,
829 struct pet_independence *,
830 scop->n_independence + 1);
831 if (!independences)
832 goto error;
833 scop->independences = independences;
834 scop->independences[scop->n_independence] = independence;
835 scop->n_independence++;
837 return scop;
838 error:
839 pet_independence_free(independence);
840 pet_scop_free(scop);
841 return NULL;
844 /* Store the concatenation of the independences of "scop1" and "scop2"
845 * in "scop".
847 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
848 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
850 int i, off;
852 if (!scop)
853 return NULL;
855 if (scop2->n_independence == 0) {
856 scop->n_independence = scop1->n_independence;
857 scop->independences = scop1->independences;
858 scop1->n_independence = 0;
859 scop1->independences = NULL;
860 return scop;
863 if (scop1->n_independence == 0) {
864 scop->n_independence = scop2->n_independence;
865 scop->independences = scop2->independences;
866 scop2->n_independence = 0;
867 scop2->independences = NULL;
868 return scop;
871 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
872 scop1->n_independence + scop2->n_independence);
873 if (!scop->independences)
874 return pet_scop_free(scop);
876 for (i = 0; i < scop1->n_independence; ++i) {
877 scop->independences[i] = scop1->independences[i];
878 scop1->independences[i] = NULL;
881 off = scop1->n_independence;
882 for (i = 0; i < scop2->n_independence; ++i) {
883 scop->independences[off + i] = scop2->independences[i];
884 scop2->independences[i] = NULL;
886 scop->n_independence = scop1->n_independence + scop2->n_independence;
888 return scop;
891 /* Construct a pet_scop with the given schedule
892 * that contains the offset information,
893 * arrays, statements and skip information in "scop1" and "scop2".
895 static struct pet_scop *pet_scop_add(isl_ctx *ctx,
896 __isl_take isl_schedule *schedule, struct pet_scop *scop1,
897 struct pet_scop *scop2)
899 int i;
900 isl_space *space;
901 struct pet_scop *scop = NULL;
903 if (!scop1 || !scop2)
904 goto error;
906 if (scop1->n_stmt == 0) {
907 scop2 = scop_combine_skips(scop2, scop1, scop2);
908 pet_scop_free(scop1);
909 isl_schedule_free(schedule);
910 return scop2;
913 if (scop2->n_stmt == 0) {
914 scop1 = scop_combine_skips(scop1, scop1, scop2);
915 pet_scop_free(scop2);
916 isl_schedule_free(schedule);
917 return scop1;
920 space = isl_set_get_space(scop1->context);
921 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
922 isl_schedule_copy(schedule));
923 if (!scop)
924 goto error;
926 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
927 scop1->n_array + scop2->n_array);
928 if (!scop->arrays)
929 goto error;
930 scop->n_array = scop1->n_array + scop2->n_array;
932 for (i = 0; i < scop1->n_stmt; ++i) {
933 scop->stmts[i] = scop1->stmts[i];
934 scop1->stmts[i] = NULL;
937 for (i = 0; i < scop2->n_stmt; ++i) {
938 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
939 scop2->stmts[i] = NULL;
942 for (i = 0; i < scop1->n_array; ++i) {
943 scop->arrays[i] = scop1->arrays[i];
944 scop1->arrays[i] = NULL;
947 for (i = 0; i < scop2->n_array; ++i) {
948 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
949 scop2->arrays[i] = NULL;
952 scop = scop_collect_implications(ctx, scop, scop1, scop2);
953 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
954 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
955 scop = scop_combine_skips(scop, scop1, scop2);
956 scop = scop_combine_start_end(scop, scop1, scop2);
957 scop = scop_collect_independences(ctx, scop, scop1, scop2);
959 pet_scop_free(scop1);
960 pet_scop_free(scop2);
961 isl_schedule_free(schedule);
962 return scop;
963 error:
964 pet_scop_free(scop1);
965 pet_scop_free(scop2);
966 pet_scop_free(scop);
967 isl_schedule_free(schedule);
968 return NULL;
971 /* Apply the skip condition "skip" to "scop".
972 * That is, make sure "scop" is not executed when the condition holds.
974 * If "skip" is an affine expression, we add the conditions under
975 * which the expression is zero to the context and the skip conditions
976 * of "scop".
977 * Otherwise, we add a filter on the variable attaining the value zero.
979 static struct pet_scop *restrict_skip(struct pet_scop *scop,
980 __isl_take isl_multi_pw_aff *skip)
982 isl_set *zero;
983 isl_pw_aff *pa;
984 int is_aff;
986 if (!scop || !skip)
987 goto error;
989 is_aff = multi_pw_aff_is_affine(skip);
990 if (is_aff < 0)
991 goto error;
993 if (!is_aff)
994 return pet_scop_filter(scop, skip, 0);
996 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
997 isl_multi_pw_aff_free(skip);
998 zero = isl_pw_aff_zero_set(pa);
999 scop = pet_scop_restrict(scop, zero);
1001 return scop;
1002 error:
1003 isl_multi_pw_aff_free(skip);
1004 return pet_scop_free(scop);
1007 /* Construct a pet_scop that contains the arrays, statements and
1008 * skip information in "scop1" and "scop2", where the two scops
1009 * are executed "in sequence". That is, breaks and continues
1010 * in scop1 have an effect on scop2 and the schedule of the result
1011 * is the sequence of the schedules of "scop1" and "scop2".
1013 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1014 struct pet_scop *scop2)
1016 isl_schedule *schedule;
1018 if (!scop1 || !scop2)
1019 goto error;
1021 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1022 scop2 = restrict_skip(scop2,
1023 pet_scop_get_skip(scop1, pet_skip_now));
1024 schedule = isl_schedule_sequence(isl_schedule_copy(scop1->schedule),
1025 isl_schedule_copy(scop2->schedule));
1026 return pet_scop_add(ctx, schedule, scop1, scop2);
1027 error:
1028 pet_scop_free(scop1);
1029 pet_scop_free(scop2);
1030 return NULL;
1033 /* Construct a pet_scop that contains the arrays, statements and
1034 * skip information in "scop1" and "scop2", where the two scops
1035 * are executed "in parallel". That is, any break or continue
1036 * in scop1 has no effect on scop2 and the schedule of the result
1037 * is the set of the schedules of "scop1" and "scop2".
1039 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1040 struct pet_scop *scop2)
1042 isl_schedule *schedule;
1044 if (!scop1 || !scop2)
1045 goto error;
1047 schedule = isl_schedule_set(isl_schedule_copy(scop1->schedule),
1048 isl_schedule_copy(scop2->schedule));
1049 return pet_scop_add(ctx, schedule, scop1, scop2);
1050 error:
1051 pet_scop_free(scop1);
1052 pet_scop_free(scop2);
1053 return NULL;
1056 void *pet_implication_free(struct pet_implication *implication)
1058 int i;
1060 if (!implication)
1061 return NULL;
1063 isl_map_free(implication->extension);
1065 free(implication);
1066 return NULL;
1069 void *pet_independence_free(struct pet_independence *independence)
1071 if (!independence)
1072 return NULL;
1074 isl_union_map_free(independence->filter);
1075 isl_union_set_free(independence->local);
1077 free(independence);
1078 return NULL;
1081 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1083 int i;
1084 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1086 if (!scop)
1087 return NULL;
1088 pet_loc_free(scop->loc);
1089 isl_set_free(scop->context);
1090 isl_set_free(scop->context_value);
1091 isl_schedule_free(scop->schedule);
1092 if (scop->types)
1093 for (i = 0; i < scop->n_type; ++i)
1094 pet_type_free(scop->types[i]);
1095 free(scop->types);
1096 if (scop->arrays)
1097 for (i = 0; i < scop->n_array; ++i)
1098 pet_array_free(scop->arrays[i]);
1099 free(scop->arrays);
1100 if (scop->stmts)
1101 for (i = 0; i < scop->n_stmt; ++i)
1102 pet_stmt_free(scop->stmts[i]);
1103 free(scop->stmts);
1104 if (scop->implications)
1105 for (i = 0; i < scop->n_implication; ++i)
1106 pet_implication_free(scop->implications[i]);
1107 free(scop->implications);
1108 if (scop->independences)
1109 for (i = 0; i < scop->n_independence; ++i)
1110 pet_independence_free(scop->independences[i]);
1111 free(scop->independences);
1112 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1113 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1114 free(scop);
1115 return NULL;
1118 void pet_type_dump(struct pet_type *type)
1120 if (!type)
1121 return;
1123 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1126 void pet_implication_dump(struct pet_implication *implication)
1128 if (!implication)
1129 return;
1131 fprintf(stderr, "%d\n", implication->satisfied);
1132 isl_map_dump(implication->extension);
1135 void pet_scop_dump(struct pet_scop *scop)
1137 int i;
1138 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1140 if (!scop)
1141 return;
1143 isl_set_dump(scop->context);
1144 isl_set_dump(scop->context_value);
1145 isl_schedule_dump(scop->schedule);
1146 for (i = 0; i < scop->n_type; ++i)
1147 pet_type_dump(scop->types[i]);
1148 for (i = 0; i < scop->n_array; ++i)
1149 pet_array_dump(scop->arrays[i]);
1150 for (i = 0; i < scop->n_stmt; ++i)
1151 pet_stmt_dump(scop->stmts[i]);
1152 for (i = 0; i < scop->n_implication; ++i)
1153 pet_implication_dump(scop->implications[i]);
1155 if (ext->skip[0]) {
1156 fprintf(stderr, "skip\n");
1157 isl_multi_pw_aff_dump(ext->skip[0]);
1158 isl_multi_pw_aff_dump(ext->skip[1]);
1162 /* Return 1 if the two pet_arrays are equivalent.
1164 * We don't compare element_size as this may be target dependent.
1166 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1168 if (!array1 || !array2)
1169 return 0;
1171 if (!isl_set_is_equal(array1->context, array2->context))
1172 return 0;
1173 if (!isl_set_is_equal(array1->extent, array2->extent))
1174 return 0;
1175 if (!!array1->value_bounds != !!array2->value_bounds)
1176 return 0;
1177 if (array1->value_bounds &&
1178 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1179 return 0;
1180 if (strcmp(array1->element_type, array2->element_type))
1181 return 0;
1182 if (array1->element_is_record != array2->element_is_record)
1183 return 0;
1184 if (array1->live_out != array2->live_out)
1185 return 0;
1186 if (array1->uniquely_defined != array2->uniquely_defined)
1187 return 0;
1188 if (array1->declared != array2->declared)
1189 return 0;
1190 if (array1->exposed != array2->exposed)
1191 return 0;
1193 return 1;
1196 /* Return 1 if the two pet_stmts are equivalent.
1198 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1200 int i;
1202 if (!stmt1 || !stmt2)
1203 return 0;
1205 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1206 return 0;
1207 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1208 return 0;
1209 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1210 return 0;
1211 if (stmt1->n_arg != stmt2->n_arg)
1212 return 0;
1213 for (i = 0; i < stmt1->n_arg; ++i) {
1214 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1215 return 0;
1218 return 1;
1221 /* Return 1 if the two pet_types are equivalent.
1223 * We only compare the names of the types since the exact representation
1224 * of the definition may depend on the version of clang being used.
1226 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1228 if (!type1 || !type2)
1229 return 0;
1231 if (strcmp(type1->name, type2->name))
1232 return 0;
1234 return 1;
1237 /* Return 1 if the two pet_implications are equivalent.
1239 int pet_implication_is_equal(struct pet_implication *implication1,
1240 struct pet_implication *implication2)
1242 if (!implication1 || !implication2)
1243 return 0;
1245 if (implication1->satisfied != implication2->satisfied)
1246 return 0;
1247 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1248 return 0;
1250 return 1;
1253 /* Return 1 if the two pet_independences are equivalent.
1255 int pet_independence_is_equal(struct pet_independence *independence1,
1256 struct pet_independence *independence2)
1258 if (!independence1 || !independence2)
1259 return 0;
1261 if (!isl_union_map_is_equal(independence1->filter,
1262 independence2->filter))
1263 return 0;
1264 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1265 return 0;
1267 return 1;
1270 /* Return 1 if the two pet_scops are equivalent.
1272 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1274 int i;
1275 int equal;
1277 if (!scop1 || !scop2)
1278 return 0;
1280 if (!isl_set_is_equal(scop1->context, scop2->context))
1281 return 0;
1282 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1283 return 0;
1284 equal = isl_schedule_plain_is_equal(scop1->schedule, scop2->schedule);
1285 if (equal < 0)
1286 return -1;
1287 if (!equal)
1288 return 0;
1290 if (scop1->n_type != scop2->n_type)
1291 return 0;
1292 for (i = 0; i < scop1->n_type; ++i)
1293 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1294 return 0;
1296 if (scop1->n_array != scop2->n_array)
1297 return 0;
1298 for (i = 0; i < scop1->n_array; ++i)
1299 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1300 return 0;
1302 if (scop1->n_stmt != scop2->n_stmt)
1303 return 0;
1304 for (i = 0; i < scop1->n_stmt; ++i)
1305 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1306 return 0;
1308 if (scop1->n_implication != scop2->n_implication)
1309 return 0;
1310 for (i = 0; i < scop1->n_implication; ++i)
1311 if (!pet_implication_is_equal(scop1->implications[i],
1312 scop2->implications[i]))
1313 return 0;
1315 if (scop1->n_independence != scop2->n_independence)
1316 return 0;
1317 for (i = 0; i < scop1->n_independence; ++i)
1318 if (!pet_independence_is_equal(scop1->independences[i],
1319 scop2->independences[i]))
1320 return 0;
1322 return 1;
1325 /* Does the set "extent" reference a virtual array, i.e.,
1326 * one with user pointer equal to NULL?
1327 * A virtual array does not have any members.
1329 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1331 isl_id *id;
1332 int is_virtual;
1334 if (!isl_set_has_tuple_id(extent))
1335 return 0;
1336 if (isl_set_is_wrapping(extent))
1337 return 0;
1338 id = isl_set_get_tuple_id(extent);
1339 is_virtual = !isl_id_get_user(id);
1340 isl_id_free(id);
1342 return is_virtual;
1345 /* Intersect the initial dimensions of "array" with "domain", provided
1346 * that "array" represents a virtual array.
1348 * If "array" is virtual, then We take the preimage of "domain"
1349 * over the projection of the extent of "array" onto its initial dimensions
1350 * and intersect this extent with the result.
1352 static struct pet_array *virtual_array_intersect_domain_prefix(
1353 struct pet_array *array, __isl_take isl_set *domain)
1355 int n;
1356 isl_space *space;
1357 isl_multi_aff *ma;
1359 if (!array || !extent_is_virtual_array(array->extent)) {
1360 isl_set_free(domain);
1361 return array;
1364 space = isl_set_get_space(array->extent);
1365 n = isl_set_dim(domain, isl_dim_set);
1366 ma = pet_prefix_projection(space, n);
1367 domain = isl_set_preimage_multi_aff(domain, ma);
1369 array->extent = isl_set_intersect(array->extent, domain);
1370 if (!array->extent)
1371 return pet_array_free(array);
1373 return array;
1376 /* Intersect the initial dimensions of the domain of "stmt"
1377 * with "domain".
1379 * We take the preimage of "domain" over the projection of the
1380 * domain of "stmt" onto its initial dimensions and intersect
1381 * the domain of "stmt" with the result.
1383 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1384 __isl_take isl_set *domain)
1386 int n;
1387 isl_space *space;
1388 isl_multi_aff *ma;
1390 if (!stmt)
1391 goto error;
1393 space = isl_set_get_space(stmt->domain);
1394 n = isl_set_dim(domain, isl_dim_set);
1395 ma = pet_prefix_projection(space, n);
1396 domain = isl_set_preimage_multi_aff(domain, ma);
1398 stmt->domain = isl_set_intersect(stmt->domain, domain);
1399 if (!stmt->domain)
1400 return pet_stmt_free(stmt);
1402 return stmt;
1403 error:
1404 isl_set_free(domain);
1405 return pet_stmt_free(stmt);
1408 /* Intersect the initial dimensions of the domain of "implication"
1409 * with "domain".
1411 * We take the preimage of "domain" over the projection of the
1412 * domain of "implication" onto its initial dimensions and intersect
1413 * the domain of "implication" with the result.
1415 static struct pet_implication *implication_intersect_domain_prefix(
1416 struct pet_implication *implication, __isl_take isl_set *domain)
1418 int n;
1419 isl_space *space;
1420 isl_multi_aff *ma;
1422 if (!implication)
1423 goto error;
1425 space = isl_map_get_space(implication->extension);
1426 n = isl_set_dim(domain, isl_dim_set);
1427 ma = pet_prefix_projection(isl_space_domain(space), n);
1428 domain = isl_set_preimage_multi_aff(domain, ma);
1430 implication->extension =
1431 isl_map_intersect_domain(implication->extension, domain);
1432 if (!implication->extension)
1433 return pet_implication_free(implication);
1435 return implication;
1436 error:
1437 isl_set_free(domain);
1438 return pet_implication_free(implication);
1441 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1443 * The extents of the virtual arrays match the iteration domains,
1444 * so if the iteration domain changes, we need to change those extents too.
1446 * The domain of the schedule is intersected with (i.e., replaced by)
1447 * the union of the updated iteration domains.
1449 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1450 __isl_take isl_set *domain)
1452 int i;
1454 if (!scop)
1455 goto error;
1457 for (i = 0; i < scop->n_array; ++i) {
1458 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1459 scop->arrays[i], isl_set_copy(domain));
1460 if (!scop->arrays[i])
1461 goto error;
1464 for (i = 0; i < scop->n_stmt; ++i) {
1465 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1466 isl_set_copy(domain));
1467 if (!scop->stmts[i])
1468 goto error;
1471 for (i = 0; i < scop->n_implication; ++i) {
1472 scop->implications[i] =
1473 implication_intersect_domain_prefix(scop->implications[i],
1474 isl_set_copy(domain));
1475 if (!scop->implications[i])
1476 return pet_scop_free(scop);
1479 scop->schedule = isl_schedule_intersect_domain(scop->schedule,
1480 pet_scop_get_instance_set(scop));
1481 if (!scop->schedule)
1482 goto error;
1484 isl_set_free(domain);
1485 return scop;
1486 error:
1487 isl_set_free(domain);
1488 return pet_scop_free(scop);
1491 /* Update the context with respect to an embedding into a loop
1492 * with iteration domain "dom".
1493 * The input context lives in the same space as "dom".
1494 * The output context has the inner dimension removed.
1496 * An outer loop iterator value is invalid for the embedding if
1497 * any of the corresponding inner iterator values is invalid.
1498 * That is, an outer loop iterator value is valid only if all the corresponding
1499 * inner iterator values are valid.
1500 * We therefore compute the set of outer loop iterators l
1502 * forall i: dom(l,i) => valid(l,i)
1504 * or
1506 * forall i: not dom(l,i) or valid(l,i)
1508 * or
1510 * not exists i: dom(l,i) and not valid(l,i)
1512 * i.e.,
1514 * not exists i: (dom \ valid)(l,i)
1516 * If there are any unnamed parameters in "dom", then we consider
1517 * a parameter value to be valid if it is valid for any value of those
1518 * unnamed parameters. They are therefore projected out at the end.
1520 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1521 __isl_keep isl_set *dom)
1523 int pos;
1525 pos = isl_set_dim(context, isl_dim_set) - 1;
1526 context = isl_set_subtract(isl_set_copy(dom), context);
1527 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1528 context = isl_set_complement(context);
1529 context = pet_nested_remove_from_set(context);
1531 return context;
1534 /* Update the implication with respect to an embedding into a loop
1535 * with iteration domain "dom".
1537 * Since embed_access extends virtual arrays along with the domain
1538 * of the access, we need to do the same with domain and range
1539 * of the implication. Since the original implication is only valid
1540 * within a given iteration of the loop, the extended implication
1541 * maps the extra array dimension corresponding to the extra loop
1542 * to itself.
1544 static struct pet_implication *pet_implication_embed(
1545 struct pet_implication *implication, __isl_take isl_set *dom)
1547 isl_id *id;
1548 isl_map *map;
1550 if (!implication)
1551 goto error;
1553 map = isl_set_identity(dom);
1554 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1555 map = isl_map_flat_product(map, implication->extension);
1556 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1557 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1558 implication->extension = map;
1559 if (!implication->extension)
1560 return pet_implication_free(implication);
1562 return implication;
1563 error:
1564 isl_set_free(dom);
1565 return NULL;
1568 /* Internal data structure for outer_projection_mupa.
1570 * "n" is the number of outer dimensions onto which to project.
1571 * "res" collects the result.
1573 struct pet_outer_projection_data {
1574 int n;
1575 isl_union_pw_multi_aff *res;
1578 /* Create a function that maps "set" onto its outer data->n dimensions and
1579 * add it to data->res.
1581 static isl_stat add_outer_projection(__isl_take isl_set *set, void *user)
1583 struct pet_outer_projection_data *data = user;
1584 int dim;
1585 isl_space *space;
1586 isl_pw_multi_aff *pma;
1588 dim = isl_set_dim(set, isl_dim_set);
1589 space = isl_set_get_space(set);
1590 pma = isl_pw_multi_aff_project_out_map(space,
1591 isl_dim_set, data->n, dim - data->n);
1592 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
1594 isl_set_free(set);
1596 return isl_stat_ok;
1599 /* Create and return a function that maps the sets in "domain"
1600 * onto their outer "n" dimensions.
1602 static __isl_give isl_multi_union_pw_aff *outer_projection_mupa(
1603 __isl_take isl_union_set *domain, int n)
1605 struct pet_outer_projection_data data;
1606 isl_space *space;
1608 space = isl_union_set_get_space(domain);
1609 data.n = n;
1610 data.res = isl_union_pw_multi_aff_empty(space);
1611 if (isl_union_set_foreach_set(domain, &add_outer_projection, &data) < 0)
1612 data.res = isl_union_pw_multi_aff_free(data.res);
1614 isl_union_set_free(domain);
1615 return isl_multi_union_pw_aff_from_union_pw_multi_aff(data.res);
1618 /* Embed "schedule" in a loop with schedule "prefix".
1619 * The domain of "prefix" corresponds to the outer dimensions
1620 * of the iteration domains.
1621 * We therefore construct a projection onto these outer dimensions,
1622 * compose it with "prefix" and then add the result as a band schedule.
1624 * If the domain of the schedule is empty, then there is no need
1625 * to insert any node.
1627 static __isl_give isl_schedule *schedule_embed(
1628 __isl_take isl_schedule *schedule, __isl_keep isl_multi_aff *prefix)
1630 int n;
1631 int empty;
1632 isl_union_set *domain;
1633 isl_multi_aff *ma;
1634 isl_multi_union_pw_aff *mupa;
1636 domain = isl_schedule_get_domain(schedule);
1637 empty = isl_union_set_is_empty(domain);
1638 if (empty < 0 || empty) {
1639 isl_union_set_free(domain);
1640 return empty < 0 ? isl_schedule_free(schedule) : schedule;
1643 n = isl_multi_aff_dim(prefix, isl_dim_in);
1644 mupa = outer_projection_mupa(domain, n);
1645 ma = isl_multi_aff_copy(prefix);
1646 mupa = isl_multi_union_pw_aff_apply_multi_aff(mupa, ma);
1647 schedule = isl_schedule_insert_partial_schedule(schedule, mupa);
1649 return schedule;
1652 /* Adjust the context and the schedule according to an embedding
1653 * in a loop with iteration domain "dom" and schedule "sched".
1655 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1656 __isl_take isl_multi_aff *sched)
1658 int i;
1660 if (!scop)
1661 goto error;
1663 scop->context = context_embed(scop->context, dom);
1664 if (!scop->context)
1665 goto error;
1667 scop->schedule = schedule_embed(scop->schedule, sched);
1668 if (!scop->schedule)
1669 goto error;
1671 isl_set_free(dom);
1672 isl_multi_aff_free(sched);
1673 return scop;
1674 error:
1675 isl_set_free(dom);
1676 isl_multi_aff_free(sched);
1677 return pet_scop_free(scop);
1680 /* Add extra conditions to scop->skip[type].
1682 * The new skip condition only holds if it held before
1683 * and the condition is true. It does not hold if it did not hold
1684 * before or the condition is false.
1686 * The skip condition is assumed to be an affine expression.
1688 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1689 enum pet_skip type, __isl_keep isl_set *cond)
1691 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1692 isl_pw_aff *skip;
1693 isl_set *dom;
1695 if (!scop)
1696 return NULL;
1697 if (!ext->skip[type])
1698 return scop;
1700 if (!multi_pw_aff_is_affine(ext->skip[type]))
1701 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1702 isl_error_internal, "can only restrict affine skips",
1703 return pet_scop_free(scop));
1705 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1706 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1707 cond = isl_set_copy(cond);
1708 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1709 skip = indicator_function(cond, dom);
1710 isl_multi_pw_aff_free(ext->skip[type]);
1711 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1712 if (!ext->skip[type])
1713 return pet_scop_free(scop);
1715 return scop;
1718 /* Adjust the context and the skip conditions to the fact that
1719 * the scop was created in a context where "cond" holds.
1721 * An outer loop iterator or parameter value is valid for the result
1722 * if it was valid for the original scop and satisfies "cond" or if it does
1723 * not satisfy "cond" as in this case the scop is not executed
1724 * and the original constraints on these values are irrelevant.
1726 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1727 __isl_take isl_set *cond)
1729 int i;
1731 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1732 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1734 if (!scop)
1735 goto error;
1737 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1738 scop->context = isl_set_union(scop->context,
1739 isl_set_complement(isl_set_copy(cond)));
1740 scop->context = isl_set_coalesce(scop->context);
1741 scop->context = pet_nested_remove_from_set(scop->context);
1742 if (!scop->context)
1743 goto error;
1745 isl_set_free(cond);
1746 return scop;
1747 error:
1748 isl_set_free(cond);
1749 return pet_scop_free(scop);
1752 /* Insert an argument expression corresponding to "test" in front
1753 * of the list of arguments described by *n_arg and *args.
1755 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1756 __isl_keep isl_multi_pw_aff *test)
1758 int i;
1759 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1761 if (!test)
1762 return -1;
1764 if (!*args) {
1765 *args = isl_calloc_array(ctx, pet_expr *, 1);
1766 if (!*args)
1767 return -1;
1768 } else {
1769 pet_expr **ext;
1770 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1771 if (!ext)
1772 return -1;
1773 for (i = 0; i < *n_arg; ++i)
1774 ext[1 + i] = (*args)[i];
1775 free(*args);
1776 *args = ext;
1778 (*n_arg)++;
1779 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1780 if (!(*args)[0])
1781 return -1;
1783 return 0;
1786 /* Look through the applications in "scop" for any that can be
1787 * applied to the filter expressed by "map" and "satisified".
1788 * If there is any, then apply it to "map" and return the result.
1789 * Otherwise, return "map".
1790 * "id" is the identifier of the virtual array.
1792 * We only introduce at most one implication for any given virtual array,
1793 * so we can apply the implication and return as soon as we find one.
1795 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1796 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1798 int i;
1800 for (i = 0; i < scop->n_implication; ++i) {
1801 struct pet_implication *pi = scop->implications[i];
1802 isl_id *pi_id;
1804 if (pi->satisfied != satisfied)
1805 continue;
1806 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1807 isl_id_free(pi_id);
1808 if (pi_id != id)
1809 continue;
1811 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1814 return map;
1817 /* Is the filter expressed by "test" and "satisfied" implied
1818 * by filter "pos" on "domain", with filter "expr", taking into
1819 * account the implications of "scop"?
1821 * For filter on domain implying that expressed by "test" and "satisfied",
1822 * the filter needs to be an access to the same (virtual) array as "test" and
1823 * the filter value needs to be equal to "satisfied".
1824 * Moreover, the filter access relation, possibly extended by
1825 * the implications in "scop" needs to contain "test".
1827 static int implies_filter(struct pet_scop *scop,
1828 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1829 __isl_keep isl_map *test, int satisfied)
1831 isl_id *test_id, *arg_id;
1832 isl_val *val;
1833 int is_int;
1834 int s;
1835 int is_subset;
1836 isl_map *implied;
1838 if (expr->type != pet_expr_access)
1839 return 0;
1840 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1841 arg_id = pet_expr_access_get_id(expr);
1842 isl_id_free(arg_id);
1843 isl_id_free(test_id);
1844 if (test_id != arg_id)
1845 return 0;
1846 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1847 is_int = isl_val_is_int(val);
1848 if (is_int)
1849 s = isl_val_get_num_si(val);
1850 isl_val_free(val);
1851 if (!val)
1852 return -1;
1853 if (!is_int)
1854 return 0;
1855 if (s != satisfied)
1856 return 0;
1858 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1859 implied = apply_implications(scop, implied, test_id, satisfied);
1860 is_subset = isl_map_is_subset(test, implied);
1861 isl_map_free(implied);
1863 return is_subset;
1866 /* Is the filter expressed by "test" and "satisfied" implied
1867 * by any of the filters on the domain of "stmt", taking into
1868 * account the implications of "scop"?
1870 static int filter_implied(struct pet_scop *scop,
1871 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1873 int i;
1874 int implied;
1875 isl_id *test_id;
1876 isl_map *domain;
1877 isl_map *test_map;
1879 if (!scop || !stmt || !test)
1880 return -1;
1881 if (scop->n_implication == 0)
1882 return 0;
1883 if (stmt->n_arg == 0)
1884 return 0;
1886 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1887 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1889 implied = 0;
1890 for (i = 0; i < stmt->n_arg; ++i) {
1891 implied = implies_filter(scop, domain, i, stmt->args[i],
1892 test_map, satisfied);
1893 if (implied < 0 || implied)
1894 break;
1897 isl_map_free(test_map);
1898 isl_map_free(domain);
1899 return implied;
1902 /* Make the statement "stmt" depend on the value of "test"
1903 * being equal to "satisfied" by adjusting stmt->domain.
1905 * The domain of "test" corresponds to the (zero or more) outer dimensions
1906 * of the iteration domain.
1908 * We first extend "test" to apply to the entire iteration domain and
1909 * then check if the filter that we are about to add is implied
1910 * by any of the current filters, possibly taking into account
1911 * the implications in "scop". If so, we leave "stmt" untouched and return.
1913 * Otherwise, we insert an argument corresponding to a read to "test"
1914 * from the iteration domain of "stmt" in front of the list of arguments.
1915 * We also insert a corresponding output dimension in the wrapped
1916 * map contained in stmt->domain, with value set to "satisfied".
1918 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1919 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1921 int i;
1922 int implied;
1923 isl_id *id;
1924 isl_ctx *ctx;
1925 isl_pw_multi_aff *pma;
1926 isl_multi_aff *add_dom;
1927 isl_space *space;
1928 isl_local_space *ls;
1929 int n_test_dom;
1931 if (!stmt || !test)
1932 goto error;
1934 space = pet_stmt_get_space(stmt);
1935 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1936 space = isl_space_from_domain(space);
1937 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1938 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1939 ls = isl_local_space_from_space(isl_space_domain(space));
1940 for (i = 0; i < n_test_dom; ++i) {
1941 isl_aff *aff;
1942 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1943 isl_dim_set, i);
1944 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1946 isl_local_space_free(ls);
1947 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1949 implied = filter_implied(scop, stmt, test, satisfied);
1950 if (implied < 0)
1951 goto error;
1952 if (implied) {
1953 isl_multi_pw_aff_free(test);
1954 return stmt;
1957 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1958 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1959 id, satisfied);
1960 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1962 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1963 goto error;
1965 isl_multi_pw_aff_free(test);
1966 return stmt;
1967 error:
1968 isl_multi_pw_aff_free(test);
1969 return pet_stmt_free(stmt);
1972 /* Does "scop" have a skip condition of the given "type"?
1974 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1976 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1978 if (!scop)
1979 return -1;
1980 return ext->skip[type] != NULL;
1983 /* Does "scop" have a skip condition of the given "type" that
1984 * is an affine expression?
1986 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1988 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1990 if (!scop)
1991 return -1;
1992 if (!ext->skip[type])
1993 return 0;
1994 return multi_pw_aff_is_affine(ext->skip[type]);
1997 /* Does "scop" have a skip condition of the given "type" that
1998 * is not an affine expression?
2000 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2002 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2003 int aff;
2005 if (!scop)
2006 return -1;
2007 if (!ext->skip[type])
2008 return 0;
2009 aff = multi_pw_aff_is_affine(ext->skip[type]);
2010 if (aff < 0)
2011 return -1;
2012 return !aff;
2015 /* Does "scop" have a skip condition of the given "type" that
2016 * is affine and holds on the entire domain?
2018 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2020 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2021 isl_pw_aff *pa;
2022 isl_set *set;
2023 int is_aff;
2024 int is_univ;
2026 is_aff = pet_scop_has_affine_skip(scop, type);
2027 if (is_aff < 0 || !is_aff)
2028 return is_aff;
2030 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2031 set = isl_pw_aff_non_zero_set(pa);
2032 is_univ = isl_set_plain_is_universe(set);
2033 isl_set_free(set);
2035 return is_univ;
2038 /* Replace scop->skip[type] by "skip".
2040 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2041 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2043 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2045 if (!scop || !skip)
2046 goto error;
2048 isl_multi_pw_aff_free(ext->skip[type]);
2049 ext->skip[type] = skip;
2051 return scop;
2052 error:
2053 isl_multi_pw_aff_free(skip);
2054 return pet_scop_free(scop);
2057 /* Return a copy of scop->skip[type].
2059 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2060 enum pet_skip type)
2062 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2064 if (!scop)
2065 return NULL;
2067 return isl_multi_pw_aff_copy(ext->skip[type]);
2070 /* Assuming scop->skip[type] is an affine expression,
2071 * return the constraints on the outer loop domain for which the skip condition
2072 * holds.
2074 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2075 enum pet_skip type)
2077 isl_multi_pw_aff *skip;
2078 isl_pw_aff *pa;
2080 skip = pet_scop_get_skip(scop, type);
2081 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2082 isl_multi_pw_aff_free(skip);
2083 return isl_pw_aff_non_zero_set(pa);
2086 /* Return the identifier of the variable that is accessed by
2087 * the skip condition of the given type.
2089 * The skip condition is assumed not to be an affine condition.
2091 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2092 enum pet_skip type)
2094 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2096 if (!scop)
2097 return NULL;
2099 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2102 /* Return an access pet_expr corresponding to the skip condition
2103 * of the given type.
2105 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2106 enum pet_skip type)
2108 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2111 /* Drop the skip condition scop->skip[type].
2113 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2115 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2117 if (!scop)
2118 return;
2120 isl_multi_pw_aff_free(ext->skip[type]);
2121 ext->skip[type] = NULL;
2124 /* Drop all skip conditions on "scop".
2126 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2128 pet_scop_reset_skip(scop, pet_skip_now);
2129 pet_scop_reset_skip(scop, pet_skip_later);
2131 return scop;
2134 /* Make the skip condition (if any) depend on the value of "test" being
2135 * equal to "satisfied".
2137 * We only support the case where the original skip condition is universal,
2138 * i.e., where skipping is unconditional, and where satisfied == 1.
2139 * In this case, the skip condition is changed to skip only when
2140 * "test" is equal to one.
2142 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2143 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2145 int is_univ = 0;
2147 if (!scop)
2148 return NULL;
2149 if (!pet_scop_has_skip(scop, type))
2150 return scop;
2152 if (satisfied)
2153 is_univ = pet_scop_has_universal_skip(scop, type);
2154 if (is_univ < 0)
2155 return pet_scop_free(scop);
2156 if (satisfied && is_univ) {
2157 isl_multi_pw_aff *skip;
2158 skip = isl_multi_pw_aff_copy(test);
2159 scop = pet_scop_set_skip(scop, type, skip);
2160 if (!scop)
2161 return NULL;
2162 } else {
2163 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2164 "skip expression cannot be filtered",
2165 return pet_scop_free(scop));
2168 return scop;
2171 /* Make all statements in "scop" depend on the value of "test"
2172 * being equal to "satisfied" by adjusting their domains.
2174 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2175 __isl_take isl_multi_pw_aff *test, int satisfied)
2177 int i;
2179 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2180 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2182 if (!scop || !test)
2183 goto error;
2185 for (i = 0; i < scop->n_stmt; ++i) {
2186 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2187 isl_multi_pw_aff_copy(test), satisfied);
2188 if (!scop->stmts[i])
2189 goto error;
2192 isl_multi_pw_aff_free(test);
2193 return scop;
2194 error:
2195 isl_multi_pw_aff_free(test);
2196 return pet_scop_free(scop);
2199 /* Add the parameters of the access expression "expr" to "space".
2201 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2203 int i;
2204 isl_space *expr_space;
2205 isl_space **space = user;
2207 expr_space = pet_expr_access_get_parameter_space(expr);
2208 *space = isl_space_align_params(*space, expr_space);
2210 return *space ? 0 : -1;
2213 /* Add all parameters in "stmt" to "space" and return the result.
2215 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2216 __isl_take isl_space *space)
2218 int i;
2220 if (!stmt)
2221 return isl_space_free(space);
2223 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2224 for (i = 0; i < stmt->n_arg; ++i)
2225 if (pet_expr_foreach_access_expr(stmt->args[i],
2226 &access_collect_params, &space) < 0)
2227 space = isl_space_free(space);
2228 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2229 &space) < 0)
2230 space = isl_space_free(space);
2232 return space;
2235 /* Add all parameters in "array" to "space" and return the result.
2237 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2238 __isl_take isl_space *space)
2240 if (!array)
2241 return isl_space_free(space);
2243 space = isl_space_align_params(space,
2244 isl_set_get_space(array->context));
2245 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2247 return space;
2250 /* Add all parameters in "independence" to "space" and return the result.
2252 static __isl_give isl_space *independence_collect_params(
2253 struct pet_independence *independence, __isl_take isl_space *space)
2255 if (!independence)
2256 return isl_space_free(space);
2258 space = isl_space_align_params(space,
2259 isl_union_map_get_space(independence->filter));
2260 space = isl_space_align_params(space,
2261 isl_union_set_get_space(independence->local));
2263 return space;
2266 /* Collect all parameters in "scop" in a parameter space and return the result.
2268 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2270 isl_space *space;
2271 int i;
2273 if (!scop)
2274 return NULL;
2276 space = isl_set_get_space(scop->context);
2278 for (i = 0; i < scop->n_array; ++i)
2279 space = array_collect_params(scop->arrays[i], space);
2281 for (i = 0; i < scop->n_stmt; ++i)
2282 space = stmt_collect_params(scop->stmts[i], space);
2284 for (i = 0; i < scop->n_independence; ++i)
2285 space = independence_collect_params(scop->independences[i],
2286 space);
2288 return space;
2291 /* Add all parameters in "space" to the domain and
2292 * all access relations in "stmt".
2294 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2295 __isl_take isl_space *space)
2297 int i;
2299 if (!stmt)
2300 goto error;
2302 stmt->domain = isl_set_align_params(stmt->domain,
2303 isl_space_copy(space));
2305 for (i = 0; i < stmt->n_arg; ++i) {
2306 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2307 isl_space_copy(space));
2308 if (!stmt->args[i])
2309 goto error;
2311 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2313 if (!stmt->domain || !stmt->body)
2314 goto error;
2316 isl_space_free(space);
2317 return stmt;
2318 error:
2319 isl_space_free(space);
2320 return pet_stmt_free(stmt);
2323 /* Add all parameters in "space" to "array".
2325 static struct pet_array *array_propagate_params(struct pet_array *array,
2326 __isl_take isl_space *space)
2328 if (!array)
2329 goto error;
2331 array->context = isl_set_align_params(array->context,
2332 isl_space_copy(space));
2333 array->extent = isl_set_align_params(array->extent,
2334 isl_space_copy(space));
2335 if (array->value_bounds) {
2336 array->value_bounds = isl_set_align_params(array->value_bounds,
2337 isl_space_copy(space));
2338 if (!array->value_bounds)
2339 goto error;
2342 if (!array->context || !array->extent)
2343 goto error;
2345 isl_space_free(space);
2346 return array;
2347 error:
2348 isl_space_free(space);
2349 return pet_array_free(array);
2352 /* Add all parameters in "space" to "independence".
2354 static struct pet_independence *independence_propagate_params(
2355 struct pet_independence *independence, __isl_take isl_space *space)
2357 if (!independence)
2358 goto error;
2360 independence->filter = isl_union_map_align_params(independence->filter,
2361 isl_space_copy(space));
2362 independence->local = isl_union_set_align_params(independence->local,
2363 isl_space_copy(space));
2364 if (!independence->filter || !independence->local)
2365 goto error;
2367 isl_space_free(space);
2368 return independence;
2369 error:
2370 isl_space_free(space);
2371 return pet_independence_free(independence);
2374 /* Add all parameters in "space" to "scop".
2376 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2377 __isl_take isl_space *space)
2379 int i;
2381 if (!scop)
2382 goto error;
2384 scop->context = isl_set_align_params(scop->context,
2385 isl_space_copy(space));
2386 scop->schedule = isl_schedule_align_params(scop->schedule,
2387 isl_space_copy(space));
2388 if (!scop->context || !scop->schedule)
2389 goto error;
2391 for (i = 0; i < scop->n_array; ++i) {
2392 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2393 isl_space_copy(space));
2394 if (!scop->arrays[i])
2395 goto error;
2398 for (i = 0; i < scop->n_stmt; ++i) {
2399 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2400 isl_space_copy(space));
2401 if (!scop->stmts[i])
2402 goto error;
2405 for (i = 0; i < scop->n_independence; ++i) {
2406 scop->independences[i] = independence_propagate_params(
2407 scop->independences[i], isl_space_copy(space));
2408 if (!scop->independences[i])
2409 goto error;
2412 isl_space_free(space);
2413 return scop;
2414 error:
2415 isl_space_free(space);
2416 return pet_scop_free(scop);
2419 /* Update all isl_sets and isl_maps in "scop" such that they all
2420 * have the same parameters.
2422 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2424 isl_space *space;
2426 if (!scop)
2427 return NULL;
2429 space = scop_collect_params(scop);
2431 scop = scop_propagate_params(scop, space);
2433 return scop;
2436 /* Add the access relation of the give "type" of the access expression "expr"
2437 * to "accesses" and return the result.
2438 * The domain of the access relation is intersected with "domain".
2439 * If "tag" is set, then the access relation is tagged with
2440 * the corresponding reference identifier.
2442 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2443 enum pet_expr_access_type type, int tag,
2444 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2446 isl_union_map *access;
2448 access = pet_expr_access_get_access(expr, type);
2449 access = isl_union_map_intersect_domain(access,
2450 isl_union_set_copy(domain));
2451 if (tag)
2452 access = pet_expr_tag_access(expr, access);
2453 return isl_union_map_union(accesses, access);
2456 /* Internal data structure for expr_collect_accesses.
2458 * "type" is the type of accesses we want to collect.
2459 * "tag" is set if the access relations should be tagged with
2460 * the corresponding reference identifiers.
2461 * "domain" are constraints on the domain of the access relations.
2462 * "accesses" collects the results.
2464 struct pet_expr_collect_accesses_data {
2465 enum pet_expr_access_type type;
2466 int tag;
2467 isl_union_set *domain;
2469 isl_union_map *accesses;
2472 /* Add the access relation of the access expression "expr"
2473 * to data->accesses if the access expression is a read and we are collecting
2474 * reads and/or it is a write and we are collecting writes.
2475 * The domains of the access relations are intersected with data->domain.
2476 * If data->tag is set, then the access relations are tagged with
2477 * the corresponding reference identifiers.
2479 * If data->type is pet_expr_access_must_write, then we only add
2480 * the accesses that are definitely performed. Otherwise, we add
2481 * all potential accesses.
2482 * In particular, if the access has any arguments, then in case of
2483 * pet_expr_access_must_write we currently skip the access completely.
2484 * In other cases, we project out the values of the access arguments.
2486 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2488 struct pet_expr_collect_accesses_data *data = user;
2489 int i;
2490 isl_id *id;
2491 isl_space *dim;
2493 if (!expr)
2494 return -1;
2496 if (pet_expr_is_affine(expr))
2497 return 0;
2498 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2499 return 0;
2501 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2502 ((data->type == pet_expr_access_may_write ||
2503 data->type == pet_expr_access_must_write) && expr->acc.write))
2504 data->accesses = expr_collect_access(expr,
2505 data->type, data->tag,
2506 data->accesses, data->domain);
2508 return data->accesses ? 0 : -1;
2511 /* Collect and return all access relations of the given "type" in "stmt".
2512 * If "tag" is set, then the access relations are tagged with
2513 * the corresponding reference identifiers.
2514 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2515 * we simply add the argument of the kill operation.
2517 * If we are looking for definite accesses (pet_expr_access_must_write
2518 * or pet_expr_access_killed), then we only add the accesses that are
2519 * definitely performed. Otherwise, we add all potential accesses.
2520 * In particular, if the statement has any arguments, then if we are looking
2521 * for definite accesses we currently skip the statement completely. Othewise,
2522 * we project out the values of the statement arguments.
2523 * If the statement body is not an expression tree, then we cannot
2524 * know for sure if/when the accesses inside the tree are performed.
2525 * We therefore ignore such statements when we are looking for
2526 * definite accesses.
2528 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2529 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2531 struct pet_expr_collect_accesses_data data = { type, tag };
2532 int must;
2533 isl_set *domain;
2535 if (!stmt)
2536 return NULL;
2538 data.accesses = isl_union_map_empty(dim);
2540 if (type == pet_expr_access_must_write ||
2541 type == pet_expr_access_killed)
2542 must = 1;
2543 else
2544 must = 0;
2546 if (must && stmt->n_arg > 0)
2547 return data.accesses;
2548 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2549 return data.accesses;
2551 domain = drop_arguments(isl_set_copy(stmt->domain));
2552 data.domain = isl_union_set_from_set(domain);
2554 if (type == pet_expr_access_killed) {
2555 pet_expr *body, *arg;
2557 body = pet_tree_expr_get_expr(stmt->body);
2558 arg = pet_expr_get_arg(body, 0);
2559 data.accesses = expr_collect_access(arg,
2560 pet_expr_access_killed, tag,
2561 data.accesses, data.domain);
2562 pet_expr_free(arg);
2563 pet_expr_free(body);
2564 } else if (pet_tree_foreach_access_expr(stmt->body,
2565 &expr_collect_accesses, &data) < 0)
2566 data.accesses = isl_union_map_free(data.accesses);
2568 isl_union_set_free(data.domain);
2570 return data.accesses;
2573 /* Is "stmt" an assignment statement?
2575 int pet_stmt_is_assign(struct pet_stmt *stmt)
2577 if (!stmt)
2578 return 0;
2579 return pet_tree_is_assign(stmt->body);
2582 /* Is "stmt" a kill statement?
2584 int pet_stmt_is_kill(struct pet_stmt *stmt)
2586 if (!stmt)
2587 return 0;
2588 return pet_tree_is_kill(stmt->body);
2591 /* Is "stmt" an assume statement?
2593 int pet_stmt_is_assume(struct pet_stmt *stmt)
2595 if (!stmt)
2596 return 0;
2597 return pet_tree_is_assume(stmt->body);
2600 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2602 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2603 __isl_keep isl_map *map, __isl_keep isl_set *set)
2605 isl_map *gist;
2607 gist = isl_map_copy(map);
2608 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2609 return isl_union_map_add_map(umap, gist);
2612 /* Compute a mapping from all arrays (of structs) in scop
2613 * to their members.
2615 * If "from_outermost" is set, then the domain only consists
2616 * of outermost arrays.
2617 * If "to_innermost" is set, then the range only consists
2618 * of innermost arrays.
2620 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2621 int from_outermost, int to_innermost)
2623 int i;
2624 isl_union_map *to_inner;
2626 if (!scop)
2627 return NULL;
2629 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2631 for (i = 0; i < scop->n_array; ++i) {
2632 struct pet_array *array = scop->arrays[i];
2633 isl_set *set;
2634 isl_map *map;
2636 if (to_innermost && array->element_is_record)
2637 continue;
2639 set = isl_set_copy(array->extent);
2640 map = isl_set_identity(isl_set_copy(set));
2642 while (set && isl_set_is_wrapping(set)) {
2643 isl_id *id;
2644 isl_map *wrapped;
2646 if (!from_outermost)
2647 to_inner = add_gisted(to_inner, map, set);
2649 id = isl_set_get_tuple_id(set);
2650 wrapped = isl_set_unwrap(set);
2651 wrapped = isl_map_domain_map(wrapped);
2652 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2653 map = isl_map_apply_domain(map, wrapped);
2654 set = isl_map_domain(isl_map_copy(map));
2657 map = isl_map_gist_domain(map, set);
2658 to_inner = isl_union_map_add_map(to_inner, map);
2661 return to_inner;
2664 /* Compute a mapping from all arrays (of structs) in scop
2665 * to their innermost arrays.
2667 * In particular, for each array of a primitive type, the result
2668 * contains the identity mapping on that array.
2669 * For each array involving member accesses, the result
2670 * contains a mapping from the elements of any intermediate array of structs
2671 * to all corresponding elements of the innermost nested arrays.
2673 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2674 struct pet_scop *scop)
2676 return compute_to_inner(scop, 0, 1);
2679 /* Compute a mapping from all outermost arrays (of structs) in scop
2680 * to their innermost members.
2682 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2684 return compute_to_inner(scop, 1, 1);
2687 /* Compute a mapping from all outermost arrays (of structs) in scop
2688 * to their members, including the outermost arrays themselves.
2690 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2692 return compute_to_inner(scop, 1, 0);
2695 /* Collect and return all access relations of the given "type" in "scop".
2696 * If "type" is pet_expr_access_killed, then we only add the arguments of
2697 * kill operations.
2698 * If we are looking for definite accesses (pet_expr_access_must_write
2699 * or pet_expr_access_killed), then we only add the accesses that are
2700 * definitely performed. Otherwise, we add all potential accesses.
2701 * If "tag" is set, then the access relations are tagged with
2702 * the corresponding reference identifiers.
2703 * For accesses to structures, the returned access relation accesses
2704 * all individual fields in the structures.
2706 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2707 enum pet_expr_access_type type, int tag)
2709 int i;
2710 isl_union_map *accesses;
2711 isl_union_set *arrays;
2712 isl_union_map *to_inner;
2714 if (!scop)
2715 return NULL;
2717 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2719 for (i = 0; i < scop->n_stmt; ++i) {
2720 struct pet_stmt *stmt = scop->stmts[i];
2721 isl_union_map *accesses_i;
2722 isl_space *space;
2724 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2725 continue;
2727 space = isl_set_get_space(scop->context);
2728 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2729 accesses = isl_union_map_union(accesses, accesses_i);
2732 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2733 for (i = 0; i < scop->n_array; ++i) {
2734 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2735 arrays = isl_union_set_add_set(arrays, extent);
2737 accesses = isl_union_map_intersect_range(accesses, arrays);
2739 to_inner = pet_scop_compute_any_to_inner(scop);
2740 accesses = isl_union_map_apply_range(accesses, to_inner);
2742 return accesses;
2745 /* Return the potential read access relation.
2747 __isl_give isl_union_map *pet_scop_get_may_reads(struct pet_scop *scop)
2749 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2752 /* Return the potential write access relation.
2754 __isl_give isl_union_map *pet_scop_get_may_writes(struct pet_scop *scop)
2756 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2759 /* Return the definite write access relation.
2761 __isl_give isl_union_map *pet_scop_get_must_writes(struct pet_scop *scop)
2763 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2766 /* Return the definite kill access relation.
2768 __isl_give isl_union_map *pet_scop_get_must_kills(struct pet_scop *scop)
2770 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2773 /* Return the tagged potential read access relation.
2775 __isl_give isl_union_map *pet_scop_get_tagged_may_reads(
2776 struct pet_scop *scop)
2778 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2781 /* Return the tagged potential write access relation.
2783 __isl_give isl_union_map *pet_scop_get_tagged_may_writes(
2784 struct pet_scop *scop)
2786 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2789 /* Return the tagged definite write access relation.
2791 __isl_give isl_union_map *pet_scop_get_tagged_must_writes(
2792 struct pet_scop *scop)
2794 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2797 /* Return the tagged definite kill access relation.
2799 __isl_give isl_union_map *pet_scop_get_tagged_must_kills(
2800 struct pet_scop *scop)
2802 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2805 /* Collect and return the set of all statement instances in "scop".
2807 __isl_give isl_union_set *pet_scop_get_instance_set(struct pet_scop *scop)
2809 int i;
2810 isl_set *domain_i;
2811 isl_union_set *domain;
2813 if (!scop)
2814 return NULL;
2816 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2818 for (i = 0; i < scop->n_stmt; ++i) {
2819 domain_i = isl_set_copy(scop->stmts[i]->domain);
2820 if (scop->stmts[i]->n_arg > 0)
2821 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2822 domain = isl_union_set_add_set(domain, domain_i);
2825 return domain;
2828 /* Return the context of "scop".
2830 __isl_give isl_set *pet_scop_get_context(__isl_keep pet_scop *scop)
2832 if (!scop)
2833 return NULL;
2835 return isl_set_copy(scop->context);
2838 /* Return the schedule of "scop".
2840 __isl_give isl_schedule *pet_scop_get_schedule(__isl_keep pet_scop *scop)
2842 if (!scop)
2843 return NULL;
2845 return isl_schedule_copy(scop->schedule);
2848 /* Add a reference identifier to all access expressions in "stmt".
2849 * "n_ref" points to an integer that contains the sequence number
2850 * of the next reference.
2852 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2854 int i;
2856 if (!stmt)
2857 return NULL;
2859 for (i = 0; i < stmt->n_arg; ++i) {
2860 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2861 if (!stmt->args[i])
2862 return pet_stmt_free(stmt);
2865 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2866 if (!stmt->body)
2867 return pet_stmt_free(stmt);
2869 return stmt;
2872 /* Add a reference identifier to all access expressions in "scop".
2874 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2876 int i;
2877 int n_ref;
2879 if (!scop)
2880 return NULL;
2882 n_ref = 0;
2883 for (i = 0; i < scop->n_stmt; ++i) {
2884 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2885 if (!scop->stmts[i])
2886 return pet_scop_free(scop);
2889 return scop;
2892 /* Reset the user pointer on all parameter ids in "array".
2894 static struct pet_array *array_anonymize(struct pet_array *array)
2896 if (!array)
2897 return NULL;
2899 array->context = isl_set_reset_user(array->context);
2900 array->extent = isl_set_reset_user(array->extent);
2901 if (!array->context || !array->extent)
2902 return pet_array_free(array);
2904 return array;
2907 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2909 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2911 int i;
2912 isl_space *space;
2913 isl_set *domain;
2915 if (!stmt)
2916 return NULL;
2918 stmt->domain = isl_set_reset_user(stmt->domain);
2919 if (!stmt->domain)
2920 return pet_stmt_free(stmt);
2922 for (i = 0; i < stmt->n_arg; ++i) {
2923 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2924 if (!stmt->args[i])
2925 return pet_stmt_free(stmt);
2928 stmt->body = pet_tree_anonymize(stmt->body);
2929 if (!stmt->body)
2930 return pet_stmt_free(stmt);
2932 return stmt;
2935 /* Reset the user pointer on the tuple ids and all parameter ids
2936 * in "implication".
2938 static struct pet_implication *implication_anonymize(
2939 struct pet_implication *implication)
2941 if (!implication)
2942 return NULL;
2944 implication->extension = isl_map_reset_user(implication->extension);
2945 if (!implication->extension)
2946 return pet_implication_free(implication);
2948 return implication;
2951 /* Reset the user pointer on the tuple ids and all parameter ids
2952 * in "independence".
2954 static struct pet_independence *independence_anonymize(
2955 struct pet_independence *independence)
2957 if (!independence)
2958 return NULL;
2960 independence->filter = isl_union_map_reset_user(independence->filter);
2961 independence->local = isl_union_set_reset_user(independence->local);
2962 if (!independence->filter || !independence->local)
2963 return pet_independence_free(independence);
2965 return independence;
2968 /* Reset the user pointer on all parameter and tuple ids in "scop".
2970 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2972 int i;
2974 if (!scop)
2975 return NULL;
2977 scop->context = isl_set_reset_user(scop->context);
2978 scop->context_value = isl_set_reset_user(scop->context_value);
2979 scop->schedule = isl_schedule_reset_user(scop->schedule);
2980 if (!scop->context || !scop->context_value || !scop->schedule)
2981 return pet_scop_free(scop);
2983 for (i = 0; i < scop->n_array; ++i) {
2984 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2985 if (!scop->arrays[i])
2986 return pet_scop_free(scop);
2989 for (i = 0; i < scop->n_stmt; ++i) {
2990 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2991 if (!scop->stmts[i])
2992 return pet_scop_free(scop);
2995 for (i = 0; i < scop->n_implication; ++i) {
2996 scop->implications[i] =
2997 implication_anonymize(scop->implications[i]);
2998 if (!scop->implications[i])
2999 return pet_scop_free(scop);
3002 for (i = 0; i < scop->n_independence; ++i) {
3003 scop->independences[i] =
3004 independence_anonymize(scop->independences[i]);
3005 if (!scop->independences[i])
3006 return pet_scop_free(scop);
3009 return scop;
3012 /* Compute the gist of the iteration domain and all access relations
3013 * of "stmt" based on the constraints on the parameters specified by "context"
3014 * and the constraints on the values of nested accesses specified
3015 * by "value_bounds".
3017 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3018 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3020 int i;
3021 isl_set *domain;
3023 if (!stmt)
3024 return NULL;
3026 domain = isl_set_copy(stmt->domain);
3027 if (stmt->n_arg > 0)
3028 domain = isl_map_domain(isl_set_unwrap(domain));
3030 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3032 for (i = 0; i < stmt->n_arg; ++i) {
3033 stmt->args[i] = pet_expr_gist(stmt->args[i],
3034 domain, value_bounds);
3035 if (!stmt->args[i])
3036 goto error;
3039 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
3040 if (!stmt->body)
3041 goto error;
3043 isl_set_free(domain);
3045 domain = isl_set_universe(pet_stmt_get_space(stmt));
3046 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3047 if (stmt->n_arg > 0)
3048 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3049 value_bounds);
3050 stmt->domain = isl_set_gist(stmt->domain, domain);
3051 if (!stmt->domain)
3052 return pet_stmt_free(stmt);
3054 return stmt;
3055 error:
3056 isl_set_free(domain);
3057 return pet_stmt_free(stmt);
3060 /* Compute the gist of the extent of the array
3061 * based on the constraints on the parameters specified by "context".
3063 static struct pet_array *array_gist(struct pet_array *array,
3064 __isl_keep isl_set *context)
3066 if (!array)
3067 return NULL;
3069 array->extent = isl_set_gist_params(array->extent,
3070 isl_set_copy(context));
3071 if (!array->extent)
3072 return pet_array_free(array);
3074 return array;
3077 /* Compute the gist of all sets and relations in "scop"
3078 * based on the constraints on the parameters specified by "scop->context"
3079 * and the constraints on the values of nested accesses specified
3080 * by "value_bounds".
3082 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3083 __isl_keep isl_union_map *value_bounds)
3085 int i;
3087 if (!scop)
3088 return NULL;
3090 scop->context = isl_set_coalesce(scop->context);
3091 if (!scop->context)
3092 return pet_scop_free(scop);
3094 scop->schedule = isl_schedule_gist_domain_params(scop->schedule,
3095 isl_set_copy(scop->context));
3096 if (!scop->schedule)
3097 return pet_scop_free(scop);
3099 for (i = 0; i < scop->n_array; ++i) {
3100 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3101 if (!scop->arrays[i])
3102 return pet_scop_free(scop);
3105 for (i = 0; i < scop->n_stmt; ++i) {
3106 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3107 value_bounds);
3108 if (!scop->stmts[i])
3109 return pet_scop_free(scop);
3112 return scop;
3115 /* Intersect the context of "scop" with "context".
3116 * To ensure that we don't introduce any unnamed parameters in
3117 * the context of "scop", we first remove the unnamed parameters
3118 * from "context".
3120 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3121 __isl_take isl_set *context)
3123 if (!scop)
3124 goto error;
3126 context = pet_nested_remove_from_set(context);
3127 scop->context = isl_set_intersect(scop->context, context);
3128 if (!scop->context)
3129 return pet_scop_free(scop);
3131 return scop;
3132 error:
3133 isl_set_free(context);
3134 return pet_scop_free(scop);
3137 /* Drop the current context of "scop". That is, replace the context
3138 * by a universal set.
3140 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3142 isl_space *space;
3144 if (!scop)
3145 return NULL;
3147 space = isl_set_get_space(scop->context);
3148 isl_set_free(scop->context);
3149 scop->context = isl_set_universe(space);
3150 if (!scop->context)
3151 return pet_scop_free(scop);
3153 return scop;
3156 /* Append "array" to the arrays of "scop".
3158 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3159 struct pet_array *array)
3161 isl_ctx *ctx;
3162 struct pet_array **arrays;
3164 if (!array || !scop)
3165 goto error;
3167 ctx = isl_set_get_ctx(scop->context);
3168 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3169 scop->n_array + 1);
3170 if (!arrays)
3171 goto error;
3172 scop->arrays = arrays;
3173 scop->arrays[scop->n_array] = array;
3174 scop->n_array++;
3175 scop->context = isl_set_intersect_params(scop->context,
3176 isl_set_copy(array->context));
3177 if (!scop->context)
3178 return pet_scop_free(scop);
3180 return scop;
3181 error:
3182 pet_array_free(array);
3183 return pet_scop_free(scop);
3186 /* Create an index expression for an access to a virtual array
3187 * representing the result of a condition.
3188 * Unlike other accessed data, the id of the array is NULL as
3189 * there is no ValueDecl in the program corresponding to the virtual
3190 * array.
3191 * The index expression is created as an identity mapping on "space".
3192 * That is, the dimension of the array is the same as that of "space".
3194 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3195 int test_nr)
3197 isl_id *id;
3198 char name[50];
3200 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3201 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3202 space = isl_space_map_from_set(space);
3203 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3204 return isl_multi_pw_aff_identity(space);
3207 /* Add an array with the given extent to the list
3208 * of arrays in "scop" and return the extended pet_scop.
3209 * Specifically, the extent is determined by the image of "domain"
3210 * under "index".
3211 * "int_size" is the number of bytes needed to represent values of type "int".
3212 * The array is marked as attaining values 0 and 1 only and
3213 * as each element being assigned at most once.
3215 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3216 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3217 int int_size)
3219 isl_ctx *ctx;
3220 isl_space *space;
3221 struct pet_array *array;
3222 isl_map *access;
3224 if (!scop || !domain || !index)
3225 goto error;
3227 ctx = isl_multi_pw_aff_get_ctx(index);
3228 array = isl_calloc_type(ctx, struct pet_array);
3229 if (!array)
3230 goto error;
3232 access = isl_map_from_multi_pw_aff(index);
3233 access = isl_map_intersect_domain(access, domain);
3234 array->extent = isl_map_range(access);
3235 space = isl_space_params_alloc(ctx, 0);
3236 array->context = isl_set_universe(space);
3237 space = isl_space_set_alloc(ctx, 0, 1);
3238 array->value_bounds = isl_set_universe(space);
3239 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3240 isl_dim_set, 0, 0);
3241 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3242 isl_dim_set, 0, 1);
3243 array->element_type = strdup("int");
3244 array->element_size = int_size;
3245 array->uniquely_defined = 1;
3247 if (!array->extent || !array->context)
3248 array = pet_array_free(array);
3250 scop = pet_scop_add_array(scop, array);
3252 return scop;
3253 error:
3254 isl_set_free(domain);
3255 isl_multi_pw_aff_free(index);
3256 return pet_scop_free(scop);
3259 /* Create and return an implication on filter values equal to "satisfied"
3260 * with extension "map".
3262 static struct pet_implication *new_implication(__isl_take isl_map *map,
3263 int satisfied)
3265 isl_ctx *ctx;
3266 struct pet_implication *implication;
3268 if (!map)
3269 return NULL;
3270 ctx = isl_map_get_ctx(map);
3271 implication = isl_alloc_type(ctx, struct pet_implication);
3272 if (!implication)
3273 goto error;
3275 implication->extension = map;
3276 implication->satisfied = satisfied;
3278 return implication;
3279 error:
3280 isl_map_free(map);
3281 return NULL;
3284 /* Add an implication on filter values equal to "satisfied"
3285 * with extension "map" to "scop".
3287 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3288 __isl_take isl_map *map, int satisfied)
3290 isl_ctx *ctx;
3291 struct pet_implication *implication;
3292 struct pet_implication **implications;
3294 implication = new_implication(map, satisfied);
3295 if (!scop || !implication)
3296 goto error;
3298 ctx = isl_set_get_ctx(scop->context);
3299 implications = isl_realloc_array(ctx, scop->implications,
3300 struct pet_implication *,
3301 scop->n_implication + 1);
3302 if (!implications)
3303 goto error;
3304 scop->implications = implications;
3305 scop->implications[scop->n_implication] = implication;
3306 scop->n_implication++;
3308 return scop;
3309 error:
3310 pet_implication_free(implication);
3311 return pet_scop_free(scop);
3314 /* Create and return a function that maps the iteration domains
3315 * of the statements in "scop" onto their outer "n" dimensions.
3316 * "space" is the parameters space of the created function.
3318 static __isl_give isl_union_pw_multi_aff *outer_projection(
3319 struct pet_scop *scop, __isl_take isl_space *space, int n)
3321 int i;
3322 isl_union_pw_multi_aff *res;
3324 res = isl_union_pw_multi_aff_empty(space);
3326 if (!scop)
3327 return isl_union_pw_multi_aff_free(res);
3329 for (i = 0; i < scop->n_stmt; ++i) {
3330 struct pet_stmt *stmt = scop->stmts[i];
3331 isl_space *space;
3332 isl_multi_aff *ma;
3333 isl_pw_multi_aff *pma;
3335 space = pet_stmt_get_space(stmt);
3336 ma = pet_prefix_projection(space, n);
3337 pma = isl_pw_multi_aff_from_multi_aff(ma);
3338 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3341 return res;
3344 /* Add an independence to "scop" for the inner iterator of "domain"
3345 * with local variables "local", where "domain" represents the outer
3346 * loop iterators of all statements in "scop".
3347 * If "sign" is positive, then the inner iterator increases.
3348 * Otherwise it decreases.
3350 * The independence is supposed to filter out any dependence of
3351 * an iteration of domain on a previous iteration along the inner dimension.
3352 * We therefore create a mapping from an iteration to later iterations and
3353 * then plug in the projection of the iterations domains of "scop"
3354 * onto the outer loop iterators.
3356 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3357 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3359 int i, dim;
3360 isl_space *space;
3361 isl_map *map;
3362 isl_union_map *independence;
3363 isl_union_pw_multi_aff *proj;
3365 if (!scop || !domain || !local)
3366 goto error;
3368 dim = isl_set_dim(domain, isl_dim_set);
3369 space = isl_space_map_from_set(isl_set_get_space(domain));
3370 map = isl_map_universe(space);
3371 for (i = 0; i + 1 < dim; ++i)
3372 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3373 if (sign > 0)
3374 map = isl_map_order_lt(map,
3375 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3376 else
3377 map = isl_map_order_gt(map,
3378 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3380 independence = isl_union_map_from_map(map);
3381 space = isl_space_params(isl_set_get_space(domain));
3382 proj = outer_projection(scop, space, dim);
3383 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3384 independence, isl_union_pw_multi_aff_copy(proj));
3385 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3386 independence, proj);
3388 scop = pet_scop_add_independence(scop, independence, local);
3390 return scop;
3391 error:
3392 isl_union_set_free(local);
3393 return pet_scop_free(scop);
3396 /* Given an access expression, check if it is data dependent.
3397 * If so, set *found and abort the search.
3399 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3401 int *found = user;
3403 if (pet_expr_get_n_arg(expr) > 0) {
3404 *found = 1;
3405 return -1;
3408 return 0;
3411 /* Does "scop" contain any data dependent accesses?
3413 * Check the body of each statement for such accesses.
3415 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3417 int i;
3418 int found = 0;
3420 if (!scop)
3421 return -1;
3423 for (i = 0; i < scop->n_stmt; ++i) {
3424 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3425 &is_data_dependent, &found);
3426 if (r < 0 && !found)
3427 return -1;
3428 if (found)
3429 return found;
3432 return found;
3435 /* Does "scop" contain and data dependent conditions?
3437 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3439 int i;
3441 if (!scop)
3442 return -1;
3444 for (i = 0; i < scop->n_stmt; ++i)
3445 if (scop->stmts[i]->n_arg > 0)
3446 return 1;
3448 return 0;
3451 /* Keep track of the "input" file inside the (extended) "scop".
3453 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3455 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3457 if (!scop)
3458 return NULL;
3460 ext->input = input;
3462 return scop;
3465 /* Print the original code corresponding to "scop" to printer "p".
3467 * pet_scop_print_original can only be called from
3468 * a pet_transform_C_source callback. This means that the input
3469 * file is stored in the extended scop and that the printer prints
3470 * to a file.
3472 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3473 __isl_take isl_printer *p)
3475 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3476 FILE *output;
3477 unsigned start, end;
3479 if (!scop || !p)
3480 return isl_printer_free(p);
3482 if (!ext->input)
3483 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3484 "no input file stored in scop",
3485 return isl_printer_free(p));
3487 output = isl_printer_get_file(p);
3488 if (!output)
3489 return isl_printer_free(p);
3491 start = pet_loc_get_start(scop->loc);
3492 end = pet_loc_get_end(scop->loc);
3493 if (copy(ext->input, output, start, end) < 0)
3494 return isl_printer_free(p);
3496 return p;