construct pet_scop from intermediate pet_tree data structure
[pet.git] / scop.c
blob86e9124f6c8b02e61d16b88139e40e4443863c4a
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 /* Prefix the schedule of "stmt" with an extra dimension with constant
1033 * value "pos".
1035 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1037 if (!stmt)
1038 return NULL;
1040 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1041 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1042 if (!stmt->schedule)
1043 return pet_stmt_free(stmt);
1045 return stmt;
1048 /* Prefix the schedules of all statements in "scop" with an extra
1049 * dimension with constant value "pos".
1051 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1053 int i;
1055 if (!scop)
1056 return NULL;
1058 for (i = 0; i < scop->n_stmt; ++i) {
1059 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1060 if (!scop->stmts[i])
1061 return pet_scop_free(scop);
1064 return scop;
1067 /* Given a set with a parameter at "param_pos" that refers to the
1068 * iterator, "move" the iterator to the first set dimension.
1069 * That is, essentially equate the parameter to the first set dimension
1070 * and then project it out.
1072 * The first set dimension may however refer to a virtual iterator,
1073 * while the parameter refers to the "real" iterator.
1074 * We therefore need to take into account the affine expression "iv_map", which
1075 * expresses the real iterator in terms of the virtual iterator.
1076 * In particular, we equate the set dimension to the input of the map
1077 * and the parameter to the output of the map and then project out
1078 * everything we don't need anymore.
1080 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1081 int param_pos, __isl_take isl_aff *iv_map)
1083 isl_map *map, *map2;
1084 map = isl_map_from_domain(set);
1085 map = isl_map_add_dims(map, isl_dim_out, 1);
1086 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1087 map2 = isl_map_from_aff(iv_map);
1088 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1089 map = isl_map_apply_range(map, map2);
1090 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1091 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1092 return isl_map_domain(map);
1095 /* Data used in embed_access.
1096 * extend adds an iterator to the iteration domain (through precomposition).
1097 * iv_map expresses the real iterator in terms of the virtual iterator
1098 * var_id represents the induction variable of the corresponding loop
1100 struct pet_embed_access {
1101 isl_multi_pw_aff *extend;
1102 isl_aff *iv_map;
1103 isl_id *var_id;
1106 /* Given an index expression, return an expression for the outer iterator.
1108 static __isl_give isl_aff *index_outer_iterator(
1109 __isl_take isl_multi_pw_aff *index)
1111 isl_space *space;
1112 isl_local_space *ls;
1114 space = isl_multi_pw_aff_get_domain_space(index);
1115 isl_multi_pw_aff_free(index);
1117 ls = isl_local_space_from_space(space);
1118 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1121 /* Replace an index expression that references the new (outer) iterator variable
1122 * by one that references the corresponding (real) iterator.
1124 * The input index expression is of the form
1126 * { S[i',...] -> i[] }
1128 * where i' refers to the virtual iterator.
1130 * iv_map is of the form
1132 * { [i'] -> [i] }
1134 * Return the index expression
1136 * { S[i',...] -> [i] }
1138 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1139 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1141 isl_space *space;
1142 isl_aff *aff;
1144 aff = index_outer_iterator(index);
1145 space = isl_aff_get_space(aff);
1146 iv_map = isl_aff_align_params(iv_map, space);
1147 aff = isl_aff_pullback_aff(iv_map, aff);
1149 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1152 /* Given an index expression "index" that refers to the (real) iterator
1153 * through the parameter at position "pos", plug in "iv_map", expressing
1154 * the real iterator in terms of the virtual (outer) iterator.
1156 * In particular, the index expression is of the form
1158 * [..., i, ...] -> { S[i',...] -> ... i ... }
1160 * where i refers to the real iterator and i' refers to the virtual iterator.
1162 * iv_map is of the form
1164 * { [i'] -> [i] }
1166 * Return the index expression
1168 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1171 * We first move the parameter to the input
1173 * [..., ...] -> { [i, i',...] -> ... i ... }
1175 * and construct
1177 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1179 * and then combine the two to obtain the desired result.
1181 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1182 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1184 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1185 isl_multi_aff *ma;
1187 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1188 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1189 isl_dim_param, pos, 1);
1191 space = isl_space_map_from_set(space);
1192 ma = isl_multi_aff_identity(isl_space_copy(space));
1193 iv_map = isl_aff_align_params(iv_map, space);
1194 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1195 ma = isl_multi_aff_flat_range_product(
1196 isl_multi_aff_from_aff(iv_map), ma);
1197 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1199 return index;
1202 /* Does the index expression "index" reference a virtual array, i.e.,
1203 * one with user pointer equal to NULL?
1204 * A virtual array does not have any members.
1206 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1208 isl_id *id;
1209 int is_virtual;
1211 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1212 return 0;
1213 if (isl_multi_pw_aff_range_is_wrapping(index))
1214 return 0;
1215 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1216 is_virtual = !isl_id_get_user(id);
1217 isl_id_free(id);
1219 return is_virtual;
1222 /* Does the access relation "access" 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 access_is_virtual_array(__isl_keep isl_map *access)
1228 isl_id *id;
1229 int is_virtual;
1231 if (!isl_map_has_tuple_id(access, isl_dim_out))
1232 return 0;
1233 if (isl_map_range_is_wrapping(access))
1234 return 0;
1235 id = isl_map_get_tuple_id(access, isl_dim_out);
1236 is_virtual = !isl_id_get_user(id);
1237 isl_id_free(id);
1239 return is_virtual;
1242 /* Embed the given index expression in an extra outer loop.
1243 * The domain of the index expression has already been updated.
1245 * If the access refers to the induction variable, then it is
1246 * turned into an access to the set of integers with index (and value)
1247 * equal to the induction variable.
1249 * If the accessed array is a virtual array (with user
1250 * pointer equal to NULL), as created by create_test_index,
1251 * then it is extended along with the domain of the index expression.
1253 static __isl_give isl_multi_pw_aff *embed_index_expression(
1254 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1256 isl_id *array_id = NULL;
1257 int pos;
1259 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1260 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1261 if (array_id == data->var_id) {
1262 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1263 } else if (index_is_virtual_array(index)) {
1264 isl_aff *aff;
1265 isl_multi_pw_aff *mpa;
1267 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1268 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1269 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1270 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1271 isl_id_copy(array_id));
1273 isl_id_free(array_id);
1275 pos = isl_multi_pw_aff_find_dim_by_id(index,
1276 isl_dim_param, data->var_id);
1277 if (pos >= 0)
1278 index = index_internalize_iv(index, pos,
1279 isl_aff_copy(data->iv_map));
1280 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1281 isl_id_copy(data->var_id));
1283 return index;
1286 /* Embed the given access relation in an extra outer loop.
1287 * The domain of the access relation has already been updated.
1289 * If the access refers to the induction variable, then it is
1290 * turned into an access to the set of integers with index (and value)
1291 * equal to the induction variable.
1293 * If the induction variable appears in the constraints (as a parameter),
1294 * then the parameter is equated to the newly introduced iteration
1295 * domain dimension and subsequently projected out.
1297 * Similarly, if the accessed array is a virtual array (with user
1298 * pointer equal to NULL), as created by create_test_index,
1299 * then it is extended along with the domain of the access.
1301 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1302 struct pet_embed_access *data)
1304 isl_id *array_id = NULL;
1305 int pos;
1307 if (isl_map_has_tuple_id(access, isl_dim_out))
1308 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1309 if (array_id == data->var_id || access_is_virtual_array(access)) {
1310 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1311 access = isl_map_equate(access,
1312 isl_dim_in, 0, isl_dim_out, 0);
1313 if (array_id == data->var_id)
1314 access = isl_map_apply_range(access,
1315 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1316 else
1317 access = isl_map_set_tuple_id(access, isl_dim_out,
1318 isl_id_copy(array_id));
1320 isl_id_free(array_id);
1322 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1323 if (pos >= 0) {
1324 isl_set *set = isl_map_wrap(access);
1325 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1326 access = isl_set_unwrap(set);
1328 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1329 isl_id_copy(data->var_id));
1331 return access;
1334 /* Given an access expression, embed the associated access relation and
1335 * index expression in an extra outer loop.
1337 * We first update the domains to insert the extra dimension and
1338 * then update the access relation and index expression to take
1339 * into account the mapping "iv_map" from virtual iterator
1340 * to real iterator.
1342 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
1344 struct pet_embed_access *data = user;
1346 expr = pet_expr_cow(expr);
1347 expr = pet_expr_access_update_domain(expr, data->extend);
1348 if (!expr)
1349 return NULL;
1351 expr->acc.access = embed_access_relation(expr->acc.access, data);
1352 expr->acc.index = embed_index_expression(expr->acc.index, data);
1353 if (!expr->acc.access || !expr->acc.index)
1354 return pet_expr_free(expr);
1356 return expr;
1359 /* Embed all access subexpressions of "expr" in an extra loop.
1360 * "extend" inserts an outer loop iterator in the iteration domains
1361 * (through precomposition).
1362 * "iv_map" expresses the real iterator in terms of the virtual iterator
1363 * "var_id" represents the induction variable.
1365 static __isl_give pet_expr *expr_embed(__isl_take pet_expr *expr,
1366 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1367 __isl_keep isl_id *var_id)
1369 struct pet_embed_access data =
1370 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1372 expr = pet_expr_map_access(expr, &embed_access, &data);
1373 isl_aff_free(iv_map);
1374 isl_multi_pw_aff_free(extend);
1375 return expr;
1378 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1379 * "dom" and schedule "sched". "var_id" represents the induction variable
1380 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1381 * That is, it expresses the iterator that some of the parameters in "stmt"
1382 * may refer to in terms of the iterator used in "dom" and
1383 * the domain of "sched".
1385 * The iteration domain and schedule of the statement are updated
1386 * according to the iteration domain and schedule of the new loop.
1387 * If stmt->domain is a wrapped map, then the iteration domain
1388 * is the domain of this map, so we need to be careful to adjust
1389 * this domain.
1391 * If the induction variable appears in the constraints (as a parameter)
1392 * of the current iteration domain or the schedule of the statement,
1393 * then the parameter is equated to the newly introduced iteration
1394 * domain dimension and subsequently projected out.
1396 * Finally, all access relations are updated based on the extra loop.
1398 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1399 __isl_take isl_set *dom, __isl_take isl_map *sched,
1400 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1402 int i;
1403 int pos;
1404 isl_id *stmt_id;
1405 isl_space *dim;
1406 isl_multi_pw_aff *extend;
1408 if (!stmt)
1409 goto error;
1411 if (isl_set_is_wrapping(stmt->domain)) {
1412 isl_map *map;
1413 isl_map *ext;
1414 isl_space *ran_dim;
1416 map = isl_set_unwrap(stmt->domain);
1417 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1418 ran_dim = isl_space_range(isl_map_get_space(map));
1419 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1420 isl_set_universe(ran_dim));
1421 map = isl_map_flat_domain_product(ext, map);
1422 map = isl_map_set_tuple_id(map, isl_dim_in,
1423 isl_id_copy(stmt_id));
1424 dim = isl_space_domain(isl_map_get_space(map));
1425 stmt->domain = isl_map_wrap(map);
1426 } else {
1427 stmt_id = isl_set_get_tuple_id(stmt->domain);
1428 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1429 stmt->domain);
1430 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1431 isl_id_copy(stmt_id));
1432 dim = isl_set_get_space(stmt->domain);
1435 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1436 if (pos >= 0)
1437 stmt->domain = internalize_iv(stmt->domain, pos,
1438 isl_aff_copy(iv_map));
1440 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1441 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1442 isl_dim_in, stmt_id);
1444 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1445 if (pos >= 0) {
1446 isl_set *set = isl_map_wrap(stmt->schedule);
1447 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1448 stmt->schedule = isl_set_unwrap(set);
1451 dim = isl_space_map_from_set(dim);
1452 extend = isl_multi_pw_aff_identity(dim);
1453 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1454 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1455 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1456 for (i = 0; i < stmt->n_arg; ++i)
1457 stmt->args[i] = expr_embed(stmt->args[i],
1458 isl_multi_pw_aff_copy(extend),
1459 isl_aff_copy(iv_map), var_id);
1460 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1462 isl_set_free(dom);
1463 isl_id_free(var_id);
1465 for (i = 0; i < stmt->n_arg; ++i)
1466 if (!stmt->args[i])
1467 return pet_stmt_free(stmt);
1468 if (!stmt->domain || !stmt->schedule || !stmt->body)
1469 return pet_stmt_free(stmt);
1470 return stmt;
1471 error:
1472 isl_set_free(dom);
1473 isl_map_free(sched);
1474 isl_aff_free(iv_map);
1475 isl_id_free(var_id);
1476 return NULL;
1479 /* Embed the given pet_array in an extra outer loop with iteration domain
1480 * "dom".
1481 * This embedding only has an effect on virtual arrays (those with
1482 * user pointer equal to NULL), which need to be extended along with
1483 * the iteration domain.
1485 static struct pet_array *pet_array_embed(struct pet_array *array,
1486 __isl_take isl_set *dom)
1488 isl_id *array_id = NULL;
1490 if (!array)
1491 goto error;
1493 if (isl_set_has_tuple_id(array->extent))
1494 array_id = isl_set_get_tuple_id(array->extent);
1496 if (array_id && !isl_id_get_user(array_id)) {
1497 array->extent = isl_set_flat_product(dom, array->extent);
1498 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1499 if (!array->extent)
1500 return pet_array_free(array);
1501 } else {
1502 isl_set_free(dom);
1503 isl_id_free(array_id);
1506 return array;
1507 error:
1508 isl_set_free(dom);
1509 return NULL;
1512 /* Update the context with respect to an embedding into a loop
1513 * with iteration domain "dom" and induction variable "id".
1514 * "iv_map" expresses the real iterator (parameter "id") in terms
1515 * of a possibly virtual iterator (used in "dom").
1517 * If the current context is independent of "id", we don't need
1518 * to do anything.
1519 * Otherwise, a parameter value is invalid for the embedding if
1520 * any of the corresponding iterator values is invalid.
1521 * That is, a parameter value is valid only if all the corresponding
1522 * iterator values are valid.
1523 * We therefore compute the set of parameters
1525 * forall i in dom : valid (i)
1527 * or
1529 * not exists i in dom : not valid(i)
1531 * i.e.,
1533 * not exists i in dom \ valid(i)
1535 * Before we subtract valid(i) from dom, we first need to substitute
1536 * the real iterator for the virtual iterator.
1538 * If there are any unnamed parameters in "dom", then we consider
1539 * a parameter value to be valid if it is valid for any value of those
1540 * unnamed parameters. They are therefore projected out at the end.
1542 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1543 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1544 __isl_keep isl_id *id)
1546 int pos;
1547 isl_multi_aff *ma;
1549 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1550 if (pos < 0)
1551 return context;
1553 context = isl_set_from_params(context);
1554 context = isl_set_add_dims(context, isl_dim_set, 1);
1555 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1556 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1557 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1558 context = isl_set_preimage_multi_aff(context, ma);
1559 context = isl_set_subtract(isl_set_copy(dom), context);
1560 context = isl_set_params(context);
1561 context = isl_set_complement(context);
1562 context = pet_nested_remove_from_set(context);
1563 return context;
1566 /* Update the implication with respect to an embedding into a loop
1567 * with iteration domain "dom".
1569 * Since embed_access extends virtual arrays along with the domain
1570 * of the access, we need to do the same with domain and range
1571 * of the implication. Since the original implication is only valid
1572 * within a given iteration of the loop, the extended implication
1573 * maps the extra array dimension corresponding to the extra loop
1574 * to itself.
1576 static struct pet_implication *pet_implication_embed(
1577 struct pet_implication *implication, __isl_take isl_set *dom)
1579 isl_id *id;
1580 isl_map *map;
1582 if (!implication)
1583 goto error;
1585 map = isl_set_identity(dom);
1586 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1587 map = isl_map_flat_product(map, implication->extension);
1588 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1589 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1590 implication->extension = map;
1591 if (!implication->extension)
1592 return pet_implication_free(implication);
1594 return implication;
1595 error:
1596 isl_set_free(dom);
1597 return NULL;
1600 /* Embed all statements and arrays in "scop" in an extra outer loop
1601 * with iteration domain "dom" and schedule "sched".
1602 * "id" represents the induction variable of the loop.
1603 * "iv_map" maps a possibly virtual iterator to the real iterator.
1604 * That is, it expresses the iterator that some of the parameters in "scop"
1605 * may refer to in terms of the iterator used in "dom" and
1606 * the domain of "sched".
1608 * Any skip conditions within the loop have no effect outside of the loop.
1609 * The caller is responsible for making sure skip[pet_skip_later] has been
1610 * taken into account.
1612 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1613 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
1614 __isl_take isl_id *id)
1616 int i;
1617 isl_map *sched_map;
1619 sched_map = isl_map_from_aff(sched);
1621 if (!scop)
1622 goto error;
1624 pet_scop_reset_skip(scop, pet_skip_now);
1625 pet_scop_reset_skip(scop, pet_skip_later);
1627 scop->context = context_embed(scop->context, dom, iv_map, id);
1628 if (!scop->context)
1629 goto error;
1631 for (i = 0; i < scop->n_stmt; ++i) {
1632 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1633 isl_set_copy(dom), isl_map_copy(sched_map),
1634 isl_aff_copy(iv_map), isl_id_copy(id));
1635 if (!scop->stmts[i])
1636 goto error;
1639 for (i = 0; i < scop->n_array; ++i) {
1640 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1641 isl_set_copy(dom));
1642 if (!scop->arrays[i])
1643 goto error;
1646 for (i = 0; i < scop->n_implication; ++i) {
1647 scop->implications[i] =
1648 pet_implication_embed(scop->implications[i],
1649 isl_set_copy(dom));
1650 if (!scop->implications[i])
1651 goto error;
1654 isl_set_free(dom);
1655 isl_map_free(sched_map);
1656 isl_aff_free(iv_map);
1657 isl_id_free(id);
1658 return scop;
1659 error:
1660 isl_set_free(dom);
1661 isl_map_free(sched_map);
1662 isl_aff_free(iv_map);
1663 isl_id_free(id);
1664 return pet_scop_free(scop);
1667 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1669 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1670 __isl_take isl_set *cond)
1672 if (!stmt)
1673 goto error;
1675 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1677 return stmt;
1678 error:
1679 isl_set_free(cond);
1680 return pet_stmt_free(stmt);
1683 /* Add extra conditions to scop->skip[type].
1685 * The new skip condition only holds if it held before
1686 * and the condition is true. It does not hold if it did not hold
1687 * before or the condition is false.
1689 * The skip condition is assumed to be an affine expression.
1691 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1692 enum pet_skip type, __isl_keep isl_set *cond)
1694 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1695 isl_pw_aff *skip;
1696 isl_set *dom;
1698 if (!scop)
1699 return NULL;
1700 if (!ext->skip[type])
1701 return scop;
1703 if (!multi_pw_aff_is_affine(ext->skip[type]))
1704 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1705 isl_error_internal, "can only restrict affine skips",
1706 return pet_scop_free(scop));
1708 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1709 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1710 cond = isl_set_copy(cond);
1711 cond = isl_set_from_params(cond);
1712 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1713 skip = indicator_function(cond, dom);
1714 isl_multi_pw_aff_free(ext->skip[type]);
1715 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1716 if (!ext->skip[type])
1717 return pet_scop_free(scop);
1719 return scop;
1722 /* Add extra conditions on the parameters to all iteration domains
1723 * and skip conditions.
1725 * A parameter value is valid for the result if it was valid
1726 * for the original scop and satisfies "cond" or if it does
1727 * not satisfy "cond" as in this case the scop is not executed
1728 * and the original constraints on the parameters are irrelevant.
1730 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1731 __isl_take isl_set *cond)
1733 int i;
1735 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1736 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1738 if (!scop)
1739 goto error;
1741 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1742 scop->context = isl_set_union(scop->context,
1743 isl_set_complement(isl_set_copy(cond)));
1744 scop->context = isl_set_coalesce(scop->context);
1745 scop->context = pet_nested_remove_from_set(scop->context);
1746 if (!scop->context)
1747 goto error;
1749 for (i = 0; i < scop->n_stmt; ++i) {
1750 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1751 isl_set_copy(cond));
1752 if (!scop->stmts[i])
1753 goto error;
1756 isl_set_free(cond);
1757 return scop;
1758 error:
1759 isl_set_free(cond);
1760 return pet_scop_free(scop);
1763 /* Insert an argument expression corresponding to "test" in front
1764 * of the list of arguments described by *n_arg and *args.
1766 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1767 __isl_keep isl_multi_pw_aff *test)
1769 int i;
1770 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1772 if (!test)
1773 return -1;
1775 if (!*args) {
1776 *args = isl_calloc_array(ctx, pet_expr *, 1);
1777 if (!*args)
1778 return -1;
1779 } else {
1780 pet_expr **ext;
1781 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1782 if (!ext)
1783 return -1;
1784 for (i = 0; i < *n_arg; ++i)
1785 ext[1 + i] = (*args)[i];
1786 free(*args);
1787 *args = ext;
1789 (*n_arg)++;
1790 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1791 if (!(*args)[0])
1792 return -1;
1794 return 0;
1797 /* Look through the applications in "scop" for any that can be
1798 * applied to the filter expressed by "map" and "satisified".
1799 * If there is any, then apply it to "map" and return the result.
1800 * Otherwise, return "map".
1801 * "id" is the identifier of the virtual array.
1803 * We only introduce at most one implication for any given virtual array,
1804 * so we can apply the implication and return as soon as we find one.
1806 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1807 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1809 int i;
1811 for (i = 0; i < scop->n_implication; ++i) {
1812 struct pet_implication *pi = scop->implications[i];
1813 isl_id *pi_id;
1815 if (pi->satisfied != satisfied)
1816 continue;
1817 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1818 isl_id_free(pi_id);
1819 if (pi_id != id)
1820 continue;
1822 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1825 return map;
1828 /* Is the filter expressed by "test" and "satisfied" implied
1829 * by filter "pos" on "domain", with filter "expr", taking into
1830 * account the implications of "scop"?
1832 * For filter on domain implying that expressed by "test" and "satisfied",
1833 * the filter needs to be an access to the same (virtual) array as "test" and
1834 * the filter value needs to be equal to "satisfied".
1835 * Moreover, the filter access relation, possibly extended by
1836 * the implications in "scop" needs to contain "test".
1838 static int implies_filter(struct pet_scop *scop,
1839 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1840 __isl_keep isl_map *test, int satisfied)
1842 isl_id *test_id, *arg_id;
1843 isl_val *val;
1844 int is_int;
1845 int s;
1846 int is_subset;
1847 isl_map *implied;
1849 if (expr->type != pet_expr_access)
1850 return 0;
1851 test_id = isl_map_get_tuple_id(test, isl_dim_out);
1852 arg_id = pet_expr_access_get_id(expr);
1853 isl_id_free(arg_id);
1854 isl_id_free(test_id);
1855 if (test_id != arg_id)
1856 return 0;
1857 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
1858 is_int = isl_val_is_int(val);
1859 if (is_int)
1860 s = isl_val_get_num_si(val);
1861 isl_val_free(val);
1862 if (!val)
1863 return -1;
1864 if (!is_int)
1865 return 0;
1866 if (s != satisfied)
1867 return 0;
1869 implied = isl_map_copy(expr->acc.access);
1870 implied = apply_implications(scop, implied, test_id, satisfied);
1871 is_subset = isl_map_is_subset(test, implied);
1872 isl_map_free(implied);
1874 return is_subset;
1877 /* Is the filter expressed by "test" and "satisfied" implied
1878 * by any of the filters on the domain of "stmt", taking into
1879 * account the implications of "scop"?
1881 static int filter_implied(struct pet_scop *scop,
1882 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
1884 int i;
1885 int implied;
1886 isl_id *test_id;
1887 isl_map *domain;
1888 isl_map *test_map;
1890 if (!scop || !stmt || !test)
1891 return -1;
1892 if (scop->n_implication == 0)
1893 return 0;
1894 if (stmt->n_arg == 0)
1895 return 0;
1897 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
1898 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
1900 implied = 0;
1901 for (i = 0; i < stmt->n_arg; ++i) {
1902 implied = implies_filter(scop, domain, i, stmt->args[i],
1903 test_map, satisfied);
1904 if (implied < 0 || implied)
1905 break;
1908 isl_map_free(test_map);
1909 isl_map_free(domain);
1910 return implied;
1913 /* Make the statement "stmt" depend on the value of "test"
1914 * being equal to "satisfied" by adjusting stmt->domain.
1916 * The domain of "test" corresponds to the (zero or more) outer dimensions
1917 * of the iteration domain.
1919 * We first extend "test" to apply to the entire iteration domain and
1920 * then check if the filter that we are about to add is implied
1921 * by any of the current filters, possibly taking into account
1922 * the implications in "scop". If so, we leave "stmt" untouched and return.
1924 * Otherwise, we insert an argument corresponding to a read to "test"
1925 * from the iteration domain of "stmt" in front of the list of arguments.
1926 * We also insert a corresponding output dimension in the wrapped
1927 * map contained in stmt->domain, with value set to "satisfied".
1929 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
1930 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
1932 int i;
1933 int implied;
1934 isl_id *id;
1935 isl_ctx *ctx;
1936 isl_pw_multi_aff *pma;
1937 isl_multi_aff *add_dom;
1938 isl_space *space;
1939 isl_local_space *ls;
1940 int n_test_dom;
1942 if (!stmt || !test)
1943 goto error;
1945 space = pet_stmt_get_space(stmt);
1946 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
1947 space = isl_space_from_domain(space);
1948 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
1949 add_dom = isl_multi_aff_zero(isl_space_copy(space));
1950 ls = isl_local_space_from_space(isl_space_domain(space));
1951 for (i = 0; i < n_test_dom; ++i) {
1952 isl_aff *aff;
1953 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
1954 isl_dim_set, i);
1955 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
1957 isl_local_space_free(ls);
1958 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
1960 implied = filter_implied(scop, stmt, test, satisfied);
1961 if (implied < 0)
1962 goto error;
1963 if (implied) {
1964 isl_multi_pw_aff_free(test);
1965 return stmt;
1968 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
1969 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
1970 id, satisfied);
1971 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
1973 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
1974 goto error;
1976 isl_multi_pw_aff_free(test);
1977 return stmt;
1978 error:
1979 isl_multi_pw_aff_free(test);
1980 return pet_stmt_free(stmt);
1983 /* Does "scop" have a skip condition of the given "type"?
1985 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
1987 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1989 if (!scop)
1990 return -1;
1991 return ext->skip[type] != NULL;
1994 /* Does "scop" have a skip condition of the given "type" that
1995 * is an affine expression?
1997 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
1999 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2001 if (!scop)
2002 return -1;
2003 if (!ext->skip[type])
2004 return 0;
2005 return multi_pw_aff_is_affine(ext->skip[type]);
2008 /* Does "scop" have a skip condition of the given "type" that
2009 * is not an affine expression?
2011 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2013 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2014 int aff;
2016 if (!scop)
2017 return -1;
2018 if (!ext->skip[type])
2019 return 0;
2020 aff = multi_pw_aff_is_affine(ext->skip[type]);
2021 if (aff < 0)
2022 return -1;
2023 return !aff;
2026 /* Does "scop" have a skip condition of the given "type" that
2027 * is affine and holds on the entire domain?
2029 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2031 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2032 isl_pw_aff *pa;
2033 isl_set *set;
2034 int is_aff;
2035 int is_univ;
2037 is_aff = pet_scop_has_affine_skip(scop, type);
2038 if (is_aff < 0 || !is_aff)
2039 return is_aff;
2041 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2042 set = isl_pw_aff_non_zero_set(pa);
2043 is_univ = isl_set_plain_is_universe(set);
2044 isl_set_free(set);
2046 return is_univ;
2049 /* Replace scop->skip[type] by "skip".
2051 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2052 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2054 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2056 if (!scop || !skip)
2057 goto error;
2059 isl_multi_pw_aff_free(ext->skip[type]);
2060 ext->skip[type] = skip;
2062 return scop;
2063 error:
2064 isl_multi_pw_aff_free(skip);
2065 return pet_scop_free(scop);
2068 /* Return a copy of scop->skip[type].
2070 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2071 enum pet_skip type)
2073 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2075 if (!scop)
2076 return NULL;
2078 return isl_multi_pw_aff_copy(ext->skip[type]);
2081 /* Assuming scop->skip[type] is an affine expression,
2082 * return the constraints on the parameters for which the skip condition
2083 * holds.
2085 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2086 enum pet_skip type)
2088 isl_multi_pw_aff *skip;
2089 isl_pw_aff *pa;
2091 skip = pet_scop_get_skip(scop, type);
2092 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2093 isl_multi_pw_aff_free(skip);
2094 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2097 /* Return the identifier of the variable that is accessed by
2098 * the skip condition of the given type.
2100 * The skip condition is assumed not to be an affine condition.
2102 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2103 enum pet_skip type)
2105 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2107 if (!scop)
2108 return NULL;
2110 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2113 /* Return an access pet_expr corresponding to the skip condition
2114 * of the given type.
2116 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2117 enum pet_skip type)
2119 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2122 /* Drop the the skip condition scop->skip[type].
2124 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2126 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2128 if (!scop)
2129 return;
2131 isl_multi_pw_aff_free(ext->skip[type]);
2132 ext->skip[type] = NULL;
2135 /* Make the skip condition (if any) depend on the value of "test" being
2136 * equal to "satisfied".
2138 * We only support the case where the original skip condition is universal,
2139 * i.e., where skipping is unconditional, and where satisfied == 1.
2140 * In this case, the skip condition is changed to skip only when
2141 * "test" is equal to one.
2143 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2144 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2146 int is_univ = 0;
2148 if (!scop)
2149 return NULL;
2150 if (!pet_scop_has_skip(scop, type))
2151 return scop;
2153 if (satisfied)
2154 is_univ = pet_scop_has_universal_skip(scop, type);
2155 if (is_univ < 0)
2156 return pet_scop_free(scop);
2157 if (satisfied && is_univ) {
2158 isl_multi_pw_aff *skip;
2159 skip = isl_multi_pw_aff_copy(test);
2160 scop = pet_scop_set_skip(scop, type, skip);
2161 if (!scop)
2162 return NULL;
2163 } else {
2164 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2165 "skip expression cannot be filtered",
2166 return pet_scop_free(scop));
2169 return scop;
2172 /* Make all statements in "scop" depend on the value of "test"
2173 * being equal to "satisfied" by adjusting their domains.
2175 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2176 __isl_take isl_multi_pw_aff *test, int satisfied)
2178 int i;
2180 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2181 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2183 if (!scop || !test)
2184 goto error;
2186 for (i = 0; i < scop->n_stmt; ++i) {
2187 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2188 isl_multi_pw_aff_copy(test), satisfied);
2189 if (!scop->stmts[i])
2190 goto error;
2193 isl_multi_pw_aff_free(test);
2194 return scop;
2195 error:
2196 isl_multi_pw_aff_free(test);
2197 return pet_scop_free(scop);
2200 /* Add all parameters in "expr" to "space" and return the result.
2202 static __isl_give isl_space *expr_collect_params(__isl_keep pet_expr *expr,
2203 __isl_take isl_space *space)
2205 int i;
2207 if (!expr)
2208 goto error;
2209 for (i = 0; i < expr->n_arg; ++i)
2210 space = expr_collect_params(expr->args[i], space);
2212 if (expr->type == pet_expr_access)
2213 space = isl_space_align_params(space,
2214 isl_map_get_space(expr->acc.access));
2216 return space;
2217 error:
2218 pet_expr_free(expr);
2219 return isl_space_free(space);
2222 /* Add all parameters in "stmt" to "space" and return the result.
2224 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2225 __isl_take isl_space *space)
2227 int i;
2229 if (!stmt)
2230 return isl_space_free(space);
2232 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2233 space = isl_space_align_params(space,
2234 isl_map_get_space(stmt->schedule));
2235 for (i = 0; i < stmt->n_arg; ++i)
2236 space = expr_collect_params(stmt->args[i], space);
2237 space = expr_collect_params(stmt->body, space);
2239 return space;
2242 /* Add all parameters in "array" to "space" and return the result.
2244 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2245 __isl_take isl_space *space)
2247 if (!array)
2248 return isl_space_free(space);
2250 space = isl_space_align_params(space,
2251 isl_set_get_space(array->context));
2252 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2254 return space;
2257 /* Add all parameters in "scop" to "space" and return the result.
2259 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2260 __isl_take isl_space *space)
2262 int i;
2264 if (!scop)
2265 return isl_space_free(space);
2267 for (i = 0; i < scop->n_array; ++i)
2268 space = array_collect_params(scop->arrays[i], space);
2270 for (i = 0; i < scop->n_stmt; ++i)
2271 space = stmt_collect_params(scop->stmts[i], space);
2273 return space;
2276 /* Add all parameters in "space" to the domain, schedule and
2277 * all access relations in "stmt".
2279 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2280 __isl_take isl_space *space)
2282 int i;
2284 if (!stmt)
2285 goto error;
2287 stmt->domain = isl_set_align_params(stmt->domain,
2288 isl_space_copy(space));
2289 stmt->schedule = isl_map_align_params(stmt->schedule,
2290 isl_space_copy(space));
2292 for (i = 0; i < stmt->n_arg; ++i) {
2293 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2294 isl_space_copy(space));
2295 if (!stmt->args[i])
2296 goto error;
2298 stmt->body = pet_expr_align_params(stmt->body, isl_space_copy(space));
2300 if (!stmt->domain || !stmt->schedule || !stmt->body)
2301 goto error;
2303 isl_space_free(space);
2304 return stmt;
2305 error:
2306 isl_space_free(space);
2307 return pet_stmt_free(stmt);
2310 /* Add all parameters in "space" to "array".
2312 static struct pet_array *array_propagate_params(struct pet_array *array,
2313 __isl_take isl_space *space)
2315 if (!array)
2316 goto error;
2318 array->context = isl_set_align_params(array->context,
2319 isl_space_copy(space));
2320 array->extent = isl_set_align_params(array->extent,
2321 isl_space_copy(space));
2322 if (array->value_bounds) {
2323 array->value_bounds = isl_set_align_params(array->value_bounds,
2324 isl_space_copy(space));
2325 if (!array->value_bounds)
2326 goto error;
2329 if (!array->context || !array->extent)
2330 goto error;
2332 isl_space_free(space);
2333 return array;
2334 error:
2335 isl_space_free(space);
2336 return pet_array_free(array);
2339 /* Add all parameters in "space" to "scop".
2341 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2342 __isl_take isl_space *space)
2344 int i;
2346 if (!scop)
2347 goto error;
2349 for (i = 0; i < scop->n_array; ++i) {
2350 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2351 isl_space_copy(space));
2352 if (!scop->arrays[i])
2353 goto error;
2356 for (i = 0; i < scop->n_stmt; ++i) {
2357 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2358 isl_space_copy(space));
2359 if (!scop->stmts[i])
2360 goto error;
2363 isl_space_free(space);
2364 return scop;
2365 error:
2366 isl_space_free(space);
2367 return pet_scop_free(scop);
2370 /* Update all isl_sets and isl_maps in "scop" such that they all
2371 * have the same parameters.
2373 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2375 isl_space *space;
2377 if (!scop)
2378 return NULL;
2380 space = isl_set_get_space(scop->context);
2381 space = scop_collect_params(scop, space);
2383 scop->context = isl_set_align_params(scop->context,
2384 isl_space_copy(space));
2385 scop = scop_propagate_params(scop, space);
2387 if (scop && !scop->context)
2388 return pet_scop_free(scop);
2390 return scop;
2393 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2394 * in "space" by a value equal to the corresponding parameter.
2396 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2397 __isl_take isl_space *space)
2399 if (!stmt)
2400 goto error;
2402 stmt->body = pet_expr_detect_parameter_accesses(stmt->body,
2403 isl_space_copy(space));
2405 if (!stmt->domain || !stmt->schedule || !stmt->body)
2406 goto error;
2408 isl_space_free(space);
2409 return stmt;
2410 error:
2411 isl_space_free(space);
2412 return pet_stmt_free(stmt);
2415 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2416 * in "space" by a value equal to the corresponding parameter.
2418 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2419 __isl_take isl_space *space)
2421 int i;
2423 if (!scop)
2424 goto error;
2426 for (i = 0; i < scop->n_stmt; ++i) {
2427 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2428 isl_space_copy(space));
2429 if (!scop->stmts[i])
2430 goto error;
2433 isl_space_free(space);
2434 return scop;
2435 error:
2436 isl_space_free(space);
2437 return pet_scop_free(scop);
2440 /* Replace all accesses to (0D) arrays that correspond to any of
2441 * the parameters used in "scop" by a value equal
2442 * to the corresponding parameter.
2444 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2446 isl_space *space;
2448 if (!scop)
2449 return NULL;
2451 space = isl_set_get_space(scop->context);
2452 space = scop_collect_params(scop, space);
2454 scop = scop_detect_parameter_accesses(scop, space);
2456 return scop;
2459 /* Add the access relation of the access expression "expr" to "accesses" and
2460 * return the result.
2461 * The domain of the access relation is intersected with "domain".
2462 * If "tag" is set, then the access relation is tagged with
2463 * the corresponding reference identifier.
2465 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2466 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2468 isl_map *access;
2470 access = pet_expr_access_get_may_access(expr);
2471 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2472 if (tag)
2473 access = pet_expr_tag_access(expr, access);
2474 return isl_union_map_add_map(accesses, access);
2477 /* Add all read access relations (if "read" is set) and/or all write
2478 * access relations (if "write" is set) to "accesses" and return the result.
2479 * The domains of the access relations are intersected with "domain".
2480 * If "tag" is set, then the access relations are tagged with
2481 * the corresponding reference identifiers.
2483 * If "must" is set, then we only add the accesses that are definitely
2484 * performed. Otherwise, we add all potential accesses.
2485 * In particular, if the access has any arguments, then if "must" is
2486 * set we currently skip the access completely. If "must" is not set,
2487 * we project out the values of the access arguments.
2489 static __isl_give isl_union_map *expr_collect_accesses(
2490 __isl_keep pet_expr *expr, int read, int write, int must, int tag,
2491 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2493 int i;
2494 isl_id *id;
2495 isl_space *dim;
2497 if (!expr)
2498 return isl_union_map_free(accesses);
2500 for (i = 0; i < expr->n_arg; ++i)
2501 accesses = expr_collect_accesses(expr->args[i],
2502 read, write, must, tag, accesses, domain);
2504 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2505 ((read && expr->acc.read) || (write && expr->acc.write)) &&
2506 (!must || expr->n_arg == 0)) {
2507 accesses = expr_collect_access(expr, tag, accesses, domain);
2510 return accesses;
2513 /* Collect and return all read access relations (if "read" is set)
2514 * and/or all write access relations (if "write" is set) in "stmt".
2515 * If "tag" is set, then the access relations are tagged with
2516 * the corresponding reference identifiers.
2517 * If "kill" is set, then "stmt" is a kill statement and we simply
2518 * add the argument of the kill operation.
2520 * If "must" is set, then we only add the accesses that are definitely
2521 * performed. Otherwise, we add all potential accesses.
2522 * In particular, if the statement has any arguments, then if "must" is
2523 * set we currently skip the statement completely. If "must" is not set,
2524 * we project out the values of the statement arguments.
2526 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2527 int read, int write, int kill, int must, int tag,
2528 __isl_take isl_space *dim)
2530 isl_union_map *accesses;
2531 isl_set *domain;
2533 if (!stmt)
2534 return NULL;
2536 accesses = isl_union_map_empty(dim);
2538 if (must && stmt->n_arg > 0)
2539 return accesses;
2541 domain = isl_set_copy(stmt->domain);
2542 if (isl_set_is_wrapping(domain))
2543 domain = isl_map_domain(isl_set_unwrap(domain));
2545 if (kill)
2546 accesses = expr_collect_access(stmt->body->args[0], tag,
2547 accesses, domain);
2548 else
2549 accesses = expr_collect_accesses(stmt->body, read, write,
2550 must, tag, accesses, domain);
2551 isl_set_free(domain);
2553 return accesses;
2556 /* Is "stmt" an assignment statement?
2558 int pet_stmt_is_assign(struct pet_stmt *stmt)
2560 if (!stmt)
2561 return 0;
2562 if (stmt->body->type != pet_expr_op)
2563 return 0;
2564 return stmt->body->op == pet_op_assign;
2567 /* Is "stmt" a kill statement?
2569 int pet_stmt_is_kill(struct pet_stmt *stmt)
2571 if (!stmt)
2572 return 0;
2573 if (stmt->body->type != pet_expr_op)
2574 return 0;
2575 return stmt->body->op == pet_op_kill;
2578 /* Is "stmt" an assume statement?
2580 int pet_stmt_is_assume(struct pet_stmt *stmt)
2582 if (!stmt)
2583 return 0;
2584 return pet_expr_is_assume(stmt->body);
2587 /* Compute a mapping from all arrays (of structs) in scop
2588 * to their innermost arrays.
2590 * In particular, for each array of a primitive type, the result
2591 * contains the identity mapping on that array.
2592 * For each array involving member accesses, the result
2593 * contains a mapping from the elements of any intermediate array of structs
2594 * to all corresponding elements of the innermost nested arrays.
2596 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2598 int i;
2599 isl_union_map *to_inner;
2601 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2603 for (i = 0; i < scop->n_array; ++i) {
2604 struct pet_array *array = scop->arrays[i];
2605 isl_set *set;
2606 isl_map *map, *gist;
2608 if (array->element_is_record)
2609 continue;
2611 map = isl_set_identity(isl_set_copy(array->extent));
2613 set = isl_map_domain(isl_map_copy(map));
2614 gist = isl_map_copy(map);
2615 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2616 to_inner = isl_union_map_add_map(to_inner, gist);
2618 while (set && isl_set_is_wrapping(set)) {
2619 isl_id *id;
2620 isl_map *wrapped;
2622 id = isl_set_get_tuple_id(set);
2623 wrapped = isl_set_unwrap(set);
2624 wrapped = isl_map_domain_map(wrapped);
2625 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2626 map = isl_map_apply_domain(map, wrapped);
2627 set = isl_map_domain(isl_map_copy(map));
2628 gist = isl_map_copy(map);
2629 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2630 to_inner = isl_union_map_add_map(to_inner, gist);
2633 isl_set_free(set);
2634 isl_map_free(map);
2637 return to_inner;
2640 /* Collect and return all read access relations (if "read" is set)
2641 * and/or all write access relations (if "write" is set) in "scop".
2642 * If "kill" is set, then we only add the arguments of kill operations.
2643 * If "must" is set, then we only add the accesses that are definitely
2644 * performed. Otherwise, we add all potential accesses.
2645 * If "tag" is set, then the access relations are tagged with
2646 * the corresponding reference identifiers.
2647 * For accesses to structures, the returned access relation accesses
2648 * all individual fields in the structures.
2650 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2651 int read, int write, int kill, int must, int tag)
2653 int i;
2654 isl_union_map *accesses;
2655 isl_union_set *arrays;
2656 isl_union_map *to_inner;
2658 if (!scop)
2659 return NULL;
2661 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2663 for (i = 0; i < scop->n_stmt; ++i) {
2664 struct pet_stmt *stmt = scop->stmts[i];
2665 isl_union_map *accesses_i;
2666 isl_space *space;
2668 if (kill && !pet_stmt_is_kill(stmt))
2669 continue;
2671 space = isl_set_get_space(scop->context);
2672 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2673 must, tag, space);
2674 accesses = isl_union_map_union(accesses, accesses_i);
2677 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2678 for (i = 0; i < scop->n_array; ++i) {
2679 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2680 arrays = isl_union_set_add_set(arrays, extent);
2682 accesses = isl_union_map_intersect_range(accesses, arrays);
2684 to_inner = compute_to_inner(scop);
2685 accesses = isl_union_map_apply_range(accesses, to_inner);
2687 return accesses;
2690 /* Collect all potential read access relations.
2692 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2694 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2697 /* Collect all potential write access relations.
2699 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2701 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2704 /* Collect all definite write access relations.
2706 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2708 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2711 /* Collect all definite kill access relations.
2713 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2715 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2718 /* Collect all tagged potential read access relations.
2720 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2721 struct pet_scop *scop)
2723 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2726 /* Collect all tagged potential write access relations.
2728 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2729 struct pet_scop *scop)
2731 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2734 /* Collect all tagged definite write access relations.
2736 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2737 struct pet_scop *scop)
2739 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2742 /* Collect all tagged definite kill access relations.
2744 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2745 struct pet_scop *scop)
2747 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2750 /* Collect and return the union of iteration domains in "scop".
2752 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2754 int i;
2755 isl_set *domain_i;
2756 isl_union_set *domain;
2758 if (!scop)
2759 return NULL;
2761 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2763 for (i = 0; i < scop->n_stmt; ++i) {
2764 domain_i = isl_set_copy(scop->stmts[i]->domain);
2765 domain = isl_union_set_add_set(domain, domain_i);
2768 return domain;
2771 /* Collect and return the schedules of the statements in "scop".
2772 * The range is normalized to the maximal number of scheduling
2773 * dimensions.
2775 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2777 int i, j;
2778 isl_map *schedule_i;
2779 isl_union_map *schedule;
2780 int depth, max_depth = 0;
2782 if (!scop)
2783 return NULL;
2785 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2787 for (i = 0; i < scop->n_stmt; ++i) {
2788 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2789 if (depth > max_depth)
2790 max_depth = depth;
2793 for (i = 0; i < scop->n_stmt; ++i) {
2794 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2795 depth = isl_map_dim(schedule_i, isl_dim_out);
2796 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2797 max_depth - depth);
2798 for (j = depth; j < max_depth; ++j)
2799 schedule_i = isl_map_fix_si(schedule_i,
2800 isl_dim_out, j, 0);
2801 schedule = isl_union_map_add_map(schedule, schedule_i);
2804 return schedule;
2807 /* Does statement "stmt" write to "id"?
2809 static int stmt_writes(struct pet_stmt *stmt, __isl_keep isl_id *id)
2811 return pet_expr_writes(stmt->body, id);
2814 /* Is there any write access in "scop" that accesses "id"?
2816 int pet_scop_writes(struct pet_scop *scop, __isl_keep isl_id *id)
2818 int i;
2820 if (!scop)
2821 return -1;
2823 for (i = 0; i < scop->n_stmt; ++i) {
2824 int writes = stmt_writes(scop->stmts[i], id);
2825 if (writes < 0 || writes)
2826 return writes;
2829 return 0;
2832 /* Add a reference identifier to all access expressions in "stmt".
2833 * "n_ref" points to an integer that contains the sequence number
2834 * of the next reference.
2836 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2838 int i;
2840 if (!stmt)
2841 return NULL;
2843 for (i = 0; i < stmt->n_arg; ++i) {
2844 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2845 if (!stmt->args[i])
2846 return pet_stmt_free(stmt);
2849 stmt->body = pet_expr_add_ref_ids(stmt->body, n_ref);
2850 if (!stmt->body)
2851 return pet_stmt_free(stmt);
2853 return stmt;
2856 /* Add a reference identifier to all access expressions in "scop".
2858 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2860 int i;
2861 int n_ref;
2863 if (!scop)
2864 return NULL;
2866 n_ref = 0;
2867 for (i = 0; i < scop->n_stmt; ++i) {
2868 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
2869 if (!scop->stmts[i])
2870 return pet_scop_free(scop);
2873 return scop;
2876 /* Reset the user pointer on all parameter ids in "array".
2878 static struct pet_array *array_anonymize(struct pet_array *array)
2880 if (!array)
2881 return NULL;
2883 array->context = isl_set_reset_user(array->context);
2884 array->extent = isl_set_reset_user(array->extent);
2885 if (!array->context || !array->extent)
2886 return pet_array_free(array);
2888 return array;
2891 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2893 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
2895 int i;
2896 isl_space *space;
2897 isl_set *domain;
2899 if (!stmt)
2900 return NULL;
2902 stmt->domain = isl_set_reset_user(stmt->domain);
2903 stmt->schedule = isl_map_reset_user(stmt->schedule);
2904 if (!stmt->domain || !stmt->schedule)
2905 return pet_stmt_free(stmt);
2907 for (i = 0; i < stmt->n_arg; ++i) {
2908 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
2909 if (!stmt->args[i])
2910 return pet_stmt_free(stmt);
2913 stmt->body = pet_expr_anonymize(stmt->body);
2914 if (!stmt->body)
2915 return pet_stmt_free(stmt);
2917 return stmt;
2920 /* Reset the user pointer on the tuple ids and all parameter ids
2921 * in "implication".
2923 static struct pet_implication *implication_anonymize(
2924 struct pet_implication *implication)
2926 if (!implication)
2927 return NULL;
2929 implication->extension = isl_map_reset_user(implication->extension);
2930 if (!implication->extension)
2931 return pet_implication_free(implication);
2933 return implication;
2936 /* Reset the user pointer on all parameter and tuple ids in "scop".
2938 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
2940 int i;
2942 if (!scop)
2943 return NULL;
2945 scop->context = isl_set_reset_user(scop->context);
2946 scop->context_value = isl_set_reset_user(scop->context_value);
2947 if (!scop->context || !scop->context_value)
2948 return pet_scop_free(scop);
2950 for (i = 0; i < scop->n_array; ++i) {
2951 scop->arrays[i] = array_anonymize(scop->arrays[i]);
2952 if (!scop->arrays[i])
2953 return pet_scop_free(scop);
2956 for (i = 0; i < scop->n_stmt; ++i) {
2957 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
2958 if (!scop->stmts[i])
2959 return pet_scop_free(scop);
2962 for (i = 0; i < scop->n_implication; ++i) {
2963 scop->implications[i] =
2964 implication_anonymize(scop->implications[i]);
2965 if (!scop->implications[i])
2966 return pet_scop_free(scop);
2969 return scop;
2972 /* Compute the gist of the iteration domain and all access relations
2973 * of "stmt" based on the constraints on the parameters specified by "context"
2974 * and the constraints on the values of nested accesses specified
2975 * by "value_bounds".
2977 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
2978 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
2980 int i;
2981 isl_set *domain;
2983 if (!stmt)
2984 return NULL;
2986 domain = isl_set_copy(stmt->domain);
2987 if (stmt->n_arg > 0)
2988 domain = isl_map_domain(isl_set_unwrap(domain));
2990 domain = isl_set_intersect_params(domain, isl_set_copy(context));
2992 for (i = 0; i < stmt->n_arg; ++i) {
2993 stmt->args[i] = pet_expr_gist(stmt->args[i],
2994 domain, value_bounds);
2995 if (!stmt->args[i])
2996 goto error;
2999 stmt->body = pet_expr_gist(stmt->body, domain, value_bounds);
3000 if (!stmt->body)
3001 goto error;
3003 isl_set_free(domain);
3005 domain = isl_set_universe(pet_stmt_get_space(stmt));
3006 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3007 if (stmt->n_arg > 0)
3008 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3009 value_bounds);
3010 stmt->domain = isl_set_gist(stmt->domain, domain);
3011 if (!stmt->domain)
3012 return pet_stmt_free(stmt);
3014 return stmt;
3015 error:
3016 isl_set_free(domain);
3017 return pet_stmt_free(stmt);
3020 /* Compute the gist of the extent of the array
3021 * based on the constraints on the parameters specified by "context".
3023 static struct pet_array *array_gist(struct pet_array *array,
3024 __isl_keep isl_set *context)
3026 if (!array)
3027 return NULL;
3029 array->extent = isl_set_gist_params(array->extent,
3030 isl_set_copy(context));
3031 if (!array->extent)
3032 return pet_array_free(array);
3034 return array;
3037 /* Compute the gist of all sets and relations in "scop"
3038 * based on the constraints on the parameters specified by "scop->context"
3039 * and the constraints on the values of nested accesses specified
3040 * by "value_bounds".
3042 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3043 __isl_keep isl_union_map *value_bounds)
3045 int i;
3047 if (!scop)
3048 return NULL;
3050 scop->context = isl_set_coalesce(scop->context);
3051 if (!scop->context)
3052 return pet_scop_free(scop);
3054 for (i = 0; i < scop->n_array; ++i) {
3055 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3056 if (!scop->arrays[i])
3057 return pet_scop_free(scop);
3060 for (i = 0; i < scop->n_stmt; ++i) {
3061 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3062 value_bounds);
3063 if (!scop->stmts[i])
3064 return pet_scop_free(scop);
3067 return scop;
3070 /* Intersect the context of "scop" with "context".
3071 * To ensure that we don't introduce any unnamed parameters in
3072 * the context of "scop", we first remove the unnamed parameters
3073 * from "context".
3075 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3076 __isl_take isl_set *context)
3078 if (!scop)
3079 goto error;
3081 context = pet_nested_remove_from_set(context);
3082 scop->context = isl_set_intersect(scop->context, context);
3083 if (!scop->context)
3084 return pet_scop_free(scop);
3086 return scop;
3087 error:
3088 isl_set_free(context);
3089 return pet_scop_free(scop);
3092 /* Drop the current context of "scop". That is, replace the context
3093 * by a universal set.
3095 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3097 isl_space *space;
3099 if (!scop)
3100 return NULL;
3102 space = isl_set_get_space(scop->context);
3103 isl_set_free(scop->context);
3104 scop->context = isl_set_universe(space);
3105 if (!scop->context)
3106 return pet_scop_free(scop);
3108 return scop;
3111 /* Append "array" to the arrays of "scop".
3113 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3114 struct pet_array *array)
3116 isl_ctx *ctx;
3117 struct pet_array **arrays;
3119 if (!array || !scop)
3120 goto error;
3122 ctx = isl_set_get_ctx(scop->context);
3123 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3124 scop->n_array + 1);
3125 if (!arrays)
3126 goto error;
3127 scop->arrays = arrays;
3128 scop->arrays[scop->n_array] = array;
3129 scop->n_array++;
3131 return scop;
3132 error:
3133 pet_array_free(array);
3134 return pet_scop_free(scop);
3137 /* Create an index expression for an access to a virtual array
3138 * representing the result of a condition.
3139 * Unlike other accessed data, the id of the array is NULL as
3140 * there is no ValueDecl in the program corresponding to the virtual
3141 * array.
3142 * The array starts out as a scalar, but grows along with the
3143 * statement writing to the array in pet_scop_embed.
3145 __isl_give isl_multi_pw_aff *pet_create_test_index(isl_ctx *ctx, int test_nr)
3147 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
3148 isl_id *id;
3149 char name[50];
3151 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3152 id = isl_id_alloc(ctx, name, NULL);
3153 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
3154 return isl_multi_pw_aff_zero(dim);
3157 /* Add an array with the given extent (range of "index") to the list
3158 * of arrays in "scop" and return the extended pet_scop.
3159 * "int_size" is the number of bytes needed to represent values of type "int".
3160 * The array is marked as attaining values 0 and 1 only and
3161 * as each element being assigned at most once.
3163 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3164 __isl_take isl_multi_pw_aff *index, int int_size)
3166 isl_ctx *ctx;
3167 isl_space *space;
3168 struct pet_array *array;
3169 isl_map *access;
3171 if (!scop || !index)
3172 goto error;
3174 ctx = isl_multi_pw_aff_get_ctx(index);
3175 array = isl_calloc_type(ctx, struct pet_array);
3176 if (!array)
3177 goto error;
3179 access = isl_map_from_multi_pw_aff(index);
3180 array->extent = isl_map_range(access);
3181 space = isl_space_params_alloc(ctx, 0);
3182 array->context = isl_set_universe(space);
3183 space = isl_space_set_alloc(ctx, 0, 1);
3184 array->value_bounds = isl_set_universe(space);
3185 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3186 isl_dim_set, 0, 0);
3187 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3188 isl_dim_set, 0, 1);
3189 array->element_type = strdup("int");
3190 array->element_size = int_size;
3191 array->uniquely_defined = 1;
3193 if (!array->extent || !array->context)
3194 array = pet_array_free(array);
3196 scop = pet_scop_add_array(scop, array);
3198 return scop;
3199 error:
3200 isl_multi_pw_aff_free(index);
3201 return pet_scop_free(scop);
3204 /* Create and return an implication on filter values equal to "satisfied"
3205 * with extension "map".
3207 static struct pet_implication *new_implication(__isl_take isl_map *map,
3208 int satisfied)
3210 isl_ctx *ctx;
3211 struct pet_implication *implication;
3213 if (!map)
3214 return NULL;
3215 ctx = isl_map_get_ctx(map);
3216 implication = isl_alloc_type(ctx, struct pet_implication);
3217 if (!implication)
3218 goto error;
3220 implication->extension = map;
3221 implication->satisfied = satisfied;
3223 return implication;
3224 error:
3225 isl_map_free(map);
3226 return NULL;
3229 /* Add an implication on filter values equal to "satisfied"
3230 * with extension "map" to "scop".
3232 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3233 __isl_take isl_map *map, int satisfied)
3235 isl_ctx *ctx;
3236 struct pet_implication *implication;
3237 struct pet_implication **implications;
3239 implication = new_implication(map, satisfied);
3240 if (!scop || !implication)
3241 goto error;
3243 ctx = isl_set_get_ctx(scop->context);
3244 implications = isl_realloc_array(ctx, scop->implications,
3245 struct pet_implication *,
3246 scop->n_implication + 1);
3247 if (!implications)
3248 goto error;
3249 scop->implications = implications;
3250 scop->implications[scop->n_implication] = implication;
3251 scop->n_implication++;
3253 return scop;
3254 error:
3255 pet_implication_free(implication);
3256 return pet_scop_free(scop);
3259 /* Given an access expression, check if it is data dependent.
3260 * If so, set *found and abort the search.
3262 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3264 int *found = user;
3266 if (pet_expr_get_n_arg(expr) > 0) {
3267 *found = 1;
3268 return -1;
3271 return 0;
3274 /* Does "scop" contain any data dependent accesses?
3276 * Check the body of each statement for such accesses.
3278 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3280 int i;
3281 int found = 0;
3283 if (!scop)
3284 return -1;
3286 for (i = 0; i < scop->n_stmt; ++i) {
3287 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
3288 &is_data_dependent, &found);
3289 if (r < 0 && !found)
3290 return -1;
3291 if (found)
3292 return found;
3295 return found;
3298 /* Does "scop" contain and data dependent conditions?
3300 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3302 int i;
3304 if (!scop)
3305 return -1;
3307 for (i = 0; i < scop->n_stmt; ++i)
3308 if (scop->stmts[i]->n_arg > 0)
3309 return 1;
3311 return 0;
3314 /* Keep track of the "input" file inside the (extended) "scop".
3316 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3318 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3320 if (!scop)
3321 return NULL;
3323 ext->input = input;
3325 return scop;
3328 /* Print the original code corresponding to "scop" to printer "p".
3330 * pet_scop_print_original can only be called from
3331 * a pet_transform_C_source callback. This means that the input
3332 * file is stored in the extended scop and that the printer prints
3333 * to a file.
3335 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3336 __isl_take isl_printer *p)
3338 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3339 FILE *output;
3340 unsigned start, end;
3342 if (!scop || !p)
3343 return isl_printer_free(p);
3345 if (!ext->input)
3346 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3347 "no input file stored in scop",
3348 return isl_printer_free(p));
3350 output = isl_printer_get_file(p);
3351 if (!output)
3352 return isl_printer_free(p);
3354 start = pet_loc_get_start(scop->loc);
3355 end = pet_loc_get_end(scop->loc);
3356 if (copy(ext->input, output, start, end) < 0)
3357 return isl_printer_free(p);
3359 return p;