pet_stmt_from_pet_expr: minor clean-up
[pet.git] / scop.c
bloba8b9b9ba14e9323163e10d33022f314480e2a6d5
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "aff.h"
40 #include "expr.h"
41 #include "filter.h"
42 #include "loc.h"
43 #include "nest.h"
44 #include "scop.h"
45 #include "print.h"
46 #include "value_bounds.h"
48 /* pet_scop with extra information that is used during parsing and printing.
50 * In particular, we keep track of conditions under which we want
51 * to skip the rest of the current loop iteration (skip[pet_skip_now])
52 * and of conditions under which we want to skip subsequent
53 * loop iterations (skip[pet_skip_later]).
55 * The conditions are represented as index expressions defined
56 * over a zero-dimensional domain. The index expression is either
57 * a boolean affine expression or an access to a variable, which
58 * is assumed to attain values zero and one. The condition holds
59 * if the variable has value one or if the affine expression
60 * has value one (typically for only part of the parameter space).
62 * A missing condition (skip[type] == NULL) means that we don't want
63 * to skip anything.
65 * Additionally, we keep track of the original input file
66 * inside pet_transform_C_source.
68 struct pet_scop_ext {
69 struct pet_scop scop;
71 isl_multi_pw_aff *skip[2];
72 FILE *input;
75 /* Construct a pet_stmt with given location and statement
76 * number from a pet_expr.
77 * The initial iteration domain is the zero-dimensional universe.
78 * The name of the domain is given by "label" if it is non-NULL.
79 * Otherwise, the name is constructed as S_<id>.
80 * The domains of all access relations are modified to refer
81 * to the statement iteration domain.
83 struct pet_stmt *pet_stmt_from_pet_expr(__isl_take pet_loc *loc,
84 __isl_take isl_id *label, int id, __isl_take pet_expr *expr)
86 struct pet_stmt *stmt;
87 isl_ctx *ctx;
88 isl_space *space;
89 isl_set *dom;
90 isl_map *sched;
91 isl_multi_pw_aff *add_name;
92 char name[50];
94 if (!loc || !expr)
95 goto error;
97 ctx = pet_expr_get_ctx(expr);
98 stmt = isl_calloc_type(ctx, struct pet_stmt);
99 if (!stmt)
100 goto error;
102 space = isl_space_set_alloc(ctx, 0, 0);
103 if (!label) {
104 snprintf(name, sizeof(name), "S_%d", id);
105 label = isl_id_alloc(ctx, name, NULL);
107 space = isl_space_set_tuple_id(space, isl_dim_set, label);
108 dom = isl_set_universe(isl_space_copy(space));
109 sched = isl_map_from_domain(isl_set_copy(dom));
111 space = isl_space_from_domain(space);
112 add_name = isl_multi_pw_aff_zero(space);
113 expr = pet_expr_update_domain(expr, add_name);
115 stmt->loc = loc;
116 stmt->domain = dom;
117 stmt->schedule = sched;
118 stmt->body = expr;
120 if (!stmt->domain || !stmt->schedule || !stmt->body)
121 return pet_stmt_free(stmt);
123 return stmt;
124 error:
125 isl_id_free(label);
126 pet_loc_free(loc);
127 pet_expr_free(expr);
128 return NULL;
131 void *pet_stmt_free(struct pet_stmt *stmt)
133 int i;
135 if (!stmt)
136 return NULL;
138 pet_loc_free(stmt->loc);
139 isl_set_free(stmt->domain);
140 isl_map_free(stmt->schedule);
141 pet_expr_free(stmt->body);
143 for (i = 0; i < stmt->n_arg; ++i)
144 pet_expr_free(stmt->args[i]);
145 free(stmt->args);
147 free(stmt);
148 return NULL;
151 /* Return the iteration space of "stmt".
153 * If the statement has arguments, then stmt->domain is a wrapped map
154 * mapping the iteration domain to the values of the arguments
155 * for which this statement is executed.
156 * In this case, we need to extract the domain space of this wrapped map.
158 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
160 isl_space *space;
162 if (!stmt)
163 return NULL;
165 space = isl_set_get_space(stmt->domain);
166 if (isl_space_is_wrapping(space))
167 space = isl_space_domain(isl_space_unwrap(space));
169 return space;
172 static void stmt_dump(struct pet_stmt *stmt, int indent)
174 int i;
176 if (!stmt)
177 return;
179 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
180 fprintf(stderr, "%*s", indent, "");
181 isl_set_dump(stmt->domain);
182 fprintf(stderr, "%*s", indent, "");
183 isl_map_dump(stmt->schedule);
184 pet_expr_dump_with_indent(stmt->body, indent);
185 for (i = 0; i < stmt->n_arg; ++i)
186 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
189 void pet_stmt_dump(struct pet_stmt *stmt)
191 stmt_dump(stmt, 0);
194 /* Allocate a new pet_type with the given "name" and "definition".
196 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
197 const char *definition)
199 struct pet_type *type;
201 type = isl_alloc_type(ctx, struct pet_type);
202 if (!type)
203 return NULL;
205 type->name = strdup(name);
206 type->definition = strdup(definition);
208 if (!type->name || !type->definition)
209 return pet_type_free(type);
211 return type;
214 /* Free "type" and return NULL.
216 struct pet_type *pet_type_free(struct pet_type *type)
218 if (!type)
219 return NULL;
221 free(type->name);
222 free(type->definition);
224 free(type);
225 return NULL;
228 struct pet_array *pet_array_free(struct pet_array *array)
230 if (!array)
231 return NULL;
233 isl_set_free(array->context);
234 isl_set_free(array->extent);
235 isl_set_free(array->value_bounds);
236 free(array->element_type);
238 free(array);
239 return NULL;
242 void pet_array_dump(struct pet_array *array)
244 if (!array)
245 return;
247 isl_set_dump(array->context);
248 isl_set_dump(array->extent);
249 isl_set_dump(array->value_bounds);
250 fprintf(stderr, "%s%s%s\n", array->element_type,
251 array->element_is_record ? " element-is-record" : "",
252 array->live_out ? " live-out" : "");
255 /* Alloc a pet_scop structure, with extra room for information that
256 * is only used during parsing.
258 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
260 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
263 /* Construct a pet_scop with room for n statements.
265 * Since no information on the location is known at this point,
266 * scop->loc is initialized with pet_loc_dummy.
268 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
270 isl_space *space;
271 struct pet_scop *scop;
273 scop = pet_scop_alloc(ctx);
274 if (!scop)
275 return NULL;
277 space = isl_space_params_alloc(ctx, 0);
278 scop->context = isl_set_universe(isl_space_copy(space));
279 scop->context_value = isl_set_universe(space);
280 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
281 if (!scop->context || !scop->stmts)
282 return pet_scop_free(scop);
284 scop->loc = &pet_loc_dummy;
285 scop->n_stmt = n;
287 return scop;
290 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
292 return scop_alloc(ctx, 0);
295 /* Update "context" with respect to the valid parameter values for "access".
297 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
298 __isl_take isl_set *context)
300 context = isl_set_intersect(context,
301 isl_map_params(isl_map_copy(access)));
302 return context;
305 /* Update "context" with respect to the valid parameter values for "expr".
307 * If "expr" represents a conditional operator, then a parameter value
308 * needs to be valid for the condition and for at least one of the
309 * remaining two arguments.
310 * If the condition is an affine expression, then we can be a bit more specific.
311 * The parameter then has to be valid for the second argument for
312 * non-zero accesses and valid for the third argument for zero accesses.
314 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
315 __isl_take isl_set *context)
317 int i;
319 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
320 int is_aff;
321 isl_set *context1, *context2;
323 is_aff = pet_expr_is_affine(expr->args[0]);
324 if (is_aff < 0)
325 goto error;
327 context = expr_extract_context(expr->args[0], context);
328 context1 = expr_extract_context(expr->args[1],
329 isl_set_copy(context));
330 context2 = expr_extract_context(expr->args[2], context);
332 if (is_aff) {
333 isl_map *access;
334 isl_set *zero_set;
336 access = isl_map_copy(expr->args[0]->acc.access);
337 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
338 zero_set = isl_map_params(access);
339 context1 = isl_set_subtract(context1,
340 isl_set_copy(zero_set));
341 context2 = isl_set_intersect(context2, zero_set);
344 context = isl_set_union(context1, context2);
345 context = isl_set_coalesce(context);
347 return context;
350 for (i = 0; i < expr->n_arg; ++i)
351 context = expr_extract_context(expr->args[i], context);
353 if (expr->type == pet_expr_access)
354 context = access_extract_context(expr->acc.access, context);
356 return context;
357 error:
358 isl_set_free(context);
359 return NULL;
362 /* Update "context" with respect to the valid parameter values for "stmt".
364 * If the statement is an assume statement with an affine expression,
365 * then intersect "context" with that expression.
366 * Otherwise, intersect "context" with the contexts of the expressions
367 * inside "stmt".
369 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
370 __isl_take isl_set *context)
372 int i;
374 if (pet_stmt_is_assume(stmt) &&
375 pet_expr_is_affine(stmt->body->args[0])) {
376 isl_multi_pw_aff *index;
377 isl_pw_aff *pa;
378 isl_set *cond;
380 index = stmt->body->args[0]->acc.index;
381 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
382 cond = isl_set_params(isl_pw_aff_non_zero_set(pa));
383 return isl_set_intersect(context, cond);
386 for (i = 0; i < stmt->n_arg; ++i)
387 context = expr_extract_context(stmt->args[i], context);
389 context = expr_extract_context(stmt->body, context);
391 return context;
394 /* Construct a pet_scop that contains the given pet_stmt.
396 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
398 struct pet_scop *scop;
400 if (!stmt)
401 return NULL;
403 scop = scop_alloc(ctx, 1);
404 if (!scop)
405 goto error;
407 scop->context = stmt_extract_context(stmt, scop->context);
408 if (!scop->context)
409 goto error;
411 scop->stmts[0] = stmt;
412 scop->loc = pet_loc_copy(stmt->loc);
414 if (!scop->loc)
415 return pet_scop_free(scop);
417 return scop;
418 error:
419 pet_stmt_free(stmt);
420 pet_scop_free(scop);
421 return NULL;
424 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
425 * does it represent an affine expression?
427 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
429 int has_id;
431 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
432 if (has_id < 0)
433 return -1;
435 return !has_id;
438 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
440 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
441 __isl_take isl_set *dom)
443 isl_pw_aff *pa;
444 pa = isl_set_indicator_function(set);
445 pa = isl_pw_aff_intersect_domain(pa, dom);
446 return pa;
449 /* Return "lhs || rhs", defined on the shared definition domain.
451 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
452 __isl_take isl_pw_aff *rhs)
454 isl_set *cond;
455 isl_set *dom;
457 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
458 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
459 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
460 isl_pw_aff_non_zero_set(rhs));
461 cond = isl_set_coalesce(cond);
462 return indicator_function(cond, dom);
465 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
466 * ext may be equal to either ext1 or ext2.
468 * The two skips that need to be combined are assumed to be affine expressions.
470 * We need to skip in ext if we need to skip in either ext1 or ext2.
471 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
473 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
474 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
475 enum pet_skip type)
477 isl_pw_aff *skip, *skip1, *skip2;
479 if (!ext)
480 return NULL;
481 if (!ext1->skip[type] && !ext2->skip[type])
482 return ext;
483 if (!ext1->skip[type]) {
484 if (ext == ext2)
485 return ext;
486 ext->skip[type] = ext2->skip[type];
487 ext2->skip[type] = NULL;
488 return ext;
490 if (!ext2->skip[type]) {
491 if (ext == ext1)
492 return ext;
493 ext->skip[type] = ext1->skip[type];
494 ext1->skip[type] = NULL;
495 return ext;
498 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
499 !multi_pw_aff_is_affine(ext2->skip[type]))
500 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
501 isl_error_internal, "can only combine affine skips",
502 goto error);
504 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
505 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
506 skip = pw_aff_or(skip1, skip2);
507 isl_multi_pw_aff_free(ext1->skip[type]);
508 ext1->skip[type] = NULL;
509 isl_multi_pw_aff_free(ext2->skip[type]);
510 ext2->skip[type] = NULL;
511 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
512 if (!ext->skip[type])
513 goto error;
515 return ext;
516 error:
517 pet_scop_free(&ext->scop);
518 return NULL;
521 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
522 * where type takes on the values pet_skip_now and pet_skip_later.
523 * scop may be equal to either scop1 or scop2.
525 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
526 struct pet_scop *scop1, struct pet_scop *scop2)
528 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
529 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
530 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
532 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
533 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
534 return &ext->scop;
537 /* Update start and end of scop->loc to include the region from "start"
538 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
539 * does not have any offset information yet and we simply take the information
540 * from "start" and "end". Otherwise, we update loc using "start" and "end".
542 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
543 unsigned start, unsigned end)
545 if (!scop)
546 return NULL;
548 if (scop->loc == &pet_loc_dummy)
549 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
550 start, end, -1);
551 else
552 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
554 if (!scop->loc)
555 return pet_scop_free(scop);
557 return scop;
560 /* Update start and end of scop->loc to include the region identified
561 * by "loc".
563 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
564 __isl_keep pet_loc *loc)
566 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
567 pet_loc_get_end(loc));
570 /* Replace the location of "scop" by "loc".
572 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
573 __isl_take pet_loc *loc)
575 if (!scop || !loc)
576 goto error;
578 pet_loc_free(scop->loc);
579 scop->loc = loc;
581 return scop;
582 error:
583 pet_loc_free(loc);
584 pet_scop_free(scop);
585 return NULL;
588 /* Does "implication" appear in the list of implications of "scop"?
590 static int is_known_implication(struct pet_scop *scop,
591 struct pet_implication *implication)
593 int i;
595 for (i = 0; i < scop->n_implication; ++i) {
596 struct pet_implication *pi = scop->implications[i];
597 int equal;
599 if (pi->satisfied != implication->satisfied)
600 continue;
601 equal = isl_map_is_equal(pi->extension, implication->extension);
602 if (equal < 0)
603 return -1;
604 if (equal)
605 return 1;
608 return 0;
611 /* Store the concatenation of the implications of "scop1" and "scop2"
612 * in "scop", removing duplicates (i.e., implications in "scop2" that
613 * already appear in "scop1").
615 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
616 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
618 int i, j;
620 if (!scop)
621 return NULL;
623 if (scop2->n_implication == 0) {
624 scop->n_implication = scop1->n_implication;
625 scop->implications = scop1->implications;
626 scop1->n_implication = 0;
627 scop1->implications = NULL;
628 return scop;
631 if (scop1->n_implication == 0) {
632 scop->n_implication = scop2->n_implication;
633 scop->implications = scop2->implications;
634 scop2->n_implication = 0;
635 scop2->implications = NULL;
636 return scop;
639 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
640 scop1->n_implication + scop2->n_implication);
641 if (!scop->implications)
642 return pet_scop_free(scop);
644 for (i = 0; i < scop1->n_implication; ++i) {
645 scop->implications[i] = scop1->implications[i];
646 scop1->implications[i] = NULL;
649 scop->n_implication = scop1->n_implication;
650 j = scop1->n_implication;
651 for (i = 0; i < scop2->n_implication; ++i) {
652 int known;
654 known = is_known_implication(scop, scop2->implications[i]);
655 if (known < 0)
656 return pet_scop_free(scop);
657 if (known)
658 continue;
659 scop->implications[j++] = scop2->implications[i];
660 scop2->implications[i] = NULL;
662 scop->n_implication = j;
664 return scop;
667 /* Combine the offset information of "scop1" and "scop2" into "scop".
669 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
670 struct pet_scop *scop1, struct pet_scop *scop2)
672 if (scop1->loc != &pet_loc_dummy)
673 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
674 if (scop2->loc != &pet_loc_dummy)
675 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
676 return scop;
679 /* Construct a pet_scop that contains the offset information,
680 * arrays, statements and skip information in "scop1" and "scop2".
682 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
683 struct pet_scop *scop2)
685 int i;
686 struct pet_scop *scop = NULL;
688 if (!scop1 || !scop2)
689 goto error;
691 if (scop1->n_stmt == 0) {
692 scop2 = scop_combine_skips(scop2, scop1, scop2);
693 pet_scop_free(scop1);
694 return scop2;
697 if (scop2->n_stmt == 0) {
698 scop1 = scop_combine_skips(scop1, scop1, scop2);
699 pet_scop_free(scop2);
700 return scop1;
703 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
704 if (!scop)
705 goto error;
707 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
708 scop1->n_array + scop2->n_array);
709 if (!scop->arrays)
710 goto error;
711 scop->n_array = scop1->n_array + scop2->n_array;
713 for (i = 0; i < scop1->n_stmt; ++i) {
714 scop->stmts[i] = scop1->stmts[i];
715 scop1->stmts[i] = NULL;
718 for (i = 0; i < scop2->n_stmt; ++i) {
719 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
720 scop2->stmts[i] = NULL;
723 for (i = 0; i < scop1->n_array; ++i) {
724 scop->arrays[i] = scop1->arrays[i];
725 scop1->arrays[i] = NULL;
728 for (i = 0; i < scop2->n_array; ++i) {
729 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
730 scop2->arrays[i] = NULL;
733 scop = scop_collect_implications(ctx, scop, scop1, scop2);
734 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
735 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
736 scop = scop_combine_skips(scop, scop1, scop2);
737 scop = scop_combine_start_end(scop, scop1, scop2);
739 pet_scop_free(scop1);
740 pet_scop_free(scop2);
741 return scop;
742 error:
743 pet_scop_free(scop1);
744 pet_scop_free(scop2);
745 pet_scop_free(scop);
746 return NULL;
749 /* Apply the skip condition "skip" to "scop".
750 * That is, make sure "scop" is not executed when the condition holds.
752 * If "skip" is an affine expression, we add the conditions under
753 * which the expression is zero to the iteration domains.
754 * Otherwise, we add a filter on the variable attaining the value zero.
756 static struct pet_scop *restrict_skip(struct pet_scop *scop,
757 __isl_take isl_multi_pw_aff *skip)
759 isl_set *zero;
760 isl_pw_aff *pa;
761 int is_aff;
763 if (!scop || !skip)
764 goto error;
766 is_aff = multi_pw_aff_is_affine(skip);
767 if (is_aff < 0)
768 goto error;
770 if (!is_aff)
771 return pet_scop_filter(scop, skip, 0);
773 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
774 isl_multi_pw_aff_free(skip);
775 zero = isl_set_params(isl_pw_aff_zero_set(pa));
776 scop = pet_scop_restrict(scop, zero);
778 return scop;
779 error:
780 isl_multi_pw_aff_free(skip);
781 return pet_scop_free(scop);
784 /* Construct a pet_scop that contains the arrays, statements and
785 * skip information in "scop1" and "scop2", where the two scops
786 * are executed "in sequence". That is, breaks and continues
787 * in scop1 have an effect on scop2.
789 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
790 struct pet_scop *scop2)
792 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
793 scop2 = restrict_skip(scop2,
794 pet_scop_get_skip(scop1, pet_skip_now));
795 return pet_scop_add(ctx, scop1, scop2);
798 /* Construct a pet_scop that contains the arrays, statements and
799 * skip information in "scop1" and "scop2", where the two scops
800 * are executed "in parallel". That is, any break or continue
801 * in scop1 has no effect on scop2.
803 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
804 struct pet_scop *scop2)
806 return pet_scop_add(ctx, scop1, scop2);
809 void *pet_implication_free(struct pet_implication *implication)
811 int i;
813 if (!implication)
814 return NULL;
816 isl_map_free(implication->extension);
818 free(implication);
819 return NULL;
822 struct pet_scop *pet_scop_free(struct pet_scop *scop)
824 int i;
825 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
827 if (!scop)
828 return NULL;
829 pet_loc_free(scop->loc);
830 isl_set_free(scop->context);
831 isl_set_free(scop->context_value);
832 if (scop->types)
833 for (i = 0; i < scop->n_type; ++i)
834 pet_type_free(scop->types[i]);
835 free(scop->types);
836 if (scop->arrays)
837 for (i = 0; i < scop->n_array; ++i)
838 pet_array_free(scop->arrays[i]);
839 free(scop->arrays);
840 if (scop->stmts)
841 for (i = 0; i < scop->n_stmt; ++i)
842 pet_stmt_free(scop->stmts[i]);
843 free(scop->stmts);
844 if (scop->implications)
845 for (i = 0; i < scop->n_implication; ++i)
846 pet_implication_free(scop->implications[i]);
847 free(scop->implications);
848 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
849 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
850 free(scop);
851 return NULL;
854 void pet_type_dump(struct pet_type *type)
856 if (!type)
857 return;
859 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
862 void pet_implication_dump(struct pet_implication *implication)
864 if (!implication)
865 return;
867 fprintf(stderr, "%d\n", implication->satisfied);
868 isl_map_dump(implication->extension);
871 void pet_scop_dump(struct pet_scop *scop)
873 int i;
874 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
876 if (!scop)
877 return;
879 isl_set_dump(scop->context);
880 isl_set_dump(scop->context_value);
881 for (i = 0; i < scop->n_type; ++i)
882 pet_type_dump(scop->types[i]);
883 for (i = 0; i < scop->n_array; ++i)
884 pet_array_dump(scop->arrays[i]);
885 for (i = 0; i < scop->n_stmt; ++i)
886 pet_stmt_dump(scop->stmts[i]);
887 for (i = 0; i < scop->n_implication; ++i)
888 pet_implication_dump(scop->implications[i]);
890 if (ext->skip[0]) {
891 fprintf(stderr, "skip\n");
892 isl_multi_pw_aff_dump(ext->skip[0]);
893 isl_multi_pw_aff_dump(ext->skip[1]);
897 /* Return 1 if the two pet_arrays are equivalent.
899 * We don't compare element_size as this may be target dependent.
901 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
903 if (!array1 || !array2)
904 return 0;
906 if (!isl_set_is_equal(array1->context, array2->context))
907 return 0;
908 if (!isl_set_is_equal(array1->extent, array2->extent))
909 return 0;
910 if (!!array1->value_bounds != !!array2->value_bounds)
911 return 0;
912 if (array1->value_bounds &&
913 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
914 return 0;
915 if (strcmp(array1->element_type, array2->element_type))
916 return 0;
917 if (array1->element_is_record != array2->element_is_record)
918 return 0;
919 if (array1->live_out != array2->live_out)
920 return 0;
921 if (array1->uniquely_defined != array2->uniquely_defined)
922 return 0;
923 if (array1->declared != array2->declared)
924 return 0;
925 if (array1->exposed != array2->exposed)
926 return 0;
928 return 1;
931 /* Return 1 if the two pet_stmts are equivalent.
933 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
935 int i;
937 if (!stmt1 || !stmt2)
938 return 0;
940 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
941 return 0;
942 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
943 return 0;
944 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
945 return 0;
946 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
947 return 0;
948 if (stmt1->n_arg != stmt2->n_arg)
949 return 0;
950 for (i = 0; i < stmt1->n_arg; ++i) {
951 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
952 return 0;
955 return 1;
958 /* Return 1 if the two pet_types are equivalent.
960 * We only compare the names of the types since the exact representation
961 * of the definition may depend on the version of clang being used.
963 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
965 if (!type1 || !type2)
966 return 0;
968 if (strcmp(type1->name, type2->name))
969 return 0;
971 return 1;
974 /* Return 1 if the two pet_implications are equivalent.
976 int pet_implication_is_equal(struct pet_implication *implication1,
977 struct pet_implication *implication2)
979 if (!implication1 || !implication2)
980 return 0;
982 if (implication1->satisfied != implication2->satisfied)
983 return 0;
984 if (!isl_map_is_equal(implication1->extension, implication2->extension))
985 return 0;
987 return 1;
990 /* Return 1 if the two pet_scops are equivalent.
992 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
994 int i;
996 if (!scop1 || !scop2)
997 return 0;
999 if (!isl_set_is_equal(scop1->context, scop2->context))
1000 return 0;
1001 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1002 return 0;
1004 if (scop1->n_type != scop2->n_type)
1005 return 0;
1006 for (i = 0; i < scop1->n_type; ++i)
1007 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1008 return 0;
1010 if (scop1->n_array != scop2->n_array)
1011 return 0;
1012 for (i = 0; i < scop1->n_array; ++i)
1013 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1014 return 0;
1016 if (scop1->n_stmt != scop2->n_stmt)
1017 return 0;
1018 for (i = 0; i < scop1->n_stmt; ++i)
1019 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1020 return 0;
1022 if (scop1->n_implication != scop2->n_implication)
1023 return 0;
1024 for (i = 0; i < scop1->n_implication; ++i)
1025 if (!pet_implication_is_equal(scop1->implications[i],
1026 scop2->implications[i]))
1027 return 0;
1029 return 1;
1032 /* Does the set "extent" reference a virtual array, i.e.,
1033 * one with user pointer equal to NULL?
1034 * A virtual array does not have any members.
1036 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1038 isl_id *id;
1039 int is_virtual;
1041 if (!isl_set_has_tuple_id(extent))
1042 return 0;
1043 if (isl_set_is_wrapping(extent))
1044 return 0;
1045 id = isl_set_get_tuple_id(extent);
1046 is_virtual = !isl_id_get_user(id);
1047 isl_id_free(id);
1049 return is_virtual;
1052 /* Intersect the initial dimensions of "array" with "domain", provided
1053 * that "array" represents a virtual array.
1055 * If "array" is virtual, then We take the preimage of "domain"
1056 * over the projection of the extent of "array" onto its initial dimensions
1057 * and intersect this extent with the result.
1059 static struct pet_array *virtual_array_intersect_domain_prefix(
1060 struct pet_array *array, __isl_take isl_set *domain)
1062 int n;
1063 isl_space *space;
1064 isl_multi_aff *ma;
1066 if (!array || !extent_is_virtual_array(array->extent)) {
1067 isl_set_free(domain);
1068 return array;
1071 space = isl_set_get_space(array->extent);
1072 n = isl_set_dim(domain, isl_dim_set);
1073 ma = pet_prefix_projection(space, n);
1074 domain = isl_set_preimage_multi_aff(domain, ma);
1076 array->extent = isl_set_intersect(array->extent, domain);
1077 if (!array->extent)
1078 return pet_array_free(array);
1080 return array;
1083 /* Intersect the initial dimensions of the domain of "stmt"
1084 * with "domain".
1086 * We take the preimage of "domain" over the projection of the
1087 * domain of "stmt" onto its initial dimensions and intersect
1088 * the domain of "stmt" with the result.
1090 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1091 __isl_take isl_set *domain)
1093 int n;
1094 isl_space *space;
1095 isl_multi_aff *ma;
1097 if (!stmt)
1098 goto error;
1100 space = isl_set_get_space(stmt->domain);
1101 n = isl_set_dim(domain, isl_dim_set);
1102 ma = pet_prefix_projection(space, n);
1103 domain = isl_set_preimage_multi_aff(domain, ma);
1105 stmt->domain = isl_set_intersect(stmt->domain, domain);
1106 if (!stmt->domain)
1107 return pet_stmt_free(stmt);
1109 return stmt;
1110 error:
1111 isl_set_free(domain);
1112 return pet_stmt_free(stmt);
1115 /* Intersect the initial dimensions of the domain of "implication"
1116 * with "domain".
1118 * We take the preimage of "domain" over the projection of the
1119 * domain of "implication" onto its initial dimensions and intersect
1120 * the domain of "implication" with the result.
1122 static struct pet_implication *implication_intersect_domain_prefix(
1123 struct pet_implication *implication, __isl_take isl_set *domain)
1125 int n;
1126 isl_space *space;
1127 isl_multi_aff *ma;
1129 if (!implication)
1130 goto error;
1132 space = isl_map_get_space(implication->extension);
1133 n = isl_set_dim(domain, isl_dim_set);
1134 ma = pet_prefix_projection(isl_space_domain(space), n);
1135 domain = isl_set_preimage_multi_aff(domain, ma);
1137 implication->extension =
1138 isl_map_intersect_domain(implication->extension, domain);
1139 if (!implication->extension)
1140 return pet_implication_free(implication);
1142 return implication;
1143 error:
1144 isl_set_free(domain);
1145 return pet_implication_free(implication);
1148 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1150 * The extents of the virtual arrays match the iteration domains,
1151 * so if the iteration domain changes, we need to change those extents too.
1153 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1154 __isl_take isl_set *domain)
1156 int i;
1158 if (!scop)
1159 goto error;
1161 for (i = 0; i < scop->n_array; ++i) {
1162 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1163 scop->arrays[i], isl_set_copy(domain));
1164 if (!scop->arrays[i])
1165 goto error;
1168 for (i = 0; i < scop->n_stmt; ++i) {
1169 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1170 isl_set_copy(domain));
1171 if (!scop->stmts[i])
1172 goto error;
1175 for (i = 0; i < scop->n_implication; ++i) {
1176 scop->implications[i] =
1177 implication_intersect_domain_prefix(scop->implications[i],
1178 isl_set_copy(domain));
1179 if (!scop->implications[i])
1180 return pet_scop_free(scop);
1183 isl_set_free(domain);
1184 return scop;
1185 error:
1186 isl_set_free(domain);
1187 return pet_scop_free(scop);
1190 /* Prefix the schedule of "stmt" with an extra dimension with constant
1191 * value "pos".
1193 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1195 if (!stmt)
1196 return NULL;
1198 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1199 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1200 if (!stmt->schedule)
1201 return pet_stmt_free(stmt);
1203 return stmt;
1206 /* Prefix the schedules of all statements in "scop" with an extra
1207 * dimension with constant value "pos".
1209 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1211 int i;
1213 if (!scop)
1214 return NULL;
1216 for (i = 0; i < scop->n_stmt; ++i) {
1217 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1218 if (!scop->stmts[i])
1219 return pet_scop_free(scop);
1222 return scop;
1225 /* Given a set with a parameter at "param_pos" that refers to the
1226 * iterator, "move" the iterator to the first set dimension.
1227 * That is, essentially equate the parameter to the first set dimension
1228 * and then project it out.
1230 * The first set dimension may however refer to a virtual iterator,
1231 * while the parameter refers to the "real" iterator.
1232 * We therefore need to take into account the affine expression "iv_map", which
1233 * expresses the real iterator in terms of the virtual iterator.
1234 * In particular, we equate the set dimension to the input of the map
1235 * and the parameter to the output of the map and then project out
1236 * everything we don't need anymore.
1238 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1239 int param_pos, __isl_take isl_aff *iv_map)
1241 isl_map *map, *map2;
1242 map = isl_map_from_domain(set);
1243 map = isl_map_add_dims(map, isl_dim_out, 1);
1244 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1245 map2 = isl_map_from_aff(iv_map);
1246 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1247 map = isl_map_apply_range(map, map2);
1248 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1249 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1250 return isl_map_domain(map);
1253 /* Data used in embed_access.
1254 * extend adds an iterator to the iteration domain (through precomposition).
1255 * iv_map expresses the real iterator in terms of the virtual iterator
1256 * var_id represents the induction variable of the corresponding loop
1258 struct pet_embed_access {
1259 isl_multi_pw_aff *extend;
1260 isl_aff *iv_map;
1261 isl_id *var_id;
1264 /* Given an index expression, return an expression for the outer iterator.
1266 static __isl_give isl_aff *index_outer_iterator(
1267 __isl_take isl_multi_pw_aff *index)
1269 isl_space *space;
1270 isl_local_space *ls;
1272 space = isl_multi_pw_aff_get_domain_space(index);
1273 isl_multi_pw_aff_free(index);
1275 ls = isl_local_space_from_space(space);
1276 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1279 /* Replace an index expression that references the new (outer) iterator variable
1280 * by one that references the corresponding (real) iterator.
1282 * The input index expression is of the form
1284 * { S[i',...] -> i[] }
1286 * where i' refers to the virtual iterator.
1288 * iv_map is of the form
1290 * { [i'] -> [i] }
1292 * Return the index expression
1294 * { S[i',...] -> [i] }
1296 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1297 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1299 isl_space *space;
1300 isl_aff *aff;
1302 aff = index_outer_iterator(index);
1303 space = isl_aff_get_space(aff);
1304 iv_map = isl_aff_align_params(iv_map, space);
1305 aff = isl_aff_pullback_aff(iv_map, aff);
1307 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1310 /* Given an index expression "index" that refers to the (real) iterator
1311 * through the parameter at position "pos", plug in "iv_map", expressing
1312 * the real iterator in terms of the virtual (outer) iterator.
1314 * In particular, the index expression is of the form
1316 * [..., i, ...] -> { S[i',...] -> ... i ... }
1318 * where i refers to the real iterator and i' refers to the virtual iterator.
1320 * iv_map is of the form
1322 * { [i'] -> [i] }
1324 * Return the index expression
1326 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1329 * We first move the parameter to the input
1331 * [..., ...] -> { [i, i',...] -> ... i ... }
1333 * and construct
1335 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1337 * and then combine the two to obtain the desired result.
1339 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1340 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1342 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1343 isl_multi_aff *ma;
1345 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1346 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1347 isl_dim_param, pos, 1);
1349 space = isl_space_map_from_set(space);
1350 ma = isl_multi_aff_identity(isl_space_copy(space));
1351 iv_map = isl_aff_align_params(iv_map, space);
1352 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1353 ma = isl_multi_aff_flat_range_product(
1354 isl_multi_aff_from_aff(iv_map), ma);
1355 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1357 return index;
1360 /* Does the index expression "index" reference a virtual array, i.e.,
1361 * one with user pointer equal to NULL?
1362 * A virtual array does not have any members.
1364 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1366 isl_id *id;
1367 int is_virtual;
1369 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1370 return 0;
1371 if (isl_multi_pw_aff_range_is_wrapping(index))
1372 return 0;
1373 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1374 is_virtual = !isl_id_get_user(id);
1375 isl_id_free(id);
1377 return is_virtual;
1380 /* Does the access relation "access" reference a virtual array, i.e.,
1381 * one with user pointer equal to NULL?
1382 * A virtual array does not have any members.
1384 static int access_is_virtual_array(__isl_keep isl_map *access)
1386 isl_id *id;
1387 int is_virtual;
1389 if (!isl_map_has_tuple_id(access, isl_dim_out))
1390 return 0;
1391 if (isl_map_range_is_wrapping(access))
1392 return 0;
1393 id = isl_map_get_tuple_id(access, isl_dim_out);
1394 is_virtual = !isl_id_get_user(id);
1395 isl_id_free(id);
1397 return is_virtual;
1400 /* Embed the given index expression in an extra outer loop.
1401 * The domain of the index expression has already been updated.
1403 * If the access refers to the induction variable, then it is
1404 * turned into an access to the set of integers with index (and value)
1405 * equal to the induction variable.
1407 * If the accessed array is a virtual array (with user
1408 * pointer equal to NULL), as created by create_test_index,
1409 * then it is extended along with the domain of the index expression.
1411 static __isl_give isl_multi_pw_aff *embed_index_expression(
1412 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1414 isl_id *array_id = NULL;
1415 int pos;
1417 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1418 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1419 if (array_id == data->var_id) {
1420 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1421 } else if (index_is_virtual_array(index)) {
1422 isl_aff *aff;
1423 isl_multi_pw_aff *mpa;
1425 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1426 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1427 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1428 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1429 isl_id_copy(array_id));
1431 isl_id_free(array_id);
1433 pos = isl_multi_pw_aff_find_dim_by_id(index,
1434 isl_dim_param, data->var_id);
1435 if (pos >= 0)
1436 index = index_internalize_iv(index, pos,
1437 isl_aff_copy(data->iv_map));
1438 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1439 isl_id_copy(data->var_id));
1441 return index;
1444 /* Embed the given access relation in an extra outer loop.
1445 * The domain of the access relation has already been updated.
1447 * If the access refers to the induction variable, then it is
1448 * turned into an access to the set of integers with index (and value)
1449 * equal to the induction variable.
1451 * If the induction variable appears in the constraints (as a parameter),
1452 * then the parameter is equated to the newly introduced iteration
1453 * domain dimension and subsequently projected out.
1455 * Similarly, if the accessed array is a virtual array (with user
1456 * pointer equal to NULL), as created by create_test_index,
1457 * then it is extended along with the domain of the access.
1459 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1460 struct pet_embed_access *data)
1462 isl_id *array_id = NULL;
1463 int pos;
1465 if (isl_map_has_tuple_id(access, isl_dim_out))
1466 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1467 if (array_id == data->var_id || access_is_virtual_array(access)) {
1468 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1469 access = isl_map_equate(access,
1470 isl_dim_in, 0, isl_dim_out, 0);
1471 if (array_id == data->var_id)
1472 access = isl_map_apply_range(access,
1473 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1474 else
1475 access = isl_map_set_tuple_id(access, isl_dim_out,
1476 isl_id_copy(array_id));
1478 isl_id_free(array_id);
1480 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1481 if (pos >= 0) {
1482 isl_set *set = isl_map_wrap(access);
1483 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1484 access = isl_set_unwrap(set);
1486 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1487 isl_id_copy(data->var_id));
1489 return access;
1492 /* Given an access expression, embed the associated access relation and
1493 * index expression in an extra outer loop.
1495 * We first update the domains to insert the extra dimension and
1496 * then update the access relation and index expression to take
1497 * into account the mapping "iv_map" from virtual iterator
1498 * to real iterator.
1500 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
1502 struct pet_embed_access *data = user;
1504 expr = pet_expr_cow(expr);
1505 expr = pet_expr_access_update_domain(expr, data->extend);
1506 if (!expr)
1507 return NULL;
1509 expr->acc.access = embed_access_relation(expr->acc.access, data);
1510 expr->acc.index = embed_index_expression(expr->acc.index, data);
1511 if (!expr->acc.access || !expr->acc.index)
1512 return pet_expr_free(expr);
1514 return expr;
1517 /* Embed all access subexpressions of "expr" in an extra loop.
1518 * "extend" inserts an outer loop iterator in the iteration domains
1519 * (through precomposition).
1520 * "iv_map" expresses the real iterator in terms of the virtual iterator
1521 * "var_id" represents the induction variable.
1523 static __isl_give pet_expr *expr_embed(__isl_take pet_expr *expr,
1524 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1525 __isl_keep isl_id *var_id)
1527 struct pet_embed_access data =
1528 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1530 expr = pet_expr_map_access(expr, &embed_access, &data);
1531 isl_aff_free(iv_map);
1532 isl_multi_pw_aff_free(extend);
1533 return expr;
1536 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1537 * "dom" and schedule "sched". "var_id" represents the induction variable
1538 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1539 * That is, it expresses the iterator that some of the parameters in "stmt"
1540 * may refer to in terms of the iterator used in "dom" and
1541 * the domain of "sched".
1543 * The iteration domain and schedule of the statement are updated
1544 * according to the iteration domain and schedule of the new loop.
1545 * If stmt->domain is a wrapped map, then the iteration domain
1546 * is the domain of this map, so we need to be careful to adjust
1547 * this domain.
1549 * If the induction variable appears in the constraints (as a parameter)
1550 * of the current iteration domain or the schedule of the statement,
1551 * then the parameter is equated to the newly introduced iteration
1552 * domain dimension and subsequently projected out.
1554 * Finally, all access relations are updated based on the extra loop.
1556 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1557 __isl_take isl_set *dom, __isl_take isl_map *sched,
1558 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1560 int i;
1561 int pos;
1562 isl_id *stmt_id;
1563 isl_space *dim;
1564 isl_multi_pw_aff *extend;
1566 if (!stmt)
1567 goto error;
1569 if (isl_set_is_wrapping(stmt->domain)) {
1570 isl_map *map;
1571 isl_map *ext;
1572 isl_space *ran_dim;
1574 map = isl_set_unwrap(stmt->domain);
1575 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1576 ran_dim = isl_space_range(isl_map_get_space(map));
1577 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1578 isl_set_universe(ran_dim));
1579 map = isl_map_flat_domain_product(ext, map);
1580 map = isl_map_set_tuple_id(map, isl_dim_in,
1581 isl_id_copy(stmt_id));
1582 dim = isl_space_domain(isl_map_get_space(map));
1583 stmt->domain = isl_map_wrap(map);
1584 } else {
1585 stmt_id = isl_set_get_tuple_id(stmt->domain);
1586 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1587 stmt->domain);
1588 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1589 isl_id_copy(stmt_id));
1590 dim = isl_set_get_space(stmt->domain);
1593 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1594 if (pos >= 0)
1595 stmt->domain = internalize_iv(stmt->domain, pos,
1596 isl_aff_copy(iv_map));
1598 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1599 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1600 isl_dim_in, stmt_id);
1602 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1603 if (pos >= 0) {
1604 isl_set *set = isl_map_wrap(stmt->schedule);
1605 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1606 stmt->schedule = isl_set_unwrap(set);
1609 dim = isl_space_map_from_set(dim);
1610 extend = isl_multi_pw_aff_identity(dim);
1611 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1612 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1613 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1614 for (i = 0; i < stmt->n_arg; ++i)
1615 stmt->args[i] = expr_embed(stmt->args[i],
1616 isl_multi_pw_aff_copy(extend),
1617 isl_aff_copy(iv_map), var_id);
1618 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1620 isl_set_free(dom);
1621 isl_id_free(var_id);
1623 for (i = 0; i < stmt->n_arg; ++i)
1624 if (!stmt->args[i])
1625 return pet_stmt_free(stmt);
1626 if (!stmt->domain || !stmt->schedule || !stmt->body)
1627 return pet_stmt_free(stmt);
1628 return stmt;
1629 error:
1630 isl_set_free(dom);
1631 isl_map_free(sched);
1632 isl_aff_free(iv_map);
1633 isl_id_free(var_id);
1634 return NULL;
1637 /* Embed the given pet_array in an extra outer loop with iteration domain
1638 * "dom".
1639 * This embedding only has an effect on virtual arrays (those with
1640 * user pointer equal to NULL), which need to be extended along with
1641 * the iteration domain.
1643 static struct pet_array *pet_array_embed(struct pet_array *array,
1644 __isl_take isl_set *dom)
1646 isl_id *array_id = NULL;
1648 if (!array)
1649 goto error;
1650 if (!extent_is_virtual_array(array->extent)) {
1651 isl_set_free(dom);
1652 return array;
1655 array_id = isl_set_get_tuple_id(array->extent);
1656 array->extent = isl_set_flat_product(dom, array->extent);
1657 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1658 if (!array->extent)
1659 return pet_array_free(array);
1661 return array;
1662 error:
1663 isl_set_free(dom);
1664 return NULL;
1667 /* Update the context with respect to an embedding into a loop
1668 * with iteration domain "dom" and induction variable "id".
1669 * "iv_map" expresses the real iterator (parameter "id") in terms
1670 * of a possibly virtual iterator (used in "dom").
1672 * If the current context is independent of "id", we don't need
1673 * to do anything.
1674 * Otherwise, a parameter value is invalid for the embedding if
1675 * any of the corresponding iterator values is invalid.
1676 * That is, a parameter value is valid only if all the corresponding
1677 * iterator values are valid.
1678 * We therefore compute the set of parameters
1680 * forall i in dom : valid (i)
1682 * or
1684 * not exists i in dom : not valid(i)
1686 * i.e.,
1688 * not exists i in dom \ valid(i)
1690 * Before we subtract valid(i) from dom, we first need to substitute
1691 * the real iterator for the virtual iterator.
1693 * If there are any unnamed parameters in "dom", then we consider
1694 * a parameter value to be valid if it is valid for any value of those
1695 * unnamed parameters. They are therefore projected out at the end.
1697 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1698 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1699 __isl_keep isl_id *id)
1701 int pos;
1702 isl_multi_aff *ma;
1704 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1705 if (pos < 0)
1706 return context;
1708 context = isl_set_from_params(context);
1709 context = isl_set_add_dims(context, isl_dim_set, 1);
1710 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1711 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1712 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1713 context = isl_set_preimage_multi_aff(context, ma);
1714 context = isl_set_subtract(isl_set_copy(dom), context);
1715 context = isl_set_params(context);
1716 context = isl_set_complement(context);
1717 context = pet_nested_remove_from_set(context);
1718 return context;
1721 /* Update the implication with respect to an embedding into a loop
1722 * with iteration domain "dom".
1724 * Since embed_access extends virtual arrays along with the domain
1725 * of the access, we need to do the same with domain and range
1726 * of the implication. Since the original implication is only valid
1727 * within a given iteration of the loop, the extended implication
1728 * maps the extra array dimension corresponding to the extra loop
1729 * to itself.
1731 static struct pet_implication *pet_implication_embed(
1732 struct pet_implication *implication, __isl_take isl_set *dom)
1734 isl_id *id;
1735 isl_map *map;
1737 if (!implication)
1738 goto error;
1740 map = isl_set_identity(dom);
1741 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1742 map = isl_map_flat_product(map, implication->extension);
1743 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1744 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1745 implication->extension = map;
1746 if (!implication->extension)
1747 return pet_implication_free(implication);
1749 return implication;
1750 error:
1751 isl_set_free(dom);
1752 return NULL;
1755 /* Embed all statements and arrays in "scop" in an extra outer loop
1756 * with iteration domain "dom" and schedule "sched".
1757 * "id" represents the induction variable of the loop.
1758 * "iv_map" maps a possibly virtual iterator to the real iterator.
1759 * That is, it expresses the iterator that some of the parameters in "scop"
1760 * may refer to in terms of the iterator used in "dom" and
1761 * the domain of "sched".
1763 * Any skip conditions within the loop have no effect outside of the loop.
1764 * The caller is responsible for making sure skip[pet_skip_later] has been
1765 * taken into account.
1767 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1768 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
1769 __isl_take isl_id *id)
1771 int i;
1772 isl_map *sched_map;
1774 sched_map = isl_map_from_aff(sched);
1776 if (!scop)
1777 goto error;
1779 pet_scop_reset_skip(scop, pet_skip_now);
1780 pet_scop_reset_skip(scop, pet_skip_later);
1782 scop->context = context_embed(scop->context, dom, iv_map, id);
1783 if (!scop->context)
1784 goto error;
1786 for (i = 0; i < scop->n_stmt; ++i) {
1787 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1788 isl_set_copy(dom), isl_map_copy(sched_map),
1789 isl_aff_copy(iv_map), isl_id_copy(id));
1790 if (!scop->stmts[i])
1791 goto error;
1794 for (i = 0; i < scop->n_array; ++i) {
1795 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1796 isl_set_copy(dom));
1797 if (!scop->arrays[i])
1798 goto error;
1801 for (i = 0; i < scop->n_implication; ++i) {
1802 scop->implications[i] =
1803 pet_implication_embed(scop->implications[i],
1804 isl_set_copy(dom));
1805 if (!scop->implications[i])
1806 goto error;
1809 isl_set_free(dom);
1810 isl_map_free(sched_map);
1811 isl_aff_free(iv_map);
1812 isl_id_free(id);
1813 return scop;
1814 error:
1815 isl_set_free(dom);
1816 isl_map_free(sched_map);
1817 isl_aff_free(iv_map);
1818 isl_id_free(id);
1819 return pet_scop_free(scop);
1822 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1824 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1825 __isl_take isl_set *cond)
1827 if (!stmt)
1828 goto error;
1830 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1832 return stmt;
1833 error:
1834 isl_set_free(cond);
1835 return pet_stmt_free(stmt);
1838 /* Add extra conditions to scop->skip[type].
1840 * The new skip condition only holds if it held before
1841 * and the condition is true. It does not hold if it did not hold
1842 * before or the condition is false.
1844 * The skip condition is assumed to be an affine expression.
1846 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1847 enum pet_skip type, __isl_keep isl_set *cond)
1849 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1850 isl_pw_aff *skip;
1851 isl_set *dom;
1853 if (!scop)
1854 return NULL;
1855 if (!ext->skip[type])
1856 return scop;
1858 if (!multi_pw_aff_is_affine(ext->skip[type]))
1859 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1860 isl_error_internal, "can only restrict affine skips",
1861 return pet_scop_free(scop));
1863 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1864 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1865 cond = isl_set_copy(cond);
1866 cond = isl_set_from_params(cond);
1867 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1868 skip = indicator_function(cond, dom);
1869 isl_multi_pw_aff_free(ext->skip[type]);
1870 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1871 if (!ext->skip[type])
1872 return pet_scop_free(scop);
1874 return scop;
1877 /* Add extra conditions on the parameters to all iteration domains
1878 * and skip conditions.
1880 * A parameter value is valid for the result if it was valid
1881 * for the original scop and satisfies "cond" or if it does
1882 * not satisfy "cond" as in this case the scop is not executed
1883 * and the original constraints on the parameters are irrelevant.
1885 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1886 __isl_take isl_set *cond)
1888 int i;
1890 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1891 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1893 if (!scop)
1894 goto error;
1896 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1897 scop->context = isl_set_union(scop->context,
1898 isl_set_complement(isl_set_copy(cond)));
1899 scop->context = isl_set_coalesce(scop->context);
1900 scop->context = pet_nested_remove_from_set(scop->context);
1901 if (!scop->context)
1902 goto error;
1904 for (i = 0; i < scop->n_stmt; ++i) {
1905 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1906 isl_set_copy(cond));
1907 if (!scop->stmts[i])
1908 goto error;
1911 isl_set_free(cond);
1912 return scop;
1913 error:
1914 isl_set_free(cond);
1915 return pet_scop_free(scop);
1918 /* Insert an argument expression corresponding to "test" in front
1919 * of the list of arguments described by *n_arg and *args.
1921 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1922 __isl_keep isl_multi_pw_aff *test)
1924 int i;
1925 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1927 if (!test)
1928 return -1;
1930 if (!*args) {
1931 *args = isl_calloc_array(ctx, pet_expr *, 1);
1932 if (!*args)
1933 return -1;
1934 } else {
1935 pet_expr **ext;
1936 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1937 if (!ext)
1938 return -1;
1939 for (i = 0; i < *n_arg; ++i)
1940 ext[1 + i] = (*args)[i];
1941 free(*args);
1942 *args = ext;
1944 (*n_arg)++;
1945 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1946 if (!(*args)[0])
1947 return -1;
1949 return 0;
1952 /* Look through the applications in "scop" for any that can be
1953 * applied to the filter expressed by "map" and "satisified".
1954 * If there is any, then apply it to "map" and return the result.
1955 * Otherwise, return "map".
1956 * "id" is the identifier of the virtual array.
1958 * We only introduce at most one implication for any given virtual array,
1959 * so we can apply the implication and return as soon as we find one.
1961 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1962 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1964 int i;
1966 for (i = 0; i < scop->n_implication; ++i) {
1967 struct pet_implication *pi = scop->implications[i];
1968 isl_id *pi_id;
1970 if (pi->satisfied != satisfied)
1971 continue;
1972 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1973 isl_id_free(pi_id);
1974 if (pi_id != id)
1975 continue;
1977 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1980 return map;
1983 /* Is the filter expressed by "test" and "satisfied" implied
1984 * by filter "pos" on "domain", with filter "expr", taking into
1985 * account the implications of "scop"?
1987 * For filter on domain implying that expressed by "test" and "satisfied",
1988 * the filter needs to be an access to the same (virtual) array as "test" and
1989 * the filter value needs to be equal to "satisfied".
1990 * Moreover, the filter access relation, possibly extended by
1991 * the implications in "scop" needs to contain "test".
1993 static int implies_filter(struct pet_scop *scop,
1994 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1995 __isl_keep isl_map *test, int satisfied)
1997 isl_id *test_id, *arg_id;
1998 isl_val *val;
1999 int is_int;
2000 int s;
2001 int is_subset;
2002 isl_map *implied;
2004 if (expr->type != pet_expr_access)
2005 return 0;
2006 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2007 arg_id = pet_expr_access_get_id(expr);
2008 isl_id_free(arg_id);
2009 isl_id_free(test_id);
2010 if (test_id != arg_id)
2011 return 0;
2012 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2013 is_int = isl_val_is_int(val);
2014 if (is_int)
2015 s = isl_val_get_num_si(val);
2016 isl_val_free(val);
2017 if (!val)
2018 return -1;
2019 if (!is_int)
2020 return 0;
2021 if (s != satisfied)
2022 return 0;
2024 implied = isl_map_copy(expr->acc.access);
2025 implied = apply_implications(scop, implied, test_id, satisfied);
2026 is_subset = isl_map_is_subset(test, implied);
2027 isl_map_free(implied);
2029 return is_subset;
2032 /* Is the filter expressed by "test" and "satisfied" implied
2033 * by any of the filters on the domain of "stmt", taking into
2034 * account the implications of "scop"?
2036 static int filter_implied(struct pet_scop *scop,
2037 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2039 int i;
2040 int implied;
2041 isl_id *test_id;
2042 isl_map *domain;
2043 isl_map *test_map;
2045 if (!scop || !stmt || !test)
2046 return -1;
2047 if (scop->n_implication == 0)
2048 return 0;
2049 if (stmt->n_arg == 0)
2050 return 0;
2052 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2053 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2055 implied = 0;
2056 for (i = 0; i < stmt->n_arg; ++i) {
2057 implied = implies_filter(scop, domain, i, stmt->args[i],
2058 test_map, satisfied);
2059 if (implied < 0 || implied)
2060 break;
2063 isl_map_free(test_map);
2064 isl_map_free(domain);
2065 return implied;
2068 /* Make the statement "stmt" depend on the value of "test"
2069 * being equal to "satisfied" by adjusting stmt->domain.
2071 * The domain of "test" corresponds to the (zero or more) outer dimensions
2072 * of the iteration domain.
2074 * We first extend "test" to apply to the entire iteration domain and
2075 * then check if the filter that we are about to add is implied
2076 * by any of the current filters, possibly taking into account
2077 * the implications in "scop". If so, we leave "stmt" untouched and return.
2079 * Otherwise, we insert an argument corresponding to a read to "test"
2080 * from the iteration domain of "stmt" in front of the list of arguments.
2081 * We also insert a corresponding output dimension in the wrapped
2082 * map contained in stmt->domain, with value set to "satisfied".
2084 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2085 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2087 int i;
2088 int implied;
2089 isl_id *id;
2090 isl_ctx *ctx;
2091 isl_pw_multi_aff *pma;
2092 isl_multi_aff *add_dom;
2093 isl_space *space;
2094 isl_local_space *ls;
2095 int n_test_dom;
2097 if (!stmt || !test)
2098 goto error;
2100 space = pet_stmt_get_space(stmt);
2101 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2102 space = isl_space_from_domain(space);
2103 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2104 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2105 ls = isl_local_space_from_space(isl_space_domain(space));
2106 for (i = 0; i < n_test_dom; ++i) {
2107 isl_aff *aff;
2108 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2109 isl_dim_set, i);
2110 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2112 isl_local_space_free(ls);
2113 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2115 implied = filter_implied(scop, stmt, test, satisfied);
2116 if (implied < 0)
2117 goto error;
2118 if (implied) {
2119 isl_multi_pw_aff_free(test);
2120 return stmt;
2123 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2124 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
2125 id, satisfied);
2126 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2128 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2129 goto error;
2131 isl_multi_pw_aff_free(test);
2132 return stmt;
2133 error:
2134 isl_multi_pw_aff_free(test);
2135 return pet_stmt_free(stmt);
2138 /* Does "scop" have a skip condition of the given "type"?
2140 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2142 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2144 if (!scop)
2145 return -1;
2146 return ext->skip[type] != NULL;
2149 /* Does "scop" have a skip condition of the given "type" that
2150 * is an affine expression?
2152 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2154 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2156 if (!scop)
2157 return -1;
2158 if (!ext->skip[type])
2159 return 0;
2160 return multi_pw_aff_is_affine(ext->skip[type]);
2163 /* Does "scop" have a skip condition of the given "type" that
2164 * is not an affine expression?
2166 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2168 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2169 int aff;
2171 if (!scop)
2172 return -1;
2173 if (!ext->skip[type])
2174 return 0;
2175 aff = multi_pw_aff_is_affine(ext->skip[type]);
2176 if (aff < 0)
2177 return -1;
2178 return !aff;
2181 /* Does "scop" have a skip condition of the given "type" that
2182 * is affine and holds on the entire domain?
2184 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2186 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2187 isl_pw_aff *pa;
2188 isl_set *set;
2189 int is_aff;
2190 int is_univ;
2192 is_aff = pet_scop_has_affine_skip(scop, type);
2193 if (is_aff < 0 || !is_aff)
2194 return is_aff;
2196 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2197 set = isl_pw_aff_non_zero_set(pa);
2198 is_univ = isl_set_plain_is_universe(set);
2199 isl_set_free(set);
2201 return is_univ;
2204 /* Replace scop->skip[type] by "skip".
2206 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2207 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2209 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2211 if (!scop || !skip)
2212 goto error;
2214 isl_multi_pw_aff_free(ext->skip[type]);
2215 ext->skip[type] = skip;
2217 return scop;
2218 error:
2219 isl_multi_pw_aff_free(skip);
2220 return pet_scop_free(scop);
2223 /* Return a copy of scop->skip[type].
2225 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2226 enum pet_skip type)
2228 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2230 if (!scop)
2231 return NULL;
2233 return isl_multi_pw_aff_copy(ext->skip[type]);
2236 /* Assuming scop->skip[type] is an affine expression,
2237 * return the constraints on the parameters for which the skip condition
2238 * holds.
2240 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2241 enum pet_skip type)
2243 isl_multi_pw_aff *skip;
2244 isl_pw_aff *pa;
2246 skip = pet_scop_get_skip(scop, type);
2247 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2248 isl_multi_pw_aff_free(skip);
2249 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2252 /* Return the identifier of the variable that is accessed by
2253 * the skip condition of the given type.
2255 * The skip condition is assumed not to be an affine condition.
2257 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2258 enum pet_skip type)
2260 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2262 if (!scop)
2263 return NULL;
2265 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2268 /* Return an access pet_expr corresponding to the skip condition
2269 * of the given type.
2271 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2272 enum pet_skip type)
2274 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2277 /* Drop the the skip condition scop->skip[type].
2279 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2281 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2283 if (!scop)
2284 return;
2286 isl_multi_pw_aff_free(ext->skip[type]);
2287 ext->skip[type] = NULL;
2290 /* Make the skip condition (if any) depend on the value of "test" being
2291 * equal to "satisfied".
2293 * We only support the case where the original skip condition is universal,
2294 * i.e., where skipping is unconditional, and where satisfied == 1.
2295 * In this case, the skip condition is changed to skip only when
2296 * "test" is equal to one.
2298 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2299 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2301 int is_univ = 0;
2303 if (!scop)
2304 return NULL;
2305 if (!pet_scop_has_skip(scop, type))
2306 return scop;
2308 if (satisfied)
2309 is_univ = pet_scop_has_universal_skip(scop, type);
2310 if (is_univ < 0)
2311 return pet_scop_free(scop);
2312 if (satisfied && is_univ) {
2313 isl_multi_pw_aff *skip;
2314 skip = isl_multi_pw_aff_copy(test);
2315 scop = pet_scop_set_skip(scop, type, skip);
2316 if (!scop)
2317 return NULL;
2318 } else {
2319 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2320 "skip expression cannot be filtered",
2321 return pet_scop_free(scop));
2324 return scop;
2327 /* Make all statements in "scop" depend on the value of "test"
2328 * being equal to "satisfied" by adjusting their domains.
2330 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2331 __isl_take isl_multi_pw_aff *test, int satisfied)
2333 int i;
2335 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2336 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2338 if (!scop || !test)
2339 goto error;
2341 for (i = 0; i < scop->n_stmt; ++i) {
2342 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2343 isl_multi_pw_aff_copy(test), satisfied);
2344 if (!scop->stmts[i])
2345 goto error;
2348 isl_multi_pw_aff_free(test);
2349 return scop;
2350 error:
2351 isl_multi_pw_aff_free(test);
2352 return pet_scop_free(scop);
2355 /* Add all parameters in "expr" to "space" and return the result.
2357 static __isl_give isl_space *expr_collect_params(__isl_keep pet_expr *expr,
2358 __isl_take isl_space *space)
2360 int i;
2362 if (!expr)
2363 goto error;
2364 for (i = 0; i < expr->n_arg; ++i)
2365 space = expr_collect_params(expr->args[i], space);
2367 if (expr->type == pet_expr_access)
2368 space = isl_space_align_params(space,
2369 isl_map_get_space(expr->acc.access));
2371 return space;
2372 error:
2373 pet_expr_free(expr);
2374 return isl_space_free(space);
2377 /* Add all parameters in "stmt" to "space" and return the result.
2379 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2380 __isl_take isl_space *space)
2382 int i;
2384 if (!stmt)
2385 return isl_space_free(space);
2387 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2388 space = isl_space_align_params(space,
2389 isl_map_get_space(stmt->schedule));
2390 for (i = 0; i < stmt->n_arg; ++i)
2391 space = expr_collect_params(stmt->args[i], space);
2392 space = expr_collect_params(stmt->body, space);
2394 return space;
2397 /* Add all parameters in "array" to "space" and return the result.
2399 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2400 __isl_take isl_space *space)
2402 if (!array)
2403 return isl_space_free(space);
2405 space = isl_space_align_params(space,
2406 isl_set_get_space(array->context));
2407 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2409 return space;
2412 /* Add all parameters in "scop" to "space" and return the result.
2414 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2415 __isl_take isl_space *space)
2417 int i;
2419 if (!scop)
2420 return isl_space_free(space);
2422 for (i = 0; i < scop->n_array; ++i)
2423 space = array_collect_params(scop->arrays[i], space);
2425 for (i = 0; i < scop->n_stmt; ++i)
2426 space = stmt_collect_params(scop->stmts[i], space);
2428 return space;
2431 /* Add all parameters in "space" to the domain, schedule and
2432 * all access relations in "stmt".
2434 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2435 __isl_take isl_space *space)
2437 int i;
2439 if (!stmt)
2440 goto error;
2442 stmt->domain = isl_set_align_params(stmt->domain,
2443 isl_space_copy(space));
2444 stmt->schedule = isl_map_align_params(stmt->schedule,
2445 isl_space_copy(space));
2447 for (i = 0; i < stmt->n_arg; ++i) {
2448 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2449 isl_space_copy(space));
2450 if (!stmt->args[i])
2451 goto error;
2453 stmt->body = pet_expr_align_params(stmt->body, isl_space_copy(space));
2455 if (!stmt->domain || !stmt->schedule || !stmt->body)
2456 goto error;
2458 isl_space_free(space);
2459 return stmt;
2460 error:
2461 isl_space_free(space);
2462 return pet_stmt_free(stmt);
2465 /* Add all parameters in "space" to "array".
2467 static struct pet_array *array_propagate_params(struct pet_array *array,
2468 __isl_take isl_space *space)
2470 if (!array)
2471 goto error;
2473 array->context = isl_set_align_params(array->context,
2474 isl_space_copy(space));
2475 array->extent = isl_set_align_params(array->extent,
2476 isl_space_copy(space));
2477 if (array->value_bounds) {
2478 array->value_bounds = isl_set_align_params(array->value_bounds,
2479 isl_space_copy(space));
2480 if (!array->value_bounds)
2481 goto error;
2484 if (!array->context || !array->extent)
2485 goto error;
2487 isl_space_free(space);
2488 return array;
2489 error:
2490 isl_space_free(space);
2491 return pet_array_free(array);
2494 /* Add all parameters in "space" to "scop".
2496 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2497 __isl_take isl_space *space)
2499 int i;
2501 if (!scop)
2502 goto error;
2504 for (i = 0; i < scop->n_array; ++i) {
2505 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2506 isl_space_copy(space));
2507 if (!scop->arrays[i])
2508 goto error;
2511 for (i = 0; i < scop->n_stmt; ++i) {
2512 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2513 isl_space_copy(space));
2514 if (!scop->stmts[i])
2515 goto error;
2518 isl_space_free(space);
2519 return scop;
2520 error:
2521 isl_space_free(space);
2522 return pet_scop_free(scop);
2525 /* Update all isl_sets and isl_maps in "scop" such that they all
2526 * have the same parameters.
2528 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2530 isl_space *space;
2532 if (!scop)
2533 return NULL;
2535 space = isl_set_get_space(scop->context);
2536 space = scop_collect_params(scop, space);
2538 scop->context = isl_set_align_params(scop->context,
2539 isl_space_copy(space));
2540 scop = scop_propagate_params(scop, space);
2542 if (scop && !scop->context)
2543 return pet_scop_free(scop);
2545 return scop;
2548 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2549 * in "space" by a value equal to the corresponding parameter.
2551 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2552 __isl_take isl_space *space)
2554 if (!stmt)
2555 goto error;
2557 stmt->body = pet_expr_detect_parameter_accesses(stmt->body,
2558 isl_space_copy(space));
2560 if (!stmt->domain || !stmt->schedule || !stmt->body)
2561 goto error;
2563 isl_space_free(space);
2564 return stmt;
2565 error:
2566 isl_space_free(space);
2567 return pet_stmt_free(stmt);
2570 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2571 * in "space" by a value equal to the corresponding parameter.
2573 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2574 __isl_take isl_space *space)
2576 int i;
2578 if (!scop)
2579 goto error;
2581 for (i = 0; i < scop->n_stmt; ++i) {
2582 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2583 isl_space_copy(space));
2584 if (!scop->stmts[i])
2585 goto error;
2588 isl_space_free(space);
2589 return scop;
2590 error:
2591 isl_space_free(space);
2592 return pet_scop_free(scop);
2595 /* Replace all accesses to (0D) arrays that correspond to any of
2596 * the parameters used in "scop" by a value equal
2597 * to the corresponding parameter.
2599 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2601 isl_space *space;
2603 if (!scop)
2604 return NULL;
2606 space = isl_set_get_space(scop->context);
2607 space = scop_collect_params(scop, space);
2609 scop = scop_detect_parameter_accesses(scop, space);
2611 return scop;
2614 /* Add the access relation of the access expression "expr" to "accesses" and
2615 * return the result.
2616 * The domain of the access relation is intersected with "domain".
2617 * If "tag" is set, then the access relation is tagged with
2618 * the corresponding reference identifier.
2620 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2621 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2623 isl_map *access;
2625 access = pet_expr_access_get_may_access(expr);
2626 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2627 if (tag)
2628 access = pet_expr_tag_access(expr, access);
2629 return isl_union_map_add_map(accesses, access);
2632 /* Add all read access relations (if "read" is set) and/or all write
2633 * access relations (if "write" is set) to "accesses" and return the result.
2634 * The domains of the access relations are intersected with "domain".
2635 * If "tag" is set, then the access relations are tagged with
2636 * the corresponding reference identifiers.
2638 * If "must" is set, then we only add the accesses that are definitely
2639 * performed. Otherwise, we add all potential accesses.
2640 * In particular, if the access has any arguments, then if "must" is
2641 * set we currently skip the access completely. If "must" is not set,
2642 * we project out the values of the access arguments.
2644 static __isl_give isl_union_map *expr_collect_accesses(
2645 __isl_keep pet_expr *expr, int read, int write, int must, int tag,
2646 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2648 int i;
2649 isl_id *id;
2650 isl_space *dim;
2652 if (!expr)
2653 return isl_union_map_free(accesses);
2655 for (i = 0; i < expr->n_arg; ++i)
2656 accesses = expr_collect_accesses(expr->args[i],
2657 read, write, must, tag, accesses, domain);
2659 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2660 ((read && expr->acc.read) || (write && expr->acc.write)) &&
2661 (!must || expr->n_arg == 0)) {
2662 accesses = expr_collect_access(expr, tag, accesses, domain);
2665 return accesses;
2668 /* Collect and return all read access relations (if "read" is set)
2669 * and/or all write access relations (if "write" is set) in "stmt".
2670 * If "tag" is set, then the access relations are tagged with
2671 * the corresponding reference identifiers.
2672 * If "kill" is set, then "stmt" is a kill statement and we simply
2673 * add the argument of the kill operation.
2675 * If "must" is set, then we only add the accesses that are definitely
2676 * performed. Otherwise, we add all potential accesses.
2677 * In particular, if the statement has any arguments, then if "must" is
2678 * set we currently skip the statement completely. If "must" is not set,
2679 * we project out the values of the statement arguments.
2681 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2682 int read, int write, int kill, int must, int tag,
2683 __isl_take isl_space *dim)
2685 isl_union_map *accesses;
2686 isl_set *domain;
2688 if (!stmt)
2689 return NULL;
2691 accesses = isl_union_map_empty(dim);
2693 if (must && stmt->n_arg > 0)
2694 return accesses;
2696 domain = isl_set_copy(stmt->domain);
2697 if (isl_set_is_wrapping(domain))
2698 domain = isl_map_domain(isl_set_unwrap(domain));
2700 if (kill)
2701 accesses = expr_collect_access(stmt->body->args[0], tag,
2702 accesses, domain);
2703 else
2704 accesses = expr_collect_accesses(stmt->body, read, write,
2705 must, tag, accesses, domain);
2706 isl_set_free(domain);
2708 return accesses;
2711 /* Is "stmt" an assignment statement?
2713 int pet_stmt_is_assign(struct pet_stmt *stmt)
2715 if (!stmt)
2716 return 0;
2717 if (stmt->body->type != pet_expr_op)
2718 return 0;
2719 return stmt->body->op == pet_op_assign;
2722 /* Is "stmt" a kill statement?
2724 int pet_stmt_is_kill(struct pet_stmt *stmt)
2726 if (!stmt)
2727 return 0;
2728 if (stmt->body->type != pet_expr_op)
2729 return 0;
2730 return stmt->body->op == pet_op_kill;
2733 /* Is "stmt" an assume statement?
2735 int pet_stmt_is_assume(struct pet_stmt *stmt)
2737 if (!stmt)
2738 return 0;
2739 return pet_expr_is_assume(stmt->body);
2742 /* Compute a mapping from all arrays (of structs) in scop
2743 * to their innermost arrays.
2745 * In particular, for each array of a primitive type, the result
2746 * contains the identity mapping on that array.
2747 * For each array involving member accesses, the result
2748 * contains a mapping from the elements of any intermediate array of structs
2749 * to all corresponding elements of the innermost nested arrays.
2751 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2753 int i;
2754 isl_union_map *to_inner;
2756 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2758 for (i = 0; i < scop->n_array; ++i) {
2759 struct pet_array *array = scop->arrays[i];
2760 isl_set *set;
2761 isl_map *map, *gist;
2763 if (array->element_is_record)
2764 continue;
2766 map = isl_set_identity(isl_set_copy(array->extent));
2768 set = isl_map_domain(isl_map_copy(map));
2769 gist = isl_map_copy(map);
2770 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2771 to_inner = isl_union_map_add_map(to_inner, gist);
2773 while (set && isl_set_is_wrapping(set)) {
2774 isl_id *id;
2775 isl_map *wrapped;
2777 id = isl_set_get_tuple_id(set);
2778 wrapped = isl_set_unwrap(set);
2779 wrapped = isl_map_domain_map(wrapped);
2780 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2781 map = isl_map_apply_domain(map, wrapped);
2782 set = isl_map_domain(isl_map_copy(map));
2783 gist = isl_map_copy(map);
2784 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2785 to_inner = isl_union_map_add_map(to_inner, gist);
2788 isl_set_free(set);
2789 isl_map_free(map);
2792 return to_inner;
2795 /* Collect and return all read access relations (if "read" is set)
2796 * and/or all write access relations (if "write" is set) in "scop".
2797 * If "kill" is set, then we only add the arguments of kill operations.
2798 * If "must" is set, then we only add the accesses that are definitely
2799 * performed. Otherwise, we add all potential accesses.
2800 * If "tag" is set, then the access relations are tagged with
2801 * the corresponding reference identifiers.
2802 * For accesses to structures, the returned access relation accesses
2803 * all individual fields in the structures.
2805 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2806 int read, int write, int kill, int must, int tag)
2808 int i;
2809 isl_union_map *accesses;
2810 isl_union_set *arrays;
2811 isl_union_map *to_inner;
2813 if (!scop)
2814 return NULL;
2816 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2818 for (i = 0; i < scop->n_stmt; ++i) {
2819 struct pet_stmt *stmt = scop->stmts[i];
2820 isl_union_map *accesses_i;
2821 isl_space *space;
2823 if (kill && !pet_stmt_is_kill(stmt))
2824 continue;
2826 space = isl_set_get_space(scop->context);
2827 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2828 must, tag, space);
2829 accesses = isl_union_map_union(accesses, accesses_i);
2832 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2833 for (i = 0; i < scop->n_array; ++i) {
2834 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2835 arrays = isl_union_set_add_set(arrays, extent);
2837 accesses = isl_union_map_intersect_range(accesses, arrays);
2839 to_inner = compute_to_inner(scop);
2840 accesses = isl_union_map_apply_range(accesses, to_inner);
2842 return accesses;
2845 /* Collect all potential read access relations.
2847 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2849 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2852 /* Collect all potential write access relations.
2854 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2856 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2859 /* Collect all definite write access relations.
2861 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2863 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2866 /* Collect all definite kill access relations.
2868 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2870 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2873 /* Collect all tagged potential read access relations.
2875 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2876 struct pet_scop *scop)
2878 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2881 /* Collect all tagged potential write access relations.
2883 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2884 struct pet_scop *scop)
2886 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2889 /* Collect all tagged definite write access relations.
2891 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2892 struct pet_scop *scop)
2894 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2897 /* Collect all tagged definite kill access relations.
2899 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2900 struct pet_scop *scop)
2902 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2905 /* Collect and return the union of iteration domains in "scop".
2907 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2909 int i;
2910 isl_set *domain_i;
2911 isl_union_set *domain;
2913 if (!scop)
2914 return NULL;
2916 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2918 for (i = 0; i < scop->n_stmt; ++i) {
2919 domain_i = isl_set_copy(scop->stmts[i]->domain);
2920 domain = isl_union_set_add_set(domain, domain_i);
2923 return domain;
2926 /* Collect and return the schedules of the statements in "scop".
2927 * The range is normalized to the maximal number of scheduling
2928 * dimensions.
2930 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2932 int i, j;
2933 isl_map *schedule_i;
2934 isl_union_map *schedule;
2935 int depth, max_depth = 0;
2937 if (!scop)
2938 return NULL;
2940 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2942 for (i = 0; i < scop->n_stmt; ++i) {
2943 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2944 if (depth > max_depth)
2945 max_depth = depth;
2948 for (i = 0; i < scop->n_stmt; ++i) {
2949 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2950 depth = isl_map_dim(schedule_i, isl_dim_out);
2951 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2952 max_depth - depth);
2953 for (j = depth; j < max_depth; ++j)
2954 schedule_i = isl_map_fix_si(schedule_i,
2955 isl_dim_out, j, 0);
2956 schedule = isl_union_map_add_map(schedule, schedule_i);
2959 return schedule;
2962 /* Add a reference identifier to all access expressions in "stmt".
2963 * "n_ref" points to an integer that contains the sequence number
2964 * of the next reference.
2966 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2968 int i;
2970 if (!stmt)
2971 return NULL;
2973 for (i = 0; i < stmt->n_arg; ++i) {
2974 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2975 if (!stmt->args[i])
2976 return pet_stmt_free(stmt);
2979 stmt->body = pet_expr_add_ref_ids(stmt->body, n_ref);
2980 if (!stmt->body)
2981 return pet_stmt_free(stmt);
2983 return stmt;
2986 /* Add a reference identifier to all access expressions in "scop".
2988 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2990 int i;
2991 int n_ref;
2993 if (!scop)
2994 return NULL;
2996 n_ref = 0;
2997 for (i = 0; i < scop->n_stmt; ++i) {
2998 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2999 if (!scop->stmts[i])
3000 return pet_scop_free(scop);
3003 return scop;
3006 /* Reset the user pointer on all parameter ids in "array".
3008 static struct pet_array *array_anonymize(struct pet_array *array)
3010 if (!array)
3011 return NULL;
3013 array->context = isl_set_reset_user(array->context);
3014 array->extent = isl_set_reset_user(array->extent);
3015 if (!array->context || !array->extent)
3016 return pet_array_free(array);
3018 return array;
3021 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3023 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3025 int i;
3026 isl_space *space;
3027 isl_set *domain;
3029 if (!stmt)
3030 return NULL;
3032 stmt->domain = isl_set_reset_user(stmt->domain);
3033 stmt->schedule = isl_map_reset_user(stmt->schedule);
3034 if (!stmt->domain || !stmt->schedule)
3035 return pet_stmt_free(stmt);
3037 for (i = 0; i < stmt->n_arg; ++i) {
3038 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
3039 if (!stmt->args[i])
3040 return pet_stmt_free(stmt);
3043 stmt->body = pet_expr_anonymize(stmt->body);
3044 if (!stmt->body)
3045 return pet_stmt_free(stmt);
3047 return stmt;
3050 /* Reset the user pointer on the tuple ids and all parameter ids
3051 * in "implication".
3053 static struct pet_implication *implication_anonymize(
3054 struct pet_implication *implication)
3056 if (!implication)
3057 return NULL;
3059 implication->extension = isl_map_reset_user(implication->extension);
3060 if (!implication->extension)
3061 return pet_implication_free(implication);
3063 return implication;
3066 /* Reset the user pointer on all parameter and tuple ids in "scop".
3068 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3070 int i;
3072 if (!scop)
3073 return NULL;
3075 scop->context = isl_set_reset_user(scop->context);
3076 scop->context_value = isl_set_reset_user(scop->context_value);
3077 if (!scop->context || !scop->context_value)
3078 return pet_scop_free(scop);
3080 for (i = 0; i < scop->n_array; ++i) {
3081 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3082 if (!scop->arrays[i])
3083 return pet_scop_free(scop);
3086 for (i = 0; i < scop->n_stmt; ++i) {
3087 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3088 if (!scop->stmts[i])
3089 return pet_scop_free(scop);
3092 for (i = 0; i < scop->n_implication; ++i) {
3093 scop->implications[i] =
3094 implication_anonymize(scop->implications[i]);
3095 if (!scop->implications[i])
3096 return pet_scop_free(scop);
3099 return scop;
3102 /* Compute the gist of the iteration domain and all access relations
3103 * of "stmt" based on the constraints on the parameters specified by "context"
3104 * and the constraints on the values of nested accesses specified
3105 * by "value_bounds".
3107 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3108 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3110 int i;
3111 isl_set *domain;
3113 if (!stmt)
3114 return NULL;
3116 domain = isl_set_copy(stmt->domain);
3117 if (stmt->n_arg > 0)
3118 domain = isl_map_domain(isl_set_unwrap(domain));
3120 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3122 for (i = 0; i < stmt->n_arg; ++i) {
3123 stmt->args[i] = pet_expr_gist(stmt->args[i],
3124 domain, value_bounds);
3125 if (!stmt->args[i])
3126 goto error;
3129 stmt->body = pet_expr_gist(stmt->body, domain, value_bounds);
3130 if (!stmt->body)
3131 goto error;
3133 isl_set_free(domain);
3135 domain = isl_set_universe(pet_stmt_get_space(stmt));
3136 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3137 if (stmt->n_arg > 0)
3138 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3139 value_bounds);
3140 stmt->domain = isl_set_gist(stmt->domain, domain);
3141 if (!stmt->domain)
3142 return pet_stmt_free(stmt);
3144 return stmt;
3145 error:
3146 isl_set_free(domain);
3147 return pet_stmt_free(stmt);
3150 /* Compute the gist of the extent of the array
3151 * based on the constraints on the parameters specified by "context".
3153 static struct pet_array *array_gist(struct pet_array *array,
3154 __isl_keep isl_set *context)
3156 if (!array)
3157 return NULL;
3159 array->extent = isl_set_gist_params(array->extent,
3160 isl_set_copy(context));
3161 if (!array->extent)
3162 return pet_array_free(array);
3164 return array;
3167 /* Compute the gist of all sets and relations in "scop"
3168 * based on the constraints on the parameters specified by "scop->context"
3169 * and the constraints on the values of nested accesses specified
3170 * by "value_bounds".
3172 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3173 __isl_keep isl_union_map *value_bounds)
3175 int i;
3177 if (!scop)
3178 return NULL;
3180 scop->context = isl_set_coalesce(scop->context);
3181 if (!scop->context)
3182 return pet_scop_free(scop);
3184 for (i = 0; i < scop->n_array; ++i) {
3185 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3186 if (!scop->arrays[i])
3187 return pet_scop_free(scop);
3190 for (i = 0; i < scop->n_stmt; ++i) {
3191 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3192 value_bounds);
3193 if (!scop->stmts[i])
3194 return pet_scop_free(scop);
3197 return scop;
3200 /* Intersect the context of "scop" with "context".
3201 * To ensure that we don't introduce any unnamed parameters in
3202 * the context of "scop", we first remove the unnamed parameters
3203 * from "context".
3205 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3206 __isl_take isl_set *context)
3208 if (!scop)
3209 goto error;
3211 context = pet_nested_remove_from_set(context);
3212 scop->context = isl_set_intersect(scop->context, context);
3213 if (!scop->context)
3214 return pet_scop_free(scop);
3216 return scop;
3217 error:
3218 isl_set_free(context);
3219 return pet_scop_free(scop);
3222 /* Drop the current context of "scop". That is, replace the context
3223 * by a universal set.
3225 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3227 isl_space *space;
3229 if (!scop)
3230 return NULL;
3232 space = isl_set_get_space(scop->context);
3233 isl_set_free(scop->context);
3234 scop->context = isl_set_universe(space);
3235 if (!scop->context)
3236 return pet_scop_free(scop);
3238 return scop;
3241 /* Append "array" to the arrays of "scop".
3243 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3244 struct pet_array *array)
3246 isl_ctx *ctx;
3247 struct pet_array **arrays;
3249 if (!array || !scop)
3250 goto error;
3252 ctx = isl_set_get_ctx(scop->context);
3253 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3254 scop->n_array + 1);
3255 if (!arrays)
3256 goto error;
3257 scop->arrays = arrays;
3258 scop->arrays[scop->n_array] = array;
3259 scop->n_array++;
3261 return scop;
3262 error:
3263 pet_array_free(array);
3264 return pet_scop_free(scop);
3267 /* Create an index expression for an access to a virtual array
3268 * representing the result of a condition.
3269 * Unlike other accessed data, the id of the array is NULL as
3270 * there is no ValueDecl in the program corresponding to the virtual
3271 * array.
3272 * The array starts out as a scalar, but grows along with the
3273 * statement writing to the array in pet_scop_embed.
3275 __isl_give isl_multi_pw_aff *pet_create_test_index(isl_ctx *ctx, int test_nr)
3277 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
3278 isl_id *id;
3279 char name[50];
3281 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3282 id = isl_id_alloc(ctx, name, NULL);
3283 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
3284 return isl_multi_pw_aff_zero(dim);
3287 /* Add an array with the given extent (range of "index") to the list
3288 * of arrays in "scop" and return the extended pet_scop.
3289 * "int_size" is the number of bytes needed to represent values of type "int".
3290 * The array is marked as attaining values 0 and 1 only and
3291 * as each element being assigned at most once.
3293 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3294 __isl_take isl_multi_pw_aff *index, int int_size)
3296 isl_ctx *ctx;
3297 isl_space *space;
3298 struct pet_array *array;
3299 isl_map *access;
3301 if (!scop || !index)
3302 goto error;
3304 ctx = isl_multi_pw_aff_get_ctx(index);
3305 array = isl_calloc_type(ctx, struct pet_array);
3306 if (!array)
3307 goto error;
3309 access = isl_map_from_multi_pw_aff(index);
3310 array->extent = isl_map_range(access);
3311 space = isl_space_params_alloc(ctx, 0);
3312 array->context = isl_set_universe(space);
3313 space = isl_space_set_alloc(ctx, 0, 1);
3314 array->value_bounds = isl_set_universe(space);
3315 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3316 isl_dim_set, 0, 0);
3317 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3318 isl_dim_set, 0, 1);
3319 array->element_type = strdup("int");
3320 array->element_size = int_size;
3321 array->uniquely_defined = 1;
3323 if (!array->extent || !array->context)
3324 array = pet_array_free(array);
3326 scop = pet_scop_add_array(scop, array);
3328 return scop;
3329 error:
3330 isl_multi_pw_aff_free(index);
3331 return pet_scop_free(scop);
3334 /* Create and return an implication on filter values equal to "satisfied"
3335 * with extension "map".
3337 static struct pet_implication *new_implication(__isl_take isl_map *map,
3338 int satisfied)
3340 isl_ctx *ctx;
3341 struct pet_implication *implication;
3343 if (!map)
3344 return NULL;
3345 ctx = isl_map_get_ctx(map);
3346 implication = isl_alloc_type(ctx, struct pet_implication);
3347 if (!implication)
3348 goto error;
3350 implication->extension = map;
3351 implication->satisfied = satisfied;
3353 return implication;
3354 error:
3355 isl_map_free(map);
3356 return NULL;
3359 /* Add an implication on filter values equal to "satisfied"
3360 * with extension "map" to "scop".
3362 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3363 __isl_take isl_map *map, int satisfied)
3365 isl_ctx *ctx;
3366 struct pet_implication *implication;
3367 struct pet_implication **implications;
3369 implication = new_implication(map, satisfied);
3370 if (!scop || !implication)
3371 goto error;
3373 ctx = isl_set_get_ctx(scop->context);
3374 implications = isl_realloc_array(ctx, scop->implications,
3375 struct pet_implication *,
3376 scop->n_implication + 1);
3377 if (!implications)
3378 goto error;
3379 scop->implications = implications;
3380 scop->implications[scop->n_implication] = implication;
3381 scop->n_implication++;
3383 return scop;
3384 error:
3385 pet_implication_free(implication);
3386 return pet_scop_free(scop);
3389 /* Given an access expression, check if it is data dependent.
3390 * If so, set *found and abort the search.
3392 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3394 int *found = user;
3396 if (pet_expr_get_n_arg(expr) > 0) {
3397 *found = 1;
3398 return -1;
3401 return 0;
3404 /* Does "scop" contain any data dependent accesses?
3406 * Check the body of each statement for such accesses.
3408 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3410 int i;
3411 int found = 0;
3413 if (!scop)
3414 return -1;
3416 for (i = 0; i < scop->n_stmt; ++i) {
3417 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
3418 &is_data_dependent, &found);
3419 if (r < 0 && !found)
3420 return -1;
3421 if (found)
3422 return found;
3425 return found;
3428 /* Does "scop" contain and data dependent conditions?
3430 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3432 int i;
3434 if (!scop)
3435 return -1;
3437 for (i = 0; i < scop->n_stmt; ++i)
3438 if (scop->stmts[i]->n_arg > 0)
3439 return 1;
3441 return 0;
3444 /* Keep track of the "input" file inside the (extended) "scop".
3446 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3448 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3450 if (!scop)
3451 return NULL;
3453 ext->input = input;
3455 return scop;
3458 /* Print the original code corresponding to "scop" to printer "p".
3460 * pet_scop_print_original can only be called from
3461 * a pet_transform_C_source callback. This means that the input
3462 * file is stored in the extended scop and that the printer prints
3463 * to a file.
3465 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3466 __isl_take isl_printer *p)
3468 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3469 FILE *output;
3470 unsigned start, end;
3472 if (!scop || !p)
3473 return isl_printer_free(p);
3475 if (!ext->input)
3476 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3477 "no input file stored in scop",
3478 return isl_printer_free(p));
3480 output = isl_printer_get_file(p);
3481 if (!output)
3482 return isl_printer_free(p);
3484 start = pet_loc_get_start(scop->loc);
3485 end = pet_loc_get_end(scop->loc);
3486 if (copy(ext->input, output, start, end) < 0)
3487 return isl_printer_free(p);
3489 return p;