2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
47 #include "value_bounds.h"
49 /* pet_scop with extra information that is used during parsing and printing.
51 * In particular, we keep track of conditions under which we want
52 * to skip the rest of the current loop iteration (skip[pet_skip_now])
53 * and of conditions under which we want to skip subsequent
54 * loop iterations (skip[pet_skip_later]).
56 * The conditions are represented as index expressions defined
57 * over the outer loop iterators. The index expression is either
58 * a boolean affine expression or an access to a variable, which
59 * is assumed to attain values zero and one. The condition holds
60 * if the variable has value one or if the affine expression
61 * has value one (typically for only part of the domain).
63 * A missing condition (skip[type] == NULL) means that we don't want
66 * Additionally, we keep track of the original input file
67 * inside pet_transform_C_source.
72 isl_multi_pw_aff
*skip
[2];
76 /* Construct a pet_stmt with given domain and statement number from a pet_tree.
77 * The input domain is anonymous and is the same as the domains
78 * of the access expressions inside "tree".
79 * These domains are modified to include the name of the statement.
80 * This name is given by tree->label if it is non-NULL.
81 * Otherwise, the name is constructed as S_<id>.
83 struct pet_stmt
*pet_stmt_from_pet_tree(__isl_take isl_set
*domain
,
84 int id
, __isl_take pet_tree
*tree
)
86 struct pet_stmt
*stmt
;
92 isl_multi_pw_aff
*add_name
;
98 ctx
= pet_tree_get_ctx(tree
);
99 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
104 label
= isl_id_copy(tree
->label
);
106 snprintf(name
, sizeof(name
), "S_%d", id
);
107 label
= isl_id_alloc(ctx
, name
, NULL
);
109 domain
= isl_set_set_tuple_id(domain
, label
);
110 space
= isl_set_get_space(domain
);
111 space
= pet_nested_remove_from_space(space
);
112 sched
= isl_map_universe(isl_space_from_domain(isl_space_copy(space
)));
113 ma
= pet_prefix_projection(space
, isl_space_dim(space
, isl_dim_set
));
115 add_name
= isl_multi_pw_aff_from_multi_aff(ma
);
116 tree
= pet_tree_update_domain(tree
, add_name
);
118 stmt
->loc
= pet_tree_get_loc(tree
);
119 stmt
->domain
= domain
;
120 stmt
->schedule
= sched
;
123 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
124 return pet_stmt_free(stmt
);
128 isl_set_free(domain
);
134 void *pet_stmt_free(struct pet_stmt
*stmt
)
141 pet_loc_free(stmt
->loc
);
142 isl_set_free(stmt
->domain
);
143 isl_map_free(stmt
->schedule
);
144 pet_tree_free(stmt
->body
);
146 for (i
= 0; i
< stmt
->n_arg
; ++i
)
147 pet_expr_free(stmt
->args
[i
]);
154 /* Return the iteration space of "stmt".
156 * If the statement has arguments, then stmt->domain is a wrapped map
157 * mapping the iteration domain to the values of the arguments
158 * for which this statement is executed.
159 * In this case, we need to extract the domain space of this wrapped map.
161 __isl_give isl_space
*pet_stmt_get_space(struct pet_stmt
*stmt
)
168 space
= isl_set_get_space(stmt
->domain
);
169 if (isl_space_is_wrapping(space
))
170 space
= isl_space_domain(isl_space_unwrap(space
));
175 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
182 fprintf(stderr
, "%*s%d\n", indent
, "", pet_loc_get_line(stmt
->loc
));
183 fprintf(stderr
, "%*s", indent
, "");
184 isl_set_dump(stmt
->domain
);
185 fprintf(stderr
, "%*s", indent
, "");
186 isl_map_dump(stmt
->schedule
);
187 pet_tree_dump_with_indent(stmt
->body
, indent
);
188 for (i
= 0; i
< stmt
->n_arg
; ++i
)
189 pet_expr_dump_with_indent(stmt
->args
[i
], indent
+ 2);
192 void pet_stmt_dump(struct pet_stmt
*stmt
)
197 /* Allocate a new pet_type with the given "name" and "definition".
199 struct pet_type
*pet_type_alloc(isl_ctx
*ctx
, const char *name
,
200 const char *definition
)
202 struct pet_type
*type
;
204 type
= isl_alloc_type(ctx
, struct pet_type
);
208 type
->name
= strdup(name
);
209 type
->definition
= strdup(definition
);
211 if (!type
->name
|| !type
->definition
)
212 return pet_type_free(type
);
217 /* Free "type" and return NULL.
219 struct pet_type
*pet_type_free(struct pet_type
*type
)
225 free(type
->definition
);
231 struct pet_array
*pet_array_free(struct pet_array
*array
)
236 isl_set_free(array
->context
);
237 isl_set_free(array
->extent
);
238 isl_set_free(array
->value_bounds
);
239 free(array
->element_type
);
245 void pet_array_dump(struct pet_array
*array
)
250 isl_set_dump(array
->context
);
251 isl_set_dump(array
->extent
);
252 isl_set_dump(array
->value_bounds
);
253 fprintf(stderr
, "%s%s%s\n", array
->element_type
,
254 array
->element_is_record
? " element-is-record" : "",
255 array
->live_out
? " live-out" : "");
258 /* Alloc a pet_scop structure, with extra room for information that
259 * is only used during parsing.
261 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
263 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
266 /* Construct a pet_scop in the given space and with room for n statements.
268 * The context is initialized as a universe set in "space".
270 * Since no information on the location is known at this point,
271 * scop->loc is initialized with pet_loc_dummy.
273 static struct pet_scop
*scop_alloc(__isl_take isl_space
*space
, int n
)
276 struct pet_scop
*scop
;
281 ctx
= isl_space_get_ctx(space
);
282 scop
= pet_scop_alloc(ctx
);
286 scop
->context
= isl_set_universe(isl_space_copy(space
));
287 scop
->context_value
= isl_set_universe(isl_space_params(space
));
288 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
289 if (!scop
->context
|| !scop
->stmts
)
290 return pet_scop_free(scop
);
292 scop
->loc
= &pet_loc_dummy
;
298 /* Construct a pet_scop in the given space containing 0 statements.
300 struct pet_scop
*pet_scop_empty(__isl_take isl_space
*space
)
302 return scop_alloc(space
, 0);
305 /* Return the constraints on the iteration domain in the access relation
307 * If the corresponding access expression has arguments then the domain
308 * of "access" is a wrapped relation with the iteration domain in the domain
309 * and the arguments in the range.
311 static __isl_give isl_set
*access_domain(__isl_take isl_map
*access
)
315 domain
= isl_map_domain(access
);
316 if (isl_set_is_wrapping(domain
))
317 domain
= isl_map_domain(isl_set_unwrap(domain
));
322 /* Update "context" with the constraints imposed on the outer iteration
323 * domain by "access".
324 * "context" lives in an anonymous space, while the domain of "access"
325 * refers to a particular statement. This reference therefore needs to be
328 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
329 __isl_take isl_set
*context
)
333 domain
= access_domain(isl_map_copy(access
));
334 domain
= isl_set_reset_tuple_id(domain
);
335 context
= isl_set_intersect(context
, domain
);
339 /* Update "context" with the constraints imposed on the outer iteration
342 * "context" lives in an anonymous space, while the domains of
343 * the access relations in "expr" refer to a particular statement.
344 * This reference therefore needs to be stripped off.
346 * If "expr" represents a conditional operator, then a parameter or outer
347 * iterator value needs to be valid for the condition and
348 * for at least one of the remaining two arguments.
349 * If the condition is an affine expression, then we can be a bit more specific.
350 * The value then has to be valid for the second argument for
351 * non-zero accesses and valid for the third argument for zero accesses.
353 static __isl_give isl_set
*expr_extract_context(__isl_keep pet_expr
*expr
,
354 __isl_take isl_set
*context
)
358 if (expr
->type
== pet_expr_op
&& expr
->op
== pet_op_cond
) {
360 isl_set
*context1
, *context2
;
362 is_aff
= pet_expr_is_affine(expr
->args
[0]);
366 context
= expr_extract_context(expr
->args
[0], context
);
367 context1
= expr_extract_context(expr
->args
[1],
368 isl_set_copy(context
));
369 context2
= expr_extract_context(expr
->args
[2], context
);
375 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
376 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
377 zero_set
= access_domain(access
);
378 zero_set
= isl_set_reset_tuple_id(zero_set
);
379 context1
= isl_set_subtract(context1
,
380 isl_set_copy(zero_set
));
381 context2
= isl_set_intersect(context2
, zero_set
);
384 context
= isl_set_union(context1
, context2
);
385 context
= isl_set_coalesce(context
);
390 for (i
= 0; i
< expr
->n_arg
; ++i
)
391 context
= expr_extract_context(expr
->args
[i
], context
);
393 if (expr
->type
== pet_expr_access
)
394 context
= access_extract_context(expr
->acc
.access
, context
);
398 isl_set_free(context
);
402 /* Is "stmt" an assume statement with an affine assumption?
404 int pet_stmt_is_affine_assume(struct pet_stmt
*stmt
)
408 return pet_tree_is_affine_assume(stmt
->body
);
411 /* Given an assume statement "stmt" with an access argument,
412 * return the index expression of the argument.
414 __isl_give isl_multi_pw_aff
*pet_stmt_assume_get_index(struct pet_stmt
*stmt
)
418 return pet_tree_assume_get_index(stmt
->body
);
421 /* Update "context" with the constraints imposed on the outer iteration
424 * If the statement is an assume statement with an affine expression,
425 * then intersect "context" with that expression.
426 * Otherwise, if the statement body is an expression tree,
427 * then intersect "context" with the context of this expression.
428 * Note that we cannot safely extract a context from subtrees
429 * of the statement body since we cannot tell when those subtrees
430 * are executed, if at all.
432 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
433 __isl_take isl_set
*context
)
438 if (pet_stmt_is_affine_assume(stmt
)) {
439 isl_multi_pw_aff
*index
;
443 index
= pet_stmt_assume_get_index(stmt
);
444 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
445 isl_multi_pw_aff_free(index
);
446 cond
= isl_pw_aff_non_zero_set(pa
);
447 cond
= isl_set_reset_tuple_id(cond
);
448 return isl_set_intersect(context
, cond
);
451 for (i
= 0; i
< stmt
->n_arg
; ++i
)
452 context
= expr_extract_context(stmt
->args
[i
], context
);
454 if (pet_tree_get_type(stmt
->body
) != pet_tree_expr
)
457 body
= pet_tree_expr_get_expr(stmt
->body
);
458 context
= expr_extract_context(body
, context
);
464 /* Construct a pet_scop in the given space that contains the given pet_stmt.
466 struct pet_scop
*pet_scop_from_pet_stmt(__isl_take isl_space
*space
,
467 struct pet_stmt
*stmt
)
469 struct pet_scop
*scop
;
472 space
= isl_space_free(space
);
474 scop
= scop_alloc(space
, 1);
478 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
482 scop
->stmts
[0] = stmt
;
483 scop
->loc
= pet_loc_copy(stmt
->loc
);
486 return pet_scop_free(scop
);
495 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
496 * does it represent an affine expression?
498 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff
*mpa
)
502 has_id
= isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
);
509 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
511 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
512 __isl_take isl_set
*dom
)
515 pa
= isl_set_indicator_function(set
);
516 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
520 /* Return "lhs || rhs", defined on the shared definition domain.
522 static __isl_give isl_pw_aff
*pw_aff_or(__isl_take isl_pw_aff
*lhs
,
523 __isl_take isl_pw_aff
*rhs
)
528 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
529 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
530 cond
= isl_set_union(isl_pw_aff_non_zero_set(lhs
),
531 isl_pw_aff_non_zero_set(rhs
));
532 cond
= isl_set_coalesce(cond
);
533 return indicator_function(cond
, dom
);
536 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
537 * ext may be equal to either ext1 or ext2.
539 * The two skips that need to be combined are assumed to be affine expressions.
541 * We need to skip in ext if we need to skip in either ext1 or ext2.
542 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
544 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
545 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
548 isl_pw_aff
*skip
, *skip1
, *skip2
;
552 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
554 if (!ext1
->skip
[type
]) {
557 ext
->skip
[type
] = ext2
->skip
[type
];
558 ext2
->skip
[type
] = NULL
;
561 if (!ext2
->skip
[type
]) {
564 ext
->skip
[type
] = ext1
->skip
[type
];
565 ext1
->skip
[type
] = NULL
;
569 if (!multi_pw_aff_is_affine(ext1
->skip
[type
]) ||
570 !multi_pw_aff_is_affine(ext2
->skip
[type
]))
571 isl_die(isl_multi_pw_aff_get_ctx(ext1
->skip
[type
]),
572 isl_error_internal
, "can only combine affine skips",
575 skip1
= isl_multi_pw_aff_get_pw_aff(ext1
->skip
[type
], 0);
576 skip2
= isl_multi_pw_aff_get_pw_aff(ext2
->skip
[type
], 0);
577 skip
= pw_aff_or(skip1
, skip2
);
578 isl_multi_pw_aff_free(ext1
->skip
[type
]);
579 ext1
->skip
[type
] = NULL
;
580 isl_multi_pw_aff_free(ext2
->skip
[type
]);
581 ext2
->skip
[type
] = NULL
;
582 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
583 if (!ext
->skip
[type
])
588 pet_scop_free(&ext
->scop
);
592 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
593 * where type takes on the values pet_skip_now and pet_skip_later.
594 * scop may be equal to either scop1 or scop2.
596 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
597 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
599 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
600 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
601 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
603 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
604 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
608 /* Update start and end of scop->loc to include the region from "start"
609 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
610 * does not have any offset information yet and we simply take the information
611 * from "start" and "end". Otherwise, we update loc using "start" and "end".
613 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
614 unsigned start
, unsigned end
)
619 if (scop
->loc
== &pet_loc_dummy
)
620 scop
->loc
= pet_loc_alloc(isl_set_get_ctx(scop
->context
),
621 start
, end
, -1, strdup(""));
623 scop
->loc
= pet_loc_update_start_end(scop
->loc
, start
, end
);
626 return pet_scop_free(scop
);
631 /* Update start and end of scop->loc to include the region identified
634 struct pet_scop
*pet_scop_update_start_end_from_loc(struct pet_scop
*scop
,
635 __isl_keep pet_loc
*loc
)
637 return pet_scop_update_start_end(scop
, pet_loc_get_start(loc
),
638 pet_loc_get_end(loc
));
641 /* Replace the location of "scop" by "loc".
643 struct pet_scop
*pet_scop_set_loc(struct pet_scop
*scop
,
644 __isl_take pet_loc
*loc
)
649 pet_loc_free(scop
->loc
);
659 /* Does "implication" appear in the list of implications of "scop"?
661 static int is_known_implication(struct pet_scop
*scop
,
662 struct pet_implication
*implication
)
666 for (i
= 0; i
< scop
->n_implication
; ++i
) {
667 struct pet_implication
*pi
= scop
->implications
[i
];
670 if (pi
->satisfied
!= implication
->satisfied
)
672 equal
= isl_map_is_equal(pi
->extension
, implication
->extension
);
682 /* Store the concatenation of the implications of "scop1" and "scop2"
683 * in "scop", removing duplicates (i.e., implications in "scop2" that
684 * already appear in "scop1").
686 static struct pet_scop
*scop_collect_implications(isl_ctx
*ctx
,
687 struct pet_scop
*scop
, struct pet_scop
*scop1
, struct pet_scop
*scop2
)
694 if (scop2
->n_implication
== 0) {
695 scop
->n_implication
= scop1
->n_implication
;
696 scop
->implications
= scop1
->implications
;
697 scop1
->n_implication
= 0;
698 scop1
->implications
= NULL
;
702 if (scop1
->n_implication
== 0) {
703 scop
->n_implication
= scop2
->n_implication
;
704 scop
->implications
= scop2
->implications
;
705 scop2
->n_implication
= 0;
706 scop2
->implications
= NULL
;
710 scop
->implications
= isl_calloc_array(ctx
, struct pet_implication
*,
711 scop1
->n_implication
+ scop2
->n_implication
);
712 if (!scop
->implications
)
713 return pet_scop_free(scop
);
715 for (i
= 0; i
< scop1
->n_implication
; ++i
) {
716 scop
->implications
[i
] = scop1
->implications
[i
];
717 scop1
->implications
[i
] = NULL
;
720 scop
->n_implication
= scop1
->n_implication
;
721 j
= scop1
->n_implication
;
722 for (i
= 0; i
< scop2
->n_implication
; ++i
) {
725 known
= is_known_implication(scop
, scop2
->implications
[i
]);
727 return pet_scop_free(scop
);
730 scop
->implications
[j
++] = scop2
->implications
[i
];
731 scop2
->implications
[i
] = NULL
;
733 scop
->n_implication
= j
;
738 /* Combine the offset information of "scop1" and "scop2" into "scop".
740 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
741 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
743 if (scop1
->loc
!= &pet_loc_dummy
)
744 scop
= pet_scop_update_start_end_from_loc(scop
, scop1
->loc
);
745 if (scop2
->loc
!= &pet_loc_dummy
)
746 scop
= pet_scop_update_start_end_from_loc(scop
, scop2
->loc
);
750 /* Construct a pet_scop that contains the offset information,
751 * arrays, statements and skip information in "scop1" and "scop2".
753 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
754 struct pet_scop
*scop2
)
758 struct pet_scop
*scop
= NULL
;
760 if (!scop1
|| !scop2
)
763 if (scop1
->n_stmt
== 0) {
764 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
765 pet_scop_free(scop1
);
769 if (scop2
->n_stmt
== 0) {
770 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
771 pet_scop_free(scop2
);
775 space
= isl_set_get_space(scop1
->context
);
776 scop
= scop_alloc(space
, scop1
->n_stmt
+ scop2
->n_stmt
);
780 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
781 scop1
->n_array
+ scop2
->n_array
);
784 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
786 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
787 scop
->stmts
[i
] = scop1
->stmts
[i
];
788 scop1
->stmts
[i
] = NULL
;
791 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
792 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
793 scop2
->stmts
[i
] = NULL
;
796 for (i
= 0; i
< scop1
->n_array
; ++i
) {
797 scop
->arrays
[i
] = scop1
->arrays
[i
];
798 scop1
->arrays
[i
] = NULL
;
801 for (i
= 0; i
< scop2
->n_array
; ++i
) {
802 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
803 scop2
->arrays
[i
] = NULL
;
806 scop
= scop_collect_implications(ctx
, scop
, scop1
, scop2
);
807 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
808 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
809 scop
= scop_combine_skips(scop
, scop1
, scop2
);
810 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
812 pet_scop_free(scop1
);
813 pet_scop_free(scop2
);
816 pet_scop_free(scop1
);
817 pet_scop_free(scop2
);
822 /* Apply the skip condition "skip" to "scop".
823 * That is, make sure "scop" is not executed when the condition holds.
825 * If "skip" is an affine expression, we add the conditions under
826 * which the expression is zero to the iteration domains.
827 * Otherwise, we add a filter on the variable attaining the value zero.
829 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
830 __isl_take isl_multi_pw_aff
*skip
)
839 is_aff
= multi_pw_aff_is_affine(skip
);
844 return pet_scop_filter(scop
, skip
, 0);
846 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
847 isl_multi_pw_aff_free(skip
);
848 zero
= isl_pw_aff_zero_set(pa
);
849 scop
= pet_scop_restrict(scop
, zero
);
853 isl_multi_pw_aff_free(skip
);
854 return pet_scop_free(scop
);
857 /* Construct a pet_scop that contains the arrays, statements and
858 * skip information in "scop1" and "scop2", where the two scops
859 * are executed "in sequence". That is, breaks and continues
860 * in scop1 have an effect on scop2.
862 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
863 struct pet_scop
*scop2
)
865 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
866 scop2
= restrict_skip(scop2
,
867 pet_scop_get_skip(scop1
, pet_skip_now
));
868 return pet_scop_add(ctx
, scop1
, scop2
);
871 /* Construct a pet_scop that contains the arrays, statements and
872 * skip information in "scop1" and "scop2", where the two scops
873 * are executed "in parallel". That is, any break or continue
874 * in scop1 has no effect on scop2.
876 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
877 struct pet_scop
*scop2
)
879 return pet_scop_add(ctx
, scop1
, scop2
);
882 void *pet_implication_free(struct pet_implication
*implication
)
889 isl_map_free(implication
->extension
);
895 struct pet_scop
*pet_scop_free(struct pet_scop
*scop
)
898 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
902 pet_loc_free(scop
->loc
);
903 isl_set_free(scop
->context
);
904 isl_set_free(scop
->context_value
);
906 for (i
= 0; i
< scop
->n_type
; ++i
)
907 pet_type_free(scop
->types
[i
]);
910 for (i
= 0; i
< scop
->n_array
; ++i
)
911 pet_array_free(scop
->arrays
[i
]);
914 for (i
= 0; i
< scop
->n_stmt
; ++i
)
915 pet_stmt_free(scop
->stmts
[i
]);
917 if (scop
->implications
)
918 for (i
= 0; i
< scop
->n_implication
; ++i
)
919 pet_implication_free(scop
->implications
[i
]);
920 free(scop
->implications
);
921 isl_multi_pw_aff_free(ext
->skip
[pet_skip_now
]);
922 isl_multi_pw_aff_free(ext
->skip
[pet_skip_later
]);
927 void pet_type_dump(struct pet_type
*type
)
932 fprintf(stderr
, "%s -> %s\n", type
->name
, type
->definition
);
935 void pet_implication_dump(struct pet_implication
*implication
)
940 fprintf(stderr
, "%d\n", implication
->satisfied
);
941 isl_map_dump(implication
->extension
);
944 void pet_scop_dump(struct pet_scop
*scop
)
947 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
952 isl_set_dump(scop
->context
);
953 isl_set_dump(scop
->context_value
);
954 for (i
= 0; i
< scop
->n_type
; ++i
)
955 pet_type_dump(scop
->types
[i
]);
956 for (i
= 0; i
< scop
->n_array
; ++i
)
957 pet_array_dump(scop
->arrays
[i
]);
958 for (i
= 0; i
< scop
->n_stmt
; ++i
)
959 pet_stmt_dump(scop
->stmts
[i
]);
960 for (i
= 0; i
< scop
->n_implication
; ++i
)
961 pet_implication_dump(scop
->implications
[i
]);
964 fprintf(stderr
, "skip\n");
965 isl_multi_pw_aff_dump(ext
->skip
[0]);
966 isl_multi_pw_aff_dump(ext
->skip
[1]);
970 /* Return 1 if the two pet_arrays are equivalent.
972 * We don't compare element_size as this may be target dependent.
974 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
976 if (!array1
|| !array2
)
979 if (!isl_set_is_equal(array1
->context
, array2
->context
))
981 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
983 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
985 if (array1
->value_bounds
&&
986 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
988 if (strcmp(array1
->element_type
, array2
->element_type
))
990 if (array1
->element_is_record
!= array2
->element_is_record
)
992 if (array1
->live_out
!= array2
->live_out
)
994 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
996 if (array1
->declared
!= array2
->declared
)
998 if (array1
->exposed
!= array2
->exposed
)
1004 /* Return 1 if the two pet_stmts are equivalent.
1006 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
1010 if (!stmt1
|| !stmt2
)
1013 if (pet_loc_get_line(stmt1
->loc
) != pet_loc_get_line(stmt2
->loc
))
1015 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
1017 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
1019 if (!pet_tree_is_equal(stmt1
->body
, stmt2
->body
))
1021 if (stmt1
->n_arg
!= stmt2
->n_arg
)
1023 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
1024 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
1031 /* Return 1 if the two pet_types are equivalent.
1033 * We only compare the names of the types since the exact representation
1034 * of the definition may depend on the version of clang being used.
1036 int pet_type_is_equal(struct pet_type
*type1
, struct pet_type
*type2
)
1038 if (!type1
|| !type2
)
1041 if (strcmp(type1
->name
, type2
->name
))
1047 /* Return 1 if the two pet_implications are equivalent.
1049 int pet_implication_is_equal(struct pet_implication
*implication1
,
1050 struct pet_implication
*implication2
)
1052 if (!implication1
|| !implication2
)
1055 if (implication1
->satisfied
!= implication2
->satisfied
)
1057 if (!isl_map_is_equal(implication1
->extension
, implication2
->extension
))
1063 /* Return 1 if the two pet_scops are equivalent.
1065 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1069 if (!scop1
|| !scop2
)
1072 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1074 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1077 if (scop1
->n_type
!= scop2
->n_type
)
1079 for (i
= 0; i
< scop1
->n_type
; ++i
)
1080 if (!pet_type_is_equal(scop1
->types
[i
], scop2
->types
[i
]))
1083 if (scop1
->n_array
!= scop2
->n_array
)
1085 for (i
= 0; i
< scop1
->n_array
; ++i
)
1086 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1089 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1091 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1092 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1095 if (scop1
->n_implication
!= scop2
->n_implication
)
1097 for (i
= 0; i
< scop1
->n_implication
; ++i
)
1098 if (!pet_implication_is_equal(scop1
->implications
[i
],
1099 scop2
->implications
[i
]))
1105 /* Does the set "extent" reference a virtual array, i.e.,
1106 * one with user pointer equal to NULL?
1107 * A virtual array does not have any members.
1109 static int extent_is_virtual_array(__isl_keep isl_set
*extent
)
1114 if (!isl_set_has_tuple_id(extent
))
1116 if (isl_set_is_wrapping(extent
))
1118 id
= isl_set_get_tuple_id(extent
);
1119 is_virtual
= !isl_id_get_user(id
);
1125 /* Intersect the initial dimensions of "array" with "domain", provided
1126 * that "array" represents a virtual array.
1128 * If "array" is virtual, then We take the preimage of "domain"
1129 * over the projection of the extent of "array" onto its initial dimensions
1130 * and intersect this extent with the result.
1132 static struct pet_array
*virtual_array_intersect_domain_prefix(
1133 struct pet_array
*array
, __isl_take isl_set
*domain
)
1139 if (!array
|| !extent_is_virtual_array(array
->extent
)) {
1140 isl_set_free(domain
);
1144 space
= isl_set_get_space(array
->extent
);
1145 n
= isl_set_dim(domain
, isl_dim_set
);
1146 ma
= pet_prefix_projection(space
, n
);
1147 domain
= isl_set_preimage_multi_aff(domain
, ma
);
1149 array
->extent
= isl_set_intersect(array
->extent
, domain
);
1151 return pet_array_free(array
);
1156 /* Intersect the initial dimensions of the domain of "stmt"
1159 * We take the preimage of "domain" over the projection of the
1160 * domain of "stmt" onto its initial dimensions and intersect
1161 * the domain of "stmt" with the result.
1163 static struct pet_stmt
*stmt_intersect_domain_prefix(struct pet_stmt
*stmt
,
1164 __isl_take isl_set
*domain
)
1173 space
= isl_set_get_space(stmt
->domain
);
1174 n
= isl_set_dim(domain
, isl_dim_set
);
1175 ma
= pet_prefix_projection(space
, n
);
1176 domain
= isl_set_preimage_multi_aff(domain
, ma
);
1178 stmt
->domain
= isl_set_intersect(stmt
->domain
, domain
);
1180 return pet_stmt_free(stmt
);
1184 isl_set_free(domain
);
1185 return pet_stmt_free(stmt
);
1188 /* Intersect the initial dimensions of the domain of "implication"
1191 * We take the preimage of "domain" over the projection of the
1192 * domain of "implication" onto its initial dimensions and intersect
1193 * the domain of "implication" with the result.
1195 static struct pet_implication
*implication_intersect_domain_prefix(
1196 struct pet_implication
*implication
, __isl_take isl_set
*domain
)
1205 space
= isl_map_get_space(implication
->extension
);
1206 n
= isl_set_dim(domain
, isl_dim_set
);
1207 ma
= pet_prefix_projection(isl_space_domain(space
), n
);
1208 domain
= isl_set_preimage_multi_aff(domain
, ma
);
1210 implication
->extension
=
1211 isl_map_intersect_domain(implication
->extension
, domain
);
1212 if (!implication
->extension
)
1213 return pet_implication_free(implication
);
1217 isl_set_free(domain
);
1218 return pet_implication_free(implication
);
1221 /* Intersect the initial dimensions of the domains in "scop" with "domain".
1223 * The extents of the virtual arrays match the iteration domains,
1224 * so if the iteration domain changes, we need to change those extents too.
1226 struct pet_scop
*pet_scop_intersect_domain_prefix(struct pet_scop
*scop
,
1227 __isl_take isl_set
*domain
)
1234 for (i
= 0; i
< scop
->n_array
; ++i
) {
1235 scop
->arrays
[i
] = virtual_array_intersect_domain_prefix(
1236 scop
->arrays
[i
], isl_set_copy(domain
));
1237 if (!scop
->arrays
[i
])
1241 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1242 scop
->stmts
[i
] = stmt_intersect_domain_prefix(scop
->stmts
[i
],
1243 isl_set_copy(domain
));
1244 if (!scop
->stmts
[i
])
1248 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1249 scop
->implications
[i
] =
1250 implication_intersect_domain_prefix(scop
->implications
[i
],
1251 isl_set_copy(domain
));
1252 if (!scop
->implications
[i
])
1253 return pet_scop_free(scop
);
1256 isl_set_free(domain
);
1259 isl_set_free(domain
);
1260 return pet_scop_free(scop
);
1263 /* Prefix the schedule of "stmt" with an extra dimension with constant
1266 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1271 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1272 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1273 if (!stmt
->schedule
)
1274 return pet_stmt_free(stmt
);
1279 /* Prefix the schedules of all statements in "scop" with an extra
1280 * dimension with constant value "pos".
1282 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1289 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1290 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1291 if (!scop
->stmts
[i
])
1292 return pet_scop_free(scop
);
1298 /* Prefix the schedule of "stmt" with "sched".
1300 * The domain of "sched" refers the current outer loop iterators and
1301 * needs to be mapped to the iteration domain of "stmt" first
1302 * before being prepended to the schedule of "stmt".
1304 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1305 __isl_take isl_map
*sched
)
1314 space
= pet_stmt_get_space(stmt
);
1315 n
= isl_map_dim(sched
, isl_dim_in
);
1316 ma
= pet_prefix_projection(space
, n
);
1317 sched
= isl_map_preimage_domain_multi_aff(sched
, ma
);
1318 stmt
->schedule
= isl_map_flat_range_product(sched
, stmt
->schedule
);
1319 if (!stmt
->schedule
)
1320 return pet_stmt_free(stmt
);
1324 isl_map_free(sched
);
1328 /* Update the context with respect to an embedding into a loop
1329 * with iteration domain "dom".
1330 * The input context lives in the same space as "dom".
1331 * The output context has the inner dimension removed.
1333 * An outer loop iterator value is invalid for the embedding if
1334 * any of the corresponding inner iterator values is invalid.
1335 * That is, an outer loop iterator value is valid only if all the corresponding
1336 * inner iterator values are valid.
1337 * We therefore compute the set of outer loop iterators l
1339 * forall i: dom(l,i) => valid(l,i)
1343 * forall i: not dom(l,i) or valid(l,i)
1347 * not exists i: dom(l,i) and not valid(l,i)
1351 * not exists i: (dom \ valid)(l,i)
1353 * If there are any unnamed parameters in "dom", then we consider
1354 * a parameter value to be valid if it is valid for any value of those
1355 * unnamed parameters. They are therefore projected out at the end.
1357 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1358 __isl_keep isl_set
*dom
)
1362 pos
= isl_set_dim(context
, isl_dim_set
) - 1;
1363 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1364 context
= isl_set_project_out(context
, isl_dim_set
, pos
, 1);
1365 context
= isl_set_complement(context
);
1366 context
= pet_nested_remove_from_set(context
);
1371 /* Update the implication with respect to an embedding into a loop
1372 * with iteration domain "dom".
1374 * Since embed_access extends virtual arrays along with the domain
1375 * of the access, we need to do the same with domain and range
1376 * of the implication. Since the original implication is only valid
1377 * within a given iteration of the loop, the extended implication
1378 * maps the extra array dimension corresponding to the extra loop
1381 static struct pet_implication
*pet_implication_embed(
1382 struct pet_implication
*implication
, __isl_take isl_set
*dom
)
1390 map
= isl_set_identity(dom
);
1391 id
= isl_map_get_tuple_id(implication
->extension
, isl_dim_in
);
1392 map
= isl_map_flat_product(map
, implication
->extension
);
1393 map
= isl_map_set_tuple_id(map
, isl_dim_in
, isl_id_copy(id
));
1394 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
1395 implication
->extension
= map
;
1396 if (!implication
->extension
)
1397 return pet_implication_free(implication
);
1405 /* Adjust the context and statement schedules according to an embedding
1406 * in a loop with iteration domain "dom" and schedule "sched".
1408 * Any skip conditions within the loop have no effect outside of the loop.
1409 * The caller is responsible for making sure skip[pet_skip_later] has been
1410 * taken into account.
1412 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1413 __isl_take isl_aff
*sched
)
1418 sched_map
= isl_map_from_aff(sched
);
1423 pet_scop_reset_skip(scop
, pet_skip_now
);
1424 pet_scop_reset_skip(scop
, pet_skip_later
);
1426 scop
->context
= context_embed(scop
->context
, dom
);
1430 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1431 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1432 isl_map_copy(sched_map
));
1433 if (!scop
->stmts
[i
])
1438 isl_map_free(sched_map
);
1442 isl_map_free(sched_map
);
1443 return pet_scop_free(scop
);
1446 /* Add extra conditions to scop->skip[type].
1448 * The new skip condition only holds if it held before
1449 * and the condition is true. It does not hold if it did not hold
1450 * before or the condition is false.
1452 * The skip condition is assumed to be an affine expression.
1454 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1455 enum pet_skip type
, __isl_keep isl_set
*cond
)
1457 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1463 if (!ext
->skip
[type
])
1466 if (!multi_pw_aff_is_affine(ext
->skip
[type
]))
1467 isl_die(isl_multi_pw_aff_get_ctx(ext
->skip
[type
]),
1468 isl_error_internal
, "can only restrict affine skips",
1469 return pet_scop_free(scop
));
1471 skip
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
1472 dom
= isl_pw_aff_domain(isl_pw_aff_copy(skip
));
1473 cond
= isl_set_copy(cond
);
1474 cond
= isl_set_intersect(cond
, isl_pw_aff_non_zero_set(skip
));
1475 skip
= indicator_function(cond
, dom
);
1476 isl_multi_pw_aff_free(ext
->skip
[type
]);
1477 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
1478 if (!ext
->skip
[type
])
1479 return pet_scop_free(scop
);
1484 /* Adjust the context and the skip conditions to the fact that
1485 * the scop was created in a context where "cond" holds.
1487 * An outer loop iterator or parameter value is valid for the result
1488 * if it was valid for the original scop and satisfies "cond" or if it does
1489 * not satisfy "cond" as in this case the scop is not executed
1490 * and the original constraints on these values are irrelevant.
1492 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1493 __isl_take isl_set
*cond
)
1497 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1498 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1503 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1504 scop
->context
= isl_set_union(scop
->context
,
1505 isl_set_complement(isl_set_copy(cond
)));
1506 scop
->context
= isl_set_coalesce(scop
->context
);
1507 scop
->context
= pet_nested_remove_from_set(scop
->context
);
1515 return pet_scop_free(scop
);
1518 /* Insert an argument expression corresponding to "test" in front
1519 * of the list of arguments described by *n_arg and *args.
1521 static int args_insert_access(unsigned *n_arg
, pet_expr
***args
,
1522 __isl_keep isl_multi_pw_aff
*test
)
1525 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1531 *args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1536 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + *n_arg
);
1539 for (i
= 0; i
< *n_arg
; ++i
)
1540 ext
[1 + i
] = (*args
)[i
];
1545 (*args
)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1552 /* Look through the applications in "scop" for any that can be
1553 * applied to the filter expressed by "map" and "satisified".
1554 * If there is any, then apply it to "map" and return the result.
1555 * Otherwise, return "map".
1556 * "id" is the identifier of the virtual array.
1558 * We only introduce at most one implication for any given virtual array,
1559 * so we can apply the implication and return as soon as we find one.
1561 static __isl_give isl_map
*apply_implications(struct pet_scop
*scop
,
1562 __isl_take isl_map
*map
, __isl_keep isl_id
*id
, int satisfied
)
1566 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1567 struct pet_implication
*pi
= scop
->implications
[i
];
1570 if (pi
->satisfied
!= satisfied
)
1572 pi_id
= isl_map_get_tuple_id(pi
->extension
, isl_dim_in
);
1577 return isl_map_apply_range(map
, isl_map_copy(pi
->extension
));
1583 /* Is the filter expressed by "test" and "satisfied" implied
1584 * by filter "pos" on "domain", with filter "expr", taking into
1585 * account the implications of "scop"?
1587 * For filter on domain implying that expressed by "test" and "satisfied",
1588 * the filter needs to be an access to the same (virtual) array as "test" and
1589 * the filter value needs to be equal to "satisfied".
1590 * Moreover, the filter access relation, possibly extended by
1591 * the implications in "scop" needs to contain "test".
1593 static int implies_filter(struct pet_scop
*scop
,
1594 __isl_keep isl_map
*domain
, int pos
, __isl_keep pet_expr
*expr
,
1595 __isl_keep isl_map
*test
, int satisfied
)
1597 isl_id
*test_id
, *arg_id
;
1604 if (expr
->type
!= pet_expr_access
)
1606 test_id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1607 arg_id
= pet_expr_access_get_id(expr
);
1608 isl_id_free(arg_id
);
1609 isl_id_free(test_id
);
1610 if (test_id
!= arg_id
)
1612 val
= isl_map_plain_get_val_if_fixed(domain
, isl_dim_out
, pos
);
1613 is_int
= isl_val_is_int(val
);
1615 s
= isl_val_get_num_si(val
);
1624 implied
= isl_map_copy(expr
->acc
.access
);
1625 implied
= apply_implications(scop
, implied
, test_id
, satisfied
);
1626 is_subset
= isl_map_is_subset(test
, implied
);
1627 isl_map_free(implied
);
1632 /* Is the filter expressed by "test" and "satisfied" implied
1633 * by any of the filters on the domain of "stmt", taking into
1634 * account the implications of "scop"?
1636 static int filter_implied(struct pet_scop
*scop
,
1637 struct pet_stmt
*stmt
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
1645 if (!scop
|| !stmt
|| !test
)
1647 if (scop
->n_implication
== 0)
1649 if (stmt
->n_arg
== 0)
1652 domain
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
1653 test_map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test
));
1656 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
1657 implied
= implies_filter(scop
, domain
, i
, stmt
->args
[i
],
1658 test_map
, satisfied
);
1659 if (implied
< 0 || implied
)
1663 isl_map_free(test_map
);
1664 isl_map_free(domain
);
1668 /* Make the statement "stmt" depend on the value of "test"
1669 * being equal to "satisfied" by adjusting stmt->domain.
1671 * The domain of "test" corresponds to the (zero or more) outer dimensions
1672 * of the iteration domain.
1674 * We first extend "test" to apply to the entire iteration domain and
1675 * then check if the filter that we are about to add is implied
1676 * by any of the current filters, possibly taking into account
1677 * the implications in "scop". If so, we leave "stmt" untouched and return.
1679 * Otherwise, we insert an argument corresponding to a read to "test"
1680 * from the iteration domain of "stmt" in front of the list of arguments.
1681 * We also insert a corresponding output dimension in the wrapped
1682 * map contained in stmt->domain, with value set to "satisfied".
1684 static struct pet_stmt
*stmt_filter(struct pet_scop
*scop
,
1685 struct pet_stmt
*stmt
, __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1691 isl_pw_multi_aff
*pma
;
1692 isl_multi_aff
*add_dom
;
1694 isl_local_space
*ls
;
1700 space
= pet_stmt_get_space(stmt
);
1701 n_test_dom
= isl_multi_pw_aff_dim(test
, isl_dim_in
);
1702 space
= isl_space_from_domain(space
);
1703 space
= isl_space_add_dims(space
, isl_dim_out
, n_test_dom
);
1704 add_dom
= isl_multi_aff_zero(isl_space_copy(space
));
1705 ls
= isl_local_space_from_space(isl_space_domain(space
));
1706 for (i
= 0; i
< n_test_dom
; ++i
) {
1708 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1710 add_dom
= isl_multi_aff_set_aff(add_dom
, i
, aff
);
1712 isl_local_space_free(ls
);
1713 test
= isl_multi_pw_aff_pullback_multi_aff(test
, add_dom
);
1715 implied
= filter_implied(scop
, stmt
, test
, satisfied
);
1719 isl_multi_pw_aff_free(test
);
1723 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1724 pma
= pet_filter_insert_pma(isl_set_get_space(stmt
->domain
),
1726 stmt
->domain
= isl_set_preimage_pw_multi_aff(stmt
->domain
, pma
);
1728 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1731 isl_multi_pw_aff_free(test
);
1734 isl_multi_pw_aff_free(test
);
1735 return pet_stmt_free(stmt
);
1738 /* Does "scop" have a skip condition of the given "type"?
1740 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
1742 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1746 return ext
->skip
[type
] != NULL
;
1749 /* Does "scop" have a skip condition of the given "type" that
1750 * is an affine expression?
1752 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
1754 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1758 if (!ext
->skip
[type
])
1760 return multi_pw_aff_is_affine(ext
->skip
[type
]);
1763 /* Does "scop" have a skip condition of the given "type" that
1764 * is not an affine expression?
1766 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
1768 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1773 if (!ext
->skip
[type
])
1775 aff
= multi_pw_aff_is_affine(ext
->skip
[type
]);
1781 /* Does "scop" have a skip condition of the given "type" that
1782 * is affine and holds on the entire domain?
1784 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
1786 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1792 is_aff
= pet_scop_has_affine_skip(scop
, type
);
1793 if (is_aff
< 0 || !is_aff
)
1796 pa
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
1797 set
= isl_pw_aff_non_zero_set(pa
);
1798 is_univ
= isl_set_plain_is_universe(set
);
1804 /* Replace scop->skip[type] by "skip".
1806 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
1807 enum pet_skip type
, __isl_take isl_multi_pw_aff
*skip
)
1809 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1814 isl_multi_pw_aff_free(ext
->skip
[type
]);
1815 ext
->skip
[type
] = skip
;
1819 isl_multi_pw_aff_free(skip
);
1820 return pet_scop_free(scop
);
1823 /* Return a copy of scop->skip[type].
1825 __isl_give isl_multi_pw_aff
*pet_scop_get_skip(struct pet_scop
*scop
,
1828 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1833 return isl_multi_pw_aff_copy(ext
->skip
[type
]);
1836 /* Assuming scop->skip[type] is an affine expression,
1837 * return the constraints on the outer loop domain for which the skip condition
1840 __isl_give isl_set
*pet_scop_get_affine_skip_domain(struct pet_scop
*scop
,
1843 isl_multi_pw_aff
*skip
;
1846 skip
= pet_scop_get_skip(scop
, type
);
1847 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
1848 isl_multi_pw_aff_free(skip
);
1849 return isl_pw_aff_non_zero_set(pa
);
1852 /* Return the identifier of the variable that is accessed by
1853 * the skip condition of the given type.
1855 * The skip condition is assumed not to be an affine condition.
1857 __isl_give isl_id
*pet_scop_get_skip_id(struct pet_scop
*scop
,
1860 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1865 return isl_multi_pw_aff_get_tuple_id(ext
->skip
[type
], isl_dim_out
);
1868 /* Return an access pet_expr corresponding to the skip condition
1869 * of the given type.
1871 __isl_give pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
1874 return pet_expr_from_index(pet_scop_get_skip(scop
, type
));
1877 /* Drop the the skip condition scop->skip[type].
1879 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
1881 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1886 isl_multi_pw_aff_free(ext
->skip
[type
]);
1887 ext
->skip
[type
] = NULL
;
1890 /* Make the skip condition (if any) depend on the value of "test" being
1891 * equal to "satisfied".
1893 * We only support the case where the original skip condition is universal,
1894 * i.e., where skipping is unconditional, and where satisfied == 1.
1895 * In this case, the skip condition is changed to skip only when
1896 * "test" is equal to one.
1898 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
1899 enum pet_skip type
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
1905 if (!pet_scop_has_skip(scop
, type
))
1909 is_univ
= pet_scop_has_universal_skip(scop
, type
);
1911 return pet_scop_free(scop
);
1912 if (satisfied
&& is_univ
) {
1913 isl_multi_pw_aff
*skip
;
1914 skip
= isl_multi_pw_aff_copy(test
);
1915 scop
= pet_scop_set_skip(scop
, type
, skip
);
1919 isl_die(isl_multi_pw_aff_get_ctx(test
), isl_error_internal
,
1920 "skip expression cannot be filtered",
1921 return pet_scop_free(scop
));
1927 /* Make all statements in "scop" depend on the value of "test"
1928 * being equal to "satisfied" by adjusting their domains.
1930 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
1931 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1935 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
1936 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
1941 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1942 scop
->stmts
[i
] = stmt_filter(scop
, scop
->stmts
[i
],
1943 isl_multi_pw_aff_copy(test
), satisfied
);
1944 if (!scop
->stmts
[i
])
1948 isl_multi_pw_aff_free(test
);
1951 isl_multi_pw_aff_free(test
);
1952 return pet_scop_free(scop
);
1955 /* Add the parameters of the access expression "expr" to "space".
1957 static int access_collect_params(__isl_keep pet_expr
*expr
, void *user
)
1960 isl_space
**space
= user
;
1962 *space
= isl_space_align_params(*space
,
1963 isl_map_get_space(expr
->acc
.access
));
1965 return *space
? 0 : -1;
1968 /* Add all parameters in "stmt" to "space" and return the result.
1970 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
1971 __isl_take isl_space
*space
)
1976 return isl_space_free(space
);
1978 space
= isl_space_align_params(space
, isl_set_get_space(stmt
->domain
));
1979 space
= isl_space_align_params(space
,
1980 isl_map_get_space(stmt
->schedule
));
1981 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1982 if (pet_expr_foreach_access_expr(stmt
->args
[i
],
1983 &access_collect_params
, &space
) < 0)
1984 space
= isl_space_free(space
);
1985 if (pet_tree_foreach_access_expr(stmt
->body
, &access_collect_params
,
1987 space
= isl_space_free(space
);
1992 /* Add all parameters in "array" to "space" and return the result.
1994 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
1995 __isl_take isl_space
*space
)
1998 return isl_space_free(space
);
2000 space
= isl_space_align_params(space
,
2001 isl_set_get_space(array
->context
));
2002 space
= isl_space_align_params(space
, isl_set_get_space(array
->extent
));
2007 /* Add all parameters in "scop" to "space" and return the result.
2009 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2010 __isl_take isl_space
*space
)
2015 return isl_space_free(space
);
2017 for (i
= 0; i
< scop
->n_array
; ++i
)
2018 space
= array_collect_params(scop
->arrays
[i
], space
);
2020 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2021 space
= stmt_collect_params(scop
->stmts
[i
], space
);
2026 /* Add all parameters in "space" to the domain, schedule and
2027 * all access relations in "stmt".
2029 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2030 __isl_take isl_space
*space
)
2037 stmt
->domain
= isl_set_align_params(stmt
->domain
,
2038 isl_space_copy(space
));
2039 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2040 isl_space_copy(space
));
2042 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2043 stmt
->args
[i
] = pet_expr_align_params(stmt
->args
[i
],
2044 isl_space_copy(space
));
2048 stmt
->body
= pet_tree_align_params(stmt
->body
, isl_space_copy(space
));
2050 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2053 isl_space_free(space
);
2056 isl_space_free(space
);
2057 return pet_stmt_free(stmt
);
2060 /* Add all parameters in "space" to "array".
2062 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2063 __isl_take isl_space
*space
)
2068 array
->context
= isl_set_align_params(array
->context
,
2069 isl_space_copy(space
));
2070 array
->extent
= isl_set_align_params(array
->extent
,
2071 isl_space_copy(space
));
2072 if (array
->value_bounds
) {
2073 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2074 isl_space_copy(space
));
2075 if (!array
->value_bounds
)
2079 if (!array
->context
|| !array
->extent
)
2082 isl_space_free(space
);
2085 isl_space_free(space
);
2086 return pet_array_free(array
);
2089 /* Add all parameters in "space" to "scop".
2091 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2092 __isl_take isl_space
*space
)
2099 for (i
= 0; i
< scop
->n_array
; ++i
) {
2100 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2101 isl_space_copy(space
));
2102 if (!scop
->arrays
[i
])
2106 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2107 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2108 isl_space_copy(space
));
2109 if (!scop
->stmts
[i
])
2113 isl_space_free(space
);
2116 isl_space_free(space
);
2117 return pet_scop_free(scop
);
2120 /* Update all isl_sets and isl_maps in "scop" such that they all
2121 * have the same parameters.
2123 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2130 space
= isl_set_get_space(scop
->context
);
2131 space
= scop_collect_params(scop
, space
);
2133 scop
->context
= isl_set_align_params(scop
->context
,
2134 isl_space_copy(space
));
2135 scop
= scop_propagate_params(scop
, space
);
2137 if (scop
&& !scop
->context
)
2138 return pet_scop_free(scop
);
2143 /* Add the access relation of the access expression "expr" to "accesses" and
2144 * return the result.
2145 * The domain of the access relation is intersected with "domain".
2146 * If "tag" is set, then the access relation is tagged with
2147 * the corresponding reference identifier.
2149 static __isl_give isl_union_map
*expr_collect_access(__isl_keep pet_expr
*expr
,
2150 int tag
, __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
2154 access
= pet_expr_access_get_may_access(expr
);
2155 access
= isl_map_intersect_domain(access
, isl_set_copy(domain
));
2157 access
= pet_expr_tag_access(expr
, access
);
2158 return isl_union_map_add_map(accesses
, access
);
2161 /* Internal data structure for expr_collect_accesses.
2163 * "read" is set if we want to collect read accesses.
2164 * "write" is set if we want to collect write accesses.
2165 * "must" is set if we only want definite accesses.
2166 * "tag" is set if the access relations should be tagged with
2167 * the corresponding reference identifiers.
2168 * "domain" are constraints on the domain of the access relations.
2169 * "accesses" collects the results.
2171 struct pet_expr_collect_accesses_data
{
2178 isl_union_map
*accesses
;
2181 /* Add the access relation of the access expression "expr"
2182 * to data->accesses if the access expression is a read and data->read is set
2183 * and/or it is a write and data->write is set.
2184 * The domains of the access relations are intersected with data->domain.
2185 * If data->tag is set, then the access relations are tagged with
2186 * the corresponding reference identifiers.
2188 * If data->must is set, then we only add the accesses that are definitely
2189 * performed. Otherwise, we add all potential accesses.
2190 * In particular, if the access has any arguments, then if data->must is
2191 * set we currently skip the access completely. If data->must is not set,
2192 * we project out the values of the access arguments.
2194 static int expr_collect_accesses(__isl_keep pet_expr
*expr
, void *user
)
2196 struct pet_expr_collect_accesses_data
*data
= user
;
2204 if (pet_expr_is_affine(expr
))
2206 if (data
->must
&& expr
->n_arg
!= 0)
2209 if ((data
->read
&& expr
->acc
.read
) || (data
->write
&& expr
->acc
.write
))
2210 data
->accesses
= expr_collect_access(expr
, data
->tag
,
2211 data
->accesses
, data
->domain
);
2213 return data
->accesses
? 0 : -1;
2216 /* Collect and return all read access relations (if "read" is set)
2217 * and/or all write access relations (if "write" is set) in "stmt".
2218 * If "tag" is set, then the access relations are tagged with
2219 * the corresponding reference identifiers.
2220 * If "kill" is set, then "stmt" is a kill statement and we simply
2221 * add the argument of the kill operation.
2223 * If "must" is set, then we only add the accesses that are definitely
2224 * performed. Otherwise, we add all potential accesses.
2225 * In particular, if the statement has any arguments, then if "must" is
2226 * set we currently skip the statement completely. If "must" is not set,
2227 * we project out the values of the statement arguments.
2228 * If the statement body is not an expression tree, then we cannot
2229 * know for sure if/when the accesses inside the tree are performed.
2230 * We therefore ignore such statements when "must" is set.
2232 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2233 int read
, int write
, int kill
, int must
, int tag
,
2234 __isl_take isl_space
*dim
)
2236 struct pet_expr_collect_accesses_data data
= { read
, write
, must
, tag
};
2241 data
.accesses
= isl_union_map_empty(dim
);
2243 if (must
&& stmt
->n_arg
> 0)
2244 return data
.accesses
;
2245 if (must
&& pet_tree_get_type(stmt
->body
) != pet_tree_expr
)
2246 return data
.accesses
;
2248 data
.domain
= isl_set_copy(stmt
->domain
);
2249 if (isl_set_is_wrapping(data
.domain
))
2250 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
2253 pet_expr
*body
, *arg
;
2255 body
= pet_tree_expr_get_expr(stmt
->body
);
2256 arg
= pet_expr_get_arg(body
, 0);
2257 data
.accesses
= expr_collect_access(arg
, tag
,
2258 data
.accesses
, data
.domain
);
2260 pet_expr_free(body
);
2261 } else if (pet_tree_foreach_access_expr(stmt
->body
,
2262 &expr_collect_accesses
, &data
) < 0)
2263 data
.accesses
= isl_union_map_free(data
.accesses
);
2265 isl_set_free(data
.domain
);
2267 return data
.accesses
;
2270 /* Is "stmt" an assignment statement?
2272 int pet_stmt_is_assign(struct pet_stmt
*stmt
)
2276 return pet_tree_is_assign(stmt
->body
);
2279 /* Is "stmt" a kill statement?
2281 int pet_stmt_is_kill(struct pet_stmt
*stmt
)
2285 return pet_tree_is_kill(stmt
->body
);
2288 /* Is "stmt" an assume statement?
2290 int pet_stmt_is_assume(struct pet_stmt
*stmt
)
2294 return pet_tree_is_assume(stmt
->body
);
2297 /* Compute a mapping from all arrays (of structs) in scop
2298 * to their innermost arrays.
2300 * In particular, for each array of a primitive type, the result
2301 * contains the identity mapping on that array.
2302 * For each array involving member accesses, the result
2303 * contains a mapping from the elements of any intermediate array of structs
2304 * to all corresponding elements of the innermost nested arrays.
2306 static __isl_give isl_union_map
*compute_to_inner(struct pet_scop
*scop
)
2309 isl_union_map
*to_inner
;
2311 to_inner
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2313 for (i
= 0; i
< scop
->n_array
; ++i
) {
2314 struct pet_array
*array
= scop
->arrays
[i
];
2316 isl_map
*map
, *gist
;
2318 if (array
->element_is_record
)
2321 map
= isl_set_identity(isl_set_copy(array
->extent
));
2323 set
= isl_map_domain(isl_map_copy(map
));
2324 gist
= isl_map_copy(map
);
2325 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2326 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2328 while (set
&& isl_set_is_wrapping(set
)) {
2332 id
= isl_set_get_tuple_id(set
);
2333 wrapped
= isl_set_unwrap(set
);
2334 wrapped
= isl_map_domain_map(wrapped
);
2335 wrapped
= isl_map_set_tuple_id(wrapped
, isl_dim_in
, id
);
2336 map
= isl_map_apply_domain(map
, wrapped
);
2337 set
= isl_map_domain(isl_map_copy(map
));
2338 gist
= isl_map_copy(map
);
2339 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2340 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2350 /* Collect and return all read access relations (if "read" is set)
2351 * and/or all write access relations (if "write" is set) in "scop".
2352 * If "kill" is set, then we only add the arguments of kill operations.
2353 * If "must" is set, then we only add the accesses that are definitely
2354 * performed. Otherwise, we add all potential accesses.
2355 * If "tag" is set, then the access relations are tagged with
2356 * the corresponding reference identifiers.
2357 * For accesses to structures, the returned access relation accesses
2358 * all individual fields in the structures.
2360 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2361 int read
, int write
, int kill
, int must
, int tag
)
2364 isl_union_map
*accesses
;
2365 isl_union_set
*arrays
;
2366 isl_union_map
*to_inner
;
2371 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2373 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2374 struct pet_stmt
*stmt
= scop
->stmts
[i
];
2375 isl_union_map
*accesses_i
;
2378 if (kill
&& !pet_stmt_is_kill(stmt
))
2381 space
= isl_set_get_space(scop
->context
);
2382 accesses_i
= stmt_collect_accesses(stmt
, read
, write
, kill
,
2384 accesses
= isl_union_map_union(accesses
, accesses_i
);
2387 arrays
= isl_union_set_empty(isl_union_map_get_space(accesses
));
2388 for (i
= 0; i
< scop
->n_array
; ++i
) {
2389 isl_set
*extent
= isl_set_copy(scop
->arrays
[i
]->extent
);
2390 arrays
= isl_union_set_add_set(arrays
, extent
);
2392 accesses
= isl_union_map_intersect_range(accesses
, arrays
);
2394 to_inner
= compute_to_inner(scop
);
2395 accesses
= isl_union_map_apply_range(accesses
, to_inner
);
2400 /* Collect all potential read access relations.
2402 __isl_give isl_union_map
*pet_scop_collect_may_reads(struct pet_scop
*scop
)
2404 return scop_collect_accesses(scop
, 1, 0, 0, 0, 0);
2407 /* Collect all potential write access relations.
2409 __isl_give isl_union_map
*pet_scop_collect_may_writes(struct pet_scop
*scop
)
2411 return scop_collect_accesses(scop
, 0, 1, 0, 0, 0);
2414 /* Collect all definite write access relations.
2416 __isl_give isl_union_map
*pet_scop_collect_must_writes(struct pet_scop
*scop
)
2418 return scop_collect_accesses(scop
, 0, 1, 0, 1, 0);
2421 /* Collect all definite kill access relations.
2423 __isl_give isl_union_map
*pet_scop_collect_must_kills(struct pet_scop
*scop
)
2425 return scop_collect_accesses(scop
, 0, 0, 1, 1, 0);
2428 /* Collect all tagged potential read access relations.
2430 __isl_give isl_union_map
*pet_scop_collect_tagged_may_reads(
2431 struct pet_scop
*scop
)
2433 return scop_collect_accesses(scop
, 1, 0, 0, 0, 1);
2436 /* Collect all tagged potential write access relations.
2438 __isl_give isl_union_map
*pet_scop_collect_tagged_may_writes(
2439 struct pet_scop
*scop
)
2441 return scop_collect_accesses(scop
, 0, 1, 0, 0, 1);
2444 /* Collect all tagged definite write access relations.
2446 __isl_give isl_union_map
*pet_scop_collect_tagged_must_writes(
2447 struct pet_scop
*scop
)
2449 return scop_collect_accesses(scop
, 0, 1, 0, 1, 1);
2452 /* Collect all tagged definite kill access relations.
2454 __isl_give isl_union_map
*pet_scop_collect_tagged_must_kills(
2455 struct pet_scop
*scop
)
2457 return scop_collect_accesses(scop
, 0, 0, 1, 1, 1);
2460 /* Collect and return the union of iteration domains in "scop".
2462 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2466 isl_union_set
*domain
;
2471 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2473 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2474 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2475 domain
= isl_union_set_add_set(domain
, domain_i
);
2481 /* Collect and return the schedules of the statements in "scop".
2482 * The range is normalized to the maximal number of scheduling
2485 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2488 isl_map
*schedule_i
;
2489 isl_union_map
*schedule
;
2490 int depth
, max_depth
= 0;
2495 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2497 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2498 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2499 if (depth
> max_depth
)
2503 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2504 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2505 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2506 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2508 for (j
= depth
; j
< max_depth
; ++j
)
2509 schedule_i
= isl_map_fix_si(schedule_i
,
2511 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2517 /* Add a reference identifier to all access expressions in "stmt".
2518 * "n_ref" points to an integer that contains the sequence number
2519 * of the next reference.
2521 static struct pet_stmt
*stmt_add_ref_ids(struct pet_stmt
*stmt
, int *n_ref
)
2528 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2529 stmt
->args
[i
] = pet_expr_add_ref_ids(stmt
->args
[i
], n_ref
);
2531 return pet_stmt_free(stmt
);
2534 stmt
->body
= pet_tree_add_ref_ids(stmt
->body
, n_ref
);
2536 return pet_stmt_free(stmt
);
2541 /* Add a reference identifier to all access expressions in "scop".
2543 struct pet_scop
*pet_scop_add_ref_ids(struct pet_scop
*scop
)
2552 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2553 scop
->stmts
[i
] = stmt_add_ref_ids(scop
->stmts
[i
], &n_ref
);
2554 if (!scop
->stmts
[i
])
2555 return pet_scop_free(scop
);
2561 /* Reset the user pointer on all parameter ids in "array".
2563 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2568 array
->context
= isl_set_reset_user(array
->context
);
2569 array
->extent
= isl_set_reset_user(array
->extent
);
2570 if (!array
->context
|| !array
->extent
)
2571 return pet_array_free(array
);
2576 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2578 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2587 stmt
->domain
= isl_set_reset_user(stmt
->domain
);
2588 stmt
->schedule
= isl_map_reset_user(stmt
->schedule
);
2589 if (!stmt
->domain
|| !stmt
->schedule
)
2590 return pet_stmt_free(stmt
);
2592 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2593 stmt
->args
[i
] = pet_expr_anonymize(stmt
->args
[i
]);
2595 return pet_stmt_free(stmt
);
2598 stmt
->body
= pet_tree_anonymize(stmt
->body
);
2600 return pet_stmt_free(stmt
);
2605 /* Reset the user pointer on the tuple ids and all parameter ids
2608 static struct pet_implication
*implication_anonymize(
2609 struct pet_implication
*implication
)
2614 implication
->extension
= isl_map_reset_user(implication
->extension
);
2615 if (!implication
->extension
)
2616 return pet_implication_free(implication
);
2621 /* Reset the user pointer on all parameter and tuple ids in "scop".
2623 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2630 scop
->context
= isl_set_reset_user(scop
->context
);
2631 scop
->context_value
= isl_set_reset_user(scop
->context_value
);
2632 if (!scop
->context
|| !scop
->context_value
)
2633 return pet_scop_free(scop
);
2635 for (i
= 0; i
< scop
->n_array
; ++i
) {
2636 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2637 if (!scop
->arrays
[i
])
2638 return pet_scop_free(scop
);
2641 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2642 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2643 if (!scop
->stmts
[i
])
2644 return pet_scop_free(scop
);
2647 for (i
= 0; i
< scop
->n_implication
; ++i
) {
2648 scop
->implications
[i
] =
2649 implication_anonymize(scop
->implications
[i
]);
2650 if (!scop
->implications
[i
])
2651 return pet_scop_free(scop
);
2657 /* Compute the gist of the iteration domain and all access relations
2658 * of "stmt" based on the constraints on the parameters specified by "context"
2659 * and the constraints on the values of nested accesses specified
2660 * by "value_bounds".
2662 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2663 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2671 domain
= isl_set_copy(stmt
->domain
);
2672 if (stmt
->n_arg
> 0)
2673 domain
= isl_map_domain(isl_set_unwrap(domain
));
2675 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2677 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2678 stmt
->args
[i
] = pet_expr_gist(stmt
->args
[i
],
2679 domain
, value_bounds
);
2684 stmt
->body
= pet_tree_gist(stmt
->body
, domain
, value_bounds
);
2688 isl_set_free(domain
);
2690 domain
= isl_set_universe(pet_stmt_get_space(stmt
));
2691 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2692 if (stmt
->n_arg
> 0)
2693 domain
= pet_value_bounds_apply(domain
, stmt
->n_arg
, stmt
->args
,
2695 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
2697 return pet_stmt_free(stmt
);
2701 isl_set_free(domain
);
2702 return pet_stmt_free(stmt
);
2705 /* Compute the gist of the extent of the array
2706 * based on the constraints on the parameters specified by "context".
2708 static struct pet_array
*array_gist(struct pet_array
*array
,
2709 __isl_keep isl_set
*context
)
2714 array
->extent
= isl_set_gist_params(array
->extent
,
2715 isl_set_copy(context
));
2717 return pet_array_free(array
);
2722 /* Compute the gist of all sets and relations in "scop"
2723 * based on the constraints on the parameters specified by "scop->context"
2724 * and the constraints on the values of nested accesses specified
2725 * by "value_bounds".
2727 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
2728 __isl_keep isl_union_map
*value_bounds
)
2735 scop
->context
= isl_set_coalesce(scop
->context
);
2737 return pet_scop_free(scop
);
2739 for (i
= 0; i
< scop
->n_array
; ++i
) {
2740 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
2741 if (!scop
->arrays
[i
])
2742 return pet_scop_free(scop
);
2745 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2746 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
2748 if (!scop
->stmts
[i
])
2749 return pet_scop_free(scop
);
2755 /* Intersect the context of "scop" with "context".
2756 * To ensure that we don't introduce any unnamed parameters in
2757 * the context of "scop", we first remove the unnamed parameters
2760 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
2761 __isl_take isl_set
*context
)
2766 context
= pet_nested_remove_from_set(context
);
2767 scop
->context
= isl_set_intersect(scop
->context
, context
);
2769 return pet_scop_free(scop
);
2773 isl_set_free(context
);
2774 return pet_scop_free(scop
);
2777 /* Drop the current context of "scop". That is, replace the context
2778 * by a universal set.
2780 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
2787 space
= isl_set_get_space(scop
->context
);
2788 isl_set_free(scop
->context
);
2789 scop
->context
= isl_set_universe(space
);
2791 return pet_scop_free(scop
);
2796 /* Append "array" to the arrays of "scop".
2798 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
2799 struct pet_array
*array
)
2802 struct pet_array
**arrays
;
2804 if (!array
|| !scop
)
2807 ctx
= isl_set_get_ctx(scop
->context
);
2808 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
2812 scop
->arrays
= arrays
;
2813 scop
->arrays
[scop
->n_array
] = array
;
2818 pet_array_free(array
);
2819 return pet_scop_free(scop
);
2822 /* Create an index expression for an access to a virtual array
2823 * representing the result of a condition.
2824 * Unlike other accessed data, the id of the array is NULL as
2825 * there is no ValueDecl in the program corresponding to the virtual
2827 * The index expression is created as an identity mapping on "space".
2828 * That is, the dimension of the array is the same as that of "space".
2830 __isl_give isl_multi_pw_aff
*pet_create_test_index(__isl_take isl_space
*space
,
2836 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
2837 id
= isl_id_alloc(isl_space_get_ctx(space
), name
, NULL
);
2838 space
= isl_space_map_from_set(space
);
2839 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
2840 return isl_multi_pw_aff_identity(space
);
2843 /* Add an array with the given extent to the list
2844 * of arrays in "scop" and return the extended pet_scop.
2845 * Specifically, the extent is determined by the image of "domain"
2847 * "int_size" is the number of bytes needed to represent values of type "int".
2848 * The array is marked as attaining values 0 and 1 only and
2849 * as each element being assigned at most once.
2851 struct pet_scop
*pet_scop_add_boolean_array(struct pet_scop
*scop
,
2852 __isl_take isl_set
*domain
, __isl_take isl_multi_pw_aff
*index
,
2857 struct pet_array
*array
;
2860 if (!scop
|| !domain
|| !index
)
2863 ctx
= isl_multi_pw_aff_get_ctx(index
);
2864 array
= isl_calloc_type(ctx
, struct pet_array
);
2868 access
= isl_map_from_multi_pw_aff(index
);
2869 access
= isl_map_intersect_domain(access
, domain
);
2870 array
->extent
= isl_map_range(access
);
2871 space
= isl_space_params_alloc(ctx
, 0);
2872 array
->context
= isl_set_universe(space
);
2873 space
= isl_space_set_alloc(ctx
, 0, 1);
2874 array
->value_bounds
= isl_set_universe(space
);
2875 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
2877 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
2879 array
->element_type
= strdup("int");
2880 array
->element_size
= int_size
;
2881 array
->uniquely_defined
= 1;
2883 if (!array
->extent
|| !array
->context
)
2884 array
= pet_array_free(array
);
2886 scop
= pet_scop_add_array(scop
, array
);
2890 isl_set_free(domain
);
2891 isl_multi_pw_aff_free(index
);
2892 return pet_scop_free(scop
);
2895 /* Create and return an implication on filter values equal to "satisfied"
2896 * with extension "map".
2898 static struct pet_implication
*new_implication(__isl_take isl_map
*map
,
2902 struct pet_implication
*implication
;
2906 ctx
= isl_map_get_ctx(map
);
2907 implication
= isl_alloc_type(ctx
, struct pet_implication
);
2911 implication
->extension
= map
;
2912 implication
->satisfied
= satisfied
;
2920 /* Add an implication on filter values equal to "satisfied"
2921 * with extension "map" to "scop".
2923 struct pet_scop
*pet_scop_add_implication(struct pet_scop
*scop
,
2924 __isl_take isl_map
*map
, int satisfied
)
2927 struct pet_implication
*implication
;
2928 struct pet_implication
**implications
;
2930 implication
= new_implication(map
, satisfied
);
2931 if (!scop
|| !implication
)
2934 ctx
= isl_set_get_ctx(scop
->context
);
2935 implications
= isl_realloc_array(ctx
, scop
->implications
,
2936 struct pet_implication
*,
2937 scop
->n_implication
+ 1);
2940 scop
->implications
= implications
;
2941 scop
->implications
[scop
->n_implication
] = implication
;
2942 scop
->n_implication
++;
2946 pet_implication_free(implication
);
2947 return pet_scop_free(scop
);
2950 /* Given an access expression, check if it is data dependent.
2951 * If so, set *found and abort the search.
2953 static int is_data_dependent(__isl_keep pet_expr
*expr
, void *user
)
2957 if (pet_expr_get_n_arg(expr
) > 0) {
2965 /* Does "scop" contain any data dependent accesses?
2967 * Check the body of each statement for such accesses.
2969 int pet_scop_has_data_dependent_accesses(struct pet_scop
*scop
)
2977 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2978 int r
= pet_tree_foreach_access_expr(scop
->stmts
[i
]->body
,
2979 &is_data_dependent
, &found
);
2980 if (r
< 0 && !found
)
2989 /* Does "scop" contain and data dependent conditions?
2991 int pet_scop_has_data_dependent_conditions(struct pet_scop
*scop
)
2998 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2999 if (scop
->stmts
[i
]->n_arg
> 0)
3005 /* Keep track of the "input" file inside the (extended) "scop".
3007 struct pet_scop
*pet_scop_set_input_file(struct pet_scop
*scop
, FILE *input
)
3009 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3019 /* Print the original code corresponding to "scop" to printer "p".
3021 * pet_scop_print_original can only be called from
3022 * a pet_transform_C_source callback. This means that the input
3023 * file is stored in the extended scop and that the printer prints
3026 __isl_give isl_printer
*pet_scop_print_original(struct pet_scop
*scop
,
3027 __isl_take isl_printer
*p
)
3029 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3031 unsigned start
, end
;
3034 return isl_printer_free(p
);
3037 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
3038 "no input file stored in scop",
3039 return isl_printer_free(p
));
3041 output
= isl_printer_get_file(p
);
3043 return isl_printer_free(p
);
3045 start
= pet_loc_get_start(scop
->loc
);
3046 end
= pet_loc_get_end(scop
->loc
);
3047 if (copy(ext
->input
, output
, start
, end
) < 0)
3048 return isl_printer_free(p
);