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_double
] = "double",
46 [pet_expr_unary
] = "unary",
47 [pet_expr_binary
] = "binary",
48 [pet_expr_ternary
] = "ternary"
51 static char *op_str
[] = {
52 [pet_op_add_assign
] = "+=",
53 [pet_op_sub_assign
] = "-=",
54 [pet_op_mul_assign
] = "*=",
55 [pet_op_div_assign
] = "/=",
56 [pet_op_assign
] = "=",
66 [pet_op_post_inc
] = "++",
67 [pet_op_post_dec
] = "--",
68 [pet_op_pre_inc
] = "++",
69 [pet_op_pre_dec
] = "--",
70 [pet_op_address_of
] = "&"
73 const char *pet_op_str(enum pet_op_type op
)
78 int pet_op_is_inc_dec(enum pet_op_type op
)
80 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
81 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
84 const char *pet_type_str(enum pet_expr_type type
)
86 return type_str
[type
];
89 enum pet_op_type
pet_str_op(const char *str
)
93 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
94 if (!strcmp(op_str
[i
], str
))
100 enum pet_expr_type
pet_str_type(const char *str
)
104 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
105 if (!strcmp(type_str
[i
], str
))
111 /* Construct a pet_expr from an access relation.
112 * By default, it is considered to be a read access.
114 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
116 isl_ctx
*ctx
= isl_map_get_ctx(access
);
117 struct pet_expr
*expr
;
121 expr
= isl_calloc_type(ctx
, struct pet_expr
);
125 expr
->type
= pet_expr_access
;
126 expr
->acc
.access
= access
;
132 isl_map_free(access
);
136 /* Construct a unary pet_expr that performs "op" on "arg".
138 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
139 struct pet_expr
*arg
)
141 struct pet_expr
*expr
;
145 expr
= isl_alloc_type(ctx
, struct pet_expr
);
149 expr
->type
= pet_expr_unary
;
152 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
155 expr
->args
[pet_un_arg
] = arg
;
163 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
165 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
166 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
168 struct pet_expr
*expr
;
172 expr
= isl_alloc_type(ctx
, struct pet_expr
);
176 expr
->type
= pet_expr_binary
;
179 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
182 expr
->args
[pet_bin_lhs
] = lhs
;
183 expr
->args
[pet_bin_rhs
] = rhs
;
192 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
194 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
195 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
197 struct pet_expr
*expr
;
199 if (!cond
|| !lhs
|| !rhs
)
201 expr
= isl_alloc_type(ctx
, struct pet_expr
);
205 expr
->type
= pet_expr_ternary
;
207 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
210 expr
->args
[pet_ter_cond
] = cond
;
211 expr
->args
[pet_ter_true
] = lhs
;
212 expr
->args
[pet_ter_false
] = rhs
;
222 /* Construct a call pet_expr that calls function "name" with "n_arg"
223 * arguments. The caller is responsible for filling in the arguments.
225 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
228 struct pet_expr
*expr
;
230 expr
= isl_alloc_type(ctx
, struct pet_expr
);
234 expr
->type
= pet_expr_call
;
236 expr
->name
= strdup(name
);
237 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
238 if (!expr
->name
|| !expr
->args
)
239 return pet_expr_free(expr
);
244 /* Construct a pet_expr that represents the double "d".
246 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double d
)
248 struct pet_expr
*expr
;
250 expr
= isl_calloc_type(ctx
, struct pet_expr
);
254 expr
->type
= pet_expr_double
;
260 void *pet_expr_free(struct pet_expr
*expr
)
267 for (i
= 0; i
< expr
->n_arg
; ++i
)
268 pet_expr_free(expr
->args
[i
]);
271 switch (expr
->type
) {
272 case pet_expr_access
:
273 isl_map_free(expr
->acc
.access
);
278 case pet_expr_double
:
280 case pet_expr_binary
:
281 case pet_expr_ternary
:
289 static void expr_dump(struct pet_expr
*expr
, int indent
)
296 fprintf(stderr
, "%*s", indent
, "");
298 switch (expr
->type
) {
299 case pet_expr_double
:
300 fprintf(stderr
, "%g\n", expr
->d
);
302 case pet_expr_access
:
303 isl_map_dump(expr
->acc
.access
);
304 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
306 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
307 "", expr
->acc
.write
);
308 for (i
= 0; i
< expr
->n_arg
; ++i
)
309 expr_dump(expr
->args
[i
], indent
+ 2);
312 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
313 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
315 case pet_expr_binary
:
316 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
317 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
318 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
320 case pet_expr_ternary
:
321 fprintf(stderr
, "?:\n");
322 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
323 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
324 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
327 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
328 for (i
= 0; i
< expr
->n_arg
; ++i
)
329 expr_dump(expr
->args
[i
], indent
+ 2);
334 void pet_expr_dump(struct pet_expr
*expr
)
339 /* Does "expr" represent an access to an unnamed space, i.e.,
340 * does it represent an affine expression?
342 int pet_expr_is_affine(struct pet_expr
*expr
)
348 if (expr
->type
!= pet_expr_access
)
351 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
358 /* Return 1 if the two pet_exprs are equivalent.
360 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
364 if (!expr1
|| !expr2
)
367 if (expr1
->type
!= expr2
->type
)
369 if (expr1
->n_arg
!= expr2
->n_arg
)
371 for (i
= 0; i
< expr1
->n_arg
; ++i
)
372 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
374 switch (expr1
->type
) {
375 case pet_expr_double
:
376 if (expr1
->d
!= expr2
->d
)
379 case pet_expr_access
:
380 if (expr1
->acc
.read
!= expr2
->acc
.read
)
382 if (expr1
->acc
.write
!= expr2
->acc
.write
)
384 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
386 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
390 case pet_expr_binary
:
391 case pet_expr_ternary
:
392 if (expr1
->op
!= expr2
->op
)
396 if (strcmp(expr1
->name
, expr2
->name
))
404 /* Add extra conditions on the parameters to all access relations in "expr".
406 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
407 __isl_take isl_set
*cond
)
414 for (i
= 0; i
< expr
->n_arg
; ++i
) {
415 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
421 if (expr
->type
== pet_expr_access
) {
422 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
424 if (!expr
->acc
.access
)
432 return pet_expr_free(expr
);
435 /* Modify all access relations in "expr" by calling "fn" on them.
437 struct pet_expr
*pet_expr_foreach_access(struct pet_expr
*expr
,
438 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
446 for (i
= 0; i
< expr
->n_arg
; ++i
) {
447 expr
->args
[i
] = pet_expr_foreach_access(expr
->args
[i
], fn
, user
);
449 return pet_expr_free(expr
);
452 if (expr
->type
== pet_expr_access
) {
453 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
454 if (!expr
->acc
.access
)
455 return pet_expr_free(expr
);
461 /* Modify all expressions of type pet_expr_access in "expr"
462 * by calling "fn" on them.
464 struct pet_expr
*pet_expr_foreach_access_expr(struct pet_expr
*expr
,
465 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
473 for (i
= 0; i
< expr
->n_arg
; ++i
) {
474 expr
->args
[i
] = pet_expr_foreach_access_expr(expr
->args
[i
],
477 return pet_expr_free(expr
);
480 if (expr
->type
== pet_expr_access
)
481 expr
= fn(expr
, user
);
486 /* Modify the given access relation based on the given iteration space
488 * If the access has any arguments then the domain of the access relation
489 * is a wrapped mapping from the iteration space to the space of
490 * argument values. We only need to change the domain of this wrapped
491 * mapping, so we extend the input transformation with an identity mapping
492 * on the space of argument values.
494 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
497 isl_map
*update
= user
;
500 update
= isl_map_copy(update
);
502 dim
= isl_map_get_space(access
);
503 dim
= isl_space_domain(dim
);
504 if (!isl_space_is_wrapping(dim
))
508 dim
= isl_space_unwrap(dim
);
509 dim
= isl_space_range(dim
);
510 dim
= isl_space_map_from_set(dim
);
511 id
= isl_map_identity(dim
);
512 update
= isl_map_product(update
, id
);
515 return isl_map_apply_domain(access
, update
);
518 /* Modify all access relations in "expr" based on the given iteration space
521 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
522 __isl_take isl_map
*update
)
524 expr
= pet_expr_foreach_access(expr
, &update_domain
, update
);
525 isl_map_free(update
);
529 /* Construct a pet_stmt with given line number and statement
530 * number from a pet_expr.
531 * The initial iteration domain is the zero-dimensional universe.
532 * The name of the domain is given by "label" if it is non-NULL.
533 * Otherwise, the name is constructed as S_<id>.
534 * The domains of all access relations are modified to refer
535 * to the statement iteration domain.
537 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
538 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
540 struct pet_stmt
*stmt
;
550 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
554 dim
= isl_space_set_alloc(ctx
, 0, 0);
556 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
558 snprintf(name
, sizeof(name
), "S_%d", id
);
559 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
561 dom
= isl_set_universe(isl_space_copy(dim
));
562 sched
= isl_map_from_domain(isl_set_copy(dom
));
564 dim
= isl_space_from_range(dim
);
565 add_name
= isl_map_universe(dim
);
566 expr
= expr_update_domain(expr
, add_name
);
570 stmt
->schedule
= sched
;
573 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
574 return pet_stmt_free(stmt
);
579 return pet_expr_free(expr
);
582 void *pet_stmt_free(struct pet_stmt
*stmt
)
589 isl_set_free(stmt
->domain
);
590 isl_map_free(stmt
->schedule
);
591 pet_expr_free(stmt
->body
);
593 for (i
= 0; i
< stmt
->n_arg
; ++i
)
594 pet_expr_free(stmt
->args
[i
]);
601 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
608 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
609 fprintf(stderr
, "%*s", indent
, "");
610 isl_set_dump(stmt
->domain
);
611 fprintf(stderr
, "%*s", indent
, "");
612 isl_map_dump(stmt
->schedule
);
613 expr_dump(stmt
->body
, indent
);
614 for (i
= 0; i
< stmt
->n_arg
; ++i
)
615 expr_dump(stmt
->args
[i
], indent
+ 2);
618 void pet_stmt_dump(struct pet_stmt
*stmt
)
623 void *pet_array_free(struct pet_array
*array
)
628 isl_set_free(array
->context
);
629 isl_set_free(array
->extent
);
630 isl_set_free(array
->value_bounds
);
631 free(array
->element_type
);
637 void pet_array_dump(struct pet_array
*array
)
642 isl_set_dump(array
->context
);
643 isl_set_dump(array
->extent
);
644 isl_set_dump(array
->value_bounds
);
645 fprintf(stderr
, "%s %s\n", array
->element_type
,
646 array
->live_out
? "live-out" : "");
649 /* Construct a pet_scop with room for n statements.
651 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
654 struct pet_scop
*scop
;
656 scop
= isl_calloc_type(ctx
, struct pet_scop
);
660 space
= isl_space_params_alloc(ctx
, 0);
661 scop
->context
= isl_set_universe(isl_space_copy(space
));
662 scop
->context_value
= isl_set_universe(space
);
663 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
664 if (!scop
->context
|| !scop
->stmts
)
665 return pet_scop_free(scop
);
672 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
674 return scop_alloc(ctx
, 0);
677 /* Update "context" with respect to the valid parameter values for "access".
679 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
680 __isl_take isl_set
*context
)
682 context
= isl_set_intersect(context
,
683 isl_map_params(isl_map_copy(access
)));
687 /* Update "context" with respect to the valid parameter values for "expr".
689 * If "expr" represents a ternary operator, then a parameter value
690 * needs to be valid for the condition and for at least one of the
691 * remaining two arguments.
692 * If the condition is an affine expression, then we can be a bit more specific.
693 * The parameter then has to be valid for the second argument for
694 * non-zero accesses and valid for the third argument for zero accesses.
696 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
697 __isl_take isl_set
*context
)
701 if (expr
->type
== pet_expr_ternary
) {
703 isl_set
*context1
, *context2
;
705 is_aff
= pet_expr_is_affine(expr
->args
[0]);
709 context
= expr_extract_context(expr
->args
[0], context
);
710 context1
= expr_extract_context(expr
->args
[1],
711 isl_set_copy(context
));
712 context2
= expr_extract_context(expr
->args
[2], context
);
718 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
719 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
720 zero_set
= isl_map_params(access
);
721 context1
= isl_set_subtract(context1
,
722 isl_set_copy(zero_set
));
723 context2
= isl_set_intersect(context2
, zero_set
);
726 context
= isl_set_union(context1
, context2
);
727 context
= isl_set_coalesce(context
);
732 for (i
= 0; i
< expr
->n_arg
; ++i
)
733 context
= expr_extract_context(expr
->args
[i
], context
);
735 if (expr
->type
== pet_expr_access
)
736 context
= access_extract_context(expr
->acc
.access
, context
);
740 isl_set_free(context
);
744 /* Update "context" with respect to the valid parameter values for "stmt".
746 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
747 __isl_take isl_set
*context
)
751 for (i
= 0; i
< stmt
->n_arg
; ++i
)
752 context
= expr_extract_context(stmt
->args
[i
], context
);
754 context
= expr_extract_context(stmt
->body
, context
);
759 /* Construct a pet_scop that contains the given pet_stmt.
761 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
763 struct pet_scop
*scop
;
768 scop
= scop_alloc(ctx
, 1);
770 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
774 scop
->stmts
[0] = stmt
;
783 /* Construct a pet_scop that contains the arrays and the statements
784 * in "scop1" and "scop2".
786 struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
787 struct pet_scop
*scop2
)
790 struct pet_scop
*scop
;
792 if (!scop1
|| !scop2
)
795 if (scop1
->n_stmt
== 0) {
796 pet_scop_free(scop1
);
800 if (scop2
->n_stmt
== 0) {
801 pet_scop_free(scop2
);
805 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
809 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
810 scop1
->n_array
+ scop2
->n_array
);
813 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
815 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
816 scop
->stmts
[i
] = scop1
->stmts
[i
];
817 scop1
->stmts
[i
] = NULL
;
820 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
821 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
822 scop2
->stmts
[i
] = NULL
;
825 for (i
= 0; i
< scop1
->n_array
; ++i
) {
826 scop
->arrays
[i
] = scop1
->arrays
[i
];
827 scop1
->arrays
[i
] = NULL
;
830 for (i
= 0; i
< scop2
->n_array
; ++i
) {
831 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
832 scop2
->arrays
[i
] = NULL
;
835 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
836 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
838 pet_scop_free(scop1
);
839 pet_scop_free(scop2
);
842 pet_scop_free(scop1
);
843 pet_scop_free(scop2
);
847 void *pet_scop_free(struct pet_scop
*scop
)
853 isl_set_free(scop
->context
);
854 isl_set_free(scop
->context_value
);
856 for (i
= 0; i
< scop
->n_array
; ++i
)
857 pet_array_free(scop
->arrays
[i
]);
860 for (i
= 0; i
< scop
->n_stmt
; ++i
)
861 pet_stmt_free(scop
->stmts
[i
]);
867 void pet_scop_dump(struct pet_scop
*scop
)
874 isl_set_dump(scop
->context
);
875 isl_set_dump(scop
->context_value
);
876 for (i
= 0; i
< scop
->n_array
; ++i
)
877 pet_array_dump(scop
->arrays
[i
]);
878 for (i
= 0; i
< scop
->n_stmt
; ++i
)
879 pet_stmt_dump(scop
->stmts
[i
]);
882 /* Return 1 if the two pet_arrays are equivalent.
884 * We don't compare element_size as this may be target dependent.
886 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
888 if (!array1
|| !array2
)
891 if (!isl_set_is_equal(array1
->context
, array2
->context
))
893 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
895 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
897 if (array1
->value_bounds
&&
898 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
900 if (strcmp(array1
->element_type
, array2
->element_type
))
902 if (array1
->live_out
!= array2
->live_out
)
904 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
910 /* Return 1 if the two pet_stmts are equivalent.
912 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
916 if (!stmt1
|| !stmt2
)
919 if (stmt1
->line
!= stmt2
->line
)
921 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
923 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
925 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
927 if (stmt1
->n_arg
!= stmt2
->n_arg
)
929 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
930 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
937 /* Return 1 if the two pet_scops are equivalent.
939 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
943 if (!scop1
|| !scop2
)
946 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
948 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
951 if (scop1
->n_array
!= scop2
->n_array
)
953 for (i
= 0; i
< scop1
->n_array
; ++i
)
954 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
957 if (scop1
->n_stmt
!= scop2
->n_stmt
)
959 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
960 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
966 /* Prefix the schedule of "stmt" with an extra dimension with constant
969 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
974 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
975 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
977 return pet_stmt_free(stmt
);
982 /* Prefix the schedules of all statements in "scop" with an extra
983 * dimension with constant value "pos".
985 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
992 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
993 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
995 return pet_scop_free(scop
);
1001 /* Given a set with a parameter at "param_pos" that refers to the
1002 * iterator, "move" the iterator to the first set dimension.
1003 * That is, essentially equate the parameter to the first set dimension
1004 * and then project it out.
1006 * The first set dimension may however refer to a virtual iterator,
1007 * while the parameter refers to the "real" iterator.
1008 * We therefore need to take into account the mapping "iv_map", which
1009 * maps the virtual iterator to the real iterator.
1010 * In particular, we equate the set dimension to the input of the map
1011 * and the parameter to the output of the map and then project out
1012 * everything we don't need anymore.
1014 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1015 int param_pos
, __isl_take isl_map
*iv_map
)
1018 map
= isl_map_from_domain(set
);
1019 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1020 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1021 iv_map
= isl_map_align_params(iv_map
, isl_map_get_space(map
));
1022 map
= isl_map_apply_range(map
, iv_map
);
1023 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1024 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1025 return isl_map_domain(map
);
1028 /* Data used in embed_access.
1029 * extend adds an iterator to the iteration domain
1030 * iv_map maps the virtual iterator to the real iterator
1031 * var_id represents the induction variable of the corresponding loop
1033 struct pet_embed_access
{
1039 /* Embed the access relation in an extra outer loop.
1041 * We first update the iteration domain to insert the extra dimension.
1043 * If the access refers to the induction variable, then it is
1044 * turned into an access to the set of integers with index (and value)
1045 * equal to the induction variable.
1047 * If the induction variable appears in the constraints (as a parameter),
1048 * then the parameter is equated to the newly introduced iteration
1049 * domain dimension and subsequently projected out.
1051 * Similarly, if the accessed array is a virtual array (with user
1052 * pointer equal to NULL), as created by create_test_access,
1053 * then it is extended along with the domain of the access.
1055 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
1058 struct pet_embed_access
*data
= user
;
1059 isl_id
*array_id
= NULL
;
1062 access
= update_domain(access
, data
->extend
);
1064 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1065 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1066 if (array_id
== data
->var_id
||
1067 (array_id
&& !isl_id_get_user(array_id
))) {
1068 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1069 access
= isl_map_equate(access
,
1070 isl_dim_in
, 0, isl_dim_out
, 0);
1071 if (array_id
== data
->var_id
)
1072 access
= isl_map_apply_range(access
,
1073 isl_map_copy(data
->iv_map
));
1075 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1076 isl_id_copy(array_id
));
1078 isl_id_free(array_id
);
1080 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1082 isl_set
*set
= isl_map_wrap(access
);
1083 set
= internalize_iv(set
, pos
, isl_map_copy(data
->iv_map
));
1084 access
= isl_set_unwrap(set
);
1086 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1087 isl_id_copy(data
->var_id
));
1092 /* Embed all access relations in "expr" in an extra loop.
1093 * "extend" inserts an outer loop iterator in the iteration domains.
1094 * "iv_map" maps the virtual iterator to the real iterator
1095 * "var_id" represents the induction variable.
1097 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
1098 __isl_take isl_map
*extend
, __isl_take isl_map
*iv_map
,
1099 __isl_keep isl_id
*var_id
)
1101 struct pet_embed_access data
=
1102 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1104 expr
= pet_expr_foreach_access(expr
, &embed_access
, &data
);
1105 isl_map_free(iv_map
);
1106 isl_map_free(extend
);
1110 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1111 * "dom" and schedule "sched". "var_id" represents the induction variable
1112 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1113 * That is, it maps the iterator used in "dom" and the domain of "sched"
1114 * to the iterator that some of the parameters in "stmt" may refer to.
1116 * The iteration domain and schedule of the statement are updated
1117 * according to the iteration domain and schedule of the new loop.
1118 * If stmt->domain is a wrapped map, then the iteration domain
1119 * is the domain of this map, so we need to be careful to adjust
1122 * If the induction variable appears in the constraints (as a parameter)
1123 * of the current iteration domain or the schedule of the statement,
1124 * then the parameter is equated to the newly introduced iteration
1125 * domain dimension and subsequently projected out.
1127 * Finally, all access relations are updated based on the extra loop.
1129 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1130 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1131 __isl_take isl_map
*iv_map
, __isl_take isl_id
*var_id
)
1142 if (isl_set_is_wrapping(stmt
->domain
)) {
1147 map
= isl_set_unwrap(stmt
->domain
);
1148 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1149 ran_dim
= isl_space_range(isl_map_get_space(map
));
1150 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1151 isl_set_universe(ran_dim
));
1152 map
= isl_map_flat_domain_product(ext
, map
);
1153 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1154 isl_id_copy(stmt_id
));
1155 dim
= isl_space_domain(isl_map_get_space(map
));
1156 stmt
->domain
= isl_map_wrap(map
);
1158 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1159 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1161 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1162 isl_id_copy(stmt_id
));
1163 dim
= isl_set_get_space(stmt
->domain
);
1166 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1168 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1169 isl_map_copy(iv_map
));
1171 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1172 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1173 isl_dim_in
, stmt_id
);
1175 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1177 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1178 set
= internalize_iv(set
, pos
, isl_map_copy(iv_map
));
1179 stmt
->schedule
= isl_set_unwrap(set
);
1182 dim
= isl_space_map_from_set(dim
);
1183 extend
= isl_map_identity(dim
);
1184 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
1185 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
1186 isl_map_get_tuple_id(extend
, isl_dim_out
));
1187 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1188 stmt
->args
[i
] = expr_embed(stmt
->args
[i
], isl_map_copy(extend
),
1189 isl_map_copy(iv_map
), var_id
);
1190 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1193 isl_id_free(var_id
);
1195 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1197 return pet_stmt_free(stmt
);
1198 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1199 return pet_stmt_free(stmt
);
1203 isl_map_free(sched
);
1204 isl_map_free(iv_map
);
1205 isl_id_free(var_id
);
1209 /* Embed the given pet_array in an extra outer loop with iteration domain
1211 * This embedding only has an effect on virtual arrays (those with
1212 * user pointer equal to NULL), which need to be extended along with
1213 * the iteration domain.
1215 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1216 __isl_take isl_set
*dom
)
1218 isl_id
*array_id
= NULL
;
1223 if (isl_set_has_tuple_id(array
->extent
))
1224 array_id
= isl_set_get_tuple_id(array
->extent
);
1226 if (array_id
&& !isl_id_get_user(array_id
)) {
1227 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1228 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1231 isl_id_free(array_id
);
1240 /* Project out all unnamed parameters from "set" and return the result.
1242 static __isl_give isl_set
*set_project_out_unnamed_params(
1243 __isl_take isl_set
*set
)
1247 n
= isl_set_dim(set
, isl_dim_param
);
1248 for (i
= n
- 1; i
>= 0; --i
) {
1249 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1251 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1257 /* Update the context with respect to an embedding into a loop
1258 * with iteration domain "dom" and induction variable "id".
1259 * "iv_map" maps a possibly virtual iterator (used in "dom")
1260 * to the real iterator (parameter "id").
1262 * If the current context is independent of "id", we don't need
1264 * Otherwise, a parameter value is invalid for the embedding if
1265 * any of the corresponding iterator values is invalid.
1266 * That is, a parameter value is valid only if all the corresponding
1267 * iterator values are valid.
1268 * We therefore compute the set of parameters
1270 * forall i in dom : valid (i)
1274 * not exists i in dom : not valid(i)
1278 * not exists i in dom \ valid(i)
1280 * Before we subtract valid(i) from dom, we first need to map
1281 * the real iterator to the virtual iterator.
1283 * If there are any unnamed parameters in "dom", then we consider
1284 * a parameter value to be valid if it is valid for any value of those
1285 * unnamed parameters. They are therefore projected out at the end.
1287 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1288 __isl_keep isl_set
*dom
, __isl_keep isl_map
*iv_map
,
1289 __isl_keep isl_id
*id
)
1293 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1297 context
= isl_set_from_params(context
);
1298 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1299 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1300 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1301 context
= isl_set_apply(context
, isl_map_reverse(isl_map_copy(iv_map
)));
1302 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1303 context
= isl_set_params(context
);
1304 context
= isl_set_complement(context
);
1305 context
= set_project_out_unnamed_params(context
);
1309 /* Embed all statements and arrays in "scop" in an extra outer loop
1310 * with iteration domain "dom" and schedule "sched".
1311 * "id" represents the induction variable of the loop.
1312 * "iv_map" maps a possibly virtual iterator to the real iterator.
1313 * That is, it maps the iterator used in "dom" and the domain of "sched"
1314 * to the iterator that some of the parameters in "scop" may refer to.
1316 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1317 __isl_take isl_map
*sched
, __isl_take isl_map
*iv_map
,
1318 __isl_take isl_id
*id
)
1325 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1329 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1330 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1331 isl_set_copy(dom
), isl_map_copy(sched
),
1332 isl_map_copy(iv_map
), isl_id_copy(id
));
1333 if (!scop
->stmts
[i
])
1337 for (i
= 0; i
< scop
->n_array
; ++i
) {
1338 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1340 if (!scop
->arrays
[i
])
1345 isl_map_free(sched
);
1346 isl_map_free(iv_map
);
1351 isl_map_free(sched
);
1352 isl_map_free(iv_map
);
1354 return pet_scop_free(scop
);
1357 /* Add extra conditions on the parameters to iteration domain of "stmt".
1359 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1360 __isl_take isl_set
*cond
)
1365 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1370 return pet_stmt_free(stmt
);
1373 /* Add extra conditions on the parameters to all iteration domains.
1375 * A parameter value is valid for the result if it was valid
1376 * for the original scop and satisfies "cond" or if it does
1377 * not satisfy "cond" as in this case the scop is not executed
1378 * and the original constraints on the parameters are irrelevant.
1380 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1381 __isl_take isl_set
*cond
)
1388 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1389 scop
->context
= isl_set_union(scop
->context
,
1390 isl_set_complement(isl_set_copy(cond
)));
1391 scop
->context
= isl_set_coalesce(scop
->context
);
1392 scop
->context
= set_project_out_unnamed_params(scop
->context
);
1396 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1397 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1398 isl_set_copy(cond
));
1399 if (!scop
->stmts
[i
])
1407 return pet_scop_free(scop
);
1410 /* Make the statement "stmt" depend on the value of "test"
1411 * being equal to "satisfied" by adjusting stmt->domain.
1413 * The domain of "test" corresponds to the (zero or more) outer dimensions
1414 * of the iteration domain.
1416 * We insert an argument corresponding to a read to "test"
1417 * from the iteration domain of "stmt" in front of the list of arguments.
1418 * We also insert a corresponding output dimension in the wrapped
1419 * map contained in stmt->domain, with value set to "satisfied".
1421 static struct pet_stmt
*stmt_filter(struct pet_stmt
*stmt
,
1422 __isl_take isl_map
*test
, int satisfied
)
1427 isl_map
*map
, *add_dom
;
1434 if (isl_set_is_wrapping(stmt
->domain
))
1435 map
= isl_set_unwrap(stmt
->domain
);
1437 map
= isl_map_from_domain(stmt
->domain
);
1438 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, 1);
1439 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1440 map
= isl_map_set_dim_id(map
, isl_dim_out
, 0, id
);
1441 map
= isl_map_fix_si(map
, isl_dim_out
, 0, satisfied
);
1442 dom
= isl_set_universe(isl_space_domain(isl_map_get_space(map
)));
1443 n_test_dom
= isl_map_dim(test
, isl_dim_in
);
1444 add_dom
= isl_map_from_range(dom
);
1445 add_dom
= isl_map_add_dims(add_dom
, isl_dim_in
, n_test_dom
);
1446 for (i
= 0; i
< n_test_dom
; ++i
)
1447 add_dom
= isl_map_equate(add_dom
, isl_dim_in
, i
,
1449 test
= isl_map_apply_domain(test
, add_dom
);
1451 stmt
->domain
= isl_map_wrap(map
);
1453 ctx
= isl_map_get_ctx(test
);
1455 stmt
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1459 struct pet_expr
**args
;
1460 args
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + stmt
->n_arg
);
1463 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1464 args
[1 + i
] = stmt
->args
[i
];
1469 stmt
->args
[0] = pet_expr_from_access(isl_map_copy(test
));
1477 return pet_stmt_free(stmt
);
1480 /* Make all statements in "scop" depend on the value of "test"
1481 * being equal to "satisfied" by adjusting their domains.
1483 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
1484 __isl_take isl_map
*test
, int satisfied
)
1491 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1492 scop
->stmts
[i
] = stmt_filter(scop
->stmts
[i
],
1493 isl_map_copy(test
), satisfied
);
1494 if (!scop
->stmts
[i
])
1502 return pet_scop_free(scop
);
1505 /* Add all parameters in "expr" to "dim" and return the result.
1507 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
1508 __isl_take isl_space
*dim
)
1514 for (i
= 0; i
< expr
->n_arg
; ++i
)
1516 dim
= expr_collect_params(expr
->args
[i
], dim
);
1518 if (expr
->type
== pet_expr_access
)
1519 dim
= isl_space_align_params(dim
,
1520 isl_map_get_space(expr
->acc
.access
));
1524 isl_space_free(dim
);
1525 return pet_expr_free(expr
);
1528 /* Add all parameters in "stmt" to "dim" and return the result.
1530 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
1531 __isl_take isl_space
*dim
)
1536 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
1537 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
1538 dim
= expr_collect_params(stmt
->body
, dim
);
1542 isl_space_free(dim
);
1543 return pet_stmt_free(stmt
);
1546 /* Add all parameters in "array" to "dim" and return the result.
1548 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
1549 __isl_take isl_space
*dim
)
1554 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
1555 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
1559 isl_space_free(dim
);
1560 return pet_array_free(array
);
1563 /* Add all parameters in "scop" to "dim" and return the result.
1565 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
1566 __isl_take isl_space
*dim
)
1573 for (i
= 0; i
< scop
->n_array
; ++i
)
1574 dim
= array_collect_params(scop
->arrays
[i
], dim
);
1576 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1577 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
1581 isl_space_free(dim
);
1582 return pet_scop_free(scop
);
1585 /* Add all parameters in "dim" to all access relations in "expr".
1587 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
1588 __isl_take isl_space
*dim
)
1595 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1597 expr_propagate_params(expr
->args
[i
],
1598 isl_space_copy(dim
));
1603 if (expr
->type
== pet_expr_access
) {
1604 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1605 isl_space_copy(dim
));
1606 if (!expr
->acc
.access
)
1610 isl_space_free(dim
);
1613 isl_space_free(dim
);
1614 return pet_expr_free(expr
);
1617 /* Add all parameters in "dim" to the domain, schedule and
1618 * all access relations in "stmt".
1620 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
1621 __isl_take isl_space
*dim
)
1626 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
1627 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
1628 isl_space_copy(dim
));
1629 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
1631 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1634 isl_space_free(dim
);
1637 isl_space_free(dim
);
1638 return pet_stmt_free(stmt
);
1641 /* Add all parameters in "dim" to "array".
1643 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
1644 __isl_take isl_space
*dim
)
1649 array
->context
= isl_set_align_params(array
->context
,
1650 isl_space_copy(dim
));
1651 array
->extent
= isl_set_align_params(array
->extent
,
1652 isl_space_copy(dim
));
1653 if (array
->value_bounds
) {
1654 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
1655 isl_space_copy(dim
));
1656 if (!array
->value_bounds
)
1660 if (!array
->context
|| !array
->extent
)
1663 isl_space_free(dim
);
1666 isl_space_free(dim
);
1667 return pet_array_free(array
);
1670 /* Add all parameters in "dim" to "scop".
1672 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
1673 __isl_take isl_space
*dim
)
1680 for (i
= 0; i
< scop
->n_array
; ++i
) {
1681 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
1682 isl_space_copy(dim
));
1683 if (!scop
->arrays
[i
])
1687 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1688 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
1689 isl_space_copy(dim
));
1690 if (!scop
->stmts
[i
])
1694 isl_space_free(dim
);
1697 isl_space_free(dim
);
1698 return pet_scop_free(scop
);
1701 /* Update all isl_sets and isl_maps in "scop" such that they all
1702 * have the same parameters.
1704 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
1711 dim
= isl_set_get_space(scop
->context
);
1712 dim
= scop_collect_params(scop
, dim
);
1714 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
1715 scop
= scop_propagate_params(scop
, dim
);
1720 /* Check if the given access relation accesses a (0D) array that corresponds
1721 * to one of the parameters in "dim". If so, replace the array access
1722 * by an access to the set of integers with as index (and value)
1725 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1726 __isl_take isl_space
*dim
)
1728 isl_id
*array_id
= NULL
;
1731 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1732 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1733 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
1735 isl_space_free(dim
);
1738 isl_id_free(array_id
);
1742 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1744 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1745 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1748 isl_id_free(array_id
);
1750 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1751 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1756 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1757 * in "dim" by a value equal to the corresponding parameter.
1759 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
1760 __isl_take isl_space
*dim
)
1767 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1769 expr_detect_parameter_accesses(expr
->args
[i
],
1770 isl_space_copy(dim
));
1775 if (expr
->type
== pet_expr_access
) {
1776 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1777 isl_space_copy(dim
));
1778 if (!expr
->acc
.access
)
1782 isl_space_free(dim
);
1785 isl_space_free(dim
);
1786 return pet_expr_free(expr
);
1789 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1790 * in "dim" by a value equal to the corresponding parameter.
1792 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
1793 __isl_take isl_space
*dim
)
1798 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
1799 isl_space_copy(dim
));
1801 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1804 isl_space_free(dim
);
1807 isl_space_free(dim
);
1808 return pet_stmt_free(stmt
);
1811 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1812 * in "dim" by a value equal to the corresponding parameter.
1814 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
1815 __isl_take isl_space
*dim
)
1822 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1823 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
1824 isl_space_copy(dim
));
1825 if (!scop
->stmts
[i
])
1829 isl_space_free(dim
);
1832 isl_space_free(dim
);
1833 return pet_scop_free(scop
);
1836 /* Replace all accesses to (0D) arrays that correspond to any of
1837 * the parameters used in "scop" by a value equal
1838 * to the corresponding parameter.
1840 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
1847 dim
= isl_set_get_space(scop
->context
);
1848 dim
= scop_collect_params(scop
, dim
);
1850 scop
= scop_detect_parameter_accesses(scop
, dim
);
1855 /* Add all read access relations (if "read" is set) and/or all write
1856 * access relations (if "write" is set) to "accesses" and return the result.
1858 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
1859 int read
, int write
, __isl_take isl_union_map
*accesses
)
1868 for (i
= 0; i
< expr
->n_arg
; ++i
)
1869 accesses
= expr_collect_accesses(expr
->args
[i
],
1870 read
, write
, accesses
);
1872 if (expr
->type
== pet_expr_access
&&
1873 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
1874 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
1875 accesses
= isl_union_map_add_map(accesses
,
1876 isl_map_copy(expr
->acc
.access
));
1881 /* Collect and return all read access relations (if "read" is set)
1882 * and/or all write * access relations (if "write" is set) in "stmt".
1884 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
1885 int read
, int write
, __isl_take isl_space
*dim
)
1887 isl_union_map
*accesses
;
1892 accesses
= isl_union_map_empty(dim
);
1893 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
1894 accesses
= isl_union_map_intersect_domain(accesses
,
1895 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
1900 /* Collect and return all read access relations (if "read" is set)
1901 * and/or all write * access relations (if "write" is set) in "scop".
1903 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
1904 int read
, int write
)
1907 isl_union_map
*accesses
;
1912 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1914 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1915 isl_union_map
*accesses_i
;
1916 isl_space
*dim
= isl_set_get_space(scop
->context
);
1917 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
1919 accesses
= isl_union_map_union(accesses
, accesses_i
);
1925 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
1927 return scop_collect_accesses(scop
, 1, 0);
1930 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
1932 return scop_collect_accesses(scop
, 0, 1);
1935 /* Collect and return the union of iteration domains in "scop".
1937 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
1941 isl_union_set
*domain
;
1946 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
1948 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1949 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
1950 domain
= isl_union_set_add_set(domain
, domain_i
);
1956 /* Collect and return the schedules of the statements in "scop".
1957 * The range is normalized to the maximal number of scheduling
1960 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
1963 isl_map
*schedule_i
;
1964 isl_union_map
*schedule
;
1965 int depth
, max_depth
= 0;
1970 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1972 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1973 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
1974 if (depth
> max_depth
)
1978 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1979 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
1980 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
1981 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
1983 for (j
= depth
; j
< max_depth
; ++j
)
1984 schedule_i
= isl_map_fix_si(schedule_i
,
1986 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
1992 /* Does expression "expr" write to "id"?
1994 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
1999 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2000 int writes
= expr_writes(expr
->args
[i
], id
);
2001 if (writes
< 0 || writes
)
2005 if (expr
->type
!= pet_expr_access
)
2007 if (!expr
->acc
.write
)
2009 if (!isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
))
2012 write_id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
2013 isl_id_free(write_id
);
2018 return write_id
== id
;
2021 /* Does statement "stmt" write to "id"?
2023 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
2025 return expr_writes(stmt
->body
, id
);
2028 /* Is there any write access in "scop" that accesses "id"?
2030 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
2037 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2038 int writes
= stmt_writes(scop
->stmts
[i
], id
);
2039 if (writes
< 0 || writes
)
2046 /* Reset the user pointer on all parameter ids in "set".
2048 static __isl_give isl_set
*set_anonymize(__isl_take isl_set
*set
)
2052 n
= isl_set_dim(set
, isl_dim_param
);
2053 for (i
= 0; i
< n
; ++i
) {
2054 isl_id
*id
= isl_set_get_dim_id(set
, isl_dim_param
, i
);
2055 const char *name
= isl_id_get_name(id
);
2056 set
= isl_set_set_dim_name(set
, isl_dim_param
, i
, name
);
2063 /* Reset the user pointer on all parameter ids in "map".
2065 static __isl_give isl_map
*map_anonymize(__isl_take isl_map
*map
)
2069 n
= isl_map_dim(map
, isl_dim_param
);
2070 for (i
= 0; i
< n
; ++i
) {
2071 isl_id
*id
= isl_map_get_dim_id(map
, isl_dim_param
, i
);
2072 const char *name
= isl_id_get_name(id
);
2073 map
= isl_map_set_dim_name(map
, isl_dim_param
, i
, name
);
2080 /* Reset the user pointer on all parameter ids in "array".
2082 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2087 array
->context
= set_anonymize(array
->context
);
2088 array
->extent
= set_anonymize(array
->extent
);
2089 if (!array
->context
|| !array
->extent
)
2090 return pet_array_free(array
);
2095 /* Reset the user pointer on all parameter ids in "access".
2097 static __isl_give isl_map
*access_anonymize(__isl_take isl_map
*access
,
2100 access
= map_anonymize(access
);
2105 /* Reset the user pointer on all parameter ids in "stmt".
2107 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2116 stmt
->domain
= set_anonymize(stmt
->domain
);
2117 stmt
->schedule
= map_anonymize(stmt
->schedule
);
2118 if (!stmt
->domain
|| !stmt
->schedule
)
2119 return pet_stmt_free(stmt
);
2121 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2122 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
2123 &access_anonymize
, NULL
);
2125 return pet_stmt_free(stmt
);
2128 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
2129 &access_anonymize
, NULL
);
2131 return pet_stmt_free(stmt
);
2136 /* Reset the user pointer on all parameter ids in "scop".
2138 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2145 scop
->context
= set_anonymize(scop
->context
);
2146 scop
->context_value
= set_anonymize(scop
->context_value
);
2147 if (!scop
->context
|| !scop
->context_value
)
2148 return pet_scop_free(scop
);
2150 for (i
= 0; i
< scop
->n_array
; ++i
) {
2151 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2152 if (!scop
->arrays
[i
])
2153 return pet_scop_free(scop
);
2156 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2157 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2158 if (!scop
->stmts
[i
])
2159 return pet_scop_free(scop
);
2165 /* Given a set "domain", return a wrapped relation with the given set
2166 * as domain and a range of dimension "n_arg", where each coordinate
2167 * is either unbounded or, if the corresponding element of args is of
2168 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2170 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
2171 unsigned n_arg
, struct pet_expr
**args
,
2172 __isl_keep isl_union_map
*value_bounds
)
2177 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2179 map
= isl_map_from_domain(domain
);
2180 space
= isl_map_get_space(map
);
2181 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2183 for (i
= 0; i
< n_arg
; ++i
) {
2185 struct pet_expr
*arg
= args
[i
];
2189 map_i
= isl_map_universe(isl_space_copy(space
));
2190 if (arg
->type
== pet_expr_access
) {
2192 id
= isl_map_get_tuple_id(arg
->acc
.access
, isl_dim_out
);
2193 space2
= isl_space_alloc(ctx
, 0, 0, 1);
2194 space2
= isl_space_set_tuple_id(space2
, isl_dim_in
, id
);
2195 vb
= isl_union_map_extract_map(value_bounds
, space2
);
2196 if (!isl_map_plain_is_empty(vb
))
2197 map_i
= isl_map_intersect_range(map_i
,
2202 map
= isl_map_flat_range_product(map
, map_i
);
2204 isl_space_free(space
);
2206 return isl_map_wrap(map
);
2209 /* Data used in access_gist() callback.
2211 struct pet_access_gist_data
{
2213 isl_union_map
*value_bounds
;
2216 /* Given an expression "expr" of type pet_expr_access, compute
2217 * the gist of the associated access relation with respect to
2218 * data->domain and the bounds on the values of the arguments
2219 * of the expression.
2221 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
2223 struct pet_access_gist_data
*data
= user
;
2226 domain
= isl_set_copy(data
->domain
);
2227 if (expr
->n_arg
> 0)
2228 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
2229 data
->value_bounds
);
2231 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
, domain
);
2232 if (!expr
->acc
.access
)
2233 return pet_expr_free(expr
);
2238 /* Compute the gist of the iteration domain and all access relations
2239 * of "stmt" based on the constraints on the parameters specified by "context"
2240 * and the constraints on the values of nested accesses specified
2241 * by "value_bounds".
2243 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2244 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2249 struct pet_access_gist_data data
;
2254 data
.domain
= isl_set_copy(stmt
->domain
);
2255 data
.value_bounds
= value_bounds
;
2256 if (stmt
->n_arg
> 0)
2257 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
2259 data
.domain
= isl_set_intersect_params(data
.domain
,
2260 isl_set_copy(context
));
2262 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2263 stmt
->args
[i
] = pet_expr_foreach_access_expr(stmt
->args
[i
],
2264 &access_gist
, &data
);
2269 stmt
->body
= pet_expr_foreach_access_expr(stmt
->body
,
2270 &access_gist
, &data
);
2274 isl_set_free(data
.domain
);
2276 space
= isl_set_get_space(stmt
->domain
);
2277 if (isl_space_is_wrapping(space
))
2278 space
= isl_space_domain(isl_space_unwrap(space
));
2279 domain
= isl_set_universe(space
);
2280 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2281 if (stmt
->n_arg
> 0)
2282 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
2284 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
2286 return pet_stmt_free(stmt
);
2290 isl_set_free(data
.domain
);
2291 return pet_stmt_free(stmt
);
2294 /* Compute the gist of the extent of the array
2295 * based on the constraints on the parameters specified by "context".
2297 static struct pet_array
*array_gist(struct pet_array
*array
,
2298 __isl_keep isl_set
*context
)
2303 array
->extent
= isl_set_gist_params(array
->extent
,
2304 isl_set_copy(context
));
2306 return pet_array_free(array
);
2311 /* Compute the gist of all sets and relations in "scop"
2312 * based on the constraints on the parameters specified by "scop->context"
2313 * and the constraints on the values of nested accesses specified
2314 * by "value_bounds".
2316 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
2317 __isl_keep isl_union_map
*value_bounds
)
2324 scop
->context
= isl_set_coalesce(scop
->context
);
2326 return pet_scop_free(scop
);
2328 for (i
= 0; i
< scop
->n_array
; ++i
) {
2329 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
2330 if (!scop
->arrays
[i
])
2331 return pet_scop_free(scop
);
2334 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2335 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
2337 if (!scop
->stmts
[i
])
2338 return pet_scop_free(scop
);
2344 /* Intersect the context of "scop" with "context".
2345 * To ensure that we don't introduce any unnamed parameters in
2346 * the context of "scop", we first remove the unnamed parameters
2349 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
2350 __isl_take isl_set
*context
)
2355 context
= set_project_out_unnamed_params(context
);
2356 scop
->context
= isl_set_intersect(scop
->context
, context
);
2358 return pet_scop_free(scop
);
2362 isl_set_free(context
);
2363 return pet_scop_free(scop
);
2366 /* Drop the current context of "scop". That is, replace the context
2367 * by a universal set.
2369 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
2376 space
= isl_set_get_space(scop
->context
);
2377 isl_set_free(scop
->context
);
2378 scop
->context
= isl_set_universe(space
);
2380 return pet_scop_free(scop
);