scop.c: extent_is_virtual_array: check for members in extent
[pet.git] / scop.c
blobcc9d8a1dbab2268b3230c1a89dada8f851b69218
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?
1034 * A virtual array does not have any members.
1036 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1038 isl_id *id;
1039 int is_virtual;
1041 if (!isl_set_has_tuple_id(extent))
1042 return 0;
1043 if (isl_set_is_wrapping(extent))
1044 return 0;
1045 id = isl_set_get_tuple_id(extent);
1046 is_virtual = !isl_id_get_user(id);
1047 isl_id_free(id);
1049 return is_virtual;
1052 /* Prefix the schedule of "stmt" with an extra dimension with constant
1053 * value "pos".
1055 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1057 if (!stmt)
1058 return NULL;
1060 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1061 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1062 if (!stmt->schedule)
1063 return pet_stmt_free(stmt);
1065 return stmt;
1068 /* Prefix the schedules of all statements in "scop" with an extra
1069 * dimension with constant value "pos".
1071 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1073 int i;
1075 if (!scop)
1076 return NULL;
1078 for (i = 0; i < scop->n_stmt; ++i) {
1079 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1080 if (!scop->stmts[i])
1081 return pet_scop_free(scop);
1084 return scop;
1087 /* Given a set with a parameter at "param_pos" that refers to the
1088 * iterator, "move" the iterator to the first set dimension.
1089 * That is, essentially equate the parameter to the first set dimension
1090 * and then project it out.
1092 * The first set dimension may however refer to a virtual iterator,
1093 * while the parameter refers to the "real" iterator.
1094 * We therefore need to take into account the affine expression "iv_map", which
1095 * expresses the real iterator in terms of the virtual iterator.
1096 * In particular, we equate the set dimension to the input of the map
1097 * and the parameter to the output of the map and then project out
1098 * everything we don't need anymore.
1100 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1101 int param_pos, __isl_take isl_aff *iv_map)
1103 isl_map *map, *map2;
1104 map = isl_map_from_domain(set);
1105 map = isl_map_add_dims(map, isl_dim_out, 1);
1106 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1107 map2 = isl_map_from_aff(iv_map);
1108 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1109 map = isl_map_apply_range(map, map2);
1110 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1111 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1112 return isl_map_domain(map);
1115 /* Data used in embed_access.
1116 * extend adds an iterator to the iteration domain (through precomposition).
1117 * iv_map expresses the real iterator in terms of the virtual iterator
1118 * var_id represents the induction variable of the corresponding loop
1120 struct pet_embed_access {
1121 isl_multi_pw_aff *extend;
1122 isl_aff *iv_map;
1123 isl_id *var_id;
1126 /* Given an index expression, return an expression for the outer iterator.
1128 static __isl_give isl_aff *index_outer_iterator(
1129 __isl_take isl_multi_pw_aff *index)
1131 isl_space *space;
1132 isl_local_space *ls;
1134 space = isl_multi_pw_aff_get_domain_space(index);
1135 isl_multi_pw_aff_free(index);
1137 ls = isl_local_space_from_space(space);
1138 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1141 /* Replace an index expression that references the new (outer) iterator variable
1142 * by one that references the corresponding (real) iterator.
1144 * The input index expression is of the form
1146 * { S[i',...] -> i[] }
1148 * where i' refers to the virtual iterator.
1150 * iv_map is of the form
1152 * { [i'] -> [i] }
1154 * Return the index expression
1156 * { S[i',...] -> [i] }
1158 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1159 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1161 isl_space *space;
1162 isl_aff *aff;
1164 aff = index_outer_iterator(index);
1165 space = isl_aff_get_space(aff);
1166 iv_map = isl_aff_align_params(iv_map, space);
1167 aff = isl_aff_pullback_aff(iv_map, aff);
1169 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1172 /* Given an index expression "index" that refers to the (real) iterator
1173 * through the parameter at position "pos", plug in "iv_map", expressing
1174 * the real iterator in terms of the virtual (outer) iterator.
1176 * In particular, the index expression is of the form
1178 * [..., i, ...] -> { S[i',...] -> ... i ... }
1180 * where i refers to the real iterator and i' refers to the virtual iterator.
1182 * iv_map is of the form
1184 * { [i'] -> [i] }
1186 * Return the index expression
1188 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1191 * We first move the parameter to the input
1193 * [..., ...] -> { [i, i',...] -> ... i ... }
1195 * and construct
1197 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1199 * and then combine the two to obtain the desired result.
1201 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1202 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1204 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1205 isl_multi_aff *ma;
1207 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1208 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1209 isl_dim_param, pos, 1);
1211 space = isl_space_map_from_set(space);
1212 ma = isl_multi_aff_identity(isl_space_copy(space));
1213 iv_map = isl_aff_align_params(iv_map, space);
1214 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1215 ma = isl_multi_aff_flat_range_product(
1216 isl_multi_aff_from_aff(iv_map), ma);
1217 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1219 return index;
1222 /* Does the index expression "index" reference a virtual array, i.e.,
1223 * one with user pointer equal to NULL?
1224 * A virtual array does not have any members.
1226 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1228 isl_id *id;
1229 int is_virtual;
1231 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1232 return 0;
1233 if (isl_multi_pw_aff_range_is_wrapping(index))
1234 return 0;
1235 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1236 is_virtual = !isl_id_get_user(id);
1237 isl_id_free(id);
1239 return is_virtual;
1242 /* Does the access relation "access" reference a virtual array, i.e.,
1243 * one with user pointer equal to NULL?
1244 * A virtual array does not have any members.
1246 static int access_is_virtual_array(__isl_keep isl_map *access)
1248 isl_id *id;
1249 int is_virtual;
1251 if (!isl_map_has_tuple_id(access, isl_dim_out))
1252 return 0;
1253 if (isl_map_range_is_wrapping(access))
1254 return 0;
1255 id = isl_map_get_tuple_id(access, isl_dim_out);
1256 is_virtual = !isl_id_get_user(id);
1257 isl_id_free(id);
1259 return is_virtual;
1262 /* Embed the given index expression in an extra outer loop.
1263 * The domain of the index expression has already been updated.
1265 * If the access refers to the induction variable, then it is
1266 * turned into an access to the set of integers with index (and value)
1267 * equal to the induction variable.
1269 * If the accessed array is a virtual array (with user
1270 * pointer equal to NULL), as created by create_test_index,
1271 * then it is extended along with the domain of the index expression.
1273 static __isl_give isl_multi_pw_aff *embed_index_expression(
1274 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1276 isl_id *array_id = NULL;
1277 int pos;
1279 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1280 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1281 if (array_id == data->var_id) {
1282 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1283 } else if (index_is_virtual_array(index)) {
1284 isl_aff *aff;
1285 isl_multi_pw_aff *mpa;
1287 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1288 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1289 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1290 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1291 isl_id_copy(array_id));
1293 isl_id_free(array_id);
1295 pos = isl_multi_pw_aff_find_dim_by_id(index,
1296 isl_dim_param, data->var_id);
1297 if (pos >= 0)
1298 index = index_internalize_iv(index, pos,
1299 isl_aff_copy(data->iv_map));
1300 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1301 isl_id_copy(data->var_id));
1303 return index;
1306 /* Embed the given access relation in an extra outer loop.
1307 * The domain of the access relation has already been updated.
1309 * If the access refers to the induction variable, then it is
1310 * turned into an access to the set of integers with index (and value)
1311 * equal to the induction variable.
1313 * If the induction variable appears in the constraints (as a parameter),
1314 * then the parameter is equated to the newly introduced iteration
1315 * domain dimension and subsequently projected out.
1317 * Similarly, if the accessed array is a virtual array (with user
1318 * pointer equal to NULL), as created by create_test_index,
1319 * then it is extended along with the domain of the access.
1321 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1322 struct pet_embed_access *data)
1324 isl_id *array_id = NULL;
1325 int pos;
1327 if (isl_map_has_tuple_id(access, isl_dim_out))
1328 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1329 if (array_id == data->var_id || access_is_virtual_array(access)) {
1330 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1331 access = isl_map_equate(access,
1332 isl_dim_in, 0, isl_dim_out, 0);
1333 if (array_id == data->var_id)
1334 access = isl_map_apply_range(access,
1335 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1336 else
1337 access = isl_map_set_tuple_id(access, isl_dim_out,
1338 isl_id_copy(array_id));
1340 isl_id_free(array_id);
1342 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1343 if (pos >= 0) {
1344 isl_set *set = isl_map_wrap(access);
1345 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1346 access = isl_set_unwrap(set);
1348 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1349 isl_id_copy(data->var_id));
1351 return access;
1354 /* Given an access expression, embed the associated access relation and
1355 * index expression in an extra outer loop.
1357 * We first update the domains to insert the extra dimension and
1358 * then update the access relation and index expression to take
1359 * into account the mapping "iv_map" from virtual iterator
1360 * to real iterator.
1362 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
1364 struct pet_embed_access *data = user;
1366 expr = pet_expr_cow(expr);
1367 expr = pet_expr_access_update_domain(expr, data->extend);
1368 if (!expr)
1369 return NULL;
1371 expr->acc.access = embed_access_relation(expr->acc.access, data);
1372 expr->acc.index = embed_index_expression(expr->acc.index, data);
1373 if (!expr->acc.access || !expr->acc.index)
1374 return pet_expr_free(expr);
1376 return expr;
1379 /* Embed all access subexpressions of "expr" in an extra loop.
1380 * "extend" inserts an outer loop iterator in the iteration domains
1381 * (through precomposition).
1382 * "iv_map" expresses the real iterator in terms of the virtual iterator
1383 * "var_id" represents the induction variable.
1385 static __isl_give pet_expr *expr_embed(__isl_take pet_expr *expr,
1386 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1387 __isl_keep isl_id *var_id)
1389 struct pet_embed_access data =
1390 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1392 expr = pet_expr_map_access(expr, &embed_access, &data);
1393 isl_aff_free(iv_map);
1394 isl_multi_pw_aff_free(extend);
1395 return expr;
1398 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1399 * "dom" and schedule "sched". "var_id" represents the induction variable
1400 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1401 * That is, it expresses the iterator that some of the parameters in "stmt"
1402 * may refer to in terms of the iterator used in "dom" and
1403 * the domain of "sched".
1405 * The iteration domain and schedule of the statement are updated
1406 * according to the iteration domain and schedule of the new loop.
1407 * If stmt->domain is a wrapped map, then the iteration domain
1408 * is the domain of this map, so we need to be careful to adjust
1409 * this domain.
1411 * If the induction variable appears in the constraints (as a parameter)
1412 * of the current iteration domain or the schedule of the statement,
1413 * then the parameter is equated to the newly introduced iteration
1414 * domain dimension and subsequently projected out.
1416 * Finally, all access relations are updated based on the extra loop.
1418 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1419 __isl_take isl_set *dom, __isl_take isl_map *sched,
1420 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1422 int i;
1423 int pos;
1424 isl_id *stmt_id;
1425 isl_space *dim;
1426 isl_multi_pw_aff *extend;
1428 if (!stmt)
1429 goto error;
1431 if (isl_set_is_wrapping(stmt->domain)) {
1432 isl_map *map;
1433 isl_map *ext;
1434 isl_space *ran_dim;
1436 map = isl_set_unwrap(stmt->domain);
1437 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1438 ran_dim = isl_space_range(isl_map_get_space(map));
1439 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1440 isl_set_universe(ran_dim));
1441 map = isl_map_flat_domain_product(ext, map);
1442 map = isl_map_set_tuple_id(map, isl_dim_in,
1443 isl_id_copy(stmt_id));
1444 dim = isl_space_domain(isl_map_get_space(map));
1445 stmt->domain = isl_map_wrap(map);
1446 } else {
1447 stmt_id = isl_set_get_tuple_id(stmt->domain);
1448 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1449 stmt->domain);
1450 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1451 isl_id_copy(stmt_id));
1452 dim = isl_set_get_space(stmt->domain);
1455 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1456 if (pos >= 0)
1457 stmt->domain = internalize_iv(stmt->domain, pos,
1458 isl_aff_copy(iv_map));
1460 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1461 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1462 isl_dim_in, stmt_id);
1464 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1465 if (pos >= 0) {
1466 isl_set *set = isl_map_wrap(stmt->schedule);
1467 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1468 stmt->schedule = isl_set_unwrap(set);
1471 dim = isl_space_map_from_set(dim);
1472 extend = isl_multi_pw_aff_identity(dim);
1473 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1474 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1475 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1476 for (i = 0; i < stmt->n_arg; ++i)
1477 stmt->args[i] = expr_embed(stmt->args[i],
1478 isl_multi_pw_aff_copy(extend),
1479 isl_aff_copy(iv_map), var_id);
1480 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1482 isl_set_free(dom);
1483 isl_id_free(var_id);
1485 for (i = 0; i < stmt->n_arg; ++i)
1486 if (!stmt->args[i])
1487 return pet_stmt_free(stmt);
1488 if (!stmt->domain || !stmt->schedule || !stmt->body)
1489 return pet_stmt_free(stmt);
1490 return stmt;
1491 error:
1492 isl_set_free(dom);
1493 isl_map_free(sched);
1494 isl_aff_free(iv_map);
1495 isl_id_free(var_id);
1496 return NULL;
1499 /* Embed the given pet_array in an extra outer loop with iteration domain
1500 * "dom".
1501 * This embedding only has an effect on virtual arrays (those with
1502 * user pointer equal to NULL), which need to be extended along with
1503 * the iteration domain.
1505 static struct pet_array *pet_array_embed(struct pet_array *array,
1506 __isl_take isl_set *dom)
1508 isl_id *array_id = NULL;
1510 if (!array)
1511 goto error;
1512 if (!extent_is_virtual_array(array->extent)) {
1513 isl_set_free(dom);
1514 return array;
1517 array_id = isl_set_get_tuple_id(array->extent);
1518 array->extent = isl_set_flat_product(dom, array->extent);
1519 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1520 if (!array->extent)
1521 return pet_array_free(array);
1523 return array;
1524 error:
1525 isl_set_free(dom);
1526 return NULL;
1529 /* Update the context with respect to an embedding into a loop
1530 * with iteration domain "dom" and induction variable "id".
1531 * "iv_map" expresses the real iterator (parameter "id") in terms
1532 * of a possibly virtual iterator (used in "dom").
1534 * If the current context is independent of "id", we don't need
1535 * to do anything.
1536 * Otherwise, a parameter value is invalid for the embedding if
1537 * any of the corresponding iterator values is invalid.
1538 * That is, a parameter value is valid only if all the corresponding
1539 * iterator values are valid.
1540 * We therefore compute the set of parameters
1542 * forall i in dom : valid (i)
1544 * or
1546 * not exists i in dom : not valid(i)
1548 * i.e.,
1550 * not exists i in dom \ valid(i)
1552 * Before we subtract valid(i) from dom, we first need to substitute
1553 * the real iterator for the virtual iterator.
1555 * If there are any unnamed parameters in "dom", then we consider
1556 * a parameter value to be valid if it is valid for any value of those
1557 * unnamed parameters. They are therefore projected out at the end.
1559 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1560 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1561 __isl_keep isl_id *id)
1563 int pos;
1564 isl_multi_aff *ma;
1566 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1567 if (pos < 0)
1568 return context;
1570 context = isl_set_from_params(context);
1571 context = isl_set_add_dims(context, isl_dim_set, 1);
1572 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1573 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1574 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1575 context = isl_set_preimage_multi_aff(context, ma);
1576 context = isl_set_subtract(isl_set_copy(dom), context);
1577 context = isl_set_params(context);
1578 context = isl_set_complement(context);
1579 context = pet_nested_remove_from_set(context);
1580 return context;
1583 /* Update the implication with respect to an embedding into a loop
1584 * with iteration domain "dom".
1586 * Since embed_access extends virtual arrays along with the domain
1587 * of the access, we need to do the same with domain and range
1588 * of the implication. Since the original implication is only valid
1589 * within a given iteration of the loop, the extended implication
1590 * maps the extra array dimension corresponding to the extra loop
1591 * to itself.
1593 static struct pet_implication *pet_implication_embed(
1594 struct pet_implication *implication, __isl_take isl_set *dom)
1596 isl_id *id;
1597 isl_map *map;
1599 if (!implication)
1600 goto error;
1602 map = isl_set_identity(dom);
1603 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1604 map = isl_map_flat_product(map, implication->extension);
1605 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1606 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1607 implication->extension = map;
1608 if (!implication->extension)
1609 return pet_implication_free(implication);
1611 return implication;
1612 error:
1613 isl_set_free(dom);
1614 return NULL;
1617 /* Embed all statements and arrays in "scop" in an extra outer loop
1618 * with iteration domain "dom" and schedule "sched".
1619 * "id" represents the induction variable of the loop.
1620 * "iv_map" maps a possibly virtual iterator to the real iterator.
1621 * That is, it expresses the iterator that some of the parameters in "scop"
1622 * may refer to in terms of the iterator used in "dom" and
1623 * the domain of "sched".
1625 * Any skip conditions within the loop have no effect outside of the loop.
1626 * The caller is responsible for making sure skip[pet_skip_later] has been
1627 * taken into account.
1629 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1630 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
1631 __isl_take isl_id *id)
1633 int i;
1634 isl_map *sched_map;
1636 sched_map = isl_map_from_aff(sched);
1638 if (!scop)
1639 goto error;
1641 pet_scop_reset_skip(scop, pet_skip_now);
1642 pet_scop_reset_skip(scop, pet_skip_later);
1644 scop->context = context_embed(scop->context, dom, iv_map, id);
1645 if (!scop->context)
1646 goto error;
1648 for (i = 0; i < scop->n_stmt; ++i) {
1649 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1650 isl_set_copy(dom), isl_map_copy(sched_map),
1651 isl_aff_copy(iv_map), isl_id_copy(id));
1652 if (!scop->stmts[i])
1653 goto error;
1656 for (i = 0; i < scop->n_array; ++i) {
1657 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1658 isl_set_copy(dom));
1659 if (!scop->arrays[i])
1660 goto error;
1663 for (i = 0; i < scop->n_implication; ++i) {
1664 scop->implications[i] =
1665 pet_implication_embed(scop->implications[i],
1666 isl_set_copy(dom));
1667 if (!scop->implications[i])
1668 goto error;
1671 isl_set_free(dom);
1672 isl_map_free(sched_map);
1673 isl_aff_free(iv_map);
1674 isl_id_free(id);
1675 return scop;
1676 error:
1677 isl_set_free(dom);
1678 isl_map_free(sched_map);
1679 isl_aff_free(iv_map);
1680 isl_id_free(id);
1681 return pet_scop_free(scop);
1684 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1686 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1687 __isl_take isl_set *cond)
1689 if (!stmt)
1690 goto error;
1692 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1694 return stmt;
1695 error:
1696 isl_set_free(cond);
1697 return pet_stmt_free(stmt);
1700 /* Add extra conditions to scop->skip[type].
1702 * The new skip condition only holds if it held before
1703 * and the condition is true. It does not hold if it did not hold
1704 * before or the condition is false.
1706 * The skip condition is assumed to be an affine expression.
1708 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1709 enum pet_skip type, __isl_keep isl_set *cond)
1711 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1712 isl_pw_aff *skip;
1713 isl_set *dom;
1715 if (!scop)
1716 return NULL;
1717 if (!ext->skip[type])
1718 return scop;
1720 if (!multi_pw_aff_is_affine(ext->skip[type]))
1721 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1722 isl_error_internal, "can only restrict affine skips",
1723 return pet_scop_free(scop));
1725 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1726 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1727 cond = isl_set_copy(cond);
1728 cond = isl_set_from_params(cond);
1729 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1730 skip = indicator_function(cond, dom);
1731 isl_multi_pw_aff_free(ext->skip[type]);
1732 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1733 if (!ext->skip[type])
1734 return pet_scop_free(scop);
1736 return scop;
1739 /* Add extra conditions on the parameters to all iteration domains
1740 * and skip conditions.
1742 * A parameter value is valid for the result if it was valid
1743 * for the original scop and satisfies "cond" or if it does
1744 * not satisfy "cond" as in this case the scop is not executed
1745 * and the original constraints on the parameters are irrelevant.
1747 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1748 __isl_take isl_set *cond)
1750 int i;
1752 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1753 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1755 if (!scop)
1756 goto error;
1758 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1759 scop->context = isl_set_union(scop->context,
1760 isl_set_complement(isl_set_copy(cond)));
1761 scop->context = isl_set_coalesce(scop->context);
1762 scop->context = pet_nested_remove_from_set(scop->context);
1763 if (!scop->context)
1764 goto error;
1766 for (i = 0; i < scop->n_stmt; ++i) {
1767 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1768 isl_set_copy(cond));
1769 if (!scop->stmts[i])
1770 goto error;
1773 isl_set_free(cond);
1774 return scop;
1775 error:
1776 isl_set_free(cond);
1777 return pet_scop_free(scop);
1780 /* Insert an argument expression corresponding to "test" in front
1781 * of the list of arguments described by *n_arg and *args.
1783 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1784 __isl_keep isl_multi_pw_aff *test)
1786 int i;
1787 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1789 if (!test)
1790 return -1;
1792 if (!*args) {
1793 *args = isl_calloc_array(ctx, pet_expr *, 1);
1794 if (!*args)
1795 return -1;
1796 } else {
1797 pet_expr **ext;
1798 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1799 if (!ext)
1800 return -1;
1801 for (i = 0; i < *n_arg; ++i)
1802 ext[1 + i] = (*args)[i];
1803 free(*args);
1804 *args = ext;
1806 (*n_arg)++;
1807 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1808 if (!(*args)[0])
1809 return -1;
1811 return 0;
1814 /* Look through the applications in "scop" for any that can be
1815 * applied to the filter expressed by "map" and "satisified".
1816 * If there is any, then apply it to "map" and return the result.
1817 * Otherwise, return "map".
1818 * "id" is the identifier of the virtual array.
1820 * We only introduce at most one implication for any given virtual array,
1821 * so we can apply the implication and return as soon as we find one.
1823 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1824 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1826 int i;
1828 for (i = 0; i < scop->n_implication; ++i) {
1829 struct pet_implication *pi = scop->implications[i];
1830 isl_id *pi_id;
1832 if (pi->satisfied != satisfied)
1833 continue;
1834 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1835 isl_id_free(pi_id);
1836 if (pi_id != id)
1837 continue;
1839 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1842 return map;
1845 /* Is the filter expressed by "test" and "satisfied" implied
1846 * by filter "pos" on "domain", with filter "expr", taking into
1847 * account the implications of "scop"?
1849 * For filter on domain implying that expressed by "test" and "satisfied",
1850 * the filter needs to be an access to the same (virtual) array as "test" and
1851 * the filter value needs to be equal to "satisfied".
1852 * Moreover, the filter access relation, possibly extended by
1853 * the implications in "scop" needs to contain "test".
1855 static int implies_filter(struct pet_scop *scop,
1856 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1857 __isl_keep isl_map *test, int satisfied)
1859 isl_id *test_id, *arg_id;
1860 isl_val *val;
1861 int is_int;
1862 int s;
1863 int is_subset;
1864 isl_map *implied;
1866 if (expr->type != pet_expr_access)
1867 return 0;
1868 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1869 arg_id = pet_expr_access_get_id(expr);
1870 isl_id_free(arg_id);
1871 isl_id_free(test_id);
1872 if (test_id != arg_id)
1873 return 0;
1874 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1875 is_int = isl_val_is_int(val);
1876 if (is_int)
1877 s = isl_val_get_num_si(val);
1878 isl_val_free(val);
1879 if (!val)
1880 return -1;
1881 if (!is_int)
1882 return 0;
1883 if (s != satisfied)
1884 return 0;
1886 implied = isl_map_copy(expr->acc.access);
1887 implied = apply_implications(scop, implied, test_id, satisfied);
1888 is_subset = isl_map_is_subset(test, implied);
1889 isl_map_free(implied);
1891 return is_subset;
1894 /* Is the filter expressed by "test" and "satisfied" implied
1895 * by any of the filters on the domain of "stmt", taking into
1896 * account the implications of "scop"?
1898 static int filter_implied(struct pet_scop *scop,
1899 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1901 int i;
1902 int implied;
1903 isl_id *test_id;
1904 isl_map *domain;
1905 isl_map *test_map;
1907 if (!scop || !stmt || !test)
1908 return -1;
1909 if (scop->n_implication == 0)
1910 return 0;
1911 if (stmt->n_arg == 0)
1912 return 0;
1914 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1915 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1917 implied = 0;
1918 for (i = 0; i < stmt->n_arg; ++i) {
1919 implied = implies_filter(scop, domain, i, stmt->args[i],
1920 test_map, satisfied);
1921 if (implied < 0 || implied)
1922 break;
1925 isl_map_free(test_map);
1926 isl_map_free(domain);
1927 return implied;
1930 /* Make the statement "stmt" depend on the value of "test"
1931 * being equal to "satisfied" by adjusting stmt->domain.
1933 * The domain of "test" corresponds to the (zero or more) outer dimensions
1934 * of the iteration domain.
1936 * We first extend "test" to apply to the entire iteration domain and
1937 * then check if the filter that we are about to add is implied
1938 * by any of the current filters, possibly taking into account
1939 * the implications in "scop". If so, we leave "stmt" untouched and return.
1941 * Otherwise, we insert an argument corresponding to a read to "test"
1942 * from the iteration domain of "stmt" in front of the list of arguments.
1943 * We also insert a corresponding output dimension in the wrapped
1944 * map contained in stmt->domain, with value set to "satisfied".
1946 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1947 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1949 int i;
1950 int implied;
1951 isl_id *id;
1952 isl_ctx *ctx;
1953 isl_pw_multi_aff *pma;
1954 isl_multi_aff *add_dom;
1955 isl_space *space;
1956 isl_local_space *ls;
1957 int n_test_dom;
1959 if (!stmt || !test)
1960 goto error;
1962 space = pet_stmt_get_space(stmt);
1963 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1964 space = isl_space_from_domain(space);
1965 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1966 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1967 ls = isl_local_space_from_space(isl_space_domain(space));
1968 for (i = 0; i < n_test_dom; ++i) {
1969 isl_aff *aff;
1970 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1971 isl_dim_set, i);
1972 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1974 isl_local_space_free(ls);
1975 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1977 implied = filter_implied(scop, stmt, test, satisfied);
1978 if (implied < 0)
1979 goto error;
1980 if (implied) {
1981 isl_multi_pw_aff_free(test);
1982 return stmt;
1985 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1986 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1987 id, satisfied);
1988 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1990 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1991 goto error;
1993 isl_multi_pw_aff_free(test);
1994 return stmt;
1995 error:
1996 isl_multi_pw_aff_free(test);
1997 return pet_stmt_free(stmt);
2000 /* Does "scop" have a skip condition of the given "type"?
2002 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2004 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2006 if (!scop)
2007 return -1;
2008 return ext->skip[type] != NULL;
2011 /* Does "scop" have a skip condition of the given "type" that
2012 * is an affine expression?
2014 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2016 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2018 if (!scop)
2019 return -1;
2020 if (!ext->skip[type])
2021 return 0;
2022 return multi_pw_aff_is_affine(ext->skip[type]);
2025 /* Does "scop" have a skip condition of the given "type" that
2026 * is not an affine expression?
2028 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2030 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2031 int aff;
2033 if (!scop)
2034 return -1;
2035 if (!ext->skip[type])
2036 return 0;
2037 aff = multi_pw_aff_is_affine(ext->skip[type]);
2038 if (aff < 0)
2039 return -1;
2040 return !aff;
2043 /* Does "scop" have a skip condition of the given "type" that
2044 * is affine and holds on the entire domain?
2046 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2048 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2049 isl_pw_aff *pa;
2050 isl_set *set;
2051 int is_aff;
2052 int is_univ;
2054 is_aff = pet_scop_has_affine_skip(scop, type);
2055 if (is_aff < 0 || !is_aff)
2056 return is_aff;
2058 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2059 set = isl_pw_aff_non_zero_set(pa);
2060 is_univ = isl_set_plain_is_universe(set);
2061 isl_set_free(set);
2063 return is_univ;
2066 /* Replace scop->skip[type] by "skip".
2068 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2069 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2071 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2073 if (!scop || !skip)
2074 goto error;
2076 isl_multi_pw_aff_free(ext->skip[type]);
2077 ext->skip[type] = skip;
2079 return scop;
2080 error:
2081 isl_multi_pw_aff_free(skip);
2082 return pet_scop_free(scop);
2085 /* Return a copy of scop->skip[type].
2087 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2088 enum pet_skip type)
2090 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2092 if (!scop)
2093 return NULL;
2095 return isl_multi_pw_aff_copy(ext->skip[type]);
2098 /* Assuming scop->skip[type] is an affine expression,
2099 * return the constraints on the parameters for which the skip condition
2100 * holds.
2102 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2103 enum pet_skip type)
2105 isl_multi_pw_aff *skip;
2106 isl_pw_aff *pa;
2108 skip = pet_scop_get_skip(scop, type);
2109 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2110 isl_multi_pw_aff_free(skip);
2111 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2114 /* Return the identifier of the variable that is accessed by
2115 * the skip condition of the given type.
2117 * The skip condition is assumed not to be an affine condition.
2119 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2120 enum pet_skip type)
2122 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2124 if (!scop)
2125 return NULL;
2127 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2130 /* Return an access pet_expr corresponding to the skip condition
2131 * of the given type.
2133 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2134 enum pet_skip type)
2136 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2139 /* Drop the the skip condition scop->skip[type].
2141 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2143 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2145 if (!scop)
2146 return;
2148 isl_multi_pw_aff_free(ext->skip[type]);
2149 ext->skip[type] = NULL;
2152 /* Make the skip condition (if any) depend on the value of "test" being
2153 * equal to "satisfied".
2155 * We only support the case where the original skip condition is universal,
2156 * i.e., where skipping is unconditional, and where satisfied == 1.
2157 * In this case, the skip condition is changed to skip only when
2158 * "test" is equal to one.
2160 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2161 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2163 int is_univ = 0;
2165 if (!scop)
2166 return NULL;
2167 if (!pet_scop_has_skip(scop, type))
2168 return scop;
2170 if (satisfied)
2171 is_univ = pet_scop_has_universal_skip(scop, type);
2172 if (is_univ < 0)
2173 return pet_scop_free(scop);
2174 if (satisfied && is_univ) {
2175 isl_multi_pw_aff *skip;
2176 skip = isl_multi_pw_aff_copy(test);
2177 scop = pet_scop_set_skip(scop, type, skip);
2178 if (!scop)
2179 return NULL;
2180 } else {
2181 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2182 "skip expression cannot be filtered",
2183 return pet_scop_free(scop));
2186 return scop;
2189 /* Make all statements in "scop" depend on the value of "test"
2190 * being equal to "satisfied" by adjusting their domains.
2192 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2193 __isl_take isl_multi_pw_aff *test, int satisfied)
2195 int i;
2197 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2198 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2200 if (!scop || !test)
2201 goto error;
2203 for (i = 0; i < scop->n_stmt; ++i) {
2204 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2205 isl_multi_pw_aff_copy(test), satisfied);
2206 if (!scop->stmts[i])
2207 goto error;
2210 isl_multi_pw_aff_free(test);
2211 return scop;
2212 error:
2213 isl_multi_pw_aff_free(test);
2214 return pet_scop_free(scop);
2217 /* Add all parameters in "expr" to "space" and return the result.
2219 static __isl_give isl_space *expr_collect_params(__isl_keep pet_expr *expr,
2220 __isl_take isl_space *space)
2222 int i;
2224 if (!expr)
2225 goto error;
2226 for (i = 0; i < expr->n_arg; ++i)
2227 space = expr_collect_params(expr->args[i], space);
2229 if (expr->type == pet_expr_access)
2230 space = isl_space_align_params(space,
2231 isl_map_get_space(expr->acc.access));
2233 return space;
2234 error:
2235 pet_expr_free(expr);
2236 return isl_space_free(space);
2239 /* Add all parameters in "stmt" to "space" and return the result.
2241 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2242 __isl_take isl_space *space)
2244 int i;
2246 if (!stmt)
2247 return isl_space_free(space);
2249 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2250 space = isl_space_align_params(space,
2251 isl_map_get_space(stmt->schedule));
2252 for (i = 0; i < stmt->n_arg; ++i)
2253 space = expr_collect_params(stmt->args[i], space);
2254 space = expr_collect_params(stmt->body, space);
2256 return space;
2259 /* Add all parameters in "array" to "space" and return the result.
2261 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2262 __isl_take isl_space *space)
2264 if (!array)
2265 return isl_space_free(space);
2267 space = isl_space_align_params(space,
2268 isl_set_get_space(array->context));
2269 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2271 return space;
2274 /* Add all parameters in "scop" to "space" and return the result.
2276 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2277 __isl_take isl_space *space)
2279 int i;
2281 if (!scop)
2282 return isl_space_free(space);
2284 for (i = 0; i < scop->n_array; ++i)
2285 space = array_collect_params(scop->arrays[i], space);
2287 for (i = 0; i < scop->n_stmt; ++i)
2288 space = stmt_collect_params(scop->stmts[i], space);
2290 return space;
2293 /* Add all parameters in "space" to the domain, schedule and
2294 * all access relations in "stmt".
2296 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2297 __isl_take isl_space *space)
2299 int i;
2301 if (!stmt)
2302 goto error;
2304 stmt->domain = isl_set_align_params(stmt->domain,
2305 isl_space_copy(space));
2306 stmt->schedule = isl_map_align_params(stmt->schedule,
2307 isl_space_copy(space));
2309 for (i = 0; i < stmt->n_arg; ++i) {
2310 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2311 isl_space_copy(space));
2312 if (!stmt->args[i])
2313 goto error;
2315 stmt->body = pet_expr_align_params(stmt->body, isl_space_copy(space));
2317 if (!stmt->domain || !stmt->schedule || !stmt->body)
2318 goto error;
2320 isl_space_free(space);
2321 return stmt;
2322 error:
2323 isl_space_free(space);
2324 return pet_stmt_free(stmt);
2327 /* Add all parameters in "space" to "array".
2329 static struct pet_array *array_propagate_params(struct pet_array *array,
2330 __isl_take isl_space *space)
2332 if (!array)
2333 goto error;
2335 array->context = isl_set_align_params(array->context,
2336 isl_space_copy(space));
2337 array->extent = isl_set_align_params(array->extent,
2338 isl_space_copy(space));
2339 if (array->value_bounds) {
2340 array->value_bounds = isl_set_align_params(array->value_bounds,
2341 isl_space_copy(space));
2342 if (!array->value_bounds)
2343 goto error;
2346 if (!array->context || !array->extent)
2347 goto error;
2349 isl_space_free(space);
2350 return array;
2351 error:
2352 isl_space_free(space);
2353 return pet_array_free(array);
2356 /* Add all parameters in "space" to "scop".
2358 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2359 __isl_take isl_space *space)
2361 int i;
2363 if (!scop)
2364 goto error;
2366 for (i = 0; i < scop->n_array; ++i) {
2367 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2368 isl_space_copy(space));
2369 if (!scop->arrays[i])
2370 goto error;
2373 for (i = 0; i < scop->n_stmt; ++i) {
2374 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2375 isl_space_copy(space));
2376 if (!scop->stmts[i])
2377 goto error;
2380 isl_space_free(space);
2381 return scop;
2382 error:
2383 isl_space_free(space);
2384 return pet_scop_free(scop);
2387 /* Update all isl_sets and isl_maps in "scop" such that they all
2388 * have the same parameters.
2390 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2392 isl_space *space;
2394 if (!scop)
2395 return NULL;
2397 space = isl_set_get_space(scop->context);
2398 space = scop_collect_params(scop, space);
2400 scop->context = isl_set_align_params(scop->context,
2401 isl_space_copy(space));
2402 scop = scop_propagate_params(scop, space);
2404 if (scop && !scop->context)
2405 return pet_scop_free(scop);
2407 return scop;
2410 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2411 * in "space" by a value equal to the corresponding parameter.
2413 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2414 __isl_take isl_space *space)
2416 if (!stmt)
2417 goto error;
2419 stmt->body = pet_expr_detect_parameter_accesses(stmt->body,
2420 isl_space_copy(space));
2422 if (!stmt->domain || !stmt->schedule || !stmt->body)
2423 goto error;
2425 isl_space_free(space);
2426 return stmt;
2427 error:
2428 isl_space_free(space);
2429 return pet_stmt_free(stmt);
2432 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2433 * in "space" by a value equal to the corresponding parameter.
2435 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2436 __isl_take isl_space *space)
2438 int i;
2440 if (!scop)
2441 goto error;
2443 for (i = 0; i < scop->n_stmt; ++i) {
2444 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2445 isl_space_copy(space));
2446 if (!scop->stmts[i])
2447 goto error;
2450 isl_space_free(space);
2451 return scop;
2452 error:
2453 isl_space_free(space);
2454 return pet_scop_free(scop);
2457 /* Replace all accesses to (0D) arrays that correspond to any of
2458 * the parameters used in "scop" by a value equal
2459 * to the corresponding parameter.
2461 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2463 isl_space *space;
2465 if (!scop)
2466 return NULL;
2468 space = isl_set_get_space(scop->context);
2469 space = scop_collect_params(scop, space);
2471 scop = scop_detect_parameter_accesses(scop, space);
2473 return scop;
2476 /* Add the access relation of the access expression "expr" to "accesses" and
2477 * return the result.
2478 * The domain of the access relation is intersected with "domain".
2479 * If "tag" is set, then the access relation is tagged with
2480 * the corresponding reference identifier.
2482 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2483 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2485 isl_map *access;
2487 access = pet_expr_access_get_may_access(expr);
2488 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2489 if (tag)
2490 access = pet_expr_tag_access(expr, access);
2491 return isl_union_map_add_map(accesses, access);
2494 /* Add all read access relations (if "read" is set) and/or all write
2495 * access relations (if "write" is set) to "accesses" and return the result.
2496 * The domains of the access relations are intersected with "domain".
2497 * If "tag" is set, then the access relations are tagged with
2498 * the corresponding reference identifiers.
2500 * If "must" is set, then we only add the accesses that are definitely
2501 * performed. Otherwise, we add all potential accesses.
2502 * In particular, if the access has any arguments, then if "must" is
2503 * set we currently skip the access completely. If "must" is not set,
2504 * we project out the values of the access arguments.
2506 static __isl_give isl_union_map *expr_collect_accesses(
2507 __isl_keep pet_expr *expr, int read, int write, int must, int tag,
2508 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2510 int i;
2511 isl_id *id;
2512 isl_space *dim;
2514 if (!expr)
2515 return isl_union_map_free(accesses);
2517 for (i = 0; i < expr->n_arg; ++i)
2518 accesses = expr_collect_accesses(expr->args[i],
2519 read, write, must, tag, accesses, domain);
2521 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2522 ((read && expr->acc.read) || (write && expr->acc.write)) &&
2523 (!must || expr->n_arg == 0)) {
2524 accesses = expr_collect_access(expr, tag, accesses, domain);
2527 return accesses;
2530 /* Collect and return all read access relations (if "read" is set)
2531 * and/or all write access relations (if "write" is set) in "stmt".
2532 * If "tag" is set, then the access relations are tagged with
2533 * the corresponding reference identifiers.
2534 * If "kill" is set, then "stmt" is a kill statement and we simply
2535 * add the argument of the kill operation.
2537 * If "must" is set, then we only add the accesses that are definitely
2538 * performed. Otherwise, we add all potential accesses.
2539 * In particular, if the statement has any arguments, then if "must" is
2540 * set we currently skip the statement completely. If "must" is not set,
2541 * we project out the values of the statement arguments.
2543 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2544 int read, int write, int kill, int must, int tag,
2545 __isl_take isl_space *dim)
2547 isl_union_map *accesses;
2548 isl_set *domain;
2550 if (!stmt)
2551 return NULL;
2553 accesses = isl_union_map_empty(dim);
2555 if (must && stmt->n_arg > 0)
2556 return accesses;
2558 domain = isl_set_copy(stmt->domain);
2559 if (isl_set_is_wrapping(domain))
2560 domain = isl_map_domain(isl_set_unwrap(domain));
2562 if (kill)
2563 accesses = expr_collect_access(stmt->body->args[0], tag,
2564 accesses, domain);
2565 else
2566 accesses = expr_collect_accesses(stmt->body, read, write,
2567 must, tag, accesses, domain);
2568 isl_set_free(domain);
2570 return accesses;
2573 /* Is "stmt" an assignment statement?
2575 int pet_stmt_is_assign(struct pet_stmt *stmt)
2577 if (!stmt)
2578 return 0;
2579 if (stmt->body->type != pet_expr_op)
2580 return 0;
2581 return stmt->body->op == pet_op_assign;
2584 /* Is "stmt" a kill statement?
2586 int pet_stmt_is_kill(struct pet_stmt *stmt)
2588 if (!stmt)
2589 return 0;
2590 if (stmt->body->type != pet_expr_op)
2591 return 0;
2592 return stmt->body->op == pet_op_kill;
2595 /* Is "stmt" an assume statement?
2597 int pet_stmt_is_assume(struct pet_stmt *stmt)
2599 if (!stmt)
2600 return 0;
2601 return pet_expr_is_assume(stmt->body);
2604 /* Compute a mapping from all arrays (of structs) in scop
2605 * to their innermost arrays.
2607 * In particular, for each array of a primitive type, the result
2608 * contains the identity mapping on that array.
2609 * For each array involving member accesses, the result
2610 * contains a mapping from the elements of any intermediate array of structs
2611 * to all corresponding elements of the innermost nested arrays.
2613 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2615 int i;
2616 isl_union_map *to_inner;
2618 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2620 for (i = 0; i < scop->n_array; ++i) {
2621 struct pet_array *array = scop->arrays[i];
2622 isl_set *set;
2623 isl_map *map, *gist;
2625 if (array->element_is_record)
2626 continue;
2628 map = isl_set_identity(isl_set_copy(array->extent));
2630 set = isl_map_domain(isl_map_copy(map));
2631 gist = isl_map_copy(map);
2632 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2633 to_inner = isl_union_map_add_map(to_inner, gist);
2635 while (set && isl_set_is_wrapping(set)) {
2636 isl_id *id;
2637 isl_map *wrapped;
2639 id = isl_set_get_tuple_id(set);
2640 wrapped = isl_set_unwrap(set);
2641 wrapped = isl_map_domain_map(wrapped);
2642 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2643 map = isl_map_apply_domain(map, wrapped);
2644 set = isl_map_domain(isl_map_copy(map));
2645 gist = isl_map_copy(map);
2646 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2647 to_inner = isl_union_map_add_map(to_inner, gist);
2650 isl_set_free(set);
2651 isl_map_free(map);
2654 return to_inner;
2657 /* Collect and return all read access relations (if "read" is set)
2658 * and/or all write access relations (if "write" is set) in "scop".
2659 * If "kill" is set, then we only add the arguments of kill operations.
2660 * If "must" is set, then we only add the accesses that are definitely
2661 * performed. Otherwise, we add all potential accesses.
2662 * If "tag" is set, then the access relations are tagged with
2663 * the corresponding reference identifiers.
2664 * For accesses to structures, the returned access relation accesses
2665 * all individual fields in the structures.
2667 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2668 int read, int write, int kill, int must, int tag)
2670 int i;
2671 isl_union_map *accesses;
2672 isl_union_set *arrays;
2673 isl_union_map *to_inner;
2675 if (!scop)
2676 return NULL;
2678 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2680 for (i = 0; i < scop->n_stmt; ++i) {
2681 struct pet_stmt *stmt = scop->stmts[i];
2682 isl_union_map *accesses_i;
2683 isl_space *space;
2685 if (kill && !pet_stmt_is_kill(stmt))
2686 continue;
2688 space = isl_set_get_space(scop->context);
2689 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2690 must, tag, space);
2691 accesses = isl_union_map_union(accesses, accesses_i);
2694 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2695 for (i = 0; i < scop->n_array; ++i) {
2696 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2697 arrays = isl_union_set_add_set(arrays, extent);
2699 accesses = isl_union_map_intersect_range(accesses, arrays);
2701 to_inner = compute_to_inner(scop);
2702 accesses = isl_union_map_apply_range(accesses, to_inner);
2704 return accesses;
2707 /* Collect all potential read access relations.
2709 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2711 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2714 /* Collect all potential write access relations.
2716 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2718 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2721 /* Collect all definite write access relations.
2723 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2725 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2728 /* Collect all definite kill access relations.
2730 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2732 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2735 /* Collect all tagged potential read access relations.
2737 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2738 struct pet_scop *scop)
2740 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2743 /* Collect all tagged potential write access relations.
2745 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2746 struct pet_scop *scop)
2748 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2751 /* Collect all tagged definite write access relations.
2753 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2754 struct pet_scop *scop)
2756 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2759 /* Collect all tagged definite kill access relations.
2761 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2762 struct pet_scop *scop)
2764 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2767 /* Collect and return the union of iteration domains in "scop".
2769 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2771 int i;
2772 isl_set *domain_i;
2773 isl_union_set *domain;
2775 if (!scop)
2776 return NULL;
2778 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2780 for (i = 0; i < scop->n_stmt; ++i) {
2781 domain_i = isl_set_copy(scop->stmts[i]->domain);
2782 domain = isl_union_set_add_set(domain, domain_i);
2785 return domain;
2788 /* Collect and return the schedules of the statements in "scop".
2789 * The range is normalized to the maximal number of scheduling
2790 * dimensions.
2792 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2794 int i, j;
2795 isl_map *schedule_i;
2796 isl_union_map *schedule;
2797 int depth, max_depth = 0;
2799 if (!scop)
2800 return NULL;
2802 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2804 for (i = 0; i < scop->n_stmt; ++i) {
2805 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2806 if (depth > max_depth)
2807 max_depth = depth;
2810 for (i = 0; i < scop->n_stmt; ++i) {
2811 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2812 depth = isl_map_dim(schedule_i, isl_dim_out);
2813 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2814 max_depth - depth);
2815 for (j = depth; j < max_depth; ++j)
2816 schedule_i = isl_map_fix_si(schedule_i,
2817 isl_dim_out, j, 0);
2818 schedule = isl_union_map_add_map(schedule, schedule_i);
2821 return schedule;
2824 /* Add a reference identifier to all access expressions in "stmt".
2825 * "n_ref" points to an integer that contains the sequence number
2826 * of the next reference.
2828 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2830 int i;
2832 if (!stmt)
2833 return NULL;
2835 for (i = 0; i < stmt->n_arg; ++i) {
2836 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2837 if (!stmt->args[i])
2838 return pet_stmt_free(stmt);
2841 stmt->body = pet_expr_add_ref_ids(stmt->body, n_ref);
2842 if (!stmt->body)
2843 return pet_stmt_free(stmt);
2845 return stmt;
2848 /* Add a reference identifier to all access expressions in "scop".
2850 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2852 int i;
2853 int n_ref;
2855 if (!scop)
2856 return NULL;
2858 n_ref = 0;
2859 for (i = 0; i < scop->n_stmt; ++i) {
2860 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2861 if (!scop->stmts[i])
2862 return pet_scop_free(scop);
2865 return scop;
2868 /* Reset the user pointer on all parameter ids in "array".
2870 static struct pet_array *array_anonymize(struct pet_array *array)
2872 if (!array)
2873 return NULL;
2875 array->context = isl_set_reset_user(array->context);
2876 array->extent = isl_set_reset_user(array->extent);
2877 if (!array->context || !array->extent)
2878 return pet_array_free(array);
2880 return array;
2883 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2885 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2887 int i;
2888 isl_space *space;
2889 isl_set *domain;
2891 if (!stmt)
2892 return NULL;
2894 stmt->domain = isl_set_reset_user(stmt->domain);
2895 stmt->schedule = isl_map_reset_user(stmt->schedule);
2896 if (!stmt->domain || !stmt->schedule)
2897 return pet_stmt_free(stmt);
2899 for (i = 0; i < stmt->n_arg; ++i) {
2900 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2901 if (!stmt->args[i])
2902 return pet_stmt_free(stmt);
2905 stmt->body = pet_expr_anonymize(stmt->body);
2906 if (!stmt->body)
2907 return pet_stmt_free(stmt);
2909 return stmt;
2912 /* Reset the user pointer on the tuple ids and all parameter ids
2913 * in "implication".
2915 static struct pet_implication *implication_anonymize(
2916 struct pet_implication *implication)
2918 if (!implication)
2919 return NULL;
2921 implication->extension = isl_map_reset_user(implication->extension);
2922 if (!implication->extension)
2923 return pet_implication_free(implication);
2925 return implication;
2928 /* Reset the user pointer on all parameter and tuple ids in "scop".
2930 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2932 int i;
2934 if (!scop)
2935 return NULL;
2937 scop->context = isl_set_reset_user(scop->context);
2938 scop->context_value = isl_set_reset_user(scop->context_value);
2939 if (!scop->context || !scop->context_value)
2940 return pet_scop_free(scop);
2942 for (i = 0; i < scop->n_array; ++i) {
2943 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2944 if (!scop->arrays[i])
2945 return pet_scop_free(scop);
2948 for (i = 0; i < scop->n_stmt; ++i) {
2949 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2950 if (!scop->stmts[i])
2951 return pet_scop_free(scop);
2954 for (i = 0; i < scop->n_implication; ++i) {
2955 scop->implications[i] =
2956 implication_anonymize(scop->implications[i]);
2957 if (!scop->implications[i])
2958 return pet_scop_free(scop);
2961 return scop;
2964 /* Compute the gist of the iteration domain and all access relations
2965 * of "stmt" based on the constraints on the parameters specified by "context"
2966 * and the constraints on the values of nested accesses specified
2967 * by "value_bounds".
2969 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2970 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2972 int i;
2973 isl_set *domain;
2975 if (!stmt)
2976 return NULL;
2978 domain = isl_set_copy(stmt->domain);
2979 if (stmt->n_arg > 0)
2980 domain = isl_map_domain(isl_set_unwrap(domain));
2982 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2984 for (i = 0; i < stmt->n_arg; ++i) {
2985 stmt->args[i] = pet_expr_gist(stmt->args[i],
2986 domain, value_bounds);
2987 if (!stmt->args[i])
2988 goto error;
2991 stmt->body = pet_expr_gist(stmt->body, domain, value_bounds);
2992 if (!stmt->body)
2993 goto error;
2995 isl_set_free(domain);
2997 domain = isl_set_universe(pet_stmt_get_space(stmt));
2998 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2999 if (stmt->n_arg > 0)
3000 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3001 value_bounds);
3002 stmt->domain = isl_set_gist(stmt->domain, domain);
3003 if (!stmt->domain)
3004 return pet_stmt_free(stmt);
3006 return stmt;
3007 error:
3008 isl_set_free(domain);
3009 return pet_stmt_free(stmt);
3012 /* Compute the gist of the extent of the array
3013 * based on the constraints on the parameters specified by "context".
3015 static struct pet_array *array_gist(struct pet_array *array,
3016 __isl_keep isl_set *context)
3018 if (!array)
3019 return NULL;
3021 array->extent = isl_set_gist_params(array->extent,
3022 isl_set_copy(context));
3023 if (!array->extent)
3024 return pet_array_free(array);
3026 return array;
3029 /* Compute the gist of all sets and relations in "scop"
3030 * based on the constraints on the parameters specified by "scop->context"
3031 * and the constraints on the values of nested accesses specified
3032 * by "value_bounds".
3034 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3035 __isl_keep isl_union_map *value_bounds)
3037 int i;
3039 if (!scop)
3040 return NULL;
3042 scop->context = isl_set_coalesce(scop->context);
3043 if (!scop->context)
3044 return pet_scop_free(scop);
3046 for (i = 0; i < scop->n_array; ++i) {
3047 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3048 if (!scop->arrays[i])
3049 return pet_scop_free(scop);
3052 for (i = 0; i < scop->n_stmt; ++i) {
3053 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3054 value_bounds);
3055 if (!scop->stmts[i])
3056 return pet_scop_free(scop);
3059 return scop;
3062 /* Intersect the context of "scop" with "context".
3063 * To ensure that we don't introduce any unnamed parameters in
3064 * the context of "scop", we first remove the unnamed parameters
3065 * from "context".
3067 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3068 __isl_take isl_set *context)
3070 if (!scop)
3071 goto error;
3073 context = pet_nested_remove_from_set(context);
3074 scop->context = isl_set_intersect(scop->context, context);
3075 if (!scop->context)
3076 return pet_scop_free(scop);
3078 return scop;
3079 error:
3080 isl_set_free(context);
3081 return pet_scop_free(scop);
3084 /* Drop the current context of "scop". That is, replace the context
3085 * by a universal set.
3087 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3089 isl_space *space;
3091 if (!scop)
3092 return NULL;
3094 space = isl_set_get_space(scop->context);
3095 isl_set_free(scop->context);
3096 scop->context = isl_set_universe(space);
3097 if (!scop->context)
3098 return pet_scop_free(scop);
3100 return scop;
3103 /* Append "array" to the arrays of "scop".
3105 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3106 struct pet_array *array)
3108 isl_ctx *ctx;
3109 struct pet_array **arrays;
3111 if (!array || !scop)
3112 goto error;
3114 ctx = isl_set_get_ctx(scop->context);
3115 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3116 scop->n_array + 1);
3117 if (!arrays)
3118 goto error;
3119 scop->arrays = arrays;
3120 scop->arrays[scop->n_array] = array;
3121 scop->n_array++;
3123 return scop;
3124 error:
3125 pet_array_free(array);
3126 return pet_scop_free(scop);
3129 /* Create an index expression for an access to a virtual array
3130 * representing the result of a condition.
3131 * Unlike other accessed data, the id of the array is NULL as
3132 * there is no ValueDecl in the program corresponding to the virtual
3133 * array.
3134 * The array starts out as a scalar, but grows along with the
3135 * statement writing to the array in pet_scop_embed.
3137 __isl_give isl_multi_pw_aff *pet_create_test_index(isl_ctx *ctx, int test_nr)
3139 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
3140 isl_id *id;
3141 char name[50];
3143 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3144 id = isl_id_alloc(ctx, name, NULL);
3145 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
3146 return isl_multi_pw_aff_zero(dim);
3149 /* Add an array with the given extent (range of "index") to the list
3150 * of arrays in "scop" and return the extended pet_scop.
3151 * "int_size" is the number of bytes needed to represent values of type "int".
3152 * The array is marked as attaining values 0 and 1 only and
3153 * as each element being assigned at most once.
3155 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3156 __isl_take isl_multi_pw_aff *index, int int_size)
3158 isl_ctx *ctx;
3159 isl_space *space;
3160 struct pet_array *array;
3161 isl_map *access;
3163 if (!scop || !index)
3164 goto error;
3166 ctx = isl_multi_pw_aff_get_ctx(index);
3167 array = isl_calloc_type(ctx, struct pet_array);
3168 if (!array)
3169 goto error;
3171 access = isl_map_from_multi_pw_aff(index);
3172 array->extent = isl_map_range(access);
3173 space = isl_space_params_alloc(ctx, 0);
3174 array->context = isl_set_universe(space);
3175 space = isl_space_set_alloc(ctx, 0, 1);
3176 array->value_bounds = isl_set_universe(space);
3177 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3178 isl_dim_set, 0, 0);
3179 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3180 isl_dim_set, 0, 1);
3181 array->element_type = strdup("int");
3182 array->element_size = int_size;
3183 array->uniquely_defined = 1;
3185 if (!array->extent || !array->context)
3186 array = pet_array_free(array);
3188 scop = pet_scop_add_array(scop, array);
3190 return scop;
3191 error:
3192 isl_multi_pw_aff_free(index);
3193 return pet_scop_free(scop);
3196 /* Create and return an implication on filter values equal to "satisfied"
3197 * with extension "map".
3199 static struct pet_implication *new_implication(__isl_take isl_map *map,
3200 int satisfied)
3202 isl_ctx *ctx;
3203 struct pet_implication *implication;
3205 if (!map)
3206 return NULL;
3207 ctx = isl_map_get_ctx(map);
3208 implication = isl_alloc_type(ctx, struct pet_implication);
3209 if (!implication)
3210 goto error;
3212 implication->extension = map;
3213 implication->satisfied = satisfied;
3215 return implication;
3216 error:
3217 isl_map_free(map);
3218 return NULL;
3221 /* Add an implication on filter values equal to "satisfied"
3222 * with extension "map" to "scop".
3224 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3225 __isl_take isl_map *map, int satisfied)
3227 isl_ctx *ctx;
3228 struct pet_implication *implication;
3229 struct pet_implication **implications;
3231 implication = new_implication(map, satisfied);
3232 if (!scop || !implication)
3233 goto error;
3235 ctx = isl_set_get_ctx(scop->context);
3236 implications = isl_realloc_array(ctx, scop->implications,
3237 struct pet_implication *,
3238 scop->n_implication + 1);
3239 if (!implications)
3240 goto error;
3241 scop->implications = implications;
3242 scop->implications[scop->n_implication] = implication;
3243 scop->n_implication++;
3245 return scop;
3246 error:
3247 pet_implication_free(implication);
3248 return pet_scop_free(scop);
3251 /* Given an access expression, check if it is data dependent.
3252 * If so, set *found and abort the search.
3254 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3256 int *found = user;
3258 if (pet_expr_get_n_arg(expr) > 0) {
3259 *found = 1;
3260 return -1;
3263 return 0;
3266 /* Does "scop" contain any data dependent accesses?
3268 * Check the body of each statement for such accesses.
3270 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3272 int i;
3273 int found = 0;
3275 if (!scop)
3276 return -1;
3278 for (i = 0; i < scop->n_stmt; ++i) {
3279 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
3280 &is_data_dependent, &found);
3281 if (r < 0 && !found)
3282 return -1;
3283 if (found)
3284 return found;
3287 return found;
3290 /* Does "scop" contain and data dependent conditions?
3292 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3294 int i;
3296 if (!scop)
3297 return -1;
3299 for (i = 0; i < scop->n_stmt; ++i)
3300 if (scop->stmts[i]->n_arg > 0)
3301 return 1;
3303 return 0;
3306 /* Keep track of the "input" file inside the (extended) "scop".
3308 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3310 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3312 if (!scop)
3313 return NULL;
3315 ext->input = input;
3317 return scop;
3320 /* Print the original code corresponding to "scop" to printer "p".
3322 * pet_scop_print_original can only be called from
3323 * a pet_transform_C_source callback. This means that the input
3324 * file is stored in the extended scop and that the printer prints
3325 * to a file.
3327 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3328 __isl_take isl_printer *p)
3330 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3331 FILE *output;
3332 unsigned start, end;
3334 if (!scop || !p)
3335 return isl_printer_free(p);
3337 if (!ext->input)
3338 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3339 "no input file stored in scop",
3340 return isl_printer_free(p));
3342 output = isl_printer_get_file(p);
3343 if (!output)
3344 return isl_printer_free(p);
3346 start = pet_loc_get_start(scop->loc);
3347 end = pet_loc_get_end(scop->loc);
3348 if (copy(ext->input, output, start, end) < 0)
3349 return isl_printer_free(p);
3351 return p;