2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012 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
35 #include <isl/constraint.h>
36 #include <isl/union_set.h>
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
42 static char *type_str
[] = {
43 [pet_expr_access
] = "access",
44 [pet_expr_call
] = "call",
45 [pet_expr_cast
] = "cast",
46 [pet_expr_double
] = "double",
47 [pet_expr_unary
] = "unary",
48 [pet_expr_binary
] = "binary",
49 [pet_expr_ternary
] = "ternary"
52 static char *op_str
[] = {
53 [pet_op_add_assign
] = "+=",
54 [pet_op_sub_assign
] = "-=",
55 [pet_op_mul_assign
] = "*=",
56 [pet_op_div_assign
] = "/=",
57 [pet_op_assign
] = "=",
68 [pet_op_post_inc
] = "++",
69 [pet_op_post_dec
] = "--",
70 [pet_op_pre_inc
] = "++",
71 [pet_op_pre_dec
] = "--",
72 [pet_op_address_of
] = "&",
73 [pet_op_kill
] = "kill"
76 /* pet_scop with extra information that is only used during parsing.
78 * In particular, we keep track of conditions under which we want
79 * to skip the rest of the current loop iteration (skip[pet_skip_now])
80 * and of conditions under which we want to skip subsequent
81 * loop iterations (skip[pet_skip_later]).
83 * The conditions are represented either by a variable, which
84 * is assumed to attain values zero and one, or by a boolean affine
85 * expression. The condition holds if the variable has value one
86 * or if the affine expression has value one (typically for only
87 * part of the parameter space).
89 * A missing condition (skip[type] == NULL) means that we don't want
98 const char *pet_op_str(enum pet_op_type op
)
103 int pet_op_is_inc_dec(enum pet_op_type op
)
105 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
106 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
109 const char *pet_type_str(enum pet_expr_type type
)
111 return type_str
[type
];
114 enum pet_op_type
pet_str_op(const char *str
)
118 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
119 if (!strcmp(op_str
[i
], str
))
125 enum pet_expr_type
pet_str_type(const char *str
)
129 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
130 if (!strcmp(type_str
[i
], str
))
136 /* Construct a pet_expr from an access relation.
137 * By default, it is considered to be a read access.
139 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
141 isl_ctx
*ctx
= isl_map_get_ctx(access
);
142 struct pet_expr
*expr
;
146 expr
= isl_calloc_type(ctx
, struct pet_expr
);
150 expr
->type
= pet_expr_access
;
151 expr
->acc
.access
= access
;
157 isl_map_free(access
);
161 /* Construct a pet_expr that kills the elements specified by "access".
163 struct pet_expr
*pet_expr_kill_from_access(__isl_take isl_map
*access
)
166 struct pet_expr
*expr
;
168 ctx
= isl_map_get_ctx(access
);
169 expr
= pet_expr_from_access(access
);
173 return pet_expr_new_unary(ctx
, pet_op_kill
, expr
);
176 /* Construct a unary pet_expr that performs "op" on "arg".
178 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
179 struct pet_expr
*arg
)
181 struct pet_expr
*expr
;
185 expr
= isl_alloc_type(ctx
, struct pet_expr
);
189 expr
->type
= pet_expr_unary
;
192 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
195 expr
->args
[pet_un_arg
] = arg
;
203 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
205 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
206 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
208 struct pet_expr
*expr
;
212 expr
= isl_alloc_type(ctx
, struct pet_expr
);
216 expr
->type
= pet_expr_binary
;
219 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
222 expr
->args
[pet_bin_lhs
] = lhs
;
223 expr
->args
[pet_bin_rhs
] = rhs
;
232 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
234 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
235 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
237 struct pet_expr
*expr
;
239 if (!cond
|| !lhs
|| !rhs
)
241 expr
= isl_alloc_type(ctx
, struct pet_expr
);
245 expr
->type
= pet_expr_ternary
;
247 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
250 expr
->args
[pet_ter_cond
] = cond
;
251 expr
->args
[pet_ter_true
] = lhs
;
252 expr
->args
[pet_ter_false
] = rhs
;
262 /* Construct a call pet_expr that calls function "name" with "n_arg"
263 * arguments. The caller is responsible for filling in the arguments.
265 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
268 struct pet_expr
*expr
;
270 expr
= isl_alloc_type(ctx
, struct pet_expr
);
274 expr
->type
= pet_expr_call
;
276 expr
->name
= strdup(name
);
277 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
278 if (!expr
->name
|| !expr
->args
)
279 return pet_expr_free(expr
);
284 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
286 struct pet_expr
*pet_expr_new_cast(isl_ctx
*ctx
, const char *type_name
,
287 struct pet_expr
*arg
)
289 struct pet_expr
*expr
;
294 expr
= isl_alloc_type(ctx
, struct pet_expr
);
298 expr
->type
= pet_expr_cast
;
300 expr
->type_name
= strdup(type_name
);
301 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
302 if (!expr
->type_name
|| !expr
->args
)
314 /* Construct a pet_expr that represents the double "d".
316 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double val
, const char *s
)
318 struct pet_expr
*expr
;
320 expr
= isl_calloc_type(ctx
, struct pet_expr
);
324 expr
->type
= pet_expr_double
;
326 expr
->d
.s
= strdup(s
);
328 return pet_expr_free(expr
);
333 void *pet_expr_free(struct pet_expr
*expr
)
340 for (i
= 0; i
< expr
->n_arg
; ++i
)
341 pet_expr_free(expr
->args
[i
]);
344 switch (expr
->type
) {
345 case pet_expr_access
:
346 isl_map_free(expr
->acc
.access
);
352 free(expr
->type_name
);
354 case pet_expr_double
:
358 case pet_expr_binary
:
359 case pet_expr_ternary
:
367 static void expr_dump(struct pet_expr
*expr
, int indent
)
374 fprintf(stderr
, "%*s", indent
, "");
376 switch (expr
->type
) {
377 case pet_expr_double
:
378 fprintf(stderr
, "%s\n", expr
->d
.s
);
380 case pet_expr_access
:
381 isl_map_dump(expr
->acc
.access
);
382 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
384 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
385 "", expr
->acc
.write
);
386 for (i
= 0; i
< expr
->n_arg
; ++i
)
387 expr_dump(expr
->args
[i
], indent
+ 2);
390 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
391 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
393 case pet_expr_binary
:
394 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
395 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
396 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
398 case pet_expr_ternary
:
399 fprintf(stderr
, "?:\n");
400 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
401 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
402 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
405 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
406 for (i
= 0; i
< expr
->n_arg
; ++i
)
407 expr_dump(expr
->args
[i
], indent
+ 2);
410 fprintf(stderr
, "(%s)\n", expr
->type_name
);
411 for (i
= 0; i
< expr
->n_arg
; ++i
)
412 expr_dump(expr
->args
[i
], indent
+ 2);
417 void pet_expr_dump(struct pet_expr
*expr
)
422 /* Does "expr" represent an access to an unnamed space, i.e.,
423 * does it represent an affine expression?
425 int pet_expr_is_affine(struct pet_expr
*expr
)
431 if (expr
->type
!= pet_expr_access
)
434 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
441 /* Return 1 if the two pet_exprs are equivalent.
443 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
447 if (!expr1
|| !expr2
)
450 if (expr1
->type
!= expr2
->type
)
452 if (expr1
->n_arg
!= expr2
->n_arg
)
454 for (i
= 0; i
< expr1
->n_arg
; ++i
)
455 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
457 switch (expr1
->type
) {
458 case pet_expr_double
:
459 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
461 if (expr1
->d
.val
!= expr2
->d
.val
)
464 case pet_expr_access
:
465 if (expr1
->acc
.read
!= expr2
->acc
.read
)
467 if (expr1
->acc
.write
!= expr2
->acc
.write
)
469 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
471 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
475 case pet_expr_binary
:
476 case pet_expr_ternary
:
477 if (expr1
->op
!= expr2
->op
)
481 if (strcmp(expr1
->name
, expr2
->name
))
485 if (strcmp(expr1
->type_name
, expr2
->type_name
))
493 /* Add extra conditions on the parameters to all access relations in "expr".
495 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
496 __isl_take isl_set
*cond
)
503 for (i
= 0; i
< expr
->n_arg
; ++i
) {
504 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
510 if (expr
->type
== pet_expr_access
) {
511 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
513 if (!expr
->acc
.access
)
521 return pet_expr_free(expr
);
524 /* Modify all access relations in "expr" by calling "fn" on them.
526 struct pet_expr
*pet_expr_foreach_access(struct pet_expr
*expr
,
527 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
535 for (i
= 0; i
< expr
->n_arg
; ++i
) {
536 expr
->args
[i
] = pet_expr_foreach_access(expr
->args
[i
], fn
, user
);
538 return pet_expr_free(expr
);
541 if (expr
->type
== pet_expr_access
) {
542 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
543 if (!expr
->acc
.access
)
544 return pet_expr_free(expr
);
550 /* Modify all expressions of type pet_expr_access in "expr"
551 * by calling "fn" on them.
553 struct pet_expr
*pet_expr_foreach_access_expr(struct pet_expr
*expr
,
554 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
562 for (i
= 0; i
< expr
->n_arg
; ++i
) {
563 expr
->args
[i
] = pet_expr_foreach_access_expr(expr
->args
[i
],
566 return pet_expr_free(expr
);
569 if (expr
->type
== pet_expr_access
)
570 expr
= fn(expr
, user
);
575 /* Modify the given access relation based on the given iteration space
577 * If the access has any arguments then the domain of the access relation
578 * is a wrapped mapping from the iteration space to the space of
579 * argument values. We only need to change the domain of this wrapped
580 * mapping, so we extend the input transformation with an identity mapping
581 * on the space of argument values.
583 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
586 isl_map
*update
= user
;
589 update
= isl_map_copy(update
);
591 dim
= isl_map_get_space(access
);
592 dim
= isl_space_domain(dim
);
593 if (!isl_space_is_wrapping(dim
))
597 dim
= isl_space_unwrap(dim
);
598 dim
= isl_space_range(dim
);
599 dim
= isl_space_map_from_set(dim
);
600 id
= isl_map_identity(dim
);
601 update
= isl_map_product(update
, id
);
604 return isl_map_apply_domain(access
, update
);
607 /* Modify all access relations in "expr" based on the given iteration space
610 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
611 __isl_take isl_map
*update
)
613 expr
= pet_expr_foreach_access(expr
, &update_domain
, update
);
614 isl_map_free(update
);
618 /* Construct a pet_stmt with given line number and statement
619 * number from a pet_expr.
620 * The initial iteration domain is the zero-dimensional universe.
621 * The name of the domain is given by "label" if it is non-NULL.
622 * Otherwise, the name is constructed as S_<id>.
623 * The domains of all access relations are modified to refer
624 * to the statement iteration domain.
626 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
627 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
629 struct pet_stmt
*stmt
;
639 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
643 dim
= isl_space_set_alloc(ctx
, 0, 0);
645 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
647 snprintf(name
, sizeof(name
), "S_%d", id
);
648 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
650 dom
= isl_set_universe(isl_space_copy(dim
));
651 sched
= isl_map_from_domain(isl_set_copy(dom
));
653 dim
= isl_space_from_range(dim
);
654 add_name
= isl_map_universe(dim
);
655 expr
= expr_update_domain(expr
, add_name
);
659 stmt
->schedule
= sched
;
662 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
663 return pet_stmt_free(stmt
);
668 return pet_expr_free(expr
);
671 void *pet_stmt_free(struct pet_stmt
*stmt
)
678 isl_set_free(stmt
->domain
);
679 isl_map_free(stmt
->schedule
);
680 pet_expr_free(stmt
->body
);
682 for (i
= 0; i
< stmt
->n_arg
; ++i
)
683 pet_expr_free(stmt
->args
[i
]);
690 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
697 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
698 fprintf(stderr
, "%*s", indent
, "");
699 isl_set_dump(stmt
->domain
);
700 fprintf(stderr
, "%*s", indent
, "");
701 isl_map_dump(stmt
->schedule
);
702 expr_dump(stmt
->body
, indent
);
703 for (i
= 0; i
< stmt
->n_arg
; ++i
)
704 expr_dump(stmt
->args
[i
], indent
+ 2);
707 void pet_stmt_dump(struct pet_stmt
*stmt
)
712 struct pet_array
*pet_array_free(struct pet_array
*array
)
717 isl_set_free(array
->context
);
718 isl_set_free(array
->extent
);
719 isl_set_free(array
->value_bounds
);
720 free(array
->element_type
);
726 void pet_array_dump(struct pet_array
*array
)
731 isl_set_dump(array
->context
);
732 isl_set_dump(array
->extent
);
733 isl_set_dump(array
->value_bounds
);
734 fprintf(stderr
, "%s %s\n", array
->element_type
,
735 array
->live_out
? "live-out" : "");
738 /* Alloc a pet_scop structure, with extra room for information that
739 * is only used during parsing.
741 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
743 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
746 /* Construct a pet_scop with room for n statements.
748 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
751 struct pet_scop
*scop
;
753 scop
= pet_scop_alloc(ctx
);
757 space
= isl_space_params_alloc(ctx
, 0);
758 scop
->context
= isl_set_universe(isl_space_copy(space
));
759 scop
->context_value
= isl_set_universe(space
);
760 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
761 if (!scop
->context
|| !scop
->stmts
)
762 return pet_scop_free(scop
);
769 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
771 return scop_alloc(ctx
, 0);
774 /* Update "context" with respect to the valid parameter values for "access".
776 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
777 __isl_take isl_set
*context
)
779 context
= isl_set_intersect(context
,
780 isl_map_params(isl_map_copy(access
)));
784 /* Update "context" with respect to the valid parameter values for "expr".
786 * If "expr" represents a ternary operator, then a parameter value
787 * needs to be valid for the condition and for at least one of the
788 * remaining two arguments.
789 * If the condition is an affine expression, then we can be a bit more specific.
790 * The parameter then has to be valid for the second argument for
791 * non-zero accesses and valid for the third argument for zero accesses.
793 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
794 __isl_take isl_set
*context
)
798 if (expr
->type
== pet_expr_ternary
) {
800 isl_set
*context1
, *context2
;
802 is_aff
= pet_expr_is_affine(expr
->args
[0]);
806 context
= expr_extract_context(expr
->args
[0], context
);
807 context1
= expr_extract_context(expr
->args
[1],
808 isl_set_copy(context
));
809 context2
= expr_extract_context(expr
->args
[2], context
);
815 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
816 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
817 zero_set
= isl_map_params(access
);
818 context1
= isl_set_subtract(context1
,
819 isl_set_copy(zero_set
));
820 context2
= isl_set_intersect(context2
, zero_set
);
823 context
= isl_set_union(context1
, context2
);
824 context
= isl_set_coalesce(context
);
829 for (i
= 0; i
< expr
->n_arg
; ++i
)
830 context
= expr_extract_context(expr
->args
[i
], context
);
832 if (expr
->type
== pet_expr_access
)
833 context
= access_extract_context(expr
->acc
.access
, context
);
837 isl_set_free(context
);
841 /* Update "context" with respect to the valid parameter values for "stmt".
843 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
844 __isl_take isl_set
*context
)
848 for (i
= 0; i
< stmt
->n_arg
; ++i
)
849 context
= expr_extract_context(stmt
->args
[i
], context
);
851 context
= expr_extract_context(stmt
->body
, context
);
856 /* Construct a pet_scop that contains the given pet_stmt.
858 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
860 struct pet_scop
*scop
;
865 scop
= scop_alloc(ctx
, 1);
867 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
871 scop
->stmts
[0] = stmt
;
880 /* Does "set" represent an element of an unnamed space, i.e.,
881 * does it represent an affine expression?
883 static int set_is_affine(__isl_keep isl_set
*set
)
887 has_id
= isl_set_has_tuple_id(set
);
894 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
895 * ext may be equal to either ext1 or ext2.
897 * The two skips that need to be combined are assumed to be affine expressions.
899 * We need to skip in ext if we need to skip in either ext1 or ext2.
900 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
902 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
903 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
906 isl_set
*set
, *skip1
, *skip2
;
910 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
912 if (!ext1
->skip
[type
]) {
915 ext
->skip
[type
] = ext2
->skip
[type
];
916 ext2
->skip
[type
] = NULL
;
919 if (!ext2
->skip
[type
]) {
922 ext
->skip
[type
] = ext1
->skip
[type
];
923 ext1
->skip
[type
] = NULL
;
927 if (!set_is_affine(ext1
->skip
[type
]) ||
928 !set_is_affine(ext2
->skip
[type
]))
929 isl_die(isl_set_get_ctx(ext1
->skip
[type
]), isl_error_internal
,
930 "can only combine affine skips",
931 return pet_scop_free(&ext
->scop
));
933 skip1
= isl_set_copy(ext1
->skip
[type
]);
934 skip2
= isl_set_copy(ext2
->skip
[type
]);
935 set
= isl_set_intersect(
936 isl_set_fix_si(isl_set_copy(skip1
), isl_dim_set
, 0, 0),
937 isl_set_fix_si(isl_set_copy(skip2
), isl_dim_set
, 0, 0));
938 set
= isl_set_union(set
, isl_set_fix_si(skip1
, isl_dim_set
, 0, 1));
939 set
= isl_set_union(set
, isl_set_fix_si(skip2
, isl_dim_set
, 0, 1));
940 set
= isl_set_coalesce(set
);
941 isl_set_free(ext1
->skip
[type
]);
942 ext1
->skip
[type
] = NULL
;
943 isl_set_free(ext2
->skip
[type
]);
944 ext2
->skip
[type
] = NULL
;
945 ext
->skip
[type
] = set
;
946 if (!ext
->skip
[type
])
947 return pet_scop_free(&ext
->scop
);
952 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
953 * where type takes on the values pet_skip_now and pet_skip_later.
954 * scop may be equal to either scop1 or scop2.
956 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
957 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
959 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
960 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
961 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
963 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
964 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
968 /* Update scop->start and scop->end to include the region from "start"
969 * to "end". In particular, if scop->end == 0, then "scop" does not
970 * have any offset information yet and we simply take the information
971 * from "start" and "end". Otherwise, we update the fields if the
972 * region from "start" to "end" is not already included.
974 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
975 unsigned start
, unsigned end
)
979 if (scop
->end
== 0) {
983 if (start
< scop
->start
)
992 /* Combine the offset information of "scop1" and "scop2" into "scop".
994 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
995 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
998 scop
= pet_scop_update_start_end(scop
,
999 scop1
->start
, scop1
->end
);
1001 scop
= pet_scop_update_start_end(scop
,
1002 scop2
->start
, scop2
->end
);
1006 /* Construct a pet_scop that contains the offset information,
1007 * arrays, statements and skip information in "scop1" and "scop2".
1009 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1010 struct pet_scop
*scop2
)
1013 struct pet_scop
*scop
;
1015 if (!scop1
|| !scop2
)
1018 if (scop1
->n_stmt
== 0) {
1019 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
1020 pet_scop_free(scop1
);
1024 if (scop2
->n_stmt
== 0) {
1025 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
1026 pet_scop_free(scop2
);
1030 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
1034 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
1035 scop1
->n_array
+ scop2
->n_array
);
1038 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
1040 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
1041 scop
->stmts
[i
] = scop1
->stmts
[i
];
1042 scop1
->stmts
[i
] = NULL
;
1045 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
1046 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
1047 scop2
->stmts
[i
] = NULL
;
1050 for (i
= 0; i
< scop1
->n_array
; ++i
) {
1051 scop
->arrays
[i
] = scop1
->arrays
[i
];
1052 scop1
->arrays
[i
] = NULL
;
1055 for (i
= 0; i
< scop2
->n_array
; ++i
) {
1056 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
1057 scop2
->arrays
[i
] = NULL
;
1060 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
1061 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
1062 scop
= scop_combine_skips(scop
, scop1
, scop2
);
1063 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
1065 pet_scop_free(scop1
);
1066 pet_scop_free(scop2
);
1069 pet_scop_free(scop1
);
1070 pet_scop_free(scop2
);
1074 /* Apply the skip condition "skip" to "scop".
1075 * That is, make sure "scop" is not executed when the condition holds.
1077 * If "skip" is an affine expression, we add the conditions under
1078 * which the expression is zero to the iteration domains.
1079 * Otherwise, we add a filter on the variable attaining the value zero.
1081 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
1082 __isl_take isl_set
*skip
)
1090 is_aff
= set_is_affine(skip
);
1095 return pet_scop_filter(scop
, isl_map_from_range(skip
), 0);
1097 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 0);
1098 scop
= pet_scop_restrict(scop
, isl_set_params(skip
));
1103 return pet_scop_free(scop
);
1106 /* Construct a pet_scop that contains the arrays, statements and
1107 * skip information in "scop1" and "scop2", where the two scops
1108 * are executed "in sequence". That is, breaks and continues
1109 * in scop1 have an effect on scop2.
1111 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1112 struct pet_scop
*scop2
)
1114 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
1115 scop2
= restrict_skip(scop2
,
1116 pet_scop_get_skip(scop1
, pet_skip_now
));
1117 return pet_scop_add(ctx
, scop1
, scop2
);
1120 /* Construct a pet_scop that contains the arrays, statements and
1121 * skip information in "scop1" and "scop2", where the two scops
1122 * are executed "in parallel". That is, any break or continue
1123 * in scop1 has no effect on scop2.
1125 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1126 struct pet_scop
*scop2
)
1128 return pet_scop_add(ctx
, scop1
, scop2
);
1131 void *pet_scop_free(struct pet_scop
*scop
)
1134 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1138 isl_set_free(scop
->context
);
1139 isl_set_free(scop
->context_value
);
1141 for (i
= 0; i
< scop
->n_array
; ++i
)
1142 pet_array_free(scop
->arrays
[i
]);
1145 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1146 pet_stmt_free(scop
->stmts
[i
]);
1148 isl_set_free(ext
->skip
[pet_skip_now
]);
1149 isl_set_free(ext
->skip
[pet_skip_later
]);
1154 void pet_scop_dump(struct pet_scop
*scop
)
1157 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1162 isl_set_dump(scop
->context
);
1163 isl_set_dump(scop
->context_value
);
1164 for (i
= 0; i
< scop
->n_array
; ++i
)
1165 pet_array_dump(scop
->arrays
[i
]);
1166 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1167 pet_stmt_dump(scop
->stmts
[i
]);
1170 fprintf(stderr
, "skip\n");
1171 isl_set_dump(ext
->skip
[0]);
1172 isl_set_dump(ext
->skip
[1]);
1176 /* Return 1 if the two pet_arrays are equivalent.
1178 * We don't compare element_size as this may be target dependent.
1180 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
1182 if (!array1
|| !array2
)
1185 if (!isl_set_is_equal(array1
->context
, array2
->context
))
1187 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
1189 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
1191 if (array1
->value_bounds
&&
1192 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
1194 if (strcmp(array1
->element_type
, array2
->element_type
))
1196 if (array1
->live_out
!= array2
->live_out
)
1198 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
1200 if (array1
->declared
!= array2
->declared
)
1202 if (array1
->exposed
!= array2
->exposed
)
1208 /* Return 1 if the two pet_stmts are equivalent.
1210 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
1214 if (!stmt1
|| !stmt2
)
1217 if (stmt1
->line
!= stmt2
->line
)
1219 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
1221 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
1223 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
1225 if (stmt1
->n_arg
!= stmt2
->n_arg
)
1227 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
1228 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
1235 /* Return 1 if the two pet_scops are equivalent.
1237 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1241 if (!scop1
|| !scop2
)
1244 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1246 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1249 if (scop1
->n_array
!= scop2
->n_array
)
1251 for (i
= 0; i
< scop1
->n_array
; ++i
)
1252 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1255 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1257 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1258 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1264 /* Prefix the schedule of "stmt" with an extra dimension with constant
1267 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1272 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1273 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1274 if (!stmt
->schedule
)
1275 return pet_stmt_free(stmt
);
1280 /* Prefix the schedules of all statements in "scop" with an extra
1281 * dimension with constant value "pos".
1283 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1290 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1291 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1292 if (!scop
->stmts
[i
])
1293 return pet_scop_free(scop
);
1299 /* Given a set with a parameter at "param_pos" that refers to the
1300 * iterator, "move" the iterator to the first set dimension.
1301 * That is, essentially equate the parameter to the first set dimension
1302 * and then project it out.
1304 * The first set dimension may however refer to a virtual iterator,
1305 * while the parameter refers to the "real" iterator.
1306 * We therefore need to take into account the mapping "iv_map", which
1307 * maps the virtual iterator to the real iterator.
1308 * In particular, we equate the set dimension to the input of the map
1309 * and the parameter to the output of the map and then project out
1310 * everything we don't need anymore.
1312 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1313 int param_pos
, __isl_take isl_map
*iv_map
)
1316 map
= isl_map_from_domain(set
);
1317 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1318 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1319 iv_map
= isl_map_align_params(iv_map
, isl_map_get_space(map
));
1320 map
= isl_map_apply_range(map
, iv_map
);
1321 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1322 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1323 return isl_map_domain(map
);
1326 /* Data used in embed_access.
1327 * extend adds an iterator to the iteration domain
1328 * iv_map maps the virtual iterator to the real iterator
1329 * var_id represents the induction variable of the corresponding loop
1331 struct pet_embed_access
{
1337 /* Embed the access relation in an extra outer loop.
1339 * We first update the iteration domain to insert the extra dimension.
1341 * If the access refers to the induction variable, then it is
1342 * turned into an access to the set of integers with index (and value)
1343 * equal to the induction variable.
1345 * If the induction variable appears in the constraints (as a parameter),
1346 * then the parameter is equated to the newly introduced iteration
1347 * domain dimension and subsequently projected out.
1349 * Similarly, if the accessed array is a virtual array (with user
1350 * pointer equal to NULL), as created by create_test_access,
1351 * then it is extended along with the domain of the access.
1353 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
1356 struct pet_embed_access
*data
= user
;
1357 isl_id
*array_id
= NULL
;
1360 access
= update_domain(access
, data
->extend
);
1362 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1363 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1364 if (array_id
== data
->var_id
||
1365 (array_id
&& !isl_id_get_user(array_id
))) {
1366 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1367 access
= isl_map_equate(access
,
1368 isl_dim_in
, 0, isl_dim_out
, 0);
1369 if (array_id
== data
->var_id
)
1370 access
= isl_map_apply_range(access
,
1371 isl_map_copy(data
->iv_map
));
1373 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1374 isl_id_copy(array_id
));
1376 isl_id_free(array_id
);
1378 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1380 isl_set
*set
= isl_map_wrap(access
);
1381 set
= internalize_iv(set
, pos
, isl_map_copy(data
->iv_map
));
1382 access
= isl_set_unwrap(set
);
1384 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1385 isl_id_copy(data
->var_id
));
1390 /* Embed all access relations in "expr" in an extra loop.
1391 * "extend" inserts an outer loop iterator in the iteration domains.
1392 * "iv_map" maps the virtual iterator to the real iterator
1393 * "var_id" represents the induction variable.
1395 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
1396 __isl_take isl_map
*extend
, __isl_take isl_map
*iv_map
,
1397 __isl_keep isl_id
*var_id
)
1399 struct pet_embed_access data
=
1400 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1402 expr
= pet_expr_foreach_access(expr
, &embed_access
, &data
);
1403 isl_map_free(iv_map
);
1404 isl_map_free(extend
);
1408 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1409 * "dom" and schedule "sched". "var_id" represents the induction variable
1410 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1411 * That is, it maps the iterator used in "dom" and the domain of "sched"
1412 * to the iterator that some of the parameters in "stmt" may refer to.
1414 * The iteration domain and schedule of the statement are updated
1415 * according to the iteration domain and schedule of the new loop.
1416 * If stmt->domain is a wrapped map, then the iteration domain
1417 * is the domain of this map, so we need to be careful to adjust
1420 * If the induction variable appears in the constraints (as a parameter)
1421 * of the current iteration domain or the schedule of the statement,
1422 * then the parameter is equated to the newly introduced iteration
1423 * domain dimension and subsequently projected out.
1425 * Finally, all access relations are updated based on the extra loop.
1427 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1428 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1429 __isl_take isl_map
*iv_map
, __isl_take isl_id
*var_id
)
1440 if (isl_set_is_wrapping(stmt
->domain
)) {
1445 map
= isl_set_unwrap(stmt
->domain
);
1446 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1447 ran_dim
= isl_space_range(isl_map_get_space(map
));
1448 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1449 isl_set_universe(ran_dim
));
1450 map
= isl_map_flat_domain_product(ext
, map
);
1451 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1452 isl_id_copy(stmt_id
));
1453 dim
= isl_space_domain(isl_map_get_space(map
));
1454 stmt
->domain
= isl_map_wrap(map
);
1456 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1457 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1459 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1460 isl_id_copy(stmt_id
));
1461 dim
= isl_set_get_space(stmt
->domain
);
1464 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1466 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1467 isl_map_copy(iv_map
));
1469 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1470 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1471 isl_dim_in
, stmt_id
);
1473 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1475 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1476 set
= internalize_iv(set
, pos
, isl_map_copy(iv_map
));
1477 stmt
->schedule
= isl_set_unwrap(set
);
1480 dim
= isl_space_map_from_set(dim
);
1481 extend
= isl_map_identity(dim
);
1482 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
1483 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
1484 isl_map_get_tuple_id(extend
, isl_dim_out
));
1485 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1486 stmt
->args
[i
] = expr_embed(stmt
->args
[i
], isl_map_copy(extend
),
1487 isl_map_copy(iv_map
), var_id
);
1488 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1491 isl_id_free(var_id
);
1493 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1495 return pet_stmt_free(stmt
);
1496 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1497 return pet_stmt_free(stmt
);
1501 isl_map_free(sched
);
1502 isl_map_free(iv_map
);
1503 isl_id_free(var_id
);
1507 /* Embed the given pet_array in an extra outer loop with iteration domain
1509 * This embedding only has an effect on virtual arrays (those with
1510 * user pointer equal to NULL), which need to be extended along with
1511 * the iteration domain.
1513 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1514 __isl_take isl_set
*dom
)
1516 isl_id
*array_id
= NULL
;
1521 if (isl_set_has_tuple_id(array
->extent
))
1522 array_id
= isl_set_get_tuple_id(array
->extent
);
1524 if (array_id
&& !isl_id_get_user(array_id
)) {
1525 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1526 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1529 isl_id_free(array_id
);
1538 /* Project out all unnamed parameters from "set" and return the result.
1540 static __isl_give isl_set
*set_project_out_unnamed_params(
1541 __isl_take isl_set
*set
)
1545 n
= isl_set_dim(set
, isl_dim_param
);
1546 for (i
= n
- 1; i
>= 0; --i
) {
1547 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1549 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1555 /* Update the context with respect to an embedding into a loop
1556 * with iteration domain "dom" and induction variable "id".
1557 * "iv_map" maps a possibly virtual iterator (used in "dom")
1558 * to the real iterator (parameter "id").
1560 * If the current context is independent of "id", we don't need
1562 * Otherwise, a parameter value is invalid for the embedding if
1563 * any of the corresponding iterator values is invalid.
1564 * That is, a parameter value is valid only if all the corresponding
1565 * iterator values are valid.
1566 * We therefore compute the set of parameters
1568 * forall i in dom : valid (i)
1572 * not exists i in dom : not valid(i)
1576 * not exists i in dom \ valid(i)
1578 * Before we subtract valid(i) from dom, we first need to map
1579 * the real iterator to the virtual iterator.
1581 * If there are any unnamed parameters in "dom", then we consider
1582 * a parameter value to be valid if it is valid for any value of those
1583 * unnamed parameters. They are therefore projected out at the end.
1585 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1586 __isl_keep isl_set
*dom
, __isl_keep isl_map
*iv_map
,
1587 __isl_keep isl_id
*id
)
1591 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1595 context
= isl_set_from_params(context
);
1596 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1597 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1598 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1599 context
= isl_set_apply(context
, isl_map_reverse(isl_map_copy(iv_map
)));
1600 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1601 context
= isl_set_params(context
);
1602 context
= isl_set_complement(context
);
1603 context
= set_project_out_unnamed_params(context
);
1607 /* Embed all statements and arrays in "scop" in an extra outer loop
1608 * with iteration domain "dom" and schedule "sched".
1609 * "id" represents the induction variable of the loop.
1610 * "iv_map" maps a possibly virtual iterator to the real iterator.
1611 * That is, it maps the iterator used in "dom" and the domain of "sched"
1612 * to the iterator that some of the parameters in "scop" may refer to.
1614 * Any skip conditions within the loop have no effect outside of the loop.
1615 * The caller is responsible for making sure skip[pet_skip_later] has been
1616 * taken into account.
1618 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1619 __isl_take isl_map
*sched
, __isl_take isl_map
*iv_map
,
1620 __isl_take isl_id
*id
)
1627 pet_scop_reset_skip(scop
, pet_skip_now
);
1628 pet_scop_reset_skip(scop
, pet_skip_later
);
1630 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1634 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1635 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1636 isl_set_copy(dom
), isl_map_copy(sched
),
1637 isl_map_copy(iv_map
), isl_id_copy(id
));
1638 if (!scop
->stmts
[i
])
1642 for (i
= 0; i
< scop
->n_array
; ++i
) {
1643 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1645 if (!scop
->arrays
[i
])
1650 isl_map_free(sched
);
1651 isl_map_free(iv_map
);
1656 isl_map_free(sched
);
1657 isl_map_free(iv_map
);
1659 return pet_scop_free(scop
);
1662 /* Add extra conditions on the parameters to iteration domain of "stmt".
1664 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1665 __isl_take isl_set
*cond
)
1670 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1675 return pet_stmt_free(stmt
);
1678 /* Add extra conditions to scop->skip[type].
1680 * The new skip condition only holds if it held before
1681 * and the condition is true. It does not hold if it did not hold
1682 * before or the condition is false.
1684 * The skip condition is assumed to be an affine expression.
1686 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1687 enum pet_skip type
, __isl_keep isl_set
*cond
)
1689 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1695 if (!ext
->skip
[type
])
1698 if (!set_is_affine(ext
->skip
[type
]))
1699 isl_die(isl_set_get_ctx(ext
->skip
[type
]), isl_error_internal
,
1700 "can only resrict affine skips",
1701 return pet_scop_free(scop
));
1703 skip
= ext
->skip
[type
];
1704 skip
= isl_set_intersect_params(skip
, isl_set_copy(cond
));
1705 set
= isl_set_from_params(isl_set_copy(cond
));
1706 set
= isl_set_complement(set
);
1707 set
= isl_set_add_dims(set
, isl_dim_set
, 1);
1708 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 0);
1709 skip
= isl_set_union(skip
, set
);
1710 ext
->skip
[type
] = skip
;
1711 if (!ext
->skip
[type
])
1712 return pet_scop_free(scop
);
1717 /* Add extra conditions on the parameters to all iteration domains
1718 * and skip conditions.
1720 * A parameter value is valid for the result if it was valid
1721 * for the original scop and satisfies "cond" or if it does
1722 * not satisfy "cond" as in this case the scop is not executed
1723 * and the original constraints on the parameters are irrelevant.
1725 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1726 __isl_take isl_set
*cond
)
1730 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1731 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1736 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1737 scop
->context
= isl_set_union(scop
->context
,
1738 isl_set_complement(isl_set_copy(cond
)));
1739 scop
->context
= isl_set_coalesce(scop
->context
);
1740 scop
->context
= set_project_out_unnamed_params(scop
->context
);
1744 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1745 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1746 isl_set_copy(cond
));
1747 if (!scop
->stmts
[i
])
1755 return pet_scop_free(scop
);
1758 /* Construct a map that inserts a filter value with name "id" and value
1759 * "satisfied" in the list of filter values embedded in the set space "space".
1761 * If "space" does not contain any filter values yet, we first create
1762 * a map that inserts 0 filter values, i.e.,
1764 * space -> [space -> []]
1766 * We can now assume that space is of the form [dom -> [filters]]
1767 * We construct an identity mapping on dom and a mapping on filters
1768 * that inserts the new filter
1771 * [filters] -> [satisfied, filters]
1773 * and then compute the cross product
1775 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1777 static __isl_give isl_map
*insert_filter_map(__isl_take isl_space
*space
,
1778 __isl_take isl_id
*id
, int satisfied
)
1781 isl_map
*map
, *map_dom
, *map_ran
;
1784 if (isl_space_is_wrapping(space
)) {
1785 space2
= isl_space_map_from_set(isl_space_copy(space
));
1786 map
= isl_map_identity(space2
);
1787 space
= isl_space_unwrap(space
);
1789 space
= isl_space_from_domain(space
);
1790 map
= isl_map_universe(isl_space_copy(space
));
1791 map
= isl_map_reverse(isl_map_domain_map(map
));
1794 space2
= isl_space_domain(isl_space_copy(space
));
1795 map_dom
= isl_map_identity(isl_space_map_from_set(space2
));
1796 space
= isl_space_range(space
);
1797 map_ran
= isl_map_identity(isl_space_map_from_set(space
));
1798 map_ran
= isl_map_insert_dims(map_ran
, isl_dim_out
, 0, 1);
1799 map_ran
= isl_map_set_dim_id(map_ran
, isl_dim_out
, 0, id
);
1800 map_ran
= isl_map_fix_si(map_ran
, isl_dim_out
, 0, satisfied
);
1802 map
= isl_map_apply_range(map
, isl_map_product(map_dom
, map_ran
));
1807 /* Insert an argument expression corresponding to "test" in front
1808 * of the list of arguments described by *n_arg and *args.
1810 static int args_insert_access(unsigned *n_arg
, struct pet_expr
***args
,
1811 __isl_keep isl_map
*test
)
1814 isl_ctx
*ctx
= isl_map_get_ctx(test
);
1820 *args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1824 struct pet_expr
**ext
;
1825 ext
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + *n_arg
);
1828 for (i
= 0; i
< *n_arg
; ++i
)
1829 ext
[1 + i
] = (*args
)[i
];
1834 (*args
)[0] = pet_expr_from_access(isl_map_copy(test
));
1841 /* Make the expression "expr" depend on the value of "test"
1842 * being equal to "satisfied".
1844 * If "test" is an affine expression, we simply add the conditions
1845 * on the expression have the value "satisfied" to all access relations.
1847 * Otherwise, we add a filter to "expr" (which is then assumed to be
1848 * an access expression) corresponding to "test" being equal to "satisfied".
1850 struct pet_expr
*pet_expr_filter(struct pet_expr
*expr
,
1851 __isl_take isl_map
*test
, int satisfied
)
1861 if (!isl_map_has_tuple_id(test
, isl_dim_out
)) {
1862 test
= isl_map_fix_si(test
, isl_dim_out
, 0, satisfied
);
1863 return pet_expr_restrict(expr
, isl_map_params(test
));
1866 ctx
= isl_map_get_ctx(test
);
1867 if (expr
->type
!= pet_expr_access
)
1868 isl_die(ctx
, isl_error_invalid
,
1869 "can only filter access expressions", goto error
);
1871 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1872 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1873 map
= insert_filter_map(space
, id
, satisfied
);
1875 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
1876 if (!expr
->acc
.access
)
1879 if (args_insert_access(&expr
->n_arg
, &expr
->args
, test
) < 0)
1886 return pet_expr_free(expr
);
1889 /* Make the statement "stmt" depend on the value of "test"
1890 * being equal to "satisfied" by adjusting stmt->domain.
1892 * The domain of "test" corresponds to the (zero or more) outer dimensions
1893 * of the iteration domain.
1895 * We insert an argument corresponding to a read to "test"
1896 * from the iteration domain of "stmt" in front of the list of arguments.
1897 * We also insert a corresponding output dimension in the wrapped
1898 * map contained in stmt->domain, with value set to "satisfied".
1900 static struct pet_stmt
*stmt_filter(struct pet_stmt
*stmt
,
1901 __isl_take isl_map
*test
, int satisfied
)
1906 isl_map
*map
, *add_dom
;
1914 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1915 map
= insert_filter_map(isl_set_get_space(stmt
->domain
), id
, satisfied
);
1916 stmt
->domain
= isl_set_apply(stmt
->domain
, map
);
1918 space
= isl_space_unwrap(isl_set_get_space(stmt
->domain
));
1919 dom
= isl_set_universe(isl_space_domain(space
));
1920 n_test_dom
= isl_map_dim(test
, isl_dim_in
);
1921 add_dom
= isl_map_from_range(dom
);
1922 add_dom
= isl_map_add_dims(add_dom
, isl_dim_in
, n_test_dom
);
1923 for (i
= 0; i
< n_test_dom
; ++i
)
1924 add_dom
= isl_map_equate(add_dom
, isl_dim_in
, i
,
1926 test
= isl_map_apply_domain(test
, add_dom
);
1928 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1935 return pet_stmt_free(stmt
);
1938 /* Does "scop" have a skip condition of the given "type"?
1940 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
1942 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1946 return ext
->skip
[type
] != NULL
;
1949 /* Does "scop" have a skip condition of the given "type" that
1950 * is an affine expression?
1952 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
1954 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1958 if (!ext
->skip
[type
])
1960 return set_is_affine(ext
->skip
[type
]);
1963 /* Does "scop" have a skip condition of the given "type" that
1964 * is not an affine expression?
1966 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
1968 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1973 if (!ext
->skip
[type
])
1975 aff
= set_is_affine(ext
->skip
[type
]);
1981 /* Does "scop" have a skip condition of the given "type" that
1982 * is affine and holds on the entire domain?
1984 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
1986 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1991 is_aff
= pet_scop_has_affine_skip(scop
, type
);
1992 if (is_aff
< 0 || !is_aff
)
1995 set
= isl_set_copy(ext
->skip
[type
]);
1996 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
1997 set
= isl_set_params(set
);
1998 is_univ
= isl_set_plain_is_universe(set
);
2004 /* Replace scop->skip[type] by "skip".
2006 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
2007 enum pet_skip type
, __isl_take isl_set
*skip
)
2009 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2014 isl_set_free(ext
->skip
[type
]);
2015 ext
->skip
[type
] = skip
;
2020 return pet_scop_free(scop
);
2023 /* Return a copy of scop->skip[type].
2025 __isl_give isl_set
*pet_scop_get_skip(struct pet_scop
*scop
,
2028 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2033 return isl_set_copy(ext
->skip
[type
]);
2036 /* Return a map to the skip condition of the given type.
2038 __isl_give isl_map
*pet_scop_get_skip_map(struct pet_scop
*scop
,
2041 return isl_map_from_range(pet_scop_get_skip(scop
, type
));
2044 /* Return an access pet_expr corresponding to the skip condition
2045 * of the given type.
2047 struct pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
2050 return pet_expr_from_access(pet_scop_get_skip_map(scop
, type
));
2053 /* Drop the the skip condition scop->skip[type].
2055 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
2057 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2062 isl_set_free(ext
->skip
[type
]);
2063 ext
->skip
[type
] = NULL
;
2066 /* Make the skip condition (if any) depend on the value of "test" being
2067 * equal to "satisfied".
2069 * We only support the case where the original skip condition is universal,
2070 * i.e., where skipping is unconditional, and where satisfied == 1.
2071 * In this case, the skip condition is changed to skip only when
2072 * "test" is equal to one.
2074 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
2075 enum pet_skip type
, __isl_keep isl_map
*test
, int satisfied
)
2081 if (!pet_scop_has_skip(scop
, type
))
2085 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2087 return pet_scop_free(scop
);
2088 if (satisfied
&& is_univ
) {
2089 scop
= pet_scop_set_skip(scop
, type
,
2090 isl_map_range(isl_map_copy(test
)));
2094 isl_die(isl_map_get_ctx(test
), isl_error_internal
,
2095 "skip expression cannot be filtered",
2096 return pet_scop_free(scop
));
2102 /* Make all statements in "scop" depend on the value of "test"
2103 * being equal to "satisfied" by adjusting their domains.
2105 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2106 __isl_take isl_map
*test
, int satisfied
)
2110 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
2111 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
2116 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2117 scop
->stmts
[i
] = stmt_filter(scop
->stmts
[i
],
2118 isl_map_copy(test
), satisfied
);
2119 if (!scop
->stmts
[i
])
2127 return pet_scop_free(scop
);
2130 /* Do the filters "i" and "j" always have the same value?
2132 static int equal_filter_values(__isl_keep isl_set
*domain
, int i
, int j
)
2134 isl_map
*map
, *test
;
2137 map
= isl_set_unwrap(isl_set_copy(domain
));
2138 test
= isl_map_universe(isl_map_get_space(map
));
2139 test
= isl_map_equate(test
, isl_dim_out
, i
, isl_dim_out
, j
);
2140 equal
= isl_map_is_subset(map
, test
);
2147 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2148 * access relation, the union of the two access relations.
2150 static struct pet_stmt
*merge_filter_pair(struct pet_stmt
*stmt
, int i
, int j
)
2158 stmt
->args
[i
]->acc
.access
= isl_map_union(stmt
->args
[i
]->acc
.access
,
2159 isl_map_copy(stmt
->args
[j
]->acc
.access
));
2160 stmt
->args
[i
]->acc
.access
= isl_map_coalesce(stmt
->args
[i
]->acc
.access
);
2162 pet_expr_free(stmt
->args
[j
]);
2163 for (k
= j
; k
< stmt
->n_arg
- 1; ++k
)
2164 stmt
->args
[k
] = stmt
->args
[k
+ 1];
2167 map
= isl_set_unwrap(stmt
->domain
);
2168 map
= isl_map_project_out(map
, isl_dim_out
, j
, 1);
2169 stmt
->domain
= isl_map_wrap(map
);
2171 if (!stmt
->domain
|| !stmt
->args
[i
]->acc
.access
)
2172 return pet_stmt_free(stmt
);
2177 /* Look for any pair of filters that access the same filter variable
2178 * and that have the same filter value and merge them into a single
2179 * filter with as filter access relation the union of the filter access
2182 static struct pet_stmt
*stmt_merge_filters(struct pet_stmt
*stmt
)
2185 isl_space
*space_i
, *space_j
;
2189 if (stmt
->n_arg
<= 1)
2192 for (i
= 0; i
< stmt
->n_arg
- 1; ++i
) {
2193 if (stmt
->args
[i
]->type
!= pet_expr_access
)
2195 if (pet_expr_is_affine(stmt
->args
[i
]))
2198 space_i
= isl_map_get_space(stmt
->args
[i
]->acc
.access
);
2200 for (j
= stmt
->n_arg
- 1; j
> i
; --j
) {
2203 if (stmt
->args
[j
]->type
!= pet_expr_access
)
2205 if (pet_expr_is_affine(stmt
->args
[j
]))
2208 space_j
= isl_map_get_space(stmt
->args
[j
]->acc
.access
);
2210 eq
= isl_space_is_equal(space_i
, space_j
);
2212 eq
= equal_filter_values(stmt
->domain
, i
, j
);
2214 stmt
= merge_filter_pair(stmt
, i
, j
);
2216 isl_space_free(space_j
);
2218 if (eq
< 0 || !stmt
)
2222 isl_space_free(space_i
);
2225 return pet_stmt_free(stmt
);
2231 /* Look for any pair of filters that access the same filter variable
2232 * and that have the same filter value and merge them into a single
2233 * filter with as filter access relation the union of the filter access
2236 struct pet_scop
*pet_scop_merge_filters(struct pet_scop
*scop
)
2243 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2244 scop
->stmts
[i
] = stmt_merge_filters(scop
->stmts
[i
]);
2245 if (!scop
->stmts
[i
])
2246 return pet_scop_free(scop
);
2252 /* Add all parameters in "expr" to "dim" and return the result.
2254 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
2255 __isl_take isl_space
*dim
)
2261 for (i
= 0; i
< expr
->n_arg
; ++i
)
2263 dim
= expr_collect_params(expr
->args
[i
], dim
);
2265 if (expr
->type
== pet_expr_access
)
2266 dim
= isl_space_align_params(dim
,
2267 isl_map_get_space(expr
->acc
.access
));
2271 isl_space_free(dim
);
2272 return pet_expr_free(expr
);
2275 /* Add all parameters in "stmt" to "dim" and return the result.
2277 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
2278 __isl_take isl_space
*dim
)
2283 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
2284 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
2285 dim
= expr_collect_params(stmt
->body
, dim
);
2289 isl_space_free(dim
);
2290 return pet_stmt_free(stmt
);
2293 /* Add all parameters in "array" to "dim" and return the result.
2295 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
2296 __isl_take isl_space
*dim
)
2301 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
2302 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
2306 pet_array_free(array
);
2307 return isl_space_free(dim
);
2310 /* Add all parameters in "scop" to "dim" and return the result.
2312 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2313 __isl_take isl_space
*dim
)
2320 for (i
= 0; i
< scop
->n_array
; ++i
)
2321 dim
= array_collect_params(scop
->arrays
[i
], dim
);
2323 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2324 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
2328 isl_space_free(dim
);
2329 return pet_scop_free(scop
);
2332 /* Add all parameters in "dim" to all access relations in "expr".
2334 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
2335 __isl_take isl_space
*dim
)
2342 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2344 expr_propagate_params(expr
->args
[i
],
2345 isl_space_copy(dim
));
2350 if (expr
->type
== pet_expr_access
) {
2351 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
2352 isl_space_copy(dim
));
2353 if (!expr
->acc
.access
)
2357 isl_space_free(dim
);
2360 isl_space_free(dim
);
2361 return pet_expr_free(expr
);
2364 /* Add all parameters in "dim" to the domain, schedule and
2365 * all access relations in "stmt".
2367 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2368 __isl_take isl_space
*dim
)
2373 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
2374 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2375 isl_space_copy(dim
));
2376 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
2378 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2381 isl_space_free(dim
);
2384 isl_space_free(dim
);
2385 return pet_stmt_free(stmt
);
2388 /* Add all parameters in "dim" to "array".
2390 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2391 __isl_take isl_space
*dim
)
2396 array
->context
= isl_set_align_params(array
->context
,
2397 isl_space_copy(dim
));
2398 array
->extent
= isl_set_align_params(array
->extent
,
2399 isl_space_copy(dim
));
2400 if (array
->value_bounds
) {
2401 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2402 isl_space_copy(dim
));
2403 if (!array
->value_bounds
)
2407 if (!array
->context
|| !array
->extent
)
2410 isl_space_free(dim
);
2413 isl_space_free(dim
);
2414 return pet_array_free(array
);
2417 /* Add all parameters in "dim" to "scop".
2419 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2420 __isl_take isl_space
*dim
)
2427 for (i
= 0; i
< scop
->n_array
; ++i
) {
2428 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2429 isl_space_copy(dim
));
2430 if (!scop
->arrays
[i
])
2434 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2435 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2436 isl_space_copy(dim
));
2437 if (!scop
->stmts
[i
])
2441 isl_space_free(dim
);
2444 isl_space_free(dim
);
2445 return pet_scop_free(scop
);
2448 /* Update all isl_sets and isl_maps in "scop" such that they all
2449 * have the same parameters.
2451 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2458 dim
= isl_set_get_space(scop
->context
);
2459 dim
= scop_collect_params(scop
, dim
);
2461 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
2462 scop
= scop_propagate_params(scop
, dim
);
2467 /* Check if the given access relation accesses a (0D) array that corresponds
2468 * to one of the parameters in "dim". If so, replace the array access
2469 * by an access to the set of integers with as index (and value)
2472 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
2473 __isl_take isl_space
*dim
)
2475 isl_id
*array_id
= NULL
;
2478 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
2479 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
2480 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
2482 isl_space_free(dim
);
2485 isl_id_free(array_id
);
2489 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
2491 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
2492 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
2495 isl_id_free(array_id
);
2497 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
2498 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
2503 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2504 * in "dim" by a value equal to the corresponding parameter.
2506 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
2507 __isl_take isl_space
*dim
)
2514 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2516 expr_detect_parameter_accesses(expr
->args
[i
],
2517 isl_space_copy(dim
));
2522 if (expr
->type
== pet_expr_access
) {
2523 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
2524 isl_space_copy(dim
));
2525 if (!expr
->acc
.access
)
2529 isl_space_free(dim
);
2532 isl_space_free(dim
);
2533 return pet_expr_free(expr
);
2536 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2537 * in "dim" by a value equal to the corresponding parameter.
2539 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
2540 __isl_take isl_space
*dim
)
2545 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
2546 isl_space_copy(dim
));
2548 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2551 isl_space_free(dim
);
2554 isl_space_free(dim
);
2555 return pet_stmt_free(stmt
);
2558 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2559 * in "dim" by a value equal to the corresponding parameter.
2561 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
2562 __isl_take isl_space
*dim
)
2569 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2570 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
2571 isl_space_copy(dim
));
2572 if (!scop
->stmts
[i
])
2576 isl_space_free(dim
);
2579 isl_space_free(dim
);
2580 return pet_scop_free(scop
);
2583 /* Replace all accesses to (0D) arrays that correspond to any of
2584 * the parameters used in "scop" by a value equal
2585 * to the corresponding parameter.
2587 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
2594 dim
= isl_set_get_space(scop
->context
);
2595 dim
= scop_collect_params(scop
, dim
);
2597 scop
= scop_detect_parameter_accesses(scop
, dim
);
2602 /* Add all read access relations (if "read" is set) and/or all write
2603 * access relations (if "write" is set) to "accesses" and return the result.
2605 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
2606 int read
, int write
, __isl_take isl_union_map
*accesses
)
2615 for (i
= 0; i
< expr
->n_arg
; ++i
)
2616 accesses
= expr_collect_accesses(expr
->args
[i
],
2617 read
, write
, accesses
);
2619 if (expr
->type
== pet_expr_access
&&
2620 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
2621 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
2622 accesses
= isl_union_map_add_map(accesses
,
2623 isl_map_copy(expr
->acc
.access
));
2628 /* Collect and return all read access relations (if "read" is set)
2629 * and/or all write access relations (if "write" is set) in "stmt".
2631 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2632 int read
, int write
, __isl_take isl_space
*dim
)
2634 isl_union_map
*accesses
;
2639 accesses
= isl_union_map_empty(dim
);
2640 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
2641 accesses
= isl_union_map_intersect_domain(accesses
,
2642 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
2647 /* Collect and return all read access relations (if "read" is set)
2648 * and/or all write access relations (if "write" is set) in "scop".
2650 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2651 int read
, int write
)
2654 isl_union_map
*accesses
;
2659 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2661 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2662 isl_union_map
*accesses_i
;
2663 isl_space
*dim
= isl_set_get_space(scop
->context
);
2664 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
2666 accesses
= isl_union_map_union(accesses
, accesses_i
);
2672 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
2674 return scop_collect_accesses(scop
, 1, 0);
2677 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
2679 return scop_collect_accesses(scop
, 0, 1);
2682 /* Collect and return the union of iteration domains in "scop".
2684 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2688 isl_union_set
*domain
;
2693 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2695 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2696 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2697 domain
= isl_union_set_add_set(domain
, domain_i
);
2703 /* Collect and return the schedules of the statements in "scop".
2704 * The range is normalized to the maximal number of scheduling
2707 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2710 isl_map
*schedule_i
;
2711 isl_union_map
*schedule
;
2712 int depth
, max_depth
= 0;
2717 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2719 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2720 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2721 if (depth
> max_depth
)
2725 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2726 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2727 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2728 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2730 for (j
= depth
; j
< max_depth
; ++j
)
2731 schedule_i
= isl_map_fix_si(schedule_i
,
2733 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2739 /* Does expression "expr" write to "id"?
2741 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
2746 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2747 int writes
= expr_writes(expr
->args
[i
], id
);
2748 if (writes
< 0 || writes
)
2752 if (expr
->type
!= pet_expr_access
)
2754 if (!expr
->acc
.write
)
2756 if (!isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
))
2759 write_id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
2760 isl_id_free(write_id
);
2765 return write_id
== id
;
2768 /* Does statement "stmt" write to "id"?
2770 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
2772 return expr_writes(stmt
->body
, id
);
2775 /* Is there any write access in "scop" that accesses "id"?
2777 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
2784 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2785 int writes
= stmt_writes(scop
->stmts
[i
], id
);
2786 if (writes
< 0 || writes
)
2793 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2795 static __isl_give isl_set
*set_anonymize(__isl_take isl_set
*set
)
2799 n
= isl_set_dim(set
, isl_dim_param
);
2800 for (i
= 0; i
< n
; ++i
) {
2801 isl_id
*id
= isl_set_get_dim_id(set
, isl_dim_param
, i
);
2802 const char *name
= isl_id_get_name(id
);
2803 set
= isl_set_set_dim_name(set
, isl_dim_param
, i
, name
);
2807 if (!isl_set_is_params(set
) && isl_set_has_tuple_id(set
)) {
2808 isl_id
*id
= isl_set_get_tuple_id(set
);
2809 const char *name
= isl_id_get_name(id
);
2810 set
= isl_set_set_tuple_name(set
, name
);
2817 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2819 static __isl_give isl_map
*map_anonymize(__isl_take isl_map
*map
)
2823 n
= isl_map_dim(map
, isl_dim_param
);
2824 for (i
= 0; i
< n
; ++i
) {
2825 isl_id
*id
= isl_map_get_dim_id(map
, isl_dim_param
, i
);
2826 const char *name
= isl_id_get_name(id
);
2827 map
= isl_map_set_dim_name(map
, isl_dim_param
, i
, name
);
2831 if (isl_map_has_tuple_id(map
, isl_dim_in
)) {
2832 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_in
);
2833 const char *name
= isl_id_get_name(id
);
2834 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
2838 if (isl_map_has_tuple_id(map
, isl_dim_out
)) {
2839 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_out
);
2840 const char *name
= isl_id_get_name(id
);
2841 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
2848 /* Reset the user pointer on all parameter ids in "array".
2850 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2855 array
->context
= set_anonymize(array
->context
);
2856 array
->extent
= set_anonymize(array
->extent
);
2857 if (!array
->context
|| !array
->extent
)
2858 return pet_array_free(array
);
2863 /* Reset the user pointer on all parameter and tuple ids in "access".
2865 static __isl_give isl_map
*access_anonymize(__isl_take isl_map
*access
,
2868 access
= map_anonymize(access
);
2873 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2875 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2884 stmt
->domain
= set_anonymize(stmt
->domain
);
2885 stmt
->schedule
= map_anonymize(stmt
->schedule
);
2886 if (!stmt
->domain
|| !stmt
->schedule
)
2887 return pet_stmt_free(stmt
);
2889 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2890 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
2891 &access_anonymize
, NULL
);
2893 return pet_stmt_free(stmt
);
2896 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
2897 &access_anonymize
, NULL
);
2899 return pet_stmt_free(stmt
);
2904 /* Reset the user pointer on all parameter and tuple ids in "scop".
2906 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2913 scop
->context
= set_anonymize(scop
->context
);
2914 scop
->context_value
= set_anonymize(scop
->context_value
);
2915 if (!scop
->context
|| !scop
->context_value
)
2916 return pet_scop_free(scop
);
2918 for (i
= 0; i
< scop
->n_array
; ++i
) {
2919 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2920 if (!scop
->arrays
[i
])
2921 return pet_scop_free(scop
);
2924 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2925 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2926 if (!scop
->stmts
[i
])
2927 return pet_scop_free(scop
);
2933 /* Given a set "domain", return a wrapped relation with the given set
2934 * as domain and a range of dimension "n_arg", where each coordinate
2935 * is either unbounded or, if the corresponding element of args is of
2936 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2938 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
2939 unsigned n_arg
, struct pet_expr
**args
,
2940 __isl_keep isl_union_map
*value_bounds
)
2945 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2947 map
= isl_map_from_domain(domain
);
2948 space
= isl_map_get_space(map
);
2949 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2951 for (i
= 0; i
< n_arg
; ++i
) {
2953 struct pet_expr
*arg
= args
[i
];
2957 map_i
= isl_map_universe(isl_space_copy(space
));
2958 if (arg
->type
== pet_expr_access
) {
2960 id
= isl_map_get_tuple_id(arg
->acc
.access
, isl_dim_out
);
2961 space2
= isl_space_alloc(ctx
, 0, 0, 1);
2962 space2
= isl_space_set_tuple_id(space2
, isl_dim_in
, id
);
2963 vb
= isl_union_map_extract_map(value_bounds
, space2
);
2964 if (!isl_map_plain_is_empty(vb
))
2965 map_i
= isl_map_intersect_range(map_i
,
2970 map
= isl_map_flat_range_product(map
, map_i
);
2972 isl_space_free(space
);
2974 return isl_map_wrap(map
);
2977 /* Data used in access_gist() callback.
2979 struct pet_access_gist_data
{
2981 isl_union_map
*value_bounds
;
2984 /* Given an expression "expr" of type pet_expr_access, compute
2985 * the gist of the associated access relation with respect to
2986 * data->domain and the bounds on the values of the arguments
2987 * of the expression.
2989 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
2991 struct pet_access_gist_data
*data
= user
;
2994 domain
= isl_set_copy(data
->domain
);
2995 if (expr
->n_arg
> 0)
2996 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
2997 data
->value_bounds
);
2999 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
, domain
);
3000 if (!expr
->acc
.access
)
3001 return pet_expr_free(expr
);
3006 /* Compute the gist of the iteration domain and all access relations
3007 * of "stmt" based on the constraints on the parameters specified by "context"
3008 * and the constraints on the values of nested accesses specified
3009 * by "value_bounds".
3011 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
3012 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
3017 struct pet_access_gist_data data
;
3022 data
.domain
= isl_set_copy(stmt
->domain
);
3023 data
.value_bounds
= value_bounds
;
3024 if (stmt
->n_arg
> 0)
3025 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
3027 data
.domain
= isl_set_intersect_params(data
.domain
,
3028 isl_set_copy(context
));
3030 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
3031 stmt
->args
[i
] = pet_expr_foreach_access_expr(stmt
->args
[i
],
3032 &access_gist
, &data
);
3037 stmt
->body
= pet_expr_foreach_access_expr(stmt
->body
,
3038 &access_gist
, &data
);
3042 isl_set_free(data
.domain
);
3044 space
= isl_set_get_space(stmt
->domain
);
3045 if (isl_space_is_wrapping(space
))
3046 space
= isl_space_domain(isl_space_unwrap(space
));
3047 domain
= isl_set_universe(space
);
3048 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
3049 if (stmt
->n_arg
> 0)
3050 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
3052 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
3054 return pet_stmt_free(stmt
);
3058 isl_set_free(data
.domain
);
3059 return pet_stmt_free(stmt
);
3062 /* Compute the gist of the extent of the array
3063 * based on the constraints on the parameters specified by "context".
3065 static struct pet_array
*array_gist(struct pet_array
*array
,
3066 __isl_keep isl_set
*context
)
3071 array
->extent
= isl_set_gist_params(array
->extent
,
3072 isl_set_copy(context
));
3074 return pet_array_free(array
);
3079 /* Compute the gist of all sets and relations in "scop"
3080 * based on the constraints on the parameters specified by "scop->context"
3081 * and the constraints on the values of nested accesses specified
3082 * by "value_bounds".
3084 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
3085 __isl_keep isl_union_map
*value_bounds
)
3092 scop
->context
= isl_set_coalesce(scop
->context
);
3094 return pet_scop_free(scop
);
3096 for (i
= 0; i
< scop
->n_array
; ++i
) {
3097 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
3098 if (!scop
->arrays
[i
])
3099 return pet_scop_free(scop
);
3102 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3103 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
3105 if (!scop
->stmts
[i
])
3106 return pet_scop_free(scop
);
3112 /* Intersect the context of "scop" with "context".
3113 * To ensure that we don't introduce any unnamed parameters in
3114 * the context of "scop", we first remove the unnamed parameters
3117 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
3118 __isl_take isl_set
*context
)
3123 context
= set_project_out_unnamed_params(context
);
3124 scop
->context
= isl_set_intersect(scop
->context
, context
);
3126 return pet_scop_free(scop
);
3130 isl_set_free(context
);
3131 return pet_scop_free(scop
);
3134 /* Drop the current context of "scop". That is, replace the context
3135 * by a universal set.
3137 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
3144 space
= isl_set_get_space(scop
->context
);
3145 isl_set_free(scop
->context
);
3146 scop
->context
= isl_set_universe(space
);
3148 return pet_scop_free(scop
);
3153 /* Append "array" to the arrays of "scop".
3155 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
3156 struct pet_array
*array
)
3159 struct pet_array
**arrays
;
3161 if (!array
|| !scop
)
3164 ctx
= isl_set_get_ctx(scop
->context
);
3165 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3169 scop
->arrays
= arrays
;
3170 scop
->arrays
[scop
->n_array
] = array
;
3175 pet_array_free(array
);
3176 return pet_scop_free(scop
);