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
36 #include <isl/constraint.h>
37 #include <isl/union_set.h>
41 #define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
43 static char *type_str
[] = {
44 [pet_expr_access
] = "access",
45 [pet_expr_call
] = "call",
46 [pet_expr_cast
] = "cast",
47 [pet_expr_double
] = "double",
48 [pet_expr_unary
] = "unary",
49 [pet_expr_binary
] = "binary",
50 [pet_expr_ternary
] = "ternary"
53 static char *op_str
[] = {
54 [pet_op_add_assign
] = "+=",
55 [pet_op_sub_assign
] = "-=",
56 [pet_op_mul_assign
] = "*=",
57 [pet_op_div_assign
] = "/=",
58 [pet_op_assign
] = "=",
69 [pet_op_post_inc
] = "++",
70 [pet_op_post_dec
] = "--",
71 [pet_op_pre_inc
] = "++",
72 [pet_op_pre_dec
] = "--",
73 [pet_op_address_of
] = "&",
74 [pet_op_kill
] = "kill"
77 /* pet_scop with extra information that is only used during parsing.
79 * In particular, we keep track of conditions under which we want
80 * to skip the rest of the current loop iteration (skip[pet_skip_now])
81 * and of conditions under which we want to skip subsequent
82 * loop iterations (skip[pet_skip_later]).
84 * The conditions are represented either by a variable, which
85 * is assumed to attain values zero and one, or by a boolean affine
86 * expression. The condition holds if the variable has value one
87 * or if the affine expression has value one (typically for only
88 * part of the parameter space).
90 * A missing condition (skip[type] == NULL) means that we don't want
99 const char *pet_op_str(enum pet_op_type op
)
104 int pet_op_is_inc_dec(enum pet_op_type op
)
106 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
107 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
110 const char *pet_type_str(enum pet_expr_type type
)
112 return type_str
[type
];
115 enum pet_op_type
pet_str_op(const char *str
)
119 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
120 if (!strcmp(op_str
[i
], str
))
126 enum pet_expr_type
pet_str_type(const char *str
)
130 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
131 if (!strcmp(type_str
[i
], str
))
137 /* Construct a pet_expr from an access relation.
138 * By default, it is considered to be a read access.
140 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
142 isl_ctx
*ctx
= isl_map_get_ctx(access
);
143 struct pet_expr
*expr
;
147 expr
= isl_calloc_type(ctx
, struct pet_expr
);
151 expr
->type
= pet_expr_access
;
152 expr
->acc
.access
= access
;
158 isl_map_free(access
);
162 /* Construct a pet_expr that kills the elements specified by "access".
164 struct pet_expr
*pet_expr_kill_from_access(__isl_take isl_map
*access
)
167 struct pet_expr
*expr
;
169 ctx
= isl_map_get_ctx(access
);
170 expr
= pet_expr_from_access(access
);
174 return pet_expr_new_unary(ctx
, pet_op_kill
, expr
);
177 /* Construct a unary pet_expr that performs "op" on "arg".
179 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
180 struct pet_expr
*arg
)
182 struct pet_expr
*expr
;
186 expr
= isl_alloc_type(ctx
, struct pet_expr
);
190 expr
->type
= pet_expr_unary
;
193 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
196 expr
->args
[pet_un_arg
] = arg
;
204 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
206 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
207 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
209 struct pet_expr
*expr
;
213 expr
= isl_alloc_type(ctx
, struct pet_expr
);
217 expr
->type
= pet_expr_binary
;
220 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
223 expr
->args
[pet_bin_lhs
] = lhs
;
224 expr
->args
[pet_bin_rhs
] = rhs
;
233 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
235 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
236 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
238 struct pet_expr
*expr
;
240 if (!cond
|| !lhs
|| !rhs
)
242 expr
= isl_alloc_type(ctx
, struct pet_expr
);
246 expr
->type
= pet_expr_ternary
;
248 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
251 expr
->args
[pet_ter_cond
] = cond
;
252 expr
->args
[pet_ter_true
] = lhs
;
253 expr
->args
[pet_ter_false
] = rhs
;
263 /* Construct a call pet_expr that calls function "name" with "n_arg"
264 * arguments. The caller is responsible for filling in the arguments.
266 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
269 struct pet_expr
*expr
;
271 expr
= isl_alloc_type(ctx
, struct pet_expr
);
275 expr
->type
= pet_expr_call
;
277 expr
->name
= strdup(name
);
278 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
279 if (!expr
->name
|| !expr
->args
)
280 return pet_expr_free(expr
);
285 /* Construct a pet_expr that represents the cast of "arg" to "type_name".
287 struct pet_expr
*pet_expr_new_cast(isl_ctx
*ctx
, const char *type_name
,
288 struct pet_expr
*arg
)
290 struct pet_expr
*expr
;
295 expr
= isl_alloc_type(ctx
, struct pet_expr
);
299 expr
->type
= pet_expr_cast
;
301 expr
->type_name
= strdup(type_name
);
302 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
303 if (!expr
->type_name
|| !expr
->args
)
315 /* Construct a pet_expr that represents the double "d".
317 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double val
, const char *s
)
319 struct pet_expr
*expr
;
321 expr
= isl_calloc_type(ctx
, struct pet_expr
);
325 expr
->type
= pet_expr_double
;
327 expr
->d
.s
= strdup(s
);
329 return pet_expr_free(expr
);
334 void *pet_expr_free(struct pet_expr
*expr
)
341 for (i
= 0; i
< expr
->n_arg
; ++i
)
342 pet_expr_free(expr
->args
[i
]);
345 switch (expr
->type
) {
346 case pet_expr_access
:
347 isl_map_free(expr
->acc
.access
);
353 free(expr
->type_name
);
355 case pet_expr_double
:
359 case pet_expr_binary
:
360 case pet_expr_ternary
:
368 static void expr_dump(struct pet_expr
*expr
, int indent
)
375 fprintf(stderr
, "%*s", indent
, "");
377 switch (expr
->type
) {
378 case pet_expr_double
:
379 fprintf(stderr
, "%s\n", expr
->d
.s
);
381 case pet_expr_access
:
382 isl_map_dump(expr
->acc
.access
);
383 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
385 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
386 "", expr
->acc
.write
);
387 for (i
= 0; i
< expr
->n_arg
; ++i
)
388 expr_dump(expr
->args
[i
], indent
+ 2);
391 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
392 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
394 case pet_expr_binary
:
395 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
396 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
397 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
399 case pet_expr_ternary
:
400 fprintf(stderr
, "?:\n");
401 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
402 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
403 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
406 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
407 for (i
= 0; i
< expr
->n_arg
; ++i
)
408 expr_dump(expr
->args
[i
], indent
+ 2);
411 fprintf(stderr
, "(%s)\n", expr
->type_name
);
412 for (i
= 0; i
< expr
->n_arg
; ++i
)
413 expr_dump(expr
->args
[i
], indent
+ 2);
418 void pet_expr_dump(struct pet_expr
*expr
)
423 /* Does "expr" represent an access to an unnamed space, i.e.,
424 * does it represent an affine expression?
426 int pet_expr_is_affine(struct pet_expr
*expr
)
432 if (expr
->type
!= pet_expr_access
)
435 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
442 /* Return 1 if the two pet_exprs are equivalent.
444 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
448 if (!expr1
|| !expr2
)
451 if (expr1
->type
!= expr2
->type
)
453 if (expr1
->n_arg
!= expr2
->n_arg
)
455 for (i
= 0; i
< expr1
->n_arg
; ++i
)
456 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
458 switch (expr1
->type
) {
459 case pet_expr_double
:
460 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
462 if (expr1
->d
.val
!= expr2
->d
.val
)
465 case pet_expr_access
:
466 if (expr1
->acc
.read
!= expr2
->acc
.read
)
468 if (expr1
->acc
.write
!= expr2
->acc
.write
)
470 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
472 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
476 case pet_expr_binary
:
477 case pet_expr_ternary
:
478 if (expr1
->op
!= expr2
->op
)
482 if (strcmp(expr1
->name
, expr2
->name
))
486 if (strcmp(expr1
->type_name
, expr2
->type_name
))
494 /* Add extra conditions on the parameters to all access relations in "expr".
496 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
497 __isl_take isl_set
*cond
)
504 for (i
= 0; i
< expr
->n_arg
; ++i
) {
505 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
511 if (expr
->type
== pet_expr_access
) {
512 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
514 if (!expr
->acc
.access
)
522 return pet_expr_free(expr
);
525 /* Modify all access relations in "expr" by calling "fn" on them.
527 struct pet_expr
*pet_expr_foreach_access(struct pet_expr
*expr
,
528 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
536 for (i
= 0; i
< expr
->n_arg
; ++i
) {
537 expr
->args
[i
] = pet_expr_foreach_access(expr
->args
[i
], fn
, user
);
539 return pet_expr_free(expr
);
542 if (expr
->type
== pet_expr_access
) {
543 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
544 if (!expr
->acc
.access
)
545 return pet_expr_free(expr
);
551 /* Modify all expressions of type pet_expr_access in "expr"
552 * by calling "fn" on them.
554 struct pet_expr
*pet_expr_foreach_access_expr(struct pet_expr
*expr
,
555 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
563 for (i
= 0; i
< expr
->n_arg
; ++i
) {
564 expr
->args
[i
] = pet_expr_foreach_access_expr(expr
->args
[i
],
567 return pet_expr_free(expr
);
570 if (expr
->type
== pet_expr_access
)
571 expr
= fn(expr
, user
);
576 /* Modify the given access relation based on the given iteration space
578 * If the access has any arguments then the domain of the access relation
579 * is a wrapped mapping from the iteration space to the space of
580 * argument values. We only need to change the domain of this wrapped
581 * mapping, so we extend the input transformation with an identity mapping
582 * on the space of argument values.
584 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
587 isl_map
*update
= user
;
590 update
= isl_map_copy(update
);
592 dim
= isl_map_get_space(access
);
593 dim
= isl_space_domain(dim
);
594 if (!isl_space_is_wrapping(dim
))
598 dim
= isl_space_unwrap(dim
);
599 dim
= isl_space_range(dim
);
600 dim
= isl_space_map_from_set(dim
);
601 id
= isl_map_identity(dim
);
602 update
= isl_map_product(update
, id
);
605 return isl_map_apply_domain(access
, update
);
608 /* Modify all access relations in "expr" based on the given iteration space
611 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
612 __isl_take isl_map
*update
)
614 expr
= pet_expr_foreach_access(expr
, &update_domain
, update
);
615 isl_map_free(update
);
619 /* Construct a pet_stmt with given line number and statement
620 * number from a pet_expr.
621 * The initial iteration domain is the zero-dimensional universe.
622 * The name of the domain is given by "label" if it is non-NULL.
623 * Otherwise, the name is constructed as S_<id>.
624 * The domains of all access relations are modified to refer
625 * to the statement iteration domain.
627 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
628 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
630 struct pet_stmt
*stmt
;
640 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
644 dim
= isl_space_set_alloc(ctx
, 0, 0);
646 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
648 snprintf(name
, sizeof(name
), "S_%d", id
);
649 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
651 dom
= isl_set_universe(isl_space_copy(dim
));
652 sched
= isl_map_from_domain(isl_set_copy(dom
));
654 dim
= isl_space_from_range(dim
);
655 add_name
= isl_map_universe(dim
);
656 expr
= expr_update_domain(expr
, add_name
);
660 stmt
->schedule
= sched
;
663 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
664 return pet_stmt_free(stmt
);
669 return pet_expr_free(expr
);
672 void *pet_stmt_free(struct pet_stmt
*stmt
)
679 isl_set_free(stmt
->domain
);
680 isl_map_free(stmt
->schedule
);
681 pet_expr_free(stmt
->body
);
683 for (i
= 0; i
< stmt
->n_arg
; ++i
)
684 pet_expr_free(stmt
->args
[i
]);
691 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
698 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
699 fprintf(stderr
, "%*s", indent
, "");
700 isl_set_dump(stmt
->domain
);
701 fprintf(stderr
, "%*s", indent
, "");
702 isl_map_dump(stmt
->schedule
);
703 expr_dump(stmt
->body
, indent
);
704 for (i
= 0; i
< stmt
->n_arg
; ++i
)
705 expr_dump(stmt
->args
[i
], indent
+ 2);
708 void pet_stmt_dump(struct pet_stmt
*stmt
)
713 struct pet_array
*pet_array_free(struct pet_array
*array
)
718 isl_set_free(array
->context
);
719 isl_set_free(array
->extent
);
720 isl_set_free(array
->value_bounds
);
721 free(array
->element_type
);
727 void pet_array_dump(struct pet_array
*array
)
732 isl_set_dump(array
->context
);
733 isl_set_dump(array
->extent
);
734 isl_set_dump(array
->value_bounds
);
735 fprintf(stderr
, "%s %s\n", array
->element_type
,
736 array
->live_out
? "live-out" : "");
739 /* Alloc a pet_scop structure, with extra room for information that
740 * is only used during parsing.
742 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
744 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
747 /* Construct a pet_scop with room for n statements.
749 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
752 struct pet_scop
*scop
;
754 scop
= pet_scop_alloc(ctx
);
758 space
= isl_space_params_alloc(ctx
, 0);
759 scop
->context
= isl_set_universe(isl_space_copy(space
));
760 scop
->context_value
= isl_set_universe(space
);
761 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
762 if (!scop
->context
|| !scop
->stmts
)
763 return pet_scop_free(scop
);
770 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
772 return scop_alloc(ctx
, 0);
775 /* Update "context" with respect to the valid parameter values for "access".
777 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
778 __isl_take isl_set
*context
)
780 context
= isl_set_intersect(context
,
781 isl_map_params(isl_map_copy(access
)));
785 /* Update "context" with respect to the valid parameter values for "expr".
787 * If "expr" represents a ternary operator, then a parameter value
788 * needs to be valid for the condition and for at least one of the
789 * remaining two arguments.
790 * If the condition is an affine expression, then we can be a bit more specific.
791 * The parameter then has to be valid for the second argument for
792 * non-zero accesses and valid for the third argument for zero accesses.
794 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
795 __isl_take isl_set
*context
)
799 if (expr
->type
== pet_expr_ternary
) {
801 isl_set
*context1
, *context2
;
803 is_aff
= pet_expr_is_affine(expr
->args
[0]);
807 context
= expr_extract_context(expr
->args
[0], context
);
808 context1
= expr_extract_context(expr
->args
[1],
809 isl_set_copy(context
));
810 context2
= expr_extract_context(expr
->args
[2], context
);
816 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
817 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
818 zero_set
= isl_map_params(access
);
819 context1
= isl_set_subtract(context1
,
820 isl_set_copy(zero_set
));
821 context2
= isl_set_intersect(context2
, zero_set
);
824 context
= isl_set_union(context1
, context2
);
825 context
= isl_set_coalesce(context
);
830 for (i
= 0; i
< expr
->n_arg
; ++i
)
831 context
= expr_extract_context(expr
->args
[i
], context
);
833 if (expr
->type
== pet_expr_access
)
834 context
= access_extract_context(expr
->acc
.access
, context
);
838 isl_set_free(context
);
842 /* Update "context" with respect to the valid parameter values for "stmt".
844 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
845 __isl_take isl_set
*context
)
849 for (i
= 0; i
< stmt
->n_arg
; ++i
)
850 context
= expr_extract_context(stmt
->args
[i
], context
);
852 context
= expr_extract_context(stmt
->body
, context
);
857 /* Construct a pet_scop that contains the given pet_stmt.
859 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
861 struct pet_scop
*scop
;
866 scop
= scop_alloc(ctx
, 1);
870 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
874 scop
->stmts
[0] = stmt
;
883 /* Does "set" represent an element of an unnamed space, i.e.,
884 * does it represent an affine expression?
886 static int set_is_affine(__isl_keep isl_set
*set
)
890 has_id
= isl_set_has_tuple_id(set
);
897 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
898 * ext may be equal to either ext1 or ext2.
900 * The two skips that need to be combined are assumed to be affine expressions.
902 * We need to skip in ext if we need to skip in either ext1 or ext2.
903 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
905 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
906 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
909 isl_set
*set
, *skip1
, *skip2
;
913 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
915 if (!ext1
->skip
[type
]) {
918 ext
->skip
[type
] = ext2
->skip
[type
];
919 ext2
->skip
[type
] = NULL
;
922 if (!ext2
->skip
[type
]) {
925 ext
->skip
[type
] = ext1
->skip
[type
];
926 ext1
->skip
[type
] = NULL
;
930 if (!set_is_affine(ext1
->skip
[type
]) ||
931 !set_is_affine(ext2
->skip
[type
]))
932 isl_die(isl_set_get_ctx(ext1
->skip
[type
]), isl_error_internal
,
933 "can only combine affine skips",
934 return pet_scop_free(&ext
->scop
));
936 skip1
= isl_set_copy(ext1
->skip
[type
]);
937 skip2
= isl_set_copy(ext2
->skip
[type
]);
938 set
= isl_set_intersect(
939 isl_set_fix_si(isl_set_copy(skip1
), isl_dim_set
, 0, 0),
940 isl_set_fix_si(isl_set_copy(skip2
), isl_dim_set
, 0, 0));
941 set
= isl_set_union(set
, isl_set_fix_si(skip1
, isl_dim_set
, 0, 1));
942 set
= isl_set_union(set
, isl_set_fix_si(skip2
, isl_dim_set
, 0, 1));
943 set
= isl_set_coalesce(set
);
944 isl_set_free(ext1
->skip
[type
]);
945 ext1
->skip
[type
] = NULL
;
946 isl_set_free(ext2
->skip
[type
]);
947 ext2
->skip
[type
] = NULL
;
948 ext
->skip
[type
] = set
;
949 if (!ext
->skip
[type
])
950 return pet_scop_free(&ext
->scop
);
955 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
956 * where type takes on the values pet_skip_now and pet_skip_later.
957 * scop may be equal to either scop1 or scop2.
959 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
960 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
962 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
963 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
964 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
966 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
967 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
971 /* Update scop->start and scop->end to include the region from "start"
972 * to "end". In particular, if scop->end == 0, then "scop" does not
973 * have any offset information yet and we simply take the information
974 * from "start" and "end". Otherwise, we update the fields if the
975 * region from "start" to "end" is not already included.
977 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
978 unsigned start
, unsigned end
)
982 if (scop
->end
== 0) {
986 if (start
< scop
->start
)
995 /* Combine the offset information of "scop1" and "scop2" into "scop".
997 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
998 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1001 scop
= pet_scop_update_start_end(scop
,
1002 scop1
->start
, scop1
->end
);
1004 scop
= pet_scop_update_start_end(scop
,
1005 scop2
->start
, scop2
->end
);
1009 /* Construct a pet_scop that contains the offset information,
1010 * arrays, statements and skip information in "scop1" and "scop2".
1012 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1013 struct pet_scop
*scop2
)
1016 struct pet_scop
*scop
;
1018 if (!scop1
|| !scop2
)
1021 if (scop1
->n_stmt
== 0) {
1022 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
1023 pet_scop_free(scop1
);
1027 if (scop2
->n_stmt
== 0) {
1028 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
1029 pet_scop_free(scop2
);
1033 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
1037 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
1038 scop1
->n_array
+ scop2
->n_array
);
1041 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
1043 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
1044 scop
->stmts
[i
] = scop1
->stmts
[i
];
1045 scop1
->stmts
[i
] = NULL
;
1048 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
1049 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
1050 scop2
->stmts
[i
] = NULL
;
1053 for (i
= 0; i
< scop1
->n_array
; ++i
) {
1054 scop
->arrays
[i
] = scop1
->arrays
[i
];
1055 scop1
->arrays
[i
] = NULL
;
1058 for (i
= 0; i
< scop2
->n_array
; ++i
) {
1059 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
1060 scop2
->arrays
[i
] = NULL
;
1063 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
1064 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
1065 scop
= scop_combine_skips(scop
, scop1
, scop2
);
1066 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
1068 pet_scop_free(scop1
);
1069 pet_scop_free(scop2
);
1072 pet_scop_free(scop1
);
1073 pet_scop_free(scop2
);
1077 /* Apply the skip condition "skip" to "scop".
1078 * That is, make sure "scop" is not executed when the condition holds.
1080 * If "skip" is an affine expression, we add the conditions under
1081 * which the expression is zero to the iteration domains.
1082 * Otherwise, we add a filter on the variable attaining the value zero.
1084 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
1085 __isl_take isl_set
*skip
)
1093 is_aff
= set_is_affine(skip
);
1098 return pet_scop_filter(scop
, isl_map_from_range(skip
), 0);
1100 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 0);
1101 scop
= pet_scop_restrict(scop
, isl_set_params(skip
));
1106 return pet_scop_free(scop
);
1109 /* Construct a pet_scop that contains the arrays, statements and
1110 * skip information in "scop1" and "scop2", where the two scops
1111 * are executed "in sequence". That is, breaks and continues
1112 * in scop1 have an effect on scop2.
1114 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1115 struct pet_scop
*scop2
)
1117 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
1118 scop2
= restrict_skip(scop2
,
1119 pet_scop_get_skip(scop1
, pet_skip_now
));
1120 return pet_scop_add(ctx
, scop1
, scop2
);
1123 /* Construct a pet_scop that contains the arrays, statements and
1124 * skip information in "scop1" and "scop2", where the two scops
1125 * are executed "in parallel". That is, any break or continue
1126 * in scop1 has no effect on scop2.
1128 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1129 struct pet_scop
*scop2
)
1131 return pet_scop_add(ctx
, scop1
, scop2
);
1134 void *pet_scop_free(struct pet_scop
*scop
)
1137 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1141 isl_set_free(scop
->context
);
1142 isl_set_free(scop
->context_value
);
1144 for (i
= 0; i
< scop
->n_array
; ++i
)
1145 pet_array_free(scop
->arrays
[i
]);
1148 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1149 pet_stmt_free(scop
->stmts
[i
]);
1151 isl_set_free(ext
->skip
[pet_skip_now
]);
1152 isl_set_free(ext
->skip
[pet_skip_later
]);
1157 void pet_scop_dump(struct pet_scop
*scop
)
1160 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1165 isl_set_dump(scop
->context
);
1166 isl_set_dump(scop
->context_value
);
1167 for (i
= 0; i
< scop
->n_array
; ++i
)
1168 pet_array_dump(scop
->arrays
[i
]);
1169 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1170 pet_stmt_dump(scop
->stmts
[i
]);
1173 fprintf(stderr
, "skip\n");
1174 isl_set_dump(ext
->skip
[0]);
1175 isl_set_dump(ext
->skip
[1]);
1179 /* Return 1 if the two pet_arrays are equivalent.
1181 * We don't compare element_size as this may be target dependent.
1183 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
1185 if (!array1
|| !array2
)
1188 if (!isl_set_is_equal(array1
->context
, array2
->context
))
1190 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
1192 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
1194 if (array1
->value_bounds
&&
1195 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
1197 if (strcmp(array1
->element_type
, array2
->element_type
))
1199 if (array1
->live_out
!= array2
->live_out
)
1201 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
1203 if (array1
->declared
!= array2
->declared
)
1205 if (array1
->exposed
!= array2
->exposed
)
1211 /* Return 1 if the two pet_stmts are equivalent.
1213 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
1217 if (!stmt1
|| !stmt2
)
1220 if (stmt1
->line
!= stmt2
->line
)
1222 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
1224 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
1226 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
1228 if (stmt1
->n_arg
!= stmt2
->n_arg
)
1230 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
1231 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
1238 /* Return 1 if the two pet_scops are equivalent.
1240 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1244 if (!scop1
|| !scop2
)
1247 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1249 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1252 if (scop1
->n_array
!= scop2
->n_array
)
1254 for (i
= 0; i
< scop1
->n_array
; ++i
)
1255 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1258 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1260 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1261 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1267 /* Prefix the schedule of "stmt" with an extra dimension with constant
1270 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1275 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1276 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1277 if (!stmt
->schedule
)
1278 return pet_stmt_free(stmt
);
1283 /* Prefix the schedules of all statements in "scop" with an extra
1284 * dimension with constant value "pos".
1286 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1293 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1294 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1295 if (!scop
->stmts
[i
])
1296 return pet_scop_free(scop
);
1302 /* Given a set with a parameter at "param_pos" that refers to the
1303 * iterator, "move" the iterator to the first set dimension.
1304 * That is, essentially equate the parameter to the first set dimension
1305 * and then project it out.
1307 * The first set dimension may however refer to a virtual iterator,
1308 * while the parameter refers to the "real" iterator.
1309 * We therefore need to take into account the mapping "iv_map", which
1310 * maps the virtual iterator to the real iterator.
1311 * In particular, we equate the set dimension to the input of the map
1312 * and the parameter to the output of the map and then project out
1313 * everything we don't need anymore.
1315 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1316 int param_pos
, __isl_take isl_map
*iv_map
)
1319 map
= isl_map_from_domain(set
);
1320 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1321 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1322 iv_map
= isl_map_align_params(iv_map
, isl_map_get_space(map
));
1323 map
= isl_map_apply_range(map
, iv_map
);
1324 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1325 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1326 return isl_map_domain(map
);
1329 /* Data used in embed_access.
1330 * extend adds an iterator to the iteration domain
1331 * iv_map maps the virtual iterator to the real iterator
1332 * var_id represents the induction variable of the corresponding loop
1334 struct pet_embed_access
{
1340 /* Embed the access relation in an extra outer loop.
1342 * We first update the iteration domain to insert the extra dimension.
1344 * If the access refers to the induction variable, then it is
1345 * turned into an access to the set of integers with index (and value)
1346 * equal to the induction variable.
1348 * If the induction variable appears in the constraints (as a parameter),
1349 * then the parameter is equated to the newly introduced iteration
1350 * domain dimension and subsequently projected out.
1352 * Similarly, if the accessed array is a virtual array (with user
1353 * pointer equal to NULL), as created by create_test_access,
1354 * then it is extended along with the domain of the access.
1356 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
1359 struct pet_embed_access
*data
= user
;
1360 isl_id
*array_id
= NULL
;
1363 access
= update_domain(access
, data
->extend
);
1365 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1366 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1367 if (array_id
== data
->var_id
||
1368 (array_id
&& !isl_id_get_user(array_id
))) {
1369 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1370 access
= isl_map_equate(access
,
1371 isl_dim_in
, 0, isl_dim_out
, 0);
1372 if (array_id
== data
->var_id
)
1373 access
= isl_map_apply_range(access
,
1374 isl_map_copy(data
->iv_map
));
1376 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1377 isl_id_copy(array_id
));
1379 isl_id_free(array_id
);
1381 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1383 isl_set
*set
= isl_map_wrap(access
);
1384 set
= internalize_iv(set
, pos
, isl_map_copy(data
->iv_map
));
1385 access
= isl_set_unwrap(set
);
1387 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1388 isl_id_copy(data
->var_id
));
1393 /* Embed all access relations in "expr" in an extra loop.
1394 * "extend" inserts an outer loop iterator in the iteration domains.
1395 * "iv_map" maps the virtual iterator to the real iterator
1396 * "var_id" represents the induction variable.
1398 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
1399 __isl_take isl_map
*extend
, __isl_take isl_map
*iv_map
,
1400 __isl_keep isl_id
*var_id
)
1402 struct pet_embed_access data
=
1403 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1405 expr
= pet_expr_foreach_access(expr
, &embed_access
, &data
);
1406 isl_map_free(iv_map
);
1407 isl_map_free(extend
);
1411 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1412 * "dom" and schedule "sched". "var_id" represents the induction variable
1413 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1414 * That is, it maps the iterator used in "dom" and the domain of "sched"
1415 * to the iterator that some of the parameters in "stmt" may refer to.
1417 * The iteration domain and schedule of the statement are updated
1418 * according to the iteration domain and schedule of the new loop.
1419 * If stmt->domain is a wrapped map, then the iteration domain
1420 * is the domain of this map, so we need to be careful to adjust
1423 * If the induction variable appears in the constraints (as a parameter)
1424 * of the current iteration domain or the schedule of the statement,
1425 * then the parameter is equated to the newly introduced iteration
1426 * domain dimension and subsequently projected out.
1428 * Finally, all access relations are updated based on the extra loop.
1430 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1431 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1432 __isl_take isl_map
*iv_map
, __isl_take isl_id
*var_id
)
1443 if (isl_set_is_wrapping(stmt
->domain
)) {
1448 map
= isl_set_unwrap(stmt
->domain
);
1449 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1450 ran_dim
= isl_space_range(isl_map_get_space(map
));
1451 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1452 isl_set_universe(ran_dim
));
1453 map
= isl_map_flat_domain_product(ext
, map
);
1454 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1455 isl_id_copy(stmt_id
));
1456 dim
= isl_space_domain(isl_map_get_space(map
));
1457 stmt
->domain
= isl_map_wrap(map
);
1459 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1460 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1462 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1463 isl_id_copy(stmt_id
));
1464 dim
= isl_set_get_space(stmt
->domain
);
1467 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1469 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1470 isl_map_copy(iv_map
));
1472 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1473 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1474 isl_dim_in
, stmt_id
);
1476 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1478 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1479 set
= internalize_iv(set
, pos
, isl_map_copy(iv_map
));
1480 stmt
->schedule
= isl_set_unwrap(set
);
1483 dim
= isl_space_map_from_set(dim
);
1484 extend
= isl_map_identity(dim
);
1485 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
1486 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
1487 isl_map_get_tuple_id(extend
, isl_dim_out
));
1488 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1489 stmt
->args
[i
] = expr_embed(stmt
->args
[i
], isl_map_copy(extend
),
1490 isl_map_copy(iv_map
), var_id
);
1491 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1494 isl_id_free(var_id
);
1496 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1498 return pet_stmt_free(stmt
);
1499 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1500 return pet_stmt_free(stmt
);
1504 isl_map_free(sched
);
1505 isl_map_free(iv_map
);
1506 isl_id_free(var_id
);
1510 /* Embed the given pet_array in an extra outer loop with iteration domain
1512 * This embedding only has an effect on virtual arrays (those with
1513 * user pointer equal to NULL), which need to be extended along with
1514 * the iteration domain.
1516 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1517 __isl_take isl_set
*dom
)
1519 isl_id
*array_id
= NULL
;
1524 if (isl_set_has_tuple_id(array
->extent
))
1525 array_id
= isl_set_get_tuple_id(array
->extent
);
1527 if (array_id
&& !isl_id_get_user(array_id
)) {
1528 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1529 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1532 isl_id_free(array_id
);
1541 /* Project out all unnamed parameters from "set" and return the result.
1543 static __isl_give isl_set
*set_project_out_unnamed_params(
1544 __isl_take isl_set
*set
)
1548 n
= isl_set_dim(set
, isl_dim_param
);
1549 for (i
= n
- 1; i
>= 0; --i
) {
1550 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1552 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1558 /* Update the context with respect to an embedding into a loop
1559 * with iteration domain "dom" and induction variable "id".
1560 * "iv_map" maps a possibly virtual iterator (used in "dom")
1561 * to the real iterator (parameter "id").
1563 * If the current context is independent of "id", we don't need
1565 * Otherwise, a parameter value is invalid for the embedding if
1566 * any of the corresponding iterator values is invalid.
1567 * That is, a parameter value is valid only if all the corresponding
1568 * iterator values are valid.
1569 * We therefore compute the set of parameters
1571 * forall i in dom : valid (i)
1575 * not exists i in dom : not valid(i)
1579 * not exists i in dom \ valid(i)
1581 * Before we subtract valid(i) from dom, we first need to map
1582 * the real iterator to the virtual iterator.
1584 * If there are any unnamed parameters in "dom", then we consider
1585 * a parameter value to be valid if it is valid for any value of those
1586 * unnamed parameters. They are therefore projected out at the end.
1588 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1589 __isl_keep isl_set
*dom
, __isl_keep isl_map
*iv_map
,
1590 __isl_keep isl_id
*id
)
1594 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1598 context
= isl_set_from_params(context
);
1599 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1600 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1601 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1602 context
= isl_set_apply(context
, isl_map_reverse(isl_map_copy(iv_map
)));
1603 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1604 context
= isl_set_params(context
);
1605 context
= isl_set_complement(context
);
1606 context
= set_project_out_unnamed_params(context
);
1610 /* Embed all statements and arrays in "scop" in an extra outer loop
1611 * with iteration domain "dom" and schedule "sched".
1612 * "id" represents the induction variable of the loop.
1613 * "iv_map" maps a possibly virtual iterator to the real iterator.
1614 * That is, it maps the iterator used in "dom" and the domain of "sched"
1615 * to the iterator that some of the parameters in "scop" may refer to.
1617 * Any skip conditions within the loop have no effect outside of the loop.
1618 * The caller is responsible for making sure skip[pet_skip_later] has been
1619 * taken into account.
1621 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1622 __isl_take isl_map
*sched
, __isl_take isl_map
*iv_map
,
1623 __isl_take isl_id
*id
)
1630 pet_scop_reset_skip(scop
, pet_skip_now
);
1631 pet_scop_reset_skip(scop
, pet_skip_later
);
1633 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1637 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1638 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1639 isl_set_copy(dom
), isl_map_copy(sched
),
1640 isl_map_copy(iv_map
), isl_id_copy(id
));
1641 if (!scop
->stmts
[i
])
1645 for (i
= 0; i
< scop
->n_array
; ++i
) {
1646 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1648 if (!scop
->arrays
[i
])
1653 isl_map_free(sched
);
1654 isl_map_free(iv_map
);
1659 isl_map_free(sched
);
1660 isl_map_free(iv_map
);
1662 return pet_scop_free(scop
);
1665 /* Add extra conditions on the parameters to iteration domain of "stmt".
1667 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1668 __isl_take isl_set
*cond
)
1673 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1678 return pet_stmt_free(stmt
);
1681 /* Add extra conditions to scop->skip[type].
1683 * The new skip condition only holds if it held before
1684 * and the condition is true. It does not hold if it did not hold
1685 * before or the condition is false.
1687 * The skip condition is assumed to be an affine expression.
1689 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1690 enum pet_skip type
, __isl_keep isl_set
*cond
)
1692 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1698 if (!ext
->skip
[type
])
1701 if (!set_is_affine(ext
->skip
[type
]))
1702 isl_die(isl_set_get_ctx(ext
->skip
[type
]), isl_error_internal
,
1703 "can only resrict affine skips",
1704 return pet_scop_free(scop
));
1706 skip
= ext
->skip
[type
];
1707 skip
= isl_set_intersect_params(skip
, isl_set_copy(cond
));
1708 set
= isl_set_from_params(isl_set_copy(cond
));
1709 set
= isl_set_complement(set
);
1710 set
= isl_set_add_dims(set
, isl_dim_set
, 1);
1711 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 0);
1712 skip
= isl_set_union(skip
, set
);
1713 ext
->skip
[type
] = skip
;
1714 if (!ext
->skip
[type
])
1715 return pet_scop_free(scop
);
1720 /* Add extra conditions on the parameters to all iteration domains
1721 * and skip conditions.
1723 * A parameter value is valid for the result if it was valid
1724 * for the original scop and satisfies "cond" or if it does
1725 * not satisfy "cond" as in this case the scop is not executed
1726 * and the original constraints on the parameters are irrelevant.
1728 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1729 __isl_take isl_set
*cond
)
1733 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1734 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1739 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1740 scop
->context
= isl_set_union(scop
->context
,
1741 isl_set_complement(isl_set_copy(cond
)));
1742 scop
->context
= isl_set_coalesce(scop
->context
);
1743 scop
->context
= set_project_out_unnamed_params(scop
->context
);
1747 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1748 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1749 isl_set_copy(cond
));
1750 if (!scop
->stmts
[i
])
1758 return pet_scop_free(scop
);
1761 /* Construct a map that inserts a filter value with name "id" and value
1762 * "satisfied" in the list of filter values embedded in the set space "space".
1764 * If "space" does not contain any filter values yet, we first create
1765 * a map that inserts 0 filter values, i.e.,
1767 * space -> [space -> []]
1769 * We can now assume that space is of the form [dom -> [filters]]
1770 * We construct an identity mapping on dom and a mapping on filters
1771 * that inserts the new filter
1774 * [filters] -> [satisfied, filters]
1776 * and then compute the cross product
1778 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1780 static __isl_give isl_map
*insert_filter_map(__isl_take isl_space
*space
,
1781 __isl_take isl_id
*id
, int satisfied
)
1784 isl_map
*map
, *map_dom
, *map_ran
;
1787 if (isl_space_is_wrapping(space
)) {
1788 space2
= isl_space_map_from_set(isl_space_copy(space
));
1789 map
= isl_map_identity(space2
);
1790 space
= isl_space_unwrap(space
);
1792 space
= isl_space_from_domain(space
);
1793 map
= isl_map_universe(isl_space_copy(space
));
1794 map
= isl_map_reverse(isl_map_domain_map(map
));
1797 space2
= isl_space_domain(isl_space_copy(space
));
1798 map_dom
= isl_map_identity(isl_space_map_from_set(space2
));
1799 space
= isl_space_range(space
);
1800 map_ran
= isl_map_identity(isl_space_map_from_set(space
));
1801 map_ran
= isl_map_insert_dims(map_ran
, isl_dim_out
, 0, 1);
1802 map_ran
= isl_map_set_dim_id(map_ran
, isl_dim_out
, 0, id
);
1803 map_ran
= isl_map_fix_si(map_ran
, isl_dim_out
, 0, satisfied
);
1805 map
= isl_map_apply_range(map
, isl_map_product(map_dom
, map_ran
));
1810 /* Insert an argument expression corresponding to "test" in front
1811 * of the list of arguments described by *n_arg and *args.
1813 static int args_insert_access(unsigned *n_arg
, struct pet_expr
***args
,
1814 __isl_keep isl_map
*test
)
1817 isl_ctx
*ctx
= isl_map_get_ctx(test
);
1823 *args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1827 struct pet_expr
**ext
;
1828 ext
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + *n_arg
);
1831 for (i
= 0; i
< *n_arg
; ++i
)
1832 ext
[1 + i
] = (*args
)[i
];
1837 (*args
)[0] = pet_expr_from_access(isl_map_copy(test
));
1844 /* Make the expression "expr" depend on the value of "test"
1845 * being equal to "satisfied".
1847 * If "test" is an affine expression, we simply add the conditions
1848 * on the expression have the value "satisfied" to all access relations.
1850 * Otherwise, we add a filter to "expr" (which is then assumed to be
1851 * an access expression) corresponding to "test" being equal to "satisfied".
1853 struct pet_expr
*pet_expr_filter(struct pet_expr
*expr
,
1854 __isl_take isl_map
*test
, int satisfied
)
1864 if (!isl_map_has_tuple_id(test
, isl_dim_out
)) {
1865 test
= isl_map_fix_si(test
, isl_dim_out
, 0, satisfied
);
1866 return pet_expr_restrict(expr
, isl_map_params(test
));
1869 ctx
= isl_map_get_ctx(test
);
1870 if (expr
->type
!= pet_expr_access
)
1871 isl_die(ctx
, isl_error_invalid
,
1872 "can only filter access expressions", goto error
);
1874 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1875 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1876 map
= insert_filter_map(space
, id
, satisfied
);
1878 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
1879 if (!expr
->acc
.access
)
1882 if (args_insert_access(&expr
->n_arg
, &expr
->args
, test
) < 0)
1889 return pet_expr_free(expr
);
1892 /* Make the statement "stmt" depend on the value of "test"
1893 * being equal to "satisfied" by adjusting stmt->domain.
1895 * The domain of "test" corresponds to the (zero or more) outer dimensions
1896 * of the iteration domain.
1898 * We insert an argument corresponding to a read to "test"
1899 * from the iteration domain of "stmt" in front of the list of arguments.
1900 * We also insert a corresponding output dimension in the wrapped
1901 * map contained in stmt->domain, with value set to "satisfied".
1903 static struct pet_stmt
*stmt_filter(struct pet_stmt
*stmt
,
1904 __isl_take isl_map
*test
, int satisfied
)
1909 isl_map
*map
, *add_dom
;
1917 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1918 map
= insert_filter_map(isl_set_get_space(stmt
->domain
), id
, satisfied
);
1919 stmt
->domain
= isl_set_apply(stmt
->domain
, map
);
1921 space
= isl_space_unwrap(isl_set_get_space(stmt
->domain
));
1922 dom
= isl_set_universe(isl_space_domain(space
));
1923 n_test_dom
= isl_map_dim(test
, isl_dim_in
);
1924 add_dom
= isl_map_from_range(dom
);
1925 add_dom
= isl_map_add_dims(add_dom
, isl_dim_in
, n_test_dom
);
1926 for (i
= 0; i
< n_test_dom
; ++i
)
1927 add_dom
= isl_map_equate(add_dom
, isl_dim_in
, i
,
1929 test
= isl_map_apply_domain(test
, add_dom
);
1931 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1938 return pet_stmt_free(stmt
);
1941 /* Does "scop" have a skip condition of the given "type"?
1943 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
1945 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1949 return ext
->skip
[type
] != NULL
;
1952 /* Does "scop" have a skip condition of the given "type" that
1953 * is an affine expression?
1955 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
1957 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1961 if (!ext
->skip
[type
])
1963 return set_is_affine(ext
->skip
[type
]);
1966 /* Does "scop" have a skip condition of the given "type" that
1967 * is not an affine expression?
1969 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
1971 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1976 if (!ext
->skip
[type
])
1978 aff
= set_is_affine(ext
->skip
[type
]);
1984 /* Does "scop" have a skip condition of the given "type" that
1985 * is affine and holds on the entire domain?
1987 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
1989 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1994 is_aff
= pet_scop_has_affine_skip(scop
, type
);
1995 if (is_aff
< 0 || !is_aff
)
1998 set
= isl_set_copy(ext
->skip
[type
]);
1999 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
2000 set
= isl_set_params(set
);
2001 is_univ
= isl_set_plain_is_universe(set
);
2007 /* Replace scop->skip[type] by "skip".
2009 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
2010 enum pet_skip type
, __isl_take isl_set
*skip
)
2012 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2017 isl_set_free(ext
->skip
[type
]);
2018 ext
->skip
[type
] = skip
;
2023 return pet_scop_free(scop
);
2026 /* Return a copy of scop->skip[type].
2028 __isl_give isl_set
*pet_scop_get_skip(struct pet_scop
*scop
,
2031 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2036 return isl_set_copy(ext
->skip
[type
]);
2039 /* Return a map to the skip condition of the given type.
2041 __isl_give isl_map
*pet_scop_get_skip_map(struct pet_scop
*scop
,
2044 return isl_map_from_range(pet_scop_get_skip(scop
, type
));
2047 /* Return an access pet_expr corresponding to the skip condition
2048 * of the given type.
2050 struct pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
2053 return pet_expr_from_access(pet_scop_get_skip_map(scop
, type
));
2056 /* Drop the the skip condition scop->skip[type].
2058 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
2060 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2065 isl_set_free(ext
->skip
[type
]);
2066 ext
->skip
[type
] = NULL
;
2069 /* Make the skip condition (if any) depend on the value of "test" being
2070 * equal to "satisfied".
2072 * We only support the case where the original skip condition is universal,
2073 * i.e., where skipping is unconditional, and where satisfied == 1.
2074 * In this case, the skip condition is changed to skip only when
2075 * "test" is equal to one.
2077 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
2078 enum pet_skip type
, __isl_keep isl_map
*test
, int satisfied
)
2084 if (!pet_scop_has_skip(scop
, type
))
2088 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2090 return pet_scop_free(scop
);
2091 if (satisfied
&& is_univ
) {
2092 scop
= pet_scop_set_skip(scop
, type
,
2093 isl_map_range(isl_map_copy(test
)));
2097 isl_die(isl_map_get_ctx(test
), isl_error_internal
,
2098 "skip expression cannot be filtered",
2099 return pet_scop_free(scop
));
2105 /* Make all statements in "scop" depend on the value of "test"
2106 * being equal to "satisfied" by adjusting their domains.
2108 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2109 __isl_take isl_map
*test
, int satisfied
)
2113 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
2114 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
2119 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2120 scop
->stmts
[i
] = stmt_filter(scop
->stmts
[i
],
2121 isl_map_copy(test
), satisfied
);
2122 if (!scop
->stmts
[i
])
2130 return pet_scop_free(scop
);
2133 /* Do the filters "i" and "j" always have the same value?
2135 static int equal_filter_values(__isl_keep isl_set
*domain
, int i
, int j
)
2137 isl_map
*map
, *test
;
2140 map
= isl_set_unwrap(isl_set_copy(domain
));
2141 test
= isl_map_universe(isl_map_get_space(map
));
2142 test
= isl_map_equate(test
, isl_dim_out
, i
, isl_dim_out
, j
);
2143 equal
= isl_map_is_subset(map
, test
);
2150 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2151 * access relation, the union of the two access relations.
2153 static struct pet_stmt
*merge_filter_pair(struct pet_stmt
*stmt
, int i
, int j
)
2161 stmt
->args
[i
]->acc
.access
= isl_map_union(stmt
->args
[i
]->acc
.access
,
2162 isl_map_copy(stmt
->args
[j
]->acc
.access
));
2163 stmt
->args
[i
]->acc
.access
= isl_map_coalesce(stmt
->args
[i
]->acc
.access
);
2165 pet_expr_free(stmt
->args
[j
]);
2166 for (k
= j
; k
< stmt
->n_arg
- 1; ++k
)
2167 stmt
->args
[k
] = stmt
->args
[k
+ 1];
2170 map
= isl_set_unwrap(stmt
->domain
);
2171 map
= isl_map_project_out(map
, isl_dim_out
, j
, 1);
2172 stmt
->domain
= isl_map_wrap(map
);
2174 if (!stmt
->domain
|| !stmt
->args
[i
]->acc
.access
)
2175 return pet_stmt_free(stmt
);
2180 /* Look for any pair of filters that access the same filter variable
2181 * and that have the same filter value and merge them into a single
2182 * filter with as filter access relation the union of the filter access
2185 static struct pet_stmt
*stmt_merge_filters(struct pet_stmt
*stmt
)
2188 isl_space
*space_i
, *space_j
;
2192 if (stmt
->n_arg
<= 1)
2195 for (i
= 0; i
< stmt
->n_arg
- 1; ++i
) {
2196 if (stmt
->args
[i
]->type
!= pet_expr_access
)
2198 if (pet_expr_is_affine(stmt
->args
[i
]))
2201 space_i
= isl_map_get_space(stmt
->args
[i
]->acc
.access
);
2203 for (j
= stmt
->n_arg
- 1; j
> i
; --j
) {
2206 if (stmt
->args
[j
]->type
!= pet_expr_access
)
2208 if (pet_expr_is_affine(stmt
->args
[j
]))
2211 space_j
= isl_map_get_space(stmt
->args
[j
]->acc
.access
);
2213 eq
= isl_space_is_equal(space_i
, space_j
);
2215 eq
= equal_filter_values(stmt
->domain
, i
, j
);
2217 stmt
= merge_filter_pair(stmt
, i
, j
);
2219 isl_space_free(space_j
);
2221 if (eq
< 0 || !stmt
)
2225 isl_space_free(space_i
);
2228 return pet_stmt_free(stmt
);
2234 /* Look for any pair of filters that access the same filter variable
2235 * and that have the same filter value and merge them into a single
2236 * filter with as filter access relation the union of the filter access
2239 struct pet_scop
*pet_scop_merge_filters(struct pet_scop
*scop
)
2246 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2247 scop
->stmts
[i
] = stmt_merge_filters(scop
->stmts
[i
]);
2248 if (!scop
->stmts
[i
])
2249 return pet_scop_free(scop
);
2255 /* Add all parameters in "expr" to "dim" and return the result.
2257 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
2258 __isl_take isl_space
*dim
)
2264 for (i
= 0; i
< expr
->n_arg
; ++i
)
2266 dim
= expr_collect_params(expr
->args
[i
], dim
);
2268 if (expr
->type
== pet_expr_access
)
2269 dim
= isl_space_align_params(dim
,
2270 isl_map_get_space(expr
->acc
.access
));
2274 isl_space_free(dim
);
2275 return pet_expr_free(expr
);
2278 /* Add all parameters in "stmt" to "dim" and return the result.
2280 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
2281 __isl_take isl_space
*dim
)
2286 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
2287 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
2288 dim
= expr_collect_params(stmt
->body
, dim
);
2292 isl_space_free(dim
);
2293 return pet_stmt_free(stmt
);
2296 /* Add all parameters in "array" to "dim" and return the result.
2298 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
2299 __isl_take isl_space
*dim
)
2304 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
2305 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
2309 pet_array_free(array
);
2310 return isl_space_free(dim
);
2313 /* Add all parameters in "scop" to "dim" and return the result.
2315 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2316 __isl_take isl_space
*dim
)
2323 for (i
= 0; i
< scop
->n_array
; ++i
)
2324 dim
= array_collect_params(scop
->arrays
[i
], dim
);
2326 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2327 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
2331 isl_space_free(dim
);
2332 return pet_scop_free(scop
);
2335 /* Add all parameters in "dim" to all access relations in "expr".
2337 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
2338 __isl_take isl_space
*dim
)
2345 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2347 expr_propagate_params(expr
->args
[i
],
2348 isl_space_copy(dim
));
2353 if (expr
->type
== pet_expr_access
) {
2354 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
2355 isl_space_copy(dim
));
2356 if (!expr
->acc
.access
)
2360 isl_space_free(dim
);
2363 isl_space_free(dim
);
2364 return pet_expr_free(expr
);
2367 /* Add all parameters in "dim" to the domain, schedule and
2368 * all access relations in "stmt".
2370 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2371 __isl_take isl_space
*dim
)
2376 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
2377 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2378 isl_space_copy(dim
));
2379 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
2381 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2384 isl_space_free(dim
);
2387 isl_space_free(dim
);
2388 return pet_stmt_free(stmt
);
2391 /* Add all parameters in "dim" to "array".
2393 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2394 __isl_take isl_space
*dim
)
2399 array
->context
= isl_set_align_params(array
->context
,
2400 isl_space_copy(dim
));
2401 array
->extent
= isl_set_align_params(array
->extent
,
2402 isl_space_copy(dim
));
2403 if (array
->value_bounds
) {
2404 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2405 isl_space_copy(dim
));
2406 if (!array
->value_bounds
)
2410 if (!array
->context
|| !array
->extent
)
2413 isl_space_free(dim
);
2416 isl_space_free(dim
);
2417 return pet_array_free(array
);
2420 /* Add all parameters in "dim" to "scop".
2422 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2423 __isl_take isl_space
*dim
)
2430 for (i
= 0; i
< scop
->n_array
; ++i
) {
2431 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2432 isl_space_copy(dim
));
2433 if (!scop
->arrays
[i
])
2437 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2438 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2439 isl_space_copy(dim
));
2440 if (!scop
->stmts
[i
])
2444 isl_space_free(dim
);
2447 isl_space_free(dim
);
2448 return pet_scop_free(scop
);
2451 /* Update all isl_sets and isl_maps in "scop" such that they all
2452 * have the same parameters.
2454 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2461 dim
= isl_set_get_space(scop
->context
);
2462 dim
= scop_collect_params(scop
, dim
);
2464 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
2465 scop
= scop_propagate_params(scop
, dim
);
2470 /* Check if the given access relation accesses a (0D) array that corresponds
2471 * to one of the parameters in "dim". If so, replace the array access
2472 * by an access to the set of integers with as index (and value)
2475 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
2476 __isl_take isl_space
*dim
)
2478 isl_id
*array_id
= NULL
;
2481 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
2482 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
2483 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
2485 isl_space_free(dim
);
2488 isl_id_free(array_id
);
2492 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
2494 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
2495 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
2498 isl_id_free(array_id
);
2500 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
2501 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
2506 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2507 * in "dim" by a value equal to the corresponding parameter.
2509 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
2510 __isl_take isl_space
*dim
)
2517 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2519 expr_detect_parameter_accesses(expr
->args
[i
],
2520 isl_space_copy(dim
));
2525 if (expr
->type
== pet_expr_access
) {
2526 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
2527 isl_space_copy(dim
));
2528 if (!expr
->acc
.access
)
2532 isl_space_free(dim
);
2535 isl_space_free(dim
);
2536 return pet_expr_free(expr
);
2539 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2540 * in "dim" by a value equal to the corresponding parameter.
2542 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
2543 __isl_take isl_space
*dim
)
2548 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
2549 isl_space_copy(dim
));
2551 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2554 isl_space_free(dim
);
2557 isl_space_free(dim
);
2558 return pet_stmt_free(stmt
);
2561 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2562 * in "dim" by a value equal to the corresponding parameter.
2564 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
2565 __isl_take isl_space
*dim
)
2572 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2573 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
2574 isl_space_copy(dim
));
2575 if (!scop
->stmts
[i
])
2579 isl_space_free(dim
);
2582 isl_space_free(dim
);
2583 return pet_scop_free(scop
);
2586 /* Replace all accesses to (0D) arrays that correspond to any of
2587 * the parameters used in "scop" by a value equal
2588 * to the corresponding parameter.
2590 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
2597 dim
= isl_set_get_space(scop
->context
);
2598 dim
= scop_collect_params(scop
, dim
);
2600 scop
= scop_detect_parameter_accesses(scop
, dim
);
2605 /* Add all read access relations (if "read" is set) and/or all write
2606 * access relations (if "write" is set) to "accesses" and return the result.
2608 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
2609 int read
, int write
, __isl_take isl_union_map
*accesses
)
2618 for (i
= 0; i
< expr
->n_arg
; ++i
)
2619 accesses
= expr_collect_accesses(expr
->args
[i
],
2620 read
, write
, accesses
);
2622 if (expr
->type
== pet_expr_access
&&
2623 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
2624 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
2625 accesses
= isl_union_map_add_map(accesses
,
2626 isl_map_copy(expr
->acc
.access
));
2631 /* Collect and return all read access relations (if "read" is set)
2632 * and/or all write access relations (if "write" is set) in "stmt".
2634 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2635 int read
, int write
, __isl_take isl_space
*dim
)
2637 isl_union_map
*accesses
;
2642 accesses
= isl_union_map_empty(dim
);
2643 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
2644 accesses
= isl_union_map_intersect_domain(accesses
,
2645 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
2650 /* Collect and return all read access relations (if "read" is set)
2651 * and/or all write access relations (if "write" is set) in "scop".
2653 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2654 int read
, int write
)
2657 isl_union_map
*accesses
;
2662 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2664 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2665 isl_union_map
*accesses_i
;
2666 isl_space
*dim
= isl_set_get_space(scop
->context
);
2667 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
2669 accesses
= isl_union_map_union(accesses
, accesses_i
);
2675 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
2677 return scop_collect_accesses(scop
, 1, 0);
2680 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
2682 return scop_collect_accesses(scop
, 0, 1);
2685 /* Collect and return the union of iteration domains in "scop".
2687 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2691 isl_union_set
*domain
;
2696 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2698 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2699 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2700 domain
= isl_union_set_add_set(domain
, domain_i
);
2706 /* Collect and return the schedules of the statements in "scop".
2707 * The range is normalized to the maximal number of scheduling
2710 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2713 isl_map
*schedule_i
;
2714 isl_union_map
*schedule
;
2715 int depth
, max_depth
= 0;
2720 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2722 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2723 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2724 if (depth
> max_depth
)
2728 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2729 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2730 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2731 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2733 for (j
= depth
; j
< max_depth
; ++j
)
2734 schedule_i
= isl_map_fix_si(schedule_i
,
2736 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2742 /* Does expression "expr" write to "id"?
2744 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
2749 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2750 int writes
= expr_writes(expr
->args
[i
], id
);
2751 if (writes
< 0 || writes
)
2755 if (expr
->type
!= pet_expr_access
)
2757 if (!expr
->acc
.write
)
2759 if (!isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
))
2762 write_id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
2763 isl_id_free(write_id
);
2768 return write_id
== id
;
2771 /* Does statement "stmt" write to "id"?
2773 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
2775 return expr_writes(stmt
->body
, id
);
2778 /* Is there any write access in "scop" that accesses "id"?
2780 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
2787 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2788 int writes
= stmt_writes(scop
->stmts
[i
], id
);
2789 if (writes
< 0 || writes
)
2796 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2798 static __isl_give isl_set
*set_anonymize(__isl_take isl_set
*set
)
2802 n
= isl_set_dim(set
, isl_dim_param
);
2803 for (i
= 0; i
< n
; ++i
) {
2804 isl_id
*id
= isl_set_get_dim_id(set
, isl_dim_param
, i
);
2805 const char *name
= isl_id_get_name(id
);
2806 set
= isl_set_set_dim_name(set
, isl_dim_param
, i
, name
);
2810 if (!isl_set_is_params(set
) && isl_set_has_tuple_id(set
)) {
2811 isl_id
*id
= isl_set_get_tuple_id(set
);
2812 const char *name
= isl_id_get_name(id
);
2813 set
= isl_set_set_tuple_name(set
, name
);
2820 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2822 static __isl_give isl_map
*map_anonymize(__isl_take isl_map
*map
)
2826 n
= isl_map_dim(map
, isl_dim_param
);
2827 for (i
= 0; i
< n
; ++i
) {
2828 isl_id
*id
= isl_map_get_dim_id(map
, isl_dim_param
, i
);
2829 const char *name
= isl_id_get_name(id
);
2830 map
= isl_map_set_dim_name(map
, isl_dim_param
, i
, name
);
2834 if (isl_map_has_tuple_id(map
, isl_dim_in
)) {
2835 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_in
);
2836 const char *name
= isl_id_get_name(id
);
2837 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
2841 if (isl_map_has_tuple_id(map
, isl_dim_out
)) {
2842 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_out
);
2843 const char *name
= isl_id_get_name(id
);
2844 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
2851 /* Reset the user pointer on all parameter ids in "array".
2853 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2858 array
->context
= set_anonymize(array
->context
);
2859 array
->extent
= set_anonymize(array
->extent
);
2860 if (!array
->context
|| !array
->extent
)
2861 return pet_array_free(array
);
2866 /* Reset the user pointer on all parameter and tuple ids in "access".
2868 static __isl_give isl_map
*access_anonymize(__isl_take isl_map
*access
,
2871 access
= map_anonymize(access
);
2876 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2878 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2887 stmt
->domain
= set_anonymize(stmt
->domain
);
2888 stmt
->schedule
= map_anonymize(stmt
->schedule
);
2889 if (!stmt
->domain
|| !stmt
->schedule
)
2890 return pet_stmt_free(stmt
);
2892 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2893 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
2894 &access_anonymize
, NULL
);
2896 return pet_stmt_free(stmt
);
2899 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
2900 &access_anonymize
, NULL
);
2902 return pet_stmt_free(stmt
);
2907 /* Reset the user pointer on all parameter and tuple ids in "scop".
2909 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2916 scop
->context
= set_anonymize(scop
->context
);
2917 scop
->context_value
= set_anonymize(scop
->context_value
);
2918 if (!scop
->context
|| !scop
->context_value
)
2919 return pet_scop_free(scop
);
2921 for (i
= 0; i
< scop
->n_array
; ++i
) {
2922 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2923 if (!scop
->arrays
[i
])
2924 return pet_scop_free(scop
);
2927 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2928 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2929 if (!scop
->stmts
[i
])
2930 return pet_scop_free(scop
);
2936 /* Given a set "domain", return a wrapped relation with the given set
2937 * as domain and a range of dimension "n_arg", where each coordinate
2938 * is either unbounded or, if the corresponding element of args is of
2939 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2941 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
2942 unsigned n_arg
, struct pet_expr
**args
,
2943 __isl_keep isl_union_map
*value_bounds
)
2948 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2950 map
= isl_map_from_domain(domain
);
2951 space
= isl_map_get_space(map
);
2952 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2954 for (i
= 0; i
< n_arg
; ++i
) {
2956 struct pet_expr
*arg
= args
[i
];
2960 map_i
= isl_map_universe(isl_space_copy(space
));
2961 if (arg
->type
== pet_expr_access
) {
2963 id
= isl_map_get_tuple_id(arg
->acc
.access
, isl_dim_out
);
2964 space2
= isl_space_alloc(ctx
, 0, 0, 1);
2965 space2
= isl_space_set_tuple_id(space2
, isl_dim_in
, id
);
2966 vb
= isl_union_map_extract_map(value_bounds
, space2
);
2967 if (!isl_map_plain_is_empty(vb
))
2968 map_i
= isl_map_intersect_range(map_i
,
2973 map
= isl_map_flat_range_product(map
, map_i
);
2975 isl_space_free(space
);
2977 return isl_map_wrap(map
);
2980 /* Data used in access_gist() callback.
2982 struct pet_access_gist_data
{
2984 isl_union_map
*value_bounds
;
2987 /* Given an expression "expr" of type pet_expr_access, compute
2988 * the gist of the associated access relation with respect to
2989 * data->domain and the bounds on the values of the arguments
2990 * of the expression.
2992 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
2994 struct pet_access_gist_data
*data
= user
;
2997 domain
= isl_set_copy(data
->domain
);
2998 if (expr
->n_arg
> 0)
2999 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
3000 data
->value_bounds
);
3002 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
, domain
);
3003 if (!expr
->acc
.access
)
3004 return pet_expr_free(expr
);
3009 /* Compute the gist of the iteration domain and all access relations
3010 * of "stmt" based on the constraints on the parameters specified by "context"
3011 * and the constraints on the values of nested accesses specified
3012 * by "value_bounds".
3014 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
3015 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
3020 struct pet_access_gist_data data
;
3025 data
.domain
= isl_set_copy(stmt
->domain
);
3026 data
.value_bounds
= value_bounds
;
3027 if (stmt
->n_arg
> 0)
3028 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
3030 data
.domain
= isl_set_intersect_params(data
.domain
,
3031 isl_set_copy(context
));
3033 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
3034 stmt
->args
[i
] = pet_expr_foreach_access_expr(stmt
->args
[i
],
3035 &access_gist
, &data
);
3040 stmt
->body
= pet_expr_foreach_access_expr(stmt
->body
,
3041 &access_gist
, &data
);
3045 isl_set_free(data
.domain
);
3047 space
= isl_set_get_space(stmt
->domain
);
3048 if (isl_space_is_wrapping(space
))
3049 space
= isl_space_domain(isl_space_unwrap(space
));
3050 domain
= isl_set_universe(space
);
3051 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
3052 if (stmt
->n_arg
> 0)
3053 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
3055 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
3057 return pet_stmt_free(stmt
);
3061 isl_set_free(data
.domain
);
3062 return pet_stmt_free(stmt
);
3065 /* Compute the gist of the extent of the array
3066 * based on the constraints on the parameters specified by "context".
3068 static struct pet_array
*array_gist(struct pet_array
*array
,
3069 __isl_keep isl_set
*context
)
3074 array
->extent
= isl_set_gist_params(array
->extent
,
3075 isl_set_copy(context
));
3077 return pet_array_free(array
);
3082 /* Compute the gist of all sets and relations in "scop"
3083 * based on the constraints on the parameters specified by "scop->context"
3084 * and the constraints on the values of nested accesses specified
3085 * by "value_bounds".
3087 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
3088 __isl_keep isl_union_map
*value_bounds
)
3095 scop
->context
= isl_set_coalesce(scop
->context
);
3097 return pet_scop_free(scop
);
3099 for (i
= 0; i
< scop
->n_array
; ++i
) {
3100 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
3101 if (!scop
->arrays
[i
])
3102 return pet_scop_free(scop
);
3105 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3106 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
3108 if (!scop
->stmts
[i
])
3109 return pet_scop_free(scop
);
3115 /* Intersect the context of "scop" with "context".
3116 * To ensure that we don't introduce any unnamed parameters in
3117 * the context of "scop", we first remove the unnamed parameters
3120 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
3121 __isl_take isl_set
*context
)
3126 context
= set_project_out_unnamed_params(context
);
3127 scop
->context
= isl_set_intersect(scop
->context
, context
);
3129 return pet_scop_free(scop
);
3133 isl_set_free(context
);
3134 return pet_scop_free(scop
);
3137 /* Drop the current context of "scop". That is, replace the context
3138 * by a universal set.
3140 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
3147 space
= isl_set_get_space(scop
->context
);
3148 isl_set_free(scop
->context
);
3149 scop
->context
= isl_set_universe(space
);
3151 return pet_scop_free(scop
);
3156 /* Append "array" to the arrays of "scop".
3158 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
3159 struct pet_array
*array
)
3162 struct pet_array
**arrays
;
3164 if (!array
|| !scop
)
3167 ctx
= isl_set_get_ctx(scop
->context
);
3168 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3172 scop
->arrays
= arrays
;
3173 scop
->arrays
[scop
->n_array
] = array
;
3178 pet_array_free(array
);
3179 return pet_scop_free(scop
);