pet_array_embed: separate out extent_is_virtual_array
[pet.git] / scop.c
blobfe20c72b39ae79eaa9a294540a4c041aad4b1931
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 "expr.h"
40 #include "filter.h"
41 #include "loc.h"
42 #include "nest.h"
43 #include "scop.h"
44 #include "print.h"
45 #include "value_bounds.h"
47 /* pet_scop with extra information that is used during parsing and printing.
49 * In particular, we keep track of conditions under which we want
50 * to skip the rest of the current loop iteration (skip[pet_skip_now])
51 * and of conditions under which we want to skip subsequent
52 * loop iterations (skip[pet_skip_later]).
54 * The conditions are represented as index expressions defined
55 * over a zero-dimensional domain. The index expression is either
56 * a boolean affine expression or an access to a variable, which
57 * is assumed to attain values zero and one. The condition holds
58 * if the variable has value one or if the affine expression
59 * has value one (typically for only part of the parameter space).
61 * A missing condition (skip[type] == NULL) means that we don't want
62 * to skip anything.
64 * Additionally, we keep track of the original input file
65 * inside pet_transform_C_source.
67 struct pet_scop_ext {
68 struct pet_scop scop;
70 isl_multi_pw_aff *skip[2];
71 FILE *input;
74 /* Construct a pet_stmt with given location and statement
75 * number from a pet_expr.
76 * The initial iteration domain is the zero-dimensional universe.
77 * The name of the domain is given by "label" if it is non-NULL.
78 * Otherwise, the name is constructed as S_<id>.
79 * The domains of all access relations are modified to refer
80 * to the statement iteration domain.
82 struct pet_stmt *pet_stmt_from_pet_expr(__isl_take pet_loc *loc,
83 __isl_take isl_id *label, int id, __isl_take pet_expr *expr)
85 struct pet_stmt *stmt;
86 isl_ctx *ctx;
87 isl_space *dim;
88 isl_set *dom;
89 isl_map *sched;
90 isl_multi_pw_aff *add_name;
91 char name[50];
93 if (!loc || !expr)
94 goto error;
96 ctx = pet_expr_get_ctx(expr);
97 stmt = isl_calloc_type(ctx, struct pet_stmt);
98 if (!stmt)
99 goto error;
101 dim = isl_space_set_alloc(ctx, 0, 0);
102 if (label)
103 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
104 else {
105 snprintf(name, sizeof(name), "S_%d", id);
106 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
108 dom = isl_set_universe(isl_space_copy(dim));
109 sched = isl_map_from_domain(isl_set_copy(dom));
111 dim = isl_space_from_domain(dim);
112 add_name = isl_multi_pw_aff_zero(dim);
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?
1035 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1037 isl_id *id;
1038 int is_virtual;
1040 if (!isl_set_has_tuple_id(extent))
1041 return 0;
1042 id = isl_set_get_tuple_id(extent);
1043 is_virtual = !isl_id_get_user(id);
1044 isl_id_free(id);
1046 return is_virtual;
1049 /* Prefix the schedule of "stmt" with an extra dimension with constant
1050 * value "pos".
1052 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1054 if (!stmt)
1055 return NULL;
1057 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1058 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1059 if (!stmt->schedule)
1060 return pet_stmt_free(stmt);
1062 return stmt;
1065 /* Prefix the schedules of all statements in "scop" with an extra
1066 * dimension with constant value "pos".
1068 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1070 int i;
1072 if (!scop)
1073 return NULL;
1075 for (i = 0; i < scop->n_stmt; ++i) {
1076 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1077 if (!scop->stmts[i])
1078 return pet_scop_free(scop);
1081 return scop;
1084 /* Given a set with a parameter at "param_pos" that refers to the
1085 * iterator, "move" the iterator to the first set dimension.
1086 * That is, essentially equate the parameter to the first set dimension
1087 * and then project it out.
1089 * The first set dimension may however refer to a virtual iterator,
1090 * while the parameter refers to the "real" iterator.
1091 * We therefore need to take into account the affine expression "iv_map", which
1092 * expresses the real iterator in terms of the virtual iterator.
1093 * In particular, we equate the set dimension to the input of the map
1094 * and the parameter to the output of the map and then project out
1095 * everything we don't need anymore.
1097 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1098 int param_pos, __isl_take isl_aff *iv_map)
1100 isl_map *map, *map2;
1101 map = isl_map_from_domain(set);
1102 map = isl_map_add_dims(map, isl_dim_out, 1);
1103 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1104 map2 = isl_map_from_aff(iv_map);
1105 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1106 map = isl_map_apply_range(map, map2);
1107 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1108 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1109 return isl_map_domain(map);
1112 /* Data used in embed_access.
1113 * extend adds an iterator to the iteration domain (through precomposition).
1114 * iv_map expresses the real iterator in terms of the virtual iterator
1115 * var_id represents the induction variable of the corresponding loop
1117 struct pet_embed_access {
1118 isl_multi_pw_aff *extend;
1119 isl_aff *iv_map;
1120 isl_id *var_id;
1123 /* Given an index expression, return an expression for the outer iterator.
1125 static __isl_give isl_aff *index_outer_iterator(
1126 __isl_take isl_multi_pw_aff *index)
1128 isl_space *space;
1129 isl_local_space *ls;
1131 space = isl_multi_pw_aff_get_domain_space(index);
1132 isl_multi_pw_aff_free(index);
1134 ls = isl_local_space_from_space(space);
1135 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1138 /* Replace an index expression that references the new (outer) iterator variable
1139 * by one that references the corresponding (real) iterator.
1141 * The input index expression is of the form
1143 * { S[i',...] -> i[] }
1145 * where i' refers to the virtual iterator.
1147 * iv_map is of the form
1149 * { [i'] -> [i] }
1151 * Return the index expression
1153 * { S[i',...] -> [i] }
1155 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1156 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1158 isl_space *space;
1159 isl_aff *aff;
1161 aff = index_outer_iterator(index);
1162 space = isl_aff_get_space(aff);
1163 iv_map = isl_aff_align_params(iv_map, space);
1164 aff = isl_aff_pullback_aff(iv_map, aff);
1166 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1169 /* Given an index expression "index" that refers to the (real) iterator
1170 * through the parameter at position "pos", plug in "iv_map", expressing
1171 * the real iterator in terms of the virtual (outer) iterator.
1173 * In particular, the index expression is of the form
1175 * [..., i, ...] -> { S[i',...] -> ... i ... }
1177 * where i refers to the real iterator and i' refers to the virtual iterator.
1179 * iv_map is of the form
1181 * { [i'] -> [i] }
1183 * Return the index expression
1185 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1188 * We first move the parameter to the input
1190 * [..., ...] -> { [i, i',...] -> ... i ... }
1192 * and construct
1194 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1196 * and then combine the two to obtain the desired result.
1198 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1199 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1201 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1202 isl_multi_aff *ma;
1204 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1205 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1206 isl_dim_param, pos, 1);
1208 space = isl_space_map_from_set(space);
1209 ma = isl_multi_aff_identity(isl_space_copy(space));
1210 iv_map = isl_aff_align_params(iv_map, space);
1211 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1212 ma = isl_multi_aff_flat_range_product(
1213 isl_multi_aff_from_aff(iv_map), ma);
1214 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1216 return index;
1219 /* Does the index expression "index" reference a virtual array, i.e.,
1220 * one with user pointer equal to NULL?
1221 * A virtual array does not have any members.
1223 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1225 isl_id *id;
1226 int is_virtual;
1228 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1229 return 0;
1230 if (isl_multi_pw_aff_range_is_wrapping(index))
1231 return 0;
1232 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1233 is_virtual = !isl_id_get_user(id);
1234 isl_id_free(id);
1236 return is_virtual;
1239 /* Does the access relation "access" reference a virtual array, i.e.,
1240 * one with user pointer equal to NULL?
1241 * A virtual array does not have any members.
1243 static int access_is_virtual_array(__isl_keep isl_map *access)
1245 isl_id *id;
1246 int is_virtual;
1248 if (!isl_map_has_tuple_id(access, isl_dim_out))
1249 return 0;
1250 if (isl_map_range_is_wrapping(access))
1251 return 0;
1252 id = isl_map_get_tuple_id(access, isl_dim_out);
1253 is_virtual = !isl_id_get_user(id);
1254 isl_id_free(id);
1256 return is_virtual;
1259 /* Embed the given index expression in an extra outer loop.
1260 * The domain of the index expression has already been updated.
1262 * If the access refers to the induction variable, then it is
1263 * turned into an access to the set of integers with index (and value)
1264 * equal to the induction variable.
1266 * If the accessed array is a virtual array (with user
1267 * pointer equal to NULL), as created by create_test_index,
1268 * then it is extended along with the domain of the index expression.
1270 static __isl_give isl_multi_pw_aff *embed_index_expression(
1271 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1273 isl_id *array_id = NULL;
1274 int pos;
1276 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1277 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1278 if (array_id == data->var_id) {
1279 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1280 } else if (index_is_virtual_array(index)) {
1281 isl_aff *aff;
1282 isl_multi_pw_aff *mpa;
1284 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1285 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1286 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1287 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1288 isl_id_copy(array_id));
1290 isl_id_free(array_id);
1292 pos = isl_multi_pw_aff_find_dim_by_id(index,
1293 isl_dim_param, data->var_id);
1294 if (pos >= 0)
1295 index = index_internalize_iv(index, pos,
1296 isl_aff_copy(data->iv_map));
1297 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1298 isl_id_copy(data->var_id));
1300 return index;
1303 /* Embed the given access relation in an extra outer loop.
1304 * The domain of the access relation has already been updated.
1306 * If the access refers to the induction variable, then it is
1307 * turned into an access to the set of integers with index (and value)
1308 * equal to the induction variable.
1310 * If the induction variable appears in the constraints (as a parameter),
1311 * then the parameter is equated to the newly introduced iteration
1312 * domain dimension and subsequently projected out.
1314 * Similarly, if the accessed array is a virtual array (with user
1315 * pointer equal to NULL), as created by create_test_index,
1316 * then it is extended along with the domain of the access.
1318 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1319 struct pet_embed_access *data)
1321 isl_id *array_id = NULL;
1322 int pos;
1324 if (isl_map_has_tuple_id(access, isl_dim_out))
1325 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1326 if (array_id == data->var_id || access_is_virtual_array(access)) {
1327 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1328 access = isl_map_equate(access,
1329 isl_dim_in, 0, isl_dim_out, 0);
1330 if (array_id == data->var_id)
1331 access = isl_map_apply_range(access,
1332 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1333 else
1334 access = isl_map_set_tuple_id(access, isl_dim_out,
1335 isl_id_copy(array_id));
1337 isl_id_free(array_id);
1339 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1340 if (pos >= 0) {
1341 isl_set *set = isl_map_wrap(access);
1342 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1343 access = isl_set_unwrap(set);
1345 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1346 isl_id_copy(data->var_id));
1348 return access;
1351 /* Given an access expression, embed the associated access relation and
1352 * index expression in an extra outer loop.
1354 * We first update the domains to insert the extra dimension and
1355 * then update the access relation and index expression to take
1356 * into account the mapping "iv_map" from virtual iterator
1357 * to real iterator.
1359 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
1361 struct pet_embed_access *data = user;
1363 expr = pet_expr_cow(expr);
1364 expr = pet_expr_access_update_domain(expr, data->extend);
1365 if (!expr)
1366 return NULL;
1368 expr->acc.access = embed_access_relation(expr->acc.access, data);
1369 expr->acc.index = embed_index_expression(expr->acc.index, data);
1370 if (!expr->acc.access || !expr->acc.index)
1371 return pet_expr_free(expr);
1373 return expr;
1376 /* Embed all access subexpressions of "expr" in an extra loop.
1377 * "extend" inserts an outer loop iterator in the iteration domains
1378 * (through precomposition).
1379 * "iv_map" expresses the real iterator in terms of the virtual iterator
1380 * "var_id" represents the induction variable.
1382 static __isl_give pet_expr *expr_embed(__isl_take pet_expr *expr,
1383 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1384 __isl_keep isl_id *var_id)
1386 struct pet_embed_access data =
1387 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1389 expr = pet_expr_map_access(expr, &embed_access, &data);
1390 isl_aff_free(iv_map);
1391 isl_multi_pw_aff_free(extend);
1392 return expr;
1395 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1396 * "dom" and schedule "sched". "var_id" represents the induction variable
1397 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1398 * That is, it expresses the iterator that some of the parameters in "stmt"
1399 * may refer to in terms of the iterator used in "dom" and
1400 * the domain of "sched".
1402 * The iteration domain and schedule of the statement are updated
1403 * according to the iteration domain and schedule of the new loop.
1404 * If stmt->domain is a wrapped map, then the iteration domain
1405 * is the domain of this map, so we need to be careful to adjust
1406 * this domain.
1408 * If the induction variable appears in the constraints (as a parameter)
1409 * of the current iteration domain or the schedule of the statement,
1410 * then the parameter is equated to the newly introduced iteration
1411 * domain dimension and subsequently projected out.
1413 * Finally, all access relations are updated based on the extra loop.
1415 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1416 __isl_take isl_set *dom, __isl_take isl_map *sched,
1417 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1419 int i;
1420 int pos;
1421 isl_id *stmt_id;
1422 isl_space *dim;
1423 isl_multi_pw_aff *extend;
1425 if (!stmt)
1426 goto error;
1428 if (isl_set_is_wrapping(stmt->domain)) {
1429 isl_map *map;
1430 isl_map *ext;
1431 isl_space *ran_dim;
1433 map = isl_set_unwrap(stmt->domain);
1434 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1435 ran_dim = isl_space_range(isl_map_get_space(map));
1436 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1437 isl_set_universe(ran_dim));
1438 map = isl_map_flat_domain_product(ext, map);
1439 map = isl_map_set_tuple_id(map, isl_dim_in,
1440 isl_id_copy(stmt_id));
1441 dim = isl_space_domain(isl_map_get_space(map));
1442 stmt->domain = isl_map_wrap(map);
1443 } else {
1444 stmt_id = isl_set_get_tuple_id(stmt->domain);
1445 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1446 stmt->domain);
1447 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1448 isl_id_copy(stmt_id));
1449 dim = isl_set_get_space(stmt->domain);
1452 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1453 if (pos >= 0)
1454 stmt->domain = internalize_iv(stmt->domain, pos,
1455 isl_aff_copy(iv_map));
1457 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1458 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1459 isl_dim_in, stmt_id);
1461 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1462 if (pos >= 0) {
1463 isl_set *set = isl_map_wrap(stmt->schedule);
1464 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1465 stmt->schedule = isl_set_unwrap(set);
1468 dim = isl_space_map_from_set(dim);
1469 extend = isl_multi_pw_aff_identity(dim);
1470 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1471 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1472 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1473 for (i = 0; i < stmt->n_arg; ++i)
1474 stmt->args[i] = expr_embed(stmt->args[i],
1475 isl_multi_pw_aff_copy(extend),
1476 isl_aff_copy(iv_map), var_id);
1477 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1479 isl_set_free(dom);
1480 isl_id_free(var_id);
1482 for (i = 0; i < stmt->n_arg; ++i)
1483 if (!stmt->args[i])
1484 return pet_stmt_free(stmt);
1485 if (!stmt->domain || !stmt->schedule || !stmt->body)
1486 return pet_stmt_free(stmt);
1487 return stmt;
1488 error:
1489 isl_set_free(dom);
1490 isl_map_free(sched);
1491 isl_aff_free(iv_map);
1492 isl_id_free(var_id);
1493 return NULL;
1496 /* Embed the given pet_array in an extra outer loop with iteration domain
1497 * "dom".
1498 * This embedding only has an effect on virtual arrays (those with
1499 * user pointer equal to NULL), which need to be extended along with
1500 * the iteration domain.
1502 static struct pet_array *pet_array_embed(struct pet_array *array,
1503 __isl_take isl_set *dom)
1505 isl_id *array_id = NULL;
1507 if (!array)
1508 goto error;
1509 if (!extent_is_virtual_array(array->extent)) {
1510 isl_set_free(dom);
1511 return array;
1514 array_id = isl_set_get_tuple_id(array->extent);
1515 array->extent = isl_set_flat_product(dom, array->extent);
1516 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1517 if (!array->extent)
1518 return pet_array_free(array);
1520 return array;
1521 error:
1522 isl_set_free(dom);
1523 return NULL;
1526 /* Update the context with respect to an embedding into a loop
1527 * with iteration domain "dom" and induction variable "id".
1528 * "iv_map" expresses the real iterator (parameter "id") in terms
1529 * of a possibly virtual iterator (used in "dom").
1531 * If the current context is independent of "id", we don't need
1532 * to do anything.
1533 * Otherwise, a parameter value is invalid for the embedding if
1534 * any of the corresponding iterator values is invalid.
1535 * That is, a parameter value is valid only if all the corresponding
1536 * iterator values are valid.
1537 * We therefore compute the set of parameters
1539 * forall i in dom : valid (i)
1541 * or
1543 * not exists i in dom : not valid(i)
1545 * i.e.,
1547 * not exists i in dom \ valid(i)
1549 * Before we subtract valid(i) from dom, we first need to substitute
1550 * the real iterator for the virtual iterator.
1552 * If there are any unnamed parameters in "dom", then we consider
1553 * a parameter value to be valid if it is valid for any value of those
1554 * unnamed parameters. They are therefore projected out at the end.
1556 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1557 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1558 __isl_keep isl_id *id)
1560 int pos;
1561 isl_multi_aff *ma;
1563 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1564 if (pos < 0)
1565 return context;
1567 context = isl_set_from_params(context);
1568 context = isl_set_add_dims(context, isl_dim_set, 1);
1569 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1570 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1571 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1572 context = isl_set_preimage_multi_aff(context, ma);
1573 context = isl_set_subtract(isl_set_copy(dom), context);
1574 context = isl_set_params(context);
1575 context = isl_set_complement(context);
1576 context = pet_nested_remove_from_set(context);
1577 return context;
1580 /* Update the implication with respect to an embedding into a loop
1581 * with iteration domain "dom".
1583 * Since embed_access extends virtual arrays along with the domain
1584 * of the access, we need to do the same with domain and range
1585 * of the implication. Since the original implication is only valid
1586 * within a given iteration of the loop, the extended implication
1587 * maps the extra array dimension corresponding to the extra loop
1588 * to itself.
1590 static struct pet_implication *pet_implication_embed(
1591 struct pet_implication *implication, __isl_take isl_set *dom)
1593 isl_id *id;
1594 isl_map *map;
1596 if (!implication)
1597 goto error;
1599 map = isl_set_identity(dom);
1600 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1601 map = isl_map_flat_product(map, implication->extension);
1602 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1603 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1604 implication->extension = map;
1605 if (!implication->extension)
1606 return pet_implication_free(implication);
1608 return implication;
1609 error:
1610 isl_set_free(dom);
1611 return NULL;
1614 /* Embed all statements and arrays in "scop" in an extra outer loop
1615 * with iteration domain "dom" and schedule "sched".
1616 * "id" represents the induction variable of the loop.
1617 * "iv_map" maps a possibly virtual iterator to the real iterator.
1618 * That is, it expresses the iterator that some of the parameters in "scop"
1619 * may refer to in terms of the iterator used in "dom" and
1620 * the domain of "sched".
1622 * Any skip conditions within the loop have no effect outside of the loop.
1623 * The caller is responsible for making sure skip[pet_skip_later] has been
1624 * taken into account.
1626 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1627 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
1628 __isl_take isl_id *id)
1630 int i;
1631 isl_map *sched_map;
1633 sched_map = isl_map_from_aff(sched);
1635 if (!scop)
1636 goto error;
1638 pet_scop_reset_skip(scop, pet_skip_now);
1639 pet_scop_reset_skip(scop, pet_skip_later);
1641 scop->context = context_embed(scop->context, dom, iv_map, id);
1642 if (!scop->context)
1643 goto error;
1645 for (i = 0; i < scop->n_stmt; ++i) {
1646 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1647 isl_set_copy(dom), isl_map_copy(sched_map),
1648 isl_aff_copy(iv_map), isl_id_copy(id));
1649 if (!scop->stmts[i])
1650 goto error;
1653 for (i = 0; i < scop->n_array; ++i) {
1654 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1655 isl_set_copy(dom));
1656 if (!scop->arrays[i])
1657 goto error;
1660 for (i = 0; i < scop->n_implication; ++i) {
1661 scop->implications[i] =
1662 pet_implication_embed(scop->implications[i],
1663 isl_set_copy(dom));
1664 if (!scop->implications[i])
1665 goto error;
1668 isl_set_free(dom);
1669 isl_map_free(sched_map);
1670 isl_aff_free(iv_map);
1671 isl_id_free(id);
1672 return scop;
1673 error:
1674 isl_set_free(dom);
1675 isl_map_free(sched_map);
1676 isl_aff_free(iv_map);
1677 isl_id_free(id);
1678 return pet_scop_free(scop);
1681 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1683 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1684 __isl_take isl_set *cond)
1686 if (!stmt)
1687 goto error;
1689 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1691 return stmt;
1692 error:
1693 isl_set_free(cond);
1694 return pet_stmt_free(stmt);
1697 /* Add extra conditions to scop->skip[type].
1699 * The new skip condition only holds if it held before
1700 * and the condition is true. It does not hold if it did not hold
1701 * before or the condition is false.
1703 * The skip condition is assumed to be an affine expression.
1705 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1706 enum pet_skip type, __isl_keep isl_set *cond)
1708 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1709 isl_pw_aff *skip;
1710 isl_set *dom;
1712 if (!scop)
1713 return NULL;
1714 if (!ext->skip[type])
1715 return scop;
1717 if (!multi_pw_aff_is_affine(ext->skip[type]))
1718 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1719 isl_error_internal, "can only restrict affine skips",
1720 return pet_scop_free(scop));
1722 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1723 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1724 cond = isl_set_copy(cond);
1725 cond = isl_set_from_params(cond);
1726 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1727 skip = indicator_function(cond, dom);
1728 isl_multi_pw_aff_free(ext->skip[type]);
1729 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1730 if (!ext->skip[type])
1731 return pet_scop_free(scop);
1733 return scop;
1736 /* Add extra conditions on the parameters to all iteration domains
1737 * and skip conditions.
1739 * A parameter value is valid for the result if it was valid
1740 * for the original scop and satisfies "cond" or if it does
1741 * not satisfy "cond" as in this case the scop is not executed
1742 * and the original constraints on the parameters are irrelevant.
1744 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1745 __isl_take isl_set *cond)
1747 int i;
1749 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1750 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1752 if (!scop)
1753 goto error;
1755 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1756 scop->context = isl_set_union(scop->context,
1757 isl_set_complement(isl_set_copy(cond)));
1758 scop->context = isl_set_coalesce(scop->context);
1759 scop->context = pet_nested_remove_from_set(scop->context);
1760 if (!scop->context)
1761 goto error;
1763 for (i = 0; i < scop->n_stmt; ++i) {
1764 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1765 isl_set_copy(cond));
1766 if (!scop->stmts[i])
1767 goto error;
1770 isl_set_free(cond);
1771 return scop;
1772 error:
1773 isl_set_free(cond);
1774 return pet_scop_free(scop);
1777 /* Insert an argument expression corresponding to "test" in front
1778 * of the list of arguments described by *n_arg and *args.
1780 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1781 __isl_keep isl_multi_pw_aff *test)
1783 int i;
1784 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1786 if (!test)
1787 return -1;
1789 if (!*args) {
1790 *args = isl_calloc_array(ctx, pet_expr *, 1);
1791 if (!*args)
1792 return -1;
1793 } else {
1794 pet_expr **ext;
1795 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1796 if (!ext)
1797 return -1;
1798 for (i = 0; i < *n_arg; ++i)
1799 ext[1 + i] = (*args)[i];
1800 free(*args);
1801 *args = ext;
1803 (*n_arg)++;
1804 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1805 if (!(*args)[0])
1806 return -1;
1808 return 0;
1811 /* Look through the applications in "scop" for any that can be
1812 * applied to the filter expressed by "map" and "satisified".
1813 * If there is any, then apply it to "map" and return the result.
1814 * Otherwise, return "map".
1815 * "id" is the identifier of the virtual array.
1817 * We only introduce at most one implication for any given virtual array,
1818 * so we can apply the implication and return as soon as we find one.
1820 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1821 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1823 int i;
1825 for (i = 0; i < scop->n_implication; ++i) {
1826 struct pet_implication *pi = scop->implications[i];
1827 isl_id *pi_id;
1829 if (pi->satisfied != satisfied)
1830 continue;
1831 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1832 isl_id_free(pi_id);
1833 if (pi_id != id)
1834 continue;
1836 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1839 return map;
1842 /* Is the filter expressed by "test" and "satisfied" implied
1843 * by filter "pos" on "domain", with filter "expr", taking into
1844 * account the implications of "scop"?
1846 * For filter on domain implying that expressed by "test" and "satisfied",
1847 * the filter needs to be an access to the same (virtual) array as "test" and
1848 * the filter value needs to be equal to "satisfied".
1849 * Moreover, the filter access relation, possibly extended by
1850 * the implications in "scop" needs to contain "test".
1852 static int implies_filter(struct pet_scop *scop,
1853 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1854 __isl_keep isl_map *test, int satisfied)
1856 isl_id *test_id, *arg_id;
1857 isl_val *val;
1858 int is_int;
1859 int s;
1860 int is_subset;
1861 isl_map *implied;
1863 if (expr->type != pet_expr_access)
1864 return 0;
1865 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1866 arg_id = pet_expr_access_get_id(expr);
1867 isl_id_free(arg_id);
1868 isl_id_free(test_id);
1869 if (test_id != arg_id)
1870 return 0;
1871 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1872 is_int = isl_val_is_int(val);
1873 if (is_int)
1874 s = isl_val_get_num_si(val);
1875 isl_val_free(val);
1876 if (!val)
1877 return -1;
1878 if (!is_int)
1879 return 0;
1880 if (s != satisfied)
1881 return 0;
1883 implied = isl_map_copy(expr->acc.access);
1884 implied = apply_implications(scop, implied, test_id, satisfied);
1885 is_subset = isl_map_is_subset(test, implied);
1886 isl_map_free(implied);
1888 return is_subset;
1891 /* Is the filter expressed by "test" and "satisfied" implied
1892 * by any of the filters on the domain of "stmt", taking into
1893 * account the implications of "scop"?
1895 static int filter_implied(struct pet_scop *scop,
1896 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1898 int i;
1899 int implied;
1900 isl_id *test_id;
1901 isl_map *domain;
1902 isl_map *test_map;
1904 if (!scop || !stmt || !test)
1905 return -1;
1906 if (scop->n_implication == 0)
1907 return 0;
1908 if (stmt->n_arg == 0)
1909 return 0;
1911 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1912 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1914 implied = 0;
1915 for (i = 0; i < stmt->n_arg; ++i) {
1916 implied = implies_filter(scop, domain, i, stmt->args[i],
1917 test_map, satisfied);
1918 if (implied < 0 || implied)
1919 break;
1922 isl_map_free(test_map);
1923 isl_map_free(domain);
1924 return implied;
1927 /* Make the statement "stmt" depend on the value of "test"
1928 * being equal to "satisfied" by adjusting stmt->domain.
1930 * The domain of "test" corresponds to the (zero or more) outer dimensions
1931 * of the iteration domain.
1933 * We first extend "test" to apply to the entire iteration domain and
1934 * then check if the filter that we are about to add is implied
1935 * by any of the current filters, possibly taking into account
1936 * the implications in "scop". If so, we leave "stmt" untouched and return.
1938 * Otherwise, we insert an argument corresponding to a read to "test"
1939 * from the iteration domain of "stmt" in front of the list of arguments.
1940 * We also insert a corresponding output dimension in the wrapped
1941 * map contained in stmt->domain, with value set to "satisfied".
1943 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1944 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1946 int i;
1947 int implied;
1948 isl_id *id;
1949 isl_ctx *ctx;
1950 isl_pw_multi_aff *pma;
1951 isl_multi_aff *add_dom;
1952 isl_space *space;
1953 isl_local_space *ls;
1954 int n_test_dom;
1956 if (!stmt || !test)
1957 goto error;
1959 space = pet_stmt_get_space(stmt);
1960 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1961 space = isl_space_from_domain(space);
1962 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1963 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1964 ls = isl_local_space_from_space(isl_space_domain(space));
1965 for (i = 0; i < n_test_dom; ++i) {
1966 isl_aff *aff;
1967 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1968 isl_dim_set, i);
1969 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1971 isl_local_space_free(ls);
1972 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1974 implied = filter_implied(scop, stmt, test, satisfied);
1975 if (implied < 0)
1976 goto error;
1977 if (implied) {
1978 isl_multi_pw_aff_free(test);
1979 return stmt;
1982 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1983 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1984 id, satisfied);
1985 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1987 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1988 goto error;
1990 isl_multi_pw_aff_free(test);
1991 return stmt;
1992 error:
1993 isl_multi_pw_aff_free(test);
1994 return pet_stmt_free(stmt);
1997 /* Does "scop" have a skip condition of the given "type"?
1999 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2001 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2003 if (!scop)
2004 return -1;
2005 return ext->skip[type] != NULL;
2008 /* Does "scop" have a skip condition of the given "type" that
2009 * is an affine expression?
2011 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2013 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2015 if (!scop)
2016 return -1;
2017 if (!ext->skip[type])
2018 return 0;
2019 return multi_pw_aff_is_affine(ext->skip[type]);
2022 /* Does "scop" have a skip condition of the given "type" that
2023 * is not an affine expression?
2025 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2027 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2028 int aff;
2030 if (!scop)
2031 return -1;
2032 if (!ext->skip[type])
2033 return 0;
2034 aff = multi_pw_aff_is_affine(ext->skip[type]);
2035 if (aff < 0)
2036 return -1;
2037 return !aff;
2040 /* Does "scop" have a skip condition of the given "type" that
2041 * is affine and holds on the entire domain?
2043 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2045 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2046 isl_pw_aff *pa;
2047 isl_set *set;
2048 int is_aff;
2049 int is_univ;
2051 is_aff = pet_scop_has_affine_skip(scop, type);
2052 if (is_aff < 0 || !is_aff)
2053 return is_aff;
2055 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2056 set = isl_pw_aff_non_zero_set(pa);
2057 is_univ = isl_set_plain_is_universe(set);
2058 isl_set_free(set);
2060 return is_univ;
2063 /* Replace scop->skip[type] by "skip".
2065 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2066 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2068 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2070 if (!scop || !skip)
2071 goto error;
2073 isl_multi_pw_aff_free(ext->skip[type]);
2074 ext->skip[type] = skip;
2076 return scop;
2077 error:
2078 isl_multi_pw_aff_free(skip);
2079 return pet_scop_free(scop);
2082 /* Return a copy of scop->skip[type].
2084 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2085 enum pet_skip type)
2087 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2089 if (!scop)
2090 return NULL;
2092 return isl_multi_pw_aff_copy(ext->skip[type]);
2095 /* Assuming scop->skip[type] is an affine expression,
2096 * return the constraints on the parameters for which the skip condition
2097 * holds.
2099 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2100 enum pet_skip type)
2102 isl_multi_pw_aff *skip;
2103 isl_pw_aff *pa;
2105 skip = pet_scop_get_skip(scop, type);
2106 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2107 isl_multi_pw_aff_free(skip);
2108 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2111 /* Return the identifier of the variable that is accessed by
2112 * the skip condition of the given type.
2114 * The skip condition is assumed not to be an affine condition.
2116 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2117 enum pet_skip type)
2119 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2121 if (!scop)
2122 return NULL;
2124 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2127 /* Return an access pet_expr corresponding to the skip condition
2128 * of the given type.
2130 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2131 enum pet_skip type)
2133 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2136 /* Drop the the skip condition scop->skip[type].
2138 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2140 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2142 if (!scop)
2143 return;
2145 isl_multi_pw_aff_free(ext->skip[type]);
2146 ext->skip[type] = NULL;
2149 /* Make the skip condition (if any) depend on the value of "test" being
2150 * equal to "satisfied".
2152 * We only support the case where the original skip condition is universal,
2153 * i.e., where skipping is unconditional, and where satisfied == 1.
2154 * In this case, the skip condition is changed to skip only when
2155 * "test" is equal to one.
2157 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2158 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2160 int is_univ = 0;
2162 if (!scop)
2163 return NULL;
2164 if (!pet_scop_has_skip(scop, type))
2165 return scop;
2167 if (satisfied)
2168 is_univ = pet_scop_has_universal_skip(scop, type);
2169 if (is_univ < 0)
2170 return pet_scop_free(scop);
2171 if (satisfied && is_univ) {
2172 isl_multi_pw_aff *skip;
2173 skip = isl_multi_pw_aff_copy(test);
2174 scop = pet_scop_set_skip(scop, type, skip);
2175 if (!scop)
2176 return NULL;
2177 } else {
2178 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2179 "skip expression cannot be filtered",
2180 return pet_scop_free(scop));
2183 return scop;
2186 /* Make all statements in "scop" depend on the value of "test"
2187 * being equal to "satisfied" by adjusting their domains.
2189 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2190 __isl_take isl_multi_pw_aff *test, int satisfied)
2192 int i;
2194 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2195 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2197 if (!scop || !test)
2198 goto error;
2200 for (i = 0; i < scop->n_stmt; ++i) {
2201 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2202 isl_multi_pw_aff_copy(test), satisfied);
2203 if (!scop->stmts[i])
2204 goto error;
2207 isl_multi_pw_aff_free(test);
2208 return scop;
2209 error:
2210 isl_multi_pw_aff_free(test);
2211 return pet_scop_free(scop);
2214 /* Add all parameters in "expr" to "space" and return the result.
2216 static __isl_give isl_space *expr_collect_params(__isl_keep pet_expr *expr,
2217 __isl_take isl_space *space)
2219 int i;
2221 if (!expr)
2222 goto error;
2223 for (i = 0; i < expr->n_arg; ++i)
2224 space = expr_collect_params(expr->args[i], space);
2226 if (expr->type == pet_expr_access)
2227 space = isl_space_align_params(space,
2228 isl_map_get_space(expr->acc.access));
2230 return space;
2231 error:
2232 pet_expr_free(expr);
2233 return isl_space_free(space);
2236 /* Add all parameters in "stmt" to "space" and return the result.
2238 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2239 __isl_take isl_space *space)
2241 int i;
2243 if (!stmt)
2244 return isl_space_free(space);
2246 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2247 space = isl_space_align_params(space,
2248 isl_map_get_space(stmt->schedule));
2249 for (i = 0; i < stmt->n_arg; ++i)
2250 space = expr_collect_params(stmt->args[i], space);
2251 space = expr_collect_params(stmt->body, space);
2253 return space;
2256 /* Add all parameters in "array" to "space" and return the result.
2258 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2259 __isl_take isl_space *space)
2261 if (!array)
2262 return isl_space_free(space);
2264 space = isl_space_align_params(space,
2265 isl_set_get_space(array->context));
2266 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2268 return space;
2271 /* Add all parameters in "scop" to "space" and return the result.
2273 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2274 __isl_take isl_space *space)
2276 int i;
2278 if (!scop)
2279 return isl_space_free(space);
2281 for (i = 0; i < scop->n_array; ++i)
2282 space = array_collect_params(scop->arrays[i], space);
2284 for (i = 0; i < scop->n_stmt; ++i)
2285 space = stmt_collect_params(scop->stmts[i], space);
2287 return space;
2290 /* Add all parameters in "space" to the domain, schedule and
2291 * all access relations in "stmt".
2293 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2294 __isl_take isl_space *space)
2296 int i;
2298 if (!stmt)
2299 goto error;
2301 stmt->domain = isl_set_align_params(stmt->domain,
2302 isl_space_copy(space));
2303 stmt->schedule = isl_map_align_params(stmt->schedule,
2304 isl_space_copy(space));
2306 for (i = 0; i < stmt->n_arg; ++i) {
2307 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2308 isl_space_copy(space));
2309 if (!stmt->args[i])
2310 goto error;
2312 stmt->body = pet_expr_align_params(stmt->body, isl_space_copy(space));
2314 if (!stmt->domain || !stmt->schedule || !stmt->body)
2315 goto error;
2317 isl_space_free(space);
2318 return stmt;
2319 error:
2320 isl_space_free(space);
2321 return pet_stmt_free(stmt);
2324 /* Add all parameters in "space" to "array".
2326 static struct pet_array *array_propagate_params(struct pet_array *array,
2327 __isl_take isl_space *space)
2329 if (!array)
2330 goto error;
2332 array->context = isl_set_align_params(array->context,
2333 isl_space_copy(space));
2334 array->extent = isl_set_align_params(array->extent,
2335 isl_space_copy(space));
2336 if (array->value_bounds) {
2337 array->value_bounds = isl_set_align_params(array->value_bounds,
2338 isl_space_copy(space));
2339 if (!array->value_bounds)
2340 goto error;
2343 if (!array->context || !array->extent)
2344 goto error;
2346 isl_space_free(space);
2347 return array;
2348 error:
2349 isl_space_free(space);
2350 return pet_array_free(array);
2353 /* Add all parameters in "space" to "scop".
2355 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2356 __isl_take isl_space *space)
2358 int i;
2360 if (!scop)
2361 goto error;
2363 for (i = 0; i < scop->n_array; ++i) {
2364 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2365 isl_space_copy(space));
2366 if (!scop->arrays[i])
2367 goto error;
2370 for (i = 0; i < scop->n_stmt; ++i) {
2371 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2372 isl_space_copy(space));
2373 if (!scop->stmts[i])
2374 goto error;
2377 isl_space_free(space);
2378 return scop;
2379 error:
2380 isl_space_free(space);
2381 return pet_scop_free(scop);
2384 /* Update all isl_sets and isl_maps in "scop" such that they all
2385 * have the same parameters.
2387 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2389 isl_space *space;
2391 if (!scop)
2392 return NULL;
2394 space = isl_set_get_space(scop->context);
2395 space = scop_collect_params(scop, space);
2397 scop->context = isl_set_align_params(scop->context,
2398 isl_space_copy(space));
2399 scop = scop_propagate_params(scop, space);
2401 if (scop && !scop->context)
2402 return pet_scop_free(scop);
2404 return scop;
2407 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2408 * in "space" by a value equal to the corresponding parameter.
2410 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2411 __isl_take isl_space *space)
2413 if (!stmt)
2414 goto error;
2416 stmt->body = pet_expr_detect_parameter_accesses(stmt->body,
2417 isl_space_copy(space));
2419 if (!stmt->domain || !stmt->schedule || !stmt->body)
2420 goto error;
2422 isl_space_free(space);
2423 return stmt;
2424 error:
2425 isl_space_free(space);
2426 return pet_stmt_free(stmt);
2429 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2430 * in "space" by a value equal to the corresponding parameter.
2432 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2433 __isl_take isl_space *space)
2435 int i;
2437 if (!scop)
2438 goto error;
2440 for (i = 0; i < scop->n_stmt; ++i) {
2441 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2442 isl_space_copy(space));
2443 if (!scop->stmts[i])
2444 goto error;
2447 isl_space_free(space);
2448 return scop;
2449 error:
2450 isl_space_free(space);
2451 return pet_scop_free(scop);
2454 /* Replace all accesses to (0D) arrays that correspond to any of
2455 * the parameters used in "scop" by a value equal
2456 * to the corresponding parameter.
2458 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2460 isl_space *space;
2462 if (!scop)
2463 return NULL;
2465 space = isl_set_get_space(scop->context);
2466 space = scop_collect_params(scop, space);
2468 scop = scop_detect_parameter_accesses(scop, space);
2470 return scop;
2473 /* Add the access relation of the access expression "expr" to "accesses" and
2474 * return the result.
2475 * The domain of the access relation is intersected with "domain".
2476 * If "tag" is set, then the access relation is tagged with
2477 * the corresponding reference identifier.
2479 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2480 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2482 isl_map *access;
2484 access = pet_expr_access_get_may_access(expr);
2485 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2486 if (tag)
2487 access = pet_expr_tag_access(expr, access);
2488 return isl_union_map_add_map(accesses, access);
2491 /* Add all read access relations (if "read" is set) and/or all write
2492 * access relations (if "write" is set) to "accesses" and return the result.
2493 * The domains of the access relations are intersected with "domain".
2494 * If "tag" is set, then the access relations are tagged with
2495 * the corresponding reference identifiers.
2497 * If "must" is set, then we only add the accesses that are definitely
2498 * performed. Otherwise, we add all potential accesses.
2499 * In particular, if the access has any arguments, then if "must" is
2500 * set we currently skip the access completely. If "must" is not set,
2501 * we project out the values of the access arguments.
2503 static __isl_give isl_union_map *expr_collect_accesses(
2504 __isl_keep pet_expr *expr, int read, int write, int must, int tag,
2505 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2507 int i;
2508 isl_id *id;
2509 isl_space *dim;
2511 if (!expr)
2512 return isl_union_map_free(accesses);
2514 for (i = 0; i < expr->n_arg; ++i)
2515 accesses = expr_collect_accesses(expr->args[i],
2516 read, write, must, tag, accesses, domain);
2518 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2519 ((read && expr->acc.read) || (write && expr->acc.write)) &&
2520 (!must || expr->n_arg == 0)) {
2521 accesses = expr_collect_access(expr, tag, accesses, domain);
2524 return accesses;
2527 /* Collect and return all read access relations (if "read" is set)
2528 * and/or all write access relations (if "write" is set) in "stmt".
2529 * If "tag" is set, then the access relations are tagged with
2530 * the corresponding reference identifiers.
2531 * If "kill" is set, then "stmt" is a kill statement and we simply
2532 * add the argument of the kill operation.
2534 * If "must" is set, then we only add the accesses that are definitely
2535 * performed. Otherwise, we add all potential accesses.
2536 * In particular, if the statement has any arguments, then if "must" is
2537 * set we currently skip the statement completely. If "must" is not set,
2538 * we project out the values of the statement arguments.
2540 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2541 int read, int write, int kill, int must, int tag,
2542 __isl_take isl_space *dim)
2544 isl_union_map *accesses;
2545 isl_set *domain;
2547 if (!stmt)
2548 return NULL;
2550 accesses = isl_union_map_empty(dim);
2552 if (must && stmt->n_arg > 0)
2553 return accesses;
2555 domain = isl_set_copy(stmt->domain);
2556 if (isl_set_is_wrapping(domain))
2557 domain = isl_map_domain(isl_set_unwrap(domain));
2559 if (kill)
2560 accesses = expr_collect_access(stmt->body->args[0], tag,
2561 accesses, domain);
2562 else
2563 accesses = expr_collect_accesses(stmt->body, read, write,
2564 must, tag, accesses, domain);
2565 isl_set_free(domain);
2567 return accesses;
2570 /* Is "stmt" an assignment statement?
2572 int pet_stmt_is_assign(struct pet_stmt *stmt)
2574 if (!stmt)
2575 return 0;
2576 if (stmt->body->type != pet_expr_op)
2577 return 0;
2578 return stmt->body->op == pet_op_assign;
2581 /* Is "stmt" a kill statement?
2583 int pet_stmt_is_kill(struct pet_stmt *stmt)
2585 if (!stmt)
2586 return 0;
2587 if (stmt->body->type != pet_expr_op)
2588 return 0;
2589 return stmt->body->op == pet_op_kill;
2592 /* Is "stmt" an assume statement?
2594 int pet_stmt_is_assume(struct pet_stmt *stmt)
2596 if (!stmt)
2597 return 0;
2598 return pet_expr_is_assume(stmt->body);
2601 /* Compute a mapping from all arrays (of structs) in scop
2602 * to their innermost arrays.
2604 * In particular, for each array of a primitive type, the result
2605 * contains the identity mapping on that array.
2606 * For each array involving member accesses, the result
2607 * contains a mapping from the elements of any intermediate array of structs
2608 * to all corresponding elements of the innermost nested arrays.
2610 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2612 int i;
2613 isl_union_map *to_inner;
2615 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2617 for (i = 0; i < scop->n_array; ++i) {
2618 struct pet_array *array = scop->arrays[i];
2619 isl_set *set;
2620 isl_map *map, *gist;
2622 if (array->element_is_record)
2623 continue;
2625 map = isl_set_identity(isl_set_copy(array->extent));
2627 set = isl_map_domain(isl_map_copy(map));
2628 gist = isl_map_copy(map);
2629 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2630 to_inner = isl_union_map_add_map(to_inner, gist);
2632 while (set && isl_set_is_wrapping(set)) {
2633 isl_id *id;
2634 isl_map *wrapped;
2636 id = isl_set_get_tuple_id(set);
2637 wrapped = isl_set_unwrap(set);
2638 wrapped = isl_map_domain_map(wrapped);
2639 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2640 map = isl_map_apply_domain(map, wrapped);
2641 set = isl_map_domain(isl_map_copy(map));
2642 gist = isl_map_copy(map);
2643 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2644 to_inner = isl_union_map_add_map(to_inner, gist);
2647 isl_set_free(set);
2648 isl_map_free(map);
2651 return to_inner;
2654 /* Collect and return all read access relations (if "read" is set)
2655 * and/or all write access relations (if "write" is set) in "scop".
2656 * If "kill" is set, then we only add the arguments of kill operations.
2657 * If "must" is set, then we only add the accesses that are definitely
2658 * performed. Otherwise, we add all potential accesses.
2659 * If "tag" is set, then the access relations are tagged with
2660 * the corresponding reference identifiers.
2661 * For accesses to structures, the returned access relation accesses
2662 * all individual fields in the structures.
2664 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2665 int read, int write, int kill, int must, int tag)
2667 int i;
2668 isl_union_map *accesses;
2669 isl_union_set *arrays;
2670 isl_union_map *to_inner;
2672 if (!scop)
2673 return NULL;
2675 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2677 for (i = 0; i < scop->n_stmt; ++i) {
2678 struct pet_stmt *stmt = scop->stmts[i];
2679 isl_union_map *accesses_i;
2680 isl_space *space;
2682 if (kill && !pet_stmt_is_kill(stmt))
2683 continue;
2685 space = isl_set_get_space(scop->context);
2686 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2687 must, tag, space);
2688 accesses = isl_union_map_union(accesses, accesses_i);
2691 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2692 for (i = 0; i < scop->n_array; ++i) {
2693 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2694 arrays = isl_union_set_add_set(arrays, extent);
2696 accesses = isl_union_map_intersect_range(accesses, arrays);
2698 to_inner = compute_to_inner(scop);
2699 accesses = isl_union_map_apply_range(accesses, to_inner);
2701 return accesses;
2704 /* Collect all potential read access relations.
2706 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2708 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2711 /* Collect all potential write access relations.
2713 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2715 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2718 /* Collect all definite write access relations.
2720 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2722 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2725 /* Collect all definite kill access relations.
2727 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2729 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2732 /* Collect all tagged potential read access relations.
2734 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2735 struct pet_scop *scop)
2737 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2740 /* Collect all tagged potential write access relations.
2742 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2743 struct pet_scop *scop)
2745 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2748 /* Collect all tagged definite write access relations.
2750 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2751 struct pet_scop *scop)
2753 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2756 /* Collect all tagged definite kill access relations.
2758 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2759 struct pet_scop *scop)
2761 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2764 /* Collect and return the union of iteration domains in "scop".
2766 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2768 int i;
2769 isl_set *domain_i;
2770 isl_union_set *domain;
2772 if (!scop)
2773 return NULL;
2775 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2777 for (i = 0; i < scop->n_stmt; ++i) {
2778 domain_i = isl_set_copy(scop->stmts[i]->domain);
2779 domain = isl_union_set_add_set(domain, domain_i);
2782 return domain;
2785 /* Collect and return the schedules of the statements in "scop".
2786 * The range is normalized to the maximal number of scheduling
2787 * dimensions.
2789 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2791 int i, j;
2792 isl_map *schedule_i;
2793 isl_union_map *schedule;
2794 int depth, max_depth = 0;
2796 if (!scop)
2797 return NULL;
2799 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2801 for (i = 0; i < scop->n_stmt; ++i) {
2802 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2803 if (depth > max_depth)
2804 max_depth = depth;
2807 for (i = 0; i < scop->n_stmt; ++i) {
2808 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2809 depth = isl_map_dim(schedule_i, isl_dim_out);
2810 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2811 max_depth - depth);
2812 for (j = depth; j < max_depth; ++j)
2813 schedule_i = isl_map_fix_si(schedule_i,
2814 isl_dim_out, j, 0);
2815 schedule = isl_union_map_add_map(schedule, schedule_i);
2818 return schedule;
2821 /* Add a reference identifier to all access expressions in "stmt".
2822 * "n_ref" points to an integer that contains the sequence number
2823 * of the next reference.
2825 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2827 int i;
2829 if (!stmt)
2830 return NULL;
2832 for (i = 0; i < stmt->n_arg; ++i) {
2833 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2834 if (!stmt->args[i])
2835 return pet_stmt_free(stmt);
2838 stmt->body = pet_expr_add_ref_ids(stmt->body, n_ref);
2839 if (!stmt->body)
2840 return pet_stmt_free(stmt);
2842 return stmt;
2845 /* Add a reference identifier to all access expressions in "scop".
2847 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2849 int i;
2850 int n_ref;
2852 if (!scop)
2853 return NULL;
2855 n_ref = 0;
2856 for (i = 0; i < scop->n_stmt; ++i) {
2857 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2858 if (!scop->stmts[i])
2859 return pet_scop_free(scop);
2862 return scop;
2865 /* Reset the user pointer on all parameter ids in "array".
2867 static struct pet_array *array_anonymize(struct pet_array *array)
2869 if (!array)
2870 return NULL;
2872 array->context = isl_set_reset_user(array->context);
2873 array->extent = isl_set_reset_user(array->extent);
2874 if (!array->context || !array->extent)
2875 return pet_array_free(array);
2877 return array;
2880 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2882 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2884 int i;
2885 isl_space *space;
2886 isl_set *domain;
2888 if (!stmt)
2889 return NULL;
2891 stmt->domain = isl_set_reset_user(stmt->domain);
2892 stmt->schedule = isl_map_reset_user(stmt->schedule);
2893 if (!stmt->domain || !stmt->schedule)
2894 return pet_stmt_free(stmt);
2896 for (i = 0; i < stmt->n_arg; ++i) {
2897 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2898 if (!stmt->args[i])
2899 return pet_stmt_free(stmt);
2902 stmt->body = pet_expr_anonymize(stmt->body);
2903 if (!stmt->body)
2904 return pet_stmt_free(stmt);
2906 return stmt;
2909 /* Reset the user pointer on the tuple ids and all parameter ids
2910 * in "implication".
2912 static struct pet_implication *implication_anonymize(
2913 struct pet_implication *implication)
2915 if (!implication)
2916 return NULL;
2918 implication->extension = isl_map_reset_user(implication->extension);
2919 if (!implication->extension)
2920 return pet_implication_free(implication);
2922 return implication;
2925 /* Reset the user pointer on all parameter and tuple ids in "scop".
2927 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2929 int i;
2931 if (!scop)
2932 return NULL;
2934 scop->context = isl_set_reset_user(scop->context);
2935 scop->context_value = isl_set_reset_user(scop->context_value);
2936 if (!scop->context || !scop->context_value)
2937 return pet_scop_free(scop);
2939 for (i = 0; i < scop->n_array; ++i) {
2940 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2941 if (!scop->arrays[i])
2942 return pet_scop_free(scop);
2945 for (i = 0; i < scop->n_stmt; ++i) {
2946 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2947 if (!scop->stmts[i])
2948 return pet_scop_free(scop);
2951 for (i = 0; i < scop->n_implication; ++i) {
2952 scop->implications[i] =
2953 implication_anonymize(scop->implications[i]);
2954 if (!scop->implications[i])
2955 return pet_scop_free(scop);
2958 return scop;
2961 /* Compute the gist of the iteration domain and all access relations
2962 * of "stmt" based on the constraints on the parameters specified by "context"
2963 * and the constraints on the values of nested accesses specified
2964 * by "value_bounds".
2966 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2967 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2969 int i;
2970 isl_set *domain;
2972 if (!stmt)
2973 return NULL;
2975 domain = isl_set_copy(stmt->domain);
2976 if (stmt->n_arg > 0)
2977 domain = isl_map_domain(isl_set_unwrap(domain));
2979 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2981 for (i = 0; i < stmt->n_arg; ++i) {
2982 stmt->args[i] = pet_expr_gist(stmt->args[i],
2983 domain, value_bounds);
2984 if (!stmt->args[i])
2985 goto error;
2988 stmt->body = pet_expr_gist(stmt->body, domain, value_bounds);
2989 if (!stmt->body)
2990 goto error;
2992 isl_set_free(domain);
2994 domain = isl_set_universe(pet_stmt_get_space(stmt));
2995 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2996 if (stmt->n_arg > 0)
2997 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
2998 value_bounds);
2999 stmt->domain = isl_set_gist(stmt->domain, domain);
3000 if (!stmt->domain)
3001 return pet_stmt_free(stmt);
3003 return stmt;
3004 error:
3005 isl_set_free(domain);
3006 return pet_stmt_free(stmt);
3009 /* Compute the gist of the extent of the array
3010 * based on the constraints on the parameters specified by "context".
3012 static struct pet_array *array_gist(struct pet_array *array,
3013 __isl_keep isl_set *context)
3015 if (!array)
3016 return NULL;
3018 array->extent = isl_set_gist_params(array->extent,
3019 isl_set_copy(context));
3020 if (!array->extent)
3021 return pet_array_free(array);
3023 return array;
3026 /* Compute the gist of all sets and relations in "scop"
3027 * based on the constraints on the parameters specified by "scop->context"
3028 * and the constraints on the values of nested accesses specified
3029 * by "value_bounds".
3031 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3032 __isl_keep isl_union_map *value_bounds)
3034 int i;
3036 if (!scop)
3037 return NULL;
3039 scop->context = isl_set_coalesce(scop->context);
3040 if (!scop->context)
3041 return pet_scop_free(scop);
3043 for (i = 0; i < scop->n_array; ++i) {
3044 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3045 if (!scop->arrays[i])
3046 return pet_scop_free(scop);
3049 for (i = 0; i < scop->n_stmt; ++i) {
3050 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3051 value_bounds);
3052 if (!scop->stmts[i])
3053 return pet_scop_free(scop);
3056 return scop;
3059 /* Intersect the context of "scop" with "context".
3060 * To ensure that we don't introduce any unnamed parameters in
3061 * the context of "scop", we first remove the unnamed parameters
3062 * from "context".
3064 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3065 __isl_take isl_set *context)
3067 if (!scop)
3068 goto error;
3070 context = pet_nested_remove_from_set(context);
3071 scop->context = isl_set_intersect(scop->context, context);
3072 if (!scop->context)
3073 return pet_scop_free(scop);
3075 return scop;
3076 error:
3077 isl_set_free(context);
3078 return pet_scop_free(scop);
3081 /* Drop the current context of "scop". That is, replace the context
3082 * by a universal set.
3084 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3086 isl_space *space;
3088 if (!scop)
3089 return NULL;
3091 space = isl_set_get_space(scop->context);
3092 isl_set_free(scop->context);
3093 scop->context = isl_set_universe(space);
3094 if (!scop->context)
3095 return pet_scop_free(scop);
3097 return scop;
3100 /* Append "array" to the arrays of "scop".
3102 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3103 struct pet_array *array)
3105 isl_ctx *ctx;
3106 struct pet_array **arrays;
3108 if (!array || !scop)
3109 goto error;
3111 ctx = isl_set_get_ctx(scop->context);
3112 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3113 scop->n_array + 1);
3114 if (!arrays)
3115 goto error;
3116 scop->arrays = arrays;
3117 scop->arrays[scop->n_array] = array;
3118 scop->n_array++;
3120 return scop;
3121 error:
3122 pet_array_free(array);
3123 return pet_scop_free(scop);
3126 /* Create an index expression for an access to a virtual array
3127 * representing the result of a condition.
3128 * Unlike other accessed data, the id of the array is NULL as
3129 * there is no ValueDecl in the program corresponding to the virtual
3130 * array.
3131 * The array starts out as a scalar, but grows along with the
3132 * statement writing to the array in pet_scop_embed.
3134 __isl_give isl_multi_pw_aff *pet_create_test_index(isl_ctx *ctx, int test_nr)
3136 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
3137 isl_id *id;
3138 char name[50];
3140 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3141 id = isl_id_alloc(ctx, name, NULL);
3142 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
3143 return isl_multi_pw_aff_zero(dim);
3146 /* Add an array with the given extent (range of "index") to the list
3147 * of arrays in "scop" and return the extended pet_scop.
3148 * "int_size" is the number of bytes needed to represent values of type "int".
3149 * The array is marked as attaining values 0 and 1 only and
3150 * as each element being assigned at most once.
3152 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3153 __isl_take isl_multi_pw_aff *index, int int_size)
3155 isl_ctx *ctx;
3156 isl_space *space;
3157 struct pet_array *array;
3158 isl_map *access;
3160 if (!scop || !index)
3161 goto error;
3163 ctx = isl_multi_pw_aff_get_ctx(index);
3164 array = isl_calloc_type(ctx, struct pet_array);
3165 if (!array)
3166 goto error;
3168 access = isl_map_from_multi_pw_aff(index);
3169 array->extent = isl_map_range(access);
3170 space = isl_space_params_alloc(ctx, 0);
3171 array->context = isl_set_universe(space);
3172 space = isl_space_set_alloc(ctx, 0, 1);
3173 array->value_bounds = isl_set_universe(space);
3174 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3175 isl_dim_set, 0, 0);
3176 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3177 isl_dim_set, 0, 1);
3178 array->element_type = strdup("int");
3179 array->element_size = int_size;
3180 array->uniquely_defined = 1;
3182 if (!array->extent || !array->context)
3183 array = pet_array_free(array);
3185 scop = pet_scop_add_array(scop, array);
3187 return scop;
3188 error:
3189 isl_multi_pw_aff_free(index);
3190 return pet_scop_free(scop);
3193 /* Create and return an implication on filter values equal to "satisfied"
3194 * with extension "map".
3196 static struct pet_implication *new_implication(__isl_take isl_map *map,
3197 int satisfied)
3199 isl_ctx *ctx;
3200 struct pet_implication *implication;
3202 if (!map)
3203 return NULL;
3204 ctx = isl_map_get_ctx(map);
3205 implication = isl_alloc_type(ctx, struct pet_implication);
3206 if (!implication)
3207 goto error;
3209 implication->extension = map;
3210 implication->satisfied = satisfied;
3212 return implication;
3213 error:
3214 isl_map_free(map);
3215 return NULL;
3218 /* Add an implication on filter values equal to "satisfied"
3219 * with extension "map" to "scop".
3221 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3222 __isl_take isl_map *map, int satisfied)
3224 isl_ctx *ctx;
3225 struct pet_implication *implication;
3226 struct pet_implication **implications;
3228 implication = new_implication(map, satisfied);
3229 if (!scop || !implication)
3230 goto error;
3232 ctx = isl_set_get_ctx(scop->context);
3233 implications = isl_realloc_array(ctx, scop->implications,
3234 struct pet_implication *,
3235 scop->n_implication + 1);
3236 if (!implications)
3237 goto error;
3238 scop->implications = implications;
3239 scop->implications[scop->n_implication] = implication;
3240 scop->n_implication++;
3242 return scop;
3243 error:
3244 pet_implication_free(implication);
3245 return pet_scop_free(scop);
3248 /* Given an access expression, check if it is data dependent.
3249 * If so, set *found and abort the search.
3251 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3253 int *found = user;
3255 if (pet_expr_get_n_arg(expr) > 0) {
3256 *found = 1;
3257 return -1;
3260 return 0;
3263 /* Does "scop" contain any data dependent accesses?
3265 * Check the body of each statement for such accesses.
3267 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3269 int i;
3270 int found = 0;
3272 if (!scop)
3273 return -1;
3275 for (i = 0; i < scop->n_stmt; ++i) {
3276 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
3277 &is_data_dependent, &found);
3278 if (r < 0 && !found)
3279 return -1;
3280 if (found)
3281 return found;
3284 return found;
3287 /* Does "scop" contain and data dependent conditions?
3289 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3291 int i;
3293 if (!scop)
3294 return -1;
3296 for (i = 0; i < scop->n_stmt; ++i)
3297 if (scop->stmts[i]->n_arg > 0)
3298 return 1;
3300 return 0;
3303 /* Keep track of the "input" file inside the (extended) "scop".
3305 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3307 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3309 if (!scop)
3310 return NULL;
3312 ext->input = input;
3314 return scop;
3317 /* Print the original code corresponding to "scop" to printer "p".
3319 * pet_scop_print_original can only be called from
3320 * a pet_transform_C_source callback. This means that the input
3321 * file is stored in the extended scop and that the printer prints
3322 * to a file.
3324 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3325 __isl_take isl_printer *p)
3327 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3328 FILE *output;
3329 unsigned start, end;
3331 if (!scop || !p)
3332 return isl_printer_free(p);
3334 if (!ext->input)
3335 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3336 "no input file stored in scop",
3337 return isl_printer_free(p));
3339 output = isl_printer_get_file(p);
3340 if (!output)
3341 return isl_printer_free(p);
3343 start = pet_loc_get_start(scop->loc);
3344 end = pet_loc_get_end(scop->loc);
3345 if (copy(ext->input, output, start, end) < 0)
3346 return isl_printer_free(p);
3348 return p;