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
39 #include <llvm/Support/raw_ostream.h>
40 #include <clang/AST/ASTContext.h>
41 #include <clang/AST/ASTDiagnostic.h>
42 #include <clang/AST/Expr.h>
43 #include <clang/AST/RecursiveASTVisitor.h>
46 #include <isl/space.h>
60 #include "scop_plus.h"
66 using namespace clang
;
68 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
78 return pet_op_post_inc
;
80 return pet_op_post_dec
;
82 return pet_op_pre_inc
;
84 return pet_op_pre_dec
;
90 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
94 return pet_op_add_assign
;
96 return pet_op_sub_assign
;
98 return pet_op_mul_assign
;
100 return pet_op_div_assign
;
102 return pet_op_assign
;
144 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
145 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
147 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
148 SourceLocation(), var
, false, var
->getInnerLocStart(),
149 var
->getType(), VK_LValue
);
151 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
152 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
154 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
155 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
159 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
161 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
162 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
166 /* Check if the element type corresponding to the given array type
167 * has a const qualifier.
169 static bool const_base(QualType qt
)
171 const Type
*type
= qt
.getTypePtr();
173 if (type
->isPointerType())
174 return const_base(type
->getPointeeType());
175 if (type
->isArrayType()) {
176 const ArrayType
*atype
;
177 type
= type
->getCanonicalTypeInternal().getTypePtr();
178 atype
= cast
<ArrayType
>(type
);
179 return const_base(atype
->getElementType());
182 return qt
.isConstQualified();
185 /* Create an isl_id that refers to the named declarator "decl".
187 static __isl_give isl_id
*create_decl_id(isl_ctx
*ctx
, NamedDecl
*decl
)
189 return isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
192 /* Look for any assignments to scalar variables in part of the parse
193 * tree and mark them as having an unknown value in "pc".
194 * If the address of a scalar variable is being taken, then mark
195 * it as having an unknown value as well. As an exception,
196 * if the address is passed to a function
197 * that is declared to receive a const pointer, then "pc" is not updated.
199 * This ensures that we won't use any previously stored value
200 * in the current subtree and its parents.
202 struct clear_assignments
: RecursiveASTVisitor
<clear_assignments
> {
205 set
<UnaryOperator
*> skip
;
207 clear_assignments(pet_context
*&pc
) : pc(pc
),
208 ctx(pet_context_get_ctx(pc
)) {}
210 /* Check for "address of" operators whose value is passed
211 * to a const pointer argument and add them to "skip", so that
212 * we can skip them in VisitUnaryOperator.
214 bool VisitCallExpr(CallExpr
*expr
) {
216 fd
= expr
->getDirectCallee();
219 for (int i
= 0; i
< expr
->getNumArgs(); ++i
) {
220 Expr
*arg
= expr
->getArg(i
);
222 if (arg
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
223 ImplicitCastExpr
*ice
;
224 ice
= cast
<ImplicitCastExpr
>(arg
);
225 arg
= ice
->getSubExpr();
227 if (arg
->getStmtClass() != Stmt::UnaryOperatorClass
)
229 op
= cast
<UnaryOperator
>(arg
);
230 if (op
->getOpcode() != UO_AddrOf
)
232 if (const_base(fd
->getParamDecl(i
)->getType()))
238 bool VisitUnaryOperator(UnaryOperator
*expr
) {
243 switch (expr
->getOpcode()) {
253 if (skip
.find(expr
) != skip
.end())
256 arg
= expr
->getSubExpr();
257 if (arg
->getStmtClass() != Stmt::DeclRefExprClass
)
259 ref
= cast
<DeclRefExpr
>(arg
);
260 decl
= ref
->getDecl();
261 pc
= pet_context_mark_assigned(pc
, create_decl_id(ctx
, decl
));
265 bool VisitBinaryOperator(BinaryOperator
*expr
) {
270 if (!expr
->isAssignmentOp())
272 lhs
= expr
->getLHS();
273 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
)
275 ref
= cast
<DeclRefExpr
>(lhs
);
276 decl
= ref
->getDecl();
277 pc
= pet_context_mark_assigned(pc
, create_decl_id(ctx
, decl
));
284 isl_union_map_free(value_bounds
);
287 /* Report a diagnostic, unless autodetect is set.
289 void PetScan::report(Stmt
*stmt
, unsigned id
)
291 if (options
->autodetect
)
294 SourceLocation loc
= stmt
->getLocStart();
295 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
296 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
299 /* Called if we found something we (currently) cannot handle.
300 * We'll provide more informative warnings later.
302 * We only actually complain if autodetect is false.
304 void PetScan::unsupported(Stmt
*stmt
)
306 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
307 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
312 /* Report a missing prototype, unless autodetect is set.
314 void PetScan::report_prototype_required(Stmt
*stmt
)
316 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
317 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
318 "prototype required");
322 /* Report a missing increment, unless autodetect is set.
324 void PetScan::report_missing_increment(Stmt
*stmt
)
326 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
327 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
328 "missing increment");
332 /* Extract an integer from "expr".
334 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
336 const Type
*type
= expr
->getType().getTypePtr();
337 int is_signed
= type
->hasSignedIntegerRepresentation();
338 llvm::APInt val
= expr
->getValue();
339 int is_negative
= is_signed
&& val
.isNegative();
345 v
= extract_unsigned(ctx
, val
);
352 /* Extract an integer from "val", which is assumed to be non-negative.
354 __isl_give isl_val
*PetScan::extract_unsigned(isl_ctx
*ctx
,
355 const llvm::APInt
&val
)
358 const uint64_t *data
;
360 data
= val
.getRawData();
361 n
= val
.getNumWords();
362 return isl_val_int_from_chunks(ctx
, n
, sizeof(uint64_t), data
);
365 /* Extract an integer from "expr".
366 * Return NULL if "expr" does not (obviously) represent an integer.
368 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
370 return extract_int(expr
->getSubExpr());
373 /* Extract an integer from "expr".
374 * Return NULL if "expr" does not (obviously) represent an integer.
376 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
378 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
379 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
380 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
381 return extract_int(cast
<ParenExpr
>(expr
));
387 /* Extract a pet_expr from the APInt "val", which is assumed
388 * to be non-negative.
390 __isl_give pet_expr
*PetScan::extract_expr(const llvm::APInt
&val
)
392 return pet_expr_new_int(extract_unsigned(ctx
, val
));
395 /* Return the number of bits needed to represent the type "qt",
396 * if it is an integer type. Otherwise return 0.
397 * If qt is signed then return the opposite of the number of bits.
399 static int get_type_size(QualType qt
, ASTContext
&ast_context
)
403 if (!qt
->isIntegerType())
406 size
= ast_context
.getIntWidth(qt
);
407 if (!qt
->isUnsignedIntegerType())
413 /* Return the number of bits needed to represent the type of "decl",
414 * if it is an integer type. Otherwise return 0.
415 * If qt is signed then return the opposite of the number of bits.
417 static int get_type_size(ValueDecl
*decl
)
419 return get_type_size(decl
->getType(), decl
->getASTContext());
422 /* Bound parameter "pos" of "set" to the possible values of "decl".
424 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
425 unsigned pos
, ValueDecl
*decl
)
431 ctx
= isl_set_get_ctx(set
);
432 type_size
= get_type_size(decl
);
434 isl_die(ctx
, isl_error_invalid
, "not an integer type",
435 return isl_set_free(set
));
437 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
438 bound
= isl_val_int_from_ui(ctx
, type_size
);
439 bound
= isl_val_2exp(bound
);
440 bound
= isl_val_sub_ui(bound
, 1);
441 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
443 bound
= isl_val_int_from_ui(ctx
, -type_size
- 1);
444 bound
= isl_val_2exp(bound
);
445 bound
= isl_val_sub_ui(bound
, 1);
446 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
447 isl_val_copy(bound
));
448 bound
= isl_val_neg(bound
);
449 bound
= isl_val_sub_ui(bound
, 1);
450 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
456 /* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
458 static __isl_give isl_pw_aff
*indicator_function(__isl_take isl_set
*set
,
459 __isl_take isl_set
*dom
)
462 pa
= isl_set_indicator_function(set
);
463 pa
= isl_pw_aff_intersect_domain(pa
, isl_set_coalesce(dom
));
467 __isl_give pet_expr
*PetScan::extract_index_expr(ImplicitCastExpr
*expr
)
469 return extract_index_expr(expr
->getSubExpr());
472 /* Return the depth of an array of the given type.
474 static int array_depth(const Type
*type
)
476 if (type
->isPointerType())
477 return 1 + array_depth(type
->getPointeeType().getTypePtr());
478 if (type
->isArrayType()) {
479 const ArrayType
*atype
;
480 type
= type
->getCanonicalTypeInternal().getTypePtr();
481 atype
= cast
<ArrayType
>(type
);
482 return 1 + array_depth(atype
->getElementType().getTypePtr());
487 /* Return the depth of the array accessed by the index expression "index".
488 * If "index" is an affine expression, i.e., if it does not access
489 * any array, then return 1.
490 * If "index" represent a member access, i.e., if its range is a wrapped
491 * relation, then return the sum of the depth of the array of structures
492 * and that of the member inside the structure.
494 static int extract_depth(__isl_keep isl_multi_pw_aff
*index
)
502 if (isl_multi_pw_aff_range_is_wrapping(index
)) {
503 int domain_depth
, range_depth
;
504 isl_multi_pw_aff
*domain
, *range
;
506 domain
= isl_multi_pw_aff_copy(index
);
507 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
508 domain_depth
= extract_depth(domain
);
509 isl_multi_pw_aff_free(domain
);
510 range
= isl_multi_pw_aff_copy(index
);
511 range
= isl_multi_pw_aff_range_factor_range(range
);
512 range_depth
= extract_depth(range
);
513 isl_multi_pw_aff_free(range
);
515 return domain_depth
+ range_depth
;
518 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
521 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
524 decl
= (ValueDecl
*) isl_id_get_user(id
);
527 return array_depth(decl
->getType().getTypePtr());
530 /* Return the depth of the array accessed by the access expression "expr".
532 static int extract_depth(__isl_keep pet_expr
*expr
)
534 isl_multi_pw_aff
*index
;
537 index
= pet_expr_access_get_index(expr
);
538 depth
= extract_depth(index
);
539 isl_multi_pw_aff_free(index
);
544 /* Construct a pet_expr representing an index expression for an access
545 * to the variable referenced by "expr".
547 __isl_give pet_expr
*PetScan::extract_index_expr(DeclRefExpr
*expr
)
549 return extract_index_expr(expr
->getDecl());
552 /* Construct a pet_expr representing an index expression for an access
553 * to the variable "decl".
555 __isl_give pet_expr
*PetScan::extract_index_expr(ValueDecl
*decl
)
557 isl_id
*id
= create_decl_id(ctx
, decl
);
558 isl_space
*space
= isl_space_alloc(ctx
, 0, 0, 0);
560 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
562 return pet_expr_from_index(isl_multi_pw_aff_zero(space
));
565 /* Construct a pet_expr representing the index expression "expr"
566 * Return NULL on error.
568 __isl_give pet_expr
*PetScan::extract_index_expr(Expr
*expr
)
570 switch (expr
->getStmtClass()) {
571 case Stmt::ImplicitCastExprClass
:
572 return extract_index_expr(cast
<ImplicitCastExpr
>(expr
));
573 case Stmt::DeclRefExprClass
:
574 return extract_index_expr(cast
<DeclRefExpr
>(expr
));
575 case Stmt::ArraySubscriptExprClass
:
576 return extract_index_expr(cast
<ArraySubscriptExpr
>(expr
));
577 case Stmt::IntegerLiteralClass
:
578 return extract_expr(cast
<IntegerLiteral
>(expr
));
579 case Stmt::MemberExprClass
:
580 return extract_index_expr(cast
<MemberExpr
>(expr
));
587 /* Extract an index expression from the given array subscript expression.
589 * We first extract an index expression from the base.
590 * This will result in an index expression with a range that corresponds
591 * to the earlier indices.
592 * We then extract the current index and let
593 * pet_expr_access_subscript combine the two.
595 __isl_give pet_expr
*PetScan::extract_index_expr(ArraySubscriptExpr
*expr
)
597 Expr
*base
= expr
->getBase();
598 Expr
*idx
= expr
->getIdx();
602 base_expr
= extract_index_expr(base
);
603 index
= extract_expr(idx
);
605 base_expr
= pet_expr_access_subscript(base_expr
, index
);
610 /* Extract an index expression from a member expression.
612 * If the base access (to the structure containing the member)
617 * and the member is called "f", then the member access is of
622 * If the member access is to an anonymous struct, then simply return
626 * If the member access in the source code is of the form
630 * then it is treated as
634 __isl_give pet_expr
*PetScan::extract_index_expr(MemberExpr
*expr
)
636 Expr
*base
= expr
->getBase();
637 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
638 pet_expr
*base_index
;
641 base_index
= extract_index_expr(base
);
643 if (expr
->isArrow()) {
644 pet_expr
*index
= pet_expr_new_int(isl_val_zero(ctx
));
645 base_index
= pet_expr_access_subscript(base_index
, index
);
648 if (field
->isAnonymousStructOrUnion())
651 id
= create_decl_id(ctx
, field
);
653 return pet_expr_access_member(base_index
, id
);
656 /* Extract an affine expression from a boolean expression
657 * within the context "pc".
658 * In particular, return the expression "expr ? 1 : 0".
659 * Return NULL if we are unable to extract an affine expression.
661 * We first convert the clang::Expr to a pet_expr and
662 * then extract an affine expression from that pet_expr.
664 __isl_give isl_pw_aff
*PetScan::extract_condition(Expr
*expr
,
665 __isl_keep pet_context
*pc
)
671 isl_set
*u
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
672 return indicator_function(u
, isl_set_copy(u
));
675 pe
= extract_expr(expr
);
676 pe
= pet_expr_plug_in_args(pe
, pc
);
677 pc
= pet_context_copy(pc
);
678 pc
= pet_context_set_allow_nested(pc
, nesting_enabled
);
679 cond
= pet_expr_extract_affine_condition(pe
, pc
);
680 if (isl_pw_aff_involves_nan(cond
))
681 cond
= isl_pw_aff_free(cond
);
682 pet_context_free(pc
);
687 /* Mark the given access pet_expr as a write.
689 static __isl_give pet_expr
*mark_write(__isl_take pet_expr
*access
)
691 access
= pet_expr_access_set_write(access
, 1);
692 access
= pet_expr_access_set_read(access
, 0);
697 /* Construct a pet_expr representing a unary operator expression.
699 __isl_give pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
704 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
705 if (op
== pet_op_last
) {
710 arg
= extract_expr(expr
->getSubExpr());
712 if (expr
->isIncrementDecrementOp() &&
713 pet_expr_get_type(arg
) == pet_expr_access
) {
714 arg
= mark_write(arg
);
715 arg
= pet_expr_access_set_read(arg
, 1);
718 return pet_expr_new_unary(op
, arg
);
721 /* If the access expression "expr" writes to a (non-virtual) scalar,
722 * then mark the scalar as having an unknown value in "pc".
724 static int clear_write(__isl_keep pet_expr
*expr
, void *user
)
727 pet_context
**pc
= (pet_context
**) user
;
729 if (!pet_expr_access_is_write(expr
))
731 if (!pet_expr_is_scalar_access(expr
))
734 id
= pet_expr_access_get_id(expr
);
735 if (isl_id_get_user(id
))
736 *pc
= pet_context_mark_assigned(*pc
, id
);
743 /* Update "pc" by taking into account the writes in "stmt".
744 * That is, first mark all scalar variables that are written by "stmt"
745 * as having an unknown value. Afterwards,
746 * if "stmt" is a top-level (i.e., unconditional) assignment
747 * to a scalar variable, then update "pc" accordingly.
749 * In particular, if the lhs of the assignment is a scalar variable, then mark
750 * the variable as having been assigned. If, furthermore, the rhs
751 * is an affine expression, then keep track of this value in "pc"
752 * so that we can plug it in when we later come across the same variable.
754 * We skip assignments to virtual arrays (those with NULL user pointer).
756 __isl_give pet_context
*PetScan::handle_writes(struct pet_stmt
*stmt
,
757 __isl_take pet_context
*pc
)
759 pet_expr
*body
= stmt
->body
;
764 if (pet_expr_foreach_access_expr(body
, &clear_write
, &pc
) < 0)
765 return pet_context_free(pc
);
767 if (!pet_stmt_is_assign(stmt
))
769 if (!isl_set_plain_is_universe(stmt
->domain
))
771 arg
= pet_expr_get_arg(body
, 0);
772 if (!pet_expr_is_scalar_access(arg
)) {
777 id
= pet_expr_access_get_id(arg
);
780 if (!isl_id_get_user(id
)) {
785 arg
= pet_expr_get_arg(body
, 1);
786 pa
= pet_expr_extract_affine(arg
, pc
);
787 pc
= pet_context_mark_assigned(pc
, isl_id_copy(id
));
790 if (isl_pw_aff_involves_nan(pa
))
791 pa
= isl_pw_aff_free(pa
);
797 pc
= pet_context_set_value(pc
, id
, pa
);
802 /* Update "pc" based on the write accesses (and, in particular,
803 * assignments) in "scop".
805 __isl_give pet_context
*PetScan::handle_writes(struct pet_scop
*scop
,
806 __isl_take pet_context
*pc
)
809 return pet_context_free(pc
);
810 for (int i
= 0; i
< scop
->n_stmt
; ++i
)
811 pc
= handle_writes(scop
->stmts
[i
], pc
);
816 /* Construct a pet_expr representing a binary operator expression.
818 * If the top level operator is an assignment and the LHS is an access,
819 * then we mark that access as a write. If the operator is a compound
820 * assignment, the access is marked as both a read and a write.
822 __isl_give pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
828 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
829 if (op
== pet_op_last
) {
834 lhs
= extract_expr(expr
->getLHS());
835 rhs
= extract_expr(expr
->getRHS());
837 if (expr
->isAssignmentOp() &&
838 pet_expr_get_type(lhs
) == pet_expr_access
) {
839 lhs
= mark_write(lhs
);
840 if (expr
->isCompoundAssignmentOp())
841 lhs
= pet_expr_access_set_read(lhs
, 1);
844 type_size
= get_type_size(expr
->getType(), ast_context
);
845 return pet_expr_new_binary(type_size
, op
, lhs
, rhs
);
848 /* Construct a pet_scop with a single statement killing the entire
849 * array "array" within the context "pc".
851 struct pet_scop
*PetScan::kill(Stmt
*stmt
, struct pet_array
*array
,
852 __isl_keep pet_context
*pc
)
856 isl_multi_pw_aff
*index
;
862 access
= isl_map_from_range(isl_set_copy(array
->extent
));
863 id
= isl_set_get_tuple_id(array
->extent
);
864 space
= isl_space_alloc(ctx
, 0, 0, 0);
865 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
866 index
= isl_multi_pw_aff_zero(space
);
867 expr
= pet_expr_kill_from_access_and_index(access
, index
);
868 return extract(expr
, stmt
->getSourceRange(), false, pc
);
871 /* Construct a pet_scop for a (single) variable declaration
872 * within the context "pc".
874 * The scop contains the variable being declared (as an array)
875 * and a statement killing the array.
877 * If the variable is initialized in the AST, then the scop
878 * also contains an assignment to the variable.
880 struct pet_scop
*PetScan::extract(DeclStmt
*stmt
, __isl_keep pet_context
*pc
)
885 pet_expr
*lhs
, *rhs
, *pe
;
886 struct pet_scop
*scop_decl
, *scop
;
887 struct pet_array
*array
;
889 if (!stmt
->isSingleDecl()) {
894 decl
= stmt
->getSingleDecl();
895 vd
= cast
<VarDecl
>(decl
);
897 array
= extract_array(ctx
, vd
, NULL
, pc
);
900 scop_decl
= kill(stmt
, array
, pc
);
901 scop_decl
= pet_scop_add_array(scop_decl
, array
);
906 lhs
= extract_access_expr(vd
);
907 rhs
= extract_expr(vd
->getInit());
909 lhs
= mark_write(lhs
);
911 type_size
= get_type_size(vd
->getType(), ast_context
);
912 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, lhs
, rhs
);
913 scop
= extract(pe
, stmt
->getSourceRange(), false, pc
);
915 scop_decl
= pet_scop_prefix(scop_decl
, 0);
916 scop
= pet_scop_prefix(scop
, 1);
918 scop
= pet_scop_add_seq(ctx
, scop_decl
, scop
);
923 /* Construct a pet_expr representing a conditional operation.
925 __isl_give pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
927 pet_expr
*cond
, *lhs
, *rhs
;
930 cond
= extract_expr(expr
->getCond());
931 lhs
= extract_expr(expr
->getTrueExpr());
932 rhs
= extract_expr(expr
->getFalseExpr());
934 return pet_expr_new_ternary(cond
, lhs
, rhs
);
937 __isl_give pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
939 return extract_expr(expr
->getSubExpr());
942 /* Construct a pet_expr representing a floating point value.
944 * If the floating point literal does not appear in a macro,
945 * then we use the original representation in the source code
946 * as the string representation. Otherwise, we use the pretty
947 * printer to produce a string representation.
949 __isl_give pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
953 const LangOptions
&LO
= PP
.getLangOpts();
954 SourceLocation loc
= expr
->getLocation();
956 if (!loc
.isMacroID()) {
957 SourceManager
&SM
= PP
.getSourceManager();
958 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
959 s
= string(SM
.getCharacterData(loc
), len
);
961 llvm::raw_string_ostream
S(s
);
962 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
965 d
= expr
->getValueAsApproximateDouble();
966 return pet_expr_new_double(ctx
, d
, s
.c_str());
969 /* Convert the index expression "index" into an access pet_expr of type "qt".
971 __isl_give pet_expr
*PetScan::extract_access_expr(QualType qt
,
972 __isl_take pet_expr
*index
)
977 depth
= extract_depth(index
);
978 type_size
= get_type_size(qt
, ast_context
);
980 index
= pet_expr_set_type_size(index
, type_size
);
981 index
= pet_expr_access_set_depth(index
, depth
);
986 /* Extract an index expression from "expr" and then convert it into
987 * an access pet_expr.
989 __isl_give pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
991 return extract_access_expr(expr
->getType(), extract_index_expr(expr
));
994 /* Extract an index expression from "decl" and then convert it into
995 * an access pet_expr.
997 __isl_give pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
999 return extract_access_expr(decl
->getType(), extract_index_expr(decl
));
1002 __isl_give pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
1004 return extract_expr(expr
->getSubExpr());
1007 /* Extract an assume statement from the argument "expr"
1008 * of a __pencil_assume statement.
1010 __isl_give pet_expr
*PetScan::extract_assume(Expr
*expr
)
1012 return pet_expr_new_unary(pet_op_assume
, extract_expr(expr
));
1015 /* Construct a pet_expr corresponding to the function call argument "expr".
1016 * The argument appears in position "pos" of a call to function "fd".
1018 * If we are passing along a pointer to an array element
1019 * or an entire row or even higher dimensional slice of an array,
1020 * then the function being called may write into the array.
1022 * We assume here that if the function is declared to take a pointer
1023 * to a const type, then the function will perform a read
1024 * and that otherwise, it will perform a write.
1026 __isl_give pet_expr
*PetScan::extract_argument(FunctionDecl
*fd
, int pos
,
1030 int is_addr
= 0, is_partial
= 0;
1033 if (expr
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
1034 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(expr
);
1035 expr
= ice
->getSubExpr();
1037 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
) {
1038 UnaryOperator
*op
= cast
<UnaryOperator
>(expr
);
1039 if (op
->getOpcode() == UO_AddrOf
) {
1041 expr
= op
->getSubExpr();
1044 res
= extract_expr(expr
);
1047 sc
= expr
->getStmtClass();
1048 if ((sc
== Stmt::ArraySubscriptExprClass
||
1049 sc
== Stmt::MemberExprClass
) &&
1050 array_depth(expr
->getType().getTypePtr()) > 0)
1052 if ((is_addr
|| is_partial
) &&
1053 pet_expr_get_type(res
) == pet_expr_access
) {
1055 if (!fd
->hasPrototype()) {
1056 report_prototype_required(expr
);
1057 return pet_expr_free(res
);
1059 parm
= fd
->getParamDecl(pos
);
1060 if (!const_base(parm
->getType()))
1061 res
= mark_write(res
);
1065 res
= pet_expr_new_unary(pet_op_address_of
, res
);
1069 /* Construct a pet_expr representing a function call.
1071 * In the special case of a "call" to __pencil_assume,
1072 * construct an assume expression instead.
1074 __isl_give pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1076 pet_expr
*res
= NULL
;
1081 fd
= expr
->getDirectCallee();
1087 name
= fd
->getDeclName().getAsString();
1088 n_arg
= expr
->getNumArgs();
1090 if (n_arg
== 1 && name
== "__pencil_assume")
1091 return extract_assume(expr
->getArg(0));
1093 res
= pet_expr_new_call(ctx
, name
.c_str(), n_arg
);
1097 for (int i
= 0; i
< n_arg
; ++i
) {
1098 Expr
*arg
= expr
->getArg(i
);
1099 res
= pet_expr_set_arg(res
, i
,
1100 PetScan::extract_argument(fd
, i
, arg
));
1106 /* Construct a pet_expr representing a (C style) cast.
1108 __isl_give pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1113 arg
= extract_expr(expr
->getSubExpr());
1117 type
= expr
->getTypeAsWritten();
1118 return pet_expr_new_cast(type
.getAsString().c_str(), arg
);
1121 /* Construct a pet_expr representing an integer.
1123 __isl_give pet_expr
*PetScan::extract_expr(IntegerLiteral
*expr
)
1125 return pet_expr_new_int(extract_int(expr
));
1128 /* Try and construct a pet_expr representing "expr".
1130 __isl_give pet_expr
*PetScan::extract_expr(Expr
*expr
)
1132 switch (expr
->getStmtClass()) {
1133 case Stmt::UnaryOperatorClass
:
1134 return extract_expr(cast
<UnaryOperator
>(expr
));
1135 case Stmt::CompoundAssignOperatorClass
:
1136 case Stmt::BinaryOperatorClass
:
1137 return extract_expr(cast
<BinaryOperator
>(expr
));
1138 case Stmt::ImplicitCastExprClass
:
1139 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1140 case Stmt::ArraySubscriptExprClass
:
1141 case Stmt::DeclRefExprClass
:
1142 case Stmt::MemberExprClass
:
1143 return extract_access_expr(expr
);
1144 case Stmt::IntegerLiteralClass
:
1145 return extract_expr(cast
<IntegerLiteral
>(expr
));
1146 case Stmt::FloatingLiteralClass
:
1147 return extract_expr(cast
<FloatingLiteral
>(expr
));
1148 case Stmt::ParenExprClass
:
1149 return extract_expr(cast
<ParenExpr
>(expr
));
1150 case Stmt::ConditionalOperatorClass
:
1151 return extract_expr(cast
<ConditionalOperator
>(expr
));
1152 case Stmt::CallExprClass
:
1153 return extract_expr(cast
<CallExpr
>(expr
));
1154 case Stmt::CStyleCastExprClass
:
1155 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1162 /* Check if the given initialization statement is an assignment.
1163 * If so, return that assignment. Otherwise return NULL.
1165 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1167 BinaryOperator
*ass
;
1169 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1172 ass
= cast
<BinaryOperator
>(init
);
1173 if (ass
->getOpcode() != BO_Assign
)
1179 /* Check if the given initialization statement is a declaration
1180 * of a single variable.
1181 * If so, return that declaration. Otherwise return NULL.
1183 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1187 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1190 decl
= cast
<DeclStmt
>(init
);
1192 if (!decl
->isSingleDecl())
1195 return decl
->getSingleDecl();
1198 /* Given the assignment operator in the initialization of a for loop,
1199 * extract the induction variable, i.e., the (integer)variable being
1202 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1209 lhs
= init
->getLHS();
1210 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1215 ref
= cast
<DeclRefExpr
>(lhs
);
1216 decl
= ref
->getDecl();
1217 type
= decl
->getType().getTypePtr();
1219 if (!type
->isIntegerType()) {
1227 /* Given the initialization statement of a for loop and the single
1228 * declaration in this initialization statement,
1229 * extract the induction variable, i.e., the (integer) variable being
1232 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1236 vd
= cast
<VarDecl
>(decl
);
1238 const QualType type
= vd
->getType();
1239 if (!type
->isIntegerType()) {
1244 if (!vd
->getInit()) {
1252 /* Check that op is of the form iv++ or iv--.
1253 * Return a pet_expr representing "1" or "-1" accordingly.
1255 __isl_give pet_expr
*PetScan::extract_unary_increment(
1256 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1262 if (!op
->isIncrementDecrementOp()) {
1267 sub
= op
->getSubExpr();
1268 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1273 ref
= cast
<DeclRefExpr
>(sub
);
1274 if (ref
->getDecl() != iv
) {
1279 if (op
->isIncrementOp())
1280 v
= isl_val_one(ctx
);
1282 v
= isl_val_negone(ctx
);
1284 return pet_expr_new_int(v
);
1287 /* Check if op is of the form
1291 * and return the increment "expr - iv" as a pet_expr.
1293 __isl_give pet_expr
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1294 clang::ValueDecl
*iv
)
1299 pet_expr
*expr
, *expr_iv
;
1301 if (op
->getOpcode() != BO_Assign
) {
1307 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1312 ref
= cast
<DeclRefExpr
>(lhs
);
1313 if (ref
->getDecl() != iv
) {
1318 expr
= extract_expr(op
->getRHS());
1319 expr_iv
= extract_expr(lhs
);
1321 type_size
= get_type_size(iv
->getType(), ast_context
);
1322 return pet_expr_new_binary(type_size
, pet_op_sub
, expr
, expr_iv
);
1325 /* Check that op is of the form iv += cst or iv -= cst
1326 * and return a pet_expr corresponding to cst or -cst accordingly.
1328 __isl_give pet_expr
*PetScan::extract_compound_increment(
1329 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1335 BinaryOperatorKind opcode
;
1337 opcode
= op
->getOpcode();
1338 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1342 if (opcode
== BO_SubAssign
)
1346 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1351 ref
= cast
<DeclRefExpr
>(lhs
);
1352 if (ref
->getDecl() != iv
) {
1357 expr
= extract_expr(op
->getRHS());
1359 expr
= pet_expr_new_unary(pet_op_minus
, expr
);
1364 /* Check that the increment of the given for loop increments
1365 * (or decrements) the induction variable "iv" and return
1366 * the increment as a pet_expr if successful.
1368 __isl_give pet_expr
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1371 Stmt
*inc
= stmt
->getInc();
1374 report_missing_increment(stmt
);
1378 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1379 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1380 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1381 return extract_compound_increment(
1382 cast
<CompoundAssignOperator
>(inc
), iv
);
1383 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1384 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1390 /* Embed the given iteration domain in an extra outer loop
1391 * with induction variable "var".
1392 * If this variable appeared as a parameter in the constraints,
1393 * it is replaced by the new outermost dimension.
1395 static __isl_give isl_set
*embed(__isl_take isl_set
*set
,
1396 __isl_take isl_id
*var
)
1400 set
= isl_set_insert_dims(set
, isl_dim_set
, 0, 1);
1401 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, var
);
1403 set
= isl_set_equate(set
, isl_dim_param
, pos
, isl_dim_set
, 0);
1404 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
1411 /* Return those elements in the space of "cond" that come after
1412 * (based on "sign") an element in "cond".
1414 static __isl_give isl_set
*after(__isl_take isl_set
*cond
, int sign
)
1416 isl_map
*previous_to_this
;
1419 previous_to_this
= isl_map_lex_lt(isl_set_get_space(cond
));
1421 previous_to_this
= isl_map_lex_gt(isl_set_get_space(cond
));
1423 cond
= isl_set_apply(cond
, previous_to_this
);
1428 /* Create the infinite iteration domain
1430 * { [id] : id >= 0 }
1432 * If "scop" has an affine skip of type pet_skip_later,
1433 * then remove those iterations i that have an earlier iteration
1434 * where the skip condition is satisfied, meaning that iteration i
1436 * Since we are dealing with a loop without loop iterator,
1437 * the skip condition cannot refer to the current loop iterator and
1438 * so effectively, the returned set is of the form
1440 * { [0]; [id] : id >= 1 and not skip }
1442 static __isl_give isl_set
*infinite_domain(__isl_take isl_id
*id
,
1443 struct pet_scop
*scop
)
1445 isl_ctx
*ctx
= isl_id_get_ctx(id
);
1449 domain
= isl_set_nat_universe(isl_space_set_alloc(ctx
, 0, 1));
1450 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, id
);
1452 if (!pet_scop_has_affine_skip(scop
, pet_skip_later
))
1455 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
1456 skip
= embed(skip
, isl_id_copy(id
));
1457 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
1458 domain
= isl_set_subtract(domain
, after(skip
, 1));
1463 /* Create an identity affine expression on the space containing "domain",
1464 * which is assumed to be one-dimensional.
1466 static __isl_give isl_aff
*identity_aff(__isl_keep isl_set
*domain
)
1468 isl_local_space
*ls
;
1470 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
1471 return isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1474 /* Create an affine expression that maps elements
1475 * of a single-dimensional array "id_test" to the previous element
1476 * (according to "inc"), provided this element belongs to "domain".
1477 * That is, create the affine expression
1479 * { id[x] -> id[x - inc] : x - inc in domain }
1481 static __isl_give isl_multi_pw_aff
*map_to_previous(__isl_take isl_id
*id_test
,
1482 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
1485 isl_local_space
*ls
;
1487 isl_multi_pw_aff
*prev
;
1489 space
= isl_set_get_space(domain
);
1490 ls
= isl_local_space_from_space(space
);
1491 aff
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
1492 aff
= isl_aff_add_constant_val(aff
, isl_val_neg(inc
));
1493 prev
= isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
1494 domain
= isl_set_preimage_multi_pw_aff(domain
,
1495 isl_multi_pw_aff_copy(prev
));
1496 prev
= isl_multi_pw_aff_intersect_domain(prev
, domain
);
1497 prev
= isl_multi_pw_aff_set_tuple_id(prev
, isl_dim_out
, id_test
);
1502 /* Add an implication to "scop" expressing that if an element of
1503 * virtual array "id_test" has value "satisfied" then all previous elements
1504 * of this array also have that value. The set of previous elements
1505 * is bounded by "domain". If "sign" is negative then the iterator
1506 * is decreasing and we express that all subsequent array elements
1507 * (but still defined previously) have the same value.
1509 static struct pet_scop
*add_implication(struct pet_scop
*scop
,
1510 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
, int sign
,
1516 domain
= isl_set_set_tuple_id(domain
, id_test
);
1517 space
= isl_set_get_space(domain
);
1519 map
= isl_map_lex_ge(space
);
1521 map
= isl_map_lex_le(space
);
1522 map
= isl_map_intersect_range(map
, domain
);
1523 scop
= pet_scop_add_implication(scop
, map
, satisfied
);
1528 /* Add a filter to "scop" that imposes that it is only executed
1529 * when the variable identified by "id_test" has a zero value
1530 * for all previous iterations of "domain".
1532 * In particular, add a filter that imposes that the array
1533 * has a zero value at the previous iteration of domain and
1534 * add an implication that implies that it then has that
1535 * value for all previous iterations.
1537 static struct pet_scop
*scop_add_break(struct pet_scop
*scop
,
1538 __isl_take isl_id
*id_test
, __isl_take isl_set
*domain
,
1539 __isl_take isl_val
*inc
)
1541 isl_multi_pw_aff
*prev
;
1542 int sign
= isl_val_sgn(inc
);
1544 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
1545 scop
= add_implication(scop
, id_test
, domain
, sign
, 0);
1546 scop
= pet_scop_filter(scop
, prev
, 0);
1551 /* Construct a pet_scop for an infinite loop around the given body
1552 * within the context "pc".
1554 * We extract a pet_scop for the body and then embed it in a loop with
1563 * If the body contains any break, then it is taken into
1564 * account in infinite_domain (if the skip condition is affine)
1565 * or in scop_add_break (if the skip condition is not affine).
1567 * If we were only able to extract part of the body, then simply
1570 struct pet_scop
*PetScan::extract_infinite_loop(Stmt
*body
,
1571 __isl_keep pet_context
*pc
)
1573 isl_id
*id
, *id_test
;
1576 struct pet_scop
*scop
;
1579 scop
= extract(body
, pc
);
1585 id
= isl_id_alloc(ctx
, "t", NULL
);
1586 domain
= infinite_domain(isl_id_copy(id
), scop
);
1587 ident
= identity_aff(domain
);
1589 has_var_break
= pet_scop_has_var_skip(scop
, pet_skip_later
);
1591 id_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
1593 scop
= pet_scop_embed(scop
, isl_set_copy(domain
),
1594 isl_aff_copy(ident
), ident
, id
);
1596 scop
= scop_add_break(scop
, id_test
, domain
, isl_val_one(ctx
));
1598 isl_set_free(domain
);
1603 /* Construct a pet_scop for an infinite loop, i.e., a loop of the form
1608 * within the context "pc".
1610 struct pet_scop
*PetScan::extract_infinite_for(ForStmt
*stmt
,
1611 __isl_keep pet_context
*pc
)
1613 struct pet_scop
*scop
;
1615 pc
= pet_context_copy(pc
);
1616 clear_assignments
clear(pc
);
1617 clear
.TraverseStmt(stmt
->getBody());
1619 scop
= extract_infinite_loop(stmt
->getBody(), pc
);
1620 pet_context_free(pc
);
1624 /* Add an array with the given extent (range of "index") to the list
1625 * of arrays in "scop" and return the extended pet_scop.
1626 * The array is marked as attaining values 0 and 1 only and
1627 * as each element being assigned at most once.
1629 static struct pet_scop
*scop_add_array(struct pet_scop
*scop
,
1630 __isl_keep isl_multi_pw_aff
*index
, clang::ASTContext
&ast_ctx
)
1632 int int_size
= ast_ctx
.getTypeInfo(ast_ctx
.IntTy
).first
/ 8;
1634 return pet_scop_add_boolean_array(scop
, isl_multi_pw_aff_copy(index
),
1638 /* Construct a pet_scop for a while loop of the form
1643 * within the context "pc".
1644 * In particular, construct a scop for an infinite loop around body and
1645 * intersect the domain with the affine expression.
1646 * Note that this intersection may result in an empty loop.
1648 struct pet_scop
*PetScan::extract_affine_while(__isl_take isl_pw_aff
*pa
,
1649 Stmt
*body
, __isl_take pet_context
*pc
)
1651 struct pet_scop
*scop
;
1655 valid
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
1656 dom
= isl_pw_aff_non_zero_set(pa
);
1657 scop
= extract_infinite_loop(body
, pc
);
1658 scop
= pet_scop_restrict(scop
, isl_set_params(dom
));
1659 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
1661 pet_context_free(pc
);
1665 /* Construct a scop for a while, given the scops for the condition
1666 * and the body, the filter identifier and the iteration domain of
1669 * In particular, the scop for the condition is filtered to depend
1670 * on "id_test" evaluating to true for all previous iterations
1671 * of the loop, while the scop for the body is filtered to depend
1672 * on "id_test" evaluating to true for all iterations up to the
1673 * current iteration.
1674 * The actual filter only imposes that this virtual array has
1675 * value one on the previous or the current iteration.
1676 * The fact that this condition also applies to the previous
1677 * iterations is enforced by an implication.
1679 * These filtered scops are then combined into a single scop.
1681 * "sign" is positive if the iterator increases and negative
1684 static struct pet_scop
*scop_add_while(struct pet_scop
*scop_cond
,
1685 struct pet_scop
*scop_body
, __isl_take isl_id
*id_test
,
1686 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
1688 isl_ctx
*ctx
= isl_set_get_ctx(domain
);
1690 isl_multi_pw_aff
*test_index
;
1691 isl_multi_pw_aff
*prev
;
1692 int sign
= isl_val_sgn(inc
);
1693 struct pet_scop
*scop
;
1695 prev
= map_to_previous(isl_id_copy(id_test
), isl_set_copy(domain
), inc
);
1696 scop_cond
= pet_scop_filter(scop_cond
, prev
, 1);
1698 space
= isl_space_map_from_set(isl_set_get_space(domain
));
1699 test_index
= isl_multi_pw_aff_identity(space
);
1700 test_index
= isl_multi_pw_aff_set_tuple_id(test_index
, isl_dim_out
,
1701 isl_id_copy(id_test
));
1702 scop_body
= pet_scop_filter(scop_body
, test_index
, 1);
1704 scop
= pet_scop_add_seq(ctx
, scop_cond
, scop_body
);
1705 scop
= add_implication(scop
, id_test
, domain
, sign
, 1);
1710 /* Check if the while loop is of the form
1712 * while (affine expression)
1715 * If so, call extract_affine_while to construct a scop.
1717 * Otherwise, extract the body and pass control to extract_while
1718 * to extend the iteration domain with an infinite loop.
1719 * If we were only able to extract part of the body, then simply
1722 * "pc" is the context in which the affine expressions in the scop are created.
1724 struct pet_scop
*PetScan::extract(WhileStmt
*stmt
, __isl_keep pet_context
*pc
)
1727 int test_nr
, stmt_nr
;
1729 struct pet_scop
*scop
, *scop_body
;
1731 cond
= stmt
->getCond();
1737 pc
= pet_context_copy(pc
);
1738 clear_assignments
clear(pc
);
1739 clear
.TraverseStmt(stmt
->getBody());
1741 pa
= extract_condition(cond
, pc
);
1743 return extract_affine_while(pa
, stmt
->getBody(), pc
);
1745 if (!allow_nested
) {
1747 pet_context_free(pc
);
1753 scop_body
= extract(stmt
->getBody(), pc
);
1755 pet_context_free(pc
);
1759 return extract_while(cond
, test_nr
, stmt_nr
, scop_body
, NULL
, pc
);
1762 /* Construct a generic while scop, with iteration domain
1763 * { [t] : t >= 0 } around "scop_body" within the context "pc".
1764 * The scop consists of two parts,
1765 * one for evaluating the condition "cond" and one for the body.
1766 * "test_nr" is the sequence number of the virtual test variable that contains
1767 * the result of the condition and "stmt_nr" is the sequence number
1768 * of the statement that evaluates the condition.
1769 * If "scop_inc" is not NULL, then it is added at the end of the body,
1770 * after replacing any skip conditions resulting from continue statements
1771 * by the skip conditions resulting from break statements (if any).
1773 * The schedule is adjusted to reflect that the condition is evaluated
1774 * before the body is executed and the body is filtered to depend
1775 * on the result of the condition evaluating to true on all iterations
1776 * up to the current iteration, while the evaluation of the condition itself
1777 * is filtered to depend on the result of the condition evaluating to true
1778 * on all previous iterations.
1779 * The context of the scop representing the body is dropped
1780 * because we don't know how many times the body will be executed,
1783 * If the body contains any break, then it is taken into
1784 * account in infinite_domain (if the skip condition is affine)
1785 * or in scop_add_break (if the skip condition is not affine).
1787 struct pet_scop
*PetScan::extract_while(Expr
*cond
, int test_nr
, int stmt_nr
,
1788 struct pet_scop
*scop_body
, struct pet_scop
*scop_inc
,
1789 __isl_take pet_context
*pc
)
1791 isl_id
*id
, *id_test
, *id_break_test
;
1794 isl_multi_pw_aff
*test_index
;
1795 struct pet_scop
*scop
;
1798 test_index
= pet_create_test_index(ctx
, test_nr
);
1799 scop
= extract_non_affine_condition(cond
, stmt_nr
,
1800 isl_multi_pw_aff_copy(test_index
), pc
);
1801 scop
= scop_add_array(scop
, test_index
, ast_context
);
1802 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
, isl_dim_out
);
1803 isl_multi_pw_aff_free(test_index
);
1805 id
= isl_id_alloc(ctx
, "t", NULL
);
1806 domain
= infinite_domain(isl_id_copy(id
), scop_body
);
1807 ident
= identity_aff(domain
);
1809 has_var_break
= pet_scop_has_var_skip(scop_body
, pet_skip_later
);
1811 id_break_test
= pet_scop_get_skip_id(scop_body
, pet_skip_later
);
1813 scop
= pet_scop_prefix(scop
, 0);
1814 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), isl_aff_copy(ident
),
1815 isl_aff_copy(ident
), isl_id_copy(id
));
1816 scop_body
= pet_scop_reset_context(scop_body
);
1817 scop_body
= pet_scop_prefix(scop_body
, 1);
1819 scop_inc
= pet_scop_prefix(scop_inc
, 2);
1820 if (pet_scop_has_skip(scop_body
, pet_skip_later
)) {
1821 isl_multi_pw_aff
*skip
;
1822 skip
= pet_scop_get_skip(scop_body
, pet_skip_later
);
1823 scop_body
= pet_scop_set_skip(scop_body
,
1824 pet_skip_now
, skip
);
1826 pet_scop_reset_skip(scop_body
, pet_skip_now
);
1827 scop_body
= pet_scop_add_seq(ctx
, scop_body
, scop_inc
);
1829 scop_body
= pet_scop_embed(scop_body
, isl_set_copy(domain
),
1830 isl_aff_copy(ident
), ident
, id
);
1832 if (has_var_break
) {
1833 scop
= scop_add_break(scop
, isl_id_copy(id_break_test
),
1834 isl_set_copy(domain
), isl_val_one(ctx
));
1835 scop_body
= scop_add_break(scop_body
, id_break_test
,
1836 isl_set_copy(domain
), isl_val_one(ctx
));
1838 scop
= scop_add_while(scop
, scop_body
, id_test
, domain
,
1841 pet_context_free(pc
);
1845 /* Check whether "cond" expresses a simple loop bound
1846 * on the only set dimension.
1847 * In particular, if "up" is set then "cond" should contain only
1848 * upper bounds on the set dimension.
1849 * Otherwise, it should contain only lower bounds.
1851 static bool is_simple_bound(__isl_keep isl_set
*cond
, __isl_keep isl_val
*inc
)
1853 if (isl_val_is_pos(inc
))
1854 return !isl_set_dim_has_any_lower_bound(cond
, isl_dim_set
, 0);
1856 return !isl_set_dim_has_any_upper_bound(cond
, isl_dim_set
, 0);
1859 /* Extend a condition on a given iteration of a loop to one that
1860 * imposes the same condition on all previous iterations.
1861 * "domain" expresses the lower [upper] bound on the iterations
1862 * when inc is positive [negative].
1864 * In particular, we construct the condition (when inc is positive)
1866 * forall i' : (domain(i') and i' <= i) => cond(i')
1868 * which is equivalent to
1870 * not exists i' : domain(i') and i' <= i and not cond(i')
1872 * We construct this set by negating cond, applying a map
1874 * { [i'] -> [i] : domain(i') and i' <= i }
1876 * and then negating the result again.
1878 static __isl_give isl_set
*valid_for_each_iteration(__isl_take isl_set
*cond
,
1879 __isl_take isl_set
*domain
, __isl_take isl_val
*inc
)
1881 isl_map
*previous_to_this
;
1883 if (isl_val_is_pos(inc
))
1884 previous_to_this
= isl_map_lex_le(isl_set_get_space(domain
));
1886 previous_to_this
= isl_map_lex_ge(isl_set_get_space(domain
));
1888 previous_to_this
= isl_map_intersect_domain(previous_to_this
, domain
);
1890 cond
= isl_set_complement(cond
);
1891 cond
= isl_set_apply(cond
, previous_to_this
);
1892 cond
= isl_set_complement(cond
);
1899 /* Construct a domain of the form
1901 * [id] -> { : exists a: id = init + a * inc and a >= 0 }
1903 static __isl_give isl_set
*strided_domain(__isl_take isl_id
*id
,
1904 __isl_take isl_pw_aff
*init
, __isl_take isl_val
*inc
)
1910 init
= isl_pw_aff_insert_dims(init
, isl_dim_in
, 0, 1);
1911 dim
= isl_pw_aff_get_domain_space(init
);
1912 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1913 aff
= isl_aff_add_coefficient_val(aff
, isl_dim_in
, 0, inc
);
1914 init
= isl_pw_aff_add(init
, isl_pw_aff_from_aff(aff
));
1916 dim
= isl_space_set_alloc(isl_pw_aff_get_ctx(init
), 1, 1);
1917 dim
= isl_space_set_dim_id(dim
, isl_dim_param
, 0, id
);
1918 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1919 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_param
, 0, 1);
1921 set
= isl_pw_aff_eq_set(isl_pw_aff_from_aff(aff
), init
);
1923 set
= isl_set_lower_bound_si(set
, isl_dim_set
, 0, 0);
1925 return isl_set_params(set
);
1928 /* Assuming "cond" represents a bound on a loop where the loop
1929 * iterator "iv" is incremented (or decremented) by one, check if wrapping
1932 * Under the given assumptions, wrapping is only possible if "cond" allows
1933 * for the last value before wrapping, i.e., 2^width - 1 in case of an
1934 * increasing iterator and 0 in case of a decreasing iterator.
1936 static bool can_wrap(__isl_keep isl_set
*cond
, ValueDecl
*iv
,
1937 __isl_keep isl_val
*inc
)
1944 test
= isl_set_copy(cond
);
1946 ctx
= isl_set_get_ctx(test
);
1947 if (isl_val_is_neg(inc
))
1948 limit
= isl_val_zero(ctx
);
1950 limit
= isl_val_int_from_ui(ctx
, get_type_size(iv
));
1951 limit
= isl_val_2exp(limit
);
1952 limit
= isl_val_sub_ui(limit
, 1);
1955 test
= isl_set_fix_val(cond
, isl_dim_set
, 0, limit
);
1956 cw
= !isl_set_is_empty(test
);
1962 /* Given a one-dimensional space, construct the following affine expression
1965 * { [v] -> [v mod 2^width] }
1967 * where width is the number of bits used to represent the values
1968 * of the unsigned variable "iv".
1970 static __isl_give isl_aff
*compute_wrapping(__isl_take isl_space
*dim
,
1977 ctx
= isl_space_get_ctx(dim
);
1978 mod
= isl_val_int_from_ui(ctx
, get_type_size(iv
));
1979 mod
= isl_val_2exp(mod
);
1981 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
1982 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
1983 aff
= isl_aff_mod_val(aff
, mod
);
1988 /* Project out the parameter "id" from "set".
1990 static __isl_give isl_set
*set_project_out_by_id(__isl_take isl_set
*set
,
1991 __isl_keep isl_id
*id
)
1995 pos
= isl_set_find_dim_by_id(set
, isl_dim_param
, id
);
1997 set
= isl_set_project_out(set
, isl_dim_param
, pos
, 1);
2002 /* Compute the set of parameters for which "set1" is a subset of "set2".
2004 * set1 is a subset of set2 if
2006 * forall i in set1 : i in set2
2010 * not exists i in set1 and i not in set2
2014 * not exists i in set1 \ set2
2016 static __isl_give isl_set
*enforce_subset(__isl_take isl_set
*set1
,
2017 __isl_take isl_set
*set2
)
2019 return isl_set_complement(isl_set_params(isl_set_subtract(set1
, set2
)));
2022 /* Compute the set of parameter values for which "cond" holds
2023 * on the next iteration for each element of "dom".
2025 * We first construct mapping { [i] -> [i + inc] }, apply that to "dom"
2026 * and then compute the set of parameters for which the result is a subset
2029 static __isl_give isl_set
*valid_on_next(__isl_take isl_set
*cond
,
2030 __isl_take isl_set
*dom
, __isl_take isl_val
*inc
)
2036 space
= isl_set_get_space(dom
);
2037 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(space
));
2038 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, 0, 1);
2039 aff
= isl_aff_add_constant_val(aff
, inc
);
2040 next
= isl_map_from_basic_map(isl_basic_map_from_aff(aff
));
2042 dom
= isl_set_apply(dom
, next
);
2044 return enforce_subset(dom
, cond
);
2047 /* Extract the for loop "stmt" as a while loop within the context "pc".
2048 * "iv" is the loop iterator. "init" is the initialization.
2049 * "inc" is the increment.
2051 * That is, the for loop has the form
2053 * for (iv = init; cond; iv += inc)
2064 * except that the skips resulting from any continue statements
2065 * in body do not apply to the increment, but are replaced by the skips
2066 * resulting from break statements.
2068 * If "iv" is declared in the for loop, then it is killed before
2069 * and after the loop.
2071 struct pet_scop
*PetScan::extract_non_affine_for(ForStmt
*stmt
, ValueDecl
*iv
,
2072 __isl_take pet_expr
*init
, __isl_take pet_expr
*inc
,
2073 __isl_take pet_context
*pc
)
2076 int test_nr
, stmt_nr
;
2078 struct pet_scop
*scop_init
, *scop_inc
, *scop
, *scop_body
;
2080 struct pet_array
*array
;
2081 struct pet_scop
*scop_kill
;
2083 if (!allow_nested
) {
2085 pet_context_free(pc
);
2089 pc
= pet_context_mark_assigned(pc
, create_decl_id(ctx
, iv
));
2091 declared
= !initialization_assignment(stmt
->getInit());
2093 expr_iv
= extract_access_expr(iv
);
2094 expr_iv
= mark_write(expr_iv
);
2095 type_size
= pet_expr_get_type_size(expr_iv
);
2096 init
= pet_expr_new_binary(type_size
, pet_op_assign
, expr_iv
, init
);
2097 scop_init
= extract(init
, stmt
->getInit()->getSourceRange(), false, pc
);
2098 scop_init
= pet_scop_prefix(scop_init
, declared
);
2102 scop_body
= extract(stmt
->getBody(), pc
);
2104 pet_scop_free(scop_init
);
2105 pet_context_free(pc
);
2109 expr_iv
= extract_access_expr(iv
);
2110 expr_iv
= mark_write(expr_iv
);
2111 type_size
= pet_expr_get_type_size(expr_iv
);
2112 inc
= pet_expr_new_binary(type_size
, pet_op_add_assign
, expr_iv
, inc
);
2113 scop_inc
= extract(inc
, stmt
->getInc()->getSourceRange(), false, pc
);
2115 pet_scop_free(scop_init
);
2116 pet_scop_free(scop_body
);
2117 pet_context_free(pc
);
2121 scop
= extract_while(stmt
->getCond(), test_nr
, stmt_nr
, scop_body
,
2122 scop_inc
, pet_context_copy(pc
));
2124 scop
= pet_scop_prefix(scop
, declared
+ 1);
2125 scop
= pet_scop_add_seq(ctx
, scop_init
, scop
);
2128 pet_context_free(pc
);
2132 array
= extract_array(ctx
, iv
, NULL
, pc
);
2134 array
->declared
= 1;
2135 scop_kill
= kill(stmt
, array
, pc
);
2136 scop_kill
= pet_scop_prefix(scop_kill
, 0);
2137 scop
= pet_scop_add_seq(ctx
, scop_kill
, scop
);
2138 scop_kill
= kill(stmt
, array
, pc
);
2139 scop_kill
= pet_scop_add_array(scop_kill
, array
);
2140 scop_kill
= pet_scop_prefix(scop_kill
, 3);
2141 scop
= pet_scop_add_seq(ctx
, scop
, scop_kill
);
2143 pet_context_free(pc
);
2147 /* Construct a pet_scop for a for statement within the context "pc".
2148 * The for loop is required to be of one of the following forms
2150 * for (i = init; condition; ++i)
2151 * for (i = init; condition; --i)
2152 * for (i = init; condition; i += constant)
2153 * for (i = init; condition; i -= constant)
2155 * The initialization of the for loop should either be an assignment
2156 * of a static affine value to an integer variable, or a declaration
2157 * of such a variable with initialization.
2159 * If the initialization or the increment do not satisfy the above
2160 * conditions, i.e., if the initialization is not static affine
2161 * or the increment is not constant, then the for loop is extracted
2162 * as a while loop instead.
2164 * The condition is allowed to contain nested accesses, provided
2165 * they are not being written to inside the body of the loop.
2166 * Otherwise, or if the condition is otherwise non-affine, the for loop is
2167 * essentially treated as a while loop, with iteration domain
2168 * { [i] : i >= init }.
2170 * We extract a pet_scop for the body and then embed it in a loop with
2171 * iteration domain and schedule
2173 * { [i] : i >= init and condition' }
2178 * { [i] : i <= init and condition' }
2181 * Where condition' is equal to condition if the latter is
2182 * a simple upper [lower] bound and a condition that is extended
2183 * to apply to all previous iterations otherwise.
2185 * If the condition is non-affine, then we drop the condition from the
2186 * iteration domain and instead create a separate statement
2187 * for evaluating the condition. The body is then filtered to depend
2188 * on the result of the condition evaluating to true on all iterations
2189 * up to the current iteration, while the evaluation the condition itself
2190 * is filtered to depend on the result of the condition evaluating to true
2191 * on all previous iterations.
2192 * The context of the scop representing the body is dropped
2193 * because we don't know how many times the body will be executed,
2196 * If the stride of the loop is not 1, then "i >= init" is replaced by
2198 * (exists a: i = init + stride * a and a >= 0)
2200 * If the loop iterator i is unsigned, then wrapping may occur.
2201 * We therefore use a virtual iterator instead that does not wrap.
2202 * However, the condition in the code applies
2203 * to the wrapped value, so we need to change condition(i)
2204 * into condition([i % 2^width]). Similarly, we replace all accesses
2205 * to the original iterator by the wrapping of the virtual iterator.
2206 * Note that there may be no need to perform this final wrapping
2207 * if the loop condition (after wrapping) satisfies certain conditions.
2208 * However, the is_simple_bound condition is not enough since it doesn't
2209 * check if there even is an upper bound.
2211 * Wrapping on unsigned iterators can be avoided entirely if
2212 * loop condition is simple, the loop iterator is incremented
2213 * [decremented] by one and the last value before wrapping cannot
2214 * possibly satisfy the loop condition.
2216 * Before extracting a pet_scop from the body we remove all
2217 * assignments in "pc" to variables that are assigned
2218 * somewhere in the body of the loop.
2220 * Valid parameters for a for loop are those for which the initial
2221 * value itself, the increment on each domain iteration and
2222 * the condition on both the initial value and
2223 * the result of incrementing the iterator for each iteration of the domain
2225 * If the loop condition is non-affine, then we only consider validity
2226 * of the initial value.
2228 * If the body contains any break, then we keep track of it in "skip"
2229 * (if the skip condition is affine) or it is handled in scop_add_break
2230 * (if the skip condition is not affine).
2231 * Note that the affine break condition needs to be considered with
2232 * respect to previous iterations in the virtual domain (if any).
2234 * If we were only able to extract part of the body, then simply
2237 struct pet_scop
*PetScan::extract_for(ForStmt
*stmt
, __isl_keep pet_context
*pc
)
2239 BinaryOperator
*ass
;
2244 isl_local_space
*ls
;
2247 isl_set
*cond
= NULL
;
2248 isl_set
*skip
= NULL
;
2249 isl_id
*id
, *id_test
= NULL
, *id_break_test
;
2250 struct pet_scop
*scop
, *scop_cond
= NULL
;
2256 bool has_affine_break
;
2258 isl_aff
*wrap
= NULL
;
2259 isl_pw_aff
*pa
, *pa_inc
, *init_val
;
2260 isl_set
*valid_init
;
2261 isl_set
*valid_cond
;
2262 isl_set
*valid_cond_init
;
2263 isl_set
*valid_cond_next
;
2266 pet_expr
*pe_init
, *pe_inc
;
2267 pet_context
*pc_init_val
;
2269 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc())
2270 return extract_infinite_for(stmt
, pc
);
2272 init
= stmt
->getInit();
2277 if ((ass
= initialization_assignment(init
)) != NULL
) {
2278 iv
= extract_induction_variable(ass
);
2281 lhs
= ass
->getLHS();
2282 rhs
= ass
->getRHS();
2283 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
2284 VarDecl
*var
= extract_induction_variable(init
, decl
);
2288 rhs
= var
->getInit();
2289 lhs
= create_DeclRefExpr(var
);
2291 unsupported(stmt
->getInit());
2295 id
= create_decl_id(ctx
, iv
);
2297 pc
= pet_context_copy(pc
);
2298 pc
= pet_context_clear_value(pc
, isl_id_copy(id
));
2299 clear_assignments
clear(pc
);
2300 clear
.TraverseStmt(stmt
->getBody());
2302 pe_init
= extract_expr(rhs
);
2303 pe_inc
= extract_increment(stmt
, iv
);
2304 pc_init_val
= pet_context_copy(pc
);
2305 pc_init_val
= pet_context_mark_unknown(pc_init_val
, isl_id_copy(id
));
2306 init_val
= pet_expr_extract_affine(pe_init
, pc_init_val
);
2307 pet_context_free(pc_init_val
);
2308 pa_inc
= pet_expr_extract_affine(pe_inc
, pc
);
2309 inc
= pet_extract_cst(pa_inc
);
2310 if (!pe_init
|| !pe_inc
|| !inc
|| isl_val_is_nan(inc
) ||
2311 isl_pw_aff_involves_nan(pa_inc
) ||
2312 isl_pw_aff_involves_nan(init_val
)) {
2315 isl_pw_aff_free(pa_inc
);
2316 isl_pw_aff_free(init_val
);
2317 if (pe_init
&& pe_inc
&& !(pa_inc
&& !inc
))
2318 return extract_non_affine_for(stmt
, iv
,
2319 pe_init
, pe_inc
, pc
);
2320 pet_expr_free(pe_init
);
2321 pet_expr_free(pe_inc
);
2322 pet_context_free(pc
);
2325 pet_expr_free(pe_inc
);
2327 pa
= try_extract_nested_condition(stmt
->getCond(), pc
);
2328 if (allow_nested
&& (!pa
|| pet_nested_any_in_pw_aff(pa
)))
2331 scop
= extract(stmt
->getBody(), pc
);
2334 isl_pw_aff_free(init_val
);
2335 isl_pw_aff_free(pa_inc
);
2336 isl_pw_aff_free(pa
);
2337 pet_expr_free(pe_init
);
2339 pet_context_free(pc
);
2343 valid_inc
= isl_pw_aff_domain(pa_inc
);
2345 is_unsigned
= iv
->getType()->isUnsignedIntegerType();
2347 has_affine_break
= scop
&&
2348 pet_scop_has_affine_skip(scop
, pet_skip_later
);
2349 if (has_affine_break
)
2350 skip
= pet_scop_get_affine_skip_domain(scop
, pet_skip_later
);
2351 has_var_break
= scop
&& pet_scop_has_var_skip(scop
, pet_skip_later
);
2353 id_break_test
= pet_scop_get_skip_id(scop
, pet_skip_later
);
2355 if (pa
&& !is_nested_allowed(pa
, scop
)) {
2356 isl_pw_aff_free(pa
);
2360 if (!allow_nested
&& !pa
)
2361 pa
= extract_condition(stmt
->getCond(), pc
);
2362 valid_cond
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2363 cond
= isl_pw_aff_non_zero_set(pa
);
2364 if (allow_nested
&& !cond
) {
2365 isl_multi_pw_aff
*test_index
;
2366 int save_n_stmt
= n_stmt
;
2367 test_index
= pet_create_test_index(ctx
, n_test
++);
2369 scop_cond
= extract_non_affine_condition(stmt
->getCond(),
2370 n_stmt
++, isl_multi_pw_aff_copy(test_index
), pc
);
2371 n_stmt
= save_n_stmt
;
2372 scop_cond
= scop_add_array(scop_cond
, test_index
, ast_context
);
2373 id_test
= isl_multi_pw_aff_get_tuple_id(test_index
,
2375 isl_multi_pw_aff_free(test_index
);
2376 scop_cond
= pet_scop_prefix(scop_cond
, 0);
2377 scop
= pet_scop_reset_context(scop
);
2378 scop
= pet_scop_prefix(scop
, 1);
2379 cond
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2382 cond
= embed(cond
, isl_id_copy(id
));
2383 skip
= embed(skip
, isl_id_copy(id
));
2384 valid_cond
= isl_set_coalesce(valid_cond
);
2385 valid_cond
= embed(valid_cond
, isl_id_copy(id
));
2386 valid_inc
= embed(valid_inc
, isl_id_copy(id
));
2387 is_one
= isl_val_is_one(inc
) || isl_val_is_negone(inc
);
2388 is_virtual
= is_unsigned
&& (!is_one
|| can_wrap(cond
, iv
, inc
));
2390 valid_cond_init
= enforce_subset(
2391 isl_map_range(isl_map_from_pw_aff(isl_pw_aff_copy(init_val
))),
2392 isl_set_copy(valid_cond
));
2393 if (is_one
&& !is_virtual
) {
2395 isl_pw_aff_free(init_val
);
2396 pe_iv
= extract_access_expr(iv
);
2397 pa
= pet_expr_extract_comparison(
2398 isl_val_is_pos(inc
) ? pet_op_ge
: pet_op_le
,
2399 pe_iv
, pe_init
, pc
);
2400 pet_expr_free(pe_iv
);
2401 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2402 valid_init
= set_project_out_by_id(valid_init
, id
);
2403 domain
= isl_pw_aff_non_zero_set(pa
);
2405 valid_init
= isl_pw_aff_domain(isl_pw_aff_copy(init_val
));
2406 domain
= strided_domain(isl_id_copy(id
), init_val
,
2410 domain
= embed(domain
, isl_id_copy(id
));
2413 wrap
= compute_wrapping(isl_set_get_space(cond
), iv
);
2414 rev_wrap
= isl_map_from_aff(isl_aff_copy(wrap
));
2415 rev_wrap
= isl_map_reverse(rev_wrap
);
2416 cond
= isl_set_apply(cond
, isl_map_copy(rev_wrap
));
2417 skip
= isl_set_apply(skip
, isl_map_copy(rev_wrap
));
2418 valid_cond
= isl_set_apply(valid_cond
, isl_map_copy(rev_wrap
));
2419 valid_inc
= isl_set_apply(valid_inc
, rev_wrap
);
2421 is_simple
= is_simple_bound(cond
, inc
);
2423 cond
= isl_set_gist(cond
, isl_set_copy(domain
));
2424 is_simple
= is_simple_bound(cond
, inc
);
2427 cond
= valid_for_each_iteration(cond
,
2428 isl_set_copy(domain
), isl_val_copy(inc
));
2429 domain
= isl_set_intersect(domain
, cond
);
2430 if (has_affine_break
) {
2431 skip
= isl_set_intersect(skip
, isl_set_copy(domain
));
2432 skip
= after(skip
, isl_val_sgn(inc
));
2433 domain
= isl_set_subtract(domain
, skip
);
2435 domain
= isl_set_set_dim_id(domain
, isl_dim_set
, 0, isl_id_copy(id
));
2436 ls
= isl_local_space_from_space(isl_set_get_space(domain
));
2437 sched
= isl_aff_var_on_domain(ls
, isl_dim_set
, 0);
2438 if (isl_val_is_neg(inc
))
2439 sched
= isl_aff_neg(sched
);
2441 valid_cond_next
= valid_on_next(valid_cond
, isl_set_copy(domain
),
2443 valid_inc
= enforce_subset(isl_set_copy(domain
), valid_inc
);
2446 wrap
= identity_aff(domain
);
2448 scop_cond
= pet_scop_embed(scop_cond
, isl_set_copy(domain
),
2449 isl_aff_copy(sched
), isl_aff_copy(wrap
), isl_id_copy(id
));
2450 scop
= pet_scop_embed(scop
, isl_set_copy(domain
), sched
, wrap
, id
);
2451 scop
= pet_scop_resolve_nested(scop
);
2453 scop
= scop_add_break(scop
, id_break_test
, isl_set_copy(domain
),
2456 scop
= scop_add_while(scop_cond
, scop
, id_test
, domain
,
2458 isl_set_free(valid_inc
);
2460 scop
= pet_scop_restrict_context(scop
, valid_inc
);
2461 scop
= pet_scop_restrict_context(scop
, valid_cond_next
);
2462 scop
= pet_scop_restrict_context(scop
, valid_cond_init
);
2463 isl_set_free(domain
);
2466 pet_expr_free(pe_init
);
2469 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid_init
));
2471 pet_context_free(pc
);
2475 /* Try and construct a pet_scop corresponding to a compound statement
2476 * within the context "pc".
2478 * "skip_declarations" is set if we should skip initial declarations
2479 * in the children of the compound statements. This then implies
2480 * that this sequence of children should not be treated as a block
2481 * since the initial statements may be skipped.
2483 struct pet_scop
*PetScan::extract(CompoundStmt
*stmt
,
2484 __isl_keep pet_context
*pc
, bool skip_declarations
)
2486 return extract(stmt
->children(),
2487 !skip_declarations
, skip_declarations
, pc
);
2490 /* Return the file offset of the expansion location of "Loc".
2492 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
2494 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
2497 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
2499 /* Return a SourceLocation for the location after the first semicolon
2500 * after "loc". If Lexer::findLocationAfterToken is available, we simply
2501 * call it and also skip trailing spaces and newline.
2503 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
2504 const LangOptions
&LO
)
2506 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
2511 /* Return a SourceLocation for the location after the first semicolon
2512 * after "loc". If Lexer::findLocationAfterToken is not available,
2513 * we look in the underlying character data for the first semicolon.
2515 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
2516 const LangOptions
&LO
)
2519 const char *s
= SM
.getCharacterData(loc
);
2521 semi
= strchr(s
, ';');
2523 return SourceLocation();
2524 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
2529 /* If the token at "loc" is the first token on the line, then return
2530 * a location referring to the start of the line.
2531 * Otherwise, return "loc".
2533 * This function is used to extend a scop to the start of the line
2534 * if the first token of the scop is also the first token on the line.
2536 * We look for the first token on the line. If its location is equal to "loc",
2537 * then the latter is the location of the first token on the line.
2539 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
2540 SourceManager
&SM
, const LangOptions
&LO
)
2542 std::pair
<FileID
, unsigned> file_offset_pair
;
2543 llvm::StringRef file
;
2546 SourceLocation token_loc
, line_loc
;
2549 loc
= SM
.getExpansionLoc(loc
);
2550 col
= SM
.getExpansionColumnNumber(loc
);
2551 line_loc
= loc
.getLocWithOffset(1 - col
);
2552 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
2553 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
2554 pos
= file
.data() + file_offset_pair
.second
;
2556 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
2557 file
.begin(), pos
, file
.end());
2558 lexer
.LexFromRawLexer(tok
);
2559 token_loc
= tok
.getLocation();
2561 if (token_loc
== loc
)
2567 /* Construct a pet_loc corresponding to the region covered by "range".
2568 * If "skip_semi" is set, then we assume "range" is followed by
2569 * a semicolon and also include this semicolon.
2571 __isl_give pet_loc
*PetScan::construct_pet_loc(SourceRange range
,
2574 SourceLocation loc
= range
.getBegin();
2575 SourceManager
&SM
= PP
.getSourceManager();
2576 const LangOptions
&LO
= PP
.getLangOpts();
2577 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
2578 unsigned start
, end
;
2580 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
);
2581 start
= getExpansionOffset(SM
, loc
);
2582 loc
= range
.getEnd();
2584 loc
= location_after_semi(loc
, SM
, LO
);
2586 loc
= PP
.getLocForEndOfToken(loc
);
2587 end
= getExpansionOffset(SM
, loc
);
2589 return pet_loc_alloc(ctx
, start
, end
, line
);
2592 /* Convert a top-level pet_expr to a pet_scop with one statement
2593 * within the context "pc".
2594 * This mainly involves resolving nested expression parameters
2595 * and setting the name of the iteration space.
2596 * The name is given by "label" if it is non-NULL. Otherwise,
2597 * it is of the form S_<n_stmt>.
2598 * start and end of the pet_scop are derived from "range" and "skip_semi".
2599 * In particular, if "skip_semi" is set then the semicolon following "range"
2602 struct pet_scop
*PetScan::extract(__isl_take pet_expr
*expr
, SourceRange range
,
2603 bool skip_semi
, __isl_keep pet_context
*pc
, __isl_take isl_id
*label
)
2605 struct pet_stmt
*ps
;
2606 struct pet_scop
*scop
;
2609 expr
= pet_expr_plug_in_args(expr
, pc
);
2610 expr
= pet_expr_resolve_nested(expr
);
2611 expr
= pet_expr_resolve_assume(expr
, pc
);
2613 loc
= construct_pet_loc(range
, skip_semi
);
2614 ps
= pet_stmt_from_pet_expr(loc
, label
, n_stmt
++, expr
);
2615 scop
= pet_scop_from_pet_stmt(ctx
, ps
);
2619 /* Check whether "expr" is an affine constraint within the context "pc".
2621 bool PetScan::is_affine_condition(Expr
*expr
, __isl_keep pet_context
*pc
)
2625 cond
= extract_condition(expr
, pc
);
2626 isl_pw_aff_free(cond
);
2628 return cond
!= NULL
;
2631 /* Check if we can extract a condition from "expr" within the context "pc".
2632 * Return the condition as an isl_pw_aff if we can and NULL otherwise.
2633 * If allow_nested is set, then the condition may involve parameters
2634 * corresponding to nested accesses.
2635 * We turn on autodetection so that we won't generate any warnings.
2637 __isl_give isl_pw_aff
*PetScan::try_extract_nested_condition(Expr
*expr
,
2638 __isl_keep pet_context
*pc
)
2641 int save_autodetect
= options
->autodetect
;
2642 bool save_nesting
= nesting_enabled
;
2644 options
->autodetect
= 1;
2645 nesting_enabled
= allow_nested
;
2646 cond
= extract_condition(expr
, pc
);
2648 options
->autodetect
= save_autodetect
;
2649 nesting_enabled
= save_nesting
;
2654 /* If the top-level expression of "stmt" is an assignment, then
2655 * return that assignment as a BinaryOperator.
2656 * Otherwise return NULL.
2658 static BinaryOperator
*top_assignment_or_null(Stmt
*stmt
)
2660 BinaryOperator
*ass
;
2664 if (stmt
->getStmtClass() != Stmt::BinaryOperatorClass
)
2667 ass
= cast
<BinaryOperator
>(stmt
);
2668 if(ass
->getOpcode() != BO_Assign
)
2674 /* Check if the given if statement is a conditional assignement
2675 * with a non-affine condition within the context "pc".
2676 * If so, construct a pet_scop corresponding to this conditional assignment.
2677 * Otherwise return NULL.
2679 * In particular we check if "stmt" is of the form
2686 * where a is some array or scalar access.
2687 * The constructed pet_scop then corresponds to the expression
2689 * a = condition ? f(...) : g(...)
2691 * All access relations in f(...) are intersected with condition
2692 * while all access relation in g(...) are intersected with the complement.
2694 struct pet_scop
*PetScan::extract_conditional_assignment(IfStmt
*stmt
,
2695 __isl_keep pet_context
*pc
)
2697 BinaryOperator
*ass_then
, *ass_else
;
2698 pet_expr
*write_then
, *write_else
;
2699 isl_set
*cond
, *comp
;
2700 isl_multi_pw_aff
*index
;
2704 pet_expr
*pe_cond
, *pe_then
, *pe_else
, *pe
;
2705 bool save_nesting
= nesting_enabled
;
2707 if (!options
->detect_conditional_assignment
)
2710 ass_then
= top_assignment_or_null(stmt
->getThen());
2711 ass_else
= top_assignment_or_null(stmt
->getElse());
2713 if (!ass_then
|| !ass_else
)
2716 if (is_affine_condition(stmt
->getCond(), pc
))
2719 write_then
= extract_access_expr(ass_then
->getLHS());
2720 write_else
= extract_access_expr(ass_else
->getLHS());
2722 equal
= pet_expr_is_equal(write_then
, write_else
);
2723 pet_expr_free(write_else
);
2724 if (equal
< 0 || !equal
) {
2725 pet_expr_free(write_then
);
2729 nesting_enabled
= allow_nested
;
2730 pa
= extract_condition(stmt
->getCond(), pc
);
2731 nesting_enabled
= save_nesting
;
2732 cond
= isl_pw_aff_non_zero_set(isl_pw_aff_copy(pa
));
2733 comp
= isl_pw_aff_zero_set(isl_pw_aff_copy(pa
));
2734 index
= isl_multi_pw_aff_from_pw_aff(pa
);
2736 pe_cond
= pet_expr_from_index(index
);
2738 pe_then
= extract_expr(ass_then
->getRHS());
2739 pe_then
= pet_expr_restrict(pe_then
, cond
);
2740 pe_else
= extract_expr(ass_else
->getRHS());
2741 pe_else
= pet_expr_restrict(pe_else
, comp
);
2743 pe
= pet_expr_new_ternary(pe_cond
, pe_then
, pe_else
);
2744 write_then
= pet_expr_access_set_write(write_then
, 1);
2745 write_then
= pet_expr_access_set_read(write_then
, 0);
2746 type_size
= get_type_size(ass_then
->getType(), ast_context
);
2747 pe
= pet_expr_new_binary(type_size
, pet_op_assign
, write_then
, pe
);
2748 return extract(pe
, stmt
->getSourceRange(), false, pc
);
2751 /* Create a pet_scop with a single statement with name S_<stmt_nr>,
2752 * evaluating "cond" and writing the result to a virtual scalar,
2753 * as expressed by "index".
2754 * Do so within the context "pc".
2756 struct pet_scop
*PetScan::extract_non_affine_condition(Expr
*cond
, int stmt_nr
,
2757 __isl_take isl_multi_pw_aff
*index
, __isl_keep pet_context
*pc
)
2759 pet_expr
*expr
, *write
;
2760 struct pet_stmt
*ps
;
2763 write
= pet_expr_from_index(index
);
2764 write
= pet_expr_access_set_write(write
, 1);
2765 write
= pet_expr_access_set_read(write
, 0);
2766 expr
= extract_expr(cond
);
2767 expr
= pet_expr_plug_in_args(expr
, pc
);
2768 expr
= pet_expr_resolve_nested(expr
);
2769 expr
= pet_expr_new_binary(1, pet_op_assign
, write
, expr
);
2770 loc
= construct_pet_loc(cond
->getSourceRange(), false);
2771 ps
= pet_stmt_from_pet_expr(loc
, NULL
, stmt_nr
, expr
);
2772 return pet_scop_from_pet_stmt(ctx
, ps
);
2775 /* Given an access expression "expr", is the variable accessed by
2776 * "expr" assigned anywhere inside "scop"?
2778 static bool is_assigned(__isl_keep pet_expr
*expr
, pet_scop
*scop
)
2780 bool assigned
= false;
2783 id
= pet_expr_access_get_id(expr
);
2784 assigned
= pet_scop_writes(scop
, id
);
2790 /* Are all nested access parameters in "pa" allowed given "scop".
2791 * In particular, is none of them written by anywhere inside "scop".
2793 * If "scop" has any skip conditions, then no nested access parameters
2794 * are allowed. In particular, if there is any nested access in a guard
2795 * for a piece of code containing a "continue", then we want to introduce
2796 * a separate statement for evaluating this guard so that we can express
2797 * that the result is false for all previous iterations.
2799 bool PetScan::is_nested_allowed(__isl_keep isl_pw_aff
*pa
, pet_scop
*scop
)
2806 if (!pet_nested_any_in_pw_aff(pa
))
2809 if (pet_scop_has_skip(scop
, pet_skip_now
))
2812 nparam
= isl_pw_aff_dim(pa
, isl_dim_param
);
2813 for (int i
= 0; i
< nparam
; ++i
) {
2814 isl_id
*id
= isl_pw_aff_get_dim_id(pa
, isl_dim_param
, i
);
2818 if (!pet_nested_in_id(id
)) {
2823 expr
= pet_nested_extract_expr(id
);
2824 allowed
= pet_expr_get_type(expr
) == pet_expr_access
&&
2825 !is_assigned(expr
, scop
);
2827 pet_expr_free(expr
);
2837 /* Construct a pet_scop for a non-affine if statement within the context "pc".
2839 * We create a separate statement that writes the result
2840 * of the non-affine condition to a virtual scalar.
2841 * A constraint requiring the value of this virtual scalar to be one
2842 * is added to the iteration domains of the then branch.
2843 * Similarly, a constraint requiring the value of this virtual scalar
2844 * to be zero is added to the iteration domains of the else branch, if any.
2845 * We adjust the schedules to ensure that the virtual scalar is written
2846 * before it is read.
2848 * If there are any breaks or continues in the then and/or else
2849 * branches, then we may have to compute a new skip condition.
2850 * This is handled using a pet_skip_info object.
2851 * On initialization, the object checks if skip conditions need
2852 * to be computed. If so, it does so in pet_skip_info_if_extract_index and
2853 * adds them in pet_skip_info_if_add.
2855 struct pet_scop
*PetScan::extract_non_affine_if(Expr
*cond
,
2856 struct pet_scop
*scop_then
, struct pet_scop
*scop_else
,
2857 bool have_else
, int stmt_id
, __isl_take pet_context
*pc
)
2859 struct pet_scop
*scop
;
2860 isl_multi_pw_aff
*test_index
;
2862 int save_n_stmt
= n_stmt
;
2864 test_index
= pet_create_test_index(ctx
, n_test
++);
2866 scop
= extract_non_affine_condition(cond
, n_stmt
++,
2867 isl_multi_pw_aff_copy(test_index
), pc
);
2868 n_stmt
= save_n_stmt
;
2869 scop
= scop_add_array(scop
, test_index
, ast_context
);
2872 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
, have_else
, 0);
2873 int_size
= ast_context
.getTypeInfo(ast_context
.IntTy
).first
/ 8;
2874 pet_skip_info_if_extract_index(&skip
, test_index
, int_size
,
2877 scop
= pet_scop_prefix(scop
, 0);
2878 scop_then
= pet_scop_prefix(scop_then
, 1);
2879 scop_then
= pet_scop_filter(scop_then
,
2880 isl_multi_pw_aff_copy(test_index
), 1);
2882 scop_else
= pet_scop_prefix(scop_else
, 1);
2883 scop_else
= pet_scop_filter(scop_else
, test_index
, 0);
2884 scop_then
= pet_scop_add_par(ctx
, scop_then
, scop_else
);
2886 isl_multi_pw_aff_free(test_index
);
2888 scop
= pet_scop_add_seq(ctx
, scop
, scop_then
);
2890 scop
= pet_skip_info_if_add(&skip
, scop
, 2);
2892 pet_context_free(pc
);
2896 /* Construct a pet_scop for an if statement within the context "pc".
2898 * If the condition fits the pattern of a conditional assignment,
2899 * then it is handled by extract_conditional_assignment.
2900 * Otherwise, we do the following.
2902 * If the condition is affine, then the condition is added
2903 * to the iteration domains of the then branch, while the
2904 * opposite of the condition in added to the iteration domains
2905 * of the else branch, if any.
2906 * We allow the condition to be dynamic, i.e., to refer to
2907 * scalars or array elements that may be written to outside
2908 * of the given if statement. These nested accesses are then represented
2909 * as output dimensions in the wrapping iteration domain.
2910 * If it is also written _inside_ the then or else branch, then
2911 * we treat the condition as non-affine.
2912 * As explained in extract_non_affine_if, this will introduce
2913 * an extra statement.
2914 * For aesthetic reasons, we want this statement to have a statement
2915 * number that is lower than those of the then and else branches.
2916 * In order to evaluate if we will need such a statement, however, we
2917 * first construct scops for the then and else branches.
2918 * We therefore reserve a statement number if we might have to
2919 * introduce such an extra statement.
2921 * If the condition is not affine, then the scop is created in
2922 * extract_non_affine_if.
2924 * If there are any breaks or continues in the then and/or else
2925 * branches, then we may have to compute a new skip condition.
2926 * This is handled using a pet_skip_info object.
2927 * On initialization, the object checks if skip conditions need
2928 * to be computed. If so, it does so in pet_skip_info_if_extract_cond and
2929 * adds them in pet_skip_info_if_add.
2931 struct pet_scop
*PetScan::extract(IfStmt
*stmt
, __isl_keep pet_context
*pc
)
2933 struct pet_scop
*scop_then
, *scop_else
= NULL
, *scop
;
2940 pc
= pet_context_copy(pc
);
2941 clear_assignments
clear(pc
);
2942 clear
.TraverseStmt(stmt
->getThen());
2943 if (stmt
->getElse())
2944 clear
.TraverseStmt(stmt
->getElse());
2946 scop
= extract_conditional_assignment(stmt
, pc
);
2948 pet_context_free(pc
);
2952 cond
= try_extract_nested_condition(stmt
->getCond(), pc
);
2953 if (allow_nested
&& (!cond
|| pet_nested_any_in_pw_aff(cond
)))
2956 scop_then
= extract(stmt
->getThen(), pc
);
2958 if (stmt
->getElse()) {
2959 scop_else
= extract(stmt
->getElse(), pc
);
2960 if (options
->autodetect
) {
2961 if (scop_then
&& !scop_else
) {
2963 isl_pw_aff_free(cond
);
2964 pet_context_free(pc
);
2967 if (!scop_then
&& scop_else
) {
2969 isl_pw_aff_free(cond
);
2970 pet_context_free(pc
);
2977 (!is_nested_allowed(cond
, scop_then
) ||
2978 (stmt
->getElse() && !is_nested_allowed(cond
, scop_else
)))) {
2979 isl_pw_aff_free(cond
);
2982 if (allow_nested
&& !cond
)
2983 return extract_non_affine_if(stmt
->getCond(), scop_then
,
2984 scop_else
, stmt
->getElse(), stmt_id
, pc
);
2987 cond
= extract_condition(stmt
->getCond(), pc
);
2990 pet_skip_info_if_init(&skip
, ctx
, scop_then
, scop_else
,
2991 stmt
->getElse() != NULL
, 1);
2992 pet_skip_info_if_extract_cond(&skip
, cond
, int_size
, &n_stmt
, &n_test
);
2994 valid
= isl_pw_aff_domain(isl_pw_aff_copy(cond
));
2995 set
= isl_pw_aff_non_zero_set(cond
);
2996 scop
= pet_scop_restrict(scop_then
, isl_set_params(isl_set_copy(set
)));
2998 if (stmt
->getElse()) {
2999 set
= isl_set_subtract(isl_set_copy(valid
), set
);
3000 scop_else
= pet_scop_restrict(scop_else
, isl_set_params(set
));
3001 scop
= pet_scop_add_par(ctx
, scop
, scop_else
);
3004 scop
= pet_scop_resolve_nested(scop
);
3005 scop
= pet_scop_restrict_context(scop
, isl_set_params(valid
));
3007 if (pet_skip_info_has_skip(&skip
))
3008 scop
= pet_scop_prefix(scop
, 0);
3009 scop
= pet_skip_info_if_add(&skip
, scop
, 1);
3011 pet_context_free(pc
);
3015 /* Try and construct a pet_scop for a label statement within the context "pc".
3016 * We currently only allow labels on expression statements.
3018 struct pet_scop
*PetScan::extract(LabelStmt
*stmt
, __isl_keep pet_context
*pc
)
3023 sub
= stmt
->getSubStmt();
3024 if (!isa
<Expr
>(sub
)) {
3029 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
3031 return extract(extract_expr(cast
<Expr
>(sub
)), stmt
->getSourceRange(),
3035 /* Return a one-dimensional multi piecewise affine expression that is equal
3036 * to the constant 1 and is defined over a zero-dimensional domain.
3038 static __isl_give isl_multi_pw_aff
*one_mpa(isl_ctx
*ctx
)
3041 isl_local_space
*ls
;
3044 space
= isl_space_set_alloc(ctx
, 0, 0);
3045 ls
= isl_local_space_from_space(space
);
3046 aff
= isl_aff_zero_on_domain(ls
);
3047 aff
= isl_aff_set_constant_si(aff
, 1);
3049 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff
));
3052 /* Construct a pet_scop for a continue statement.
3054 * We simply create an empty scop with a universal pet_skip_now
3055 * skip condition. This skip condition will then be taken into
3056 * account by the enclosing loop construct, possibly after
3057 * being incorporated into outer skip conditions.
3059 struct pet_scop
*PetScan::extract(ContinueStmt
*stmt
)
3063 scop
= pet_scop_empty(ctx
);
3067 scop
= pet_scop_set_skip(scop
, pet_skip_now
, one_mpa(ctx
));
3072 /* Construct a pet_scop for a break statement.
3074 * We simply create an empty scop with both a universal pet_skip_now
3075 * skip condition and a universal pet_skip_later skip condition.
3076 * These skip conditions will then be taken into
3077 * account by the enclosing loop construct, possibly after
3078 * being incorporated into outer skip conditions.
3080 struct pet_scop
*PetScan::extract(BreakStmt
*stmt
)
3083 isl_multi_pw_aff
*skip
;
3085 scop
= pet_scop_empty(ctx
);
3089 skip
= one_mpa(ctx
);
3090 scop
= pet_scop_set_skip(scop
, pet_skip_now
,
3091 isl_multi_pw_aff_copy(skip
));
3092 scop
= pet_scop_set_skip(scop
, pet_skip_later
, skip
);
3097 /* Try and construct a pet_scop corresponding to "stmt"
3098 * within the context "pc".
3100 * If "stmt" is a compound statement, then "skip_declarations"
3101 * indicates whether we should skip initial declarations in the
3102 * compound statement.
3104 * If the constructed pet_scop is not a (possibly) partial representation
3105 * of "stmt", we update start and end of the pet_scop to those of "stmt".
3106 * In particular, if skip_declarations is set, then we may have skipped
3107 * declarations inside "stmt" and so the pet_scop may not represent
3108 * the entire "stmt".
3109 * Note that this function may be called with "stmt" referring to the entire
3110 * body of the function, including the outer braces. In such cases,
3111 * skip_declarations will be set and the braces will not be taken into
3112 * account in scop->start and scop->end.
3114 struct pet_scop
*PetScan::extract(Stmt
*stmt
, __isl_keep pet_context
*pc
,
3115 bool skip_declarations
)
3117 struct pet_scop
*scop
;
3120 if (isa
<Expr
>(stmt
))
3121 return extract(extract_expr(cast
<Expr
>(stmt
)),
3122 stmt
->getSourceRange(), true, pc
);
3124 switch (stmt
->getStmtClass()) {
3125 case Stmt::WhileStmtClass
:
3126 scop
= extract(cast
<WhileStmt
>(stmt
), pc
);
3128 case Stmt::ForStmtClass
:
3129 scop
= extract_for(cast
<ForStmt
>(stmt
), pc
);
3131 case Stmt::IfStmtClass
:
3132 scop
= extract(cast
<IfStmt
>(stmt
), pc
);
3134 case Stmt::CompoundStmtClass
:
3135 scop
= extract(cast
<CompoundStmt
>(stmt
), pc
, skip_declarations
);
3137 case Stmt::LabelStmtClass
:
3138 scop
= extract(cast
<LabelStmt
>(stmt
), pc
);
3140 case Stmt::ContinueStmtClass
:
3141 scop
= extract(cast
<ContinueStmt
>(stmt
));
3143 case Stmt::BreakStmtClass
:
3144 scop
= extract(cast
<BreakStmt
>(stmt
));
3146 case Stmt::DeclStmtClass
:
3147 scop
= extract(cast
<DeclStmt
>(stmt
), pc
);
3154 if (partial
|| skip_declarations
)
3157 loc
= construct_pet_loc(stmt
->getSourceRange(), false);
3158 scop
= pet_scop_update_start_end_from_loc(scop
, loc
);
3164 /* Extract a clone of the kill statement in "scop".
3165 * "scop" is expected to have been created from a DeclStmt
3166 * and should have the kill as its first statement.
3168 struct pet_stmt
*PetScan::extract_kill(struct pet_scop
*scop
)
3171 struct pet_stmt
*stmt
;
3172 isl_multi_pw_aff
*index
;
3179 if (scop
->n_stmt
< 1)
3180 isl_die(ctx
, isl_error_internal
,
3181 "expecting at least one statement", return NULL
);
3182 stmt
= scop
->stmts
[0];
3183 if (!pet_stmt_is_kill(stmt
))
3184 isl_die(ctx
, isl_error_internal
,
3185 "expecting kill statement", return NULL
);
3187 arg
= pet_expr_get_arg(stmt
->body
, 0);
3188 index
= pet_expr_access_get_index(arg
);
3189 access
= pet_expr_access_get_access(arg
);
3191 index
= isl_multi_pw_aff_reset_tuple_id(index
, isl_dim_in
);
3192 access
= isl_map_reset_tuple_id(access
, isl_dim_in
);
3193 kill
= pet_expr_kill_from_access_and_index(access
, index
);
3194 loc
= pet_loc_copy(stmt
->loc
);
3195 return pet_stmt_from_pet_expr(loc
, NULL
, n_stmt
++, kill
);
3198 /* Mark all arrays in "scop" as being exposed.
3200 static struct pet_scop
*mark_exposed(struct pet_scop
*scop
)
3204 for (int i
= 0; i
< scop
->n_array
; ++i
)
3205 scop
->arrays
[i
]->exposed
= 1;
3209 /* Try and construct a pet_scop corresponding to (part of)
3210 * a sequence of statements within the context "pc".
3212 * "block" is set if the sequence respresents the children of
3213 * a compound statement.
3214 * "skip_declarations" is set if we should skip initial declarations
3215 * in the sequence of statements.
3217 * After extracting a statement, we update "pc"
3218 * based on the top-level assignments in the statement
3219 * so that we can exploit them in subsequent statements in the same block.
3221 * If there are any breaks or continues in the individual statements,
3222 * then we may have to compute a new skip condition.
3223 * This is handled using a pet_skip_info object.
3224 * On initialization, the object checks if skip conditions need
3225 * to be computed. If so, it does so in pet_skip_info_seq_extract and
3226 * adds them in pet_skip_info_seq_add.
3228 * If "block" is set, then we need to insert kill statements at
3229 * the end of the block for any array that has been declared by
3230 * one of the statements in the sequence. Each of these declarations
3231 * results in the construction of a kill statement at the place
3232 * of the declaration, so we simply collect duplicates of
3233 * those kill statements and append these duplicates to the constructed scop.
3235 * If "block" is not set, then any array declared by one of the statements
3236 * in the sequence is marked as being exposed.
3238 * If autodetect is set, then we allow the extraction of only a subrange
3239 * of the sequence of statements. However, if there is at least one statement
3240 * for which we could not construct a scop and the final range contains
3241 * either no statements or at least one kill, then we discard the entire
3244 struct pet_scop
*PetScan::extract(StmtRange stmt_range
, bool block
,
3245 bool skip_declarations
, __isl_keep pet_context
*pc
)
3251 bool partial_range
= false;
3252 set
<struct pet_stmt
*> kills
;
3253 set
<struct pet_stmt
*>::iterator it
;
3255 int_size
= ast_context
.getTypeInfo(ast_context
.IntTy
).first
/ 8;
3257 pc
= pet_context_copy(pc
);
3258 scop
= pet_scop_empty(ctx
);
3259 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
) {
3261 struct pet_scop
*scop_i
;
3263 if (scop
->n_stmt
== 0 && skip_declarations
&&
3264 child
->getStmtClass() == Stmt::DeclStmtClass
)
3267 scop_i
= extract(child
, pc
);
3268 if (scop
->n_stmt
!= 0 && partial
) {
3269 pet_scop_free(scop_i
);
3272 pc
= handle_writes(scop_i
, pc
);
3274 pet_skip_info_seq_init(&skip
, ctx
, scop
, scop_i
);
3275 pet_skip_info_seq_extract(&skip
, int_size
, &n_stmt
, &n_test
);
3276 if (pet_skip_info_has_skip(&skip
))
3277 scop_i
= pet_scop_prefix(scop_i
, 0);
3278 if (scop_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
) {
3280 kills
.insert(extract_kill(scop_i
));
3282 scop_i
= mark_exposed(scop_i
);
3284 scop_i
= pet_scop_prefix(scop_i
, j
);
3285 if (options
->autodetect
) {
3287 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
3289 partial_range
= true;
3290 if (scop
->n_stmt
!= 0 && !scop_i
)
3293 scop
= pet_scop_add_seq(ctx
, scop
, scop_i
);
3296 scop
= pet_skip_info_seq_add(&skip
, scop
, j
);
3298 if (partial
|| !scop
)
3302 for (it
= kills
.begin(); it
!= kills
.end(); ++it
) {
3304 scop_j
= pet_scop_from_pet_stmt(ctx
, *it
);
3305 scop_j
= pet_scop_prefix(scop_j
, j
);
3306 scop
= pet_scop_add_seq(ctx
, scop
, scop_j
);
3309 pet_context_free(pc
);
3311 if (scop
&& partial_range
) {
3312 if (scop
->n_stmt
== 0 || kills
.size() != 0) {
3313 pet_scop_free(scop
);
3322 /* Check if the scop marked by the user is exactly this Stmt
3323 * or part of this Stmt.
3324 * If so, return a pet_scop corresponding to the marked region.
3325 * The pet_scop is created within the context "pc".
3326 * Otherwise, return NULL.
3328 struct pet_scop
*PetScan::scan(Stmt
*stmt
, __isl_keep pet_context
*pc
)
3330 SourceManager
&SM
= PP
.getSourceManager();
3331 unsigned start_off
, end_off
;
3333 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
3334 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
3336 if (start_off
> loc
.end
)
3338 if (end_off
< loc
.start
)
3340 if (start_off
>= loc
.start
&& end_off
<= loc
.end
) {
3341 return extract(stmt
, pc
);
3345 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
3346 Stmt
*child
= *start
;
3349 start_off
= getExpansionOffset(SM
, child
->getLocStart());
3350 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
3351 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
3352 return scan(child
, pc
);
3353 if (start_off
>= loc
.start
)
3358 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
3360 start_off
= SM
.getFileOffset(child
->getLocStart());
3361 if (start_off
>= loc
.end
)
3365 return extract(StmtRange(start
, end
), false, false, pc
);
3368 /* Set the size of index "pos" of "array" to "size".
3369 * In particular, add a constraint of the form
3373 * to array->extent and a constraint of the form
3377 * to array->context.
3379 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
3380 __isl_take isl_pw_aff
*size
)
3393 valid
= isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
)));
3394 array
->context
= isl_set_intersect(array
->context
, valid
);
3396 dim
= isl_set_get_space(array
->extent
);
3397 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
3398 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
3399 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
3400 index
= isl_pw_aff_alloc(univ
, aff
);
3402 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
3403 isl_set_dim(array
->extent
, isl_dim_set
));
3404 id
= isl_set_get_tuple_id(array
->extent
);
3405 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
3406 bound
= isl_pw_aff_lt_set(index
, size
);
3408 array
->extent
= isl_set_intersect(array
->extent
, bound
);
3410 if (!array
->context
|| !array
->extent
)
3411 return pet_array_free(array
);
3415 isl_pw_aff_free(size
);
3419 /* Figure out the size of the array at position "pos" and all
3420 * subsequent positions from "type" and update the corresponding
3421 * argument of "expr" accordingly.
3423 __isl_give pet_expr
*PetScan::set_upper_bounds(__isl_take pet_expr
*expr
,
3424 const Type
*type
, int pos
)
3426 const ArrayType
*atype
;
3432 if (type
->isPointerType()) {
3433 type
= type
->getPointeeType().getTypePtr();
3434 return set_upper_bounds(expr
, type
, pos
+ 1);
3436 if (!type
->isArrayType())
3439 type
= type
->getCanonicalTypeInternal().getTypePtr();
3440 atype
= cast
<ArrayType
>(type
);
3442 if (type
->isConstantArrayType()) {
3443 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
3444 size
= extract_expr(ca
->getSize());
3445 expr
= pet_expr_set_arg(expr
, pos
, size
);
3446 } else if (type
->isVariableArrayType()) {
3447 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
3448 size
= extract_expr(vla
->getSizeExpr());
3449 expr
= pet_expr_set_arg(expr
, pos
, size
);
3452 type
= atype
->getElementType().getTypePtr();
3454 return set_upper_bounds(expr
, type
, pos
+ 1);
3457 /* Does "expr" represent the "integer" infinity?
3459 static int is_infty(__isl_keep pet_expr
*expr
)
3464 if (pet_expr_get_type(expr
) != pet_expr_int
)
3466 v
= pet_expr_int_get_val(expr
);
3467 res
= isl_val_is_infty(v
);
3473 /* Figure out the dimensions of an array "array" based on its type
3474 * "type" and update "array" accordingly.
3476 * We first construct a pet_expr that holds the sizes of the array
3477 * in each dimension. The expression is initialized to infinity
3478 * and updated from the type.
3480 * The arguments of the size expression that have been updated
3481 * are then converted to an affine expression within the context "pc" and
3482 * incorporated into the size of "array". If we are unable to convert
3483 * a size expression to an affine expression, then we leave
3484 * the corresponding size of "array" untouched.
3486 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
3487 const Type
*type
, __isl_keep pet_context
*pc
)
3489 int depth
= array_depth(type
);
3490 pet_expr
*expr
, *inf
;
3495 inf
= pet_expr_new_int(isl_val_infty(ctx
));
3496 expr
= pet_expr_new_call(ctx
, "bounds", depth
);
3497 for (int i
= 0; i
< depth
; ++i
)
3498 expr
= pet_expr_set_arg(expr
, i
, pet_expr_copy(inf
));
3501 expr
= set_upper_bounds(expr
, type
, 0);
3503 for (int i
= 0; i
< depth
; ++i
) {
3507 arg
= pet_expr_get_arg(expr
, i
);
3508 if (!is_infty(arg
)) {
3509 size
= pet_expr_extract_affine(arg
, pc
);
3511 array
= pet_array_free(array
);
3512 else if (isl_pw_aff_involves_nan(size
))
3513 isl_pw_aff_free(size
);
3515 array
= update_size(array
, i
, size
);
3519 pet_expr_free(expr
);
3524 /* Is "T" the type of a variable length array with static size?
3526 static bool is_vla_with_static_size(QualType T
)
3528 const VariableArrayType
*vlatype
;
3530 if (!T
->isVariableArrayType())
3532 vlatype
= cast
<VariableArrayType
>(T
);
3533 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
3536 /* Return the type of "decl" as an array.
3538 * In particular, if "decl" is a parameter declaration that
3539 * is a variable length array with a static size, then
3540 * return the original type (i.e., the variable length array).
3541 * Otherwise, return the type of decl.
3543 static QualType
get_array_type(ValueDecl
*decl
)
3548 parm
= dyn_cast
<ParmVarDecl
>(decl
);
3550 return decl
->getType();
3552 T
= parm
->getOriginalType();
3553 if (!is_vla_with_static_size(T
))
3554 return decl
->getType();
3558 /* Does "decl" have definition that we can keep track of in a pet_type?
3560 static bool has_printable_definition(RecordDecl
*decl
)
3562 if (!decl
->getDeclName())
3564 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
3567 /* Construct and return a pet_array corresponding to the variable "decl".
3568 * In particular, initialize array->extent to
3570 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
3572 * and then call set_upper_bounds to set the upper bounds on the indices
3573 * based on the type of the variable. The upper bounds are converted
3574 * to affine expressions within the context "pc".
3576 * If the base type is that of a record with a top-level definition and
3577 * if "types" is not null, then the RecordDecl corresponding to the type
3578 * is added to "types".
3580 * If the base type is that of a record with no top-level definition,
3581 * then we replace it by "<subfield>".
3583 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
,
3584 lex_recorddecl_set
*types
, __isl_keep pet_context
*pc
)
3586 struct pet_array
*array
;
3587 QualType qt
= get_array_type(decl
);
3588 const Type
*type
= qt
.getTypePtr();
3589 int depth
= array_depth(type
);
3590 QualType base
= pet_clang_base_type(qt
);
3595 array
= isl_calloc_type(ctx
, struct pet_array
);
3599 id
= create_decl_id(ctx
, decl
);
3600 dim
= isl_space_set_alloc(ctx
, 0, depth
);
3601 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
3603 array
->extent
= isl_set_nat_universe(dim
);
3605 dim
= isl_space_params_alloc(ctx
, 0);
3606 array
->context
= isl_set_universe(dim
);
3608 array
= set_upper_bounds(array
, type
, pc
);
3612 name
= base
.getAsString();
3614 if (types
&& base
->isRecordType()) {
3615 RecordDecl
*decl
= pet_clang_record_decl(base
);
3616 if (has_printable_definition(decl
))
3617 types
->insert(decl
);
3619 name
= "<subfield>";
3622 array
->element_type
= strdup(name
.c_str());
3623 array
->element_is_record
= base
->isRecordType();
3624 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
3629 /* Construct and return a pet_array corresponding to the sequence
3630 * of declarations "decls".
3631 * The upper bounds of the array are converted to affine expressions
3632 * within the context "pc".
3633 * If the sequence contains a single declaration, then it corresponds
3634 * to a simple array access. Otherwise, it corresponds to a member access,
3635 * with the declaration for the substructure following that of the containing
3636 * structure in the sequence of declarations.
3637 * We start with the outermost substructure and then combine it with
3638 * information from the inner structures.
3640 * Additionally, keep track of all required types in "types".
3642 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
,
3643 vector
<ValueDecl
*> decls
, lex_recorddecl_set
*types
,
3644 __isl_keep pet_context
*pc
)
3646 struct pet_array
*array
;
3647 vector
<ValueDecl
*>::iterator it
;
3651 array
= extract_array(ctx
, *it
, types
, pc
);
3653 for (++it
; it
!= decls
.end(); ++it
) {
3654 struct pet_array
*parent
;
3655 const char *base_name
, *field_name
;
3659 array
= extract_array(ctx
, *it
, types
, pc
);
3661 return pet_array_free(parent
);
3663 base_name
= isl_set_get_tuple_name(parent
->extent
);
3664 field_name
= isl_set_get_tuple_name(array
->extent
);
3665 product_name
= pet_array_member_access_name(ctx
,
3666 base_name
, field_name
);
3668 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
3671 array
->extent
= isl_set_set_tuple_name(array
->extent
,
3673 array
->context
= isl_set_intersect(array
->context
,
3674 isl_set_copy(parent
->context
));
3676 pet_array_free(parent
);
3679 if (!array
->extent
|| !array
->context
|| !product_name
)
3680 return pet_array_free(array
);
3686 /* Add a pet_type corresponding to "decl" to "scop, provided
3687 * it is a member of "types" and it has not been added before
3688 * (i.e., it is not a member of "types_done".
3690 * Since we want the user to be able to print the types
3691 * in the order in which they appear in the scop, we need to
3692 * make sure that types of fields in a structure appear before
3693 * that structure. We therefore call ourselves recursively
3694 * on the types of all record subfields.
3696 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3697 RecordDecl
*decl
, Preprocessor
&PP
, lex_recorddecl_set
&types
,
3698 lex_recorddecl_set
&types_done
)
3701 llvm::raw_string_ostream
S(s
);
3702 RecordDecl::field_iterator it
;
3704 if (types
.find(decl
) == types
.end())
3706 if (types_done
.find(decl
) != types_done
.end())
3709 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
3711 QualType type
= it
->getType();
3713 if (!type
->isRecordType())
3715 record
= pet_clang_record_decl(type
);
3716 scop
= add_type(ctx
, scop
, record
, PP
, types
, types_done
);
3719 if (strlen(decl
->getName().str().c_str()) == 0)
3722 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3725 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3726 decl
->getName().str().c_str(), s
.c_str());
3727 if (!scop
->types
[scop
->n_type
])
3728 return pet_scop_free(scop
);
3730 types_done
.insert(decl
);
3737 /* Construct a list of pet_arrays, one for each array (or scalar)
3738 * accessed inside "scop", add this list to "scop" and return the result.
3739 * The upper bounds of the arrays are converted to affine expressions
3740 * within the context "pc".
3742 * The context of "scop" is updated with the intersection of
3743 * the contexts of all arrays, i.e., constraints on the parameters
3744 * that ensure that the arrays have a valid (non-negative) size.
3746 * If the any of the extracted arrays refers to a member access,
3747 * then also add the required types to "scop".
3749 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
,
3750 __isl_keep pet_context
*pc
)
3753 array_desc_set arrays
;
3754 array_desc_set::iterator it
;
3755 lex_recorddecl_set types
;
3756 lex_recorddecl_set types_done
;
3757 lex_recorddecl_set::iterator types_it
;
3759 struct pet_array
**scop_arrays
;
3764 pet_scop_collect_arrays(scop
, arrays
);
3765 if (arrays
.size() == 0)
3768 n_array
= scop
->n_array
;
3770 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3771 n_array
+ arrays
.size());
3774 scop
->arrays
= scop_arrays
;
3776 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
3777 struct pet_array
*array
;
3778 array
= extract_array(ctx
, *it
, &types
, pc
);
3779 scop
->arrays
[n_array
+ i
] = array
;
3780 if (!scop
->arrays
[n_array
+ i
])
3783 scop
->context
= isl_set_intersect(scop
->context
,
3784 isl_set_copy(array
->context
));
3789 if (types
.size() == 0)
3792 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, types
.size());
3796 for (types_it
= types
.begin(); types_it
!= types
.end(); ++types_it
)
3797 scop
= add_type(ctx
, scop
, *types_it
, PP
, types
, types_done
);
3801 pet_scop_free(scop
);
3805 /* Bound all parameters in scop->context to the possible values
3806 * of the corresponding C variable.
3808 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
3815 n
= isl_set_dim(scop
->context
, isl_dim_param
);
3816 for (int i
= 0; i
< n
; ++i
) {
3820 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
3821 if (pet_nested_in_id(id
)) {
3823 isl_die(isl_set_get_ctx(scop
->context
),
3825 "unresolved nested parameter", goto error
);
3827 decl
= (ValueDecl
*) isl_id_get_user(id
);
3830 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
3838 pet_scop_free(scop
);
3842 /* Construct a pet_scop from the given function.
3844 * If the scop was delimited by scop and endscop pragmas, then we override
3845 * the file offsets by those derived from the pragmas.
3847 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
3853 stmt
= fd
->getBody();
3855 pc
= pet_context_alloc(isl_space_set_alloc(ctx
, 0, 0));
3856 if (options
->autodetect
) {
3857 scop
= extract(stmt
, pc
, true);
3859 scop
= scan(stmt
, pc
);
3860 scop
= pet_scop_update_start_end(scop
, loc
.start
, loc
.end
);
3862 scop
= pet_scop_detect_parameter_accesses(scop
);
3863 scop
= scan_arrays(scop
, pc
);
3864 pet_context_free(pc
);
3865 scop
= add_parameter_bounds(scop
);
3866 scop
= pet_scop_gist(scop
, value_bounds
);