tree2scop.c: scop_from_affine_for: introduce is_non_affine variable
[pet.git] / scop.c
blob9042310e2fdfde5f26c05fd08b5f3363483e9f81
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <string.h>
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
39 #include "aff.h"
40 #include "expr.h"
41 #include "filter.h"
42 #include "loc.h"
43 #include "nest.h"
44 #include "scop.h"
45 #include "print.h"
46 #include "value_bounds.h"
48 /* pet_scop with extra information that is used during parsing and printing.
50 * In particular, we keep track of conditions under which we want
51 * to skip the rest of the current loop iteration (skip[pet_skip_now])
52 * and of conditions under which we want to skip subsequent
53 * loop iterations (skip[pet_skip_later]).
55 * The conditions are represented as index expressions defined
56 * over a zero-dimensional domain. The index expression is either
57 * a boolean affine expression or an access to a variable, which
58 * is assumed to attain values zero and one. The condition holds
59 * if the variable has value one or if the affine expression
60 * has value one (typically for only part of the parameter space).
62 * A missing condition (skip[type] == NULL) means that we don't want
63 * to skip anything.
65 * Additionally, we keep track of the original input file
66 * inside pet_transform_C_source.
68 struct pet_scop_ext {
69 struct pet_scop scop;
71 isl_multi_pw_aff *skip[2];
72 FILE *input;
75 /* Construct a pet_stmt with given location and statement
76 * number from a pet_expr.
77 * The initial iteration domain is the zero-dimensional universe.
78 * The name of the domain is given by "label" if it is non-NULL.
79 * Otherwise, the name is constructed as S_<id>.
80 * The domains of all access relations are modified to refer
81 * to the statement iteration domain.
83 struct pet_stmt *pet_stmt_from_pet_expr(__isl_take pet_loc *loc,
84 __isl_take isl_id *label, int id, __isl_take pet_expr *expr)
86 struct pet_stmt *stmt;
87 isl_ctx *ctx;
88 isl_space *dim;
89 isl_set *dom;
90 isl_map *sched;
91 isl_multi_pw_aff *add_name;
92 char name[50];
94 if (!loc || !expr)
95 goto error;
97 ctx = pet_expr_get_ctx(expr);
98 stmt = isl_calloc_type(ctx, struct pet_stmt);
99 if (!stmt)
100 goto error;
102 dim = isl_space_set_alloc(ctx, 0, 0);
103 if (label)
104 dim = isl_space_set_tuple_id(dim, isl_dim_set, label);
105 else {
106 snprintf(name, sizeof(name), "S_%d", id);
107 dim = isl_space_set_tuple_name(dim, isl_dim_set, name);
109 dom = isl_set_universe(isl_space_copy(dim));
110 sched = isl_map_from_domain(isl_set_copy(dom));
112 dim = isl_space_from_domain(dim);
113 add_name = isl_multi_pw_aff_zero(dim);
114 expr = pet_expr_update_domain(expr, add_name);
116 stmt->loc = loc;
117 stmt->domain = dom;
118 stmt->schedule = sched;
119 stmt->body = expr;
121 if (!stmt->domain || !stmt->schedule || !stmt->body)
122 return pet_stmt_free(stmt);
124 return stmt;
125 error:
126 isl_id_free(label);
127 pet_loc_free(loc);
128 pet_expr_free(expr);
129 return NULL;
132 void *pet_stmt_free(struct pet_stmt *stmt)
134 int i;
136 if (!stmt)
137 return NULL;
139 pet_loc_free(stmt->loc);
140 isl_set_free(stmt->domain);
141 isl_map_free(stmt->schedule);
142 pet_expr_free(stmt->body);
144 for (i = 0; i < stmt->n_arg; ++i)
145 pet_expr_free(stmt->args[i]);
146 free(stmt->args);
148 free(stmt);
149 return NULL;
152 /* Return the iteration space of "stmt".
154 * If the statement has arguments, then stmt->domain is a wrapped map
155 * mapping the iteration domain to the values of the arguments
156 * for which this statement is executed.
157 * In this case, we need to extract the domain space of this wrapped map.
159 __isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
161 isl_space *space;
163 if (!stmt)
164 return NULL;
166 space = isl_set_get_space(stmt->domain);
167 if (isl_space_is_wrapping(space))
168 space = isl_space_domain(isl_space_unwrap(space));
170 return space;
173 static void stmt_dump(struct pet_stmt *stmt, int indent)
175 int i;
177 if (!stmt)
178 return;
180 fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
181 fprintf(stderr, "%*s", indent, "");
182 isl_set_dump(stmt->domain);
183 fprintf(stderr, "%*s", indent, "");
184 isl_map_dump(stmt->schedule);
185 pet_expr_dump_with_indent(stmt->body, indent);
186 for (i = 0; i < stmt->n_arg; ++i)
187 pet_expr_dump_with_indent(stmt->args[i], indent + 2);
190 void pet_stmt_dump(struct pet_stmt *stmt)
192 stmt_dump(stmt, 0);
195 /* Allocate a new pet_type with the given "name" and "definition".
197 struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
198 const char *definition)
200 struct pet_type *type;
202 type = isl_alloc_type(ctx, struct pet_type);
203 if (!type)
204 return NULL;
206 type->name = strdup(name);
207 type->definition = strdup(definition);
209 if (!type->name || !type->definition)
210 return pet_type_free(type);
212 return type;
215 /* Free "type" and return NULL.
217 struct pet_type *pet_type_free(struct pet_type *type)
219 if (!type)
220 return NULL;
222 free(type->name);
223 free(type->definition);
225 free(type);
226 return NULL;
229 struct pet_array *pet_array_free(struct pet_array *array)
231 if (!array)
232 return NULL;
234 isl_set_free(array->context);
235 isl_set_free(array->extent);
236 isl_set_free(array->value_bounds);
237 free(array->element_type);
239 free(array);
240 return NULL;
243 void pet_array_dump(struct pet_array *array)
245 if (!array)
246 return;
248 isl_set_dump(array->context);
249 isl_set_dump(array->extent);
250 isl_set_dump(array->value_bounds);
251 fprintf(stderr, "%s%s%s\n", array->element_type,
252 array->element_is_record ? " element-is-record" : "",
253 array->live_out ? " live-out" : "");
256 /* Alloc a pet_scop structure, with extra room for information that
257 * is only used during parsing.
259 struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
261 return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
264 /* Construct a pet_scop with room for n statements.
266 * Since no information on the location is known at this point,
267 * scop->loc is initialized with pet_loc_dummy.
269 static struct pet_scop *scop_alloc(isl_ctx *ctx, int n)
271 isl_space *space;
272 struct pet_scop *scop;
274 scop = pet_scop_alloc(ctx);
275 if (!scop)
276 return NULL;
278 space = isl_space_params_alloc(ctx, 0);
279 scop->context = isl_set_universe(isl_space_copy(space));
280 scop->context_value = isl_set_universe(space);
281 scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
282 if (!scop->context || !scop->stmts)
283 return pet_scop_free(scop);
285 scop->loc = &pet_loc_dummy;
286 scop->n_stmt = n;
288 return scop;
291 struct pet_scop *pet_scop_empty(isl_ctx *ctx)
293 return scop_alloc(ctx, 0);
296 /* Update "context" with respect to the valid parameter values for "access".
298 static __isl_give isl_set *access_extract_context(__isl_keep isl_map *access,
299 __isl_take isl_set *context)
301 context = isl_set_intersect(context,
302 isl_map_params(isl_map_copy(access)));
303 return context;
306 /* Update "context" with respect to the valid parameter values for "expr".
308 * If "expr" represents a conditional operator, then a parameter value
309 * needs to be valid for the condition and for at least one of the
310 * remaining two arguments.
311 * If the condition is an affine expression, then we can be a bit more specific.
312 * The parameter then has to be valid for the second argument for
313 * non-zero accesses and valid for the third argument for zero accesses.
315 static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
316 __isl_take isl_set *context)
318 int i;
320 if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
321 int is_aff;
322 isl_set *context1, *context2;
324 is_aff = pet_expr_is_affine(expr->args[0]);
325 if (is_aff < 0)
326 goto error;
328 context = expr_extract_context(expr->args[0], context);
329 context1 = expr_extract_context(expr->args[1],
330 isl_set_copy(context));
331 context2 = expr_extract_context(expr->args[2], context);
333 if (is_aff) {
334 isl_map *access;
335 isl_set *zero_set;
337 access = isl_map_copy(expr->args[0]->acc.access);
338 access = isl_map_fix_si(access, isl_dim_out, 0, 0);
339 zero_set = isl_map_params(access);
340 context1 = isl_set_subtract(context1,
341 isl_set_copy(zero_set));
342 context2 = isl_set_intersect(context2, zero_set);
345 context = isl_set_union(context1, context2);
346 context = isl_set_coalesce(context);
348 return context;
351 for (i = 0; i < expr->n_arg; ++i)
352 context = expr_extract_context(expr->args[i], context);
354 if (expr->type == pet_expr_access)
355 context = access_extract_context(expr->acc.access, context);
357 return context;
358 error:
359 isl_set_free(context);
360 return NULL;
363 /* Update "context" with respect to the valid parameter values for "stmt".
365 * If the statement is an assume statement with an affine expression,
366 * then intersect "context" with that expression.
367 * Otherwise, intersect "context" with the contexts of the expressions
368 * inside "stmt".
370 static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
371 __isl_take isl_set *context)
373 int i;
375 if (pet_stmt_is_assume(stmt) &&
376 pet_expr_is_affine(stmt->body->args[0])) {
377 isl_multi_pw_aff *index;
378 isl_pw_aff *pa;
379 isl_set *cond;
381 index = stmt->body->args[0]->acc.index;
382 pa = isl_multi_pw_aff_get_pw_aff(index, 0);
383 cond = isl_set_params(isl_pw_aff_non_zero_set(pa));
384 return isl_set_intersect(context, cond);
387 for (i = 0; i < stmt->n_arg; ++i)
388 context = expr_extract_context(stmt->args[i], context);
390 context = expr_extract_context(stmt->body, context);
392 return context;
395 /* Construct a pet_scop that contains the given pet_stmt.
397 struct pet_scop *pet_scop_from_pet_stmt(isl_ctx *ctx, struct pet_stmt *stmt)
399 struct pet_scop *scop;
401 if (!stmt)
402 return NULL;
404 scop = scop_alloc(ctx, 1);
405 if (!scop)
406 goto error;
408 scop->context = stmt_extract_context(stmt, scop->context);
409 if (!scop->context)
410 goto error;
412 scop->stmts[0] = stmt;
413 scop->loc = pet_loc_copy(stmt->loc);
415 if (!scop->loc)
416 return pet_scop_free(scop);
418 return scop;
419 error:
420 pet_stmt_free(stmt);
421 pet_scop_free(scop);
422 return NULL;
425 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
426 * does it represent an affine expression?
428 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
430 int has_id;
432 has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
433 if (has_id < 0)
434 return -1;
436 return !has_id;
439 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
441 static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
442 __isl_take isl_set *dom)
444 isl_pw_aff *pa;
445 pa = isl_set_indicator_function(set);
446 pa = isl_pw_aff_intersect_domain(pa, dom);
447 return pa;
450 /* Return "lhs || rhs", defined on the shared definition domain.
452 static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
453 __isl_take isl_pw_aff *rhs)
455 isl_set *cond;
456 isl_set *dom;
458 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
459 isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
460 cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
461 isl_pw_aff_non_zero_set(rhs));
462 cond = isl_set_coalesce(cond);
463 return indicator_function(cond, dom);
466 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
467 * ext may be equal to either ext1 or ext2.
469 * The two skips that need to be combined are assumed to be affine expressions.
471 * We need to skip in ext if we need to skip in either ext1 or ext2.
472 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
474 static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
475 struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
476 enum pet_skip type)
478 isl_pw_aff *skip, *skip1, *skip2;
480 if (!ext)
481 return NULL;
482 if (!ext1->skip[type] && !ext2->skip[type])
483 return ext;
484 if (!ext1->skip[type]) {
485 if (ext == ext2)
486 return ext;
487 ext->skip[type] = ext2->skip[type];
488 ext2->skip[type] = NULL;
489 return ext;
491 if (!ext2->skip[type]) {
492 if (ext == ext1)
493 return ext;
494 ext->skip[type] = ext1->skip[type];
495 ext1->skip[type] = NULL;
496 return ext;
499 if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
500 !multi_pw_aff_is_affine(ext2->skip[type]))
501 isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
502 isl_error_internal, "can only combine affine skips",
503 goto error);
505 skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
506 skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
507 skip = pw_aff_or(skip1, skip2);
508 isl_multi_pw_aff_free(ext1->skip[type]);
509 ext1->skip[type] = NULL;
510 isl_multi_pw_aff_free(ext2->skip[type]);
511 ext2->skip[type] = NULL;
512 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
513 if (!ext->skip[type])
514 goto error;
516 return ext;
517 error:
518 pet_scop_free(&ext->scop);
519 return NULL;
522 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
523 * where type takes on the values pet_skip_now and pet_skip_later.
524 * scop may be equal to either scop1 or scop2.
526 static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
527 struct pet_scop *scop1, struct pet_scop *scop2)
529 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
530 struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
531 struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
533 ext = combine_skips(ext, ext1, ext2, pet_skip_now);
534 ext = combine_skips(ext, ext1, ext2, pet_skip_later);
535 return &ext->scop;
538 /* Update start and end of scop->loc to include the region from "start"
539 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
540 * does not have any offset information yet and we simply take the information
541 * from "start" and "end". Otherwise, we update loc using "start" and "end".
543 struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
544 unsigned start, unsigned end)
546 if (!scop)
547 return NULL;
549 if (scop->loc == &pet_loc_dummy)
550 scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
551 start, end, -1);
552 else
553 scop->loc = pet_loc_update_start_end(scop->loc, start, end);
555 if (!scop->loc)
556 return pet_scop_free(scop);
558 return scop;
561 /* Update start and end of scop->loc to include the region identified
562 * by "loc".
564 struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
565 __isl_keep pet_loc *loc)
567 return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
568 pet_loc_get_end(loc));
571 /* Replace the location of "scop" by "loc".
573 struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
574 __isl_take pet_loc *loc)
576 if (!scop || !loc)
577 goto error;
579 pet_loc_free(scop->loc);
580 scop->loc = loc;
582 return scop;
583 error:
584 pet_loc_free(loc);
585 pet_scop_free(scop);
586 return NULL;
589 /* Does "implication" appear in the list of implications of "scop"?
591 static int is_known_implication(struct pet_scop *scop,
592 struct pet_implication *implication)
594 int i;
596 for (i = 0; i < scop->n_implication; ++i) {
597 struct pet_implication *pi = scop->implications[i];
598 int equal;
600 if (pi->satisfied != implication->satisfied)
601 continue;
602 equal = isl_map_is_equal(pi->extension, implication->extension);
603 if (equal < 0)
604 return -1;
605 if (equal)
606 return 1;
609 return 0;
612 /* Store the concatenation of the implications of "scop1" and "scop2"
613 * in "scop", removing duplicates (i.e., implications in "scop2" that
614 * already appear in "scop1").
616 static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
617 struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
619 int i, j;
621 if (!scop)
622 return NULL;
624 if (scop2->n_implication == 0) {
625 scop->n_implication = scop1->n_implication;
626 scop->implications = scop1->implications;
627 scop1->n_implication = 0;
628 scop1->implications = NULL;
629 return scop;
632 if (scop1->n_implication == 0) {
633 scop->n_implication = scop2->n_implication;
634 scop->implications = scop2->implications;
635 scop2->n_implication = 0;
636 scop2->implications = NULL;
637 return scop;
640 scop->implications = isl_calloc_array(ctx, struct pet_implication *,
641 scop1->n_implication + scop2->n_implication);
642 if (!scop->implications)
643 return pet_scop_free(scop);
645 for (i = 0; i < scop1->n_implication; ++i) {
646 scop->implications[i] = scop1->implications[i];
647 scop1->implications[i] = NULL;
650 scop->n_implication = scop1->n_implication;
651 j = scop1->n_implication;
652 for (i = 0; i < scop2->n_implication; ++i) {
653 int known;
655 known = is_known_implication(scop, scop2->implications[i]);
656 if (known < 0)
657 return pet_scop_free(scop);
658 if (known)
659 continue;
660 scop->implications[j++] = scop2->implications[i];
661 scop2->implications[i] = NULL;
663 scop->n_implication = j;
665 return scop;
668 /* Combine the offset information of "scop1" and "scop2" into "scop".
670 static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
671 struct pet_scop *scop1, struct pet_scop *scop2)
673 if (scop1->loc != &pet_loc_dummy)
674 scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
675 if (scop2->loc != &pet_loc_dummy)
676 scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
677 return scop;
680 /* Construct a pet_scop that contains the offset information,
681 * arrays, statements and skip information in "scop1" and "scop2".
683 static struct pet_scop *pet_scop_add(isl_ctx *ctx, struct pet_scop *scop1,
684 struct pet_scop *scop2)
686 int i;
687 struct pet_scop *scop = NULL;
689 if (!scop1 || !scop2)
690 goto error;
692 if (scop1->n_stmt == 0) {
693 scop2 = scop_combine_skips(scop2, scop1, scop2);
694 pet_scop_free(scop1);
695 return scop2;
698 if (scop2->n_stmt == 0) {
699 scop1 = scop_combine_skips(scop1, scop1, scop2);
700 pet_scop_free(scop2);
701 return scop1;
704 scop = scop_alloc(ctx, scop1->n_stmt + scop2->n_stmt);
705 if (!scop)
706 goto error;
708 scop->arrays = isl_calloc_array(ctx, struct pet_array *,
709 scop1->n_array + scop2->n_array);
710 if (!scop->arrays)
711 goto error;
712 scop->n_array = scop1->n_array + scop2->n_array;
714 for (i = 0; i < scop1->n_stmt; ++i) {
715 scop->stmts[i] = scop1->stmts[i];
716 scop1->stmts[i] = NULL;
719 for (i = 0; i < scop2->n_stmt; ++i) {
720 scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
721 scop2->stmts[i] = NULL;
724 for (i = 0; i < scop1->n_array; ++i) {
725 scop->arrays[i] = scop1->arrays[i];
726 scop1->arrays[i] = NULL;
729 for (i = 0; i < scop2->n_array; ++i) {
730 scop->arrays[scop1->n_array + i] = scop2->arrays[i];
731 scop2->arrays[i] = NULL;
734 scop = scop_collect_implications(ctx, scop, scop1, scop2);
735 scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
736 scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
737 scop = scop_combine_skips(scop, scop1, scop2);
738 scop = scop_combine_start_end(scop, scop1, scop2);
740 pet_scop_free(scop1);
741 pet_scop_free(scop2);
742 return scop;
743 error:
744 pet_scop_free(scop1);
745 pet_scop_free(scop2);
746 pet_scop_free(scop);
747 return NULL;
750 /* Apply the skip condition "skip" to "scop".
751 * That is, make sure "scop" is not executed when the condition holds.
753 * If "skip" is an affine expression, we add the conditions under
754 * which the expression is zero to the iteration domains.
755 * Otherwise, we add a filter on the variable attaining the value zero.
757 static struct pet_scop *restrict_skip(struct pet_scop *scop,
758 __isl_take isl_multi_pw_aff *skip)
760 isl_set *zero;
761 isl_pw_aff *pa;
762 int is_aff;
764 if (!scop || !skip)
765 goto error;
767 is_aff = multi_pw_aff_is_affine(skip);
768 if (is_aff < 0)
769 goto error;
771 if (!is_aff)
772 return pet_scop_filter(scop, skip, 0);
774 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
775 isl_multi_pw_aff_free(skip);
776 zero = isl_set_params(isl_pw_aff_zero_set(pa));
777 scop = pet_scop_restrict(scop, zero);
779 return scop;
780 error:
781 isl_multi_pw_aff_free(skip);
782 return pet_scop_free(scop);
785 /* Construct a pet_scop that contains the arrays, statements and
786 * skip information in "scop1" and "scop2", where the two scops
787 * are executed "in sequence". That is, breaks and continues
788 * in scop1 have an effect on scop2.
790 struct pet_scop *pet_scop_add_seq(isl_ctx *ctx, struct pet_scop *scop1,
791 struct pet_scop *scop2)
793 if (scop1 && pet_scop_has_skip(scop1, pet_skip_now))
794 scop2 = restrict_skip(scop2,
795 pet_scop_get_skip(scop1, pet_skip_now));
796 return pet_scop_add(ctx, scop1, scop2);
799 /* Construct a pet_scop that contains the arrays, statements and
800 * skip information in "scop1" and "scop2", where the two scops
801 * are executed "in parallel". That is, any break or continue
802 * in scop1 has no effect on scop2.
804 struct pet_scop *pet_scop_add_par(isl_ctx *ctx, struct pet_scop *scop1,
805 struct pet_scop *scop2)
807 return pet_scop_add(ctx, scop1, scop2);
810 void *pet_implication_free(struct pet_implication *implication)
812 int i;
814 if (!implication)
815 return NULL;
817 isl_map_free(implication->extension);
819 free(implication);
820 return NULL;
823 struct pet_scop *pet_scop_free(struct pet_scop *scop)
825 int i;
826 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
828 if (!scop)
829 return NULL;
830 pet_loc_free(scop->loc);
831 isl_set_free(scop->context);
832 isl_set_free(scop->context_value);
833 if (scop->types)
834 for (i = 0; i < scop->n_type; ++i)
835 pet_type_free(scop->types[i]);
836 free(scop->types);
837 if (scop->arrays)
838 for (i = 0; i < scop->n_array; ++i)
839 pet_array_free(scop->arrays[i]);
840 free(scop->arrays);
841 if (scop->stmts)
842 for (i = 0; i < scop->n_stmt; ++i)
843 pet_stmt_free(scop->stmts[i]);
844 free(scop->stmts);
845 if (scop->implications)
846 for (i = 0; i < scop->n_implication; ++i)
847 pet_implication_free(scop->implications[i]);
848 free(scop->implications);
849 isl_multi_pw_aff_free(ext->skip[pet_skip_now]);
850 isl_multi_pw_aff_free(ext->skip[pet_skip_later]);
851 free(scop);
852 return NULL;
855 void pet_type_dump(struct pet_type *type)
857 if (!type)
858 return;
860 fprintf(stderr, "%s -> %s\n", type->name, type->definition);
863 void pet_implication_dump(struct pet_implication *implication)
865 if (!implication)
866 return;
868 fprintf(stderr, "%d\n", implication->satisfied);
869 isl_map_dump(implication->extension);
872 void pet_scop_dump(struct pet_scop *scop)
874 int i;
875 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
877 if (!scop)
878 return;
880 isl_set_dump(scop->context);
881 isl_set_dump(scop->context_value);
882 for (i = 0; i < scop->n_type; ++i)
883 pet_type_dump(scop->types[i]);
884 for (i = 0; i < scop->n_array; ++i)
885 pet_array_dump(scop->arrays[i]);
886 for (i = 0; i < scop->n_stmt; ++i)
887 pet_stmt_dump(scop->stmts[i]);
888 for (i = 0; i < scop->n_implication; ++i)
889 pet_implication_dump(scop->implications[i]);
891 if (ext->skip[0]) {
892 fprintf(stderr, "skip\n");
893 isl_multi_pw_aff_dump(ext->skip[0]);
894 isl_multi_pw_aff_dump(ext->skip[1]);
898 /* Return 1 if the two pet_arrays are equivalent.
900 * We don't compare element_size as this may be target dependent.
902 int pet_array_is_equal(struct pet_array *array1, struct pet_array *array2)
904 if (!array1 || !array2)
905 return 0;
907 if (!isl_set_is_equal(array1->context, array2->context))
908 return 0;
909 if (!isl_set_is_equal(array1->extent, array2->extent))
910 return 0;
911 if (!!array1->value_bounds != !!array2->value_bounds)
912 return 0;
913 if (array1->value_bounds &&
914 !isl_set_is_equal(array1->value_bounds, array2->value_bounds))
915 return 0;
916 if (strcmp(array1->element_type, array2->element_type))
917 return 0;
918 if (array1->element_is_record != array2->element_is_record)
919 return 0;
920 if (array1->live_out != array2->live_out)
921 return 0;
922 if (array1->uniquely_defined != array2->uniquely_defined)
923 return 0;
924 if (array1->declared != array2->declared)
925 return 0;
926 if (array1->exposed != array2->exposed)
927 return 0;
929 return 1;
932 /* Return 1 if the two pet_stmts are equivalent.
934 int pet_stmt_is_equal(struct pet_stmt *stmt1, struct pet_stmt *stmt2)
936 int i;
938 if (!stmt1 || !stmt2)
939 return 0;
941 if (pet_loc_get_line(stmt1->loc) != pet_loc_get_line(stmt2->loc))
942 return 0;
943 if (!isl_set_is_equal(stmt1->domain, stmt2->domain))
944 return 0;
945 if (!isl_map_is_equal(stmt1->schedule, stmt2->schedule))
946 return 0;
947 if (!pet_expr_is_equal(stmt1->body, stmt2->body))
948 return 0;
949 if (stmt1->n_arg != stmt2->n_arg)
950 return 0;
951 for (i = 0; i < stmt1->n_arg; ++i) {
952 if (!pet_expr_is_equal(stmt1->args[i], stmt2->args[i]))
953 return 0;
956 return 1;
959 /* Return 1 if the two pet_types are equivalent.
961 * We only compare the names of the types since the exact representation
962 * of the definition may depend on the version of clang being used.
964 int pet_type_is_equal(struct pet_type *type1, struct pet_type *type2)
966 if (!type1 || !type2)
967 return 0;
969 if (strcmp(type1->name, type2->name))
970 return 0;
972 return 1;
975 /* Return 1 if the two pet_implications are equivalent.
977 int pet_implication_is_equal(struct pet_implication *implication1,
978 struct pet_implication *implication2)
980 if (!implication1 || !implication2)
981 return 0;
983 if (implication1->satisfied != implication2->satisfied)
984 return 0;
985 if (!isl_map_is_equal(implication1->extension, implication2->extension))
986 return 0;
988 return 1;
991 /* Return 1 if the two pet_scops are equivalent.
993 int pet_scop_is_equal(struct pet_scop *scop1, struct pet_scop *scop2)
995 int i;
997 if (!scop1 || !scop2)
998 return 0;
1000 if (!isl_set_is_equal(scop1->context, scop2->context))
1001 return 0;
1002 if (!isl_set_is_equal(scop1->context_value, scop2->context_value))
1003 return 0;
1005 if (scop1->n_type != scop2->n_type)
1006 return 0;
1007 for (i = 0; i < scop1->n_type; ++i)
1008 if (!pet_type_is_equal(scop1->types[i], scop2->types[i]))
1009 return 0;
1011 if (scop1->n_array != scop2->n_array)
1012 return 0;
1013 for (i = 0; i < scop1->n_array; ++i)
1014 if (!pet_array_is_equal(scop1->arrays[i], scop2->arrays[i]))
1015 return 0;
1017 if (scop1->n_stmt != scop2->n_stmt)
1018 return 0;
1019 for (i = 0; i < scop1->n_stmt; ++i)
1020 if (!pet_stmt_is_equal(scop1->stmts[i], scop2->stmts[i]))
1021 return 0;
1023 if (scop1->n_implication != scop2->n_implication)
1024 return 0;
1025 for (i = 0; i < scop1->n_implication; ++i)
1026 if (!pet_implication_is_equal(scop1->implications[i],
1027 scop2->implications[i]))
1028 return 0;
1030 return 1;
1033 /* Does the set "extent" reference a virtual array, i.e.,
1034 * one with user pointer equal to NULL?
1035 * A virtual array does not have any members.
1037 static int extent_is_virtual_array(__isl_keep isl_set *extent)
1039 isl_id *id;
1040 int is_virtual;
1042 if (!isl_set_has_tuple_id(extent))
1043 return 0;
1044 if (isl_set_is_wrapping(extent))
1045 return 0;
1046 id = isl_set_get_tuple_id(extent);
1047 is_virtual = !isl_id_get_user(id);
1048 isl_id_free(id);
1050 return is_virtual;
1053 /* Intersect the initial dimensions of "array" with "domain", provided
1054 * that "array" represents a virtual array.
1056 * If "array" is virtual, then We take the preimage of "domain"
1057 * over the projection of the extent of "array" onto its initial dimensions
1058 * and intersect this extent with the result.
1060 static struct pet_array *virtual_array_intersect_domain_prefix(
1061 struct pet_array *array, __isl_take isl_set *domain)
1063 int n;
1064 isl_space *space;
1065 isl_multi_aff *ma;
1067 if (!array || !extent_is_virtual_array(array->extent)) {
1068 isl_set_free(domain);
1069 return array;
1072 space = isl_set_get_space(array->extent);
1073 n = isl_set_dim(domain, isl_dim_set);
1074 ma = pet_prefix_projection(space, n);
1075 domain = isl_set_preimage_multi_aff(domain, ma);
1077 array->extent = isl_set_intersect(array->extent, domain);
1078 if (!array->extent)
1079 return pet_array_free(array);
1081 return array;
1084 /* Intersect the initial dimensions of the domain of "stmt"
1085 * with "domain".
1087 * We take the preimage of "domain" over the projection of the
1088 * domain of "stmt" onto its initial dimensions and intersect
1089 * the domain of "stmt" with the result.
1091 static struct pet_stmt *stmt_intersect_domain_prefix(struct pet_stmt *stmt,
1092 __isl_take isl_set *domain)
1094 int n;
1095 isl_space *space;
1096 isl_multi_aff *ma;
1098 if (!stmt)
1099 goto error;
1101 space = isl_set_get_space(stmt->domain);
1102 n = isl_set_dim(domain, isl_dim_set);
1103 ma = pet_prefix_projection(space, n);
1104 domain = isl_set_preimage_multi_aff(domain, ma);
1106 stmt->domain = isl_set_intersect(stmt->domain, domain);
1107 if (!stmt->domain)
1108 return pet_stmt_free(stmt);
1110 return stmt;
1111 error:
1112 isl_set_free(domain);
1113 return pet_stmt_free(stmt);
1116 /* Intersect the initial dimensions of the domain of "implication"
1117 * with "domain".
1119 * We take the preimage of "domain" over the projection of the
1120 * domain of "implication" onto its initial dimensions and intersect
1121 * the domain of "implication" with the result.
1123 static struct pet_implication *implication_intersect_domain_prefix(
1124 struct pet_implication *implication, __isl_take isl_set *domain)
1126 int n;
1127 isl_space *space;
1128 isl_multi_aff *ma;
1130 if (!implication)
1131 goto error;
1133 space = isl_map_get_space(implication->extension);
1134 n = isl_set_dim(domain, isl_dim_set);
1135 ma = pet_prefix_projection(isl_space_domain(space), n);
1136 domain = isl_set_preimage_multi_aff(domain, ma);
1138 implication->extension =
1139 isl_map_intersect_domain(implication->extension, domain);
1140 if (!implication->extension)
1141 return pet_implication_free(implication);
1143 return implication;
1144 error:
1145 isl_set_free(domain);
1146 return pet_implication_free(implication);
1149 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1151 * The extents of the virtual arrays match the iteration domains,
1152 * so if the iteration domain changes, we need to change those extents too.
1154 struct pet_scop *pet_scop_intersect_domain_prefix(struct pet_scop *scop,
1155 __isl_take isl_set *domain)
1157 int i;
1159 if (!scop)
1160 goto error;
1162 for (i = 0; i < scop->n_array; ++i) {
1163 scop->arrays[i] = virtual_array_intersect_domain_prefix(
1164 scop->arrays[i], isl_set_copy(domain));
1165 if (!scop->arrays[i])
1166 goto error;
1169 for (i = 0; i < scop->n_stmt; ++i) {
1170 scop->stmts[i] = stmt_intersect_domain_prefix(scop->stmts[i],
1171 isl_set_copy(domain));
1172 if (!scop->stmts[i])
1173 goto error;
1176 for (i = 0; i < scop->n_implication; ++i) {
1177 scop->implications[i] =
1178 implication_intersect_domain_prefix(scop->implications[i],
1179 isl_set_copy(domain));
1180 if (!scop->implications[i])
1181 return pet_scop_free(scop);
1184 isl_set_free(domain);
1185 return scop;
1186 error:
1187 isl_set_free(domain);
1188 return pet_scop_free(scop);
1191 /* Prefix the schedule of "stmt" with an extra dimension with constant
1192 * value "pos".
1194 struct pet_stmt *pet_stmt_prefix(struct pet_stmt *stmt, int pos)
1196 if (!stmt)
1197 return NULL;
1199 stmt->schedule = isl_map_insert_dims(stmt->schedule, isl_dim_out, 0, 1);
1200 stmt->schedule = isl_map_fix_si(stmt->schedule, isl_dim_out, 0, pos);
1201 if (!stmt->schedule)
1202 return pet_stmt_free(stmt);
1204 return stmt;
1207 /* Prefix the schedules of all statements in "scop" with an extra
1208 * dimension with constant value "pos".
1210 struct pet_scop *pet_scop_prefix(struct pet_scop *scop, int pos)
1212 int i;
1214 if (!scop)
1215 return NULL;
1217 for (i = 0; i < scop->n_stmt; ++i) {
1218 scop->stmts[i] = pet_stmt_prefix(scop->stmts[i], pos);
1219 if (!scop->stmts[i])
1220 return pet_scop_free(scop);
1223 return scop;
1226 /* Given a set with a parameter at "param_pos" that refers to the
1227 * iterator, "move" the iterator to the first set dimension.
1228 * That is, essentially equate the parameter to the first set dimension
1229 * and then project it out.
1231 * The first set dimension may however refer to a virtual iterator,
1232 * while the parameter refers to the "real" iterator.
1233 * We therefore need to take into account the affine expression "iv_map", which
1234 * expresses the real iterator in terms of the virtual iterator.
1235 * In particular, we equate the set dimension to the input of the map
1236 * and the parameter to the output of the map and then project out
1237 * everything we don't need anymore.
1239 static __isl_give isl_set *internalize_iv(__isl_take isl_set *set,
1240 int param_pos, __isl_take isl_aff *iv_map)
1242 isl_map *map, *map2;
1243 map = isl_map_from_domain(set);
1244 map = isl_map_add_dims(map, isl_dim_out, 1);
1245 map = isl_map_equate(map, isl_dim_in, 0, isl_dim_out, 0);
1246 map2 = isl_map_from_aff(iv_map);
1247 map2 = isl_map_align_params(map2, isl_map_get_space(map));
1248 map = isl_map_apply_range(map, map2);
1249 map = isl_map_equate(map, isl_dim_param, param_pos, isl_dim_out, 0);
1250 map = isl_map_project_out(map, isl_dim_param, param_pos, 1);
1251 return isl_map_domain(map);
1254 /* Data used in embed_access.
1255 * extend adds an iterator to the iteration domain (through precomposition).
1256 * iv_map expresses the real iterator in terms of the virtual iterator
1257 * var_id represents the induction variable of the corresponding loop
1259 struct pet_embed_access {
1260 isl_multi_pw_aff *extend;
1261 isl_aff *iv_map;
1262 isl_id *var_id;
1265 /* Given an index expression, return an expression for the outer iterator.
1267 static __isl_give isl_aff *index_outer_iterator(
1268 __isl_take isl_multi_pw_aff *index)
1270 isl_space *space;
1271 isl_local_space *ls;
1273 space = isl_multi_pw_aff_get_domain_space(index);
1274 isl_multi_pw_aff_free(index);
1276 ls = isl_local_space_from_space(space);
1277 return isl_aff_var_on_domain(ls, isl_dim_set, 0);
1280 /* Replace an index expression that references the new (outer) iterator variable
1281 * by one that references the corresponding (real) iterator.
1283 * The input index expression is of the form
1285 * { S[i',...] -> i[] }
1287 * where i' refers to the virtual iterator.
1289 * iv_map is of the form
1291 * { [i'] -> [i] }
1293 * Return the index expression
1295 * { S[i',...] -> [i] }
1297 static __isl_give isl_multi_pw_aff *replace_by_iterator(
1298 __isl_take isl_multi_pw_aff *index, __isl_take isl_aff *iv_map)
1300 isl_space *space;
1301 isl_aff *aff;
1303 aff = index_outer_iterator(index);
1304 space = isl_aff_get_space(aff);
1305 iv_map = isl_aff_align_params(iv_map, space);
1306 aff = isl_aff_pullback_aff(iv_map, aff);
1308 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1311 /* Given an index expression "index" that refers to the (real) iterator
1312 * through the parameter at position "pos", plug in "iv_map", expressing
1313 * the real iterator in terms of the virtual (outer) iterator.
1315 * In particular, the index expression is of the form
1317 * [..., i, ...] -> { S[i',...] -> ... i ... }
1319 * where i refers to the real iterator and i' refers to the virtual iterator.
1321 * iv_map is of the form
1323 * { [i'] -> [i] }
1325 * Return the index expression
1327 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1330 * We first move the parameter to the input
1332 * [..., ...] -> { [i, i',...] -> ... i ... }
1334 * and construct
1336 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1338 * and then combine the two to obtain the desired result.
1340 static __isl_give isl_multi_pw_aff *index_internalize_iv(
1341 __isl_take isl_multi_pw_aff *index, int pos, __isl_take isl_aff *iv_map)
1343 isl_space *space = isl_multi_pw_aff_get_domain_space(index);
1344 isl_multi_aff *ma;
1346 space = isl_space_drop_dims(space, isl_dim_param, pos, 1);
1347 index = isl_multi_pw_aff_move_dims(index, isl_dim_in, 0,
1348 isl_dim_param, pos, 1);
1350 space = isl_space_map_from_set(space);
1351 ma = isl_multi_aff_identity(isl_space_copy(space));
1352 iv_map = isl_aff_align_params(iv_map, space);
1353 iv_map = isl_aff_pullback_aff(iv_map, isl_multi_aff_get_aff(ma, 0));
1354 ma = isl_multi_aff_flat_range_product(
1355 isl_multi_aff_from_aff(iv_map), ma);
1356 index = isl_multi_pw_aff_pullback_multi_aff(index, ma);
1358 return index;
1361 /* Does the index expression "index" reference a virtual array, i.e.,
1362 * one with user pointer equal to NULL?
1363 * A virtual array does not have any members.
1365 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff *index)
1367 isl_id *id;
1368 int is_virtual;
1370 if (!isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1371 return 0;
1372 if (isl_multi_pw_aff_range_is_wrapping(index))
1373 return 0;
1374 id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1375 is_virtual = !isl_id_get_user(id);
1376 isl_id_free(id);
1378 return is_virtual;
1381 /* Does the access relation "access" reference a virtual array, i.e.,
1382 * one with user pointer equal to NULL?
1383 * A virtual array does not have any members.
1385 static int access_is_virtual_array(__isl_keep isl_map *access)
1387 isl_id *id;
1388 int is_virtual;
1390 if (!isl_map_has_tuple_id(access, isl_dim_out))
1391 return 0;
1392 if (isl_map_range_is_wrapping(access))
1393 return 0;
1394 id = isl_map_get_tuple_id(access, isl_dim_out);
1395 is_virtual = !isl_id_get_user(id);
1396 isl_id_free(id);
1398 return is_virtual;
1401 /* Embed the given index expression in an extra outer loop.
1402 * The domain of the index expression has already been updated.
1404 * If the access refers to the induction variable, then it is
1405 * turned into an access to the set of integers with index (and value)
1406 * equal to the induction variable.
1408 * If the accessed array is a virtual array (with user
1409 * pointer equal to NULL), as created by create_test_index,
1410 * then it is extended along with the domain of the index expression.
1412 static __isl_give isl_multi_pw_aff *embed_index_expression(
1413 __isl_take isl_multi_pw_aff *index, struct pet_embed_access *data)
1415 isl_id *array_id = NULL;
1416 int pos;
1418 if (isl_multi_pw_aff_has_tuple_id(index, isl_dim_out))
1419 array_id = isl_multi_pw_aff_get_tuple_id(index, isl_dim_out);
1420 if (array_id == data->var_id) {
1421 index = replace_by_iterator(index, isl_aff_copy(data->iv_map));
1422 } else if (index_is_virtual_array(index)) {
1423 isl_aff *aff;
1424 isl_multi_pw_aff *mpa;
1426 aff = index_outer_iterator(isl_multi_pw_aff_copy(index));
1427 mpa = isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
1428 index = isl_multi_pw_aff_flat_range_product(mpa, index);
1429 index = isl_multi_pw_aff_set_tuple_id(index, isl_dim_out,
1430 isl_id_copy(array_id));
1432 isl_id_free(array_id);
1434 pos = isl_multi_pw_aff_find_dim_by_id(index,
1435 isl_dim_param, data->var_id);
1436 if (pos >= 0)
1437 index = index_internalize_iv(index, pos,
1438 isl_aff_copy(data->iv_map));
1439 index = isl_multi_pw_aff_set_dim_id(index, isl_dim_in, 0,
1440 isl_id_copy(data->var_id));
1442 return index;
1445 /* Embed the given access relation in an extra outer loop.
1446 * The domain of the access relation has already been updated.
1448 * If the access refers to the induction variable, then it is
1449 * turned into an access to the set of integers with index (and value)
1450 * equal to the induction variable.
1452 * If the induction variable appears in the constraints (as a parameter),
1453 * then the parameter is equated to the newly introduced iteration
1454 * domain dimension and subsequently projected out.
1456 * Similarly, if the accessed array is a virtual array (with user
1457 * pointer equal to NULL), as created by create_test_index,
1458 * then it is extended along with the domain of the access.
1460 static __isl_give isl_map *embed_access_relation(__isl_take isl_map *access,
1461 struct pet_embed_access *data)
1463 isl_id *array_id = NULL;
1464 int pos;
1466 if (isl_map_has_tuple_id(access, isl_dim_out))
1467 array_id = isl_map_get_tuple_id(access, isl_dim_out);
1468 if (array_id == data->var_id || access_is_virtual_array(access)) {
1469 access = isl_map_insert_dims(access, isl_dim_out, 0, 1);
1470 access = isl_map_equate(access,
1471 isl_dim_in, 0, isl_dim_out, 0);
1472 if (array_id == data->var_id)
1473 access = isl_map_apply_range(access,
1474 isl_map_from_aff(isl_aff_copy(data->iv_map)));
1475 else
1476 access = isl_map_set_tuple_id(access, isl_dim_out,
1477 isl_id_copy(array_id));
1479 isl_id_free(array_id);
1481 pos = isl_map_find_dim_by_id(access, isl_dim_param, data->var_id);
1482 if (pos >= 0) {
1483 isl_set *set = isl_map_wrap(access);
1484 set = internalize_iv(set, pos, isl_aff_copy(data->iv_map));
1485 access = isl_set_unwrap(set);
1487 access = isl_map_set_dim_id(access, isl_dim_in, 0,
1488 isl_id_copy(data->var_id));
1490 return access;
1493 /* Given an access expression, embed the associated access relation and
1494 * index expression in an extra outer loop.
1496 * We first update the domains to insert the extra dimension and
1497 * then update the access relation and index expression to take
1498 * into account the mapping "iv_map" from virtual iterator
1499 * to real iterator.
1501 static __isl_give pet_expr *embed_access(__isl_take pet_expr *expr, void *user)
1503 struct pet_embed_access *data = user;
1505 expr = pet_expr_cow(expr);
1506 expr = pet_expr_access_update_domain(expr, data->extend);
1507 if (!expr)
1508 return NULL;
1510 expr->acc.access = embed_access_relation(expr->acc.access, data);
1511 expr->acc.index = embed_index_expression(expr->acc.index, data);
1512 if (!expr->acc.access || !expr->acc.index)
1513 return pet_expr_free(expr);
1515 return expr;
1518 /* Embed all access subexpressions of "expr" in an extra loop.
1519 * "extend" inserts an outer loop iterator in the iteration domains
1520 * (through precomposition).
1521 * "iv_map" expresses the real iterator in terms of the virtual iterator
1522 * "var_id" represents the induction variable.
1524 static __isl_give pet_expr *expr_embed(__isl_take pet_expr *expr,
1525 __isl_take isl_multi_pw_aff *extend, __isl_take isl_aff *iv_map,
1526 __isl_keep isl_id *var_id)
1528 struct pet_embed_access data =
1529 { .extend = extend, .iv_map = iv_map, .var_id = var_id };
1531 expr = pet_expr_map_access(expr, &embed_access, &data);
1532 isl_aff_free(iv_map);
1533 isl_multi_pw_aff_free(extend);
1534 return expr;
1537 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1538 * "dom" and schedule "sched". "var_id" represents the induction variable
1539 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1540 * That is, it expresses the iterator that some of the parameters in "stmt"
1541 * may refer to in terms of the iterator used in "dom" and
1542 * the domain of "sched".
1544 * The iteration domain and schedule of the statement are updated
1545 * according to the iteration domain and schedule of the new loop.
1546 * If stmt->domain is a wrapped map, then the iteration domain
1547 * is the domain of this map, so we need to be careful to adjust
1548 * this domain.
1550 * If the induction variable appears in the constraints (as a parameter)
1551 * of the current iteration domain or the schedule of the statement,
1552 * then the parameter is equated to the newly introduced iteration
1553 * domain dimension and subsequently projected out.
1555 * Finally, all access relations are updated based on the extra loop.
1557 static struct pet_stmt *pet_stmt_embed(struct pet_stmt *stmt,
1558 __isl_take isl_set *dom, __isl_take isl_map *sched,
1559 __isl_take isl_aff *iv_map, __isl_take isl_id *var_id)
1561 int i;
1562 int pos;
1563 isl_id *stmt_id;
1564 isl_space *dim;
1565 isl_multi_pw_aff *extend;
1567 if (!stmt)
1568 goto error;
1570 if (isl_set_is_wrapping(stmt->domain)) {
1571 isl_map *map;
1572 isl_map *ext;
1573 isl_space *ran_dim;
1575 map = isl_set_unwrap(stmt->domain);
1576 stmt_id = isl_map_get_tuple_id(map, isl_dim_in);
1577 ran_dim = isl_space_range(isl_map_get_space(map));
1578 ext = isl_map_from_domain_and_range(isl_set_copy(dom),
1579 isl_set_universe(ran_dim));
1580 map = isl_map_flat_domain_product(ext, map);
1581 map = isl_map_set_tuple_id(map, isl_dim_in,
1582 isl_id_copy(stmt_id));
1583 dim = isl_space_domain(isl_map_get_space(map));
1584 stmt->domain = isl_map_wrap(map);
1585 } else {
1586 stmt_id = isl_set_get_tuple_id(stmt->domain);
1587 stmt->domain = isl_set_flat_product(isl_set_copy(dom),
1588 stmt->domain);
1589 stmt->domain = isl_set_set_tuple_id(stmt->domain,
1590 isl_id_copy(stmt_id));
1591 dim = isl_set_get_space(stmt->domain);
1594 pos = isl_set_find_dim_by_id(stmt->domain, isl_dim_param, var_id);
1595 if (pos >= 0)
1596 stmt->domain = internalize_iv(stmt->domain, pos,
1597 isl_aff_copy(iv_map));
1599 stmt->schedule = isl_map_flat_product(sched, stmt->schedule);
1600 stmt->schedule = isl_map_set_tuple_id(stmt->schedule,
1601 isl_dim_in, stmt_id);
1603 pos = isl_map_find_dim_by_id(stmt->schedule, isl_dim_param, var_id);
1604 if (pos >= 0) {
1605 isl_set *set = isl_map_wrap(stmt->schedule);
1606 set = internalize_iv(set, pos, isl_aff_copy(iv_map));
1607 stmt->schedule = isl_set_unwrap(set);
1610 dim = isl_space_map_from_set(dim);
1611 extend = isl_multi_pw_aff_identity(dim);
1612 extend = isl_multi_pw_aff_drop_dims(extend, isl_dim_out, 0, 1);
1613 extend = isl_multi_pw_aff_set_tuple_id(extend, isl_dim_out,
1614 isl_multi_pw_aff_get_tuple_id(extend, isl_dim_in));
1615 for (i = 0; i < stmt->n_arg; ++i)
1616 stmt->args[i] = expr_embed(stmt->args[i],
1617 isl_multi_pw_aff_copy(extend),
1618 isl_aff_copy(iv_map), var_id);
1619 stmt->body = expr_embed(stmt->body, extend, iv_map, var_id);
1621 isl_set_free(dom);
1622 isl_id_free(var_id);
1624 for (i = 0; i < stmt->n_arg; ++i)
1625 if (!stmt->args[i])
1626 return pet_stmt_free(stmt);
1627 if (!stmt->domain || !stmt->schedule || !stmt->body)
1628 return pet_stmt_free(stmt);
1629 return stmt;
1630 error:
1631 isl_set_free(dom);
1632 isl_map_free(sched);
1633 isl_aff_free(iv_map);
1634 isl_id_free(var_id);
1635 return NULL;
1638 /* Embed the given pet_array in an extra outer loop with iteration domain
1639 * "dom".
1640 * This embedding only has an effect on virtual arrays (those with
1641 * user pointer equal to NULL), which need to be extended along with
1642 * the iteration domain.
1644 static struct pet_array *pet_array_embed(struct pet_array *array,
1645 __isl_take isl_set *dom)
1647 isl_id *array_id = NULL;
1649 if (!array)
1650 goto error;
1651 if (!extent_is_virtual_array(array->extent)) {
1652 isl_set_free(dom);
1653 return array;
1656 array_id = isl_set_get_tuple_id(array->extent);
1657 array->extent = isl_set_flat_product(dom, array->extent);
1658 array->extent = isl_set_set_tuple_id(array->extent, array_id);
1659 if (!array->extent)
1660 return pet_array_free(array);
1662 return array;
1663 error:
1664 isl_set_free(dom);
1665 return NULL;
1668 /* Update the context with respect to an embedding into a loop
1669 * with iteration domain "dom" and induction variable "id".
1670 * "iv_map" expresses the real iterator (parameter "id") in terms
1671 * of a possibly virtual iterator (used in "dom").
1673 * If the current context is independent of "id", we don't need
1674 * to do anything.
1675 * Otherwise, a parameter value is invalid for the embedding if
1676 * any of the corresponding iterator values is invalid.
1677 * That is, a parameter value is valid only if all the corresponding
1678 * iterator values are valid.
1679 * We therefore compute the set of parameters
1681 * forall i in dom : valid (i)
1683 * or
1685 * not exists i in dom : not valid(i)
1687 * i.e.,
1689 * not exists i in dom \ valid(i)
1691 * Before we subtract valid(i) from dom, we first need to substitute
1692 * the real iterator for the virtual iterator.
1694 * If there are any unnamed parameters in "dom", then we consider
1695 * a parameter value to be valid if it is valid for any value of those
1696 * unnamed parameters. They are therefore projected out at the end.
1698 static __isl_give isl_set *context_embed(__isl_take isl_set *context,
1699 __isl_keep isl_set *dom, __isl_keep isl_aff *iv_map,
1700 __isl_keep isl_id *id)
1702 int pos;
1703 isl_multi_aff *ma;
1705 pos = isl_set_find_dim_by_id(context, isl_dim_param, id);
1706 if (pos < 0)
1707 return context;
1709 context = isl_set_from_params(context);
1710 context = isl_set_add_dims(context, isl_dim_set, 1);
1711 context = isl_set_equate(context, isl_dim_param, pos, isl_dim_set, 0);
1712 context = isl_set_project_out(context, isl_dim_param, pos, 1);
1713 ma = isl_multi_aff_from_aff(isl_aff_copy(iv_map));
1714 context = isl_set_preimage_multi_aff(context, ma);
1715 context = isl_set_subtract(isl_set_copy(dom), context);
1716 context = isl_set_params(context);
1717 context = isl_set_complement(context);
1718 context = pet_nested_remove_from_set(context);
1719 return context;
1722 /* Update the implication with respect to an embedding into a loop
1723 * with iteration domain "dom".
1725 * Since embed_access extends virtual arrays along with the domain
1726 * of the access, we need to do the same with domain and range
1727 * of the implication. Since the original implication is only valid
1728 * within a given iteration of the loop, the extended implication
1729 * maps the extra array dimension corresponding to the extra loop
1730 * to itself.
1732 static struct pet_implication *pet_implication_embed(
1733 struct pet_implication *implication, __isl_take isl_set *dom)
1735 isl_id *id;
1736 isl_map *map;
1738 if (!implication)
1739 goto error;
1741 map = isl_set_identity(dom);
1742 id = isl_map_get_tuple_id(implication->extension, isl_dim_in);
1743 map = isl_map_flat_product(map, implication->extension);
1744 map = isl_map_set_tuple_id(map, isl_dim_in, isl_id_copy(id));
1745 map = isl_map_set_tuple_id(map, isl_dim_out, id);
1746 implication->extension = map;
1747 if (!implication->extension)
1748 return pet_implication_free(implication);
1750 return implication;
1751 error:
1752 isl_set_free(dom);
1753 return NULL;
1756 /* Embed all statements and arrays in "scop" in an extra outer loop
1757 * with iteration domain "dom" and schedule "sched".
1758 * "id" represents the induction variable of the loop.
1759 * "iv_map" maps a possibly virtual iterator to the real iterator.
1760 * That is, it expresses the iterator that some of the parameters in "scop"
1761 * may refer to in terms of the iterator used in "dom" and
1762 * the domain of "sched".
1764 * Any skip conditions within the loop have no effect outside of the loop.
1765 * The caller is responsible for making sure skip[pet_skip_later] has been
1766 * taken into account.
1768 struct pet_scop *pet_scop_embed(struct pet_scop *scop, __isl_take isl_set *dom,
1769 __isl_take isl_aff *sched, __isl_take isl_aff *iv_map,
1770 __isl_take isl_id *id)
1772 int i;
1773 isl_map *sched_map;
1775 sched_map = isl_map_from_aff(sched);
1777 if (!scop)
1778 goto error;
1780 pet_scop_reset_skip(scop, pet_skip_now);
1781 pet_scop_reset_skip(scop, pet_skip_later);
1783 scop->context = context_embed(scop->context, dom, iv_map, id);
1784 if (!scop->context)
1785 goto error;
1787 for (i = 0; i < scop->n_stmt; ++i) {
1788 scop->stmts[i] = pet_stmt_embed(scop->stmts[i],
1789 isl_set_copy(dom), isl_map_copy(sched_map),
1790 isl_aff_copy(iv_map), isl_id_copy(id));
1791 if (!scop->stmts[i])
1792 goto error;
1795 for (i = 0; i < scop->n_array; ++i) {
1796 scop->arrays[i] = pet_array_embed(scop->arrays[i],
1797 isl_set_copy(dom));
1798 if (!scop->arrays[i])
1799 goto error;
1802 for (i = 0; i < scop->n_implication; ++i) {
1803 scop->implications[i] =
1804 pet_implication_embed(scop->implications[i],
1805 isl_set_copy(dom));
1806 if (!scop->implications[i])
1807 goto error;
1810 isl_set_free(dom);
1811 isl_map_free(sched_map);
1812 isl_aff_free(iv_map);
1813 isl_id_free(id);
1814 return scop;
1815 error:
1816 isl_set_free(dom);
1817 isl_map_free(sched_map);
1818 isl_aff_free(iv_map);
1819 isl_id_free(id);
1820 return pet_scop_free(scop);
1823 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1825 static struct pet_stmt *stmt_restrict(struct pet_stmt *stmt,
1826 __isl_take isl_set *cond)
1828 if (!stmt)
1829 goto error;
1831 stmt->domain = isl_set_intersect_params(stmt->domain, cond);
1833 return stmt;
1834 error:
1835 isl_set_free(cond);
1836 return pet_stmt_free(stmt);
1839 /* Add extra conditions to scop->skip[type].
1841 * The new skip condition only holds if it held before
1842 * and the condition is true. It does not hold if it did not hold
1843 * before or the condition is false.
1845 * The skip condition is assumed to be an affine expression.
1847 static struct pet_scop *pet_scop_restrict_skip(struct pet_scop *scop,
1848 enum pet_skip type, __isl_keep isl_set *cond)
1850 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
1851 isl_pw_aff *skip;
1852 isl_set *dom;
1854 if (!scop)
1855 return NULL;
1856 if (!ext->skip[type])
1857 return scop;
1859 if (!multi_pw_aff_is_affine(ext->skip[type]))
1860 isl_die(isl_multi_pw_aff_get_ctx(ext->skip[type]),
1861 isl_error_internal, "can only restrict affine skips",
1862 return pet_scop_free(scop));
1864 skip = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
1865 dom = isl_pw_aff_domain(isl_pw_aff_copy(skip));
1866 cond = isl_set_copy(cond);
1867 cond = isl_set_from_params(cond);
1868 cond = isl_set_intersect(cond, isl_pw_aff_non_zero_set(skip));
1869 skip = indicator_function(cond, dom);
1870 isl_multi_pw_aff_free(ext->skip[type]);
1871 ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
1872 if (!ext->skip[type])
1873 return pet_scop_free(scop);
1875 return scop;
1878 /* Add extra conditions on the parameters to all iteration domains
1879 * and skip conditions.
1881 * A parameter value is valid for the result if it was valid
1882 * for the original scop and satisfies "cond" or if it does
1883 * not satisfy "cond" as in this case the scop is not executed
1884 * and the original constraints on the parameters are irrelevant.
1886 struct pet_scop *pet_scop_restrict(struct pet_scop *scop,
1887 __isl_take isl_set *cond)
1889 int i;
1891 scop = pet_scop_restrict_skip(scop, pet_skip_now, cond);
1892 scop = pet_scop_restrict_skip(scop, pet_skip_later, cond);
1894 if (!scop)
1895 goto error;
1897 scop->context = isl_set_intersect(scop->context, isl_set_copy(cond));
1898 scop->context = isl_set_union(scop->context,
1899 isl_set_complement(isl_set_copy(cond)));
1900 scop->context = isl_set_coalesce(scop->context);
1901 scop->context = pet_nested_remove_from_set(scop->context);
1902 if (!scop->context)
1903 goto error;
1905 for (i = 0; i < scop->n_stmt; ++i) {
1906 scop->stmts[i] = stmt_restrict(scop->stmts[i],
1907 isl_set_copy(cond));
1908 if (!scop->stmts[i])
1909 goto error;
1912 isl_set_free(cond);
1913 return scop;
1914 error:
1915 isl_set_free(cond);
1916 return pet_scop_free(scop);
1919 /* Insert an argument expression corresponding to "test" in front
1920 * of the list of arguments described by *n_arg and *args.
1922 static int args_insert_access(unsigned *n_arg, pet_expr ***args,
1923 __isl_keep isl_multi_pw_aff *test)
1925 int i;
1926 isl_ctx *ctx = isl_multi_pw_aff_get_ctx(test);
1928 if (!test)
1929 return -1;
1931 if (!*args) {
1932 *args = isl_calloc_array(ctx, pet_expr *, 1);
1933 if (!*args)
1934 return -1;
1935 } else {
1936 pet_expr **ext;
1937 ext = isl_calloc_array(ctx, pet_expr *, 1 + *n_arg);
1938 if (!ext)
1939 return -1;
1940 for (i = 0; i < *n_arg; ++i)
1941 ext[1 + i] = (*args)[i];
1942 free(*args);
1943 *args = ext;
1945 (*n_arg)++;
1946 (*args)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test));
1947 if (!(*args)[0])
1948 return -1;
1950 return 0;
1953 /* Look through the applications in "scop" for any that can be
1954 * applied to the filter expressed by "map" and "satisified".
1955 * If there is any, then apply it to "map" and return the result.
1956 * Otherwise, return "map".
1957 * "id" is the identifier of the virtual array.
1959 * We only introduce at most one implication for any given virtual array,
1960 * so we can apply the implication and return as soon as we find one.
1962 static __isl_give isl_map *apply_implications(struct pet_scop *scop,
1963 __isl_take isl_map *map, __isl_keep isl_id *id, int satisfied)
1965 int i;
1967 for (i = 0; i < scop->n_implication; ++i) {
1968 struct pet_implication *pi = scop->implications[i];
1969 isl_id *pi_id;
1971 if (pi->satisfied != satisfied)
1972 continue;
1973 pi_id = isl_map_get_tuple_id(pi->extension, isl_dim_in);
1974 isl_id_free(pi_id);
1975 if (pi_id != id)
1976 continue;
1978 return isl_map_apply_range(map, isl_map_copy(pi->extension));
1981 return map;
1984 /* Is the filter expressed by "test" and "satisfied" implied
1985 * by filter "pos" on "domain", with filter "expr", taking into
1986 * account the implications of "scop"?
1988 * For filter on domain implying that expressed by "test" and "satisfied",
1989 * the filter needs to be an access to the same (virtual) array as "test" and
1990 * the filter value needs to be equal to "satisfied".
1991 * Moreover, the filter access relation, possibly extended by
1992 * the implications in "scop" needs to contain "test".
1994 static int implies_filter(struct pet_scop *scop,
1995 __isl_keep isl_map *domain, int pos, __isl_keep pet_expr *expr,
1996 __isl_keep isl_map *test, int satisfied)
1998 isl_id *test_id, *arg_id;
1999 isl_val *val;
2000 int is_int;
2001 int s;
2002 int is_subset;
2003 isl_map *implied;
2005 if (expr->type != pet_expr_access)
2006 return 0;
2007 test_id = isl_map_get_tuple_id(test, isl_dim_out);
2008 arg_id = pet_expr_access_get_id(expr);
2009 isl_id_free(arg_id);
2010 isl_id_free(test_id);
2011 if (test_id != arg_id)
2012 return 0;
2013 val = isl_map_plain_get_val_if_fixed(domain, isl_dim_out, pos);
2014 is_int = isl_val_is_int(val);
2015 if (is_int)
2016 s = isl_val_get_num_si(val);
2017 isl_val_free(val);
2018 if (!val)
2019 return -1;
2020 if (!is_int)
2021 return 0;
2022 if (s != satisfied)
2023 return 0;
2025 implied = isl_map_copy(expr->acc.access);
2026 implied = apply_implications(scop, implied, test_id, satisfied);
2027 is_subset = isl_map_is_subset(test, implied);
2028 isl_map_free(implied);
2030 return is_subset;
2033 /* Is the filter expressed by "test" and "satisfied" implied
2034 * by any of the filters on the domain of "stmt", taking into
2035 * account the implications of "scop"?
2037 static int filter_implied(struct pet_scop *scop,
2038 struct pet_stmt *stmt, __isl_keep isl_multi_pw_aff *test, int satisfied)
2040 int i;
2041 int implied;
2042 isl_id *test_id;
2043 isl_map *domain;
2044 isl_map *test_map;
2046 if (!scop || !stmt || !test)
2047 return -1;
2048 if (scop->n_implication == 0)
2049 return 0;
2050 if (stmt->n_arg == 0)
2051 return 0;
2053 domain = isl_set_unwrap(isl_set_copy(stmt->domain));
2054 test_map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test));
2056 implied = 0;
2057 for (i = 0; i < stmt->n_arg; ++i) {
2058 implied = implies_filter(scop, domain, i, stmt->args[i],
2059 test_map, satisfied);
2060 if (implied < 0 || implied)
2061 break;
2064 isl_map_free(test_map);
2065 isl_map_free(domain);
2066 return implied;
2069 /* Make the statement "stmt" depend on the value of "test"
2070 * being equal to "satisfied" by adjusting stmt->domain.
2072 * The domain of "test" corresponds to the (zero or more) outer dimensions
2073 * of the iteration domain.
2075 * We first extend "test" to apply to the entire iteration domain and
2076 * then check if the filter that we are about to add is implied
2077 * by any of the current filters, possibly taking into account
2078 * the implications in "scop". If so, we leave "stmt" untouched and return.
2080 * Otherwise, we insert an argument corresponding to a read to "test"
2081 * from the iteration domain of "stmt" in front of the list of arguments.
2082 * We also insert a corresponding output dimension in the wrapped
2083 * map contained in stmt->domain, with value set to "satisfied".
2085 static struct pet_stmt *stmt_filter(struct pet_scop *scop,
2086 struct pet_stmt *stmt, __isl_take isl_multi_pw_aff *test, int satisfied)
2088 int i;
2089 int implied;
2090 isl_id *id;
2091 isl_ctx *ctx;
2092 isl_pw_multi_aff *pma;
2093 isl_multi_aff *add_dom;
2094 isl_space *space;
2095 isl_local_space *ls;
2096 int n_test_dom;
2098 if (!stmt || !test)
2099 goto error;
2101 space = pet_stmt_get_space(stmt);
2102 n_test_dom = isl_multi_pw_aff_dim(test, isl_dim_in);
2103 space = isl_space_from_domain(space);
2104 space = isl_space_add_dims(space, isl_dim_out, n_test_dom);
2105 add_dom = isl_multi_aff_zero(isl_space_copy(space));
2106 ls = isl_local_space_from_space(isl_space_domain(space));
2107 for (i = 0; i < n_test_dom; ++i) {
2108 isl_aff *aff;
2109 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
2110 isl_dim_set, i);
2111 add_dom = isl_multi_aff_set_aff(add_dom, i, aff);
2113 isl_local_space_free(ls);
2114 test = isl_multi_pw_aff_pullback_multi_aff(test, add_dom);
2116 implied = filter_implied(scop, stmt, test, satisfied);
2117 if (implied < 0)
2118 goto error;
2119 if (implied) {
2120 isl_multi_pw_aff_free(test);
2121 return stmt;
2124 id = isl_multi_pw_aff_get_tuple_id(test, isl_dim_out);
2125 pma = pet_filter_insert_pma(isl_set_get_space(stmt->domain),
2126 id, satisfied);
2127 stmt->domain = isl_set_preimage_pw_multi_aff(stmt->domain, pma);
2129 if (args_insert_access(&stmt->n_arg, &stmt->args, test) < 0)
2130 goto error;
2132 isl_multi_pw_aff_free(test);
2133 return stmt;
2134 error:
2135 isl_multi_pw_aff_free(test);
2136 return pet_stmt_free(stmt);
2139 /* Does "scop" have a skip condition of the given "type"?
2141 int pet_scop_has_skip(struct pet_scop *scop, enum pet_skip type)
2143 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2145 if (!scop)
2146 return -1;
2147 return ext->skip[type] != NULL;
2150 /* Does "scop" have a skip condition of the given "type" that
2151 * is an affine expression?
2153 int pet_scop_has_affine_skip(struct pet_scop *scop, enum pet_skip type)
2155 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2157 if (!scop)
2158 return -1;
2159 if (!ext->skip[type])
2160 return 0;
2161 return multi_pw_aff_is_affine(ext->skip[type]);
2164 /* Does "scop" have a skip condition of the given "type" that
2165 * is not an affine expression?
2167 int pet_scop_has_var_skip(struct pet_scop *scop, enum pet_skip type)
2169 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2170 int aff;
2172 if (!scop)
2173 return -1;
2174 if (!ext->skip[type])
2175 return 0;
2176 aff = multi_pw_aff_is_affine(ext->skip[type]);
2177 if (aff < 0)
2178 return -1;
2179 return !aff;
2182 /* Does "scop" have a skip condition of the given "type" that
2183 * is affine and holds on the entire domain?
2185 int pet_scop_has_universal_skip(struct pet_scop *scop, enum pet_skip type)
2187 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2188 isl_pw_aff *pa;
2189 isl_set *set;
2190 int is_aff;
2191 int is_univ;
2193 is_aff = pet_scop_has_affine_skip(scop, type);
2194 if (is_aff < 0 || !is_aff)
2195 return is_aff;
2197 pa = isl_multi_pw_aff_get_pw_aff(ext->skip[type], 0);
2198 set = isl_pw_aff_non_zero_set(pa);
2199 is_univ = isl_set_plain_is_universe(set);
2200 isl_set_free(set);
2202 return is_univ;
2205 /* Replace scop->skip[type] by "skip".
2207 struct pet_scop *pet_scop_set_skip(struct pet_scop *scop,
2208 enum pet_skip type, __isl_take isl_multi_pw_aff *skip)
2210 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2212 if (!scop || !skip)
2213 goto error;
2215 isl_multi_pw_aff_free(ext->skip[type]);
2216 ext->skip[type] = skip;
2218 return scop;
2219 error:
2220 isl_multi_pw_aff_free(skip);
2221 return pet_scop_free(scop);
2224 /* Return a copy of scop->skip[type].
2226 __isl_give isl_multi_pw_aff *pet_scop_get_skip(struct pet_scop *scop,
2227 enum pet_skip type)
2229 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2231 if (!scop)
2232 return NULL;
2234 return isl_multi_pw_aff_copy(ext->skip[type]);
2237 /* Assuming scop->skip[type] is an affine expression,
2238 * return the constraints on the parameters for which the skip condition
2239 * holds.
2241 __isl_give isl_set *pet_scop_get_affine_skip_domain(struct pet_scop *scop,
2242 enum pet_skip type)
2244 isl_multi_pw_aff *skip;
2245 isl_pw_aff *pa;
2247 skip = pet_scop_get_skip(scop, type);
2248 pa = isl_multi_pw_aff_get_pw_aff(skip, 0);
2249 isl_multi_pw_aff_free(skip);
2250 return isl_set_params(isl_pw_aff_non_zero_set(pa));
2253 /* Return the identifier of the variable that is accessed by
2254 * the skip condition of the given type.
2256 * The skip condition is assumed not to be an affine condition.
2258 __isl_give isl_id *pet_scop_get_skip_id(struct pet_scop *scop,
2259 enum pet_skip type)
2261 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2263 if (!scop)
2264 return NULL;
2266 return isl_multi_pw_aff_get_tuple_id(ext->skip[type], isl_dim_out);
2269 /* Return an access pet_expr corresponding to the skip condition
2270 * of the given type.
2272 __isl_give pet_expr *pet_scop_get_skip_expr(struct pet_scop *scop,
2273 enum pet_skip type)
2275 return pet_expr_from_index(pet_scop_get_skip(scop, type));
2278 /* Drop the the skip condition scop->skip[type].
2280 void pet_scop_reset_skip(struct pet_scop *scop, enum pet_skip type)
2282 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
2284 if (!scop)
2285 return;
2287 isl_multi_pw_aff_free(ext->skip[type]);
2288 ext->skip[type] = NULL;
2291 /* Make the skip condition (if any) depend on the value of "test" being
2292 * equal to "satisfied".
2294 * We only support the case where the original skip condition is universal,
2295 * i.e., where skipping is unconditional, and where satisfied == 1.
2296 * In this case, the skip condition is changed to skip only when
2297 * "test" is equal to one.
2299 static struct pet_scop *pet_scop_filter_skip(struct pet_scop *scop,
2300 enum pet_skip type, __isl_keep isl_multi_pw_aff *test, int satisfied)
2302 int is_univ = 0;
2304 if (!scop)
2305 return NULL;
2306 if (!pet_scop_has_skip(scop, type))
2307 return scop;
2309 if (satisfied)
2310 is_univ = pet_scop_has_universal_skip(scop, type);
2311 if (is_univ < 0)
2312 return pet_scop_free(scop);
2313 if (satisfied && is_univ) {
2314 isl_multi_pw_aff *skip;
2315 skip = isl_multi_pw_aff_copy(test);
2316 scop = pet_scop_set_skip(scop, type, skip);
2317 if (!scop)
2318 return NULL;
2319 } else {
2320 isl_die(isl_multi_pw_aff_get_ctx(test), isl_error_internal,
2321 "skip expression cannot be filtered",
2322 return pet_scop_free(scop));
2325 return scop;
2328 /* Make all statements in "scop" depend on the value of "test"
2329 * being equal to "satisfied" by adjusting their domains.
2331 struct pet_scop *pet_scop_filter(struct pet_scop *scop,
2332 __isl_take isl_multi_pw_aff *test, int satisfied)
2334 int i;
2336 scop = pet_scop_filter_skip(scop, pet_skip_now, test, satisfied);
2337 scop = pet_scop_filter_skip(scop, pet_skip_later, test, satisfied);
2339 if (!scop || !test)
2340 goto error;
2342 for (i = 0; i < scop->n_stmt; ++i) {
2343 scop->stmts[i] = stmt_filter(scop, scop->stmts[i],
2344 isl_multi_pw_aff_copy(test), satisfied);
2345 if (!scop->stmts[i])
2346 goto error;
2349 isl_multi_pw_aff_free(test);
2350 return scop;
2351 error:
2352 isl_multi_pw_aff_free(test);
2353 return pet_scop_free(scop);
2356 /* Add all parameters in "expr" to "space" and return the result.
2358 static __isl_give isl_space *expr_collect_params(__isl_keep pet_expr *expr,
2359 __isl_take isl_space *space)
2361 int i;
2363 if (!expr)
2364 goto error;
2365 for (i = 0; i < expr->n_arg; ++i)
2366 space = expr_collect_params(expr->args[i], space);
2368 if (expr->type == pet_expr_access)
2369 space = isl_space_align_params(space,
2370 isl_map_get_space(expr->acc.access));
2372 return space;
2373 error:
2374 pet_expr_free(expr);
2375 return isl_space_free(space);
2378 /* Add all parameters in "stmt" to "space" and return the result.
2380 static __isl_give isl_space *stmt_collect_params(struct pet_stmt *stmt,
2381 __isl_take isl_space *space)
2383 int i;
2385 if (!stmt)
2386 return isl_space_free(space);
2388 space = isl_space_align_params(space, isl_set_get_space(stmt->domain));
2389 space = isl_space_align_params(space,
2390 isl_map_get_space(stmt->schedule));
2391 for (i = 0; i < stmt->n_arg; ++i)
2392 space = expr_collect_params(stmt->args[i], space);
2393 space = expr_collect_params(stmt->body, space);
2395 return space;
2398 /* Add all parameters in "array" to "space" and return the result.
2400 static __isl_give isl_space *array_collect_params(struct pet_array *array,
2401 __isl_take isl_space *space)
2403 if (!array)
2404 return isl_space_free(space);
2406 space = isl_space_align_params(space,
2407 isl_set_get_space(array->context));
2408 space = isl_space_align_params(space, isl_set_get_space(array->extent));
2410 return space;
2413 /* Add all parameters in "scop" to "space" and return the result.
2415 static __isl_give isl_space *scop_collect_params(struct pet_scop *scop,
2416 __isl_take isl_space *space)
2418 int i;
2420 if (!scop)
2421 return isl_space_free(space);
2423 for (i = 0; i < scop->n_array; ++i)
2424 space = array_collect_params(scop->arrays[i], space);
2426 for (i = 0; i < scop->n_stmt; ++i)
2427 space = stmt_collect_params(scop->stmts[i], space);
2429 return space;
2432 /* Add all parameters in "space" to the domain, schedule and
2433 * all access relations in "stmt".
2435 static struct pet_stmt *stmt_propagate_params(struct pet_stmt *stmt,
2436 __isl_take isl_space *space)
2438 int i;
2440 if (!stmt)
2441 goto error;
2443 stmt->domain = isl_set_align_params(stmt->domain,
2444 isl_space_copy(space));
2445 stmt->schedule = isl_map_align_params(stmt->schedule,
2446 isl_space_copy(space));
2448 for (i = 0; i < stmt->n_arg; ++i) {
2449 stmt->args[i] = pet_expr_align_params(stmt->args[i],
2450 isl_space_copy(space));
2451 if (!stmt->args[i])
2452 goto error;
2454 stmt->body = pet_expr_align_params(stmt->body, isl_space_copy(space));
2456 if (!stmt->domain || !stmt->schedule || !stmt->body)
2457 goto error;
2459 isl_space_free(space);
2460 return stmt;
2461 error:
2462 isl_space_free(space);
2463 return pet_stmt_free(stmt);
2466 /* Add all parameters in "space" to "array".
2468 static struct pet_array *array_propagate_params(struct pet_array *array,
2469 __isl_take isl_space *space)
2471 if (!array)
2472 goto error;
2474 array->context = isl_set_align_params(array->context,
2475 isl_space_copy(space));
2476 array->extent = isl_set_align_params(array->extent,
2477 isl_space_copy(space));
2478 if (array->value_bounds) {
2479 array->value_bounds = isl_set_align_params(array->value_bounds,
2480 isl_space_copy(space));
2481 if (!array->value_bounds)
2482 goto error;
2485 if (!array->context || !array->extent)
2486 goto error;
2488 isl_space_free(space);
2489 return array;
2490 error:
2491 isl_space_free(space);
2492 return pet_array_free(array);
2495 /* Add all parameters in "space" to "scop".
2497 static struct pet_scop *scop_propagate_params(struct pet_scop *scop,
2498 __isl_take isl_space *space)
2500 int i;
2502 if (!scop)
2503 goto error;
2505 for (i = 0; i < scop->n_array; ++i) {
2506 scop->arrays[i] = array_propagate_params(scop->arrays[i],
2507 isl_space_copy(space));
2508 if (!scop->arrays[i])
2509 goto error;
2512 for (i = 0; i < scop->n_stmt; ++i) {
2513 scop->stmts[i] = stmt_propagate_params(scop->stmts[i],
2514 isl_space_copy(space));
2515 if (!scop->stmts[i])
2516 goto error;
2519 isl_space_free(space);
2520 return scop;
2521 error:
2522 isl_space_free(space);
2523 return pet_scop_free(scop);
2526 /* Update all isl_sets and isl_maps in "scop" such that they all
2527 * have the same parameters.
2529 struct pet_scop *pet_scop_align_params(struct pet_scop *scop)
2531 isl_space *space;
2533 if (!scop)
2534 return NULL;
2536 space = isl_set_get_space(scop->context);
2537 space = scop_collect_params(scop, space);
2539 scop->context = isl_set_align_params(scop->context,
2540 isl_space_copy(space));
2541 scop = scop_propagate_params(scop, space);
2543 if (scop && !scop->context)
2544 return pet_scop_free(scop);
2546 return scop;
2549 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2550 * in "space" by a value equal to the corresponding parameter.
2552 static struct pet_stmt *stmt_detect_parameter_accesses(struct pet_stmt *stmt,
2553 __isl_take isl_space *space)
2555 if (!stmt)
2556 goto error;
2558 stmt->body = pet_expr_detect_parameter_accesses(stmt->body,
2559 isl_space_copy(space));
2561 if (!stmt->domain || !stmt->schedule || !stmt->body)
2562 goto error;
2564 isl_space_free(space);
2565 return stmt;
2566 error:
2567 isl_space_free(space);
2568 return pet_stmt_free(stmt);
2571 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2572 * in "space" by a value equal to the corresponding parameter.
2574 static struct pet_scop *scop_detect_parameter_accesses(struct pet_scop *scop,
2575 __isl_take isl_space *space)
2577 int i;
2579 if (!scop)
2580 goto error;
2582 for (i = 0; i < scop->n_stmt; ++i) {
2583 scop->stmts[i] = stmt_detect_parameter_accesses(scop->stmts[i],
2584 isl_space_copy(space));
2585 if (!scop->stmts[i])
2586 goto error;
2589 isl_space_free(space);
2590 return scop;
2591 error:
2592 isl_space_free(space);
2593 return pet_scop_free(scop);
2596 /* Replace all accesses to (0D) arrays that correspond to any of
2597 * the parameters used in "scop" by a value equal
2598 * to the corresponding parameter.
2600 struct pet_scop *pet_scop_detect_parameter_accesses(struct pet_scop *scop)
2602 isl_space *space;
2604 if (!scop)
2605 return NULL;
2607 space = isl_set_get_space(scop->context);
2608 space = scop_collect_params(scop, space);
2610 scop = scop_detect_parameter_accesses(scop, space);
2612 return scop;
2615 /* Add the access relation of the access expression "expr" to "accesses" and
2616 * return the result.
2617 * The domain of the access relation is intersected with "domain".
2618 * If "tag" is set, then the access relation is tagged with
2619 * the corresponding reference identifier.
2621 static __isl_give isl_union_map *expr_collect_access(__isl_keep pet_expr *expr,
2622 int tag, __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2624 isl_map *access;
2626 access = pet_expr_access_get_may_access(expr);
2627 access = isl_map_intersect_domain(access, isl_set_copy(domain));
2628 if (tag)
2629 access = pet_expr_tag_access(expr, access);
2630 return isl_union_map_add_map(accesses, access);
2633 /* Add all read access relations (if "read" is set) and/or all write
2634 * access relations (if "write" is set) to "accesses" and return the result.
2635 * The domains of the access relations are intersected with "domain".
2636 * If "tag" is set, then the access relations are tagged with
2637 * the corresponding reference identifiers.
2639 * If "must" is set, then we only add the accesses that are definitely
2640 * performed. Otherwise, we add all potential accesses.
2641 * In particular, if the access has any arguments, then if "must" is
2642 * set we currently skip the access completely. If "must" is not set,
2643 * we project out the values of the access arguments.
2645 static __isl_give isl_union_map *expr_collect_accesses(
2646 __isl_keep pet_expr *expr, int read, int write, int must, int tag,
2647 __isl_take isl_union_map *accesses, __isl_keep isl_set *domain)
2649 int i;
2650 isl_id *id;
2651 isl_space *dim;
2653 if (!expr)
2654 return isl_union_map_free(accesses);
2656 for (i = 0; i < expr->n_arg; ++i)
2657 accesses = expr_collect_accesses(expr->args[i],
2658 read, write, must, tag, accesses, domain);
2660 if (expr->type == pet_expr_access && !pet_expr_is_affine(expr) &&
2661 ((read && expr->acc.read) || (write && expr->acc.write)) &&
2662 (!must || expr->n_arg == 0)) {
2663 accesses = expr_collect_access(expr, tag, accesses, domain);
2666 return accesses;
2669 /* Collect and return all read access relations (if "read" is set)
2670 * and/or all write access relations (if "write" is set) in "stmt".
2671 * If "tag" is set, then the access relations are tagged with
2672 * the corresponding reference identifiers.
2673 * If "kill" is set, then "stmt" is a kill statement and we simply
2674 * add the argument of the kill operation.
2676 * If "must" is set, then we only add the accesses that are definitely
2677 * performed. Otherwise, we add all potential accesses.
2678 * In particular, if the statement has any arguments, then if "must" is
2679 * set we currently skip the statement completely. If "must" is not set,
2680 * we project out the values of the statement arguments.
2682 static __isl_give isl_union_map *stmt_collect_accesses(struct pet_stmt *stmt,
2683 int read, int write, int kill, int must, int tag,
2684 __isl_take isl_space *dim)
2686 isl_union_map *accesses;
2687 isl_set *domain;
2689 if (!stmt)
2690 return NULL;
2692 accesses = isl_union_map_empty(dim);
2694 if (must && stmt->n_arg > 0)
2695 return accesses;
2697 domain = isl_set_copy(stmt->domain);
2698 if (isl_set_is_wrapping(domain))
2699 domain = isl_map_domain(isl_set_unwrap(domain));
2701 if (kill)
2702 accesses = expr_collect_access(stmt->body->args[0], tag,
2703 accesses, domain);
2704 else
2705 accesses = expr_collect_accesses(stmt->body, read, write,
2706 must, tag, accesses, domain);
2707 isl_set_free(domain);
2709 return accesses;
2712 /* Is "stmt" an assignment statement?
2714 int pet_stmt_is_assign(struct pet_stmt *stmt)
2716 if (!stmt)
2717 return 0;
2718 if (stmt->body->type != pet_expr_op)
2719 return 0;
2720 return stmt->body->op == pet_op_assign;
2723 /* Is "stmt" a kill statement?
2725 int pet_stmt_is_kill(struct pet_stmt *stmt)
2727 if (!stmt)
2728 return 0;
2729 if (stmt->body->type != pet_expr_op)
2730 return 0;
2731 return stmt->body->op == pet_op_kill;
2734 /* Is "stmt" an assume statement?
2736 int pet_stmt_is_assume(struct pet_stmt *stmt)
2738 if (!stmt)
2739 return 0;
2740 return pet_expr_is_assume(stmt->body);
2743 /* Compute a mapping from all arrays (of structs) in scop
2744 * to their innermost arrays.
2746 * In particular, for each array of a primitive type, the result
2747 * contains the identity mapping on that array.
2748 * For each array involving member accesses, the result
2749 * contains a mapping from the elements of any intermediate array of structs
2750 * to all corresponding elements of the innermost nested arrays.
2752 static __isl_give isl_union_map *compute_to_inner(struct pet_scop *scop)
2754 int i;
2755 isl_union_map *to_inner;
2757 to_inner = isl_union_map_empty(isl_set_get_space(scop->context));
2759 for (i = 0; i < scop->n_array; ++i) {
2760 struct pet_array *array = scop->arrays[i];
2761 isl_set *set;
2762 isl_map *map, *gist;
2764 if (array->element_is_record)
2765 continue;
2767 map = isl_set_identity(isl_set_copy(array->extent));
2769 set = isl_map_domain(isl_map_copy(map));
2770 gist = isl_map_copy(map);
2771 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2772 to_inner = isl_union_map_add_map(to_inner, gist);
2774 while (set && isl_set_is_wrapping(set)) {
2775 isl_id *id;
2776 isl_map *wrapped;
2778 id = isl_set_get_tuple_id(set);
2779 wrapped = isl_set_unwrap(set);
2780 wrapped = isl_map_domain_map(wrapped);
2781 wrapped = isl_map_set_tuple_id(wrapped, isl_dim_in, id);
2782 map = isl_map_apply_domain(map, wrapped);
2783 set = isl_map_domain(isl_map_copy(map));
2784 gist = isl_map_copy(map);
2785 gist = isl_map_gist_domain(gist, isl_set_copy(set));
2786 to_inner = isl_union_map_add_map(to_inner, gist);
2789 isl_set_free(set);
2790 isl_map_free(map);
2793 return to_inner;
2796 /* Collect and return all read access relations (if "read" is set)
2797 * and/or all write access relations (if "write" is set) in "scop".
2798 * If "kill" is set, then we only add the arguments of kill operations.
2799 * If "must" is set, then we only add the accesses that are definitely
2800 * performed. Otherwise, we add all potential accesses.
2801 * If "tag" is set, then the access relations are tagged with
2802 * the corresponding reference identifiers.
2803 * For accesses to structures, the returned access relation accesses
2804 * all individual fields in the structures.
2806 static __isl_give isl_union_map *scop_collect_accesses(struct pet_scop *scop,
2807 int read, int write, int kill, int must, int tag)
2809 int i;
2810 isl_union_map *accesses;
2811 isl_union_set *arrays;
2812 isl_union_map *to_inner;
2814 if (!scop)
2815 return NULL;
2817 accesses = isl_union_map_empty(isl_set_get_space(scop->context));
2819 for (i = 0; i < scop->n_stmt; ++i) {
2820 struct pet_stmt *stmt = scop->stmts[i];
2821 isl_union_map *accesses_i;
2822 isl_space *space;
2824 if (kill && !pet_stmt_is_kill(stmt))
2825 continue;
2827 space = isl_set_get_space(scop->context);
2828 accesses_i = stmt_collect_accesses(stmt, read, write, kill,
2829 must, tag, space);
2830 accesses = isl_union_map_union(accesses, accesses_i);
2833 arrays = isl_union_set_empty(isl_union_map_get_space(accesses));
2834 for (i = 0; i < scop->n_array; ++i) {
2835 isl_set *extent = isl_set_copy(scop->arrays[i]->extent);
2836 arrays = isl_union_set_add_set(arrays, extent);
2838 accesses = isl_union_map_intersect_range(accesses, arrays);
2840 to_inner = compute_to_inner(scop);
2841 accesses = isl_union_map_apply_range(accesses, to_inner);
2843 return accesses;
2846 /* Collect all potential read access relations.
2848 __isl_give isl_union_map *pet_scop_collect_may_reads(struct pet_scop *scop)
2850 return scop_collect_accesses(scop, 1, 0, 0, 0, 0);
2853 /* Collect all potential write access relations.
2855 __isl_give isl_union_map *pet_scop_collect_may_writes(struct pet_scop *scop)
2857 return scop_collect_accesses(scop, 0, 1, 0, 0, 0);
2860 /* Collect all definite write access relations.
2862 __isl_give isl_union_map *pet_scop_collect_must_writes(struct pet_scop *scop)
2864 return scop_collect_accesses(scop, 0, 1, 0, 1, 0);
2867 /* Collect all definite kill access relations.
2869 __isl_give isl_union_map *pet_scop_collect_must_kills(struct pet_scop *scop)
2871 return scop_collect_accesses(scop, 0, 0, 1, 1, 0);
2874 /* Collect all tagged potential read access relations.
2876 __isl_give isl_union_map *pet_scop_collect_tagged_may_reads(
2877 struct pet_scop *scop)
2879 return scop_collect_accesses(scop, 1, 0, 0, 0, 1);
2882 /* Collect all tagged potential write access relations.
2884 __isl_give isl_union_map *pet_scop_collect_tagged_may_writes(
2885 struct pet_scop *scop)
2887 return scop_collect_accesses(scop, 0, 1, 0, 0, 1);
2890 /* Collect all tagged definite write access relations.
2892 __isl_give isl_union_map *pet_scop_collect_tagged_must_writes(
2893 struct pet_scop *scop)
2895 return scop_collect_accesses(scop, 0, 1, 0, 1, 1);
2898 /* Collect all tagged definite kill access relations.
2900 __isl_give isl_union_map *pet_scop_collect_tagged_must_kills(
2901 struct pet_scop *scop)
2903 return scop_collect_accesses(scop, 0, 0, 1, 1, 1);
2906 /* Collect and return the union of iteration domains in "scop".
2908 __isl_give isl_union_set *pet_scop_collect_domains(struct pet_scop *scop)
2910 int i;
2911 isl_set *domain_i;
2912 isl_union_set *domain;
2914 if (!scop)
2915 return NULL;
2917 domain = isl_union_set_empty(isl_set_get_space(scop->context));
2919 for (i = 0; i < scop->n_stmt; ++i) {
2920 domain_i = isl_set_copy(scop->stmts[i]->domain);
2921 domain = isl_union_set_add_set(domain, domain_i);
2924 return domain;
2927 /* Collect and return the schedules of the statements in "scop".
2928 * The range is normalized to the maximal number of scheduling
2929 * dimensions.
2931 __isl_give isl_union_map *pet_scop_collect_schedule(struct pet_scop *scop)
2933 int i, j;
2934 isl_map *schedule_i;
2935 isl_union_map *schedule;
2936 int depth, max_depth = 0;
2938 if (!scop)
2939 return NULL;
2941 schedule = isl_union_map_empty(isl_set_get_space(scop->context));
2943 for (i = 0; i < scop->n_stmt; ++i) {
2944 depth = isl_map_dim(scop->stmts[i]->schedule, isl_dim_out);
2945 if (depth > max_depth)
2946 max_depth = depth;
2949 for (i = 0; i < scop->n_stmt; ++i) {
2950 schedule_i = isl_map_copy(scop->stmts[i]->schedule);
2951 depth = isl_map_dim(schedule_i, isl_dim_out);
2952 schedule_i = isl_map_add_dims(schedule_i, isl_dim_out,
2953 max_depth - depth);
2954 for (j = depth; j < max_depth; ++j)
2955 schedule_i = isl_map_fix_si(schedule_i,
2956 isl_dim_out, j, 0);
2957 schedule = isl_union_map_add_map(schedule, schedule_i);
2960 return schedule;
2963 /* Add a reference identifier to all access expressions in "stmt".
2964 * "n_ref" points to an integer that contains the sequence number
2965 * of the next reference.
2967 static struct pet_stmt *stmt_add_ref_ids(struct pet_stmt *stmt, int *n_ref)
2969 int i;
2971 if (!stmt)
2972 return NULL;
2974 for (i = 0; i < stmt->n_arg; ++i) {
2975 stmt->args[i] = pet_expr_add_ref_ids(stmt->args[i], n_ref);
2976 if (!stmt->args[i])
2977 return pet_stmt_free(stmt);
2980 stmt->body = pet_expr_add_ref_ids(stmt->body, n_ref);
2981 if (!stmt->body)
2982 return pet_stmt_free(stmt);
2984 return stmt;
2987 /* Add a reference identifier to all access expressions in "scop".
2989 struct pet_scop *pet_scop_add_ref_ids(struct pet_scop *scop)
2991 int i;
2992 int n_ref;
2994 if (!scop)
2995 return NULL;
2997 n_ref = 0;
2998 for (i = 0; i < scop->n_stmt; ++i) {
2999 scop->stmts[i] = stmt_add_ref_ids(scop->stmts[i], &n_ref);
3000 if (!scop->stmts[i])
3001 return pet_scop_free(scop);
3004 return scop;
3007 /* Reset the user pointer on all parameter ids in "array".
3009 static struct pet_array *array_anonymize(struct pet_array *array)
3011 if (!array)
3012 return NULL;
3014 array->context = isl_set_reset_user(array->context);
3015 array->extent = isl_set_reset_user(array->extent);
3016 if (!array->context || !array->extent)
3017 return pet_array_free(array);
3019 return array;
3022 /* Reset the user pointer on all parameter and tuple ids in "stmt".
3024 static struct pet_stmt *stmt_anonymize(struct pet_stmt *stmt)
3026 int i;
3027 isl_space *space;
3028 isl_set *domain;
3030 if (!stmt)
3031 return NULL;
3033 stmt->domain = isl_set_reset_user(stmt->domain);
3034 stmt->schedule = isl_map_reset_user(stmt->schedule);
3035 if (!stmt->domain || !stmt->schedule)
3036 return pet_stmt_free(stmt);
3038 for (i = 0; i < stmt->n_arg; ++i) {
3039 stmt->args[i] = pet_expr_anonymize(stmt->args[i]);
3040 if (!stmt->args[i])
3041 return pet_stmt_free(stmt);
3044 stmt->body = pet_expr_anonymize(stmt->body);
3045 if (!stmt->body)
3046 return pet_stmt_free(stmt);
3048 return stmt;
3051 /* Reset the user pointer on the tuple ids and all parameter ids
3052 * in "implication".
3054 static struct pet_implication *implication_anonymize(
3055 struct pet_implication *implication)
3057 if (!implication)
3058 return NULL;
3060 implication->extension = isl_map_reset_user(implication->extension);
3061 if (!implication->extension)
3062 return pet_implication_free(implication);
3064 return implication;
3067 /* Reset the user pointer on all parameter and tuple ids in "scop".
3069 struct pet_scop *pet_scop_anonymize(struct pet_scop *scop)
3071 int i;
3073 if (!scop)
3074 return NULL;
3076 scop->context = isl_set_reset_user(scop->context);
3077 scop->context_value = isl_set_reset_user(scop->context_value);
3078 if (!scop->context || !scop->context_value)
3079 return pet_scop_free(scop);
3081 for (i = 0; i < scop->n_array; ++i) {
3082 scop->arrays[i] = array_anonymize(scop->arrays[i]);
3083 if (!scop->arrays[i])
3084 return pet_scop_free(scop);
3087 for (i = 0; i < scop->n_stmt; ++i) {
3088 scop->stmts[i] = stmt_anonymize(scop->stmts[i]);
3089 if (!scop->stmts[i])
3090 return pet_scop_free(scop);
3093 for (i = 0; i < scop->n_implication; ++i) {
3094 scop->implications[i] =
3095 implication_anonymize(scop->implications[i]);
3096 if (!scop->implications[i])
3097 return pet_scop_free(scop);
3100 return scop;
3103 /* Compute the gist of the iteration domain and all access relations
3104 * of "stmt" based on the constraints on the parameters specified by "context"
3105 * and the constraints on the values of nested accesses specified
3106 * by "value_bounds".
3108 static struct pet_stmt *stmt_gist(struct pet_stmt *stmt,
3109 __isl_keep isl_set *context, __isl_keep isl_union_map *value_bounds)
3111 int i;
3112 isl_set *domain;
3114 if (!stmt)
3115 return NULL;
3117 domain = isl_set_copy(stmt->domain);
3118 if (stmt->n_arg > 0)
3119 domain = isl_map_domain(isl_set_unwrap(domain));
3121 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3123 for (i = 0; i < stmt->n_arg; ++i) {
3124 stmt->args[i] = pet_expr_gist(stmt->args[i],
3125 domain, value_bounds);
3126 if (!stmt->args[i])
3127 goto error;
3130 stmt->body = pet_expr_gist(stmt->body, domain, value_bounds);
3131 if (!stmt->body)
3132 goto error;
3134 isl_set_free(domain);
3136 domain = isl_set_universe(pet_stmt_get_space(stmt));
3137 domain = isl_set_intersect_params(domain, isl_set_copy(context));
3138 if (stmt->n_arg > 0)
3139 domain = pet_value_bounds_apply(domain, stmt->n_arg, stmt->args,
3140 value_bounds);
3141 stmt->domain = isl_set_gist(stmt->domain, domain);
3142 if (!stmt->domain)
3143 return pet_stmt_free(stmt);
3145 return stmt;
3146 error:
3147 isl_set_free(domain);
3148 return pet_stmt_free(stmt);
3151 /* Compute the gist of the extent of the array
3152 * based on the constraints on the parameters specified by "context".
3154 static struct pet_array *array_gist(struct pet_array *array,
3155 __isl_keep isl_set *context)
3157 if (!array)
3158 return NULL;
3160 array->extent = isl_set_gist_params(array->extent,
3161 isl_set_copy(context));
3162 if (!array->extent)
3163 return pet_array_free(array);
3165 return array;
3168 /* Compute the gist of all sets and relations in "scop"
3169 * based on the constraints on the parameters specified by "scop->context"
3170 * and the constraints on the values of nested accesses specified
3171 * by "value_bounds".
3173 struct pet_scop *pet_scop_gist(struct pet_scop *scop,
3174 __isl_keep isl_union_map *value_bounds)
3176 int i;
3178 if (!scop)
3179 return NULL;
3181 scop->context = isl_set_coalesce(scop->context);
3182 if (!scop->context)
3183 return pet_scop_free(scop);
3185 for (i = 0; i < scop->n_array; ++i) {
3186 scop->arrays[i] = array_gist(scop->arrays[i], scop->context);
3187 if (!scop->arrays[i])
3188 return pet_scop_free(scop);
3191 for (i = 0; i < scop->n_stmt; ++i) {
3192 scop->stmts[i] = stmt_gist(scop->stmts[i], scop->context,
3193 value_bounds);
3194 if (!scop->stmts[i])
3195 return pet_scop_free(scop);
3198 return scop;
3201 /* Intersect the context of "scop" with "context".
3202 * To ensure that we don't introduce any unnamed parameters in
3203 * the context of "scop", we first remove the unnamed parameters
3204 * from "context".
3206 struct pet_scop *pet_scop_restrict_context(struct pet_scop *scop,
3207 __isl_take isl_set *context)
3209 if (!scop)
3210 goto error;
3212 context = pet_nested_remove_from_set(context);
3213 scop->context = isl_set_intersect(scop->context, context);
3214 if (!scop->context)
3215 return pet_scop_free(scop);
3217 return scop;
3218 error:
3219 isl_set_free(context);
3220 return pet_scop_free(scop);
3223 /* Drop the current context of "scop". That is, replace the context
3224 * by a universal set.
3226 struct pet_scop *pet_scop_reset_context(struct pet_scop *scop)
3228 isl_space *space;
3230 if (!scop)
3231 return NULL;
3233 space = isl_set_get_space(scop->context);
3234 isl_set_free(scop->context);
3235 scop->context = isl_set_universe(space);
3236 if (!scop->context)
3237 return pet_scop_free(scop);
3239 return scop;
3242 /* Append "array" to the arrays of "scop".
3244 struct pet_scop *pet_scop_add_array(struct pet_scop *scop,
3245 struct pet_array *array)
3247 isl_ctx *ctx;
3248 struct pet_array **arrays;
3250 if (!array || !scop)
3251 goto error;
3253 ctx = isl_set_get_ctx(scop->context);
3254 arrays = isl_realloc_array(ctx, scop->arrays, struct pet_array *,
3255 scop->n_array + 1);
3256 if (!arrays)
3257 goto error;
3258 scop->arrays = arrays;
3259 scop->arrays[scop->n_array] = array;
3260 scop->n_array++;
3262 return scop;
3263 error:
3264 pet_array_free(array);
3265 return pet_scop_free(scop);
3268 /* Create an index expression for an access to a virtual array
3269 * representing the result of a condition.
3270 * Unlike other accessed data, the id of the array is NULL as
3271 * there is no ValueDecl in the program corresponding to the virtual
3272 * array.
3273 * The array starts out as a scalar, but grows along with the
3274 * statement writing to the array in pet_scop_embed.
3276 __isl_give isl_multi_pw_aff *pet_create_test_index(isl_ctx *ctx, int test_nr)
3278 isl_space *dim = isl_space_alloc(ctx, 0, 0, 0);
3279 isl_id *id;
3280 char name[50];
3282 snprintf(name, sizeof(name), "__pet_test_%d", test_nr);
3283 id = isl_id_alloc(ctx, name, NULL);
3284 dim = isl_space_set_tuple_id(dim, isl_dim_out, id);
3285 return isl_multi_pw_aff_zero(dim);
3288 /* Add an array with the given extent (range of "index") to the list
3289 * of arrays in "scop" and return the extended pet_scop.
3290 * "int_size" is the number of bytes needed to represent values of type "int".
3291 * The array is marked as attaining values 0 and 1 only and
3292 * as each element being assigned at most once.
3294 struct pet_scop *pet_scop_add_boolean_array(struct pet_scop *scop,
3295 __isl_take isl_multi_pw_aff *index, int int_size)
3297 isl_ctx *ctx;
3298 isl_space *space;
3299 struct pet_array *array;
3300 isl_map *access;
3302 if (!scop || !index)
3303 goto error;
3305 ctx = isl_multi_pw_aff_get_ctx(index);
3306 array = isl_calloc_type(ctx, struct pet_array);
3307 if (!array)
3308 goto error;
3310 access = isl_map_from_multi_pw_aff(index);
3311 array->extent = isl_map_range(access);
3312 space = isl_space_params_alloc(ctx, 0);
3313 array->context = isl_set_universe(space);
3314 space = isl_space_set_alloc(ctx, 0, 1);
3315 array->value_bounds = isl_set_universe(space);
3316 array->value_bounds = isl_set_lower_bound_si(array->value_bounds,
3317 isl_dim_set, 0, 0);
3318 array->value_bounds = isl_set_upper_bound_si(array->value_bounds,
3319 isl_dim_set, 0, 1);
3320 array->element_type = strdup("int");
3321 array->element_size = int_size;
3322 array->uniquely_defined = 1;
3324 if (!array->extent || !array->context)
3325 array = pet_array_free(array);
3327 scop = pet_scop_add_array(scop, array);
3329 return scop;
3330 error:
3331 isl_multi_pw_aff_free(index);
3332 return pet_scop_free(scop);
3335 /* Create and return an implication on filter values equal to "satisfied"
3336 * with extension "map".
3338 static struct pet_implication *new_implication(__isl_take isl_map *map,
3339 int satisfied)
3341 isl_ctx *ctx;
3342 struct pet_implication *implication;
3344 if (!map)
3345 return NULL;
3346 ctx = isl_map_get_ctx(map);
3347 implication = isl_alloc_type(ctx, struct pet_implication);
3348 if (!implication)
3349 goto error;
3351 implication->extension = map;
3352 implication->satisfied = satisfied;
3354 return implication;
3355 error:
3356 isl_map_free(map);
3357 return NULL;
3360 /* Add an implication on filter values equal to "satisfied"
3361 * with extension "map" to "scop".
3363 struct pet_scop *pet_scop_add_implication(struct pet_scop *scop,
3364 __isl_take isl_map *map, int satisfied)
3366 isl_ctx *ctx;
3367 struct pet_implication *implication;
3368 struct pet_implication **implications;
3370 implication = new_implication(map, satisfied);
3371 if (!scop || !implication)
3372 goto error;
3374 ctx = isl_set_get_ctx(scop->context);
3375 implications = isl_realloc_array(ctx, scop->implications,
3376 struct pet_implication *,
3377 scop->n_implication + 1);
3378 if (!implications)
3379 goto error;
3380 scop->implications = implications;
3381 scop->implications[scop->n_implication] = implication;
3382 scop->n_implication++;
3384 return scop;
3385 error:
3386 pet_implication_free(implication);
3387 return pet_scop_free(scop);
3390 /* Given an access expression, check if it is data dependent.
3391 * If so, set *found and abort the search.
3393 static int is_data_dependent(__isl_keep pet_expr *expr, void *user)
3395 int *found = user;
3397 if (pet_expr_get_n_arg(expr) > 0) {
3398 *found = 1;
3399 return -1;
3402 return 0;
3405 /* Does "scop" contain any data dependent accesses?
3407 * Check the body of each statement for such accesses.
3409 int pet_scop_has_data_dependent_accesses(struct pet_scop *scop)
3411 int i;
3412 int found = 0;
3414 if (!scop)
3415 return -1;
3417 for (i = 0; i < scop->n_stmt; ++i) {
3418 int r = pet_expr_foreach_access_expr(scop->stmts[i]->body,
3419 &is_data_dependent, &found);
3420 if (r < 0 && !found)
3421 return -1;
3422 if (found)
3423 return found;
3426 return found;
3429 /* Does "scop" contain and data dependent conditions?
3431 int pet_scop_has_data_dependent_conditions(struct pet_scop *scop)
3433 int i;
3435 if (!scop)
3436 return -1;
3438 for (i = 0; i < scop->n_stmt; ++i)
3439 if (scop->stmts[i]->n_arg > 0)
3440 return 1;
3442 return 0;
3445 /* Keep track of the "input" file inside the (extended) "scop".
3447 struct pet_scop *pet_scop_set_input_file(struct pet_scop *scop, FILE *input)
3449 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3451 if (!scop)
3452 return NULL;
3454 ext->input = input;
3456 return scop;
3459 /* Print the original code corresponding to "scop" to printer "p".
3461 * pet_scop_print_original can only be called from
3462 * a pet_transform_C_source callback. This means that the input
3463 * file is stored in the extended scop and that the printer prints
3464 * to a file.
3466 __isl_give isl_printer *pet_scop_print_original(struct pet_scop *scop,
3467 __isl_take isl_printer *p)
3469 struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
3470 FILE *output;
3471 unsigned start, end;
3473 if (!scop || !p)
3474 return isl_printer_free(p);
3476 if (!ext->input)
3477 isl_die(isl_printer_get_ctx(p), isl_error_invalid,
3478 "no input file stored in scop",
3479 return isl_printer_free(p));
3481 output = isl_printer_get_file(p);
3482 if (!output)
3483 return isl_printer_free(p);
3485 start = pet_loc_get_start(scop->loc);
3486 end = pet_loc_get_end(scop->loc);
3487 if (copy(ext->input, output, start, end) < 0)
3488 return isl_printer_free(p);
3490 return p;