scop.c: pet_implication_free: drop unused variable
[pet.git] / scop.c
blob64f5e3f1d6f15a808bbae0243ba38b27dba5a6a7
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/ctx.h>
37 #include <isl/id.h>
38 #include <isl/space.h>
39 #include <isl/local_space.h>
40 #include <isl/constraint.h>
41 #include <isl/val.h>
42 #include <isl/aff.h>
43 #include <isl/set.h>
44 #include <isl/map.h>
45 #include <isl/union_set.h>
46 #include <isl/union_map.h>
47 #include <isl/schedule_node.h>
49 #include "aff.h"
50 #include "expr.h"
51 #include "expr_access_type.h"
52 #include "filter.h"
53 #include "loc.h"
54 #include "nest.h"
55 #include "scop.h"
56 #include "tree.h"
57 #include "print.h"
58 #include "value_bounds.h"
60 /* pet_scop with extra information that is used during parsing and printing.
62 * In particular, we keep track of conditions under which we want
63 * to skip the rest of the current loop iteration (skip[pet_skip_now])
64 * and of conditions under which we want to skip subsequent
65 * loop iterations (skip[pet_skip_later]).
67 * The conditions are represented as index expressions defined
68 * over the outer loop iterators. The index expression is either
69 * a boolean affine expression or an access to a variable, which
70 * is assumed to attain values zero and one. The condition holds
71 * if the variable has value one or if the affine expression
72 * has value one (typically for only part of the domain).
74 * A missing condition (skip[type] == NULL) means that we don't want
75 * to skip anything.
77 * Additionally, we keep track of the original input file
78 * inside pet_transform_C_source.
80 struct pet_scop_ext {
81 struct pet_scop scop;
83 isl_multi_pw_aff *skip[2];
84 FILE *input;
87 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
88 * The input domain is anonymous and is the same as the domains
89 * of the access expressions inside "tree".
90 * These domains are modified to include the name of the statement.
91 * This name is given by tree->label if it is non-NULL.
92 * Otherwise, the name is constructed as S_<id>.
94 struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
95 int id, __isl_take pet_tree *tree)
97 struct pet_stmt *stmt;
98 isl_ctx *ctx;
99 isl_id *label;
100 isl_space *space;
101 isl_multi_aff *ma;
102 isl_multi_pw_aff *add_name;
103 char name[50];
105 if (!domain || !tree)
106 goto error;
108 ctx = pet_tree_get_ctx(tree);
109 stmt = isl_calloc_type(ctx, struct pet_stmt);
110 if (!stmt)
111 goto error;
113 if (tree->label) {
114 label = isl_id_copy(tree->label);
115 } else {
116 snprintf(name, sizeof(name), "S_%d", id);
117 label = isl_id_alloc(ctx, name, NULL);
119 domain = isl_set_set_tuple_id(domain, label);
120 space = isl_set_get_space(domain);
121 space = pet_nested_remove_from_space(space);
122 ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
124 add_name = isl_multi_pw_aff_from_multi_aff(ma);
125 tree = pet_tree_update_domain(tree, add_name);
127 stmt->loc = pet_tree_get_loc(tree);
128 stmt->domain = domain;
129 stmt->body = tree;
131 if (!stmt->domain || !stmt->body)
132 return pet_stmt_free(stmt);
134 return stmt;
135 error:
136 isl_set_free(domain);
137 pet_tree_free(tree);
138 return NULL;
141 void *pet_stmt_free(struct pet_stmt *stmt)
143 int i;
145 if (!stmt)
146 return NULL;
148 pet_loc_free(stmt->loc);
149 isl_set_free(stmt->domain);
150 pet_tree_free(stmt->body);
152 for (i = 0; i < stmt->n_arg; ++i)
153 pet_expr_free(stmt->args[i]);
154 free(stmt->args);
156 free(stmt);
157 return NULL;
160 /* Return the iteration space of "stmt".
162 * If the statement has arguments, then stmt->domain is a wrapped map
163 * mapping the iteration domain to the values of the arguments
164 * for which this statement is executed.
165 * In this case, we need to extract the domain space of this wrapped map.
167 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
169 isl_space *space;
171 if (!stmt)
172 return NULL;
174 space = isl_set_get_space(stmt->domain);
175 if (isl_space_is_wrapping(space))
176 space = isl_space_domain(isl_space_unwrap(space));
178 return space;
181 static void stmt_dump(struct pet_stmt *stmt, int indent)
183 int i;
185 if (!stmt)
186 return;
188 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
189 fprintf(stderr, "%*s", indent, "");
190 isl_set_dump(stmt->domain);
191 pet_tree_dump_with_indent(stmt->body, indent);
192 for (i = 0; i < stmt->n_arg; ++i)
193 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
196 void pet_stmt_dump(struct pet_stmt *stmt)
198 stmt_dump(stmt, 0);
201 /* Allocate a new pet_type with the given "name" and "definition".
203 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
204 const char *definition)
206 struct pet_type *type;
208 type = isl_alloc_type(ctx, struct pet_type);
209 if (!type)
210 return NULL;
212 type->name = strdup(name);
213 type->definition = strdup(definition);
215 if (!type->name || !type->definition)
216 return pet_type_free(type);
218 return type;
221 /* Free "type" and return NULL.
223 struct pet_type *pet_type_free(struct pet_type *type)
225 if (!type)
226 return NULL;
228 free(type->name);
229 free(type->definition);
231 free(type);
232 return NULL;
235 struct pet_array *pet_array_free(struct pet_array *array)
237 if (!array)
238 return NULL;
240 isl_set_free(array->context);
241 isl_set_free(array->extent);
242 isl_set_free(array->value_bounds);
243 free(array->element_type);
245 free(array);
246 return NULL;
249 void pet_array_dump(struct pet_array *array)
251 if (!array)
252 return;
254 isl_set_dump(array->context);
255 isl_set_dump(array->extent);
256 isl_set_dump(array->value_bounds);
257 fprintf(stderr, "%s%s%s\n", array->element_type,
258 array->element_is_record ? " element-is-record" : "",
259 array->live_out ? " live-out" : "");
262 /* Alloc a pet_scop structure, with extra room for information that
263 * is only used during parsing.
265 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
267 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
270 /* Construct a pet_scop in the given space, with the given schedule and
271 * room for n statements.
273 * The context is initialized as a universe set in "space".
275 * Since no information on the location is known at this point,
276 * scop->loc is initialized with pet_loc_dummy.
278 static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n,
279 __isl_take isl_schedule *schedule)
281 isl_ctx *ctx;
282 struct pet_scop *scop;
284 if (!space || !schedule)
285 goto error;
287 ctx = isl_space_get_ctx(space);
288 scop = pet_scop_alloc(ctx);
289 if (!scop)
290 goto error;
292 scop->context = isl_set_universe(isl_space_copy(space));
293 scop->context_value = isl_set_universe(isl_space_params(space));
294 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
295 scop->schedule = schedule;
296 if (!scop->context || !scop->stmts)
297 return pet_scop_free(scop);
299 scop->loc = &pet_loc_dummy;
300 scop->n_stmt = n;
302 return scop;
303 error:
304 isl_space_free(space);
305 isl_schedule_free(schedule);
306 return NULL;
309 /* Construct a pet_scop in the given space containing 0 statements
310 * (and therefore an empty iteration domain).
312 struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
314 isl_schedule *schedule;
316 schedule = isl_schedule_empty(isl_space_copy(space));
318 return scop_alloc(space, 0, schedule);
321 /* Given either an iteration domain or a wrapped map with
322 * the iteration domain in the domain and some arguments
323 * in the range, return the iteration domain.
324 * That is, drop the arguments if there are any.
326 static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
328 if (isl_set_is_wrapping(domain))
329 domain = isl_map_domain(isl_set_unwrap(domain));
330 return domain;
333 /* Update "context" with the constraints imposed on the outer iteration
334 * domain by access expression "expr".
335 * "context" lives in an anonymous space, while the domain of the access
336 * relation of "expr" refers to a particular statement.
337 * This reference therefore needs to be stripped off.
339 static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
340 __isl_take isl_set *context)
342 isl_multi_pw_aff *mpa;
343 isl_set *domain;
345 mpa = pet_expr_access_get_index(expr);
346 domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
347 domain = isl_set_reset_tuple_id(domain);
348 context = isl_set_intersect(context, domain);
349 return context;
352 /* Update "context" with the constraints imposed on the outer iteration
353 * domain by "expr".
355 * "context" lives in an anonymous space, while the domains of
356 * the access relations in "expr" refer to a particular statement.
357 * This reference therefore needs to be stripped off.
359 * If "expr" represents a conditional operator, then a parameter or outer
360 * iterator value needs to be valid for the condition and
361 * for at least one of the remaining two arguments.
362 * If the condition is an affine expression, then we can be a bit more specific.
363 * The value then has to be valid for the second argument for
364 * non-zero accesses and valid for the third argument for zero accesses.
366 * If "expr" represents a kill statement, then its argument is the entire
367 * extent of the array being killed. Do not update "context" based
368 * on this argument as that would impose constraints that ensure that
369 * the array is non-empty.
371 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
372 __isl_take isl_set *context)
374 int i;
376 if (expr->type == pet_expr_op && expr->op == pet_op_kill)
377 return context;
379 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
380 int is_aff;
381 isl_set *context1, *context2;
383 is_aff = pet_expr_is_affine(expr->args[0]);
384 if (is_aff < 0)
385 goto error;
387 context = expr_extract_context(expr->args[0], context);
388 context1 = expr_extract_context(expr->args[1],
389 isl_set_copy(context));
390 context2 = expr_extract_context(expr->args[2], context);
392 if (is_aff) {
393 isl_multi_pw_aff *mpa;
394 isl_pw_aff *pa;
395 isl_set *zero_set;
397 mpa = pet_expr_access_get_index(expr->args[0]);
398 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
399 isl_multi_pw_aff_free(mpa);
400 zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
401 zero_set = isl_set_reset_tuple_id(zero_set);
402 context1 = isl_set_subtract(context1,
403 isl_set_copy(zero_set));
404 context2 = isl_set_intersect(context2, zero_set);
407 context = isl_set_union(context1, context2);
408 context = isl_set_coalesce(context);
410 return context;
413 for (i = 0; i < expr->n_arg; ++i)
414 context = expr_extract_context(expr->args[i], context);
416 if (expr->type == pet_expr_access)
417 context = access_extract_context(expr, context);
419 return context;
420 error:
421 isl_set_free(context);
422 return NULL;
425 /* Is "stmt" an assume statement with an affine assumption?
427 isl_bool pet_stmt_is_affine_assume(struct pet_stmt *stmt)
429 if (!stmt)
430 return isl_bool_error;
431 return pet_tree_is_affine_assume(stmt->body);
434 /* Given an assume statement "stmt" with an access argument,
435 * return the index expression of the argument.
437 __isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
439 if (!stmt)
440 return NULL;
441 return pet_tree_assume_get_index(stmt->body);
444 /* Assuming "stmt" is an assume statement with an affine assumption,
445 * return the assumption as a set.
447 __isl_give isl_set *pet_stmt_assume_get_affine_condition(struct pet_stmt *stmt)
449 isl_multi_pw_aff *index;
450 isl_pw_aff *pa;
452 index = pet_stmt_assume_get_index(stmt);
453 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
454 isl_multi_pw_aff_free(index);
455 return isl_pw_aff_non_zero_set(pa);
458 /* Update "context" with the constraints imposed on the outer iteration
459 * domain by "stmt".
461 * If the statement is an assume statement with an affine expression,
462 * then intersect "context" with that expression.
463 * Otherwise, if the statement body is an expression tree,
464 * then intersect "context" with the context of this expression.
465 * Note that we cannot safely extract a context from subtrees
466 * of the statement body since we cannot tell when those subtrees
467 * are executed, if at all.
469 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
470 __isl_take isl_set *context)
472 int i;
473 isl_bool affine;
474 pet_expr *body;
476 affine = pet_stmt_is_affine_assume(stmt);
477 if (affine < 0)
478 return isl_set_free(context);
479 if (affine) {
480 isl_set *cond;
482 cond = pet_stmt_assume_get_affine_condition(stmt);
483 cond = isl_set_reset_tuple_id(cond);
484 return isl_set_intersect(context, cond);
487 for (i = 0; i < stmt->n_arg; ++i)
488 context = expr_extract_context(stmt->args[i], context);
490 if (pet_tree_get_type(stmt->body) != pet_tree_expr)
491 return context;
493 body = pet_tree_expr_get_expr(stmt->body);
494 context = expr_extract_context(body, context);
495 pet_expr_free(body);
497 return context;
500 /* Construct a pet_scop in the given space that contains the given pet_stmt.
501 * The initial schedule consists of only the iteration domain.
503 struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
504 struct pet_stmt *stmt)
506 struct pet_scop *scop;
507 isl_set *set;
508 isl_union_set *domain;
509 isl_schedule *schedule;
511 if (!stmt) {
512 isl_space_free(space);
513 return NULL;
516 set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
517 domain = isl_union_set_from_set(set);
518 schedule = isl_schedule_from_domain(domain);
520 scop = scop_alloc(space, 1, schedule);
521 if (!scop)
522 goto error;
524 scop->context = stmt_extract_context(stmt, scop->context);
525 if (!scop->context)
526 goto error;
528 scop->stmts[0] = stmt;
529 scop->loc = pet_loc_copy(stmt->loc);
531 if (!scop->loc)
532 return pet_scop_free(scop);
534 return scop;
535 error:
536 pet_stmt_free(stmt);
537 pet_scop_free(scop);
538 return NULL;
541 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
542 * does it represent an affine expression?
544 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
546 int has_id;
548 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
549 if (has_id < 0)
550 return -1;
552 return !has_id;
555 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
557 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
558 __isl_take isl_set *dom)
560 isl_pw_aff *pa;
561 pa = isl_set_indicator_function(set);
562 pa = isl_pw_aff_intersect_domain(pa, dom);
563 return pa;
566 /* Return "lhs || rhs", defined on the shared definition domain.
568 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
569 __isl_take isl_pw_aff *rhs)
571 isl_set *cond;
572 isl_set *dom;
574 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
575 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
576 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
577 isl_pw_aff_non_zero_set(rhs));
578 cond = isl_set_coalesce(cond);
579 return indicator_function(cond, dom);
582 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
583 * ext may be equal to either ext1 or ext2.
585 * The two skips that need to be combined are assumed to be affine expressions.
587 * We need to skip in ext if we need to skip in either ext1 or ext2.
588 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
590 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
591 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
592 enum pet_skip type)
594 isl_pw_aff *skip, *skip1, *skip2;
596 if (!ext)
597 return NULL;
598 if (!ext1->skip[type] && !ext2->skip[type])
599 return ext;
600 if (!ext1->skip[type]) {
601 if (ext == ext2)
602 return ext;
603 ext->skip[type] = ext2->skip[type];
604 ext2->skip[type] = NULL;
605 return ext;
607 if (!ext2->skip[type]) {
608 if (ext == ext1)
609 return ext;
610 ext->skip[type] = ext1->skip[type];
611 ext1->skip[type] = NULL;
612 return ext;
615 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
616 !multi_pw_aff_is_affine(ext2->skip[type]))
617 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
618 isl_error_internal, "can only combine affine skips",
619 goto error);
621 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
622 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
623 skip = pw_aff_or(skip1, skip2);
624 isl_multi_pw_aff_free(ext1->skip[type]);
625 ext1->skip[type] = NULL;
626 isl_multi_pw_aff_free(ext2->skip[type]);
627 ext2->skip[type] = NULL;
628 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
629 if (!ext->skip[type])
630 goto error;
632 return ext;
633 error:
634 pet_scop_free(&ext->scop);
635 return NULL;
638 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
639 * where type takes on the values pet_skip_now and pet_skip_later.
640 * scop may be equal to either scop1 or scop2.
642 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
643 struct pet_scop *scop1, struct pet_scop *scop2)
645 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
646 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
647 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
649 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
650 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
651 return &ext->scop;
654 /* Update start and end of scop->loc to include the region from "start"
655 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
656 * does not have any offset information yet and we simply take the information
657 * from "start" and "end". Otherwise, we update loc using "start" and "end".
659 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
660 unsigned start, unsigned end)
662 if (!scop)
663 return NULL;
665 if (scop->loc == &pet_loc_dummy)
666 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
667 start, end, -1, strdup(""));
668 else
669 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
671 if (!scop->loc)
672 return pet_scop_free(scop);
674 return scop;
677 /* Update start and end of scop->loc to include the region identified
678 * by "loc".
680 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
681 __isl_keep pet_loc *loc)
683 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
684 pet_loc_get_end(loc));
687 /* Replace the location of "scop" by "loc".
689 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
690 __isl_take pet_loc *loc)
692 if (!scop || !loc)
693 goto error;
695 pet_loc_free(scop->loc);
696 scop->loc = loc;
698 return scop;
699 error:
700 pet_loc_free(loc);
701 pet_scop_free(scop);
702 return NULL;
705 /* Does "implication" appear in the list of implications of "scop"?
707 static int is_known_implication(struct pet_scop *scop,
708 struct pet_implication *implication)
710 int i;
712 for (i = 0; i < scop->n_implication; ++i) {
713 struct pet_implication *pi = scop->implications[i];
714 int equal;
716 if (pi->satisfied != implication->satisfied)
717 continue;
718 equal = isl_map_is_equal(pi->extension, implication->extension);
719 if (equal < 0)
720 return -1;
721 if (equal)
722 return 1;
725 return 0;
728 /* Store the concatenation of the implications of "scop1" and "scop2"
729 * in "scop", removing duplicates (i.e., implications in "scop2" that
730 * already appear in "scop1").
732 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
733 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
735 int i, j;
737 if (!scop)
738 return NULL;
740 if (scop2->n_implication == 0) {
741 scop->n_implication = scop1->n_implication;
742 scop->implications = scop1->implications;
743 scop1->n_implication = 0;
744 scop1->implications = NULL;
745 return scop;
748 if (scop1->n_implication == 0) {
749 scop->n_implication = scop2->n_implication;
750 scop->implications = scop2->implications;
751 scop2->n_implication = 0;
752 scop2->implications = NULL;
753 return scop;
756 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
757 scop1->n_implication + scop2->n_implication);
758 if (!scop->implications)
759 return pet_scop_free(scop);
761 for (i = 0; i < scop1->n_implication; ++i) {
762 scop->implications[i] = scop1->implications[i];
763 scop1->implications[i] = NULL;
766 scop->n_implication = scop1->n_implication;
767 j = scop1->n_implication;
768 for (i = 0; i < scop2->n_implication; ++i) {
769 int known;
771 known = is_known_implication(scop, scop2->implications[i]);
772 if (known < 0)
773 return pet_scop_free(scop);
774 if (known)
775 continue;
776 scop->implications[j++] = scop2->implications[i];
777 scop2->implications[i] = NULL;
779 scop->n_implication = j;
781 return scop;
784 /* Combine the offset information of "scop1" and "scop2" into "scop".
786 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
787 struct pet_scop *scop1, struct pet_scop *scop2)
789 if (scop1->loc != &pet_loc_dummy)
790 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
791 if (scop2->loc != &pet_loc_dummy)
792 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
793 return scop;
796 /* Create and return an independence that filters out the dependences
797 * in "filter" with local variables "local".
799 static struct pet_independence *new_independence(
800 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
802 isl_ctx *ctx;
803 struct pet_independence *independence;
805 if (!filter || !local)
806 goto error;
807 ctx = isl_union_map_get_ctx(filter);
808 independence = isl_alloc_type(ctx, struct pet_independence);
809 if (!independence)
810 goto error;
812 independence->filter = filter;
813 independence->local = local;
815 return independence;
816 error:
817 isl_union_map_free(filter);
818 isl_union_set_free(local);
819 return NULL;
822 /* Add an independence that filters out the dependences
823 * in "filter" with local variables "local" to "scop".
825 struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
826 __isl_take isl_union_map *filter, __isl_take isl_union_set *local)
828 isl_ctx *ctx;
829 struct pet_independence *independence;
830 struct pet_independence **independences;
832 ctx = isl_union_map_get_ctx(filter);
833 independence = new_independence(filter, local);
834 if (!scop || !independence)
835 goto error;
837 independences = isl_realloc_array(ctx, scop->independences,
838 struct pet_independence *,
839 scop->n_independence + 1);
840 if (!independences)
841 goto error;
842 scop->independences = independences;
843 scop->independences[scop->n_independence] = independence;
844 scop->n_independence++;
846 return scop;
847 error:
848 pet_independence_free(independence);
849 pet_scop_free(scop);
850 return NULL;
853 /* Store the concatenation of the independences of "scop1" and "scop2"
854 * in "scop".
856 static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
857 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
859 int i, off;
861 if (!scop)
862 return NULL;
864 if (scop2->n_independence == 0) {
865 scop->n_independence = scop1->n_independence;
866 scop->independences = scop1->independences;
867 scop1->n_independence = 0;
868 scop1->independences = NULL;
869 return scop;
872 if (scop1->n_independence == 0) {
873 scop->n_independence = scop2->n_independence;
874 scop->independences = scop2->independences;
875 scop2->n_independence = 0;
876 scop2->independences = NULL;
877 return scop;
880 scop->independences = isl_calloc_array(ctx, struct pet_independence *,
881 scop1->n_independence + scop2->n_independence);
882 if (!scop->independences)
883 return pet_scop_free(scop);
885 for (i = 0; i < scop1->n_independence; ++i) {
886 scop->independences[i] = scop1->independences[i];
887 scop1->independences[i] = NULL;
890 off = scop1->n_independence;
891 for (i = 0; i < scop2->n_independence; ++i) {
892 scop->independences[off + i] = scop2->independences[i];
893 scop2->independences[i] = NULL;
895 scop->n_independence = scop1->n_independence + scop2->n_independence;
897 return scop;
900 /* Construct a pet_scop with the given schedule
901 * that contains the offset information,
902 * arrays, statements and skip information in "scop1" and "scop2".
904 static struct pet_scop *pet_scop_add(isl_ctx *ctx,
905 __isl_take isl_schedule *schedule, struct pet_scop *scop1,
906 struct pet_scop *scop2)
908 int i;
909 isl_space *space;
910 struct pet_scop *scop = NULL;
912 if (!scop1 || !scop2)
913 goto error;
915 if (scop1->n_stmt == 0) {
916 scop2 = scop_combine_skips(scop2, scop1, scop2);
917 pet_scop_free(scop1);
918 isl_schedule_free(schedule);
919 return scop2;
922 if (scop2->n_stmt == 0) {
923 scop1 = scop_combine_skips(scop1, scop1, scop2);
924 pet_scop_free(scop2);
925 isl_schedule_free(schedule);
926 return scop1;
929 space = isl_set_get_space(scop1->context);
930 scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
931 isl_schedule_copy(schedule));
932 if (!scop)
933 goto error;
935 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
936 scop1->n_array + scop2->n_array);
937 if (!scop->arrays)
938 goto error;
939 scop->n_array = scop1->n_array + scop2->n_array;
941 for (i = 0; i < scop1->n_stmt; ++i) {
942 scop->stmts[i] = scop1->stmts[i];
943 scop1->stmts[i] = NULL;
946 for (i = 0; i < scop2->n_stmt; ++i) {
947 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
948 scop2->stmts[i] = NULL;
951 for (i = 0; i < scop1->n_array; ++i) {
952 scop->arrays[i] = scop1->arrays[i];
953 scop1->arrays[i] = NULL;
956 for (i = 0; i < scop2->n_array; ++i) {
957 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
958 scop2->arrays[i] = NULL;
961 scop = scop_collect_implications(ctx, scop, scop1, scop2);
962 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
963 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
964 scop = scop_combine_skips(scop, scop1, scop2);
965 scop = scop_combine_start_end(scop, scop1, scop2);
966 scop = scop_collect_independences(ctx, scop, scop1, scop2);
968 pet_scop_free(scop1);
969 pet_scop_free(scop2);
970 isl_schedule_free(schedule);
971 return scop;
972 error:
973 pet_scop_free(scop1);
974 pet_scop_free(scop2);
975 pet_scop_free(scop);
976 isl_schedule_free(schedule);
977 return NULL;
980 /* Apply the skip condition "skip" to "scop".
981 * That is, make sure "scop" is not executed when the condition holds.
983 * If "skip" is an affine expression, we add the conditions under
984 * which the expression is zero to the context and the skip conditions
985 * of "scop".
986 * Otherwise, we add a filter on the variable attaining the value zero.
988 static struct pet_scop *restrict_skip(struct pet_scop *scop,
989 __isl_take isl_multi_pw_aff *skip)
991 isl_set *zero;
992 isl_pw_aff *pa;
993 int is_aff;
995 if (!scop || !skip)
996 goto error;
998 is_aff = multi_pw_aff_is_affine(skip);
999 if (is_aff < 0)
1000 goto error;
1002 if (!is_aff)
1003 return pet_scop_filter(scop, skip, 0);
1005 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
1006 isl_multi_pw_aff_free(skip);
1007 zero = isl_pw_aff_zero_set(pa);
1008 scop = pet_scop_restrict(scop, zero);
1010 return scop;
1011 error:
1012 isl_multi_pw_aff_free(skip);
1013 return pet_scop_free(scop);
1016 /* Construct a pet_scop that contains the arrays, statements and
1017 * skip information in "scop1" and "scop2", where the two scops
1018 * are executed "in sequence". That is, breaks and continues
1019 * in scop1 have an effect on scop2 and the schedule of the result
1020 * is the sequence of the schedules of "scop1" and "scop2".
1022 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
1023 struct pet_scop *scop2)
1025 isl_schedule *schedule;
1027 if (!scop1 || !scop2)
1028 goto error;
1030 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
1031 scop2 = restrict_skip(scop2,
1032 pet_scop_get_skip(scop1, pet_skip_now));
1033 schedule = isl_schedule_sequence(isl_schedule_copy(scop1->schedule),
1034 isl_schedule_copy(scop2->schedule));
1035 return pet_scop_add(ctx, schedule, scop1, scop2);
1036 error:
1037 pet_scop_free(scop1);
1038 pet_scop_free(scop2);
1039 return NULL;
1042 /* Construct a pet_scop that contains the arrays, statements and
1043 * skip information in "scop1" and "scop2", where the two scops
1044 * are executed "in parallel". That is, any break or continue
1045 * in scop1 has no effect on scop2 and the schedule of the result
1046 * is the set of the schedules of "scop1" and "scop2".
1048 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
1049 struct pet_scop *scop2)
1051 isl_schedule *schedule;
1053 if (!scop1 || !scop2)
1054 goto error;
1056 schedule = isl_schedule_set(isl_schedule_copy(scop1->schedule),
1057 isl_schedule_copy(scop2->schedule));
1058 return pet_scop_add(ctx, schedule, scop1, scop2);
1059 error:
1060 pet_scop_free(scop1);
1061 pet_scop_free(scop2);
1062 return NULL;
1065 void *pet_implication_free(struct pet_implication *implication)
1067 if (!implication)
1068 return NULL;
1070 isl_map_free(implication->extension);
1072 free(implication);
1073 return NULL;
1076 void *pet_independence_free(struct pet_independence *independence)
1078 if (!independence)
1079 return NULL;
1081 isl_union_map_free(independence->filter);
1082 isl_union_set_free(independence->local);
1084 free(independence);
1085 return NULL;
1088 struct pet_scop *pet_scop_free(struct pet_scop *scop)
1090 int i;
1091 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1093 if (!scop)
1094 return NULL;
1095 pet_loc_free(scop->loc);
1096 isl_set_free(scop->context);
1097 isl_set_free(scop->context_value);
1098 isl_schedule_free(scop->schedule);
1099 if (scop->types)
1100 for (i = 0; i < scop->n_type; ++i)
1101 pet_type_free(scop->types[i]);
1102 free(scop->types);
1103 if (scop->arrays)
1104 for (i = 0; i < scop->n_array; ++i)
1105 pet_array_free(scop->arrays[i]);
1106 free(scop->arrays);
1107 if (scop->stmts)
1108 for (i = 0; i < scop->n_stmt; ++i)
1109 pet_stmt_free(scop->stmts[i]);
1110 free(scop->stmts);
1111 if (scop->implications)
1112 for (i = 0; i < scop->n_implication; ++i)
1113 pet_implication_free(scop->implications[i]);
1114 free(scop->implications);
1115 if (scop->independences)
1116 for (i = 0; i < scop->n_independence; ++i)
1117 pet_independence_free(scop->independences[i]);
1118 free(scop->independences);
1119 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
1120 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
1121 free(scop);
1122 return NULL;
1125 void pet_type_dump(struct pet_type *type)
1127 if (!type)
1128 return;
1130 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
1133 void pet_implication_dump(struct pet_implication *implication)
1135 if (!implication)
1136 return;
1138 fprintf(stderr, "%d\n", implication->satisfied);
1139 isl_map_dump(implication->extension);
1142 void pet_scop_dump(struct pet_scop *scop)
1144 int i;
1145 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1147 if (!scop)
1148 return;
1150 isl_set_dump(scop->context);
1151 isl_set_dump(scop->context_value);
1152 isl_schedule_dump(scop->schedule);
1153 for (i = 0; i < scop->n_type; ++i)
1154 pet_type_dump(scop->types[i]);
1155 for (i = 0; i < scop->n_array; ++i)
1156 pet_array_dump(scop->arrays[i]);
1157 for (i = 0; i < scop->n_stmt; ++i)
1158 pet_stmt_dump(scop->stmts[i]);
1159 for (i = 0; i < scop->n_implication; ++i)
1160 pet_implication_dump(scop->implications[i]);
1162 if (ext->skip[0]) {
1163 fprintf(stderr, "skip\n");
1164 isl_multi_pw_aff_dump(ext->skip[0]);
1165 isl_multi_pw_aff_dump(ext->skip[1]);
1169 /* Return 1 if the two pet_arrays are equivalent.
1171 * We don't compare element_size as this may be target dependent.
1173 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
1175 if (!array1 || !array2)
1176 return 0;
1178 if (!isl_set_is_equal(array1->context, array2->context))
1179 return 0;
1180 if (!isl_set_is_equal(array1->extent, array2->extent))
1181 return 0;
1182 if (!!array1->value_bounds != !!array2->value_bounds)
1183 return 0;
1184 if (array1->value_bounds &&
1185 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
1186 return 0;
1187 if (strcmp(array1->element_type, array2->element_type))
1188 return 0;
1189 if (array1->element_is_record != array2->element_is_record)
1190 return 0;
1191 if (array1->live_out != array2->live_out)
1192 return 0;
1193 if (array1->uniquely_defined != array2->uniquely_defined)
1194 return 0;
1195 if (array1->declared != array2->declared)
1196 return 0;
1197 if (array1->exposed != array2->exposed)
1198 return 0;
1200 return 1;
1203 /* Return 1 if the two pet_stmts are equivalent.
1205 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
1207 int i;
1209 if (!stmt1 || !stmt2)
1210 return 0;
1212 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
1213 return 0;
1214 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
1215 return 0;
1216 if (!pet_tree_is_equal(stmt1->body, stmt2->body))
1217 return 0;
1218 if (stmt1->n_arg != stmt2->n_arg)
1219 return 0;
1220 for (i = 0; i < stmt1->n_arg; ++i) {
1221 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
1222 return 0;
1225 return 1;
1228 /* Return 1 if the two pet_types are equivalent.
1230 * We only compare the names of the types since the exact representation
1231 * of the definition may depend on the version of clang being used.
1233 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
1235 if (!type1 || !type2)
1236 return 0;
1238 if (strcmp(type1->name, type2->name))
1239 return 0;
1241 return 1;
1244 /* Return 1 if the two pet_implications are equivalent.
1246 int pet_implication_is_equal(struct pet_implication *implication1,
1247 struct pet_implication *implication2)
1249 if (!implication1 || !implication2)
1250 return 0;
1252 if (implication1->satisfied != implication2->satisfied)
1253 return 0;
1254 if (!isl_map_is_equal(implication1->extension, implication2->extension))
1255 return 0;
1257 return 1;
1260 /* Return 1 if the two pet_independences are equivalent.
1262 int pet_independence_is_equal(struct pet_independence *independence1,
1263 struct pet_independence *independence2)
1265 if (!independence1 || !independence2)
1266 return 0;
1268 if (!isl_union_map_is_equal(independence1->filter,
1269 independence2->filter))
1270 return 0;
1271 if (!isl_union_set_is_equal(independence1->local, independence2->local))
1272 return 0;
1274 return 1;
1277 /* Return 1 if the two pet_scops are equivalent.
1279 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
1281 int i;
1282 int equal;
1284 if (!scop1 || !scop2)
1285 return 0;
1287 if (!isl_set_is_equal(scop1->context, scop2->context))
1288 return 0;
1289 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1290 return 0;
1291 equal = isl_schedule_plain_is_equal(scop1->schedule, scop2->schedule);
1292 if (equal < 0)
1293 return -1;
1294 if (!equal)
1295 return 0;
1297 if (scop1->n_type != scop2->n_type)
1298 return 0;
1299 for (i = 0; i < scop1->n_type; ++i)
1300 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1301 return 0;
1303 if (scop1->n_array != scop2->n_array)
1304 return 0;
1305 for (i = 0; i < scop1->n_array; ++i)
1306 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1307 return 0;
1309 if (scop1->n_stmt != scop2->n_stmt)
1310 return 0;
1311 for (i = 0; i < scop1->n_stmt; ++i)
1312 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1313 return 0;
1315 if (scop1->n_implication != scop2->n_implication)
1316 return 0;
1317 for (i = 0; i < scop1->n_implication; ++i)
1318 if (!pet_implication_is_equal(scop1->implications[i],
1319 scop2->implications[i]))
1320 return 0;
1322 if (scop1->n_independence != scop2->n_independence)
1323 return 0;
1324 for (i = 0; i < scop1->n_independence; ++i)
1325 if (!pet_independence_is_equal(scop1->independences[i],
1326 scop2->independences[i]))
1327 return 0;
1329 return 1;
1332 /* Does the set "extent" reference a virtual array, i.e.,
1333 * one with user pointer equal to NULL?
1334 * A virtual array does not have any members.
1336 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1338 isl_id *id;
1339 int is_virtual;
1341 if (!isl_set_has_tuple_id(extent))
1342 return 0;
1343 if (isl_set_is_wrapping(extent))
1344 return 0;
1345 id = isl_set_get_tuple_id(extent);
1346 is_virtual = !isl_id_get_user(id);
1347 isl_id_free(id);
1349 return is_virtual;
1352 /* Intersect the initial dimensions of "array" with "domain", provided
1353 * that "array" represents a virtual array.
1355 * If "array" is virtual, then We take the preimage of "domain"
1356 * over the projection of the extent of "array" onto its initial dimensions
1357 * and intersect this extent with the result.
1359 static struct pet_array *virtual_array_intersect_domain_prefix(
1360 struct pet_array *array, __isl_take isl_set *domain)
1362 int n;
1363 isl_space *space;
1364 isl_multi_aff *ma;
1366 if (!array || !extent_is_virtual_array(array->extent)) {
1367 isl_set_free(domain);
1368 return array;
1371 space = isl_set_get_space(array->extent);
1372 n = isl_set_dim(domain, isl_dim_set);
1373 ma = pet_prefix_projection(space, n);
1374 domain = isl_set_preimage_multi_aff(domain, ma);
1376 array->extent = isl_set_intersect(array->extent, domain);
1377 if (!array->extent)
1378 return pet_array_free(array);
1380 return array;
1383 /* Intersect the initial dimensions of the domain of "stmt"
1384 * with "domain".
1386 * We take the preimage of "domain" over the projection of the
1387 * domain of "stmt" onto its initial dimensions and intersect
1388 * the domain of "stmt" with the result.
1390 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1391 __isl_take isl_set *domain)
1393 int n;
1394 isl_space *space;
1395 isl_multi_aff *ma;
1397 if (!stmt)
1398 goto error;
1400 space = isl_set_get_space(stmt->domain);
1401 n = isl_set_dim(domain, isl_dim_set);
1402 ma = pet_prefix_projection(space, n);
1403 domain = isl_set_preimage_multi_aff(domain, ma);
1405 stmt->domain = isl_set_intersect(stmt->domain, domain);
1406 if (!stmt->domain)
1407 return pet_stmt_free(stmt);
1409 return stmt;
1410 error:
1411 isl_set_free(domain);
1412 return pet_stmt_free(stmt);
1415 /* Intersect the initial dimensions of the domain of "implication"
1416 * with "domain".
1418 * We take the preimage of "domain" over the projection of the
1419 * domain of "implication" onto its initial dimensions and intersect
1420 * the domain of "implication" with the result.
1422 static struct pet_implication *implication_intersect_domain_prefix(
1423 struct pet_implication *implication, __isl_take isl_set *domain)
1425 int n;
1426 isl_space *space;
1427 isl_multi_aff *ma;
1429 if (!implication)
1430 goto error;
1432 space = isl_map_get_space(implication->extension);
1433 n = isl_set_dim(domain, isl_dim_set);
1434 ma = pet_prefix_projection(isl_space_domain(space), n);
1435 domain = isl_set_preimage_multi_aff(domain, ma);
1437 implication->extension =
1438 isl_map_intersect_domain(implication->extension, domain);
1439 if (!implication->extension)
1440 return pet_implication_free(implication);
1442 return implication;
1443 error:
1444 isl_set_free(domain);
1445 return pet_implication_free(implication);
1448 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1450 * The extents of the virtual arrays match the iteration domains,
1451 * so if the iteration domain changes, we need to change those extents too.
1453 * The domain of the schedule is intersected with (i.e., replaced by)
1454 * the union of the updated iteration domains.
1456 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1457 __isl_take isl_set *domain)
1459 int i;
1461 if (!scop)
1462 goto error;
1464 for (i = 0; i < scop->n_array; ++i) {
1465 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1466 scop->arrays[i], isl_set_copy(domain));
1467 if (!scop->arrays[i])
1468 goto error;
1471 for (i = 0; i < scop->n_stmt; ++i) {
1472 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1473 isl_set_copy(domain));
1474 if (!scop->stmts[i])
1475 goto error;
1478 for (i = 0; i < scop->n_implication; ++i) {
1479 scop->implications[i] =
1480 implication_intersect_domain_prefix(scop->implications[i],
1481 isl_set_copy(domain));
1482 if (!scop->implications[i])
1483 return pet_scop_free(scop);
1486 scop->schedule = isl_schedule_intersect_domain(scop->schedule,
1487 pet_scop_get_instance_set(scop));
1488 if (!scop->schedule)
1489 goto error;
1491 isl_set_free(domain);
1492 return scop;
1493 error:
1494 isl_set_free(domain);
1495 return pet_scop_free(scop);
1498 /* Update the context with respect to an embedding into a loop
1499 * with iteration domain "dom".
1500 * The input context lives in the same space as "dom".
1501 * The output context has the inner dimension removed.
1503 * An outer loop iterator value is invalid for the embedding if
1504 * any of the corresponding inner iterator values is invalid.
1505 * That is, an outer loop iterator value is valid only if all the corresponding
1506 * inner iterator values are valid.
1507 * We therefore compute the set of outer loop iterators l
1509 * forall i: dom(l,i) => valid(l,i)
1511 * or
1513 * forall i: not dom(l,i) or valid(l,i)
1515 * or
1517 * not exists i: dom(l,i) and not valid(l,i)
1519 * i.e.,
1521 * not exists i: (dom \ valid)(l,i)
1523 * If there are any unnamed parameters in "dom", then we consider
1524 * a parameter value to be valid if it is valid for any value of those
1525 * unnamed parameters. They are therefore projected out at the end.
1527 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1528 __isl_keep isl_set *dom)
1530 int pos;
1532 pos = isl_set_dim(context, isl_dim_set) - 1;
1533 context = isl_set_subtract(isl_set_copy(dom), context);
1534 context = isl_set_project_out(context, isl_dim_set, pos, 1);
1535 context = isl_set_complement(context);
1536 context = pet_nested_remove_from_set(context);
1538 return context;
1541 /* Internal data structure for outer_projection_mupa.
1543 * "n" is the number of outer dimensions onto which to project.
1544 * "res" collects the result.
1546 struct pet_outer_projection_data {
1547 int n;
1548 isl_union_pw_multi_aff *res;
1551 /* Create a function that maps "set" onto its outer data->n dimensions and
1552 * add it to data->res.
1554 static isl_stat add_outer_projection(__isl_take isl_set *set, void *user)
1556 struct pet_outer_projection_data *data = user;
1557 int dim;
1558 isl_space *space;
1559 isl_pw_multi_aff *pma;
1561 dim = isl_set_dim(set, isl_dim_set);
1562 space = isl_set_get_space(set);
1563 pma = isl_pw_multi_aff_project_out_map(space,
1564 isl_dim_set, data->n, dim - data->n);
1565 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
1567 isl_set_free(set);
1569 return isl_stat_ok;
1572 /* Create and return a function that maps the sets in "domain"
1573 * onto their outer "n" dimensions.
1575 static __isl_give isl_multi_union_pw_aff *outer_projection_mupa(
1576 __isl_take isl_union_set *domain, int n)
1578 struct pet_outer_projection_data data;
1579 isl_space *space;
1581 space = isl_union_set_get_space(domain);
1582 data.n = n;
1583 data.res = isl_union_pw_multi_aff_empty(space);
1584 if (isl_union_set_foreach_set(domain, &add_outer_projection, &data) < 0)
1585 data.res = isl_union_pw_multi_aff_free(data.res);
1587 isl_union_set_free(domain);
1588 return isl_multi_union_pw_aff_from_union_pw_multi_aff(data.res);
1591 /* Embed "schedule" in a loop with schedule "prefix".
1592 * The domain of "prefix" corresponds to the outer dimensions
1593 * of the iteration domains.
1594 * We therefore construct a projection onto these outer dimensions,
1595 * compose it with "prefix" and then add the result as a band schedule.
1597 * If the domain of the schedule is empty, then there is no need
1598 * to insert any node.
1600 static __isl_give isl_schedule *schedule_embed(
1601 __isl_take isl_schedule *schedule, __isl_keep isl_multi_aff *prefix)
1603 int n;
1604 int empty;
1605 isl_union_set *domain;
1606 isl_multi_aff *ma;
1607 isl_multi_union_pw_aff *mupa;
1609 domain = isl_schedule_get_domain(schedule);
1610 empty = isl_union_set_is_empty(domain);
1611 if (empty < 0 || empty) {
1612 isl_union_set_free(domain);
1613 return empty < 0 ? isl_schedule_free(schedule) : schedule;
1616 n = isl_multi_aff_dim(prefix, isl_dim_in);
1617 mupa = outer_projection_mupa(domain, n);
1618 ma = isl_multi_aff_copy(prefix);
1619 mupa = isl_multi_union_pw_aff_apply_multi_aff(mupa, ma);
1620 schedule = isl_schedule_insert_partial_schedule(schedule, mupa);
1622 return schedule;
1625 /* Adjust the context and the schedule according to an embedding
1626 * in a loop with iteration domain "dom" and schedule "sched".
1628 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1629 __isl_take isl_multi_aff *sched)
1631 int i;
1633 if (!scop)
1634 goto error;
1636 scop->context = context_embed(scop->context, dom);
1637 if (!scop->context)
1638 goto error;
1640 scop->schedule = schedule_embed(scop->schedule, sched);
1641 if (!scop->schedule)
1642 goto error;
1644 isl_set_free(dom);
1645 isl_multi_aff_free(sched);
1646 return scop;
1647 error:
1648 isl_set_free(dom);
1649 isl_multi_aff_free(sched);
1650 return pet_scop_free(scop);
1653 /* Add extra conditions to scop->skip[type].
1655 * The new skip condition only holds if it held before
1656 * and the condition is true. It does not hold if it did not hold
1657 * before or the condition is false.
1659 * The skip condition is assumed to be an affine expression.
1661 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1662 enum pet_skip type, __isl_keep isl_set *cond)
1664 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1665 isl_pw_aff *skip;
1666 isl_set *dom;
1668 if (!scop)
1669 return NULL;
1670 if (!ext->skip[type])
1671 return scop;
1673 if (!multi_pw_aff_is_affine(ext->skip[type]))
1674 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1675 isl_error_internal, "can only restrict affine skips",
1676 return pet_scop_free(scop));
1678 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1679 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1680 cond = isl_set_copy(cond);
1681 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1682 skip = indicator_function(cond, dom);
1683 isl_multi_pw_aff_free(ext->skip[type]);
1684 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1685 if (!ext->skip[type])
1686 return pet_scop_free(scop);
1688 return scop;
1691 /* Adjust the context and the skip conditions to the fact that
1692 * the scop was created in a context where "cond" holds.
1694 * An outer loop iterator or parameter value is valid for the result
1695 * if it was valid for the original scop and satisfies "cond" or if it does
1696 * not satisfy "cond" as in this case the scop is not executed
1697 * and the original constraints on these values are irrelevant.
1699 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1700 __isl_take isl_set *cond)
1702 int i;
1704 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1705 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1707 if (!scop)
1708 goto error;
1710 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1711 scop->context = isl_set_union(scop->context,
1712 isl_set_complement(isl_set_copy(cond)));
1713 scop->context = isl_set_coalesce(scop->context);
1714 scop->context = pet_nested_remove_from_set(scop->context);
1715 if (!scop->context)
1716 goto error;
1718 isl_set_free(cond);
1719 return scop;
1720 error:
1721 isl_set_free(cond);
1722 return pet_scop_free(scop);
1725 /* Insert an argument expression corresponding to "test" in front
1726 * of the list of arguments described by *n_arg and *args.
1728 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1729 __isl_keep isl_multi_pw_aff *test)
1731 int i;
1732 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1734 if (!test)
1735 return -1;
1737 if (!*args) {
1738 *args = isl_calloc_array(ctx, pet_expr *, 1);
1739 if (!*args)
1740 return -1;
1741 } else {
1742 pet_expr **ext;
1743 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1744 if (!ext)
1745 return -1;
1746 for (i = 0; i < *n_arg; ++i)
1747 ext[1 + i] = (*args)[i];
1748 free(*args);
1749 *args = ext;
1751 (*n_arg)++;
1752 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1753 if (!(*args)[0])
1754 return -1;
1756 return 0;
1759 /* Look through the applications in "scop" for any that can be
1760 * applied to the filter expressed by "map" and "satisified".
1761 * If there is any, then apply it to "map" and return the result.
1762 * Otherwise, return "map".
1763 * "id" is the identifier of the virtual array.
1765 * We only introduce at most one implication for any given virtual array,
1766 * so we can apply the implication and return as soon as we find one.
1768 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1769 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1771 int i;
1773 for (i = 0; i < scop->n_implication; ++i) {
1774 struct pet_implication *pi = scop->implications[i];
1775 isl_id *pi_id;
1777 if (pi->satisfied != satisfied)
1778 continue;
1779 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1780 isl_id_free(pi_id);
1781 if (pi_id != id)
1782 continue;
1784 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1787 return map;
1790 /* Is the filter expressed by "test" and "satisfied" implied
1791 * by filter "pos" on "domain", with filter "expr", taking into
1792 * account the implications of "scop"?
1794 * For filter on domain implying that expressed by "test" and "satisfied",
1795 * the filter needs to be an access to the same (virtual) array as "test" and
1796 * the filter value needs to be equal to "satisfied".
1797 * Moreover, the filter access relation, possibly extended by
1798 * the implications in "scop" needs to contain "test".
1800 static int implies_filter(struct pet_scop *scop,
1801 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1802 __isl_keep isl_map *test, int satisfied)
1804 isl_id *test_id, *arg_id;
1805 isl_val *val;
1806 int is_int;
1807 int s;
1808 int is_subset;
1809 isl_map *implied;
1811 if (expr->type != pet_expr_access)
1812 return 0;
1813 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1814 arg_id = pet_expr_access_get_id(expr);
1815 isl_id_free(arg_id);
1816 isl_id_free(test_id);
1817 if (test_id != arg_id)
1818 return 0;
1819 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1820 is_int = isl_val_is_int(val);
1821 if (is_int)
1822 s = isl_val_get_num_si(val);
1823 isl_val_free(val);
1824 if (!val)
1825 return -1;
1826 if (!is_int)
1827 return 0;
1828 if (s != satisfied)
1829 return 0;
1831 implied = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
1832 implied = apply_implications(scop, implied, test_id, satisfied);
1833 is_subset = isl_map_is_subset(test, implied);
1834 isl_map_free(implied);
1836 return is_subset;
1839 /* Is the filter expressed by "test" and "satisfied" implied
1840 * by any of the filters on the domain of "stmt", taking into
1841 * account the implications of "scop"?
1843 static int filter_implied(struct pet_scop *scop,
1844 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1846 int i;
1847 int implied;
1848 isl_id *test_id;
1849 isl_map *domain;
1850 isl_map *test_map;
1852 if (!scop || !stmt || !test)
1853 return -1;
1854 if (scop->n_implication == 0)
1855 return 0;
1856 if (stmt->n_arg == 0)
1857 return 0;
1859 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1860 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1862 implied = 0;
1863 for (i = 0; i < stmt->n_arg; ++i) {
1864 implied = implies_filter(scop, domain, i, stmt->args[i],
1865 test_map, satisfied);
1866 if (implied < 0 || implied)
1867 break;
1870 isl_map_free(test_map);
1871 isl_map_free(domain);
1872 return implied;
1875 /* Make the statement "stmt" depend on the value of "test"
1876 * being equal to "satisfied" by adjusting stmt->domain.
1878 * The domain of "test" corresponds to the (zero or more) outer dimensions
1879 * of the iteration domain.
1881 * We first extend "test" to apply to the entire iteration domain and
1882 * then check if the filter that we are about to add is implied
1883 * by any of the current filters, possibly taking into account
1884 * the implications in "scop". If so, we leave "stmt" untouched and return.
1886 * Otherwise, we insert an argument corresponding to a read to "test"
1887 * from the iteration domain of "stmt" in front of the list of arguments.
1888 * We also insert a corresponding output dimension in the wrapped
1889 * map contained in stmt->domain, with value set to "satisfied".
1891 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1892 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1894 int i;
1895 int implied;
1896 isl_id *id;
1897 isl_ctx *ctx;
1898 isl_pw_multi_aff *pma;
1899 isl_multi_aff *add_dom;
1900 isl_space *space;
1901 isl_local_space *ls;
1902 int n_test_dom;
1904 if (!stmt || !test)
1905 goto error;
1907 space = pet_stmt_get_space(stmt);
1908 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1909 space = isl_space_from_domain(space);
1910 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1911 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1912 ls = isl_local_space_from_space(isl_space_domain(space));
1913 for (i = 0; i < n_test_dom; ++i) {
1914 isl_aff *aff;
1915 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1916 isl_dim_set, i);
1917 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1919 isl_local_space_free(ls);
1920 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1922 implied = filter_implied(scop, stmt, test, satisfied);
1923 if (implied < 0)
1924 goto error;
1925 if (implied) {
1926 isl_multi_pw_aff_free(test);
1927 return stmt;
1930 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1931 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1932 id, satisfied);
1933 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1935 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1936 goto error;
1938 isl_multi_pw_aff_free(test);
1939 return stmt;
1940 error:
1941 isl_multi_pw_aff_free(test);
1942 return pet_stmt_free(stmt);
1945 /* Does "scop" have a skip condition of the given "type"?
1947 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1949 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1951 if (!scop)
1952 return -1;
1953 return ext->skip[type] != NULL;
1956 /* Does "scop" have a skip condition of the given "type" that
1957 * is an affine expression?
1959 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1961 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1963 if (!scop)
1964 return -1;
1965 if (!ext->skip[type])
1966 return 0;
1967 return multi_pw_aff_is_affine(ext->skip[type]);
1970 /* Does "scop" have a skip condition of the given "type" that
1971 * is not an affine expression?
1973 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
1975 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1976 int aff;
1978 if (!scop)
1979 return -1;
1980 if (!ext->skip[type])
1981 return 0;
1982 aff = multi_pw_aff_is_affine(ext->skip[type]);
1983 if (aff < 0)
1984 return -1;
1985 return !aff;
1988 /* Does "scop" have a skip condition of the given "type" that
1989 * is affine and holds on the entire domain?
1991 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
1993 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1994 isl_pw_aff *pa;
1995 isl_set *set;
1996 int is_aff;
1997 int is_univ;
1999 is_aff = pet_scop_has_affine_skip(scop, type);
2000 if (is_aff < 0 || !is_aff)
2001 return is_aff;
2003 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2004 set = isl_pw_aff_non_zero_set(pa);
2005 is_univ = isl_set_plain_is_universe(set);
2006 isl_set_free(set);
2008 return is_univ;
2011 /* Replace scop->skip[type] by "skip".
2013 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2014 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2016 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2018 if (!scop || !skip)
2019 goto error;
2021 isl_multi_pw_aff_free(ext->skip[type]);
2022 ext->skip[type] = skip;
2024 return scop;
2025 error:
2026 isl_multi_pw_aff_free(skip);
2027 return pet_scop_free(scop);
2030 /* Return a copy of scop->skip[type].
2032 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2033 enum pet_skip type)
2035 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2037 if (!scop)
2038 return NULL;
2040 return isl_multi_pw_aff_copy(ext->skip[type]);
2043 /* Assuming scop->skip[type] is an affine expression,
2044 * return the constraints on the outer loop domain for which the skip condition
2045 * holds.
2047 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2048 enum pet_skip type)
2050 isl_multi_pw_aff *skip;
2051 isl_pw_aff *pa;
2053 skip = pet_scop_get_skip(scop, type);
2054 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2055 isl_multi_pw_aff_free(skip);
2056 return isl_pw_aff_non_zero_set(pa);
2059 /* Return the identifier of the variable that is accessed by
2060 * the skip condition of the given type.
2062 * The skip condition is assumed not to be an affine condition.
2064 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2065 enum pet_skip type)
2067 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2069 if (!scop)
2070 return NULL;
2072 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2075 /* Return an access pet_expr corresponding to the skip condition
2076 * of the given type.
2078 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2079 enum pet_skip type)
2081 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2084 /* Drop the skip condition scop->skip[type].
2086 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2088 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2090 if (!scop)
2091 return;
2093 isl_multi_pw_aff_free(ext->skip[type]);
2094 ext->skip[type] = NULL;
2097 /* Drop all skip conditions on "scop".
2099 struct pet_scop *pet_scop_reset_skips(struct pet_scop *scop)
2101 pet_scop_reset_skip(scop, pet_skip_now);
2102 pet_scop_reset_skip(scop, pet_skip_later);
2104 return scop;
2107 /* Make the skip condition (if any) depend on the value of "test" being
2108 * equal to "satisfied".
2110 * We only support the case where the original skip condition is universal,
2111 * i.e., where skipping is unconditional, and where satisfied == 1.
2112 * In this case, the skip condition is changed to skip only when
2113 * "test" is equal to one.
2115 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2116 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2118 int is_univ = 0;
2120 if (!scop)
2121 return NULL;
2122 if (!pet_scop_has_skip(scop, type))
2123 return scop;
2125 if (satisfied)
2126 is_univ = pet_scop_has_universal_skip(scop, type);
2127 if (is_univ < 0)
2128 return pet_scop_free(scop);
2129 if (satisfied && is_univ) {
2130 isl_multi_pw_aff *skip;
2131 skip = isl_multi_pw_aff_copy(test);
2132 scop = pet_scop_set_skip(scop, type, skip);
2133 if (!scop)
2134 return NULL;
2135 } else {
2136 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2137 "skip expression cannot be filtered",
2138 return pet_scop_free(scop));
2141 return scop;
2144 /* Make all statements in "scop" depend on the value of "test"
2145 * being equal to "satisfied" by adjusting their domains.
2147 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2148 __isl_take isl_multi_pw_aff *test, int satisfied)
2150 int i;
2152 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2153 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2155 if (!scop || !test)
2156 goto error;
2158 for (i = 0; i < scop->n_stmt; ++i) {
2159 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2160 isl_multi_pw_aff_copy(test), satisfied);
2161 if (!scop->stmts[i])
2162 goto error;
2165 isl_multi_pw_aff_free(test);
2166 return scop;
2167 error:
2168 isl_multi_pw_aff_free(test);
2169 return pet_scop_free(scop);
2172 /* Add the parameters of the access expression "expr" to "space".
2174 static int access_collect_params(__isl_keep pet_expr *expr, void *user)
2176 int i;
2177 isl_space *expr_space;
2178 isl_space **space = user;
2180 expr_space = pet_expr_access_get_parameter_space(expr);
2181 *space = isl_space_align_params(*space, expr_space);
2183 return *space ? 0 : -1;
2186 /* Add all parameters in "stmt" to "space" and return the result.
2188 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2189 __isl_take isl_space *space)
2191 int i;
2193 if (!stmt)
2194 return isl_space_free(space);
2196 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2197 for (i = 0; i < stmt->n_arg; ++i)
2198 if (pet_expr_foreach_access_expr(stmt->args[i],
2199 &access_collect_params, &space) < 0)
2200 space = isl_space_free(space);
2201 if (pet_tree_foreach_access_expr(stmt->body, &access_collect_params,
2202 &space) < 0)
2203 space = isl_space_free(space);
2205 return space;
2208 /* Add all parameters in "array" to "space" and return the result.
2210 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2211 __isl_take isl_space *space)
2213 if (!array)
2214 return isl_space_free(space);
2216 space = isl_space_align_params(space,
2217 isl_set_get_space(array->context));
2218 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2220 return space;
2223 /* Add all parameters in "independence" to "space" and return the result.
2225 static __isl_give isl_space *independence_collect_params(
2226 struct pet_independence *independence, __isl_take isl_space *space)
2228 if (!independence)
2229 return isl_space_free(space);
2231 space = isl_space_align_params(space,
2232 isl_union_map_get_space(independence->filter));
2233 space = isl_space_align_params(space,
2234 isl_union_set_get_space(independence->local));
2236 return space;
2239 /* Collect all parameters in "scop" in a parameter space and return the result.
2241 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop)
2243 isl_space *space;
2244 int i;
2246 if (!scop)
2247 return NULL;
2249 space = isl_set_get_space(scop->context);
2251 for (i = 0; i < scop->n_array; ++i)
2252 space = array_collect_params(scop->arrays[i], space);
2254 for (i = 0; i < scop->n_stmt; ++i)
2255 space = stmt_collect_params(scop->stmts[i], space);
2257 for (i = 0; i < scop->n_independence; ++i)
2258 space = independence_collect_params(scop->independences[i],
2259 space);
2261 return space;
2264 /* Add all parameters in "space" to the domain and
2265 * all access relations in "stmt".
2267 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2268 __isl_take isl_space *space)
2270 int i;
2272 if (!stmt)
2273 goto error;
2275 stmt->domain = isl_set_align_params(stmt->domain,
2276 isl_space_copy(space));
2278 for (i = 0; i < stmt->n_arg; ++i) {
2279 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2280 isl_space_copy(space));
2281 if (!stmt->args[i])
2282 goto error;
2284 stmt->body = pet_tree_align_params(stmt->body, isl_space_copy(space));
2286 if (!stmt->domain || !stmt->body)
2287 goto error;
2289 isl_space_free(space);
2290 return stmt;
2291 error:
2292 isl_space_free(space);
2293 return pet_stmt_free(stmt);
2296 /* Add all parameters in "space" to "array".
2298 static struct pet_array *array_propagate_params(struct pet_array *array,
2299 __isl_take isl_space *space)
2301 if (!array)
2302 goto error;
2304 array->context = isl_set_align_params(array->context,
2305 isl_space_copy(space));
2306 array->extent = isl_set_align_params(array->extent,
2307 isl_space_copy(space));
2308 if (array->value_bounds) {
2309 array->value_bounds = isl_set_align_params(array->value_bounds,
2310 isl_space_copy(space));
2311 if (!array->value_bounds)
2312 goto error;
2315 if (!array->context || !array->extent)
2316 goto error;
2318 isl_space_free(space);
2319 return array;
2320 error:
2321 isl_space_free(space);
2322 return pet_array_free(array);
2325 /* Add all parameters in "space" to "independence".
2327 static struct pet_independence *independence_propagate_params(
2328 struct pet_independence *independence, __isl_take isl_space *space)
2330 if (!independence)
2331 goto error;
2333 independence->filter = isl_union_map_align_params(independence->filter,
2334 isl_space_copy(space));
2335 independence->local = isl_union_set_align_params(independence->local,
2336 isl_space_copy(space));
2337 if (!independence->filter || !independence->local)
2338 goto error;
2340 isl_space_free(space);
2341 return independence;
2342 error:
2343 isl_space_free(space);
2344 return pet_independence_free(independence);
2347 /* Add all parameters in "space" to "scop".
2349 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2350 __isl_take isl_space *space)
2352 int i;
2354 if (!scop)
2355 goto error;
2357 scop->context = isl_set_align_params(scop->context,
2358 isl_space_copy(space));
2359 scop->schedule = isl_schedule_align_params(scop->schedule,
2360 isl_space_copy(space));
2361 if (!scop->context || !scop->schedule)
2362 goto error;
2364 for (i = 0; i < scop->n_array; ++i) {
2365 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2366 isl_space_copy(space));
2367 if (!scop->arrays[i])
2368 goto error;
2371 for (i = 0; i < scop->n_stmt; ++i) {
2372 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2373 isl_space_copy(space));
2374 if (!scop->stmts[i])
2375 goto error;
2378 for (i = 0; i < scop->n_independence; ++i) {
2379 scop->independences[i] = independence_propagate_params(
2380 scop->independences[i], isl_space_copy(space));
2381 if (!scop->independences[i])
2382 goto error;
2385 isl_space_free(space);
2386 return scop;
2387 error:
2388 isl_space_free(space);
2389 return pet_scop_free(scop);
2392 /* Update all isl_sets and isl_maps in "scop" such that they all
2393 * have the same parameters.
2395 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2397 isl_space *space;
2399 if (!scop)
2400 return NULL;
2402 space = scop_collect_params(scop);
2404 scop = scop_propagate_params(scop, space);
2406 return scop;
2409 /* Add the access relation of the give "type" of the access expression "expr"
2410 * to "accesses" and return the result.
2411 * The domain of the access relation is intersected with "domain".
2412 * If "tag" is set, then the access relation is tagged with
2413 * the corresponding reference identifier.
2415 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2416 enum pet_expr_access_type type, int tag,
2417 __isl_take isl_union_map *accesses, __isl_keep isl_union_set *domain)
2419 isl_union_map *access;
2421 access = pet_expr_access_get_access(expr, type);
2422 access = isl_union_map_intersect_domain(access,
2423 isl_union_set_copy(domain));
2424 if (tag)
2425 access = pet_expr_tag_access(expr, access);
2426 return isl_union_map_union(accesses, access);
2429 /* Internal data structure for expr_collect_accesses.
2431 * "type" is the type of accesses we want to collect.
2432 * "tag" is set if the access relations should be tagged with
2433 * the corresponding reference identifiers.
2434 * "domain" are constraints on the domain of the access relations.
2435 * "accesses" collects the results.
2437 struct pet_expr_collect_accesses_data {
2438 enum pet_expr_access_type type;
2439 int tag;
2440 isl_union_set *domain;
2442 isl_union_map *accesses;
2445 /* Add the access relation of the access expression "expr"
2446 * to data->accesses if the access expression is a read and we are collecting
2447 * reads and/or it is a write and we are collecting writes.
2448 * The domains of the access relations are intersected with data->domain.
2449 * If data->tag is set, then the access relations are tagged with
2450 * the corresponding reference identifiers.
2452 * If data->type is pet_expr_access_must_write, then we only add
2453 * the accesses that are definitely performed. Otherwise, we add
2454 * all potential accesses.
2455 * In particular, if the access has any arguments, then in case of
2456 * pet_expr_access_must_write we currently skip the access completely.
2457 * In other cases, we project out the values of the access arguments.
2459 static int expr_collect_accesses(__isl_keep pet_expr *expr, void *user)
2461 struct pet_expr_collect_accesses_data *data = user;
2462 int i;
2463 isl_id *id;
2464 isl_space *dim;
2466 if (!expr)
2467 return -1;
2469 if (pet_expr_is_affine(expr))
2470 return 0;
2471 if (data->type == pet_expr_access_must_write && expr->n_arg != 0)
2472 return 0;
2474 if ((data->type == pet_expr_access_may_read && expr->acc.read) ||
2475 ((data->type == pet_expr_access_may_write ||
2476 data->type == pet_expr_access_must_write) && expr->acc.write))
2477 data->accesses = expr_collect_access(expr,
2478 data->type, data->tag,
2479 data->accesses, data->domain);
2481 return data->accesses ? 0 : -1;
2484 /* Collect and return all access relations of the given "type" in "stmt".
2485 * If "tag" is set, then the access relations are tagged with
2486 * the corresponding reference identifiers.
2487 * If "type" is pet_expr_access_killed, then "stmt" is a kill statement and
2488 * we simply add the argument of the kill operation.
2490 * If we are looking for definite accesses (pet_expr_access_must_write
2491 * or pet_expr_access_killed), then we only add the accesses that are
2492 * definitely performed. Otherwise, we add all potential accesses.
2493 * In particular, if the statement has any arguments, then if we are looking
2494 * for definite accesses we currently skip the statement completely. Othewise,
2495 * we project out the values of the statement arguments.
2496 * If the statement body is not an expression tree, then we cannot
2497 * know for sure if/when the accesses inside the tree are performed.
2498 * We therefore ignore such statements when we are looking for
2499 * definite accesses.
2501 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2502 enum pet_expr_access_type type, int tag, __isl_take isl_space *dim)
2504 struct pet_expr_collect_accesses_data data = { type, tag };
2505 int must;
2506 isl_set *domain;
2508 if (!stmt)
2509 return NULL;
2511 data.accesses = isl_union_map_empty(dim);
2513 if (type == pet_expr_access_must_write ||
2514 type == pet_expr_access_killed)
2515 must = 1;
2516 else
2517 must = 0;
2519 if (must && stmt->n_arg > 0)
2520 return data.accesses;
2521 if (must && pet_tree_get_type(stmt->body) != pet_tree_expr)
2522 return data.accesses;
2524 domain = drop_arguments(isl_set_copy(stmt->domain));
2525 data.domain = isl_union_set_from_set(domain);
2527 if (type == pet_expr_access_killed) {
2528 pet_expr *body, *arg;
2530 body = pet_tree_expr_get_expr(stmt->body);
2531 arg = pet_expr_get_arg(body, 0);
2532 data.accesses = expr_collect_access(arg,
2533 pet_expr_access_killed, tag,
2534 data.accesses, data.domain);
2535 pet_expr_free(arg);
2536 pet_expr_free(body);
2537 } else if (pet_tree_foreach_access_expr(stmt->body,
2538 &expr_collect_accesses, &data) < 0)
2539 data.accesses = isl_union_map_free(data.accesses);
2541 isl_union_set_free(data.domain);
2543 return data.accesses;
2546 /* Is "stmt" an assignment statement?
2548 int pet_stmt_is_assign(struct pet_stmt *stmt)
2550 if (!stmt)
2551 return 0;
2552 return pet_tree_is_assign(stmt->body);
2555 /* Is "stmt" a kill statement?
2557 int pet_stmt_is_kill(struct pet_stmt *stmt)
2559 if (!stmt)
2560 return 0;
2561 return pet_tree_is_kill(stmt->body);
2564 /* Is "stmt" an assume statement?
2566 int pet_stmt_is_assume(struct pet_stmt *stmt)
2568 if (!stmt)
2569 return 0;
2570 return pet_tree_is_assume(stmt->body);
2573 /* Helper function to add a domain gisted copy of "map" (wrt "set") to "umap".
2575 static __isl_give isl_union_map *add_gisted(__isl_take isl_union_map *umap,
2576 __isl_keep isl_map *map, __isl_keep isl_set *set)
2578 isl_map *gist;
2580 gist = isl_map_copy(map);
2581 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2582 return isl_union_map_add_map(umap, gist);
2585 /* Compute a mapping from all arrays (of structs) in scop
2586 * to their members.
2588 * If "from_outermost" is set, then the domain only consists
2589 * of outermost arrays.
2590 * If "to_innermost" is set, then the range only consists
2591 * of innermost arrays.
2593 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop,
2594 int from_outermost, int to_innermost)
2596 int i;
2597 isl_union_map *to_inner;
2599 if (!scop)
2600 return NULL;
2602 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2604 for (i = 0; i < scop->n_array; ++i) {
2605 struct pet_array *array = scop->arrays[i];
2606 isl_set *set;
2607 isl_map *map;
2609 if (to_innermost && array->element_is_record)
2610 continue;
2612 set = isl_set_copy(array->extent);
2613 map = isl_set_identity(isl_set_copy(set));
2615 while (set && isl_set_is_wrapping(set)) {
2616 isl_id *id;
2617 isl_map *wrapped;
2619 if (!from_outermost)
2620 to_inner = add_gisted(to_inner, map, set);
2622 id = isl_set_get_tuple_id(set);
2623 wrapped = isl_set_unwrap(set);
2624 wrapped = isl_map_domain_map(wrapped);
2625 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2626 map = isl_map_apply_domain(map, wrapped);
2627 set = isl_map_domain(isl_map_copy(map));
2630 map = isl_map_gist_domain(map, set);
2631 to_inner = isl_union_map_add_map(to_inner, map);
2634 return to_inner;
2637 /* Compute a mapping from all arrays (of structs) in scop
2638 * to their innermost arrays.
2640 * In particular, for each array of a primitive type, the result
2641 * contains the identity mapping on that array.
2642 * For each array involving member accesses, the result
2643 * contains a mapping from the elements of any intermediate array of structs
2644 * to all corresponding elements of the innermost nested arrays.
2646 static __isl_give isl_union_map *pet_scop_compute_any_to_inner(
2647 struct pet_scop *scop)
2649 return compute_to_inner(scop, 0, 1);
2652 /* Compute a mapping from all outermost arrays (of structs) in scop
2653 * to their innermost members.
2655 __isl_give isl_union_map *pet_scop_compute_outer_to_inner(struct pet_scop *scop)
2657 return compute_to_inner(scop, 1, 1);
2660 /* Compute a mapping from all outermost arrays (of structs) in scop
2661 * to their members, including the outermost arrays themselves.
2663 __isl_give isl_union_map *pet_scop_compute_outer_to_any(struct pet_scop *scop)
2665 return compute_to_inner(scop, 1, 0);
2668 /* Collect and return all access relations of the given "type" in "scop".
2669 * If "type" is pet_expr_access_killed, then we only add the arguments of
2670 * kill operations.
2671 * If we are looking for definite accesses (pet_expr_access_must_write
2672 * or pet_expr_access_killed), then we only add the accesses that are
2673 * definitely performed. Otherwise, we add all potential accesses.
2674 * If "tag" is set, then the access relations are tagged with
2675 * the corresponding reference identifiers.
2676 * For accesses to structures, the returned access relation accesses
2677 * all individual fields in the structures.
2679 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2680 enum pet_expr_access_type type, int tag)
2682 int i;
2683 isl_union_map *accesses;
2684 isl_union_set *arrays;
2685 isl_union_map *to_inner;
2687 if (!scop)
2688 return NULL;
2690 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2692 for (i = 0; i < scop->n_stmt; ++i) {
2693 struct pet_stmt *stmt = scop->stmts[i];
2694 isl_union_map *accesses_i;
2695 isl_space *space;
2697 if (type == pet_expr_access_killed && !pet_stmt_is_kill(stmt))
2698 continue;
2700 space = isl_set_get_space(scop->context);
2701 accesses_i = stmt_collect_accesses(stmt, type, tag, space);
2702 accesses = isl_union_map_union(accesses, accesses_i);
2705 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2706 for (i = 0; i < scop->n_array; ++i) {
2707 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2708 arrays = isl_union_set_add_set(arrays, extent);
2710 accesses = isl_union_map_intersect_range(accesses, arrays);
2712 to_inner = pet_scop_compute_any_to_inner(scop);
2713 accesses = isl_union_map_apply_range(accesses, to_inner);
2715 return accesses;
2718 /* Return the potential read access relation.
2720 __isl_give isl_union_map *pet_scop_get_may_reads(struct pet_scop *scop)
2722 return scop_collect_accesses(scop, pet_expr_access_may_read, 0);
2725 /* Return the potential write access relation.
2727 __isl_give isl_union_map *pet_scop_get_may_writes(struct pet_scop *scop)
2729 return scop_collect_accesses(scop, pet_expr_access_may_write, 0);
2732 /* Return the definite write access relation.
2734 __isl_give isl_union_map *pet_scop_get_must_writes(struct pet_scop *scop)
2736 return scop_collect_accesses(scop, pet_expr_access_must_write, 0);
2739 /* Return the definite kill access relation.
2741 __isl_give isl_union_map *pet_scop_get_must_kills(struct pet_scop *scop)
2743 return scop_collect_accesses(scop, pet_expr_access_killed, 0);
2746 /* Return the tagged potential read access relation.
2748 __isl_give isl_union_map *pet_scop_get_tagged_may_reads(
2749 struct pet_scop *scop)
2751 return scop_collect_accesses(scop, pet_expr_access_may_read, 1);
2754 /* Return the tagged potential write access relation.
2756 __isl_give isl_union_map *pet_scop_get_tagged_may_writes(
2757 struct pet_scop *scop)
2759 return scop_collect_accesses(scop, pet_expr_access_may_write, 1);
2762 /* Return the tagged definite write access relation.
2764 __isl_give isl_union_map *pet_scop_get_tagged_must_writes(
2765 struct pet_scop *scop)
2767 return scop_collect_accesses(scop, pet_expr_access_must_write, 1);
2770 /* Return the tagged definite kill access relation.
2772 __isl_give isl_union_map *pet_scop_get_tagged_must_kills(
2773 struct pet_scop *scop)
2775 return scop_collect_accesses(scop, pet_expr_access_killed, 1);
2778 /* Collect and return the set of all statement instances in "scop".
2780 __isl_give isl_union_set *pet_scop_get_instance_set(struct pet_scop *scop)
2782 int i;
2783 isl_set *domain_i;
2784 isl_union_set *domain;
2786 if (!scop)
2787 return NULL;
2789 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2791 for (i = 0; i < scop->n_stmt; ++i) {
2792 domain_i = isl_set_copy(scop->stmts[i]->domain);
2793 if (scop->stmts[i]->n_arg > 0)
2794 domain_i = isl_map_domain(isl_set_unwrap(domain_i));
2795 domain = isl_union_set_add_set(domain, domain_i);
2798 return domain;
2801 /* Return the context of "scop".
2803 __isl_give isl_set *pet_scop_get_context(__isl_keep pet_scop *scop)
2805 if (!scop)
2806 return NULL;
2808 return isl_set_copy(scop->context);
2811 /* Return the schedule of "scop".
2813 __isl_give isl_schedule *pet_scop_get_schedule(__isl_keep pet_scop *scop)
2815 if (!scop)
2816 return NULL;
2818 return isl_schedule_copy(scop->schedule);
2821 /* Add a reference identifier to all access expressions in "stmt".
2822 * "n_ref" points to an integer that contains the sequence number
2823 * of the next reference.
2825 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2827 int i;
2829 if (!stmt)
2830 return NULL;
2832 for (i = 0; i < stmt->n_arg; ++i) {
2833 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2834 if (!stmt->args[i])
2835 return pet_stmt_free(stmt);
2838 stmt->body = pet_tree_add_ref_ids(stmt->body, n_ref);
2839 if (!stmt->body)
2840 return pet_stmt_free(stmt);
2842 return stmt;
2845 /* Add a reference identifier to all access expressions in "scop".
2847 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2849 int i;
2850 int n_ref;
2852 if (!scop)
2853 return NULL;
2855 n_ref = 0;
2856 for (i = 0; i < scop->n_stmt; ++i) {
2857 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2858 if (!scop->stmts[i])
2859 return pet_scop_free(scop);
2862 return scop;
2865 /* Reset the user pointer on all parameter ids in "array".
2867 static struct pet_array *array_anonymize(struct pet_array *array)
2869 if (!array)
2870 return NULL;
2872 array->context = isl_set_reset_user(array->context);
2873 array->extent = isl_set_reset_user(array->extent);
2874 if (!array->context || !array->extent)
2875 return pet_array_free(array);
2877 return array;
2880 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2882 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2884 int i;
2885 isl_space *space;
2886 isl_set *domain;
2888 if (!stmt)
2889 return NULL;
2891 stmt->domain = isl_set_reset_user(stmt->domain);
2892 if (!stmt->domain)
2893 return pet_stmt_free(stmt);
2895 for (i = 0; i < stmt->n_arg; ++i) {
2896 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2897 if (!stmt->args[i])
2898 return pet_stmt_free(stmt);
2901 stmt->body = pet_tree_anonymize(stmt->body);
2902 if (!stmt->body)
2903 return pet_stmt_free(stmt);
2905 return stmt;
2908 /* Reset the user pointer on the tuple ids and all parameter ids
2909 * in "implication".
2911 static struct pet_implication *implication_anonymize(
2912 struct pet_implication *implication)
2914 if (!implication)
2915 return NULL;
2917 implication->extension = isl_map_reset_user(implication->extension);
2918 if (!implication->extension)
2919 return pet_implication_free(implication);
2921 return implication;
2924 /* Reset the user pointer on the tuple ids and all parameter ids
2925 * in "independence".
2927 static struct pet_independence *independence_anonymize(
2928 struct pet_independence *independence)
2930 if (!independence)
2931 return NULL;
2933 independence->filter = isl_union_map_reset_user(independence->filter);
2934 independence->local = isl_union_set_reset_user(independence->local);
2935 if (!independence->filter || !independence->local)
2936 return pet_independence_free(independence);
2938 return independence;
2941 /* Reset the user pointer on all parameter and tuple ids in "scop".
2943 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2945 int i;
2947 if (!scop)
2948 return NULL;
2950 scop->context = isl_set_reset_user(scop->context);
2951 scop->context_value = isl_set_reset_user(scop->context_value);
2952 scop->schedule = isl_schedule_reset_user(scop->schedule);
2953 if (!scop->context || !scop->context_value || !scop->schedule)
2954 return pet_scop_free(scop);
2956 for (i = 0; i < scop->n_array; ++i) {
2957 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2958 if (!scop->arrays[i])
2959 return pet_scop_free(scop);
2962 for (i = 0; i < scop->n_stmt; ++i) {
2963 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2964 if (!scop->stmts[i])
2965 return pet_scop_free(scop);
2968 for (i = 0; i < scop->n_implication; ++i) {
2969 scop->implications[i] =
2970 implication_anonymize(scop->implications[i]);
2971 if (!scop->implications[i])
2972 return pet_scop_free(scop);
2975 for (i = 0; i < scop->n_independence; ++i) {
2976 scop->independences[i] =
2977 independence_anonymize(scop->independences[i]);
2978 if (!scop->independences[i])
2979 return pet_scop_free(scop);
2982 return scop;
2985 /* Compute the gist of the iteration domain and all access relations
2986 * of "stmt" based on the constraints on the parameters specified by "context"
2987 * and the constraints on the values of nested accesses specified
2988 * by "value_bounds".
2990 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2991 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2993 int i;
2994 isl_set *domain;
2996 if (!stmt)
2997 return NULL;
2999 domain = isl_set_copy(stmt->domain);
3000 if (stmt->n_arg > 0)
3001 domain = isl_map_domain(isl_set_unwrap(domain));
3003 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3005 for (i = 0; i < stmt->n_arg; ++i) {
3006 stmt->args[i] = pet_expr_gist(stmt->args[i],
3007 domain, value_bounds);
3008 if (!stmt->args[i])
3009 goto error;
3012 stmt->body = pet_tree_gist(stmt->body, domain, value_bounds);
3013 if (!stmt->body)
3014 goto error;
3016 isl_set_free(domain);
3018 domain = isl_set_universe(pet_stmt_get_space(stmt));
3019 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3020 if (stmt->n_arg > 0)
3021 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3022 value_bounds);
3023 stmt->domain = isl_set_gist(stmt->domain, domain);
3024 if (!stmt->domain)
3025 return pet_stmt_free(stmt);
3027 return stmt;
3028 error:
3029 isl_set_free(domain);
3030 return pet_stmt_free(stmt);
3033 /* Compute the gist of the extent of the array
3034 * based on the constraints on the parameters specified by "context".
3036 static struct pet_array *array_gist(struct pet_array *array,
3037 __isl_keep isl_set *context)
3039 if (!array)
3040 return NULL;
3042 array->extent = isl_set_gist_params(array->extent,
3043 isl_set_copy(context));
3044 if (!array->extent)
3045 return pet_array_free(array);
3047 return array;
3050 /* Compute the gist of all sets and relations in "scop"
3051 * based on the constraints on the parameters specified by "scop->context"
3052 * and the constraints on the values of nested accesses specified
3053 * by "value_bounds".
3055 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3056 __isl_keep isl_union_map *value_bounds)
3058 int i;
3060 if (!scop)
3061 return NULL;
3063 scop->context = isl_set_coalesce(scop->context);
3064 if (!scop->context)
3065 return pet_scop_free(scop);
3067 scop->schedule = isl_schedule_gist_domain_params(scop->schedule,
3068 isl_set_copy(scop->context));
3069 if (!scop->schedule)
3070 return pet_scop_free(scop);
3072 for (i = 0; i < scop->n_array; ++i) {
3073 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3074 if (!scop->arrays[i])
3075 return pet_scop_free(scop);
3078 for (i = 0; i < scop->n_stmt; ++i) {
3079 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3080 value_bounds);
3081 if (!scop->stmts[i])
3082 return pet_scop_free(scop);
3085 return scop;
3088 /* Intersect the context of "scop" with "context".
3089 * To ensure that we don't introduce any unnamed parameters in
3090 * the context of "scop", we first remove the unnamed parameters
3091 * from "context".
3093 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3094 __isl_take isl_set *context)
3096 if (!scop)
3097 goto error;
3099 context = pet_nested_remove_from_set(context);
3100 scop->context = isl_set_intersect(scop->context, context);
3101 if (!scop->context)
3102 return pet_scop_free(scop);
3104 return scop;
3105 error:
3106 isl_set_free(context);
3107 return pet_scop_free(scop);
3110 /* Drop the current context of "scop". That is, replace the context
3111 * by a universal set.
3113 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3115 isl_space *space;
3117 if (!scop)
3118 return NULL;
3120 space = isl_set_get_space(scop->context);
3121 isl_set_free(scop->context);
3122 scop->context = isl_set_universe(space);
3123 if (!scop->context)
3124 return pet_scop_free(scop);
3126 return scop;
3129 /* Append "array" to the arrays of "scop".
3131 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3132 struct pet_array *array)
3134 isl_ctx *ctx;
3135 struct pet_array **arrays;
3137 if (!array || !scop)
3138 goto error;
3140 ctx = isl_set_get_ctx(scop->context);
3141 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3142 scop->n_array + 1);
3143 if (!arrays)
3144 goto error;
3145 scop->arrays = arrays;
3146 scop->arrays[scop->n_array] = array;
3147 scop->n_array++;
3148 scop->context = isl_set_intersect_params(scop->context,
3149 isl_set_copy(array->context));
3150 if (!scop->context)
3151 return pet_scop_free(scop);
3153 return scop;
3154 error:
3155 pet_array_free(array);
3156 return pet_scop_free(scop);
3159 /* Create an index expression for an access to a virtual array
3160 * representing the result of a condition.
3161 * Unlike other accessed data, the id of the array is NULL as
3162 * there is no ValueDecl in the program corresponding to the virtual
3163 * array.
3164 * The index expression is created as an identity mapping on "space".
3165 * That is, the dimension of the array is the same as that of "space".
3167 __isl_give isl_multi_pw_aff *pet_create_test_index(__isl_take isl_space *space,
3168 int test_nr)
3170 isl_id *id;
3171 char name[50];
3173 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3174 id = isl_id_alloc(isl_space_get_ctx(space), name, NULL);
3175 space = isl_space_map_from_set(space);
3176 space = isl_space_set_tuple_id(space, isl_dim_out, id);
3177 return isl_multi_pw_aff_identity(space);
3180 /* Add an array with the given extent to the list
3181 * of arrays in "scop" and return the extended pet_scop.
3182 * Specifically, the extent is determined by the image of "domain"
3183 * under "index".
3184 * "int_size" is the number of bytes needed to represent values of type "int".
3185 * The array is marked as attaining values 0 and 1 only and
3186 * as each element being assigned at most once.
3188 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3189 __isl_take isl_set *domain, __isl_take isl_multi_pw_aff *index,
3190 int int_size)
3192 isl_ctx *ctx;
3193 isl_space *space;
3194 struct pet_array *array;
3195 isl_map *access;
3197 if (!scop || !domain || !index)
3198 goto error;
3200 ctx = isl_multi_pw_aff_get_ctx(index);
3201 array = isl_calloc_type(ctx, struct pet_array);
3202 if (!array)
3203 goto error;
3205 access = isl_map_from_multi_pw_aff(index);
3206 access = isl_map_intersect_domain(access, domain);
3207 array->extent = isl_map_range(access);
3208 space = isl_space_params_alloc(ctx, 0);
3209 array->context = isl_set_universe(space);
3210 space = isl_space_set_alloc(ctx, 0, 1);
3211 array->value_bounds = isl_set_universe(space);
3212 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3213 isl_dim_set, 0, 0);
3214 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3215 isl_dim_set, 0, 1);
3216 array->element_type = strdup("int");
3217 array->element_size = int_size;
3218 array->uniquely_defined = 1;
3220 if (!array->extent || !array->context)
3221 array = pet_array_free(array);
3223 scop = pet_scop_add_array(scop, array);
3225 return scop;
3226 error:
3227 isl_set_free(domain);
3228 isl_multi_pw_aff_free(index);
3229 return pet_scop_free(scop);
3232 /* Create and return an implication on filter values equal to "satisfied"
3233 * with extension "map".
3235 static struct pet_implication *new_implication(__isl_take isl_map *map,
3236 int satisfied)
3238 isl_ctx *ctx;
3239 struct pet_implication *implication;
3241 if (!map)
3242 return NULL;
3243 ctx = isl_map_get_ctx(map);
3244 implication = isl_alloc_type(ctx, struct pet_implication);
3245 if (!implication)
3246 goto error;
3248 implication->extension = map;
3249 implication->satisfied = satisfied;
3251 return implication;
3252 error:
3253 isl_map_free(map);
3254 return NULL;
3257 /* Add an implication on filter values equal to "satisfied"
3258 * with extension "map" to "scop".
3260 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3261 __isl_take isl_map *map, int satisfied)
3263 isl_ctx *ctx;
3264 struct pet_implication *implication;
3265 struct pet_implication **implications;
3267 implication = new_implication(map, satisfied);
3268 if (!scop || !implication)
3269 goto error;
3271 ctx = isl_set_get_ctx(scop->context);
3272 implications = isl_realloc_array(ctx, scop->implications,
3273 struct pet_implication *,
3274 scop->n_implication + 1);
3275 if (!implications)
3276 goto error;
3277 scop->implications = implications;
3278 scop->implications[scop->n_implication] = implication;
3279 scop->n_implication++;
3281 return scop;
3282 error:
3283 pet_implication_free(implication);
3284 return pet_scop_free(scop);
3287 /* Create and return a function that maps the iteration domains
3288 * of the statements in "scop" onto their outer "n" dimensions.
3289 * "space" is the parameters space of the created function.
3291 static __isl_give isl_union_pw_multi_aff *outer_projection(
3292 struct pet_scop *scop, __isl_take isl_space *space, int n)
3294 int i;
3295 isl_union_pw_multi_aff *res;
3297 res = isl_union_pw_multi_aff_empty(space);
3299 if (!scop)
3300 return isl_union_pw_multi_aff_free(res);
3302 for (i = 0; i < scop->n_stmt; ++i) {
3303 struct pet_stmt *stmt = scop->stmts[i];
3304 isl_space *space;
3305 isl_multi_aff *ma;
3306 isl_pw_multi_aff *pma;
3308 space = pet_stmt_get_space(stmt);
3309 ma = pet_prefix_projection(space, n);
3310 pma = isl_pw_multi_aff_from_multi_aff(ma);
3311 res = isl_union_pw_multi_aff_add_pw_multi_aff(res, pma);
3314 return res;
3317 /* Add an independence to "scop" for the inner iterator of "domain"
3318 * with local variables "local", where "domain" represents the outer
3319 * loop iterators of all statements in "scop".
3320 * If "sign" is positive, then the inner iterator increases.
3321 * Otherwise it decreases.
3323 * The independence is supposed to filter out any dependence of
3324 * an iteration of domain on a previous iteration along the inner dimension.
3325 * We therefore create a mapping from an iteration to later iterations and
3326 * then plug in the projection of the iterations domains of "scop"
3327 * onto the outer loop iterators.
3329 struct pet_scop *pet_scop_set_independent(struct pet_scop *scop,
3330 __isl_keep isl_set *domain, __isl_take isl_union_set *local, int sign)
3332 int i, dim;
3333 isl_space *space;
3334 isl_map *map;
3335 isl_union_map *independence;
3336 isl_union_pw_multi_aff *proj;
3338 if (!scop || !domain || !local)
3339 goto error;
3341 dim = isl_set_dim(domain, isl_dim_set);
3342 space = isl_space_map_from_set(isl_set_get_space(domain));
3343 map = isl_map_universe(space);
3344 for (i = 0; i + 1 < dim; ++i)
3345 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
3346 if (sign > 0)
3347 map = isl_map_order_lt(map,
3348 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3349 else
3350 map = isl_map_order_gt(map,
3351 isl_dim_in, dim - 1, isl_dim_out, dim - 1);
3353 independence = isl_union_map_from_map(map);
3354 space = isl_space_params(isl_set_get_space(domain));
3355 proj = outer_projection(scop, space, dim);
3356 independence = isl_union_map_preimage_domain_union_pw_multi_aff(
3357 independence, isl_union_pw_multi_aff_copy(proj));
3358 independence = isl_union_map_preimage_range_union_pw_multi_aff(
3359 independence, proj);
3361 scop = pet_scop_add_independence(scop, independence, local);
3363 return scop;
3364 error:
3365 isl_union_set_free(local);
3366 return pet_scop_free(scop);
3369 /* Given an access expression, check if it is data dependent.
3370 * If so, set *found and abort the search.
3372 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3374 int *found = user;
3376 if (pet_expr_get_n_arg(expr) > 0) {
3377 *found = 1;
3378 return -1;
3381 return 0;
3384 /* Does "scop" contain any data dependent accesses?
3386 * Check the body of each statement for such accesses.
3388 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3390 int i;
3391 int found = 0;
3393 if (!scop)
3394 return -1;
3396 for (i = 0; i < scop->n_stmt; ++i) {
3397 int r = pet_tree_foreach_access_expr(scop->stmts[i]->body,
3398 &is_data_dependent, &found);
3399 if (r < 0 && !found)
3400 return -1;
3401 if (found)
3402 return found;
3405 return found;
3408 /* Does "scop" contain and data dependent conditions?
3410 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3412 int i;
3414 if (!scop)
3415 return -1;
3417 for (i = 0; i < scop->n_stmt; ++i)
3418 if (scop->stmts[i]->n_arg > 0)
3419 return 1;
3421 return 0;
3424 /* Keep track of the "input" file inside the (extended) "scop".
3426 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3428 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3430 if (!scop)
3431 return NULL;
3433 ext->input = input;
3435 return scop;
3438 /* Print the original code corresponding to "scop" to printer "p".
3440 * pet_scop_print_original can only be called from
3441 * a pet_transform_C_source callback. This means that the input
3442 * file is stored in the extended scop and that the printer prints
3443 * to a file.
3445 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3446 __isl_take isl_printer *p)
3448 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3449 FILE *output;
3450 unsigned start, end;
3452 if (!scop || !p)
3453 return isl_printer_free(p);
3455 if (!ext->input)
3456 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3457 "no input file stored in scop",
3458 return isl_printer_free(p));
3460 output = isl_printer_get_file(p);
3461 if (!output)
3462 return isl_printer_free(p);
3464 start = pet_loc_get_start(scop->loc);
3465 end = pet_loc_get_end(scop->loc);
3466 if (copy(ext->input, output, start, end) < 0)
3467 return isl_printer_free(p);
3469 return p;