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_address_of
] = "&"
69 const char *pet_op_str(enum pet_op_type op
)
74 const char *pet_type_str(enum pet_expr_type type
)
76 return type_str
[type
];
79 enum pet_op_type
pet_str_op(const char *str
)
83 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
84 if (!strcmp(op_str
[i
], str
))
90 enum pet_expr_type
pet_str_type(const char *str
)
94 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
95 if (!strcmp(type_str
[i
], str
))
101 /* Construct a pet_expr from an access relation.
102 * By default, it is considered to be a read access.
104 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
106 isl_ctx
*ctx
= isl_map_get_ctx(access
);
107 struct pet_expr
*expr
;
111 expr
= isl_calloc_type(ctx
, struct pet_expr
);
115 expr
->type
= pet_expr_access
;
116 expr
->acc
.access
= access
;
122 isl_map_free(access
);
126 /* Construct a unary pet_expr that performs "op" on "arg".
128 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
129 struct pet_expr
*arg
)
131 struct pet_expr
*expr
;
135 expr
= isl_alloc_type(ctx
, struct pet_expr
);
139 expr
->type
= pet_expr_unary
;
142 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
145 expr
->args
[pet_un_arg
] = arg
;
153 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
155 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
156 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
158 struct pet_expr
*expr
;
162 expr
= isl_alloc_type(ctx
, struct pet_expr
);
166 expr
->type
= pet_expr_binary
;
169 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
172 expr
->args
[pet_bin_lhs
] = lhs
;
173 expr
->args
[pet_bin_rhs
] = rhs
;
182 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
184 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
185 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
187 struct pet_expr
*expr
;
189 if (!cond
|| !lhs
|| !rhs
)
191 expr
= isl_alloc_type(ctx
, struct pet_expr
);
195 expr
->type
= pet_expr_ternary
;
197 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
200 expr
->args
[pet_ter_cond
] = cond
;
201 expr
->args
[pet_ter_true
] = lhs
;
202 expr
->args
[pet_ter_false
] = rhs
;
212 /* Construct a call pet_expr that calls function "name" with "n_arg"
213 * arguments. The caller is responsible for filling in the arguments.
215 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
218 struct pet_expr
*expr
;
220 expr
= isl_alloc_type(ctx
, struct pet_expr
);
224 expr
->type
= pet_expr_call
;
226 expr
->name
= strdup(name
);
227 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
228 if (!expr
->name
|| !expr
->args
)
229 return pet_expr_free(expr
);
234 /* Construct a pet_expr that represents the double "d".
236 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double d
)
238 struct pet_expr
*expr
;
240 expr
= isl_calloc_type(ctx
, struct pet_expr
);
244 expr
->type
= pet_expr_double
;
250 void *pet_expr_free(struct pet_expr
*expr
)
257 for (i
= 0; i
< expr
->n_arg
; ++i
)
258 pet_expr_free(expr
->args
[i
]);
261 switch (expr
->type
) {
262 case pet_expr_access
:
263 isl_map_free(expr
->acc
.access
);
268 case pet_expr_double
:
270 case pet_expr_binary
:
271 case pet_expr_ternary
:
279 static void expr_dump(struct pet_expr
*expr
, int indent
)
286 fprintf(stderr
, "%*s", indent
, "");
288 switch (expr
->type
) {
289 case pet_expr_double
:
290 fprintf(stderr
, "%g\n", expr
->d
);
292 case pet_expr_access
:
293 isl_map_dump(expr
->acc
.access
);
294 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
296 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
297 "", expr
->acc
.write
);
298 for (i
= 0; i
< expr
->n_arg
; ++i
)
299 expr_dump(expr
->args
[i
], indent
+ 2);
302 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
303 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
305 case pet_expr_binary
:
306 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
307 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
308 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
310 case pet_expr_ternary
:
311 fprintf(stderr
, "?:\n");
312 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
313 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
314 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
317 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
318 for (i
= 0; i
< expr
->n_arg
; ++i
)
319 expr_dump(expr
->args
[i
], indent
+ 2);
324 void pet_expr_dump(struct pet_expr
*expr
)
329 /* Return 1 if the two pet_exprs are equivalent.
331 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
335 if (!expr1
|| !expr2
)
338 if (expr1
->type
!= expr2
->type
)
340 if (expr1
->n_arg
!= expr2
->n_arg
)
342 for (i
= 0; i
< expr1
->n_arg
; ++i
)
343 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
345 switch (expr1
->type
) {
346 case pet_expr_double
:
347 if (expr1
->d
!= expr2
->d
)
350 case pet_expr_access
:
351 if (expr1
->acc
.read
!= expr2
->acc
.read
)
353 if (expr1
->acc
.write
!= expr2
->acc
.write
)
355 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
357 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
361 case pet_expr_binary
:
362 case pet_expr_ternary
:
363 if (expr1
->op
!= expr2
->op
)
367 if (strcmp(expr1
->name
, expr2
->name
))
375 /* Add extra conditions on the parameters to all access relations in "expr".
377 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
378 __isl_take isl_set
*cond
)
385 for (i
= 0; i
< expr
->n_arg
; ++i
) {
386 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
392 if (expr
->type
== pet_expr_access
) {
393 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
395 if (!expr
->acc
.access
)
403 return pet_expr_free(expr
);
406 /* Modify all access relations in "expr" by calling "fn" on them.
408 struct pet_expr
*pet_expr_foreach_access(struct pet_expr
*expr
,
409 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
417 for (i
= 0; i
< expr
->n_arg
; ++i
) {
418 expr
->args
[i
] = pet_expr_foreach_access(expr
->args
[i
], fn
, user
);
420 return pet_expr_free(expr
);
423 if (expr
->type
== pet_expr_access
) {
424 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
425 if (!expr
->acc
.access
)
426 return pet_expr_free(expr
);
432 /* Modify all expressions of type pet_expr_access in "expr"
433 * by calling "fn" on them.
435 struct pet_expr
*pet_expr_foreach_access_expr(struct pet_expr
*expr
,
436 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
444 for (i
= 0; i
< expr
->n_arg
; ++i
) {
445 expr
->args
[i
] = pet_expr_foreach_access_expr(expr
->args
[i
],
448 return pet_expr_free(expr
);
451 if (expr
->type
== pet_expr_access
)
452 expr
= fn(expr
, user
);
457 /* Modify the given access relation based on the given iteration space
459 * If the access has any arguments then the domain of the access relation
460 * is a wrapped mapping from the iteration space to the space of
461 * argument values. We only need to change the domain of this wrapped
462 * mapping, so we extend the input transformation with an identity mapping
463 * on the space of argument values.
465 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
468 isl_map
*update
= user
;
471 update
= isl_map_copy(update
);
473 dim
= isl_map_get_space(access
);
474 dim
= isl_space_domain(dim
);
475 if (!isl_space_is_wrapping(dim
))
479 dim
= isl_space_unwrap(dim
);
480 dim
= isl_space_range(dim
);
481 dim
= isl_space_map_from_set(dim
);
482 id
= isl_map_identity(dim
);
483 update
= isl_map_product(update
, id
);
486 return isl_map_apply_domain(access
, update
);
489 /* Modify all access relations in "expr" based on the given iteration space
492 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
493 __isl_take isl_map
*update
)
495 expr
= pet_expr_foreach_access(expr
, &update_domain
, update
);
496 isl_map_free(update
);
500 /* Construct a pet_stmt with given line number and statement
501 * number from a pet_expr.
502 * The initial iteration domain is the zero-dimensional universe.
503 * The name of the domain is given by "label" if it is non-NULL.
504 * Otherwise, the name is constructed as S_<id>.
505 * The domains of all access relations are modified to refer
506 * to the statement iteration domain.
508 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
509 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
511 struct pet_stmt
*stmt
;
521 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
525 dim
= isl_space_set_alloc(ctx
, 0, 0);
527 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
529 snprintf(name
, sizeof(name
), "S_%d", id
);
530 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
532 dom
= isl_set_universe(isl_space_copy(dim
));
533 sched
= isl_map_from_domain(isl_set_copy(dom
));
535 dim
= isl_space_from_range(dim
);
536 add_name
= isl_map_universe(dim
);
537 expr
= expr_update_domain(expr
, add_name
);
541 stmt
->schedule
= sched
;
544 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
545 return pet_stmt_free(stmt
);
550 return pet_expr_free(expr
);
553 void *pet_stmt_free(struct pet_stmt
*stmt
)
560 isl_set_free(stmt
->domain
);
561 isl_map_free(stmt
->schedule
);
562 pet_expr_free(stmt
->body
);
564 for (i
= 0; i
< stmt
->n_arg
; ++i
)
565 pet_expr_free(stmt
->args
[i
]);
572 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
579 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
580 fprintf(stderr
, "%*s", indent
, "");
581 isl_set_dump(stmt
->domain
);
582 fprintf(stderr
, "%*s", indent
, "");
583 isl_map_dump(stmt
->schedule
);
584 expr_dump(stmt
->body
, indent
);
585 for (i
= 0; i
< stmt
->n_arg
; ++i
)
586 expr_dump(stmt
->args
[i
], indent
+ 2);
589 void pet_stmt_dump(struct pet_stmt
*stmt
)
594 void *pet_array_free(struct pet_array
*array
)
599 isl_set_free(array
->context
);
600 isl_set_free(array
->extent
);
601 isl_set_free(array
->value_bounds
);
602 free(array
->element_type
);
608 void pet_array_dump(struct pet_array
*array
)
613 isl_set_dump(array
->context
);
614 isl_set_dump(array
->extent
);
615 isl_set_dump(array
->value_bounds
);
616 fprintf(stderr
, "%s %s\n", array
->element_type
,
617 array
->live_out
? "live-out" : "");
620 /* Construct a pet_scop with room for n statements.
622 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
625 struct pet_scop
*scop
;
627 scop
= isl_calloc_type(ctx
, struct pet_scop
);
631 space
= isl_space_params_alloc(ctx
, 0);
632 scop
->context
= isl_set_universe(isl_space_copy(space
));
633 scop
->context_value
= isl_set_universe(space
);
634 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
635 if (!scop
->context
|| !scop
->stmts
)
636 return pet_scop_free(scop
);
643 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
645 return scop_alloc(ctx
, 0);
648 /* Construct a pet_scop that contains the given pet_stmt.
650 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
652 struct pet_scop
*scop
;
657 scop
= scop_alloc(ctx
, 1);
659 scop
->stmts
[0] = stmt
;
668 /* Construct a pet_scop that contains the arrays and the statements
669 * in "scop1" and "scop2".
671 struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
672 struct pet_scop
*scop2
)
675 struct pet_scop
*scop
;
677 if (!scop1
|| !scop2
)
680 if (scop1
->n_stmt
== 0) {
681 pet_scop_free(scop1
);
685 if (scop2
->n_stmt
== 0) {
686 pet_scop_free(scop2
);
690 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
694 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
695 scop1
->n_array
+ scop2
->n_array
);
698 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
700 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
701 scop
->stmts
[i
] = scop1
->stmts
[i
];
702 scop1
->stmts
[i
] = NULL
;
705 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
706 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
707 scop2
->stmts
[i
] = NULL
;
710 for (i
= 0; i
< scop1
->n_array
; ++i
) {
711 scop
->arrays
[i
] = scop1
->arrays
[i
];
712 scop1
->arrays
[i
] = NULL
;
715 for (i
= 0; i
< scop2
->n_array
; ++i
) {
716 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
717 scop2
->arrays
[i
] = NULL
;
720 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
721 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
723 pet_scop_free(scop1
);
724 pet_scop_free(scop2
);
727 pet_scop_free(scop1
);
728 pet_scop_free(scop2
);
732 void *pet_scop_free(struct pet_scop
*scop
)
738 isl_set_free(scop
->context
);
739 isl_set_free(scop
->context_value
);
741 for (i
= 0; i
< scop
->n_array
; ++i
)
742 pet_array_free(scop
->arrays
[i
]);
745 for (i
= 0; i
< scop
->n_stmt
; ++i
)
746 pet_stmt_free(scop
->stmts
[i
]);
752 void pet_scop_dump(struct pet_scop
*scop
)
759 isl_set_dump(scop
->context
);
760 isl_set_dump(scop
->context_value
);
761 for (i
= 0; i
< scop
->n_array
; ++i
)
762 pet_array_dump(scop
->arrays
[i
]);
763 for (i
= 0; i
< scop
->n_stmt
; ++i
)
764 pet_stmt_dump(scop
->stmts
[i
]);
767 /* Return 1 if the two pet_arrays are equivalent.
769 * We don't compare element_size as this may be target dependent.
771 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
773 if (!array1
|| !array2
)
776 if (!isl_set_is_equal(array1
->context
, array2
->context
))
778 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
780 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
782 if (array1
->value_bounds
&&
783 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
785 if (strcmp(array1
->element_type
, array2
->element_type
))
787 if (array1
->live_out
!= array2
->live_out
)
793 /* Return 1 if the two pet_stmts are equivalent.
795 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
799 if (!stmt1
|| !stmt2
)
802 if (stmt1
->line
!= stmt2
->line
)
804 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
806 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
808 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
810 if (stmt1
->n_arg
!= stmt2
->n_arg
)
812 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
813 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
820 /* Return 1 if the two pet_scops are equivalent.
822 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
826 if (!scop1
|| !scop2
)
829 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
831 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
834 if (scop1
->n_array
!= scop2
->n_array
)
836 for (i
= 0; i
< scop1
->n_array
; ++i
)
837 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
840 if (scop1
->n_stmt
!= scop2
->n_stmt
)
842 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
843 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
849 /* Prefix the schedule of "stmt" with an extra dimension with constant
852 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
857 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
858 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
860 return pet_stmt_free(stmt
);
865 /* Prefix the schedules of all statements in "scop" with an extra
866 * dimension with constant value "pos".
868 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
875 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
876 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
878 return pet_scop_free(scop
);
884 /* Data used in embed_access.
885 * extend adds an iterator to the iteration domain
886 * var_id represents the induction variable of the corresponding loop
888 struct pet_embed_access
{
893 /* Embed the access relation in an extra outer loop.
895 * We first update the iteration domain to insert the extra dimension.
897 * If the access refers to the induction variable, then it is
898 * turned into an access to the set of integers with index (and value)
899 * equal to the induction variable.
901 * If the induction variable appears in the constraints (as a parameter),
902 * then the parameter is equated to the newly introduced iteration
903 * domain dimension and subsequently projected out.
905 * Similarly, if the accessed array is a virtual array (with user
906 * pointer equal to NULL), as created by create_test_access,
907 * then it is extended along with the domain of the access.
909 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
912 struct pet_embed_access
*data
= user
;
913 isl_id
*array_id
= NULL
;
916 access
= update_domain(access
, data
->extend
);
918 if (isl_map_has_tuple_id(access
, isl_dim_out
))
919 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
920 if (array_id
== data
->var_id
||
921 (array_id
&& !isl_id_get_user(array_id
))) {
922 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
923 access
= isl_map_equate(access
,
924 isl_dim_in
, 0, isl_dim_out
, 0);
925 if (array_id
!= data
->var_id
)
926 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
927 isl_id_copy(array_id
));
929 isl_id_free(array_id
);
931 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
933 access
= isl_map_equate(access
,
934 isl_dim_param
, pos
, isl_dim_in
, 0);
935 access
= isl_map_project_out(access
, isl_dim_param
, pos
, 1);
937 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
938 isl_id_copy(data
->var_id
));
943 /* Embed all access relations in "expr" in an extra loop.
944 * "extend" inserts an outer loop iterator in the iteration domains.
945 * "var_id" represents the induction variable.
947 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
948 __isl_take isl_map
*extend
, __isl_keep isl_id
*var_id
)
950 struct pet_embed_access data
= { .extend
= extend
, .var_id
= var_id
};
952 expr
= pet_expr_foreach_access(expr
, &embed_access
, &data
);
953 isl_map_free(extend
);
957 /* Embed the given pet_stmt in an extra outer loop with iteration domain
958 * "dom" and schedule "sched". "var_id" represents the induction variable
961 * The iteration domain and schedule of the statement are updated
962 * according to the iteration domain and schedule of the new loop.
963 * If stmt->domain is a wrapped map, then the iteration domain
964 * is the domain of this map, so we need to be careful to adjust
967 * If the induction variable appears in the constraints (as a parameter)
968 * of the current iteration domain or the schedule of the statement,
969 * then the parameter is equated to the newly introduced iteration
970 * domain dimension and subsequently projected out.
972 * Finally, all access relations are updated based on the extra loop.
974 struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
, __isl_take isl_set
*dom
,
975 __isl_take isl_map
*sched
, __isl_take isl_id
*var_id
)
986 if (isl_set_is_wrapping(stmt
->domain
)) {
991 map
= isl_set_unwrap(stmt
->domain
);
992 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
993 ran_dim
= isl_space_range(isl_map_get_space(map
));
994 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
995 isl_set_universe(ran_dim
));
996 map
= isl_map_flat_domain_product(ext
, map
);
997 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
998 isl_id_copy(stmt_id
));
999 dim
= isl_space_domain(isl_map_get_space(map
));
1000 stmt
->domain
= isl_map_wrap(map
);
1002 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1003 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1005 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1006 isl_id_copy(stmt_id
));
1007 dim
= isl_set_get_space(stmt
->domain
);
1010 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1012 stmt
->domain
= isl_set_equate(stmt
->domain
,
1013 isl_dim_param
, pos
, isl_dim_set
, 0);
1014 stmt
->domain
= isl_set_project_out(stmt
->domain
,
1015 isl_dim_param
, pos
, 1);
1018 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1019 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1020 isl_dim_in
, stmt_id
);
1022 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1024 stmt
->schedule
= isl_map_equate(stmt
->schedule
,
1025 isl_dim_param
, pos
, isl_dim_in
, 0);
1026 stmt
->schedule
= isl_map_project_out(stmt
->schedule
,
1027 isl_dim_param
, pos
, 1);
1030 dim
= isl_space_map_from_set(dim
);
1031 extend
= isl_map_identity(dim
);
1032 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
1033 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
1034 isl_map_get_tuple_id(extend
, isl_dim_out
));
1035 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1036 stmt
->args
[i
] = expr_embed(stmt
->args
[i
],
1037 isl_map_copy(extend
), var_id
);
1038 stmt
->body
= expr_embed(stmt
->body
, extend
, var_id
);
1041 isl_id_free(var_id
);
1043 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1045 return pet_stmt_free(stmt
);
1046 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1047 return pet_stmt_free(stmt
);
1051 isl_map_free(sched
);
1052 isl_id_free(var_id
);
1056 /* Embed the given pet_array in an extra outer loop with iteration domain
1058 * This embedding only has an effect on virtual arrays (those with
1059 * user pointer equal to NULL), which need to be extended along with
1060 * the iteration domain.
1062 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1063 __isl_take isl_set
*dom
)
1065 isl_id
*array_id
= NULL
;
1070 if (isl_set_has_tuple_id(array
->extent
))
1071 array_id
= isl_set_get_tuple_id(array
->extent
);
1073 if (array_id
&& !isl_id_get_user(array_id
)) {
1074 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1075 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1078 isl_id_free(array_id
);
1087 /* Project out all unnamed parameters from "set" and return the result.
1089 static __isl_give isl_set
*set_project_out_unnamed_params(
1090 __isl_take isl_set
*set
)
1094 n
= isl_set_dim(set
, isl_dim_param
);
1095 for (i
= n
- 1; i
>= 0; --i
) {
1096 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1098 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1104 /* Embed all statements and arrays in "scop" in an extra outer loop
1105 * with iteration domain "dom" and schedule "sched".
1106 * "var_id" represents the induction variable of the loop.
1108 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1109 __isl_take isl_map
*sched
, __isl_take isl_id
*id
)
1116 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1117 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1119 isl_map_copy(sched
), isl_id_copy(id
));
1120 if (!scop
->stmts
[i
])
1124 for (i
= 0; i
< scop
->n_array
; ++i
) {
1125 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1127 if (!scop
->arrays
[i
])
1132 isl_map_free(sched
);
1137 isl_map_free(sched
);
1139 return pet_scop_free(scop
);
1142 /* Add extra conditions on the parameters to iteration domain of "stmt".
1144 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1145 __isl_take isl_set
*cond
)
1150 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1155 return pet_stmt_free(stmt
);
1158 /* Add extra conditions on the parameters to all iteration domains.
1160 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1161 __isl_take isl_set
*cond
)
1168 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1169 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1170 isl_set_copy(cond
));
1171 if (!scop
->stmts
[i
])
1179 return pet_scop_free(scop
);
1182 /* Make the statements "stmt" depend on the value of "test"
1183 * being equal to "satisfied" by adjusting stmt->domain.
1185 * We insert an argument corresponding to a read to "test"
1186 * from the iteration domain of "stmt" in front of the list of arguments.
1187 * We also insert a corresponding output dimension in the wrapped
1188 * map contained in stmt->domain, with value set to "satisfied".
1190 static struct pet_stmt
*stmt_filter(struct pet_stmt
*stmt
,
1191 __isl_take isl_map
*test
, int satisfied
)
1202 if (isl_set_is_wrapping(stmt
->domain
))
1203 map
= isl_set_unwrap(stmt
->domain
);
1205 map
= isl_map_from_domain(stmt
->domain
);
1206 map
= isl_map_insert_dims(map
, isl_dim_out
, 0, 1);
1207 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1208 map
= isl_map_set_dim_id(map
, isl_dim_out
, 0, id
);
1209 map
= isl_map_fix_si(map
, isl_dim_out
, 0, satisfied
);
1210 dom
= isl_set_universe(isl_space_domain(isl_map_get_space(map
)));
1211 test
= isl_map_apply_domain(test
, isl_map_from_range(dom
));
1213 stmt
->domain
= isl_map_wrap(map
);
1215 ctx
= isl_map_get_ctx(test
);
1217 stmt
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1221 struct pet_expr
**args
;
1222 args
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + stmt
->n_arg
);
1225 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1226 args
[1 + i
] = stmt
->args
[i
];
1231 stmt
->args
[0] = pet_expr_from_access(isl_map_copy(test
));
1239 return pet_stmt_free(stmt
);
1242 /* Make all statements in "scop" depend on the value of "test"
1243 * being equal to "satisfied" by adjusting their domains.
1245 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
1246 __isl_take isl_map
*test
, int satisfied
)
1253 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1254 scop
->stmts
[i
] = stmt_filter(scop
->stmts
[i
],
1255 isl_map_copy(test
), satisfied
);
1256 if (!scop
->stmts
[i
])
1264 return pet_scop_free(scop
);
1267 /* Add all parameters in "expr" to "dim" and return the result.
1269 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
1270 __isl_take isl_space
*dim
)
1276 for (i
= 0; i
< expr
->n_arg
; ++i
)
1278 dim
= expr_collect_params(expr
->args
[i
], dim
);
1280 if (expr
->type
== pet_expr_access
)
1281 dim
= isl_space_align_params(dim
,
1282 isl_map_get_space(expr
->acc
.access
));
1286 isl_space_free(dim
);
1287 return pet_expr_free(expr
);
1290 /* Add all parameters in "stmt" to "dim" and return the result.
1292 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
1293 __isl_take isl_space
*dim
)
1298 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
1299 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
1300 dim
= expr_collect_params(stmt
->body
, dim
);
1304 isl_space_free(dim
);
1305 return pet_stmt_free(stmt
);
1308 /* Add all parameters in "array" to "dim" and return the result.
1310 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
1311 __isl_take isl_space
*dim
)
1316 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
1317 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
1321 isl_space_free(dim
);
1322 return pet_array_free(array
);
1325 /* Add all parameters in "scop" to "dim" and return the result.
1327 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
1328 __isl_take isl_space
*dim
)
1335 for (i
= 0; i
< scop
->n_array
; ++i
)
1336 dim
= array_collect_params(scop
->arrays
[i
], dim
);
1338 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1339 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
1343 isl_space_free(dim
);
1344 return pet_scop_free(scop
);
1347 /* Add all parameters in "dim" to all access relations in "expr".
1349 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
1350 __isl_take isl_space
*dim
)
1357 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1359 expr_propagate_params(expr
->args
[i
],
1360 isl_space_copy(dim
));
1365 if (expr
->type
== pet_expr_access
) {
1366 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
1367 isl_space_copy(dim
));
1368 if (!expr
->acc
.access
)
1372 isl_space_free(dim
);
1375 isl_space_free(dim
);
1376 return pet_expr_free(expr
);
1379 /* Add all parameters in "dim" to the domain, schedule and
1380 * all access relations in "stmt".
1382 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
1383 __isl_take isl_space
*dim
)
1388 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
1389 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
1390 isl_space_copy(dim
));
1391 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
1393 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1396 isl_space_free(dim
);
1399 isl_space_free(dim
);
1400 return pet_stmt_free(stmt
);
1403 /* Add all parameters in "dim" to "array".
1405 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
1406 __isl_take isl_space
*dim
)
1411 array
->context
= isl_set_align_params(array
->context
,
1412 isl_space_copy(dim
));
1413 array
->extent
= isl_set_align_params(array
->extent
,
1414 isl_space_copy(dim
));
1415 if (array
->value_bounds
) {
1416 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
1417 isl_space_copy(dim
));
1418 if (!array
->value_bounds
)
1422 if (!array
->context
|| !array
->extent
)
1425 isl_space_free(dim
);
1428 isl_space_free(dim
);
1429 return pet_array_free(array
);
1432 /* Add all parameters in "dim" to "scop".
1434 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
1435 __isl_take isl_space
*dim
)
1442 for (i
= 0; i
< scop
->n_array
; ++i
) {
1443 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
1444 isl_space_copy(dim
));
1445 if (!scop
->arrays
[i
])
1449 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1450 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
1451 isl_space_copy(dim
));
1452 if (!scop
->stmts
[i
])
1456 isl_space_free(dim
);
1459 isl_space_free(dim
);
1460 return pet_scop_free(scop
);
1463 /* Update all isl_sets and isl_maps in "scop" such that they all
1464 * have the same parameters.
1466 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
1473 dim
= isl_set_get_space(scop
->context
);
1474 dim
= scop_collect_params(scop
, dim
);
1476 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
1477 scop
= scop_propagate_params(scop
, dim
);
1482 /* Check if the given access relation accesses a (0D) array that corresponds
1483 * to one of the parameters in "dim". If so, replace the array access
1484 * by an access to the set of integers with as index (and value)
1487 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
1488 __isl_take isl_space
*dim
)
1490 isl_id
*array_id
= NULL
;
1493 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
1494 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1495 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
1497 isl_space_free(dim
);
1500 isl_id_free(array_id
);
1504 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
1506 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
1507 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
1510 isl_id_free(array_id
);
1512 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1513 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
1518 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1519 * in "dim" by a value equal to the corresponding parameter.
1521 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
1522 __isl_take isl_space
*dim
)
1529 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1531 expr_detect_parameter_accesses(expr
->args
[i
],
1532 isl_space_copy(dim
));
1537 if (expr
->type
== pet_expr_access
) {
1538 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
1539 isl_space_copy(dim
));
1540 if (!expr
->acc
.access
)
1544 isl_space_free(dim
);
1547 isl_space_free(dim
);
1548 return pet_expr_free(expr
);
1551 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1552 * in "dim" by a value equal to the corresponding parameter.
1554 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
1555 __isl_take isl_space
*dim
)
1560 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
1561 isl_space_copy(dim
));
1563 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1566 isl_space_free(dim
);
1569 isl_space_free(dim
);
1570 return pet_stmt_free(stmt
);
1573 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
1574 * in "dim" by a value equal to the corresponding parameter.
1576 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
1577 __isl_take isl_space
*dim
)
1584 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1585 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
1586 isl_space_copy(dim
));
1587 if (!scop
->stmts
[i
])
1591 isl_space_free(dim
);
1594 isl_space_free(dim
);
1595 return pet_scop_free(scop
);
1598 /* Replace all accesses to (0D) arrays that correspond to any of
1599 * the parameters used in "scop" by a value equal
1600 * to the corresponding parameter.
1602 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
1609 dim
= isl_set_get_space(scop
->context
);
1610 dim
= scop_collect_params(scop
, dim
);
1612 scop
= scop_detect_parameter_accesses(scop
, dim
);
1617 /* Add all read access relations (if "read" is set) and/or all write
1618 * access relations (if "write" is set) to "accesses" and return the result.
1620 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
1621 int read
, int write
, __isl_take isl_union_map
*accesses
)
1630 for (i
= 0; i
< expr
->n_arg
; ++i
)
1631 accesses
= expr_collect_accesses(expr
->args
[i
],
1632 read
, write
, accesses
);
1634 if (expr
->type
== pet_expr_access
&&
1635 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
1636 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
1637 accesses
= isl_union_map_add_map(accesses
,
1638 isl_map_copy(expr
->acc
.access
));
1643 /* Collect and return all read access relations (if "read" is set)
1644 * and/or all write * access relations (if "write" is set) in "stmt".
1646 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
1647 int read
, int write
, __isl_take isl_space
*dim
)
1649 isl_union_map
*accesses
;
1654 accesses
= isl_union_map_empty(dim
);
1655 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
1656 accesses
= isl_union_map_intersect_domain(accesses
,
1657 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
1662 /* Collect and return all read access relations (if "read" is set)
1663 * and/or all write * access relations (if "write" is set) in "scop".
1665 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
1666 int read
, int write
)
1669 isl_union_map
*accesses
;
1674 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1676 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1677 isl_union_map
*accesses_i
;
1678 isl_space
*dim
= isl_set_get_space(scop
->context
);
1679 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
1681 accesses
= isl_union_map_union(accesses
, accesses_i
);
1687 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
1689 return scop_collect_accesses(scop
, 1, 0);
1692 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
1694 return scop_collect_accesses(scop
, 0, 1);
1697 /* Collect and return the union of iteration domains in "scop".
1699 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
1703 isl_union_set
*domain
;
1708 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
1710 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1711 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
1712 domain
= isl_union_set_add_set(domain
, domain_i
);
1718 /* Collect and return the schedules of the statements in "scop".
1719 * The range is normalized to the maximal number of scheduling
1722 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
1725 isl_map
*schedule_i
;
1726 isl_union_map
*schedule
;
1727 int depth
, max_depth
= 0;
1732 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
1734 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1735 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
1736 if (depth
> max_depth
)
1740 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1741 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
1742 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
1743 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
1745 for (j
= depth
; j
< max_depth
; ++j
)
1746 schedule_i
= isl_map_fix_si(schedule_i
,
1748 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
1754 /* Does expression "expr" write to "id"?
1756 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
1761 for (i
= 0; i
< expr
->n_arg
; ++i
) {
1762 int writes
= expr_writes(expr
->args
[i
], id
);
1763 if (writes
< 0 || writes
)
1767 if (expr
->type
!= pet_expr_access
)
1769 if (!expr
->acc
.write
)
1771 if (!isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
))
1774 write_id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
1775 isl_id_free(write_id
);
1780 return write_id
== id
;
1783 /* Does statement "stmt" write to "id"?
1785 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
1787 return expr_writes(stmt
->body
, id
);
1790 /* Is there any write access in "scop" that accesses "id"?
1792 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
1799 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1800 int writes
= stmt_writes(scop
->stmts
[i
], id
);
1801 if (writes
< 0 || writes
)
1808 /* Reset the user pointer on all parameter ids in "set".
1810 static __isl_give isl_set
*set_anonymize(__isl_take isl_set
*set
)
1814 n
= isl_set_dim(set
, isl_dim_param
);
1815 for (i
= 0; i
< n
; ++i
) {
1816 isl_id
*id
= isl_set_get_dim_id(set
, isl_dim_param
, i
);
1817 const char *name
= isl_id_get_name(id
);
1818 set
= isl_set_set_dim_name(set
, isl_dim_param
, i
, name
);
1825 /* Reset the user pointer on all parameter ids in "map".
1827 static __isl_give isl_map
*map_anonymize(__isl_take isl_map
*map
)
1831 n
= isl_map_dim(map
, isl_dim_param
);
1832 for (i
= 0; i
< n
; ++i
) {
1833 isl_id
*id
= isl_map_get_dim_id(map
, isl_dim_param
, i
);
1834 const char *name
= isl_id_get_name(id
);
1835 map
= isl_map_set_dim_name(map
, isl_dim_param
, i
, name
);
1842 /* Reset the user pointer on all parameter ids in "array".
1844 static struct pet_array
*array_anonymize(struct pet_array
*array
)
1849 array
->context
= set_anonymize(array
->context
);
1850 array
->extent
= set_anonymize(array
->extent
);
1851 if (!array
->context
|| !array
->extent
)
1852 return pet_array_free(array
);
1857 /* Reset the user pointer on all parameter ids in "access".
1859 static __isl_give isl_map
*access_anonymize(__isl_take isl_map
*access
,
1862 access
= map_anonymize(access
);
1867 /* Reset the user pointer on all parameter ids in "stmt".
1869 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
1878 stmt
->domain
= set_anonymize(stmt
->domain
);
1879 stmt
->schedule
= map_anonymize(stmt
->schedule
);
1880 if (!stmt
->domain
|| !stmt
->schedule
)
1881 return pet_stmt_free(stmt
);
1883 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
1884 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
1885 &access_anonymize
, NULL
);
1887 return pet_stmt_free(stmt
);
1890 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
1891 &access_anonymize
, NULL
);
1893 return pet_stmt_free(stmt
);
1898 /* Reset the user pointer on all parameter ids in "scop".
1900 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
1907 scop
->context
= set_anonymize(scop
->context
);
1908 scop
->context_value
= set_anonymize(scop
->context_value
);
1909 if (!scop
->context
|| !scop
->context_value
)
1910 return pet_scop_free(scop
);
1912 for (i
= 0; i
< scop
->n_array
; ++i
) {
1913 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
1914 if (!scop
->arrays
[i
])
1915 return pet_scop_free(scop
);
1918 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1919 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
1920 if (!scop
->stmts
[i
])
1921 return pet_scop_free(scop
);
1927 /* Given a set "domain", return a wrapped relation with the given set
1928 * as domain and a range of dimension "n_arg", where each coordinate
1929 * is either unbounded or, if the corresponding element of args is of
1930 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
1932 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
1933 unsigned n_arg
, struct pet_expr
**args
,
1934 __isl_keep isl_union_map
*value_bounds
)
1939 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
1941 map
= isl_map_from_domain(domain
);
1942 space
= isl_map_get_space(map
);
1943 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
1945 for (i
= 0; i
< n_arg
; ++i
) {
1947 struct pet_expr
*arg
= args
[i
];
1951 map_i
= isl_map_universe(isl_space_copy(space
));
1952 if (arg
->type
== pet_expr_access
) {
1954 id
= isl_map_get_tuple_id(arg
->acc
.access
, isl_dim_out
);
1955 space2
= isl_space_alloc(ctx
, 0, 0, 1);
1956 space2
= isl_space_set_tuple_id(space2
, isl_dim_in
, id
);
1957 vb
= isl_union_map_extract_map(value_bounds
, space2
);
1958 if (!isl_map_plain_is_empty(vb
))
1959 map_i
= isl_map_intersect_range(map_i
,
1964 map
= isl_map_flat_range_product(map
, map_i
);
1966 isl_space_free(space
);
1968 return isl_map_wrap(map
);
1971 /* Data used in access_gist() callback.
1973 struct pet_access_gist_data
{
1975 isl_union_map
*value_bounds
;
1978 /* Given an expression "expr" of type pet_expr_access, compute
1979 * the gist of the associated access relation with respect to
1980 * data->domain and the bounds on the values of the arguments
1981 * of the expression.
1983 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
1985 struct pet_access_gist_data
*data
= user
;
1988 domain
= isl_set_copy(data
->domain
);
1989 if (expr
->n_arg
> 0)
1990 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
1991 data
->value_bounds
);
1993 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
, domain
);
1994 if (!expr
->acc
.access
)
1995 return pet_expr_free(expr
);
2000 /* Compute the gist of the iteration domain and all access relations
2001 * of "stmt" based on the constraints on the parameters specified by "context"
2002 * and the constraints on the values of nested accesses specified
2003 * by "value_bounds".
2005 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2006 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2011 struct pet_access_gist_data data
;
2016 data
.domain
= isl_set_copy(stmt
->domain
);
2017 data
.value_bounds
= value_bounds
;
2018 if (stmt
->n_arg
> 0)
2019 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
2021 data
.domain
= isl_set_intersect_params(data
.domain
,
2022 isl_set_copy(context
));
2024 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2025 stmt
->args
[i
] = pet_expr_foreach_access_expr(stmt
->args
[i
],
2026 &access_gist
, &data
);
2031 stmt
->body
= pet_expr_foreach_access_expr(stmt
->body
,
2032 &access_gist
, &data
);
2036 isl_set_free(data
.domain
);
2038 space
= isl_set_get_space(stmt
->domain
);
2039 if (isl_space_is_wrapping(space
))
2040 space
= isl_space_domain(isl_space_unwrap(space
));
2041 domain
= isl_set_universe(space
);
2042 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2043 if (stmt
->n_arg
> 0)
2044 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
2046 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
2048 return pet_stmt_free(stmt
);
2052 isl_set_free(data
.domain
);
2053 return pet_stmt_free(stmt
);
2056 /* Compute the gist of the extent of the array
2057 * based on the constraints on the parameters specified by "context".
2059 static struct pet_array
*array_gist(struct pet_array
*array
,
2060 __isl_keep isl_set
*context
)
2065 array
->extent
= isl_set_gist_params(array
->extent
,
2066 isl_set_copy(context
));
2068 return pet_array_free(array
);
2073 /* Compute the gist of all sets and relations in "scop"
2074 * based on the constraints on the parameters specified by "scop->context"
2075 * and the constraints on the values of nested accesses specified
2076 * by "value_bounds".
2078 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
2079 __isl_keep isl_union_map
*value_bounds
)
2086 scop
->context
= isl_set_coalesce(scop
->context
);
2088 return pet_scop_free(scop
);
2090 for (i
= 0; i
< scop
->n_array
; ++i
) {
2091 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
2092 if (!scop
->arrays
[i
])
2093 return pet_scop_free(scop
);
2096 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2097 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
2099 if (!scop
->stmts
[i
])
2100 return pet_scop_free(scop
);
2106 /* Intersect the context of "scop" with "context".
2107 * To ensure that we don't introduce any unnamed parameters in
2108 * the context of "scop", we first remove the unnamed parameters
2111 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
2112 __isl_take isl_set
*context
)
2117 context
= set_project_out_unnamed_params(context
);
2118 scop
->context
= isl_set_intersect(scop
->context
, context
);
2120 return pet_scop_free(scop
);
2124 isl_set_free(context
);
2125 return pet_scop_free(scop
);