2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2014 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>
43 #include "value_bounds.h"
45 /* pet_scop with extra information that is used during parsing and printing.
47 * In particular, we keep track of conditions under which we want
48 * to skip the rest of the current loop iteration (skip[pet_skip_now])
49 * and of conditions under which we want to skip subsequent
50 * loop iterations (skip[pet_skip_later]).
52 * The conditions are represented as index expressions defined
53 * over a zero-dimensional domain. The index expression is either
54 * a boolean affine expression or an access to a variable, which
55 * is assumed to attain values zero and one. The condition holds
56 * if the variable has value one or if the affine expression
57 * has value one (typically for only part of the parameter space).
59 * A missing condition (skip[type] == NULL) means that we don't want
62 * Additionally, we keep track of the original input file
63 * inside pet_transform_C_source.
68 isl_multi_pw_aff
*skip
[2];
72 /* Construct a pet_stmt with given line number and statement
73 * number from a pet_expr.
74 * The initial iteration domain is the zero-dimensional universe.
75 * The name of the domain is given by "label" if it is non-NULL.
76 * Otherwise, the name is constructed as S_<id>.
77 * The domains of all access relations are modified to refer
78 * to the statement iteration domain.
80 struct pet_stmt
*pet_stmt_from_pet_expr(isl_ctx
*ctx
, int line
,
81 __isl_take isl_id
*label
, int id
, struct pet_expr
*expr
)
83 struct pet_stmt
*stmt
;
87 isl_multi_pw_aff
*add_name
;
93 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
97 dim
= isl_space_set_alloc(ctx
, 0, 0);
99 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
101 snprintf(name
, sizeof(name
), "S_%d", id
);
102 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
104 dom
= isl_set_universe(isl_space_copy(dim
));
105 sched
= isl_map_from_domain(isl_set_copy(dom
));
107 dim
= isl_space_from_domain(dim
);
108 add_name
= isl_multi_pw_aff_zero(dim
);
109 expr
= pet_expr_update_domain(expr
, add_name
);
113 stmt
->schedule
= sched
;
116 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
117 return pet_stmt_free(stmt
);
126 void *pet_stmt_free(struct pet_stmt
*stmt
)
133 isl_set_free(stmt
->domain
);
134 isl_map_free(stmt
->schedule
);
135 pet_expr_free(stmt
->body
);
137 for (i
= 0; i
< stmt
->n_arg
; ++i
)
138 pet_expr_free(stmt
->args
[i
]);
145 /* Return the iteration space of "stmt".
147 * If the statement has arguments, then stmt->domain is a wrapped map
148 * mapping the iteration domain to the values of the arguments
149 * for which this statement is executed.
150 * In this case, we need to extract the domain space of this wrapped map.
152 __isl_give isl_space
*pet_stmt_get_space(struct pet_stmt
*stmt
)
159 space
= isl_set_get_space(stmt
->domain
);
160 if (isl_space_is_wrapping(space
))
161 space
= isl_space_domain(isl_space_unwrap(space
));
166 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
173 fprintf(stderr
, "%*s%d\n", indent
, "", stmt
->line
);
174 fprintf(stderr
, "%*s", indent
, "");
175 isl_set_dump(stmt
->domain
);
176 fprintf(stderr
, "%*s", indent
, "");
177 isl_map_dump(stmt
->schedule
);
178 pet_expr_dump_with_indent(stmt
->body
, indent
);
179 for (i
= 0; i
< stmt
->n_arg
; ++i
)
180 pet_expr_dump_with_indent(stmt
->args
[i
], indent
+ 2);
183 void pet_stmt_dump(struct pet_stmt
*stmt
)
188 /* Allocate a new pet_type with the given "name" and "definition".
190 struct pet_type
*pet_type_alloc(isl_ctx
*ctx
, const char *name
,
191 const char *definition
)
193 struct pet_type
*type
;
195 type
= isl_alloc_type(ctx
, struct pet_type
);
199 type
->name
= strdup(name
);
200 type
->definition
= strdup(definition
);
202 if (!type
->name
|| !type
->definition
)
203 return pet_type_free(type
);
208 /* Free "type" and return NULL.
210 struct pet_type
*pet_type_free(struct pet_type
*type
)
216 free(type
->definition
);
222 struct pet_array
*pet_array_free(struct pet_array
*array
)
227 isl_set_free(array
->context
);
228 isl_set_free(array
->extent
);
229 isl_set_free(array
->value_bounds
);
230 free(array
->element_type
);
236 void pet_array_dump(struct pet_array
*array
)
241 isl_set_dump(array
->context
);
242 isl_set_dump(array
->extent
);
243 isl_set_dump(array
->value_bounds
);
244 fprintf(stderr
, "%s%s%s\n", array
->element_type
,
245 array
->element_is_record
? " element-is-record" : "",
246 array
->live_out
? " live-out" : "");
249 /* Alloc a pet_scop structure, with extra room for information that
250 * is only used during parsing.
252 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
254 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
257 /* Construct a pet_scop with room for n statements.
259 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
262 struct pet_scop
*scop
;
264 scop
= pet_scop_alloc(ctx
);
268 space
= isl_space_params_alloc(ctx
, 0);
269 scop
->context
= isl_set_universe(isl_space_copy(space
));
270 scop
->context_value
= isl_set_universe(space
);
271 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
272 if (!scop
->context
|| !scop
->stmts
)
273 return pet_scop_free(scop
);
280 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
282 return scop_alloc(ctx
, 0);
285 /* Update "context" with respect to the valid parameter values for "access".
287 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
288 __isl_take isl_set
*context
)
290 context
= isl_set_intersect(context
,
291 isl_map_params(isl_map_copy(access
)));
295 /* Update "context" with respect to the valid parameter values for "expr".
297 * If "expr" represents a conditional operator, then a parameter value
298 * needs to be valid for the condition and for at least one of the
299 * remaining two arguments.
300 * If the condition is an affine expression, then we can be a bit more specific.
301 * The parameter then has to be valid for the second argument for
302 * non-zero accesses and valid for the third argument for zero accesses.
304 static __isl_give isl_set
*expr_extract_context(struct pet_expr
*expr
,
305 __isl_take isl_set
*context
)
309 if (expr
->type
== pet_expr_op
&& expr
->op
== pet_op_cond
) {
311 isl_set
*context1
, *context2
;
313 is_aff
= pet_expr_is_affine(expr
->args
[0]);
317 context
= expr_extract_context(expr
->args
[0], context
);
318 context1
= expr_extract_context(expr
->args
[1],
319 isl_set_copy(context
));
320 context2
= expr_extract_context(expr
->args
[2], context
);
326 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
327 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
328 zero_set
= isl_map_params(access
);
329 context1
= isl_set_subtract(context1
,
330 isl_set_copy(zero_set
));
331 context2
= isl_set_intersect(context2
, zero_set
);
334 context
= isl_set_union(context1
, context2
);
335 context
= isl_set_coalesce(context
);
340 for (i
= 0; i
< expr
->n_arg
; ++i
)
341 context
= expr_extract_context(expr
->args
[i
], context
);
343 if (expr
->type
== pet_expr_access
)
344 context
= access_extract_context(expr
->acc
.access
, context
);
348 isl_set_free(context
);
352 /* Update "context" with respect to the valid parameter values for "stmt".
354 * If the statement is an assume statement with an affine expression,
355 * then intersect "context" with that expression.
356 * Otherwise, intersect "context" with the contexts of the expressions
359 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
360 __isl_take isl_set
*context
)
364 if (pet_stmt_is_assume(stmt
) &&
365 pet_expr_is_affine(stmt
->body
->args
[0])) {
366 isl_multi_pw_aff
*index
;
370 index
= stmt
->body
->args
[0]->acc
.index
;
371 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
372 cond
= isl_set_params(isl_pw_aff_non_zero_set(pa
));
373 return isl_set_intersect(context
, cond
);
376 for (i
= 0; i
< stmt
->n_arg
; ++i
)
377 context
= expr_extract_context(stmt
->args
[i
], context
);
379 context
= expr_extract_context(stmt
->body
, context
);
384 /* Construct a pet_scop that contains the given pet_stmt.
386 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
388 struct pet_scop
*scop
;
393 scop
= scop_alloc(ctx
, 1);
397 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
401 scop
->stmts
[0] = stmt
;
410 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
411 * does it represent an affine expression?
413 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff
*mpa
)
417 has_id
= isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
);
424 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
426 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
427 __isl_take isl_set
*dom
)
430 pa
= isl_set_indicator_function(set
);
431 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
435 /* Return "lhs || rhs", defined on the shared definition domain.
437 static __isl_give isl_pw_aff
*pw_aff_or(__isl_take isl_pw_aff
*lhs
,
438 __isl_take isl_pw_aff
*rhs
)
443 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
444 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
445 cond
= isl_set_union(isl_pw_aff_non_zero_set(lhs
),
446 isl_pw_aff_non_zero_set(rhs
));
447 cond
= isl_set_coalesce(cond
);
448 return indicator_function(cond
, dom
);
451 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
452 * ext may be equal to either ext1 or ext2.
454 * The two skips that need to be combined are assumed to be affine expressions.
456 * We need to skip in ext if we need to skip in either ext1 or ext2.
457 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
459 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
460 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
463 isl_pw_aff
*skip
, *skip1
, *skip2
;
467 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
469 if (!ext1
->skip
[type
]) {
472 ext
->skip
[type
] = ext2
->skip
[type
];
473 ext2
->skip
[type
] = NULL
;
476 if (!ext2
->skip
[type
]) {
479 ext
->skip
[type
] = ext1
->skip
[type
];
480 ext1
->skip
[type
] = NULL
;
484 if (!multi_pw_aff_is_affine(ext1
->skip
[type
]) ||
485 !multi_pw_aff_is_affine(ext2
->skip
[type
]))
486 isl_die(isl_multi_pw_aff_get_ctx(ext1
->skip
[type
]),
487 isl_error_internal
, "can only combine affine skips",
490 skip1
= isl_multi_pw_aff_get_pw_aff(ext1
->skip
[type
], 0);
491 skip2
= isl_multi_pw_aff_get_pw_aff(ext2
->skip
[type
], 0);
492 skip
= pw_aff_or(skip1
, skip2
);
493 isl_multi_pw_aff_free(ext1
->skip
[type
]);
494 ext1
->skip
[type
] = NULL
;
495 isl_multi_pw_aff_free(ext2
->skip
[type
]);
496 ext2
->skip
[type
] = NULL
;
497 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
498 if (!ext
->skip
[type
])
503 pet_scop_free(&ext
->scop
);
507 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
508 * where type takes on the values pet_skip_now and pet_skip_later.
509 * scop may be equal to either scop1 or scop2.
511 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
512 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
514 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
515 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
516 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
518 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
519 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
523 /* Update scop->start and scop->end to include the region from "start"
524 * to "end". In particular, if scop->end == 0, then "scop" does not
525 * have any offset information yet and we simply take the information
526 * from "start" and "end". Otherwise, we update the fields if the
527 * region from "start" to "end" is not already included.
529 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
530 unsigned start
, unsigned end
)
534 if (scop
->end
== 0) {
538 if (start
< scop
->start
)
547 /* Does "implication" appear in the list of implications of "scop"?
549 static int is_known_implication(struct pet_scop
*scop
,
550 struct pet_implication
*implication
)
554 for (i
= 0; i
< scop
->n_implication
; ++i
) {
555 struct pet_implication
*pi
= scop
->implications
[i
];
558 if (pi
->satisfied
!= implication
->satisfied
)
560 equal
= isl_map_is_equal(pi
->extension
, implication
->extension
);
570 /* Store the concatenation of the implications of "scop1" and "scop2"
571 * in "scop", removing duplicates (i.e., implications in "scop2" that
572 * already appear in "scop1").
574 static struct pet_scop
*scop_collect_implications(isl_ctx
*ctx
,
575 struct pet_scop
*scop
, struct pet_scop
*scop1
, struct pet_scop
*scop2
)
582 if (scop2
->n_implication
== 0) {
583 scop
->n_implication
= scop1
->n_implication
;
584 scop
->implications
= scop1
->implications
;
585 scop1
->n_implication
= 0;
586 scop1
->implications
= NULL
;
590 if (scop1
->n_implication
== 0) {
591 scop
->n_implication
= scop2
->n_implication
;
592 scop
->implications
= scop2
->implications
;
593 scop2
->n_implication
= 0;
594 scop2
->implications
= NULL
;
598 scop
->implications
= isl_calloc_array(ctx
, struct pet_implication
*,
599 scop1
->n_implication
+ scop2
->n_implication
);
600 if (!scop
->implications
)
601 return pet_scop_free(scop
);
603 for (i
= 0; i
< scop1
->n_implication
; ++i
) {
604 scop
->implications
[i
] = scop1
->implications
[i
];
605 scop1
->implications
[i
] = NULL
;
608 scop
->n_implication
= scop1
->n_implication
;
609 j
= scop1
->n_implication
;
610 for (i
= 0; i
< scop2
->n_implication
; ++i
) {
613 known
= is_known_implication(scop
, scop2
->implications
[i
]);
615 return pet_scop_free(scop
);
618 scop
->implications
[j
++] = scop2
->implications
[i
];
619 scop2
->implications
[i
] = NULL
;
621 scop
->n_implication
= j
;
626 /* Combine the offset information of "scop1" and "scop2" into "scop".
628 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
629 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
632 scop
= pet_scop_update_start_end(scop
,
633 scop1
->start
, scop1
->end
);
635 scop
= pet_scop_update_start_end(scop
,
636 scop2
->start
, scop2
->end
);
640 /* Construct a pet_scop that contains the offset information,
641 * arrays, statements and skip information in "scop1" and "scop2".
643 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
644 struct pet_scop
*scop2
)
647 struct pet_scop
*scop
= NULL
;
649 if (!scop1
|| !scop2
)
652 if (scop1
->n_stmt
== 0) {
653 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
654 pet_scop_free(scop1
);
658 if (scop2
->n_stmt
== 0) {
659 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
660 pet_scop_free(scop2
);
664 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
668 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
669 scop1
->n_array
+ scop2
->n_array
);
672 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
674 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
675 scop
->stmts
[i
] = scop1
->stmts
[i
];
676 scop1
->stmts
[i
] = NULL
;
679 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
680 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
681 scop2
->stmts
[i
] = NULL
;
684 for (i
= 0; i
< scop1
->n_array
; ++i
) {
685 scop
->arrays
[i
] = scop1
->arrays
[i
];
686 scop1
->arrays
[i
] = NULL
;
689 for (i
= 0; i
< scop2
->n_array
; ++i
) {
690 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
691 scop2
->arrays
[i
] = NULL
;
694 scop
= scop_collect_implications(ctx
, scop
, scop1
, scop2
);
695 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
696 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
697 scop
= scop_combine_skips(scop
, scop1
, scop2
);
698 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
700 pet_scop_free(scop1
);
701 pet_scop_free(scop2
);
704 pet_scop_free(scop1
);
705 pet_scop_free(scop2
);
710 /* Apply the skip condition "skip" to "scop".
711 * That is, make sure "scop" is not executed when the condition holds.
713 * If "skip" is an affine expression, we add the conditions under
714 * which the expression is zero to the iteration domains.
715 * Otherwise, we add a filter on the variable attaining the value zero.
717 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
718 __isl_take isl_multi_pw_aff
*skip
)
727 is_aff
= multi_pw_aff_is_affine(skip
);
732 return pet_scop_filter(scop
, skip
, 0);
734 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
735 isl_multi_pw_aff_free(skip
);
736 zero
= isl_set_params(isl_pw_aff_zero_set(pa
));
737 scop
= pet_scop_restrict(scop
, zero
);
741 isl_multi_pw_aff_free(skip
);
742 return pet_scop_free(scop
);
745 /* Construct a pet_scop that contains the arrays, statements and
746 * skip information in "scop1" and "scop2", where the two scops
747 * are executed "in sequence". That is, breaks and continues
748 * in scop1 have an effect on scop2.
750 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
751 struct pet_scop
*scop2
)
753 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
754 scop2
= restrict_skip(scop2
,
755 pet_scop_get_skip(scop1
, pet_skip_now
));
756 return pet_scop_add(ctx
, scop1
, scop2
);
759 /* Construct a pet_scop that contains the arrays, statements and
760 * skip information in "scop1" and "scop2", where the two scops
761 * are executed "in parallel". That is, any break or continue
762 * in scop1 has no effect on scop2.
764 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
765 struct pet_scop
*scop2
)
767 return pet_scop_add(ctx
, scop1
, scop2
);
770 void *pet_implication_free(struct pet_implication
*implication
)
777 isl_map_free(implication
->extension
);
783 struct pet_scop
*pet_scop_free(struct pet_scop
*scop
)
786 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
790 isl_set_free(scop
->context
);
791 isl_set_free(scop
->context_value
);
793 for (i
= 0; i
< scop
->n_type
; ++i
)
794 pet_type_free(scop
->types
[i
]);
797 for (i
= 0; i
< scop
->n_array
; ++i
)
798 pet_array_free(scop
->arrays
[i
]);
801 for (i
= 0; i
< scop
->n_stmt
; ++i
)
802 pet_stmt_free(scop
->stmts
[i
]);
804 if (scop
->implications
)
805 for (i
= 0; i
< scop
->n_implication
; ++i
)
806 pet_implication_free(scop
->implications
[i
]);
807 free(scop
->implications
);
808 isl_multi_pw_aff_free(ext
->skip
[pet_skip_now
]);
809 isl_multi_pw_aff_free(ext
->skip
[pet_skip_later
]);
814 void pet_type_dump(struct pet_type
*type
)
819 fprintf(stderr
, "%s -> %s\n", type
->name
, type
->definition
);
822 void pet_implication_dump(struct pet_implication
*implication
)
827 fprintf(stderr
, "%d\n", implication
->satisfied
);
828 isl_map_dump(implication
->extension
);
831 void pet_scop_dump(struct pet_scop
*scop
)
834 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
839 isl_set_dump(scop
->context
);
840 isl_set_dump(scop
->context_value
);
841 for (i
= 0; i
< scop
->n_type
; ++i
)
842 pet_type_dump(scop
->types
[i
]);
843 for (i
= 0; i
< scop
->n_array
; ++i
)
844 pet_array_dump(scop
->arrays
[i
]);
845 for (i
= 0; i
< scop
->n_stmt
; ++i
)
846 pet_stmt_dump(scop
->stmts
[i
]);
847 for (i
= 0; i
< scop
->n_implication
; ++i
)
848 pet_implication_dump(scop
->implications
[i
]);
851 fprintf(stderr
, "skip\n");
852 isl_multi_pw_aff_dump(ext
->skip
[0]);
853 isl_multi_pw_aff_dump(ext
->skip
[1]);
857 /* Return 1 if the two pet_arrays are equivalent.
859 * We don't compare element_size as this may be target dependent.
861 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
863 if (!array1
|| !array2
)
866 if (!isl_set_is_equal(array1
->context
, array2
->context
))
868 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
870 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
872 if (array1
->value_bounds
&&
873 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
875 if (strcmp(array1
->element_type
, array2
->element_type
))
877 if (array1
->element_is_record
!= array2
->element_is_record
)
879 if (array1
->live_out
!= array2
->live_out
)
881 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
883 if (array1
->declared
!= array2
->declared
)
885 if (array1
->exposed
!= array2
->exposed
)
891 /* Return 1 if the two pet_stmts are equivalent.
893 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
897 if (!stmt1
|| !stmt2
)
900 if (stmt1
->line
!= stmt2
->line
)
902 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
904 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
906 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
908 if (stmt1
->n_arg
!= stmt2
->n_arg
)
910 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
911 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
918 /* Return 1 if the two pet_types are equivalent.
920 * We only compare the names of the types since the exact representation
921 * of the definition may depend on the version of clang being used.
923 int pet_type_is_equal(struct pet_type
*type1
, struct pet_type
*type2
)
925 if (!type1
|| !type2
)
928 if (strcmp(type1
->name
, type2
->name
))
934 /* Return 1 if the two pet_implications are equivalent.
936 int pet_implication_is_equal(struct pet_implication
*implication1
,
937 struct pet_implication
*implication2
)
939 if (!implication1
|| !implication2
)
942 if (implication1
->satisfied
!= implication2
->satisfied
)
944 if (!isl_map_is_equal(implication1
->extension
, implication2
->extension
))
950 /* Return 1 if the two pet_scops are equivalent.
952 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
956 if (!scop1
|| !scop2
)
959 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
961 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
964 if (scop1
->n_type
!= scop2
->n_type
)
966 for (i
= 0; i
< scop1
->n_type
; ++i
)
967 if (!pet_type_is_equal(scop1
->types
[i
], scop2
->types
[i
]))
970 if (scop1
->n_array
!= scop2
->n_array
)
972 for (i
= 0; i
< scop1
->n_array
; ++i
)
973 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
976 if (scop1
->n_stmt
!= scop2
->n_stmt
)
978 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
979 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
982 if (scop1
->n_implication
!= scop2
->n_implication
)
984 for (i
= 0; i
< scop1
->n_implication
; ++i
)
985 if (!pet_implication_is_equal(scop1
->implications
[i
],
986 scop2
->implications
[i
]))
992 /* Prefix the schedule of "stmt" with an extra dimension with constant
995 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1000 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1001 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1002 if (!stmt
->schedule
)
1003 return pet_stmt_free(stmt
);
1008 /* Prefix the schedules of all statements in "scop" with an extra
1009 * dimension with constant value "pos".
1011 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1018 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1019 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1020 if (!scop
->stmts
[i
])
1021 return pet_scop_free(scop
);
1027 /* Given a set with a parameter at "param_pos" that refers to the
1028 * iterator, "move" the iterator to the first set dimension.
1029 * That is, essentially equate the parameter to the first set dimension
1030 * and then project it out.
1032 * The first set dimension may however refer to a virtual iterator,
1033 * while the parameter refers to the "real" iterator.
1034 * We therefore need to take into account the affine expression "iv_map", which
1035 * expresses the real iterator in terms of the virtual iterator.
1036 * In particular, we equate the set dimension to the input of the map
1037 * and the parameter to the output of the map and then project out
1038 * everything we don't need anymore.
1040 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1041 int param_pos
, __isl_take isl_aff
*iv_map
)
1043 isl_map
*map
, *map2
;
1044 map
= isl_map_from_domain(set
);
1045 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1046 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1047 map2
= isl_map_from_aff(iv_map
);
1048 map2
= isl_map_align_params(map2
, isl_map_get_space(map
));
1049 map
= isl_map_apply_range(map
, map2
);
1050 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1051 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1052 return isl_map_domain(map
);
1055 /* Data used in embed_access.
1056 * extend adds an iterator to the iteration domain (through precomposition).
1057 * iv_map expresses the real iterator in terms of the virtual iterator
1058 * var_id represents the induction variable of the corresponding loop
1060 struct pet_embed_access
{
1061 isl_multi_pw_aff
*extend
;
1066 /* Given an index expression, return an expression for the outer iterator.
1068 static __isl_give isl_aff
*index_outer_iterator(
1069 __isl_take isl_multi_pw_aff
*index
)
1072 isl_local_space
*ls
;
1074 space
= isl_multi_pw_aff_get_domain_space(index
);
1075 isl_multi_pw_aff_free(index
);
1077 ls
= isl_local_space_from_space(space
);
1078 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1081 /* Replace an index expression that references the new (outer) iterator variable
1082 * by one that references the corresponding (real) iterator.
1084 * The input index expression is of the form
1086 * { S[i',...] -> i[] }
1088 * where i' refers to the virtual iterator.
1090 * iv_map is of the form
1094 * Return the index expression
1096 * { S[i',...] -> [i] }
1098 static __isl_give isl_multi_pw_aff
*replace_by_iterator(
1099 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_aff
*iv_map
)
1104 aff
= index_outer_iterator(index
);
1105 space
= isl_aff_get_space(aff
);
1106 iv_map
= isl_aff_align_params(iv_map
, space
);
1107 aff
= isl_aff_pullback_aff(iv_map
, aff
);
1109 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1112 /* Given an index expression "index" that refers to the (real) iterator
1113 * through the parameter at position "pos", plug in "iv_map", expressing
1114 * the real iterator in terms of the virtual (outer) iterator.
1116 * In particular, the index expression is of the form
1118 * [..., i, ...] -> { S[i',...] -> ... i ... }
1120 * where i refers to the real iterator and i' refers to the virtual iterator.
1122 * iv_map is of the form
1126 * Return the index expression
1128 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1131 * We first move the parameter to the input
1133 * [..., ...] -> { [i, i',...] -> ... i ... }
1137 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1139 * and then combine the two to obtain the desired result.
1141 static __isl_give isl_multi_pw_aff
*index_internalize_iv(
1142 __isl_take isl_multi_pw_aff
*index
, int pos
, __isl_take isl_aff
*iv_map
)
1144 isl_space
*space
= isl_multi_pw_aff_get_domain_space(index
);
1147 space
= isl_space_drop_dims(space
, isl_dim_param
, pos
, 1);
1148 index
= isl_multi_pw_aff_move_dims(index
, isl_dim_in
, 0,
1149 isl_dim_param
, pos
, 1);
1151 space
= isl_space_map_from_set(space
);
1152 ma
= isl_multi_aff_identity(isl_space_copy(space
));
1153 iv_map
= isl_aff_align_params(iv_map
, space
);
1154 iv_map
= isl_aff_pullback_aff(iv_map
, isl_multi_aff_get_aff(ma
, 0));
1155 ma
= isl_multi_aff_flat_range_product(
1156 isl_multi_aff_from_aff(iv_map
), ma
);
1157 index
= isl_multi_pw_aff_pullback_multi_aff(index
, ma
);
1162 /* Does the index expression "index" reference a virtual array, i.e.,
1163 * one with user pointer equal to NULL?
1164 * A virtual array does not have any members.
1166 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff
*index
)
1171 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
1173 if (isl_multi_pw_aff_range_is_wrapping(index
))
1175 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1176 is_virtual
= !isl_id_get_user(id
);
1182 /* Does the access relation "access" reference a virtual array, i.e.,
1183 * one with user pointer equal to NULL?
1184 * A virtual array does not have any members.
1186 static int access_is_virtual_array(__isl_keep isl_map
*access
)
1191 if (!isl_map_has_tuple_id(access
, isl_dim_out
))
1193 if (isl_map_range_is_wrapping(access
))
1195 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1196 is_virtual
= !isl_id_get_user(id
);
1202 /* Embed the given index expression in an extra outer loop.
1203 * The domain of the index expression has already been updated.
1205 * If the access refers to the induction variable, then it is
1206 * turned into an access to the set of integers with index (and value)
1207 * equal to the induction variable.
1209 * If the accessed array is a virtual array (with user
1210 * pointer equal to NULL), as created by create_test_index,
1211 * then it is extended along with the domain of the index expression.
1213 static __isl_give isl_multi_pw_aff
*embed_index_expression(
1214 __isl_take isl_multi_pw_aff
*index
, struct pet_embed_access
*data
)
1216 isl_id
*array_id
= NULL
;
1219 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
1220 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1221 if (array_id
== data
->var_id
) {
1222 index
= replace_by_iterator(index
, isl_aff_copy(data
->iv_map
));
1223 } else if (index_is_virtual_array(index
)) {
1225 isl_multi_pw_aff
*mpa
;
1227 aff
= index_outer_iterator(isl_multi_pw_aff_copy(index
));
1228 mpa
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1229 index
= isl_multi_pw_aff_flat_range_product(mpa
, index
);
1230 index
= isl_multi_pw_aff_set_tuple_id(index
, isl_dim_out
,
1231 isl_id_copy(array_id
));
1233 isl_id_free(array_id
);
1235 pos
= isl_multi_pw_aff_find_dim_by_id(index
,
1236 isl_dim_param
, data
->var_id
);
1238 index
= index_internalize_iv(index
, pos
,
1239 isl_aff_copy(data
->iv_map
));
1240 index
= isl_multi_pw_aff_set_dim_id(index
, isl_dim_in
, 0,
1241 isl_id_copy(data
->var_id
));
1246 /* Embed the given access relation in an extra outer loop.
1247 * The domain of the access relation has already been updated.
1249 * If the access refers to the induction variable, then it is
1250 * turned into an access to the set of integers with index (and value)
1251 * equal to the induction variable.
1253 * If the induction variable appears in the constraints (as a parameter),
1254 * then the parameter is equated to the newly introduced iteration
1255 * domain dimension and subsequently projected out.
1257 * Similarly, if the accessed array is a virtual array (with user
1258 * pointer equal to NULL), as created by create_test_index,
1259 * then it is extended along with the domain of the access.
1261 static __isl_give isl_map
*embed_access_relation(__isl_take isl_map
*access
,
1262 struct pet_embed_access
*data
)
1264 isl_id
*array_id
= NULL
;
1267 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1268 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1269 if (array_id
== data
->var_id
|| access_is_virtual_array(access
)) {
1270 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1271 access
= isl_map_equate(access
,
1272 isl_dim_in
, 0, isl_dim_out
, 0);
1273 if (array_id
== data
->var_id
)
1274 access
= isl_map_apply_range(access
,
1275 isl_map_from_aff(isl_aff_copy(data
->iv_map
)));
1277 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1278 isl_id_copy(array_id
));
1280 isl_id_free(array_id
);
1282 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1284 isl_set
*set
= isl_map_wrap(access
);
1285 set
= internalize_iv(set
, pos
, isl_aff_copy(data
->iv_map
));
1286 access
= isl_set_unwrap(set
);
1288 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1289 isl_id_copy(data
->var_id
));
1294 /* Given an access expression, embed the associated access relation and
1295 * index expression in an extra outer loop.
1297 * We first update the domains to insert the extra dimension and
1298 * then update the access relation and index expression to take
1299 * into account the mapping "iv_map" from virtual iterator
1302 static struct pet_expr
*embed_access(struct pet_expr
*expr
, void *user
)
1304 struct pet_embed_access
*data
= user
;
1306 expr
= pet_expr_access_update_domain(expr
, data
->extend
);
1310 expr
->acc
.access
= embed_access_relation(expr
->acc
.access
, data
);
1311 expr
->acc
.index
= embed_index_expression(expr
->acc
.index
, data
);
1312 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1313 return pet_expr_free(expr
);
1318 /* Embed all access subexpressions of "expr" in an extra loop.
1319 * "extend" inserts an outer loop iterator in the iteration domains
1320 * (through precomposition).
1321 * "iv_map" expresses the real iterator in terms of the virtual iterator
1322 * "var_id" represents the induction variable.
1324 static struct pet_expr
*expr_embed(struct pet_expr
*expr
,
1325 __isl_take isl_multi_pw_aff
*extend
, __isl_take isl_aff
*iv_map
,
1326 __isl_keep isl_id
*var_id
)
1328 struct pet_embed_access data
=
1329 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1331 expr
= pet_expr_map_access(expr
, &embed_access
, &data
);
1332 isl_aff_free(iv_map
);
1333 isl_multi_pw_aff_free(extend
);
1337 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1338 * "dom" and schedule "sched". "var_id" represents the induction variable
1339 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1340 * That is, it expresses the iterator that some of the parameters in "stmt"
1341 * may refer to in terms of the iterator used in "dom" and
1342 * the domain of "sched".
1344 * The iteration domain and schedule of the statement are updated
1345 * according to the iteration domain and schedule of the new loop.
1346 * If stmt->domain is a wrapped map, then the iteration domain
1347 * is the domain of this map, so we need to be careful to adjust
1350 * If the induction variable appears in the constraints (as a parameter)
1351 * of the current iteration domain or the schedule of the statement,
1352 * then the parameter is equated to the newly introduced iteration
1353 * domain dimension and subsequently projected out.
1355 * Finally, all access relations are updated based on the extra loop.
1357 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1358 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1359 __isl_take isl_aff
*iv_map
, __isl_take isl_id
*var_id
)
1365 isl_multi_pw_aff
*extend
;
1370 if (isl_set_is_wrapping(stmt
->domain
)) {
1375 map
= isl_set_unwrap(stmt
->domain
);
1376 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1377 ran_dim
= isl_space_range(isl_map_get_space(map
));
1378 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1379 isl_set_universe(ran_dim
));
1380 map
= isl_map_flat_domain_product(ext
, map
);
1381 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1382 isl_id_copy(stmt_id
));
1383 dim
= isl_space_domain(isl_map_get_space(map
));
1384 stmt
->domain
= isl_map_wrap(map
);
1386 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1387 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1389 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1390 isl_id_copy(stmt_id
));
1391 dim
= isl_set_get_space(stmt
->domain
);
1394 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1396 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1397 isl_aff_copy(iv_map
));
1399 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1400 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1401 isl_dim_in
, stmt_id
);
1403 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1405 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1406 set
= internalize_iv(set
, pos
, isl_aff_copy(iv_map
));
1407 stmt
->schedule
= isl_set_unwrap(set
);
1410 dim
= isl_space_map_from_set(dim
);
1411 extend
= isl_multi_pw_aff_identity(dim
);
1412 extend
= isl_multi_pw_aff_drop_dims(extend
, isl_dim_out
, 0, 1);
1413 extend
= isl_multi_pw_aff_set_tuple_id(extend
, isl_dim_out
,
1414 isl_multi_pw_aff_get_tuple_id(extend
, isl_dim_in
));
1415 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1416 stmt
->args
[i
] = expr_embed(stmt
->args
[i
],
1417 isl_multi_pw_aff_copy(extend
),
1418 isl_aff_copy(iv_map
), var_id
);
1419 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1422 isl_id_free(var_id
);
1424 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1426 return pet_stmt_free(stmt
);
1427 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1428 return pet_stmt_free(stmt
);
1432 isl_map_free(sched
);
1433 isl_aff_free(iv_map
);
1434 isl_id_free(var_id
);
1438 /* Embed the given pet_array in an extra outer loop with iteration domain
1440 * This embedding only has an effect on virtual arrays (those with
1441 * user pointer equal to NULL), which need to be extended along with
1442 * the iteration domain.
1444 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1445 __isl_take isl_set
*dom
)
1447 isl_id
*array_id
= NULL
;
1452 if (isl_set_has_tuple_id(array
->extent
))
1453 array_id
= isl_set_get_tuple_id(array
->extent
);
1455 if (array_id
&& !isl_id_get_user(array_id
)) {
1456 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1457 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1459 return pet_array_free(array
);
1462 isl_id_free(array_id
);
1471 /* Project out all unnamed parameters from "set" and return the result.
1473 static __isl_give isl_set
*set_project_out_unnamed_params(
1474 __isl_take isl_set
*set
)
1478 n
= isl_set_dim(set
, isl_dim_param
);
1479 for (i
= n
- 1; i
>= 0; --i
) {
1480 if (isl_set_has_dim_name(set
, isl_dim_param
, i
))
1482 set
= isl_set_project_out(set
, isl_dim_param
, i
, 1);
1488 /* Update the context with respect to an embedding into a loop
1489 * with iteration domain "dom" and induction variable "id".
1490 * "iv_map" expresses the real iterator (parameter "id") in terms
1491 * of a possibly virtual iterator (used in "dom").
1493 * If the current context is independent of "id", we don't need
1495 * Otherwise, a parameter value is invalid for the embedding if
1496 * any of the corresponding iterator values is invalid.
1497 * That is, a parameter value is valid only if all the corresponding
1498 * iterator values are valid.
1499 * We therefore compute the set of parameters
1501 * forall i in dom : valid (i)
1505 * not exists i in dom : not valid(i)
1509 * not exists i in dom \ valid(i)
1511 * Before we subtract valid(i) from dom, we first need to substitute
1512 * the real iterator for the virtual iterator.
1514 * If there are any unnamed parameters in "dom", then we consider
1515 * a parameter value to be valid if it is valid for any value of those
1516 * unnamed parameters. They are therefore projected out at the end.
1518 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1519 __isl_keep isl_set
*dom
, __isl_keep isl_aff
*iv_map
,
1520 __isl_keep isl_id
*id
)
1525 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1529 context
= isl_set_from_params(context
);
1530 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1531 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1532 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1533 ma
= isl_multi_aff_from_aff(isl_aff_copy(iv_map
));
1534 context
= isl_set_preimage_multi_aff(context
, ma
);
1535 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1536 context
= isl_set_params(context
);
1537 context
= isl_set_complement(context
);
1538 context
= set_project_out_unnamed_params(context
);
1542 /* Update the implication with respect to an embedding into a loop
1543 * with iteration domain "dom".
1545 * Since embed_access extends virtual arrays along with the domain
1546 * of the access, we need to do the same with domain and range
1547 * of the implication. Since the original implication is only valid
1548 * within a given iteration of the loop, the extended implication
1549 * maps the extra array dimension corresponding to the extra loop
1552 static struct pet_implication
*pet_implication_embed(
1553 struct pet_implication
*implication
, __isl_take isl_set
*dom
)
1561 map
= isl_set_identity(dom
);
1562 id
= isl_map_get_tuple_id(implication
->extension
, isl_dim_in
);
1563 map
= isl_map_flat_product(map
, implication
->extension
);
1564 map
= isl_map_set_tuple_id(map
, isl_dim_in
, isl_id_copy(id
));
1565 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
1566 implication
->extension
= map
;
1567 if (!implication
->extension
)
1568 return pet_implication_free(implication
);
1576 /* Embed all statements and arrays in "scop" in an extra outer loop
1577 * with iteration domain "dom" and schedule "sched".
1578 * "id" represents the induction variable of the loop.
1579 * "iv_map" maps a possibly virtual iterator to the real iterator.
1580 * That is, it expresses the iterator that some of the parameters in "scop"
1581 * may refer to in terms of the iterator used in "dom" and
1582 * the domain of "sched".
1584 * Any skip conditions within the loop have no effect outside of the loop.
1585 * The caller is responsible for making sure skip[pet_skip_later] has been
1586 * taken into account.
1588 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1589 __isl_take isl_aff
*sched
, __isl_take isl_aff
*iv_map
,
1590 __isl_take isl_id
*id
)
1595 sched_map
= isl_map_from_aff(sched
);
1600 pet_scop_reset_skip(scop
, pet_skip_now
);
1601 pet_scop_reset_skip(scop
, pet_skip_later
);
1603 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1607 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1608 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1609 isl_set_copy(dom
), isl_map_copy(sched_map
),
1610 isl_aff_copy(iv_map
), isl_id_copy(id
));
1611 if (!scop
->stmts
[i
])
1615 for (i
= 0; i
< scop
->n_array
; ++i
) {
1616 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1618 if (!scop
->arrays
[i
])
1622 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1623 scop
->implications
[i
] =
1624 pet_implication_embed(scop
->implications
[i
],
1626 if (!scop
->implications
[i
])
1631 isl_map_free(sched_map
);
1632 isl_aff_free(iv_map
);
1637 isl_map_free(sched_map
);
1638 isl_aff_free(iv_map
);
1640 return pet_scop_free(scop
);
1643 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1645 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1646 __isl_take isl_set
*cond
)
1651 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1656 return pet_stmt_free(stmt
);
1659 /* Add extra conditions to scop->skip[type].
1661 * The new skip condition only holds if it held before
1662 * and the condition is true. It does not hold if it did not hold
1663 * before or the condition is false.
1665 * The skip condition is assumed to be an affine expression.
1667 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1668 enum pet_skip type
, __isl_keep isl_set
*cond
)
1670 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1676 if (!ext
->skip
[type
])
1679 if (!multi_pw_aff_is_affine(ext
->skip
[type
]))
1680 isl_die(isl_multi_pw_aff_get_ctx(ext
->skip
[type
]),
1681 isl_error_internal
, "can only restrict affine skips",
1682 return pet_scop_free(scop
));
1684 skip
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
1685 dom
= isl_pw_aff_domain(isl_pw_aff_copy(skip
));
1686 cond
= isl_set_copy(cond
);
1687 cond
= isl_set_from_params(cond
);
1688 cond
= isl_set_intersect(cond
, isl_pw_aff_non_zero_set(skip
));
1689 skip
= indicator_function(cond
, dom
);
1690 isl_multi_pw_aff_free(ext
->skip
[type
]);
1691 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
1692 if (!ext
->skip
[type
])
1693 return pet_scop_free(scop
);
1698 /* Add extra conditions on the parameters to all iteration domains
1699 * and skip conditions.
1701 * A parameter value is valid for the result if it was valid
1702 * for the original scop and satisfies "cond" or if it does
1703 * not satisfy "cond" as in this case the scop is not executed
1704 * and the original constraints on the parameters are irrelevant.
1706 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1707 __isl_take isl_set
*cond
)
1711 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1712 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1717 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1718 scop
->context
= isl_set_union(scop
->context
,
1719 isl_set_complement(isl_set_copy(cond
)));
1720 scop
->context
= isl_set_coalesce(scop
->context
);
1721 scop
->context
= set_project_out_unnamed_params(scop
->context
);
1725 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1726 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1727 isl_set_copy(cond
));
1728 if (!scop
->stmts
[i
])
1736 return pet_scop_free(scop
);
1739 /* Insert an argument expression corresponding to "test" in front
1740 * of the list of arguments described by *n_arg and *args.
1742 static int args_insert_access(unsigned *n_arg
, struct pet_expr
***args
,
1743 __isl_keep isl_multi_pw_aff
*test
)
1746 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1752 *args
= isl_calloc_array(ctx
, struct pet_expr
*, 1);
1756 struct pet_expr
**ext
;
1757 ext
= isl_calloc_array(ctx
, struct pet_expr
*, 1 + *n_arg
);
1760 for (i
= 0; i
< *n_arg
; ++i
)
1761 ext
[1 + i
] = (*args
)[i
];
1766 (*args
)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1773 /* Look through the applications in "scop" for any that can be
1774 * applied to the filter expressed by "map" and "satisified".
1775 * If there is any, then apply it to "map" and return the result.
1776 * Otherwise, return "map".
1777 * "id" is the identifier of the virtual array.
1779 * We only introduce at most one implication for any given virtual array,
1780 * so we can apply the implication and return as soon as we find one.
1782 static __isl_give isl_map
*apply_implications(struct pet_scop
*scop
,
1783 __isl_take isl_map
*map
, __isl_keep isl_id
*id
, int satisfied
)
1787 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1788 struct pet_implication
*pi
= scop
->implications
[i
];
1791 if (pi
->satisfied
!= satisfied
)
1793 pi_id
= isl_map_get_tuple_id(pi
->extension
, isl_dim_in
);
1798 return isl_map_apply_range(map
, isl_map_copy(pi
->extension
));
1804 /* Is the filter expressed by "test" and "satisfied" implied
1805 * by filter "pos" on "domain", with filter "expr", taking into
1806 * account the implications of "scop"?
1808 * For filter on domain implying that expressed by "test" and "satisfied",
1809 * the filter needs to be an access to the same (virtual) array as "test" and
1810 * the filter value needs to be equal to "satisfied".
1811 * Moreover, the filter access relation, possibly extended by
1812 * the implications in "scop" needs to contain "test".
1814 static int implies_filter(struct pet_scop
*scop
,
1815 __isl_keep isl_map
*domain
, int pos
, struct pet_expr
*expr
,
1816 __isl_keep isl_map
*test
, int satisfied
)
1818 isl_id
*test_id
, *arg_id
;
1825 if (expr
->type
!= pet_expr_access
)
1827 test_id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1828 arg_id
= pet_expr_access_get_id(expr
);
1829 isl_id_free(arg_id
);
1830 isl_id_free(test_id
);
1831 if (test_id
!= arg_id
)
1833 val
= isl_map_plain_get_val_if_fixed(domain
, isl_dim_out
, pos
);
1834 is_int
= isl_val_is_int(val
);
1836 s
= isl_val_get_num_si(val
);
1845 implied
= isl_map_copy(expr
->acc
.access
);
1846 implied
= apply_implications(scop
, implied
, test_id
, satisfied
);
1847 is_subset
= isl_map_is_subset(test
, implied
);
1848 isl_map_free(implied
);
1853 /* Is the filter expressed by "test" and "satisfied" implied
1854 * by any of the filters on the domain of "stmt", taking into
1855 * account the implications of "scop"?
1857 static int filter_implied(struct pet_scop
*scop
,
1858 struct pet_stmt
*stmt
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
1866 if (!scop
|| !stmt
|| !test
)
1868 if (scop
->n_implication
== 0)
1870 if (stmt
->n_arg
== 0)
1873 domain
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
1874 test_map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test
));
1877 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
1878 implied
= implies_filter(scop
, domain
, i
, stmt
->args
[i
],
1879 test_map
, satisfied
);
1880 if (implied
< 0 || implied
)
1884 isl_map_free(test_map
);
1885 isl_map_free(domain
);
1889 /* Make the statement "stmt" depend on the value of "test"
1890 * being equal to "satisfied" by adjusting stmt->domain.
1892 * The domain of "test" corresponds to the (zero or more) outer dimensions
1893 * of the iteration domain.
1895 * We first extend "test" to apply to the entire iteration domain and
1896 * then check if the filter that we are about to add is implied
1897 * by any of the current filters, possibly taking into account
1898 * the implications in "scop". If so, we leave "stmt" untouched and return.
1900 * Otherwise, we insert an argument corresponding to a read to "test"
1901 * from the iteration domain of "stmt" in front of the list of arguments.
1902 * We also insert a corresponding output dimension in the wrapped
1903 * map contained in stmt->domain, with value set to "satisfied".
1905 static struct pet_stmt
*stmt_filter(struct pet_scop
*scop
,
1906 struct pet_stmt
*stmt
, __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1912 isl_pw_multi_aff
*pma
;
1913 isl_multi_aff
*add_dom
;
1915 isl_local_space
*ls
;
1921 space
= pet_stmt_get_space(stmt
);
1922 n_test_dom
= isl_multi_pw_aff_dim(test
, isl_dim_in
);
1923 space
= isl_space_from_domain(space
);
1924 space
= isl_space_add_dims(space
, isl_dim_out
, n_test_dom
);
1925 add_dom
= isl_multi_aff_zero(isl_space_copy(space
));
1926 ls
= isl_local_space_from_space(isl_space_domain(space
));
1927 for (i
= 0; i
< n_test_dom
; ++i
) {
1929 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1931 add_dom
= isl_multi_aff_set_aff(add_dom
, i
, aff
);
1933 isl_local_space_free(ls
);
1934 test
= isl_multi_pw_aff_pullback_multi_aff(test
, add_dom
);
1936 implied
= filter_implied(scop
, stmt
, test
, satisfied
);
1940 isl_multi_pw_aff_free(test
);
1944 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1945 pma
= pet_filter_insert_pma(isl_set_get_space(stmt
->domain
),
1947 stmt
->domain
= isl_set_preimage_pw_multi_aff(stmt
->domain
, pma
);
1949 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1952 isl_multi_pw_aff_free(test
);
1955 isl_multi_pw_aff_free(test
);
1956 return pet_stmt_free(stmt
);
1959 /* Does "scop" have a skip condition of the given "type"?
1961 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
1963 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1967 return ext
->skip
[type
] != NULL
;
1970 /* Does "scop" have a skip condition of the given "type" that
1971 * is an affine expression?
1973 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
1975 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1979 if (!ext
->skip
[type
])
1981 return multi_pw_aff_is_affine(ext
->skip
[type
]);
1984 /* Does "scop" have a skip condition of the given "type" that
1985 * is not an affine expression?
1987 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
1989 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1994 if (!ext
->skip
[type
])
1996 aff
= multi_pw_aff_is_affine(ext
->skip
[type
]);
2002 /* Does "scop" have a skip condition of the given "type" that
2003 * is affine and holds on the entire domain?
2005 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
2007 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2013 is_aff
= pet_scop_has_affine_skip(scop
, type
);
2014 if (is_aff
< 0 || !is_aff
)
2017 pa
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
2018 set
= isl_pw_aff_non_zero_set(pa
);
2019 is_univ
= isl_set_plain_is_universe(set
);
2025 /* Replace scop->skip[type] by "skip".
2027 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
2028 enum pet_skip type
, __isl_take isl_multi_pw_aff
*skip
)
2030 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2035 isl_multi_pw_aff_free(ext
->skip
[type
]);
2036 ext
->skip
[type
] = skip
;
2040 isl_multi_pw_aff_free(skip
);
2041 return pet_scop_free(scop
);
2044 /* Return a copy of scop->skip[type].
2046 __isl_give isl_multi_pw_aff
*pet_scop_get_skip(struct pet_scop
*scop
,
2049 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2054 return isl_multi_pw_aff_copy(ext
->skip
[type
]);
2057 /* Assuming scop->skip[type] is an affine expression,
2058 * return the constraints on the parameters for which the skip condition
2061 __isl_give isl_set
*pet_scop_get_affine_skip_domain(struct pet_scop
*scop
,
2064 isl_multi_pw_aff
*skip
;
2067 skip
= pet_scop_get_skip(scop
, type
);
2068 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
2069 isl_multi_pw_aff_free(skip
);
2070 return isl_set_params(isl_pw_aff_non_zero_set(pa
));
2073 /* Return the identifier of the variable that is accessed by
2074 * the skip condition of the given type.
2076 * The skip condition is assumed not to be an affine condition.
2078 __isl_give isl_id
*pet_scop_get_skip_id(struct pet_scop
*scop
,
2081 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2086 return isl_multi_pw_aff_get_tuple_id(ext
->skip
[type
], isl_dim_out
);
2089 /* Return an access pet_expr corresponding to the skip condition
2090 * of the given type.
2092 struct pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
2095 return pet_expr_from_index(pet_scop_get_skip(scop
, type
));
2098 /* Drop the the skip condition scop->skip[type].
2100 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
2102 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2107 isl_multi_pw_aff_free(ext
->skip
[type
]);
2108 ext
->skip
[type
] = NULL
;
2111 /* Make the skip condition (if any) depend on the value of "test" being
2112 * equal to "satisfied".
2114 * We only support the case where the original skip condition is universal,
2115 * i.e., where skipping is unconditional, and where satisfied == 1.
2116 * In this case, the skip condition is changed to skip only when
2117 * "test" is equal to one.
2119 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
2120 enum pet_skip type
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
2126 if (!pet_scop_has_skip(scop
, type
))
2130 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2132 return pet_scop_free(scop
);
2133 if (satisfied
&& is_univ
) {
2134 isl_multi_pw_aff
*skip
;
2135 skip
= isl_multi_pw_aff_copy(test
);
2136 scop
= pet_scop_set_skip(scop
, type
, skip
);
2140 isl_die(isl_multi_pw_aff_get_ctx(test
), isl_error_internal
,
2141 "skip expression cannot be filtered",
2142 return pet_scop_free(scop
));
2148 /* Make all statements in "scop" depend on the value of "test"
2149 * being equal to "satisfied" by adjusting their domains.
2151 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2152 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2156 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
2157 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
2162 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2163 scop
->stmts
[i
] = stmt_filter(scop
, scop
->stmts
[i
],
2164 isl_multi_pw_aff_copy(test
), satisfied
);
2165 if (!scop
->stmts
[i
])
2169 isl_multi_pw_aff_free(test
);
2172 isl_multi_pw_aff_free(test
);
2173 return pet_scop_free(scop
);
2176 /* Add all parameters in "expr" to "space" and return the result.
2178 static __isl_give isl_space
*expr_collect_params(struct pet_expr
*expr
,
2179 __isl_take isl_space
*space
)
2185 for (i
= 0; i
< expr
->n_arg
; ++i
)
2186 space
= expr_collect_params(expr
->args
[i
], space
);
2188 if (expr
->type
== pet_expr_access
)
2189 space
= isl_space_align_params(space
,
2190 isl_map_get_space(expr
->acc
.access
));
2194 pet_expr_free(expr
);
2195 return isl_space_free(space
);
2198 /* Add all parameters in "stmt" to "space" and return the result.
2200 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
2201 __isl_take isl_space
*space
)
2206 return isl_space_free(space
);
2208 space
= isl_space_align_params(space
, isl_set_get_space(stmt
->domain
));
2209 space
= isl_space_align_params(space
,
2210 isl_map_get_space(stmt
->schedule
));
2211 for (i
= 0; i
< stmt
->n_arg
; ++i
)
2212 space
= expr_collect_params(stmt
->args
[i
], space
);
2213 space
= expr_collect_params(stmt
->body
, space
);
2218 /* Add all parameters in "array" to "space" and return the result.
2220 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
2221 __isl_take isl_space
*space
)
2224 return isl_space_free(space
);
2226 space
= isl_space_align_params(space
,
2227 isl_set_get_space(array
->context
));
2228 space
= isl_space_align_params(space
, isl_set_get_space(array
->extent
));
2233 /* Add all parameters in "scop" to "space" and return the result.
2235 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2236 __isl_take isl_space
*space
)
2241 return isl_space_free(space
);
2243 for (i
= 0; i
< scop
->n_array
; ++i
)
2244 space
= array_collect_params(scop
->arrays
[i
], space
);
2246 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2247 space
= stmt_collect_params(scop
->stmts
[i
], space
);
2252 /* Add all parameters in "space" to the domain, schedule and
2253 * all access relations in "stmt".
2255 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2256 __isl_take isl_space
*space
)
2263 stmt
->domain
= isl_set_align_params(stmt
->domain
,
2264 isl_space_copy(space
));
2265 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2266 isl_space_copy(space
));
2268 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2269 stmt
->args
[i
] = pet_expr_align_params(stmt
->args
[i
],
2270 isl_space_copy(space
));
2274 stmt
->body
= pet_expr_align_params(stmt
->body
, isl_space_copy(space
));
2276 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2279 isl_space_free(space
);
2282 isl_space_free(space
);
2283 return pet_stmt_free(stmt
);
2286 /* Add all parameters in "space" to "array".
2288 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2289 __isl_take isl_space
*space
)
2294 array
->context
= isl_set_align_params(array
->context
,
2295 isl_space_copy(space
));
2296 array
->extent
= isl_set_align_params(array
->extent
,
2297 isl_space_copy(space
));
2298 if (array
->value_bounds
) {
2299 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2300 isl_space_copy(space
));
2301 if (!array
->value_bounds
)
2305 if (!array
->context
|| !array
->extent
)
2308 isl_space_free(space
);
2311 isl_space_free(space
);
2312 return pet_array_free(array
);
2315 /* Add all parameters in "space" to "scop".
2317 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2318 __isl_take isl_space
*space
)
2325 for (i
= 0; i
< scop
->n_array
; ++i
) {
2326 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2327 isl_space_copy(space
));
2328 if (!scop
->arrays
[i
])
2332 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2333 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2334 isl_space_copy(space
));
2335 if (!scop
->stmts
[i
])
2339 isl_space_free(space
);
2342 isl_space_free(space
);
2343 return pet_scop_free(scop
);
2346 /* Update all isl_sets and isl_maps in "scop" such that they all
2347 * have the same parameters.
2349 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2356 space
= isl_set_get_space(scop
->context
);
2357 space
= scop_collect_params(scop
, space
);
2359 scop
->context
= isl_set_align_params(scop
->context
,
2360 isl_space_copy(space
));
2361 scop
= scop_propagate_params(scop
, space
);
2363 if (scop
&& !scop
->context
)
2364 return pet_scop_free(scop
);
2369 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2370 * in "space" by a value equal to the corresponding parameter.
2372 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
2373 __isl_take isl_space
*space
)
2378 stmt
->body
= pet_expr_detect_parameter_accesses(stmt
->body
,
2379 isl_space_copy(space
));
2381 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2384 isl_space_free(space
);
2387 isl_space_free(space
);
2388 return pet_stmt_free(stmt
);
2391 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2392 * in "space" by a value equal to the corresponding parameter.
2394 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
2395 __isl_take isl_space
*space
)
2402 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2403 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
2404 isl_space_copy(space
));
2405 if (!scop
->stmts
[i
])
2409 isl_space_free(space
);
2412 isl_space_free(space
);
2413 return pet_scop_free(scop
);
2416 /* Replace all accesses to (0D) arrays that correspond to any of
2417 * the parameters used in "scop" by a value equal
2418 * to the corresponding parameter.
2420 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
2427 space
= isl_set_get_space(scop
->context
);
2428 space
= scop_collect_params(scop
, space
);
2430 scop
= scop_detect_parameter_accesses(scop
, space
);
2435 /* Add the access relation of the access expression "expr" to "accesses" and
2436 * return the result.
2437 * The domain of the access relation is intersected with "domain".
2438 * If "tag" is set, then the access relation is tagged with
2439 * the corresponding reference identifier.
2441 static __isl_give isl_union_map
*expr_collect_access(struct pet_expr
*expr
,
2442 int tag
, __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
2446 access
= pet_expr_access_get_may_access(expr
);
2447 access
= isl_map_intersect_domain(access
, isl_set_copy(domain
));
2449 access
= pet_expr_tag_access(expr
, access
);
2450 return isl_union_map_add_map(accesses
, access
);
2453 /* Add all read access relations (if "read" is set) and/or all write
2454 * access relations (if "write" is set) to "accesses" and return the result.
2455 * The domains of the access relations are intersected with "domain".
2456 * If "tag" is set, then the access relations are tagged with
2457 * the corresponding reference identifiers.
2459 * If "must" is set, then we only add the accesses that are definitely
2460 * performed. Otherwise, we add all potential accesses.
2461 * In particular, if the access has any arguments, then if "must" is
2462 * set we currently skip the access completely. If "must" is not set,
2463 * we project out the values of the access arguments.
2465 static __isl_give isl_union_map
*expr_collect_accesses(struct pet_expr
*expr
,
2466 int read
, int write
, int must
, int tag
,
2467 __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
2474 return isl_union_map_free(accesses
);
2476 for (i
= 0; i
< expr
->n_arg
; ++i
)
2477 accesses
= expr_collect_accesses(expr
->args
[i
],
2478 read
, write
, must
, tag
, accesses
, domain
);
2480 if (expr
->type
== pet_expr_access
&& !pet_expr_is_affine(expr
) &&
2481 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)) &&
2482 (!must
|| expr
->n_arg
== 0)) {
2483 accesses
= expr_collect_access(expr
, tag
, accesses
, domain
);
2489 /* Collect and return all read access relations (if "read" is set)
2490 * and/or all write access relations (if "write" is set) in "stmt".
2491 * If "tag" is set, then the access relations are tagged with
2492 * the corresponding reference identifiers.
2493 * If "kill" is set, then "stmt" is a kill statement and we simply
2494 * add the argument of the kill operation.
2496 * If "must" is set, then we only add the accesses that are definitely
2497 * performed. Otherwise, we add all potential accesses.
2498 * In particular, if the statement has any arguments, then if "must" is
2499 * set we currently skip the statement completely. If "must" is not set,
2500 * we project out the values of the statement arguments.
2502 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2503 int read
, int write
, int kill
, int must
, int tag
,
2504 __isl_take isl_space
*dim
)
2506 isl_union_map
*accesses
;
2512 accesses
= isl_union_map_empty(dim
);
2514 if (must
&& stmt
->n_arg
> 0)
2517 domain
= isl_set_copy(stmt
->domain
);
2518 if (isl_set_is_wrapping(domain
))
2519 domain
= isl_map_domain(isl_set_unwrap(domain
));
2522 accesses
= expr_collect_access(stmt
->body
->args
[0], tag
,
2525 accesses
= expr_collect_accesses(stmt
->body
, read
, write
,
2526 must
, tag
, accesses
, domain
);
2527 isl_set_free(domain
);
2532 /* Is "stmt" an assignment statement?
2534 int pet_stmt_is_assign(struct pet_stmt
*stmt
)
2538 if (stmt
->body
->type
!= pet_expr_op
)
2540 return stmt
->body
->op
== pet_op_assign
;
2543 /* Is "stmt" a kill statement?
2545 int pet_stmt_is_kill(struct pet_stmt
*stmt
)
2549 if (stmt
->body
->type
!= pet_expr_op
)
2551 return stmt
->body
->op
== pet_op_kill
;
2554 /* Is "stmt" an assume statement?
2556 int pet_stmt_is_assume(struct pet_stmt
*stmt
)
2558 if (stmt
->body
->type
!= pet_expr_op
)
2560 return stmt
->body
->op
== pet_op_assume
;
2563 /* Compute a mapping from all arrays (of structs) in scop
2564 * to their innermost arrays.
2566 * In particular, for each array of a primitive type, the result
2567 * contains the identity mapping on that array.
2568 * For each array involving member accesses, the result
2569 * contains a mapping from the elements of any intermediate array of structs
2570 * to all corresponding elements of the innermost nested arrays.
2572 static __isl_give isl_union_map
*compute_to_inner(struct pet_scop
*scop
)
2575 isl_union_map
*to_inner
;
2577 to_inner
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2579 for (i
= 0; i
< scop
->n_array
; ++i
) {
2580 struct pet_array
*array
= scop
->arrays
[i
];
2582 isl_map
*map
, *gist
;
2584 if (array
->element_is_record
)
2587 map
= isl_set_identity(isl_set_copy(array
->extent
));
2589 set
= isl_map_domain(isl_map_copy(map
));
2590 gist
= isl_map_copy(map
);
2591 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2592 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2594 while (set
&& isl_set_is_wrapping(set
)) {
2598 id
= isl_set_get_tuple_id(set
);
2599 wrapped
= isl_set_unwrap(set
);
2600 wrapped
= isl_map_domain_map(wrapped
);
2601 wrapped
= isl_map_set_tuple_id(wrapped
, isl_dim_in
, id
);
2602 map
= isl_map_apply_domain(map
, wrapped
);
2603 set
= isl_map_domain(isl_map_copy(map
));
2604 gist
= isl_map_copy(map
);
2605 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2606 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2616 /* Collect and return all read access relations (if "read" is set)
2617 * and/or all write access relations (if "write" is set) in "scop".
2618 * If "kill" is set, then we only add the arguments of kill operations.
2619 * If "must" is set, then we only add the accesses that are definitely
2620 * performed. Otherwise, we add all potential accesses.
2621 * If "tag" is set, then the access relations are tagged with
2622 * the corresponding reference identifiers.
2623 * For accesses to structures, the returned access relation accesses
2624 * all individual fields in the structures.
2626 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2627 int read
, int write
, int kill
, int must
, int tag
)
2630 isl_union_map
*accesses
;
2631 isl_union_set
*arrays
;
2632 isl_union_map
*to_inner
;
2637 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2639 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2640 struct pet_stmt
*stmt
= scop
->stmts
[i
];
2641 isl_union_map
*accesses_i
;
2644 if (kill
&& !pet_stmt_is_kill(stmt
))
2647 space
= isl_set_get_space(scop
->context
);
2648 accesses_i
= stmt_collect_accesses(stmt
, read
, write
, kill
,
2650 accesses
= isl_union_map_union(accesses
, accesses_i
);
2653 arrays
= isl_union_set_empty(isl_union_map_get_space(accesses
));
2654 for (i
= 0; i
< scop
->n_array
; ++i
) {
2655 isl_set
*extent
= isl_set_copy(scop
->arrays
[i
]->extent
);
2656 arrays
= isl_union_set_add_set(arrays
, extent
);
2658 accesses
= isl_union_map_intersect_range(accesses
, arrays
);
2660 to_inner
= compute_to_inner(scop
);
2661 accesses
= isl_union_map_apply_range(accesses
, to_inner
);
2666 /* Collect all potential read access relations.
2668 __isl_give isl_union_map
*pet_scop_collect_may_reads(struct pet_scop
*scop
)
2670 return scop_collect_accesses(scop
, 1, 0, 0, 0, 0);
2673 /* Collect all potential write access relations.
2675 __isl_give isl_union_map
*pet_scop_collect_may_writes(struct pet_scop
*scop
)
2677 return scop_collect_accesses(scop
, 0, 1, 0, 0, 0);
2680 /* Collect all definite write access relations.
2682 __isl_give isl_union_map
*pet_scop_collect_must_writes(struct pet_scop
*scop
)
2684 return scop_collect_accesses(scop
, 0, 1, 0, 1, 0);
2687 /* Collect all definite kill access relations.
2689 __isl_give isl_union_map
*pet_scop_collect_must_kills(struct pet_scop
*scop
)
2691 return scop_collect_accesses(scop
, 0, 0, 1, 1, 0);
2694 /* Collect all tagged potential read access relations.
2696 __isl_give isl_union_map
*pet_scop_collect_tagged_may_reads(
2697 struct pet_scop
*scop
)
2699 return scop_collect_accesses(scop
, 1, 0, 0, 0, 1);
2702 /* Collect all tagged potential write access relations.
2704 __isl_give isl_union_map
*pet_scop_collect_tagged_may_writes(
2705 struct pet_scop
*scop
)
2707 return scop_collect_accesses(scop
, 0, 1, 0, 0, 1);
2710 /* Collect all tagged definite write access relations.
2712 __isl_give isl_union_map
*pet_scop_collect_tagged_must_writes(
2713 struct pet_scop
*scop
)
2715 return scop_collect_accesses(scop
, 0, 1, 0, 1, 1);
2718 /* Collect all tagged definite kill access relations.
2720 __isl_give isl_union_map
*pet_scop_collect_tagged_must_kills(
2721 struct pet_scop
*scop
)
2723 return scop_collect_accesses(scop
, 0, 0, 1, 1, 1);
2726 /* Collect and return the union of iteration domains in "scop".
2728 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2732 isl_union_set
*domain
;
2737 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2739 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2740 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2741 domain
= isl_union_set_add_set(domain
, domain_i
);
2747 /* Collect and return the schedules of the statements in "scop".
2748 * The range is normalized to the maximal number of scheduling
2751 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2754 isl_map
*schedule_i
;
2755 isl_union_map
*schedule
;
2756 int depth
, max_depth
= 0;
2761 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2763 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2764 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2765 if (depth
> max_depth
)
2769 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2770 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2771 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2772 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2774 for (j
= depth
; j
< max_depth
; ++j
)
2775 schedule_i
= isl_map_fix_si(schedule_i
,
2777 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2783 /* Does statement "stmt" write to "id"?
2785 static int stmt_writes(struct pet_stmt
*stmt
, __isl_keep isl_id
*id
)
2787 return pet_expr_writes(stmt
->body
, id
);
2790 /* Is there any write access in "scop" that accesses "id"?
2792 int pet_scop_writes(struct pet_scop
*scop
, __isl_keep isl_id
*id
)
2799 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2800 int writes
= stmt_writes(scop
->stmts
[i
], id
);
2801 if (writes
< 0 || writes
)
2808 /* Add a reference identifier to all access expressions in "stmt".
2809 * "n_ref" points to an integer that contains the sequence number
2810 * of the next reference.
2812 static struct pet_stmt
*stmt_add_ref_ids(struct pet_stmt
*stmt
, int *n_ref
)
2819 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2820 stmt
->args
[i
] = pet_expr_add_ref_ids(stmt
->args
[i
], n_ref
);
2822 return pet_stmt_free(stmt
);
2825 stmt
->body
= pet_expr_add_ref_ids(stmt
->body
, n_ref
);
2827 return pet_stmt_free(stmt
);
2832 /* Add a reference identifier to all access expressions in "scop".
2834 struct pet_scop
*pet_scop_add_ref_ids(struct pet_scop
*scop
)
2843 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2844 scop
->stmts
[i
] = stmt_add_ref_ids(scop
->stmts
[i
], &n_ref
);
2845 if (!scop
->stmts
[i
])
2846 return pet_scop_free(scop
);
2852 /* Reset the user pointer on all parameter ids in "array".
2854 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2859 array
->context
= isl_set_reset_user(array
->context
);
2860 array
->extent
= isl_set_reset_user(array
->extent
);
2861 if (!array
->context
|| !array
->extent
)
2862 return pet_array_free(array
);
2867 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2869 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2878 stmt
->domain
= isl_set_reset_user(stmt
->domain
);
2879 stmt
->schedule
= isl_map_reset_user(stmt
->schedule
);
2880 if (!stmt
->domain
|| !stmt
->schedule
)
2881 return pet_stmt_free(stmt
);
2883 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2884 stmt
->args
[i
] = pet_expr_anonymize(stmt
->args
[i
]);
2886 return pet_stmt_free(stmt
);
2889 stmt
->body
= pet_expr_anonymize(stmt
->body
);
2891 return pet_stmt_free(stmt
);
2896 /* Reset the user pointer on the tuple ids and all parameter ids
2899 static struct pet_implication
*implication_anonymize(
2900 struct pet_implication
*implication
)
2905 implication
->extension
= isl_map_reset_user(implication
->extension
);
2906 if (!implication
->extension
)
2907 return pet_implication_free(implication
);
2912 /* Reset the user pointer on all parameter and tuple ids in "scop".
2914 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2921 scop
->context
= isl_set_reset_user(scop
->context
);
2922 scop
->context_value
= isl_set_reset_user(scop
->context_value
);
2923 if (!scop
->context
|| !scop
->context_value
)
2924 return pet_scop_free(scop
);
2926 for (i
= 0; i
< scop
->n_array
; ++i
) {
2927 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2928 if (!scop
->arrays
[i
])
2929 return pet_scop_free(scop
);
2932 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2933 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2934 if (!scop
->stmts
[i
])
2935 return pet_scop_free(scop
);
2938 for (i
= 0; i
< scop
->n_implication
; ++i
) {
2939 scop
->implications
[i
] =
2940 implication_anonymize(scop
->implications
[i
]);
2941 if (!scop
->implications
[i
])
2942 return pet_scop_free(scop
);
2948 /* Compute the gist of the iteration domain and all access relations
2949 * of "stmt" based on the constraints on the parameters specified by "context"
2950 * and the constraints on the values of nested accesses specified
2951 * by "value_bounds".
2953 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2954 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2962 domain
= isl_set_copy(stmt
->domain
);
2963 if (stmt
->n_arg
> 0)
2964 domain
= isl_map_domain(isl_set_unwrap(domain
));
2966 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2968 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2969 stmt
->args
[i
] = pet_expr_gist(stmt
->args
[i
],
2970 domain
, value_bounds
);
2975 stmt
->body
= pet_expr_gist(stmt
->body
, domain
, value_bounds
);
2979 isl_set_free(domain
);
2981 domain
= isl_set_universe(pet_stmt_get_space(stmt
));
2982 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2983 if (stmt
->n_arg
> 0)
2984 domain
= pet_value_bounds_apply(domain
, stmt
->n_arg
, stmt
->args
,
2986 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
2988 return pet_stmt_free(stmt
);
2992 isl_set_free(domain
);
2993 return pet_stmt_free(stmt
);
2996 /* Compute the gist of the extent of the array
2997 * based on the constraints on the parameters specified by "context".
2999 static struct pet_array
*array_gist(struct pet_array
*array
,
3000 __isl_keep isl_set
*context
)
3005 array
->extent
= isl_set_gist_params(array
->extent
,
3006 isl_set_copy(context
));
3008 return pet_array_free(array
);
3013 /* Compute the gist of all sets and relations in "scop"
3014 * based on the constraints on the parameters specified by "scop->context"
3015 * and the constraints on the values of nested accesses specified
3016 * by "value_bounds".
3018 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
3019 __isl_keep isl_union_map
*value_bounds
)
3026 scop
->context
= isl_set_coalesce(scop
->context
);
3028 return pet_scop_free(scop
);
3030 for (i
= 0; i
< scop
->n_array
; ++i
) {
3031 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
3032 if (!scop
->arrays
[i
])
3033 return pet_scop_free(scop
);
3036 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3037 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
3039 if (!scop
->stmts
[i
])
3040 return pet_scop_free(scop
);
3046 /* Intersect the context of "scop" with "context".
3047 * To ensure that we don't introduce any unnamed parameters in
3048 * the context of "scop", we first remove the unnamed parameters
3051 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
3052 __isl_take isl_set
*context
)
3057 context
= set_project_out_unnamed_params(context
);
3058 scop
->context
= isl_set_intersect(scop
->context
, context
);
3060 return pet_scop_free(scop
);
3064 isl_set_free(context
);
3065 return pet_scop_free(scop
);
3068 /* Drop the current context of "scop". That is, replace the context
3069 * by a universal set.
3071 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
3078 space
= isl_set_get_space(scop
->context
);
3079 isl_set_free(scop
->context
);
3080 scop
->context
= isl_set_universe(space
);
3082 return pet_scop_free(scop
);
3087 /* Append "array" to the arrays of "scop".
3089 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
3090 struct pet_array
*array
)
3093 struct pet_array
**arrays
;
3095 if (!array
|| !scop
)
3098 ctx
= isl_set_get_ctx(scop
->context
);
3099 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3103 scop
->arrays
= arrays
;
3104 scop
->arrays
[scop
->n_array
] = array
;
3109 pet_array_free(array
);
3110 return pet_scop_free(scop
);
3113 /* Create and return an implication on filter values equal to "satisfied"
3114 * with extension "map".
3116 static struct pet_implication
*new_implication(__isl_take isl_map
*map
,
3120 struct pet_implication
*implication
;
3124 ctx
= isl_map_get_ctx(map
);
3125 implication
= isl_alloc_type(ctx
, struct pet_implication
);
3129 implication
->extension
= map
;
3130 implication
->satisfied
= satisfied
;
3138 /* Add an implication on filter values equal to "satisfied"
3139 * with extension "map" to "scop".
3141 struct pet_scop
*pet_scop_add_implication(struct pet_scop
*scop
,
3142 __isl_take isl_map
*map
, int satisfied
)
3145 struct pet_implication
*implication
;
3146 struct pet_implication
**implications
;
3148 implication
= new_implication(map
, satisfied
);
3149 if (!scop
|| !implication
)
3152 ctx
= isl_set_get_ctx(scop
->context
);
3153 implications
= isl_realloc_array(ctx
, scop
->implications
,
3154 struct pet_implication
*,
3155 scop
->n_implication
+ 1);
3158 scop
->implications
= implications
;
3159 scop
->implications
[scop
->n_implication
] = implication
;
3160 scop
->n_implication
++;
3164 pet_implication_free(implication
);
3165 return pet_scop_free(scop
);
3168 /* Given an access expression, check if it is data dependent.
3169 * If so, set *found and abort the search.
3171 static int is_data_dependent(struct pet_expr
*expr
, void *user
)
3183 /* Does "scop" contain any data dependent accesses?
3185 * Check the body of each statement for such accesses.
3187 int pet_scop_has_data_dependent_accesses(struct pet_scop
*scop
)
3195 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3196 int r
= pet_expr_foreach_access_expr(scop
->stmts
[i
]->body
,
3197 &is_data_dependent
, &found
);
3198 if (r
< 0 && !found
)
3207 /* Does "scop" contain and data dependent conditions?
3209 int pet_scop_has_data_dependent_conditions(struct pet_scop
*scop
)
3216 for (i
= 0; i
< scop
->n_stmt
; ++i
)
3217 if (scop
->stmts
[i
]->n_arg
> 0)
3223 /* Keep track of the "input" file inside the (extended) "scop".
3225 struct pet_scop
*pet_scop_set_input_file(struct pet_scop
*scop
, FILE *input
)
3227 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3237 /* Print the original code corresponding to "scop" to printer "p".
3239 * pet_scop_print_original can only be called from
3240 * a pet_transform_C_source callback. This means that the input
3241 * file is stored in the extended scop and that the printer prints
3244 __isl_give isl_printer
*pet_scop_print_original(struct pet_scop
*scop
,
3245 __isl_take isl_printer
*p
)
3247 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3251 return isl_printer_free(p
);
3254 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
3255 "no input file stored in scop",
3256 return isl_printer_free(p
));
3258 output
= isl_printer_get_file(p
);
3260 return isl_printer_free(p
);
3262 if (copy(ext
->input
, output
, scop
->start
, scop
->end
) < 0)
3263 return isl_printer_free(p
);