1 #include <isl/constraint.h>
2 #include <isl/union_set.h>
6 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
8 static char *type_str
[] = {
9 [pet_expr_access
] = "access",
10 [pet_expr_call
] = "call",
11 [pet_expr_double
] = "double",
12 [pet_expr_unary
] = "unary",
13 [pet_expr_binary
] = "binary",
14 [pet_expr_ternary
] = "ternary"
17 static char *op_str
[] = {
18 [pet_op_add_assign
] = "+=",
19 [pet_op_sub_assign
] = "-=",
20 [pet_op_mul_assign
] = "*=",
21 [pet_op_div_assign
] = "/=",
22 [pet_op_assign
] = "=",
33 const char *pet_op_str(enum pet_op_type op
)
38 const char *pet_type_str(enum pet_expr_type type
)
40 return type_str
[type
];
43 enum pet_op_type
pet_str_op(const char *str
)
47 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
48 if (!strcmp(op_str
[i
], str
))
54 enum pet_expr_type
pet_str_type(const char *str
)
58 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
59 if (!strcmp(type_str
[i
], str
))
65 /* Construct a pet_expr from an access relation.
66 * By default, it is considered to be a read access.
68 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
70 isl_ctx
*ctx
= isl_map_get_ctx(access
);
71 struct pet_expr
*expr
;
75 expr
= isl_calloc_type(ctx
, struct pet_expr
);
79 expr
->type
= pet_expr_access
;
80 expr
->acc
.access
= access
;
90 /* Construct a unary pet_expr that performs "op" on "arg".
92 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
95 struct pet_expr
*expr
;
99 expr
= isl_alloc_type(ctx
, struct pet_expr
);
103 expr
->type
= pet_expr_unary
;
106 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
109 expr
->args
[pet_un_arg
] = arg
;
117 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
119 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
120 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
122 struct pet_expr
*expr
;
126 expr
= isl_alloc_type(ctx
, struct pet_expr
);
130 expr
->type
= pet_expr_binary
;
133 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
136 expr
->args
[pet_bin_lhs
] = lhs
;
137 expr
->args
[pet_bin_rhs
] = rhs
;
146 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
148 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
149 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
151 struct pet_expr
*expr
;
153 if (!cond
|| !lhs
|| !rhs
)
155 expr
= isl_alloc_type(ctx
, struct pet_expr
);
159 expr
->type
= pet_expr_ternary
;
161 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
164 expr
->args
[pet_ter_cond
] = cond
;
165 expr
->args
[pet_ter_true
] = lhs
;
166 expr
->args
[pet_ter_false
] = rhs
;
176 /* Construct a call pet_expr that calls function "name" with "n_arg"
177 * arguments. The caller is responsible for filling in the arguments.
179 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
182 struct pet_expr
*expr
;
184 expr
= isl_alloc_type(ctx
, struct pet_expr
);
188 expr
->type
= pet_expr_call
;
190 expr
->name
= strdup(name
);
191 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
192 if (!expr
->name
|| !expr
->args
)
193 return pet_expr_free(expr
);
198 /* Construct a pet_expr that represents the double "d".
200 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double d
)
202 struct pet_expr
*expr
;
204 expr
= isl_calloc_type(ctx
, struct pet_expr
);
208 expr
->type
= pet_expr_double
;
214 void *pet_expr_free(struct pet_expr
*expr
)
221 for (i
= 0; i
< expr
->n_arg
; ++i
)
222 pet_expr_free(expr
->args
[i
]);
225 switch (expr
->type
) {
226 case pet_expr_access
:
227 isl_map_free(expr
->acc
.access
);
232 case pet_expr_double
:
234 case pet_expr_binary
:
235 case pet_expr_ternary
:
243 static void expr_dump(struct pet_expr
*expr
, int indent
)
250 fprintf(stderr
, "%*s", indent
, "");
252 switch (expr
->type
) {
253 case pet_expr_double
:
254 fprintf(stderr
, "%g\n", expr
->d
);
256 case pet_expr_access
:
257 isl_map_dump(expr
->acc
.access
);
258 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
260 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
261 "", expr
->acc
.write
);
262 for (i
= 0; i
< expr
->n_arg
; ++i
)
263 expr_dump(expr
->args
[i
], indent
+ 2);
266 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
267 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
269 case pet_expr_binary
:
270 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
271 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
272 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
274 case pet_expr_ternary
:
275 fprintf(stderr
, "?:\n");
276 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
277 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
278 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
281 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
282 for (i
= 0; i
< expr
->n_arg
; ++i
)
283 expr_dump(expr
->args
[i
], indent
+ 2);
288 void pet_expr_dump(struct pet_expr
*expr
)
293 /* Return 1 if the two pet_exprs are equivalent.
295 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
299 if (!expr1
|| !expr2
)
302 if (expr1
->type
!= expr2
->type
)
304 if (expr1
->n_arg
!= expr2
->n_arg
)
306 for (i
= 0; i
< expr1
->n_arg
; ++i
)
307 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
309 switch (expr1
->type
) {
310 case pet_expr_double
:
311 if (expr1
->d
!= expr2
->d
)
314 case pet_expr_access
:
315 if (expr1
->acc
.read
!= expr2
->acc
.read
)
317 if (expr1
->acc
.write
!= expr2
->acc
.write
)
319 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
323 case pet_expr_binary
:
324 case pet_expr_ternary
:
325 if (expr1
->op
!= expr2
->op
)
329 if (strcmp(expr1
->name
, expr2
->name
))
337 /* Add extra conditions on the parameters to all access relations in "expr".
339 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
340 __isl_take isl_set
*cond
)
347 for (i
= 0; i
< expr
->n_arg
; ++i
) {
348 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
354 if (expr
->type
== pet_expr_access
) {
355 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
357 if (!expr
->acc
.access
)
365 return pet_expr_free(expr
);
368 /* Modify all access relations in "expr" by calling "fn" on them.
370 static struct pet_expr
*expr_foreach_access(struct pet_expr
*expr
,
371 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
379 for (i
= 0; i
< expr
->n_arg
; ++i
) {
380 expr
->args
[i
] = expr_foreach_access(expr
->args
[i
], fn
, user
);
382 return pet_expr_free(expr
);
385 if (expr
->type
== pet_expr_access
) {
386 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
387 if (!expr
->acc
.access
)
388 return pet_expr_free(expr
);
394 /* Modify the given access relation based on the given iteration space
396 * If the access has any arguments then the domain of the access relation
397 * is a wrapped mapping from the iteration space to the space of
398 * argument values. We only need to change the domain of this wrapped
399 * mapping, so we extend the input transformation with an identity mapping
400 * on the space of argument values.
402 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
405 isl_map
*update
= user
;
408 update
= isl_map_copy(update
);
410 dim
= isl_map_get_dim(access
);
411 dim
= isl_dim_domain(dim
);
412 if (!isl_dim_is_wrapping(dim
))
416 dim
= isl_dim_unwrap(dim
);
417 dim
= isl_dim_range(dim
);
418 dim
= isl_dim_map_from_set(dim
);
419 id
= isl_map_identity(dim
);
420 update
= isl_map_product(update
, id
);
423 return isl_map_apply_domain(access
, update
);
426 /* Modify all access relations in "expr" based on the given iteration space
429 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
430 __isl_take isl_map
*update
)
432 expr
= expr_foreach_access(expr
, &update_domain
, update
);
433 isl_map_free(update
);
437 /* Construct a pet_stmt with given line number and statement
438 * number from a pet_expr.
439 * The initial iteration domain is the zero-dimensional universe.
440 * The domains of all access relations are modified to refer
441 * to the statement iteration domain.
443 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
, int id
,
444 struct pet_expr
*expr
)
446 struct pet_stmt
*stmt
;
456 stmt
= isl_alloc_type(ctx
, struct pet_stmt
);
458 return pet_expr_free(expr
);
460 dim
= isl_dim_set_alloc(ctx
, 0, 0);
461 snprintf(name
, sizeof(name
), "S_%d", id
);
462 dim
= isl_dim_set_tuple_name(dim
, isl_dim_set
, name
);
463 dom
= isl_set_universe(isl_dim_copy(dim
));
464 sched
= isl_map_from_domain(isl_set_copy(dom
));
466 dim
= isl_dim_from_range(dim
);
467 add_name
= isl_map_universe(dim
);
468 expr
= expr_update_domain(expr
, add_name
);
472 stmt
->schedule
= sched
;
475 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
476 return pet_stmt_free(stmt
);
481 void *pet_stmt_free(struct pet_stmt
*stmt
)
486 isl_set_free(stmt
->domain
);
487 isl_map_free(stmt
->schedule
);
488 pet_expr_free(stmt
->body
);
494 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
499 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
500 fprintf(stderr
, "%*s", indent
, "");
501 isl_set_dump(stmt
->domain
);
502 fprintf(stderr
, "%*s", indent
, "");
503 isl_map_dump(stmt
->schedule
);
504 expr_dump(stmt
->body
, indent
);
507 void pet_stmt_dump(struct pet_stmt
*stmt
)
512 void *pet_array_free(struct pet_array
*array
)
517 isl_set_free(array
->context
);
518 isl_set_free(array
->extent
);
519 isl_set_free(array
->value_bounds
);
520 free(array
->element_type
);
526 void pet_array_dump(struct pet_array
*array
)
531 isl_set_dump(array
->context
);
532 isl_set_dump(array
->extent
);
533 isl_set_dump(array
->value_bounds
);
534 fprintf(stderr
, "%s %s\n", array
->element_type
,
535 array
->live_out
? "live-out" : "");
538 /* Construct a pet_scop with room for n statements.
540 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
542 struct pet_scop
*scop
;
544 scop
= isl_calloc_type(ctx
, struct pet_scop
);
548 scop
->context
= isl_set_universe(isl_dim_set_alloc(ctx
, 0, 0));
549 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
550 if (!scop
->context
|| !scop
->stmts
)
551 return pet_scop_free(scop
);
558 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
560 return scop_alloc(ctx
, 0);
563 /* Construct a pet_scop that contains the given pet_stmt.
565 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
567 struct pet_scop
*scop
;
572 scop
= scop_alloc(ctx
, 1);
574 scop
->stmts
[0] = stmt
;
583 /* Construct a pet_scop that contains the statements in "scop1" and "scop2".
585 struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
586 struct pet_scop
*scop2
)
589 struct pet_scop
*scop
;
591 if (!scop1
|| !scop2
)
594 if (scop1
->n_stmt
== 0) {
595 pet_scop_free(scop1
);
599 if (scop2
->n_stmt
== 0) {
600 pet_scop_free(scop2
);
604 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
608 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
609 scop
->stmts
[i
] = scop1
->stmts
[i
];
610 scop1
->stmts
[i
] = NULL
;
613 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
614 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
615 scop2
->stmts
[i
] = NULL
;
618 pet_scop_free(scop1
);
619 pet_scop_free(scop2
);
622 pet_scop_free(scop1
);
623 pet_scop_free(scop2
);
627 void *pet_scop_free(struct pet_scop
*scop
)
633 isl_set_free(scop
->context
);
635 for (i
= 0; i
< scop
->n_array
; ++i
)
636 pet_array_free(scop
->arrays
[i
]);
639 for (i
= 0; i
< scop
->n_stmt
; ++i
)
640 pet_stmt_free(scop
->stmts
[i
]);
646 void pet_scop_dump(struct pet_scop
*scop
)
653 isl_set_dump(scop
->context
);
654 for (i
= 0; i
< scop
->n_array
; ++i
)
655 pet_array_dump(scop
->arrays
[i
]);
656 for (i
= 0; i
< scop
->n_stmt
; ++i
)
657 pet_stmt_dump(scop
->stmts
[i
]);
660 /* Return 1 if the two pet_arrays are equivalent.
662 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
664 if (!array1
|| !array2
)
667 if (!isl_set_is_equal(array1
->context
, array2
->context
))
669 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
671 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
673 if (array1
->value_bounds
&&
674 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
676 if (strcmp(array1
->element_type
, array2
->element_type
))
678 if (array1
->live_out
!= array2
->live_out
)
684 /* Return 1 if the two pet_stmts are equivalent.
686 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
688 if (!stmt1
|| !stmt2
)
691 if (stmt1
->line
!= stmt2
->line
)
693 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
695 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
697 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
703 /* Return 1 if the two pet_scops are equivalent.
705 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
709 if (!scop1
|| !scop2
)
712 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
715 if (scop1
->n_array
!= scop2
->n_array
)
717 for (i
= 0; i
< scop1
->n_array
; ++i
)
718 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
721 if (scop1
->n_stmt
!= scop2
->n_stmt
)
723 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
724 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
730 /* Prefix the schedule of "stmt" with an extra dimension with constant
733 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
738 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
739 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
741 return pet_stmt_free(stmt
);
746 /* Prefix the schedules of all statements in "scop" with an extra
747 * dimension with constant value "pos".
749 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
756 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
757 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
759 return pet_scop_free(scop
);
765 /* Data used in embed_access.
766 * extend adds an iterator to the iteration domain
767 * var_id represents the induction variable of the corresponding loop
769 struct pet_embed_access
{
774 /* Embed the access relation in an extra outer loop.
776 * We first update the iteration domain to insert the extra dimension.
778 * If the access refers to the induction variable, then it is
779 * turned into an access to the set of integers with index (and value)
780 * equal to the induction variable.
782 * If the induction variable appears in the constraints (as a parameter),
783 * then the parameter is equated to the newly introduced iteration
784 * domain dimension and subsequently projected out.
786 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
789 struct pet_embed_access
*data
= user
;
790 isl_id
*array_id
= NULL
;
793 access
= update_domain(access
, data
->extend
);
795 if (isl_map_has_tuple_id(access
, isl_dim_out
))
796 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
797 if (array_id
== data
->var_id
) {
798 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
799 access
= isl_map_equate(access
,
800 isl_dim_in
, 0, isl_dim_out
, 0);
802 isl_id_free(array_id
);
804 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
806 access
= isl_map_equate(access
,
807 isl_dim_param
, pos
, isl_dim_in
, 0);
808 access
= isl_map_project_out(access
, isl_dim_param
, pos
, 1);
810 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
811 isl_id_copy(data
->var_id
));
816 /* Embed all access relations in "expr" in an extra loop.
817 * "extend" inserts an outer loop iterator in the iteration domains.
818 * "var_id" represents the induction variable.
820 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
821 __isl_take isl_map
*extend
, __isl_keep isl_id
*var_id
)
823 struct pet_embed_access data
= { .extend
= extend
, .var_id
= var_id
};
825 expr
= expr_foreach_access(expr
, &embed_access
, &data
);
826 isl_map_free(extend
);
830 /* Embed the given pet_stmt in an extra outer loop with iteration domain
831 * "dom" and schedule "sched". "var_id" represents the induction variable
834 * The iteration domain and schedule of the statement are updated
835 * according to the iteration domain and schedule of the new loop.
837 * If the induction variable appears in the constraints (as a parameter)
838 * of the current iteration domain or the schedule of the statement,
839 * then the parameter is equated to the newly introduced iteration
840 * domain dimension and subsequently projected out.
842 * Finally, all access relations are updated based on the extra loop.
844 struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
, __isl_take isl_set
*dom
,
845 __isl_take isl_map
*sched
, __isl_take isl_id
*var_id
)
852 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
853 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
), stmt
->domain
);
854 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
, isl_id_copy(stmt_id
));
856 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
858 stmt
->domain
= isl_set_equate(stmt
->domain
,
859 isl_dim_param
, pos
, isl_dim_set
, 0);
860 stmt
->domain
= isl_set_project_out(stmt
->domain
,
861 isl_dim_param
, pos
, 1);
864 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
865 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
866 isl_dim_in
, stmt_id
);
868 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
870 stmt
->schedule
= isl_map_equate(stmt
->schedule
,
871 isl_dim_param
, pos
, isl_dim_in
, 0);
872 stmt
->schedule
= isl_map_project_out(stmt
->schedule
,
873 isl_dim_param
, pos
, 1);
876 dim
= isl_dim_map_from_set(isl_set_get_dim(stmt
->domain
));
877 extend
= isl_map_identity(dim
);
878 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
879 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
880 isl_map_get_tuple_id(extend
, isl_dim_out
));
881 stmt
->body
= expr_embed(stmt
->body
, extend
, var_id
);
886 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
887 return pet_stmt_free(stmt
);
891 /* Embed all statements in "scop" in an extra outer loop with iteration domain
892 * "dom" and schedule "sched". "var_id" represents the induction variable
895 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
896 __isl_take isl_map
*sched
, __isl_take isl_id
*id
)
903 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
904 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
906 isl_map_copy(sched
), isl_id_copy(id
));
919 return pet_scop_free(scop
);
922 /* Add extra conditions on the parameters to iteration domain of "stmt".
924 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
925 __isl_take isl_set
*cond
)
930 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
935 return pet_stmt_free(stmt
);
938 /* Add extra conditions on the parameters to all iteration domains.
940 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
941 __isl_take isl_set
*cond
)
948 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
949 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
959 return pet_scop_free(scop
);
962 /* Add all parameters in "expr" to "dim" and return the result.
964 static __isl_give isl_dim
*expr_collect_params(struct pet_expr
*expr
,
965 __isl_take isl_dim
*dim
)
971 for (i
= 0; i
< expr
->n_arg
; ++i
)
973 dim
= expr_collect_params(expr
->args
[i
], dim
);
975 if (expr
->type
== pet_expr_access
)
976 dim
= isl_dim_align_params(dim
,
977 isl_map_get_dim(expr
->acc
.access
));
982 return pet_expr_free(expr
);
985 /* Add all parameters in "stmt" to "dim" and return the result.
987 static __isl_give isl_dim
*stmt_collect_params(struct pet_stmt
*stmt
,
988 __isl_take isl_dim
*dim
)
993 dim
= isl_dim_align_params(dim
, isl_set_get_dim(stmt
->domain
));
994 dim
= isl_dim_align_params(dim
, isl_map_get_dim(stmt
->schedule
));
995 dim
= expr_collect_params(stmt
->body
, dim
);
1000 return pet_stmt_free(stmt
);
1003 /* Add all parameters in "array" to "dim" and return the result.
1005 static __isl_give isl_dim
*array_collect_params(struct pet_array
*array
,
1006 __isl_take isl_dim
*dim
)
1011 dim
= isl_dim_align_params(dim
, isl_set_get_dim(array
->context
));
1012 dim
= isl_dim_align_params(dim
, isl_set_get_dim(array
->extent
));
1017 return pet_array_free(array
);
1020 /* Add all parameters in "scop" to "dim" and return the result.
1022 static __isl_give isl_dim
*scop_collect_params(struct pet_scop
*scop
,
1023 __isl_take isl_dim
*dim
)
1030 for (i
= 0; i
< scop
->n_array
; ++i
)
1031 dim
= array_collect_params(scop
->arrays
[i
], dim
);
1033 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1034 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
1039 return pet_scop_free(scop
);
1042 /* Add all parameters in "dim" to all access relations in "expr".
1044 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
1045 __isl_take isl_dim
*dim
)
1052 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1054 expr_propagate_params(expr
->args
[i
],
1060 if (expr
->type
== pet_expr_access
) {
1061 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1063 if (!expr
->acc
.access
)
1071 return pet_expr_free(expr
);
1074 /* Add all parameters in "dim" to the domain, schedule and
1075 * all access relations in "stmt".
1077 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
1078 __isl_take isl_dim
*dim
)
1083 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_dim_copy(dim
));
1084 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
1086 stmt
->body
= expr_propagate_params(stmt
->body
, isl_dim_copy(dim
));
1088 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1095 return pet_stmt_free(stmt
);
1098 /* Add all parameters in "dim" to "array".
1100 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
1101 __isl_take isl_dim
*dim
)
1106 array
->context
= isl_set_align_params(array
->context
,
1108 array
->extent
= isl_set_align_params(array
->extent
,
1110 if (array
->value_bounds
) {
1111 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
1113 if (!array
->value_bounds
)
1117 if (!array
->context
|| !array
->extent
)
1124 return pet_array_free(array
);
1127 /* Add all parameters in "dim" to "scop".
1129 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
1130 __isl_take isl_dim
*dim
)
1137 for (i
= 0; i
< scop
->n_array
; ++i
) {
1138 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
1140 if (!scop
->arrays
[i
])
1144 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1145 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
1147 if (!scop
->stmts
[i
])
1155 return pet_scop_free(scop
);
1158 /* Update all isl_sets and isl_maps in "scop" such that they all
1159 * have the same parameters.
1161 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
1168 dim
= isl_set_get_dim(scop
->context
);
1169 dim
= scop_collect_params(scop
, dim
);
1171 scop
->context
= isl_set_align_params(scop
->context
, isl_dim_copy(dim
));
1172 scop
= scop_propagate_params(scop
, dim
);
1177 /* Check if the given access relation accesses a (0D) array that corresponds
1178 * to one of the parameters in "dim". If so, replace the array access
1179 * by an access to the set of integers with as index (and value)
1182 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1183 __isl_take isl_dim
*dim
)
1185 isl_id
*array_id
= NULL
;
1188 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1189 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1190 pos
= isl_dim_find_dim_by_id(dim
, isl_dim_param
, array_id
);
1195 isl_id_free(array_id
);
1199 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1201 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1202 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1205 isl_id_free(array_id
);
1207 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1208 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1213 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1214 * in "dim" by a value equal to the corresponding parameter.
1216 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
1217 __isl_take isl_dim
*dim
)
1224 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1226 expr_detect_parameter_accesses(expr
->args
[i
],
1232 if (expr
->type
== pet_expr_access
) {
1233 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1235 if (!expr
->acc
.access
)
1243 return pet_expr_free(expr
);
1246 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1247 * in "dim" by a value equal to the corresponding parameter.
1249 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
1250 __isl_take isl_dim
*dim
)
1255 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
1258 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1265 return pet_stmt_free(stmt
);
1268 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1269 * in "dim" by a value equal to the corresponding parameter.
1271 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
1272 __isl_take isl_dim
*dim
)
1279 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1280 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
1282 if (!scop
->stmts
[i
])
1290 return pet_scop_free(scop
);
1293 /* Replace all accesses to (0D) arrays that correspond to any of
1294 * the parameters used in "scop" by a value equal
1295 * to the corresponding parameter.
1297 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
1304 dim
= isl_set_get_dim(scop
->context
);
1305 dim
= scop_collect_params(scop
, dim
);
1307 scop
= scop_detect_parameter_accesses(scop
, dim
);
1312 /* Add all read access relations (if "read" is set) and/or all write
1313 * access relations (if "write" is set) to "accesses" and return the result.
1315 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
1316 int read
, int write
, __isl_take isl_union_map
*accesses
)
1325 for (i
= 0; i
< expr
->n_arg
; ++i
)
1326 accesses
= expr_collect_accesses(expr
->args
[i
],
1327 read
, write
, accesses
);
1329 if (expr
->type
== pet_expr_access
) {
1330 id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
1332 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
1333 accesses
= isl_union_map_add_map(accesses
,
1334 isl_map_copy(expr
->acc
.access
));
1341 /* Collect and return all read access relations (if "read" is set)
1342 * and/or all write * access relations (if "write" is set) in "stmt".
1344 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
1345 int read
, int write
, __isl_take isl_dim
*dim
)
1347 isl_union_map
*accesses
;
1352 accesses
= isl_union_map_empty(dim
);
1353 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
1354 accesses
= isl_union_map_intersect_domain(accesses
,
1355 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
1360 /* Collect and return all read access relations (if "read" is set)
1361 * and/or all write * access relations (if "write" is set) in "scop".
1363 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
1364 int read
, int write
)
1367 isl_union_map
*accesses
;
1372 accesses
= isl_union_map_empty(isl_set_get_dim(scop
->context
));
1374 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1375 isl_union_map
*accesses_i
;
1376 isl_dim
*dim
= isl_set_get_dim(scop
->context
);
1377 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
1379 accesses
= isl_union_map_union(accesses
, accesses_i
);
1385 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
1387 return scop_collect_accesses(scop
, 1, 0);
1390 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
1392 return scop_collect_accesses(scop
, 0, 1);
1395 /* Collect and return the union of iteration domains in "scop".
1397 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
1401 isl_union_set
*domain
;
1406 domain
= isl_union_set_empty(isl_set_get_dim(scop
->context
));
1408 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1409 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
1410 domain
= isl_union_set_add_set(domain
, domain_i
);
1416 /* Collect and return the schedules of the statements in "scop".
1417 * The range is normalized to the maximal number of scheduling
1420 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
1423 isl_map
*schedule_i
;
1424 isl_union_map
*schedule
;
1425 int depth
, max_depth
= 0;
1430 schedule
= isl_union_map_empty(isl_set_get_dim(scop
->context
));
1432 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1433 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
1434 if (depth
> max_depth
)
1438 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1439 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
1440 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
1441 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
1443 for (j
= depth
; j
< max_depth
; ++j
)
1444 schedule_i
= isl_map_fix_si(schedule_i
,
1446 schedule
= isl_union_map_add_map(schedule
, schedule_i
);