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>
45 #include "value_bounds.h"
47 /* pet_scop with extra information that is used during parsing and printing.
49 * In particular, we keep track of conditions under which we want
50 * to skip the rest of the current loop iteration (skip[pet_skip_now])
51 * and of conditions under which we want to skip subsequent
52 * loop iterations (skip[pet_skip_later]).
54 * The conditions are represented as index expressions defined
55 * over a zero-dimensional domain. The index expression is either
56 * a boolean affine expression or an access to a variable, which
57 * is assumed to attain values zero and one. The condition holds
58 * if the variable has value one or if the affine expression
59 * has value one (typically for only part of the parameter space).
61 * A missing condition (skip[type] == NULL) means that we don't want
64 * Additionally, we keep track of the original input file
65 * inside pet_transform_C_source.
70 isl_multi_pw_aff
*skip
[2];
74 /* Construct a pet_stmt with given location and statement
75 * number from a pet_expr.
76 * The initial iteration domain is the zero-dimensional universe.
77 * The name of the domain is given by "label" if it is non-NULL.
78 * Otherwise, the name is constructed as S_<id>.
79 * The domains of all access relations are modified to refer
80 * to the statement iteration domain.
82 struct pet_stmt
*pet_stmt_from_pet_expr(__isl_take pet_loc
*loc
,
83 __isl_take isl_id
*label
, int id
, __isl_take pet_expr
*expr
)
85 struct pet_stmt
*stmt
;
90 isl_multi_pw_aff
*add_name
;
96 ctx
= pet_expr_get_ctx(expr
);
97 stmt
= isl_calloc_type(ctx
, struct pet_stmt
);
101 dim
= isl_space_set_alloc(ctx
, 0, 0);
103 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, label
);
105 snprintf(name
, sizeof(name
), "S_%d", id
);
106 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, name
);
108 dom
= isl_set_universe(isl_space_copy(dim
));
109 sched
= isl_map_from_domain(isl_set_copy(dom
));
111 dim
= isl_space_from_domain(dim
);
112 add_name
= isl_multi_pw_aff_zero(dim
);
113 expr
= pet_expr_update_domain(expr
, add_name
);
117 stmt
->schedule
= sched
;
120 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
121 return pet_stmt_free(stmt
);
131 void *pet_stmt_free(struct pet_stmt
*stmt
)
138 pet_loc_free(stmt
->loc
);
139 isl_set_free(stmt
->domain
);
140 isl_map_free(stmt
->schedule
);
141 pet_expr_free(stmt
->body
);
143 for (i
= 0; i
< stmt
->n_arg
; ++i
)
144 pet_expr_free(stmt
->args
[i
]);
151 /* Return the iteration space of "stmt".
153 * If the statement has arguments, then stmt->domain is a wrapped map
154 * mapping the iteration domain to the values of the arguments
155 * for which this statement is executed.
156 * In this case, we need to extract the domain space of this wrapped map.
158 __isl_give isl_space
*pet_stmt_get_space(struct pet_stmt
*stmt
)
165 space
= isl_set_get_space(stmt
->domain
);
166 if (isl_space_is_wrapping(space
))
167 space
= isl_space_domain(isl_space_unwrap(space
));
172 static void stmt_dump(struct pet_stmt
*stmt
, int indent
)
179 fprintf(stderr
, "%*s%d\n", indent
, "", pet_loc_get_line(stmt
->loc
));
180 fprintf(stderr
, "%*s", indent
, "");
181 isl_set_dump(stmt
->domain
);
182 fprintf(stderr
, "%*s", indent
, "");
183 isl_map_dump(stmt
->schedule
);
184 pet_expr_dump_with_indent(stmt
->body
, indent
);
185 for (i
= 0; i
< stmt
->n_arg
; ++i
)
186 pet_expr_dump_with_indent(stmt
->args
[i
], indent
+ 2);
189 void pet_stmt_dump(struct pet_stmt
*stmt
)
194 /* Allocate a new pet_type with the given "name" and "definition".
196 struct pet_type
*pet_type_alloc(isl_ctx
*ctx
, const char *name
,
197 const char *definition
)
199 struct pet_type
*type
;
201 type
= isl_alloc_type(ctx
, struct pet_type
);
205 type
->name
= strdup(name
);
206 type
->definition
= strdup(definition
);
208 if (!type
->name
|| !type
->definition
)
209 return pet_type_free(type
);
214 /* Free "type" and return NULL.
216 struct pet_type
*pet_type_free(struct pet_type
*type
)
222 free(type
->definition
);
228 struct pet_array
*pet_array_free(struct pet_array
*array
)
233 isl_set_free(array
->context
);
234 isl_set_free(array
->extent
);
235 isl_set_free(array
->value_bounds
);
236 free(array
->element_type
);
242 void pet_array_dump(struct pet_array
*array
)
247 isl_set_dump(array
->context
);
248 isl_set_dump(array
->extent
);
249 isl_set_dump(array
->value_bounds
);
250 fprintf(stderr
, "%s%s%s\n", array
->element_type
,
251 array
->element_is_record
? " element-is-record" : "",
252 array
->live_out
? " live-out" : "");
255 /* Alloc a pet_scop structure, with extra room for information that
256 * is only used during parsing.
258 struct pet_scop
*pet_scop_alloc(isl_ctx
*ctx
)
260 return &isl_calloc_type(ctx
, struct pet_scop_ext
)->scop
;
263 /* Construct a pet_scop with room for n statements.
265 * Since no information on the location is known at this point,
266 * scop->loc is initialized with pet_loc_dummy.
268 static struct pet_scop
*scop_alloc(isl_ctx
*ctx
, int n
)
271 struct pet_scop
*scop
;
273 scop
= pet_scop_alloc(ctx
);
277 space
= isl_space_params_alloc(ctx
, 0);
278 scop
->context
= isl_set_universe(isl_space_copy(space
));
279 scop
->context_value
= isl_set_universe(space
);
280 scop
->stmts
= isl_calloc_array(ctx
, struct pet_stmt
*, n
);
281 if (!scop
->context
|| !scop
->stmts
)
282 return pet_scop_free(scop
);
284 scop
->loc
= &pet_loc_dummy
;
290 struct pet_scop
*pet_scop_empty(isl_ctx
*ctx
)
292 return scop_alloc(ctx
, 0);
295 /* Update "context" with respect to the valid parameter values for "access".
297 static __isl_give isl_set
*access_extract_context(__isl_keep isl_map
*access
,
298 __isl_take isl_set
*context
)
300 context
= isl_set_intersect(context
,
301 isl_map_params(isl_map_copy(access
)));
305 /* Update "context" with respect to the valid parameter values for "expr".
307 * If "expr" represents a conditional operator, then a parameter value
308 * needs to be valid for the condition and for at least one of the
309 * remaining two arguments.
310 * If the condition is an affine expression, then we can be a bit more specific.
311 * The parameter then has to be valid for the second argument for
312 * non-zero accesses and valid for the third argument for zero accesses.
314 static __isl_give isl_set
*expr_extract_context(__isl_keep pet_expr
*expr
,
315 __isl_take isl_set
*context
)
319 if (expr
->type
== pet_expr_op
&& expr
->op
== pet_op_cond
) {
321 isl_set
*context1
, *context2
;
323 is_aff
= pet_expr_is_affine(expr
->args
[0]);
327 context
= expr_extract_context(expr
->args
[0], context
);
328 context1
= expr_extract_context(expr
->args
[1],
329 isl_set_copy(context
));
330 context2
= expr_extract_context(expr
->args
[2], context
);
336 access
= isl_map_copy(expr
->args
[0]->acc
.access
);
337 access
= isl_map_fix_si(access
, isl_dim_out
, 0, 0);
338 zero_set
= isl_map_params(access
);
339 context1
= isl_set_subtract(context1
,
340 isl_set_copy(zero_set
));
341 context2
= isl_set_intersect(context2
, zero_set
);
344 context
= isl_set_union(context1
, context2
);
345 context
= isl_set_coalesce(context
);
350 for (i
= 0; i
< expr
->n_arg
; ++i
)
351 context
= expr_extract_context(expr
->args
[i
], context
);
353 if (expr
->type
== pet_expr_access
)
354 context
= access_extract_context(expr
->acc
.access
, context
);
358 isl_set_free(context
);
362 /* Update "context" with respect to the valid parameter values for "stmt".
364 * If the statement is an assume statement with an affine expression,
365 * then intersect "context" with that expression.
366 * Otherwise, intersect "context" with the contexts of the expressions
369 static __isl_give isl_set
*stmt_extract_context(struct pet_stmt
*stmt
,
370 __isl_take isl_set
*context
)
374 if (pet_stmt_is_assume(stmt
) &&
375 pet_expr_is_affine(stmt
->body
->args
[0])) {
376 isl_multi_pw_aff
*index
;
380 index
= stmt
->body
->args
[0]->acc
.index
;
381 pa
= isl_multi_pw_aff_get_pw_aff(index
, 0);
382 cond
= isl_set_params(isl_pw_aff_non_zero_set(pa
));
383 return isl_set_intersect(context
, cond
);
386 for (i
= 0; i
< stmt
->n_arg
; ++i
)
387 context
= expr_extract_context(stmt
->args
[i
], context
);
389 context
= expr_extract_context(stmt
->body
, context
);
394 /* Construct a pet_scop that contains the given pet_stmt.
396 struct pet_scop
*pet_scop_from_pet_stmt(isl_ctx
*ctx
, struct pet_stmt
*stmt
)
398 struct pet_scop
*scop
;
403 scop
= scop_alloc(ctx
, 1);
407 scop
->context
= stmt_extract_context(stmt
, scop
->context
);
411 scop
->stmts
[0] = stmt
;
412 scop
->loc
= pet_loc_copy(stmt
->loc
);
415 return pet_scop_free(scop
);
424 /* Does "mpa" represent an access to an element of an unnamed space, i.e.,
425 * does it represent an affine expression?
427 static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff
*mpa
)
431 has_id
= isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
);
438 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
440 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
441 __isl_take isl_set
*dom
)
444 pa
= isl_set_indicator_function(set
);
445 pa
= isl_pw_aff_intersect_domain(pa
, dom
);
449 /* Return "lhs || rhs", defined on the shared definition domain.
451 static __isl_give isl_pw_aff
*pw_aff_or(__isl_take isl_pw_aff
*lhs
,
452 __isl_take isl_pw_aff
*rhs
)
457 dom
= isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs
)),
458 isl_pw_aff_domain(isl_pw_aff_copy(rhs
)));
459 cond
= isl_set_union(isl_pw_aff_non_zero_set(lhs
),
460 isl_pw_aff_non_zero_set(rhs
));
461 cond
= isl_set_coalesce(cond
);
462 return indicator_function(cond
, dom
);
465 /* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
466 * ext may be equal to either ext1 or ext2.
468 * The two skips that need to be combined are assumed to be affine expressions.
470 * We need to skip in ext if we need to skip in either ext1 or ext2.
471 * We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
473 static struct pet_scop_ext
*combine_skips(struct pet_scop_ext
*ext
,
474 struct pet_scop_ext
*ext1
, struct pet_scop_ext
*ext2
,
477 isl_pw_aff
*skip
, *skip1
, *skip2
;
481 if (!ext1
->skip
[type
] && !ext2
->skip
[type
])
483 if (!ext1
->skip
[type
]) {
486 ext
->skip
[type
] = ext2
->skip
[type
];
487 ext2
->skip
[type
] = NULL
;
490 if (!ext2
->skip
[type
]) {
493 ext
->skip
[type
] = ext1
->skip
[type
];
494 ext1
->skip
[type
] = NULL
;
498 if (!multi_pw_aff_is_affine(ext1
->skip
[type
]) ||
499 !multi_pw_aff_is_affine(ext2
->skip
[type
]))
500 isl_die(isl_multi_pw_aff_get_ctx(ext1
->skip
[type
]),
501 isl_error_internal
, "can only combine affine skips",
504 skip1
= isl_multi_pw_aff_get_pw_aff(ext1
->skip
[type
], 0);
505 skip2
= isl_multi_pw_aff_get_pw_aff(ext2
->skip
[type
], 0);
506 skip
= pw_aff_or(skip1
, skip2
);
507 isl_multi_pw_aff_free(ext1
->skip
[type
]);
508 ext1
->skip
[type
] = NULL
;
509 isl_multi_pw_aff_free(ext2
->skip
[type
]);
510 ext2
->skip
[type
] = NULL
;
511 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
512 if (!ext
->skip
[type
])
517 pet_scop_free(&ext
->scop
);
521 /* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
522 * where type takes on the values pet_skip_now and pet_skip_later.
523 * scop may be equal to either scop1 or scop2.
525 static struct pet_scop
*scop_combine_skips(struct pet_scop
*scop
,
526 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
528 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
529 struct pet_scop_ext
*ext1
= (struct pet_scop_ext
*) scop1
;
530 struct pet_scop_ext
*ext2
= (struct pet_scop_ext
*) scop2
;
532 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_now
);
533 ext
= combine_skips(ext
, ext1
, ext2
, pet_skip_later
);
537 /* Update start and end of scop->loc to include the region from "start"
538 * to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
539 * does not have any offset information yet and we simply take the information
540 * from "start" and "end". Otherwise, we update loc using "start" and "end".
542 struct pet_scop
*pet_scop_update_start_end(struct pet_scop
*scop
,
543 unsigned start
, unsigned end
)
548 if (scop
->loc
== &pet_loc_dummy
)
549 scop
->loc
= pet_loc_alloc(isl_set_get_ctx(scop
->context
),
552 scop
->loc
= pet_loc_update_start_end(scop
->loc
, start
, end
);
555 return pet_scop_free(scop
);
560 /* Update start and end of scop->loc to include the region identified
563 struct pet_scop
*pet_scop_update_start_end_from_loc(struct pet_scop
*scop
,
564 __isl_keep pet_loc
*loc
)
566 return pet_scop_update_start_end(scop
, pet_loc_get_start(loc
),
567 pet_loc_get_end(loc
));
570 /* Replace the location of "scop" by "loc".
572 struct pet_scop
*pet_scop_set_loc(struct pet_scop
*scop
,
573 __isl_take pet_loc
*loc
)
578 pet_loc_free(scop
->loc
);
588 /* Does "implication" appear in the list of implications of "scop"?
590 static int is_known_implication(struct pet_scop
*scop
,
591 struct pet_implication
*implication
)
595 for (i
= 0; i
< scop
->n_implication
; ++i
) {
596 struct pet_implication
*pi
= scop
->implications
[i
];
599 if (pi
->satisfied
!= implication
->satisfied
)
601 equal
= isl_map_is_equal(pi
->extension
, implication
->extension
);
611 /* Store the concatenation of the implications of "scop1" and "scop2"
612 * in "scop", removing duplicates (i.e., implications in "scop2" that
613 * already appear in "scop1").
615 static struct pet_scop
*scop_collect_implications(isl_ctx
*ctx
,
616 struct pet_scop
*scop
, struct pet_scop
*scop1
, struct pet_scop
*scop2
)
623 if (scop2
->n_implication
== 0) {
624 scop
->n_implication
= scop1
->n_implication
;
625 scop
->implications
= scop1
->implications
;
626 scop1
->n_implication
= 0;
627 scop1
->implications
= NULL
;
631 if (scop1
->n_implication
== 0) {
632 scop
->n_implication
= scop2
->n_implication
;
633 scop
->implications
= scop2
->implications
;
634 scop2
->n_implication
= 0;
635 scop2
->implications
= NULL
;
639 scop
->implications
= isl_calloc_array(ctx
, struct pet_implication
*,
640 scop1
->n_implication
+ scop2
->n_implication
);
641 if (!scop
->implications
)
642 return pet_scop_free(scop
);
644 for (i
= 0; i
< scop1
->n_implication
; ++i
) {
645 scop
->implications
[i
] = scop1
->implications
[i
];
646 scop1
->implications
[i
] = NULL
;
649 scop
->n_implication
= scop1
->n_implication
;
650 j
= scop1
->n_implication
;
651 for (i
= 0; i
< scop2
->n_implication
; ++i
) {
654 known
= is_known_implication(scop
, scop2
->implications
[i
]);
656 return pet_scop_free(scop
);
659 scop
->implications
[j
++] = scop2
->implications
[i
];
660 scop2
->implications
[i
] = NULL
;
662 scop
->n_implication
= j
;
667 /* Combine the offset information of "scop1" and "scop2" into "scop".
669 static struct pet_scop
*scop_combine_start_end(struct pet_scop
*scop
,
670 struct pet_scop
*scop1
, struct pet_scop
*scop2
)
672 if (scop1
->loc
!= &pet_loc_dummy
)
673 scop
= pet_scop_update_start_end_from_loc(scop
, scop1
->loc
);
674 if (scop2
->loc
!= &pet_loc_dummy
)
675 scop
= pet_scop_update_start_end_from_loc(scop
, scop2
->loc
);
679 /* Construct a pet_scop that contains the offset information,
680 * arrays, statements and skip information in "scop1" and "scop2".
682 static struct pet_scop
*pet_scop_add(isl_ctx
*ctx
, struct pet_scop
*scop1
,
683 struct pet_scop
*scop2
)
686 struct pet_scop
*scop
= NULL
;
688 if (!scop1
|| !scop2
)
691 if (scop1
->n_stmt
== 0) {
692 scop2
= scop_combine_skips(scop2
, scop1
, scop2
);
693 pet_scop_free(scop1
);
697 if (scop2
->n_stmt
== 0) {
698 scop1
= scop_combine_skips(scop1
, scop1
, scop2
);
699 pet_scop_free(scop2
);
703 scop
= scop_alloc(ctx
, scop1
->n_stmt
+ scop2
->n_stmt
);
707 scop
->arrays
= isl_calloc_array(ctx
, struct pet_array
*,
708 scop1
->n_array
+ scop2
->n_array
);
711 scop
->n_array
= scop1
->n_array
+ scop2
->n_array
;
713 for (i
= 0; i
< scop1
->n_stmt
; ++i
) {
714 scop
->stmts
[i
] = scop1
->stmts
[i
];
715 scop1
->stmts
[i
] = NULL
;
718 for (i
= 0; i
< scop2
->n_stmt
; ++i
) {
719 scop
->stmts
[scop1
->n_stmt
+ i
] = scop2
->stmts
[i
];
720 scop2
->stmts
[i
] = NULL
;
723 for (i
= 0; i
< scop1
->n_array
; ++i
) {
724 scop
->arrays
[i
] = scop1
->arrays
[i
];
725 scop1
->arrays
[i
] = NULL
;
728 for (i
= 0; i
< scop2
->n_array
; ++i
) {
729 scop
->arrays
[scop1
->n_array
+ i
] = scop2
->arrays
[i
];
730 scop2
->arrays
[i
] = NULL
;
733 scop
= scop_collect_implications(ctx
, scop
, scop1
, scop2
);
734 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop1
->context
));
735 scop
= pet_scop_restrict_context(scop
, isl_set_copy(scop2
->context
));
736 scop
= scop_combine_skips(scop
, scop1
, scop2
);
737 scop
= scop_combine_start_end(scop
, scop1
, scop2
);
739 pet_scop_free(scop1
);
740 pet_scop_free(scop2
);
743 pet_scop_free(scop1
);
744 pet_scop_free(scop2
);
749 /* Apply the skip condition "skip" to "scop".
750 * That is, make sure "scop" is not executed when the condition holds.
752 * If "skip" is an affine expression, we add the conditions under
753 * which the expression is zero to the iteration domains.
754 * Otherwise, we add a filter on the variable attaining the value zero.
756 static struct pet_scop
*restrict_skip(struct pet_scop
*scop
,
757 __isl_take isl_multi_pw_aff
*skip
)
766 is_aff
= multi_pw_aff_is_affine(skip
);
771 return pet_scop_filter(scop
, skip
, 0);
773 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
774 isl_multi_pw_aff_free(skip
);
775 zero
= isl_set_params(isl_pw_aff_zero_set(pa
));
776 scop
= pet_scop_restrict(scop
, zero
);
780 isl_multi_pw_aff_free(skip
);
781 return pet_scop_free(scop
);
784 /* Construct a pet_scop that contains the arrays, statements and
785 * skip information in "scop1" and "scop2", where the two scops
786 * are executed "in sequence". That is, breaks and continues
787 * in scop1 have an effect on scop2.
789 struct pet_scop
*pet_scop_add_seq(isl_ctx
*ctx
, struct pet_scop
*scop1
,
790 struct pet_scop
*scop2
)
792 if (scop1
&& pet_scop_has_skip(scop1
, pet_skip_now
))
793 scop2
= restrict_skip(scop2
,
794 pet_scop_get_skip(scop1
, pet_skip_now
));
795 return pet_scop_add(ctx
, scop1
, scop2
);
798 /* Construct a pet_scop that contains the arrays, statements and
799 * skip information in "scop1" and "scop2", where the two scops
800 * are executed "in parallel". That is, any break or continue
801 * in scop1 has no effect on scop2.
803 struct pet_scop
*pet_scop_add_par(isl_ctx
*ctx
, struct pet_scop
*scop1
,
804 struct pet_scop
*scop2
)
806 return pet_scop_add(ctx
, scop1
, scop2
);
809 void *pet_implication_free(struct pet_implication
*implication
)
816 isl_map_free(implication
->extension
);
822 struct pet_scop
*pet_scop_free(struct pet_scop
*scop
)
825 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
829 pet_loc_free(scop
->loc
);
830 isl_set_free(scop
->context
);
831 isl_set_free(scop
->context_value
);
833 for (i
= 0; i
< scop
->n_type
; ++i
)
834 pet_type_free(scop
->types
[i
]);
837 for (i
= 0; i
< scop
->n_array
; ++i
)
838 pet_array_free(scop
->arrays
[i
]);
841 for (i
= 0; i
< scop
->n_stmt
; ++i
)
842 pet_stmt_free(scop
->stmts
[i
]);
844 if (scop
->implications
)
845 for (i
= 0; i
< scop
->n_implication
; ++i
)
846 pet_implication_free(scop
->implications
[i
]);
847 free(scop
->implications
);
848 isl_multi_pw_aff_free(ext
->skip
[pet_skip_now
]);
849 isl_multi_pw_aff_free(ext
->skip
[pet_skip_later
]);
854 void pet_type_dump(struct pet_type
*type
)
859 fprintf(stderr
, "%s -> %s\n", type
->name
, type
->definition
);
862 void pet_implication_dump(struct pet_implication
*implication
)
867 fprintf(stderr
, "%d\n", implication
->satisfied
);
868 isl_map_dump(implication
->extension
);
871 void pet_scop_dump(struct pet_scop
*scop
)
874 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
879 isl_set_dump(scop
->context
);
880 isl_set_dump(scop
->context_value
);
881 for (i
= 0; i
< scop
->n_type
; ++i
)
882 pet_type_dump(scop
->types
[i
]);
883 for (i
= 0; i
< scop
->n_array
; ++i
)
884 pet_array_dump(scop
->arrays
[i
]);
885 for (i
= 0; i
< scop
->n_stmt
; ++i
)
886 pet_stmt_dump(scop
->stmts
[i
]);
887 for (i
= 0; i
< scop
->n_implication
; ++i
)
888 pet_implication_dump(scop
->implications
[i
]);
891 fprintf(stderr
, "skip\n");
892 isl_multi_pw_aff_dump(ext
->skip
[0]);
893 isl_multi_pw_aff_dump(ext
->skip
[1]);
897 /* Return 1 if the two pet_arrays are equivalent.
899 * We don't compare element_size as this may be target dependent.
901 int pet_array_is_equal(struct pet_array
*array1
, struct pet_array
*array2
)
903 if (!array1
|| !array2
)
906 if (!isl_set_is_equal(array1
->context
, array2
->context
))
908 if (!isl_set_is_equal(array1
->extent
, array2
->extent
))
910 if (!!array1
->value_bounds
!= !!array2
->value_bounds
)
912 if (array1
->value_bounds
&&
913 !isl_set_is_equal(array1
->value_bounds
, array2
->value_bounds
))
915 if (strcmp(array1
->element_type
, array2
->element_type
))
917 if (array1
->element_is_record
!= array2
->element_is_record
)
919 if (array1
->live_out
!= array2
->live_out
)
921 if (array1
->uniquely_defined
!= array2
->uniquely_defined
)
923 if (array1
->declared
!= array2
->declared
)
925 if (array1
->exposed
!= array2
->exposed
)
931 /* Return 1 if the two pet_stmts are equivalent.
933 int pet_stmt_is_equal(struct pet_stmt
*stmt1
, struct pet_stmt
*stmt2
)
937 if (!stmt1
|| !stmt2
)
940 if (pet_loc_get_line(stmt1
->loc
) != pet_loc_get_line(stmt2
->loc
))
942 if (!isl_set_is_equal(stmt1
->domain
, stmt2
->domain
))
944 if (!isl_map_is_equal(stmt1
->schedule
, stmt2
->schedule
))
946 if (!pet_expr_is_equal(stmt1
->body
, stmt2
->body
))
948 if (stmt1
->n_arg
!= stmt2
->n_arg
)
950 for (i
= 0; i
< stmt1
->n_arg
; ++i
) {
951 if (!pet_expr_is_equal(stmt1
->args
[i
], stmt2
->args
[i
]))
958 /* Return 1 if the two pet_types are equivalent.
960 * We only compare the names of the types since the exact representation
961 * of the definition may depend on the version of clang being used.
963 int pet_type_is_equal(struct pet_type
*type1
, struct pet_type
*type2
)
965 if (!type1
|| !type2
)
968 if (strcmp(type1
->name
, type2
->name
))
974 /* Return 1 if the two pet_implications are equivalent.
976 int pet_implication_is_equal(struct pet_implication
*implication1
,
977 struct pet_implication
*implication2
)
979 if (!implication1
|| !implication2
)
982 if (implication1
->satisfied
!= implication2
->satisfied
)
984 if (!isl_map_is_equal(implication1
->extension
, implication2
->extension
))
990 /* Return 1 if the two pet_scops are equivalent.
992 int pet_scop_is_equal(struct pet_scop
*scop1
, struct pet_scop
*scop2
)
996 if (!scop1
|| !scop2
)
999 if (!isl_set_is_equal(scop1
->context
, scop2
->context
))
1001 if (!isl_set_is_equal(scop1
->context_value
, scop2
->context_value
))
1004 if (scop1
->n_type
!= scop2
->n_type
)
1006 for (i
= 0; i
< scop1
->n_type
; ++i
)
1007 if (!pet_type_is_equal(scop1
->types
[i
], scop2
->types
[i
]))
1010 if (scop1
->n_array
!= scop2
->n_array
)
1012 for (i
= 0; i
< scop1
->n_array
; ++i
)
1013 if (!pet_array_is_equal(scop1
->arrays
[i
], scop2
->arrays
[i
]))
1016 if (scop1
->n_stmt
!= scop2
->n_stmt
)
1018 for (i
= 0; i
< scop1
->n_stmt
; ++i
)
1019 if (!pet_stmt_is_equal(scop1
->stmts
[i
], scop2
->stmts
[i
]))
1022 if (scop1
->n_implication
!= scop2
->n_implication
)
1024 for (i
= 0; i
< scop1
->n_implication
; ++i
)
1025 if (!pet_implication_is_equal(scop1
->implications
[i
],
1026 scop2
->implications
[i
]))
1032 /* Does the set "extent" reference a virtual array, i.e.,
1033 * one with user pointer equal to NULL?
1035 static int extent_is_virtual_array(__isl_keep isl_set
*extent
)
1040 if (!isl_set_has_tuple_id(extent
))
1042 id
= isl_set_get_tuple_id(extent
);
1043 is_virtual
= !isl_id_get_user(id
);
1049 /* Prefix the schedule of "stmt" with an extra dimension with constant
1052 struct pet_stmt
*pet_stmt_prefix(struct pet_stmt
*stmt
, int pos
)
1057 stmt
->schedule
= isl_map_insert_dims(stmt
->schedule
, isl_dim_out
, 0, 1);
1058 stmt
->schedule
= isl_map_fix_si(stmt
->schedule
, isl_dim_out
, 0, pos
);
1059 if (!stmt
->schedule
)
1060 return pet_stmt_free(stmt
);
1065 /* Prefix the schedules of all statements in "scop" with an extra
1066 * dimension with constant value "pos".
1068 struct pet_scop
*pet_scop_prefix(struct pet_scop
*scop
, int pos
)
1075 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1076 scop
->stmts
[i
] = pet_stmt_prefix(scop
->stmts
[i
], pos
);
1077 if (!scop
->stmts
[i
])
1078 return pet_scop_free(scop
);
1084 /* Given a set with a parameter at "param_pos" that refers to the
1085 * iterator, "move" the iterator to the first set dimension.
1086 * That is, essentially equate the parameter to the first set dimension
1087 * and then project it out.
1089 * The first set dimension may however refer to a virtual iterator,
1090 * while the parameter refers to the "real" iterator.
1091 * We therefore need to take into account the affine expression "iv_map", which
1092 * expresses the real iterator in terms of the virtual iterator.
1093 * In particular, we equate the set dimension to the input of the map
1094 * and the parameter to the output of the map and then project out
1095 * everything we don't need anymore.
1097 static __isl_give isl_set
*internalize_iv(__isl_take isl_set
*set
,
1098 int param_pos
, __isl_take isl_aff
*iv_map
)
1100 isl_map
*map
, *map2
;
1101 map
= isl_map_from_domain(set
);
1102 map
= isl_map_add_dims(map
, isl_dim_out
, 1);
1103 map
= isl_map_equate(map
, isl_dim_in
, 0, isl_dim_out
, 0);
1104 map2
= isl_map_from_aff(iv_map
);
1105 map2
= isl_map_align_params(map2
, isl_map_get_space(map
));
1106 map
= isl_map_apply_range(map
, map2
);
1107 map
= isl_map_equate(map
, isl_dim_param
, param_pos
, isl_dim_out
, 0);
1108 map
= isl_map_project_out(map
, isl_dim_param
, param_pos
, 1);
1109 return isl_map_domain(map
);
1112 /* Data used in embed_access.
1113 * extend adds an iterator to the iteration domain (through precomposition).
1114 * iv_map expresses the real iterator in terms of the virtual iterator
1115 * var_id represents the induction variable of the corresponding loop
1117 struct pet_embed_access
{
1118 isl_multi_pw_aff
*extend
;
1123 /* Given an index expression, return an expression for the outer iterator.
1125 static __isl_give isl_aff
*index_outer_iterator(
1126 __isl_take isl_multi_pw_aff
*index
)
1129 isl_local_space
*ls
;
1131 space
= isl_multi_pw_aff_get_domain_space(index
);
1132 isl_multi_pw_aff_free(index
);
1134 ls
= isl_local_space_from_space(space
);
1135 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1138 /* Replace an index expression that references the new (outer) iterator variable
1139 * by one that references the corresponding (real) iterator.
1141 * The input index expression is of the form
1143 * { S[i',...] -> i[] }
1145 * where i' refers to the virtual iterator.
1147 * iv_map is of the form
1151 * Return the index expression
1153 * { S[i',...] -> [i] }
1155 static __isl_give isl_multi_pw_aff
*replace_by_iterator(
1156 __isl_take isl_multi_pw_aff
*index
, __isl_take isl_aff
*iv_map
)
1161 aff
= index_outer_iterator(index
);
1162 space
= isl_aff_get_space(aff
);
1163 iv_map
= isl_aff_align_params(iv_map
, space
);
1164 aff
= isl_aff_pullback_aff(iv_map
, aff
);
1166 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1169 /* Given an index expression "index" that refers to the (real) iterator
1170 * through the parameter at position "pos", plug in "iv_map", expressing
1171 * the real iterator in terms of the virtual (outer) iterator.
1173 * In particular, the index expression is of the form
1175 * [..., i, ...] -> { S[i',...] -> ... i ... }
1177 * where i refers to the real iterator and i' refers to the virtual iterator.
1179 * iv_map is of the form
1183 * Return the index expression
1185 * [..., ...] -> { S[i',...] -> ... iv_map(i') ... }
1188 * We first move the parameter to the input
1190 * [..., ...] -> { [i, i',...] -> ... i ... }
1194 * { S[i',...] -> [i=iv_map(i'), i', ...] }
1196 * and then combine the two to obtain the desired result.
1198 static __isl_give isl_multi_pw_aff
*index_internalize_iv(
1199 __isl_take isl_multi_pw_aff
*index
, int pos
, __isl_take isl_aff
*iv_map
)
1201 isl_space
*space
= isl_multi_pw_aff_get_domain_space(index
);
1204 space
= isl_space_drop_dims(space
, isl_dim_param
, pos
, 1);
1205 index
= isl_multi_pw_aff_move_dims(index
, isl_dim_in
, 0,
1206 isl_dim_param
, pos
, 1);
1208 space
= isl_space_map_from_set(space
);
1209 ma
= isl_multi_aff_identity(isl_space_copy(space
));
1210 iv_map
= isl_aff_align_params(iv_map
, space
);
1211 iv_map
= isl_aff_pullback_aff(iv_map
, isl_multi_aff_get_aff(ma
, 0));
1212 ma
= isl_multi_aff_flat_range_product(
1213 isl_multi_aff_from_aff(iv_map
), ma
);
1214 index
= isl_multi_pw_aff_pullback_multi_aff(index
, ma
);
1219 /* Does the index expression "index" reference a virtual array, i.e.,
1220 * one with user pointer equal to NULL?
1221 * A virtual array does not have any members.
1223 static int index_is_virtual_array(__isl_keep isl_multi_pw_aff
*index
)
1228 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
1230 if (isl_multi_pw_aff_range_is_wrapping(index
))
1232 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1233 is_virtual
= !isl_id_get_user(id
);
1239 /* Does the access relation "access" reference a virtual array, i.e.,
1240 * one with user pointer equal to NULL?
1241 * A virtual array does not have any members.
1243 static int access_is_virtual_array(__isl_keep isl_map
*access
)
1248 if (!isl_map_has_tuple_id(access
, isl_dim_out
))
1250 if (isl_map_range_is_wrapping(access
))
1252 id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1253 is_virtual
= !isl_id_get_user(id
);
1259 /* Embed the given index expression in an extra outer loop.
1260 * The domain of the index expression has already been updated.
1262 * If the access refers to the induction variable, then it is
1263 * turned into an access to the set of integers with index (and value)
1264 * equal to the induction variable.
1266 * If the accessed array is a virtual array (with user
1267 * pointer equal to NULL), as created by create_test_index,
1268 * then it is extended along with the domain of the index expression.
1270 static __isl_give isl_multi_pw_aff
*embed_index_expression(
1271 __isl_take isl_multi_pw_aff
*index
, struct pet_embed_access
*data
)
1273 isl_id
*array_id
= NULL
;
1276 if (isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
1277 array_id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
1278 if (array_id
== data
->var_id
) {
1279 index
= replace_by_iterator(index
, isl_aff_copy(data
->iv_map
));
1280 } else if (index_is_virtual_array(index
)) {
1282 isl_multi_pw_aff
*mpa
;
1284 aff
= index_outer_iterator(isl_multi_pw_aff_copy(index
));
1285 mpa
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1286 index
= isl_multi_pw_aff_flat_range_product(mpa
, index
);
1287 index
= isl_multi_pw_aff_set_tuple_id(index
, isl_dim_out
,
1288 isl_id_copy(array_id
));
1290 isl_id_free(array_id
);
1292 pos
= isl_multi_pw_aff_find_dim_by_id(index
,
1293 isl_dim_param
, data
->var_id
);
1295 index
= index_internalize_iv(index
, pos
,
1296 isl_aff_copy(data
->iv_map
));
1297 index
= isl_multi_pw_aff_set_dim_id(index
, isl_dim_in
, 0,
1298 isl_id_copy(data
->var_id
));
1303 /* Embed the given access relation in an extra outer loop.
1304 * The domain of the access relation has already been updated.
1306 * If the access refers to the induction variable, then it is
1307 * turned into an access to the set of integers with index (and value)
1308 * equal to the induction variable.
1310 * If the induction variable appears in the constraints (as a parameter),
1311 * then the parameter is equated to the newly introduced iteration
1312 * domain dimension and subsequently projected out.
1314 * Similarly, if the accessed array is a virtual array (with user
1315 * pointer equal to NULL), as created by create_test_index,
1316 * then it is extended along with the domain of the access.
1318 static __isl_give isl_map
*embed_access_relation(__isl_take isl_map
*access
,
1319 struct pet_embed_access
*data
)
1321 isl_id
*array_id
= NULL
;
1324 if (isl_map_has_tuple_id(access
, isl_dim_out
))
1325 array_id
= isl_map_get_tuple_id(access
, isl_dim_out
);
1326 if (array_id
== data
->var_id
|| access_is_virtual_array(access
)) {
1327 access
= isl_map_insert_dims(access
, isl_dim_out
, 0, 1);
1328 access
= isl_map_equate(access
,
1329 isl_dim_in
, 0, isl_dim_out
, 0);
1330 if (array_id
== data
->var_id
)
1331 access
= isl_map_apply_range(access
,
1332 isl_map_from_aff(isl_aff_copy(data
->iv_map
)));
1334 access
= isl_map_set_tuple_id(access
, isl_dim_out
,
1335 isl_id_copy(array_id
));
1337 isl_id_free(array_id
);
1339 pos
= isl_map_find_dim_by_id(access
, isl_dim_param
, data
->var_id
);
1341 isl_set
*set
= isl_map_wrap(access
);
1342 set
= internalize_iv(set
, pos
, isl_aff_copy(data
->iv_map
));
1343 access
= isl_set_unwrap(set
);
1345 access
= isl_map_set_dim_id(access
, isl_dim_in
, 0,
1346 isl_id_copy(data
->var_id
));
1351 /* Given an access expression, embed the associated access relation and
1352 * index expression in an extra outer loop.
1354 * We first update the domains to insert the extra dimension and
1355 * then update the access relation and index expression to take
1356 * into account the mapping "iv_map" from virtual iterator
1359 static __isl_give pet_expr
*embed_access(__isl_take pet_expr
*expr
, void *user
)
1361 struct pet_embed_access
*data
= user
;
1363 expr
= pet_expr_cow(expr
);
1364 expr
= pet_expr_access_update_domain(expr
, data
->extend
);
1368 expr
->acc
.access
= embed_access_relation(expr
->acc
.access
, data
);
1369 expr
->acc
.index
= embed_index_expression(expr
->acc
.index
, data
);
1370 if (!expr
->acc
.access
|| !expr
->acc
.index
)
1371 return pet_expr_free(expr
);
1376 /* Embed all access subexpressions of "expr" in an extra loop.
1377 * "extend" inserts an outer loop iterator in the iteration domains
1378 * (through precomposition).
1379 * "iv_map" expresses the real iterator in terms of the virtual iterator
1380 * "var_id" represents the induction variable.
1382 static __isl_give pet_expr
*expr_embed(__isl_take pet_expr
*expr
,
1383 __isl_take isl_multi_pw_aff
*extend
, __isl_take isl_aff
*iv_map
,
1384 __isl_keep isl_id
*var_id
)
1386 struct pet_embed_access data
=
1387 { .extend
= extend
, .iv_map
= iv_map
, .var_id
= var_id
};
1389 expr
= pet_expr_map_access(expr
, &embed_access
, &data
);
1390 isl_aff_free(iv_map
);
1391 isl_multi_pw_aff_free(extend
);
1395 /* Embed the given pet_stmt in an extra outer loop with iteration domain
1396 * "dom" and schedule "sched". "var_id" represents the induction variable
1397 * of the loop. "iv_map" maps a possibly virtual iterator to the real iterator.
1398 * That is, it expresses the iterator that some of the parameters in "stmt"
1399 * may refer to in terms of the iterator used in "dom" and
1400 * the domain of "sched".
1402 * The iteration domain and schedule of the statement are updated
1403 * according to the iteration domain and schedule of the new loop.
1404 * If stmt->domain is a wrapped map, then the iteration domain
1405 * is the domain of this map, so we need to be careful to adjust
1408 * If the induction variable appears in the constraints (as a parameter)
1409 * of the current iteration domain or the schedule of the statement,
1410 * then the parameter is equated to the newly introduced iteration
1411 * domain dimension and subsequently projected out.
1413 * Finally, all access relations are updated based on the extra loop.
1415 static struct pet_stmt
*pet_stmt_embed(struct pet_stmt
*stmt
,
1416 __isl_take isl_set
*dom
, __isl_take isl_map
*sched
,
1417 __isl_take isl_aff
*iv_map
, __isl_take isl_id
*var_id
)
1423 isl_multi_pw_aff
*extend
;
1428 if (isl_set_is_wrapping(stmt
->domain
)) {
1433 map
= isl_set_unwrap(stmt
->domain
);
1434 stmt_id
= isl_map_get_tuple_id(map
, isl_dim_in
);
1435 ran_dim
= isl_space_range(isl_map_get_space(map
));
1436 ext
= isl_map_from_domain_and_range(isl_set_copy(dom
),
1437 isl_set_universe(ran_dim
));
1438 map
= isl_map_flat_domain_product(ext
, map
);
1439 map
= isl_map_set_tuple_id(map
, isl_dim_in
,
1440 isl_id_copy(stmt_id
));
1441 dim
= isl_space_domain(isl_map_get_space(map
));
1442 stmt
->domain
= isl_map_wrap(map
);
1444 stmt_id
= isl_set_get_tuple_id(stmt
->domain
);
1445 stmt
->domain
= isl_set_flat_product(isl_set_copy(dom
),
1447 stmt
->domain
= isl_set_set_tuple_id(stmt
->domain
,
1448 isl_id_copy(stmt_id
));
1449 dim
= isl_set_get_space(stmt
->domain
);
1452 pos
= isl_set_find_dim_by_id(stmt
->domain
, isl_dim_param
, var_id
);
1454 stmt
->domain
= internalize_iv(stmt
->domain
, pos
,
1455 isl_aff_copy(iv_map
));
1457 stmt
->schedule
= isl_map_flat_product(sched
, stmt
->schedule
);
1458 stmt
->schedule
= isl_map_set_tuple_id(stmt
->schedule
,
1459 isl_dim_in
, stmt_id
);
1461 pos
= isl_map_find_dim_by_id(stmt
->schedule
, isl_dim_param
, var_id
);
1463 isl_set
*set
= isl_map_wrap(stmt
->schedule
);
1464 set
= internalize_iv(set
, pos
, isl_aff_copy(iv_map
));
1465 stmt
->schedule
= isl_set_unwrap(set
);
1468 dim
= isl_space_map_from_set(dim
);
1469 extend
= isl_multi_pw_aff_identity(dim
);
1470 extend
= isl_multi_pw_aff_drop_dims(extend
, isl_dim_out
, 0, 1);
1471 extend
= isl_multi_pw_aff_set_tuple_id(extend
, isl_dim_out
,
1472 isl_multi_pw_aff_get_tuple_id(extend
, isl_dim_in
));
1473 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1474 stmt
->args
[i
] = expr_embed(stmt
->args
[i
],
1475 isl_multi_pw_aff_copy(extend
),
1476 isl_aff_copy(iv_map
), var_id
);
1477 stmt
->body
= expr_embed(stmt
->body
, extend
, iv_map
, var_id
);
1480 isl_id_free(var_id
);
1482 for (i
= 0; i
< stmt
->n_arg
; ++i
)
1484 return pet_stmt_free(stmt
);
1485 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
1486 return pet_stmt_free(stmt
);
1490 isl_map_free(sched
);
1491 isl_aff_free(iv_map
);
1492 isl_id_free(var_id
);
1496 /* Embed the given pet_array in an extra outer loop with iteration domain
1498 * This embedding only has an effect on virtual arrays (those with
1499 * user pointer equal to NULL), which need to be extended along with
1500 * the iteration domain.
1502 static struct pet_array
*pet_array_embed(struct pet_array
*array
,
1503 __isl_take isl_set
*dom
)
1505 isl_id
*array_id
= NULL
;
1509 if (!extent_is_virtual_array(array
->extent
)) {
1514 array_id
= isl_set_get_tuple_id(array
->extent
);
1515 array
->extent
= isl_set_flat_product(dom
, array
->extent
);
1516 array
->extent
= isl_set_set_tuple_id(array
->extent
, array_id
);
1518 return pet_array_free(array
);
1526 /* Update the context with respect to an embedding into a loop
1527 * with iteration domain "dom" and induction variable "id".
1528 * "iv_map" expresses the real iterator (parameter "id") in terms
1529 * of a possibly virtual iterator (used in "dom").
1531 * If the current context is independent of "id", we don't need
1533 * Otherwise, a parameter value is invalid for the embedding if
1534 * any of the corresponding iterator values is invalid.
1535 * That is, a parameter value is valid only if all the corresponding
1536 * iterator values are valid.
1537 * We therefore compute the set of parameters
1539 * forall i in dom : valid (i)
1543 * not exists i in dom : not valid(i)
1547 * not exists i in dom \ valid(i)
1549 * Before we subtract valid(i) from dom, we first need to substitute
1550 * the real iterator for the virtual iterator.
1552 * If there are any unnamed parameters in "dom", then we consider
1553 * a parameter value to be valid if it is valid for any value of those
1554 * unnamed parameters. They are therefore projected out at the end.
1556 static __isl_give isl_set
*context_embed(__isl_take isl_set
*context
,
1557 __isl_keep isl_set
*dom
, __isl_keep isl_aff
*iv_map
,
1558 __isl_keep isl_id
*id
)
1563 pos
= isl_set_find_dim_by_id(context
, isl_dim_param
, id
);
1567 context
= isl_set_from_params(context
);
1568 context
= isl_set_add_dims(context
, isl_dim_set
, 1);
1569 context
= isl_set_equate(context
, isl_dim_param
, pos
, isl_dim_set
, 0);
1570 context
= isl_set_project_out(context
, isl_dim_param
, pos
, 1);
1571 ma
= isl_multi_aff_from_aff(isl_aff_copy(iv_map
));
1572 context
= isl_set_preimage_multi_aff(context
, ma
);
1573 context
= isl_set_subtract(isl_set_copy(dom
), context
);
1574 context
= isl_set_params(context
);
1575 context
= isl_set_complement(context
);
1576 context
= pet_nested_remove_from_set(context
);
1580 /* Update the implication with respect to an embedding into a loop
1581 * with iteration domain "dom".
1583 * Since embed_access extends virtual arrays along with the domain
1584 * of the access, we need to do the same with domain and range
1585 * of the implication. Since the original implication is only valid
1586 * within a given iteration of the loop, the extended implication
1587 * maps the extra array dimension corresponding to the extra loop
1590 static struct pet_implication
*pet_implication_embed(
1591 struct pet_implication
*implication
, __isl_take isl_set
*dom
)
1599 map
= isl_set_identity(dom
);
1600 id
= isl_map_get_tuple_id(implication
->extension
, isl_dim_in
);
1601 map
= isl_map_flat_product(map
, implication
->extension
);
1602 map
= isl_map_set_tuple_id(map
, isl_dim_in
, isl_id_copy(id
));
1603 map
= isl_map_set_tuple_id(map
, isl_dim_out
, id
);
1604 implication
->extension
= map
;
1605 if (!implication
->extension
)
1606 return pet_implication_free(implication
);
1614 /* Embed all statements and arrays in "scop" in an extra outer loop
1615 * with iteration domain "dom" and schedule "sched".
1616 * "id" represents the induction variable of the loop.
1617 * "iv_map" maps a possibly virtual iterator to the real iterator.
1618 * That is, it expresses the iterator that some of the parameters in "scop"
1619 * may refer to in terms of the iterator used in "dom" and
1620 * the domain of "sched".
1622 * Any skip conditions within the loop have no effect outside of the loop.
1623 * The caller is responsible for making sure skip[pet_skip_later] has been
1624 * taken into account.
1626 struct pet_scop
*pet_scop_embed(struct pet_scop
*scop
, __isl_take isl_set
*dom
,
1627 __isl_take isl_aff
*sched
, __isl_take isl_aff
*iv_map
,
1628 __isl_take isl_id
*id
)
1633 sched_map
= isl_map_from_aff(sched
);
1638 pet_scop_reset_skip(scop
, pet_skip_now
);
1639 pet_scop_reset_skip(scop
, pet_skip_later
);
1641 scop
->context
= context_embed(scop
->context
, dom
, iv_map
, id
);
1645 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1646 scop
->stmts
[i
] = pet_stmt_embed(scop
->stmts
[i
],
1647 isl_set_copy(dom
), isl_map_copy(sched_map
),
1648 isl_aff_copy(iv_map
), isl_id_copy(id
));
1649 if (!scop
->stmts
[i
])
1653 for (i
= 0; i
< scop
->n_array
; ++i
) {
1654 scop
->arrays
[i
] = pet_array_embed(scop
->arrays
[i
],
1656 if (!scop
->arrays
[i
])
1660 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1661 scop
->implications
[i
] =
1662 pet_implication_embed(scop
->implications
[i
],
1664 if (!scop
->implications
[i
])
1669 isl_map_free(sched_map
);
1670 isl_aff_free(iv_map
);
1675 isl_map_free(sched_map
);
1676 isl_aff_free(iv_map
);
1678 return pet_scop_free(scop
);
1681 /* Add extra conditions on the parameters to the iteration domain of "stmt".
1683 static struct pet_stmt
*stmt_restrict(struct pet_stmt
*stmt
,
1684 __isl_take isl_set
*cond
)
1689 stmt
->domain
= isl_set_intersect_params(stmt
->domain
, cond
);
1694 return pet_stmt_free(stmt
);
1697 /* Add extra conditions to scop->skip[type].
1699 * The new skip condition only holds if it held before
1700 * and the condition is true. It does not hold if it did not hold
1701 * before or the condition is false.
1703 * The skip condition is assumed to be an affine expression.
1705 static struct pet_scop
*pet_scop_restrict_skip(struct pet_scop
*scop
,
1706 enum pet_skip type
, __isl_keep isl_set
*cond
)
1708 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
1714 if (!ext
->skip
[type
])
1717 if (!multi_pw_aff_is_affine(ext
->skip
[type
]))
1718 isl_die(isl_multi_pw_aff_get_ctx(ext
->skip
[type
]),
1719 isl_error_internal
, "can only restrict affine skips",
1720 return pet_scop_free(scop
));
1722 skip
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
1723 dom
= isl_pw_aff_domain(isl_pw_aff_copy(skip
));
1724 cond
= isl_set_copy(cond
);
1725 cond
= isl_set_from_params(cond
);
1726 cond
= isl_set_intersect(cond
, isl_pw_aff_non_zero_set(skip
));
1727 skip
= indicator_function(cond
, dom
);
1728 isl_multi_pw_aff_free(ext
->skip
[type
]);
1729 ext
->skip
[type
] = isl_multi_pw_aff_from_pw_aff(skip
);
1730 if (!ext
->skip
[type
])
1731 return pet_scop_free(scop
);
1736 /* Add extra conditions on the parameters to all iteration domains
1737 * and skip conditions.
1739 * A parameter value is valid for the result if it was valid
1740 * for the original scop and satisfies "cond" or if it does
1741 * not satisfy "cond" as in this case the scop is not executed
1742 * and the original constraints on the parameters are irrelevant.
1744 struct pet_scop
*pet_scop_restrict(struct pet_scop
*scop
,
1745 __isl_take isl_set
*cond
)
1749 scop
= pet_scop_restrict_skip(scop
, pet_skip_now
, cond
);
1750 scop
= pet_scop_restrict_skip(scop
, pet_skip_later
, cond
);
1755 scop
->context
= isl_set_intersect(scop
->context
, isl_set_copy(cond
));
1756 scop
->context
= isl_set_union(scop
->context
,
1757 isl_set_complement(isl_set_copy(cond
)));
1758 scop
->context
= isl_set_coalesce(scop
->context
);
1759 scop
->context
= pet_nested_remove_from_set(scop
->context
);
1763 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
1764 scop
->stmts
[i
] = stmt_restrict(scop
->stmts
[i
],
1765 isl_set_copy(cond
));
1766 if (!scop
->stmts
[i
])
1774 return pet_scop_free(scop
);
1777 /* Insert an argument expression corresponding to "test" in front
1778 * of the list of arguments described by *n_arg and *args.
1780 static int args_insert_access(unsigned *n_arg
, pet_expr
***args
,
1781 __isl_keep isl_multi_pw_aff
*test
)
1784 isl_ctx
*ctx
= isl_multi_pw_aff_get_ctx(test
);
1790 *args
= isl_calloc_array(ctx
, pet_expr
*, 1);
1795 ext
= isl_calloc_array(ctx
, pet_expr
*, 1 + *n_arg
);
1798 for (i
= 0; i
< *n_arg
; ++i
)
1799 ext
[1 + i
] = (*args
)[i
];
1804 (*args
)[0] = pet_expr_from_index(isl_multi_pw_aff_copy(test
));
1811 /* Look through the applications in "scop" for any that can be
1812 * applied to the filter expressed by "map" and "satisified".
1813 * If there is any, then apply it to "map" and return the result.
1814 * Otherwise, return "map".
1815 * "id" is the identifier of the virtual array.
1817 * We only introduce at most one implication for any given virtual array,
1818 * so we can apply the implication and return as soon as we find one.
1820 static __isl_give isl_map
*apply_implications(struct pet_scop
*scop
,
1821 __isl_take isl_map
*map
, __isl_keep isl_id
*id
, int satisfied
)
1825 for (i
= 0; i
< scop
->n_implication
; ++i
) {
1826 struct pet_implication
*pi
= scop
->implications
[i
];
1829 if (pi
->satisfied
!= satisfied
)
1831 pi_id
= isl_map_get_tuple_id(pi
->extension
, isl_dim_in
);
1836 return isl_map_apply_range(map
, isl_map_copy(pi
->extension
));
1842 /* Is the filter expressed by "test" and "satisfied" implied
1843 * by filter "pos" on "domain", with filter "expr", taking into
1844 * account the implications of "scop"?
1846 * For filter on domain implying that expressed by "test" and "satisfied",
1847 * the filter needs to be an access to the same (virtual) array as "test" and
1848 * the filter value needs to be equal to "satisfied".
1849 * Moreover, the filter access relation, possibly extended by
1850 * the implications in "scop" needs to contain "test".
1852 static int implies_filter(struct pet_scop
*scop
,
1853 __isl_keep isl_map
*domain
, int pos
, __isl_keep pet_expr
*expr
,
1854 __isl_keep isl_map
*test
, int satisfied
)
1856 isl_id
*test_id
, *arg_id
;
1863 if (expr
->type
!= pet_expr_access
)
1865 test_id
= isl_map_get_tuple_id(test
, isl_dim_out
);
1866 arg_id
= pet_expr_access_get_id(expr
);
1867 isl_id_free(arg_id
);
1868 isl_id_free(test_id
);
1869 if (test_id
!= arg_id
)
1871 val
= isl_map_plain_get_val_if_fixed(domain
, isl_dim_out
, pos
);
1872 is_int
= isl_val_is_int(val
);
1874 s
= isl_val_get_num_si(val
);
1883 implied
= isl_map_copy(expr
->acc
.access
);
1884 implied
= apply_implications(scop
, implied
, test_id
, satisfied
);
1885 is_subset
= isl_map_is_subset(test
, implied
);
1886 isl_map_free(implied
);
1891 /* Is the filter expressed by "test" and "satisfied" implied
1892 * by any of the filters on the domain of "stmt", taking into
1893 * account the implications of "scop"?
1895 static int filter_implied(struct pet_scop
*scop
,
1896 struct pet_stmt
*stmt
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
1904 if (!scop
|| !stmt
|| !test
)
1906 if (scop
->n_implication
== 0)
1908 if (stmt
->n_arg
== 0)
1911 domain
= isl_set_unwrap(isl_set_copy(stmt
->domain
));
1912 test_map
= isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(test
));
1915 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
1916 implied
= implies_filter(scop
, domain
, i
, stmt
->args
[i
],
1917 test_map
, satisfied
);
1918 if (implied
< 0 || implied
)
1922 isl_map_free(test_map
);
1923 isl_map_free(domain
);
1927 /* Make the statement "stmt" depend on the value of "test"
1928 * being equal to "satisfied" by adjusting stmt->domain.
1930 * The domain of "test" corresponds to the (zero or more) outer dimensions
1931 * of the iteration domain.
1933 * We first extend "test" to apply to the entire iteration domain and
1934 * then check if the filter that we are about to add is implied
1935 * by any of the current filters, possibly taking into account
1936 * the implications in "scop". If so, we leave "stmt" untouched and return.
1938 * Otherwise, we insert an argument corresponding to a read to "test"
1939 * from the iteration domain of "stmt" in front of the list of arguments.
1940 * We also insert a corresponding output dimension in the wrapped
1941 * map contained in stmt->domain, with value set to "satisfied".
1943 static struct pet_stmt
*stmt_filter(struct pet_scop
*scop
,
1944 struct pet_stmt
*stmt
, __isl_take isl_multi_pw_aff
*test
, int satisfied
)
1950 isl_pw_multi_aff
*pma
;
1951 isl_multi_aff
*add_dom
;
1953 isl_local_space
*ls
;
1959 space
= pet_stmt_get_space(stmt
);
1960 n_test_dom
= isl_multi_pw_aff_dim(test
, isl_dim_in
);
1961 space
= isl_space_from_domain(space
);
1962 space
= isl_space_add_dims(space
, isl_dim_out
, n_test_dom
);
1963 add_dom
= isl_multi_aff_zero(isl_space_copy(space
));
1964 ls
= isl_local_space_from_space(isl_space_domain(space
));
1965 for (i
= 0; i
< n_test_dom
; ++i
) {
1967 aff
= isl_aff_var_on_domain(isl_local_space_copy(ls
),
1969 add_dom
= isl_multi_aff_set_aff(add_dom
, i
, aff
);
1971 isl_local_space_free(ls
);
1972 test
= isl_multi_pw_aff_pullback_multi_aff(test
, add_dom
);
1974 implied
= filter_implied(scop
, stmt
, test
, satisfied
);
1978 isl_multi_pw_aff_free(test
);
1982 id
= isl_multi_pw_aff_get_tuple_id(test
, isl_dim_out
);
1983 pma
= pet_filter_insert_pma(isl_set_get_space(stmt
->domain
),
1985 stmt
->domain
= isl_set_preimage_pw_multi_aff(stmt
->domain
, pma
);
1987 if (args_insert_access(&stmt
->n_arg
, &stmt
->args
, test
) < 0)
1990 isl_multi_pw_aff_free(test
);
1993 isl_multi_pw_aff_free(test
);
1994 return pet_stmt_free(stmt
);
1997 /* Does "scop" have a skip condition of the given "type"?
1999 int pet_scop_has_skip(struct pet_scop
*scop
, enum pet_skip type
)
2001 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2005 return ext
->skip
[type
] != NULL
;
2008 /* Does "scop" have a skip condition of the given "type" that
2009 * is an affine expression?
2011 int pet_scop_has_affine_skip(struct pet_scop
*scop
, enum pet_skip type
)
2013 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2017 if (!ext
->skip
[type
])
2019 return multi_pw_aff_is_affine(ext
->skip
[type
]);
2022 /* Does "scop" have a skip condition of the given "type" that
2023 * is not an affine expression?
2025 int pet_scop_has_var_skip(struct pet_scop
*scop
, enum pet_skip type
)
2027 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2032 if (!ext
->skip
[type
])
2034 aff
= multi_pw_aff_is_affine(ext
->skip
[type
]);
2040 /* Does "scop" have a skip condition of the given "type" that
2041 * is affine and holds on the entire domain?
2043 int pet_scop_has_universal_skip(struct pet_scop
*scop
, enum pet_skip type
)
2045 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2051 is_aff
= pet_scop_has_affine_skip(scop
, type
);
2052 if (is_aff
< 0 || !is_aff
)
2055 pa
= isl_multi_pw_aff_get_pw_aff(ext
->skip
[type
], 0);
2056 set
= isl_pw_aff_non_zero_set(pa
);
2057 is_univ
= isl_set_plain_is_universe(set
);
2063 /* Replace scop->skip[type] by "skip".
2065 struct pet_scop
*pet_scop_set_skip(struct pet_scop
*scop
,
2066 enum pet_skip type
, __isl_take isl_multi_pw_aff
*skip
)
2068 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2073 isl_multi_pw_aff_free(ext
->skip
[type
]);
2074 ext
->skip
[type
] = skip
;
2078 isl_multi_pw_aff_free(skip
);
2079 return pet_scop_free(scop
);
2082 /* Return a copy of scop->skip[type].
2084 __isl_give isl_multi_pw_aff
*pet_scop_get_skip(struct pet_scop
*scop
,
2087 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2092 return isl_multi_pw_aff_copy(ext
->skip
[type
]);
2095 /* Assuming scop->skip[type] is an affine expression,
2096 * return the constraints on the parameters for which the skip condition
2099 __isl_give isl_set
*pet_scop_get_affine_skip_domain(struct pet_scop
*scop
,
2102 isl_multi_pw_aff
*skip
;
2105 skip
= pet_scop_get_skip(scop
, type
);
2106 pa
= isl_multi_pw_aff_get_pw_aff(skip
, 0);
2107 isl_multi_pw_aff_free(skip
);
2108 return isl_set_params(isl_pw_aff_non_zero_set(pa
));
2111 /* Return the identifier of the variable that is accessed by
2112 * the skip condition of the given type.
2114 * The skip condition is assumed not to be an affine condition.
2116 __isl_give isl_id
*pet_scop_get_skip_id(struct pet_scop
*scop
,
2119 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2124 return isl_multi_pw_aff_get_tuple_id(ext
->skip
[type
], isl_dim_out
);
2127 /* Return an access pet_expr corresponding to the skip condition
2128 * of the given type.
2130 __isl_give pet_expr
*pet_scop_get_skip_expr(struct pet_scop
*scop
,
2133 return pet_expr_from_index(pet_scop_get_skip(scop
, type
));
2136 /* Drop the the skip condition scop->skip[type].
2138 void pet_scop_reset_skip(struct pet_scop
*scop
, enum pet_skip type
)
2140 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
2145 isl_multi_pw_aff_free(ext
->skip
[type
]);
2146 ext
->skip
[type
] = NULL
;
2149 /* Make the skip condition (if any) depend on the value of "test" being
2150 * equal to "satisfied".
2152 * We only support the case where the original skip condition is universal,
2153 * i.e., where skipping is unconditional, and where satisfied == 1.
2154 * In this case, the skip condition is changed to skip only when
2155 * "test" is equal to one.
2157 static struct pet_scop
*pet_scop_filter_skip(struct pet_scop
*scop
,
2158 enum pet_skip type
, __isl_keep isl_multi_pw_aff
*test
, int satisfied
)
2164 if (!pet_scop_has_skip(scop
, type
))
2168 is_univ
= pet_scop_has_universal_skip(scop
, type
);
2170 return pet_scop_free(scop
);
2171 if (satisfied
&& is_univ
) {
2172 isl_multi_pw_aff
*skip
;
2173 skip
= isl_multi_pw_aff_copy(test
);
2174 scop
= pet_scop_set_skip(scop
, type
, skip
);
2178 isl_die(isl_multi_pw_aff_get_ctx(test
), isl_error_internal
,
2179 "skip expression cannot be filtered",
2180 return pet_scop_free(scop
));
2186 /* Make all statements in "scop" depend on the value of "test"
2187 * being equal to "satisfied" by adjusting their domains.
2189 struct pet_scop
*pet_scop_filter(struct pet_scop
*scop
,
2190 __isl_take isl_multi_pw_aff
*test
, int satisfied
)
2194 scop
= pet_scop_filter_skip(scop
, pet_skip_now
, test
, satisfied
);
2195 scop
= pet_scop_filter_skip(scop
, pet_skip_later
, test
, satisfied
);
2200 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2201 scop
->stmts
[i
] = stmt_filter(scop
, scop
->stmts
[i
],
2202 isl_multi_pw_aff_copy(test
), satisfied
);
2203 if (!scop
->stmts
[i
])
2207 isl_multi_pw_aff_free(test
);
2210 isl_multi_pw_aff_free(test
);
2211 return pet_scop_free(scop
);
2214 /* Add all parameters in "expr" to "space" and return the result.
2216 static __isl_give isl_space
*expr_collect_params(__isl_keep pet_expr
*expr
,
2217 __isl_take isl_space
*space
)
2223 for (i
= 0; i
< expr
->n_arg
; ++i
)
2224 space
= expr_collect_params(expr
->args
[i
], space
);
2226 if (expr
->type
== pet_expr_access
)
2227 space
= isl_space_align_params(space
,
2228 isl_map_get_space(expr
->acc
.access
));
2232 pet_expr_free(expr
);
2233 return isl_space_free(space
);
2236 /* Add all parameters in "stmt" to "space" and return the result.
2238 static __isl_give isl_space
*stmt_collect_params(struct pet_stmt
*stmt
,
2239 __isl_take isl_space
*space
)
2244 return isl_space_free(space
);
2246 space
= isl_space_align_params(space
, isl_set_get_space(stmt
->domain
));
2247 space
= isl_space_align_params(space
,
2248 isl_map_get_space(stmt
->schedule
));
2249 for (i
= 0; i
< stmt
->n_arg
; ++i
)
2250 space
= expr_collect_params(stmt
->args
[i
], space
);
2251 space
= expr_collect_params(stmt
->body
, space
);
2256 /* Add all parameters in "array" to "space" and return the result.
2258 static __isl_give isl_space
*array_collect_params(struct pet_array
*array
,
2259 __isl_take isl_space
*space
)
2262 return isl_space_free(space
);
2264 space
= isl_space_align_params(space
,
2265 isl_set_get_space(array
->context
));
2266 space
= isl_space_align_params(space
, isl_set_get_space(array
->extent
));
2271 /* Add all parameters in "scop" to "space" and return the result.
2273 static __isl_give isl_space
*scop_collect_params(struct pet_scop
*scop
,
2274 __isl_take isl_space
*space
)
2279 return isl_space_free(space
);
2281 for (i
= 0; i
< scop
->n_array
; ++i
)
2282 space
= array_collect_params(scop
->arrays
[i
], space
);
2284 for (i
= 0; i
< scop
->n_stmt
; ++i
)
2285 space
= stmt_collect_params(scop
->stmts
[i
], space
);
2290 /* Add all parameters in "space" to the domain, schedule and
2291 * all access relations in "stmt".
2293 static struct pet_stmt
*stmt_propagate_params(struct pet_stmt
*stmt
,
2294 __isl_take isl_space
*space
)
2301 stmt
->domain
= isl_set_align_params(stmt
->domain
,
2302 isl_space_copy(space
));
2303 stmt
->schedule
= isl_map_align_params(stmt
->schedule
,
2304 isl_space_copy(space
));
2306 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2307 stmt
->args
[i
] = pet_expr_align_params(stmt
->args
[i
],
2308 isl_space_copy(space
));
2312 stmt
->body
= pet_expr_align_params(stmt
->body
, isl_space_copy(space
));
2314 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2317 isl_space_free(space
);
2320 isl_space_free(space
);
2321 return pet_stmt_free(stmt
);
2324 /* Add all parameters in "space" to "array".
2326 static struct pet_array
*array_propagate_params(struct pet_array
*array
,
2327 __isl_take isl_space
*space
)
2332 array
->context
= isl_set_align_params(array
->context
,
2333 isl_space_copy(space
));
2334 array
->extent
= isl_set_align_params(array
->extent
,
2335 isl_space_copy(space
));
2336 if (array
->value_bounds
) {
2337 array
->value_bounds
= isl_set_align_params(array
->value_bounds
,
2338 isl_space_copy(space
));
2339 if (!array
->value_bounds
)
2343 if (!array
->context
|| !array
->extent
)
2346 isl_space_free(space
);
2349 isl_space_free(space
);
2350 return pet_array_free(array
);
2353 /* Add all parameters in "space" to "scop".
2355 static struct pet_scop
*scop_propagate_params(struct pet_scop
*scop
,
2356 __isl_take isl_space
*space
)
2363 for (i
= 0; i
< scop
->n_array
; ++i
) {
2364 scop
->arrays
[i
] = array_propagate_params(scop
->arrays
[i
],
2365 isl_space_copy(space
));
2366 if (!scop
->arrays
[i
])
2370 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2371 scop
->stmts
[i
] = stmt_propagate_params(scop
->stmts
[i
],
2372 isl_space_copy(space
));
2373 if (!scop
->stmts
[i
])
2377 isl_space_free(space
);
2380 isl_space_free(space
);
2381 return pet_scop_free(scop
);
2384 /* Update all isl_sets and isl_maps in "scop" such that they all
2385 * have the same parameters.
2387 struct pet_scop
*pet_scop_align_params(struct pet_scop
*scop
)
2394 space
= isl_set_get_space(scop
->context
);
2395 space
= scop_collect_params(scop
, space
);
2397 scop
->context
= isl_set_align_params(scop
->context
,
2398 isl_space_copy(space
));
2399 scop
= scop_propagate_params(scop
, space
);
2401 if (scop
&& !scop
->context
)
2402 return pet_scop_free(scop
);
2407 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2408 * in "space" by a value equal to the corresponding parameter.
2410 static struct pet_stmt
*stmt_detect_parameter_accesses(struct pet_stmt
*stmt
,
2411 __isl_take isl_space
*space
)
2416 stmt
->body
= pet_expr_detect_parameter_accesses(stmt
->body
,
2417 isl_space_copy(space
));
2419 if (!stmt
->domain
|| !stmt
->schedule
|| !stmt
->body
)
2422 isl_space_free(space
);
2425 isl_space_free(space
);
2426 return pet_stmt_free(stmt
);
2429 /* Replace all accesses to (0D) arrays that correspond to one of the parameters
2430 * in "space" by a value equal to the corresponding parameter.
2432 static struct pet_scop
*scop_detect_parameter_accesses(struct pet_scop
*scop
,
2433 __isl_take isl_space
*space
)
2440 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2441 scop
->stmts
[i
] = stmt_detect_parameter_accesses(scop
->stmts
[i
],
2442 isl_space_copy(space
));
2443 if (!scop
->stmts
[i
])
2447 isl_space_free(space
);
2450 isl_space_free(space
);
2451 return pet_scop_free(scop
);
2454 /* Replace all accesses to (0D) arrays that correspond to any of
2455 * the parameters used in "scop" by a value equal
2456 * to the corresponding parameter.
2458 struct pet_scop
*pet_scop_detect_parameter_accesses(struct pet_scop
*scop
)
2465 space
= isl_set_get_space(scop
->context
);
2466 space
= scop_collect_params(scop
, space
);
2468 scop
= scop_detect_parameter_accesses(scop
, space
);
2473 /* Add the access relation of the access expression "expr" to "accesses" and
2474 * return the result.
2475 * The domain of the access relation is intersected with "domain".
2476 * If "tag" is set, then the access relation is tagged with
2477 * the corresponding reference identifier.
2479 static __isl_give isl_union_map
*expr_collect_access(__isl_keep pet_expr
*expr
,
2480 int tag
, __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
2484 access
= pet_expr_access_get_may_access(expr
);
2485 access
= isl_map_intersect_domain(access
, isl_set_copy(domain
));
2487 access
= pet_expr_tag_access(expr
, access
);
2488 return isl_union_map_add_map(accesses
, access
);
2491 /* Add all read access relations (if "read" is set) and/or all write
2492 * access relations (if "write" is set) to "accesses" and return the result.
2493 * The domains of the access relations are intersected with "domain".
2494 * If "tag" is set, then the access relations are tagged with
2495 * the corresponding reference identifiers.
2497 * If "must" is set, then we only add the accesses that are definitely
2498 * performed. Otherwise, we add all potential accesses.
2499 * In particular, if the access has any arguments, then if "must" is
2500 * set we currently skip the access completely. If "must" is not set,
2501 * we project out the values of the access arguments.
2503 static __isl_give isl_union_map
*expr_collect_accesses(
2504 __isl_keep pet_expr
*expr
, int read
, int write
, int must
, int tag
,
2505 __isl_take isl_union_map
*accesses
, __isl_keep isl_set
*domain
)
2512 return isl_union_map_free(accesses
);
2514 for (i
= 0; i
< expr
->n_arg
; ++i
)
2515 accesses
= expr_collect_accesses(expr
->args
[i
],
2516 read
, write
, must
, tag
, accesses
, domain
);
2518 if (expr
->type
== pet_expr_access
&& !pet_expr_is_affine(expr
) &&
2519 ((read
&& expr
->acc
.read
) || (write
&& expr
->acc
.write
)) &&
2520 (!must
|| expr
->n_arg
== 0)) {
2521 accesses
= expr_collect_access(expr
, tag
, accesses
, domain
);
2527 /* Collect and return all read access relations (if "read" is set)
2528 * and/or all write access relations (if "write" is set) in "stmt".
2529 * If "tag" is set, then the access relations are tagged with
2530 * the corresponding reference identifiers.
2531 * If "kill" is set, then "stmt" is a kill statement and we simply
2532 * add the argument of the kill operation.
2534 * If "must" is set, then we only add the accesses that are definitely
2535 * performed. Otherwise, we add all potential accesses.
2536 * In particular, if the statement has any arguments, then if "must" is
2537 * set we currently skip the statement completely. If "must" is not set,
2538 * we project out the values of the statement arguments.
2540 static __isl_give isl_union_map
*stmt_collect_accesses(struct pet_stmt
*stmt
,
2541 int read
, int write
, int kill
, int must
, int tag
,
2542 __isl_take isl_space
*dim
)
2544 isl_union_map
*accesses
;
2550 accesses
= isl_union_map_empty(dim
);
2552 if (must
&& stmt
->n_arg
> 0)
2555 domain
= isl_set_copy(stmt
->domain
);
2556 if (isl_set_is_wrapping(domain
))
2557 domain
= isl_map_domain(isl_set_unwrap(domain
));
2560 accesses
= expr_collect_access(stmt
->body
->args
[0], tag
,
2563 accesses
= expr_collect_accesses(stmt
->body
, read
, write
,
2564 must
, tag
, accesses
, domain
);
2565 isl_set_free(domain
);
2570 /* Is "stmt" an assignment statement?
2572 int pet_stmt_is_assign(struct pet_stmt
*stmt
)
2576 if (stmt
->body
->type
!= pet_expr_op
)
2578 return stmt
->body
->op
== pet_op_assign
;
2581 /* Is "stmt" a kill statement?
2583 int pet_stmt_is_kill(struct pet_stmt
*stmt
)
2587 if (stmt
->body
->type
!= pet_expr_op
)
2589 return stmt
->body
->op
== pet_op_kill
;
2592 /* Is "stmt" an assume statement?
2594 int pet_stmt_is_assume(struct pet_stmt
*stmt
)
2598 return pet_expr_is_assume(stmt
->body
);
2601 /* Compute a mapping from all arrays (of structs) in scop
2602 * to their innermost arrays.
2604 * In particular, for each array of a primitive type, the result
2605 * contains the identity mapping on that array.
2606 * For each array involving member accesses, the result
2607 * contains a mapping from the elements of any intermediate array of structs
2608 * to all corresponding elements of the innermost nested arrays.
2610 static __isl_give isl_union_map
*compute_to_inner(struct pet_scop
*scop
)
2613 isl_union_map
*to_inner
;
2615 to_inner
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2617 for (i
= 0; i
< scop
->n_array
; ++i
) {
2618 struct pet_array
*array
= scop
->arrays
[i
];
2620 isl_map
*map
, *gist
;
2622 if (array
->element_is_record
)
2625 map
= isl_set_identity(isl_set_copy(array
->extent
));
2627 set
= isl_map_domain(isl_map_copy(map
));
2628 gist
= isl_map_copy(map
);
2629 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2630 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2632 while (set
&& isl_set_is_wrapping(set
)) {
2636 id
= isl_set_get_tuple_id(set
);
2637 wrapped
= isl_set_unwrap(set
);
2638 wrapped
= isl_map_domain_map(wrapped
);
2639 wrapped
= isl_map_set_tuple_id(wrapped
, isl_dim_in
, id
);
2640 map
= isl_map_apply_domain(map
, wrapped
);
2641 set
= isl_map_domain(isl_map_copy(map
));
2642 gist
= isl_map_copy(map
);
2643 gist
= isl_map_gist_domain(gist
, isl_set_copy(set
));
2644 to_inner
= isl_union_map_add_map(to_inner
, gist
);
2654 /* Collect and return all read access relations (if "read" is set)
2655 * and/or all write access relations (if "write" is set) in "scop".
2656 * If "kill" is set, then we only add the arguments of kill operations.
2657 * If "must" is set, then we only add the accesses that are definitely
2658 * performed. Otherwise, we add all potential accesses.
2659 * If "tag" is set, then the access relations are tagged with
2660 * the corresponding reference identifiers.
2661 * For accesses to structures, the returned access relation accesses
2662 * all individual fields in the structures.
2664 static __isl_give isl_union_map
*scop_collect_accesses(struct pet_scop
*scop
,
2665 int read
, int write
, int kill
, int must
, int tag
)
2668 isl_union_map
*accesses
;
2669 isl_union_set
*arrays
;
2670 isl_union_map
*to_inner
;
2675 accesses
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2677 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2678 struct pet_stmt
*stmt
= scop
->stmts
[i
];
2679 isl_union_map
*accesses_i
;
2682 if (kill
&& !pet_stmt_is_kill(stmt
))
2685 space
= isl_set_get_space(scop
->context
);
2686 accesses_i
= stmt_collect_accesses(stmt
, read
, write
, kill
,
2688 accesses
= isl_union_map_union(accesses
, accesses_i
);
2691 arrays
= isl_union_set_empty(isl_union_map_get_space(accesses
));
2692 for (i
= 0; i
< scop
->n_array
; ++i
) {
2693 isl_set
*extent
= isl_set_copy(scop
->arrays
[i
]->extent
);
2694 arrays
= isl_union_set_add_set(arrays
, extent
);
2696 accesses
= isl_union_map_intersect_range(accesses
, arrays
);
2698 to_inner
= compute_to_inner(scop
);
2699 accesses
= isl_union_map_apply_range(accesses
, to_inner
);
2704 /* Collect all potential read access relations.
2706 __isl_give isl_union_map
*pet_scop_collect_may_reads(struct pet_scop
*scop
)
2708 return scop_collect_accesses(scop
, 1, 0, 0, 0, 0);
2711 /* Collect all potential write access relations.
2713 __isl_give isl_union_map
*pet_scop_collect_may_writes(struct pet_scop
*scop
)
2715 return scop_collect_accesses(scop
, 0, 1, 0, 0, 0);
2718 /* Collect all definite write access relations.
2720 __isl_give isl_union_map
*pet_scop_collect_must_writes(struct pet_scop
*scop
)
2722 return scop_collect_accesses(scop
, 0, 1, 0, 1, 0);
2725 /* Collect all definite kill access relations.
2727 __isl_give isl_union_map
*pet_scop_collect_must_kills(struct pet_scop
*scop
)
2729 return scop_collect_accesses(scop
, 0, 0, 1, 1, 0);
2732 /* Collect all tagged potential read access relations.
2734 __isl_give isl_union_map
*pet_scop_collect_tagged_may_reads(
2735 struct pet_scop
*scop
)
2737 return scop_collect_accesses(scop
, 1, 0, 0, 0, 1);
2740 /* Collect all tagged potential write access relations.
2742 __isl_give isl_union_map
*pet_scop_collect_tagged_may_writes(
2743 struct pet_scop
*scop
)
2745 return scop_collect_accesses(scop
, 0, 1, 0, 0, 1);
2748 /* Collect all tagged definite write access relations.
2750 __isl_give isl_union_map
*pet_scop_collect_tagged_must_writes(
2751 struct pet_scop
*scop
)
2753 return scop_collect_accesses(scop
, 0, 1, 0, 1, 1);
2756 /* Collect all tagged definite kill access relations.
2758 __isl_give isl_union_map
*pet_scop_collect_tagged_must_kills(
2759 struct pet_scop
*scop
)
2761 return scop_collect_accesses(scop
, 0, 0, 1, 1, 1);
2764 /* Collect and return the union of iteration domains in "scop".
2766 __isl_give isl_union_set
*pet_scop_collect_domains(struct pet_scop
*scop
)
2770 isl_union_set
*domain
;
2775 domain
= isl_union_set_empty(isl_set_get_space(scop
->context
));
2777 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2778 domain_i
= isl_set_copy(scop
->stmts
[i
]->domain
);
2779 domain
= isl_union_set_add_set(domain
, domain_i
);
2785 /* Collect and return the schedules of the statements in "scop".
2786 * The range is normalized to the maximal number of scheduling
2789 __isl_give isl_union_map
*pet_scop_collect_schedule(struct pet_scop
*scop
)
2792 isl_map
*schedule_i
;
2793 isl_union_map
*schedule
;
2794 int depth
, max_depth
= 0;
2799 schedule
= isl_union_map_empty(isl_set_get_space(scop
->context
));
2801 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2802 depth
= isl_map_dim(scop
->stmts
[i
]->schedule
, isl_dim_out
);
2803 if (depth
> max_depth
)
2807 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2808 schedule_i
= isl_map_copy(scop
->stmts
[i
]->schedule
);
2809 depth
= isl_map_dim(schedule_i
, isl_dim_out
);
2810 schedule_i
= isl_map_add_dims(schedule_i
, isl_dim_out
,
2812 for (j
= depth
; j
< max_depth
; ++j
)
2813 schedule_i
= isl_map_fix_si(schedule_i
,
2815 schedule
= isl_union_map_add_map(schedule
, schedule_i
);
2821 /* Add a reference identifier to all access expressions in "stmt".
2822 * "n_ref" points to an integer that contains the sequence number
2823 * of the next reference.
2825 static struct pet_stmt
*stmt_add_ref_ids(struct pet_stmt
*stmt
, int *n_ref
)
2832 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2833 stmt
->args
[i
] = pet_expr_add_ref_ids(stmt
->args
[i
], n_ref
);
2835 return pet_stmt_free(stmt
);
2838 stmt
->body
= pet_expr_add_ref_ids(stmt
->body
, n_ref
);
2840 return pet_stmt_free(stmt
);
2845 /* Add a reference identifier to all access expressions in "scop".
2847 struct pet_scop
*pet_scop_add_ref_ids(struct pet_scop
*scop
)
2856 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2857 scop
->stmts
[i
] = stmt_add_ref_ids(scop
->stmts
[i
], &n_ref
);
2858 if (!scop
->stmts
[i
])
2859 return pet_scop_free(scop
);
2865 /* Reset the user pointer on all parameter ids in "array".
2867 static struct pet_array
*array_anonymize(struct pet_array
*array
)
2872 array
->context
= isl_set_reset_user(array
->context
);
2873 array
->extent
= isl_set_reset_user(array
->extent
);
2874 if (!array
->context
|| !array
->extent
)
2875 return pet_array_free(array
);
2880 /* Reset the user pointer on all parameter and tuple ids in "stmt".
2882 static struct pet_stmt
*stmt_anonymize(struct pet_stmt
*stmt
)
2891 stmt
->domain
= isl_set_reset_user(stmt
->domain
);
2892 stmt
->schedule
= isl_map_reset_user(stmt
->schedule
);
2893 if (!stmt
->domain
|| !stmt
->schedule
)
2894 return pet_stmt_free(stmt
);
2896 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2897 stmt
->args
[i
] = pet_expr_anonymize(stmt
->args
[i
]);
2899 return pet_stmt_free(stmt
);
2902 stmt
->body
= pet_expr_anonymize(stmt
->body
);
2904 return pet_stmt_free(stmt
);
2909 /* Reset the user pointer on the tuple ids and all parameter ids
2912 static struct pet_implication
*implication_anonymize(
2913 struct pet_implication
*implication
)
2918 implication
->extension
= isl_map_reset_user(implication
->extension
);
2919 if (!implication
->extension
)
2920 return pet_implication_free(implication
);
2925 /* Reset the user pointer on all parameter and tuple ids in "scop".
2927 struct pet_scop
*pet_scop_anonymize(struct pet_scop
*scop
)
2934 scop
->context
= isl_set_reset_user(scop
->context
);
2935 scop
->context_value
= isl_set_reset_user(scop
->context_value
);
2936 if (!scop
->context
|| !scop
->context_value
)
2937 return pet_scop_free(scop
);
2939 for (i
= 0; i
< scop
->n_array
; ++i
) {
2940 scop
->arrays
[i
] = array_anonymize(scop
->arrays
[i
]);
2941 if (!scop
->arrays
[i
])
2942 return pet_scop_free(scop
);
2945 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
2946 scop
->stmts
[i
] = stmt_anonymize(scop
->stmts
[i
]);
2947 if (!scop
->stmts
[i
])
2948 return pet_scop_free(scop
);
2951 for (i
= 0; i
< scop
->n_implication
; ++i
) {
2952 scop
->implications
[i
] =
2953 implication_anonymize(scop
->implications
[i
]);
2954 if (!scop
->implications
[i
])
2955 return pet_scop_free(scop
);
2961 /* Compute the gist of the iteration domain and all access relations
2962 * of "stmt" based on the constraints on the parameters specified by "context"
2963 * and the constraints on the values of nested accesses specified
2964 * by "value_bounds".
2966 static struct pet_stmt
*stmt_gist(struct pet_stmt
*stmt
,
2967 __isl_keep isl_set
*context
, __isl_keep isl_union_map
*value_bounds
)
2975 domain
= isl_set_copy(stmt
->domain
);
2976 if (stmt
->n_arg
> 0)
2977 domain
= isl_map_domain(isl_set_unwrap(domain
));
2979 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2981 for (i
= 0; i
< stmt
->n_arg
; ++i
) {
2982 stmt
->args
[i
] = pet_expr_gist(stmt
->args
[i
],
2983 domain
, value_bounds
);
2988 stmt
->body
= pet_expr_gist(stmt
->body
, domain
, value_bounds
);
2992 isl_set_free(domain
);
2994 domain
= isl_set_universe(pet_stmt_get_space(stmt
));
2995 domain
= isl_set_intersect_params(domain
, isl_set_copy(context
));
2996 if (stmt
->n_arg
> 0)
2997 domain
= pet_value_bounds_apply(domain
, stmt
->n_arg
, stmt
->args
,
2999 stmt
->domain
= isl_set_gist(stmt
->domain
, domain
);
3001 return pet_stmt_free(stmt
);
3005 isl_set_free(domain
);
3006 return pet_stmt_free(stmt
);
3009 /* Compute the gist of the extent of the array
3010 * based on the constraints on the parameters specified by "context".
3012 static struct pet_array
*array_gist(struct pet_array
*array
,
3013 __isl_keep isl_set
*context
)
3018 array
->extent
= isl_set_gist_params(array
->extent
,
3019 isl_set_copy(context
));
3021 return pet_array_free(array
);
3026 /* Compute the gist of all sets and relations in "scop"
3027 * based on the constraints on the parameters specified by "scop->context"
3028 * and the constraints on the values of nested accesses specified
3029 * by "value_bounds".
3031 struct pet_scop
*pet_scop_gist(struct pet_scop
*scop
,
3032 __isl_keep isl_union_map
*value_bounds
)
3039 scop
->context
= isl_set_coalesce(scop
->context
);
3041 return pet_scop_free(scop
);
3043 for (i
= 0; i
< scop
->n_array
; ++i
) {
3044 scop
->arrays
[i
] = array_gist(scop
->arrays
[i
], scop
->context
);
3045 if (!scop
->arrays
[i
])
3046 return pet_scop_free(scop
);
3049 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3050 scop
->stmts
[i
] = stmt_gist(scop
->stmts
[i
], scop
->context
,
3052 if (!scop
->stmts
[i
])
3053 return pet_scop_free(scop
);
3059 /* Intersect the context of "scop" with "context".
3060 * To ensure that we don't introduce any unnamed parameters in
3061 * the context of "scop", we first remove the unnamed parameters
3064 struct pet_scop
*pet_scop_restrict_context(struct pet_scop
*scop
,
3065 __isl_take isl_set
*context
)
3070 context
= pet_nested_remove_from_set(context
);
3071 scop
->context
= isl_set_intersect(scop
->context
, context
);
3073 return pet_scop_free(scop
);
3077 isl_set_free(context
);
3078 return pet_scop_free(scop
);
3081 /* Drop the current context of "scop". That is, replace the context
3082 * by a universal set.
3084 struct pet_scop
*pet_scop_reset_context(struct pet_scop
*scop
)
3091 space
= isl_set_get_space(scop
->context
);
3092 isl_set_free(scop
->context
);
3093 scop
->context
= isl_set_universe(space
);
3095 return pet_scop_free(scop
);
3100 /* Append "array" to the arrays of "scop".
3102 struct pet_scop
*pet_scop_add_array(struct pet_scop
*scop
,
3103 struct pet_array
*array
)
3106 struct pet_array
**arrays
;
3108 if (!array
|| !scop
)
3111 ctx
= isl_set_get_ctx(scop
->context
);
3112 arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3116 scop
->arrays
= arrays
;
3117 scop
->arrays
[scop
->n_array
] = array
;
3122 pet_array_free(array
);
3123 return pet_scop_free(scop
);
3126 /* Create an index expression for an access to a virtual array
3127 * representing the result of a condition.
3128 * Unlike other accessed data, the id of the array is NULL as
3129 * there is no ValueDecl in the program corresponding to the virtual
3131 * The array starts out as a scalar, but grows along with the
3132 * statement writing to the array in pet_scop_embed.
3134 __isl_give isl_multi_pw_aff
*pet_create_test_index(isl_ctx
*ctx
, int test_nr
)
3136 isl_space
*dim
= isl_space_alloc(ctx
, 0, 0, 0);
3140 snprintf(name
, sizeof(name
), "__pet_test_%d", test_nr
);
3141 id
= isl_id_alloc(ctx
, name
, NULL
);
3142 dim
= isl_space_set_tuple_id(dim
, isl_dim_out
, id
);
3143 return isl_multi_pw_aff_zero(dim
);
3146 /* Add an array with the given extent (range of "index") to the list
3147 * of arrays in "scop" and return the extended pet_scop.
3148 * "int_size" is the number of bytes needed to represent values of type "int".
3149 * The array is marked as attaining values 0 and 1 only and
3150 * as each element being assigned at most once.
3152 struct pet_scop
*pet_scop_add_boolean_array(struct pet_scop
*scop
,
3153 __isl_take isl_multi_pw_aff
*index
, int int_size
)
3157 struct pet_array
*array
;
3160 if (!scop
|| !index
)
3163 ctx
= isl_multi_pw_aff_get_ctx(index
);
3164 array
= isl_calloc_type(ctx
, struct pet_array
);
3168 access
= isl_map_from_multi_pw_aff(index
);
3169 array
->extent
= isl_map_range(access
);
3170 space
= isl_space_params_alloc(ctx
, 0);
3171 array
->context
= isl_set_universe(space
);
3172 space
= isl_space_set_alloc(ctx
, 0, 1);
3173 array
->value_bounds
= isl_set_universe(space
);
3174 array
->value_bounds
= isl_set_lower_bound_si(array
->value_bounds
,
3176 array
->value_bounds
= isl_set_upper_bound_si(array
->value_bounds
,
3178 array
->element_type
= strdup("int");
3179 array
->element_size
= int_size
;
3180 array
->uniquely_defined
= 1;
3182 if (!array
->extent
|| !array
->context
)
3183 array
= pet_array_free(array
);
3185 scop
= pet_scop_add_array(scop
, array
);
3189 isl_multi_pw_aff_free(index
);
3190 return pet_scop_free(scop
);
3193 /* Create and return an implication on filter values equal to "satisfied"
3194 * with extension "map".
3196 static struct pet_implication
*new_implication(__isl_take isl_map
*map
,
3200 struct pet_implication
*implication
;
3204 ctx
= isl_map_get_ctx(map
);
3205 implication
= isl_alloc_type(ctx
, struct pet_implication
);
3209 implication
->extension
= map
;
3210 implication
->satisfied
= satisfied
;
3218 /* Add an implication on filter values equal to "satisfied"
3219 * with extension "map" to "scop".
3221 struct pet_scop
*pet_scop_add_implication(struct pet_scop
*scop
,
3222 __isl_take isl_map
*map
, int satisfied
)
3225 struct pet_implication
*implication
;
3226 struct pet_implication
**implications
;
3228 implication
= new_implication(map
, satisfied
);
3229 if (!scop
|| !implication
)
3232 ctx
= isl_set_get_ctx(scop
->context
);
3233 implications
= isl_realloc_array(ctx
, scop
->implications
,
3234 struct pet_implication
*,
3235 scop
->n_implication
+ 1);
3238 scop
->implications
= implications
;
3239 scop
->implications
[scop
->n_implication
] = implication
;
3240 scop
->n_implication
++;
3244 pet_implication_free(implication
);
3245 return pet_scop_free(scop
);
3248 /* Given an access expression, check if it is data dependent.
3249 * If so, set *found and abort the search.
3251 static int is_data_dependent(__isl_keep pet_expr
*expr
, void *user
)
3255 if (pet_expr_get_n_arg(expr
) > 0) {
3263 /* Does "scop" contain any data dependent accesses?
3265 * Check the body of each statement for such accesses.
3267 int pet_scop_has_data_dependent_accesses(struct pet_scop
*scop
)
3275 for (i
= 0; i
< scop
->n_stmt
; ++i
) {
3276 int r
= pet_expr_foreach_access_expr(scop
->stmts
[i
]->body
,
3277 &is_data_dependent
, &found
);
3278 if (r
< 0 && !found
)
3287 /* Does "scop" contain and data dependent conditions?
3289 int pet_scop_has_data_dependent_conditions(struct pet_scop
*scop
)
3296 for (i
= 0; i
< scop
->n_stmt
; ++i
)
3297 if (scop
->stmts
[i
]->n_arg
> 0)
3303 /* Keep track of the "input" file inside the (extended) "scop".
3305 struct pet_scop
*pet_scop_set_input_file(struct pet_scop
*scop
, FILE *input
)
3307 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3317 /* Print the original code corresponding to "scop" to printer "p".
3319 * pet_scop_print_original can only be called from
3320 * a pet_transform_C_source callback. This means that the input
3321 * file is stored in the extended scop and that the printer prints
3324 __isl_give isl_printer
*pet_scop_print_original(struct pet_scop
*scop
,
3325 __isl_take isl_printer
*p
)
3327 struct pet_scop_ext
*ext
= (struct pet_scop_ext
*) scop
;
3329 unsigned start
, end
;
3332 return isl_printer_free(p
);
3335 isl_die(isl_printer_get_ctx(p
), isl_error_invalid
,
3336 "no input file stored in scop",
3337 return isl_printer_free(p
));
3339 output
= isl_printer_get_file(p
);
3341 return isl_printer_free(p
);
3343 start
= pet_loc_get_start(scop
->loc
);
3344 end
= pet_loc_get_end(scop
->loc
);
3345 if (copy(ext
->input
, output
, start
, end
) < 0)
3346 return isl_printer_free(p
);