2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2012-2015 Ecole Normale Superieure. All rights reserved.
4 * Copyright 2015-2016 Sven Verdoolaege. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as
32 * representing official policies, either expressed or implied, of
43 #include <llvm/Support/raw_ostream.h>
44 #include <clang/AST/ASTContext.h>
45 #include <clang/AST/ASTDiagnostic.h>
46 #include <clang/AST/Attr.h>
47 #include <clang/AST/Expr.h>
48 #include <clang/AST/RecursiveASTVisitor.h>
51 #include <isl/space.h>
54 #include <isl/union_set.h>
63 #include "killed_locals.h"
68 #include "scop_plus.h"
69 #include "substituter.h"
71 #include "tree2scop.h"
74 using namespace clang
;
76 static enum pet_op_type
UnaryOperatorKind2pet_op_type(UnaryOperatorKind kind
)
86 return pet_op_post_inc
;
88 return pet_op_post_dec
;
90 return pet_op_pre_inc
;
92 return pet_op_pre_dec
;
98 static enum pet_op_type
BinaryOperatorKind2pet_op_type(BinaryOperatorKind kind
)
102 return pet_op_add_assign
;
104 return pet_op_sub_assign
;
106 return pet_op_mul_assign
;
108 return pet_op_div_assign
;
110 return pet_op_assign
;
152 #ifdef GETTYPEINFORETURNSTYPEINFO
154 static int size_in_bytes(ASTContext
&context
, QualType type
)
156 return context
.getTypeInfo(type
).Width
/ 8;
161 static int size_in_bytes(ASTContext
&context
, QualType type
)
163 return context
.getTypeInfo(type
).first
/ 8;
168 /* Check if the element type corresponding to the given array type
169 * has a const qualifier.
171 static bool const_base(QualType qt
)
173 const Type
*type
= qt
.getTypePtr();
175 if (type
->isPointerType())
176 return const_base(type
->getPointeeType());
177 if (type
->isArrayType()) {
178 const ArrayType
*atype
;
179 type
= type
->getCanonicalTypeInternal().getTypePtr();
180 atype
= cast
<ArrayType
>(type
);
181 return const_base(atype
->getElementType());
184 return qt
.isConstQualified();
189 std::map
<const Type
*, pet_expr
*>::iterator it
;
190 std::map
<FunctionDecl
*, pet_function_summary
*>::iterator it_s
;
192 for (it
= type_size
.begin(); it
!= type_size
.end(); ++it
)
193 pet_expr_free(it
->second
);
194 for (it_s
= summary_cache
.begin(); it_s
!= summary_cache
.end(); ++it_s
)
195 pet_function_summary_free(it_s
->second
);
197 isl_id_to_pet_expr_free(id_size
);
198 isl_union_map_free(value_bounds
);
201 /* Report a diagnostic on the range "range", unless autodetect is set.
203 void PetScan::report(SourceRange range
, unsigned id
)
205 if (options
->autodetect
)
208 SourceLocation loc
= range
.getBegin();
209 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
210 DiagnosticBuilder B
= diag
.Report(loc
, id
) << range
;
213 /* Report a diagnostic on "stmt", unless autodetect is set.
215 void PetScan::report(Stmt
*stmt
, unsigned id
)
217 report(stmt
->getSourceRange(), id
);
220 /* Report a diagnostic on "decl", unless autodetect is set.
222 void PetScan::report(Decl
*decl
, unsigned id
)
224 report(decl
->getSourceRange(), id
);
227 /* Called if we found something we (currently) cannot handle.
228 * We'll provide more informative warnings later.
230 * We only actually complain if autodetect is false.
232 void PetScan::unsupported(Stmt
*stmt
)
234 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
235 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
240 /* Report an unsupported unary operator, unless autodetect is set.
242 void PetScan::report_unsupported_unary_operator(Stmt
*stmt
)
244 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
245 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
246 "this type of unary operator is not supported");
250 /* Report an unsupported binary operator, unless autodetect is set.
252 void PetScan::report_unsupported_binary_operator(Stmt
*stmt
)
254 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
255 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
256 "this type of binary operator is not supported");
260 /* Report an unsupported statement type, unless autodetect is set.
262 void PetScan::report_unsupported_statement_type(Stmt
*stmt
)
264 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
265 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
266 "this type of statement is not supported");
270 /* Report a missing prototype, unless autodetect is set.
272 void PetScan::report_prototype_required(Stmt
*stmt
)
274 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
275 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
276 "prototype required");
280 /* Report a missing increment, unless autodetect is set.
282 void PetScan::report_missing_increment(Stmt
*stmt
)
284 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
285 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
286 "missing increment");
290 /* Report a missing summary function, unless autodetect is set.
292 void PetScan::report_missing_summary_function(Stmt
*stmt
)
294 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
295 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
296 "missing summary function");
300 /* Report a missing summary function body, unless autodetect is set.
302 void PetScan::report_missing_summary_function_body(Stmt
*stmt
)
304 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
305 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
306 "missing summary function body");
310 /* Report an unsupported argument in a call to an inlined function,
311 * unless autodetect is set.
313 void PetScan::report_unsupported_inline_function_argument(Stmt
*stmt
)
315 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
316 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
317 "unsupported inline function call argument");
321 /* Report an unsupported type of declaration, unless autodetect is set.
323 void PetScan::report_unsupported_declaration(Decl
*decl
)
325 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
326 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
327 "unsupported declaration");
331 /* Report an unbalanced pair of scop/endscop pragmas, unless autodetect is set.
333 void PetScan::report_unbalanced_pragmas(SourceLocation scop
,
334 SourceLocation endscop
)
336 if (options
->autodetect
)
339 DiagnosticsEngine
&diag
= PP
.getDiagnostics();
341 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Warning
,
342 "unbalanced endscop pragma");
343 DiagnosticBuilder B2
= diag
.Report(endscop
, id
);
346 unsigned id
= diag
.getCustomDiagID(DiagnosticsEngine::Note
,
347 "corresponding scop pragma");
348 DiagnosticBuilder B
= diag
.Report(scop
, id
);
352 /* Extract an integer from "val", which is assumed to be non-negative.
354 static __isl_give isl_val
*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 "val". If "is_signed" is set, then "val"
366 * is signed. Otherwise it it unsigned.
368 static __isl_give isl_val
*extract_int(isl_ctx
*ctx
, bool is_signed
,
371 int is_negative
= is_signed
&& val
.isNegative();
377 v
= extract_unsigned(ctx
, val
);
384 /* Extract an integer from "expr".
386 __isl_give isl_val
*PetScan::extract_int(isl_ctx
*ctx
, IntegerLiteral
*expr
)
388 const Type
*type
= expr
->getType().getTypePtr();
389 bool is_signed
= type
->hasSignedIntegerRepresentation();
391 return ::extract_int(ctx
, is_signed
, expr
->getValue());
394 /* Extract an integer from "expr".
395 * Return NULL if "expr" does not (obviously) represent an integer.
397 __isl_give isl_val
*PetScan::extract_int(clang::ParenExpr
*expr
)
399 return extract_int(expr
->getSubExpr());
402 /* Extract an integer from "expr".
403 * Return NULL if "expr" does not (obviously) represent an integer.
405 __isl_give isl_val
*PetScan::extract_int(clang::Expr
*expr
)
407 if (expr
->getStmtClass() == Stmt::IntegerLiteralClass
)
408 return extract_int(ctx
, cast
<IntegerLiteral
>(expr
));
409 if (expr
->getStmtClass() == Stmt::ParenExprClass
)
410 return extract_int(cast
<ParenExpr
>(expr
));
416 /* Extract a pet_expr from the APInt "val", which is assumed
417 * to be non-negative.
419 __isl_give pet_expr
*PetScan::extract_expr(const llvm::APInt
&val
)
421 return pet_expr_new_int(extract_unsigned(ctx
, val
));
424 /* Return the number of bits needed to represent the type of "decl",
425 * if it is an integer type. Otherwise return 0.
426 * If qt is signed then return the opposite of the number of bits.
428 static int get_type_size(ValueDecl
*decl
)
430 return pet_clang_get_type_size(decl
->getType(), decl
->getASTContext());
433 /* Bound parameter "pos" of "set" to the possible values of "decl".
435 static __isl_give isl_set
*set_parameter_bounds(__isl_take isl_set
*set
,
436 unsigned pos
, ValueDecl
*decl
)
442 ctx
= isl_set_get_ctx(set
);
443 type_size
= get_type_size(decl
);
445 isl_die(ctx
, isl_error_invalid
, "not an integer type",
446 return isl_set_free(set
));
448 set
= isl_set_lower_bound_si(set
, isl_dim_param
, pos
, 0);
449 bound
= isl_val_int_from_ui(ctx
, type_size
);
450 bound
= isl_val_2exp(bound
);
451 bound
= isl_val_sub_ui(bound
, 1);
452 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
, bound
);
454 bound
= isl_val_int_from_ui(ctx
, -type_size
- 1);
455 bound
= isl_val_2exp(bound
);
456 bound
= isl_val_sub_ui(bound
, 1);
457 set
= isl_set_upper_bound_val(set
, isl_dim_param
, pos
,
458 isl_val_copy(bound
));
459 bound
= isl_val_neg(bound
);
460 bound
= isl_val_sub_ui(bound
, 1);
461 set
= isl_set_lower_bound_val(set
, isl_dim_param
, pos
, bound
);
467 __isl_give pet_expr
*PetScan::extract_index_expr(ImplicitCastExpr
*expr
)
469 return extract_index_expr(expr
->getSubExpr());
472 /* Return the depth of the array accessed by the index expression "index".
473 * If "index" is an affine expression, i.e., if it does not access
474 * any array, then return 1.
475 * If "index" represent a member access, i.e., if its range is a wrapped
476 * relation, then return the sum of the depth of the array of structures
477 * and that of the member inside the structure.
479 static int extract_depth(__isl_keep isl_multi_pw_aff
*index
)
487 if (isl_multi_pw_aff_range_is_wrapping(index
)) {
488 int domain_depth
, range_depth
;
489 isl_multi_pw_aff
*domain
, *range
;
491 domain
= isl_multi_pw_aff_copy(index
);
492 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
493 domain_depth
= extract_depth(domain
);
494 isl_multi_pw_aff_free(domain
);
495 range
= isl_multi_pw_aff_copy(index
);
496 range
= isl_multi_pw_aff_range_factor_range(range
);
497 range_depth
= extract_depth(range
);
498 isl_multi_pw_aff_free(range
);
500 return domain_depth
+ range_depth
;
503 if (!isl_multi_pw_aff_has_tuple_id(index
, isl_dim_out
))
506 id
= isl_multi_pw_aff_get_tuple_id(index
, isl_dim_out
);
509 decl
= pet_id_get_decl(id
);
512 return pet_clang_array_depth(decl
->getType());
515 /* Return the depth of the array accessed by the access expression "expr".
517 static int extract_depth(__isl_keep pet_expr
*expr
)
519 isl_multi_pw_aff
*index
;
522 index
= pet_expr_access_get_index(expr
);
523 depth
= extract_depth(index
);
524 isl_multi_pw_aff_free(index
);
529 /* Construct a pet_expr representing an index expression for an access
530 * to the variable referenced by "expr".
532 * If "expr" references an enum constant, then return an integer expression
533 * instead, representing the value of the enum constant.
535 __isl_give pet_expr
*PetScan::extract_index_expr(DeclRefExpr
*expr
)
537 return extract_index_expr(expr
->getDecl());
540 /* Construct a pet_expr representing an index expression for an access
541 * to the variable "decl".
543 * If "decl" is an enum constant, then we return an integer expression
544 * instead, representing the value of the enum constant.
546 __isl_give pet_expr
*PetScan::extract_index_expr(ValueDecl
*decl
)
550 if (isa
<EnumConstantDecl
>(decl
))
551 return extract_expr(cast
<EnumConstantDecl
>(decl
));
553 id
= pet_id_from_decl(ctx
, decl
);
554 return pet_id_create_index_expr(id
);
557 /* Construct a pet_expr representing the index expression "expr"
558 * Return NULL on error.
560 * If "expr" is a reference to an enum constant, then return
561 * an integer expression instead, representing the value of the enum constant.
563 __isl_give pet_expr
*PetScan::extract_index_expr(Expr
*expr
)
565 switch (expr
->getStmtClass()) {
566 case Stmt::ImplicitCastExprClass
:
567 return extract_index_expr(cast
<ImplicitCastExpr
>(expr
));
568 case Stmt::DeclRefExprClass
:
569 return extract_index_expr(cast
<DeclRefExpr
>(expr
));
570 case Stmt::ArraySubscriptExprClass
:
571 return extract_index_expr(cast
<ArraySubscriptExpr
>(expr
));
572 case Stmt::IntegerLiteralClass
:
573 return extract_expr(cast
<IntegerLiteral
>(expr
));
574 case Stmt::MemberExprClass
:
575 return extract_index_expr(cast
<MemberExpr
>(expr
));
582 /* Extract an index expression from the given array subscript expression.
584 * We first extract an index expression from the base.
585 * This will result in an index expression with a range that corresponds
586 * to the earlier indices.
587 * We then extract the current index and let
588 * pet_expr_access_subscript combine the two.
590 __isl_give pet_expr
*PetScan::extract_index_expr(ArraySubscriptExpr
*expr
)
592 Expr
*base
= expr
->getBase();
593 Expr
*idx
= expr
->getIdx();
597 base_expr
= extract_index_expr(base
);
598 index
= extract_expr(idx
);
600 base_expr
= pet_expr_access_subscript(base_expr
, index
);
605 /* Extract an index expression from a member expression.
607 * If the base access (to the structure containing the member)
612 * and the member is called "f", then the member access is of
617 * If the member access is to an anonymous struct, then simply return
621 * If the member access in the source code is of the form
625 * then it is treated as
629 __isl_give pet_expr
*PetScan::extract_index_expr(MemberExpr
*expr
)
631 Expr
*base
= expr
->getBase();
632 FieldDecl
*field
= cast
<FieldDecl
>(expr
->getMemberDecl());
633 pet_expr
*base_index
;
636 base_index
= extract_index_expr(base
);
638 if (expr
->isArrow()) {
639 pet_expr
*index
= pet_expr_new_int(isl_val_zero(ctx
));
640 base_index
= pet_expr_access_subscript(base_index
, index
);
643 if (field
->isAnonymousStructOrUnion())
646 id
= pet_id_from_decl(ctx
, field
);
648 return pet_expr_access_member(base_index
, id
);
651 /* Mark the given access pet_expr as a write.
653 static __isl_give pet_expr
*mark_write(__isl_take pet_expr
*access
)
655 access
= pet_expr_access_set_write(access
, 1);
656 access
= pet_expr_access_set_read(access
, 0);
661 /* Mark the given (read) access pet_expr as also possibly being written.
662 * That is, initialize the may write access relation from the may read relation
663 * and initialize the must write access relation to the empty relation.
665 static __isl_give pet_expr
*mark_may_write(__isl_take pet_expr
*expr
)
667 isl_union_map
*access
;
668 isl_union_map
*empty
;
670 access
= pet_expr_access_get_dependent_access(expr
,
671 pet_expr_access_may_read
);
672 empty
= isl_union_map_empty(isl_union_map_get_space(access
));
673 expr
= pet_expr_access_set_access(expr
, pet_expr_access_may_write
,
675 expr
= pet_expr_access_set_access(expr
, pet_expr_access_must_write
,
681 /* Construct a pet_expr representing a unary operator expression.
683 __isl_give pet_expr
*PetScan::extract_expr(UnaryOperator
*expr
)
689 op
= UnaryOperatorKind2pet_op_type(expr
->getOpcode());
690 if (op
== pet_op_last
) {
691 report_unsupported_unary_operator(expr
);
695 arg
= extract_expr(expr
->getSubExpr());
697 if (expr
->isIncrementDecrementOp() &&
698 pet_expr_get_type(arg
) == pet_expr_access
) {
699 arg
= mark_write(arg
);
700 arg
= pet_expr_access_set_read(arg
, 1);
703 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
704 return pet_expr_new_unary(type_size
, op
, arg
);
707 /* Construct a pet_expr representing a binary operator expression.
709 * If the top level operator is an assignment and the LHS is an access,
710 * then we mark that access as a write. If the operator is a compound
711 * assignment, the access is marked as both a read and a write.
713 __isl_give pet_expr
*PetScan::extract_expr(BinaryOperator
*expr
)
719 op
= BinaryOperatorKind2pet_op_type(expr
->getOpcode());
720 if (op
== pet_op_last
) {
721 report_unsupported_binary_operator(expr
);
725 lhs
= extract_expr(expr
->getLHS());
726 rhs
= extract_expr(expr
->getRHS());
728 if (expr
->isAssignmentOp() &&
729 pet_expr_get_type(lhs
) == pet_expr_access
) {
730 lhs
= mark_write(lhs
);
731 if (expr
->isCompoundAssignmentOp())
732 lhs
= pet_expr_access_set_read(lhs
, 1);
735 type_size
= pet_clang_get_type_size(expr
->getType(), ast_context
);
736 return pet_expr_new_binary(type_size
, op
, lhs
, rhs
);
739 /* Construct a pet_tree for a variable declaration and
740 * add the declaration to the list of declarations
741 * inside the current compound statement.
743 __isl_give pet_tree
*PetScan::extract(Decl
*decl
)
749 if (!isa
<VarDecl
>(decl
)) {
750 report_unsupported_declaration(decl
);
754 vd
= cast
<VarDecl
>(decl
);
755 declarations
.push_back(vd
);
757 lhs
= extract_access_expr(vd
);
758 lhs
= mark_write(lhs
);
760 tree
= pet_tree_new_decl(lhs
);
762 rhs
= extract_expr(vd
->getInit());
763 tree
= pet_tree_new_decl_init(lhs
, rhs
);
769 /* Construct a pet_tree for a variable declaration statement.
770 * If the declaration statement declares multiple variables,
771 * then return a group of pet_trees, one for each declared variable.
773 __isl_give pet_tree
*PetScan::extract(DeclStmt
*stmt
)
778 if (!stmt
->isSingleDecl()) {
779 const DeclGroup
&group
= stmt
->getDeclGroup().getDeclGroup();
781 tree
= pet_tree_new_block(ctx
, 0, n
);
783 for (unsigned i
= 0; i
< n
; ++i
) {
787 tree_i
= extract(group
[i
]);
788 loc
= construct_pet_loc(group
[i
]->getSourceRange(),
790 tree_i
= pet_tree_set_loc(tree_i
, loc
);
791 tree
= pet_tree_block_add_child(tree
, tree_i
);
797 return extract(stmt
->getSingleDecl());
800 /* Construct a pet_expr representing a conditional operation.
802 __isl_give pet_expr
*PetScan::extract_expr(ConditionalOperator
*expr
)
804 pet_expr
*cond
, *lhs
, *rhs
;
806 cond
= extract_expr(expr
->getCond());
807 lhs
= extract_expr(expr
->getTrueExpr());
808 rhs
= extract_expr(expr
->getFalseExpr());
810 return pet_expr_new_ternary(cond
, lhs
, rhs
);
813 __isl_give pet_expr
*PetScan::extract_expr(ImplicitCastExpr
*expr
)
815 return extract_expr(expr
->getSubExpr());
818 /* Construct a pet_expr representing a floating point value.
820 * If the floating point literal does not appear in a macro,
821 * then we use the original representation in the source code
822 * as the string representation. Otherwise, we use the pretty
823 * printer to produce a string representation.
825 __isl_give pet_expr
*PetScan::extract_expr(FloatingLiteral
*expr
)
829 const LangOptions
&LO
= PP
.getLangOpts();
830 SourceLocation loc
= expr
->getLocation();
832 if (!loc
.isMacroID()) {
833 SourceManager
&SM
= PP
.getSourceManager();
834 unsigned len
= Lexer::MeasureTokenLength(loc
, SM
, LO
);
835 s
= string(SM
.getCharacterData(loc
), len
);
837 llvm::raw_string_ostream
S(s
);
838 expr
->printPretty(S
, 0, PrintingPolicy(LO
));
841 d
= expr
->getValueAsApproximateDouble();
842 return pet_expr_new_double(ctx
, d
, s
.c_str());
845 /* Convert the index expression "index" into an access pet_expr of type "qt".
847 __isl_give pet_expr
*PetScan::extract_access_expr(QualType qt
,
848 __isl_take pet_expr
*index
)
853 depth
= extract_depth(index
);
854 type_size
= pet_clang_get_type_size(qt
, ast_context
);
856 index
= pet_expr_set_type_size(index
, type_size
);
857 index
= pet_expr_access_set_depth(index
, depth
);
862 /* Extract an index expression from "expr" and then convert it into
863 * an access pet_expr.
865 * If "expr" is a reference to an enum constant, then return
866 * an integer expression instead, representing the value of the enum constant.
868 __isl_give pet_expr
*PetScan::extract_access_expr(Expr
*expr
)
872 index
= extract_index_expr(expr
);
874 if (pet_expr_get_type(index
) == pet_expr_int
)
877 return extract_access_expr(expr
->getType(), index
);
880 /* Extract an index expression from "decl" and then convert it into
881 * an access pet_expr.
883 __isl_give pet_expr
*PetScan::extract_access_expr(ValueDecl
*decl
)
885 return extract_access_expr(decl
->getType(), extract_index_expr(decl
));
888 __isl_give pet_expr
*PetScan::extract_expr(ParenExpr
*expr
)
890 return extract_expr(expr
->getSubExpr());
893 /* Extract an assume statement from the argument "expr"
894 * of a __builtin_assume or __pencil_assume statement.
896 __isl_give pet_expr
*PetScan::extract_assume(Expr
*expr
)
898 return pet_expr_new_unary(0, pet_op_assume
, extract_expr(expr
));
901 /* If "expr" is an address-of operator, then return its argument.
902 * Otherwise, return NULL.
904 static Expr
*extract_addr_of_arg(Expr
*expr
)
908 if (expr
->getStmtClass() != Stmt::UnaryOperatorClass
)
910 op
= cast
<UnaryOperator
>(expr
);
911 if (op
->getOpcode() != UO_AddrOf
)
913 return op
->getSubExpr();
916 /* Construct a pet_expr corresponding to the function call argument "expr".
917 * The argument appears in position "pos" of a call to function "fd".
919 * If we are passing along a pointer to an array element
920 * or an entire row or even higher dimensional slice of an array,
921 * then the function being called may write into the array.
923 * We assume here that if the function is declared to take a pointer
924 * to a const type, then the function may only perform a read
925 * and that otherwise, it may either perform a read or a write (or both).
926 * We only perform this check if "detect_writes" is set.
928 __isl_give pet_expr
*PetScan::extract_argument(FunctionDecl
*fd
, int pos
,
929 Expr
*expr
, bool detect_writes
)
933 int is_addr
= 0, is_partial
= 0;
935 expr
= pet_clang_strip_casts(expr
);
936 arg
= extract_addr_of_arg(expr
);
941 res
= extract_expr(expr
);
944 if (pet_clang_array_depth(expr
->getType()) > 0)
946 if (detect_writes
&& (is_addr
|| is_partial
) &&
947 pet_expr_get_type(res
) == pet_expr_access
) {
949 if (!fd
->hasPrototype()) {
950 report_prototype_required(expr
);
951 return pet_expr_free(res
);
953 parm
= fd
->getParamDecl(pos
);
954 if (!const_base(parm
->getType()))
955 res
= mark_may_write(res
);
959 res
= pet_expr_new_unary(0, pet_op_address_of
, res
);
963 /* Find the first FunctionDecl with the given name.
964 * "call" is the corresponding call expression and is only used
965 * for reporting errors.
967 * Return NULL on error.
969 FunctionDecl
*PetScan::find_decl_from_name(CallExpr
*call
, string name
)
971 TranslationUnitDecl
*tu
= ast_context
.getTranslationUnitDecl();
972 DeclContext::decl_iterator begin
= tu
->decls_begin();
973 DeclContext::decl_iterator end
= tu
->decls_end();
974 for (DeclContext::decl_iterator i
= begin
; i
!= end
; ++i
) {
975 FunctionDecl
*fd
= dyn_cast
<FunctionDecl
>(*i
);
978 if (fd
->getName().str().compare(name
) != 0)
982 report_missing_summary_function_body(call
);
985 report_missing_summary_function(call
);
989 /* Return the FunctionDecl for the summary function associated to the
990 * function called by "call".
992 * In particular, if the pencil option is set, then
993 * search for an annotate attribute formatted as
994 * "pencil_access(name)", where "name" is the name of the summary function.
996 * If no summary function was specified, then return the FunctionDecl
997 * that is actually being called.
999 * Return NULL on error.
1001 FunctionDecl
*PetScan::get_summary_function(CallExpr
*call
)
1003 FunctionDecl
*decl
= call
->getDirectCallee();
1007 if (!options
->pencil
)
1010 specific_attr_iterator
<AnnotateAttr
> begin
, end
, i
;
1011 begin
= decl
->specific_attr_begin
<AnnotateAttr
>();
1012 end
= decl
->specific_attr_end
<AnnotateAttr
>();
1013 for (i
= begin
; i
!= end
; ++i
) {
1014 string attr
= (*i
)->getAnnotation().str();
1016 const char prefix
[] = "pencil_access(";
1017 size_t start
= attr
.find(prefix
);
1018 if (start
== string::npos
)
1020 start
+= strlen(prefix
);
1021 string name
= attr
.substr(start
, attr
.find(')') - start
);
1023 return find_decl_from_name(call
, name
);
1029 /* Is "name" the name of an assume statement?
1030 * "pencil" indicates whether pencil builtins and pragmas should be supported.
1031 * "__builtin_assume" is always accepted.
1032 * If "pencil" is set, then "__pencil_assume" is also accepted.
1034 static bool is_assume(int pencil
, const string
&name
)
1036 if (name
== "__builtin_assume")
1038 return pencil
&& name
== "__pencil_assume";
1041 /* Construct a pet_expr representing a function call.
1043 * In the special case of a "call" to __builtin_assume or __pencil_assume,
1044 * construct an assume expression instead.
1046 * In the case of a "call" to __pencil_kill, the arguments
1047 * are neither read nor written (only killed), so there
1048 * is no need to check for writes to these arguments.
1050 * __pencil_assume and __pencil_kill are only recognized
1051 * when the pencil option is set.
1053 __isl_give pet_expr
*PetScan::extract_expr(CallExpr
*expr
)
1055 pet_expr
*res
= NULL
;
1061 fd
= expr
->getDirectCallee();
1067 name
= fd
->getDeclName().getAsString();
1068 n_arg
= expr
->getNumArgs();
1070 if (n_arg
== 1 && is_assume(options
->pencil
, name
))
1071 return extract_assume(expr
->getArg(0));
1072 is_kill
= options
->pencil
&& name
== "__pencil_kill";
1074 res
= pet_expr_new_call(ctx
, name
.c_str(), n_arg
);
1078 for (unsigned i
= 0; i
< n_arg
; ++i
) {
1079 Expr
*arg
= expr
->getArg(i
);
1080 res
= pet_expr_set_arg(res
, i
,
1081 PetScan::extract_argument(fd
, i
, arg
, !is_kill
));
1084 fd
= get_summary_function(expr
);
1086 return pet_expr_free(res
);
1088 res
= set_summary(res
, fd
);
1093 /* Construct a pet_expr representing a (C style) cast.
1095 __isl_give pet_expr
*PetScan::extract_expr(CStyleCastExpr
*expr
)
1100 arg
= extract_expr(expr
->getSubExpr());
1104 type
= expr
->getTypeAsWritten();
1105 return pet_expr_new_cast(type
.getAsString().c_str(), arg
);
1108 /* Construct a pet_expr representing an integer.
1110 __isl_give pet_expr
*PetScan::extract_expr(IntegerLiteral
*expr
)
1112 return pet_expr_new_int(extract_int(expr
));
1115 /* Construct a pet_expr representing the integer enum constant "ecd".
1117 __isl_give pet_expr
*PetScan::extract_expr(EnumConstantDecl
*ecd
)
1120 const llvm::APSInt
&init
= ecd
->getInitVal();
1121 v
= ::extract_int(ctx
, init
.isSigned(), init
);
1122 return pet_expr_new_int(v
);
1125 /* Try and construct a pet_expr representing "expr".
1127 __isl_give pet_expr
*PetScan::extract_expr(Expr
*expr
)
1129 switch (expr
->getStmtClass()) {
1130 case Stmt::UnaryOperatorClass
:
1131 return extract_expr(cast
<UnaryOperator
>(expr
));
1132 case Stmt::CompoundAssignOperatorClass
:
1133 case Stmt::BinaryOperatorClass
:
1134 return extract_expr(cast
<BinaryOperator
>(expr
));
1135 case Stmt::ImplicitCastExprClass
:
1136 return extract_expr(cast
<ImplicitCastExpr
>(expr
));
1137 case Stmt::ArraySubscriptExprClass
:
1138 case Stmt::DeclRefExprClass
:
1139 case Stmt::MemberExprClass
:
1140 return extract_access_expr(expr
);
1141 case Stmt::IntegerLiteralClass
:
1142 return extract_expr(cast
<IntegerLiteral
>(expr
));
1143 case Stmt::FloatingLiteralClass
:
1144 return extract_expr(cast
<FloatingLiteral
>(expr
));
1145 case Stmt::ParenExprClass
:
1146 return extract_expr(cast
<ParenExpr
>(expr
));
1147 case Stmt::ConditionalOperatorClass
:
1148 return extract_expr(cast
<ConditionalOperator
>(expr
));
1149 case Stmt::CallExprClass
:
1150 return extract_expr(cast
<CallExpr
>(expr
));
1151 case Stmt::CStyleCastExprClass
:
1152 return extract_expr(cast
<CStyleCastExpr
>(expr
));
1159 /* Check if the given initialization statement is an assignment.
1160 * If so, return that assignment. Otherwise return NULL.
1162 BinaryOperator
*PetScan::initialization_assignment(Stmt
*init
)
1164 BinaryOperator
*ass
;
1166 if (init
->getStmtClass() != Stmt::BinaryOperatorClass
)
1169 ass
= cast
<BinaryOperator
>(init
);
1170 if (ass
->getOpcode() != BO_Assign
)
1176 /* Check if the given initialization statement is a declaration
1177 * of a single variable.
1178 * If so, return that declaration. Otherwise return NULL.
1180 Decl
*PetScan::initialization_declaration(Stmt
*init
)
1184 if (init
->getStmtClass() != Stmt::DeclStmtClass
)
1187 decl
= cast
<DeclStmt
>(init
);
1189 if (!decl
->isSingleDecl())
1192 return decl
->getSingleDecl();
1195 /* Given the assignment operator in the initialization of a for loop,
1196 * extract the induction variable, i.e., the (integer)variable being
1199 ValueDecl
*PetScan::extract_induction_variable(BinaryOperator
*init
)
1206 lhs
= init
->getLHS();
1207 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1212 ref
= cast
<DeclRefExpr
>(lhs
);
1213 decl
= ref
->getDecl();
1214 type
= decl
->getType().getTypePtr();
1216 if (!type
->isIntegerType()) {
1224 /* Given the initialization statement of a for loop and the single
1225 * declaration in this initialization statement,
1226 * extract the induction variable, i.e., the (integer) variable being
1229 VarDecl
*PetScan::extract_induction_variable(Stmt
*init
, Decl
*decl
)
1233 vd
= cast
<VarDecl
>(decl
);
1235 const QualType type
= vd
->getType();
1236 if (!type
->isIntegerType()) {
1241 if (!vd
->getInit()) {
1249 /* Check that op is of the form iv++ or iv--.
1250 * Return a pet_expr representing "1" or "-1" accordingly.
1252 __isl_give pet_expr
*PetScan::extract_unary_increment(
1253 clang::UnaryOperator
*op
, clang::ValueDecl
*iv
)
1259 if (!op
->isIncrementDecrementOp()) {
1264 sub
= op
->getSubExpr();
1265 if (sub
->getStmtClass() != Stmt::DeclRefExprClass
) {
1270 ref
= cast
<DeclRefExpr
>(sub
);
1271 if (ref
->getDecl() != iv
) {
1276 if (op
->isIncrementOp())
1277 v
= isl_val_one(ctx
);
1279 v
= isl_val_negone(ctx
);
1281 return pet_expr_new_int(v
);
1284 /* Check if op is of the form
1288 * and return the increment "expr - iv" as a pet_expr.
1290 __isl_give pet_expr
*PetScan::extract_binary_increment(BinaryOperator
*op
,
1291 clang::ValueDecl
*iv
)
1296 pet_expr
*expr
, *expr_iv
;
1298 if (op
->getOpcode() != BO_Assign
) {
1304 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1309 ref
= cast
<DeclRefExpr
>(lhs
);
1310 if (ref
->getDecl() != iv
) {
1315 expr
= extract_expr(op
->getRHS());
1316 expr_iv
= extract_expr(lhs
);
1318 type_size
= pet_clang_get_type_size(iv
->getType(), ast_context
);
1319 return pet_expr_new_binary(type_size
, pet_op_sub
, expr
, expr_iv
);
1322 /* Check that op is of the form iv += cst or iv -= cst
1323 * and return a pet_expr corresponding to cst or -cst accordingly.
1325 __isl_give pet_expr
*PetScan::extract_compound_increment(
1326 CompoundAssignOperator
*op
, clang::ValueDecl
*iv
)
1332 BinaryOperatorKind opcode
;
1334 opcode
= op
->getOpcode();
1335 if (opcode
!= BO_AddAssign
&& opcode
!= BO_SubAssign
) {
1339 if (opcode
== BO_SubAssign
)
1343 if (lhs
->getStmtClass() != Stmt::DeclRefExprClass
) {
1348 ref
= cast
<DeclRefExpr
>(lhs
);
1349 if (ref
->getDecl() != iv
) {
1354 expr
= extract_expr(op
->getRHS());
1357 type_size
= pet_clang_get_type_size(op
->getType(), ast_context
);
1358 expr
= pet_expr_new_unary(type_size
, 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 /* Construct a pet_tree for a while loop.
1392 * If we were only able to extract part of the body, then simply
1395 __isl_give pet_tree
*PetScan::extract(WhileStmt
*stmt
)
1400 tree
= extract(stmt
->getBody());
1403 pe_cond
= extract_expr(stmt
->getCond());
1404 tree
= pet_tree_new_while(pe_cond
, tree
);
1409 /* Construct a pet_tree for a for statement.
1410 * The for loop is required to be of one of the following forms
1412 * for (i = init; condition; ++i)
1413 * for (i = init; condition; --i)
1414 * for (i = init; condition; i += constant)
1415 * for (i = init; condition; i -= constant)
1417 * We extract a pet_tree for the body and then include it in a pet_tree
1418 * of type pet_tree_for.
1420 * As a special case, we also allow a for loop of the form
1424 * in which case we return a pet_tree of type pet_tree_infinite_loop.
1426 * If we were only able to extract part of the body, then simply
1429 __isl_give pet_tree
*PetScan::extract_for(ForStmt
*stmt
)
1431 BinaryOperator
*ass
;
1439 pet_expr
*pe_init
, *pe_inc
, *pe_iv
, *pe_cond
;
1441 independent
= is_current_stmt_marked_independent();
1443 if (!stmt
->getInit() && !stmt
->getCond() && !stmt
->getInc()) {
1444 tree
= extract(stmt
->getBody());
1447 tree
= pet_tree_new_infinite_loop(tree
);
1451 init
= stmt
->getInit();
1456 if ((ass
= initialization_assignment(init
)) != NULL
) {
1457 iv
= extract_induction_variable(ass
);
1460 rhs
= ass
->getRHS();
1461 } else if ((decl
= initialization_declaration(init
)) != NULL
) {
1462 VarDecl
*var
= extract_induction_variable(init
, decl
);
1466 rhs
= var
->getInit();
1468 unsupported(stmt
->getInit());
1472 declared
= !initialization_assignment(stmt
->getInit());
1473 tree
= extract(stmt
->getBody());
1476 pe_iv
= extract_access_expr(iv
);
1477 pe_iv
= mark_write(pe_iv
);
1478 pe_init
= extract_expr(rhs
);
1479 if (!stmt
->getCond())
1480 pe_cond
= pet_expr_new_int(isl_val_one(ctx
));
1482 pe_cond
= extract_expr(stmt
->getCond());
1483 pe_inc
= extract_increment(stmt
, iv
);
1484 tree
= pet_tree_new_for(independent
, declared
, pe_iv
, pe_init
, pe_cond
,
1489 /* Store the names of the variables declared in decl_context
1490 * in the set declared_names. Make sure to only do this once by
1491 * setting declared_names_collected.
1493 void PetScan::collect_declared_names()
1495 DeclContext
*DC
= decl_context
;
1496 DeclContext::decl_iterator it
;
1498 if (declared_names_collected
)
1501 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1505 if (!isa
<NamedDecl
>(D
))
1507 named
= cast
<NamedDecl
>(D
);
1508 declared_names
.insert(named
->getName().str());
1511 declared_names_collected
= true;
1514 /* Add the names in "names" that are not also in this->declared_names
1515 * to this->used_names.
1516 * It is up to the caller to make sure that declared_names has been
1517 * populated, if needed.
1519 void PetScan::add_new_used_names(const std::set
<std::string
> &names
)
1521 std::set
<std::string
>::const_iterator it
;
1523 for (it
= names
.begin(); it
!= names
.end(); ++it
) {
1524 if (declared_names
.find(*it
) != declared_names
.end())
1526 used_names
.insert(*it
);
1530 /* Is the name "name" used in any declaration other than "decl"?
1532 * If the name was found to be in use before, the consider it to be in use.
1533 * Otherwise, check the DeclContext of the function containing the scop
1534 * as well as all ancestors of this DeclContext for declarations
1535 * other than "decl" that declare something called "name".
1537 bool PetScan::name_in_use(const string
&name
, Decl
*decl
)
1540 DeclContext::decl_iterator it
;
1542 if (used_names
.find(name
) != used_names
.end())
1545 for (DC
= decl_context
; DC
; DC
= DC
->getParent()) {
1546 for (it
= DC
->decls_begin(); it
!= DC
->decls_end(); ++it
) {
1552 if (!isa
<NamedDecl
>(D
))
1554 named
= cast
<NamedDecl
>(D
);
1555 if (named
->getName().str() == name
)
1563 /* Generate a new name based on "name" that is not in use.
1564 * Do so by adding a suffix _i, with i an integer.
1566 string
PetScan::generate_new_name(const string
&name
)
1571 std::ostringstream oss
;
1572 oss
<< name
<< "_" << n_rename
++;
1573 new_name
= oss
.str();
1574 } while (name_in_use(new_name
, NULL
));
1579 /* Try and construct a pet_tree corresponding to a compound statement.
1581 * "skip_declarations" is set if we should skip initial declarations
1582 * in the children of the compound statements.
1584 * Collect a new set of declarations for the current compound statement.
1585 * If any of the names in these declarations is also used by another
1586 * declaration reachable from the current function, then rename it
1587 * to a name that is not already in use.
1588 * In particular, keep track of the old and new names in a pet_substituter
1589 * and apply the substitutions to the pet_tree corresponding to the
1590 * compound statement.
1592 __isl_give pet_tree
*PetScan::extract(CompoundStmt
*stmt
,
1593 bool skip_declarations
)
1596 std::vector
<VarDecl
*> saved_declarations
;
1597 std::vector
<VarDecl
*>::iterator it
;
1598 pet_substituter substituter
;
1600 saved_declarations
= declarations
;
1601 declarations
.clear();
1602 tree
= extract(stmt
->children(), true, skip_declarations
, stmt
);
1603 for (it
= declarations
.begin(); it
!= declarations
.end(); ++it
) {
1606 VarDecl
*decl
= *it
;
1607 string name
= decl
->getName().str();
1608 bool in_use
= name_in_use(name
, decl
);
1610 used_names
.insert(name
);
1614 name
= generate_new_name(name
);
1615 id
= pet_id_from_name_and_decl(ctx
, name
.c_str(), decl
);
1616 expr
= pet_id_create_index_expr(id
);
1617 expr
= extract_access_expr(decl
->getType(), expr
);
1618 id
= pet_id_from_decl(ctx
, decl
);
1619 substituter
.add_sub(id
, expr
);
1620 used_names
.insert(name
);
1622 tree
= substituter
.substitute(tree
);
1623 declarations
= saved_declarations
;
1628 /* Return the file offset of the expansion location of "Loc".
1630 static unsigned getExpansionOffset(SourceManager
&SM
, SourceLocation Loc
)
1632 return SM
.getFileOffset(SM
.getExpansionLoc(Loc
));
1635 #ifdef HAVE_FINDLOCATIONAFTERTOKEN
1637 /* Return a SourceLocation for the location after the first semicolon
1638 * after "loc". If Lexer::findLocationAfterToken is available, we simply
1639 * call it and also skip trailing spaces and newline.
1641 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1642 const LangOptions
&LO
)
1644 return Lexer::findLocationAfterToken(loc
, tok::semi
, SM
, LO
, true);
1649 /* Return a SourceLocation for the location after the first semicolon
1650 * after "loc". If Lexer::findLocationAfterToken is not available,
1651 * we look in the underlying character data for the first semicolon.
1653 static SourceLocation
location_after_semi(SourceLocation loc
, SourceManager
&SM
,
1654 const LangOptions
&LO
)
1657 const char *s
= SM
.getCharacterData(loc
);
1659 semi
= strchr(s
, ';');
1661 return SourceLocation();
1662 return loc
.getFileLocWithOffset(semi
+ 1 - s
);
1667 /* If the token at "loc" is the first token on the line, then return
1668 * a location referring to the start of the line and set *indent
1669 * to the indentation of "loc"
1670 * Otherwise, return "loc" and set *indent to "".
1672 * This function is used to extend a scop to the start of the line
1673 * if the first token of the scop is also the first token on the line.
1675 * We look for the first token on the line. If its location is equal to "loc",
1676 * then the latter is the location of the first token on the line.
1678 static SourceLocation
move_to_start_of_line_if_first_token(SourceLocation loc
,
1679 SourceManager
&SM
, const LangOptions
&LO
, char **indent
)
1681 std::pair
<FileID
, unsigned> file_offset_pair
;
1682 llvm::StringRef file
;
1685 SourceLocation token_loc
, line_loc
;
1689 loc
= SM
.getExpansionLoc(loc
);
1690 col
= SM
.getExpansionColumnNumber(loc
);
1691 line_loc
= loc
.getLocWithOffset(1 - col
);
1692 file_offset_pair
= SM
.getDecomposedLoc(line_loc
);
1693 file
= SM
.getBufferData(file_offset_pair
.first
, NULL
);
1694 pos
= file
.data() + file_offset_pair
.second
;
1696 Lexer
lexer(SM
.getLocForStartOfFile(file_offset_pair
.first
), LO
,
1697 file
.begin(), pos
, file
.end());
1698 lexer
.LexFromRawLexer(tok
);
1699 token_loc
= tok
.getLocation();
1701 s
= SM
.getCharacterData(line_loc
);
1702 *indent
= strndup(s
, token_loc
== loc
? col
- 1 : 0);
1704 if (token_loc
== loc
)
1710 /* Construct a pet_loc corresponding to the region covered by "range".
1711 * If "skip_semi" is set, then we assume "range" is followed by
1712 * a semicolon and also include this semicolon.
1714 __isl_give pet_loc
*PetScan::construct_pet_loc(SourceRange range
,
1717 SourceLocation loc
= range
.getBegin();
1718 SourceManager
&SM
= PP
.getSourceManager();
1719 const LangOptions
&LO
= PP
.getLangOpts();
1720 int line
= PP
.getSourceManager().getExpansionLineNumber(loc
);
1721 unsigned start
, end
;
1724 loc
= move_to_start_of_line_if_first_token(loc
, SM
, LO
, &indent
);
1725 start
= getExpansionOffset(SM
, loc
);
1726 loc
= range
.getEnd();
1728 loc
= location_after_semi(loc
, SM
, LO
);
1730 loc
= PP
.getLocForEndOfToken(loc
);
1731 end
= getExpansionOffset(SM
, loc
);
1733 return pet_loc_alloc(ctx
, start
, end
, line
, indent
);
1736 /* Convert a top-level pet_expr to an expression pet_tree.
1738 __isl_give pet_tree
*PetScan::extract(__isl_take pet_expr
*expr
,
1739 SourceRange range
, bool skip_semi
)
1744 tree
= pet_tree_new_expr(expr
);
1745 loc
= construct_pet_loc(range
, skip_semi
);
1746 tree
= pet_tree_set_loc(tree
, loc
);
1751 /* Construct a pet_tree for an if statement.
1753 __isl_give pet_tree
*PetScan::extract(IfStmt
*stmt
)
1756 pet_tree
*tree
, *tree_else
;
1758 pe_cond
= extract_expr(stmt
->getCond());
1759 tree
= extract(stmt
->getThen());
1760 if (stmt
->getElse()) {
1761 tree_else
= extract(stmt
->getElse());
1762 if (options
->autodetect
) {
1763 if (tree
&& !tree_else
) {
1765 pet_expr_free(pe_cond
);
1768 if (!tree
&& tree_else
) {
1770 pet_expr_free(pe_cond
);
1774 tree
= pet_tree_new_if_else(pe_cond
, tree
, tree_else
);
1776 tree
= pet_tree_new_if(pe_cond
, tree
);
1780 /* Try and construct a pet_tree for a label statement.
1782 __isl_give pet_tree
*PetScan::extract(LabelStmt
*stmt
)
1787 label
= isl_id_alloc(ctx
, stmt
->getName(), NULL
);
1789 tree
= extract(stmt
->getSubStmt());
1790 tree
= pet_tree_set_label(tree
, label
);
1794 /* Update the location of "tree" to include the source range of "stmt".
1796 * Actually, we create a new location based on the source range of "stmt" and
1797 * then extend this new location to include the region of the original location.
1798 * This ensures that the line number of the final location refers to "stmt".
1800 __isl_give pet_tree
*PetScan::update_loc(__isl_take pet_tree
*tree
, Stmt
*stmt
)
1802 pet_loc
*loc
, *tree_loc
;
1804 tree_loc
= pet_tree_get_loc(tree
);
1805 loc
= construct_pet_loc(stmt
->getSourceRange(), false);
1806 loc
= pet_loc_update_start_end_from_loc(loc
, tree_loc
);
1807 pet_loc_free(tree_loc
);
1809 tree
= pet_tree_set_loc(tree
, loc
);
1813 /* Is "expr" of a type that can be converted to an access expression?
1815 static bool is_access_expr_type(Expr
*expr
)
1817 switch (expr
->getStmtClass()) {
1818 case Stmt::ArraySubscriptExprClass
:
1819 case Stmt::DeclRefExprClass
:
1820 case Stmt::MemberExprClass
:
1827 /* Tell the pet_inliner "inliner" about the formal arguments
1828 * in "fd" and the corresponding actual arguments in "call".
1829 * Return 0 if this was successful and -1 otherwise.
1831 * Any pointer argument is treated as an array.
1832 * The other arguments are treated as scalars.
1834 * In case of scalars, there is no restriction on the actual argument.
1835 * This actual argument is assigned to a variable with a name
1836 * that is derived from the name of the corresponding formal argument,
1837 * but made not to conflict with any variable names that are
1840 * In case of arrays, the actual argument needs to be an expression
1841 * of a type that can be converted to an access expression or the address
1842 * of such an expression, ignoring implicit and redundant casts.
1844 int PetScan::set_inliner_arguments(pet_inliner
&inliner
, CallExpr
*call
,
1849 n
= fd
->getNumParams();
1850 for (unsigned i
= 0; i
< n
; ++i
) {
1851 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
1852 QualType type
= parm
->getType();
1857 arg
= call
->getArg(i
);
1858 if (pet_clang_array_depth(type
) == 0) {
1859 string name
= parm
->getName().str();
1860 if (name_in_use(name
, NULL
))
1861 name
= generate_new_name(name
);
1862 used_names
.insert(name
);
1863 inliner
.add_scalar_arg(parm
, name
, extract_expr(arg
));
1866 arg
= pet_clang_strip_casts(arg
);
1867 sub
= extract_addr_of_arg(arg
);
1870 arg
= pet_clang_strip_casts(sub
);
1872 if (!is_access_expr_type(arg
)) {
1873 report_unsupported_inline_function_argument(arg
);
1876 expr
= extract_access_expr(arg
);
1879 inliner
.add_array_arg(parm
, expr
, is_addr
);
1885 /* Internal data structure for PetScan::substitute_array_sizes.
1886 * ps is the PetScan on which the method was called.
1887 * substituter is the substituter that is used to substitute variables
1888 * in the size expressions.
1890 struct pet_substitute_array_sizes_data
{
1892 pet_substituter
*substituter
;
1896 static int substitute_array_size(__isl_keep pet_tree
*tree
, void *user
);
1899 /* If "tree" is a declaration, then perform the substitutions
1900 * in data->substituter on its size expression and store the result
1901 * in the size expression cache of data->ps such that the modified expression
1902 * will be used in subsequent calls to get_array_size.
1904 static int substitute_array_size(__isl_keep pet_tree
*tree
, void *user
)
1906 struct pet_substitute_array_sizes_data
*data
;
1908 pet_expr
*var
, *size
;
1910 if (!pet_tree_is_decl(tree
))
1913 data
= (struct pet_substitute_array_sizes_data
*) user
;
1914 var
= pet_tree_decl_get_var(tree
);
1915 id
= pet_expr_access_get_id(var
);
1918 size
= data
->ps
->get_array_size(id
);
1919 size
= data
->substituter
->substitute(size
);
1920 data
->ps
->set_array_size(id
, size
);
1925 /* Perform the substitutions in "substituter" on all the arrays declared
1926 * inside "tree" and store the results in the size expression cache
1927 * such that the modified expressions will be used in subsequent calls
1928 * to get_array_size.
1930 int PetScan::substitute_array_sizes(__isl_keep pet_tree
*tree
,
1931 pet_substituter
*substituter
)
1933 struct pet_substitute_array_sizes_data data
= { this, substituter
};
1935 return pet_tree_foreach_sub_tree(tree
, &substitute_array_size
, &data
);
1938 /* Try and construct a pet_tree from the body of "fd" using the actual
1939 * arguments in "call" in place of the formal arguments.
1940 * "fd" is assumed to point to the declaration with a function body.
1941 * In particular, construct a block that consists of assignments
1942 * of (parts of) the actual arguments to temporary variables
1943 * followed by the inlined function body with the formal arguments
1944 * replaced by (expressions containing) these temporary variables.
1946 * The actual inlining is taken care of by the pet_inliner object.
1947 * This function merely calls set_inliner_arguments to tell
1948 * the pet_inliner about the actual arguments, extracts a pet_tree
1949 * from the body of the called function and then passes this pet_tree
1950 * to the pet_inliner.
1951 * The substitutions performed by the inliner are also applied
1952 * to the size expressions of the arrays declared in the inlined
1953 * function. These size expressions are not stored in the tree
1954 * itself, but rather in the size expression cache.
1956 * During the extraction of the function body, all variables names
1957 * that are declared in the calling function as well all variable
1958 * names that are known to be in use are considered to be in use
1959 * in the called function to ensure that there is no naming conflict.
1960 * Similarly, the additional names that are in use in the called function
1961 * are considered to be in use in the calling function as well.
1963 * The location of the pet_tree is reset to the call site to ensure
1964 * that the extent of the scop does not include the body of the called
1967 __isl_give pet_tree
*PetScan::extract_inlined_call(CallExpr
*call
,
1970 int save_autodetect
;
1973 pet_inliner
inliner(ctx
, n_arg
, ast_context
);
1975 if (set_inliner_arguments(inliner
, call
, fd
) < 0)
1978 save_autodetect
= options
->autodetect
;
1979 options
->autodetect
= 0;
1980 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
1981 isl_union_map_copy(value_bounds
), independent
);
1982 collect_declared_names();
1983 body_scan
.add_new_used_names(declared_names
);
1984 body_scan
.add_new_used_names(used_names
);
1985 tree
= body_scan
.extract(fd
->getBody(), false);
1986 add_new_used_names(body_scan
.used_names
);
1987 options
->autodetect
= save_autodetect
;
1989 tree_loc
= construct_pet_loc(call
->getSourceRange(), true);
1990 tree
= pet_tree_set_loc(tree
, tree_loc
);
1992 substitute_array_sizes(tree
, &inliner
);
1994 return inliner
.inline_tree(tree
);
1997 /* Try and construct a pet_tree corresponding
1998 * to the expression statement "stmt".
2000 * If the outer expression is a function call and if the corresponding
2001 * function body is marked "inline", then return a pet_tree
2002 * corresponding to the inlined function.
2004 __isl_give pet_tree
*PetScan::extract_expr_stmt(Stmt
*stmt
)
2008 if (stmt
->getStmtClass() == Stmt::CallExprClass
) {
2009 CallExpr
*call
= cast
<CallExpr
>(stmt
);
2010 FunctionDecl
*fd
= call
->getDirectCallee();
2011 fd
= pet_clang_find_function_decl_with_body(fd
);
2012 if (fd
&& fd
->isInlineSpecified())
2013 return extract_inlined_call(call
, fd
);
2016 expr
= extract_expr(cast
<Expr
>(stmt
));
2017 return extract(expr
, stmt
->getSourceRange(), true);
2020 /* Try and construct a pet_tree corresponding to "stmt".
2022 * If "stmt" is a compound statement, then "skip_declarations"
2023 * indicates whether we should skip initial declarations in the
2024 * compound statement.
2026 * If the constructed pet_tree is not a (possibly) partial representation
2027 * of "stmt", we update start and end of the pet_scop to those of "stmt".
2028 * In particular, if skip_declarations is set, then we may have skipped
2029 * declarations inside "stmt" and so the pet_scop may not represent
2030 * the entire "stmt".
2031 * Note that this function may be called with "stmt" referring to the entire
2032 * body of the function, including the outer braces. In such cases,
2033 * skip_declarations will be set and the braces will not be taken into
2034 * account in tree->loc.
2036 __isl_give pet_tree
*PetScan::extract(Stmt
*stmt
, bool skip_declarations
)
2040 set_current_stmt(stmt
);
2042 if (isa
<Expr
>(stmt
))
2043 return extract_expr_stmt(cast
<Expr
>(stmt
));
2045 switch (stmt
->getStmtClass()) {
2046 case Stmt::WhileStmtClass
:
2047 tree
= extract(cast
<WhileStmt
>(stmt
));
2049 case Stmt::ForStmtClass
:
2050 tree
= extract_for(cast
<ForStmt
>(stmt
));
2052 case Stmt::IfStmtClass
:
2053 tree
= extract(cast
<IfStmt
>(stmt
));
2055 case Stmt::CompoundStmtClass
:
2056 tree
= extract(cast
<CompoundStmt
>(stmt
), skip_declarations
);
2058 case Stmt::LabelStmtClass
:
2059 tree
= extract(cast
<LabelStmt
>(stmt
));
2061 case Stmt::ContinueStmtClass
:
2062 tree
= pet_tree_new_continue(ctx
);
2064 case Stmt::BreakStmtClass
:
2065 tree
= pet_tree_new_break(ctx
);
2067 case Stmt::DeclStmtClass
:
2068 tree
= extract(cast
<DeclStmt
>(stmt
));
2070 case Stmt::NullStmtClass
:
2071 tree
= pet_tree_new_block(ctx
, 0, 0);
2074 report_unsupported_statement_type(stmt
);
2078 if (partial
|| skip_declarations
)
2081 return update_loc(tree
, stmt
);
2084 /* Given a sequence of statements "stmt_range" of which the first "n_decl"
2085 * are declarations and of which the remaining statements are represented
2086 * by "tree", try and extend "tree" to include the last sequence of
2087 * the initial declarations that can be completely extracted.
2089 * We start collecting the initial declarations and start over
2090 * whenever we come across a declaration that we cannot extract.
2091 * If we have been able to extract any declarations, then we
2092 * copy over the contents of "tree" at the end of the declarations.
2093 * Otherwise, we simply return the original "tree".
2095 __isl_give pet_tree
*PetScan::insert_initial_declarations(
2096 __isl_take pet_tree
*tree
, int n_decl
, StmtRange stmt_range
)
2104 n_stmt
= pet_tree_block_n_child(tree
);
2105 is_block
= pet_tree_block_get_block(tree
);
2106 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2108 for (i
= stmt_range
.first
; n_decl
; ++i
, --n_decl
) {
2112 tree_i
= extract(child
);
2113 if (tree_i
&& !partial
) {
2114 res
= pet_tree_block_add_child(res
, tree_i
);
2117 pet_tree_free(tree_i
);
2119 if (pet_tree_block_n_child(res
) == 0)
2122 res
= pet_tree_new_block(ctx
, is_block
, n_decl
+ n_stmt
);
2125 if (pet_tree_block_n_child(res
) == 0) {
2130 for (j
= 0; j
< n_stmt
; ++j
) {
2133 tree_i
= pet_tree_block_get_child(tree
, j
);
2134 res
= pet_tree_block_add_child(res
, tree_i
);
2136 pet_tree_free(tree
);
2141 /* Try and construct a pet_tree corresponding to (part of)
2142 * a sequence of statements.
2144 * "block" is set if the sequence represents the children of
2145 * a compound statement.
2146 * "skip_declarations" is set if we should skip initial declarations
2147 * in the sequence of statements.
2148 * "parent" is the statement that has stmt_range as (some of) its children.
2150 * If autodetect is set, then we allow the extraction of only a subrange
2151 * of the sequence of statements. However, if there is at least one
2152 * kill and there is some subsequent statement for which we could not
2153 * construct a tree, then turn off the "block" property of the tree
2154 * such that no extra kill will be introduced at the end of the (partial)
2155 * block. If, on the other hand, the final range contains
2156 * no statements, then we discard the entire range.
2157 * If only a subrange of the sequence was extracted, but each statement
2158 * in the sequence was extracted completely, and if there are some
2159 * variable declarations in the sequence before or inside
2160 * the extracted subrange, then check if any of these variables are
2161 * not used after the extracted subrange. If so, add kills to these
2164 * If the entire range was extracted, apart from some initial declarations,
2165 * then we try and extend the range with the latest of those initial
2168 __isl_give pet_tree
*PetScan::extract(StmtRange stmt_range
, bool block
,
2169 bool skip_declarations
, Stmt
*parent
)
2173 bool has_kills
= false;
2174 bool partial_range
= false;
2175 bool outer_partial
= false;
2177 SourceManager
&SM
= PP
.getSourceManager();
2178 pet_killed_locals
kl(SM
);
2179 unsigned range_start
, range_end
;
2181 for (i
= stmt_range
.first
, j
= 0; i
!= stmt_range
.second
; ++i
, ++j
)
2184 tree
= pet_tree_new_block(ctx
, block
, j
);
2187 i
= stmt_range
.first
;
2188 if (skip_declarations
)
2189 for (; i
!= stmt_range
.second
; ++i
) {
2190 if ((*i
)->getStmtClass() != Stmt::DeclStmtClass
)
2192 if (options
->autodetect
)
2193 kl
.add_locals(cast
<DeclStmt
>(*i
));
2197 for (; i
!= stmt_range
.second
; ++i
) {
2201 tree_i
= extract(child
);
2202 if (pet_tree_block_n_child(tree
) != 0 && partial
) {
2203 pet_tree_free(tree_i
);
2206 if (child
->getStmtClass() == Stmt::DeclStmtClass
) {
2207 if (options
->autodetect
)
2208 kl
.add_locals(cast
<DeclStmt
>(child
));
2209 if (tree_i
&& block
)
2212 if (options
->autodetect
) {
2214 range_end
= getExpansionOffset(SM
,
2215 child
->getLocEnd());
2216 if (pet_tree_block_n_child(tree
) == 0)
2217 range_start
= getExpansionOffset(SM
,
2218 child
->getLocStart());
2219 tree
= pet_tree_block_add_child(tree
, tree_i
);
2221 partial_range
= true;
2223 if (pet_tree_block_n_child(tree
) != 0 && !tree_i
)
2224 outer_partial
= partial
= true;
2226 tree
= pet_tree_block_add_child(tree
, tree_i
);
2229 if (partial
|| !tree
)
2238 tree
= pet_tree_block_set_block(tree
, 0);
2239 if (outer_partial
) {
2240 kl
.remove_accessed_after(parent
,
2241 range_start
, range_end
);
2242 tree
= add_kills(tree
, kl
.locals
);
2244 } else if (partial_range
) {
2245 if (pet_tree_block_n_child(tree
) == 0) {
2246 pet_tree_free(tree
);
2250 } else if (skip
> 0)
2251 tree
= insert_initial_declarations(tree
, skip
, stmt_range
);
2257 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2259 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2260 __isl_keep pet_context
*pc
, void *user
);
2263 /* Construct a pet_expr that holds the sizes of the array accessed
2265 * This function is used as a callback to pet_context_add_parameters,
2266 * which is also passed a pointer to the PetScan object.
2268 static __isl_give pet_expr
*get_array_size(__isl_keep pet_expr
*access
,
2271 PetScan
*ps
= (PetScan
*) user
;
2275 id
= pet_expr_access_get_id(access
);
2276 size
= ps
->get_array_size(id
);
2282 /* Construct and return a pet_array corresponding to the variable
2283 * accessed by "access".
2284 * This function is used as a callback to pet_scop_from_pet_tree,
2285 * which is also passed a pointer to the PetScan object.
2287 static struct pet_array
*extract_array(__isl_keep pet_expr
*access
,
2288 __isl_keep pet_context
*pc
, void *user
)
2290 PetScan
*ps
= (PetScan
*) user
;
2294 id
= pet_expr_access_get_id(access
);
2295 array
= ps
->extract_array(id
, NULL
, pc
);
2301 /* Extract a function summary from the body of "fd".
2303 * We extract a scop from the function body in a context with as
2304 * parameters the integer arguments of the function.
2305 * We turn off autodetection (in case it was set) to ensure that
2306 * the entire function body is considered.
2307 * We then collect the accessed array elements and attach them
2308 * to the corresponding array arguments, taking into account
2309 * that the function body may access members of array elements.
2311 * The reason for representing the integer arguments as parameters in
2312 * the context is that if we were to instead start with a context
2313 * with the function arguments as initial dimensions, then we would not
2314 * be able to refer to them from the array extents, without turning
2315 * array extents into maps.
2317 * The result is stored in the summary_cache cache so that we can reuse
2318 * it if this method gets called on the same function again later on.
2320 __isl_give pet_function_summary
*PetScan::get_summary(FunctionDecl
*fd
)
2326 pet_function_summary
*summary
;
2329 int save_autodetect
;
2330 struct pet_scop
*scop
;
2332 isl_union_set
*may_read
, *may_write
, *must_write
;
2333 isl_union_map
*to_inner
;
2335 if (summary_cache
.find(fd
) != summary_cache
.end())
2336 return pet_function_summary_copy(summary_cache
[fd
]);
2338 space
= isl_space_set_alloc(ctx
, 0, 0);
2340 n
= fd
->getNumParams();
2341 summary
= pet_function_summary_alloc(ctx
, n
);
2342 for (unsigned i
= 0; i
< n
; ++i
) {
2343 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2344 QualType type
= parm
->getType();
2347 if (!type
->isIntegerType())
2349 id
= pet_id_from_decl(ctx
, parm
);
2350 space
= isl_space_insert_dims(space
, isl_dim_param
, 0, 1);
2351 space
= isl_space_set_dim_id(space
, isl_dim_param
, 0,
2353 summary
= pet_function_summary_set_int(summary
, i
, id
);
2356 save_autodetect
= options
->autodetect
;
2357 options
->autodetect
= 0;
2358 PetScan
body_scan(PP
, ast_context
, fd
, loc
, options
,
2359 isl_union_map_copy(value_bounds
), independent
);
2361 tree
= body_scan
.extract(fd
->getBody(), false);
2363 domain
= isl_set_universe(space
);
2364 pc
= pet_context_alloc(domain
);
2365 pc
= pet_context_add_parameters(pc
, tree
,
2366 &::get_array_size
, &body_scan
);
2367 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2368 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2369 &::extract_array
, &body_scan
, pc
);
2370 scop
= scan_arrays(scop
, pc
);
2371 may_read
= isl_union_map_range(pet_scop_get_may_reads(scop
));
2372 may_write
= isl_union_map_range(pet_scop_get_may_writes(scop
));
2373 must_write
= isl_union_map_range(pet_scop_get_must_writes(scop
));
2374 to_inner
= pet_scop_compute_outer_to_inner(scop
);
2375 pet_scop_free(scop
);
2377 for (unsigned i
= 0; i
< n
; ++i
) {
2378 ParmVarDecl
*parm
= fd
->getParamDecl(i
);
2379 QualType type
= parm
->getType();
2380 struct pet_array
*array
;
2382 isl_union_set
*data_set
;
2383 isl_union_set
*may_read_i
, *may_write_i
, *must_write_i
;
2385 if (pet_clang_array_depth(type
) == 0)
2388 array
= body_scan
.extract_array(parm
, NULL
, pc
);
2389 space
= array
? isl_set_get_space(array
->extent
) : NULL
;
2390 pet_array_free(array
);
2391 data_set
= isl_union_set_from_set(isl_set_universe(space
));
2392 data_set
= isl_union_set_apply(data_set
,
2393 isl_union_map_copy(to_inner
));
2394 may_read_i
= isl_union_set_intersect(
2395 isl_union_set_copy(may_read
),
2396 isl_union_set_copy(data_set
));
2397 may_write_i
= isl_union_set_intersect(
2398 isl_union_set_copy(may_write
),
2399 isl_union_set_copy(data_set
));
2400 must_write_i
= isl_union_set_intersect(
2401 isl_union_set_copy(must_write
), data_set
);
2402 summary
= pet_function_summary_set_array(summary
, i
,
2403 may_read_i
, may_write_i
, must_write_i
);
2406 isl_union_set_free(may_read
);
2407 isl_union_set_free(may_write
);
2408 isl_union_set_free(must_write
);
2409 isl_union_map_free(to_inner
);
2411 options
->autodetect
= save_autodetect
;
2412 pet_context_free(pc
);
2414 summary_cache
[fd
] = pet_function_summary_copy(summary
);
2419 /* If "fd" has a function body, then extract a function summary from
2420 * this body and attach it to the call expression "expr".
2422 * Even if a function body is available, "fd" itself may point
2423 * to a declaration without function body. We therefore first
2424 * replace it by the declaration that comes with a body (if any).
2426 __isl_give pet_expr
*PetScan::set_summary(__isl_take pet_expr
*expr
,
2429 pet_function_summary
*summary
;
2433 fd
= pet_clang_find_function_decl_with_body(fd
);
2437 summary
= get_summary(fd
);
2439 expr
= pet_expr_call_set_summary(expr
, summary
);
2444 /* Extract a pet_scop from "tree".
2446 * We simply call pet_scop_from_pet_tree with the appropriate arguments and
2447 * then add pet_arrays for all accessed arrays.
2448 * We populate the pet_context with assignments for all parameters used
2449 * inside "tree" or any of the size expressions for the arrays accessed
2450 * by "tree" so that they can be used in affine expressions.
2452 struct pet_scop
*PetScan::extract_scop(__isl_take pet_tree
*tree
)
2459 int_size
= size_in_bytes(ast_context
, ast_context
.IntTy
);
2461 domain
= isl_set_universe(isl_space_set_alloc(ctx
, 0, 0));
2462 pc
= pet_context_alloc(domain
);
2463 pc
= pet_context_add_parameters(pc
, tree
, &::get_array_size
, this);
2464 scop
= pet_scop_from_pet_tree(tree
, int_size
,
2465 &::extract_array
, this, pc
);
2466 scop
= scan_arrays(scop
, pc
);
2467 pet_context_free(pc
);
2472 /* Add a call to __pencil_kill to the end of "tree" that kills
2473 * all the variables in "locals" and return the result.
2475 * No location is added to the kill because the most natural
2476 * location would lie outside the scop. Attaching such a location
2477 * to this tree would extend the scope of the final result
2478 * to include the location.
2480 __isl_give pet_tree
*PetScan::add_kills(__isl_take pet_tree
*tree
,
2481 set
<ValueDecl
*> locals
)
2485 pet_tree
*kill
, *block
;
2486 set
<ValueDecl
*>::iterator it
;
2488 if (locals
.size() == 0)
2490 expr
= pet_expr_new_call(ctx
, "__pencil_kill", locals
.size());
2492 for (it
= locals
.begin(); it
!= locals
.end(); ++it
) {
2494 arg
= extract_access_expr(*it
);
2495 expr
= pet_expr_set_arg(expr
, i
++, arg
);
2497 kill
= pet_tree_new_expr(expr
);
2498 block
= pet_tree_new_block(ctx
, 0, 2);
2499 block
= pet_tree_block_add_child(block
, tree
);
2500 block
= pet_tree_block_add_child(block
, kill
);
2505 /* Check if the scop marked by the user is exactly this Stmt
2506 * or part of this Stmt.
2507 * If so, return a pet_scop corresponding to the marked region.
2508 * Otherwise, return NULL.
2510 * If the scop is not further nested inside a child of "stmt",
2511 * then check if there are any variable declarations before the scop
2512 * inside "stmt". If so, and if these variables are not used
2513 * after the scop, then add kills to the variables.
2515 * If the scop starts in the middle of one of the children, without
2516 * also ending in that child, then report an error.
2518 struct pet_scop
*PetScan::scan(Stmt
*stmt
)
2520 SourceManager
&SM
= PP
.getSourceManager();
2521 unsigned start_off
, end_off
;
2524 start_off
= getExpansionOffset(SM
, stmt
->getLocStart());
2525 end_off
= getExpansionOffset(SM
, stmt
->getLocEnd());
2527 if (start_off
> loc
.end
)
2529 if (end_off
< loc
.start
)
2532 if (start_off
>= loc
.start
&& end_off
<= loc
.end
)
2533 return extract_scop(extract(stmt
));
2535 pet_killed_locals
kl(SM
);
2537 for (start
= stmt
->child_begin(); start
!= stmt
->child_end(); ++start
) {
2538 Stmt
*child
= *start
;
2541 start_off
= getExpansionOffset(SM
, child
->getLocStart());
2542 end_off
= getExpansionOffset(SM
, child
->getLocEnd());
2543 if (start_off
< loc
.start
&& end_off
>= loc
.end
)
2545 if (start_off
>= loc
.start
)
2547 if (loc
.start
< end_off
) {
2548 report_unbalanced_pragmas(loc
.scop
, loc
.endscop
);
2551 if (isa
<DeclStmt
>(child
))
2552 kl
.add_locals(cast
<DeclStmt
>(child
));
2556 for (end
= start
; end
!= stmt
->child_end(); ++end
) {
2558 start_off
= SM
.getFileOffset(child
->getLocStart());
2559 if (start_off
>= loc
.end
)
2563 kl
.remove_accessed_after(stmt
, loc
.start
, loc
.end
);
2565 tree
= extract(StmtRange(start
, end
), false, false, stmt
);
2566 tree
= add_kills(tree
, kl
.locals
);
2567 return extract_scop(tree
);
2570 /* Set the size of index "pos" of "array" to "size".
2571 * In particular, add a constraint of the form
2575 * to array->extent and a constraint of the form
2579 * to array->context.
2581 * The domain of "size" is assumed to be zero-dimensional.
2583 static struct pet_array
*update_size(struct pet_array
*array
, int pos
,
2584 __isl_take isl_pw_aff
*size
)
2597 valid
= isl_set_params(isl_pw_aff_nonneg_set(isl_pw_aff_copy(size
)));
2598 array
->context
= isl_set_intersect(array
->context
, valid
);
2600 dim
= isl_set_get_space(array
->extent
);
2601 aff
= isl_aff_zero_on_domain(isl_local_space_from_space(dim
));
2602 aff
= isl_aff_add_coefficient_si(aff
, isl_dim_in
, pos
, 1);
2603 univ
= isl_set_universe(isl_aff_get_domain_space(aff
));
2604 index
= isl_pw_aff_alloc(univ
, aff
);
2606 size
= isl_pw_aff_add_dims(size
, isl_dim_in
,
2607 isl_set_dim(array
->extent
, isl_dim_set
));
2608 id
= isl_set_get_tuple_id(array
->extent
);
2609 size
= isl_pw_aff_set_tuple_id(size
, isl_dim_in
, id
);
2610 bound
= isl_pw_aff_lt_set(index
, size
);
2612 array
->extent
= isl_set_intersect(array
->extent
, bound
);
2614 if (!array
->context
|| !array
->extent
)
2615 return pet_array_free(array
);
2619 isl_pw_aff_free(size
);
2623 #ifdef HAVE_DECAYEDTYPE
2625 /* If "qt" is a decayed type, then set *decayed to true and
2626 * return the original type.
2628 static QualType
undecay(QualType qt
, bool *decayed
)
2630 const Type
*type
= qt
.getTypePtr();
2632 *decayed
= isa
<DecayedType
>(type
);
2634 qt
= cast
<DecayedType
>(type
)->getOriginalType();
2640 /* If "qt" is a decayed type, then set *decayed to true and
2641 * return the original type.
2642 * Since this version of clang does not define a DecayedType,
2643 * we cannot obtain the original type even if it had been decayed and
2644 * we set *decayed to false.
2646 static QualType
undecay(QualType qt
, bool *decayed
)
2654 /* Figure out the size of the array at position "pos" and all
2655 * subsequent positions from "qt" and update the corresponding
2656 * argument of "expr" accordingly.
2658 * The initial type (when pos is zero) may be a pointer type decayed
2659 * from an array type, if this initial type is the type of a function
2660 * argument. This only happens if the original array type has
2661 * a constant size in the outer dimension as otherwise we get
2662 * a VariableArrayType. Try and obtain this original type (if available) and
2663 * take the outer array size into account if it was marked static.
2665 __isl_give pet_expr
*PetScan::set_upper_bounds(__isl_take pet_expr
*expr
,
2666 QualType qt
, int pos
)
2668 const ArrayType
*atype
;
2670 bool decayed
= false;
2676 qt
= undecay(qt
, &decayed
);
2678 if (qt
->isPointerType()) {
2679 qt
= qt
->getPointeeType();
2680 return set_upper_bounds(expr
, qt
, pos
+ 1);
2682 if (!qt
->isArrayType())
2685 qt
= qt
->getCanonicalTypeInternal();
2686 atype
= cast
<ArrayType
>(qt
.getTypePtr());
2688 if (decayed
&& atype
->getSizeModifier() != ArrayType::Static
) {
2689 qt
= atype
->getElementType();
2690 return set_upper_bounds(expr
, qt
, pos
+ 1);
2693 if (qt
->isConstantArrayType()) {
2694 const ConstantArrayType
*ca
= cast
<ConstantArrayType
>(atype
);
2695 size
= extract_expr(ca
->getSize());
2696 expr
= pet_expr_set_arg(expr
, pos
, size
);
2697 } else if (qt
->isVariableArrayType()) {
2698 const VariableArrayType
*vla
= cast
<VariableArrayType
>(atype
);
2699 size
= extract_expr(vla
->getSizeExpr());
2700 expr
= pet_expr_set_arg(expr
, pos
, size
);
2703 qt
= atype
->getElementType();
2705 return set_upper_bounds(expr
, qt
, pos
+ 1);
2708 /* Construct a pet_expr that holds the sizes of the array represented by "id".
2709 * The returned expression is a call expression with as arguments
2710 * the sizes in each dimension. If we are unable to derive the size
2711 * in a given dimension, then the corresponding argument is set to infinity.
2712 * In fact, we initialize all arguments to infinity and then update
2713 * them if we are able to figure out the size.
2715 * The result is stored in the id_size cache so that it can be reused
2716 * if this method is called on the same array identifier later.
2717 * The result is also stored in the type_size cache in case
2718 * it gets called on a different array identifier with the same type.
2720 __isl_give pet_expr
*PetScan::get_array_size(__isl_keep isl_id
*id
)
2722 QualType qt
= pet_id_get_array_type(id
);
2724 pet_expr
*expr
, *inf
;
2725 const Type
*type
= qt
.getTypePtr();
2726 isl_maybe_pet_expr m
;
2728 m
= isl_id_to_pet_expr_try_get(id_size
, id
);
2729 if (m
.valid
< 0 || m
.valid
)
2731 if (type_size
.find(type
) != type_size
.end())
2732 return pet_expr_copy(type_size
[type
]);
2734 depth
= pet_clang_array_depth(qt
);
2735 inf
= pet_expr_new_int(isl_val_infty(ctx
));
2736 expr
= pet_expr_new_call(ctx
, "bounds", depth
);
2737 for (int i
= 0; i
< depth
; ++i
)
2738 expr
= pet_expr_set_arg(expr
, i
, pet_expr_copy(inf
));
2741 expr
= set_upper_bounds(expr
, qt
, 0);
2742 type_size
[type
] = pet_expr_copy(expr
);
2743 id_size
= isl_id_to_pet_expr_set(id_size
, isl_id_copy(id
),
2744 pet_expr_copy(expr
));
2749 /* Set the array size of the array identified by "id" to "size",
2750 * replacing any previously stored value.
2752 void PetScan::set_array_size(__isl_take isl_id
*id
, __isl_take pet_expr
*size
)
2754 id_size
= isl_id_to_pet_expr_set(id_size
, id
, size
);
2757 /* Does "expr" represent the "integer" infinity?
2759 static int is_infty(__isl_keep pet_expr
*expr
)
2764 if (pet_expr_get_type(expr
) != pet_expr_int
)
2766 v
= pet_expr_int_get_val(expr
);
2767 res
= isl_val_is_infty(v
);
2773 /* Figure out the dimensions of an array "array" and
2774 * update "array" accordingly.
2776 * We first construct a pet_expr that holds the sizes of the array
2777 * in each dimension. The resulting expression may containing
2778 * infinity values for dimension where we are unable to derive
2779 * a size expression.
2781 * The arguments of the size expression that have a value different from
2782 * infinity are then converted to an affine expression
2783 * within the context "pc" and incorporated into the size of "array".
2784 * If we are unable to convert a size expression to an affine expression or
2785 * if the size is not a (symbolic) constant,
2786 * then we leave the corresponding size of "array" untouched.
2788 struct pet_array
*PetScan::set_upper_bounds(struct pet_array
*array
,
2789 __isl_keep pet_context
*pc
)
2798 id
= isl_set_get_tuple_id(array
->extent
);
2799 expr
= get_array_size(id
);
2802 n
= pet_expr_get_n_arg(expr
);
2803 for (int i
= 0; i
< n
; ++i
) {
2807 arg
= pet_expr_get_arg(expr
, i
);
2808 if (!is_infty(arg
)) {
2811 size
= pet_expr_extract_affine(arg
, pc
);
2812 dim
= isl_pw_aff_dim(size
, isl_dim_in
);
2814 array
= pet_array_free(array
);
2815 else if (isl_pw_aff_involves_nan(size
) ||
2816 isl_pw_aff_involves_dims(size
, isl_dim_in
, 0, dim
))
2817 isl_pw_aff_free(size
);
2819 size
= isl_pw_aff_drop_dims(size
,
2820 isl_dim_in
, 0, dim
);
2821 array
= update_size(array
, i
, size
);
2826 pet_expr_free(expr
);
2831 /* Does "decl" have a definition that we can keep track of in a pet_type?
2833 static bool has_printable_definition(RecordDecl
*decl
)
2835 if (!decl
->getDeclName())
2837 return decl
->getLexicalDeclContext() == decl
->getDeclContext();
2840 /* Add all TypedefType objects that appear when dereferencing "type"
2843 static void insert_intermediate_typedefs(PetTypes
*types
, QualType type
)
2845 type
= pet_clang_base_or_typedef_type(type
);
2846 while (isa
<TypedefType
>(type
)) {
2847 const TypedefType
*tt
;
2849 tt
= cast
<TypedefType
>(type
);
2850 types
->insert(tt
->getDecl());
2851 type
= tt
->desugar();
2852 type
= pet_clang_base_or_typedef_type(type
);
2856 /* Construct and return a pet_array corresponding to the variable
2857 * represented by "id".
2858 * In particular, initialize array->extent to
2860 * { name[i_1,...,i_d] : i_1,...,i_d >= 0 }
2862 * and then call set_upper_bounds to set the upper bounds on the indices
2863 * based on the type of the variable. The upper bounds are converted
2864 * to affine expressions within the context "pc".
2866 * If the base type is that of a record with a top-level definition or
2867 * of a typedef and if "types" is not null, then the RecordDecl or
2868 * TypedefType corresponding to the type, as well as any intermediate
2869 * TypedefType, is added to "types".
2871 * If the base type is that of a record with no top-level definition,
2872 * then we replace it by "<subfield>".
2874 * If the variable is a scalar, i.e., a zero-dimensional array,
2875 * then the "const" qualifier, if any, is removed from the base type.
2876 * This makes it easier for users of pet to turn initializations
2879 struct pet_array
*PetScan::extract_array(__isl_keep isl_id
*id
,
2880 PetTypes
*types
, __isl_keep pet_context
*pc
)
2882 struct pet_array
*array
;
2883 QualType qt
= pet_id_get_array_type(id
);
2884 int depth
= pet_clang_array_depth(qt
);
2885 QualType base
= pet_clang_base_type(qt
);
2889 array
= isl_calloc_type(ctx
, struct pet_array
);
2893 space
= isl_space_set_alloc(ctx
, 0, depth
);
2894 space
= isl_space_set_tuple_id(space
, isl_dim_set
, isl_id_copy(id
));
2896 array
->extent
= isl_set_nat_universe(space
);
2898 space
= isl_space_params_alloc(ctx
, 0);
2899 array
->context
= isl_set_universe(space
);
2901 array
= set_upper_bounds(array
, pc
);
2906 base
.removeLocalConst();
2907 name
= base
.getAsString();
2910 insert_intermediate_typedefs(types
, qt
);
2911 if (isa
<TypedefType
>(base
)) {
2912 types
->insert(cast
<TypedefType
>(base
)->getDecl());
2913 } else if (base
->isRecordType()) {
2914 RecordDecl
*decl
= pet_clang_record_decl(base
);
2915 TypedefNameDecl
*typedecl
;
2916 typedecl
= decl
->getTypedefNameForAnonDecl();
2918 types
->insert(typedecl
);
2919 else if (has_printable_definition(decl
))
2920 types
->insert(decl
);
2922 name
= "<subfield>";
2926 array
->element_type
= strdup(name
.c_str());
2927 array
->element_is_record
= base
->isRecordType();
2928 array
->element_size
= size_in_bytes(ast_context
, base
);
2933 /* Construct and return a pet_array corresponding to the variable "decl".
2935 struct pet_array
*PetScan::extract_array(ValueDecl
*decl
,
2936 PetTypes
*types
, __isl_keep pet_context
*pc
)
2941 id
= pet_id_from_decl(ctx
, decl
);
2942 array
= extract_array(id
, types
, pc
);
2948 /* Construct and return a pet_array corresponding to the sequence
2949 * of declarations represented by "decls".
2950 * The upper bounds of the array are converted to affine expressions
2951 * within the context "pc".
2952 * If the sequence contains a single declaration, then it corresponds
2953 * to a simple array access. Otherwise, it corresponds to a member access,
2954 * with the declaration for the substructure following that of the containing
2955 * structure in the sequence of declarations.
2956 * We start with the outermost substructure and then combine it with
2957 * information from the inner structures.
2959 * Additionally, keep track of all required types in "types".
2961 struct pet_array
*PetScan::extract_array(__isl_keep isl_id_list
*decls
,
2962 PetTypes
*types
, __isl_keep pet_context
*pc
)
2966 struct pet_array
*array
;
2968 id
= isl_id_list_get_id(decls
, 0);
2969 array
= extract_array(id
, types
, pc
);
2972 n
= isl_id_list_n_id(decls
);
2973 for (i
= 1; i
< n
; ++i
) {
2974 struct pet_array
*parent
;
2975 const char *base_name
, *field_name
;
2979 id
= isl_id_list_get_id(decls
, i
);
2980 array
= extract_array(id
, types
, pc
);
2983 return pet_array_free(parent
);
2985 base_name
= isl_set_get_tuple_name(parent
->extent
);
2986 field_name
= isl_set_get_tuple_name(array
->extent
);
2987 product_name
= pet_array_member_access_name(ctx
,
2988 base_name
, field_name
);
2990 array
->extent
= isl_set_product(isl_set_copy(parent
->extent
),
2993 array
->extent
= isl_set_set_tuple_name(array
->extent
,
2995 array
->context
= isl_set_intersect(array
->context
,
2996 isl_set_copy(parent
->context
));
2998 pet_array_free(parent
);
3001 if (!array
->extent
|| !array
->context
|| !product_name
)
3002 return pet_array_free(array
);
3008 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3009 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3010 std::set
<TypeDecl
*> &types_done
);
3011 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3012 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3013 std::set
<TypeDecl
*> &types_done
);
3015 /* For each of the fields of "decl" that is itself a record type
3016 * or a typedef, or an array of such type, add a corresponding pet_type
3019 static struct pet_scop
*add_field_types(isl_ctx
*ctx
, struct pet_scop
*scop
,
3020 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3021 std::set
<TypeDecl
*> &types_done
)
3023 RecordDecl::field_iterator it
;
3025 for (it
= decl
->field_begin(); it
!= decl
->field_end(); ++it
) {
3026 QualType type
= it
->getType();
3028 type
= pet_clang_base_or_typedef_type(type
);
3029 if (isa
<TypedefType
>(type
)) {
3030 TypedefNameDecl
*typedefdecl
;
3032 typedefdecl
= cast
<TypedefType
>(type
)->getDecl();
3033 scop
= add_type(ctx
, scop
, typedefdecl
,
3034 PP
, types
, types_done
);
3035 } else if (type
->isRecordType()) {
3038 record
= pet_clang_record_decl(type
);
3039 scop
= add_type(ctx
, scop
, record
,
3040 PP
, types
, types_done
);
3047 /* Add a pet_type corresponding to "decl" to "scop", provided
3048 * it is a member of types.records and it has not been added before
3049 * (i.e., it is not a member of "types_done").
3051 * Since we want the user to be able to print the types
3052 * in the order in which they appear in the scop, we need to
3053 * make sure that types of fields in a structure appear before
3054 * that structure. We therefore call ourselves recursively
3055 * through add_field_types on the types of all record subfields.
3057 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3058 RecordDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3059 std::set
<TypeDecl
*> &types_done
)
3062 llvm::raw_string_ostream
S(s
);
3064 if (types
.records
.find(decl
) == types
.records
.end())
3066 if (types_done
.find(decl
) != types_done
.end())
3069 add_field_types(ctx
, scop
, decl
, PP
, types
, types_done
);
3071 if (strlen(decl
->getName().str().c_str()) == 0)
3074 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3077 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3078 decl
->getName().str().c_str(), s
.c_str());
3079 if (!scop
->types
[scop
->n_type
])
3080 return pet_scop_free(scop
);
3082 types_done
.insert(decl
);
3089 /* Add a pet_type corresponding to "decl" to "scop", provided
3090 * it is a member of types.typedefs and it has not been added before
3091 * (i.e., it is not a member of "types_done").
3093 * If the underlying type is a structure, then we print the typedef
3094 * ourselves since clang does not print the definition of the structure
3095 * in the typedef. We also make sure in this case that the types of
3096 * the fields in the structure are added first.
3097 * Since the definition of the structure also gets printed this way,
3098 * add it to types_done such that it will not be printed again,
3099 * not even without the typedef.
3101 static struct pet_scop
*add_type(isl_ctx
*ctx
, struct pet_scop
*scop
,
3102 TypedefNameDecl
*decl
, Preprocessor
&PP
, PetTypes
&types
,
3103 std::set
<TypeDecl
*> &types_done
)
3106 llvm::raw_string_ostream
S(s
);
3107 QualType qt
= decl
->getUnderlyingType();
3109 if (types
.typedefs
.find(decl
) == types
.typedefs
.end())
3111 if (types_done
.find(decl
) != types_done
.end())
3114 if (qt
->isRecordType()) {
3115 RecordDecl
*rec
= pet_clang_record_decl(qt
);
3117 add_field_types(ctx
, scop
, rec
, PP
, types
, types_done
);
3119 rec
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3121 S
<< decl
->getName();
3122 types_done
.insert(rec
);
3124 decl
->print(S
, PrintingPolicy(PP
.getLangOpts()));
3128 scop
->types
[scop
->n_type
] = pet_type_alloc(ctx
,
3129 decl
->getName().str().c_str(), s
.c_str());
3130 if (!scop
->types
[scop
->n_type
])
3131 return pet_scop_free(scop
);
3133 types_done
.insert(decl
);
3140 /* Construct a list of pet_arrays, one for each array (or scalar)
3141 * accessed inside "scop", add this list to "scop" and return the result.
3142 * The upper bounds of the arrays are converted to affine expressions
3143 * within the context "pc".
3145 * The context of "scop" is updated with the intersection of
3146 * the contexts of all arrays, i.e., constraints on the parameters
3147 * that ensure that the arrays have a valid (non-negative) size.
3149 * If any of the extracted arrays refers to a member access or
3150 * has a typedef'd type as base type,
3151 * then also add the required types to "scop".
3152 * The typedef types are printed first because their definitions
3153 * may include the definition of a struct and these struct definitions
3154 * should not be printed separately. While the typedef definition
3155 * is being printed, the struct is marked as having been printed as well,
3156 * such that the later printing of the struct by itself can be prevented.
3158 struct pet_scop
*PetScan::scan_arrays(struct pet_scop
*scop
,
3159 __isl_keep pet_context
*pc
)
3162 array_desc_set arrays
;
3163 array_desc_set::iterator it
;
3165 std::set
<TypeDecl
*> types_done
;
3166 std::set
<clang::RecordDecl
*, less_name
>::iterator records_it
;
3167 std::set
<clang::TypedefNameDecl
*, less_name
>::iterator typedefs_it
;
3169 struct pet_array
**scop_arrays
;
3174 pet_scop_collect_arrays(scop
, arrays
);
3175 if (arrays
.size() == 0)
3178 n_array
= scop
->n_array
;
3180 scop_arrays
= isl_realloc_array(ctx
, scop
->arrays
, struct pet_array
*,
3181 n_array
+ arrays
.size());
3184 scop
->arrays
= scop_arrays
;
3186 for (it
= arrays
.begin(), i
= 0; it
!= arrays
.end(); ++it
, ++i
) {
3187 struct pet_array
*array
;
3188 array
= extract_array(*it
, &types
, pc
);
3189 scop
->arrays
[n_array
+ i
] = array
;
3190 if (!scop
->arrays
[n_array
+ i
])
3193 scop
->context
= isl_set_intersect(scop
->context
,
3194 isl_set_copy(array
->context
));
3199 n
= types
.records
.size() + types
.typedefs
.size();
3203 scop
->types
= isl_alloc_array(ctx
, struct pet_type
*, n
);
3207 for (typedefs_it
= types
.typedefs
.begin();
3208 typedefs_it
!= types
.typedefs
.end(); ++typedefs_it
)
3209 scop
= add_type(ctx
, scop
, *typedefs_it
, PP
, types
, types_done
);
3211 for (records_it
= types
.records
.begin();
3212 records_it
!= types
.records
.end(); ++records_it
)
3213 scop
= add_type(ctx
, scop
, *records_it
, PP
, types
, types_done
);
3217 pet_scop_free(scop
);
3221 /* Bound all parameters in scop->context to the possible values
3222 * of the corresponding C variable.
3224 static struct pet_scop
*add_parameter_bounds(struct pet_scop
*scop
)
3231 n
= isl_set_dim(scop
->context
, isl_dim_param
);
3232 for (int i
= 0; i
< n
; ++i
) {
3236 id
= isl_set_get_dim_id(scop
->context
, isl_dim_param
, i
);
3237 if (pet_nested_in_id(id
)) {
3239 isl_die(isl_set_get_ctx(scop
->context
),
3241 "unresolved nested parameter", goto error
);
3243 decl
= pet_id_get_decl(id
);
3246 scop
->context
= set_parameter_bounds(scop
->context
, i
, decl
);
3254 pet_scop_free(scop
);
3258 /* Construct a pet_scop from the given function.
3260 * If the scop was delimited by scop and endscop pragmas, then we override
3261 * the file offsets by those derived from the pragmas.
3263 struct pet_scop
*PetScan::scan(FunctionDecl
*fd
)
3268 stmt
= fd
->getBody();
3270 if (options
->autodetect
) {
3271 set_current_stmt(stmt
);
3272 scop
= extract_scop(extract(stmt
, true));
3274 current_line
= loc
.start_line
;
3276 scop
= pet_scop_update_start_end(scop
, loc
.start
, loc
.end
);
3278 scop
= add_parameter_bounds(scop
);
3279 scop
= pet_scop_gist(scop
, value_bounds
);
3284 /* Update this->last_line and this->current_line based on the fact
3285 * that we are about to consider "stmt".
3287 void PetScan::set_current_stmt(Stmt
*stmt
)
3289 SourceLocation loc
= stmt
->getLocStart();
3290 SourceManager
&SM
= PP
.getSourceManager();
3292 last_line
= current_line
;
3293 current_line
= SM
.getExpansionLineNumber(loc
);
3296 /* Is the current statement marked by an independent pragma?
3297 * That is, is there an independent pragma on a line between
3298 * the line of the current statement and the line of the previous statement.
3299 * The search is not implemented very efficiently. We currently
3300 * assume that there are only a few independent pragmas, if any.
3302 bool PetScan::is_current_stmt_marked_independent()
3304 for (unsigned i
= 0; i
< independent
.size(); ++i
) {
3305 unsigned line
= independent
[i
].line
;
3307 if (last_line
< line
&& line
< current_line
)