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
] = "=",
67 [pet_op_post_inc
] = "++",
68 [pet_op_post_dec
] = "--",
69 [pet_op_pre_inc
] = "++",
70 [pet_op_pre_dec
] = "--",
71 [pet_op_address_of
] = "&",
72 [pet_op_kill
] = "kill"
75 /* pet_scop with extra information that is only used during parsing.
77 * In particular, we keep track of conditions under which we want
78 * to skip the rest of the current loop iteration (skip[pet_skip_now])
79 * and of conditions under which we want to skip subsequent
80 * loop iterations (skip[pet_skip_later]).
82 * The conditions are represented either by a variable, which
83 * is assumed to attain values zero and one, or by a boolean affine
84 * expression. The condition holds if the variable has value one
85 * or if the affine expression has value one (typically for only
86 * part of the parameter space).
88 * A missing condition (skip[type] == NULL) means that we don't want
97 const char *pet_op_str(enum pet_op_type op
)
102 int pet_op_is_inc_dec(enum pet_op_type op
)
104 return op
== pet_op_post_inc
|| op
== pet_op_post_dec
||
105 op
== pet_op_pre_inc
|| op
== pet_op_pre_dec
;
108 const char *pet_type_str(enum pet_expr_type type
)
110 return type_str
[type
];
113 enum pet_op_type
pet_str_op(const char *str
)
117 for (i
= 0; i
< ARRAY_SIZE(op_str
); ++i
)
118 if (!strcmp(op_str
[i
], str
))
124 enum pet_expr_type
pet_str_type(const char *str
)
128 for (i
= 0; i
< ARRAY_SIZE(type_str
); ++i
)
129 if (!strcmp(type_str
[i
], str
))
135 /* Construct a pet_expr from an access relation.
136 * By default, it is considered to be a read access.
138 struct pet_expr
*pet_expr_from_access(__isl_take isl_map
*access
)
140 isl_ctx
*ctx
= isl_map_get_ctx(access
);
141 struct pet_expr
*expr
;
145 expr
= isl_calloc_type(ctx
, struct pet_expr
);
149 expr
->type
= pet_expr_access
;
150 expr
->acc
.access
= access
;
156 isl_map_free(access
);
160 /* Construct a pet_expr that kills the elements specified by "access".
162 struct pet_expr
*pet_expr_kill_from_access(__isl_take isl_map
*access
)
165 struct pet_expr
*expr
;
167 ctx
= isl_map_get_ctx(access
);
168 expr
= pet_expr_from_access(access
);
172 return pet_expr_new_unary(ctx
, pet_op_kill
, expr
);
175 /* Construct a unary pet_expr that performs "op" on "arg".
177 struct pet_expr
*pet_expr_new_unary(isl_ctx
*ctx
, enum pet_op_type op
,
178 struct pet_expr
*arg
)
180 struct pet_expr
*expr
;
184 expr
= isl_alloc_type(ctx
, struct pet_expr
);
188 expr
->type
= pet_expr_unary
;
191 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
194 expr
->args
[pet_un_arg
] = arg
;
202 /* Construct a binary pet_expr that performs "op" on "lhs" and "rhs".
204 struct pet_expr
*pet_expr_new_binary(isl_ctx
*ctx
, enum pet_op_type op
,
205 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
207 struct pet_expr
*expr
;
211 expr
= isl_alloc_type(ctx
, struct pet_expr
);
215 expr
->type
= pet_expr_binary
;
218 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 2);
221 expr
->args
[pet_bin_lhs
] = lhs
;
222 expr
->args
[pet_bin_rhs
] = rhs
;
231 /* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
233 struct pet_expr
*pet_expr_new_ternary(isl_ctx
*ctx
, struct pet_expr
*cond
,
234 struct pet_expr
*lhs
, struct pet_expr
*rhs
)
236 struct pet_expr
*expr
;
238 if (!cond
|| !lhs
|| !rhs
)
240 expr
= isl_alloc_type(ctx
, struct pet_expr
);
244 expr
->type
= pet_expr_ternary
;
246 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, 3);
249 expr
->args
[pet_ter_cond
] = cond
;
250 expr
->args
[pet_ter_true
] = lhs
;
251 expr
->args
[pet_ter_false
] = rhs
;
261 /* Construct a call pet_expr that calls function "name" with "n_arg"
262 * arguments. The caller is responsible for filling in the arguments.
264 struct pet_expr
*pet_expr_new_call(isl_ctx
*ctx
, const char *name
,
267 struct pet_expr
*expr
;
269 expr
= isl_alloc_type(ctx
, struct pet_expr
);
273 expr
->type
= pet_expr_call
;
275 expr
->name
= strdup(name
);
276 expr
->args
= isl_calloc_array(ctx
, struct pet_expr
*, n_arg
);
277 if (!expr
->name
|| !expr
->args
)
278 return pet_expr_free(expr
);
283 /* Construct a pet_expr that represents the double "d".
285 struct pet_expr
*pet_expr_new_double(isl_ctx
*ctx
, double val
, const char *s
)
287 struct pet_expr
*expr
;
289 expr
= isl_calloc_type(ctx
, struct pet_expr
);
293 expr
->type
= pet_expr_double
;
295 expr
->d
.s
= strdup(s
);
297 return pet_expr_free(expr
);
302 void *pet_expr_free(struct pet_expr
*expr
)
309 for (i
= 0; i
< expr
->n_arg
; ++i
)
310 pet_expr_free(expr
->args
[i
]);
313 switch (expr
->type
) {
314 case pet_expr_access
:
315 isl_map_free(expr
->acc
.access
);
320 case pet_expr_double
:
324 case pet_expr_binary
:
325 case pet_expr_ternary
:
333 static void expr_dump(struct pet_expr
*expr
, int indent
)
340 fprintf(stderr
, "%*s", indent
, "");
342 switch (expr
->type
) {
343 case pet_expr_double
:
344 fprintf(stderr
, "%s\n", expr
->d
.s
);
346 case pet_expr_access
:
347 isl_map_dump(expr
->acc
.access
);
348 fprintf(stderr
, "%*sread: %d\n", indent
+ 2,
350 fprintf(stderr
, "%*swrite: %d\n", indent
+ 2,
351 "", expr
->acc
.write
);
352 for (i
= 0; i
< expr
->n_arg
; ++i
)
353 expr_dump(expr
->args
[i
], indent
+ 2);
356 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
357 expr_dump(expr
->args
[pet_un_arg
], indent
+ 2);
359 case pet_expr_binary
:
360 fprintf(stderr
, "%s\n", op_str
[expr
->op
]);
361 expr_dump(expr
->args
[pet_bin_lhs
], indent
+ 2);
362 expr_dump(expr
->args
[pet_bin_rhs
], indent
+ 2);
364 case pet_expr_ternary
:
365 fprintf(stderr
, "?:\n");
366 expr_dump(expr
->args
[pet_ter_cond
], indent
+ 2);
367 expr_dump(expr
->args
[pet_ter_true
], indent
+ 2);
368 expr_dump(expr
->args
[pet_ter_false
], indent
+ 2);
371 fprintf(stderr
, "%s/%d\n", expr
->name
, expr
->n_arg
);
372 for (i
= 0; i
< expr
->n_arg
; ++i
)
373 expr_dump(expr
->args
[i
], indent
+ 2);
378 void pet_expr_dump(struct pet_expr
*expr
)
383 /* Does "expr" represent an access to an unnamed space, i.e.,
384 * does it represent an affine expression?
386 int pet_expr_is_affine(struct pet_expr
*expr
)
392 if (expr
->type
!= pet_expr_access
)
395 has_id
= isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
);
402 /* Return 1 if the two pet_exprs are equivalent.
404 int pet_expr_is_equal(struct pet_expr
*expr1
, struct pet_expr
*expr2
)
408 if (!expr1
|| !expr2
)
411 if (expr1
->type
!= expr2
->type
)
413 if (expr1
->n_arg
!= expr2
->n_arg
)
415 for (i
= 0; i
< expr1
->n_arg
; ++i
)
416 if (!pet_expr_is_equal(expr1
->args
[i
], expr2
->args
[i
]))
418 switch (expr1
->type
) {
419 case pet_expr_double
:
420 if (strcmp(expr1
->d
.s
, expr2
->d
.s
))
422 if (expr1
->d
.val
!= expr2
->d
.val
)
425 case pet_expr_access
:
426 if (expr1
->acc
.read
!= expr2
->acc
.read
)
428 if (expr1
->acc
.write
!= expr2
->acc
.write
)
430 if (!expr1
->acc
.access
|| !expr2
->acc
.access
)
432 if (!isl_map_is_equal(expr1
->acc
.access
, expr2
->acc
.access
))
436 case pet_expr_binary
:
437 case pet_expr_ternary
:
438 if (expr1
->op
!= expr2
->op
)
442 if (strcmp(expr1
->name
, expr2
->name
))
450 /* Add extra conditions on the parameters to all access relations in "expr".
452 struct pet_expr
*pet_expr_restrict(struct pet_expr
*expr
,
453 __isl_take isl_set
*cond
)
460 for (i
= 0; i
< expr
->n_arg
; ++i
) {
461 expr
->args
[i
] = pet_expr_restrict(expr
->args
[i
],
467 if (expr
->type
== pet_expr_access
) {
468 expr
->acc
.access
= isl_map_intersect_params(expr
->acc
.access
,
470 if (!expr
->acc
.access
)
478 return pet_expr_free(expr
);
481 /* Modify all access relations in "expr" by calling "fn" on them.
483 struct pet_expr
*pet_expr_foreach_access(struct pet_expr
*expr
,
484 __isl_give isl_map
*(*fn
)(__isl_take isl_map
*access
, void *user
),
492 for (i
= 0; i
< expr
->n_arg
; ++i
) {
493 expr
->args
[i
] = pet_expr_foreach_access(expr
->args
[i
], fn
, user
);
495 return pet_expr_free(expr
);
498 if (expr
->type
== pet_expr_access
) {
499 expr
->acc
.access
= fn(expr
->acc
.access
, user
);
500 if (!expr
->acc
.access
)
501 return pet_expr_free(expr
);
507 /* Modify all expressions of type pet_expr_access in "expr"
508 * by calling "fn" on them.
510 struct pet_expr
*pet_expr_foreach_access_expr(struct pet_expr
*expr
,
511 struct pet_expr
*(*fn
)(struct pet_expr
*expr
, void *user
),
519 for (i
= 0; i
< expr
->n_arg
; ++i
) {
520 expr
->args
[i
] = pet_expr_foreach_access_expr(expr
->args
[i
],
523 return pet_expr_free(expr
);
526 if (expr
->type
== pet_expr_access
)
527 expr
= fn(expr
, user
);
532 /* Modify the given access relation based on the given iteration space
534 * If the access has any arguments then the domain of the access relation
535 * is a wrapped mapping from the iteration space to the space of
536 * argument values. We only need to change the domain of this wrapped
537 * mapping, so we extend the input transformation with an identity mapping
538 * on the space of argument values.
540 static __isl_give isl_map
*update_domain(__isl_take isl_map
*access
,
543 isl_map
*update
= user
;
546 update
= isl_map_copy(update
);
548 dim
= isl_map_get_space(access
);
549 dim
= isl_space_domain(dim
);
550 if (!isl_space_is_wrapping(dim
))
554 dim
= isl_space_unwrap(dim
);
555 dim
= isl_space_range(dim
);
556 dim
= isl_space_map_from_set(dim
);
557 id
= isl_map_identity(dim
);
558 update
= isl_map_product(update
, id
);
561 return isl_map_apply_domain(access
, update
);
564 /* Modify all access relations in "expr" based on the given iteration space
567 static struct pet_expr
*expr_update_domain(struct pet_expr
*expr
,
568 __isl_take isl_map
*update
)
570 expr
= pet_expr_foreach_access(expr
, &update_domain
, update
);
571 isl_map_free(update
);
575 /* Construct a pet_stmt with given line number and statement
576 * number from a pet_expr.
577 * The initial iteration domain is the zero-dimensional universe.
578 * The name of the domain is given by "label" if it is non-NULL.
579 * Otherwise, the name is constructed as S_<id>.
580 * The domains of all access relations are modified to refer
581 * to the statement iteration domain.
583 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
584 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
586 struct pet_stmt
*stmt
;
596 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
600 dim
= isl_space_set_alloc(ctx
, 0, 0);
602 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
604 snprintf(name
, sizeof(name
), "S_%d", id
);
605 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
607 dom
= isl_set_universe(isl_space_copy(dim
));
608 sched
= isl_map_from_domain(isl_set_copy(dom
));
610 dim
= isl_space_from_range(dim
);
611 add_name
= isl_map_universe(dim
);
612 expr
= expr_update_domain(expr
, add_name
);
616 stmt
->schedule
= sched
;
619 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
620 return pet_stmt_free(stmt
);
625 return pet_expr_free(expr
);
628 void *pet_stmt_free(struct pet_stmt
*stmt
)
635 isl_set_free(stmt
->domain
);
636 isl_map_free(stmt
->schedule
);
637 pet_expr_free(stmt
->body
);
639 for (i
= 0; i
< stmt
->n_arg
; ++i
)
640 pet_expr_free(stmt
->args
[i
]);
647 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
654 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
655 fprintf(stderr
, "%*s", indent
, "");
656 isl_set_dump(stmt
->domain
);
657 fprintf(stderr
, "%*s", indent
, "");
658 isl_map_dump(stmt
->schedule
);
659 expr_dump(stmt
->body
, indent
);
660 for (i
= 0; i
< stmt
->n_arg
; ++i
)
661 expr_dump(stmt
->args
[i
], indent
+ 2);
664 void pet_stmt_dump(struct pet_stmt
*stmt
)
669 struct pet_array
*pet_array_free(struct pet_array
*array
)
674 isl_set_free(array
->context
);
675 isl_set_free(array
->extent
);
676 isl_set_free(array
->value_bounds
);
677 free(array
->element_type
);
683 void pet_array_dump(struct pet_array
*array
)
688 isl_set_dump(array
->context
);
689 isl_set_dump(array
->extent
);
690 isl_set_dump(array
->value_bounds
);
691 fprintf(stderr
, "%s %s\n", array
->element_type
,
692 array
->live_out
? "live-out" : "");
695 /* Alloc a pet_scop structure, with extra room for information that
696 * is only used during parsing.
698 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
700 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
703 /* Construct a pet_scop with room for n statements.
705 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
708 struct pet_scop
*scop
;
710 scop
= pet_scop_alloc(ctx
);
714 space
= isl_space_params_alloc(ctx
, 0);
715 scop
->context
= isl_set_universe(isl_space_copy(space
));
716 scop
->context_value
= isl_set_universe(space
);
717 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
718 if (!scop
->context
|| !scop
->stmts
)
719 return pet_scop_free(scop
);
726 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
728 return scop_alloc(ctx
, 0);
731 /* Update "context" with respect to the valid parameter values for "access".
733 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
734 __isl_take isl_set
*context
)
736 context
= isl_set_intersect(context
,
737 isl_map_params(isl_map_copy(access
)));
741 /* Update "context" with respect to the valid parameter values for "expr".
743 * If "expr" represents a ternary operator, then a parameter value
744 * needs to be valid for the condition and for at least one of the
745 * remaining two arguments.
746 * If the condition is an affine expression, then we can be a bit more specific.
747 * The parameter then has to be valid for the second argument for
748 * non-zero accesses and valid for the third argument for zero accesses.
750 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
751 __isl_take isl_set
*context
)
755 if (expr
->type
== pet_expr_ternary
) {
757 isl_set
*context1
, *context2
;
759 is_aff
= pet_expr_is_affine(expr
->args
[0]);
763 context
= expr_extract_context(expr
->args
[0], context
);
764 context1
= expr_extract_context(expr
->args
[1],
765 isl_set_copy(context
));
766 context2
= expr_extract_context(expr
->args
[2], context
);
772 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
773 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
774 zero_set
= isl_map_params(access
);
775 context1
= isl_set_subtract(context1
,
776 isl_set_copy(zero_set
));
777 context2
= isl_set_intersect(context2
, zero_set
);
780 context
= isl_set_union(context1
, context2
);
781 context
= isl_set_coalesce(context
);
786 for (i
= 0; i
< expr
->n_arg
; ++i
)
787 context
= expr_extract_context(expr
->args
[i
], context
);
789 if (expr
->type
== pet_expr_access
)
790 context
= access_extract_context(expr
->acc
.access
, context
);
794 isl_set_free(context
);
798 /* Update "context" with respect to the valid parameter values for "stmt".
800 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
801 __isl_take isl_set
*context
)
805 for (i
= 0; i
< stmt
->n_arg
; ++i
)
806 context
= expr_extract_context(stmt
->args
[i
], context
);
808 context
= expr_extract_context(stmt
->body
, context
);
813 /* Construct a pet_scop that contains the given pet_stmt.
815 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
817 struct pet_scop
*scop
;
822 scop
= scop_alloc(ctx
, 1);
824 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
828 scop
->stmts
[0] = stmt
;
837 /* Does "set" represent an element of an unnamed space, i.e.,
838 * does it represent an affine expression?
840 static int set_is_affine(__isl_keep isl_set
*set
)
844 has_id
= isl_set_has_tuple_id(set
);
851 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
852 * ext may be equal to either ext1 or ext2.
854 * The two skips that need to be combined are assumed to be affine expressions.
856 * We need to skip in ext if we need to skip in either ext1 or ext2.
857 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
859 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
860 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
863 isl_set
*set
, *skip1
, *skip2
;
867 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
869 if (!ext1
->skip
[type
]) {
872 ext
->skip
[type
] = ext2
->skip
[type
];
873 ext2
->skip
[type
] = NULL
;
876 if (!ext2
->skip
[type
]) {
879 ext
->skip
[type
] = ext1
->skip
[type
];
880 ext1
->skip
[type
] = NULL
;
884 if (!set_is_affine(ext1
->skip
[type
]) ||
885 !set_is_affine(ext2
->skip
[type
]))
886 isl_die(isl_set_get_ctx(ext1
->skip
[type
]), isl_error_internal
,
887 "can only combine affine skips",
888 return pet_scop_free(&ext
->scop
));
890 skip1
= isl_set_copy(ext1
->skip
[type
]);
891 skip2
= isl_set_copy(ext2
->skip
[type
]);
892 set
= isl_set_intersect(
893 isl_set_fix_si(isl_set_copy(skip1
), isl_dim_set
, 0, 0),
894 isl_set_fix_si(isl_set_copy(skip2
), isl_dim_set
, 0, 0));
895 set
= isl_set_union(set
, isl_set_fix_si(skip1
, isl_dim_set
, 0, 1));
896 set
= isl_set_union(set
, isl_set_fix_si(skip2
, isl_dim_set
, 0, 1));
897 set
= isl_set_coalesce(set
);
898 isl_set_free(ext1
->skip
[type
]);
899 ext1
->skip
[type
] = NULL
;
900 isl_set_free(ext2
->skip
[type
]);
901 ext2
->skip
[type
] = NULL
;
902 ext
->skip
[type
] = set
;
903 if (!ext
->skip
[type
])
904 return pet_scop_free(&ext
->scop
);
909 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
910 * where type takes on the values pet_skip_now and pet_skip_later.
911 * scop may be equal to either scop1 or scop2.
913 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
914 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
916 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
917 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
918 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
920 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
921 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
925 /* Construct a pet_scop that contains the arrays, statements and
926 * skip information in "scop1" and "scop2".
928 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
929 struct pet_scop
*scop2
)
932 struct pet_scop
*scop
;
934 if (!scop1
|| !scop2
)
937 if (scop1
->n_stmt
== 0) {
938 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
939 pet_scop_free(scop1
);
943 if (scop2
->n_stmt
== 0) {
944 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
945 pet_scop_free(scop2
);
949 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
953 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
954 scop1
->n_array
+ scop2
->n_array
);
957 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
959 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
960 scop
->stmts
[i
] = scop1
->stmts
[i
];
961 scop1
->stmts
[i
] = NULL
;
964 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
965 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
966 scop2
->stmts
[i
] = NULL
;
969 for (i
= 0; i
< scop1
->n_array
; ++i
) {
970 scop
->arrays
[i
] = scop1
->arrays
[i
];
971 scop1
->arrays
[i
] = NULL
;
974 for (i
= 0; i
< scop2
->n_array
; ++i
) {
975 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
976 scop2
->arrays
[i
] = NULL
;
979 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
980 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
981 scop
= scop_combine_skips(scop
, scop1
, scop2
);
983 pet_scop_free(scop1
);
984 pet_scop_free(scop2
);
987 pet_scop_free(scop1
);
988 pet_scop_free(scop2
);
992 /* Apply the skip condition "skip" to "scop".
993 * That is, make sure "scop" is not executed when the condition holds.
995 * If "skip" is an affine expression, we add the conditions under
996 * which the expression is zero to the iteration domains.
997 * Otherwise, we add a filter on the variable attaining the value zero.
999 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
1000 __isl_take isl_set
*skip
)
1008 is_aff
= set_is_affine(skip
);
1013 return pet_scop_filter(scop
, isl_map_from_range(skip
), 0);
1015 skip
= isl_set_fix_si(skip
, isl_dim_set
, 0, 0);
1016 scop
= pet_scop_restrict(scop
, isl_set_params(skip
));
1021 return pet_scop_free(scop
);
1024 /* Construct a pet_scop that contains the arrays, statements and
1025 * skip information in "scop1" and "scop2", where the two scops
1026 * are executed "in sequence". That is, breaks and continues
1027 * in scop1 have an effect on scop2.
1029 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1030 struct pet_scop
*scop2
)
1032 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
1033 scop2
= restrict_skip(scop2
,
1034 pet_scop_get_skip(scop1
, pet_skip_now
));
1035 return pet_scop_add(ctx
, scop1
, scop2
);
1038 /* Construct a pet_scop that contains the arrays, statements and
1039 * skip information in "scop1" and "scop2", where the two scops
1040 * are executed "in parallel". That is, any break or continue
1041 * in scop1 has no effect on scop2.
1043 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
1044 struct pet_scop
*scop2
)
1046 return pet_scop_add(ctx
, scop1
, scop2
);
1049 void *pet_scop_free(struct pet_scop
*scop
)
1052 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1056 isl_set_free(scop
->context
);
1057 isl_set_free(scop
->context_value
);
1059 for (i
= 0; i
< scop
->n_array
; ++i
)
1060 pet_array_free(scop
->arrays
[i
]);
1063 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1064 pet_stmt_free(scop
->stmts
[i
]);
1066 isl_set_free(ext
->skip
[pet_skip_now
]);
1067 isl_set_free(ext
->skip
[pet_skip_later
]);
1072 void pet_scop_dump(struct pet_scop
*scop
)
1075 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1080 isl_set_dump(scop
->context
);
1081 isl_set_dump(scop
->context_value
);
1082 for (i
= 0; i
< scop
->n_array
; ++i
)
1083 pet_array_dump(scop
->arrays
[i
]);
1084 for (i
= 0; i
< scop
->n_stmt
; ++i
)
1085 pet_stmt_dump(scop
->stmts
[i
]);
1088 fprintf(stderr
, "skip\n");
1089 isl_set_dump(ext
->skip
[0]);
1090 isl_set_dump(ext
->skip
[1]);
1094 /* Return 1 if the two pet_arrays are equivalent.
1096 * We don't compare element_size as this may be target dependent.
1098 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
1100 if (!array1
|| !array2
)
1103 if (!isl_set_is_equal(array1
->context
, array2
->context
))
1105 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
1107 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
1109 if (array1
->value_bounds
&&
1110 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
1112 if (strcmp(array1
->element_type
, array2
->element_type
))
1114 if (array1
->live_out
!= array2
->live_out
)
1116 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
1118 if (array1
->declared
!= array2
->declared
)
1120 if (array1
->exposed
!= array2
->exposed
)
1126 /* Return 1 if the two pet_stmts are equivalent.
1128 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
1132 if (!stmt1
|| !stmt2
)
1135 if (stmt1
->line
!= stmt2
->line
)
1137 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
1139 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
1141 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
1143 if (stmt1
->n_arg
!= stmt2
->n_arg
)
1145 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
1146 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
1153 /* Return 1 if the two pet_scops are equivalent.
1155 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
1159 if (!scop1
|| !scop2
)
1162 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1164 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1167 if (scop1
->n_array
!= scop2
->n_array
)
1169 for (i
= 0; i
< scop1
->n_array
; ++i
)
1170 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1173 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1175 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1176 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1182 /* Prefix the schedule of "stmt" with an extra dimension with constant
1185 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1190 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1191 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1192 if (!stmt
->schedule
)
1193 return pet_stmt_free(stmt
);
1198 /* Prefix the schedules of all statements in "scop" with an extra
1199 * dimension with constant value "pos".
1201 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1208 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1209 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1210 if (!scop
->stmts
[i
])
1211 return pet_scop_free(scop
);
1217 /* Given a set with a parameter at "param_pos" that refers to the
1218 * iterator, "move" the iterator to the first set dimension.
1219 * That is, essentially equate the parameter to the first set dimension
1220 * and then project it out.
1222 * The first set dimension may however refer to a virtual iterator,
1223 * while the parameter refers to the "real" iterator.
1224 * We therefore need to take into account the mapping "iv_map", which
1225 * maps the virtual iterator to the real iterator.
1226 * In particular, we equate the set dimension to the input of the map
1227 * and the parameter to the output of the map and then project out
1228 * everything we don't need anymore.
1230 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1231 int param_pos
, __isl_take isl_map
*iv_map
)
1234 map
= isl_map_from_domain(set
);
1235 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1236 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1237 iv_map
= isl_map_align_params(iv_map
, isl_map_get_space(map
));
1238 map
= isl_map_apply_range(map
, iv_map
);
1239 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1240 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1241 return isl_map_domain(map
);
1244 /* Data used in embed_access.
1245 * extend adds an iterator to the iteration domain
1246 * iv_map maps the virtual iterator to the real iterator
1247 * var_id represents the induction variable of the corresponding loop
1249 struct pet_embed_access
{
1255 /* Embed the access relation in an extra outer loop.
1257 * We first update the iteration domain to insert the extra dimension.
1259 * If the access refers to the induction variable, then it is
1260 * turned into an access to the set of integers with index (and value)
1261 * equal to the induction variable.
1263 * If the induction variable appears in the constraints (as a parameter),
1264 * then the parameter is equated to the newly introduced iteration
1265 * domain dimension and subsequently projected out.
1267 * Similarly, if the accessed array is a virtual array (with user
1268 * pointer equal to NULL), as created by create_test_access,
1269 * then it is extended along with the domain of the access.
1271 static __isl_give isl_map
*embed_access(__isl_take isl_map
*access
,
1274 struct pet_embed_access
*data
= user
;
1275 isl_id
*array_id
= NULL
;
1278 access
= update_domain(access
, data
->extend
);
1280 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1281 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1282 if (array_id
== data
->var_id
||
1283 (array_id
&& !isl_id_get_user(array_id
))) {
1284 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1285 access
= isl_map_equate(access
,
1286 isl_dim_in
, 0, isl_dim_out
, 0);
1287 if (array_id
== data
->var_id
)
1288 access
= isl_map_apply_range(access
,
1289 isl_map_copy(data
->iv_map
));
1291 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1292 isl_id_copy(array_id
));
1294 isl_id_free(array_id
);
1296 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1298 isl_set
*set
= isl_map_wrap(access
);
1299 set
= internalize_iv(set
, pos
, isl_map_copy(data
->iv_map
));
1300 access
= isl_set_unwrap(set
);
1302 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1303 isl_id_copy(data
->var_id
));
1308 /* Embed all access relations in "expr" in an extra loop.
1309 * "extend" inserts an outer loop iterator in the iteration domains.
1310 * "iv_map" maps the virtual iterator to the real iterator
1311 * "var_id" represents the induction variable.
1313 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
1314 __isl_take isl_map
*extend
, __isl_take isl_map
*iv_map
,
1315 __isl_keep isl_id
*var_id
)
1317 struct pet_embed_access data
=
1318 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1320 expr
= pet_expr_foreach_access(expr
, &embed_access
, &data
);
1321 isl_map_free(iv_map
);
1322 isl_map_free(extend
);
1326 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1327 * "dom" and schedule "sched". "var_id" represents the induction variable
1328 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1329 * That is, it maps the iterator used in "dom" and the domain of "sched"
1330 * to the iterator that some of the parameters in "stmt" may refer to.
1332 * The iteration domain and schedule of the statement are updated
1333 * according to the iteration domain and schedule of the new loop.
1334 * If stmt->domain is a wrapped map, then the iteration domain
1335 * is the domain of this map, so we need to be careful to adjust
1338 * If the induction variable appears in the constraints (as a parameter)
1339 * of the current iteration domain or the schedule of the statement,
1340 * then the parameter is equated to the newly introduced iteration
1341 * domain dimension and subsequently projected out.
1343 * Finally, all access relations are updated based on the extra loop.
1345 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1346 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1347 __isl_take isl_map
*iv_map
, __isl_take isl_id
*var_id
)
1358 if (isl_set_is_wrapping(stmt
->domain
)) {
1363 map
= isl_set_unwrap(stmt
->domain
);
1364 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1365 ran_dim
= isl_space_range(isl_map_get_space(map
));
1366 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1367 isl_set_universe(ran_dim
));
1368 map
= isl_map_flat_domain_product(ext
, map
);
1369 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1370 isl_id_copy(stmt_id
));
1371 dim
= isl_space_domain(isl_map_get_space(map
));
1372 stmt
->domain
= isl_map_wrap(map
);
1374 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1375 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1377 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1378 isl_id_copy(stmt_id
));
1379 dim
= isl_set_get_space(stmt
->domain
);
1382 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1384 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1385 isl_map_copy(iv_map
));
1387 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1388 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1389 isl_dim_in
, stmt_id
);
1391 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1393 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1394 set
= internalize_iv(set
, pos
, isl_map_copy(iv_map
));
1395 stmt
->schedule
= isl_set_unwrap(set
);
1398 dim
= isl_space_map_from_set(dim
);
1399 extend
= isl_map_identity(dim
);
1400 extend
= isl_map_remove_dims(extend
, isl_dim_in
, 0, 1);
1401 extend
= isl_map_set_tuple_id(extend
, isl_dim_in
,
1402 isl_map_get_tuple_id(extend
, isl_dim_out
));
1403 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1404 stmt
->args
[i
] = expr_embed(stmt
->args
[i
], isl_map_copy(extend
),
1405 isl_map_copy(iv_map
), var_id
);
1406 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1409 isl_id_free(var_id
);
1411 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1413 return pet_stmt_free(stmt
);
1414 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1415 return pet_stmt_free(stmt
);
1419 isl_map_free(sched
);
1420 isl_map_free(iv_map
);
1421 isl_id_free(var_id
);
1425 /* Embed the given pet_array in an extra outer loop with iteration domain
1427 * This embedding only has an effect on virtual arrays (those with
1428 * user pointer equal to NULL), which need to be extended along with
1429 * the iteration domain.
1431 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1432 __isl_take isl_set
*dom
)
1434 isl_id
*array_id
= NULL
;
1439 if (isl_set_has_tuple_id(array
->extent
))
1440 array_id
= isl_set_get_tuple_id(array
->extent
);
1442 if (array_id
&& !isl_id_get_user(array_id
)) {
1443 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1444 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1447 isl_id_free(array_id
);
1456 /* Project out all unnamed parameters from "set" and return the result.
1458 static __isl_give isl_set
*set_project_out_unnamed_params(
1459 __isl_take isl_set
*set
)
1463 n
= isl_set_dim(set
, isl_dim_param
);
1464 for (i
= n
- 1; i
>= 0; --i
) {
1465 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1467 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1473 /* Update the context with respect to an embedding into a loop
1474 * with iteration domain "dom" and induction variable "id".
1475 * "iv_map" maps a possibly virtual iterator (used in "dom")
1476 * to the real iterator (parameter "id").
1478 * If the current context is independent of "id", we don't need
1480 * Otherwise, a parameter value is invalid for the embedding if
1481 * any of the corresponding iterator values is invalid.
1482 * That is, a parameter value is valid only if all the corresponding
1483 * iterator values are valid.
1484 * We therefore compute the set of parameters
1486 * forall i in dom : valid (i)
1490 * not exists i in dom : not valid(i)
1494 * not exists i in dom \ valid(i)
1496 * Before we subtract valid(i) from dom, we first need to map
1497 * the real iterator to the virtual iterator.
1499 * If there are any unnamed parameters in "dom", then we consider
1500 * a parameter value to be valid if it is valid for any value of those
1501 * unnamed parameters. They are therefore projected out at the end.
1503 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1504 __isl_keep isl_set
*dom
, __isl_keep isl_map
*iv_map
,
1505 __isl_keep isl_id
*id
)
1509 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1513 context
= isl_set_from_params(context
);
1514 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1515 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1516 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1517 context
= isl_set_apply(context
, isl_map_reverse(isl_map_copy(iv_map
)));
1518 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1519 context
= isl_set_params(context
);
1520 context
= isl_set_complement(context
);
1521 context
= set_project_out_unnamed_params(context
);
1525 /* Embed all statements and arrays in "scop" in an extra outer loop
1526 * with iteration domain "dom" and schedule "sched".
1527 * "id" represents the induction variable of the loop.
1528 * "iv_map" maps a possibly virtual iterator to the real iterator.
1529 * That is, it maps the iterator used in "dom" and the domain of "sched"
1530 * to the iterator that some of the parameters in "scop" may refer to.
1532 * Any skip conditions within the loop have no effect outside of the loop.
1533 * The caller is responsible for making sure skip[pet_skip_later] has been
1534 * taken into account.
1536 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1537 __isl_take isl_map
*sched
, __isl_take isl_map
*iv_map
,
1538 __isl_take isl_id
*id
)
1545 pet_scop_reset_skip(scop
, pet_skip_now
);
1546 pet_scop_reset_skip(scop
, pet_skip_later
);
1548 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1552 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1553 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1554 isl_set_copy(dom
), isl_map_copy(sched
),
1555 isl_map_copy(iv_map
), isl_id_copy(id
));
1556 if (!scop
->stmts
[i
])
1560 for (i
= 0; i
< scop
->n_array
; ++i
) {
1561 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1563 if (!scop
->arrays
[i
])
1568 isl_map_free(sched
);
1569 isl_map_free(iv_map
);
1574 isl_map_free(sched
);
1575 isl_map_free(iv_map
);
1577 return pet_scop_free(scop
);
1580 /* Add extra conditions on the parameters to iteration domain of "stmt".
1582 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1583 __isl_take isl_set
*cond
)
1588 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1593 return pet_stmt_free(stmt
);
1596 /* Add extra conditions to scop->skip[type].
1598 * The new skip condition only holds if it held before
1599 * and the condition is true. It does not hold if it did not hold
1600 * before or the condition is false.
1602 * The skip condition is assumed to be an affine expression.
1604 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1605 enum pet_skip type
, __isl_keep isl_set
*cond
)
1607 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1613 if (!ext
->skip
[type
])
1616 if (!set_is_affine(ext
->skip
[type
]))
1617 isl_die(isl_set_get_ctx(ext
->skip
[type
]), isl_error_internal
,
1618 "can only resrict affine skips",
1619 return pet_scop_free(scop
));
1621 skip
= ext
->skip
[type
];
1622 skip
= isl_set_intersect_params(skip
, isl_set_copy(cond
));
1623 set
= isl_set_from_params(isl_set_copy(cond
));
1624 set
= isl_set_complement(set
);
1625 set
= isl_set_add_dims(set
, isl_dim_set
, 1);
1626 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 0);
1627 skip
= isl_set_union(skip
, set
);
1628 ext
->skip
[type
] = skip
;
1629 if (!ext
->skip
[type
])
1630 return pet_scop_free(scop
);
1635 /* Add extra conditions on the parameters to all iteration domains
1636 * and skip conditions.
1638 * A parameter value is valid for the result if it was valid
1639 * for the original scop and satisfies "cond" or if it does
1640 * not satisfy "cond" as in this case the scop is not executed
1641 * and the original constraints on the parameters are irrelevant.
1643 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1644 __isl_take isl_set
*cond
)
1648 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1649 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1654 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1655 scop
->context
= isl_set_union(scop
->context
,
1656 isl_set_complement(isl_set_copy(cond
)));
1657 scop
->context
= isl_set_coalesce(scop
->context
);
1658 scop
->context
= set_project_out_unnamed_params(scop
->context
);
1662 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1663 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1664 isl_set_copy(cond
));
1665 if (!scop
->stmts
[i
])
1673 return pet_scop_free(scop
);
1676 /* Construct a map that inserts a filter value with name "id" and value
1677 * "satisfied" in the list of filter values embedded in the set space "space".
1679 * If "space" does not contain any filter values yet, we first create
1680 * a map that inserts 0 filter values, i.e.,
1682 * space -> [space -> []]
1684 * We can now assume that space is of the form [dom -> [filters]]
1685 * We construct an identity mapping on dom and a mapping on filters
1686 * that inserts the new filter
1689 * [filters] -> [satisfied, filters]
1691 * and then compute the cross product
1693 * [dom -> [filters]] -> [dom -> [satisfied, filters]]
1695 static __isl_give isl_map
*insert_filter_map(__isl_take isl_space
*space
,
1696 __isl_take isl_id
*id
, int satisfied
)
1699 isl_map
*map
, *map_dom
, *map_ran
;
1702 if (isl_space_is_wrapping(space
)) {
1703 space2
= isl_space_map_from_set(isl_space_copy(space
));
1704 map
= isl_map_identity(space2
);
1705 space
= isl_space_unwrap(space
);
1707 space
= isl_space_from_domain(space
);
1708 map
= isl_map_universe(isl_space_copy(space
));
1709 map
= isl_map_reverse(isl_map_domain_map(map
));
1712 space2
= isl_space_domain(isl_space_copy(space
));
1713 map_dom
= isl_map_identity(isl_space_map_from_set(space2
));
1714 space
= isl_space_range(space
);
1715 map_ran
= isl_map_identity(isl_space_map_from_set(space
));
1716 map_ran
= isl_map_insert_dims(map_ran
, isl_dim_out
, 0, 1);
1717 map_ran
= isl_map_set_dim_id(map_ran
, isl_dim_out
, 0, id
);
1718 map_ran
= isl_map_fix_si(map_ran
, isl_dim_out
, 0, satisfied
);
1720 map
= isl_map_apply_range(map
, isl_map_product(map_dom
, map_ran
));
1725 /* Insert an argument expression corresponding to "test" in front
1726 * of the list of arguments described by *n_arg and *args.
1728 static int args_insert_access(unsigned *n_arg
, struct pet_expr
***args
,
1729 __isl_keep isl_map
*test
)
1732 isl_ctx
*ctx
= isl_map_get_ctx(test
);
1738 *args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1742 struct pet_expr
**ext
;
1743 ext
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + *n_arg
);
1746 for (i
= 0; i
< *n_arg
; ++i
)
1747 ext
[1 + i
] = (*args
)[i
];
1752 (*args
)[0] = pet_expr_from_access(isl_map_copy(test
));
1759 /* Make the expression "expr" depend on the value of "test"
1760 * being equal to "satisfied".
1762 * If "test" is an affine expression, we simply add the conditions
1763 * on the expression have the value "satisfied" to all access relations.
1765 * Otherwise, we add a filter to "expr" (which is then assumed to be
1766 * an access expression) corresponding to "test" being equal to "satisfied".
1768 struct pet_expr
*pet_expr_filter(struct pet_expr
*expr
,
1769 __isl_take isl_map
*test
, int satisfied
)
1779 if (!isl_map_has_tuple_id(test
, isl_dim_out
)) {
1780 test
= isl_map_fix_si(test
, isl_dim_out
, 0, satisfied
);
1781 return pet_expr_restrict(expr
, isl_map_params(test
));
1784 ctx
= isl_map_get_ctx(test
);
1785 if (expr
->type
!= pet_expr_access
)
1786 isl_die(ctx
, isl_error_invalid
,
1787 "can only filter access expressions", goto error
);
1789 space
= isl_space_domain(isl_map_get_space(expr
->acc
.access
));
1790 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1791 map
= insert_filter_map(space
, id
, satisfied
);
1793 expr
->acc
.access
= isl_map_apply_domain(expr
->acc
.access
, map
);
1794 if (!expr
->acc
.access
)
1797 if (args_insert_access(&expr
->n_arg
, &expr
->args
, test
) < 0)
1804 return pet_expr_free(expr
);
1807 /* Make the statement "stmt" depend on the value of "test"
1808 * being equal to "satisfied" by adjusting stmt->domain.
1810 * The domain of "test" corresponds to the (zero or more) outer dimensions
1811 * of the iteration domain.
1813 * We insert an argument corresponding to a read to "test"
1814 * from the iteration domain of "stmt" in front of the list of arguments.
1815 * We also insert a corresponding output dimension in the wrapped
1816 * map contained in stmt->domain, with value set to "satisfied".
1818 static struct pet_stmt
*stmt_filter(struct pet_stmt
*stmt
,
1819 __isl_take isl_map
*test
, int satisfied
)
1824 isl_map
*map
, *add_dom
;
1832 id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1833 map
= insert_filter_map(isl_set_get_space(stmt
->domain
), id
, satisfied
);
1834 stmt
->domain
= isl_set_apply(stmt
->domain
, map
);
1836 space
= isl_space_unwrap(isl_set_get_space(stmt
->domain
));
1837 dom
= isl_set_universe(isl_space_domain(space
));
1838 n_test_dom
= isl_map_dim(test
, isl_dim_in
);
1839 add_dom
= isl_map_from_range(dom
);
1840 add_dom
= isl_map_add_dims(add_dom
, isl_dim_in
, n_test_dom
);
1841 for (i
= 0; i
< n_test_dom
; ++i
)
1842 add_dom
= isl_map_equate(add_dom
, isl_dim_in
, i
,
1844 test
= isl_map_apply_domain(test
, add_dom
);
1846 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1853 return pet_stmt_free(stmt
);
1856 /* Does "scop" have a skip condition of the given "type"?
1858 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
1860 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1864 return ext
->skip
[type
] != NULL
;
1867 /* Does "scop" have a skip condition of the given "type" that
1868 * is an affine expression?
1870 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
1872 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1876 if (!ext
->skip
[type
])
1878 return set_is_affine(ext
->skip
[type
]);
1881 /* Does "scop" have a skip condition of the given "type" that
1882 * is not an affine expression?
1884 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
1886 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1891 if (!ext
->skip
[type
])
1893 aff
= set_is_affine(ext
->skip
[type
]);
1899 /* Does "scop" have a skip condition of the given "type" that
1900 * is affine and holds on the entire domain?
1902 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
1904 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1909 is_aff
= pet_scop_has_affine_skip(scop
, type
);
1910 if (is_aff
< 0 || !is_aff
)
1913 set
= isl_set_copy(ext
->skip
[type
]);
1914 set
= isl_set_fix_si(set
, isl_dim_set
, 0, 1);
1915 set
= isl_set_params(set
);
1916 is_univ
= isl_set_plain_is_universe(set
);
1922 /* Replace scop->skip[type] by "skip".
1924 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
1925 enum pet_skip type
, __isl_take isl_set
*skip
)
1927 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1932 isl_set_free(ext
->skip
[type
]);
1933 ext
->skip
[type
] = skip
;
1938 return pet_scop_free(scop
);
1941 /* Return a copy of scop->skip[type].
1943 __isl_give isl_set
*pet_scop_get_skip(struct pet_scop
*scop
,
1946 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1951 return isl_set_copy(ext
->skip
[type
]);
1954 /* Return a map to the skip condition of the given type.
1956 __isl_give isl_map
*pet_scop_get_skip_map(struct pet_scop
*scop
,
1959 return isl_map_from_range(pet_scop_get_skip(scop
, type
));
1962 /* Return an access pet_expr corresponding to the skip condition
1963 * of the given type.
1965 struct pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
1968 return pet_expr_from_access(pet_scop_get_skip_map(scop
, type
));
1971 /* Drop the the skip condition scop->skip[type].
1973 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
1975 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1980 isl_set_free(ext
->skip
[type
]);
1981 ext
->skip
[type
] = NULL
;
1984 /* Make the skip condition (if any) depend on the value of "test" being
1985 * equal to "satisfied".
1987 * We only support the case where the original skip condition is universal,
1988 * i.e., where skipping is unconditional, and where satisfied == 1.
1989 * In this case, the skip condition is changed to skip only when
1990 * "test" is equal to one.
1992 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
1993 enum pet_skip type
, __isl_keep isl_map
*test
, int satisfied
)
1999 if (!pet_scop_has_skip(scop
, type
))
2003 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2005 return pet_scop_free(scop
);
2006 if (satisfied
&& is_univ
) {
2007 scop
= pet_scop_set_skip(scop
, type
,
2008 isl_map_range(isl_map_copy(test
)));
2012 isl_die(isl_map_get_ctx(test
), isl_error_internal
,
2013 "skip expression cannot be filtered",
2014 return pet_scop_free(scop
));
2020 /* Make all statements in "scop" depend on the value of "test"
2021 * being equal to "satisfied" by adjusting their domains.
2023 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2024 __isl_take isl_map
*test
, int satisfied
)
2028 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
2029 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
2034 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2035 scop
->stmts
[i
] = stmt_filter(scop
->stmts
[i
],
2036 isl_map_copy(test
), satisfied
);
2037 if (!scop
->stmts
[i
])
2045 return pet_scop_free(scop
);
2048 /* Do the filters "i" and "j" always have the same value?
2050 static int equal_filter_values(__isl_keep isl_set
*domain
, int i
, int j
)
2052 isl_map
*map
, *test
;
2055 map
= isl_set_unwrap(isl_set_copy(domain
));
2056 test
= isl_map_universe(isl_map_get_space(map
));
2057 test
= isl_map_equate(test
, isl_dim_out
, i
, isl_dim_out
, j
);
2058 equal
= isl_map_is_subset(map
, test
);
2065 /* Merge filters "i" and "j" into a single filter ("i") with as filter
2066 * access relation, the union of the two access relations.
2068 static struct pet_stmt
*merge_filter_pair(struct pet_stmt
*stmt
, int i
, int j
)
2076 stmt
->args
[i
]->acc
.access
= isl_map_union(stmt
->args
[i
]->acc
.access
,
2077 isl_map_copy(stmt
->args
[j
]->acc
.access
));
2078 stmt
->args
[i
]->acc
.access
= isl_map_coalesce(stmt
->args
[i
]->acc
.access
);
2080 pet_expr_free(stmt
->args
[j
]);
2081 for (k
= j
; k
< stmt
->n_arg
- 1; ++k
)
2082 stmt
->args
[k
] = stmt
->args
[k
+ 1];
2085 map
= isl_set_unwrap(stmt
->domain
);
2086 map
= isl_map_project_out(map
, isl_dim_out
, j
, 1);
2087 stmt
->domain
= isl_map_wrap(map
);
2089 if (!stmt
->domain
|| !stmt
->args
[i
]->acc
.access
)
2090 return pet_stmt_free(stmt
);
2095 /* Look for any pair of filters that access the same filter variable
2096 * and that have the same filter value and merge them into a single
2097 * filter with as filter access relation the union of the filter access
2100 static struct pet_stmt
*stmt_merge_filters(struct pet_stmt
*stmt
)
2103 isl_space
*space_i
, *space_j
;
2107 if (stmt
->n_arg
<= 1)
2110 for (i
= 0; i
< stmt
->n_arg
- 1; ++i
) {
2111 if (stmt
->args
[i
]->type
!= pet_expr_access
)
2113 if (pet_expr_is_affine(stmt
->args
[i
]))
2116 space_i
= isl_map_get_space(stmt
->args
[i
]->acc
.access
);
2118 for (j
= stmt
->n_arg
- 1; j
> i
; --j
) {
2121 if (stmt
->args
[j
]->type
!= pet_expr_access
)
2123 if (pet_expr_is_affine(stmt
->args
[j
]))
2126 space_j
= isl_map_get_space(stmt
->args
[j
]->acc
.access
);
2128 eq
= isl_space_is_equal(space_i
, space_j
);
2130 eq
= equal_filter_values(stmt
->domain
, i
, j
);
2132 stmt
= merge_filter_pair(stmt
, i
, j
);
2134 isl_space_free(space_j
);
2136 if (eq
< 0 || !stmt
)
2140 isl_space_free(space_i
);
2143 return pet_stmt_free(stmt
);
2149 /* Look for any pair of filters that access the same filter variable
2150 * and that have the same filter value and merge them into a single
2151 * filter with as filter access relation the union of the filter access
2154 struct pet_scop
*pet_scop_merge_filters(struct pet_scop
*scop
)
2161 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2162 scop
->stmts
[i
] = stmt_merge_filters(scop
->stmts
[i
]);
2163 if (!scop
->stmts
[i
])
2164 return pet_scop_free(scop
);
2170 /* Add all parameters in "expr" to "dim" and return the result.
2172 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
2173 __isl_take isl_space
*dim
)
2179 for (i
= 0; i
< expr
->n_arg
; ++i
)
2181 dim
= expr_collect_params(expr
->args
[i
], dim
);
2183 if (expr
->type
== pet_expr_access
)
2184 dim
= isl_space_align_params(dim
,
2185 isl_map_get_space(expr
->acc
.access
));
2189 isl_space_free(dim
);
2190 return pet_expr_free(expr
);
2193 /* Add all parameters in "stmt" to "dim" and return the result.
2195 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
2196 __isl_take isl_space
*dim
)
2201 dim
= isl_space_align_params(dim
, isl_set_get_space(stmt
->domain
));
2202 dim
= isl_space_align_params(dim
, isl_map_get_space(stmt
->schedule
));
2203 dim
= expr_collect_params(stmt
->body
, dim
);
2207 isl_space_free(dim
);
2208 return pet_stmt_free(stmt
);
2211 /* Add all parameters in "array" to "dim" and return the result.
2213 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
2214 __isl_take isl_space
*dim
)
2219 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->context
));
2220 dim
= isl_space_align_params(dim
, isl_set_get_space(array
->extent
));
2224 pet_array_free(array
);
2225 return isl_space_free(dim
);
2228 /* Add all parameters in "scop" to "dim" and return the result.
2230 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2231 __isl_take isl_space
*dim
)
2238 for (i
= 0; i
< scop
->n_array
; ++i
)
2239 dim
= array_collect_params(scop
->arrays
[i
], dim
);
2241 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2242 dim
= stmt_collect_params(scop
->stmts
[i
], dim
);
2246 isl_space_free(dim
);
2247 return pet_scop_free(scop
);
2250 /* Add all parameters in "dim" to all access relations in "expr".
2252 static struct pet_expr
*expr_propagate_params(struct pet_expr
*expr
,
2253 __isl_take isl_space
*dim
)
2260 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2262 expr_propagate_params(expr
->args
[i
],
2263 isl_space_copy(dim
));
2268 if (expr
->type
== pet_expr_access
) {
2269 expr
->acc
.access
= isl_map_align_params(expr
->acc
.access
,
2270 isl_space_copy(dim
));
2271 if (!expr
->acc
.access
)
2275 isl_space_free(dim
);
2278 isl_space_free(dim
);
2279 return pet_expr_free(expr
);
2282 /* Add all parameters in "dim" to the domain, schedule and
2283 * all access relations in "stmt".
2285 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2286 __isl_take isl_space
*dim
)
2291 stmt
->domain
= isl_set_align_params(stmt
->domain
, isl_space_copy(dim
));
2292 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2293 isl_space_copy(dim
));
2294 stmt
->body
= expr_propagate_params(stmt
->body
, isl_space_copy(dim
));
2296 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2299 isl_space_free(dim
);
2302 isl_space_free(dim
);
2303 return pet_stmt_free(stmt
);
2306 /* Add all parameters in "dim" to "array".
2308 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2309 __isl_take isl_space
*dim
)
2314 array
->context
= isl_set_align_params(array
->context
,
2315 isl_space_copy(dim
));
2316 array
->extent
= isl_set_align_params(array
->extent
,
2317 isl_space_copy(dim
));
2318 if (array
->value_bounds
) {
2319 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2320 isl_space_copy(dim
));
2321 if (!array
->value_bounds
)
2325 if (!array
->context
|| !array
->extent
)
2328 isl_space_free(dim
);
2331 isl_space_free(dim
);
2332 return pet_array_free(array
);
2335 /* Add all parameters in "dim" to "scop".
2337 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2338 __isl_take isl_space
*dim
)
2345 for (i
= 0; i
< scop
->n_array
; ++i
) {
2346 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2347 isl_space_copy(dim
));
2348 if (!scop
->arrays
[i
])
2352 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2353 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2354 isl_space_copy(dim
));
2355 if (!scop
->stmts
[i
])
2359 isl_space_free(dim
);
2362 isl_space_free(dim
);
2363 return pet_scop_free(scop
);
2366 /* Update all isl_sets and isl_maps in "scop" such that they all
2367 * have the same parameters.
2369 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2376 dim
= isl_set_get_space(scop
->context
);
2377 dim
= scop_collect_params(scop
, dim
);
2379 scop
->context
= isl_set_align_params(scop
->context
, isl_space_copy(dim
));
2380 scop
= scop_propagate_params(scop
, dim
);
2385 /* Check if the given access relation accesses a (0D) array that corresponds
2386 * to one of the parameters in "dim". If so, replace the array access
2387 * by an access to the set of integers with as index (and value)
2390 static __isl_give isl_map
*access_detect_parameter(__isl_take isl_map
*access
,
2391 __isl_take isl_space
*dim
)
2393 isl_id
*array_id
= NULL
;
2396 if (isl_map_has_tuple_id(access
, isl_dim_out
)) {
2397 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
2398 pos
= isl_space_find_dim_by_id(dim
, isl_dim_param
, array_id
);
2400 isl_space_free(dim
);
2403 isl_id_free(array_id
);
2407 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, array_id
);
2409 access
= isl_map_insert_dims(access
, isl_dim_param
, 0, 1);
2410 access
= isl_map_set_dim_id(access
, isl_dim_param
, 0, array_id
);
2413 isl_id_free(array_id
);
2415 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
2416 access
= isl_map_equate(access
, isl_dim_param
, pos
, isl_dim_out
, 0);
2421 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2422 * in "dim" by a value equal to the corresponding parameter.
2424 static struct pet_expr
*expr_detect_parameter_accesses(struct pet_expr
*expr
,
2425 __isl_take isl_space
*dim
)
2432 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2434 expr_detect_parameter_accesses(expr
->args
[i
],
2435 isl_space_copy(dim
));
2440 if (expr
->type
== pet_expr_access
) {
2441 expr
->acc
.access
= access_detect_parameter(expr
->acc
.access
,
2442 isl_space_copy(dim
));
2443 if (!expr
->acc
.access
)
2447 isl_space_free(dim
);
2450 isl_space_free(dim
);
2451 return pet_expr_free(expr
);
2454 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2455 * in "dim" by a value equal to the corresponding parameter.
2457 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
2458 __isl_take isl_space
*dim
)
2463 stmt
->body
= expr_detect_parameter_accesses(stmt
->body
,
2464 isl_space_copy(dim
));
2466 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2469 isl_space_free(dim
);
2472 isl_space_free(dim
);
2473 return pet_stmt_free(stmt
);
2476 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2477 * in "dim" by a value equal to the corresponding parameter.
2479 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
2480 __isl_take isl_space
*dim
)
2487 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2488 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
2489 isl_space_copy(dim
));
2490 if (!scop
->stmts
[i
])
2494 isl_space_free(dim
);
2497 isl_space_free(dim
);
2498 return pet_scop_free(scop
);
2501 /* Replace all accesses to (0D) arrays that correspond to any of
2502 * the parameters used in "scop" by a value equal
2503 * to the corresponding parameter.
2505 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
2512 dim
= isl_set_get_space(scop
->context
);
2513 dim
= scop_collect_params(scop
, dim
);
2515 scop
= scop_detect_parameter_accesses(scop
, dim
);
2520 /* Add all read access relations (if "read" is set) and/or all write
2521 * access relations (if "write" is set) to "accesses" and return the result.
2523 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
2524 int read
, int write
, __isl_take isl_union_map
*accesses
)
2533 for (i
= 0; i
< expr
->n_arg
; ++i
)
2534 accesses
= expr_collect_accesses(expr
->args
[i
],
2535 read
, write
, accesses
);
2537 if (expr
->type
== pet_expr_access
&&
2538 isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
) &&
2539 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)))
2540 accesses
= isl_union_map_add_map(accesses
,
2541 isl_map_copy(expr
->acc
.access
));
2546 /* Collect and return all read access relations (if "read" is set)
2547 * and/or all write * access relations (if "write" is set) in "stmt".
2549 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2550 int read
, int write
, __isl_take isl_space
*dim
)
2552 isl_union_map
*accesses
;
2557 accesses
= isl_union_map_empty(dim
);
2558 accesses
= expr_collect_accesses(stmt
->body
, read
, write
, accesses
);
2559 accesses
= isl_union_map_intersect_domain(accesses
,
2560 isl_union_set_from_set(isl_set_copy(stmt
->domain
)));
2565 /* Collect and return all read access relations (if "read" is set)
2566 * and/or all write * access relations (if "write" is set) in "scop".
2568 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2569 int read
, int write
)
2572 isl_union_map
*accesses
;
2577 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2579 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2580 isl_union_map
*accesses_i
;
2581 isl_space
*dim
= isl_set_get_space(scop
->context
);
2582 accesses_i
= stmt_collect_accesses(scop
->stmts
[i
],
2584 accesses
= isl_union_map_union(accesses
, accesses_i
);
2590 __isl_give isl_union_map
*pet_scop_collect_reads(struct pet_scop
*scop
)
2592 return scop_collect_accesses(scop
, 1, 0);
2595 __isl_give isl_union_map
*pet_scop_collect_writes(struct pet_scop
*scop
)
2597 return scop_collect_accesses(scop
, 0, 1);
2600 /* Collect and return the union of iteration domains in "scop".
2602 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2606 isl_union_set
*domain
;
2611 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2613 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2614 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2615 domain
= isl_union_set_add_set(domain
, domain_i
);
2621 /* Collect and return the schedules of the statements in "scop".
2622 * The range is normalized to the maximal number of scheduling
2625 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2628 isl_map
*schedule_i
;
2629 isl_union_map
*schedule
;
2630 int depth
, max_depth
= 0;
2635 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2637 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2638 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2639 if (depth
> max_depth
)
2643 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2644 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2645 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2646 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2648 for (j
= depth
; j
< max_depth
; ++j
)
2649 schedule_i
= isl_map_fix_si(schedule_i
,
2651 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2657 /* Does expression "expr" write to "id"?
2659 static int expr_writes(struct pet_expr
*expr
, __isl_keep isl_id
*id
)
2664 for (i
= 0; i
< expr
->n_arg
; ++i
) {
2665 int writes
= expr_writes(expr
->args
[i
], id
);
2666 if (writes
< 0 || writes
)
2670 if (expr
->type
!= pet_expr_access
)
2672 if (!expr
->acc
.write
)
2674 if (!isl_map_has_tuple_id(expr
->acc
.access
, isl_dim_out
))
2677 write_id
= isl_map_get_tuple_id(expr
->acc
.access
, isl_dim_out
);
2678 isl_id_free(write_id
);
2683 return write_id
== id
;
2686 /* Does statement "stmt" write to "id"?
2688 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
2690 return expr_writes(stmt
->body
, id
);
2693 /* Is there any write access in "scop" that accesses "id"?
2695 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
2702 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2703 int writes
= stmt_writes(scop
->stmts
[i
], id
);
2704 if (writes
< 0 || writes
)
2711 /* Reset the user pointer on the tuple id and all parameter ids in "set".
2713 static __isl_give isl_set
*set_anonymize(__isl_take isl_set
*set
)
2717 n
= isl_set_dim(set
, isl_dim_param
);
2718 for (i
= 0; i
< n
; ++i
) {
2719 isl_id
*id
= isl_set_get_dim_id(set
, isl_dim_param
, i
);
2720 const char *name
= isl_id_get_name(id
);
2721 set
= isl_set_set_dim_name(set
, isl_dim_param
, i
, name
);
2725 if (!isl_set_is_params(set
) && isl_set_has_tuple_id(set
)) {
2726 isl_id
*id
= isl_set_get_tuple_id(set
);
2727 const char *name
= isl_id_get_name(id
);
2728 set
= isl_set_set_tuple_name(set
, name
);
2735 /* Reset the user pointer on the tuple ids and all parameter ids in "map".
2737 static __isl_give isl_map
*map_anonymize(__isl_take isl_map
*map
)
2741 n
= isl_map_dim(map
, isl_dim_param
);
2742 for (i
= 0; i
< n
; ++i
) {
2743 isl_id
*id
= isl_map_get_dim_id(map
, isl_dim_param
, i
);
2744 const char *name
= isl_id_get_name(id
);
2745 map
= isl_map_set_dim_name(map
, isl_dim_param
, i
, name
);
2749 if (isl_map_has_tuple_id(map
, isl_dim_in
)) {
2750 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_in
);
2751 const char *name
= isl_id_get_name(id
);
2752 map
= isl_map_set_tuple_name(map
, isl_dim_in
, name
);
2756 if (isl_map_has_tuple_id(map
, isl_dim_out
)) {
2757 isl_id
*id
= isl_map_get_tuple_id(map
, isl_dim_out
);
2758 const char *name
= isl_id_get_name(id
);
2759 map
= isl_map_set_tuple_name(map
, isl_dim_out
, name
);
2766 /* Reset the user pointer on all parameter ids in "array".
2768 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2773 array
->context
= set_anonymize(array
->context
);
2774 array
->extent
= set_anonymize(array
->extent
);
2775 if (!array
->context
|| !array
->extent
)
2776 return pet_array_free(array
);
2781 /* Reset the user pointer on all parameter and tuple ids in "access".
2783 static __isl_give isl_map
*access_anonymize(__isl_take isl_map
*access
,
2786 access
= map_anonymize(access
);
2791 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2793 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2802 stmt
->domain
= set_anonymize(stmt
->domain
);
2803 stmt
->schedule
= map_anonymize(stmt
->schedule
);
2804 if (!stmt
->domain
|| !stmt
->schedule
)
2805 return pet_stmt_free(stmt
);
2807 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2808 stmt
->args
[i
] = pet_expr_foreach_access(stmt
->args
[i
],
2809 &access_anonymize
, NULL
);
2811 return pet_stmt_free(stmt
);
2814 stmt
->body
= pet_expr_foreach_access(stmt
->body
,
2815 &access_anonymize
, NULL
);
2817 return pet_stmt_free(stmt
);
2822 /* Reset the user pointer on all parameter and tuple ids in "scop".
2824 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2831 scop
->context
= set_anonymize(scop
->context
);
2832 scop
->context_value
= set_anonymize(scop
->context_value
);
2833 if (!scop
->context
|| !scop
->context_value
)
2834 return pet_scop_free(scop
);
2836 for (i
= 0; i
< scop
->n_array
; ++i
) {
2837 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2838 if (!scop
->arrays
[i
])
2839 return pet_scop_free(scop
);
2842 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2843 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2844 if (!scop
->stmts
[i
])
2845 return pet_scop_free(scop
);
2851 /* Given a set "domain", return a wrapped relation with the given set
2852 * as domain and a range of dimension "n_arg", where each coordinate
2853 * is either unbounded or, if the corresponding element of args is of
2854 * type pet_expr_access, bounded by the bounds specified by "value_bounds".
2856 static __isl_give isl_set
*apply_value_bounds(__isl_take isl_set
*domain
,
2857 unsigned n_arg
, struct pet_expr
**args
,
2858 __isl_keep isl_union_map
*value_bounds
)
2863 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
2865 map
= isl_map_from_domain(domain
);
2866 space
= isl_map_get_space(map
);
2867 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
2869 for (i
= 0; i
< n_arg
; ++i
) {
2871 struct pet_expr
*arg
= args
[i
];
2875 map_i
= isl_map_universe(isl_space_copy(space
));
2876 if (arg
->type
== pet_expr_access
) {
2878 id
= isl_map_get_tuple_id(arg
->acc
.access
, isl_dim_out
);
2879 space2
= isl_space_alloc(ctx
, 0, 0, 1);
2880 space2
= isl_space_set_tuple_id(space2
, isl_dim_in
, id
);
2881 vb
= isl_union_map_extract_map(value_bounds
, space2
);
2882 if (!isl_map_plain_is_empty(vb
))
2883 map_i
= isl_map_intersect_range(map_i
,
2888 map
= isl_map_flat_range_product(map
, map_i
);
2890 isl_space_free(space
);
2892 return isl_map_wrap(map
);
2895 /* Data used in access_gist() callback.
2897 struct pet_access_gist_data
{
2899 isl_union_map
*value_bounds
;
2902 /* Given an expression "expr" of type pet_expr_access, compute
2903 * the gist of the associated access relation with respect to
2904 * data->domain and the bounds on the values of the arguments
2905 * of the expression.
2907 static struct pet_expr
*access_gist(struct pet_expr
*expr
, void *user
)
2909 struct pet_access_gist_data
*data
= user
;
2912 domain
= isl_set_copy(data
->domain
);
2913 if (expr
->n_arg
> 0)
2914 domain
= apply_value_bounds(domain
, expr
->n_arg
, expr
->args
,
2915 data
->value_bounds
);
2917 expr
->acc
.access
= isl_map_gist_domain(expr
->acc
.access
, domain
);
2918 if (!expr
->acc
.access
)
2919 return pet_expr_free(expr
);
2924 /* Compute the gist of the iteration domain and all access relations
2925 * of "stmt" based on the constraints on the parameters specified by "context"
2926 * and the constraints on the values of nested accesses specified
2927 * by "value_bounds".
2929 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2930 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2935 struct pet_access_gist_data data
;
2940 data
.domain
= isl_set_copy(stmt
->domain
);
2941 data
.value_bounds
= value_bounds
;
2942 if (stmt
->n_arg
> 0)
2943 data
.domain
= isl_map_domain(isl_set_unwrap(data
.domain
));
2945 data
.domain
= isl_set_intersect_params(data
.domain
,
2946 isl_set_copy(context
));
2948 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2949 stmt
->args
[i
] = pet_expr_foreach_access_expr(stmt
->args
[i
],
2950 &access_gist
, &data
);
2955 stmt
->body
= pet_expr_foreach_access_expr(stmt
->body
,
2956 &access_gist
, &data
);
2960 isl_set_free(data
.domain
);
2962 space
= isl_set_get_space(stmt
->domain
);
2963 if (isl_space_is_wrapping(space
))
2964 space
= isl_space_domain(isl_space_unwrap(space
));
2965 domain
= isl_set_universe(space
);
2966 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2967 if (stmt
->n_arg
> 0)
2968 domain
= apply_value_bounds(domain
, stmt
->n_arg
, stmt
->args
,
2970 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
2972 return pet_stmt_free(stmt
);
2976 isl_set_free(data
.domain
);
2977 return pet_stmt_free(stmt
);
2980 /* Compute the gist of the extent of the array
2981 * based on the constraints on the parameters specified by "context".
2983 static struct pet_array
*array_gist(struct pet_array
*array
,
2984 __isl_keep isl_set
*context
)
2989 array
->extent
= isl_set_gist_params(array
->extent
,
2990 isl_set_copy(context
));
2992 return pet_array_free(array
);
2997 /* Compute the gist of all sets and relations in "scop"
2998 * based on the constraints on the parameters specified by "scop->context"
2999 * and the constraints on the values of nested accesses specified
3000 * by "value_bounds".
3002 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
3003 __isl_keep isl_union_map
*value_bounds
)
3010 scop
->context
= isl_set_coalesce(scop
->context
);
3012 return pet_scop_free(scop
);
3014 for (i
= 0; i
< scop
->n_array
; ++i
) {
3015 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
3016 if (!scop
->arrays
[i
])
3017 return pet_scop_free(scop
);
3020 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3021 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
3023 if (!scop
->stmts
[i
])
3024 return pet_scop_free(scop
);
3030 /* Intersect the context of "scop" with "context".
3031 * To ensure that we don't introduce any unnamed parameters in
3032 * the context of "scop", we first remove the unnamed parameters
3035 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
3036 __isl_take isl_set
*context
)
3041 context
= set_project_out_unnamed_params(context
);
3042 scop
->context
= isl_set_intersect(scop
->context
, context
);
3044 return pet_scop_free(scop
);
3048 isl_set_free(context
);
3049 return pet_scop_free(scop
);
3052 /* Drop the current context of "scop". That is, replace the context
3053 * by a universal set.
3055 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
3062 space
= isl_set_get_space(scop
->context
);
3063 isl_set_free(scop
->context
);
3064 scop
->context
= isl_set_universe(space
);
3066 return pet_scop_free(scop
);
3071 /* Append "array" to the arrays of "scop".
3073 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
3074 struct pet_array
*array
)
3077 struct pet_array
**arrays
;
3079 if (!array
|| !scop
)
3082 ctx
= isl_set_get_ctx(scop
->context
);
3083 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3087 scop
->arrays
= arrays
;
3088 scop
->arrays
[scop
->n_array
] = array
;
3093 pet_array_free(array
);
3094 return pet_scop_free(scop
);