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/Attr.h>
43 #include <clang/AST/Expr.h>
44 #include <clang/AST/RecursiveASTVisitor.h>
47 #include <isl/space.h>
60 #include "scop_plus.h"
62 #include "tree2scop.h"
67 using namespace clang
;
69 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
79 return pet_op_post_inc
;
81 return pet_op_post_dec
;
83 return pet_op_pre_inc
;
85 return pet_op_pre_dec
;
91 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
95 return pet_op_add_assign
;
97 return pet_op_sub_assign
;
99 return pet_op_mul_assign
;
101 return pet_op_div_assign
;
103 return pet_op_assign
;
145 #if defined(DECLREFEXPR_CREATE_REQUIRES_BOOL)
146 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
148 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
149 SourceLocation(), var
, false, var
->getInnerLocStart(),
150 var
->getType(), VK_LValue
);
152 #elif defined(DECLREFEXPR_CREATE_REQUIRES_SOURCELOCATION)
153 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
155 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
156 SourceLocation(), var
, var
->getInnerLocStart(), var
->getType(),
160 static DeclRefExpr
*create_DeclRefExpr(VarDecl
*var
)
162 return DeclRefExpr::Create(var
->getASTContext(), var
->getQualifierLoc(),
163 var
, var
->getInnerLocStart(), var
->getType(), VK_LValue
);
167 /* Check if the element type corresponding to the given array type
168 * has a const qualifier.
170 static bool const_base(QualType qt
)
172 const Type
*type
= qt
.getTypePtr();
174 if (type
->isPointerType())
175 return const_base(type
->getPointeeType());
176 if (type
->isArrayType()) {
177 const ArrayType
*atype
;
178 type
= type
->getCanonicalTypeInternal().getTypePtr();
179 atype
= cast
<ArrayType
>(type
);
180 return const_base(atype
->getElementType());
183 return qt
.isConstQualified();
186 /* Create an isl_id that refers to the named declarator "decl".
188 static __isl_give isl_id
*create_decl_id(isl_ctx
*ctx
, NamedDecl
*decl
)
190 return isl_id_alloc(ctx
, decl
->getName().str().c_str(), decl
);
195 std::map
<const Type
*, pet_expr
*>::iterator it
;
196 std::map
<FunctionDecl
*, pet_function_summary
*>::iterator it_s
;
198 for (it
= type_size
.begin(); it
!= type_size
.end(); ++it
)
199 pet_expr_free(it
->second
);
200 for (it_s
= summary_cache
.begin(); it_s
!= summary_cache
.end(); ++it_s
)
201 pet_function_summary_free(it_s
->second
);
203 isl_union_map_free(value_bounds
);
206 /* Report a diagnostic, unless autodetect is set.
208 void PetScan::report(Stmt
*stmt
, unsigned id
)
210 if (options
->autodetect
)
213 SourceLocation loc
= stmt
->getLocStart();
214 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
215 DiagnosticBuilder B
= diag
.Report(loc
, id
) << stmt
->getSourceRange();
218 /* Called if we found something we (currently) cannot handle.
219 * We'll provide more informative warnings later.
221 * We only actually complain if autodetect is false.
223 void PetScan::unsupported(Stmt
*stmt
)
225 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
226 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
231 /* Report a missing prototype, unless autodetect is set.
233 void PetScan::report_prototype_required(Stmt
*stmt
)
235 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
236 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
237 "prototype required");
241 /* Report a missing increment, unless autodetect is set.
243 void PetScan::report_missing_increment(Stmt
*stmt
)
245 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
246 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
247 "missing increment");
251 /* Report a missing summary function, unless autodetect is set.
253 void PetScan::report_missing_summary_function(Stmt
*stmt
)
255 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
256 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
257 "missing summary function");
261 /* Report a missing summary function body, unless autodetect is set.
263 void PetScan::report_missing_summary_function_body(Stmt
*stmt
)
265 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
266 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
267 "missing summary function body");
271 /* Extract an integer from "val", which is assumed to be non-negative.
273 static __isl_give isl_val
*extract_unsigned(isl_ctx
*ctx
,
274 const llvm::APInt
&val
)
277 const uint64_t *data
;
279 data
= val
.getRawData();
280 n
= val
.getNumWords();
281 return isl_val_int_from_chunks(ctx
, n
, sizeof(uint64_t), data
);
284 /* Extract an integer from "val". If "is_signed" is set, then "val"
285 * is signed. Otherwise it it unsigned.
287 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
, bool is_signed
,
290 int is_negative
= is_signed
&& val
.isNegative();
296 v
= extract_unsigned(ctx
, val
);
303 /* Extract an integer from "expr".
305 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
307 const Type
*type
= expr
->getType().getTypePtr();
308 bool is_signed
= type
->hasSignedIntegerRepresentation();
310 return ::extract_int(ctx
, is_signed
, expr
->getValue());
313 /* Extract an integer from "expr".
314 * Return NULL if "expr" does not (obviously) represent an integer.
316 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
318 return extract_int(expr
->getSubExpr());
321 /* Extract an integer from "expr".
322 * Return NULL if "expr" does not (obviously) represent an integer.
324 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
326 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
327 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
328 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
329 return extract_int(cast
<ParenExpr
>(expr
));
335 /* Extract a pet_expr from the APInt "val", which is assumed
336 * to be non-negative.
338 __isl_give pet_expr
*PetScan::extract_expr(const llvm::APInt
&val
)
340 return pet_expr_new_int(extract_unsigned(ctx
, val
));
343 /* Return the number of bits needed to represent the type "qt",
344 * if it is an integer type. Otherwise return 0.
345 * If qt is signed then return the opposite of the number of bits.
347 static int get_type_size(QualType qt
, ASTContext
&ast_context
)
351 if (!qt
->isIntegerType())
354 size
= ast_context
.getIntWidth(qt
);
355 if (!qt
->isUnsignedIntegerType())
361 /* Return the number of bits needed to represent the type of "decl",
362 * if it is an integer type. Otherwise return 0.
363 * If qt is signed then return the opposite of the number of bits.
365 static int get_type_size(ValueDecl
*decl
)
367 return get_type_size(decl
->getType(), decl
->getASTContext());
370 /* Bound parameter "pos" of "set" to the possible values of "decl".
372 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
373 unsigned pos
, ValueDecl
*decl
)
379 ctx
= isl_set_get_ctx(set
);
380 type_size
= get_type_size(decl
);
382 isl_die(ctx
, isl_error_invalid
, "not an integer type",
383 return isl_set_free(set
));
385 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
386 bound
= isl_val_int_from_ui(ctx
, type_size
);
387 bound
= isl_val_2exp(bound
);
388 bound
= isl_val_sub_ui(bound
, 1);
389 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
391 bound
= isl_val_int_from_ui(ctx
, -type_size
- 1);
392 bound
= isl_val_2exp(bound
);
393 bound
= isl_val_sub_ui(bound
, 1);
394 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
395 isl_val_copy(bound
));
396 bound
= isl_val_neg(bound
);
397 bound
= isl_val_sub_ui(bound
, 1);
398 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
404 __isl_give pet_expr
*PetScan::extract_index_expr(ImplicitCastExpr
*expr
)
406 return extract_index_expr(expr
->getSubExpr());
409 /* Return the depth of an array of the given type.
411 static int array_depth(const Type
*type
)
413 if (type
->isPointerType())
414 return 1 + array_depth(type
->getPointeeType().getTypePtr());
415 if (type
->isArrayType()) {
416 const ArrayType
*atype
;
417 type
= type
->getCanonicalTypeInternal().getTypePtr();
418 atype
= cast
<ArrayType
>(type
);
419 return 1 + array_depth(atype
->getElementType().getTypePtr());
424 /* Return the depth of the array accessed by the index expression "index".
425 * If "index" is an affine expression, i.e., if it does not access
426 * any array, then return 1.
427 * If "index" represent a member access, i.e., if its range is a wrapped
428 * relation, then return the sum of the depth of the array of structures
429 * and that of the member inside the structure.
431 static int extract_depth(__isl_keep isl_multi_pw_aff
*index
)
439 if (isl_multi_pw_aff_range_is_wrapping(index
)) {
440 int domain_depth
, range_depth
;
441 isl_multi_pw_aff
*domain
, *range
;
443 domain
= isl_multi_pw_aff_copy(index
);
444 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
445 domain_depth
= extract_depth(domain
);
446 isl_multi_pw_aff_free(domain
);
447 range
= isl_multi_pw_aff_copy(index
);
448 range
= isl_multi_pw_aff_range_factor_range(range
);
449 range_depth
= extract_depth(range
);
450 isl_multi_pw_aff_free(range
);
452 return domain_depth
+ range_depth
;
455 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
458 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
461 decl
= (ValueDecl
*) isl_id_get_user(id
);
464 return array_depth(decl
->getType().getTypePtr());
467 /* Return the depth of the array accessed by the access expression "expr".
469 static int extract_depth(__isl_keep pet_expr
*expr
)
471 isl_multi_pw_aff
*index
;
474 index
= pet_expr_access_get_index(expr
);
475 depth
= extract_depth(index
);
476 isl_multi_pw_aff_free(index
);
481 /* Construct a pet_expr representing an index expression for an access
482 * to the variable referenced by "expr".
484 * If "expr" references an enum constant, then return an integer expression
485 * instead, representing the value of the enum constant.
487 __isl_give pet_expr
*PetScan::extract_index_expr(DeclRefExpr
*expr
)
489 return extract_index_expr(expr
->getDecl());
492 /* Construct a pet_expr representing an index expression for an access
493 * to the variable "decl".
495 * If "decl" is an enum constant, then we return an integer expression
496 * instead, representing the value of the enum constant.
498 __isl_give pet_expr
*PetScan::extract_index_expr(ValueDecl
*decl
)
503 if (isa
<EnumConstantDecl
>(decl
))
504 return extract_expr(cast
<EnumConstantDecl
>(decl
));
506 id
= create_decl_id(ctx
, decl
);
507 space
= isl_space_alloc(ctx
, 0, 0, 0);
508 space
= isl_space_set_tuple_id(space
, isl_dim_out
, id
);
510 return pet_expr_from_index(isl_multi_pw_aff_zero(space
));
513 /* Construct a pet_expr representing the index expression "expr"
514 * Return NULL on error.
516 * If "expr" is a reference to an enum constant, then return
517 * an integer expression instead, representing the value of the enum constant.
519 __isl_give pet_expr
*PetScan::extract_index_expr(Expr
*expr
)
521 switch (expr
->getStmtClass()) {
522 case Stmt::ImplicitCastExprClass
:
523 return extract_index_expr(cast
<ImplicitCastExpr
>(expr
));
524 case Stmt::DeclRefExprClass
:
525 return extract_index_expr(cast
<DeclRefExpr
>(expr
));
526 case Stmt::ArraySubscriptExprClass
:
527 return extract_index_expr(cast
<ArraySubscriptExpr
>(expr
));
528 case Stmt::IntegerLiteralClass
:
529 return extract_expr(cast
<IntegerLiteral
>(expr
));
530 case Stmt::MemberExprClass
:
531 return extract_index_expr(cast
<MemberExpr
>(expr
));
538 /* Extract an index expression from the given array subscript expression.
540 * We first extract an index expression from the base.
541 * This will result in an index expression with a range that corresponds
542 * to the earlier indices.
543 * We then extract the current index and let
544 * pet_expr_access_subscript combine the two.
546 __isl_give pet_expr
*PetScan::extract_index_expr(ArraySubscriptExpr
*expr
)
548 Expr
*base
= expr
->getBase();
549 Expr
*idx
= expr
->getIdx();
553 base_expr
= extract_index_expr(base
);
554 index
= extract_expr(idx
);
556 base_expr
= pet_expr_access_subscript(base_expr
, index
);
561 /* Extract an index expression from a member expression.
563 * If the base access (to the structure containing the member)
568 * and the member is called "f", then the member access is of
573 * If the member access is to an anonymous struct, then simply return
577 * If the member access in the source code is of the form
581 * then it is treated as
585 __isl_give pet_expr
*PetScan::extract_index_expr(MemberExpr
*expr
)
587 Expr
*base
= expr
->getBase();
588 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
589 pet_expr
*base_index
;
592 base_index
= extract_index_expr(base
);
594 if (expr
->isArrow()) {
595 pet_expr
*index
= pet_expr_new_int(isl_val_zero(ctx
));
596 base_index
= pet_expr_access_subscript(base_index
, index
);
599 if (field
->isAnonymousStructOrUnion())
602 id
= create_decl_id(ctx
, field
);
604 return pet_expr_access_member(base_index
, id
);
607 /* Mark the given access pet_expr as a write.
609 static __isl_give pet_expr
*mark_write(__isl_take pet_expr
*access
)
611 access
= pet_expr_access_set_write(access
, 1);
612 access
= pet_expr_access_set_read(access
, 0);
617 /* Construct a pet_expr representing a unary operator expression.
619 __isl_give pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
624 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
625 if (op
== pet_op_last
) {
630 arg
= extract_expr(expr
->getSubExpr());
632 if (expr
->isIncrementDecrementOp() &&
633 pet_expr_get_type(arg
) == pet_expr_access
) {
634 arg
= mark_write(arg
);
635 arg
= pet_expr_access_set_read(arg
, 1);
638 return pet_expr_new_unary(op
, arg
);
641 /* Construct a pet_expr representing a binary operator expression.
643 * If the top level operator is an assignment and the LHS is an access,
644 * then we mark that access as a write. If the operator is a compound
645 * assignment, the access is marked as both a read and a write.
647 __isl_give pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
653 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
654 if (op
== pet_op_last
) {
659 lhs
= extract_expr(expr
->getLHS());
660 rhs
= extract_expr(expr
->getRHS());
662 if (expr
->isAssignmentOp() &&
663 pet_expr_get_type(lhs
) == pet_expr_access
) {
664 lhs
= mark_write(lhs
);
665 if (expr
->isCompoundAssignmentOp())
666 lhs
= pet_expr_access_set_read(lhs
, 1);
669 type_size
= get_type_size(expr
->getType(), ast_context
);
670 return pet_expr_new_binary(type_size
, op
, lhs
, rhs
);
673 /* Construct a pet_tree for a (single) variable declaration.
675 __isl_give pet_tree
*PetScan::extract(DeclStmt
*stmt
)
682 if (!stmt
->isSingleDecl()) {
687 decl
= stmt
->getSingleDecl();
688 vd
= cast
<VarDecl
>(decl
);
690 lhs
= extract_access_expr(vd
);
691 lhs
= mark_write(lhs
);
693 tree
= pet_tree_new_decl(lhs
);
695 rhs
= extract_expr(vd
->getInit());
696 tree
= pet_tree_new_decl_init(lhs
, rhs
);
702 /* Construct a pet_expr representing a conditional operation.
704 __isl_give pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
706 pet_expr
*cond
, *lhs
, *rhs
;
709 cond
= extract_expr(expr
->getCond());
710 lhs
= extract_expr(expr
->getTrueExpr());
711 rhs
= extract_expr(expr
->getFalseExpr());
713 return pet_expr_new_ternary(cond
, lhs
, rhs
);
716 __isl_give pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
718 return extract_expr(expr
->getSubExpr());
721 /* Construct a pet_expr representing a floating point value.
723 * If the floating point literal does not appear in a macro,
724 * then we use the original representation in the source code
725 * as the string representation. Otherwise, we use the pretty
726 * printer to produce a string representation.
728 __isl_give pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
732 const LangOptions
&LO
= PP
.getLangOpts();
733 SourceLocation loc
= expr
->getLocation();
735 if (!loc
.isMacroID()) {
736 SourceManager
&SM
= PP
.getSourceManager();
737 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
738 s
= string(SM
.getCharacterData(loc
), len
);
740 llvm::raw_string_ostream
S(s
);
741 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
744 d
= expr
->getValueAsApproximateDouble();
745 return pet_expr_new_double(ctx
, d
, s
.c_str());
748 /* Convert the index expression "index" into an access pet_expr of type "qt".
750 __isl_give pet_expr
*PetScan::extract_access_expr(QualType qt
,
751 __isl_take pet_expr
*index
)
756 depth
= extract_depth(index
);
757 type_size
= get_type_size(qt
, ast_context
);
759 index
= pet_expr_set_type_size(index
, type_size
);
760 index
= pet_expr_access_set_depth(index
, depth
);
765 /* Extract an index expression from "expr" and then convert it into
766 * an access pet_expr.
768 * If "expr" is a reference to an enum constant, then return
769 * an integer expression instead, representing the value of the enum constant.
771 __isl_give pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
775 index
= extract_index_expr(expr
);
777 if (pet_expr_get_type(index
) == pet_expr_int
)
780 return extract_access_expr(expr
->getType(), index
);
783 /* Extract an index expression from "decl" and then convert it into
784 * an access pet_expr.
786 __isl_give pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
788 return extract_access_expr(decl
->getType(), extract_index_expr(decl
));
791 __isl_give pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
793 return extract_expr(expr
->getSubExpr());
796 /* Extract an assume statement from the argument "expr"
797 * of a __pencil_assume statement.
799 __isl_give pet_expr
*PetScan::extract_assume(Expr
*expr
)
801 return pet_expr_new_unary(pet_op_assume
, extract_expr(expr
));
804 /* Construct a pet_expr corresponding to the function call argument "expr".
805 * The argument appears in position "pos" of a call to function "fd".
807 * If we are passing along a pointer to an array element
808 * or an entire row or even higher dimensional slice of an array,
809 * then the function being called may write into the array.
811 * We assume here that if the function is declared to take a pointer
812 * to a const type, then the function will perform a read
813 * and that otherwise, it will perform a write.
815 __isl_give pet_expr
*PetScan::extract_argument(FunctionDecl
*fd
, int pos
,
819 int is_addr
= 0, is_partial
= 0;
822 if (expr
->getStmtClass() == Stmt::ImplicitCastExprClass
) {
823 ImplicitCastExpr
*ice
= cast
<ImplicitCastExpr
>(expr
);
824 expr
= ice
->getSubExpr();
826 if (expr
->getStmtClass() == Stmt::UnaryOperatorClass
) {
827 UnaryOperator
*op
= cast
<UnaryOperator
>(expr
);
828 if (op
->getOpcode() == UO_AddrOf
) {
830 expr
= op
->getSubExpr();
833 res
= extract_expr(expr
);
836 sc
= expr
->getStmtClass();
837 if ((sc
== Stmt::ArraySubscriptExprClass
||
838 sc
== Stmt::DeclRefExprClass
||
839 sc
== Stmt::MemberExprClass
) &&
840 array_depth(expr
->getType().getTypePtr()) > 0)
842 if ((is_addr
|| is_partial
) &&
843 pet_expr_get_type(res
) == pet_expr_access
) {
845 if (!fd
->hasPrototype()) {
846 report_prototype_required(expr
);
847 return pet_expr_free(res
);
849 parm
= fd
->getParamDecl(pos
);
850 if (!const_base(parm
->getType()))
851 res
= mark_write(res
);
855 res
= pet_expr_new_unary(pet_op_address_of
, res
);
859 /* Find the first FunctionDecl with the given name.
860 * "call" is the corresponding call expression and is only used
861 * for reporting errors.
863 * Return NULL on error.
865 FunctionDecl
*PetScan::find_decl_from_name(CallExpr
*call
, string name
)
867 TranslationUnitDecl
*tu
= ast_context
.getTranslationUnitDecl();
868 DeclContext::decl_iterator begin
= tu
->decls_begin();
869 DeclContext::decl_iterator end
= tu
->decls_end();
870 for (DeclContext::decl_iterator i
= begin
; i
!= end
; ++i
) {
871 FunctionDecl
*fd
= dyn_cast
<FunctionDecl
>(*i
);
874 if (fd
->getName().str().compare(name
) != 0)
878 report_missing_summary_function_body(call
);
881 report_missing_summary_function(call
);
885 /* Return the FunctionDecl for the summary function associated to the
886 * function called by "call".
888 * In particular, search for an annotate attribute formatted as
889 * "pencil_access(name)", where "name" is the name of the summary function.
891 * If no summary function was specified, then return the FunctionDecl
892 * that is actually being called.
894 * Return NULL on error.
896 FunctionDecl
*PetScan::get_summary_function(CallExpr
*call
)
898 FunctionDecl
*decl
= call
->getDirectCallee();
902 specific_attr_iterator
<AnnotateAttr
> begin
, end
, i
;
903 begin
= decl
->specific_attr_begin
<AnnotateAttr
>();
904 end
= decl
->specific_attr_end
<AnnotateAttr
>();
905 for (i
= begin
; i
!= end
; ++i
) {
906 string attr
= (*i
)->getAnnotation().str();
908 const char prefix
[] = "pencil_access(";
909 size_t start
= attr
.find(prefix
);
910 if (start
== string::npos
)
912 start
+= strlen(prefix
);
913 string name
= attr
.substr(start
, attr
.find(')') - start
);
915 return find_decl_from_name(call
, name
);
921 /* Construct a pet_expr representing a function call.
923 * In the special case of a "call" to __pencil_assume,
924 * construct an assume expression instead.
926 __isl_give pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
928 pet_expr
*res
= NULL
;
933 fd
= expr
->getDirectCallee();
939 name
= fd
->getDeclName().getAsString();
940 n_arg
= expr
->getNumArgs();
942 if (n_arg
== 1 && name
== "__pencil_assume")
943 return extract_assume(expr
->getArg(0));
945 res
= pet_expr_new_call(ctx
, name
.c_str(), n_arg
);
949 for (int i
= 0; i
< n_arg
; ++i
) {
950 Expr
*arg
= expr
->getArg(i
);
951 res
= pet_expr_set_arg(res
, i
,
952 PetScan::extract_argument(fd
, i
, arg
));
955 fd
= get_summary_function(expr
);
957 return pet_expr_free(res
);
959 res
= set_summary(res
, fd
);
964 /* Construct a pet_expr representing a (C style) cast.
966 __isl_give pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
971 arg
= extract_expr(expr
->getSubExpr());
975 type
= expr
->getTypeAsWritten();
976 return pet_expr_new_cast(type
.getAsString().c_str(), arg
);
979 /* Construct a pet_expr representing an integer.
981 __isl_give pet_expr
*PetScan::extract_expr(IntegerLiteral
*expr
)
983 return pet_expr_new_int(extract_int(expr
));
986 /* Construct a pet_expr representing the integer enum constant "ecd".
988 __isl_give pet_expr
*PetScan::extract_expr(EnumConstantDecl
*ecd
)
991 const llvm::APSInt
&init
= ecd
->getInitVal();
992 v
= ::extract_int(ctx
, init
.isSigned(), init
);
993 return pet_expr_new_int(v
);
996 /* Try and construct a pet_expr representing "expr".
998 __isl_give pet_expr
*PetScan::extract_expr(Expr
*expr
)
1000 switch (expr
->getStmtClass()) {
1001 case Stmt::UnaryOperatorClass
:
1002 return extract_expr(cast
<UnaryOperator
>(expr
));
1003 case Stmt::CompoundAssignOperatorClass
:
1004 case Stmt::BinaryOperatorClass
:
1005 return extract_expr(cast
<BinaryOperator
>(expr
));
1006 case Stmt::ImplicitCastExprClass
:
1007 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1008 case Stmt::ArraySubscriptExprClass
:
1009 case Stmt::DeclRefExprClass
:
1010 case Stmt::MemberExprClass
:
1011 return extract_access_expr(expr
);
1012 case Stmt::IntegerLiteralClass
:
1013 return extract_expr(cast
<IntegerLiteral
>(expr
));
1014 case Stmt::FloatingLiteralClass
:
1015 return extract_expr(cast
<FloatingLiteral
>(expr
));
1016 case Stmt::ParenExprClass
:
1017 return extract_expr(cast
<ParenExpr
>(expr
));
1018 case Stmt::ConditionalOperatorClass
:
1019 return extract_expr(cast
<ConditionalOperator
>(expr
));
1020 case Stmt::CallExprClass
:
1021 return extract_expr(cast
<CallExpr
>(expr
));
1022 case Stmt::CStyleCastExprClass
:
1023 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1030 /* Check if the given initialization statement is an assignment.
1031 * If so, return that assignment. Otherwise return NULL.
1033 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1035 BinaryOperator
*ass
;
1037 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1040 ass
= cast
<BinaryOperator
>(init
);
1041 if (ass
->getOpcode() != BO_Assign
)
1047 /* Check if the given initialization statement is a declaration
1048 * of a single variable.
1049 * If so, return that declaration. Otherwise return NULL.
1051 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1055 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1058 decl
= cast
<DeclStmt
>(init
);
1060 if (!decl
->isSingleDecl())
1063 return decl
->getSingleDecl();
1066 /* Given the assignment operator in the initialization of a for loop,
1067 * extract the induction variable, i.e., the (integer)variable being
1070 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1077 lhs
= init
->getLHS();
1078 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1083 ref
= cast
<DeclRefExpr
>(lhs
);
1084 decl
= ref
->getDecl();
1085 type
= decl
->getType().getTypePtr();
1087 if (!type
->isIntegerType()) {
1095 /* Given the initialization statement of a for loop and the single
1096 * declaration in this initialization statement,
1097 * extract the induction variable, i.e., the (integer) variable being
1100 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1104 vd
= cast
<VarDecl
>(decl
);
1106 const QualType type
= vd
->getType();
1107 if (!type
->isIntegerType()) {
1112 if (!vd
->getInit()) {
1120 /* Check that op is of the form iv++ or iv--.
1121 * Return a pet_expr representing "1" or "-1" accordingly.
1123 __isl_give pet_expr
*PetScan::extract_unary_increment(
1124 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1130 if (!op
->isIncrementDecrementOp()) {
1135 sub
= op
->getSubExpr();
1136 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1141 ref
= cast
<DeclRefExpr
>(sub
);
1142 if (ref
->getDecl() != iv
) {
1147 if (op
->isIncrementOp())
1148 v
= isl_val_one(ctx
);
1150 v
= isl_val_negone(ctx
);
1152 return pet_expr_new_int(v
);
1155 /* Check if op is of the form
1159 * and return the increment "expr - iv" as a pet_expr.
1161 __isl_give pet_expr
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1162 clang::ValueDecl
*iv
)
1167 pet_expr
*expr
, *expr_iv
;
1169 if (op
->getOpcode() != BO_Assign
) {
1175 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1180 ref
= cast
<DeclRefExpr
>(lhs
);
1181 if (ref
->getDecl() != iv
) {
1186 expr
= extract_expr(op
->getRHS());
1187 expr_iv
= extract_expr(lhs
);
1189 type_size
= get_type_size(iv
->getType(), ast_context
);
1190 return pet_expr_new_binary(type_size
, pet_op_sub
, expr
, expr_iv
);
1193 /* Check that op is of the form iv += cst or iv -= cst
1194 * and return a pet_expr corresponding to cst or -cst accordingly.
1196 __isl_give pet_expr
*PetScan::extract_compound_increment(
1197 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1203 BinaryOperatorKind opcode
;
1205 opcode
= op
->getOpcode();
1206 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1210 if (opcode
== BO_SubAssign
)
1214 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1219 ref
= cast
<DeclRefExpr
>(lhs
);
1220 if (ref
->getDecl() != iv
) {
1225 expr
= extract_expr(op
->getRHS());
1227 expr
= pet_expr_new_unary(pet_op_minus
, expr
);
1232 /* Check that the increment of the given for loop increments
1233 * (or decrements) the induction variable "iv" and return
1234 * the increment as a pet_expr if successful.
1236 __isl_give pet_expr
*PetScan::extract_increment(clang::ForStmt
*stmt
,
1239 Stmt
*inc
= stmt
->getInc();
1242 report_missing_increment(stmt
);
1246 if (inc
->getStmtClass() == Stmt::UnaryOperatorClass
)
1247 return extract_unary_increment(cast
<UnaryOperator
>(inc
), iv
);
1248 if (inc
->getStmtClass() == Stmt::CompoundAssignOperatorClass
)
1249 return extract_compound_increment(
1250 cast
<CompoundAssignOperator
>(inc
), iv
);
1251 if (inc
->getStmtClass() == Stmt::BinaryOperatorClass
)
1252 return extract_binary_increment(cast
<BinaryOperator
>(inc
), iv
);
1258 /* Construct a pet_tree for a while loop.
1260 * If we were only able to extract part of the body, then simply
1263 __isl_give pet_tree
*PetScan::extract(WhileStmt
*stmt
)
1268 tree
= extract(stmt
->getBody());
1271 pe_cond
= extract_expr(stmt
->getCond());
1272 tree
= pet_tree_new_while(pe_cond
, tree
);
1277 /* Construct a pet_tree for a for statement.
1278 * The for loop is required to be of one of the following forms
1280 * for (i = init; condition; ++i)
1281 * for (i = init; condition; --i)
1282 * for (i = init; condition; i += constant)
1283 * for (i = init; condition; i -= constant)
1285 * We extract a pet_tree for the body and then include it in a pet_tree
1286 * of type pet_tree_for.
1288 * As a special case, we also allow a for loop of the form
1292 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1294 * If we were only able to extract part of the body, then simply
1297 __isl_give pet_tree
*PetScan::extract_for(ForStmt
*stmt
)
1299 BinaryOperator
*ass
;
1305 struct pet_scop
*scop
;
1308 pet_expr
*pe_init
, *pe_inc
, *pe_iv
, *pe_cond
;
1310 independent
= is_current_stmt_marked_independent();
1312 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc()) {
1313 tree
= extract(stmt
->getBody());
1316 tree
= pet_tree_new_infinite_loop(tree
);
1320 init
= stmt
->getInit();
1325 if ((ass
= initialization_assignment(init
)) != NULL
) {
1326 iv
= extract_induction_variable(ass
);
1329 lhs
= ass
->getLHS();
1330 rhs
= ass
->getRHS();
1331 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1332 VarDecl
*var
= extract_induction_variable(init
, decl
);
1336 rhs
= var
->getInit();
1337 lhs
= create_DeclRefExpr(var
);
1339 unsupported(stmt
->getInit());
1343 declared
= !initialization_assignment(stmt
->getInit());
1344 tree
= extract(stmt
->getBody());
1347 pe_iv
= extract_access_expr(iv
);
1348 pe_iv
= mark_write(pe_iv
);
1349 pe_init
= extract_expr(rhs
);
1350 if (!stmt
->getCond())
1351 pe_cond
= pet_expr_new_int(isl_val_one(ctx
));
1353 pe_cond
= extract_expr(stmt
->getCond());
1354 pe_inc
= extract_increment(stmt
, iv
);
1355 tree
= pet_tree_new_for(independent
, declared
, pe_iv
, pe_init
, pe_cond
,
1360 /* Try and construct a pet_tree corresponding to a compound statement.
1362 * "skip_declarations" is set if we should skip initial declarations
1363 * in the children of the compound statements. This then implies
1364 * that this sequence of children should not be treated as a block
1365 * since the initial statements may be skipped.
1367 __isl_give pet_tree
*PetScan::extract(CompoundStmt
*stmt
,
1368 bool skip_declarations
)
1370 return extract(stmt
->children(), !skip_declarations
, skip_declarations
);
1373 /* Return the file offset of the expansion location of "Loc".
1375 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
1377 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
1380 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1382 /* Return a SourceLocation for the location after the first semicolon
1383 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1384 * call it and also skip trailing spaces and newline.
1386 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1387 const LangOptions
&LO
)
1389 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
1394 /* Return a SourceLocation for the location after the first semicolon
1395 * after "loc". If Lexer::findLocationAfterToken is not available,
1396 * we look in the underlying character data for the first semicolon.
1398 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1399 const LangOptions
&LO
)
1402 const char *s
= SM
.getCharacterData(loc
);
1404 semi
= strchr(s
, ';');
1406 return SourceLocation();
1407 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
1412 /* If the token at "loc" is the first token on the line, then return
1413 * a location referring to the start of the line and set *indent
1414 * to the indentation of "loc"
1415 * Otherwise, return "loc" and set *indent to "".
1417 * This function is used to extend a scop to the start of the line
1418 * if the first token of the scop is also the first token on the line.
1420 * We look for the first token on the line. If its location is equal to "loc",
1421 * then the latter is the location of the first token on the line.
1423 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
1424 SourceManager
&SM
, const LangOptions
&LO
, char **indent
)
1426 std::pair
<FileID
, unsigned> file_offset_pair
;
1427 llvm::StringRef file
;
1430 SourceLocation token_loc
, line_loc
;
1434 loc
= SM
.getExpansionLoc(loc
);
1435 col
= SM
.getExpansionColumnNumber(loc
);
1436 line_loc
= loc
.getLocWithOffset(1 - col
);
1437 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
1438 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
1439 pos
= file
.data() + file_offset_pair
.second
;
1441 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
1442 file
.begin(), pos
, file
.end());
1443 lexer
.LexFromRawLexer(tok
);
1444 token_loc
= tok
.getLocation();
1446 s
= SM
.getCharacterData(line_loc
);
1447 *indent
= strndup(s
, token_loc
== loc
? col
- 1 : 0);
1449 if (token_loc
== loc
)
1455 /* Construct a pet_loc corresponding to the region covered by "range".
1456 * If "skip_semi" is set, then we assume "range" is followed by
1457 * a semicolon and also include this semicolon.
1459 __isl_give pet_loc
*PetScan::construct_pet_loc(SourceRange range
,
1462 SourceLocation loc
= range
.getBegin();
1463 SourceManager
&SM
= PP
.getSourceManager();
1464 const LangOptions
&LO
= PP
.getLangOpts();
1465 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
1466 unsigned start
, end
;
1469 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
, &indent
);
1470 start
= getExpansionOffset(SM
, loc
);
1471 loc
= range
.getEnd();
1473 loc
= location_after_semi(loc
, SM
, LO
);
1475 loc
= PP
.getLocForEndOfToken(loc
);
1476 end
= getExpansionOffset(SM
, loc
);
1478 return pet_loc_alloc(ctx
, start
, end
, line
, indent
);
1481 /* Convert a top-level pet_expr to an expression pet_tree.
1483 __isl_give pet_tree
*PetScan::extract(__isl_take pet_expr
*expr
,
1484 SourceRange range
, bool skip_semi
)
1489 tree
= pet_tree_new_expr(expr
);
1490 loc
= construct_pet_loc(range
, skip_semi
);
1491 tree
= pet_tree_set_loc(tree
, loc
);
1496 /* Construct a pet_tree for an if statement.
1498 __isl_give pet_tree
*PetScan::extract(IfStmt
*stmt
)
1501 pet_tree
*tree
, *tree_else
;
1502 struct pet_scop
*scop
;
1505 pe_cond
= extract_expr(stmt
->getCond());
1506 tree
= extract(stmt
->getThen());
1507 if (stmt
->getElse()) {
1508 tree_else
= extract(stmt
->getElse());
1509 if (options
->autodetect
) {
1510 if (tree
&& !tree_else
) {
1512 pet_expr_free(pe_cond
);
1515 if (!tree
&& tree_else
) {
1517 pet_expr_free(pe_cond
);
1521 tree
= pet_tree_new_if_else(pe_cond
, tree
, tree_else
);
1523 tree
= pet_tree_new_if(pe_cond
, tree
);
1527 /* Try and construct a pet_tree for a label statement.
1528 * We currently only allow labels on expression statements.
1530 __isl_give pet_tree
*PetScan::extract(LabelStmt
*stmt
)
1536 sub
= stmt
->getSubStmt();
1537 if (!isa
<Expr
>(sub
)) {
1542 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
1544 tree
= extract(extract_expr(cast
<Expr
>(sub
)), stmt
->getSourceRange(),
1546 tree
= pet_tree_set_label(tree
, label
);
1550 /* Update the location of "tree" to include the source range of "stmt".
1552 * Actually, we create a new location based on the source range of "stmt" and
1553 * then extend this new location to include the region of the original location.
1554 * This ensures that the line number of the final location refers to "stmt".
1556 __isl_give pet_tree
*PetScan::update_loc(__isl_take pet_tree
*tree
, Stmt
*stmt
)
1558 pet_loc
*loc
, *tree_loc
;
1560 tree_loc
= pet_tree_get_loc(tree
);
1561 loc
= construct_pet_loc(stmt
->getSourceRange(), false);
1562 loc
= pet_loc_update_start_end_from_loc(loc
, tree_loc
);
1563 pet_loc_free(tree_loc
);
1565 tree
= pet_tree_set_loc(tree
, loc
);
1569 /* Try and construct a pet_tree corresponding to "stmt".
1571 * If "stmt" is a compound statement, then "skip_declarations"
1572 * indicates whether we should skip initial declarations in the
1573 * compound statement.
1575 * If the constructed pet_tree is not a (possibly) partial representation
1576 * of "stmt", we update start and end of the pet_scop to those of "stmt".
1577 * In particular, if skip_declarations is set, then we may have skipped
1578 * declarations inside "stmt" and so the pet_scop may not represent
1579 * the entire "stmt".
1580 * Note that this function may be called with "stmt" referring to the entire
1581 * body of the function, including the outer braces. In such cases,
1582 * skip_declarations will be set and the braces will not be taken into
1583 * account in tree->loc.
1585 __isl_give pet_tree
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
1589 set_current_stmt(stmt
);
1591 if (isa
<Expr
>(stmt
))
1592 return extract(extract_expr(cast
<Expr
>(stmt
)),
1593 stmt
->getSourceRange(), true);
1595 switch (stmt
->getStmtClass()) {
1596 case Stmt::WhileStmtClass
:
1597 tree
= extract(cast
<WhileStmt
>(stmt
));
1599 case Stmt::ForStmtClass
:
1600 tree
= extract_for(cast
<ForStmt
>(stmt
));
1602 case Stmt::IfStmtClass
:
1603 tree
= extract(cast
<IfStmt
>(stmt
));
1605 case Stmt::CompoundStmtClass
:
1606 tree
= extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
1608 case Stmt::LabelStmtClass
:
1609 tree
= extract(cast
<LabelStmt
>(stmt
));
1611 case Stmt::ContinueStmtClass
:
1612 tree
= pet_tree_new_continue(ctx
);
1614 case Stmt::BreakStmtClass
:
1615 tree
= pet_tree_new_break(ctx
);
1617 case Stmt::DeclStmtClass
:
1618 tree
= extract(cast
<DeclStmt
>(stmt
));
1625 if (partial
|| skip_declarations
)
1628 return update_loc(tree
, stmt
);
1631 /* Try and construct a pet_tree corresponding to (part of)
1632 * a sequence of statements.
1634 * "block" is set if the sequence represents the children of
1635 * a compound statement.
1636 * "skip_declarations" is set if we should skip initial declarations
1637 * in the sequence of statements.
1639 * If autodetect is set, then we allow the extraction of only a subrange
1640 * of the sequence of statements. However, if there is at least one statement
1641 * for which we could not construct a scop and the final range contains
1642 * either no statements or at least one kill, then we discard the entire
1645 __isl_give pet_tree
*PetScan::extract(StmtRange stmt_range
, bool block
,
1646 bool skip_declarations
)
1650 bool has_kills
= false;
1651 bool partial_range
= false;
1653 set
<struct pet_stmt
*> kills
;
1654 set
<struct pet_stmt
*>::iterator it
;
1656 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
)
1659 tree
= pet_tree_new_block(ctx
, block
, j
);
1661 for (i
= stmt_range
.first
; i
!= stmt_range
.second
; ++i
) {
1665 if (pet_tree_block_n_child(tree
) == 0 && skip_declarations
&&
1666 child
->getStmtClass() == Stmt::DeclStmtClass
)
1669 tree_i
= extract(child
);
1670 if (pet_tree_block_n_child(tree
) != 0 && partial
) {
1671 pet_tree_free(tree_i
);
1674 if (tree_i
&& child
->getStmtClass() == Stmt::DeclStmtClass
&&
1677 if (options
->autodetect
) {
1679 tree
= pet_tree_block_add_child(tree
, tree_i
);
1681 partial_range
= true;
1682 if (pet_tree_block_n_child(tree
) != 0 && !tree_i
)
1685 tree
= pet_tree_block_add_child(tree
, tree_i
);
1688 if (partial
|| !tree
)
1692 if (tree
&& partial_range
) {
1693 if (pet_tree_block_n_child(tree
) == 0 || has_kills
) {
1694 pet_tree_free(tree
);
1703 /* Is "T" the type of a variable length array with static size?
1705 static bool is_vla_with_static_size(QualType T
)
1707 const VariableArrayType
*vlatype
;
1709 if (!T
->isVariableArrayType())
1711 vlatype
= cast
<VariableArrayType
>(T
);
1712 return vlatype
->getSizeModifier() == VariableArrayType::Static
;
1715 /* Return the type of "decl" as an array.
1717 * In particular, if "decl" is a parameter declaration that
1718 * is a variable length array with a static size, then
1719 * return the original type (i.e., the variable length array).
1720 * Otherwise, return the type of decl.
1722 static QualType
get_array_type(ValueDecl
*decl
)
1727 parm
= dyn_cast
<ParmVarDecl
>(decl
);
1729 return decl
->getType();
1731 T
= parm
->getOriginalType();
1732 if (!is_vla_with_static_size(T
))
1733 return decl
->getType();
1738 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
1740 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
1741 __isl_keep pet_context
*pc
, void *user
);
1744 /* Construct a pet_expr that holds the sizes of the array accessed
1746 * This function is used as a callback to pet_context_add_parameters,
1747 * which is also passed a pointer to the PetScan object.
1749 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
1752 PetScan
*ps
= (PetScan
*) user
;
1757 id
= pet_expr_access_get_id(access
);
1758 decl
= (ValueDecl
*) isl_id_get_user(id
);
1760 type
= get_array_type(decl
).getTypePtr();
1761 return ps
->get_array_size(type
);
1764 /* Construct and return a pet_array corresponding to the variable
1765 * accessed by "access".
1766 * This function is used as a callback to pet_scop_from_pet_tree,
1767 * which is also passed a pointer to the PetScan object.
1769 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
1770 __isl_keep pet_context
*pc
, void *user
)
1772 PetScan
*ps
= (PetScan
*) user
;
1777 ctx
= pet_expr_get_ctx(access
);
1778 id
= pet_expr_access_get_id(access
);
1779 iv
= (ValueDecl
*) isl_id_get_user(id
);
1781 return ps
->extract_array(ctx
, iv
, NULL
, pc
);
1784 /* Extract a function summary from the body of "fd".
1786 * We extract a scop from the function body in a context with as
1787 * parameters the integer arguments of the function.
1788 * We turn off autodetection (in case it was set) to ensure that
1789 * the entire function body is considered.
1790 * We then collect the accessed array elements and attach them
1791 * to the corresponding array arguments, taking into account
1792 * that the function body may access members of array elements.
1794 * The reason for representing the integer arguments as parameters in
1795 * the context is that if we were to instead start with a context
1796 * with the function arguments as initial dimensions, then we would not
1797 * be able to refer to them from the array extents, without turning
1798 * array extents into maps.
1800 * The result is stored in the summary_cache cache so that we can reuse
1801 * it if this method gets called on the same function again later on.
1803 __isl_give pet_function_summary
*PetScan::get_summary(FunctionDecl
*fd
)
1809 pet_function_summary
*summary
;
1812 int save_autodetect
;
1813 struct pet_scop
*scop
;
1815 isl_union_set
*may_read
, *may_write
, *must_write
;
1816 isl_union_map
*to_inner
;
1818 if (summary_cache
.find(fd
) != summary_cache
.end())
1819 return pet_function_summary_copy(summary_cache
[fd
]);
1821 space
= isl_space_set_alloc(ctx
, 0, 0);
1823 n
= fd
->getNumParams();
1824 summary
= pet_function_summary_alloc(ctx
, n
);
1825 for (int i
= 0; i
< n
; ++i
) {
1826 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1827 QualType type
= parm
->getType();
1830 if (!type
->isIntegerType())
1832 id
= create_decl_id(ctx
, parm
);
1833 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
1834 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0,
1836 summary
= pet_function_summary_set_int(summary
, i
, id
);
1839 save_autodetect
= options
->autodetect
;
1840 options
->autodetect
= 0;
1841 PetScan
body_scan(PP
, ast_context
, loc
, options
,
1842 isl_union_map_copy(value_bounds
), independent
);
1844 tree
= body_scan
.extract(fd
->getBody(), false);
1846 domain
= isl_set_universe(space
);
1847 pc
= pet_context_alloc(domain
);
1848 pc
= pet_context_add_parameters(pc
, tree
,
1849 &::get_array_size
, &body_scan
);
1850 int_size
= ast_context
.getTypeInfo(ast_context
.IntTy
).first
/ 8;
1851 scop
= pet_scop_from_pet_tree(tree
, int_size
,
1852 &::extract_array
, &body_scan
, pc
);
1853 scop
= scan_arrays(scop
, pc
);
1854 may_read
= isl_union_map_range(pet_scop_collect_may_reads(scop
));
1855 may_write
= isl_union_map_range(pet_scop_collect_may_writes(scop
));
1856 must_write
= isl_union_map_range(pet_scop_collect_must_writes(scop
));
1857 to_inner
= pet_scop_compute_outer_to_inner(scop
);
1858 pet_scop_free(scop
);
1860 for (int i
= 0; i
< n
; ++i
) {
1861 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1862 QualType type
= parm
->getType();
1863 struct pet_array
*array
;
1865 isl_union_set
*data_set
;
1866 isl_union_set
*may_read_i
, *may_write_i
, *must_write_i
;
1868 if (array_depth(type
.getTypePtr()) == 0)
1871 array
= body_scan
.extract_array(ctx
, parm
, NULL
, pc
);
1872 space
= array
? isl_set_get_space(array
->extent
) : NULL
;
1873 pet_array_free(array
);
1874 data_set
= isl_union_set_from_set(isl_set_universe(space
));
1875 data_set
= isl_union_set_apply(data_set
,
1876 isl_union_map_copy(to_inner
));
1877 may_read_i
= isl_union_set_intersect(
1878 isl_union_set_copy(may_read
),
1879 isl_union_set_copy(data_set
));
1880 may_write_i
= isl_union_set_intersect(
1881 isl_union_set_copy(may_write
),
1882 isl_union_set_copy(data_set
));
1883 must_write_i
= isl_union_set_intersect(
1884 isl_union_set_copy(must_write
), data_set
);
1885 summary
= pet_function_summary_set_array(summary
, i
,
1886 may_read_i
, may_write_i
, must_write_i
);
1889 isl_union_set_free(may_read
);
1890 isl_union_set_free(may_write
);
1891 isl_union_set_free(must_write
);
1892 isl_union_map_free(to_inner
);
1894 options
->autodetect
= save_autodetect
;
1895 pet_context_free(pc
);
1897 summary_cache
[fd
] = pet_function_summary_copy(summary
);
1902 /* If "fd" has a function body, then extract a function summary from
1903 * this body and attach it to the call expression "expr".
1905 * Even if a function body is available, "fd" itself may point
1906 * to a declaration without function body. We therefore first
1907 * replace it by the declaration that comes with a body (if any).
1909 * It is not clear why hasBody takes a reference to a const FunctionDecl *.
1910 * It seems that it is possible to directly use the iterators to obtain
1911 * a non-const pointer.
1912 * Since we are not going to use the pointer to modify anything anyway,
1913 * it seems safe to drop the constness. The alternative would be to
1914 * modify a lot of other functions to include const qualifiers.
1916 __isl_give pet_expr
*PetScan::set_summary(__isl_take pet_expr
*expr
,
1919 pet_function_summary
*summary
;
1920 const FunctionDecl
*def
;
1924 if (!fd
->hasBody(def
))
1927 fd
= const_cast<FunctionDecl
*>(def
);
1929 summary
= get_summary(fd
);
1931 expr
= pet_expr_call_set_summary(expr
, summary
);
1936 /* Extract a pet_scop from "tree".
1938 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
1939 * then add pet_arrays for all accessed arrays.
1940 * We populate the pet_context with assignments for all parameters used
1941 * inside "tree" or any of the size expressions for the arrays accessed
1942 * by "tree" so that they can be used in affine expressions.
1944 struct pet_scop
*PetScan::extract_scop(__isl_take pet_tree
*tree
)
1951 int_size
= ast_context
.getTypeInfo(ast_context
.IntTy
).first
/ 8;
1953 domain
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
1954 pc
= pet_context_alloc(domain
);
1955 pc
= pet_context_add_parameters(pc
, tree
, &::get_array_size
, this);
1956 scop
= pet_scop_from_pet_tree(tree
, int_size
,
1957 &::extract_array
, this, pc
);
1958 scop
= scan_arrays(scop
, pc
);
1959 pet_context_free(pc
);
1964 /* Check if the scop marked by the user is exactly this Stmt
1965 * or part of this Stmt.
1966 * If so, return a pet_scop corresponding to the marked region.
1967 * Otherwise, return NULL.
1969 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
1971 SourceManager
&SM
= PP
.getSourceManager();
1972 unsigned start_off
, end_off
;
1974 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
1975 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
1977 if (start_off
> loc
.end
)
1979 if (end_off
< loc
.start
)
1982 if (start_off
>= loc
.start
&& end_off
<= loc
.end
)
1983 return extract_scop(extract(stmt
));
1986 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
1987 Stmt
*child
= *start
;
1990 start_off
= getExpansionOffset(SM
, child
->getLocStart());
1991 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
1992 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
1994 if (start_off
>= loc
.start
)
1999 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2001 start_off
= SM
.getFileOffset(child
->getLocStart());
2002 if (start_off
>= loc
.end
)
2006 return extract_scop(extract(StmtRange(start
, end
), false, false));
2009 /* Set the size of index "pos" of "array" to "size".
2010 * In particular, add a constraint of the form
2014 * to array->extent and a constraint of the form
2018 * to array->context.
2020 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2021 __isl_take isl_pw_aff
*size
)
2034 valid
= isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
)));
2035 array
->context
= isl_set_intersect(array
->context
, valid
);
2037 dim
= isl_set_get_space(array
->extent
);
2038 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2039 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2040 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2041 index
= isl_pw_aff_alloc(univ
, aff
);
2043 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2044 isl_set_dim(array
->extent
, isl_dim_set
));
2045 id
= isl_set_get_tuple_id(array
->extent
);
2046 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
2047 bound
= isl_pw_aff_lt_set(index
, size
);
2049 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2051 if (!array
->context
|| !array
->extent
)
2052 return pet_array_free(array
);
2056 isl_pw_aff_free(size
);
2060 #ifdef HAVE_DECAYEDTYPE
2062 /* If "type" is a decayed type, then set *decayed to true and
2063 * return the original type.
2065 static const Type
*undecay(const Type
*type
, bool *decayed
)
2067 *decayed
= isa
<DecayedType
>(type
);
2069 type
= cast
<DecayedType
>(type
)->getOriginalType().getTypePtr();
2075 /* If "type" is a decayed type, then set *decayed to true and
2076 * return the original type.
2077 * Since this version of clang does not define a DecayedType,
2078 * we cannot obtain the original type even if it had been decayed and
2079 * we set *decayed to false.
2081 static const Type
*undecay(const Type
*type
, bool *decayed
)
2089 /* Figure out the size of the array at position "pos" and all
2090 * subsequent positions from "type" and update the corresponding
2091 * argument of "expr" accordingly.
2093 * The initial type (when pos is zero) may be a pointer type decayed
2094 * from an array type, if this initial type is the type of a function
2095 * argument. This only happens if the original array type has
2096 * a constant size in the outer dimension as otherwise we get
2097 * a VariableArrayType. Try and obtain this original type (if available) and
2098 * take the outer array size into account if it was marked static.
2100 __isl_give pet_expr
*PetScan::set_upper_bounds(__isl_take pet_expr
*expr
,
2101 const Type
*type
, int pos
)
2103 const ArrayType
*atype
;
2105 bool decayed
= false;
2111 type
= undecay(type
, &decayed
);
2113 if (type
->isPointerType()) {
2114 type
= type
->getPointeeType().getTypePtr();
2115 return set_upper_bounds(expr
, type
, pos
+ 1);
2117 if (!type
->isArrayType())
2120 type
= type
->getCanonicalTypeInternal().getTypePtr();
2121 atype
= cast
<ArrayType
>(type
);
2123 if (decayed
&& atype
->getSizeModifier() != ArrayType::Static
) {
2124 type
= atype
->getElementType().getTypePtr();
2125 return set_upper_bounds(expr
, type
, pos
+ 1);
2128 if (type
->isConstantArrayType()) {
2129 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2130 size
= extract_expr(ca
->getSize());
2131 expr
= pet_expr_set_arg(expr
, pos
, size
);
2132 } else if (type
->isVariableArrayType()) {
2133 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2134 size
= extract_expr(vla
->getSizeExpr());
2135 expr
= pet_expr_set_arg(expr
, pos
, size
);
2138 type
= atype
->getElementType().getTypePtr();
2140 return set_upper_bounds(expr
, type
, pos
+ 1);
2143 /* Construct a pet_expr that holds the sizes of an array of the given type.
2144 * The returned expression is a call expression with as arguments
2145 * the sizes in each dimension. If we are unable to derive the size
2146 * in a given dimension, then the corresponding argument is set to infinity.
2147 * In fact, we initialize all arguments to infinity and then update
2148 * them if we are able to figure out the size.
2150 * The result is stored in the type_size cache so that we can reuse
2151 * it if this method gets called on the same type again later on.
2153 __isl_give pet_expr
*PetScan::get_array_size(const Type
*type
)
2156 pet_expr
*expr
, *inf
;
2158 if (type_size
.find(type
) != type_size
.end())
2159 return pet_expr_copy(type_size
[type
]);
2161 depth
= array_depth(type
);
2162 inf
= pet_expr_new_int(isl_val_infty(ctx
));
2163 expr
= pet_expr_new_call(ctx
, "bounds", depth
);
2164 for (int i
= 0; i
< depth
; ++i
)
2165 expr
= pet_expr_set_arg(expr
, i
, pet_expr_copy(inf
));
2168 expr
= set_upper_bounds(expr
, type
, 0);
2169 type_size
[type
] = pet_expr_copy(expr
);
2174 /* Does "expr" represent the "integer" infinity?
2176 static int is_infty(__isl_keep pet_expr
*expr
)
2181 if (pet_expr_get_type(expr
) != pet_expr_int
)
2183 v
= pet_expr_int_get_val(expr
);
2184 res
= isl_val_is_infty(v
);
2190 /* Figure out the dimensions of an array "array" based on its type
2191 * "type" and update "array" accordingly.
2193 * We first construct a pet_expr that holds the sizes of the array
2194 * in each dimension. The resulting expression may containing
2195 * infinity values for dimension where we are unable to derive
2196 * a size expression.
2198 * The arguments of the size expression that have a value different from
2199 * infinity are then converted to an affine expression
2200 * within the context "pc" and incorporated into the size of "array".
2201 * If we are unable to convert a size expression to an affine expression,
2202 * then we leave the corresponding size of "array" untouched.
2204 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2205 const Type
*type
, __isl_keep pet_context
*pc
)
2213 expr
= get_array_size(type
);
2215 n
= pet_expr_get_n_arg(expr
);
2216 for (int i
= 0; i
< n
; ++i
) {
2220 arg
= pet_expr_get_arg(expr
, i
);
2221 if (!is_infty(arg
)) {
2222 size
= pet_expr_extract_affine(arg
, pc
);
2224 array
= pet_array_free(array
);
2225 else if (isl_pw_aff_involves_nan(size
))
2226 isl_pw_aff_free(size
);
2228 array
= update_size(array
, i
, size
);
2232 pet_expr_free(expr
);
2237 /* Does "decl" have definition that we can keep track of in a pet_type?
2239 static bool has_printable_definition(RecordDecl
*decl
)
2241 if (!decl
->getDeclName())
2243 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
2246 /* Construct and return a pet_array corresponding to the variable "decl".
2247 * In particular, initialize array->extent to
2249 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2251 * and then call set_upper_bounds to set the upper bounds on the indices
2252 * based on the type of the variable. The upper bounds are converted
2253 * to affine expressions within the context "pc".
2255 * If the base type is that of a record with a top-level definition and
2256 * if "types" is not null, then the RecordDecl corresponding to the type
2257 * is added to "types".
2259 * If the base type is that of a record with no top-level definition,
2260 * then we replace it by "<subfield>".
2262 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
, ValueDecl
*decl
,
2263 lex_recorddecl_set
*types
, __isl_keep pet_context
*pc
)
2265 struct pet_array
*array
;
2266 QualType qt
= get_array_type(decl
);
2267 const Type
*type
= qt
.getTypePtr();
2268 int depth
= array_depth(type
);
2269 QualType base
= pet_clang_base_type(qt
);
2274 array
= isl_calloc_type(ctx
, struct pet_array
);
2278 id
= create_decl_id(ctx
, decl
);
2279 dim
= isl_space_set_alloc(ctx
, 0, depth
);
2280 dim
= isl_space_set_tuple_id(dim
, isl_dim_set
, id
);
2282 array
->extent
= isl_set_nat_universe(dim
);
2284 dim
= isl_space_params_alloc(ctx
, 0);
2285 array
->context
= isl_set_universe(dim
);
2287 array
= set_upper_bounds(array
, type
, pc
);
2291 name
= base
.getAsString();
2293 if (types
&& base
->isRecordType()) {
2294 RecordDecl
*decl
= pet_clang_record_decl(base
);
2295 if (has_printable_definition(decl
))
2296 types
->insert(decl
);
2298 name
= "<subfield>";
2301 array
->element_type
= strdup(name
.c_str());
2302 array
->element_is_record
= base
->isRecordType();
2303 array
->element_size
= decl
->getASTContext().getTypeInfo(base
).first
/ 8;
2308 /* Construct and return a pet_array corresponding to the sequence
2309 * of declarations "decls".
2310 * The upper bounds of the array are converted to affine expressions
2311 * within the context "pc".
2312 * If the sequence contains a single declaration, then it corresponds
2313 * to a simple array access. Otherwise, it corresponds to a member access,
2314 * with the declaration for the substructure following that of the containing
2315 * structure in the sequence of declarations.
2316 * We start with the outermost substructure and then combine it with
2317 * information from the inner structures.
2319 * Additionally, keep track of all required types in "types".
2321 struct pet_array
*PetScan::extract_array(isl_ctx
*ctx
,
2322 vector
<ValueDecl
*> decls
, lex_recorddecl_set
*types
,
2323 __isl_keep pet_context
*pc
)
2325 struct pet_array
*array
;
2326 vector
<ValueDecl
*>::iterator it
;
2330 array
= extract_array(ctx
, *it
, types
, pc
);
2332 for (++it
; it
!= decls
.end(); ++it
) {
2333 struct pet_array
*parent
;
2334 const char *base_name
, *field_name
;
2338 array
= extract_array(ctx
, *it
, types
, pc
);
2340 return pet_array_free(parent
);
2342 base_name
= isl_set_get_tuple_name(parent
->extent
);
2343 field_name
= isl_set_get_tuple_name(array
->extent
);
2344 product_name
= pet_array_member_access_name(ctx
,
2345 base_name
, field_name
);
2347 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
2350 array
->extent
= isl_set_set_tuple_name(array
->extent
,
2352 array
->context
= isl_set_intersect(array
->context
,
2353 isl_set_copy(parent
->context
));
2355 pet_array_free(parent
);
2358 if (!array
->extent
|| !array
->context
|| !product_name
)
2359 return pet_array_free(array
);
2365 /* Add a pet_type corresponding to "decl" to "scop, provided
2366 * it is a member of "types" and it has not been added before
2367 * (i.e., it is not a member of "types_done".
2369 * Since we want the user to be able to print the types
2370 * in the order in which they appear in the scop, we need to
2371 * make sure that types of fields in a structure appear before
2372 * that structure. We therefore call ourselves recursively
2373 * on the types of all record subfields.
2375 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
2376 RecordDecl
*decl
, Preprocessor
&PP
, lex_recorddecl_set
&types
,
2377 lex_recorddecl_set
&types_done
)
2380 llvm::raw_string_ostream
S(s
);
2381 RecordDecl::field_iterator it
;
2383 if (types
.find(decl
) == types
.end())
2385 if (types_done
.find(decl
) != types_done
.end())
2388 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
2390 QualType type
= it
->getType();
2392 if (!type
->isRecordType())
2394 record
= pet_clang_record_decl(type
);
2395 scop
= add_type(ctx
, scop
, record
, PP
, types
, types_done
);
2398 if (strlen(decl
->getName().str().c_str()) == 0)
2401 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
2404 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
2405 decl
->getName().str().c_str(), s
.c_str());
2406 if (!scop
->types
[scop
->n_type
])
2407 return pet_scop_free(scop
);
2409 types_done
.insert(decl
);
2416 /* Construct a list of pet_arrays, one for each array (or scalar)
2417 * accessed inside "scop", add this list to "scop" and return the result.
2418 * The upper bounds of the arrays are converted to affine expressions
2419 * within the context "pc".
2421 * The context of "scop" is updated with the intersection of
2422 * the contexts of all arrays, i.e., constraints on the parameters
2423 * that ensure that the arrays have a valid (non-negative) size.
2425 * If the any of the extracted arrays refers to a member access,
2426 * then also add the required types to "scop".
2428 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
,
2429 __isl_keep pet_context
*pc
)
2432 array_desc_set arrays
;
2433 array_desc_set::iterator it
;
2434 lex_recorddecl_set types
;
2435 lex_recorddecl_set types_done
;
2436 lex_recorddecl_set::iterator types_it
;
2438 struct pet_array
**scop_arrays
;
2443 pet_scop_collect_arrays(scop
, arrays
);
2444 if (arrays
.size() == 0)
2447 n_array
= scop
->n_array
;
2449 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
2450 n_array
+ arrays
.size());
2453 scop
->arrays
= scop_arrays
;
2455 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
2456 struct pet_array
*array
;
2457 array
= extract_array(ctx
, *it
, &types
, pc
);
2458 scop
->arrays
[n_array
+ i
] = array
;
2459 if (!scop
->arrays
[n_array
+ i
])
2462 scop
->context
= isl_set_intersect(scop
->context
,
2463 isl_set_copy(array
->context
));
2468 if (types
.size() == 0)
2471 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, types
.size());
2475 for (types_it
= types
.begin(); types_it
!= types
.end(); ++types_it
)
2476 scop
= add_type(ctx
, scop
, *types_it
, PP
, types
, types_done
);
2480 pet_scop_free(scop
);
2484 /* Bound all parameters in scop->context to the possible values
2485 * of the corresponding C variable.
2487 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
2494 n
= isl_set_dim(scop
->context
, isl_dim_param
);
2495 for (int i
= 0; i
< n
; ++i
) {
2499 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
2500 if (pet_nested_in_id(id
)) {
2502 isl_die(isl_set_get_ctx(scop
->context
),
2504 "unresolved nested parameter", goto error
);
2506 decl
= (ValueDecl
*) isl_id_get_user(id
);
2509 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
2517 pet_scop_free(scop
);
2521 /* Construct a pet_scop from the given function.
2523 * If the scop was delimited by scop and endscop pragmas, then we override
2524 * the file offsets by those derived from the pragmas.
2526 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
2531 stmt
= fd
->getBody();
2533 if (options
->autodetect
) {
2534 set_current_stmt(stmt
);
2535 scop
= extract_scop(extract(stmt
, true));
2537 current_line
= loc
.start_line
;
2539 scop
= pet_scop_update_start_end(scop
, loc
.start
, loc
.end
);
2541 scop
= add_parameter_bounds(scop
);
2542 scop
= pet_scop_gist(scop
, value_bounds
);
2547 /* Update this->last_line and this->current_line based on the fact
2548 * that we are about to consider "stmt".
2550 void PetScan::set_current_stmt(Stmt
*stmt
)
2552 SourceLocation loc
= stmt
->getLocStart();
2553 SourceManager
&SM
= PP
.getSourceManager();
2555 last_line
= current_line
;
2556 current_line
= SM
.getExpansionLineNumber(loc
);
2559 /* Is the current statement marked by an independent pragma?
2560 * That is, is there an independent pragma on a line between
2561 * the line of the current statement and the line of the previous statement.
2562 * The search is not implemented very efficiently. We currently
2563 * assume that there are only a few independent pragmas, if any.
2565 bool PetScan::is_current_stmt_marked_independent()
2567 for (int i
= 0; i
< independent
.size(); ++i
) {
2568 unsigned line
= independent
[i
].line
;
2570 if (last_line
< line
&& line
< current_line
)